power_supply_sysfs.c 7.8 KB

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