power_supply_sysfs.c 7.3 KB

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