battery.c 20 KB

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