battery.c 28 KB

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