battery.c 32 KB

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