battery.c 35 KB

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