battery.c 38 KB

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