battery.c 26 KB

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