power_supply_sysfs.c 8.1 KB

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