battery.c 29 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022
  1. /*
  2. * battery.c - ACPI Battery Driver (Revision: 2.0)
  3. *
  4. * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
  5. * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
  6. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  7. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  8. *
  9. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or (at
  14. * your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful, but
  17. * WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  19. * General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  24. *
  25. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  26. */
  27. #include <linux/kernel.h>
  28. #include <linux/module.h>
  29. #include <linux/init.h>
  30. #include <linux/types.h>
  31. #include <linux/jiffies.h>
  32. #include <linux/async.h>
  33. #include <linux/dmi.h>
  34. #include <linux/slab.h>
  35. #ifdef CONFIG_ACPI_PROCFS_POWER
  36. #include <linux/proc_fs.h>
  37. #include <linux/seq_file.h>
  38. #include <asm/uaccess.h>
  39. #endif
  40. #include <acpi/acpi_bus.h>
  41. #include <acpi/acpi_drivers.h>
  42. #ifdef CONFIG_ACPI_SYSFS_POWER
  43. #include <linux/power_supply.h>
  44. #endif
  45. #define PREFIX "ACPI: "
  46. #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
  47. #define ACPI_BATTERY_CLASS "battery"
  48. #define ACPI_BATTERY_DEVICE_NAME "Battery"
  49. #define ACPI_BATTERY_NOTIFY_STATUS 0x80
  50. #define ACPI_BATTERY_NOTIFY_INFO 0x81
  51. #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
  52. #define _COMPONENT ACPI_BATTERY_COMPONENT
  53. ACPI_MODULE_NAME("battery");
  54. MODULE_AUTHOR("Paul Diefenbaugh");
  55. MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
  56. MODULE_DESCRIPTION("ACPI Battery Driver");
  57. MODULE_LICENSE("GPL");
  58. static unsigned int cache_time = 1000;
  59. module_param(cache_time, uint, 0644);
  60. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  61. #ifdef CONFIG_ACPI_PROCFS_POWER
  62. extern struct proc_dir_entry *acpi_lock_battery_dir(void);
  63. extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
  64. enum acpi_battery_files {
  65. info_tag = 0,
  66. state_tag,
  67. alarm_tag,
  68. ACPI_BATTERY_NUMFILES,
  69. };
  70. #endif
  71. static const struct acpi_device_id battery_device_ids[] = {
  72. {"PNP0C0A", 0},
  73. {"", 0},
  74. };
  75. MODULE_DEVICE_TABLE(acpi, battery_device_ids);
  76. enum {
  77. ACPI_BATTERY_ALARM_PRESENT,
  78. ACPI_BATTERY_XINFO_PRESENT,
  79. /* For buggy DSDTs that report negative 16-bit values for either
  80. * charging or discharging current and/or report 0 as 65536
  81. * due to bad math.
  82. */
  83. ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
  84. };
  85. struct acpi_battery {
  86. struct mutex lock;
  87. #ifdef CONFIG_ACPI_SYSFS_POWER
  88. struct power_supply bat;
  89. #endif
  90. struct acpi_device *device;
  91. unsigned long update_time;
  92. int rate_now;
  93. int capacity_now;
  94. int voltage_now;
  95. int design_capacity;
  96. int full_charge_capacity;
  97. int technology;
  98. int design_voltage;
  99. int design_capacity_warning;
  100. int design_capacity_low;
  101. int cycle_count;
  102. int measurement_accuracy;
  103. int max_sampling_time;
  104. int min_sampling_time;
  105. int max_averaging_interval;
  106. int min_averaging_interval;
  107. int capacity_granularity_1;
  108. int capacity_granularity_2;
  109. int alarm;
  110. char model_number[32];
  111. char serial_number[32];
  112. char type[32];
  113. char oem_info[32];
  114. int state;
  115. int power_unit;
  116. unsigned long flags;
  117. };
  118. #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
  119. inline int acpi_battery_present(struct acpi_battery *battery)
  120. {
  121. return battery->device->status.battery_present;
  122. }
  123. #ifdef CONFIG_ACPI_SYSFS_POWER
  124. static int acpi_battery_technology(struct acpi_battery *battery)
  125. {
  126. if (!strcasecmp("NiCd", battery->type))
  127. return POWER_SUPPLY_TECHNOLOGY_NiCd;
  128. if (!strcasecmp("NiMH", battery->type))
  129. return POWER_SUPPLY_TECHNOLOGY_NiMH;
  130. if (!strcasecmp("LION", battery->type))
  131. return POWER_SUPPLY_TECHNOLOGY_LION;
  132. if (!strncasecmp("LI-ION", battery->type, 6))
  133. return POWER_SUPPLY_TECHNOLOGY_LION;
  134. if (!strcasecmp("LiP", battery->type))
  135. return POWER_SUPPLY_TECHNOLOGY_LIPO;
  136. return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  137. }
  138. static int acpi_battery_get_state(struct acpi_battery *battery);
  139. static int acpi_battery_is_charged(struct acpi_battery *battery)
  140. {
  141. /* either charging or discharging */
  142. if (battery->state != 0)
  143. return 0;
  144. /* battery not reporting charge */
  145. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
  146. battery->capacity_now == 0)
  147. return 0;
  148. /* good batteries update full_charge as the batteries degrade */
  149. if (battery->full_charge_capacity == battery->capacity_now)
  150. return 1;
  151. /* fallback to using design values for broken batteries */
  152. if (battery->design_capacity == battery->capacity_now)
  153. return 1;
  154. /* we don't do any sort of metric based on percentages */
  155. return 0;
  156. }
  157. static int acpi_battery_get_property(struct power_supply *psy,
  158. enum power_supply_property psp,
  159. union power_supply_propval *val)
  160. {
  161. int ret = 0;
  162. struct acpi_battery *battery = to_acpi_battery(psy);
  163. if (acpi_battery_present(battery)) {
  164. /* run battery update only if it is present */
  165. acpi_battery_get_state(battery);
  166. } else if (psp != POWER_SUPPLY_PROP_PRESENT)
  167. return -ENODEV;
  168. switch (psp) {
  169. case POWER_SUPPLY_PROP_STATUS:
  170. if (battery->state & 0x01)
  171. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  172. else if (battery->state & 0x02)
  173. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  174. else if (acpi_battery_is_charged(battery))
  175. val->intval = POWER_SUPPLY_STATUS_FULL;
  176. else
  177. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  178. break;
  179. case POWER_SUPPLY_PROP_PRESENT:
  180. val->intval = acpi_battery_present(battery);
  181. break;
  182. case POWER_SUPPLY_PROP_TECHNOLOGY:
  183. val->intval = acpi_battery_technology(battery);
  184. break;
  185. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  186. val->intval = battery->cycle_count;
  187. break;
  188. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  189. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  190. ret = -ENODEV;
  191. else
  192. val->intval = battery->design_voltage * 1000;
  193. break;
  194. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  195. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  196. ret = -ENODEV;
  197. else
  198. val->intval = battery->voltage_now * 1000;
  199. break;
  200. case POWER_SUPPLY_PROP_CURRENT_NOW:
  201. case POWER_SUPPLY_PROP_POWER_NOW:
  202. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  203. ret = -ENODEV;
  204. else
  205. val->intval = battery->rate_now * 1000;
  206. break;
  207. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  208. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  209. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  210. ret = -ENODEV;
  211. else
  212. val->intval = battery->design_capacity * 1000;
  213. break;
  214. case POWER_SUPPLY_PROP_CHARGE_FULL:
  215. case POWER_SUPPLY_PROP_ENERGY_FULL:
  216. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  217. ret = -ENODEV;
  218. else
  219. val->intval = battery->full_charge_capacity * 1000;
  220. break;
  221. case POWER_SUPPLY_PROP_CHARGE_NOW:
  222. case POWER_SUPPLY_PROP_ENERGY_NOW:
  223. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  224. ret = -ENODEV;
  225. else
  226. val->intval = battery->capacity_now * 1000;
  227. break;
  228. case POWER_SUPPLY_PROP_MODEL_NAME:
  229. val->strval = battery->model_number;
  230. break;
  231. case POWER_SUPPLY_PROP_MANUFACTURER:
  232. val->strval = battery->oem_info;
  233. break;
  234. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  235. val->strval = battery->serial_number;
  236. break;
  237. default:
  238. ret = -EINVAL;
  239. }
  240. return ret;
  241. }
  242. static enum power_supply_property charge_battery_props[] = {
  243. POWER_SUPPLY_PROP_STATUS,
  244. POWER_SUPPLY_PROP_PRESENT,
  245. POWER_SUPPLY_PROP_TECHNOLOGY,
  246. POWER_SUPPLY_PROP_CYCLE_COUNT,
  247. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  248. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  249. POWER_SUPPLY_PROP_CURRENT_NOW,
  250. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  251. POWER_SUPPLY_PROP_CHARGE_FULL,
  252. POWER_SUPPLY_PROP_CHARGE_NOW,
  253. POWER_SUPPLY_PROP_MODEL_NAME,
  254. POWER_SUPPLY_PROP_MANUFACTURER,
  255. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  256. };
  257. static enum power_supply_property energy_battery_props[] = {
  258. POWER_SUPPLY_PROP_STATUS,
  259. POWER_SUPPLY_PROP_PRESENT,
  260. POWER_SUPPLY_PROP_TECHNOLOGY,
  261. POWER_SUPPLY_PROP_CYCLE_COUNT,
  262. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  263. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  264. POWER_SUPPLY_PROP_POWER_NOW,
  265. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  266. POWER_SUPPLY_PROP_ENERGY_FULL,
  267. POWER_SUPPLY_PROP_ENERGY_NOW,
  268. POWER_SUPPLY_PROP_MODEL_NAME,
  269. POWER_SUPPLY_PROP_MANUFACTURER,
  270. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  271. };
  272. #endif
  273. #ifdef CONFIG_ACPI_PROCFS_POWER
  274. inline char *acpi_battery_units(struct acpi_battery *battery)
  275. {
  276. return (battery->power_unit)?"mA":"mW";
  277. }
  278. #endif
  279. /* --------------------------------------------------------------------------
  280. Battery Management
  281. -------------------------------------------------------------------------- */
  282. struct acpi_offsets {
  283. size_t offset; /* offset inside struct acpi_sbs_battery */
  284. u8 mode; /* int or string? */
  285. };
  286. static struct acpi_offsets state_offsets[] = {
  287. {offsetof(struct acpi_battery, state), 0},
  288. {offsetof(struct acpi_battery, rate_now), 0},
  289. {offsetof(struct acpi_battery, capacity_now), 0},
  290. {offsetof(struct acpi_battery, voltage_now), 0},
  291. };
  292. static struct acpi_offsets info_offsets[] = {
  293. {offsetof(struct acpi_battery, power_unit), 0},
  294. {offsetof(struct acpi_battery, design_capacity), 0},
  295. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  296. {offsetof(struct acpi_battery, technology), 0},
  297. {offsetof(struct acpi_battery, design_voltage), 0},
  298. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  299. {offsetof(struct acpi_battery, design_capacity_low), 0},
  300. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  301. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  302. {offsetof(struct acpi_battery, model_number), 1},
  303. {offsetof(struct acpi_battery, serial_number), 1},
  304. {offsetof(struct acpi_battery, type), 1},
  305. {offsetof(struct acpi_battery, oem_info), 1},
  306. };
  307. static struct acpi_offsets extended_info_offsets[] = {
  308. {offsetof(struct acpi_battery, power_unit), 0},
  309. {offsetof(struct acpi_battery, design_capacity), 0},
  310. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  311. {offsetof(struct acpi_battery, technology), 0},
  312. {offsetof(struct acpi_battery, design_voltage), 0},
  313. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  314. {offsetof(struct acpi_battery, design_capacity_low), 0},
  315. {offsetof(struct acpi_battery, cycle_count), 0},
  316. {offsetof(struct acpi_battery, measurement_accuracy), 0},
  317. {offsetof(struct acpi_battery, max_sampling_time), 0},
  318. {offsetof(struct acpi_battery, min_sampling_time), 0},
  319. {offsetof(struct acpi_battery, max_averaging_interval), 0},
  320. {offsetof(struct acpi_battery, min_averaging_interval), 0},
  321. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  322. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  323. {offsetof(struct acpi_battery, model_number), 1},
  324. {offsetof(struct acpi_battery, serial_number), 1},
  325. {offsetof(struct acpi_battery, type), 1},
  326. {offsetof(struct acpi_battery, oem_info), 1},
  327. };
  328. static int extract_package(struct acpi_battery *battery,
  329. union acpi_object *package,
  330. struct acpi_offsets *offsets, int num)
  331. {
  332. int i;
  333. union acpi_object *element;
  334. if (package->type != ACPI_TYPE_PACKAGE)
  335. return -EFAULT;
  336. for (i = 0; i < num; ++i) {
  337. if (package->package.count <= i)
  338. return -EFAULT;
  339. element = &package->package.elements[i];
  340. if (offsets[i].mode) {
  341. u8 *ptr = (u8 *)battery + offsets[i].offset;
  342. if (element->type == ACPI_TYPE_STRING ||
  343. element->type == ACPI_TYPE_BUFFER)
  344. strncpy(ptr, element->string.pointer, 32);
  345. else if (element->type == ACPI_TYPE_INTEGER) {
  346. strncpy(ptr, (u8 *)&element->integer.value,
  347. sizeof(u64));
  348. ptr[sizeof(u64)] = 0;
  349. } else
  350. *ptr = 0; /* don't have value */
  351. } else {
  352. int *x = (int *)((u8 *)battery + offsets[i].offset);
  353. *x = (element->type == ACPI_TYPE_INTEGER) ?
  354. element->integer.value : -1;
  355. }
  356. }
  357. return 0;
  358. }
  359. static int acpi_battery_get_status(struct acpi_battery *battery)
  360. {
  361. if (acpi_bus_get_status(battery->device)) {
  362. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
  363. return -ENODEV;
  364. }
  365. return 0;
  366. }
  367. static int acpi_battery_get_info(struct acpi_battery *battery)
  368. {
  369. int result = -EFAULT;
  370. acpi_status status = 0;
  371. char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
  372. "_BIX" : "_BIF";
  373. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  374. if (!acpi_battery_present(battery))
  375. return 0;
  376. mutex_lock(&battery->lock);
  377. status = acpi_evaluate_object(battery->device->handle, name,
  378. NULL, &buffer);
  379. mutex_unlock(&battery->lock);
  380. if (ACPI_FAILURE(status)) {
  381. ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
  382. return -ENODEV;
  383. }
  384. if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
  385. result = extract_package(battery, buffer.pointer,
  386. extended_info_offsets,
  387. ARRAY_SIZE(extended_info_offsets));
  388. else
  389. result = extract_package(battery, buffer.pointer,
  390. info_offsets, ARRAY_SIZE(info_offsets));
  391. kfree(buffer.pointer);
  392. return result;
  393. }
  394. static int acpi_battery_get_state(struct acpi_battery *battery)
  395. {
  396. int result = 0;
  397. acpi_status status = 0;
  398. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  399. if (!acpi_battery_present(battery))
  400. return 0;
  401. if (battery->update_time &&
  402. time_before(jiffies, battery->update_time +
  403. msecs_to_jiffies(cache_time)))
  404. return 0;
  405. mutex_lock(&battery->lock);
  406. status = acpi_evaluate_object(battery->device->handle, "_BST",
  407. NULL, &buffer);
  408. mutex_unlock(&battery->lock);
  409. if (ACPI_FAILURE(status)) {
  410. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  411. return -ENODEV;
  412. }
  413. result = extract_package(battery, buffer.pointer,
  414. state_offsets, ARRAY_SIZE(state_offsets));
  415. battery->update_time = jiffies;
  416. kfree(buffer.pointer);
  417. if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
  418. battery->rate_now != -1)
  419. battery->rate_now = abs((s16)battery->rate_now);
  420. return result;
  421. }
  422. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  423. {
  424. acpi_status status = 0;
  425. union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
  426. struct acpi_object_list arg_list = { 1, &arg0 };
  427. if (!acpi_battery_present(battery) ||
  428. !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
  429. return -ENODEV;
  430. arg0.integer.value = battery->alarm;
  431. mutex_lock(&battery->lock);
  432. status = acpi_evaluate_object(battery->device->handle, "_BTP",
  433. &arg_list, NULL);
  434. mutex_unlock(&battery->lock);
  435. if (ACPI_FAILURE(status))
  436. return -ENODEV;
  437. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
  438. return 0;
  439. }
  440. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  441. {
  442. acpi_status status = AE_OK;
  443. acpi_handle handle = NULL;
  444. /* See if alarms are supported, and if so, set default */
  445. status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
  446. if (ACPI_FAILURE(status)) {
  447. clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  448. return 0;
  449. }
  450. set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  451. if (!battery->alarm)
  452. battery->alarm = battery->design_capacity_warning;
  453. return acpi_battery_set_alarm(battery);
  454. }
  455. #ifdef CONFIG_ACPI_SYSFS_POWER
  456. static ssize_t acpi_battery_alarm_show(struct device *dev,
  457. struct device_attribute *attr,
  458. char *buf)
  459. {
  460. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  461. return sprintf(buf, "%d\n", battery->alarm * 1000);
  462. }
  463. static ssize_t acpi_battery_alarm_store(struct device *dev,
  464. struct device_attribute *attr,
  465. const char *buf, size_t count)
  466. {
  467. unsigned long x;
  468. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  469. if (sscanf(buf, "%ld\n", &x) == 1)
  470. battery->alarm = x/1000;
  471. if (acpi_battery_present(battery))
  472. acpi_battery_set_alarm(battery);
  473. return count;
  474. }
  475. static struct device_attribute alarm_attr = {
  476. .attr = {.name = "alarm", .mode = 0644},
  477. .show = acpi_battery_alarm_show,
  478. .store = acpi_battery_alarm_store,
  479. };
  480. static int sysfs_add_battery(struct acpi_battery *battery)
  481. {
  482. int result;
  483. if (battery->power_unit) {
  484. battery->bat.properties = charge_battery_props;
  485. battery->bat.num_properties =
  486. ARRAY_SIZE(charge_battery_props);
  487. } else {
  488. battery->bat.properties = energy_battery_props;
  489. battery->bat.num_properties =
  490. ARRAY_SIZE(energy_battery_props);
  491. }
  492. battery->bat.name = acpi_device_bid(battery->device);
  493. battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  494. battery->bat.get_property = acpi_battery_get_property;
  495. result = power_supply_register(&battery->device->dev, &battery->bat);
  496. if (result)
  497. return result;
  498. return device_create_file(battery->bat.dev, &alarm_attr);
  499. }
  500. static void sysfs_remove_battery(struct acpi_battery *battery)
  501. {
  502. if (!battery->bat.dev)
  503. return;
  504. device_remove_file(battery->bat.dev, &alarm_attr);
  505. power_supply_unregister(&battery->bat);
  506. battery->bat.dev = NULL;
  507. }
  508. #endif
  509. static void acpi_battery_quirks(struct acpi_battery *battery)
  510. {
  511. if (dmi_name_in_vendors("Acer") && battery->power_unit) {
  512. set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
  513. }
  514. }
  515. static int acpi_battery_update(struct acpi_battery *battery)
  516. {
  517. int result, old_present = acpi_battery_present(battery);
  518. result = acpi_battery_get_status(battery);
  519. if (result)
  520. return result;
  521. if (!acpi_battery_present(battery)) {
  522. #ifdef CONFIG_ACPI_SYSFS_POWER
  523. sysfs_remove_battery(battery);
  524. #endif
  525. battery->update_time = 0;
  526. return 0;
  527. }
  528. if (!battery->update_time ||
  529. old_present != acpi_battery_present(battery)) {
  530. result = acpi_battery_get_info(battery);
  531. if (result)
  532. return result;
  533. acpi_battery_quirks(battery);
  534. acpi_battery_init_alarm(battery);
  535. }
  536. #ifdef CONFIG_ACPI_SYSFS_POWER
  537. if (!battery->bat.dev)
  538. sysfs_add_battery(battery);
  539. #endif
  540. return acpi_battery_get_state(battery);
  541. }
  542. /* --------------------------------------------------------------------------
  543. FS Interface (/proc)
  544. -------------------------------------------------------------------------- */
  545. #ifdef CONFIG_ACPI_PROCFS_POWER
  546. static struct proc_dir_entry *acpi_battery_dir;
  547. static int acpi_battery_print_info(struct seq_file *seq, int result)
  548. {
  549. struct acpi_battery *battery = seq->private;
  550. if (result)
  551. goto end;
  552. seq_printf(seq, "present: %s\n",
  553. acpi_battery_present(battery)?"yes":"no");
  554. if (!acpi_battery_present(battery))
  555. goto end;
  556. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  557. seq_printf(seq, "design capacity: unknown\n");
  558. else
  559. seq_printf(seq, "design capacity: %d %sh\n",
  560. battery->design_capacity,
  561. acpi_battery_units(battery));
  562. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  563. seq_printf(seq, "last full capacity: unknown\n");
  564. else
  565. seq_printf(seq, "last full capacity: %d %sh\n",
  566. battery->full_charge_capacity,
  567. acpi_battery_units(battery));
  568. seq_printf(seq, "battery technology: %srechargeable\n",
  569. (!battery->technology)?"non-":"");
  570. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  571. seq_printf(seq, "design voltage: unknown\n");
  572. else
  573. seq_printf(seq, "design voltage: %d mV\n",
  574. battery->design_voltage);
  575. seq_printf(seq, "design capacity warning: %d %sh\n",
  576. battery->design_capacity_warning,
  577. acpi_battery_units(battery));
  578. seq_printf(seq, "design capacity low: %d %sh\n",
  579. battery->design_capacity_low,
  580. acpi_battery_units(battery));
  581. seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
  582. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  583. battery->capacity_granularity_1,
  584. acpi_battery_units(battery));
  585. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  586. battery->capacity_granularity_2,
  587. acpi_battery_units(battery));
  588. seq_printf(seq, "model number: %s\n", battery->model_number);
  589. seq_printf(seq, "serial number: %s\n", battery->serial_number);
  590. seq_printf(seq, "battery type: %s\n", battery->type);
  591. seq_printf(seq, "OEM info: %s\n", battery->oem_info);
  592. end:
  593. if (result)
  594. seq_printf(seq, "ERROR: Unable to read battery info\n");
  595. return result;
  596. }
  597. static int acpi_battery_print_state(struct seq_file *seq, int result)
  598. {
  599. struct acpi_battery *battery = seq->private;
  600. if (result)
  601. goto end;
  602. seq_printf(seq, "present: %s\n",
  603. acpi_battery_present(battery)?"yes":"no");
  604. if (!acpi_battery_present(battery))
  605. goto end;
  606. seq_printf(seq, "capacity state: %s\n",
  607. (battery->state & 0x04)?"critical":"ok");
  608. if ((battery->state & 0x01) && (battery->state & 0x02))
  609. seq_printf(seq,
  610. "charging state: charging/discharging\n");
  611. else if (battery->state & 0x01)
  612. seq_printf(seq, "charging state: discharging\n");
  613. else if (battery->state & 0x02)
  614. seq_printf(seq, "charging state: charging\n");
  615. else
  616. seq_printf(seq, "charging state: charged\n");
  617. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  618. seq_printf(seq, "present rate: unknown\n");
  619. else
  620. seq_printf(seq, "present rate: %d %s\n",
  621. battery->rate_now, acpi_battery_units(battery));
  622. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  623. seq_printf(seq, "remaining capacity: unknown\n");
  624. else
  625. seq_printf(seq, "remaining capacity: %d %sh\n",
  626. battery->capacity_now, acpi_battery_units(battery));
  627. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  628. seq_printf(seq, "present voltage: unknown\n");
  629. else
  630. seq_printf(seq, "present voltage: %d mV\n",
  631. battery->voltage_now);
  632. end:
  633. if (result)
  634. seq_printf(seq, "ERROR: Unable to read battery state\n");
  635. return result;
  636. }
  637. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  638. {
  639. struct acpi_battery *battery = seq->private;
  640. if (result)
  641. goto end;
  642. if (!acpi_battery_present(battery)) {
  643. seq_printf(seq, "present: no\n");
  644. goto end;
  645. }
  646. seq_printf(seq, "alarm: ");
  647. if (!battery->alarm)
  648. seq_printf(seq, "unsupported\n");
  649. else
  650. seq_printf(seq, "%u %sh\n", battery->alarm,
  651. acpi_battery_units(battery));
  652. end:
  653. if (result)
  654. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  655. return result;
  656. }
  657. static ssize_t acpi_battery_write_alarm(struct file *file,
  658. const char __user * buffer,
  659. size_t count, loff_t * ppos)
  660. {
  661. int result = 0;
  662. char alarm_string[12] = { '\0' };
  663. struct seq_file *m = file->private_data;
  664. struct acpi_battery *battery = m->private;
  665. if (!battery || (count > sizeof(alarm_string) - 1))
  666. return -EINVAL;
  667. if (!acpi_battery_present(battery)) {
  668. result = -ENODEV;
  669. goto end;
  670. }
  671. if (copy_from_user(alarm_string, buffer, count)) {
  672. result = -EFAULT;
  673. goto end;
  674. }
  675. alarm_string[count] = '\0';
  676. battery->alarm = simple_strtol(alarm_string, NULL, 0);
  677. result = acpi_battery_set_alarm(battery);
  678. end:
  679. if (!result)
  680. return count;
  681. return result;
  682. }
  683. typedef int(*print_func)(struct seq_file *seq, int result);
  684. static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
  685. acpi_battery_print_info,
  686. acpi_battery_print_state,
  687. acpi_battery_print_alarm,
  688. };
  689. static int acpi_battery_read(int fid, struct seq_file *seq)
  690. {
  691. struct acpi_battery *battery = seq->private;
  692. int result = acpi_battery_update(battery);
  693. return acpi_print_funcs[fid](seq, result);
  694. }
  695. #define DECLARE_FILE_FUNCTIONS(_name) \
  696. static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
  697. { \
  698. return acpi_battery_read(_name##_tag, seq); \
  699. } \
  700. static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
  701. { \
  702. return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
  703. }
  704. DECLARE_FILE_FUNCTIONS(info);
  705. DECLARE_FILE_FUNCTIONS(state);
  706. DECLARE_FILE_FUNCTIONS(alarm);
  707. #undef DECLARE_FILE_FUNCTIONS
  708. #define FILE_DESCRIPTION_RO(_name) \
  709. { \
  710. .name = __stringify(_name), \
  711. .mode = S_IRUGO, \
  712. .ops = { \
  713. .open = acpi_battery_##_name##_open_fs, \
  714. .read = seq_read, \
  715. .llseek = seq_lseek, \
  716. .release = single_release, \
  717. .owner = THIS_MODULE, \
  718. }, \
  719. }
  720. #define FILE_DESCRIPTION_RW(_name) \
  721. { \
  722. .name = __stringify(_name), \
  723. .mode = S_IFREG | S_IRUGO | S_IWUSR, \
  724. .ops = { \
  725. .open = acpi_battery_##_name##_open_fs, \
  726. .read = seq_read, \
  727. .llseek = seq_lseek, \
  728. .write = acpi_battery_write_##_name, \
  729. .release = single_release, \
  730. .owner = THIS_MODULE, \
  731. }, \
  732. }
  733. static struct battery_file {
  734. struct file_operations ops;
  735. mode_t mode;
  736. const char *name;
  737. } acpi_battery_file[] = {
  738. FILE_DESCRIPTION_RO(info),
  739. FILE_DESCRIPTION_RO(state),
  740. FILE_DESCRIPTION_RW(alarm),
  741. };
  742. #undef FILE_DESCRIPTION_RO
  743. #undef FILE_DESCRIPTION_RW
  744. static int acpi_battery_add_fs(struct acpi_device *device)
  745. {
  746. struct proc_dir_entry *entry = NULL;
  747. int i;
  748. if (!acpi_device_dir(device)) {
  749. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  750. acpi_battery_dir);
  751. if (!acpi_device_dir(device))
  752. return -ENODEV;
  753. }
  754. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  755. entry = proc_create_data(acpi_battery_file[i].name,
  756. acpi_battery_file[i].mode,
  757. acpi_device_dir(device),
  758. &acpi_battery_file[i].ops,
  759. acpi_driver_data(device));
  760. if (!entry)
  761. return -ENODEV;
  762. }
  763. return 0;
  764. }
  765. static void acpi_battery_remove_fs(struct acpi_device *device)
  766. {
  767. int i;
  768. if (!acpi_device_dir(device))
  769. return;
  770. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
  771. remove_proc_entry(acpi_battery_file[i].name,
  772. acpi_device_dir(device));
  773. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  774. acpi_device_dir(device) = NULL;
  775. }
  776. #endif
  777. /* --------------------------------------------------------------------------
  778. Driver Interface
  779. -------------------------------------------------------------------------- */
  780. static void acpi_battery_notify(struct acpi_device *device, u32 event)
  781. {
  782. struct acpi_battery *battery = acpi_driver_data(device);
  783. #ifdef CONFIG_ACPI_SYSFS_POWER
  784. struct device *old;
  785. #endif
  786. if (!battery)
  787. return;
  788. #ifdef CONFIG_ACPI_SYSFS_POWER
  789. old = battery->bat.dev;
  790. #endif
  791. acpi_battery_update(battery);
  792. acpi_bus_generate_proc_event(device, event,
  793. acpi_battery_present(battery));
  794. acpi_bus_generate_netlink_event(device->pnp.device_class,
  795. dev_name(&device->dev), event,
  796. acpi_battery_present(battery));
  797. #ifdef CONFIG_ACPI_SYSFS_POWER
  798. /* acpi_battery_update could remove power_supply object */
  799. if (old && battery->bat.dev)
  800. power_supply_changed(&battery->bat);
  801. #endif
  802. }
  803. static int acpi_battery_add(struct acpi_device *device)
  804. {
  805. int result = 0;
  806. struct acpi_battery *battery = NULL;
  807. acpi_handle handle;
  808. if (!device)
  809. return -EINVAL;
  810. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  811. if (!battery)
  812. return -ENOMEM;
  813. battery->device = device;
  814. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  815. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  816. device->driver_data = battery;
  817. mutex_init(&battery->lock);
  818. if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
  819. "_BIX", &handle)))
  820. set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
  821. acpi_battery_update(battery);
  822. #ifdef CONFIG_ACPI_PROCFS_POWER
  823. result = acpi_battery_add_fs(device);
  824. #endif
  825. if (!result) {
  826. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  827. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  828. device->status.battery_present ? "present" : "absent");
  829. } else {
  830. #ifdef CONFIG_ACPI_PROCFS_POWER
  831. acpi_battery_remove_fs(device);
  832. #endif
  833. kfree(battery);
  834. }
  835. return result;
  836. }
  837. static int acpi_battery_remove(struct acpi_device *device, int type)
  838. {
  839. struct acpi_battery *battery = NULL;
  840. if (!device || !acpi_driver_data(device))
  841. return -EINVAL;
  842. battery = acpi_driver_data(device);
  843. #ifdef CONFIG_ACPI_PROCFS_POWER
  844. acpi_battery_remove_fs(device);
  845. #endif
  846. #ifdef CONFIG_ACPI_SYSFS_POWER
  847. sysfs_remove_battery(battery);
  848. #endif
  849. mutex_destroy(&battery->lock);
  850. kfree(battery);
  851. return 0;
  852. }
  853. /* this is needed to learn about changes made in suspended state */
  854. static int acpi_battery_resume(struct acpi_device *device)
  855. {
  856. struct acpi_battery *battery;
  857. if (!device)
  858. return -EINVAL;
  859. battery = acpi_driver_data(device);
  860. battery->update_time = 0;
  861. acpi_battery_update(battery);
  862. return 0;
  863. }
  864. static struct acpi_driver acpi_battery_driver = {
  865. .name = "battery",
  866. .class = ACPI_BATTERY_CLASS,
  867. .ids = battery_device_ids,
  868. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  869. .ops = {
  870. .add = acpi_battery_add,
  871. .resume = acpi_battery_resume,
  872. .remove = acpi_battery_remove,
  873. .notify = acpi_battery_notify,
  874. },
  875. };
  876. static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
  877. {
  878. if (acpi_disabled)
  879. return;
  880. #ifdef CONFIG_ACPI_PROCFS_POWER
  881. acpi_battery_dir = acpi_lock_battery_dir();
  882. if (!acpi_battery_dir)
  883. return;
  884. #endif
  885. if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
  886. #ifdef CONFIG_ACPI_PROCFS_POWER
  887. acpi_unlock_battery_dir(acpi_battery_dir);
  888. #endif
  889. return;
  890. }
  891. return;
  892. }
  893. static int __init acpi_battery_init(void)
  894. {
  895. async_schedule(acpi_battery_init_async, NULL);
  896. return 0;
  897. }
  898. static void __exit acpi_battery_exit(void)
  899. {
  900. acpi_bus_unregister_driver(&acpi_battery_driver);
  901. #ifdef CONFIG_ACPI_PROCFS_POWER
  902. acpi_unlock_battery_dir(acpi_battery_dir);
  903. #endif
  904. }
  905. module_init(acpi_battery_init);
  906. module_exit(acpi_battery_exit);