power_supply_sysfs.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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, .owner = THIS_MODULE }, \
  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 *health_text[] = {
  41. "Unknown", "Good", "Overheat", "Dead", "Over voltage",
  42. "Unspecified failure"
  43. };
  44. static char *technology_text[] = {
  45. "Unknown", "NiMH", "Li-ion", "Li-poly", "LiFe", "NiCd",
  46. "LiMn"
  47. };
  48. ssize_t ret;
  49. struct power_supply *psy = dev_get_drvdata(dev);
  50. const ptrdiff_t off = attr - power_supply_attrs;
  51. union power_supply_propval value;
  52. ret = psy->get_property(psy, off, &value);
  53. if (ret < 0) {
  54. if (ret != -ENODEV)
  55. dev_err(dev, "driver failed to report `%s' property\n",
  56. attr->attr.name);
  57. return ret;
  58. }
  59. if (off == POWER_SUPPLY_PROP_STATUS)
  60. return sprintf(buf, "%s\n", status_text[value.intval]);
  61. else if (off == POWER_SUPPLY_PROP_HEALTH)
  62. return sprintf(buf, "%s\n", health_text[value.intval]);
  63. else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
  64. return sprintf(buf, "%s\n", technology_text[value.intval]);
  65. else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
  66. return sprintf(buf, "%s\n", value.strval);
  67. return sprintf(buf, "%d\n", value.intval);
  68. }
  69. /* Must be in the same order as POWER_SUPPLY_PROP_* */
  70. static struct device_attribute power_supply_attrs[] = {
  71. /* Properties of type `int' */
  72. POWER_SUPPLY_ATTR(status),
  73. POWER_SUPPLY_ATTR(health),
  74. POWER_SUPPLY_ATTR(present),
  75. POWER_SUPPLY_ATTR(online),
  76. POWER_SUPPLY_ATTR(technology),
  77. POWER_SUPPLY_ATTR(voltage_max),
  78. POWER_SUPPLY_ATTR(voltage_min),
  79. POWER_SUPPLY_ATTR(voltage_max_design),
  80. POWER_SUPPLY_ATTR(voltage_min_design),
  81. POWER_SUPPLY_ATTR(voltage_now),
  82. POWER_SUPPLY_ATTR(voltage_avg),
  83. POWER_SUPPLY_ATTR(current_now),
  84. POWER_SUPPLY_ATTR(current_avg),
  85. POWER_SUPPLY_ATTR(charge_full_design),
  86. POWER_SUPPLY_ATTR(charge_empty_design),
  87. POWER_SUPPLY_ATTR(charge_full),
  88. POWER_SUPPLY_ATTR(charge_empty),
  89. POWER_SUPPLY_ATTR(charge_now),
  90. POWER_SUPPLY_ATTR(charge_avg),
  91. POWER_SUPPLY_ATTR(energy_full_design),
  92. POWER_SUPPLY_ATTR(energy_empty_design),
  93. POWER_SUPPLY_ATTR(energy_full),
  94. POWER_SUPPLY_ATTR(energy_empty),
  95. POWER_SUPPLY_ATTR(energy_now),
  96. POWER_SUPPLY_ATTR(energy_avg),
  97. POWER_SUPPLY_ATTR(capacity),
  98. POWER_SUPPLY_ATTR(temp),
  99. POWER_SUPPLY_ATTR(temp_ambient),
  100. POWER_SUPPLY_ATTR(time_to_empty_now),
  101. POWER_SUPPLY_ATTR(time_to_empty_avg),
  102. POWER_SUPPLY_ATTR(time_to_full_now),
  103. POWER_SUPPLY_ATTR(time_to_full_avg),
  104. /* Properties of type `const char *' */
  105. POWER_SUPPLY_ATTR(model_name),
  106. POWER_SUPPLY_ATTR(manufacturer),
  107. POWER_SUPPLY_ATTR(serial_number),
  108. };
  109. static ssize_t power_supply_show_static_attrs(struct device *dev,
  110. struct device_attribute *attr,
  111. char *buf) {
  112. static char *type_text[] = { "Battery", "UPS", "Mains", "USB" };
  113. struct power_supply *psy = dev_get_drvdata(dev);
  114. return sprintf(buf, "%s\n", type_text[psy->type]);
  115. }
  116. static struct device_attribute power_supply_static_attrs[] = {
  117. __ATTR(type, 0444, power_supply_show_static_attrs, NULL),
  118. };
  119. int power_supply_create_attrs(struct power_supply *psy)
  120. {
  121. int rc = 0;
  122. int i, j;
  123. for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++) {
  124. rc = device_create_file(psy->dev,
  125. &power_supply_static_attrs[i]);
  126. if (rc)
  127. goto statics_failed;
  128. }
  129. for (j = 0; j < psy->num_properties; j++) {
  130. rc = device_create_file(psy->dev,
  131. &power_supply_attrs[psy->properties[j]]);
  132. if (rc)
  133. goto dynamics_failed;
  134. }
  135. goto succeed;
  136. dynamics_failed:
  137. while (j--)
  138. device_remove_file(psy->dev,
  139. &power_supply_attrs[psy->properties[j]]);
  140. statics_failed:
  141. while (i--)
  142. device_remove_file(psy->dev, &power_supply_static_attrs[i]);
  143. succeed:
  144. return rc;
  145. }
  146. void power_supply_remove_attrs(struct power_supply *psy)
  147. {
  148. int i;
  149. for (i = 0; i < ARRAY_SIZE(power_supply_static_attrs); i++)
  150. device_remove_file(psy->dev, &power_supply_static_attrs[i]);
  151. for (i = 0; i < psy->num_properties; i++)
  152. device_remove_file(psy->dev,
  153. &power_supply_attrs[psy->properties[i]]);
  154. }
  155. static char *kstruprdup(const char *str, gfp_t gfp)
  156. {
  157. char *ret, *ustr;
  158. ustr = ret = kmalloc(strlen(str) + 1, gfp);
  159. if (!ret)
  160. return NULL;
  161. while (*str)
  162. *ustr++ = toupper(*str++);
  163. *ustr = 0;
  164. return ret;
  165. }
  166. int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
  167. {
  168. struct power_supply *psy = dev_get_drvdata(dev);
  169. int ret = 0, j;
  170. char *prop_buf;
  171. char *attrname;
  172. dev_dbg(dev, "uevent\n");
  173. if (!psy || !psy->dev) {
  174. dev_dbg(dev, "No power supply yet\n");
  175. return ret;
  176. }
  177. dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
  178. ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
  179. if (ret)
  180. return ret;
  181. prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
  182. if (!prop_buf)
  183. return -ENOMEM;
  184. for (j = 0; j < ARRAY_SIZE(power_supply_static_attrs); j++) {
  185. struct device_attribute *attr;
  186. char *line;
  187. attr = &power_supply_static_attrs[j];
  188. ret = power_supply_show_static_attrs(dev, attr, prop_buf);
  189. if (ret < 0)
  190. goto out;
  191. line = strchr(prop_buf, '\n');
  192. if (line)
  193. *line = 0;
  194. attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
  195. if (!attrname) {
  196. ret = -ENOMEM;
  197. goto out;
  198. }
  199. dev_dbg(dev, "Static prop %s=%s\n", attrname, prop_buf);
  200. ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
  201. kfree(attrname);
  202. if (ret)
  203. goto out;
  204. }
  205. dev_dbg(dev, "%zd dynamic props\n", psy->num_properties);
  206. for (j = 0; j < psy->num_properties; j++) {
  207. struct device_attribute *attr;
  208. char *line;
  209. attr = &power_supply_attrs[psy->properties[j]];
  210. ret = power_supply_show_property(dev, attr, prop_buf);
  211. if (ret == -ENODEV) {
  212. /* When a battery is absent, we expect -ENODEV. Don't abort;
  213. send the uevent with at least the the PRESENT=0 property */
  214. ret = 0;
  215. continue;
  216. }
  217. if (ret < 0)
  218. goto out;
  219. line = strchr(prop_buf, '\n');
  220. if (line)
  221. *line = 0;
  222. attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
  223. if (!attrname) {
  224. ret = -ENOMEM;
  225. goto out;
  226. }
  227. dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
  228. ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
  229. kfree(attrname);
  230. if (ret)
  231. goto out;
  232. }
  233. out:
  234. free_page((unsigned long)prop_buf);
  235. return ret;
  236. }