xref: /linux/drivers/power/supply/power_supply_sysfs.c (revision 71dfa617ea9f18e4585fe78364217cd32b1fc382)
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 #include <linux/string_helpers.h>
19 
20 #include "power_supply.h"
21 
22 #define MAX_PROP_NAME_LEN 30
23 
24 struct power_supply_attr {
25 	const char *prop_name;
26 	char attr_name[MAX_PROP_NAME_LEN + 1];
27 	struct device_attribute dev_attr;
28 	const char * const *text_values;
29 	int text_values_len;
30 };
31 
32 #define _POWER_SUPPLY_ATTR(_name, _text, _len)	\
33 [POWER_SUPPLY_PROP_ ## _name] =			\
34 {						\
35 	.prop_name = #_name,			\
36 	.attr_name = #_name "\0",		\
37 	.text_values = _text,			\
38 	.text_values_len = _len,		\
39 }
40 
41 #define POWER_SUPPLY_ATTR(_name) _POWER_SUPPLY_ATTR(_name, NULL, 0)
42 #define _POWER_SUPPLY_ENUM_ATTR(_name, _text)	\
43 	_POWER_SUPPLY_ATTR(_name, _text, ARRAY_SIZE(_text))
44 #define POWER_SUPPLY_ENUM_ATTR(_name)	\
45 	_POWER_SUPPLY_ENUM_ATTR(_name, POWER_SUPPLY_ ## _name ## _TEXT)
46 
47 static const char * const POWER_SUPPLY_TYPE_TEXT[] = {
48 	[POWER_SUPPLY_TYPE_UNKNOWN]		= "Unknown",
49 	[POWER_SUPPLY_TYPE_BATTERY]		= "Battery",
50 	[POWER_SUPPLY_TYPE_UPS]			= "UPS",
51 	[POWER_SUPPLY_TYPE_MAINS]		= "Mains",
52 	[POWER_SUPPLY_TYPE_USB]			= "USB",
53 	[POWER_SUPPLY_TYPE_USB_DCP]		= "USB_DCP",
54 	[POWER_SUPPLY_TYPE_USB_CDP]		= "USB_CDP",
55 	[POWER_SUPPLY_TYPE_USB_ACA]		= "USB_ACA",
56 	[POWER_SUPPLY_TYPE_USB_TYPE_C]		= "USB_C",
57 	[POWER_SUPPLY_TYPE_USB_PD]		= "USB_PD",
58 	[POWER_SUPPLY_TYPE_USB_PD_DRP]		= "USB_PD_DRP",
59 	[POWER_SUPPLY_TYPE_APPLE_BRICK_ID]	= "BrickID",
60 	[POWER_SUPPLY_TYPE_WIRELESS]		= "Wireless",
61 };
62 
63 static const char * const POWER_SUPPLY_USB_TYPE_TEXT[] = {
64 	[POWER_SUPPLY_USB_TYPE_UNKNOWN]		= "Unknown",
65 	[POWER_SUPPLY_USB_TYPE_SDP]		= "SDP",
66 	[POWER_SUPPLY_USB_TYPE_DCP]		= "DCP",
67 	[POWER_SUPPLY_USB_TYPE_CDP]		= "CDP",
68 	[POWER_SUPPLY_USB_TYPE_ACA]		= "ACA",
69 	[POWER_SUPPLY_USB_TYPE_C]		= "C",
70 	[POWER_SUPPLY_USB_TYPE_PD]		= "PD",
71 	[POWER_SUPPLY_USB_TYPE_PD_DRP]		= "PD_DRP",
72 	[POWER_SUPPLY_USB_TYPE_PD_PPS]		= "PD_PPS",
73 	[POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID]	= "BrickID",
74 };
75 
76 static const char * const POWER_SUPPLY_STATUS_TEXT[] = {
77 	[POWER_SUPPLY_STATUS_UNKNOWN]		= "Unknown",
78 	[POWER_SUPPLY_STATUS_CHARGING]		= "Charging",
79 	[POWER_SUPPLY_STATUS_DISCHARGING]	= "Discharging",
80 	[POWER_SUPPLY_STATUS_NOT_CHARGING]	= "Not charging",
81 	[POWER_SUPPLY_STATUS_FULL]		= "Full",
82 };
83 
84 static const char * const POWER_SUPPLY_CHARGE_TYPE_TEXT[] = {
85 	[POWER_SUPPLY_CHARGE_TYPE_UNKNOWN]	= "Unknown",
86 	[POWER_SUPPLY_CHARGE_TYPE_NONE]		= "N/A",
87 	[POWER_SUPPLY_CHARGE_TYPE_TRICKLE]	= "Trickle",
88 	[POWER_SUPPLY_CHARGE_TYPE_FAST]		= "Fast",
89 	[POWER_SUPPLY_CHARGE_TYPE_STANDARD]	= "Standard",
90 	[POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE]	= "Adaptive",
91 	[POWER_SUPPLY_CHARGE_TYPE_CUSTOM]	= "Custom",
92 	[POWER_SUPPLY_CHARGE_TYPE_LONGLIFE]	= "Long Life",
93 	[POWER_SUPPLY_CHARGE_TYPE_BYPASS]	= "Bypass",
94 };
95 
96 static const char * const POWER_SUPPLY_HEALTH_TEXT[] = {
97 	[POWER_SUPPLY_HEALTH_UNKNOWN]		    = "Unknown",
98 	[POWER_SUPPLY_HEALTH_GOOD]		    = "Good",
99 	[POWER_SUPPLY_HEALTH_OVERHEAT]		    = "Overheat",
100 	[POWER_SUPPLY_HEALTH_DEAD]		    = "Dead",
101 	[POWER_SUPPLY_HEALTH_OVERVOLTAGE]	    = "Over voltage",
102 	[POWER_SUPPLY_HEALTH_UNSPEC_FAILURE]	    = "Unspecified failure",
103 	[POWER_SUPPLY_HEALTH_COLD]		    = "Cold",
104 	[POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE] = "Watchdog timer expire",
105 	[POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE]   = "Safety timer expire",
106 	[POWER_SUPPLY_HEALTH_OVERCURRENT]	    = "Over current",
107 	[POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED]  = "Calibration required",
108 	[POWER_SUPPLY_HEALTH_WARM]		    = "Warm",
109 	[POWER_SUPPLY_HEALTH_COOL]		    = "Cool",
110 	[POWER_SUPPLY_HEALTH_HOT]		    = "Hot",
111 	[POWER_SUPPLY_HEALTH_NO_BATTERY]	    = "No battery",
112 };
113 
114 static const char * const POWER_SUPPLY_TECHNOLOGY_TEXT[] = {
115 	[POWER_SUPPLY_TECHNOLOGY_UNKNOWN]	= "Unknown",
116 	[POWER_SUPPLY_TECHNOLOGY_NiMH]		= "NiMH",
117 	[POWER_SUPPLY_TECHNOLOGY_LION]		= "Li-ion",
118 	[POWER_SUPPLY_TECHNOLOGY_LIPO]		= "Li-poly",
119 	[POWER_SUPPLY_TECHNOLOGY_LiFe]		= "LiFe",
120 	[POWER_SUPPLY_TECHNOLOGY_NiCd]		= "NiCd",
121 	[POWER_SUPPLY_TECHNOLOGY_LiMn]		= "LiMn",
122 };
123 
124 static const char * const POWER_SUPPLY_CAPACITY_LEVEL_TEXT[] = {
125 	[POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN]	= "Unknown",
126 	[POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL]	= "Critical",
127 	[POWER_SUPPLY_CAPACITY_LEVEL_LOW]	= "Low",
128 	[POWER_SUPPLY_CAPACITY_LEVEL_NORMAL]	= "Normal",
129 	[POWER_SUPPLY_CAPACITY_LEVEL_HIGH]	= "High",
130 	[POWER_SUPPLY_CAPACITY_LEVEL_FULL]	= "Full",
131 };
132 
133 static const char * const POWER_SUPPLY_SCOPE_TEXT[] = {
134 	[POWER_SUPPLY_SCOPE_UNKNOWN]	= "Unknown",
135 	[POWER_SUPPLY_SCOPE_SYSTEM]	= "System",
136 	[POWER_SUPPLY_SCOPE_DEVICE]	= "Device",
137 };
138 
139 static const char * const POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[] = {
140 	[POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO]		= "auto",
141 	[POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE]	= "inhibit-charge",
142 	[POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE]	= "force-discharge",
143 };
144 
145 static struct power_supply_attr power_supply_attrs[] = {
146 	/* Properties of type `int' */
147 	POWER_SUPPLY_ENUM_ATTR(STATUS),
148 	POWER_SUPPLY_ENUM_ATTR(CHARGE_TYPE),
149 	POWER_SUPPLY_ENUM_ATTR(HEALTH),
150 	POWER_SUPPLY_ATTR(PRESENT),
151 	POWER_SUPPLY_ATTR(ONLINE),
152 	POWER_SUPPLY_ATTR(AUTHENTIC),
153 	POWER_SUPPLY_ENUM_ATTR(TECHNOLOGY),
154 	POWER_SUPPLY_ATTR(CYCLE_COUNT),
155 	POWER_SUPPLY_ATTR(VOLTAGE_MAX),
156 	POWER_SUPPLY_ATTR(VOLTAGE_MIN),
157 	POWER_SUPPLY_ATTR(VOLTAGE_MAX_DESIGN),
158 	POWER_SUPPLY_ATTR(VOLTAGE_MIN_DESIGN),
159 	POWER_SUPPLY_ATTR(VOLTAGE_NOW),
160 	POWER_SUPPLY_ATTR(VOLTAGE_AVG),
161 	POWER_SUPPLY_ATTR(VOLTAGE_OCV),
162 	POWER_SUPPLY_ATTR(VOLTAGE_BOOT),
163 	POWER_SUPPLY_ATTR(CURRENT_MAX),
164 	POWER_SUPPLY_ATTR(CURRENT_NOW),
165 	POWER_SUPPLY_ATTR(CURRENT_AVG),
166 	POWER_SUPPLY_ATTR(CURRENT_BOOT),
167 	POWER_SUPPLY_ATTR(POWER_NOW),
168 	POWER_SUPPLY_ATTR(POWER_AVG),
169 	POWER_SUPPLY_ATTR(CHARGE_FULL_DESIGN),
170 	POWER_SUPPLY_ATTR(CHARGE_EMPTY_DESIGN),
171 	POWER_SUPPLY_ATTR(CHARGE_FULL),
172 	POWER_SUPPLY_ATTR(CHARGE_EMPTY),
173 	POWER_SUPPLY_ATTR(CHARGE_NOW),
174 	POWER_SUPPLY_ATTR(CHARGE_AVG),
175 	POWER_SUPPLY_ATTR(CHARGE_COUNTER),
176 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT),
177 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT_MAX),
178 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE),
179 	POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE_MAX),
180 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT),
181 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT_MAX),
182 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_START_THRESHOLD),
183 	POWER_SUPPLY_ATTR(CHARGE_CONTROL_END_THRESHOLD),
184 	POWER_SUPPLY_ENUM_ATTR(CHARGE_BEHAVIOUR),
185 	POWER_SUPPLY_ATTR(INPUT_CURRENT_LIMIT),
186 	POWER_SUPPLY_ATTR(INPUT_VOLTAGE_LIMIT),
187 	POWER_SUPPLY_ATTR(INPUT_POWER_LIMIT),
188 	POWER_SUPPLY_ATTR(ENERGY_FULL_DESIGN),
189 	POWER_SUPPLY_ATTR(ENERGY_EMPTY_DESIGN),
190 	POWER_SUPPLY_ATTR(ENERGY_FULL),
191 	POWER_SUPPLY_ATTR(ENERGY_EMPTY),
192 	POWER_SUPPLY_ATTR(ENERGY_NOW),
193 	POWER_SUPPLY_ATTR(ENERGY_AVG),
194 	POWER_SUPPLY_ATTR(CAPACITY),
195 	POWER_SUPPLY_ATTR(CAPACITY_ALERT_MIN),
196 	POWER_SUPPLY_ATTR(CAPACITY_ALERT_MAX),
197 	POWER_SUPPLY_ATTR(CAPACITY_ERROR_MARGIN),
198 	POWER_SUPPLY_ENUM_ATTR(CAPACITY_LEVEL),
199 	POWER_SUPPLY_ATTR(TEMP),
200 	POWER_SUPPLY_ATTR(TEMP_MAX),
201 	POWER_SUPPLY_ATTR(TEMP_MIN),
202 	POWER_SUPPLY_ATTR(TEMP_ALERT_MIN),
203 	POWER_SUPPLY_ATTR(TEMP_ALERT_MAX),
204 	POWER_SUPPLY_ATTR(TEMP_AMBIENT),
205 	POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MIN),
206 	POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MAX),
207 	POWER_SUPPLY_ATTR(TIME_TO_EMPTY_NOW),
208 	POWER_SUPPLY_ATTR(TIME_TO_EMPTY_AVG),
209 	POWER_SUPPLY_ATTR(TIME_TO_FULL_NOW),
210 	POWER_SUPPLY_ATTR(TIME_TO_FULL_AVG),
211 	POWER_SUPPLY_ENUM_ATTR(TYPE),
212 	POWER_SUPPLY_ATTR(USB_TYPE),
213 	POWER_SUPPLY_ENUM_ATTR(SCOPE),
214 	POWER_SUPPLY_ATTR(PRECHARGE_CURRENT),
215 	POWER_SUPPLY_ATTR(CHARGE_TERM_CURRENT),
216 	POWER_SUPPLY_ATTR(CALIBRATE),
217 	POWER_SUPPLY_ATTR(MANUFACTURE_YEAR),
218 	POWER_SUPPLY_ATTR(MANUFACTURE_MONTH),
219 	POWER_SUPPLY_ATTR(MANUFACTURE_DAY),
220 	/* Properties of type `const char *' */
221 	POWER_SUPPLY_ATTR(MODEL_NAME),
222 	POWER_SUPPLY_ATTR(MANUFACTURER),
223 	POWER_SUPPLY_ATTR(SERIAL_NUMBER),
224 };
225 #define POWER_SUPPLY_ATTR_CNT ARRAY_SIZE(power_supply_attrs)
226 
227 static struct attribute *
228 __power_supply_attrs[POWER_SUPPLY_ATTR_CNT + 1];
229 
230 static struct power_supply_attr *to_ps_attr(struct device_attribute *attr)
231 {
232 	return container_of(attr, struct power_supply_attr, dev_attr);
233 }
234 
235 static enum power_supply_property dev_attr_psp(struct device_attribute *attr)
236 {
237 	return  to_ps_attr(attr) - power_supply_attrs;
238 }
239 
240 static ssize_t power_supply_show_usb_type(struct device *dev,
241 					  const struct power_supply_desc *desc,
242 					  union power_supply_propval *value,
243 					  char *buf)
244 {
245 	enum power_supply_usb_type usb_type;
246 	ssize_t count = 0;
247 	bool match = false;
248 	int i;
249 
250 	for (i = 0; i < desc->num_usb_types; ++i) {
251 		usb_type = desc->usb_types[i];
252 
253 		if (value->intval == usb_type) {
254 			count += sysfs_emit_at(buf, count, "[%s] ",
255 					 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
256 			match = true;
257 		} else {
258 			count += sysfs_emit_at(buf, count, "%s ",
259 					 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
260 		}
261 	}
262 
263 	if (!match) {
264 		dev_warn(dev, "driver reporting unsupported connected type\n");
265 		return -EINVAL;
266 	}
267 
268 	if (count)
269 		buf[count - 1] = '\n';
270 
271 	return count;
272 }
273 
274 static ssize_t power_supply_show_charge_behaviour(struct device *dev,
275 						  struct power_supply *psy,
276 						  union power_supply_propval *value,
277 						  char *buf)
278 {
279 	int ret;
280 
281 	ret = power_supply_get_property(psy,
282 					POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR,
283 					value);
284 	if (ret < 0)
285 		return ret;
286 
287 	return power_supply_charge_behaviour_show(dev, psy->desc->charge_behaviours,
288 						  value->intval, buf);
289 }
290 
291 static ssize_t power_supply_show_property(struct device *dev,
292 					  struct device_attribute *attr,
293 					  char *buf) {
294 	ssize_t ret;
295 	struct power_supply *psy = dev_get_drvdata(dev);
296 	struct power_supply_attr *ps_attr = to_ps_attr(attr);
297 	enum power_supply_property psp = dev_attr_psp(attr);
298 	union power_supply_propval value;
299 
300 	if (psp == POWER_SUPPLY_PROP_TYPE) {
301 		value.intval = psy->desc->type;
302 	} else {
303 		ret = power_supply_get_property(psy, psp, &value);
304 
305 		if (ret < 0) {
306 			if (ret == -ENODATA)
307 				dev_dbg_ratelimited(dev,
308 					"driver has no data for `%s' property\n",
309 					attr->attr.name);
310 			else if (ret != -ENODEV && ret != -EAGAIN)
311 				dev_err_ratelimited(dev,
312 					"driver failed to report `%s' property: %zd\n",
313 					attr->attr.name, ret);
314 			return ret;
315 		}
316 	}
317 
318 	switch (psp) {
319 	case POWER_SUPPLY_PROP_USB_TYPE:
320 		ret = power_supply_show_usb_type(dev, psy->desc,
321 						&value, buf);
322 		break;
323 	case POWER_SUPPLY_PROP_CHARGE_BEHAVIOUR:
324 		ret = power_supply_show_charge_behaviour(dev, psy, &value, buf);
325 		break;
326 	case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
327 		ret = sysfs_emit(buf, "%s\n", value.strval);
328 		break;
329 	default:
330 		if (ps_attr->text_values_len > 0 &&
331 				value.intval < ps_attr->text_values_len && value.intval >= 0) {
332 			ret = sysfs_emit(buf, "%s\n", ps_attr->text_values[value.intval]);
333 		} else {
334 			ret = sysfs_emit(buf, "%d\n", value.intval);
335 		}
336 	}
337 
338 	return ret;
339 }
340 
341 static ssize_t power_supply_store_property(struct device *dev,
342 					   struct device_attribute *attr,
343 					   const char *buf, size_t count) {
344 	ssize_t ret;
345 	struct power_supply *psy = dev_get_drvdata(dev);
346 	struct power_supply_attr *ps_attr = to_ps_attr(attr);
347 	enum power_supply_property psp = dev_attr_psp(attr);
348 	union power_supply_propval value;
349 
350 	ret = -EINVAL;
351 	if (ps_attr->text_values_len > 0) {
352 		ret = __sysfs_match_string(ps_attr->text_values,
353 					   ps_attr->text_values_len, buf);
354 	}
355 
356 	/*
357 	 * If no match was found, then check to see if it is an integer.
358 	 * Integer values are valid for enums in addition to the text value.
359 	 */
360 	if (ret < 0) {
361 		long long_val;
362 
363 		ret = kstrtol(buf, 10, &long_val);
364 		if (ret < 0)
365 			return ret;
366 
367 		ret = long_val;
368 	}
369 
370 	value.intval = ret;
371 
372 	ret = power_supply_set_property(psy, psp, &value);
373 	if (ret < 0)
374 		return ret;
375 
376 	return count;
377 }
378 
379 static umode_t power_supply_attr_is_visible(struct kobject *kobj,
380 					   struct attribute *attr,
381 					   int attrno)
382 {
383 	struct device *dev = kobj_to_dev(kobj);
384 	struct power_supply *psy = dev_get_drvdata(dev);
385 	umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
386 	int i;
387 
388 	if (!power_supply_attrs[attrno].prop_name)
389 		return 0;
390 
391 	if (attrno == POWER_SUPPLY_PROP_TYPE)
392 		return mode;
393 
394 	for (i = 0; i < psy->desc->num_properties; i++) {
395 		int property = psy->desc->properties[i];
396 
397 		if (property == attrno) {
398 			if (psy->desc->property_is_writeable &&
399 			    psy->desc->property_is_writeable(psy, property) > 0)
400 				mode |= S_IWUSR;
401 
402 			return mode;
403 		}
404 	}
405 
406 	if (power_supply_battery_info_has_prop(psy->battery_info, attrno))
407 		return mode;
408 
409 	return 0;
410 }
411 
412 static const struct attribute_group power_supply_attr_group = {
413 	.attrs = __power_supply_attrs,
414 	.is_visible = power_supply_attr_is_visible,
415 };
416 
417 const struct attribute_group *power_supply_attr_groups[] = {
418 	&power_supply_attr_group,
419 	NULL
420 };
421 
422 void power_supply_init_attrs(void)
423 {
424 	int i;
425 
426 	for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++) {
427 		struct device_attribute *attr;
428 
429 		if (!power_supply_attrs[i].prop_name) {
430 			pr_warn("%s: Property %d skipped because it is missing from power_supply_attrs\n",
431 				__func__, i);
432 			sprintf(power_supply_attrs[i].attr_name, "_err_%d", i);
433 		} else {
434 			string_lower(power_supply_attrs[i].attr_name,
435 				     power_supply_attrs[i].attr_name);
436 		}
437 
438 		attr = &power_supply_attrs[i].dev_attr;
439 
440 		attr->attr.name = power_supply_attrs[i].attr_name;
441 		attr->show = power_supply_show_property;
442 		attr->store = power_supply_store_property;
443 		__power_supply_attrs[i] = &attr->attr;
444 	}
445 }
446 
447 static int add_prop_uevent(const struct device *dev, struct kobj_uevent_env *env,
448 			   enum power_supply_property prop, char *prop_buf)
449 {
450 	int ret = 0;
451 	struct power_supply_attr *pwr_attr;
452 	struct device_attribute *dev_attr;
453 	char *line;
454 
455 	pwr_attr = &power_supply_attrs[prop];
456 	dev_attr = &pwr_attr->dev_attr;
457 
458 	ret = power_supply_show_property((struct device *)dev, dev_attr, prop_buf);
459 	if (ret == -ENODEV || ret == -ENODATA) {
460 		/*
461 		 * When a battery is absent, we expect -ENODEV. Don't abort;
462 		 * send the uevent with at least the PRESENT=0 property
463 		 */
464 		return 0;
465 	}
466 
467 	if (ret < 0)
468 		return ret;
469 
470 	line = strchr(prop_buf, '\n');
471 	if (line)
472 		*line = 0;
473 
474 	return add_uevent_var(env, "POWER_SUPPLY_%s=%s",
475 			      pwr_attr->prop_name, prop_buf);
476 }
477 
478 int power_supply_uevent(const struct device *dev, struct kobj_uevent_env *env)
479 {
480 	const struct power_supply *psy = dev_get_drvdata(dev);
481 	const enum power_supply_property *battery_props =
482 		power_supply_battery_info_properties;
483 	unsigned long psy_drv_properties[POWER_SUPPLY_ATTR_CNT /
484 					 sizeof(unsigned long) + 1] = {0};
485 	int ret = 0, j;
486 	char *prop_buf;
487 
488 	if (!psy || !psy->desc) {
489 		dev_dbg(dev, "No power supply yet\n");
490 		return ret;
491 	}
492 
493 	ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
494 	if (ret)
495 		return ret;
496 
497 	/*
498 	 * Kernel generates KOBJ_REMOVE uevent in device removal path, after
499 	 * resources have been freed. Exit early to avoid use-after-free.
500 	 */
501 	if (psy->removing)
502 		return 0;
503 
504 	prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
505 	if (!prop_buf)
506 		return -ENOMEM;
507 
508 	ret = add_prop_uevent(dev, env, POWER_SUPPLY_PROP_TYPE, prop_buf);
509 	if (ret)
510 		goto out;
511 
512 	for (j = 0; j < psy->desc->num_properties; j++) {
513 		set_bit(psy->desc->properties[j], psy_drv_properties);
514 		ret = add_prop_uevent(dev, env, psy->desc->properties[j],
515 				      prop_buf);
516 		if (ret)
517 			goto out;
518 	}
519 
520 	for (j = 0; j < power_supply_battery_info_properties_size; j++) {
521 		if (test_bit(battery_props[j], psy_drv_properties))
522 			continue;
523 		if (!power_supply_battery_info_has_prop(psy->battery_info,
524 				battery_props[j]))
525 			continue;
526 		ret = add_prop_uevent(dev, env, battery_props[j],
527 			      prop_buf);
528 		if (ret)
529 			goto out;
530 	}
531 
532 out:
533 	free_page((unsigned long)prop_buf);
534 
535 	return ret;
536 }
537 
538 ssize_t power_supply_charge_behaviour_show(struct device *dev,
539 					   unsigned int available_behaviours,
540 					   enum power_supply_charge_behaviour current_behaviour,
541 					   char *buf)
542 {
543 	bool match = false, available, active;
544 	ssize_t count = 0;
545 	int i;
546 
547 	for (i = 0; i < ARRAY_SIZE(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT); i++) {
548 		available = available_behaviours & BIT(i);
549 		active = i == current_behaviour;
550 
551 		if (available && active) {
552 			count += sysfs_emit_at(buf, count, "[%s] ",
553 					       POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
554 			match = true;
555 		} else if (available) {
556 			count += sysfs_emit_at(buf, count, "%s ",
557 					       POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]);
558 		}
559 	}
560 
561 	if (!match) {
562 		dev_warn(dev, "driver reporting unsupported charge behaviour\n");
563 		return -EINVAL;
564 	}
565 
566 	if (count)
567 		buf[count - 1] = '\n';
568 
569 	return count;
570 }
571 EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_show);
572 
573 int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf)
574 {
575 	int i = sysfs_match_string(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT, buf);
576 
577 	if (i < 0)
578 		return i;
579 
580 	if (available_behaviours & BIT(i))
581 		return i;
582 
583 	return -EINVAL;
584 }
585 EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_parse);
586