battery.c 31 KB

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