]>
Commit | Line | Data |
---|---|---|
4a11b59d AV |
1 | /* |
2 | * Sysfs interface for the universal power supply monitor class | |
3 | * | |
4 | * Copyright © 2007 David Woodhouse <[email protected]> | |
5 | * Copyright © 2007 Anton Vorontsov <[email protected]> | |
6 | * Copyright © 2004 Szabolcs Gyurko | |
7 | * Copyright © 2003 Ian Molton <[email protected]> | |
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 | ||
25f12141 AB |
17 | #include "power_supply.h" |
18 | ||
4a11b59d AV |
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 | { \ | |
01e8ef11 | 33 | .attr = { .name = #_name, .mode = 0444 }, \ |
4a11b59d AV |
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 | }; | |
ee8076ed AS |
46 | static char *charge_type[] = { |
47 | "Unknown", "N/A", "Trickle", "Fast" | |
48 | }; | |
4a11b59d AV |
49 | static char *health_text[] = { |
50 | "Unknown", "Good", "Overheat", "Dead", "Over voltage", | |
7e386e6e | 51 | "Unspecified failure", "Cold", |
4a11b59d AV |
52 | }; |
53 | static char *technology_text[] = { | |
c7cc930f DB |
54 | "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd", |
55 | "LiMn" | |
4a11b59d | 56 | }; |
b294a290 AS |
57 | static char *capacity_level_text[] = { |
58 | "Unknown", "Critical", "Low", "Normal", "High", "Full" | |
59 | }; | |
4a11b59d AV |
60 | 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) { | |
9d233e8b AV |
68 | if (ret == -ENODATA) |
69 | dev_dbg(dev, "driver has no data for `%s' property\n", | |
70 | attr->attr.name); | |
71 | else if (ret != -ENODEV) | |
4a11b59d AV |
72 | dev_err(dev, "driver failed to report `%s' property\n", |
73 | attr->attr.name); | |
74 | return ret; | |
75 | } | |
76 | ||
77 | if (off == POWER_SUPPLY_PROP_STATUS) | |
78 | return sprintf(buf, "%s\n", status_text[value.intval]); | |
ee8076ed AS |
79 | else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE) |
80 | return sprintf(buf, "%s\n", charge_type[value.intval]); | |
4a11b59d AV |
81 | else if (off == POWER_SUPPLY_PROP_HEALTH) |
82 | return sprintf(buf, "%s\n", health_text[value.intval]); | |
83 | else if (off == POWER_SUPPLY_PROP_TECHNOLOGY) | |
84 | return sprintf(buf, "%s\n", technology_text[value.intval]); | |
b294a290 AS |
85 | else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL) |
86 | return sprintf(buf, "%s\n", capacity_level_text[value.intval]); | |
4a11b59d AV |
87 | else if (off >= POWER_SUPPLY_PROP_MODEL_NAME) |
88 | return sprintf(buf, "%s\n", value.strval); | |
89 | ||
90 | return sprintf(buf, "%d\n", value.intval); | |
91 | } | |
92 | ||
93 | /* Must be in the same order as POWER_SUPPLY_PROP_* */ | |
94 | static struct device_attribute power_supply_attrs[] = { | |
95 | /* Properties of type `int' */ | |
96 | POWER_SUPPLY_ATTR(status), | |
ee8076ed | 97 | POWER_SUPPLY_ATTR(charge_type), |
4a11b59d AV |
98 | POWER_SUPPLY_ATTR(health), |
99 | POWER_SUPPLY_ATTR(present), | |
100 | POWER_SUPPLY_ATTR(online), | |
101 | POWER_SUPPLY_ATTR(technology), | |
c7cc930f DB |
102 | POWER_SUPPLY_ATTR(voltage_max), |
103 | POWER_SUPPLY_ATTR(voltage_min), | |
4a11b59d AV |
104 | POWER_SUPPLY_ATTR(voltage_max_design), |
105 | POWER_SUPPLY_ATTR(voltage_min_design), | |
106 | POWER_SUPPLY_ATTR(voltage_now), | |
107 | POWER_SUPPLY_ATTR(voltage_avg), | |
108 | POWER_SUPPLY_ATTR(current_now), | |
109 | POWER_SUPPLY_ATTR(current_avg), | |
7faa144a AS |
110 | POWER_SUPPLY_ATTR(power_now), |
111 | POWER_SUPPLY_ATTR(power_avg), | |
4a11b59d AV |
112 | POWER_SUPPLY_ATTR(charge_full_design), |
113 | POWER_SUPPLY_ATTR(charge_empty_design), | |
114 | POWER_SUPPLY_ATTR(charge_full), | |
115 | POWER_SUPPLY_ATTR(charge_empty), | |
116 | POWER_SUPPLY_ATTR(charge_now), | |
117 | POWER_SUPPLY_ATTR(charge_avg), | |
8e552c36 | 118 | POWER_SUPPLY_ATTR(charge_counter), |
4a11b59d AV |
119 | POWER_SUPPLY_ATTR(energy_full_design), |
120 | POWER_SUPPLY_ATTR(energy_empty_design), | |
121 | POWER_SUPPLY_ATTR(energy_full), | |
122 | POWER_SUPPLY_ATTR(energy_empty), | |
123 | POWER_SUPPLY_ATTR(energy_now), | |
124 | POWER_SUPPLY_ATTR(energy_avg), | |
125 | POWER_SUPPLY_ATTR(capacity), | |
b294a290 | 126 | POWER_SUPPLY_ATTR(capacity_level), |
4a11b59d AV |
127 | POWER_SUPPLY_ATTR(temp), |
128 | POWER_SUPPLY_ATTR(temp_ambient), | |
129 | POWER_SUPPLY_ATTR(time_to_empty_now), | |
130 | POWER_SUPPLY_ATTR(time_to_empty_avg), | |
131 | POWER_SUPPLY_ATTR(time_to_full_now), | |
132 | POWER_SUPPLY_ATTR(time_to_full_avg), | |
133 | /* Properties of type `const char *' */ | |
134 | POWER_SUPPLY_ATTR(model_name), | |
135 | POWER_SUPPLY_ATTR(manufacturer), | |
7c2670bb | 136 | POWER_SUPPLY_ATTR(serial_number), |
4a11b59d AV |
137 | }; |
138 | ||
139 | static ssize_t power_supply_show_static_attrs(struct device *dev, | |
140 | struct device_attribute *attr, | |
141 | char *buf) { | |
142 | static char *type_text[] = { "Battery", "UPS", "Mains", "USB" }; | |
143 | struct power_supply *psy = dev_get_drvdata(dev); | |
144 | ||
145 | return sprintf(buf, "%s\n", type_text[psy->type]); | |
146 | } | |
147 | ||
148 | static struct device_attribute power_supply_static_attrs[] = { | |
149 | __ATTR(type, 0444, power_supply_show_static_attrs, NULL), | |
150 | }; | |
151 | ||
152 | int power_supply_create_attrs(struct power_supply *psy) | |
153 | { | |
154 | int rc = 0; | |
155 | int i, j; | |
156 | ||
157 | for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) { | |
158 | rc = device_create_file(psy->dev, | |
159 | &power_supply_static_attrs[i]); | |
160 | if (rc) | |
161 | goto statics_failed; | |
162 | } | |
163 | ||
164 | for (j = 0; j < psy->num_properties; j++) { | |
165 | rc = device_create_file(psy->dev, | |
166 | &power_supply_attrs[psy->properties[j]]); | |
167 | if (rc) | |
168 | goto dynamics_failed; | |
169 | } | |
170 | ||
171 | goto succeed; | |
172 | ||
173 | dynamics_failed: | |
174 | while (j--) | |
175 | device_remove_file(psy->dev, | |
176 | &power_supply_attrs[psy->properties[j]]); | |
177 | statics_failed: | |
178 | while (i--) | |
839dc9f1 | 179 | device_remove_file(psy->dev, &power_supply_static_attrs[i]); |
4a11b59d AV |
180 | succeed: |
181 | return rc; | |
182 | } | |
183 | ||
184 | void power_supply_remove_attrs(struct power_supply *psy) | |
185 | { | |
186 | int i; | |
187 | ||
188 | for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) | |
839dc9f1 | 189 | device_remove_file(psy->dev, &power_supply_static_attrs[i]); |
4a11b59d AV |
190 | |
191 | for (i = 0; i < psy->num_properties; i++) | |
192 | device_remove_file(psy->dev, | |
193 | &power_supply_attrs[psy->properties[i]]); | |
4a11b59d AV |
194 | } |
195 | ||
196 | static char *kstruprdup(const char *str, gfp_t gfp) | |
197 | { | |
198 | char *ret, *ustr; | |
199 | ||
200 | ustr = ret = kmalloc(strlen(str) + 1, gfp); | |
201 | ||
202 | if (!ret) | |
203 | return NULL; | |
204 | ||
205 | while (*str) | |
206 | *ustr++ = toupper(*str++); | |
207 | ||
208 | *ustr = 0; | |
209 | ||
210 | return ret; | |
211 | } | |
212 | ||
7eff2e7a | 213 | int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env) |
4a11b59d AV |
214 | { |
215 | struct power_supply *psy = dev_get_drvdata(dev); | |
7eff2e7a | 216 | int ret = 0, j; |
4a11b59d AV |
217 | char *prop_buf; |
218 | char *attrname; | |
219 | ||
220 | dev_dbg(dev, "uevent\n"); | |
221 | ||
56fa18e8 | 222 | if (!psy || !psy->dev) { |
4a11b59d AV |
223 | dev_dbg(dev, "No power supply yet\n"); |
224 | return ret; | |
225 | } | |
226 | ||
227 | dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name); | |
228 | ||
7eff2e7a | 229 | ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name); |
4a11b59d AV |
230 | if (ret) |
231 | return ret; | |
232 | ||
233 | prop_buf = (char *)get_zeroed_page(GFP_KERNEL); | |
234 | if (!prop_buf) | |
235 | return -ENOMEM; | |
236 | ||
237 | for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) { | |
238 | struct device_attribute *attr; | |
239 | char *line; | |
240 | ||
241 | attr = &power_supply_static_attrs[j]; | |
242 | ||
243 | ret = power_supply_show_static_attrs(dev, attr, prop_buf); | |
244 | if (ret < 0) | |
245 | goto out; | |
246 | ||
247 | line = strchr(prop_buf, '\n'); | |
248 | if (line) | |
249 | *line = 0; | |
250 | ||
251 | attrname = kstruprdup(attr->attr.name, GFP_KERNEL); | |
252 | if (!attrname) { | |
253 | ret = -ENOMEM; | |
254 | goto out; | |
255 | } | |
256 | ||
257 | dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf); | |
258 | ||
7eff2e7a | 259 | ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); |
4a11b59d AV |
260 | kfree(attrname); |
261 | if (ret) | |
262 | goto out; | |
263 | } | |
264 | ||
265 | dev_dbg(dev, "%zd dynamic props\n", psy->num_properties); | |
266 | ||
267 | for (j = 0; j < psy->num_properties; j++) { | |
268 | struct device_attribute *attr; | |
269 | char *line; | |
270 | ||
271 | attr = &power_supply_attrs[psy->properties[j]]; | |
272 | ||
273 | ret = power_supply_show_property(dev, attr, prop_buf); | |
274 | if (ret == -ENODEV) { | |
275 | /* When a battery is absent, we expect -ENODEV. Don't abort; | |
276 | send the uevent with at least the the PRESENT=0 property */ | |
277 | ret = 0; | |
278 | continue; | |
279 | } | |
280 | ||
281 | if (ret < 0) | |
282 | goto out; | |
283 | ||
284 | line = strchr(prop_buf, '\n'); | |
285 | if (line) | |
286 | *line = 0; | |
287 | ||
288 | attrname = kstruprdup(attr->attr.name, GFP_KERNEL); | |
289 | if (!attrname) { | |
290 | ret = -ENOMEM; | |
291 | goto out; | |
292 | } | |
293 | ||
294 | dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf); | |
295 | ||
7eff2e7a | 296 | ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); |
4a11b59d AV |
297 | kfree(attrname); |
298 | if (ret) | |
299 | goto out; | |
300 | } | |
301 | ||
302 | out: | |
303 | free_page((unsigned long)prop_buf); | |
304 | ||
305 | return ret; | |
306 | } |