power_supply_sysfs.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  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 ssize_t power_supply_show_property(struct device *dev,
  38. struct device_attribute *attr,
  39. char *buf) {
  40. static char *type_text[] = {
  41. "Unknown", "Battery", "UPS", "Mains", "USB",
  42. "USB_DCP", "USB_CDP", "USB_ACA", "USB_C",
  43. "USB_PD", "USB_PD_DRP"
  44. };
  45. static char *status_text[] = {
  46. "Unknown", "Charging", "Discharging", "Not charging", "Full"
  47. };
  48. static char *charge_type[] = {
  49. "Unknown", "N/A", "Trickle", "Fast"
  50. };
  51. static char *health_text[] = {
  52. "Unknown", "Good", "Overheat", "Dead", "Over voltage",
  53. "Unspecified failure", "Cold", "Watchdog timer expire",
  54. "Safety timer expire"
  55. };
  56. static char *technology_text[] = {
  57. "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
  58. "LiMn"
  59. };
  60. static char *capacity_level_text[] = {
  61. "Unknown", "Critical", "Low", "Normal", "High", "Full"
  62. };
  63. static char *scope_text[] = {
  64. "Unknown", "System", "Device"
  65. };
  66. ssize_t ret = 0;
  67. struct power_supply *psy = dev_get_drvdata(dev);
  68. const ptrdiff_t off = attr - power_supply_attrs;
  69. union power_supply_propval value;
  70. if (off == POWER_SUPPLY_PROP_TYPE) {
  71. value.intval = psy->desc->type;
  72. } else {
  73. ret = power_supply_get_property(psy, off, &value);
  74. if (ret < 0) {
  75. if (ret == -ENODATA)
  76. dev_dbg(dev, "driver has no data for `%s' property\n",
  77. attr->attr.name);
  78. else if (ret != -ENODEV && ret != -EAGAIN)
  79. dev_err(dev, "driver failed to report `%s' property: %zd\n",
  80. attr->attr.name, ret);
  81. return ret;
  82. }
  83. }
  84. if (off == POWER_SUPPLY_PROP_STATUS)
  85. return sprintf(buf, "%s\n", status_text[value.intval]);
  86. else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
  87. return sprintf(buf, "%s\n", charge_type[value.intval]);
  88. else if (off == POWER_SUPPLY_PROP_HEALTH)
  89. return sprintf(buf, "%s\n", health_text[value.intval]);
  90. else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
  91. return sprintf(buf, "%s\n", technology_text[value.intval]);
  92. else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
  93. return sprintf(buf, "%s\n", capacity_level_text[value.intval]);
  94. else if (off == POWER_SUPPLY_PROP_TYPE)
  95. return sprintf(buf, "%s\n", type_text[value.intval]);
  96. else if (off == POWER_SUPPLY_PROP_SCOPE)
  97. return sprintf(buf, "%s\n", scope_text[value.intval]);
  98. else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
  99. return sprintf(buf, "%s\n", value.strval);
  100. return sprintf(buf, "%d\n", value.intval);
  101. }
  102. static ssize_t power_supply_store_property(struct device *dev,
  103. struct device_attribute *attr,
  104. const char *buf, size_t count) {
  105. ssize_t ret;
  106. struct power_supply *psy = dev_get_drvdata(dev);
  107. const ptrdiff_t off = attr - power_supply_attrs;
  108. union power_supply_propval value;
  109. long long_val;
  110. /* TODO: support other types than int */
  111. ret = kstrtol(buf, 10, &long_val);
  112. if (ret < 0)
  113. return ret;
  114. value.intval = long_val;
  115. ret = power_supply_set_property(psy, off, &value);
  116. if (ret < 0)
  117. return ret;
  118. return count;
  119. }
  120. /* Must be in the same order as POWER_SUPPLY_PROP_* */
  121. static struct device_attribute power_supply_attrs[] = {
  122. /* Properties of type `int' */
  123. POWER_SUPPLY_ATTR(status),
  124. POWER_SUPPLY_ATTR(charge_type),
  125. POWER_SUPPLY_ATTR(health),
  126. POWER_SUPPLY_ATTR(present),
  127. POWER_SUPPLY_ATTR(online),
  128. POWER_SUPPLY_ATTR(authentic),
  129. POWER_SUPPLY_ATTR(technology),
  130. POWER_SUPPLY_ATTR(cycle_count),
  131. POWER_SUPPLY_ATTR(voltage_max),
  132. POWER_SUPPLY_ATTR(voltage_min),
  133. POWER_SUPPLY_ATTR(voltage_max_design),
  134. POWER_SUPPLY_ATTR(voltage_min_design),
  135. POWER_SUPPLY_ATTR(voltage_now),
  136. POWER_SUPPLY_ATTR(voltage_avg),
  137. POWER_SUPPLY_ATTR(voltage_ocv),
  138. POWER_SUPPLY_ATTR(voltage_boot),
  139. POWER_SUPPLY_ATTR(current_max),
  140. POWER_SUPPLY_ATTR(current_now),
  141. POWER_SUPPLY_ATTR(current_avg),
  142. POWER_SUPPLY_ATTR(current_boot),
  143. POWER_SUPPLY_ATTR(power_now),
  144. POWER_SUPPLY_ATTR(power_avg),
  145. POWER_SUPPLY_ATTR(charge_full_design),
  146. POWER_SUPPLY_ATTR(charge_empty_design),
  147. POWER_SUPPLY_ATTR(charge_full),
  148. POWER_SUPPLY_ATTR(charge_empty),
  149. POWER_SUPPLY_ATTR(charge_now),
  150. POWER_SUPPLY_ATTR(charge_avg),
  151. POWER_SUPPLY_ATTR(charge_counter),
  152. POWER_SUPPLY_ATTR(constant_charge_current),
  153. POWER_SUPPLY_ATTR(constant_charge_current_max),
  154. POWER_SUPPLY_ATTR(constant_charge_voltage),
  155. POWER_SUPPLY_ATTR(constant_charge_voltage_max),
  156. POWER_SUPPLY_ATTR(charge_control_limit),
  157. POWER_SUPPLY_ATTR(charge_control_limit_max),
  158. POWER_SUPPLY_ATTR(input_current_limit),
  159. POWER_SUPPLY_ATTR(energy_full_design),
  160. POWER_SUPPLY_ATTR(energy_empty_design),
  161. POWER_SUPPLY_ATTR(energy_full),
  162. POWER_SUPPLY_ATTR(energy_empty),
  163. POWER_SUPPLY_ATTR(energy_now),
  164. POWER_SUPPLY_ATTR(energy_avg),
  165. POWER_SUPPLY_ATTR(capacity),
  166. POWER_SUPPLY_ATTR(capacity_alert_min),
  167. POWER_SUPPLY_ATTR(capacity_alert_max),
  168. POWER_SUPPLY_ATTR(capacity_level),
  169. POWER_SUPPLY_ATTR(temp),
  170. POWER_SUPPLY_ATTR(temp_max),
  171. POWER_SUPPLY_ATTR(temp_min),
  172. POWER_SUPPLY_ATTR(temp_alert_min),
  173. POWER_SUPPLY_ATTR(temp_alert_max),
  174. POWER_SUPPLY_ATTR(temp_ambient),
  175. POWER_SUPPLY_ATTR(temp_ambient_alert_min),
  176. POWER_SUPPLY_ATTR(temp_ambient_alert_max),
  177. POWER_SUPPLY_ATTR(time_to_empty_now),
  178. POWER_SUPPLY_ATTR(time_to_empty_avg),
  179. POWER_SUPPLY_ATTR(time_to_full_now),
  180. POWER_SUPPLY_ATTR(time_to_full_avg),
  181. POWER_SUPPLY_ATTR(type),
  182. POWER_SUPPLY_ATTR(scope),
  183. POWER_SUPPLY_ATTR(charge_term_current),
  184. POWER_SUPPLY_ATTR(calibrate),
  185. /* Properties of type `const char *' */
  186. POWER_SUPPLY_ATTR(model_name),
  187. POWER_SUPPLY_ATTR(manufacturer),
  188. POWER_SUPPLY_ATTR(serial_number),
  189. };
  190. static struct attribute *
  191. __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
  192. static umode_t power_supply_attr_is_visible(struct kobject *kobj,
  193. struct attribute *attr,
  194. int attrno)
  195. {
  196. struct device *dev = container_of(kobj, struct device, kobj);
  197. struct power_supply *psy = dev_get_drvdata(dev);
  198. umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
  199. int i;
  200. if (attrno == POWER_SUPPLY_PROP_TYPE)
  201. return mode;
  202. for (i = 0; i < psy->desc->num_properties; i++) {
  203. int property = psy->desc->properties[i];
  204. if (property == attrno) {
  205. if (psy->desc->property_is_writeable &&
  206. psy->desc->property_is_writeable(psy, property) > 0)
  207. mode |= S_IWUSR;
  208. return mode;
  209. }
  210. }
  211. return 0;
  212. }
  213. static struct attribute_group power_supply_attr_group = {
  214. .attrs = __power_supply_attrs,
  215. .is_visible = power_supply_attr_is_visible,
  216. };
  217. static const struct attribute_group *power_supply_attr_groups[] = {
  218. &power_supply_attr_group,
  219. NULL,
  220. };
  221. void power_supply_init_attrs(struct device_type *dev_type)
  222. {
  223. int i;
  224. dev_type->groups = power_supply_attr_groups;
  225. for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
  226. __power_supply_attrs[i] = &power_supply_attrs[i].attr;
  227. }
  228. static char *kstruprdup(const char *str, gfp_t gfp)
  229. {
  230. char *ret, *ustr;
  231. ustr = ret = kmalloc(strlen(str) + 1, gfp);
  232. if (!ret)
  233. return NULL;
  234. while (*str)
  235. *ustr++ = toupper(*str++);
  236. *ustr = 0;
  237. return ret;
  238. }
  239. int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
  240. {
  241. struct power_supply *psy = dev_get_drvdata(dev);
  242. int ret = 0, j;
  243. char *prop_buf;
  244. char *attrname;
  245. dev_dbg(dev, "uevent\n");
  246. if (!psy || !psy->desc) {
  247. dev_dbg(dev, "No power supply yet\n");
  248. return ret;
  249. }
  250. dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->desc->name);
  251. ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->desc->name);
  252. if (ret)
  253. return ret;
  254. prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
  255. if (!prop_buf)
  256. return -ENOMEM;
  257. for (j = 0; j < psy->desc->num_properties; j++) {
  258. struct device_attribute *attr;
  259. char *line;
  260. attr = &power_supply_attrs[psy->desc->properties[j]];
  261. ret = power_supply_show_property(dev, attr, prop_buf);
  262. if (ret == -ENODEV || ret == -ENODATA) {
  263. /* When a battery is absent, we expect -ENODEV. Don't abort;
  264. send the uevent with at least the the PRESENT=0 property */
  265. ret = 0;
  266. continue;
  267. }
  268. if (ret < 0)
  269. goto out;
  270. line = strchr(prop_buf, '\n');
  271. if (line)
  272. *line = 0;
  273. attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
  274. if (!attrname) {
  275. ret = -ENOMEM;
  276. goto out;
  277. }
  278. dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
  279. ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
  280. kfree(attrname);
  281. if (ret)
  282. goto out;
  283. }
  284. out:
  285. free_page((unsigned long)prop_buf);
  286. return ret;
  287. }