battery.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199
  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. #include <linux/suspend.h>
  36. #include <asm/unaligned.h>
  37. #ifdef CONFIG_ACPI_PROCFS_POWER
  38. #include <linux/proc_fs.h>
  39. #include <linux/seq_file.h>
  40. #include <asm/uaccess.h>
  41. #endif
  42. #include <acpi/acpi_bus.h>
  43. #include <acpi/acpi_drivers.h>
  44. #include <linux/power_supply.h>
  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. /* Battery power unit: 0 means mW, 1 means mA */
  53. #define ACPI_BATTERY_POWER_UNIT_MA 1
  54. #define _COMPONENT ACPI_BATTERY_COMPONENT
  55. ACPI_MODULE_NAME("battery");
  56. MODULE_AUTHOR("Paul Diefenbaugh");
  57. MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
  58. MODULE_DESCRIPTION("ACPI Battery Driver");
  59. MODULE_LICENSE("GPL");
  60. static unsigned int cache_time = 1000;
  61. module_param(cache_time, uint, 0644);
  62. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  63. #ifdef CONFIG_ACPI_PROCFS_POWER
  64. extern struct proc_dir_entry *acpi_lock_battery_dir(void);
  65. extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
  66. enum acpi_battery_files {
  67. info_tag = 0,
  68. state_tag,
  69. alarm_tag,
  70. ACPI_BATTERY_NUMFILES,
  71. };
  72. #endif
  73. static const struct acpi_device_id battery_device_ids[] = {
  74. {"PNP0C0A", 0},
  75. {"", 0},
  76. };
  77. MODULE_DEVICE_TABLE(acpi, battery_device_ids);
  78. enum {
  79. ACPI_BATTERY_ALARM_PRESENT,
  80. ACPI_BATTERY_XINFO_PRESENT,
  81. ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
  82. /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
  83. switches between mWh and mAh depending on whether the system
  84. is running on battery or not. When mAh is the unit, most
  85. reported values are incorrect and need to be adjusted by
  86. 10000/design_voltage. Verified on x201, t410, t410s, and x220.
  87. Pre-2010 and 2012 models appear to always report in mWh and
  88. are thus unaffected (tested with t42, t61, t500, x200, x300,
  89. and x230). Also, in mid-2012 Lenovo issued a BIOS update for
  90. the 2011 models that fixes the issue (tested on x220 with a
  91. post-1.29 BIOS), but as of Nov. 2012, no such update is
  92. available for the 2010 models. */
  93. ACPI_BATTERY_QUIRK_THINKPAD_MAH,
  94. };
  95. struct acpi_battery {
  96. struct mutex lock;
  97. struct mutex sysfs_lock;
  98. struct power_supply bat;
  99. struct acpi_device *device;
  100. struct notifier_block pm_nb;
  101. unsigned long update_time;
  102. int rate_now;
  103. int capacity_now;
  104. int voltage_now;
  105. int design_capacity;
  106. int full_charge_capacity;
  107. int technology;
  108. int design_voltage;
  109. int design_capacity_warning;
  110. int design_capacity_low;
  111. int cycle_count;
  112. int measurement_accuracy;
  113. int max_sampling_time;
  114. int min_sampling_time;
  115. int max_averaging_interval;
  116. int min_averaging_interval;
  117. int capacity_granularity_1;
  118. int capacity_granularity_2;
  119. int alarm;
  120. char model_number[32];
  121. char serial_number[32];
  122. char type[32];
  123. char oem_info[32];
  124. int state;
  125. int power_unit;
  126. unsigned long flags;
  127. };
  128. #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
  129. inline int acpi_battery_present(struct acpi_battery *battery)
  130. {
  131. return battery->device->status.battery_present;
  132. }
  133. static int acpi_battery_technology(struct acpi_battery *battery)
  134. {
  135. if (!strcasecmp("NiCd", battery->type))
  136. return POWER_SUPPLY_TECHNOLOGY_NiCd;
  137. if (!strcasecmp("NiMH", battery->type))
  138. return POWER_SUPPLY_TECHNOLOGY_NiMH;
  139. if (!strcasecmp("LION", battery->type))
  140. return POWER_SUPPLY_TECHNOLOGY_LION;
  141. if (!strncasecmp("LI-ION", battery->type, 6))
  142. return POWER_SUPPLY_TECHNOLOGY_LION;
  143. if (!strcasecmp("LiP", battery->type))
  144. return POWER_SUPPLY_TECHNOLOGY_LIPO;
  145. return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
  146. }
  147. static int acpi_battery_get_state(struct acpi_battery *battery);
  148. static int acpi_battery_is_charged(struct acpi_battery *battery)
  149. {
  150. /* either charging or discharging */
  151. if (battery->state != 0)
  152. return 0;
  153. /* battery not reporting charge */
  154. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
  155. battery->capacity_now == 0)
  156. return 0;
  157. /* good batteries update full_charge as the batteries degrade */
  158. if (battery->full_charge_capacity == battery->capacity_now)
  159. return 1;
  160. /* fallback to using design values for broken batteries */
  161. if (battery->design_capacity == battery->capacity_now)
  162. return 1;
  163. /* we don't do any sort of metric based on percentages */
  164. return 0;
  165. }
  166. static int acpi_battery_get_property(struct power_supply *psy,
  167. enum power_supply_property psp,
  168. union power_supply_propval *val)
  169. {
  170. int ret = 0;
  171. struct acpi_battery *battery = to_acpi_battery(psy);
  172. if (acpi_battery_present(battery)) {
  173. /* run battery update only if it is present */
  174. acpi_battery_get_state(battery);
  175. } else if (psp != POWER_SUPPLY_PROP_PRESENT)
  176. return -ENODEV;
  177. switch (psp) {
  178. case POWER_SUPPLY_PROP_STATUS:
  179. if (battery->state & 0x01)
  180. val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
  181. else if (battery->state & 0x02)
  182. val->intval = POWER_SUPPLY_STATUS_CHARGING;
  183. else if (acpi_battery_is_charged(battery))
  184. val->intval = POWER_SUPPLY_STATUS_FULL;
  185. else
  186. val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
  187. break;
  188. case POWER_SUPPLY_PROP_PRESENT:
  189. val->intval = acpi_battery_present(battery);
  190. break;
  191. case POWER_SUPPLY_PROP_TECHNOLOGY:
  192. val->intval = acpi_battery_technology(battery);
  193. break;
  194. case POWER_SUPPLY_PROP_CYCLE_COUNT:
  195. val->intval = battery->cycle_count;
  196. break;
  197. case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
  198. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  199. ret = -ENODEV;
  200. else
  201. val->intval = battery->design_voltage * 1000;
  202. break;
  203. case POWER_SUPPLY_PROP_VOLTAGE_NOW:
  204. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  205. ret = -ENODEV;
  206. else
  207. val->intval = battery->voltage_now * 1000;
  208. break;
  209. case POWER_SUPPLY_PROP_CURRENT_NOW:
  210. case POWER_SUPPLY_PROP_POWER_NOW:
  211. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  212. ret = -ENODEV;
  213. else
  214. val->intval = battery->rate_now * 1000;
  215. break;
  216. case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
  217. case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
  218. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  219. ret = -ENODEV;
  220. else
  221. val->intval = battery->design_capacity * 1000;
  222. break;
  223. case POWER_SUPPLY_PROP_CHARGE_FULL:
  224. case POWER_SUPPLY_PROP_ENERGY_FULL:
  225. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  226. ret = -ENODEV;
  227. else
  228. val->intval = battery->full_charge_capacity * 1000;
  229. break;
  230. case POWER_SUPPLY_PROP_CHARGE_NOW:
  231. case POWER_SUPPLY_PROP_ENERGY_NOW:
  232. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  233. ret = -ENODEV;
  234. else
  235. val->intval = battery->capacity_now * 1000;
  236. break;
  237. case POWER_SUPPLY_PROP_CAPACITY:
  238. if (battery->capacity_now && battery->full_charge_capacity)
  239. val->intval = battery->capacity_now * 100/
  240. battery->full_charge_capacity;
  241. else
  242. val->intval = 0;
  243. break;
  244. case POWER_SUPPLY_PROP_MODEL_NAME:
  245. val->strval = battery->model_number;
  246. break;
  247. case POWER_SUPPLY_PROP_MANUFACTURER:
  248. val->strval = battery->oem_info;
  249. break;
  250. case POWER_SUPPLY_PROP_SERIAL_NUMBER:
  251. val->strval = battery->serial_number;
  252. break;
  253. default:
  254. ret = -EINVAL;
  255. }
  256. return ret;
  257. }
  258. static enum power_supply_property charge_battery_props[] = {
  259. POWER_SUPPLY_PROP_STATUS,
  260. POWER_SUPPLY_PROP_PRESENT,
  261. POWER_SUPPLY_PROP_TECHNOLOGY,
  262. POWER_SUPPLY_PROP_CYCLE_COUNT,
  263. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  264. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  265. POWER_SUPPLY_PROP_CURRENT_NOW,
  266. POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
  267. POWER_SUPPLY_PROP_CHARGE_FULL,
  268. POWER_SUPPLY_PROP_CHARGE_NOW,
  269. POWER_SUPPLY_PROP_CAPACITY,
  270. POWER_SUPPLY_PROP_MODEL_NAME,
  271. POWER_SUPPLY_PROP_MANUFACTURER,
  272. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  273. };
  274. static enum power_supply_property energy_battery_props[] = {
  275. POWER_SUPPLY_PROP_STATUS,
  276. POWER_SUPPLY_PROP_PRESENT,
  277. POWER_SUPPLY_PROP_TECHNOLOGY,
  278. POWER_SUPPLY_PROP_CYCLE_COUNT,
  279. POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
  280. POWER_SUPPLY_PROP_VOLTAGE_NOW,
  281. POWER_SUPPLY_PROP_POWER_NOW,
  282. POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
  283. POWER_SUPPLY_PROP_ENERGY_FULL,
  284. POWER_SUPPLY_PROP_ENERGY_NOW,
  285. POWER_SUPPLY_PROP_CAPACITY,
  286. POWER_SUPPLY_PROP_MODEL_NAME,
  287. POWER_SUPPLY_PROP_MANUFACTURER,
  288. POWER_SUPPLY_PROP_SERIAL_NUMBER,
  289. };
  290. #ifdef CONFIG_ACPI_PROCFS_POWER
  291. inline char *acpi_battery_units(struct acpi_battery *battery)
  292. {
  293. return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
  294. "mA" : "mW";
  295. }
  296. #endif
  297. /* --------------------------------------------------------------------------
  298. Battery Management
  299. -------------------------------------------------------------------------- */
  300. struct acpi_offsets {
  301. size_t offset; /* offset inside struct acpi_sbs_battery */
  302. u8 mode; /* int or string? */
  303. };
  304. static struct acpi_offsets state_offsets[] = {
  305. {offsetof(struct acpi_battery, state), 0},
  306. {offsetof(struct acpi_battery, rate_now), 0},
  307. {offsetof(struct acpi_battery, capacity_now), 0},
  308. {offsetof(struct acpi_battery, voltage_now), 0},
  309. };
  310. static struct acpi_offsets info_offsets[] = {
  311. {offsetof(struct acpi_battery, power_unit), 0},
  312. {offsetof(struct acpi_battery, design_capacity), 0},
  313. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  314. {offsetof(struct acpi_battery, technology), 0},
  315. {offsetof(struct acpi_battery, design_voltage), 0},
  316. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  317. {offsetof(struct acpi_battery, design_capacity_low), 0},
  318. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  319. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  320. {offsetof(struct acpi_battery, model_number), 1},
  321. {offsetof(struct acpi_battery, serial_number), 1},
  322. {offsetof(struct acpi_battery, type), 1},
  323. {offsetof(struct acpi_battery, oem_info), 1},
  324. };
  325. static struct acpi_offsets extended_info_offsets[] = {
  326. {offsetof(struct acpi_battery, power_unit), 0},
  327. {offsetof(struct acpi_battery, design_capacity), 0},
  328. {offsetof(struct acpi_battery, full_charge_capacity), 0},
  329. {offsetof(struct acpi_battery, technology), 0},
  330. {offsetof(struct acpi_battery, design_voltage), 0},
  331. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  332. {offsetof(struct acpi_battery, design_capacity_low), 0},
  333. {offsetof(struct acpi_battery, cycle_count), 0},
  334. {offsetof(struct acpi_battery, measurement_accuracy), 0},
  335. {offsetof(struct acpi_battery, max_sampling_time), 0},
  336. {offsetof(struct acpi_battery, min_sampling_time), 0},
  337. {offsetof(struct acpi_battery, max_averaging_interval), 0},
  338. {offsetof(struct acpi_battery, min_averaging_interval), 0},
  339. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  340. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  341. {offsetof(struct acpi_battery, model_number), 1},
  342. {offsetof(struct acpi_battery, serial_number), 1},
  343. {offsetof(struct acpi_battery, type), 1},
  344. {offsetof(struct acpi_battery, oem_info), 1},
  345. };
  346. static int extract_package(struct acpi_battery *battery,
  347. union acpi_object *package,
  348. struct acpi_offsets *offsets, int num)
  349. {
  350. int i;
  351. union acpi_object *element;
  352. if (package->type != ACPI_TYPE_PACKAGE)
  353. return -EFAULT;
  354. for (i = 0; i < num; ++i) {
  355. if (package->package.count <= i)
  356. return -EFAULT;
  357. element = &package->package.elements[i];
  358. if (offsets[i].mode) {
  359. u8 *ptr = (u8 *)battery + offsets[i].offset;
  360. if (element->type == ACPI_TYPE_STRING ||
  361. element->type == ACPI_TYPE_BUFFER)
  362. strncpy(ptr, element->string.pointer, 32);
  363. else if (element->type == ACPI_TYPE_INTEGER) {
  364. strncpy(ptr, (u8 *)&element->integer.value,
  365. sizeof(u64));
  366. ptr[sizeof(u64)] = 0;
  367. } else
  368. *ptr = 0; /* don't have value */
  369. } else {
  370. int *x = (int *)((u8 *)battery + offsets[i].offset);
  371. *x = (element->type == ACPI_TYPE_INTEGER) ?
  372. element->integer.value : -1;
  373. }
  374. }
  375. return 0;
  376. }
  377. static int acpi_battery_get_status(struct acpi_battery *battery)
  378. {
  379. if (acpi_bus_get_status(battery->device)) {
  380. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
  381. return -ENODEV;
  382. }
  383. return 0;
  384. }
  385. static int acpi_battery_get_info(struct acpi_battery *battery)
  386. {
  387. int result = -EFAULT;
  388. acpi_status status = 0;
  389. char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
  390. "_BIX" : "_BIF";
  391. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  392. if (!acpi_battery_present(battery))
  393. return 0;
  394. mutex_lock(&battery->lock);
  395. status = acpi_evaluate_object(battery->device->handle, name,
  396. NULL, &buffer);
  397. mutex_unlock(&battery->lock);
  398. if (ACPI_FAILURE(status)) {
  399. ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
  400. return -ENODEV;
  401. }
  402. if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
  403. result = extract_package(battery, buffer.pointer,
  404. extended_info_offsets,
  405. ARRAY_SIZE(extended_info_offsets));
  406. else
  407. result = extract_package(battery, buffer.pointer,
  408. info_offsets, ARRAY_SIZE(info_offsets));
  409. kfree(buffer.pointer);
  410. if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
  411. battery->full_charge_capacity = battery->design_capacity;
  412. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
  413. battery->power_unit && battery->design_voltage) {
  414. battery->design_capacity = battery->design_capacity *
  415. 10000 / battery->design_voltage;
  416. battery->full_charge_capacity = battery->full_charge_capacity *
  417. 10000 / battery->design_voltage;
  418. battery->design_capacity_warning =
  419. battery->design_capacity_warning *
  420. 10000 / battery->design_voltage;
  421. /* Curiously, design_capacity_low, unlike the rest of them,
  422. is correct. */
  423. /* capacity_granularity_* equal 1 on the systems tested, so
  424. it's impossible to tell if they would need an adjustment
  425. or not if their values were higher. */
  426. }
  427. return result;
  428. }
  429. static int acpi_battery_get_state(struct acpi_battery *battery)
  430. {
  431. int result = 0;
  432. acpi_status status = 0;
  433. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  434. if (!acpi_battery_present(battery))
  435. return 0;
  436. if (battery->update_time &&
  437. time_before(jiffies, battery->update_time +
  438. msecs_to_jiffies(cache_time)))
  439. return 0;
  440. mutex_lock(&battery->lock);
  441. status = acpi_evaluate_object(battery->device->handle, "_BST",
  442. NULL, &buffer);
  443. mutex_unlock(&battery->lock);
  444. if (ACPI_FAILURE(status)) {
  445. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  446. return -ENODEV;
  447. }
  448. result = extract_package(battery, buffer.pointer,
  449. state_offsets, ARRAY_SIZE(state_offsets));
  450. battery->update_time = jiffies;
  451. kfree(buffer.pointer);
  452. /* For buggy DSDTs that report negative 16-bit values for either
  453. * charging or discharging current and/or report 0 as 65536
  454. * due to bad math.
  455. */
  456. if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
  457. battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
  458. (s16)(battery->rate_now) < 0) {
  459. battery->rate_now = abs((s16)battery->rate_now);
  460. printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
  461. " invalid.\n");
  462. }
  463. if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
  464. && battery->capacity_now >= 0 && battery->capacity_now <= 100)
  465. battery->capacity_now = (battery->capacity_now *
  466. battery->full_charge_capacity) / 100;
  467. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
  468. battery->power_unit && battery->design_voltage) {
  469. battery->capacity_now = battery->capacity_now *
  470. 10000 / battery->design_voltage;
  471. }
  472. return result;
  473. }
  474. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  475. {
  476. acpi_status status = 0;
  477. union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
  478. struct acpi_object_list arg_list = { 1, &arg0 };
  479. if (!acpi_battery_present(battery) ||
  480. !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
  481. return -ENODEV;
  482. arg0.integer.value = battery->alarm;
  483. mutex_lock(&battery->lock);
  484. status = acpi_evaluate_object(battery->device->handle, "_BTP",
  485. &arg_list, NULL);
  486. mutex_unlock(&battery->lock);
  487. if (ACPI_FAILURE(status))
  488. return -ENODEV;
  489. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
  490. return 0;
  491. }
  492. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  493. {
  494. acpi_status status = AE_OK;
  495. acpi_handle handle = NULL;
  496. /* See if alarms are supported, and if so, set default */
  497. status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
  498. if (ACPI_FAILURE(status)) {
  499. clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  500. return 0;
  501. }
  502. set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
  503. if (!battery->alarm)
  504. battery->alarm = battery->design_capacity_warning;
  505. return acpi_battery_set_alarm(battery);
  506. }
  507. static ssize_t acpi_battery_alarm_show(struct device *dev,
  508. struct device_attribute *attr,
  509. char *buf)
  510. {
  511. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  512. return sprintf(buf, "%d\n", battery->alarm * 1000);
  513. }
  514. static ssize_t acpi_battery_alarm_store(struct device *dev,
  515. struct device_attribute *attr,
  516. const char *buf, size_t count)
  517. {
  518. unsigned long x;
  519. struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
  520. if (sscanf(buf, "%ld\n", &x) == 1)
  521. battery->alarm = x/1000;
  522. if (acpi_battery_present(battery))
  523. acpi_battery_set_alarm(battery);
  524. return count;
  525. }
  526. static struct device_attribute alarm_attr = {
  527. .attr = {.name = "alarm", .mode = 0644},
  528. .show = acpi_battery_alarm_show,
  529. .store = acpi_battery_alarm_store,
  530. };
  531. static int sysfs_add_battery(struct acpi_battery *battery)
  532. {
  533. int result;
  534. if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
  535. battery->bat.properties = charge_battery_props;
  536. battery->bat.num_properties =
  537. ARRAY_SIZE(charge_battery_props);
  538. } else {
  539. battery->bat.properties = energy_battery_props;
  540. battery->bat.num_properties =
  541. ARRAY_SIZE(energy_battery_props);
  542. }
  543. battery->bat.name = acpi_device_bid(battery->device);
  544. battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
  545. battery->bat.get_property = acpi_battery_get_property;
  546. result = power_supply_register(&battery->device->dev, &battery->bat);
  547. if (result)
  548. return result;
  549. return device_create_file(battery->bat.dev, &alarm_attr);
  550. }
  551. static void sysfs_remove_battery(struct acpi_battery *battery)
  552. {
  553. mutex_lock(&battery->sysfs_lock);
  554. if (!battery->bat.dev) {
  555. mutex_unlock(&battery->sysfs_lock);
  556. return;
  557. }
  558. device_remove_file(battery->bat.dev, &alarm_attr);
  559. power_supply_unregister(&battery->bat);
  560. battery->bat.dev = NULL;
  561. mutex_unlock(&battery->sysfs_lock);
  562. }
  563. static void find_battery(const struct dmi_header *dm, void *private)
  564. {
  565. struct acpi_battery *battery = (struct acpi_battery *)private;
  566. /* Note: the hardcoded offsets below have been extracted from
  567. the source code of dmidecode. */
  568. if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
  569. const u8 *dmi_data = (const u8 *)(dm + 1);
  570. int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
  571. if (dm->length >= 18)
  572. dmi_capacity *= dmi_data[17];
  573. if (battery->design_capacity * battery->design_voltage / 1000
  574. != dmi_capacity &&
  575. battery->design_capacity * 10 == dmi_capacity)
  576. set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
  577. &battery->flags);
  578. }
  579. }
  580. /*
  581. * According to the ACPI spec, some kinds of primary batteries can
  582. * report percentage battery remaining capacity directly to OS.
  583. * In this case, it reports the Last Full Charged Capacity == 100
  584. * and BatteryPresentRate == 0xFFFFFFFF.
  585. *
  586. * Now we found some battery reports percentage remaining capacity
  587. * even if it's rechargeable.
  588. * https://bugzilla.kernel.org/show_bug.cgi?id=15979
  589. *
  590. * Handle this correctly so that they won't break userspace.
  591. */
  592. static void acpi_battery_quirks(struct acpi_battery *battery)
  593. {
  594. if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
  595. return ;
  596. if (battery->full_charge_capacity == 100 &&
  597. battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
  598. battery->capacity_now >=0 && battery->capacity_now <= 100) {
  599. set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
  600. battery->full_charge_capacity = battery->design_capacity;
  601. battery->capacity_now = (battery->capacity_now *
  602. battery->full_charge_capacity) / 100;
  603. }
  604. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
  605. return ;
  606. if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
  607. const char *s;
  608. s = dmi_get_system_info(DMI_PRODUCT_VERSION);
  609. if (s && !strnicmp(s, "ThinkPad", 8)) {
  610. dmi_walk(find_battery, battery);
  611. if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
  612. &battery->flags) &&
  613. battery->design_voltage) {
  614. battery->design_capacity =
  615. battery->design_capacity *
  616. 10000 / battery->design_voltage;
  617. battery->full_charge_capacity =
  618. battery->full_charge_capacity *
  619. 10000 / battery->design_voltage;
  620. battery->design_capacity_warning =
  621. battery->design_capacity_warning *
  622. 10000 / battery->design_voltage;
  623. battery->capacity_now = battery->capacity_now *
  624. 10000 / battery->design_voltage;
  625. }
  626. }
  627. }
  628. }
  629. static int acpi_battery_update(struct acpi_battery *battery)
  630. {
  631. int result, old_present = acpi_battery_present(battery);
  632. result = acpi_battery_get_status(battery);
  633. if (result)
  634. return result;
  635. if (!acpi_battery_present(battery)) {
  636. sysfs_remove_battery(battery);
  637. battery->update_time = 0;
  638. return 0;
  639. }
  640. if (!battery->update_time ||
  641. old_present != acpi_battery_present(battery)) {
  642. result = acpi_battery_get_info(battery);
  643. if (result)
  644. return result;
  645. acpi_battery_init_alarm(battery);
  646. }
  647. if (!battery->bat.dev) {
  648. result = sysfs_add_battery(battery);
  649. if (result)
  650. return result;
  651. }
  652. result = acpi_battery_get_state(battery);
  653. acpi_battery_quirks(battery);
  654. return result;
  655. }
  656. static void acpi_battery_refresh(struct acpi_battery *battery)
  657. {
  658. int power_unit;
  659. if (!battery->bat.dev)
  660. return;
  661. power_unit = battery->power_unit;
  662. acpi_battery_get_info(battery);
  663. if (power_unit == battery->power_unit)
  664. return;
  665. /* The battery has changed its reporting units. */
  666. sysfs_remove_battery(battery);
  667. sysfs_add_battery(battery);
  668. }
  669. /* --------------------------------------------------------------------------
  670. FS Interface (/proc)
  671. -------------------------------------------------------------------------- */
  672. #ifdef CONFIG_ACPI_PROCFS_POWER
  673. static struct proc_dir_entry *acpi_battery_dir;
  674. static int acpi_battery_print_info(struct seq_file *seq, int result)
  675. {
  676. struct acpi_battery *battery = seq->private;
  677. if (result)
  678. goto end;
  679. seq_printf(seq, "present: %s\n",
  680. acpi_battery_present(battery)?"yes":"no");
  681. if (!acpi_battery_present(battery))
  682. goto end;
  683. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  684. seq_printf(seq, "design capacity: unknown\n");
  685. else
  686. seq_printf(seq, "design capacity: %d %sh\n",
  687. battery->design_capacity,
  688. acpi_battery_units(battery));
  689. if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  690. seq_printf(seq, "last full capacity: unknown\n");
  691. else
  692. seq_printf(seq, "last full capacity: %d %sh\n",
  693. battery->full_charge_capacity,
  694. acpi_battery_units(battery));
  695. seq_printf(seq, "battery technology: %srechargeable\n",
  696. (!battery->technology)?"non-":"");
  697. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  698. seq_printf(seq, "design voltage: unknown\n");
  699. else
  700. seq_printf(seq, "design voltage: %d mV\n",
  701. battery->design_voltage);
  702. seq_printf(seq, "design capacity warning: %d %sh\n",
  703. battery->design_capacity_warning,
  704. acpi_battery_units(battery));
  705. seq_printf(seq, "design capacity low: %d %sh\n",
  706. battery->design_capacity_low,
  707. acpi_battery_units(battery));
  708. seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
  709. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  710. battery->capacity_granularity_1,
  711. acpi_battery_units(battery));
  712. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  713. battery->capacity_granularity_2,
  714. acpi_battery_units(battery));
  715. seq_printf(seq, "model number: %s\n", battery->model_number);
  716. seq_printf(seq, "serial number: %s\n", battery->serial_number);
  717. seq_printf(seq, "battery type: %s\n", battery->type);
  718. seq_printf(seq, "OEM info: %s\n", battery->oem_info);
  719. end:
  720. if (result)
  721. seq_printf(seq, "ERROR: Unable to read battery info\n");
  722. return result;
  723. }
  724. static int acpi_battery_print_state(struct seq_file *seq, int result)
  725. {
  726. struct acpi_battery *battery = seq->private;
  727. if (result)
  728. goto end;
  729. seq_printf(seq, "present: %s\n",
  730. acpi_battery_present(battery)?"yes":"no");
  731. if (!acpi_battery_present(battery))
  732. goto end;
  733. seq_printf(seq, "capacity state: %s\n",
  734. (battery->state & 0x04)?"critical":"ok");
  735. if ((battery->state & 0x01) && (battery->state & 0x02))
  736. seq_printf(seq,
  737. "charging state: charging/discharging\n");
  738. else if (battery->state & 0x01)
  739. seq_printf(seq, "charging state: discharging\n");
  740. else if (battery->state & 0x02)
  741. seq_printf(seq, "charging state: charging\n");
  742. else
  743. seq_printf(seq, "charging state: charged\n");
  744. if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
  745. seq_printf(seq, "present rate: unknown\n");
  746. else
  747. seq_printf(seq, "present rate: %d %s\n",
  748. battery->rate_now, acpi_battery_units(battery));
  749. if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
  750. seq_printf(seq, "remaining capacity: unknown\n");
  751. else
  752. seq_printf(seq, "remaining capacity: %d %sh\n",
  753. battery->capacity_now, acpi_battery_units(battery));
  754. if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
  755. seq_printf(seq, "present voltage: unknown\n");
  756. else
  757. seq_printf(seq, "present voltage: %d mV\n",
  758. battery->voltage_now);
  759. end:
  760. if (result)
  761. seq_printf(seq, "ERROR: Unable to read battery state\n");
  762. return result;
  763. }
  764. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  765. {
  766. struct acpi_battery *battery = seq->private;
  767. if (result)
  768. goto end;
  769. if (!acpi_battery_present(battery)) {
  770. seq_printf(seq, "present: no\n");
  771. goto end;
  772. }
  773. seq_printf(seq, "alarm: ");
  774. if (!battery->alarm)
  775. seq_printf(seq, "unsupported\n");
  776. else
  777. seq_printf(seq, "%u %sh\n", battery->alarm,
  778. acpi_battery_units(battery));
  779. end:
  780. if (result)
  781. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  782. return result;
  783. }
  784. static ssize_t acpi_battery_write_alarm(struct file *file,
  785. const char __user * buffer,
  786. size_t count, loff_t * ppos)
  787. {
  788. int result = 0;
  789. char alarm_string[12] = { '\0' };
  790. struct seq_file *m = file->private_data;
  791. struct acpi_battery *battery = m->private;
  792. if (!battery || (count > sizeof(alarm_string) - 1))
  793. return -EINVAL;
  794. if (!acpi_battery_present(battery)) {
  795. result = -ENODEV;
  796. goto end;
  797. }
  798. if (copy_from_user(alarm_string, buffer, count)) {
  799. result = -EFAULT;
  800. goto end;
  801. }
  802. alarm_string[count] = '\0';
  803. battery->alarm = simple_strtol(alarm_string, NULL, 0);
  804. result = acpi_battery_set_alarm(battery);
  805. end:
  806. if (!result)
  807. return count;
  808. return result;
  809. }
  810. typedef int(*print_func)(struct seq_file *seq, int result);
  811. static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
  812. acpi_battery_print_info,
  813. acpi_battery_print_state,
  814. acpi_battery_print_alarm,
  815. };
  816. static int acpi_battery_read(int fid, struct seq_file *seq)
  817. {
  818. struct acpi_battery *battery = seq->private;
  819. int result = acpi_battery_update(battery);
  820. return acpi_print_funcs[fid](seq, result);
  821. }
  822. #define DECLARE_FILE_FUNCTIONS(_name) \
  823. static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
  824. { \
  825. return acpi_battery_read(_name##_tag, seq); \
  826. } \
  827. static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
  828. { \
  829. return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
  830. }
  831. DECLARE_FILE_FUNCTIONS(info);
  832. DECLARE_FILE_FUNCTIONS(state);
  833. DECLARE_FILE_FUNCTIONS(alarm);
  834. #undef DECLARE_FILE_FUNCTIONS
  835. #define FILE_DESCRIPTION_RO(_name) \
  836. { \
  837. .name = __stringify(_name), \
  838. .mode = S_IRUGO, \
  839. .ops = { \
  840. .open = acpi_battery_##_name##_open_fs, \
  841. .read = seq_read, \
  842. .llseek = seq_lseek, \
  843. .release = single_release, \
  844. .owner = THIS_MODULE, \
  845. }, \
  846. }
  847. #define FILE_DESCRIPTION_RW(_name) \
  848. { \
  849. .name = __stringify(_name), \
  850. .mode = S_IFREG | S_IRUGO | S_IWUSR, \
  851. .ops = { \
  852. .open = acpi_battery_##_name##_open_fs, \
  853. .read = seq_read, \
  854. .llseek = seq_lseek, \
  855. .write = acpi_battery_write_##_name, \
  856. .release = single_release, \
  857. .owner = THIS_MODULE, \
  858. }, \
  859. }
  860. static const struct battery_file {
  861. struct file_operations ops;
  862. umode_t mode;
  863. const char *name;
  864. } acpi_battery_file[] = {
  865. FILE_DESCRIPTION_RO(info),
  866. FILE_DESCRIPTION_RO(state),
  867. FILE_DESCRIPTION_RW(alarm),
  868. };
  869. #undef FILE_DESCRIPTION_RO
  870. #undef FILE_DESCRIPTION_RW
  871. static int acpi_battery_add_fs(struct acpi_device *device)
  872. {
  873. struct proc_dir_entry *entry = NULL;
  874. int i;
  875. printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
  876. " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
  877. if (!acpi_device_dir(device)) {
  878. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  879. acpi_battery_dir);
  880. if (!acpi_device_dir(device))
  881. return -ENODEV;
  882. }
  883. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  884. entry = proc_create_data(acpi_battery_file[i].name,
  885. acpi_battery_file[i].mode,
  886. acpi_device_dir(device),
  887. &acpi_battery_file[i].ops,
  888. acpi_driver_data(device));
  889. if (!entry)
  890. return -ENODEV;
  891. }
  892. return 0;
  893. }
  894. static void acpi_battery_remove_fs(struct acpi_device *device)
  895. {
  896. int i;
  897. if (!acpi_device_dir(device))
  898. return;
  899. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
  900. remove_proc_entry(acpi_battery_file[i].name,
  901. acpi_device_dir(device));
  902. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  903. acpi_device_dir(device) = NULL;
  904. }
  905. #endif
  906. /* --------------------------------------------------------------------------
  907. Driver Interface
  908. -------------------------------------------------------------------------- */
  909. static void acpi_battery_notify(struct acpi_device *device, u32 event)
  910. {
  911. struct acpi_battery *battery = acpi_driver_data(device);
  912. struct device *old;
  913. if (!battery)
  914. return;
  915. old = battery->bat.dev;
  916. if (event == ACPI_BATTERY_NOTIFY_INFO)
  917. acpi_battery_refresh(battery);
  918. acpi_battery_update(battery);
  919. acpi_bus_generate_proc_event(device, event,
  920. acpi_battery_present(battery));
  921. acpi_bus_generate_netlink_event(device->pnp.device_class,
  922. dev_name(&device->dev), event,
  923. acpi_battery_present(battery));
  924. /* acpi_battery_update could remove power_supply object */
  925. if (old && battery->bat.dev)
  926. power_supply_changed(&battery->bat);
  927. }
  928. static int battery_notify(struct notifier_block *nb,
  929. unsigned long mode, void *_unused)
  930. {
  931. struct acpi_battery *battery = container_of(nb, struct acpi_battery,
  932. pm_nb);
  933. switch (mode) {
  934. case PM_POST_HIBERNATION:
  935. case PM_POST_SUSPEND:
  936. if (battery->bat.dev) {
  937. sysfs_remove_battery(battery);
  938. sysfs_add_battery(battery);
  939. }
  940. break;
  941. }
  942. return 0;
  943. }
  944. static int acpi_battery_add(struct acpi_device *device)
  945. {
  946. int result = 0;
  947. struct acpi_battery *battery = NULL;
  948. acpi_handle handle;
  949. if (!device)
  950. return -EINVAL;
  951. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  952. if (!battery)
  953. return -ENOMEM;
  954. battery->device = device;
  955. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  956. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  957. device->driver_data = battery;
  958. mutex_init(&battery->lock);
  959. mutex_init(&battery->sysfs_lock);
  960. if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
  961. "_BIX", &handle)))
  962. set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
  963. result = acpi_battery_update(battery);
  964. if (result)
  965. goto fail;
  966. #ifdef CONFIG_ACPI_PROCFS_POWER
  967. result = acpi_battery_add_fs(device);
  968. #endif
  969. if (result) {
  970. #ifdef CONFIG_ACPI_PROCFS_POWER
  971. acpi_battery_remove_fs(device);
  972. #endif
  973. goto fail;
  974. }
  975. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  976. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  977. device->status.battery_present ? "present" : "absent");
  978. battery->pm_nb.notifier_call = battery_notify;
  979. register_pm_notifier(&battery->pm_nb);
  980. return result;
  981. fail:
  982. sysfs_remove_battery(battery);
  983. mutex_destroy(&battery->lock);
  984. mutex_destroy(&battery->sysfs_lock);
  985. kfree(battery);
  986. return result;
  987. }
  988. static int acpi_battery_remove(struct acpi_device *device, int type)
  989. {
  990. struct acpi_battery *battery = NULL;
  991. if (!device || !acpi_driver_data(device))
  992. return -EINVAL;
  993. battery = acpi_driver_data(device);
  994. unregister_pm_notifier(&battery->pm_nb);
  995. #ifdef CONFIG_ACPI_PROCFS_POWER
  996. acpi_battery_remove_fs(device);
  997. #endif
  998. sysfs_remove_battery(battery);
  999. mutex_destroy(&battery->lock);
  1000. mutex_destroy(&battery->sysfs_lock);
  1001. kfree(battery);
  1002. return 0;
  1003. }
  1004. #ifdef CONFIG_PM_SLEEP
  1005. /* this is needed to learn about changes made in suspended state */
  1006. static int acpi_battery_resume(struct device *dev)
  1007. {
  1008. struct acpi_battery *battery;
  1009. if (!dev)
  1010. return -EINVAL;
  1011. battery = acpi_driver_data(to_acpi_device(dev));
  1012. if (!battery)
  1013. return -EINVAL;
  1014. battery->update_time = 0;
  1015. acpi_battery_update(battery);
  1016. return 0;
  1017. }
  1018. #endif
  1019. static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
  1020. static struct acpi_driver acpi_battery_driver = {
  1021. .name = "battery",
  1022. .class = ACPI_BATTERY_CLASS,
  1023. .ids = battery_device_ids,
  1024. .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
  1025. .ops = {
  1026. .add = acpi_battery_add,
  1027. .remove = acpi_battery_remove,
  1028. .notify = acpi_battery_notify,
  1029. },
  1030. .drv.pm = &acpi_battery_pm,
  1031. };
  1032. static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
  1033. {
  1034. if (acpi_disabled)
  1035. return;
  1036. #ifdef CONFIG_ACPI_PROCFS_POWER
  1037. acpi_battery_dir = acpi_lock_battery_dir();
  1038. if (!acpi_battery_dir)
  1039. return;
  1040. #endif
  1041. if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
  1042. #ifdef CONFIG_ACPI_PROCFS_POWER
  1043. acpi_unlock_battery_dir(acpi_battery_dir);
  1044. #endif
  1045. return;
  1046. }
  1047. return;
  1048. }
  1049. static int __init acpi_battery_init(void)
  1050. {
  1051. async_schedule(acpi_battery_init_async, NULL);
  1052. return 0;
  1053. }
  1054. static void __exit acpi_battery_exit(void)
  1055. {
  1056. acpi_bus_unregister_driver(&acpi_battery_driver);
  1057. #ifdef CONFIG_ACPI_PROCFS_POWER
  1058. acpi_unlock_battery_dir(acpi_battery_dir);
  1059. #endif
  1060. }
  1061. module_init(acpi_battery_init);
  1062. module_exit(acpi_battery_exit);