battery.c 31 KB

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