Thomas Gleixner | a63a5fa | 2019-05-28 10:10:21 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 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 |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | #include <linux/ctype.h> |
Paul Gortmaker | 51990e8 | 2012-01-22 11:23:42 -0500 | [diff] [blame] | 14 | #include <linux/device.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 15 | #include <linux/power_supply.h> |
Tejun Heo | 5a0e3ad | 2010-03-24 17:04:11 +0900 | [diff] [blame] | 16 | #include <linux/slab.h> |
Paul Gortmaker | 778f523 | 2011-05-27 10:22:46 -0400 | [diff] [blame] | 17 | #include <linux/stat.h> |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 18 | |
Adrian Bunk | 25f12141 | 2007-11-26 00:25:45 +0300 | [diff] [blame] | 19 | #include "power_supply.h" |
| 20 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 21 | #define MAX_PROP_NAME_LEN 30 |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 22 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 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; |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 27 | const char * const *text_values; |
| 28 | int text_values_len; |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 29 | }; |
| 30 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 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, \ |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 38 | } |
| 39 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 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[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 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", |
Subbaraman Narayanamurthy | 5ca937f | 2020-08-13 11:34:08 -0700 | [diff] [blame] | 59 | [POWER_SUPPLY_TYPE_WIRELESS] = "Wireless", |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 62 | static const char * const POWER_SUPPLY_USB_TYPE_TEXT[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 63 | [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 Thomson | cf45004 | 2018-04-23 15:10:59 +0100 | [diff] [blame] | 73 | }; |
| 74 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 75 | static const char * const POWER_SUPPLY_STATUS_TEXT[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 76 | [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 Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 81 | }; |
| 82 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 83 | static const char * const POWER_SUPPLY_CHARGE_TYPE_TEXT[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 84 | [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 Wang | 46cbd0b | 2020-07-30 11:26:09 +0800 | [diff] [blame] | 91 | [POWER_SUPPLY_CHARGE_TYPE_LONGLIFE] = "Long Life", |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 92 | }; |
| 93 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 94 | static const char * const POWER_SUPPLY_HEALTH_TEXT[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 95 | [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 Reichel | 601c2a5 | 2020-05-13 20:56:00 +0200 | [diff] [blame] | 105 | [POWER_SUPPLY_HEALTH_CALIBRATION_REQUIRED] = "Calibration required", |
Dan Murphy | 98cc1b9 | 2020-07-20 15:43:57 -0500 | [diff] [blame] | 106 | [POWER_SUPPLY_HEALTH_WARM] = "Warm", |
| 107 | [POWER_SUPPLY_HEALTH_COOL] = "Cool", |
| 108 | [POWER_SUPPLY_HEALTH_HOT] = "Hot", |
Luca Ceresoli | 77d641b | 2021-11-08 17:27:05 +0100 | [diff] [blame] | 109 | [POWER_SUPPLY_HEALTH_NO_BATTERY] = "No battery", |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 110 | }; |
| 111 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 112 | static const char * const POWER_SUPPLY_TECHNOLOGY_TEXT[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 113 | [POWER_SUPPLY_TECHNOLOGY_UNKNOWN] = "Unknown", |
| 114 | [POWER_SUPPLY_TECHNOLOGY_NiMH] = "NiMH", |
| 115 | [POWER_SUPPLY_TECHNOLOGY_LION] = "Li-ion", |
| 116 | [POWER_SUPPLY_TECHNOLOGY_LIPO] = "Li-poly", |
| 117 | [POWER_SUPPLY_TECHNOLOGY_LiFe] = "LiFe", |
| 118 | [POWER_SUPPLY_TECHNOLOGY_NiCd] = "NiCd", |
| 119 | [POWER_SUPPLY_TECHNOLOGY_LiMn] = "LiMn", |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 120 | }; |
| 121 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 122 | static const char * const POWER_SUPPLY_CAPACITY_LEVEL_TEXT[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 123 | [POWER_SUPPLY_CAPACITY_LEVEL_UNKNOWN] = "Unknown", |
| 124 | [POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL] = "Critical", |
| 125 | [POWER_SUPPLY_CAPACITY_LEVEL_LOW] = "Low", |
| 126 | [POWER_SUPPLY_CAPACITY_LEVEL_NORMAL] = "Normal", |
| 127 | [POWER_SUPPLY_CAPACITY_LEVEL_HIGH] = "High", |
| 128 | [POWER_SUPPLY_CAPACITY_LEVEL_FULL] = "Full", |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 129 | }; |
| 130 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 131 | static const char * const POWER_SUPPLY_SCOPE_TEXT[] = { |
Mathew King | d960d91 | 2020-05-04 14:29:28 -0600 | [diff] [blame] | 132 | [POWER_SUPPLY_SCOPE_UNKNOWN] = "Unknown", |
| 133 | [POWER_SUPPLY_SCOPE_SYSTEM] = "System", |
| 134 | [POWER_SUPPLY_SCOPE_DEVICE] = "Device", |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 135 | }; |
| 136 | |
Thomas Weißschuh | 539b9c9 | 2021-11-24 00:27:02 +0100 | [diff] [blame] | 137 | static const char * const POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[] = { |
| 138 | [POWER_SUPPLY_CHARGE_BEHAVIOUR_AUTO] = "auto", |
| 139 | [POWER_SUPPLY_CHARGE_BEHAVIOUR_INHIBIT_CHARGE] = "inhibit-charge", |
| 140 | [POWER_SUPPLY_CHARGE_BEHAVIOUR_FORCE_DISCHARGE] = "force-discharge", |
| 141 | }; |
| 142 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 143 | static struct power_supply_attr power_supply_attrs[] = { |
| 144 | /* Properties of type `int' */ |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 145 | POWER_SUPPLY_ENUM_ATTR(STATUS), |
| 146 | POWER_SUPPLY_ENUM_ATTR(CHARGE_TYPE), |
| 147 | POWER_SUPPLY_ENUM_ATTR(HEALTH), |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 148 | POWER_SUPPLY_ATTR(PRESENT), |
| 149 | POWER_SUPPLY_ATTR(ONLINE), |
| 150 | POWER_SUPPLY_ATTR(AUTHENTIC), |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 151 | POWER_SUPPLY_ENUM_ATTR(TECHNOLOGY), |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 152 | POWER_SUPPLY_ATTR(CYCLE_COUNT), |
| 153 | POWER_SUPPLY_ATTR(VOLTAGE_MAX), |
| 154 | POWER_SUPPLY_ATTR(VOLTAGE_MIN), |
| 155 | POWER_SUPPLY_ATTR(VOLTAGE_MAX_DESIGN), |
| 156 | POWER_SUPPLY_ATTR(VOLTAGE_MIN_DESIGN), |
| 157 | POWER_SUPPLY_ATTR(VOLTAGE_NOW), |
| 158 | POWER_SUPPLY_ATTR(VOLTAGE_AVG), |
| 159 | POWER_SUPPLY_ATTR(VOLTAGE_OCV), |
| 160 | POWER_SUPPLY_ATTR(VOLTAGE_BOOT), |
| 161 | POWER_SUPPLY_ATTR(CURRENT_MAX), |
| 162 | POWER_SUPPLY_ATTR(CURRENT_NOW), |
| 163 | POWER_SUPPLY_ATTR(CURRENT_AVG), |
| 164 | POWER_SUPPLY_ATTR(CURRENT_BOOT), |
| 165 | POWER_SUPPLY_ATTR(POWER_NOW), |
| 166 | POWER_SUPPLY_ATTR(POWER_AVG), |
| 167 | POWER_SUPPLY_ATTR(CHARGE_FULL_DESIGN), |
| 168 | POWER_SUPPLY_ATTR(CHARGE_EMPTY_DESIGN), |
| 169 | POWER_SUPPLY_ATTR(CHARGE_FULL), |
| 170 | POWER_SUPPLY_ATTR(CHARGE_EMPTY), |
| 171 | POWER_SUPPLY_ATTR(CHARGE_NOW), |
| 172 | POWER_SUPPLY_ATTR(CHARGE_AVG), |
| 173 | POWER_SUPPLY_ATTR(CHARGE_COUNTER), |
| 174 | POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT), |
| 175 | POWER_SUPPLY_ATTR(CONSTANT_CHARGE_CURRENT_MAX), |
| 176 | POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE), |
| 177 | POWER_SUPPLY_ATTR(CONSTANT_CHARGE_VOLTAGE_MAX), |
| 178 | POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT), |
| 179 | POWER_SUPPLY_ATTR(CHARGE_CONTROL_LIMIT_MAX), |
| 180 | POWER_SUPPLY_ATTR(CHARGE_CONTROL_START_THRESHOLD), |
| 181 | POWER_SUPPLY_ATTR(CHARGE_CONTROL_END_THRESHOLD), |
Thomas Weißschuh | c8e2d92 | 2022-01-05 07:42:38 +0100 | [diff] [blame] | 182 | POWER_SUPPLY_ENUM_ATTR(CHARGE_BEHAVIOUR), |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 183 | POWER_SUPPLY_ATTR(INPUT_CURRENT_LIMIT), |
| 184 | POWER_SUPPLY_ATTR(INPUT_VOLTAGE_LIMIT), |
| 185 | POWER_SUPPLY_ATTR(INPUT_POWER_LIMIT), |
| 186 | POWER_SUPPLY_ATTR(ENERGY_FULL_DESIGN), |
| 187 | POWER_SUPPLY_ATTR(ENERGY_EMPTY_DESIGN), |
| 188 | POWER_SUPPLY_ATTR(ENERGY_FULL), |
| 189 | POWER_SUPPLY_ATTR(ENERGY_EMPTY), |
| 190 | POWER_SUPPLY_ATTR(ENERGY_NOW), |
| 191 | POWER_SUPPLY_ATTR(ENERGY_AVG), |
| 192 | POWER_SUPPLY_ATTR(CAPACITY), |
| 193 | POWER_SUPPLY_ATTR(CAPACITY_ALERT_MIN), |
| 194 | POWER_SUPPLY_ATTR(CAPACITY_ALERT_MAX), |
Sebastian Reichel | bac705a | 2020-05-13 20:55:58 +0200 | [diff] [blame] | 195 | POWER_SUPPLY_ATTR(CAPACITY_ERROR_MARGIN), |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 196 | POWER_SUPPLY_ENUM_ATTR(CAPACITY_LEVEL), |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 197 | POWER_SUPPLY_ATTR(TEMP), |
| 198 | POWER_SUPPLY_ATTR(TEMP_MAX), |
| 199 | POWER_SUPPLY_ATTR(TEMP_MIN), |
| 200 | POWER_SUPPLY_ATTR(TEMP_ALERT_MIN), |
| 201 | POWER_SUPPLY_ATTR(TEMP_ALERT_MAX), |
| 202 | POWER_SUPPLY_ATTR(TEMP_AMBIENT), |
| 203 | POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MIN), |
| 204 | POWER_SUPPLY_ATTR(TEMP_AMBIENT_ALERT_MAX), |
| 205 | POWER_SUPPLY_ATTR(TIME_TO_EMPTY_NOW), |
| 206 | POWER_SUPPLY_ATTR(TIME_TO_EMPTY_AVG), |
| 207 | POWER_SUPPLY_ATTR(TIME_TO_FULL_NOW), |
| 208 | POWER_SUPPLY_ATTR(TIME_TO_FULL_AVG), |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 209 | POWER_SUPPLY_ENUM_ATTR(TYPE), |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 210 | POWER_SUPPLY_ATTR(USB_TYPE), |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 211 | POWER_SUPPLY_ENUM_ATTR(SCOPE), |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 212 | POWER_SUPPLY_ATTR(PRECHARGE_CURRENT), |
| 213 | POWER_SUPPLY_ATTR(CHARGE_TERM_CURRENT), |
| 214 | POWER_SUPPLY_ATTR(CALIBRATE), |
Sebastian Reichel | feabe49 | 2020-05-13 20:55:59 +0200 | [diff] [blame] | 215 | POWER_SUPPLY_ATTR(MANUFACTURE_YEAR), |
| 216 | POWER_SUPPLY_ATTR(MANUFACTURE_MONTH), |
| 217 | POWER_SUPPLY_ATTR(MANUFACTURE_DAY), |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 218 | /* Properties of type `const char *' */ |
| 219 | POWER_SUPPLY_ATTR(MODEL_NAME), |
| 220 | POWER_SUPPLY_ATTR(MANUFACTURER), |
| 221 | POWER_SUPPLY_ATTR(SERIAL_NUMBER), |
| 222 | }; |
| 223 | |
| 224 | static struct attribute * |
| 225 | __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1]; |
| 226 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 227 | static struct power_supply_attr *to_ps_attr(struct device_attribute *attr) |
| 228 | { |
| 229 | return container_of(attr, struct power_supply_attr, dev_attr); |
| 230 | } |
| 231 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 232 | static enum power_supply_property dev_attr_psp(struct device_attribute *attr) |
| 233 | { |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 234 | return to_ps_attr(attr) - power_supply_attrs; |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 235 | } |
| 236 | |
Adam Thomson | cf45004 | 2018-04-23 15:10:59 +0100 | [diff] [blame] | 237 | static ssize_t power_supply_show_usb_type(struct device *dev, |
Michał Mirosław | de46e02 | 2020-04-03 22:20:32 +0200 | [diff] [blame] | 238 | const struct power_supply_desc *desc, |
Adam Thomson | cf45004 | 2018-04-23 15:10:59 +0100 | [diff] [blame] | 239 | union power_supply_propval *value, |
| 240 | char *buf) |
| 241 | { |
| 242 | enum power_supply_usb_type usb_type; |
| 243 | ssize_t count = 0; |
| 244 | bool match = false; |
| 245 | int i; |
| 246 | |
Michał Mirosław | de46e02 | 2020-04-03 22:20:32 +0200 | [diff] [blame] | 247 | for (i = 0; i < desc->num_usb_types; ++i) { |
| 248 | usb_type = desc->usb_types[i]; |
Adam Thomson | cf45004 | 2018-04-23 15:10:59 +0100 | [diff] [blame] | 249 | |
| 250 | if (value->intval == usb_type) { |
| 251 | count += sprintf(buf + count, "[%s] ", |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 252 | POWER_SUPPLY_USB_TYPE_TEXT[usb_type]); |
Adam Thomson | cf45004 | 2018-04-23 15:10:59 +0100 | [diff] [blame] | 253 | match = true; |
| 254 | } else { |
| 255 | count += sprintf(buf + count, "%s ", |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 256 | POWER_SUPPLY_USB_TYPE_TEXT[usb_type]); |
Adam Thomson | cf45004 | 2018-04-23 15:10:59 +0100 | [diff] [blame] | 257 | } |
| 258 | } |
| 259 | |
| 260 | if (!match) { |
| 261 | dev_warn(dev, "driver reporting unsupported connected type\n"); |
| 262 | return -EINVAL; |
| 263 | } |
| 264 | |
| 265 | if (count) |
| 266 | buf[count - 1] = '\n'; |
| 267 | |
| 268 | return count; |
| 269 | } |
| 270 | |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 271 | static ssize_t power_supply_show_property(struct device *dev, |
| 272 | struct device_attribute *attr, |
| 273 | char *buf) { |
Ladislav Michl | 39b4fb8 | 2018-03-21 16:54:53 +0100 | [diff] [blame] | 274 | ssize_t ret; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 275 | struct power_supply *psy = dev_get_drvdata(dev); |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 276 | struct power_supply_attr *ps_attr = to_ps_attr(attr); |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 277 | enum power_supply_property psp = dev_attr_psp(attr); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 278 | union power_supply_propval value; |
| 279 | |
Ladislav Michl | 39b4fb8 | 2018-03-21 16:54:53 +0100 | [diff] [blame] | 280 | if (psp == POWER_SUPPLY_PROP_TYPE) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 281 | value.intval = psy->desc->type; |
Viresh Kumar | 73b4a08 | 2014-09-04 17:31:34 +0530 | [diff] [blame] | 282 | } else { |
Ladislav Michl | 39b4fb8 | 2018-03-21 16:54:53 +0100 | [diff] [blame] | 283 | ret = power_supply_get_property(psy, psp, &value); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 284 | |
Viresh Kumar | 73b4a08 | 2014-09-04 17:31:34 +0530 | [diff] [blame] | 285 | if (ret < 0) { |
| 286 | if (ret == -ENODATA) |
| 287 | dev_dbg(dev, "driver has no data for `%s' property\n", |
| 288 | attr->attr.name); |
Rhyland Klein | e380538 | 2016-06-22 11:45:52 -0400 | [diff] [blame] | 289 | else if (ret != -ENODEV && ret != -EAGAIN) |
David Lechner | 87a2b65 | 2018-09-12 19:48:30 -0500 | [diff] [blame] | 290 | dev_err_ratelimited(dev, |
| 291 | "driver failed to report `%s' property: %zd\n", |
Viresh Kumar | 73b4a08 | 2014-09-04 17:31:34 +0530 | [diff] [blame] | 292 | attr->attr.name, ret); |
| 293 | return ret; |
| 294 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 295 | } |
| 296 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 297 | if (ps_attr->text_values_len > 0 && |
| 298 | value.intval < ps_attr->text_values_len && value.intval >= 0) { |
| 299 | return sprintf(buf, "%s\n", ps_attr->text_values[value.intval]); |
| 300 | } |
| 301 | |
Ladislav Michl | 39b4fb8 | 2018-03-21 16:54:53 +0100 | [diff] [blame] | 302 | switch (psp) { |
Sebastian Reichel | 1f140ff | 2018-04-26 12:18:30 +0200 | [diff] [blame] | 303 | case POWER_SUPPLY_PROP_USB_TYPE: |
Michał Mirosław | de46e02 | 2020-04-03 22:20:32 +0200 | [diff] [blame] | 304 | ret = power_supply_show_usb_type(dev, psy->desc, |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 305 | &value, buf); |
Ladislav Michl | 39b4fb8 | 2018-03-21 16:54:53 +0100 | [diff] [blame] | 306 | break; |
| 307 | case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER: |
| 308 | ret = sprintf(buf, "%s\n", value.strval); |
| 309 | break; |
| 310 | default: |
| 311 | ret = sprintf(buf, "%d\n", value.intval); |
| 312 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 313 | |
Ladislav Michl | 39b4fb8 | 2018-03-21 16:54:53 +0100 | [diff] [blame] | 314 | return ret; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 315 | } |
| 316 | |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 317 | static ssize_t power_supply_store_property(struct device *dev, |
| 318 | struct device_attribute *attr, |
| 319 | const char *buf, size_t count) { |
| 320 | ssize_t ret; |
| 321 | struct power_supply *psy = dev_get_drvdata(dev); |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 322 | struct power_supply_attr *ps_attr = to_ps_attr(attr); |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 323 | enum power_supply_property psp = dev_attr_psp(attr); |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 324 | union power_supply_propval value; |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 325 | |
Mathew King | 5b50536 | 2020-05-04 14:29:29 -0600 | [diff] [blame] | 326 | ret = -EINVAL; |
| 327 | if (ps_attr->text_values_len > 0) { |
| 328 | ret = __sysfs_match_string(ps_attr->text_values, |
| 329 | ps_attr->text_values_len, buf); |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 330 | } |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 331 | |
David Lechner | 105df60 | 2017-05-15 16:21:15 -0500 | [diff] [blame] | 332 | /* |
| 333 | * If no match was found, then check to see if it is an integer. |
| 334 | * Integer values are valid for enums in addition to the text value. |
| 335 | */ |
| 336 | if (ret < 0) { |
| 337 | long long_val; |
| 338 | |
| 339 | ret = kstrtol(buf, 10, &long_val); |
| 340 | if (ret < 0) |
| 341 | return ret; |
| 342 | |
| 343 | ret = long_val; |
| 344 | } |
| 345 | |
| 346 | value.intval = ret; |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 347 | |
Ladislav Michl | 39b4fb8 | 2018-03-21 16:54:53 +0100 | [diff] [blame] | 348 | ret = power_supply_set_property(psy, psp, &value); |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 349 | if (ret < 0) |
| 350 | return ret; |
| 351 | |
| 352 | return count; |
| 353 | } |
| 354 | |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 355 | static umode_t power_supply_attr_is_visible(struct kobject *kobj, |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 356 | struct attribute *attr, |
| 357 | int attrno) |
| 358 | { |
Wang Qing | 224941c | 2020-06-12 14:55:54 +0800 | [diff] [blame] | 359 | struct device *dev = kobj_to_dev(kobj); |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 360 | struct power_supply *psy = dev_get_drvdata(dev); |
Al Viro | 587a1f1 | 2011-07-23 23:11:19 -0400 | [diff] [blame] | 361 | umode_t mode = S_IRUSR | S_IRGRP | S_IROTH; |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 362 | int i; |
| 363 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 364 | if (!power_supply_attrs[attrno].prop_name) |
| 365 | return 0; |
| 366 | |
Daniel Mack | bbabb15 | 2010-05-25 02:39:45 +0200 | [diff] [blame] | 367 | if (attrno == POWER_SUPPLY_PROP_TYPE) |
| 368 | return mode; |
| 369 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 370 | for (i = 0; i < psy->desc->num_properties; i++) { |
| 371 | int property = psy->desc->properties[i]; |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 372 | |
| 373 | if (property == attrno) { |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 374 | if (psy->desc->property_is_writeable && |
Krzysztof Kozlowski | 5c6e3a9 | 2015-06-08 10:09:48 +0900 | [diff] [blame] | 375 | psy->desc->property_is_writeable(psy, property) > 0) |
Daniel Mack | 0011d2d | 2010-05-18 21:49:52 +0200 | [diff] [blame] | 376 | mode |= S_IWUSR; |
| 377 | |
| 378 | return mode; |
| 379 | } |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 380 | } |
| 381 | |
| 382 | return 0; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 383 | } |
| 384 | |
Rikard Falkeborn | 2f7cf24 | 2021-01-13 21:32:43 +0100 | [diff] [blame] | 385 | static const struct attribute_group power_supply_attr_group = { |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 386 | .attrs = __power_supply_attrs, |
| 387 | .is_visible = power_supply_attr_is_visible, |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 388 | }; |
| 389 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 390 | static const struct attribute_group *power_supply_attr_groups[] = { |
| 391 | &power_supply_attr_group, |
| 392 | NULL, |
| 393 | }; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 394 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 395 | static void str_to_lower(char *str) |
| 396 | { |
| 397 | while (*str) { |
| 398 | *str = tolower(*str); |
| 399 | str++; |
| 400 | } |
| 401 | } |
| 402 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 403 | void power_supply_init_attrs(struct device_type *dev_type) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 404 | { |
| 405 | int i; |
| 406 | |
Anton Vorontsov | 5f487cd | 2010-05-18 21:49:51 +0200 | [diff] [blame] | 407 | dev_type->groups = power_supply_attr_groups; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 408 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 409 | for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++) { |
| 410 | struct device_attribute *attr; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 411 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 412 | if (!power_supply_attrs[i].prop_name) { |
Masanari Iida | c236251 | 2020-12-05 10:25:32 +0900 | [diff] [blame] | 413 | pr_warn("%s: Property %d skipped because it is missing from power_supply_attrs\n", |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 414 | __func__, i); |
| 415 | sprintf(power_supply_attrs[i].attr_name, "_err_%d", i); |
| 416 | } else { |
| 417 | str_to_lower(power_supply_attrs[i].attr_name); |
| 418 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 419 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 420 | attr = &power_supply_attrs[i].dev_attr; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 421 | |
Mathew King | 2cabeaf | 2020-05-04 14:29:27 -0600 | [diff] [blame] | 422 | attr->attr.name = power_supply_attrs[i].attr_name; |
| 423 | attr->show = power_supply_show_property; |
| 424 | attr->store = power_supply_store_property; |
| 425 | __power_supply_attrs[i] = &attr->attr; |
| 426 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 427 | } |
| 428 | |
Mathew King | 2ad3d74e | 2020-05-04 14:29:30 -0600 | [diff] [blame] | 429 | static int add_prop_uevent(struct device *dev, struct kobj_uevent_env *env, |
| 430 | enum power_supply_property prop, char *prop_buf) |
| 431 | { |
| 432 | int ret = 0; |
| 433 | struct power_supply_attr *pwr_attr; |
| 434 | struct device_attribute *dev_attr; |
| 435 | char *line; |
| 436 | |
| 437 | pwr_attr = &power_supply_attrs[prop]; |
| 438 | dev_attr = &pwr_attr->dev_attr; |
| 439 | |
| 440 | ret = power_supply_show_property(dev, dev_attr, prop_buf); |
| 441 | if (ret == -ENODEV || ret == -ENODATA) { |
| 442 | /* |
| 443 | * When a battery is absent, we expect -ENODEV. Don't abort; |
| 444 | * send the uevent with at least the the PRESENT=0 property |
| 445 | */ |
| 446 | return 0; |
| 447 | } |
| 448 | |
| 449 | if (ret < 0) |
| 450 | return ret; |
| 451 | |
| 452 | line = strchr(prop_buf, '\n'); |
| 453 | if (line) |
| 454 | *line = 0; |
| 455 | |
| 456 | return add_uevent_var(env, "POWER_SUPPLY_%s=%s", |
| 457 | pwr_attr->prop_name, prop_buf); |
| 458 | } |
| 459 | |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 460 | int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env) |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 461 | { |
| 462 | struct power_supply *psy = dev_get_drvdata(dev); |
Kay Sievers | 7eff2e7 | 2007-08-14 15:15:12 +0200 | [diff] [blame] | 463 | int ret = 0, j; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 464 | char *prop_buf; |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 465 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 466 | if (!psy || !psy->desc) { |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 467 | dev_dbg(dev, "No power supply yet\n"); |
| 468 | return ret; |
| 469 | } |
| 470 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 471 | ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 472 | if (ret) |
| 473 | return ret; |
| 474 | |
| 475 | prop_buf = (char *)get_zeroed_page(GFP_KERNEL); |
| 476 | if (!prop_buf) |
| 477 | return -ENOMEM; |
| 478 | |
Mathew King | 2ad3d74e | 2020-05-04 14:29:30 -0600 | [diff] [blame] | 479 | ret = add_prop_uevent(dev, env, POWER_SUPPLY_PROP_TYPE, prop_buf); |
| 480 | if (ret) |
| 481 | goto out; |
| 482 | |
Krzysztof Kozlowski | 297d716 | 2015-03-12 08:44:11 +0100 | [diff] [blame] | 483 | for (j = 0; j < psy->desc->num_properties; j++) { |
Mathew King | 2ad3d74e | 2020-05-04 14:29:30 -0600 | [diff] [blame] | 484 | ret = add_prop_uevent(dev, env, psy->desc->properties[j], |
| 485 | prop_buf); |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 486 | if (ret) |
| 487 | goto out; |
| 488 | } |
Anton Vorontsov | 4a11b59 | 2007-05-04 00:27:45 +0400 | [diff] [blame] | 489 | |
| 490 | out: |
| 491 | free_page((unsigned long)prop_buf); |
| 492 | |
| 493 | return ret; |
| 494 | } |
Thomas Weißschuh | 539b9c9 | 2021-11-24 00:27:02 +0100 | [diff] [blame] | 495 | |
| 496 | ssize_t power_supply_charge_behaviour_show(struct device *dev, |
| 497 | unsigned int available_behaviours, |
| 498 | enum power_supply_charge_behaviour current_behaviour, |
| 499 | char *buf) |
| 500 | { |
| 501 | bool match = false, available, active; |
| 502 | ssize_t count = 0; |
| 503 | int i; |
| 504 | |
| 505 | for (i = 0; i < ARRAY_SIZE(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT); i++) { |
| 506 | available = available_behaviours & BIT(i); |
| 507 | active = i == current_behaviour; |
| 508 | |
| 509 | if (available && active) { |
| 510 | count += sysfs_emit_at(buf, count, "[%s] ", |
| 511 | POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]); |
| 512 | match = true; |
| 513 | } else if (available) { |
| 514 | count += sysfs_emit_at(buf, count, "%s ", |
| 515 | POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT[i]); |
| 516 | } |
| 517 | } |
| 518 | |
| 519 | if (!match) { |
| 520 | dev_warn(dev, "driver reporting unsupported charge behaviour\n"); |
| 521 | return -EINVAL; |
| 522 | } |
| 523 | |
| 524 | if (count) |
| 525 | buf[count - 1] = '\n'; |
| 526 | |
| 527 | return count; |
| 528 | } |
| 529 | EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_show); |
| 530 | |
| 531 | int power_supply_charge_behaviour_parse(unsigned int available_behaviours, const char *buf) |
| 532 | { |
| 533 | int i = sysfs_match_string(POWER_SUPPLY_CHARGE_BEHAVIOUR_TEXT, buf); |
| 534 | |
| 535 | if (i < 0) |
| 536 | return i; |
| 537 | |
| 538 | if (available_behaviours & BIT(i)) |
| 539 | return i; |
| 540 | |
| 541 | return -EINVAL; |
| 542 | } |
| 543 | EXPORT_SYMBOL_GPL(power_supply_charge_behaviour_parse); |