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