battery.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684
  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/proc_fs.h>
  33. #include <linux/seq_file.h>
  34. #include <asm/uaccess.h>
  35. #include <acpi/acpi_bus.h>
  36. #include <acpi/acpi_drivers.h>
  37. #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
  38. #define ACPI_BATTERY_COMPONENT 0x00040000
  39. #define ACPI_BATTERY_CLASS "battery"
  40. #define ACPI_BATTERY_DEVICE_NAME "Battery"
  41. #define ACPI_BATTERY_NOTIFY_STATUS 0x80
  42. #define ACPI_BATTERY_NOTIFY_INFO 0x81
  43. #define _COMPONENT ACPI_BATTERY_COMPONENT
  44. ACPI_MODULE_NAME("battery");
  45. MODULE_AUTHOR("Paul Diefenbaugh");
  46. MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
  47. MODULE_DESCRIPTION("ACPI Battery Driver");
  48. MODULE_LICENSE("GPL");
  49. static unsigned int cache_time = 1000;
  50. module_param(cache_time, uint, 0644);
  51. MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
  52. extern struct proc_dir_entry *acpi_lock_battery_dir(void);
  53. extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
  54. static const struct acpi_device_id battery_device_ids[] = {
  55. {"PNP0C0A", 0},
  56. {"", 0},
  57. };
  58. MODULE_DEVICE_TABLE(acpi, battery_device_ids);
  59. enum acpi_battery_files {
  60. info_tag = 0,
  61. state_tag,
  62. alarm_tag,
  63. ACPI_BATTERY_NUMFILES,
  64. };
  65. struct acpi_battery {
  66. struct mutex lock;
  67. struct acpi_device *device;
  68. unsigned long update_time;
  69. int present_rate;
  70. int remaining_capacity;
  71. int present_voltage;
  72. int design_capacity;
  73. int last_full_capacity;
  74. int technology;
  75. int design_voltage;
  76. int design_capacity_warning;
  77. int design_capacity_low;
  78. int capacity_granularity_1;
  79. int capacity_granularity_2;
  80. int alarm;
  81. char model_number[32];
  82. char serial_number[32];
  83. char type[32];
  84. char oem_info[32];
  85. int state;
  86. int power_unit;
  87. u8 alarm_present;
  88. };
  89. inline int acpi_battery_present(struct acpi_battery *battery)
  90. {
  91. return battery->device->status.battery_present;
  92. }
  93. inline char *acpi_battery_units(struct acpi_battery *battery)
  94. {
  95. return (battery->power_unit)?"mA":"mW";
  96. }
  97. /* --------------------------------------------------------------------------
  98. Battery Management
  99. -------------------------------------------------------------------------- */
  100. struct acpi_offsets {
  101. size_t offset; /* offset inside struct acpi_sbs_battery */
  102. u8 mode; /* int or string? */
  103. };
  104. static struct acpi_offsets state_offsets[] = {
  105. {offsetof(struct acpi_battery, state), 0},
  106. {offsetof(struct acpi_battery, present_rate), 0},
  107. {offsetof(struct acpi_battery, remaining_capacity), 0},
  108. {offsetof(struct acpi_battery, present_voltage), 0},
  109. };
  110. static struct acpi_offsets info_offsets[] = {
  111. {offsetof(struct acpi_battery, power_unit), 0},
  112. {offsetof(struct acpi_battery, design_capacity), 0},
  113. {offsetof(struct acpi_battery, last_full_capacity), 0},
  114. {offsetof(struct acpi_battery, technology), 0},
  115. {offsetof(struct acpi_battery, design_voltage), 0},
  116. {offsetof(struct acpi_battery, design_capacity_warning), 0},
  117. {offsetof(struct acpi_battery, design_capacity_low), 0},
  118. {offsetof(struct acpi_battery, capacity_granularity_1), 0},
  119. {offsetof(struct acpi_battery, capacity_granularity_2), 0},
  120. {offsetof(struct acpi_battery, model_number), 1},
  121. {offsetof(struct acpi_battery, serial_number), 1},
  122. {offsetof(struct acpi_battery, type), 1},
  123. {offsetof(struct acpi_battery, oem_info), 1},
  124. };
  125. static int extract_package(struct acpi_battery *battery,
  126. union acpi_object *package,
  127. struct acpi_offsets *offsets, int num)
  128. {
  129. int i, *x;
  130. union acpi_object *element;
  131. if (package->type != ACPI_TYPE_PACKAGE)
  132. return -EFAULT;
  133. for (i = 0; i < num; ++i) {
  134. if (package->package.count <= i)
  135. return -EFAULT;
  136. element = &package->package.elements[i];
  137. if (offsets[i].mode) {
  138. if (element->type != ACPI_TYPE_STRING &&
  139. element->type != ACPI_TYPE_BUFFER)
  140. return -EFAULT;
  141. strncpy((u8 *)battery + offsets[i].offset,
  142. element->string.pointer, 32);
  143. } else {
  144. if (element->type != ACPI_TYPE_INTEGER)
  145. return -EFAULT;
  146. x = (int *)((u8 *)battery + offsets[i].offset);
  147. *x = element->integer.value;
  148. }
  149. }
  150. return 0;
  151. }
  152. static int acpi_battery_get_status(struct acpi_battery *battery)
  153. {
  154. if (acpi_bus_get_status(battery->device)) {
  155. ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
  156. return -ENODEV;
  157. }
  158. return 0;
  159. }
  160. static int acpi_battery_get_info(struct acpi_battery *battery)
  161. {
  162. int result = -EFAULT;
  163. acpi_status status = 0;
  164. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  165. if (!acpi_battery_present(battery))
  166. return 0;
  167. mutex_lock(&battery->lock);
  168. status = acpi_evaluate_object(battery->device->handle, "_BIF",
  169. NULL, &buffer);
  170. mutex_unlock(&battery->lock);
  171. if (ACPI_FAILURE(status)) {
  172. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
  173. return -ENODEV;
  174. }
  175. result = extract_package(battery, buffer.pointer,
  176. info_offsets, ARRAY_SIZE(info_offsets));
  177. kfree(buffer.pointer);
  178. return result;
  179. }
  180. static int acpi_battery_get_state(struct acpi_battery *battery)
  181. {
  182. int result = 0;
  183. acpi_status status = 0;
  184. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  185. if (!acpi_battery_present(battery))
  186. return 0;
  187. if (battery->update_time &&
  188. time_before(jiffies, battery->update_time +
  189. msecs_to_jiffies(cache_time)))
  190. return 0;
  191. mutex_lock(&battery->lock);
  192. status = acpi_evaluate_object(battery->device->handle, "_BST",
  193. NULL, &buffer);
  194. mutex_unlock(&battery->lock);
  195. if (ACPI_FAILURE(status)) {
  196. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  197. return -ENODEV;
  198. }
  199. result = extract_package(battery, buffer.pointer,
  200. state_offsets, ARRAY_SIZE(state_offsets));
  201. battery->update_time = jiffies;
  202. kfree(buffer.pointer);
  203. return result;
  204. }
  205. static int acpi_battery_set_alarm(struct acpi_battery *battery)
  206. {
  207. acpi_status status = 0;
  208. union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
  209. struct acpi_object_list arg_list = { 1, &arg0 };
  210. if (!acpi_battery_present(battery)|| !battery->alarm_present)
  211. return -ENODEV;
  212. arg0.integer.value = battery->alarm;
  213. mutex_lock(&battery->lock);
  214. status = acpi_evaluate_object(battery->device->handle, "_BTP",
  215. &arg_list, NULL);
  216. mutex_unlock(&battery->lock);
  217. if (ACPI_FAILURE(status))
  218. return -ENODEV;
  219. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
  220. return 0;
  221. }
  222. static int acpi_battery_init_alarm(struct acpi_battery *battery)
  223. {
  224. acpi_status status = AE_OK;
  225. acpi_handle handle = NULL;
  226. /* See if alarms are supported, and if so, set default */
  227. status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
  228. if (ACPI_FAILURE(status)) {
  229. battery->alarm_present = 0;
  230. return 0;
  231. }
  232. battery->alarm_present = 1;
  233. if (!battery->alarm)
  234. battery->alarm = battery->design_capacity_warning;
  235. return acpi_battery_set_alarm(battery);
  236. }
  237. static int acpi_battery_update(struct acpi_battery *battery)
  238. {
  239. int saved_present = acpi_battery_present(battery);
  240. int result = acpi_battery_get_status(battery);
  241. if (result || !acpi_battery_present(battery))
  242. return result;
  243. if (saved_present != acpi_battery_present(battery) ||
  244. !battery->update_time) {
  245. battery->update_time = 0;
  246. result = acpi_battery_get_info(battery);
  247. if (result)
  248. return result;
  249. acpi_battery_init_alarm(battery);
  250. }
  251. return acpi_battery_get_state(battery);
  252. }
  253. /* --------------------------------------------------------------------------
  254. FS Interface (/proc)
  255. -------------------------------------------------------------------------- */
  256. static struct proc_dir_entry *acpi_battery_dir;
  257. static int acpi_battery_print_info(struct seq_file *seq, int result)
  258. {
  259. struct acpi_battery *battery = seq->private;
  260. if (result)
  261. goto end;
  262. seq_printf(seq, "present: %s\n",
  263. acpi_battery_present(battery)?"yes":"no");
  264. if (!acpi_battery_present(battery))
  265. goto end;
  266. if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  267. seq_printf(seq, "design capacity: unknown\n");
  268. else
  269. seq_printf(seq, "design capacity: %d %sh\n",
  270. battery->design_capacity,
  271. acpi_battery_units(battery));
  272. if (battery->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  273. seq_printf(seq, "last full capacity: unknown\n");
  274. else
  275. seq_printf(seq, "last full capacity: %d %sh\n",
  276. battery->last_full_capacity,
  277. acpi_battery_units(battery));
  278. seq_printf(seq, "battery technology: %srechargeable\n",
  279. (!battery->technology)?"non-":"");
  280. if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  281. seq_printf(seq, "design voltage: unknown\n");
  282. else
  283. seq_printf(seq, "design voltage: %d mV\n",
  284. battery->design_voltage);
  285. seq_printf(seq, "design capacity warning: %d %sh\n",
  286. battery->design_capacity_warning,
  287. acpi_battery_units(battery));
  288. seq_printf(seq, "design capacity low: %d %sh\n",
  289. battery->design_capacity_low,
  290. acpi_battery_units(battery));
  291. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  292. battery->capacity_granularity_1,
  293. acpi_battery_units(battery));
  294. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  295. battery->capacity_granularity_2,
  296. acpi_battery_units(battery));
  297. seq_printf(seq, "model number: %s\n", battery->model_number);
  298. seq_printf(seq, "serial number: %s\n", battery->serial_number);
  299. seq_printf(seq, "battery type: %s\n", battery->type);
  300. seq_printf(seq, "OEM info: %s\n", battery->oem_info);
  301. end:
  302. if (result)
  303. seq_printf(seq, "ERROR: Unable to read battery info\n");
  304. return result;
  305. }
  306. static int acpi_battery_print_state(struct seq_file *seq, int result)
  307. {
  308. struct acpi_battery *battery = seq->private;
  309. if (result)
  310. goto end;
  311. seq_printf(seq, "present: %s\n",
  312. acpi_battery_present(battery)?"yes":"no");
  313. if (!acpi_battery_present(battery))
  314. goto end;
  315. seq_printf(seq, "capacity state: %s\n",
  316. (battery->state & 0x04)?"critical":"ok");
  317. if ((battery->state & 0x01) && (battery->state & 0x02))
  318. seq_printf(seq,
  319. "charging state: charging/discharging\n");
  320. else if (battery->state & 0x01)
  321. seq_printf(seq, "charging state: discharging\n");
  322. else if (battery->state & 0x02)
  323. seq_printf(seq, "charging state: charging\n");
  324. else
  325. seq_printf(seq, "charging state: charged\n");
  326. if (battery->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
  327. seq_printf(seq, "present rate: unknown\n");
  328. else
  329. seq_printf(seq, "present rate: %d %s\n",
  330. battery->present_rate, acpi_battery_units(battery));
  331. if (battery->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  332. seq_printf(seq, "remaining capacity: unknown\n");
  333. else
  334. seq_printf(seq, "remaining capacity: %d %sh\n",
  335. battery->remaining_capacity, acpi_battery_units(battery));
  336. if (battery->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  337. seq_printf(seq, "present voltage: unknown\n");
  338. else
  339. seq_printf(seq, "present voltage: %d mV\n",
  340. battery->present_voltage);
  341. end:
  342. if (result)
  343. seq_printf(seq, "ERROR: Unable to read battery state\n");
  344. return result;
  345. }
  346. static int acpi_battery_print_alarm(struct seq_file *seq, int result)
  347. {
  348. struct acpi_battery *battery = seq->private;
  349. if (result)
  350. goto end;
  351. if (!acpi_battery_present(battery)) {
  352. seq_printf(seq, "present: no\n");
  353. goto end;
  354. }
  355. seq_printf(seq, "alarm: ");
  356. if (!battery->alarm)
  357. seq_printf(seq, "unsupported\n");
  358. else
  359. seq_printf(seq, "%u %sh\n", battery->alarm,
  360. acpi_battery_units(battery));
  361. end:
  362. if (result)
  363. seq_printf(seq, "ERROR: Unable to read battery alarm\n");
  364. return result;
  365. }
  366. static ssize_t acpi_battery_write_alarm(struct file *file,
  367. const char __user * buffer,
  368. size_t count, loff_t * ppos)
  369. {
  370. int result = 0;
  371. char alarm_string[12] = { '\0' };
  372. struct seq_file *m = file->private_data;
  373. struct acpi_battery *battery = m->private;
  374. if (!battery || (count > sizeof(alarm_string) - 1))
  375. return -EINVAL;
  376. if (result) {
  377. result = -ENODEV;
  378. goto end;
  379. }
  380. if (!acpi_battery_present(battery)) {
  381. result = -ENODEV;
  382. goto end;
  383. }
  384. if (copy_from_user(alarm_string, buffer, count)) {
  385. result = -EFAULT;
  386. goto end;
  387. }
  388. alarm_string[count] = '\0';
  389. battery->alarm = simple_strtol(alarm_string, NULL, 0);
  390. result = acpi_battery_set_alarm(battery);
  391. end:
  392. if (!result)
  393. return count;
  394. return result;
  395. }
  396. typedef int(*print_func)(struct seq_file *seq, int result);
  397. static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
  398. acpi_battery_print_info,
  399. acpi_battery_print_state,
  400. acpi_battery_print_alarm,
  401. };
  402. static int acpi_battery_read(int fid, struct seq_file *seq)
  403. {
  404. struct acpi_battery *battery = seq->private;
  405. int result = acpi_battery_update(battery);
  406. return acpi_print_funcs[fid](seq, result);
  407. }
  408. #define DECLARE_FILE_FUNCTIONS(_name) \
  409. static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
  410. { \
  411. return acpi_battery_read(_name##_tag, seq); \
  412. } \
  413. static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
  414. { \
  415. return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
  416. }
  417. DECLARE_FILE_FUNCTIONS(info);
  418. DECLARE_FILE_FUNCTIONS(state);
  419. DECLARE_FILE_FUNCTIONS(alarm);
  420. #undef DECLARE_FILE_FUNCTIONS
  421. #define FILE_DESCRIPTION_RO(_name) \
  422. { \
  423. .name = __stringify(_name), \
  424. .mode = S_IRUGO, \
  425. .ops = { \
  426. .open = acpi_battery_##_name##_open_fs, \
  427. .read = seq_read, \
  428. .llseek = seq_lseek, \
  429. .release = single_release, \
  430. .owner = THIS_MODULE, \
  431. }, \
  432. }
  433. #define FILE_DESCRIPTION_RW(_name) \
  434. { \
  435. .name = __stringify(_name), \
  436. .mode = S_IFREG | S_IRUGO | S_IWUSR, \
  437. .ops = { \
  438. .open = acpi_battery_##_name##_open_fs, \
  439. .read = seq_read, \
  440. .llseek = seq_lseek, \
  441. .write = acpi_battery_write_##_name, \
  442. .release = single_release, \
  443. .owner = THIS_MODULE, \
  444. }, \
  445. }
  446. static struct battery_file {
  447. struct file_operations ops;
  448. mode_t mode;
  449. char *name;
  450. } acpi_battery_file[] = {
  451. FILE_DESCRIPTION_RO(info),
  452. FILE_DESCRIPTION_RO(state),
  453. FILE_DESCRIPTION_RW(alarm),
  454. };
  455. #undef FILE_DESCRIPTION_RO
  456. #undef FILE_DESCRIPTION_RW
  457. static int acpi_battery_add_fs(struct acpi_device *device)
  458. {
  459. struct proc_dir_entry *entry = NULL;
  460. int i;
  461. if (!acpi_device_dir(device)) {
  462. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  463. acpi_battery_dir);
  464. if (!acpi_device_dir(device))
  465. return -ENODEV;
  466. acpi_device_dir(device)->owner = THIS_MODULE;
  467. }
  468. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
  469. entry = create_proc_entry(acpi_battery_file[i].name,
  470. acpi_battery_file[i].mode, acpi_device_dir(device));
  471. if (!entry)
  472. return -ENODEV;
  473. else {
  474. entry->proc_fops = &acpi_battery_file[i].ops;
  475. entry->data = acpi_driver_data(device);
  476. entry->owner = THIS_MODULE;
  477. }
  478. }
  479. return 0;
  480. }
  481. static void acpi_battery_remove_fs(struct acpi_device *device)
  482. {
  483. int i;
  484. if (!acpi_device_dir(device))
  485. return;
  486. for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
  487. remove_proc_entry(acpi_battery_file[i].name,
  488. acpi_device_dir(device));
  489. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  490. acpi_device_dir(device) = NULL;
  491. }
  492. /* --------------------------------------------------------------------------
  493. Driver Interface
  494. -------------------------------------------------------------------------- */
  495. static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
  496. {
  497. struct acpi_battery *battery = data;
  498. struct acpi_device *device;
  499. if (!battery)
  500. return;
  501. device = battery->device;
  502. acpi_battery_update(battery);
  503. acpi_bus_generate_proc_event(device, event,
  504. acpi_battery_present(battery));
  505. acpi_bus_generate_netlink_event(device->pnp.device_class,
  506. device->dev.bus_id, event,
  507. acpi_battery_present(battery));
  508. }
  509. static int acpi_battery_add(struct acpi_device *device)
  510. {
  511. int result = 0;
  512. acpi_status status = 0;
  513. struct acpi_battery *battery = NULL;
  514. if (!device)
  515. return -EINVAL;
  516. battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  517. if (!battery)
  518. return -ENOMEM;
  519. battery->device = device;
  520. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  521. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  522. acpi_driver_data(device) = battery;
  523. mutex_init(&battery->lock);
  524. acpi_battery_update(battery);
  525. result = acpi_battery_add_fs(device);
  526. if (result)
  527. goto end;
  528. status = acpi_install_notify_handler(device->handle,
  529. ACPI_ALL_NOTIFY,
  530. acpi_battery_notify, battery);
  531. if (ACPI_FAILURE(status)) {
  532. ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
  533. result = -ENODEV;
  534. goto end;
  535. }
  536. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  537. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  538. device->status.battery_present ? "present" : "absent");
  539. end:
  540. if (result) {
  541. acpi_battery_remove_fs(device);
  542. kfree(battery);
  543. }
  544. return result;
  545. }
  546. static int acpi_battery_remove(struct acpi_device *device, int type)
  547. {
  548. acpi_status status = 0;
  549. struct acpi_battery *battery = NULL;
  550. if (!device || !acpi_driver_data(device))
  551. return -EINVAL;
  552. battery = acpi_driver_data(device);
  553. status = acpi_remove_notify_handler(device->handle,
  554. ACPI_ALL_NOTIFY,
  555. acpi_battery_notify);
  556. acpi_battery_remove_fs(device);
  557. mutex_destroy(&battery->lock);
  558. kfree(battery);
  559. return 0;
  560. }
  561. /* this is needed to learn about changes made in suspended state */
  562. static int acpi_battery_resume(struct acpi_device *device)
  563. {
  564. struct acpi_battery *battery;
  565. if (!device)
  566. return -EINVAL;
  567. battery = acpi_driver_data(device);
  568. battery->update_time = 0;
  569. return 0;
  570. }
  571. static struct acpi_driver acpi_battery_driver = {
  572. .name = "battery",
  573. .class = ACPI_BATTERY_CLASS,
  574. .ids = battery_device_ids,
  575. .ops = {
  576. .add = acpi_battery_add,
  577. .resume = acpi_battery_resume,
  578. .remove = acpi_battery_remove,
  579. },
  580. };
  581. static int __init acpi_battery_init(void)
  582. {
  583. if (acpi_disabled)
  584. return -ENODEV;
  585. acpi_battery_dir = acpi_lock_battery_dir();
  586. if (!acpi_battery_dir)
  587. return -ENODEV;
  588. if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
  589. acpi_unlock_battery_dir(acpi_battery_dir);
  590. return -ENODEV;
  591. }
  592. return 0;
  593. }
  594. static void __exit acpi_battery_exit(void)
  595. {
  596. acpi_bus_unregister_driver(&acpi_battery_driver);
  597. acpi_unlock_battery_dir(acpi_battery_dir);
  598. }
  599. module_init(acpi_battery_init);
  600. module_exit(acpi_battery_exit);