Skip to content

Commit fa93854

Browse files
smclt30prafaeljw
authored andcommitted
battery: Add the battery hooking API
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 <[email protected]> Signed-off-by: Rafael J. Wysocki <[email protected]>
1 parent 91ab883 commit fa93854

File tree

5 files changed

+167
-16
lines changed

5 files changed

+167
-16
lines changed

drivers/acpi/ac.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include <linux/platform_device.h>
3434
#include <linux/power_supply.h>
3535
#include <linux/acpi.h>
36-
#include "battery.h"
36+
#include <acpi/battery.h>
3737

3838
#define PREFIX "ACPI: "
3939

drivers/acpi/battery.c

Lines changed: 144 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2222
*/
2323

24+
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
25+
2426
#include <linux/kernel.h>
27+
#include <linux/list.h>
2528
#include <linux/module.h>
29+
#include <linux/mutex.h>
2630
#include <linux/init.h>
2731
#include <linux/types.h>
2832
#include <linux/jiffies.h>
@@ -42,7 +46,7 @@
4246
#include <linux/acpi.h>
4347
#include <linux/power_supply.h>
4448

45-
#include "battery.h"
49+
#include <acpi/battery.h>
4650

4751
#define PREFIX "ACPI: "
4852

@@ -125,6 +129,7 @@ struct acpi_battery {
125129
struct power_supply_desc bat_desc;
126130
struct acpi_device *device;
127131
struct notifier_block pm_nb;
132+
struct list_head list;
128133
unsigned long update_time;
129134
int revision;
130135
int rate_now;
@@ -630,6 +635,139 @@ static const struct device_attribute alarm_attr = {
630635
.store = acpi_battery_alarm_store,
631636
};
632637

638+
/*
639+
* The Battery Hooking API
640+
*
641+
* This API is used inside other drivers that need to expose
642+
* platform-specific behaviour within the generic driver in a
643+
* generic way.
644+
*
645+
*/
646+
647+
static LIST_HEAD(acpi_battery_list);
648+
static LIST_HEAD(battery_hook_list);
649+
static DEFINE_MUTEX(hook_mutex);
650+
651+
void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
652+
{
653+
struct acpi_battery *battery;
654+
/*
655+
* In order to remove a hook, we first need to
656+
* de-register all the batteries that are registered.
657+
*/
658+
if (lock)
659+
mutex_lock(&hook_mutex);
660+
list_for_each_entry(battery, &acpi_battery_list, list) {
661+
hook->remove_battery(battery->bat);
662+
}
663+
list_del(&hook->list);
664+
if (lock)
665+
mutex_unlock(&hook_mutex);
666+
pr_info("extension unregistered: %s\n", hook->name);
667+
}
668+
669+
void battery_hook_unregister(struct acpi_battery_hook *hook)
670+
{
671+
__battery_hook_unregister(hook, 1);
672+
}
673+
EXPORT_SYMBOL_GPL(battery_hook_unregister);
674+
675+
void battery_hook_register(struct acpi_battery_hook *hook)
676+
{
677+
struct acpi_battery *battery;
678+
679+
mutex_lock(&hook_mutex);
680+
INIT_LIST_HEAD(&hook->list);
681+
list_add(&hook->list, &battery_hook_list);
682+
/*
683+
* Now that the driver is registered, we need
684+
* to notify the hook that a battery is available
685+
* for each battery, so that the driver may add
686+
* its attributes.
687+
*/
688+
list_for_each_entry(battery, &acpi_battery_list, list) {
689+
if (hook->add_battery(battery->bat)) {
690+
/*
691+
* If a add-battery returns non-zero,
692+
* the registration of the extension has failed,
693+
* and we will not add it to the list of loaded
694+
* hooks.
695+
*/
696+
pr_err("extension failed to load: %s", hook->name);
697+
__battery_hook_unregister(hook, 0);
698+
return;
699+
}
700+
}
701+
pr_info("new extension: %s\n", hook->name);
702+
mutex_unlock(&hook_mutex);
703+
}
704+
EXPORT_SYMBOL_GPL(battery_hook_register);
705+
706+
/*
707+
* This function gets called right after the battery sysfs
708+
* attributes have been added, so that the drivers that
709+
* define custom sysfs attributes can add their own.
710+
*/
711+
static void battery_hook_add_battery(struct acpi_battery *battery)
712+
{
713+
struct acpi_battery_hook *hook_node;
714+
715+
mutex_lock(&hook_mutex);
716+
INIT_LIST_HEAD(&battery->list);
717+
list_add(&battery->list, &acpi_battery_list);
718+
/*
719+
* Since we added a new battery to the list, we need to
720+
* iterate over the hooks and call add_battery for each
721+
* hook that was registered. This usually happens
722+
* when a battery gets hotplugged or initialized
723+
* during the battery module initialization.
724+
*/
725+
list_for_each_entry(hook_node, &battery_hook_list, list) {
726+
if (hook_node->add_battery(battery->bat)) {
727+
/*
728+
* The notification of the extensions has failed, to
729+
* prevent further errors we will unload the extension.
730+
*/
731+
__battery_hook_unregister(hook_node, 0);
732+
pr_err("error in extension, unloading: %s",
733+
hook_node->name);
734+
}
735+
}
736+
mutex_unlock(&hook_mutex);
737+
}
738+
739+
static void battery_hook_remove_battery(struct acpi_battery *battery)
740+
{
741+
struct acpi_battery_hook *hook;
742+
743+
mutex_lock(&hook_mutex);
744+
/*
745+
* Before removing the hook, we need to remove all
746+
* custom attributes from the battery.
747+
*/
748+
list_for_each_entry(hook, &battery_hook_list, list) {
749+
hook->remove_battery(battery->bat);
750+
}
751+
/* Then, just remove the battery from the list */
752+
list_del(&battery->list);
753+
mutex_unlock(&hook_mutex);
754+
}
755+
756+
static void __exit battery_hook_exit(void)
757+
{
758+
struct acpi_battery_hook *hook;
759+
struct acpi_battery_hook *ptr;
760+
/*
761+
* At this point, the acpi_bus_unregister_driver()
762+
* has called remove for all batteries. We just
763+
* need to remove the hooks.
764+
*/
765+
list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
766+
__battery_hook_unregister(hook, 1);
767+
}
768+
mutex_destroy(&hook_mutex);
769+
}
770+
633771
static int sysfs_add_battery(struct acpi_battery *battery)
634772
{
635773
struct power_supply_config psy_cfg = { .drv_data = battery, };
@@ -657,6 +795,7 @@ static int sysfs_add_battery(struct acpi_battery *battery)
657795
battery->bat = NULL;
658796
return result;
659797
}
798+
battery_hook_add_battery(battery);
660799
return device_create_file(&battery->bat->dev, &alarm_attr);
661800
}
662801

@@ -667,7 +806,7 @@ static void sysfs_remove_battery(struct acpi_battery *battery)
667806
mutex_unlock(&battery->sysfs_lock);
668807
return;
669808
}
670-
809+
battery_hook_remove_battery(battery);
671810
device_remove_file(&battery->bat->dev, &alarm_attr);
672811
power_supply_unregister(battery->bat);
673812
battery->bat = NULL;
@@ -1399,8 +1538,10 @@ static int __init acpi_battery_init(void)
13991538
static void __exit acpi_battery_exit(void)
14001539
{
14011540
async_synchronize_cookie(async_cookie + 1);
1402-
if (battery_driver_registered)
1541+
if (battery_driver_registered) {
14031542
acpi_bus_unregister_driver(&acpi_battery_driver);
1543+
battery_hook_exit();
1544+
}
14041545
#ifdef CONFIG_ACPI_PROCFS_POWER
14051546
if (acpi_battery_dir)
14061547
acpi_unlock_battery_dir(acpi_battery_dir);

drivers/acpi/battery.h

Lines changed: 0 additions & 11 deletions
This file was deleted.

drivers/acpi/sbs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@
3232
#include <linux/delay.h>
3333
#include <linux/power_supply.h>
3434
#include <linux/platform_data/x86/apple.h>
35+
#include <acpi/battery.h>
3536

3637
#include "sbshc.h"
37-
#include "battery.h"
3838

3939
#define PREFIX "ACPI: "
4040

include/acpi/battery.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
#ifndef __ACPI_BATTERY_H
3+
#define __ACPI_BATTERY_H
4+
5+
#define ACPI_BATTERY_CLASS "battery"
6+
7+
#define ACPI_BATTERY_NOTIFY_STATUS 0x80
8+
#define ACPI_BATTERY_NOTIFY_INFO 0x81
9+
#define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
10+
11+
struct acpi_battery_hook {
12+
const char *name;
13+
int (*add_battery)(struct power_supply *battery);
14+
int (*remove_battery)(struct power_supply *battery);
15+
struct list_head list;
16+
};
17+
18+
void battery_hook_register(struct acpi_battery_hook *hook);
19+
void battery_hook_unregister(struct acpi_battery_hook *hook);
20+
21+
#endif

0 commit comments

Comments
 (0)