power_supply_sysfs.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437
  1. /*
  2. * Sysfs interface for the universal power supply monitor class
  3. *
  4. * Copyright © 2007 David Woodhouse <dwmw2@infradead.org>
  5. * Copyright © 2007 Anton Vorontsov <cbou@mail.ru>
  6. * Copyright © 2004 Szabolcs Gyurko
  7. * Copyright © 2003 Ian Molton <spyro@f2s.com>
  8. *
  9. * Modified: 2004, Oct Szabolcs Gyurko
  10. *
  11. * You may use this code as per GPL version 2
  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 "power_supply.h"
  19. /*
  20. * This is because the name "current" breaks the device attr macro.
  21. * The "current" word resolves to "(get_current())" so instead of
  22. * "current" "(get_current())" appears in the sysfs.
  23. *
  24. * The source of this definition is the device.h which calls __ATTR
  25. * macro in sysfs.h which calls the __stringify macro.
  26. *
  27. * Only modification that the name is not tried to be resolved
  28. * (as a macro let's say).
  29. */
  30. #define POWER_SUPPLY_ATTR(_name) \
  31. { \
  32. .attr = { .name = #_name }, \
  33. .show = power_supply_show_property, \
  34. .store = power_supply_store_property, \
  35. }
  36. static struct device_attribute power_supply_attrs[];
  37. static const char * const power_supply_type_text[] = {
  38. "Unknown", "Battery", "UPS", "Mains", "USB",
  39. "USB_DCP", "USB_CDP", "USB_ACA", "USB_C",
  40. "USB_PD", "USB_PD_DRP", "BrickID"
  41. };
  42. static const char * const power_supply_usb_type_text[] = {
  43. "Unknown", "SDP", "DCP", "CDP", "ACA", "C",
  44. "PD", "PD_DRP", "PD_PPS", "BrickID"
  45. };
  46. static const char * const power_supply_status_text[] = {
  47. "Unknown", "Charging", "Discharging", "Not charging", "Full"
  48. };
  49. static const char * const power_supply_charge_type_text[] = {
  50. "Unknown", "N/A", "Trickle", "Fast"
  51. };
  52. static const char * const power_supply_health_text[] = {
  53. "Unknown", "Good", "Overheat", "Dead", "Over voltage",
  54. "Unspecified failure", "Cold", "Watchdog timer expire",
  55. "Safety timer expire"
  56. };
  57. static const char * const power_supply_technology_text[] = {
  58. "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
  59. "LiMn"
  60. };
  61. static const char * const power_supply_capacity_level_text[] = {
  62. "Unknown", "Critical", "Low", "Normal", "High", "Full"
  63. };
  64. static const char * const power_supply_scope_text[] = {
  65. "Unknown", "System", "Device"
  66. };
  67. static ssize_t power_supply_show_usb_type(struct device *dev,
  68. enum power_supply_usb_type *usb_types,
  69. ssize_t num_usb_types,
  70. union power_supply_propval *value,
  71. char *buf)
  72. {
  73. enum power_supply_usb_type usb_type;
  74. ssize_t count = 0;
  75. bool match = false;
  76. int i;
  77. for (i = 0; i < num_usb_types; ++i) {
  78. usb_type = usb_types[i];
  79. if (value->intval == usb_type) {
  80. count += sprintf(buf + count, "[%s] ",
  81. power_supply_usb_type_text[usb_type]);
  82. match = true;
  83. } else {
  84. count += sprintf(buf + count, "%s ",
  85. power_supply_usb_type_text[usb_type]);
  86. }
  87. }
  88. if (!match) {
  89. dev_warn(dev, "driver reporting unsupported connected type\n");
  90. return -EINVAL;
  91. }
  92. if (count)
  93. buf[count - 1] = '\n';
  94. return count;
  95. }
  96. static ssize_t power_supply_show_property(struct device *dev,
  97. struct device_attribute *attr,
  98. char *buf) {
  99. ssize_t ret;
  100. struct power_supply *psy = dev_get_drvdata(dev);
  101. enum power_supply_property psp = attr - power_supply_attrs;
  102. union power_supply_propval value;
  103. if (psp == POWER_SUPPLY_PROP_TYPE) {
  104. value.intval = psy->desc->type;
  105. } else {
  106. ret = power_supply_get_property(psy, psp, &value);
  107. if (ret < 0) {
  108. if (ret == -ENODATA)
  109. dev_dbg(dev, "driver has no data for `%s' property\n",
  110. attr->attr.name);
  111. else if (ret != -ENODEV && ret != -EAGAIN)
  112. dev_err_ratelimited(dev,
  113. "driver failed to report `%s' property: %zd\n",
  114. attr->attr.name, ret);
  115. return ret;
  116. }
  117. }
  118. switch (psp) {
  119. case POWER_SUPPLY_PROP_STATUS:
  120. ret = sprintf(buf, "%s\n",
  121. power_supply_status_text[value.intval]);
  122. break;
  123. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  124. ret = sprintf(buf, "%s\n",
  125. power_supply_charge_type_text[value.intval]);
  126. break;
  127. case POWER_SUPPLY_PROP_HEALTH:
  128. ret = sprintf(buf, "%s\n",
  129. power_supply_health_text[value.intval]);
  130. break;
  131. case POWER_SUPPLY_PROP_TECHNOLOGY:
  132. ret = sprintf(buf, "%s\n",
  133. power_supply_technology_text[value.intval]);
  134. break;
  135. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  136. ret = sprintf(buf, "%s\n",
  137. power_supply_capacity_level_text[value.intval]);
  138. break;
  139. case POWER_SUPPLY_PROP_TYPE:
  140. ret = sprintf(buf, "%s\n",
  141. power_supply_type_text[value.intval]);
  142. break;
  143. case POWER_SUPPLY_PROP_USB_TYPE:
  144. ret = power_supply_show_usb_type(dev, psy->desc->usb_types,
  145. psy->desc->num_usb_types,
  146. &value, buf);
  147. break;
  148. case POWER_SUPPLY_PROP_SCOPE:
  149. ret = sprintf(buf, "%s\n",
  150. power_supply_scope_text[value.intval]);
  151. break;
  152. case POWER_SUPPLY_PROP_MODEL_NAME ... POWER_SUPPLY_PROP_SERIAL_NUMBER:
  153. ret = sprintf(buf, "%s\n", value.strval);
  154. break;
  155. default:
  156. ret = sprintf(buf, "%d\n", value.intval);
  157. }
  158. return ret;
  159. }
  160. static ssize_t power_supply_store_property(struct device *dev,
  161. struct device_attribute *attr,
  162. const char *buf, size_t count) {
  163. ssize_t ret;
  164. struct power_supply *psy = dev_get_drvdata(dev);
  165. enum power_supply_property psp = attr - power_supply_attrs;
  166. union power_supply_propval value;
  167. switch (psp) {
  168. case POWER_SUPPLY_PROP_STATUS:
  169. ret = sysfs_match_string(power_supply_status_text, buf);
  170. break;
  171. case POWER_SUPPLY_PROP_CHARGE_TYPE:
  172. ret = sysfs_match_string(power_supply_charge_type_text, buf);
  173. break;
  174. case POWER_SUPPLY_PROP_HEALTH:
  175. ret = sysfs_match_string(power_supply_health_text, buf);
  176. break;
  177. case POWER_SUPPLY_PROP_TECHNOLOGY:
  178. ret = sysfs_match_string(power_supply_technology_text, buf);
  179. break;
  180. case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
  181. ret = sysfs_match_string(power_supply_capacity_level_text, buf);
  182. break;
  183. case POWER_SUPPLY_PROP_SCOPE:
  184. ret = sysfs_match_string(power_supply_scope_text, buf);
  185. break;
  186. default:
  187. ret = -EINVAL;
  188. }
  189. /*
  190. * If no match was found, then check to see if it is an integer.
  191. * Integer values are valid for enums in addition to the text value.
  192. */
  193. if (ret < 0) {
  194. long long_val;
  195. ret = kstrtol(buf, 10, &long_val);
  196. if (ret < 0)
  197. return ret;
  198. ret = long_val;
  199. }
  200. value.intval = ret;
  201. ret = power_supply_set_property(psy, psp, &value);
  202. if (ret < 0)
  203. return ret;
  204. return count;
  205. }
  206. /* Must be in the same order as POWER_SUPPLY_PROP_* */
  207. static struct device_attribute power_supply_attrs[] = {
  208. /* Properties of type `int' */
  209. POWER_SUPPLY_ATTR(status),
  210. POWER_SUPPLY_ATTR(charge_type),
  211. POWER_SUPPLY_ATTR(health),
  212. POWER_SUPPLY_ATTR(present),
  213. POWER_SUPPLY_ATTR(online),
  214. POWER_SUPPLY_ATTR(authentic),
  215. POWER_SUPPLY_ATTR(technology),
  216. POWER_SUPPLY_ATTR(cycle_count),
  217. POWER_SUPPLY_ATTR(voltage_max),
  218. POWER_SUPPLY_ATTR(voltage_min),
  219. POWER_SUPPLY_ATTR(voltage_max_design),
  220. POWER_SUPPLY_ATTR(voltage_min_design),
  221. POWER_SUPPLY_ATTR(voltage_now),
  222. POWER_SUPPLY_ATTR(voltage_avg),
  223. POWER_SUPPLY_ATTR(voltage_ocv),
  224. POWER_SUPPLY_ATTR(voltage_boot),
  225. POWER_SUPPLY_ATTR(current_max),
  226. POWER_SUPPLY_ATTR(current_now),
  227. POWER_SUPPLY_ATTR(current_avg),
  228. POWER_SUPPLY_ATTR(current_boot),
  229. POWER_SUPPLY_ATTR(power_now),
  230. POWER_SUPPLY_ATTR(power_avg),
  231. POWER_SUPPLY_ATTR(charge_full_design),
  232. POWER_SUPPLY_ATTR(charge_empty_design),
  233. POWER_SUPPLY_ATTR(charge_full),
  234. POWER_SUPPLY_ATTR(charge_empty),
  235. POWER_SUPPLY_ATTR(charge_now),
  236. POWER_SUPPLY_ATTR(charge_avg),
  237. POWER_SUPPLY_ATTR(charge_counter),
  238. POWER_SUPPLY_ATTR(constant_charge_current),
  239. POWER_SUPPLY_ATTR(constant_charge_current_max),
  240. POWER_SUPPLY_ATTR(constant_charge_voltage),
  241. POWER_SUPPLY_ATTR(constant_charge_voltage_max),
  242. POWER_SUPPLY_ATTR(charge_control_limit),
  243. POWER_SUPPLY_ATTR(charge_control_limit_max),
  244. POWER_SUPPLY_ATTR(input_current_limit),
  245. POWER_SUPPLY_ATTR(energy_full_design),
  246. POWER_SUPPLY_ATTR(energy_empty_design),
  247. POWER_SUPPLY_ATTR(energy_full),
  248. POWER_SUPPLY_ATTR(energy_empty),
  249. POWER_SUPPLY_ATTR(energy_now),
  250. POWER_SUPPLY_ATTR(energy_avg),
  251. POWER_SUPPLY_ATTR(capacity),
  252. POWER_SUPPLY_ATTR(capacity_alert_min),
  253. POWER_SUPPLY_ATTR(capacity_alert_max),
  254. POWER_SUPPLY_ATTR(capacity_level),
  255. POWER_SUPPLY_ATTR(temp),
  256. POWER_SUPPLY_ATTR(temp_max),
  257. POWER_SUPPLY_ATTR(temp_min),
  258. POWER_SUPPLY_ATTR(temp_alert_min),
  259. POWER_SUPPLY_ATTR(temp_alert_max),
  260. POWER_SUPPLY_ATTR(temp_ambient),
  261. POWER_SUPPLY_ATTR(temp_ambient_alert_min),
  262. POWER_SUPPLY_ATTR(temp_ambient_alert_max),
  263. POWER_SUPPLY_ATTR(time_to_empty_now),
  264. POWER_SUPPLY_ATTR(time_to_empty_avg),
  265. POWER_SUPPLY_ATTR(time_to_full_now),
  266. POWER_SUPPLY_ATTR(time_to_full_avg),
  267. POWER_SUPPLY_ATTR(type),
  268. POWER_SUPPLY_ATTR(usb_type),
  269. POWER_SUPPLY_ATTR(scope),
  270. POWER_SUPPLY_ATTR(precharge_current),
  271. POWER_SUPPLY_ATTR(charge_term_current),
  272. POWER_SUPPLY_ATTR(calibrate),
  273. /* Properties of type `const char *' */
  274. POWER_SUPPLY_ATTR(model_name),
  275. POWER_SUPPLY_ATTR(manufacturer),
  276. POWER_SUPPLY_ATTR(serial_number),
  277. };
  278. static struct attribute *
  279. __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
  280. static umode_t power_supply_attr_is_visible(struct kobject *kobj,
  281. struct attribute *attr,
  282. int attrno)
  283. {
  284. struct device *dev = container_of(kobj, struct device, kobj);
  285. struct power_supply *psy = dev_get_drvdata(dev);
  286. umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
  287. int i;
  288. if (attrno == POWER_SUPPLY_PROP_TYPE)
  289. return mode;
  290. for (i = 0; i < psy->desc->num_properties; i++) {
  291. int property = psy->desc->properties[i];
  292. if (property == attrno) {
  293. if (psy->desc->property_is_writeable &&
  294. psy->desc->property_is_writeable(psy, property) > 0)
  295. mode |= S_IWUSR;
  296. return mode;
  297. }
  298. }
  299. return 0;
  300. }
  301. static struct attribute_group power_supply_attr_group = {
  302. .attrs = __power_supply_attrs,
  303. .is_visible = power_supply_attr_is_visible,
  304. };
  305. static const struct attribute_group *power_supply_attr_groups[] = {
  306. &power_supply_attr_group,
  307. NULL,
  308. };
  309. void power_supply_init_attrs(struct device_type *dev_type)
  310. {
  311. int i;
  312. dev_type->groups = power_supply_attr_groups;
  313. for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
  314. __power_supply_attrs[i] = &power_supply_attrs[i].attr;
  315. }
  316. static char *kstruprdup(const char *str, gfp_t gfp)
  317. {
  318. char *ret, *ustr;
  319. ustr = ret = kmalloc(strlen(str) + 1, gfp);
  320. if (!ret)
  321. return NULL;
  322. while (*str)
  323. *ustr++ = toupper(*str++);
  324. *ustr = 0;
  325. return ret;
  326. }
  327. int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
  328. {
  329. struct power_supply *psy = dev_get_drvdata(dev);
  330. int ret = 0, j;
  331. char *prop_buf;
  332. char *attrname;
  333. if (!psy || !psy->desc) {
  334. dev_dbg(dev, "No power supply yet\n");
  335. return ret;
  336. }
  337. ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
  338. if (ret)
  339. return ret;
  340. prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
  341. if (!prop_buf)
  342. return -ENOMEM;
  343. for (j = 0; j < psy->desc->num_properties; j++) {
  344. struct device_attribute *attr;
  345. char *line;
  346. attr = &power_supply_attrs[psy->desc->properties[j]];
  347. ret = power_supply_show_property(dev, attr, prop_buf);
  348. if (ret == -ENODEV || ret == -ENODATA) {
  349. /* When a battery is absent, we expect -ENODEV. Don't abort;
  350. send the uevent with at least the the PRESENT=0 property */
  351. ret = 0;
  352. continue;
  353. }
  354. if (ret < 0)
  355. goto out;
  356. line = strchr(prop_buf, '\n');
  357. if (line)
  358. *line = 0;
  359. attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
  360. if (!attrname) {
  361. ret = -ENOMEM;
  362. goto out;
  363. }
  364. ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
  365. kfree(attrname);
  366. if (ret)
  367. goto out;
  368. }
  369. out:
  370. free_page((unsigned long)prop_buf);
  371. return ret;
  372. }