battery.c 20 KB

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