blob: c3d7cbcd4fad518cf07c516034ef5862f16bebb7 [file] [log] [blame]
Thomas Gleixnera63a5fa2019-05-28 10:10:21 -07001// SPDX-License-Identifier: GPL-2.0-only
Anton Vorontsov4a11b592007-05-04 00:27:45 +04002/*
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
Anton Vorontsov4a11b592007-05-04 00:27:45 +040011 */
12
13#include <linux/ctype.h>
Paul Gortmaker51990e82012-01-22 11:23:42 -050014#include <linux/device.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040015#include <linux/power_supply.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090016#include <linux/slab.h>
Paul Gortmaker778f5232011-05-27 10:22:46 -040017#include <linux/stat.h>
Anton Vorontsov4a11b592007-05-04 00:27:45 +040018
Adrian Bunk25f121412007-11-26 00:25:45 +030019#include "power_supply.h"
20
Mathew King2cabeaf2020-05-04 14:29:27 -060021#define MAX_PROP_NAME_LEN 30
Anton Vorontsov4a11b592007-05-04 00:27:45 +040022
Mathew King2cabeaf2020-05-04 14:29:27 -060023struct power_supply_attr {
24 const char *prop_name;
25 char attr_name[MAX_PROP_NAME_LEN + 1];
26 struct device_attribute dev_attr;
Mathew King5b505362020-05-04 14:29:29 -060027 const char * const *text_values;
28 int text_values_len;
Mathew King2cabeaf2020-05-04 14:29:27 -060029};
30
Mathew King5b505362020-05-04 14:29:29 -060031#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, \
Anton Vorontsov4a11b592007-05-04 00:27:45 +040038}
39
Mathew King5b505362020-05-04 14:29:29 -060040#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
46static const char * const POWER_SUPPLY_TYPE_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060047 [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",
Subbaraman Narayanamurthy5ca937f2020-08-13 11:34:08 -070059 [POWER_SUPPLY_TYPE_WIRELESS] = "Wireless",
David Lechner105df602017-05-15 16:21:15 -050060};
61
Mathew King5b505362020-05-04 14:29:29 -060062static const char * const POWER_SUPPLY_USB_TYPE_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060063 [POWER_SUPPLY_USB_TYPE_UNKNOWN] = "Unknown",
64 [POWER_SUPPLY_USB_TYPE_SDP] = "SDP",
65 [POWER_SUPPLY_USB_TYPE_DCP] = "DCP",
66 [POWER_SUPPLY_USB_TYPE_CDP] = "CDP",
67 [POWER_SUPPLY_USB_TYPE_ACA] = "ACA",
68 [POWER_SUPPLY_USB_TYPE_C] = "C",
69 [POWER_SUPPLY_USB_TYPE_PD] = "PD",
70 [POWER_SUPPLY_USB_TYPE_PD_DRP] = "PD_DRP",
71 [POWER_SUPPLY_USB_TYPE_PD_PPS] = "PD_PPS",
72 [POWER_SUPPLY_USB_TYPE_APPLE_BRICK_ID] = "BrickID",
Adam Thomsoncf450042018-04-23 15:10:59 +010073};
74
Mathew King5b505362020-05-04 14:29:29 -060075static const char * const POWER_SUPPLY_STATUS_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060076 [POWER_SUPPLY_STATUS_UNKNOWN] = "Unknown",
77 [POWER_SUPPLY_STATUS_CHARGING] = "Charging",
78 [POWER_SUPPLY_STATUS_DISCHARGING] = "Discharging",
79 [POWER_SUPPLY_STATUS_NOT_CHARGING] = "Not charging",
80 [POWER_SUPPLY_STATUS_FULL] = "Full",
David Lechner105df602017-05-15 16:21:15 -050081};
82
Mathew King5b505362020-05-04 14:29:29 -060083static const char * const POWER_SUPPLY_CHARGE_TYPE_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060084 [POWER_SUPPLY_CHARGE_TYPE_UNKNOWN] = "Unknown",
85 [POWER_SUPPLY_CHARGE_TYPE_NONE] = "N/A",
86 [POWER_SUPPLY_CHARGE_TYPE_TRICKLE] = "Trickle",
87 [POWER_SUPPLY_CHARGE_TYPE_FAST] = "Fast",
88 [POWER_SUPPLY_CHARGE_TYPE_STANDARD] = "Standard",
89 [POWER_SUPPLY_CHARGE_TYPE_ADAPTIVE] = "Adaptive",
90 [POWER_SUPPLY_CHARGE_TYPE_CUSTOM] = "Custom",
Crag Wang46cbd0b2020-07-30 11:26:09 +080091 [POWER_SUPPLY_CHARGE_TYPE_LONGLIFE] = "Long Life",
David Lechner105df602017-05-15 16:21:15 -050092};
93
Mathew King5b505362020-05-04 14:29:29 -060094static const char * const POWER_SUPPLY_HEALTH_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060095 [POWER_SUPPLY_HEALTH_UNKNOWN] = "Unknown",
96 [POWER_SUPPLY_HEALTH_GOOD] = "Good",
97 [POWER_SUPPLY_HEALTH_OVERHEAT] = "Overheat",
98 [POWER_SUPPLY_HEALTH_DEAD] = "Dead",
99 [POWER_SUPPLY_HEALTH_OVERVOLTAGE] = "Over voltage",
100 [POWER_SUPPLY_HEALTH_UNSPEC_FAILURE] = "Unspecified failure",
101 [POWER_SUPPLY_HEALTH_COLD] = "Cold",
102 [POWER_SUPPLY_HEALTH_WATCHDOG_TIMER_EXPIRE] = "Watchdog timer expire",
103 [POWER_SUPPLY_HEALTH_SAFETY_TIMER_EXPIRE] = "Safety timer expire",
104 [POWER_SUPPLY_HEALTH_OVERCURRENT] = "Over current",
Sebastian Reichel601c2a52020-05-13 20:56:00 +0200105 [POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED] = "Calibration required",
Dan Murphy98cc1b92020-07-20 15:43:57 -0500106 [POWER_SUPPLY_HEALTH_WARM] = "Warm",
107 [POWER_SUPPLY_HEALTH_COOL] = "Cool",
108 [POWER_SUPPLY_HEALTH_HOT] = "Hot",
David Lechner105df602017-05-15 16:21:15 -0500109};
110
Mathew King5b505362020-05-04 14:29:29 -0600111static const char * const POWER_SUPPLY_TECHNOLOGY_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -0600112 [POWER_SUPPLY_TECHNOLOGY_UNKNOWN] = "Unknown",
113 [POWER_SUPPLY_TECHNOLOGY_NiMH] = "NiMH",
114 [POWER_SUPPLY_TECHNOLOGY_LION] = "Li-ion",
115 [POWER_SUPPLY_TECHNOLOGY_LIPO] = "Li-poly",
116 [POWER_SUPPLY_TECHNOLOGY_LiFe] = "LiFe",
117 [POWER_SUPPLY_TECHNOLOGY_NiCd] = "NiCd",
118 [POWER_SUPPLY_TECHNOLOGY_LiMn] = "LiMn",
David Lechner105df602017-05-15 16:21:15 -0500119};
120
Mathew King5b505362020-05-04 14:29:29 -0600121static const char * const POWER_SUPPLY_CAPACITY_LEVEL_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -0600122 [POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN] = "Unknown",
123 [POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL] = "Critical",
124 [POWER_SUPPLY_CAPACITY_LEVEL_LOW] = "Low",
125 [POWER_SUPPLY_CAPACITY_LEVEL_NORMAL] = "Normal",
126 [POWER_SUPPLY_CAPACITY_LEVEL_HIGH] = "High",
127 [POWER_SUPPLY_CAPACITY_LEVEL_FULL] = "Full",
David Lechner105df602017-05-15 16:21:15 -0500128};
129
Mathew King5b505362020-05-04 14:29:29 -0600130static const char * const POWER_SUPPLY_SCOPE_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -0600131 [POWER_SUPPLY_SCOPE_UNKNOWN] = "Unknown",
132 [POWER_SUPPLY_SCOPE_SYSTEM] = "System",
133 [POWER_SUPPLY_SCOPE_DEVICE] = "Device",
David Lechner105df602017-05-15 16:21:15 -0500134};
135
Mathew King2cabeaf2020-05-04 14:29:27 -0600136static struct power_supply_attr power_supply_attrs[] = {
137 /* Properties of type `int' */
Mathew King5b505362020-05-04 14:29:29 -0600138 POWER_SUPPLY_ENUM_ATTR(STATUS),
139 POWER_SUPPLY_ENUM_ATTR(CHARGE_TYPE),
140 POWER_SUPPLY_ENUM_ATTR(HEALTH),
Mathew King2cabeaf2020-05-04 14:29:27 -0600141 POWER_SUPPLY_ATTR(PRESENT),
142 POWER_SUPPLY_ATTR(ONLINE),
143 POWER_SUPPLY_ATTR(AUTHENTIC),
Mathew King5b505362020-05-04 14:29:29 -0600144 POWER_SUPPLY_ENUM_ATTR(TECHNOLOGY),
Mathew King2cabeaf2020-05-04 14:29:27 -0600145 POWER_SUPPLY_ATTR(CYCLE_COUNT),
146 POWER_SUPPLY_ATTR(VOLTAGE_MAX),
147 POWER_SUPPLY_ATTR(VOLTAGE_MIN),
148 POWER_SUPPLY_ATTR(VOLTAGE_MAX_DESIGN),
149 POWER_SUPPLY_ATTR(VOLTAGE_MIN_DESIGN),
150 POWER_SUPPLY_ATTR(VOLTAGE_NOW),
151 POWER_SUPPLY_ATTR(VOLTAGE_AVG),
152 POWER_SUPPLY_ATTR(VOLTAGE_OCV),
153 POWER_SUPPLY_ATTR(VOLTAGE_BOOT),
154 POWER_SUPPLY_ATTR(CURRENT_MAX),
155 POWER_SUPPLY_ATTR(CURRENT_NOW),
156 POWER_SUPPLY_ATTR(CURRENT_AVG),
157 POWER_SUPPLY_ATTR(CURRENT_BOOT),
158 POWER_SUPPLY_ATTR(POWER_NOW),
159 POWER_SUPPLY_ATTR(POWER_AVG),
160 POWER_SUPPLY_ATTR(CHARGE_FULL_DESIGN),
161 POWER_SUPPLY_ATTR(CHARGE_EMPTY_DESIGN),
162 POWER_SUPPLY_ATTR(CHARGE_FULL),
163 POWER_SUPPLY_ATTR(CHARGE_EMPTY),
164 POWER_SUPPLY_ATTR(CHARGE_NOW),
165 POWER_SUPPLY_ATTR(CHARGE_AVG),
166 POWER_SUPPLY_ATTR(CHARGE_COUNTER),
167 POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT),
168 POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT_MAX),
169 POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE),
170 POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE_MAX),
171 POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT),
172 POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT_MAX),
173 POWER_SUPPLY_ATTR(CHARGE_CONTROL_START_THRESHOLD),
174 POWER_SUPPLY_ATTR(CHARGE_CONTROL_END_THRESHOLD),
175 POWER_SUPPLY_ATTR(INPUT_CURRENT_LIMIT),
176 POWER_SUPPLY_ATTR(INPUT_VOLTAGE_LIMIT),
177 POWER_SUPPLY_ATTR(INPUT_POWER_LIMIT),
178 POWER_SUPPLY_ATTR(ENERGY_FULL_DESIGN),
179 POWER_SUPPLY_ATTR(ENERGY_EMPTY_DESIGN),
180 POWER_SUPPLY_ATTR(ENERGY_FULL),
181 POWER_SUPPLY_ATTR(ENERGY_EMPTY),
182 POWER_SUPPLY_ATTR(ENERGY_NOW),
183 POWER_SUPPLY_ATTR(ENERGY_AVG),
184 POWER_SUPPLY_ATTR(CAPACITY),
185 POWER_SUPPLY_ATTR(CAPACITY_ALERT_MIN),
186 POWER_SUPPLY_ATTR(CAPACITY_ALERT_MAX),
Sebastian Reichelbac705a2020-05-13 20:55:58 +0200187 POWER_SUPPLY_ATTR(CAPACITY_ERROR_MARGIN),
Mathew King5b505362020-05-04 14:29:29 -0600188 POWER_SUPPLY_ENUM_ATTR(CAPACITY_LEVEL),
Mathew King2cabeaf2020-05-04 14:29:27 -0600189 POWER_SUPPLY_ATTR(TEMP),
190 POWER_SUPPLY_ATTR(TEMP_MAX),
191 POWER_SUPPLY_ATTR(TEMP_MIN),
192 POWER_SUPPLY_ATTR(TEMP_ALERT_MIN),
193 POWER_SUPPLY_ATTR(TEMP_ALERT_MAX),
194 POWER_SUPPLY_ATTR(TEMP_AMBIENT),
195 POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MIN),
196 POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MAX),
197 POWER_SUPPLY_ATTR(TIME_TO_EMPTY_NOW),
198 POWER_SUPPLY_ATTR(TIME_TO_EMPTY_AVG),
199 POWER_SUPPLY_ATTR(TIME_TO_FULL_NOW),
200 POWER_SUPPLY_ATTR(TIME_TO_FULL_AVG),
Mathew King5b505362020-05-04 14:29:29 -0600201 POWER_SUPPLY_ENUM_ATTR(TYPE),
Mathew King2cabeaf2020-05-04 14:29:27 -0600202 POWER_SUPPLY_ATTR(USB_TYPE),
Mathew King5b505362020-05-04 14:29:29 -0600203 POWER_SUPPLY_ENUM_ATTR(SCOPE),
Mathew King2cabeaf2020-05-04 14:29:27 -0600204 POWER_SUPPLY_ATTR(PRECHARGE_CURRENT),
205 POWER_SUPPLY_ATTR(CHARGE_TERM_CURRENT),
206 POWER_SUPPLY_ATTR(CALIBRATE),
Sebastian Reichelfeabe492020-05-13 20:55:59 +0200207 POWER_SUPPLY_ATTR(MANUFACTURE_YEAR),
208 POWER_SUPPLY_ATTR(MANUFACTURE_MONTH),
209 POWER_SUPPLY_ATTR(MANUFACTURE_DAY),
Mathew King2cabeaf2020-05-04 14:29:27 -0600210 /* Properties of type `const char *' */
211 POWER_SUPPLY_ATTR(MODEL_NAME),
212 POWER_SUPPLY_ATTR(MANUFACTURER),
213 POWER_SUPPLY_ATTR(SERIAL_NUMBER),
214};
215
216static struct attribute *
217__power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
218
Mathew King5b505362020-05-04 14:29:29 -0600219static struct power_supply_attr *to_ps_attr(struct device_attribute *attr)
220{
221 return container_of(attr, struct power_supply_attr, dev_attr);
222}
223
Mathew King2cabeaf2020-05-04 14:29:27 -0600224static enum power_supply_property dev_attr_psp(struct device_attribute *attr)
225{
Mathew King5b505362020-05-04 14:29:29 -0600226 return to_ps_attr(attr) - power_supply_attrs;
Mathew King2cabeaf2020-05-04 14:29:27 -0600227}
228
Adam Thomsoncf450042018-04-23 15:10:59 +0100229static ssize_t power_supply_show_usb_type(struct device *dev,
Michał Mirosławde46e022020-04-03 22:20:32 +0200230 const struct power_supply_desc *desc,
Adam Thomsoncf450042018-04-23 15:10:59 +0100231 union power_supply_propval *value,
232 char *buf)
233{
234 enum power_supply_usb_type usb_type;
235 ssize_t count = 0;
236 bool match = false;
237 int i;
238
Michał Mirosławde46e022020-04-03 22:20:32 +0200239 for (i = 0; i < desc->num_usb_types; ++i) {
240 usb_type = desc->usb_types[i];
Adam Thomsoncf450042018-04-23 15:10:59 +0100241
242 if (value->intval == usb_type) {
243 count += sprintf(buf + count, "[%s] ",
Mathew King5b505362020-05-04 14:29:29 -0600244 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
Adam Thomsoncf450042018-04-23 15:10:59 +0100245 match = true;
246 } else {
247 count += sprintf(buf + count, "%s ",
Mathew King5b505362020-05-04 14:29:29 -0600248 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
Adam Thomsoncf450042018-04-23 15:10:59 +0100249 }
250 }
251
252 if (!match) {
253 dev_warn(dev, "driver reporting unsupported connected type\n");
254 return -EINVAL;
255 }
256
257 if (count)
258 buf[count - 1] = '\n';
259
260 return count;
261}
262
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400263static ssize_t power_supply_show_property(struct device *dev,
264 struct device_attribute *attr,
265 char *buf) {
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100266 ssize_t ret;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400267 struct power_supply *psy = dev_get_drvdata(dev);
Mathew King5b505362020-05-04 14:29:29 -0600268 struct power_supply_attr *ps_attr = to_ps_attr(attr);
Mathew King2cabeaf2020-05-04 14:29:27 -0600269 enum power_supply_property psp = dev_attr_psp(attr);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400270 union power_supply_propval value;
271
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100272 if (psp == POWER_SUPPLY_PROP_TYPE) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100273 value.intval = psy->desc->type;
Viresh Kumar73b4a082014-09-04 17:31:34 +0530274 } else {
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100275 ret = power_supply_get_property(psy, psp, &value);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400276
Viresh Kumar73b4a082014-09-04 17:31:34 +0530277 if (ret < 0) {
278 if (ret == -ENODATA)
279 dev_dbg(dev, "driver has no data for `%s' property\n",
280 attr->attr.name);
Rhyland Kleine3805382016-06-22 11:45:52 -0400281 else if (ret != -ENODEV && ret != -EAGAIN)
David Lechner87a2b652018-09-12 19:48:30 -0500282 dev_err_ratelimited(dev,
283 "driver failed to report `%s' property: %zd\n",
Viresh Kumar73b4a082014-09-04 17:31:34 +0530284 attr->attr.name, ret);
285 return ret;
286 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400287 }
288
Mathew King5b505362020-05-04 14:29:29 -0600289 if (ps_attr->text_values_len > 0 &&
290 value.intval < ps_attr->text_values_len && value.intval >= 0) {
291 return sprintf(buf, "%s\n", ps_attr->text_values[value.intval]);
292 }
293
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100294 switch (psp) {
Sebastian Reichel1f140ff2018-04-26 12:18:30 +0200295 case POWER_SUPPLY_PROP_USB_TYPE:
Michał Mirosławde46e022020-04-03 22:20:32 +0200296 ret = power_supply_show_usb_type(dev, psy->desc,
Mathew King5b505362020-05-04 14:29:29 -0600297 &value, buf);
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100298 break;
299 case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
300 ret = sprintf(buf, "%s\n", value.strval);
301 break;
302 default:
303 ret = sprintf(buf, "%d\n", value.intval);
304 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400305
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100306 return ret;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400307}
308
Daniel Mack0011d2d2010-05-18 21:49:52 +0200309static ssize_t power_supply_store_property(struct device *dev,
310 struct device_attribute *attr,
311 const char *buf, size_t count) {
312 ssize_t ret;
313 struct power_supply *psy = dev_get_drvdata(dev);
Mathew King5b505362020-05-04 14:29:29 -0600314 struct power_supply_attr *ps_attr = to_ps_attr(attr);
Mathew King2cabeaf2020-05-04 14:29:27 -0600315 enum power_supply_property psp = dev_attr_psp(attr);
Daniel Mack0011d2d2010-05-18 21:49:52 +0200316 union power_supply_propval value;
Daniel Mack0011d2d2010-05-18 21:49:52 +0200317
Mathew King5b505362020-05-04 14:29:29 -0600318 ret = -EINVAL;
319 if (ps_attr->text_values_len > 0) {
320 ret = __sysfs_match_string(ps_attr->text_values,
321 ps_attr->text_values_len, buf);
David Lechner105df602017-05-15 16:21:15 -0500322 }
Daniel Mack0011d2d2010-05-18 21:49:52 +0200323
David Lechner105df602017-05-15 16:21:15 -0500324 /*
325 * If no match was found, then check to see if it is an integer.
326 * Integer values are valid for enums in addition to the text value.
327 */
328 if (ret < 0) {
329 long long_val;
330
331 ret = kstrtol(buf, 10, &long_val);
332 if (ret < 0)
333 return ret;
334
335 ret = long_val;
336 }
337
338 value.intval = ret;
Daniel Mack0011d2d2010-05-18 21:49:52 +0200339
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100340 ret = power_supply_set_property(psy, psp, &value);
Daniel Mack0011d2d2010-05-18 21:49:52 +0200341 if (ret < 0)
342 return ret;
343
344 return count;
345}
346
Al Viro587a1f12011-07-23 23:11:19 -0400347static umode_t power_supply_attr_is_visible(struct kobject *kobj,
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200348 struct attribute *attr,
349 int attrno)
350{
Wang Qing224941c2020-06-12 14:55:54 +0800351 struct device *dev = kobj_to_dev(kobj);
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200352 struct power_supply *psy = dev_get_drvdata(dev);
Al Viro587a1f12011-07-23 23:11:19 -0400353 umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200354 int i;
355
Mathew King2cabeaf2020-05-04 14:29:27 -0600356 if (!power_supply_attrs[attrno].prop_name)
357 return 0;
358
Daniel Mackbbabb152010-05-25 02:39:45 +0200359 if (attrno == POWER_SUPPLY_PROP_TYPE)
360 return mode;
361
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100362 for (i = 0; i < psy->desc->num_properties; i++) {
363 int property = psy->desc->properties[i];
Daniel Mack0011d2d2010-05-18 21:49:52 +0200364
365 if (property == attrno) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100366 if (psy->desc->property_is_writeable &&
Krzysztof Kozlowski5c6e3a92015-06-08 10:09:48 +0900367 psy->desc->property_is_writeable(psy, property) > 0)
Daniel Mack0011d2d2010-05-18 21:49:52 +0200368 mode |= S_IWUSR;
369
370 return mode;
371 }
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200372 }
373
374 return 0;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400375}
376
Rikard Falkeborn2f7cf242021-01-13 21:32:43 +0100377static const struct attribute_group power_supply_attr_group = {
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200378 .attrs = __power_supply_attrs,
379 .is_visible = power_supply_attr_is_visible,
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400380};
381
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200382static const struct attribute_group *power_supply_attr_groups[] = {
383 &power_supply_attr_group,
384 NULL,
385};
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400386
Mathew King2cabeaf2020-05-04 14:29:27 -0600387static void str_to_lower(char *str)
388{
389 while (*str) {
390 *str = tolower(*str);
391 str++;
392 }
393}
394
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200395void power_supply_init_attrs(struct device_type *dev_type)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400396{
397 int i;
398
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200399 dev_type->groups = power_supply_attr_groups;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400400
Mathew King2cabeaf2020-05-04 14:29:27 -0600401 for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++) {
402 struct device_attribute *attr;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400403
Mathew King2cabeaf2020-05-04 14:29:27 -0600404 if (!power_supply_attrs[i].prop_name) {
Masanari Iidac2362512020-12-05 10:25:32 +0900405 pr_warn("%s: Property %d skipped because it is missing from power_supply_attrs\n",
Mathew King2cabeaf2020-05-04 14:29:27 -0600406 __func__, i);
407 sprintf(power_supply_attrs[i].attr_name, "_err_%d", i);
408 } else {
409 str_to_lower(power_supply_attrs[i].attr_name);
410 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400411
Mathew King2cabeaf2020-05-04 14:29:27 -0600412 attr = &power_supply_attrs[i].dev_attr;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400413
Mathew King2cabeaf2020-05-04 14:29:27 -0600414 attr->attr.name = power_supply_attrs[i].attr_name;
415 attr->show = power_supply_show_property;
416 attr->store = power_supply_store_property;
417 __power_supply_attrs[i] = &attr->attr;
418 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400419}
420
Mathew King2ad3d74e2020-05-04 14:29:30 -0600421static int add_prop_uevent(struct device *dev, struct kobj_uevent_env *env,
422 enum power_supply_property prop, char *prop_buf)
423{
424 int ret = 0;
425 struct power_supply_attr *pwr_attr;
426 struct device_attribute *dev_attr;
427 char *line;
428
429 pwr_attr = &power_supply_attrs[prop];
430 dev_attr = &pwr_attr->dev_attr;
431
432 ret = power_supply_show_property(dev, dev_attr, prop_buf);
433 if (ret == -ENODEV || ret == -ENODATA) {
434 /*
435 * When a battery is absent, we expect -ENODEV. Don't abort;
436 * send the uevent with at least the the PRESENT=0 property
437 */
438 return 0;
439 }
440
441 if (ret < 0)
442 return ret;
443
444 line = strchr(prop_buf, '\n');
445 if (line)
446 *line = 0;
447
448 return add_uevent_var(env, "POWER_SUPPLY_%s=%s",
449 pwr_attr->prop_name, prop_buf);
450}
451
Kay Sievers7eff2e72007-08-14 15:15:12 +0200452int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400453{
454 struct power_supply *psy = dev_get_drvdata(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200455 int ret = 0, j;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400456 char *prop_buf;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400457
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100458 if (!psy || !psy->desc) {
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400459 dev_dbg(dev, "No power supply yet\n");
460 return ret;
461 }
462
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100463 ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400464 if (ret)
465 return ret;
466
467 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
468 if (!prop_buf)
469 return -ENOMEM;
470
Mathew King2ad3d74e2020-05-04 14:29:30 -0600471 ret = add_prop_uevent(dev, env, POWER_SUPPLY_PROP_TYPE, prop_buf);
472 if (ret)
473 goto out;
474
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100475 for (j = 0; j < psy->desc->num_properties; j++) {
Mathew King2ad3d74e2020-05-04 14:29:30 -0600476 ret = add_prop_uevent(dev, env, psy->desc->properties[j],
477 prop_buf);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400478 if (ret)
479 goto out;
480 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400481
482out:
483 free_page((unsigned long)prop_buf);
484
485 return ret;
486}