power_supply_sysfs.c 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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/power_supply.h>
  15. #include <linux/slab.h>
  16. #include "power_supply.h"
  17. /*
  18. * This is because the name "current" breaks the device attr macro.
  19. * The "current" word resolves to "(get_current())" so instead of
  20. * "current" "(get_current())" appears in the sysfs.
  21. *
  22. * The source of this definition is the device.h which calls __ATTR
  23. * macro in sysfs.h which calls the __stringify macro.
  24. *
  25. * Only modification that the name is not tried to be resolved
  26. * (as a macro let's say).
  27. */
  28. #define POWER_SUPPLY_ATTR(_name) \
  29. { \
  30. .attr = { .name = #_name, .mode = 0444 }, \
  31. .show = power_supply_show_property, \
  32. .store = NULL, \
  33. }
  34. static struct device_attribute power_supply_attrs[];
  35. static ssize_t power_supply_show_property(struct device *dev,
  36. struct device_attribute *attr,
  37. char *buf) {
  38. static char *status_text[] = {
  39. "Unknown", "Charging", "Discharging", "Not charging", "Full"
  40. };
  41. static char *charge_type[] = {
  42. "Unknown", "N/A", "Trickle", "Fast"
  43. };
  44. static char *health_text[] = {
  45. "Unknown", "Good", "Overheat", "Dead", "Over voltage",
  46. "Unspecified failure", "Cold",
  47. };
  48. static char *technology_text[] = {
  49. "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
  50. "LiMn"
  51. };
  52. static char *capacity_level_text[] = {
  53. "Unknown", "Critical", "Low", "Normal", "High", "Full"
  54. };
  55. ssize_t ret;
  56. struct power_supply *psy = dev_get_drvdata(dev);
  57. const ptrdiff_t off = attr - power_supply_attrs;
  58. union power_supply_propval value;
  59. ret = psy->get_property(psy, off, &value);
  60. if (ret < 0) {
  61. if (ret == -ENODATA)
  62. dev_dbg(dev, "driver has no data for `%s' property\n",
  63. attr->attr.name);
  64. else if (ret != -ENODEV)
  65. dev_err(dev, "driver failed to report `%s' property\n",
  66. attr->attr.name);
  67. return ret;
  68. }
  69. if (off == POWER_SUPPLY_PROP_STATUS)
  70. return sprintf(buf, "%s\n", status_text[value.intval]);
  71. else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
  72. return sprintf(buf, "%s\n", charge_type[value.intval]);
  73. else if (off == POWER_SUPPLY_PROP_HEALTH)
  74. return sprintf(buf, "%s\n", health_text[value.intval]);
  75. else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
  76. return sprintf(buf, "%s\n", technology_text[value.intval]);
  77. else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
  78. return sprintf(buf, "%s\n", capacity_level_text[value.intval]);
  79. else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
  80. return sprintf(buf, "%s\n", value.strval);
  81. return sprintf(buf, "%d\n", value.intval);
  82. }
  83. /* Must be in the same order as POWER_SUPPLY_PROP_* */
  84. static struct device_attribute power_supply_attrs[] = {
  85. /* Properties of type `int' */
  86. POWER_SUPPLY_ATTR(status),
  87. POWER_SUPPLY_ATTR(charge_type),
  88. POWER_SUPPLY_ATTR(health),
  89. POWER_SUPPLY_ATTR(present),
  90. POWER_SUPPLY_ATTR(online),
  91. POWER_SUPPLY_ATTR(technology),
  92. POWER_SUPPLY_ATTR(cycle_count),
  93. POWER_SUPPLY_ATTR(voltage_max),
  94. POWER_SUPPLY_ATTR(voltage_min),
  95. POWER_SUPPLY_ATTR(voltage_max_design),
  96. POWER_SUPPLY_ATTR(voltage_min_design),
  97. POWER_SUPPLY_ATTR(voltage_now),
  98. POWER_SUPPLY_ATTR(voltage_avg),
  99. POWER_SUPPLY_ATTR(current_now),
  100. POWER_SUPPLY_ATTR(current_avg),
  101. POWER_SUPPLY_ATTR(power_now),
  102. POWER_SUPPLY_ATTR(power_avg),
  103. POWER_SUPPLY_ATTR(charge_full_design),
  104. POWER_SUPPLY_ATTR(charge_empty_design),
  105. POWER_SUPPLY_ATTR(charge_full),
  106. POWER_SUPPLY_ATTR(charge_empty),
  107. POWER_SUPPLY_ATTR(charge_now),
  108. POWER_SUPPLY_ATTR(charge_avg),
  109. POWER_SUPPLY_ATTR(charge_counter),
  110. POWER_SUPPLY_ATTR(energy_full_design),
  111. POWER_SUPPLY_ATTR(energy_empty_design),
  112. POWER_SUPPLY_ATTR(energy_full),
  113. POWER_SUPPLY_ATTR(energy_empty),
  114. POWER_SUPPLY_ATTR(energy_now),
  115. POWER_SUPPLY_ATTR(energy_avg),
  116. POWER_SUPPLY_ATTR(capacity),
  117. POWER_SUPPLY_ATTR(capacity_level),
  118. POWER_SUPPLY_ATTR(temp),
  119. POWER_SUPPLY_ATTR(temp_ambient),
  120. POWER_SUPPLY_ATTR(time_to_empty_now),
  121. POWER_SUPPLY_ATTR(time_to_empty_avg),
  122. POWER_SUPPLY_ATTR(time_to_full_now),
  123. POWER_SUPPLY_ATTR(time_to_full_avg),
  124. /* Properties of type `const char *' */
  125. POWER_SUPPLY_ATTR(model_name),
  126. POWER_SUPPLY_ATTR(manufacturer),
  127. POWER_SUPPLY_ATTR(serial_number),
  128. };
  129. static ssize_t power_supply_show_static_attrs(struct device *dev,
  130. struct device_attribute *attr,
  131. char *buf) {
  132. static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
  133. struct power_supply *psy = dev_get_drvdata(dev);
  134. return sprintf(buf, "%s\n", type_text[psy->type]);
  135. }
  136. static struct device_attribute power_supply_static_attrs[] = {
  137. __ATTR(type, 0444, power_supply_show_static_attrs, NULL),
  138. };
  139. int power_supply_create_attrs(struct power_supply *psy)
  140. {
  141. int rc = 0;
  142. int i, j;
  143. for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
  144. rc = device_create_file(psy->dev,
  145. &power_supply_static_attrs[i]);
  146. if (rc)
  147. goto statics_failed;
  148. }
  149. for (j = 0; j < psy->num_properties; j++) {
  150. rc = device_create_file(psy->dev,
  151. &power_supply_attrs[psy->properties[j]]);
  152. if (rc)
  153. goto dynamics_failed;
  154. }
  155. goto succeed;
  156. dynamics_failed:
  157. while (j--)
  158. device_remove_file(psy->dev,
  159. &power_supply_attrs[psy->properties[j]]);
  160. statics_failed:
  161. while (i--)
  162. device_remove_file(psy->dev, &power_supply_static_attrs[i]);
  163. succeed:
  164. return rc;
  165. }
  166. void power_supply_remove_attrs(struct power_supply *psy)
  167. {
  168. int i;
  169. for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
  170. device_remove_file(psy->dev, &power_supply_static_attrs[i]);
  171. for (i = 0; i < psy->num_properties; i++)
  172. device_remove_file(psy->dev,
  173. &power_supply_attrs[psy->properties[i]]);
  174. }
  175. static char *kstruprdup(const char *str, gfp_t gfp)
  176. {
  177. char *ret, *ustr;
  178. ustr = ret = kmalloc(strlen(str) + 1, gfp);
  179. if (!ret)
  180. return NULL;
  181. while (*str)
  182. *ustr++ = toupper(*str++);
  183. *ustr = 0;
  184. return ret;
  185. }
  186. int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
  187. {
  188. struct power_supply *psy = dev_get_drvdata(dev);
  189. int ret = 0, j;
  190. char *prop_buf;
  191. char *attrname;
  192. dev_dbg(dev, "uevent\n");
  193. if (!psy || !psy->dev) {
  194. dev_dbg(dev, "No power supply yet\n");
  195. return ret;
  196. }
  197. dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
  198. ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
  199. if (ret)
  200. return ret;
  201. prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
  202. if (!prop_buf)
  203. return -ENOMEM;
  204. for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
  205. struct device_attribute *attr;
  206. char *line;
  207. attr = &power_supply_static_attrs[j];
  208. ret = power_supply_show_static_attrs(dev, attr, prop_buf);
  209. if (ret < 0)
  210. goto out;
  211. line = strchr(prop_buf, '\n');
  212. if (line)
  213. *line = 0;
  214. attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
  215. if (!attrname) {
  216. ret = -ENOMEM;
  217. goto out;
  218. }
  219. dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
  220. ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
  221. kfree(attrname);
  222. if (ret)
  223. goto out;
  224. }
  225. dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
  226. for (j = 0; j < psy->num_properties; j++) {
  227. struct device_attribute *attr;
  228. char *line;
  229. attr = &power_supply_attrs[psy->properties[j]];
  230. ret = power_supply_show_property(dev, attr, prop_buf);
  231. if (ret == -ENODEV) {
  232. /* When a battery is absent, we expect -ENODEV. Don't abort;
  233. send the uevent with at least the the PRESENT=0 property */
  234. ret = 0;
  235. continue;
  236. }
  237. if (ret < 0)
  238. goto out;
  239. line = strchr(prop_buf, '\n');
  240. if (line)
  241. *line = 0;
  242. attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
  243. if (!attrname) {
  244. ret = -ENOMEM;
  245. goto out;
  246. }
  247. dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
  248. ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
  249. kfree(attrname);
  250. if (ret)
  251. goto out;
  252. }
  253. out:
  254. free_page((unsigned long)prop_buf);
  255. return ret;
  256. }