battery.c 30 KB

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