battery.c 26 KB

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