Commit | Line | Data |
---|---|---|
4a11b59d AV |
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 | ||
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 | { \ | |
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 | }; | |
4a11b59d AV |
53 | ssize_t ret; |
54 | struct power_supply *psy = dev_get_drvdata(dev); | |
55 | const ptrdiff_t off = attr - power_supply_attrs; | |
56 | union power_supply_propval value; | |
57 | ||
58 | ret = psy->get_property(psy, off, &value); | |
59 | ||
60 | if (ret < 0) { | |
61 | if (ret != -ENODEV) | |
62 | dev_err(dev, "driver failed to report `%s' property\n", | |
63 | attr->attr.name); | |
64 | return ret; | |
65 | } | |
66 | ||
67 | if (off == POWER_SUPPLY_PROP_STATUS) | |
68 | return sprintf(buf, "%s\n", status_text[value.intval]); | |
69 | else if (off == POWER_SUPPLY_PROP_HEALTH) | |
70 | return sprintf(buf, "%s\n", health_text[value.intval]); | |
71 | else if (off == POWER_SUPPLY_PROP_TECHNOLOGY) | |
72 | return sprintf(buf, "%s\n", technology_text[value.intval]); | |
4a11b59d AV |
73 | else if (off >= POWER_SUPPLY_PROP_MODEL_NAME) |
74 | return sprintf(buf, "%s\n", value.strval); | |
75 | ||
76 | return sprintf(buf, "%d\n", value.intval); | |
77 | } | |
78 | ||
79 | /* Must be in the same order as POWER_SUPPLY_PROP_* */ | |
80 | static struct device_attribute power_supply_attrs[] = { | |
81 | /* Properties of type `int' */ | |
82 | POWER_SUPPLY_ATTR(status), | |
83 | POWER_SUPPLY_ATTR(health), | |
84 | POWER_SUPPLY_ATTR(present), | |
85 | POWER_SUPPLY_ATTR(online), | |
86 | POWER_SUPPLY_ATTR(technology), | |
87 | POWER_SUPPLY_ATTR(voltage_max_design), | |
88 | POWER_SUPPLY_ATTR(voltage_min_design), | |
89 | POWER_SUPPLY_ATTR(voltage_now), | |
90 | POWER_SUPPLY_ATTR(voltage_avg), | |
91 | POWER_SUPPLY_ATTR(current_now), | |
92 | POWER_SUPPLY_ATTR(current_avg), | |
93 | POWER_SUPPLY_ATTR(charge_full_design), | |
94 | POWER_SUPPLY_ATTR(charge_empty_design), | |
95 | POWER_SUPPLY_ATTR(charge_full), | |
96 | POWER_SUPPLY_ATTR(charge_empty), | |
97 | POWER_SUPPLY_ATTR(charge_now), | |
98 | POWER_SUPPLY_ATTR(charge_avg), | |
99 | POWER_SUPPLY_ATTR(energy_full_design), | |
100 | POWER_SUPPLY_ATTR(energy_empty_design), | |
101 | POWER_SUPPLY_ATTR(energy_full), | |
102 | POWER_SUPPLY_ATTR(energy_empty), | |
103 | POWER_SUPPLY_ATTR(energy_now), | |
104 | POWER_SUPPLY_ATTR(energy_avg), | |
105 | POWER_SUPPLY_ATTR(capacity), | |
106 | POWER_SUPPLY_ATTR(capacity_level), | |
107 | POWER_SUPPLY_ATTR(temp), | |
108 | POWER_SUPPLY_ATTR(temp_ambient), | |
109 | POWER_SUPPLY_ATTR(time_to_empty_now), | |
110 | POWER_SUPPLY_ATTR(time_to_empty_avg), | |
111 | POWER_SUPPLY_ATTR(time_to_full_now), | |
112 | POWER_SUPPLY_ATTR(time_to_full_avg), | |
113 | /* Properties of type `const char *' */ | |
114 | POWER_SUPPLY_ATTR(model_name), | |
115 | POWER_SUPPLY_ATTR(manufacturer), | |
116 | }; | |
117 | ||
118 | static ssize_t power_supply_show_static_attrs(struct device *dev, | |
119 | struct device_attribute *attr, | |
120 | char *buf) { | |
121 | static char *type_text[] = { "Battery", "UPS", "Mains", "USB" }; | |
122 | struct power_supply *psy = dev_get_drvdata(dev); | |
123 | ||
124 | return sprintf(buf, "%s\n", type_text[psy->type]); | |
125 | } | |
126 | ||
127 | static struct device_attribute power_supply_static_attrs[] = { | |
128 | __ATTR(type, 0444, power_supply_show_static_attrs, NULL), | |
129 | }; | |
130 | ||
131 | int power_supply_create_attrs(struct power_supply *psy) | |
132 | { | |
133 | int rc = 0; | |
134 | int i, j; | |
135 | ||
136 | for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) { | |
137 | rc = device_create_file(psy->dev, | |
138 | &power_supply_static_attrs[i]); | |
139 | if (rc) | |
140 | goto statics_failed; | |
141 | } | |
142 | ||
143 | for (j = 0; j < psy->num_properties; j++) { | |
144 | rc = device_create_file(psy->dev, | |
145 | &power_supply_attrs[psy->properties[j]]); | |
146 | if (rc) | |
147 | goto dynamics_failed; | |
148 | } | |
149 | ||
150 | goto succeed; | |
151 | ||
152 | dynamics_failed: | |
153 | while (j--) | |
154 | device_remove_file(psy->dev, | |
155 | &power_supply_attrs[psy->properties[j]]); | |
156 | statics_failed: | |
157 | while (i--) | |
839dc9f1 | 158 | device_remove_file(psy->dev, &power_supply_static_attrs[i]); |
4a11b59d AV |
159 | succeed: |
160 | return rc; | |
161 | } | |
162 | ||
163 | void power_supply_remove_attrs(struct power_supply *psy) | |
164 | { | |
165 | int i; | |
166 | ||
167 | for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) | |
839dc9f1 | 168 | device_remove_file(psy->dev, &power_supply_static_attrs[i]); |
4a11b59d AV |
169 | |
170 | for (i = 0; i < psy->num_properties; i++) | |
171 | device_remove_file(psy->dev, | |
172 | &power_supply_attrs[psy->properties[i]]); | |
4a11b59d AV |
173 | } |
174 | ||
175 | static char *kstruprdup(const char *str, gfp_t gfp) | |
176 | { | |
177 | char *ret, *ustr; | |
178 | ||
179 | ustr = ret = kmalloc(strlen(str) + 1, gfp); | |
180 | ||
181 | if (!ret) | |
182 | return NULL; | |
183 | ||
184 | while (*str) | |
185 | *ustr++ = toupper(*str++); | |
186 | ||
187 | *ustr = 0; | |
188 | ||
189 | return ret; | |
190 | } | |
191 | ||
7eff2e7a | 192 | int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env) |
4a11b59d AV |
193 | { |
194 | struct power_supply *psy = dev_get_drvdata(dev); | |
7eff2e7a | 195 | int ret = 0, j; |
4a11b59d AV |
196 | char *prop_buf; |
197 | char *attrname; | |
198 | ||
199 | dev_dbg(dev, "uevent\n"); | |
200 | ||
201 | if (!psy) { | |
202 | dev_dbg(dev, "No power supply yet\n"); | |
203 | return ret; | |
204 | } | |
205 | ||
206 | dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name); | |
207 | ||
7eff2e7a | 208 | ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name); |
4a11b59d AV |
209 | if (ret) |
210 | return ret; | |
211 | ||
212 | prop_buf = (char *)get_zeroed_page(GFP_KERNEL); | |
213 | if (!prop_buf) | |
214 | return -ENOMEM; | |
215 | ||
216 | for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) { | |
217 | struct device_attribute *attr; | |
218 | char *line; | |
219 | ||
220 | attr = &power_supply_static_attrs[j]; | |
221 | ||
222 | ret = power_supply_show_static_attrs(dev, attr, prop_buf); | |
223 | if (ret < 0) | |
224 | goto out; | |
225 | ||
226 | line = strchr(prop_buf, '\n'); | |
227 | if (line) | |
228 | *line = 0; | |
229 | ||
230 | attrname = kstruprdup(attr->attr.name, GFP_KERNEL); | |
231 | if (!attrname) { | |
232 | ret = -ENOMEM; | |
233 | goto out; | |
234 | } | |
235 | ||
236 | dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf); | |
237 | ||
7eff2e7a | 238 | ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); |
4a11b59d AV |
239 | kfree(attrname); |
240 | if (ret) | |
241 | goto out; | |
242 | } | |
243 | ||
244 | dev_dbg(dev, "%zd dynamic props\n", psy->num_properties); | |
245 | ||
246 | for (j = 0; j < psy->num_properties; j++) { | |
247 | struct device_attribute *attr; | |
248 | char *line; | |
249 | ||
250 | attr = &power_supply_attrs[psy->properties[j]]; | |
251 | ||
252 | ret = power_supply_show_property(dev, attr, prop_buf); | |
253 | if (ret == -ENODEV) { | |
254 | /* When a battery is absent, we expect -ENODEV. Don't abort; | |
255 | send the uevent with at least the the PRESENT=0 property */ | |
256 | ret = 0; | |
257 | continue; | |
258 | } | |
259 | ||
260 | if (ret < 0) | |
261 | goto out; | |
262 | ||
263 | line = strchr(prop_buf, '\n'); | |
264 | if (line) | |
265 | *line = 0; | |
266 | ||
267 | attrname = kstruprdup(attr->attr.name, GFP_KERNEL); | |
268 | if (!attrname) { | |
269 | ret = -ENOMEM; | |
270 | goto out; | |
271 | } | |
272 | ||
273 | dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf); | |
274 | ||
7eff2e7a | 275 | ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf); |
4a11b59d AV |
276 | kfree(attrname); |
277 | if (ret) | |
278 | goto out; | |
279 | } | |
280 | ||
281 | out: | |
282 | free_page((unsigned long)prop_buf); | |
283 | ||
284 | return ret; | |
285 | } |