battery.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781
  1. /*
  2. * acpi_battery.c - ACPI Battery Driver ($Revision: 37 $)
  3. *
  4. * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
  5. * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
  6. *
  7. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 2 of the License, or (at
  12. * your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but
  15. * WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17. * General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along
  20. * with this program; if not, write to the Free Software Foundation, Inc.,
  21. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  22. *
  23. * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  24. */
  25. #include <linux/kernel.h>
  26. #include <linux/module.h>
  27. #include <linux/init.h>
  28. #include <linux/types.h>
  29. #include <linux/proc_fs.h>
  30. #include <linux/seq_file.h>
  31. #include <asm/uaccess.h>
  32. #include <acpi/acpi_bus.h>
  33. #include <acpi/acpi_drivers.h>
  34. #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
  35. #define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS"
  36. #define ACPI_BATTERY_FORMAT_BST "NNNN"
  37. #define ACPI_BATTERY_COMPONENT 0x00040000
  38. #define ACPI_BATTERY_CLASS "battery"
  39. #define ACPI_BATTERY_HID "PNP0C0A"
  40. #define ACPI_BATTERY_DRIVER_NAME "ACPI Battery Driver"
  41. #define ACPI_BATTERY_DEVICE_NAME "Battery"
  42. #define ACPI_BATTERY_FILE_INFO "info"
  43. #define ACPI_BATTERY_FILE_STATUS "state"
  44. #define ACPI_BATTERY_FILE_ALARM "alarm"
  45. #define ACPI_BATTERY_NOTIFY_STATUS 0x80
  46. #define ACPI_BATTERY_NOTIFY_INFO 0x81
  47. #define ACPI_BATTERY_UNITS_WATTS "mW"
  48. #define ACPI_BATTERY_UNITS_AMPS "mA"
  49. #define _COMPONENT ACPI_BATTERY_COMPONENT
  50. ACPI_MODULE_NAME("acpi_battery")
  51. MODULE_AUTHOR("Paul Diefenbaugh");
  52. MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME);
  53. MODULE_LICENSE("GPL");
  54. static int acpi_battery_add(struct acpi_device *device);
  55. static int acpi_battery_remove(struct acpi_device *device, int type);
  56. static struct acpi_driver acpi_battery_driver = {
  57. .name = ACPI_BATTERY_DRIVER_NAME,
  58. .class = ACPI_BATTERY_CLASS,
  59. .ids = ACPI_BATTERY_HID,
  60. .ops = {
  61. .add = acpi_battery_add,
  62. .remove = acpi_battery_remove,
  63. },
  64. };
  65. struct acpi_battery_status {
  66. acpi_integer state;
  67. acpi_integer present_rate;
  68. acpi_integer remaining_capacity;
  69. acpi_integer present_voltage;
  70. };
  71. struct acpi_battery_info {
  72. acpi_integer power_unit;
  73. acpi_integer design_capacity;
  74. acpi_integer last_full_capacity;
  75. acpi_integer battery_technology;
  76. acpi_integer design_voltage;
  77. acpi_integer design_capacity_warning;
  78. acpi_integer design_capacity_low;
  79. acpi_integer battery_capacity_granularity_1;
  80. acpi_integer battery_capacity_granularity_2;
  81. acpi_string model_number;
  82. acpi_string serial_number;
  83. acpi_string battery_type;
  84. acpi_string oem_info;
  85. };
  86. struct acpi_battery_flags {
  87. u8 present:1; /* Bay occupied? */
  88. u8 power_unit:1; /* 0=watts, 1=apms */
  89. u8 alarm:1; /* _BTP present? */
  90. u8 reserved:5;
  91. };
  92. struct acpi_battery_trips {
  93. unsigned long warning;
  94. unsigned long low;
  95. };
  96. struct acpi_battery {
  97. struct acpi_device * device;
  98. struct acpi_battery_flags flags;
  99. struct acpi_battery_trips trips;
  100. unsigned long alarm;
  101. struct acpi_battery_info *info;
  102. };
  103. /* --------------------------------------------------------------------------
  104. Battery Management
  105. -------------------------------------------------------------------------- */
  106. static int
  107. acpi_battery_get_info(struct acpi_battery *battery,
  108. struct acpi_battery_info **bif)
  109. {
  110. int result = 0;
  111. acpi_status status = 0;
  112. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  113. struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
  114. ACPI_BATTERY_FORMAT_BIF
  115. };
  116. struct acpi_buffer data = { 0, NULL };
  117. union acpi_object *package = NULL;
  118. if (!battery || !bif)
  119. return -EINVAL;
  120. /* Evalute _BIF */
  121. status = acpi_evaluate_object(battery->device->handle, "_BIF", NULL, &buffer);
  122. if (ACPI_FAILURE(status)) {
  123. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
  124. return -ENODEV;
  125. }
  126. package = (union acpi_object *)buffer.pointer;
  127. /* Extract Package Data */
  128. status = acpi_extract_package(package, &format, &data);
  129. if (status != AE_BUFFER_OVERFLOW) {
  130. ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
  131. result = -ENODEV;
  132. goto end;
  133. }
  134. data.pointer = kmalloc(data.length, GFP_KERNEL);
  135. if (!data.pointer) {
  136. result = -ENOMEM;
  137. goto end;
  138. }
  139. memset(data.pointer, 0, data.length);
  140. status = acpi_extract_package(package, &format, &data);
  141. if (ACPI_FAILURE(status)) {
  142. ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
  143. kfree(data.pointer);
  144. result = -ENODEV;
  145. goto end;
  146. }
  147. end:
  148. kfree(buffer.pointer);
  149. if (!result)
  150. (*bif) = (struct acpi_battery_info *)data.pointer;
  151. return result;
  152. }
  153. static int
  154. acpi_battery_get_status(struct acpi_battery *battery,
  155. struct acpi_battery_status **bst)
  156. {
  157. int result = 0;
  158. acpi_status status = 0;
  159. struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
  160. struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
  161. ACPI_BATTERY_FORMAT_BST
  162. };
  163. struct acpi_buffer data = { 0, NULL };
  164. union acpi_object *package = NULL;
  165. if (!battery || !bst)
  166. return -EINVAL;
  167. /* Evalute _BST */
  168. status = acpi_evaluate_object(battery->device->handle, "_BST", NULL, &buffer);
  169. if (ACPI_FAILURE(status)) {
  170. ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
  171. return -ENODEV;
  172. }
  173. package = (union acpi_object *)buffer.pointer;
  174. /* Extract Package Data */
  175. status = acpi_extract_package(package, &format, &data);
  176. if (status != AE_BUFFER_OVERFLOW) {
  177. ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
  178. result = -ENODEV;
  179. goto end;
  180. }
  181. data.pointer = kmalloc(data.length, GFP_KERNEL);
  182. if (!data.pointer) {
  183. result = -ENOMEM;
  184. goto end;
  185. }
  186. memset(data.pointer, 0, data.length);
  187. status = acpi_extract_package(package, &format, &data);
  188. if (ACPI_FAILURE(status)) {
  189. ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
  190. kfree(data.pointer);
  191. result = -ENODEV;
  192. goto end;
  193. }
  194. end:
  195. kfree(buffer.pointer);
  196. if (!result)
  197. (*bst) = (struct acpi_battery_status *)data.pointer;
  198. return result;
  199. }
  200. static int
  201. acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm)
  202. {
  203. acpi_status status = 0;
  204. union acpi_object arg0 = { ACPI_TYPE_INTEGER };
  205. struct acpi_object_list arg_list = { 1, &arg0 };
  206. if (!battery)
  207. return -EINVAL;
  208. if (!battery->flags.alarm)
  209. return -ENODEV;
  210. arg0.integer.value = alarm;
  211. status = acpi_evaluate_object(battery->device->handle, "_BTP", &arg_list, NULL);
  212. if (ACPI_FAILURE(status))
  213. return -ENODEV;
  214. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
  215. battery->alarm = alarm;
  216. return 0;
  217. }
  218. static int acpi_battery_check(struct acpi_battery *battery)
  219. {
  220. int result = 0;
  221. acpi_status status = AE_OK;
  222. acpi_handle handle = NULL;
  223. struct acpi_device *device = NULL;
  224. struct acpi_battery_info *bif = NULL;
  225. if (!battery)
  226. return -EINVAL;
  227. device = battery->device;
  228. result = acpi_bus_get_status(device);
  229. if (result)
  230. return result;
  231. /* Insertion? */
  232. if (!battery->flags.present && device->status.battery_present) {
  233. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
  234. /* Evalute _BIF to get certain static information */
  235. result = acpi_battery_get_info(battery, &bif);
  236. if (result)
  237. return result;
  238. battery->flags.power_unit = bif->power_unit;
  239. battery->trips.warning = bif->design_capacity_warning;
  240. battery->trips.low = bif->design_capacity_low;
  241. kfree(bif);
  242. /* See if alarms are supported, and if so, set default */
  243. status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
  244. if (ACPI_SUCCESS(status)) {
  245. battery->flags.alarm = 1;
  246. acpi_battery_set_alarm(battery, battery->trips.warning);
  247. }
  248. }
  249. /* Removal? */
  250. else if (battery->flags.present && !device->status.battery_present) {
  251. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
  252. }
  253. battery->flags.present = device->status.battery_present;
  254. return result;
  255. }
  256. /* --------------------------------------------------------------------------
  257. FS Interface (/proc)
  258. -------------------------------------------------------------------------- */
  259. static struct proc_dir_entry *acpi_battery_dir;
  260. static int acpi_battery_read_info(struct seq_file *seq, void *offset)
  261. {
  262. int result = 0;
  263. struct acpi_battery *battery = (struct acpi_battery *)seq->private;
  264. struct acpi_battery_info *bif = NULL;
  265. char *units = "?";
  266. if (!battery)
  267. goto end;
  268. if (battery->flags.present)
  269. seq_printf(seq, "present: yes\n");
  270. else {
  271. seq_printf(seq, "present: no\n");
  272. goto end;
  273. }
  274. /* Battery Info (_BIF) */
  275. result = acpi_battery_get_info(battery, &bif);
  276. if (result || !bif) {
  277. seq_printf(seq, "ERROR: Unable to read battery information\n");
  278. goto end;
  279. }
  280. units =
  281. bif->
  282. power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
  283. if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  284. seq_printf(seq, "design capacity: unknown\n");
  285. else
  286. seq_printf(seq, "design capacity: %d %sh\n",
  287. (u32) bif->design_capacity, units);
  288. if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  289. seq_printf(seq, "last full capacity: unknown\n");
  290. else
  291. seq_printf(seq, "last full capacity: %d %sh\n",
  292. (u32) bif->last_full_capacity, units);
  293. switch ((u32) bif->battery_technology) {
  294. case 0:
  295. seq_printf(seq, "battery technology: non-rechargeable\n");
  296. break;
  297. case 1:
  298. seq_printf(seq, "battery technology: rechargeable\n");
  299. break;
  300. default:
  301. seq_printf(seq, "battery technology: unknown\n");
  302. break;
  303. }
  304. if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  305. seq_printf(seq, "design voltage: unknown\n");
  306. else
  307. seq_printf(seq, "design voltage: %d mV\n",
  308. (u32) bif->design_voltage);
  309. seq_printf(seq, "design capacity warning: %d %sh\n",
  310. (u32) bif->design_capacity_warning, units);
  311. seq_printf(seq, "design capacity low: %d %sh\n",
  312. (u32) bif->design_capacity_low, units);
  313. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  314. (u32) bif->battery_capacity_granularity_1, units);
  315. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  316. (u32) bif->battery_capacity_granularity_2, units);
  317. seq_printf(seq, "model number: %s\n", bif->model_number);
  318. seq_printf(seq, "serial number: %s\n", bif->serial_number);
  319. seq_printf(seq, "battery type: %s\n", bif->battery_type);
  320. seq_printf(seq, "OEM info: %s\n", bif->oem_info);
  321. end:
  322. kfree(bif);
  323. return 0;
  324. }
  325. static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
  326. {
  327. return single_open(file, acpi_battery_read_info, PDE(inode)->data);
  328. }
  329. static int acpi_battery_read_state(struct seq_file *seq, void *offset)
  330. {
  331. int result = 0;
  332. struct acpi_battery *battery = (struct acpi_battery *)seq->private;
  333. struct acpi_battery_status *bst = NULL;
  334. char *units = "?";
  335. if (!battery)
  336. goto end;
  337. if (battery->flags.present)
  338. seq_printf(seq, "present: yes\n");
  339. else {
  340. seq_printf(seq, "present: no\n");
  341. goto end;
  342. }
  343. /* Battery Units */
  344. units =
  345. battery->flags.
  346. power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
  347. /* Battery Status (_BST) */
  348. result = acpi_battery_get_status(battery, &bst);
  349. if (result || !bst) {
  350. seq_printf(seq, "ERROR: Unable to read battery status\n");
  351. goto end;
  352. }
  353. if (!(bst->state & 0x04))
  354. seq_printf(seq, "capacity state: ok\n");
  355. else
  356. seq_printf(seq, "capacity state: critical\n");
  357. if ((bst->state & 0x01) && (bst->state & 0x02)) {
  358. seq_printf(seq,
  359. "charging state: charging/discharging\n");
  360. } else if (bst->state & 0x01)
  361. seq_printf(seq, "charging state: discharging\n");
  362. else if (bst->state & 0x02)
  363. seq_printf(seq, "charging state: charging\n");
  364. else {
  365. seq_printf(seq, "charging state: charged\n");
  366. }
  367. if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
  368. seq_printf(seq, "present rate: unknown\n");
  369. else
  370. seq_printf(seq, "present rate: %d %s\n",
  371. (u32) bst->present_rate, units);
  372. if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  373. seq_printf(seq, "remaining capacity: unknown\n");
  374. else
  375. seq_printf(seq, "remaining capacity: %d %sh\n",
  376. (u32) bst->remaining_capacity, units);
  377. if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  378. seq_printf(seq, "present voltage: unknown\n");
  379. else
  380. seq_printf(seq, "present voltage: %d mV\n",
  381. (u32) bst->present_voltage);
  382. end:
  383. kfree(bst);
  384. return 0;
  385. }
  386. static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
  387. {
  388. return single_open(file, acpi_battery_read_state, PDE(inode)->data);
  389. }
  390. static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
  391. {
  392. struct acpi_battery *battery = (struct acpi_battery *)seq->private;
  393. char *units = "?";
  394. if (!battery)
  395. goto end;
  396. if (!battery->flags.present) {
  397. seq_printf(seq, "present: no\n");
  398. goto end;
  399. }
  400. /* Battery Units */
  401. units =
  402. battery->flags.
  403. power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
  404. /* Battery Alarm */
  405. seq_printf(seq, "alarm: ");
  406. if (!battery->alarm)
  407. seq_printf(seq, "unsupported\n");
  408. else
  409. seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
  410. end:
  411. return 0;
  412. }
  413. static ssize_t
  414. acpi_battery_write_alarm(struct file *file,
  415. const char __user * buffer,
  416. size_t count, loff_t * ppos)
  417. {
  418. int result = 0;
  419. char alarm_string[12] = { '\0' };
  420. struct seq_file *m = (struct seq_file *)file->private_data;
  421. struct acpi_battery *battery = (struct acpi_battery *)m->private;
  422. if (!battery || (count > sizeof(alarm_string) - 1))
  423. return -EINVAL;
  424. if (!battery->flags.present)
  425. return -ENODEV;
  426. if (copy_from_user(alarm_string, buffer, count))
  427. return -EFAULT;
  428. alarm_string[count] = '\0';
  429. result = acpi_battery_set_alarm(battery,
  430. simple_strtoul(alarm_string, NULL, 0));
  431. if (result)
  432. return result;
  433. return count;
  434. }
  435. static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
  436. {
  437. return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
  438. }
  439. static struct file_operations acpi_battery_info_ops = {
  440. .open = acpi_battery_info_open_fs,
  441. .read = seq_read,
  442. .llseek = seq_lseek,
  443. .release = single_release,
  444. .owner = THIS_MODULE,
  445. };
  446. static struct file_operations acpi_battery_state_ops = {
  447. .open = acpi_battery_state_open_fs,
  448. .read = seq_read,
  449. .llseek = seq_lseek,
  450. .release = single_release,
  451. .owner = THIS_MODULE,
  452. };
  453. static struct file_operations acpi_battery_alarm_ops = {
  454. .open = acpi_battery_alarm_open_fs,
  455. .read = seq_read,
  456. .write = acpi_battery_write_alarm,
  457. .llseek = seq_lseek,
  458. .release = single_release,
  459. .owner = THIS_MODULE,
  460. };
  461. static int acpi_battery_add_fs(struct acpi_device *device)
  462. {
  463. struct proc_dir_entry *entry = NULL;
  464. if (!acpi_device_dir(device)) {
  465. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  466. acpi_battery_dir);
  467. if (!acpi_device_dir(device))
  468. return -ENODEV;
  469. acpi_device_dir(device)->owner = THIS_MODULE;
  470. }
  471. /* 'info' [R] */
  472. entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
  473. S_IRUGO, acpi_device_dir(device));
  474. if (!entry)
  475. return -ENODEV;
  476. else {
  477. entry->proc_fops = &acpi_battery_info_ops;
  478. entry->data = acpi_driver_data(device);
  479. entry->owner = THIS_MODULE;
  480. }
  481. /* 'status' [R] */
  482. entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
  483. S_IRUGO, acpi_device_dir(device));
  484. if (!entry)
  485. return -ENODEV;
  486. else {
  487. entry->proc_fops = &acpi_battery_state_ops;
  488. entry->data = acpi_driver_data(device);
  489. entry->owner = THIS_MODULE;
  490. }
  491. /* 'alarm' [R/W] */
  492. entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
  493. S_IFREG | S_IRUGO | S_IWUSR,
  494. acpi_device_dir(device));
  495. if (!entry)
  496. return -ENODEV;
  497. else {
  498. entry->proc_fops = &acpi_battery_alarm_ops;
  499. entry->data = acpi_driver_data(device);
  500. entry->owner = THIS_MODULE;
  501. }
  502. return 0;
  503. }
  504. static int acpi_battery_remove_fs(struct acpi_device *device)
  505. {
  506. if (acpi_device_dir(device)) {
  507. remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
  508. acpi_device_dir(device));
  509. remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
  510. acpi_device_dir(device));
  511. remove_proc_entry(ACPI_BATTERY_FILE_INFO,
  512. acpi_device_dir(device));
  513. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  514. acpi_device_dir(device) = NULL;
  515. }
  516. return 0;
  517. }
  518. /* --------------------------------------------------------------------------
  519. Driver Interface
  520. -------------------------------------------------------------------------- */
  521. static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
  522. {
  523. struct acpi_battery *battery = (struct acpi_battery *)data;
  524. struct acpi_device *device = NULL;
  525. if (!battery)
  526. return;
  527. device = battery->device;
  528. switch (event) {
  529. case ACPI_BATTERY_NOTIFY_STATUS:
  530. case ACPI_BATTERY_NOTIFY_INFO:
  531. acpi_battery_check(battery);
  532. acpi_bus_generate_event(device, event, battery->flags.present);
  533. break;
  534. default:
  535. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  536. "Unsupported event [0x%x]\n", event));
  537. break;
  538. }
  539. return;
  540. }
  541. static int acpi_battery_add(struct acpi_device *device)
  542. {
  543. int result = 0;
  544. acpi_status status = 0;
  545. struct acpi_battery *battery = NULL;
  546. if (!device)
  547. return -EINVAL;
  548. battery = kmalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  549. if (!battery)
  550. return -ENOMEM;
  551. memset(battery, 0, sizeof(struct acpi_battery));
  552. battery->device = device;
  553. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  554. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  555. acpi_driver_data(device) = battery;
  556. result = acpi_battery_check(battery);
  557. if (result)
  558. goto end;
  559. result = acpi_battery_add_fs(device);
  560. if (result)
  561. goto end;
  562. status = acpi_install_notify_handler(device->handle,
  563. ACPI_DEVICE_NOTIFY,
  564. acpi_battery_notify, battery);
  565. if (ACPI_FAILURE(status)) {
  566. result = -ENODEV;
  567. goto end;
  568. }
  569. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  570. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  571. device->status.battery_present ? "present" : "absent");
  572. end:
  573. if (result) {
  574. acpi_battery_remove_fs(device);
  575. kfree(battery);
  576. }
  577. return result;
  578. }
  579. static int acpi_battery_remove(struct acpi_device *device, int type)
  580. {
  581. acpi_status status = 0;
  582. struct acpi_battery *battery = NULL;
  583. if (!device || !acpi_driver_data(device))
  584. return -EINVAL;
  585. battery = (struct acpi_battery *)acpi_driver_data(device);
  586. status = acpi_remove_notify_handler(device->handle,
  587. ACPI_DEVICE_NOTIFY,
  588. acpi_battery_notify);
  589. acpi_battery_remove_fs(device);
  590. kfree(battery);
  591. return 0;
  592. }
  593. static int __init acpi_battery_init(void)
  594. {
  595. int result = 0;
  596. acpi_battery_dir = proc_mkdir(ACPI_BATTERY_CLASS, acpi_root_dir);
  597. if (!acpi_battery_dir)
  598. return -ENODEV;
  599. acpi_battery_dir->owner = THIS_MODULE;
  600. result = acpi_bus_register_driver(&acpi_battery_driver);
  601. if (result < 0) {
  602. remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
  603. return -ENODEV;
  604. }
  605. return 0;
  606. }
  607. static void __exit acpi_battery_exit(void)
  608. {
  609. acpi_bus_unregister_driver(&acpi_battery_driver);
  610. remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
  611. return;
  612. }
  613. module_init(acpi_battery_init);
  614. module_exit(acpi_battery_exit);