power_supply_sysfs.c 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  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", "Wireless"
  43. };
  44. static char *status_text[] = {
  45. "Unknown", "Charging", "Discharging", "Not charging", "Full", "Cmd discharging"
  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. }
  83. if (off == POWER_SUPPLY_PROP_STATUS)
  84. return sprintf(buf, "%s\n", status_text[value.intval]);
  85. else if (off == POWER_SUPPLY_PROP_CHARGE_TYPE)
  86. return sprintf(buf, "%s\n", charge_type[value.intval]);
  87. else if (off == POWER_SUPPLY_PROP_HEALTH)
  88. return sprintf(buf, "%s\n", health_text[value.intval]);
  89. else if (off == POWER_SUPPLY_PROP_TECHNOLOGY)
  90. return sprintf(buf, "%s\n", technology_text[value.intval]);
  91. else if (off == POWER_SUPPLY_PROP_CAPACITY_LEVEL)
  92. return sprintf(buf, "%s\n", capacity_level_text[value.intval]);
  93. else if (off == POWER_SUPPLY_PROP_TYPE)
  94. return sprintf(buf, "%s\n", type_text[value.intval]);
  95. else if (off == POWER_SUPPLY_PROP_SCOPE)
  96. return sprintf(buf, "%s\n", scope_text[value.intval]);
  97. else if (off >= POWER_SUPPLY_PROP_MODEL_NAME)
  98. return sprintf(buf, "%s\n", value.strval);
  99. if (off == POWER_SUPPLY_PROP_CHARGE_COUNTER_EXT)
  100. return sprintf(buf, "%lld\n", value.int64val);
  101. else
  102. return sprintf(buf, "%d\n", value.intval);
  103. }
  104. static ssize_t power_supply_store_property(struct device *dev,
  105. struct device_attribute *attr,
  106. const char *buf, size_t count) {
  107. ssize_t ret;
  108. struct power_supply *psy = dev_get_drvdata(dev);
  109. const ptrdiff_t off = attr - power_supply_attrs;
  110. union power_supply_propval value;
  111. long long_val;
  112. /* TODO: support other types than int */
  113. ret = kstrtol(buf, 10, &long_val);
  114. if (ret < 0)
  115. return ret;
  116. value.intval = long_val;
  117. ret = psy->set_property(psy, off, &value);
  118. if (ret < 0)
  119. return ret;
  120. return count;
  121. }
  122. /* Must be in the same order as POWER_SUPPLY_PROP_* */
  123. static struct device_attribute power_supply_attrs[] = {
  124. /* Properties of type `int' */
  125. POWER_SUPPLY_ATTR(status),
  126. POWER_SUPPLY_ATTR(charge_type),
  127. POWER_SUPPLY_ATTR(health),
  128. POWER_SUPPLY_ATTR(present),
  129. POWER_SUPPLY_ATTR(online),
  130. POWER_SUPPLY_ATTR(authentic),
  131. POWER_SUPPLY_ATTR(technology),
  132. POWER_SUPPLY_ATTR(cycle_count),
  133. POWER_SUPPLY_ATTR(voltage_max),
  134. POWER_SUPPLY_ATTR(voltage_min),
  135. POWER_SUPPLY_ATTR(voltage_max_design),
  136. POWER_SUPPLY_ATTR(voltage_min_design),
  137. POWER_SUPPLY_ATTR(voltage_now),
  138. POWER_SUPPLY_ATTR(voltage_avg),
  139. POWER_SUPPLY_ATTR(voltage_ocv),
  140. POWER_SUPPLY_ATTR(voltage_boot),
  141. POWER_SUPPLY_ATTR(current_max),
  142. POWER_SUPPLY_ATTR(current_now),
  143. POWER_SUPPLY_ATTR(current_avg),
  144. POWER_SUPPLY_ATTR(current_boot),
  145. POWER_SUPPLY_ATTR(power_now),
  146. POWER_SUPPLY_ATTR(power_avg),
  147. POWER_SUPPLY_ATTR(charge_full_design),
  148. POWER_SUPPLY_ATTR(charge_empty_design),
  149. POWER_SUPPLY_ATTR(charge_full),
  150. POWER_SUPPLY_ATTR(charge_empty),
  151. POWER_SUPPLY_ATTR(charge_now),
  152. POWER_SUPPLY_ATTR(charge_avg),
  153. POWER_SUPPLY_ATTR(charge_counter),
  154. POWER_SUPPLY_ATTR(constant_charge_current),
  155. POWER_SUPPLY_ATTR(constant_charge_current_max),
  156. POWER_SUPPLY_ATTR(constant_charge_voltage),
  157. POWER_SUPPLY_ATTR(constant_charge_voltage_max),
  158. POWER_SUPPLY_ATTR(charge_control_limit),
  159. POWER_SUPPLY_ATTR(charge_control_limit_max),
  160. POWER_SUPPLY_ATTR(input_current_limit),
  161. POWER_SUPPLY_ATTR(energy_full_design),
  162. POWER_SUPPLY_ATTR(energy_empty_design),
  163. POWER_SUPPLY_ATTR(energy_full),
  164. POWER_SUPPLY_ATTR(energy_empty),
  165. POWER_SUPPLY_ATTR(energy_now),
  166. POWER_SUPPLY_ATTR(energy_avg),
  167. POWER_SUPPLY_ATTR(capacity),
  168. POWER_SUPPLY_ATTR(capacity_alert_min),
  169. POWER_SUPPLY_ATTR(capacity_alert_max),
  170. POWER_SUPPLY_ATTR(capacity_level),
  171. POWER_SUPPLY_ATTR(temp),
  172. POWER_SUPPLY_ATTR(temp_max),
  173. POWER_SUPPLY_ATTR(temp_min),
  174. POWER_SUPPLY_ATTR(temp_alert_min),
  175. POWER_SUPPLY_ATTR(temp_alert_max),
  176. POWER_SUPPLY_ATTR(temp_ambient),
  177. POWER_SUPPLY_ATTR(temp_ambient_alert_min),
  178. POWER_SUPPLY_ATTR(temp_ambient_alert_max),
  179. POWER_SUPPLY_ATTR(time_to_empty_now),
  180. POWER_SUPPLY_ATTR(time_to_empty_avg),
  181. POWER_SUPPLY_ATTR(time_to_full_now),
  182. POWER_SUPPLY_ATTR(time_to_full_avg),
  183. POWER_SUPPLY_ATTR(type),
  184. POWER_SUPPLY_ATTR(scope),
  185. POWER_SUPPLY_ATTR(charge_term_current),
  186. POWER_SUPPLY_ATTR(calibrate),
  187. /* Local extensions */
  188. POWER_SUPPLY_ATTR(usb_hc),
  189. POWER_SUPPLY_ATTR(usb_otg),
  190. POWER_SUPPLY_ATTR(charge_enabled),
  191. /* Local extensions of type int64_t */
  192. POWER_SUPPLY_ATTR(charge_counter_ext),
  193. /* 20100723 James Lo */
  194. POWER_SUPPLY_ATTR(batt_vol),
  195. POWER_SUPPLY_ATTR(batt_temp),
  196. /* 20100405 Add for EM */
  197. POWER_SUPPLY_ATTR(TemperatureR),
  198. POWER_SUPPLY_ATTR(TempBattVoltage),
  199. POWER_SUPPLY_ATTR(InstatVolt),
  200. POWER_SUPPLY_ATTR(BatteryAverageCurrent),
  201. POWER_SUPPLY_ATTR(BatterySenseVoltage),
  202. POWER_SUPPLY_ATTR(ISenseVoltage),
  203. POWER_SUPPLY_ATTR(ChargerVoltage),
  204. /* Dual battery */
  205. POWER_SUPPLY_ATTR(status_smb),
  206. POWER_SUPPLY_ATTR(capacity_smb),
  207. POWER_SUPPLY_ATTR(present_smb),
  208. /* ADB CMD Discharging */
  209. POWER_SUPPLY_ATTR(adjust_power),
  210. /* Properties of type `const char *' */
  211. POWER_SUPPLY_ATTR(model_name),
  212. POWER_SUPPLY_ATTR(manufacturer),
  213. POWER_SUPPLY_ATTR(serial_number),
  214. };
  215. static struct attribute *
  216. __power_supply_attrs[ARRAY_SIZE(power_supply_attrs) + 1];
  217. static umode_t power_supply_attr_is_visible(struct kobject *kobj,
  218. struct attribute *attr,
  219. int attrno)
  220. {
  221. struct device *dev = container_of(kobj, struct device, kobj);
  222. struct power_supply *psy = dev_get_drvdata(dev);
  223. umode_t mode = S_IRUSR | S_IRGRP | S_IROTH;
  224. int i;
  225. if (attrno == POWER_SUPPLY_PROP_TYPE)
  226. return mode;
  227. for (i = 0; i < psy->num_properties; i++) {
  228. int property = psy->properties[i];
  229. if (property == attrno) {
  230. if (psy->property_is_writeable &&
  231. psy->property_is_writeable(psy, property) > 0)
  232. mode |= S_IWUSR;
  233. return mode;
  234. }
  235. }
  236. return 0;
  237. }
  238. static struct attribute_group power_supply_attr_group = {
  239. .attrs = __power_supply_attrs,
  240. .is_visible = power_supply_attr_is_visible,
  241. };
  242. static const struct attribute_group *power_supply_attr_groups[] = {
  243. &power_supply_attr_group,
  244. NULL,
  245. };
  246. void power_supply_init_attrs(struct device_type *dev_type)
  247. {
  248. int i;
  249. dev_type->groups = power_supply_attr_groups;
  250. for (i = 0; i < ARRAY_SIZE(power_supply_attrs); i++)
  251. __power_supply_attrs[i] = &power_supply_attrs[i].attr;
  252. }
  253. static char *kstruprdup(const char *str, gfp_t gfp)
  254. {
  255. char *ret, *ustr;
  256. ustr = ret = kmalloc(strlen(str) + 1, gfp);
  257. if (!ret)
  258. return NULL;
  259. while (*str)
  260. *ustr++ = toupper(*str++);
  261. *ustr = 0;
  262. return ret;
  263. }
  264. int power_supply_uevent(struct device *dev, struct kobj_uevent_env *env)
  265. {
  266. struct power_supply *psy = dev_get_drvdata(dev);
  267. int ret = 0, j;
  268. char *prop_buf;
  269. char *attrname;
  270. dev_dbg(dev, "uevent\n");
  271. if (!psy || !psy->dev) {
  272. dev_dbg(dev, "No power supply yet\n");
  273. return ret;
  274. }
  275. dev_dbg(dev, "POWER_SUPPLY_NAME=%s\n", psy->name);
  276. ret = add_uevent_var(env, "POWER_SUPPLY_NAME=%s", psy->name);
  277. if (ret)
  278. return ret;
  279. prop_buf = (char *)get_zeroed_page(GFP_KERNEL);
  280. if (!prop_buf)
  281. return -ENOMEM;
  282. for (j = 0; j < psy->num_properties; j++) {
  283. struct device_attribute *attr;
  284. char *line;
  285. attr = &power_supply_attrs[psy->properties[j]];
  286. ret = power_supply_show_property(dev, attr, prop_buf);
  287. if (ret == -ENODEV || ret == -ENODATA) {
  288. /* When a battery is absent, we expect -ENODEV. Don't abort;
  289. send the uevent with at least the the PRESENT=0 property */
  290. ret = 0;
  291. continue;
  292. }
  293. if (ret < 0)
  294. goto out;
  295. line = strchr(prop_buf, '\n');
  296. if (line)
  297. *line = 0;
  298. attrname = kstruprdup(attr->attr.name, GFP_KERNEL);
  299. if (!attrname) {
  300. ret = -ENOMEM;
  301. goto out;
  302. }
  303. dev_dbg(dev, "prop %s=%s\n", attrname, prop_buf);
  304. ret = add_uevent_var(env, "POWER_SUPPLY_%s=%s", attrname, prop_buf);
  305. kfree(attrname);
  306. if (ret)
  307. goto out;
  308. }
  309. out:
  310. free_page((unsigned long)prop_buf);
  311. return ret;
  312. }