blob: 08144393d64b72e7b2a54d44929a6d47cbf18036 [file] [log] [blame]
Anton Vorontsov4a11b592007-05-04 00:27:45 +04001/*
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
Adrian Bunk25f121412007-11-26 00:25:45 +030017#include "power_supply.h"
18
Anton Vorontsov4a11b592007-05-04 00:27:45 +040019/*
20 * This is because the name "current" breaks the device attr macro.
21 * The "current" word resolves to "(get_current())" so instead of
22 * "current" "(get_current())" appears in the sysfs.
23 *
24 * The source of this definition is the device.h which calls __ATTR
25 * macro in sysfs.h which calls the __stringify macro.
26 *
27 * Only modification that the name is not tried to be resolved
28 * (as a macro let's say).
29 */
30
31#define POWER_SUPPLY_ATTR(_name) \
32{ \
Parag Warudkar01e8ef12008-10-18 20:28:50 -070033 .attr = { .name = #_name, .mode = 0444 }, \
Anton Vorontsov4a11b592007-05-04 00:27:45 +040034 .show = power_supply_show_property, \
35 .store = NULL, \
36}
37
38static struct device_attribute power_supply_attrs[];
39
40static ssize_t power_supply_show_property(struct device *dev,
41 struct device_attribute *attr,
42 char *buf) {
43 static char *status_text[] = {
44 "Unknown", "Charging", "Discharging", "Not charging", "Full"
45 };
Andres Salomonee8076e2009-07-02 09:45:18 -040046 static char *charge_type[] = {
47 "Unknown", "N/A", "Trickle", "Fast"
48 };
Anton Vorontsov4a11b592007-05-04 00:27:45 +040049 static char *health_text[] = {
50 "Unknown", "Good", "Overheat", "Dead", "Over voltage",
Mark Brown7e386e62008-11-30 22:43:21 +010051 "Unspecified failure", "Cold",
Anton Vorontsov4a11b592007-05-04 00:27:45 +040052 };
53 static char *technology_text[] = {
Dmitry Baryshkovc7cc9302008-01-07 04:12:41 +030054 "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
55 "LiMn"
Anton Vorontsov4a11b592007-05-04 00:27:45 +040056 };
Andres Salomonb294a292009-06-30 02:13:01 -040057 static char *capacity_level_text[] = {
58 "Unknown", "Critical", "Low", "Normal", "High", "Full"
59 };
Anton Vorontsov4a11b592007-05-04 00:27:45 +040060 ssize_t ret;
61 struct power_supply *psy = dev_get_drvdata(dev);
62 const ptrdiff_t off = attr - power_supply_attrs;
63 union power_supply_propval value;
64
65 ret = psy->get_property(psy, off, &value);
66
67 if (ret < 0) {
68 if (ret != -ENODEV)
69 dev_err(dev, "driver failed to report `%s' property\n",
70 attr->attr.name);
71 return ret;
72 }
73
74 if (off == POWER_SUPPLY_PROP_STATUS)
75 return sprintf(buf, "%s\n", status_text[value.intval]);
Andres Salomonee8076e2009-07-02 09:45:18 -040076 else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
77 return sprintf(buf, "%s\n", charge_type[value.intval]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040078 else if (off == POWER_SUPPLY_PROP_HEALTH)
79 return sprintf(buf, "%s\n", health_text[value.intval]);
80 else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
81 return sprintf(buf, "%s\n", technology_text[value.intval]);
Andres Salomonb294a292009-06-30 02:13:01 -040082 else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
83 return sprintf(buf, "%s\n", capacity_level_text[value.intval]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040084 else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
85 return sprintf(buf, "%s\n", value.strval);
86
87 return sprintf(buf, "%d\n", value.intval);
88}
89
90/* Must be in the same order as POWER_SUPPLY_PROP_* */
91static struct device_attribute power_supply_attrs[] = {
92 /* Properties of type `int' */
93 POWER_SUPPLY_ATTR(status),
Andres Salomonee8076e2009-07-02 09:45:18 -040094 POWER_SUPPLY_ATTR(charge_type),
Anton Vorontsov4a11b592007-05-04 00:27:45 +040095 POWER_SUPPLY_ATTR(health),
96 POWER_SUPPLY_ATTR(present),
97 POWER_SUPPLY_ATTR(online),
98 POWER_SUPPLY_ATTR(technology),
Dmitry Baryshkovc7cc9302008-01-07 04:12:41 +030099 POWER_SUPPLY_ATTR(voltage_max),
100 POWER_SUPPLY_ATTR(voltage_min),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400101 POWER_SUPPLY_ATTR(voltage_max_design),
102 POWER_SUPPLY_ATTR(voltage_min_design),
103 POWER_SUPPLY_ATTR(voltage_now),
104 POWER_SUPPLY_ATTR(voltage_avg),
105 POWER_SUPPLY_ATTR(current_now),
106 POWER_SUPPLY_ATTR(current_avg),
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -0400107 POWER_SUPPLY_ATTR(power_now),
108 POWER_SUPPLY_ATTR(power_avg),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400109 POWER_SUPPLY_ATTR(charge_full_design),
110 POWER_SUPPLY_ATTR(charge_empty_design),
111 POWER_SUPPLY_ATTR(charge_full),
112 POWER_SUPPLY_ATTR(charge_empty),
113 POWER_SUPPLY_ATTR(charge_now),
114 POWER_SUPPLY_ATTR(charge_avg),
Andres Salomon8e552c32008-05-12 21:46:29 -0400115 POWER_SUPPLY_ATTR(charge_counter),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400116 POWER_SUPPLY_ATTR(energy_full_design),
117 POWER_SUPPLY_ATTR(energy_empty_design),
118 POWER_SUPPLY_ATTR(energy_full),
119 POWER_SUPPLY_ATTR(energy_empty),
120 POWER_SUPPLY_ATTR(energy_now),
121 POWER_SUPPLY_ATTR(energy_avg),
122 POWER_SUPPLY_ATTR(capacity),
Andres Salomonb294a292009-06-30 02:13:01 -0400123 POWER_SUPPLY_ATTR(capacity_level),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400124 POWER_SUPPLY_ATTR(temp),
125 POWER_SUPPLY_ATTR(temp_ambient),
126 POWER_SUPPLY_ATTR(time_to_empty_now),
127 POWER_SUPPLY_ATTR(time_to_empty_avg),
128 POWER_SUPPLY_ATTR(time_to_full_now),
129 POWER_SUPPLY_ATTR(time_to_full_avg),
130 /* Properties of type `const char *' */
131 POWER_SUPPLY_ATTR(model_name),
132 POWER_SUPPLY_ATTR(manufacturer),
maximilian attems7c2670b2008-01-22 18:46:50 +0100133 POWER_SUPPLY_ATTR(serial_number),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400134};
135
136static ssize_t power_supply_show_static_attrs(struct device *dev,
137 struct device_attribute *attr,
138 char *buf) {
139 static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
140 struct power_supply *psy = dev_get_drvdata(dev);
141
142 return sprintf(buf, "%s\n", type_text[psy->type]);
143}
144
145static struct device_attribute power_supply_static_attrs[] = {
146 __ATTR(type, 0444, power_supply_show_static_attrs, NULL),
147};
148
149int power_supply_create_attrs(struct power_supply *psy)
150{
151 int rc = 0;
152 int i, j;
153
154 for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
155 rc = device_create_file(psy->dev,
156 &power_supply_static_attrs[i]);
157 if (rc)
158 goto statics_failed;
159 }
160
161 for (j = 0; j < psy->num_properties; j++) {
162 rc = device_create_file(psy->dev,
163 &power_supply_attrs[psy->properties[j]]);
164 if (rc)
165 goto dynamics_failed;
166 }
167
168 goto succeed;
169
170dynamics_failed:
171 while (j--)
172 device_remove_file(psy->dev,
173 &power_supply_attrs[psy->properties[j]]);
174statics_failed:
175 while (i--)
Andres Salomon839dc9f2007-12-12 14:12:59 -0500176 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400177succeed:
178 return rc;
179}
180
181void power_supply_remove_attrs(struct power_supply *psy)
182{
183 int i;
184
185 for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
Andres Salomon839dc9f2007-12-12 14:12:59 -0500186 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400187
188 for (i = 0; i < psy->num_properties; i++)
189 device_remove_file(psy->dev,
190 &power_supply_attrs[psy->properties[i]]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400191}
192
193static char *kstruprdup(const char *str, gfp_t gfp)
194{
195 char *ret, *ustr;
196
197 ustr = ret = kmalloc(strlen(str) + 1, gfp);
198
199 if (!ret)
200 return NULL;
201
202 while (*str)
203 *ustr++ = toupper(*str++);
204
205 *ustr = 0;
206
207 return ret;
208}
209
Kay Sievers7eff2e72007-08-14 15:15:12 +0200210int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400211{
212 struct power_supply *psy = dev_get_drvdata(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200213 int ret = 0, j;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400214 char *prop_buf;
215 char *attrname;
216
217 dev_dbg(dev, "uevent\n");
218
Dmitry Baryshkov56fa18e2008-06-08 19:43:42 +0400219 if (!psy || !psy->dev) {
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400220 dev_dbg(dev, "No power supply yet\n");
221 return ret;
222 }
223
224 dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
225
Kay Sievers7eff2e72007-08-14 15:15:12 +0200226 ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400227 if (ret)
228 return ret;
229
230 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
231 if (!prop_buf)
232 return -ENOMEM;
233
234 for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
235 struct device_attribute *attr;
236 char *line;
237
238 attr = &power_supply_static_attrs[j];
239
240 ret = power_supply_show_static_attrs(dev, attr, prop_buf);
241 if (ret < 0)
242 goto out;
243
244 line = strchr(prop_buf, '\n');
245 if (line)
246 *line = 0;
247
248 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
249 if (!attrname) {
250 ret = -ENOMEM;
251 goto out;
252 }
253
254 dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
255
Kay Sievers7eff2e72007-08-14 15:15:12 +0200256 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400257 kfree(attrname);
258 if (ret)
259 goto out;
260 }
261
262 dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
263
264 for (j = 0; j < psy->num_properties; j++) {
265 struct device_attribute *attr;
266 char *line;
267
268 attr = &power_supply_attrs[psy->properties[j]];
269
270 ret = power_supply_show_property(dev, attr, prop_buf);
271 if (ret == -ENODEV) {
272 /* When a battery is absent, we expect -ENODEV. Don't abort;
273 send the uevent with at least the the PRESENT=0 property */
274 ret = 0;
275 continue;
276 }
277
278 if (ret < 0)
279 goto out;
280
281 line = strchr(prop_buf, '\n');
282 if (line)
283 *line = 0;
284
285 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
286 if (!attrname) {
287 ret = -ENOMEM;
288 goto out;
289 }
290
291 dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
292
Kay Sievers7eff2e72007-08-14 15:15:12 +0200293 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400294 kfree(attrname);
295 if (ret)
296 goto out;
297 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400298
299out:
300 free_page((unsigned long)prop_buf);
301
302 return ret;
303}