battery.c 38 KB

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