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/device.h>
16 #include <linux/power_supply.h>
17 #include <linux/slab.h>
18 #include <linux/stat.h>
19 
20 #include "power_supply.h"
21 
22 /*
23  * This is because the name "current" breaks the device attr macro.
24  * The "current" word resolves to "(get_current())" so instead of
25  * "current" "(get_current())" appears in the sysfs.
26  *
27  * The source of this definition is the device.h which calls __ATTR
28  * macro in sysfs.h which calls the __stringify macro.
29  *
30  * Only modification that the name is not tried to be resolved
31  * (as a macro let's say).
32  */
33 
34 #define POWER_SUPPLY_ATTR(_name)					\
35 {									\
36 	.attr = { .name = #_name },					\
37 	.show = power_supply_show_property,				\
38 	.store = power_supply_store_property,				\
39 }
40 
41 static struct device_attribute power_supply_attrs[];
42 
43 static const char * const power_supply_type_text[] = {
44 	"Unknown", "Battery", "UPS", "Mains", "USB",
45 	"USB_DCP", "USB_CDP", "USB_ACA", "USB_C",
46 	"USB_PD", "USB_PD_DRP", "BrickID"
47 };
48 
49 static const char * const power_supply_usb_type_text[] = {
50 	"Unknown", "SDP", "DCP", "CDP", "ACA", "C",
51 	"PD", "PD_DRP", "PD_PPS", "BrickID"
52 };
53 
54 static const char * const power_supply_status_text[] = {
55 	"Unknown", "Charging", "Discharging", "Not charging", "Full"
56 };
57 
58 static const char * const power_supply_charge_type_text[] = {
59 	"Unknown", "N/A", "Trickle", "Fast", "Standard", "Adaptive", "Custom"
60 };
61 
62 static const char * const power_supply_health_text[] = {
63 	"Unknown", "Good", "Overheat", "Dead", "Over voltage",
64 	"Unspecified failure", "Cold", "Watchdog timer expire",
65 	"Safety timer expire", "Over current"
66 };
67 
68 static const char * const power_supply_technology_text[] = {
69 	"Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
70 	"LiMn"
71 };
72 
73 static const char * const power_supply_capacity_level_text[] = {
74 	"Unknown", "Critical", "Low", "Normal", "High", "Full"
75 };
76 
77 static const char * const power_supply_scope_text[] = {
78 	"Unknown", "System", "Device"
79 };
80 
81 static ssize_t power_supply_show_usb_type(struct device *dev,
82 					  enum power_supply_usb_type *usb_types,
83 					  ssize_t num_usb_types,
84 					  union power_supply_propval *value,
85 					  char *buf)
86 {
87 	enum power_supply_usb_type usb_type;
88 	ssize_t count = 0;
89 	bool match = false;
90 	int i;
91 
92 	for (i = 0; i < num_usb_types; ++i) {
93 		usb_type = usb_types[i];
94 
95 		if (value->intval == usb_type) {
96 			count += sprintf(buf + count, "[%s] ",
97 					 power_supply_usb_type_text[usb_type]);
98 			match = true;
99 		} else {
100 			count += sprintf(buf + count, "%s ",
101 					 power_supply_usb_type_text[usb_type]);
102 		}
103 	}
104 
105 	if (!match) {
106 		dev_warn(dev, "driver reporting unsupported connected type\n");
107 		return -EINVAL;
108 	}
109 
110 	if (count)
111 		buf[count - 1] = '\n';
112 
113 	return count;
114 }
115 
116 static ssize_t power_supply_show_property(struct device *dev,
117 					  struct device_attribute *attr,
118 					  char *buf) {
119 	ssize_t ret;
120 	struct power_supply *psy = dev_get_drvdata(dev);
121 	enum power_supply_property psp = attr - power_supply_attrs;
122 	union power_supply_propval value;
123 
124 	if (psp == POWER_SUPPLY_PROP_TYPE) {
125 		value.intval = psy->desc->type;
126 	} else {
127 		ret = power_supply_get_property(psy, psp, &value);
128 
129 		if (ret < 0) {
130 			if (ret == -ENODATA)
131 				dev_dbg(dev, "driver has no data for `%s' property\n",
132 					attr->attr.name);
133 			else if (ret != -ENODEV && ret != -EAGAIN)
134 				dev_err_ratelimited(dev,
135 					"driver failed to report `%s' property: %zd\n",
136 					attr->attr.name, ret);
137 			return ret;
138 		}
139 	}
140 
141 	switch (psp) {
142 	case POWER_SUPPLY_PROP_STATUS:
143 		ret = sprintf(buf, "%s\n",
144 			      power_supply_status_text[value.intval]);
145 		break;
146 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
147 		ret = sprintf(buf, "%s\n",
148 			      power_supply_charge_type_text[value.intval]);
149 		break;
150 	case POWER_SUPPLY_PROP_HEALTH:
151 		ret = sprintf(buf, "%s\n",
152 			      power_supply_health_text[value.intval]);
153 		break;
154 	case POWER_SUPPLY_PROP_TECHNOLOGY:
155 		ret = sprintf(buf, "%s\n",
156 			      power_supply_technology_text[value.intval]);
157 		break;
158 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
159 		ret = sprintf(buf, "%s\n",
160 			      power_supply_capacity_level_text[value.intval]);
161 		break;
162 	case POWER_SUPPLY_PROP_TYPE:
163 		ret = sprintf(buf, "%s\n",
164 			      power_supply_type_text[value.intval]);
165 		break;
166 	case POWER_SUPPLY_PROP_USB_TYPE:
167 		ret = power_supply_show_usb_type(dev, psy->desc->usb_types,
168 						 psy->desc->num_usb_types,
169 						 &value, buf);
170 		break;
171 	case POWER_SUPPLY_PROP_SCOPE:
172 		ret = sprintf(buf, "%s\n",
173 			      power_supply_scope_text[value.intval]);
174 		break;
175 	case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
176 		ret = sprintf(buf, "%s\n", value.strval);
177 		break;
178 	default:
179 		ret = sprintf(buf, "%d\n", value.intval);
180 	}
181 
182 	return ret;
183 }
184 
185 static ssize_t power_supply_store_property(struct device *dev,
186 					   struct device_attribute *attr,
187 					   const char *buf, size_t count) {
188 	ssize_t ret;
189 	struct power_supply *psy = dev_get_drvdata(dev);
190 	enum power_supply_property psp = attr - power_supply_attrs;
191 	union power_supply_propval value;
192 
193 	switch (psp) {
194 	case POWER_SUPPLY_PROP_STATUS:
195 		ret = sysfs_match_string(power_supply_status_text, buf);
196 		break;
197 	case POWER_SUPPLY_PROP_CHARGE_TYPE:
198 		ret = sysfs_match_string(power_supply_charge_type_text, buf);
199 		break;
200 	case POWER_SUPPLY_PROP_HEALTH:
201 		ret = sysfs_match_string(power_supply_health_text, buf);
202 		break;
203 	case POWER_SUPPLY_PROP_TECHNOLOGY:
204 		ret = sysfs_match_string(power_supply_technology_text, buf);
205 		break;
206 	case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
207 		ret = sysfs_match_string(power_supply_capacity_level_text, buf);
208 		break;
209 	case POWER_SUPPLY_PROP_SCOPE:
210 		ret = sysfs_match_string(power_supply_scope_text, buf);
211 		break;
212 	default:
213 		ret = -EINVAL;
214 	}
215 
216 	/*
217 	 * If no match was found, then check to see if it is an integer.
218 	 * Integer values are valid for enums in addition to the text value.
219 	 */
220 	if (ret < 0) {
221 		long long_val;
222 
223 		ret = kstrtol(buf, 10, &long_val);
224 		if (ret < 0)
225 			return ret;
226 
227 		ret = long_val;
228 	}
229 
230 	value.intval = ret;
231 
232 	ret = power_supply_set_property(psy, psp, &value);
233 	if (ret < 0)
234 		return ret;
235 
236 	return count;
237 }
238 
239 /* Must be in the same order as POWER_SUPPLY_PROP_* */
240 static struct device_attribute power_supply_attrs[] = {
241 	/* Properties of type `int' */
242 	POWER_SUPPLY_ATTR(status),
243 	POWER_SUPPLY_ATTR(charge_type),
244 	POWER_SUPPLY_ATTR(health),
245 	POWER_SUPPLY_ATTR(present),
246 	POWER_SUPPLY_ATTR(online),
247 	POWER_SUPPLY_ATTR(authentic),
248 	POWER_SUPPLY_ATTR(technology),
249 	POWER_SUPPLY_ATTR(cycle_count),
250 	POWER_SUPPLY_ATTR(voltage_max),
251 	POWER_SUPPLY_ATTR(voltage_min),
252 	POWER_SUPPLY_ATTR(voltage_max_design),
253 	POWER_SUPPLY_ATTR(voltage_min_design),
254 	POWER_SUPPLY_ATTR(voltage_now),
255 	POWER_SUPPLY_ATTR(voltage_avg),
256 	POWER_SUPPLY_ATTR(voltage_ocv),
257 	POWER_SUPPLY_ATTR(voltage_boot),
258 	POWER_SUPPLY_ATTR(current_max),
259 	POWER_SUPPLY_ATTR(current_now),
260 	POWER_SUPPLY_ATTR(current_avg),
261 	POWER_SUPPLY_ATTR(current_boot),
262 	POWER_SUPPLY_ATTR(power_now),
263 	POWER_SUPPLY_ATTR(power_avg),
264 	POWER_SUPPLY_ATTR(charge_full_design),
265 	POWER_SUPPLY_ATTR(charge_empty_design),
266 	POWER_SUPPLY_ATTR(charge_full),
267 	POWER_SUPPLY_ATTR(charge_empty),
268 	POWER_SUPPLY_ATTR(charge_now),
269 	POWER_SUPPLY_ATTR(charge_avg),
270 	POWER_SUPPLY_ATTR(charge_counter),
271 	POWER_SUPPLY_ATTR(constant_charge_current),
272 	POWER_SUPPLY_ATTR(constant_charge_current_max),
273 	POWER_SUPPLY_ATTR(constant_charge_voltage),
274 	POWER_SUPPLY_ATTR(constant_charge_voltage_max),
275 	POWER_SUPPLY_ATTR(charge_control_limit),
276 	POWER_SUPPLY_ATTR(charge_control_limit_max),
277 	POWER_SUPPLY_ATTR(charge_control_start_threshold),
278 	POWER_SUPPLY_ATTR(charge_control_end_threshold),
279 	POWER_SUPPLY_ATTR(input_current_limit),
280 	POWER_SUPPLY_ATTR(energy_full_design),
281 	POWER_SUPPLY_ATTR(energy_empty_design),
282 	POWER_SUPPLY_ATTR(energy_full),
283 	POWER_SUPPLY_ATTR(energy_empty),
284 	POWER_SUPPLY_ATTR(energy_now),
285 	POWER_SUPPLY_ATTR(energy_avg),
286 	POWER_SUPPLY_ATTR(capacity),
287 	POWER_SUPPLY_ATTR(capacity_alert_min),
288 	POWER_SUPPLY_ATTR(capacity_alert_max),
289 	POWER_SUPPLY_ATTR(capacity_level),
290 	POWER_SUPPLY_ATTR(temp),
291 	POWER_SUPPLY_ATTR(temp_max),
292 	POWER_SUPPLY_ATTR(temp_min),
293 	POWER_SUPPLY_ATTR(temp_alert_min),
294 	POWER_SUPPLY_ATTR(temp_alert_max),
295 	POWER_SUPPLY_ATTR(temp_ambient),
296 	POWER_SUPPLY_ATTR(temp_ambient_alert_min),
297 	POWER_SUPPLY_ATTR(temp_ambient_alert_max),
298 	POWER_SUPPLY_ATTR(time_to_empty_now),
299 	POWER_SUPPLY_ATTR(time_to_empty_avg),
300 	POWER_SUPPLY_ATTR(time_to_full_now),
301 	POWER_SUPPLY_ATTR(time_to_full_avg),
302 	POWER_SUPPLY_ATTR(type),
303 	POWER_SUPPLY_ATTR(usb_type),
304 	POWER_SUPPLY_ATTR(scope),
305 	POWER_SUPPLY_ATTR(precharge_current),
306 	POWER_SUPPLY_ATTR(charge_term_current),
307 	POWER_SUPPLY_ATTR(calibrate),
308 	/* Properties of type `const char *' */
309 	POWER_SUPPLY_ATTR(model_name),
310 	POWER_SUPPLY_ATTR(manufacturer),
311 	POWER_SUPPLY_ATTR(serial_number),
312 };
313 
314 static struct attribute *
315 __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
316 
317 static umode_t power_supply_attr_is_visible(struct kobject *kobj,
318 					   struct attribute *attr,
319 					   int attrno)
320 {
321 	struct device *dev = container_of(kobj, struct device, kobj);
322 	struct power_supply *psy = dev_get_drvdata(dev);
323 	umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
324 	int i;
325 
326 	if (attrno == POWER_SUPPLY_PROP_TYPE)
327 		return mode;
328 
329 	for (i = 0; i < psy->desc->num_properties; i++) {
330 		int property = psy->desc->properties[i];
331 
332 		if (property == attrno) {
333 			if (psy->desc->property_is_writeable &&
334 			    psy->desc->property_is_writeable(psy, property) > 0)
335 				mode |= S_IWUSR;
336 
337 			return mode;
338 		}
339 	}
340 
341 	return 0;
342 }
343 
344 static struct attribute_group power_supply_attr_group = {
345 	.attrs = __power_supply_attrs,
346 	.is_visible = power_supply_attr_is_visible,
347 };
348 
349 static const struct attribute_group *power_supply_attr_groups[] = {
350 	&power_supply_attr_group,
351 	NULL,
352 };
353 
354 void power_supply_init_attrs(struct device_type *dev_type)
355 {
356 	int i;
357 
358 	dev_type->groups = power_supply_attr_groups;
359 
360 	for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
361 		__power_supply_attrs[i] = &power_supply_attrs[i].attr;
362 }
363 
364 static char *kstruprdup(const char *str, gfp_t gfp)
365 {
366 	char *ret, *ustr;
367 
368 	ustr = ret = kmalloc(strlen(str) + 1, gfp);
369 
370 	if (!ret)
371 		return NULL;
372 
373 	while (*str)
374 		*ustr++ = toupper(*str++);
375 
376 	*ustr = 0;
377 
378 	return ret;
379 }
380 
381 int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
382 {
383 	struct power_supply *psy = dev_get_drvdata(dev);
384 	int ret = 0, j;
385 	char *prop_buf;
386 	char *attrname;
387 
388 	if (!psy || !psy->desc) {
389 		dev_dbg(dev, "No power supply yet\n");
390 		return ret;
391 	}
392 
393 	ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
394 	if (ret)
395 		return ret;
396 
397 	prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
398 	if (!prop_buf)
399 		return -ENOMEM;
400 
401 	for (j = 0; j < psy->desc->num_properties; j++) {
402 		struct device_attribute *attr;
403 		char *line;
404 
405 		attr = &power_supply_attrs[psy->desc->properties[j]];
406 
407 		ret = power_supply_show_property(dev, attr, prop_buf);
408 		if (ret == -ENODEV || ret == -ENODATA) {
409 			/* When a battery is absent, we expect -ENODEV. Don't abort;
410 			   send the uevent with at least the the PRESENT=0 property */
411 			ret = 0;
412 			continue;
413 		}
414 
415 		if (ret < 0)
416 			goto out;
417 
418 		line = strchr(prop_buf, '\n');
419 		if (line)
420 			*line = 0;
421 
422 		attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
423 		if (!attrname) {
424 			ret = -ENOMEM;
425 			goto out;
426 		}
427 
428 		ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
429 		kfree(attrname);
430 		if (ret)
431 			goto out;
432 	}
433 
434 out:
435 	free_page((unsigned long)prop_buf);
436 
437 	return ret;
438 }
439