battery.c 31 KB

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