battery.c 31 KB

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