blob: da73591017f903126f4cf59372938e91db9ea7ce [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 Bunk25f12142007-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 };
46 static char *health_text[] = {
47 "Unknown", "Good", "Overheat", "Dead", "Over voltage",
Mark Brown7e386e62008-11-30 22:43:21 +010048 "Unspecified failure", "Cold",
Anton Vorontsov4a11b592007-05-04 00:27:45 +040049 };
50 static char *technology_text[] = {
Dmitry Baryshkovc7cc9302008-01-07 04:12:41 +030051 "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
52 "LiMn"
Anton Vorontsov4a11b592007-05-04 00:27:45 +040053 };
Anton Vorontsov4a11b592007-05-04 00:27:45 +040054 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]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +040074 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_* */
81static 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),
Dmitry Baryshkovc7cc9302008-01-07 04:12:41 +030088 POWER_SUPPLY_ATTR(voltage_max),
89 POWER_SUPPLY_ATTR(voltage_min),
Anton Vorontsov4a11b592007-05-04 00:27:45 +040090 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),
Alexey Starikovskiy7faa1442009-03-27 22:23:52 -040096 POWER_SUPPLY_ATTR(power_now),
97 POWER_SUPPLY_ATTR(power_avg),
Anton Vorontsov4a11b592007-05-04 00:27:45 +040098 POWER_SUPPLY_ATTR(charge_full_design),
99 POWER_SUPPLY_ATTR(charge_empty_design),
100 POWER_SUPPLY_ATTR(charge_full),
101 POWER_SUPPLY_ATTR(charge_empty),
102 POWER_SUPPLY_ATTR(charge_now),
103 POWER_SUPPLY_ATTR(charge_avg),
Andres Salomon8e552c32008-05-12 21:46:29 -0400104 POWER_SUPPLY_ATTR(charge_counter),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400105 POWER_SUPPLY_ATTR(energy_full_design),
106 POWER_SUPPLY_ATTR(energy_empty_design),
107 POWER_SUPPLY_ATTR(energy_full),
108 POWER_SUPPLY_ATTR(energy_empty),
109 POWER_SUPPLY_ATTR(energy_now),
110 POWER_SUPPLY_ATTR(energy_avg),
111 POWER_SUPPLY_ATTR(capacity),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400112 POWER_SUPPLY_ATTR(temp),
113 POWER_SUPPLY_ATTR(temp_ambient),
114 POWER_SUPPLY_ATTR(time_to_empty_now),
115 POWER_SUPPLY_ATTR(time_to_empty_avg),
116 POWER_SUPPLY_ATTR(time_to_full_now),
117 POWER_SUPPLY_ATTR(time_to_full_avg),
118 /* Properties of type `const char *' */
119 POWER_SUPPLY_ATTR(model_name),
120 POWER_SUPPLY_ATTR(manufacturer),
maximilian attems7c2670b2008-01-22 18:46:50 +0100121 POWER_SUPPLY_ATTR(serial_number),
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400122};
123
124static ssize_t power_supply_show_static_attrs(struct device *dev,
125 struct device_attribute *attr,
126 char *buf) {
127 static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
128 struct power_supply *psy = dev_get_drvdata(dev);
129
130 return sprintf(buf, "%s\n", type_text[psy->type]);
131}
132
133static struct device_attribute power_supply_static_attrs[] = {
134 __ATTR(type, 0444, power_supply_show_static_attrs, NULL),
135};
136
137int power_supply_create_attrs(struct power_supply *psy)
138{
139 int rc = 0;
140 int i, j;
141
142 for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
143 rc = device_create_file(psy->dev,
144 &power_supply_static_attrs[i]);
145 if (rc)
146 goto statics_failed;
147 }
148
149 for (j = 0; j < psy->num_properties; j++) {
150 rc = device_create_file(psy->dev,
151 &power_supply_attrs[psy->properties[j]]);
152 if (rc)
153 goto dynamics_failed;
154 }
155
156 goto succeed;
157
158dynamics_failed:
159 while (j--)
160 device_remove_file(psy->dev,
161 &power_supply_attrs[psy->properties[j]]);
162statics_failed:
163 while (i--)
Andres Salomon839dc9f2007-12-12 14:12:59 -0500164 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400165succeed:
166 return rc;
167}
168
169void power_supply_remove_attrs(struct power_supply *psy)
170{
171 int i;
172
173 for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
Andres Salomon839dc9f2007-12-12 14:12:59 -0500174 device_remove_file(psy->dev, &power_supply_static_attrs[i]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400175
176 for (i = 0; i < psy->num_properties; i++)
177 device_remove_file(psy->dev,
178 &power_supply_attrs[psy->properties[i]]);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400179}
180
181static char *kstruprdup(const char *str, gfp_t gfp)
182{
183 char *ret, *ustr;
184
185 ustr = ret = kmalloc(strlen(str) + 1, gfp);
186
187 if (!ret)
188 return NULL;
189
190 while (*str)
191 *ustr++ = toupper(*str++);
192
193 *ustr = 0;
194
195 return ret;
196}
197
Kay Sievers7eff2e72007-08-14 15:15:12 +0200198int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400199{
200 struct power_supply *psy = dev_get_drvdata(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200201 int ret = 0, j;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400202 char *prop_buf;
203 char *attrname;
204
205 dev_dbg(dev, "uevent\n");
206
Dmitry Baryshkov56fa18e2008-06-08 19:43:42 +0400207 if (!psy || !psy->dev) {
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400208 dev_dbg(dev, "No power supply yet\n");
209 return ret;
210 }
211
212 dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
213
Kay Sievers7eff2e72007-08-14 15:15:12 +0200214 ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400215 if (ret)
216 return ret;
217
218 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
219 if (!prop_buf)
220 return -ENOMEM;
221
222 for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
223 struct device_attribute *attr;
224 char *line;
225
226 attr = &power_supply_static_attrs[j];
227
228 ret = power_supply_show_static_attrs(dev, attr, prop_buf);
229 if (ret < 0)
230 goto out;
231
232 line = strchr(prop_buf, '\n');
233 if (line)
234 *line = 0;
235
236 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
237 if (!attrname) {
238 ret = -ENOMEM;
239 goto out;
240 }
241
242 dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
243
Kay Sievers7eff2e72007-08-14 15:15:12 +0200244 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400245 kfree(attrname);
246 if (ret)
247 goto out;
248 }
249
250 dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
251
252 for (j = 0; j < psy->num_properties; j++) {
253 struct device_attribute *attr;
254 char *line;
255
256 attr = &power_supply_attrs[psy->properties[j]];
257
258 ret = power_supply_show_property(dev, attr, prop_buf);
259 if (ret == -ENODEV) {
260 /* When a battery is absent, we expect -ENODEV. Don't abort;
261 send the uevent with at least the the PRESENT=0 property */
262 ret = 0;
263 continue;
264 }
265
266 if (ret < 0)
267 goto out;
268
269 line = strchr(prop_buf, '\n');
270 if (line)
271 *line = 0;
272
273 attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
274 if (!attrname) {
275 ret = -ENOMEM;
276 goto out;
277 }
278
279 dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
280
Kay Sievers7eff2e72007-08-14 15:15:12 +0200281 ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400282 kfree(attrname);
283 if (ret)
284 goto out;
285 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400286
287out:
288 free_page((unsigned long)prop_buf);
289
290 return ret;
291}