1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *  Sysfs interface for the universal power supply monitor class
4  *
5  *  Copyright © 2007  David Woodhouse <dwmw2@infradead.org>
6  *  Copyright © 2007  Anton Vorontsov <cbou@mail.ru>
7  *  Copyright © 2004  Szabolcs Gyurko
8  *  Copyright © 2003  Ian Molton <spyro@f2s.com>
9  *
10  *  Modified: 2004, Oct     Szabolcs Gyurko
11  */
12 
13 #include <linux/ctype.h>
14 #include <linux/device.h>
15 #include <linux/power_supply.h>
16 #include <linux/slab.h>
17 #include <linux/stat.h>
18 
19 #include "power_supply.h"
20 
21 #define MAX_PROP_NAME_LEN 30
22 
23 struct power_supply_attr {
24 	const char *prop_name;
25 	char attr_name[MAX_PROP_NAME_LEN + 1];
26 	struct device_attribute dev_attr;
27 	const char * const *text_values;
28 	int text_values_len;
29 };
30 
31 #define _POWER_SUPPLY_ATTR(_name, _text, _len)	\
32 [POWER_SUPPLY_PROP_ ## _name] =			\
33 {						\
34 	.prop_name = #_name,			\
35 	.attr_name = #_name "\0",		\
36 	.text_values = _text,			\
37 	.text_values_len = _len,		\
38 }
39 
40 #define POWER_SUPPLY_ATTR(_name) _POWER_SUPPLY_ATTR(_name, NULL, 0)
41 #define _POWER_SUPPLY_ENUM_ATTR(_name, _text)	\
42 	_POWER_SUPPLY_ATTR(_name, _text, ARRAY_SIZE(_text))
43 #define POWER_SUPPLY_ENUM_ATTR(_name)	\
44 	_POWER_SUPPLY_ENUM_ATTR(_name, POWER_SUPPLY_ ## _name ## _TEXT)
45 
46 static const char * const POWER_SUPPLY_TYPE_TEXT[] = {
47 	[POWER_SUPPLY_TYPE_UNKNOWN]		= "Unknown",
48 	[POWER_SUPPLY_TYPE_BATTERY]		= "Battery",
49 	[POWER_SUPPLY_TYPE_UPS]			= "UPS",
50 	[POWER_SUPPLY_TYPE_MAINS]		= "Mains",
51 	[POWER_SUPPLY_TYPE_USB]			= "USB",
52 	[POWER_SUPPLY_TYPE_USB_DCP]		= "USB_DCP",
53 	[POWER_SUPPLY_TYPE_USB_CDP]		= "USB_CDP",
54 	[POWER_SUPPLY_TYPE_USB_ACA]		= "USB_ACA",
55 	[POWER_SUPPLY_TYPE_USB_TYPE_C]		= "USB_C",
56 	[POWER_SUPPLY_TYPE_USB_PD]		= "USB_PD",
57 	[POWER_SUPPLY_TYPE_USB_PD_DRP]		= "USB_PD_DRP",
58 	[POWER_SUPPLY_TYPE_APPLE_BRICK_ID]	= "BrickID",
59 };
60 
61 static const char * const POWER_SUPPLY_USB_TYPE_TEXT[] = {
62 	[POWER_SUPPLY_USB_TYPE_UNKNOWN]		= "Unknown",
63 	[POWER_SUPPLY_USB_TYPE_SDP]		= "SDP",
64 	[POWER_SUPPLY_USB_TYPE_DCP]		= "DCP",
65 	[POWER_SUPPLY_USB_TYPE_CDP]		= "CDP",
66 	[POWER_SUPPLY_USB_TYPE_ACA]		= "ACA",
67 	[POWER_SUPPLY_USB_TYPE_C]		= "C",
68 	[POWER_SUPPLY_USB_TYPE_PD]		= "PD",
69 	[POWER_SUPPLY_USB_TYPE_PD_DRP]		= "PD_DRP",
70 	[POWER_SUPPLY_USB_TYPE_PD_PPS]		= "PD_PPS",
71 	[POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID]	= "BrickID",
72 };
73 
74 static const char * const POWER_SUPPLY_STATUS_TEXT[] = {
75 	[POWER_SUPPLY_STATUS_UNKNOWN]		= "Unknown",
76 	[POWER_SUPPLY_STATUS_CHARGING]		= "Charging",
77 	[POWER_SUPPLY_STATUS_DISCHARGING]	= "Discharging",
78 	[POWER_SUPPLY_STATUS_NOT_CHARGING]	= "Not charging",
79 	[POWER_SUPPLY_STATUS_FULL]		= "Full",
80 };
81 
82 static const char * const POWER_SUPPLY_CHARGE_TYPE_TEXT[] = {
83 	[POWER_SUPPLY_CHARGE_TYPE_UNKNOWN]	= "Unknown",
84 	[POWER_SUPPLY_CHARGE_TYPE_NONE]		= "N/A",
85 	[POWER_SUPPLY_CHARGE_TYPE_TRICKLE]	= "Trickle",
86 	[POWER_SUPPLY_CHARGE_TYPE_FAST]		= "Fast",
87 	[POWER_SUPPLY_CHARGE_TYPE_STANDARD]	= "Standard",
88 	[POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE]	= "Adaptive",
89 	[POWER_SUPPLY_CHARGE_TYPE_CUSTOM]	= "Custom",
90 };
91 
92 static const char * const POWER_SUPPLY_HEALTH_TEXT[] = {
93 	[POWER_SUPPLY_HEALTH_UNKNOWN]		    = "Unknown",
94 	[POWER_SUPPLY_HEALTH_GOOD]		    = "Good",
95 	[POWER_SUPPLY_HEALTH_OVERHEAT]		    = "Overheat",
96 	[POWER_SUPPLY_HEALTH_DEAD]		    = "Dead",
97 	[POWER_SUPPLY_HEALTH_OVERVOLTAGE]	    = "Over voltage",
98 	[POWER_SUPPLY_HEALTH_UNSPEC_FAILURE]	    = "Unspecified failure",
99 	[POWER_SUPPLY_HEALTH_COLD]		    = "Cold",
100 	[POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE] = "Watchdog timer expire",
101 	[POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE]   = "Safety timer expire",
102 	[POWER_SUPPLY_HEALTH_OVERCURRENT]	    = "Over current",
103 	[POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED]  = "Calibration required",
104 };
105 
106 static const char * const POWER_SUPPLY_TECHNOLOGY_TEXT[] = {
107 	[POWER_SUPPLY_TECHNOLOGY_UNKNOWN]	= "Unknown",
108 	[POWER_SUPPLY_TECHNOLOGY_NiMH]		= "NiMH",
109 	[POWER_SUPPLY_TECHNOLOGY_LION]		= "Li-ion",
110 	[POWER_SUPPLY_TECHNOLOGY_LIPO]		= "Li-poly",
111 	[POWER_SUPPLY_TECHNOLOGY_LiFe]		= "LiFe",
112 	[POWER_SUPPLY_TECHNOLOGY_NiCd]		= "NiCd",
113 	[POWER_SUPPLY_TECHNOLOGY_LiMn]		= "LiMn",
114 };
115 
116 static const char * const POWER_SUPPLY_CAPACITY_LEVEL_TEXT[] = {
117 	[POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN]	= "Unknown",
118 	[POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL]	= "Critical",
119 	[POWER_SUPPLY_CAPACITY_LEVEL_LOW]	= "Low",
120 	[POWER_SUPPLY_CAPACITY_LEVEL_NORMAL]	= "Normal",
121 	[POWER_SUPPLY_CAPACITY_LEVEL_HIGH]	= "High",
122 	[POWER_SUPPLY_CAPACITY_LEVEL_FULL]	= "Full",
123 };
124 
125 static const char * const POWER_SUPPLY_SCOPE_TEXT[] = {
126 	[POWER_SUPPLY_SCOPE_UNKNOWN]	= "Unknown",
127 	[POWER_SUPPLY_SCOPE_SYSTEM]	= "System",
128 	[POWER_SUPPLY_SCOPE_DEVICE]	= "Device",
129 };
130 
131 static struct power_supply_attr power_supply_attrs[] = {
132 	/* Properties of type `int' */
133 	POWER_SUPPLY_ENUM_ATTR(STATUS),
134 	POWER_SUPPLY_ENUM_ATTR(CHARGE_TYPE),
135 	POWER_SUPPLY_ENUM_ATTR(HEALTH),
136 	POWER_SUPPLY_ATTR(PRESENT),
137 	POWER_SUPPLY_ATTR(ONLINE),
138 	POWER_SUPPLY_ATTR(AUTHENTIC),
139 	POWER_SUPPLY_ENUM_ATTR(TECHNOLOGY),
140 	POWER_SUPPLY_ATTR(CYCLE_COUNT),
141 	POWER_SUPPLY_ATTR(VOLTAGE_MAX),
142 	POWER_SUPPLY_ATTR(VOLTAGE_MIN),
143 	POWER_SUPPLY_ATTR(VOLTAGE_MAX_DESIGN),
144 	POWER_SUPPLY_ATTR(VOLTAGE_MIN_DESIGN),
145 	POWER_SUPPLY_ATTR(VOLTAGE_NOW),
146 	POWER_SUPPLY_ATTR(VOLTAGE_AVG),
147 	POWER_SUPPLY_ATTR(VOLTAGE_OCV),
148 	POWER_SUPPLY_ATTR(VOLTAGE_BOOT),
149 	POWER_SUPPLY_ATTR(CURRENT_MAX),
150 	POWER_SUPPLY_ATTR(CURRENT_NOW),
151 	POWER_SUPPLY_ATTR(CURRENT_AVG),
152 	POWER_SUPPLY_ATTR(CURRENT_BOOT),
153 	POWER_SUPPLY_ATTR(POWER_NOW),
154 	POWER_SUPPLY_ATTR(POWER_AVG),
155 	POWER_SUPPLY_ATTR(CHARGE_FULL_DESIGN),
156 	POWER_SUPPLY_ATTR(CHARGE_EMPTY_DESIGN),
157 	POWER_SUPPLY_ATTR(CHARGE_FULL),
158 	POWER_SUPPLY_ATTR(CHARGE_EMPTY),
159 	POWER_SUPPLY_ATTR(CHARGE_NOW),
160 	POWER_SUPPLY_ATTR(CHARGE_AVG),
161 	POWER_SUPPLY_ATTR(CHARGE_COUNTER),
162 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT),
163 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT_MAX),
164 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE),
165 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE_MAX),
166 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT),
167 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT_MAX),
168 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_START_THRESHOLD),
169 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_END_THRESHOLD),
170 	POWER_SUPPLY_ATTR(INPUT_CURRENT_LIMIT),
171 	POWER_SUPPLY_ATTR(INPUT_VOLTAGE_LIMIT),
172 	POWER_SUPPLY_ATTR(INPUT_POWER_LIMIT),
173 	POWER_SUPPLY_ATTR(ENERGY_FULL_DESIGN),
174 	POWER_SUPPLY_ATTR(ENERGY_EMPTY_DESIGN),
175 	POWER_SUPPLY_ATTR(ENERGY_FULL),
176 	POWER_SUPPLY_ATTR(ENERGY_EMPTY),
177 	POWER_SUPPLY_ATTR(ENERGY_NOW),
178 	POWER_SUPPLY_ATTR(ENERGY_AVG),
179 	POWER_SUPPLY_ATTR(CAPACITY),
180 	POWER_SUPPLY_ATTR(CAPACITY_ALERT_MIN),
181 	POWER_SUPPLY_ATTR(CAPACITY_ALERT_MAX),
182 	POWER_SUPPLY_ATTR(CAPACITY_ERROR_MARGIN),
183 	POWER_SUPPLY_ENUM_ATTR(CAPACITY_LEVEL),
184 	POWER_SUPPLY_ATTR(TEMP),
185 	POWER_SUPPLY_ATTR(TEMP_MAX),
186 	POWER_SUPPLY_ATTR(TEMP_MIN),
187 	POWER_SUPPLY_ATTR(TEMP_ALERT_MIN),
188 	POWER_SUPPLY_ATTR(TEMP_ALERT_MAX),
189 	POWER_SUPPLY_ATTR(TEMP_AMBIENT),
190 	POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MIN),
191 	POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MAX),
192 	POWER_SUPPLY_ATTR(TIME_TO_EMPTY_NOW),
193 	POWER_SUPPLY_ATTR(TIME_TO_EMPTY_AVG),
194 	POWER_SUPPLY_ATTR(TIME_TO_FULL_NOW),
195 	POWER_SUPPLY_ATTR(TIME_TO_FULL_AVG),
196 	POWER_SUPPLY_ENUM_ATTR(TYPE),
197 	POWER_SUPPLY_ATTR(USB_TYPE),
198 	POWER_SUPPLY_ENUM_ATTR(SCOPE),
199 	POWER_SUPPLY_ATTR(PRECHARGE_CURRENT),
200 	POWER_SUPPLY_ATTR(CHARGE_TERM_CURRENT),
201 	POWER_SUPPLY_ATTR(CALIBRATE),
202 	POWER_SUPPLY_ATTR(MANUFACTURE_YEAR),
203 	POWER_SUPPLY_ATTR(MANUFACTURE_MONTH),
204 	POWER_SUPPLY_ATTR(MANUFACTURE_DAY),
205 	/* Properties of type `const char *' */
206 	POWER_SUPPLY_ATTR(MODEL_NAME),
207 	POWER_SUPPLY_ATTR(MANUFACTURER),
208 	POWER_SUPPLY_ATTR(SERIAL_NUMBER),
209 };
210 
211 static struct attribute *
212 __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
213 
214 static struct power_supply_attr *to_ps_attr(struct device_attribute *attr)
215 {
216 	return container_of(attr, struct power_supply_attr, dev_attr);
217 }
218 
219 static enum power_supply_property dev_attr_psp(struct device_attribute *attr)
220 {
221 	return  to_ps_attr(attr) - power_supply_attrs;
222 }
223 
224 static ssize_t power_supply_show_usb_type(struct device *dev,
225 					  const struct power_supply_desc *desc,
226 					  union power_supply_propval *value,
227 					  char *buf)
228 {
229 	enum power_supply_usb_type usb_type;
230 	ssize_t count = 0;
231 	bool match = false;
232 	int i;
233 
234 	for (i = 0; i < desc->num_usb_types; ++i) {
235 		usb_type = desc->usb_types[i];
236 
237 		if (value->intval == usb_type) {
238 			count += sprintf(buf + count, "[%s] ",
239 					 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
240 			match = true;
241 		} else {
242 			count += sprintf(buf + count, "%s ",
243 					 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
244 		}
245 	}
246 
247 	if (!match) {
248 		dev_warn(dev, "driver reporting unsupported connected type\n");
249 		return -EINVAL;
250 	}
251 
252 	if (count)
253 		buf[count - 1] = '\n';
254 
255 	return count;
256 }
257 
258 static ssize_t power_supply_show_property(struct device *dev,
259 					  struct device_attribute *attr,
260 					  char *buf) {
261 	ssize_t ret;
262 	struct power_supply *psy = dev_get_drvdata(dev);
263 	struct power_supply_attr *ps_attr = to_ps_attr(attr);
264 	enum power_supply_property psp = dev_attr_psp(attr);
265 	union power_supply_propval value;
266 
267 	if (psp == POWER_SUPPLY_PROP_TYPE) {
268 		value.intval = psy->desc->type;
269 	} else {
270 		ret = power_supply_get_property(psy, psp, &value);
271 
272 		if (ret < 0) {
273 			if (ret == -ENODATA)
274 				dev_dbg(dev, "driver has no data for `%s' property\n",
275 					attr->attr.name);
276 			else if (ret != -ENODEV && ret != -EAGAIN)
277 				dev_err_ratelimited(dev,
278 					"driver failed to report `%s' property: %zd\n",
279 					attr->attr.name, ret);
280 			return ret;
281 		}
282 	}
283 
284 	if (ps_attr->text_values_len > 0 &&
285 	    value.intval < ps_attr->text_values_len && value.intval >= 0) {
286 		return sprintf(buf, "%s\n", ps_attr->text_values[value.intval]);
287 	}
288 
289 	switch (psp) {
290 	case POWER_SUPPLY_PROP_USB_TYPE:
291 		ret = power_supply_show_usb_type(dev, psy->desc,
292 						&value, buf);
293 		break;
294 	case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
295 		ret = sprintf(buf, "%s\n", value.strval);
296 		break;
297 	default:
298 		ret = sprintf(buf, "%d\n", value.intval);
299 	}
300 
301 	return ret;
302 }
303 
304 static ssize_t power_supply_store_property(struct device *dev,
305 					   struct device_attribute *attr,
306 					   const char *buf, size_t count) {
307 	ssize_t ret;
308 	struct power_supply *psy = dev_get_drvdata(dev);
309 	struct power_supply_attr *ps_attr = to_ps_attr(attr);
310 	enum power_supply_property psp = dev_attr_psp(attr);
311 	union power_supply_propval value;
312 
313 	ret = -EINVAL;
314 	if (ps_attr->text_values_len > 0) {
315 		ret = __sysfs_match_string(ps_attr->text_values,
316 					   ps_attr->text_values_len, buf);
317 	}
318 
319 	/*
320 	 * If no match was found, then check to see if it is an integer.
321 	 * Integer values are valid for enums in addition to the text value.
322 	 */
323 	if (ret < 0) {
324 		long long_val;
325 
326 		ret = kstrtol(buf, 10, &long_val);
327 		if (ret < 0)
328 			return ret;
329 
330 		ret = long_val;
331 	}
332 
333 	value.intval = ret;
334 
335 	ret = power_supply_set_property(psy, psp, &value);
336 	if (ret < 0)
337 		return ret;
338 
339 	return count;
340 }
341 
342 static umode_t power_supply_attr_is_visible(struct kobject *kobj,
343 					   struct attribute *attr,
344 					   int attrno)
345 {
346 	struct device *dev = container_of(kobj, struct device, kobj);
347 	struct power_supply *psy = dev_get_drvdata(dev);
348 	umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
349 	int i;
350 
351 	if (!power_supply_attrs[attrno].prop_name)
352 		return 0;
353 
354 	if (attrno == POWER_SUPPLY_PROP_TYPE)
355 		return mode;
356 
357 	for (i = 0; i < psy->desc->num_properties; i++) {
358 		int property = psy->desc->properties[i];
359 
360 		if (property == attrno) {
361 			if (psy->desc->property_is_writeable &&
362 			    psy->desc->property_is_writeable(psy, property) > 0)
363 				mode |= S_IWUSR;
364 
365 			return mode;
366 		}
367 	}
368 
369 	return 0;
370 }
371 
372 static struct attribute_group power_supply_attr_group = {
373 	.attrs = __power_supply_attrs,
374 	.is_visible = power_supply_attr_is_visible,
375 };
376 
377 static const struct attribute_group *power_supply_attr_groups[] = {
378 	&power_supply_attr_group,
379 	NULL,
380 };
381 
382 static void str_to_lower(char *str)
383 {
384 	while (*str) {
385 		*str = tolower(*str);
386 		str++;
387 	}
388 }
389 
390 void power_supply_init_attrs(struct device_type *dev_type)
391 {
392 	int i;
393 
394 	dev_type->groups = power_supply_attr_groups;
395 
396 	for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++) {
397 		struct device_attribute *attr;
398 
399 		if (!power_supply_attrs[i].prop_name) {
400 			pr_warn("%s: Property %d skipped because is is missing from power_supply_attrs\n",
401 				__func__, i);
402 			sprintf(power_supply_attrs[i].attr_name, "_err_%d", i);
403 		} else {
404 			str_to_lower(power_supply_attrs[i].attr_name);
405 		}
406 
407 		attr = &power_supply_attrs[i].dev_attr;
408 
409 		attr->attr.name = power_supply_attrs[i].attr_name;
410 		attr->show = power_supply_show_property;
411 		attr->store = power_supply_store_property;
412 		__power_supply_attrs[i] = &attr->attr;
413 	}
414 }
415 
416 static int add_prop_uevent(struct device *dev, struct kobj_uevent_env *env,
417 			   enum power_supply_property prop, char *prop_buf)
418 {
419 	int ret = 0;
420 	struct power_supply_attr *pwr_attr;
421 	struct device_attribute *dev_attr;
422 	char *line;
423 
424 	pwr_attr = &power_supply_attrs[prop];
425 	dev_attr = &pwr_attr->dev_attr;
426 
427 	ret = power_supply_show_property(dev, dev_attr, prop_buf);
428 	if (ret == -ENODEV || ret == -ENODATA) {
429 		/*
430 		 * When a battery is absent, we expect -ENODEV. Don't abort;
431 		 * send the uevent with at least the the PRESENT=0 property
432 		 */
433 		return 0;
434 	}
435 
436 	if (ret < 0)
437 		return ret;
438 
439 	line = strchr(prop_buf, '\n');
440 	if (line)
441 		*line = 0;
442 
443 	return add_uevent_var(env, "POWER_SUPPLY_%s=%s",
444 			      pwr_attr->prop_name, prop_buf);
445 }
446 
447 int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
448 {
449 	struct power_supply *psy = dev_get_drvdata(dev);
450 	int ret = 0, j;
451 	char *prop_buf;
452 
453 	if (!psy || !psy->desc) {
454 		dev_dbg(dev, "No power supply yet\n");
455 		return ret;
456 	}
457 
458 	ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
459 	if (ret)
460 		return ret;
461 
462 	prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
463 	if (!prop_buf)
464 		return -ENOMEM;
465 
466 	ret = add_prop_uevent(dev, env, POWER_SUPPLY_PROP_TYPE, prop_buf);
467 	if (ret)
468 		goto out;
469 
470 	for (j = 0; j < psy->desc->num_properties; j++) {
471 		ret = add_prop_uevent(dev, env, psy->desc->properties[j],
472 				      prop_buf);
473 		if (ret)
474 			goto out;
475 	}
476 
477 out:
478 	free_page((unsigned long)prop_buf);
479 
480 	return ret;
481 }
482