power_supply_sysfs.c 8.7 KB

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