blob: af0cad253f52145002d51410b081d5d419eb9d12 [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",
David Lechner105df602017-05-15 16:21:15 -050059};
60
Mathew King5b505362020-05-04 14:29:29 -060061static const char * const POWER_SUPPLY_USB_TYPE_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060062 [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",
Adam Thomsoncf450042018-04-23 15:10:59 +010072};
73
Mathew King5b505362020-05-04 14:29:29 -060074static const char * const POWER_SUPPLY_STATUS_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060075 [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",
David Lechner105df602017-05-15 16:21:15 -050080};
81
Mathew King5b505362020-05-04 14:29:29 -060082static const char * const POWER_SUPPLY_CHARGE_TYPE_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060083 [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",
David Lechner105df602017-05-15 16:21:15 -050090};
91
Mathew King5b505362020-05-04 14:29:29 -060092static const char * const POWER_SUPPLY_HEALTH_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -060093 [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",
Sebastian Reichel601c2a52020-05-13 20:56:00 +0200103 [POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED] = "Calibration required",
David Lechner105df602017-05-15 16:21:15 -0500104};
105
Mathew King5b505362020-05-04 14:29:29 -0600106static const char * const POWER_SUPPLY_TECHNOLOGY_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -0600107 [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",
David Lechner105df602017-05-15 16:21:15 -0500114};
115
Mathew King5b505362020-05-04 14:29:29 -0600116static const char * const POWER_SUPPLY_CAPACITY_LEVEL_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -0600117 [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",
David Lechner105df602017-05-15 16:21:15 -0500123};
124
Mathew King5b505362020-05-04 14:29:29 -0600125static const char * const POWER_SUPPLY_SCOPE_TEXT[] = {
Mathew Kingd960d912020-05-04 14:29:28 -0600126 [POWER_SUPPLY_SCOPE_UNKNOWN] = "Unknown",
127 [POWER_SUPPLY_SCOPE_SYSTEM] = "System",
128 [POWER_SUPPLY_SCOPE_DEVICE] = "Device",
David Lechner105df602017-05-15 16:21:15 -0500129};
130
Mathew King2cabeaf2020-05-04 14:29:27 -0600131static struct power_supply_attr power_supply_attrs[] = {
132 /* Properties of type `int' */
Mathew King5b505362020-05-04 14:29:29 -0600133 POWER_SUPPLY_ENUM_ATTR(STATUS),
134 POWER_SUPPLY_ENUM_ATTR(CHARGE_TYPE),
135 POWER_SUPPLY_ENUM_ATTR(HEALTH),
Mathew King2cabeaf2020-05-04 14:29:27 -0600136 POWER_SUPPLY_ATTR(PRESENT),
137 POWER_SUPPLY_ATTR(ONLINE),
138 POWER_SUPPLY_ATTR(AUTHENTIC),
Mathew King5b505362020-05-04 14:29:29 -0600139 POWER_SUPPLY_ENUM_ATTR(TECHNOLOGY),
Mathew King2cabeaf2020-05-04 14:29:27 -0600140 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),
Sebastian Reichelbac705a2020-05-13 20:55:58 +0200182 POWER_SUPPLY_ATTR(CAPACITY_ERROR_MARGIN),
Mathew King5b505362020-05-04 14:29:29 -0600183 POWER_SUPPLY_ENUM_ATTR(CAPACITY_LEVEL),
Mathew King2cabeaf2020-05-04 14:29:27 -0600184 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),
Mathew King5b505362020-05-04 14:29:29 -0600196 POWER_SUPPLY_ENUM_ATTR(TYPE),
Mathew King2cabeaf2020-05-04 14:29:27 -0600197 POWER_SUPPLY_ATTR(USB_TYPE),
Mathew King5b505362020-05-04 14:29:29 -0600198 POWER_SUPPLY_ENUM_ATTR(SCOPE),
Mathew King2cabeaf2020-05-04 14:29:27 -0600199 POWER_SUPPLY_ATTR(PRECHARGE_CURRENT),
200 POWER_SUPPLY_ATTR(CHARGE_TERM_CURRENT),
201 POWER_SUPPLY_ATTR(CALIBRATE),
Sebastian Reichelfeabe492020-05-13 20:55:59 +0200202 POWER_SUPPLY_ATTR(MANUFACTURE_YEAR),
203 POWER_SUPPLY_ATTR(MANUFACTURE_MONTH),
204 POWER_SUPPLY_ATTR(MANUFACTURE_DAY),
Mathew King2cabeaf2020-05-04 14:29:27 -0600205 /* Properties of type `const char *' */
206 POWER_SUPPLY_ATTR(MODEL_NAME),
207 POWER_SUPPLY_ATTR(MANUFACTURER),
208 POWER_SUPPLY_ATTR(SERIAL_NUMBER),
209};
210
211static struct attribute *
212__power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
213
Mathew King5b505362020-05-04 14:29:29 -0600214static 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
Mathew King2cabeaf2020-05-04 14:29:27 -0600219static enum power_supply_property dev_attr_psp(struct device_attribute *attr)
220{
Mathew King5b505362020-05-04 14:29:29 -0600221 return to_ps_attr(attr) - power_supply_attrs;
Mathew King2cabeaf2020-05-04 14:29:27 -0600222}
223
Adam Thomsoncf450042018-04-23 15:10:59 +0100224static ssize_t power_supply_show_usb_type(struct device *dev,
Michał Mirosławde46e022020-04-03 22:20:32 +0200225 const struct power_supply_desc *desc,
Adam Thomsoncf450042018-04-23 15:10:59 +0100226 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
Michał Mirosławde46e022020-04-03 22:20:32 +0200234 for (i = 0; i < desc->num_usb_types; ++i) {
235 usb_type = desc->usb_types[i];
Adam Thomsoncf450042018-04-23 15:10:59 +0100236
237 if (value->intval == usb_type) {
238 count += sprintf(buf + count, "[%s] ",
Mathew King5b505362020-05-04 14:29:29 -0600239 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
Adam Thomsoncf450042018-04-23 15:10:59 +0100240 match = true;
241 } else {
242 count += sprintf(buf + count, "%s ",
Mathew King5b505362020-05-04 14:29:29 -0600243 POWER_SUPPLY_USB_TYPE_TEXT[usb_type]);
Adam Thomsoncf450042018-04-23 15:10:59 +0100244 }
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
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400258static ssize_t power_supply_show_property(struct device *dev,
259 struct device_attribute *attr,
260 char *buf) {
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100261 ssize_t ret;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400262 struct power_supply *psy = dev_get_drvdata(dev);
Mathew King5b505362020-05-04 14:29:29 -0600263 struct power_supply_attr *ps_attr = to_ps_attr(attr);
Mathew King2cabeaf2020-05-04 14:29:27 -0600264 enum power_supply_property psp = dev_attr_psp(attr);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400265 union power_supply_propval value;
266
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100267 if (psp == POWER_SUPPLY_PROP_TYPE) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100268 value.intval = psy->desc->type;
Viresh Kumar73b4a082014-09-04 17:31:34 +0530269 } else {
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100270 ret = power_supply_get_property(psy, psp, &value);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400271
Viresh Kumar73b4a082014-09-04 17:31:34 +0530272 if (ret < 0) {
273 if (ret == -ENODATA)
274 dev_dbg(dev, "driver has no data for `%s' property\n",
275 attr->attr.name);
Rhyland Kleine3805382016-06-22 11:45:52 -0400276 else if (ret != -ENODEV && ret != -EAGAIN)
David Lechner87a2b652018-09-12 19:48:30 -0500277 dev_err_ratelimited(dev,
278 "driver failed to report `%s' property: %zd\n",
Viresh Kumar73b4a082014-09-04 17:31:34 +0530279 attr->attr.name, ret);
280 return ret;
281 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400282 }
283
Mathew King5b505362020-05-04 14:29:29 -0600284 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
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100289 switch (psp) {
Sebastian Reichel1f140ff2018-04-26 12:18:30 +0200290 case POWER_SUPPLY_PROP_USB_TYPE:
Michał Mirosławde46e022020-04-03 22:20:32 +0200291 ret = power_supply_show_usb_type(dev, psy->desc,
Mathew King5b505362020-05-04 14:29:29 -0600292 &value, buf);
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100293 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 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400300
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100301 return ret;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400302}
303
Daniel Mack0011d2d2010-05-18 21:49:52 +0200304static 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);
Mathew King5b505362020-05-04 14:29:29 -0600309 struct power_supply_attr *ps_attr = to_ps_attr(attr);
Mathew King2cabeaf2020-05-04 14:29:27 -0600310 enum power_supply_property psp = dev_attr_psp(attr);
Daniel Mack0011d2d2010-05-18 21:49:52 +0200311 union power_supply_propval value;
Daniel Mack0011d2d2010-05-18 21:49:52 +0200312
Mathew King5b505362020-05-04 14:29:29 -0600313 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);
David Lechner105df602017-05-15 16:21:15 -0500317 }
Daniel Mack0011d2d2010-05-18 21:49:52 +0200318
David Lechner105df602017-05-15 16:21:15 -0500319 /*
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;
Daniel Mack0011d2d2010-05-18 21:49:52 +0200334
Ladislav Michl39b4fb82018-03-21 16:54:53 +0100335 ret = power_supply_set_property(psy, psp, &value);
Daniel Mack0011d2d2010-05-18 21:49:52 +0200336 if (ret < 0)
337 return ret;
338
339 return count;
340}
341
Al Viro587a1f12011-07-23 23:11:19 -0400342static umode_t power_supply_attr_is_visible(struct kobject *kobj,
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200343 struct attribute *attr,
344 int attrno)
345{
Wang Qing224941c2020-06-12 14:55:54 +0800346 struct device *dev = kobj_to_dev(kobj);
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200347 struct power_supply *psy = dev_get_drvdata(dev);
Al Viro587a1f12011-07-23 23:11:19 -0400348 umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200349 int i;
350
Mathew King2cabeaf2020-05-04 14:29:27 -0600351 if (!power_supply_attrs[attrno].prop_name)
352 return 0;
353
Daniel Mackbbabb152010-05-25 02:39:45 +0200354 if (attrno == POWER_SUPPLY_PROP_TYPE)
355 return mode;
356
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100357 for (i = 0; i < psy->desc->num_properties; i++) {
358 int property = psy->desc->properties[i];
Daniel Mack0011d2d2010-05-18 21:49:52 +0200359
360 if (property == attrno) {
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100361 if (psy->desc->property_is_writeable &&
Krzysztof Kozlowski5c6e3a92015-06-08 10:09:48 +0900362 psy->desc->property_is_writeable(psy, property) > 0)
Daniel Mack0011d2d2010-05-18 21:49:52 +0200363 mode |= S_IWUSR;
364
365 return mode;
366 }
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200367 }
368
369 return 0;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400370}
371
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200372static struct attribute_group power_supply_attr_group = {
373 .attrs = __power_supply_attrs,
374 .is_visible = power_supply_attr_is_visible,
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400375};
376
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200377static const struct attribute_group *power_supply_attr_groups[] = {
378 &power_supply_attr_group,
379 NULL,
380};
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400381
Mathew King2cabeaf2020-05-04 14:29:27 -0600382static void str_to_lower(char *str)
383{
384 while (*str) {
385 *str = tolower(*str);
386 str++;
387 }
388}
389
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200390void power_supply_init_attrs(struct device_type *dev_type)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400391{
392 int i;
393
Anton Vorontsov5f487cd2010-05-18 21:49:51 +0200394 dev_type->groups = power_supply_attr_groups;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400395
Mathew King2cabeaf2020-05-04 14:29:27 -0600396 for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++) {
397 struct device_attribute *attr;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400398
Mathew King2cabeaf2020-05-04 14:29:27 -0600399 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 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400406
Mathew King2cabeaf2020-05-04 14:29:27 -0600407 attr = &power_supply_attrs[i].dev_attr;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400408
Mathew King2cabeaf2020-05-04 14:29:27 -0600409 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 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400414}
415
Mathew King2ad3d74e2020-05-04 14:29:30 -0600416static 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
Kay Sievers7eff2e72007-08-14 15:15:12 +0200447int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400448{
449 struct power_supply *psy = dev_get_drvdata(dev);
Kay Sievers7eff2e72007-08-14 15:15:12 +0200450 int ret = 0, j;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400451 char *prop_buf;
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400452
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100453 if (!psy || !psy->desc) {
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400454 dev_dbg(dev, "No power supply yet\n");
455 return ret;
456 }
457
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100458 ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400459 if (ret)
460 return ret;
461
462 prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
463 if (!prop_buf)
464 return -ENOMEM;
465
Mathew King2ad3d74e2020-05-04 14:29:30 -0600466 ret = add_prop_uevent(dev, env, POWER_SUPPLY_PROP_TYPE, prop_buf);
467 if (ret)
468 goto out;
469
Krzysztof Kozlowski297d7162015-03-12 08:44:11 +0100470 for (j = 0; j < psy->desc->num_properties; j++) {
Mathew King2ad3d74e2020-05-04 14:29:30 -0600471 ret = add_prop_uevent(dev, env, psy->desc->properties[j],
472 prop_buf);
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400473 if (ret)
474 goto out;
475 }
Anton Vorontsov4a11b592007-05-04 00:27:45 +0400476
477out:
478 free_page((unsigned long)prop_buf);
479
480 return ret;
481}