battery.c 37 KB

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