From patchwork Wed Feb 7 14:58:13 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ognjen Galic X-Patchwork-Id: 10205339 X-Patchwork-Delegate: rjw@sisk.pl 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 26C5D6020F for ; Wed, 7 Feb 2018 14:58:26 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0EC2228E94 for ; Wed, 7 Feb 2018 14:58:26 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0309328EC7; Wed, 7 Feb 2018 14:58:25 +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 5516728FA8 for ; Wed, 7 Feb 2018 14:58:25 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754138AbeBGO6V (ORCPT ); Wed, 7 Feb 2018 09:58:21 -0500 Received: from mail-wr0-f196.google.com ([209.85.128.196]:43539 "EHLO mail-wr0-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754115AbeBGO6T (ORCPT ); Wed, 7 Feb 2018 09:58:19 -0500 Received: by mail-wr0-f196.google.com with SMTP id b52so1339264wrd.10; Wed, 07 Feb 2018 06:58:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:subject:message-id:mime-version:content-disposition :user-agent; bh=vfhF3B8FfMXH8j6X0sbctDA4MzXykCOU1uomCZCbOCM=; b=pw4QksmUmvuP5aNuph7jymAw9E9lJuw1y2cfth/nppvYgoldqw6qOkavTQezvFx/LV UDiR31ZW5ZtuPMczbyW5IQ2ELkPCpIqKYFaqiV1XeQKB2QyZ3C0YTAT6/N3RJLiFSsS5 +/OOXpaw7spM19mi67/GvCptrxzc1j8y++xzBmv6NrbE4MUjWOsFRGgwzpm71r6gcf9Z M596HP+GrZul/Uq9tQpn00h/OBxFfKdIlUN3gS2KPYj2ASW6fKugfBW0x0p0K3PtpNi8 FEd8fqdoycK1rNZzc/wXluyW8dpi1zQwGLiUPeoci4ktZuIlocGBH43LhfPsh3Z4eHYO bVEQ== 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:subject:message-id:mime-version :content-disposition:user-agent; bh=vfhF3B8FfMXH8j6X0sbctDA4MzXykCOU1uomCZCbOCM=; b=ojg3LRgWfLH9fsLNxv+MbYIljJ13Dr8sjaecIMwrbkshKMXmBjfnxdEO0X+Y9a9BFd r+m2D2006FIQLYEwQRMql56egxGUuuDdGPpsRJcf5nfWwsjFGJbKJx+4Nxk7Zr6iZXu6 9OG1GsLVzJ99KYKsIUUOna9g0xMQ1QKwj2MmSE3st1yV4dmBnbRKjVUMqR0THbqFwB6X 5dhpV13ejQXaeoRqHeBwJGeDHqUWf/a3qyThnN8NjlOqWLMiPA9Yuh8BUPEYTcJO2pED PuqsSN0xrSDkIDnh1rMZ9IvRegE0yNg7i/gAwq+uj7Jhpz+qGxZE1r0YE9cq1B9iq4rD vhQA== X-Gm-Message-State: APf1xPB2Huor+pgyyIq8VteBus+H6n8MJB1l4NBHMeCU1IXs1w+AAaJA 8TljzgXHkbKgLwLzoJv0aOQ= X-Google-Smtp-Source: AH8x224cpvsatv1UUAt/z9LXdtGE+1pupVJJz7BF4hg+9Rd0NI6oKUK0hzsqKkonPZMiubPp4IAEWg== X-Received: by 10.223.151.53 with SMTP id r50mr5846030wrb.207.1518015497570; Wed, 07 Feb 2018 06:58:17 -0800 (PST) Received: from thinkpad (pppoe-46-239-10-245.teol.net. [46.239.10.245]) by smtp.googlemail.com with ESMTPSA id 137sm1944426wmm.46.2018.02.07.06.58.14 (version=TLS1_2 cipher=ECDHE-RSA-CHACHA20-POLY1305 bits=256/256); Wed, 07 Feb 2018 06:58:16 -0800 (PST) Date: Wed, 7 Feb 2018 15:58:13 +0100 From: Ognjen Galic To: Andy Shevchenko , "Rafael J. Wysocki" , Ognjen =?utf-8?B?R2FsacSH?= , "Rafael J. Wysocki" , Len Brown , Robert Moore , Lv Zheng , ACPI Devel Maling List , devel@acpica.org, Darren Hart , Andy Shevchenko , Henrique de Moraes Holschuh , Sebastian Reichel , Platform Driver , ibm-acpi-devel@lists.sourceforge.net, Linux PM , Christoph =?iso-8859-1?Q?B=F6hmwalder?= , Kevin Locke Subject: [PATCH v13 1/4] battery: Add the battery hooking API Message-ID: <20180207145813.icmv6rwemyejhxbk@thinkpad> MIME-Version: 1.0 Content-Disposition: inline User-Agent: NeoMutt/20170609 (1.8.3) 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. Signed-off-by: Ognjen Galic --- Notes: v7: * Implemented mutual exclusion for hooking methods and battery callbacks * Fixed a BUG where errors in other modules would occur when the modules that depend on battery get unloaded v8: * Use list_for_each_safe instead of list_for_each for the module exit function where deletion of nodes occurs v9: * No changes in this patch in v9 v10: * Fix compiler warnings in Intel's 0-day CI v11: * Fix changelog formatting * Make lists and mutex static v12: * Change all applicable list_for_each to list_for_each_entry * Define pr_fmt to include module name for pr_ v13: * Fixed the last list_for_each_entry * Move some comments around * Prevent battery registering if power_supply_register_no_ws() fails drivers/acpi/ac.c | 2 +- drivers/acpi/battery.c | 147 ++++++++++++++++++++++++++++++++++++++++++++++++- drivers/acpi/battery.h | 11 ---- drivers/acpi/sbs.c | 2 +- include/acpi/battery.h | 21 +++++++ 5 files changed, 167 insertions(+), 16 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 47a7ed557..2d8de2f8c 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 19bc44082..3fab2cb9e 100644 --- a/drivers/acpi/battery.c +++ b/drivers/acpi/battery.c @@ -21,8 +21,12 @@ * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + #include +#include #include +#include #include #include #include @@ -42,7 +46,7 @@ #include #include -#include "battery.h" +#include #define PREFIX "ACPI: " @@ -125,6 +129,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; @@ -630,6 +635,139 @@ 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. + * + */ + +static LIST_HEAD(acpi_battery_list); +static LIST_HEAD(battery_hook_list); +static DEFINE_MUTEX(hook_mutex); + +void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock) +{ + struct acpi_battery *battery; + /* + * In order to remove a hook, we first need to + * de-register all the batteries that are registered. + */ + if (lock) + mutex_lock(&hook_mutex); + list_for_each_entry(battery, &acpi_battery_list, list) { + hook->remove_battery(battery->bat); + } + list_del(&hook->list); + if (lock) + mutex_unlock(&hook_mutex); + pr_info("extension unregistered: %s\n", hook->name); +} + +void battery_hook_unregister(struct acpi_battery_hook *hook) +{ + __battery_hook_unregister(hook, 1); +} +EXPORT_SYMBOL_GPL(battery_hook_unregister); + +void battery_hook_register(struct acpi_battery_hook *hook) +{ + struct acpi_battery *battery; + + mutex_lock(&hook_mutex); + 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_entry(battery, &acpi_battery_list, list) { + if (hook->add_battery(battery->bat)) { + /* + * If a add-battery returns non-zero, + * the registration of the extension has failed, + * and we will not add it to the list of loaded + * hooks. + */ + pr_err("extension failed to load: %s", hook->name); + __battery_hook_unregister(hook, 0); + return; + } + } + pr_info("new extension: %s\n", hook->name); + mutex_unlock(&hook_mutex); +} +EXPORT_SYMBOL_GPL(battery_hook_register); + +/* + * 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. +*/ +static void battery_hook_add_battery(struct acpi_battery *battery) +{ + struct acpi_battery_hook *hook_node; + + mutex_lock(&hook_mutex); + 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_entry(hook_node, &battery_hook_list, 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, 0); + pr_err("error in extension, unloading: %s", + hook_node->name); + } + } + mutex_unlock(&hook_mutex); +} + +static void battery_hook_remove_battery(struct acpi_battery *battery) +{ + struct acpi_battery_hook *hook; + + mutex_lock(&hook_mutex); + /* + * Before removing the hook, we need to remove all + * custom attributes from the battery. + */ + list_for_each_entry(hook, &battery_hook_list, list) { + hook->remove_battery(battery->bat); + } + /* Then, just remove the battery from the list */ + list_del(&battery->list); + mutex_unlock(&hook_mutex); +} + +static void __exit battery_hook_exit(void) +{ + struct acpi_battery_hook *hook; + struct acpi_battery_hook *ptr; + /* + * At this point, the acpi_bus_unregister_driver() + * has called remove for all batteries. We just + * need to remove the hooks. + */ + list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) { + __battery_hook_unregister(hook, 1); + } + mutex_destroy(&hook_mutex); +} + static int sysfs_add_battery(struct acpi_battery *battery) { struct power_supply_config psy_cfg = { .drv_data = battery, }; @@ -657,6 +795,7 @@ static int sysfs_add_battery(struct acpi_battery *battery) battery->bat = NULL; return result; } + battery_hook_add_battery(battery); return device_create_file(&battery->bat->dev, &alarm_attr); } @@ -667,7 +806,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; @@ -1383,8 +1522,10 @@ static int __init acpi_battery_init(void) static void __exit acpi_battery_exit(void) { async_synchronize_cookie(async_cookie + 1); - if (battery_driver_registered) + if (battery_driver_registered) { acpi_bus_unregister_driver(&acpi_battery_driver); + battery_hook_exit(); + } #ifdef CONFIG_ACPI_PROCFS_POWER if (acpi_battery_dir) acpi_unlock_battery_dir(acpi_battery_dir); diff --git a/drivers/acpi/battery.h b/drivers/acpi/battery.h deleted file mode 100644 index 225f493d4..000000000 --- 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 a2428e946..295b59271 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 000000000..5d8f5d910 --- /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