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