battery.c 20 KB

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