power_supply_sysfs.c 7.8 KB

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