From patchwork Sun Dec 10 18:01:31 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Ognjen Galic X-Patchwork-Id: 10103935 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id C7E3A602A7 for ; Sun, 10 Dec 2017 18:01:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id C0A972926F for ; Sun, 10 Dec 2017 18:01:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id B54FE2928E; Sun, 10 Dec 2017 18:01:40 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.8 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED,FREEMAIL_FROM,RCVD_IN_DNSWL_HI,T_DKIM_INVALID autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 06C6229277 for ; Sun, 10 Dec 2017 18:01:39 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752329AbdLJSBh (ORCPT ); Sun, 10 Dec 2017 13:01:37 -0500 Received: from mail-wr0-f194.google.com ([209.85.128.194]:38814 "EHLO mail-wr0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752253AbdLJSBg (ORCPT ); Sun, 10 Dec 2017 13:01:36 -0500 Received: by mail-wr0-f194.google.com with SMTP id o2so15334139wro.5; Sun, 10 Dec 2017 10:01:35 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:mime-version:content-disposition :content-transfer-encoding:user-agent; bh=OT24G9K87vgAEbSKIHSbDjWCYiUc/PgUwZPyMT+EuqU=; b=TX1M72zJbO0KRE/id69om3QmX8+nl36YVo/C1zrZExn9GhBZk1PO4CMQw4YExKI+nQ 7zhJ4wIH6Zv4QSv7PM3j0846e2/zo8ffkTsmPCyr7sz1K+Apq2+ZGIev8zddyDur9rTF JaiklVEQ0/IFj159m2yYIdwnf/S1r9/u0PVPvRPBqo1u6lyX7vjHiP39ErM5x0o0vzyn Ydz82ZupMr+qK4e/WNKDkdloa3/1X9Ane+97B1ht4suMtUpdz0IpokIKG2MTdM3tIZPp B8C+Py9dBdCqbQTwL0rJgG4W+sL0aotY3ScHRuqG+Tf8BhJpapLnUoFbyh8L0UarfVHP 4m0g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:mime-version :content-disposition:content-transfer-encoding:user-agent; bh=OT24G9K87vgAEbSKIHSbDjWCYiUc/PgUwZPyMT+EuqU=; b=PHRuv0Qmuhi/sXsflFtmLlupk9a3mmhDDVGSxTnVmmEsEojiVfZ8nhqZWF0SIOl3Dk +Qiv7fPoZhbJ1sEzwEZ21dU1MFgaNlU6437q0Q/b6UUAQc+Um3CQXhmTUq5wWTZABto4 4Mhs24bOJf6LbOrJJJaddwk734PaCvJ8n4l6EfsXh7lMan9EvRjO0MKk9St8PoB25epY d2bbXMxZyikXoBnGpTtxMJAb4pTXAfnHslgQjEyLfJ7s0NGF/cqFDYDWESj5bEbe+prf CRpyWuKqLdESNirSnayoQ9o8jTPIQ4Rb8xqe/Vvatz96gdjaxReeSpw9FE3Vqu2sSGOV Svng== X-Gm-Message-State: AJaThX47l5KmcWGez9qiXqoGp/k4hGXRJs1Sa0S78Ea5Y5a2i4wE9lNs VpWQUu2WAWZ2QkjfkJ2g3O4= X-Google-Smtp-Source: AGs4zMZ4nwTauuZFL87TkEi4lrEoypqwqsO2q7IjQD5Q063rpdhnyC1qm59n9DwLU2WoJjAepDJAew== X-Received: by 10.223.148.199 with SMTP id 65mr31481155wrr.14.1512928894859; Sun, 10 Dec 2017 10:01:34 -0800 (PST) Received: from thinkpad (pppoe-46-239-10-89.teol.net. [46.239.10.89]) by smtp.gmail.com with ESMTPSA id 38sm17223410wry.34.2017.12.10.10.01.32 (version=TLS1 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Sun, 10 Dec 2017 10:01:33 -0800 (PST) Date: Sun, 10 Dec 2017 19:01:31 +0100 From: Ognjen Galic To: "Rafael J. Wysocki" , Len Brown , Robert Moore , Lv Zheng , linux-acpi@vger.kernel.org, devel@acpica.org Cc: Darren Hart , Andy Shevchenko , Henrique de Moraes Holschuh , platform-driver-x86@vger.kernel.org, ibm-acpi-devel@lists.sourceforge.net Subject: [PATCH 1/3 v5] battery: Add the battery hooking API Message-ID: <20171210180131.GA1865@thinkpad> MIME-Version: 1.0 Content-Disposition: inline User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP This is a patch that implements a generic hooking API for the generic ACPI battery driver. With this new generic API, drivers can expose platform specific behaviour via sysfs attributes in /sys/class/power_supply/BATn/ in a generic way. A perfect example of the need for this API are Lenovo ThinkPads. Lenovo ThinkPads have a ACPI extension that allows the setting of start and stop charge thresholds in the EC and battery firmware via ACPI. The thinkpad_acpi module can use this API to expose sysfs attributes that it controls inside the ACPI battery driver sysfs tree, under /sys/class/power_supply/BATN/. The file drivers/acpi/battery.h has been moved to include/acpi/battery.h and the includes inside ac.c, sbs.c, and battery.c have been adjusted to reflect that. When drivers hooks into the API, the API calls add_battery() for each battery in the system that passes it a acpi_battery struct. Then, the drivers can use device_create_file() to create new sysfs attributes with that struct and identify the batteries for per-battery attributes. Tested-by: Kevin Locke Tested-by: Christoph Böhmwalder Signed-off-by: Ognjen Galic --- drivers/acpi/ac.c | 2 +- drivers/acpi/battery.c | 139 ++++++++++++++++++++++++++++++++++++++++++++++++- drivers/acpi/battery.h | 11 ---- drivers/acpi/sbs.c | 2 +- include/acpi/battery.h | 21 ++++++++ 5 files changed, 160 insertions(+), 15 deletions(-) delete mode 100644 drivers/acpi/battery.h create mode 100644 include/acpi/battery.h diff --git a/drivers/acpi/ac.c b/drivers/acpi/ac.c index 47a7ed5..2d8de2f 100644 --- a/drivers/acpi/ac.c +++ b/drivers/acpi/ac.c @@ -33,7 +33,7 @@ #include #include #include -#include "battery.h" +#include #define PREFIX "ACPI: " diff --git a/drivers/acpi/battery.c b/drivers/acpi/battery.c index 13e7b56..2951d07 100644 --- a/drivers/acpi/battery.c +++ b/drivers/acpi/battery.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -42,7 +43,7 @@ #include #include -#include "battery.h" +#include #define PREFIX "ACPI: " @@ -124,6 +125,7 @@ struct acpi_battery { struct power_supply_desc bat_desc; struct acpi_device *device; struct notifier_block pm_nb; + struct list_head list; unsigned long update_time; int revision; int rate_now; @@ -626,6 +628,137 @@ static const struct device_attribute alarm_attr = { .store = acpi_battery_alarm_store, }; +/* + * The Battery Hooking API + * + * This API is used inside other drivers that need to expose + * platform-specific behaviour within the generic driver in a + * generic way. + * + */ + +LIST_HEAD(acpi_battery_list); +LIST_HEAD(battery_hook_list); + +void battery_hook_unregister(struct acpi_battery_hook *hook) +{ + + struct list_head *position; + struct acpi_battery *battery; + + /* + * In order to remove a hook, we first need to + * de-register all the batteries that are registered. + */ + + list_for_each(position, &acpi_battery_list) { + battery = list_entry(position, struct acpi_battery, list); + hook->remove_battery(battery->bat); + } + + /* Then, just remove the hook */ + + list_del(&hook->list); + pr_info("battery: extension unregistered: %s\n", hook->name); + +} +EXPORT_SYMBOL_GPL(battery_hook_unregister); + +void battery_hook_register(struct acpi_battery_hook *hook) +{ + struct list_head *position; + struct acpi_battery *battery; + + INIT_LIST_HEAD(&hook->list); + list_add(&hook->list, &battery_hook_list); + + /* + * Now that the driver is registered, we need + * to notify the hook that a battery is available + * for each battery, so that the driver may add + * its attributes. + */ + list_for_each(position, &acpi_battery_list) { + battery = list_entry(position, struct acpi_battery, list); + if (hook->add_battery(battery->bat)) { + + /* + * If a add-battery returns non-zero, + * the registration of the extension has failed, + * we will unload it. + */ + pr_err("battery: extension failed to load: %s", + hook->name); + battery_hook_unregister(hook); + return; + + } + } + + pr_info("battery: new extension: %s\n", hook->name); + +} +EXPORT_SYMBOL_GPL(battery_hook_register); + +static void battery_hook_add_battery(struct acpi_battery *battery) +{ + + /* + * This function gets called right after the battery sysfs + * attributes have been added, so that the drivers that + * define custom sysfs attributes can add their own. + */ + + struct list_head *position; + struct acpi_battery_hook *hook_node; + + INIT_LIST_HEAD(&battery->list); + list_add(&battery->list, &acpi_battery_list); + + /* + * Since we added a new battery to the list, we need to + * iterate over the hooks and call add_battery for each + * hook that was registered. This usually happens + * when a battery gets hotplugged or initialized + * during the battery module initialization. + */ + + list_for_each(position, &battery_hook_list) { + hook_node = list_entry(position, struct acpi_battery_hook, list); + if (hook_node->add_battery(battery->bat)) { + + /* + * The notification of the extensions has failed, to + * prevent further errors we will unload the extension. + */ + battery_hook_unregister(hook_node); + pr_err("battery: error in extension, unloading: %s", + hook_node->name); + } + } + +} + +static void battery_hook_remove_battery(struct acpi_battery *battery) +{ + struct list_head *position; + struct acpi_battery_hook *hook; + + /* + * Before removing the hook, we need to remove all + * custom attributes from the battery. + */ + list_for_each(position, &battery_hook_list) { + hook = list_entry(position, struct acpi_battery_hook, list); + hook->remove_battery(battery->bat); + } + + /* Then, just remove the battery from the list */ + + list_del(&battery->list); + +} + static int sysfs_add_battery(struct acpi_battery *battery) { struct power_supply_config psy_cfg = { .drv_data = battery, }; @@ -647,6 +780,8 @@ static int sysfs_add_battery(struct acpi_battery *battery) battery->bat = power_supply_register_no_ws(&battery->device->dev, &battery->bat_desc, &psy_cfg); + battery_hook_add_battery(battery); + if (IS_ERR(battery->bat)) { int result = PTR_ERR(battery->bat); @@ -663,7 +798,7 @@ static void sysfs_remove_battery(struct acpi_battery *battery) mutex_unlock(&battery->sysfs_lock); return; } - + battery_hook_remove_battery(battery); device_remove_file(&battery->bat->dev, &alarm_attr); power_supply_unregister(battery->bat); battery->bat = NULL; diff --git a/drivers/acpi/battery.h b/drivers/acpi/battery.h deleted file mode 100644 index 225f493..0000000 --- a/drivers/acpi/battery.h +++ /dev/null @@ -1,11 +0,0 @@ -/* SPDX-License-Identifier: GPL-2.0 */ -#ifndef __ACPI_BATTERY_H -#define __ACPI_BATTERY_H - -#define ACPI_BATTERY_CLASS "battery" - -#define ACPI_BATTERY_NOTIFY_STATUS 0x80 -#define ACPI_BATTERY_NOTIFY_INFO 0x81 -#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82 - -#endif diff --git a/drivers/acpi/sbs.c b/drivers/acpi/sbs.c index a2428e9..295b592 100644 --- a/drivers/acpi/sbs.c +++ b/drivers/acpi/sbs.c @@ -32,9 +32,9 @@ #include #include #include +#include #include "sbshc.h" -#include "battery.h" #define PREFIX "ACPI: " diff --git a/include/acpi/battery.h b/include/acpi/battery.h new file mode 100644 index 0000000..5d8f5d9 --- /dev/null +++ b/include/acpi/battery.h @@ -0,0 +1,21 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef __ACPI_BATTERY_H +#define __ACPI_BATTERY_H + +#define ACPI_BATTERY_CLASS "battery" + +#define ACPI_BATTERY_NOTIFY_STATUS 0x80 +#define ACPI_BATTERY_NOTIFY_INFO 0x81 +#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82 + +struct acpi_battery_hook { + const char *name; + int (*add_battery)(struct power_supply *battery); + int (*remove_battery)(struct power_supply *battery); + struct list_head list; +}; + +void battery_hook_register(struct acpi_battery_hook *hook); +void battery_hook_unregister(struct acpi_battery_hook *hook); + +#endif