Merge master.kernel.org:/pub/scm/linux/kernel/git/torvalds/linux-2.6 into next
[linux-2.6] / drivers / power / power_supply_sysfs.c
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 #include "power_supply.h"
18
19 /*
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 {                                                                       \
33         .attr = { .name = #_name, .mode = 0444, .owner = THIS_MODULE }, \
34         .show = power_supply_show_property,                             \
35         .store = NULL,                                                  \
36 }
37
38 static struct device_attribute power_supply_attrs[];
39
40 static 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         };
46         static char *health_text[] = {
47                 "Unknown", "Good", "Overheat", "Dead", "Over voltage",
48                 "Unspecified failure"
49         };
50         static char *technology_text[] = {
51                 "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
52                 "LiMn"
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_MODEL_NAME)
75                 return sprintf(buf, "%s\n", value.strval);
76
77         return sprintf(buf, "%d\n", value.intval);
78 }
79
80 /* Must be in the same order as POWER_SUPPLY_PROP_* */
81 static struct device_attribute power_supply_attrs[] = {
82         /* Properties of type `int' */
83         POWER_SUPPLY_ATTR(status),
84         POWER_SUPPLY_ATTR(health),
85         POWER_SUPPLY_ATTR(present),
86         POWER_SUPPLY_ATTR(online),
87         POWER_SUPPLY_ATTR(technology),
88         POWER_SUPPLY_ATTR(voltage_max),
89         POWER_SUPPLY_ATTR(voltage_min),
90         POWER_SUPPLY_ATTR(voltage_max_design),
91         POWER_SUPPLY_ATTR(voltage_min_design),
92         POWER_SUPPLY_ATTR(voltage_now),
93         POWER_SUPPLY_ATTR(voltage_avg),
94         POWER_SUPPLY_ATTR(current_now),
95         POWER_SUPPLY_ATTR(current_avg),
96         POWER_SUPPLY_ATTR(charge_full_design),
97         POWER_SUPPLY_ATTR(charge_empty_design),
98         POWER_SUPPLY_ATTR(charge_full),
99         POWER_SUPPLY_ATTR(charge_empty),
100         POWER_SUPPLY_ATTR(charge_now),
101         POWER_SUPPLY_ATTR(charge_avg),
102         POWER_SUPPLY_ATTR(energy_full_design),
103         POWER_SUPPLY_ATTR(energy_empty_design),
104         POWER_SUPPLY_ATTR(energy_full),
105         POWER_SUPPLY_ATTR(energy_empty),
106         POWER_SUPPLY_ATTR(energy_now),
107         POWER_SUPPLY_ATTR(energy_avg),
108         POWER_SUPPLY_ATTR(capacity),
109         POWER_SUPPLY_ATTR(temp),
110         POWER_SUPPLY_ATTR(temp_ambient),
111         POWER_SUPPLY_ATTR(time_to_empty_now),
112         POWER_SUPPLY_ATTR(time_to_empty_avg),
113         POWER_SUPPLY_ATTR(time_to_full_now),
114         POWER_SUPPLY_ATTR(time_to_full_avg),
115         /* Properties of type `const char *' */
116         POWER_SUPPLY_ATTR(model_name),
117         POWER_SUPPLY_ATTR(manufacturer),
118         POWER_SUPPLY_ATTR(serial_number),
119 };
120
121 static ssize_t power_supply_show_static_attrs(struct device *dev,
122                                               struct device_attribute *attr,
123                                               char *buf) {
124         static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
125         struct power_supply *psy = dev_get_drvdata(dev);
126
127         return sprintf(buf, "%s\n", type_text[psy->type]);
128 }
129
130 static struct device_attribute power_supply_static_attrs[] = {
131         __ATTR(type, 0444, power_supply_show_static_attrs, NULL),
132 };
133
134 int power_supply_create_attrs(struct power_supply *psy)
135 {
136         int rc = 0;
137         int i, j;
138
139         for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
140                 rc = device_create_file(psy->dev,
141                             &power_supply_static_attrs[i]);
142                 if (rc)
143                         goto statics_failed;
144         }
145
146         for (j = 0; j < psy->num_properties; j++) {
147                 rc = device_create_file(psy->dev,
148                             &power_supply_attrs[psy->properties[j]]);
149                 if (rc)
150                         goto dynamics_failed;
151         }
152
153         goto succeed;
154
155 dynamics_failed:
156         while (j--)
157                 device_remove_file(psy->dev,
158                            &power_supply_attrs[psy->properties[j]]);
159 statics_failed:
160         while (i--)
161                 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
162 succeed:
163         return rc;
164 }
165
166 void power_supply_remove_attrs(struct power_supply *psy)
167 {
168         int i;
169
170         for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
171                 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
172
173         for (i = 0; i < psy->num_properties; i++)
174                 device_remove_file(psy->dev,
175                             &power_supply_attrs[psy->properties[i]]);
176 }
177
178 static char *kstruprdup(const char *str, gfp_t gfp)
179 {
180         char *ret, *ustr;
181
182         ustr = ret = kmalloc(strlen(str) + 1, gfp);
183
184         if (!ret)
185                 return NULL;
186
187         while (*str)
188                 *ustr++ = toupper(*str++);
189
190         *ustr = 0;
191
192         return ret;
193 }
194
195 int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
196 {
197         struct power_supply *psy = dev_get_drvdata(dev);
198         int ret = 0, j;
199         char *prop_buf;
200         char *attrname;
201
202         dev_dbg(dev, "uevent\n");
203
204         if (!psy || !psy->dev) {
205                 dev_dbg(dev, "No power supply yet\n");
206                 return ret;
207         }
208
209         dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
210
211         ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
212         if (ret)
213                 return ret;
214
215         prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
216         if (!prop_buf)
217                 return -ENOMEM;
218
219         for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
220                 struct device_attribute *attr;
221                 char *line;
222
223                 attr = &power_supply_static_attrs[j];
224
225                 ret = power_supply_show_static_attrs(dev, attr, prop_buf);
226                 if (ret < 0)
227                         goto out;
228
229                 line = strchr(prop_buf, '\n');
230                 if (line)
231                         *line = 0;
232
233                 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
234                 if (!attrname) {
235                         ret = -ENOMEM;
236                         goto out;
237                 }
238
239                 dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
240
241                 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
242                 kfree(attrname);
243                 if (ret)
244                         goto out;
245         }
246
247         dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
248
249         for (j = 0; j < psy->num_properties; j++) {
250                 struct device_attribute *attr;
251                 char *line;
252
253                 attr = &power_supply_attrs[psy->properties[j]];
254
255                 ret = power_supply_show_property(dev, attr, prop_buf);
256                 if (ret == -ENODEV) {
257                         /* When a battery is absent, we expect -ENODEV. Don't abort;
258                            send the uevent with at least the the PRESENT=0 property */
259                         ret = 0;
260                         continue;
261                 }
262
263                 if (ret < 0)
264                         goto out;
265
266                 line = strchr(prop_buf, '\n');
267                 if (line)
268                         *line = 0;
269
270                 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
271                 if (!attrname) {
272                         ret = -ENOMEM;
273                         goto out;
274                 }
275
276                 dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
277
278                 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
279                 kfree(attrname);
280                 if (ret)
281                         goto out;
282         }
283
284 out:
285         free_page((unsigned long)prop_buf);
286
287         return ret;
288 }