battery.c 38 KB

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