diff options
author | Anton Vorontsov <cbou@mail.ru> | 2007-05-03 16:27:45 -0400 |
---|---|---|
committer | David Woodhouse <dwmw2@infradead.org> | 2007-07-10 06:25:44 -0400 |
commit | 4a11b59d8283662193a9c6a9c14c58d1b9bf0617 (patch) | |
tree | 492bf022c806ec6c554ca5f8e7c05ea6db138137 /drivers | |
parent | 7dcca30a32aadb0520417521b0c44f42d09fe05c (diff) |
[BATTERY] Universal power supply class (was: battery class)
This class is result of "external power" and "battery" classes merge,
as suggested by David Woodhouse. He also implemented uevent support.
Here how userspace seeing it now:
# ls /sys/class/power\ supply/
ac main-battery usb
# cat /sys/class/power\ supply/ac/type
AC
# cat /sys/class/power\ supply/usb/type
USB
# cat /sys/class/power\ supply/main-battery/type
Battery
# cat /sys/class/power\ supply/ac/online
1
# cat /sys/class/power\ supply/usb/online
0
# cat /sys/class/power\ supply/main-battery/status
Charging
# cat /sys/class/leds/h5400\:red-left/trigger
none h5400-radio timer hwtimer ac-online usb-online
main-battery-charging-or-full [main-battery-charging]
main-battery-full
Signed-off-by: Anton Vorontsov <cbou@mail.ru>
Signed-off-by: David Woodhouse <dwmw2@infradead.org>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
Diffstat (limited to 'drivers')
-rw-r--r-- | drivers/Kconfig | 2 | ||||
-rw-r--r-- | drivers/Makefile | 1 | ||||
-rw-r--r-- | drivers/power/Kconfig | 17 | ||||
-rw-r--r-- | drivers/power/Makefile | 15 | ||||
-rw-r--r-- | drivers/power/power_supply.h | 42 | ||||
-rw-r--r-- | drivers/power/power_supply_core.c | 168 | ||||
-rw-r--r-- | drivers/power/power_supply_leds.c | 176 | ||||
-rw-r--r-- | drivers/power/power_supply_sysfs.c | 299 |
8 files changed, 720 insertions, 0 deletions
diff --git a/drivers/Kconfig b/drivers/Kconfig index 050323fd79e9..c546de307caf 100644 --- a/drivers/Kconfig +++ b/drivers/Kconfig | |||
@@ -54,6 +54,8 @@ source "drivers/spi/Kconfig" | |||
54 | 54 | ||
55 | source "drivers/w1/Kconfig" | 55 | source "drivers/w1/Kconfig" |
56 | 56 | ||
57 | source "drivers/power/Kconfig" | ||
58 | |||
57 | source "drivers/hwmon/Kconfig" | 59 | source "drivers/hwmon/Kconfig" |
58 | 60 | ||
59 | source "drivers/mfd/Kconfig" | 61 | source "drivers/mfd/Kconfig" |
diff --git a/drivers/Makefile b/drivers/Makefile index adad2f3d438a..503d82569449 100644 --- a/drivers/Makefile +++ b/drivers/Makefile | |||
@@ -61,6 +61,7 @@ obj-$(CONFIG_I2O) += message/ | |||
61 | obj-$(CONFIG_RTC_LIB) += rtc/ | 61 | obj-$(CONFIG_RTC_LIB) += rtc/ |
62 | obj-y += i2c/ | 62 | obj-y += i2c/ |
63 | obj-$(CONFIG_W1) += w1/ | 63 | obj-$(CONFIG_W1) += w1/ |
64 | obj-$(CONFIG_POWER_SUPPLY) += power/ | ||
64 | obj-$(CONFIG_HWMON) += hwmon/ | 65 | obj-$(CONFIG_HWMON) += hwmon/ |
65 | obj-$(CONFIG_PHONE) += telephony/ | 66 | obj-$(CONFIG_PHONE) += telephony/ |
66 | obj-$(CONFIG_MD) += md/ | 67 | obj-$(CONFIG_MD) += md/ |
diff --git a/drivers/power/Kconfig b/drivers/power/Kconfig new file mode 100644 index 000000000000..7811fa627412 --- /dev/null +++ b/drivers/power/Kconfig | |||
@@ -0,0 +1,17 @@ | |||
1 | menuconfig POWER_SUPPLY | ||
2 | tristate "Power supply class support" | ||
3 | help | ||
4 | Say Y here to enable power supply class support. This allows | ||
5 | power supply (batteries, AC, USB) monitoring by userspace | ||
6 | via sysfs and uevent (if available) and/or APM kernel interface | ||
7 | (if selected below). | ||
8 | |||
9 | if POWER_SUPPLY | ||
10 | |||
11 | config POWER_SUPPLY_DEBUG | ||
12 | bool "Power supply debug" | ||
13 | help | ||
14 | Say Y here to enable debugging messages for power supply class | ||
15 | and drivers. | ||
16 | |||
17 | endif # POWER_SUPPLY | ||
diff --git a/drivers/power/Makefile b/drivers/power/Makefile new file mode 100644 index 000000000000..3c88148983a2 --- /dev/null +++ b/drivers/power/Makefile | |||
@@ -0,0 +1,15 @@ | |||
1 | power_supply-objs := power_supply_core.o | ||
2 | |||
3 | ifeq ($(CONFIG_SYSFS),y) | ||
4 | power_supply-objs += power_supply_sysfs.o | ||
5 | endif | ||
6 | |||
7 | ifeq ($(CONFIG_LEDS_TRIGGERS),y) | ||
8 | power_supply-objs += power_supply_leds.o | ||
9 | endif | ||
10 | |||
11 | ifeq ($(CONFIG_POWER_SUPPLY_DEBUG),y) | ||
12 | EXTRA_CFLAGS += -DDEBUG | ||
13 | endif | ||
14 | |||
15 | obj-$(CONFIG_POWER_SUPPLY) += power_supply.o | ||
diff --git a/drivers/power/power_supply.h b/drivers/power/power_supply.h new file mode 100644 index 000000000000..a9880d468ee4 --- /dev/null +++ b/drivers/power/power_supply.h | |||
@@ -0,0 +1,42 @@ | |||
1 | /* | ||
2 | * Functions private to power supply class | ||
3 | * | ||
4 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | ||
5 | * Copyright © 2004 Szabolcs Gyurko | ||
6 | * Copyright © 2003 Ian Molton <spyro@f2s.com> | ||
7 | * | ||
8 | * Modified: 2004, Oct Szabolcs Gyurko | ||
9 | * | ||
10 | * You may use this code as per GPL version 2 | ||
11 | */ | ||
12 | |||
13 | #ifdef CONFIG_SYSFS | ||
14 | |||
15 | extern int power_supply_create_attrs(struct power_supply *psy); | ||
16 | extern void power_supply_remove_attrs(struct power_supply *psy); | ||
17 | extern int power_supply_uevent(struct device *dev, char **envp, int num_envp, | ||
18 | char *buffer, int buffer_size); | ||
19 | |||
20 | #else | ||
21 | |||
22 | static inline int power_supply_create_attrs(struct power_supply *psy) | ||
23 | { return 0; } | ||
24 | static inline void power_supply_remove_attrs(struct power_supply *psy) {} | ||
25 | #define power_supply_uevent NULL | ||
26 | |||
27 | #endif /* CONFIG_SYSFS */ | ||
28 | |||
29 | #ifdef CONFIG_LEDS_TRIGGERS | ||
30 | |||
31 | extern void power_supply_update_leds(struct power_supply *psy); | ||
32 | extern int power_supply_create_triggers(struct power_supply *psy); | ||
33 | extern void power_supply_remove_triggers(struct power_supply *psy); | ||
34 | |||
35 | #else | ||
36 | |||
37 | static inline void power_supply_update_leds(struct power_supply *psy) {} | ||
38 | static inline int power_supply_create_triggers(struct power_supply *psy) | ||
39 | { return 0; } | ||
40 | static inline void power_supply_remove_triggers(struct power_supply *psy) {} | ||
41 | |||
42 | #endif /* CONFIG_LEDS_TRIGGERS */ | ||
diff --git a/drivers/power/power_supply_core.c b/drivers/power/power_supply_core.c new file mode 100644 index 000000000000..e87ea5156755 --- /dev/null +++ b/drivers/power/power_supply_core.c | |||
@@ -0,0 +1,168 @@ | |||
1 | /* | ||
2 | * Universal power supply monitor class | ||
3 | * | ||
4 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | ||
5 | * Copyright © 2004 Szabolcs Gyurko | ||
6 | * Copyright © 2003 Ian Molton <spyro@f2s.com> | ||
7 | * | ||
8 | * Modified: 2004, Oct Szabolcs Gyurko | ||
9 | * | ||
10 | * You may use this code as per GPL version 2 | ||
11 | */ | ||
12 | |||
13 | #include <linux/module.h> | ||
14 | #include <linux/types.h> | ||
15 | #include <linux/init.h> | ||
16 | #include <linux/device.h> | ||
17 | #include <linux/err.h> | ||
18 | #include <linux/power_supply.h> | ||
19 | #include "power_supply.h" | ||
20 | |||
21 | struct class *power_supply_class; | ||
22 | |||
23 | static void power_supply_changed_work(struct work_struct *work) | ||
24 | { | ||
25 | struct power_supply *psy = container_of(work, struct power_supply, | ||
26 | changed_work); | ||
27 | int i; | ||
28 | |||
29 | dev_dbg(psy->dev, "%s\n", __FUNCTION__); | ||
30 | |||
31 | for (i = 0; i < psy->num_supplicants; i++) { | ||
32 | struct device *dev; | ||
33 | |||
34 | down(&power_supply_class->sem); | ||
35 | list_for_each_entry(dev, &power_supply_class->devices, node) { | ||
36 | struct power_supply *pst = dev_get_drvdata(dev); | ||
37 | |||
38 | if (!strcmp(psy->supplied_to[i], pst->name)) { | ||
39 | if (pst->external_power_changed) | ||
40 | pst->external_power_changed(pst); | ||
41 | } | ||
42 | } | ||
43 | up(&power_supply_class->sem); | ||
44 | } | ||
45 | |||
46 | power_supply_update_leds(psy); | ||
47 | |||
48 | kobject_uevent(&psy->dev->kobj, KOBJ_CHANGE); | ||
49 | |||
50 | return; | ||
51 | } | ||
52 | |||
53 | void power_supply_changed(struct power_supply *psy) | ||
54 | { | ||
55 | dev_dbg(psy->dev, "%s\n", __FUNCTION__); | ||
56 | |||
57 | schedule_work(&psy->changed_work); | ||
58 | |||
59 | return; | ||
60 | } | ||
61 | |||
62 | int power_supply_am_i_supplied(struct power_supply *psy) | ||
63 | { | ||
64 | union power_supply_propval ret = {0,}; | ||
65 | struct device *dev; | ||
66 | |||
67 | down(&power_supply_class->sem); | ||
68 | list_for_each_entry(dev, &power_supply_class->devices, node) { | ||
69 | struct power_supply *epsy = dev_get_drvdata(dev); | ||
70 | int i; | ||
71 | |||
72 | for (i = 0; i < epsy->num_supplicants; i++) { | ||
73 | if (!strcmp(epsy->supplied_to[i], psy->name)) { | ||
74 | if (epsy->get_property(epsy, | ||
75 | POWER_SUPPLY_PROP_ONLINE, &ret)) | ||
76 | continue; | ||
77 | if (ret.intval) | ||
78 | goto out; | ||
79 | } | ||
80 | } | ||
81 | } | ||
82 | out: | ||
83 | up(&power_supply_class->sem); | ||
84 | |||
85 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, ret.intval); | ||
86 | |||
87 | return ret.intval; | ||
88 | } | ||
89 | |||
90 | int power_supply_register(struct device *parent, struct power_supply *psy) | ||
91 | { | ||
92 | int rc = 0; | ||
93 | |||
94 | psy->dev = device_create(power_supply_class, parent, 0, | ||
95 | "%s", psy->name); | ||
96 | if (IS_ERR(psy->dev)) { | ||
97 | rc = PTR_ERR(psy->dev); | ||
98 | goto dev_create_failed; | ||
99 | } | ||
100 | |||
101 | dev_set_drvdata(psy->dev, psy); | ||
102 | |||
103 | INIT_WORK(&psy->changed_work, power_supply_changed_work); | ||
104 | |||
105 | rc = power_supply_create_attrs(psy); | ||
106 | if (rc) | ||
107 | goto create_attrs_failed; | ||
108 | |||
109 | rc = power_supply_create_triggers(psy); | ||
110 | if (rc) | ||
111 | goto create_triggers_failed; | ||
112 | |||
113 | power_supply_changed(psy); | ||
114 | |||
115 | goto success; | ||
116 | |||
117 | create_triggers_failed: | ||
118 | power_supply_remove_attrs(psy); | ||
119 | create_attrs_failed: | ||
120 | device_unregister(psy->dev); | ||
121 | dev_create_failed: | ||
122 | success: | ||
123 | return rc; | ||
124 | } | ||
125 | |||
126 | void power_supply_unregister(struct power_supply *psy) | ||
127 | { | ||
128 | flush_scheduled_work(); | ||
129 | power_supply_remove_triggers(psy); | ||
130 | power_supply_remove_attrs(psy); | ||
131 | device_unregister(psy->dev); | ||
132 | return; | ||
133 | } | ||
134 | |||
135 | static int __init power_supply_class_init(void) | ||
136 | { | ||
137 | power_supply_class = class_create(THIS_MODULE, "power_supply"); | ||
138 | |||
139 | if (IS_ERR(power_supply_class)) | ||
140 | return PTR_ERR(power_supply_class); | ||
141 | |||
142 | power_supply_class->dev_uevent = power_supply_uevent; | ||
143 | |||
144 | return 0; | ||
145 | } | ||
146 | |||
147 | static void __exit power_supply_class_exit(void) | ||
148 | { | ||
149 | class_destroy(power_supply_class); | ||
150 | return; | ||
151 | } | ||
152 | |||
153 | EXPORT_SYMBOL_GPL(power_supply_changed); | ||
154 | EXPORT_SYMBOL_GPL(power_supply_am_i_supplied); | ||
155 | EXPORT_SYMBOL_GPL(power_supply_register); | ||
156 | EXPORT_SYMBOL_GPL(power_supply_unregister); | ||
157 | |||
158 | /* exported for the APM Power driver, APM emulation */ | ||
159 | EXPORT_SYMBOL_GPL(power_supply_class); | ||
160 | |||
161 | subsys_initcall(power_supply_class_init); | ||
162 | module_exit(power_supply_class_exit); | ||
163 | |||
164 | MODULE_DESCRIPTION("Universal power supply monitor class"); | ||
165 | MODULE_AUTHOR("Ian Molton <spyro@f2s.com>, " | ||
166 | "Szabolcs Gyurko, " | ||
167 | "Anton Vorontsov <cbou@mail.ru>"); | ||
168 | MODULE_LICENSE("GPL"); | ||
diff --git a/drivers/power/power_supply_leds.c b/drivers/power/power_supply_leds.c new file mode 100644 index 000000000000..7232490bb595 --- /dev/null +++ b/drivers/power/power_supply_leds.c | |||
@@ -0,0 +1,176 @@ | |||
1 | /* | ||
2 | * LEDs triggers for power supply class | ||
3 | * | ||
4 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | ||
5 | * Copyright © 2004 Szabolcs Gyurko | ||
6 | * Copyright © 2003 Ian Molton <spyro@f2s.com> | ||
7 | * | ||
8 | * Modified: 2004, Oct Szabolcs Gyurko | ||
9 | * | ||
10 | * You may use this code as per GPL version 2 | ||
11 | */ | ||
12 | |||
13 | #include <linux/power_supply.h> | ||
14 | |||
15 | /* Battery specific LEDs triggers. */ | ||
16 | |||
17 | static void power_supply_update_bat_leds(struct power_supply *psy) | ||
18 | { | ||
19 | union power_supply_propval status; | ||
20 | |||
21 | if (psy->get_property(psy, POWER_SUPPLY_PROP_STATUS, &status)) | ||
22 | return; | ||
23 | |||
24 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, status.intval); | ||
25 | |||
26 | switch (status.intval) { | ||
27 | case POWER_SUPPLY_STATUS_FULL: | ||
28 | led_trigger_event(psy->charging_full_trig, LED_FULL); | ||
29 | led_trigger_event(psy->charging_trig, LED_OFF); | ||
30 | led_trigger_event(psy->full_trig, LED_FULL); | ||
31 | break; | ||
32 | case POWER_SUPPLY_STATUS_CHARGING: | ||
33 | led_trigger_event(psy->charging_full_trig, LED_FULL); | ||
34 | led_trigger_event(psy->charging_trig, LED_FULL); | ||
35 | led_trigger_event(psy->full_trig, LED_OFF); | ||
36 | break; | ||
37 | default: | ||
38 | led_trigger_event(psy->charging_full_trig, LED_OFF); | ||
39 | led_trigger_event(psy->charging_trig, LED_OFF); | ||
40 | led_trigger_event(psy->full_trig, LED_OFF); | ||
41 | break; | ||
42 | } | ||
43 | |||
44 | return; | ||
45 | } | ||
46 | |||
47 | static int power_supply_create_bat_triggers(struct power_supply *psy) | ||
48 | { | ||
49 | int rc = 0; | ||
50 | |||
51 | psy->charging_full_trig_name = kmalloc(strlen(psy->name) + | ||
52 | sizeof("-charging-or-full"), GFP_KERNEL); | ||
53 | if (!psy->charging_full_trig_name) | ||
54 | goto charging_full_failed; | ||
55 | |||
56 | psy->charging_trig_name = kmalloc(strlen(psy->name) + | ||
57 | sizeof("-charging"), GFP_KERNEL); | ||
58 | if (!psy->charging_trig_name) | ||
59 | goto charging_failed; | ||
60 | |||
61 | psy->full_trig_name = kmalloc(strlen(psy->name) + | ||
62 | sizeof("-full"), GFP_KERNEL); | ||
63 | if (!psy->full_trig_name) | ||
64 | goto full_failed; | ||
65 | |||
66 | strcpy(psy->charging_full_trig_name, psy->name); | ||
67 | strcat(psy->charging_full_trig_name, "-charging-or-full"); | ||
68 | strcpy(psy->charging_trig_name, psy->name); | ||
69 | strcat(psy->charging_trig_name, "-charging"); | ||
70 | strcpy(psy->full_trig_name, psy->name); | ||
71 | strcat(psy->full_trig_name, "-full"); | ||
72 | |||
73 | led_trigger_register_simple(psy->charging_full_trig_name, | ||
74 | &psy->charging_full_trig); | ||
75 | led_trigger_register_simple(psy->charging_trig_name, | ||
76 | &psy->charging_trig); | ||
77 | led_trigger_register_simple(psy->full_trig_name, | ||
78 | &psy->full_trig); | ||
79 | |||
80 | goto success; | ||
81 | |||
82 | full_failed: | ||
83 | kfree(psy->charging_trig_name); | ||
84 | charging_failed: | ||
85 | kfree(psy->charging_full_trig_name); | ||
86 | charging_full_failed: | ||
87 | rc = -ENOMEM; | ||
88 | success: | ||
89 | return rc; | ||
90 | } | ||
91 | |||
92 | static void power_supply_remove_bat_triggers(struct power_supply *psy) | ||
93 | { | ||
94 | led_trigger_unregister_simple(psy->charging_full_trig); | ||
95 | led_trigger_unregister_simple(psy->charging_trig); | ||
96 | led_trigger_unregister_simple(psy->full_trig); | ||
97 | kfree(psy->full_trig_name); | ||
98 | kfree(psy->charging_trig_name); | ||
99 | kfree(psy->charging_full_trig_name); | ||
100 | return; | ||
101 | } | ||
102 | |||
103 | /* Generated power specific LEDs triggers. */ | ||
104 | |||
105 | static void power_supply_update_gen_leds(struct power_supply *psy) | ||
106 | { | ||
107 | union power_supply_propval online; | ||
108 | |||
109 | if (psy->get_property(psy, POWER_SUPPLY_PROP_ONLINE, &online)) | ||
110 | return; | ||
111 | |||
112 | dev_dbg(psy->dev, "%s %d\n", __FUNCTION__, online.intval); | ||
113 | |||
114 | if (online.intval) | ||
115 | led_trigger_event(psy->online_trig, LED_FULL); | ||
116 | else | ||
117 | led_trigger_event(psy->online_trig, LED_OFF); | ||
118 | |||
119 | return; | ||
120 | } | ||
121 | |||
122 | static int power_supply_create_gen_triggers(struct power_supply *psy) | ||
123 | { | ||
124 | int rc = 0; | ||
125 | |||
126 | psy->online_trig_name = kmalloc(strlen(psy->name) + sizeof("-online"), | ||
127 | GFP_KERNEL); | ||
128 | if (!psy->online_trig_name) | ||
129 | goto online_failed; | ||
130 | |||
131 | strcpy(psy->online_trig_name, psy->name); | ||
132 | strcat(psy->online_trig_name, "-online"); | ||
133 | |||
134 | led_trigger_register_simple(psy->online_trig_name, &psy->online_trig); | ||
135 | |||
136 | goto success; | ||
137 | |||
138 | online_failed: | ||
139 | rc = -ENOMEM; | ||
140 | success: | ||
141 | return rc; | ||
142 | } | ||
143 | |||
144 | static void power_supply_remove_gen_triggers(struct power_supply *psy) | ||
145 | { | ||
146 | led_trigger_unregister_simple(psy->online_trig); | ||
147 | kfree(psy->online_trig_name); | ||
148 | return; | ||
149 | } | ||
150 | |||
151 | /* Choice what triggers to create&update. */ | ||
152 | |||
153 | void power_supply_update_leds(struct power_supply *psy) | ||
154 | { | ||
155 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) | ||
156 | power_supply_update_bat_leds(psy); | ||
157 | else | ||
158 | power_supply_update_gen_leds(psy); | ||
159 | return; | ||
160 | } | ||
161 | |||
162 | int power_supply_create_triggers(struct power_supply *psy) | ||
163 | { | ||
164 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) | ||
165 | return power_supply_create_bat_triggers(psy); | ||
166 | return power_supply_create_gen_triggers(psy); | ||
167 | } | ||
168 | |||
169 | void power_supply_remove_triggers(struct power_supply *psy) | ||
170 | { | ||
171 | if (psy->type == POWER_SUPPLY_TYPE_BATTERY) | ||
172 | power_supply_remove_bat_triggers(psy); | ||
173 | else | ||
174 | power_supply_remove_gen_triggers(psy); | ||
175 | return; | ||
176 | } | ||
diff --git a/drivers/power/power_supply_sysfs.c b/drivers/power/power_supply_sysfs.c new file mode 100644 index 000000000000..c07d4258d347 --- /dev/null +++ b/drivers/power/power_supply_sysfs.c | |||
@@ -0,0 +1,299 @@ | |||
1 | /* | ||
2 | * Sysfs interface for the universal power supply monitor class | ||
3 | * | ||
4 | * Copyright © 2007 David Woodhouse <dwmw2@infradead.org> | ||
5 | * Copyright © 2007 Anton Vorontsov <cbou@mail.ru> | ||
6 | * Copyright © 2004 Szabolcs Gyurko | ||
7 | * Copyright © 2003 Ian Molton <spyro@f2s.com> | ||
8 | * | ||
9 | * Modified: 2004, Oct Szabolcs Gyurko | ||
10 | * | ||
11 | * You may use this code as per GPL version 2 | ||
12 | */ | ||
13 | |||
14 | #include <linux/ctype.h> | ||
15 | #include <linux/power_supply.h> | ||
16 | |||
17 | /* | ||
18 | * This is because the name "current" breaks the device attr macro. | ||
19 | * The "current" word resolves to "(get_current())" so instead of | ||
20 | * "current" "(get_current())" appears in the sysfs. | ||
21 | * | ||
22 | * The source of this definition is the device.h which calls __ATTR | ||
23 | * macro in sysfs.h which calls the __stringify macro. | ||
24 | * | ||
25 | * Only modification that the name is not tried to be resolved | ||
26 | * (as a macro let's say). | ||
27 | */ | ||
28 | |||
29 | #define POWER_SUPPLY_ATTR(_name) \ | ||
30 | { \ | ||
31 | .attr = { .name = #_name, .mode = 0444, .owner = THIS_MODULE }, \ | ||
32 | .show = power_supply_show_property, \ | ||
33 | .store = NULL, \ | ||
34 | } | ||
35 | |||
36 | static struct device_attribute power_supply_attrs[]; | ||
37 | |||
38 | static ssize_t power_supply_show_property(struct device *dev, | ||
39 | struct device_attribute *attr, | ||
40 | char *buf) { | ||
41 | static char *status_text[] = { | ||
42 | "Unknown", "Charging", "Discharging", "Not charging", "Full" | ||
43 | }; | ||
44 | static char *health_text[] = { | ||
45 | "Unknown", "Good", "Overheat", "Dead", "Over voltage", | ||
46 | "Unspecified failure" | ||
47 | }; | ||
48 | static char *technology_text[] = { | ||
49 | "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd" | ||
50 | }; | ||
51 | static char *capacity_level_text[] = { | ||
52 | "Unknown", "Critical", "Low", "Normal", "High", "Full" | ||
53 | }; | ||
54 | ssize_t ret; | ||
55 | struct power_supply *psy = dev_get_drvdata(dev); | ||
56 | const ptrdiff_t off = attr - power_supply_attrs; | ||
57 | union power_supply_propval value; | ||
58 | |||
59 | ret = psy->get_property(psy, off, &value); | ||
60 | |||
61 | if (ret < 0) { | ||
62 | if (ret != -ENODEV) | ||
63 | dev_err(dev, "driver failed to report `%s' property\n", | ||
64 | attr->attr.name); | ||
65 | return ret; | ||
66 | } | ||
67 | |||
68 | if (off == POWER_SUPPLY_PROP_STATUS) | ||
69 | return sprintf(buf, "%s\n", status_text[value.intval]); | ||
70 | else if (off == POWER_SUPPLY_PROP_HEALTH) | ||
71 | return sprintf(buf, "%s\n", health_text[value.intval]); | ||
72 | else if (off == POWER_SUPPLY_PROP_TECHNOLOGY) | ||
73 | return sprintf(buf, "%s\n", technology_text[value.intval]); | ||
74 | else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL) | ||
75 | return sprintf(buf, "%s\n", | ||
76 | capacity_level_text[value.intval]); | ||
77 | else if (off >= POWER_SUPPLY_PROP_MODEL_NAME) | ||
78 | return sprintf(buf, "%s\n", value.strval); | ||
79 | |||
80 | return sprintf(buf, "%d\n", value.intval); | ||
81 | } | ||
82 | |||
83 | /* Must be in the same order as POWER_SUPPLY_PROP_* */ | ||
84 | static struct device_attribute power_supply_attrs[] = { | ||
85 | /* Properties of type `int' */ | ||
86 | POWER_SUPPLY_ATTR(status), | ||
87 | POWER_SUPPLY_ATTR(health), | ||
88 | POWER_SUPPLY_ATTR(present), | ||
89 | POWER_SUPPLY_ATTR(online), | ||
90 | POWER_SUPPLY_ATTR(technology), | ||
91 | POWER_SUPPLY_ATTR(voltage_max_design), | ||
92 | POWER_SUPPLY_ATTR(voltage_min_design), | ||
93 | POWER_SUPPLY_ATTR(voltage_now), | ||
94 | POWER_SUPPLY_ATTR(voltage_avg), | ||
95 | POWER_SUPPLY_ATTR(current_now), | ||
96 | POWER_SUPPLY_ATTR(current_avg), | ||
97 | POWER_SUPPLY_ATTR(charge_full_design), | ||
98 | POWER_SUPPLY_ATTR(charge_empty_design), | ||
99 | POWER_SUPPLY_ATTR(charge_full), | ||
100 | POWER_SUPPLY_ATTR(charge_empty), | ||
101 | POWER_SUPPLY_ATTR(charge_now), | ||
102 | POWER_SUPPLY_ATTR(charge_avg), | ||
103 | POWER_SUPPLY_ATTR(energy_full_design), | ||
104 | POWER_SUPPLY_ATTR(energy_empty_design), | ||
105 | POWER_SUPPLY_ATTR(energy_full), | ||
106 | POWER_SUPPLY_ATTR(energy_empty), | ||
107 | POWER_SUPPLY_ATTR(energy_now), | ||
108 | POWER_SUPPLY_ATTR(energy_avg), | ||
109 | POWER_SUPPLY_ATTR(capacity), | ||
110 | POWER_SUPPLY_ATTR(capacity_level), | ||
111 | POWER_SUPPLY_ATTR(temp), | ||
112 | POWER_SUPPLY_ATTR(temp_ambient), | ||
113 | POWER_SUPPLY_ATTR(time_to_empty_now), | ||
114 | POWER_SUPPLY_ATTR(time_to_empty_avg), | ||
115 | POWER_SUPPLY_ATTR(time_to_full_now), | ||
116 | POWER_SUPPLY_ATTR(time_to_full_avg), | ||
117 | /* Properties of type `const char *' */ | ||
118 | POWER_SUPPLY_ATTR(model_name), | ||
119 | POWER_SUPPLY_ATTR(manufacturer), | ||
120 | }; | ||
121 | |||
122 | static ssize_t power_supply_show_static_attrs(struct device *dev, | ||
123 | struct device_attribute *attr, | ||
124 | char *buf) { | ||
125 | static char *type_text[] = { "Battery", "UPS", "Mains", "USB" }; | ||
126 | struct power_supply *psy = dev_get_drvdata(dev); | ||
127 | |||
128 | return sprintf(buf, "%s\n", type_text[psy->type]); | ||
129 | } | ||
130 | |||
131 | static struct device_attribute power_supply_static_attrs[] = { | ||
132 | __ATTR(type, 0444, power_supply_show_static_attrs, NULL), | ||
133 | }; | ||
134 | |||
135 | int power_supply_create_attrs(struct power_supply *psy) | ||
136 | { | ||
137 | int rc = 0; | ||
138 | int i, j; | ||
139 | |||
140 | for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) { | ||
141 | rc = device_create_file(psy->dev, | ||
142 | &power_supply_static_attrs[i]); | ||
143 | if (rc) | ||
144 | goto statics_failed; | ||
145 | } | ||
146 | |||
147 | for (j = 0; j < psy->num_properties; j++) { | ||
148 | rc = device_create_file(psy->dev, | ||
149 | &power_supply_attrs[psy->properties[j]]); | ||
150 | if (rc) | ||
151 | goto dynamics_failed; | ||
152 | } | ||
153 | |||
154 | goto succeed; | ||
155 | |||
156 | dynamics_failed: | ||
157 | while (j--) | ||
158 | device_remove_file(psy->dev, | ||
159 | &power_supply_attrs[psy->properties[j]]); | ||
160 | statics_failed: | ||
161 | while (i--) | ||
162 | device_remove_file(psy->dev, | ||
163 | &power_supply_static_attrs[psy->properties[i]]); | ||
164 | succeed: | ||
165 | return rc; | ||
166 | } | ||
167 | |||
168 | void power_supply_remove_attrs(struct power_supply *psy) | ||
169 | { | ||
170 | int i; | ||
171 | |||
172 | for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) | ||
173 | device_remove_file(psy->dev, | ||
174 | &power_supply_static_attrs[i]); | ||
175 | |||
176 | for (i = 0; i < psy->num_properties; i++) | ||
177 | device_remove_file(psy->dev, | ||
178 | &power_supply_attrs[psy->properties[i]]); | ||
179 | |||
180 | return; | ||
181 | } | ||
182 | |||
183 | static char *kstruprdup(const char *str, gfp_t gfp) | ||
184 | { | ||
185 | char *ret, *ustr; | ||
186 | |||
187 | ustr = ret = kmalloc(strlen(str) + 1, gfp); | ||
188 | |||
189 | if (!ret) | ||
190 | return NULL; | ||
191 | |||
192 | while (*str) | ||
193 | *ustr++ = toupper(*str++); | ||
194 | |||
195 | *ustr = 0; | ||
196 | |||
197 | return ret; | ||
198 | } | ||
199 | |||
200 | int power_supply_uevent(struct device *dev, char **envp, int num_envp, | ||
201 | char *buffer, int buffer_size) | ||
202 | { | ||
203 | struct power_supply *psy = dev_get_drvdata(dev); | ||
204 | int i = 0, length = 0, ret = 0, j; | ||
205 | char *prop_buf; | ||
206 | char *attrname; | ||
207 | |||
208 | dev_dbg(dev, "uevent\n"); | ||
209 | |||
210 | if (!psy) { | ||
211 | dev_dbg(dev, "No power supply yet\n"); | ||
212 | return ret; | ||
213 | } | ||
214 | |||
215 | dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name); | ||
216 | |||
217 | ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, | ||
218 | &length, "POWER_SUPPLY_NAME=%s", psy->name); | ||
219 | if (ret) | ||
220 | return ret; | ||
221 | |||
222 | prop_buf = (char *)get_zeroed_page(GFP_KERNEL); | ||
223 | if (!prop_buf) | ||
224 | return -ENOMEM; | ||
225 | |||
226 | for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) { | ||
227 | struct device_attribute *attr; | ||
228 | char *line; | ||
229 | |||
230 | attr = &power_supply_static_attrs[j]; | ||
231 | |||
232 | ret = power_supply_show_static_attrs(dev, attr, prop_buf); | ||
233 | if (ret < 0) | ||
234 | goto out; | ||
235 | |||
236 | line = strchr(prop_buf, '\n'); | ||
237 | if (line) | ||
238 | *line = 0; | ||
239 | |||
240 | attrname = kstruprdup(attr->attr.name, GFP_KERNEL); | ||
241 | if (!attrname) { | ||
242 | ret = -ENOMEM; | ||
243 | goto out; | ||
244 | } | ||
245 | |||
246 | dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf); | ||
247 | |||
248 | ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, | ||
249 | &length, "POWER_SUPPLY_%s=%s", | ||
250 | attrname, prop_buf); | ||
251 | kfree(attrname); | ||
252 | if (ret) | ||
253 | goto out; | ||
254 | } | ||
255 | |||
256 | dev_dbg(dev, "%zd dynamic props\n", psy->num_properties); | ||
257 | |||
258 | for (j = 0; j < psy->num_properties; j++) { | ||
259 | struct device_attribute *attr; | ||
260 | char *line; | ||
261 | |||
262 | attr = &power_supply_attrs[psy->properties[j]]; | ||
263 | |||
264 | ret = power_supply_show_property(dev, attr, prop_buf); | ||
265 | if (ret == -ENODEV) { | ||
266 | /* When a battery is absent, we expect -ENODEV. Don't abort; | ||
267 | send the uevent with at least the the PRESENT=0 property */ | ||
268 | ret = 0; | ||
269 | continue; | ||
270 | } | ||
271 | |||
272 | if (ret < 0) | ||
273 | goto out; | ||
274 | |||
275 | line = strchr(prop_buf, '\n'); | ||
276 | if (line) | ||
277 | *line = 0; | ||
278 | |||
279 | attrname = kstruprdup(attr->attr.name, GFP_KERNEL); | ||
280 | if (!attrname) { | ||
281 | ret = -ENOMEM; | ||
282 | goto out; | ||
283 | } | ||
284 | |||
285 | dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf); | ||
286 | |||
287 | ret = add_uevent_var(envp, num_envp, &i, buffer, buffer_size, | ||
288 | &length, "POWER_SUPPLY_%s=%s", | ||
289 | attrname, prop_buf); | ||
290 | kfree(attrname); | ||
291 | if (ret) | ||
292 | goto out; | ||
293 | } | ||
294 | |||
295 | out: | ||
296 | free_page((unsigned long)prop_buf); | ||
297 | |||
298 | return ret; | ||
299 | } | ||