battery.c 20 KB

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