battery.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846
  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 (
  108. struct acpi_battery *battery,
  109. struct acpi_battery_info **bif)
  110. {
  111. int result = 0;
  112. acpi_status status = 0;
  113. struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
  114. struct acpi_buffer format = {sizeof(ACPI_BATTERY_FORMAT_BIF),
  115. ACPI_BATTERY_FORMAT_BIF};
  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_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BIF\n"));
  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_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BIF\n"));
  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_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BIF\n"));
  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 (
  156. 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. 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_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BST\n"));
  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_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BST\n"));
  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_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BST\n"));
  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 (
  204. struct acpi_battery *battery,
  205. 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. ACPI_FUNCTION_TRACE("acpi_battery_set_alarm");
  211. if (!battery)
  212. return_VALUE(-EINVAL);
  213. if (!battery->flags.alarm)
  214. return_VALUE(-ENODEV);
  215. arg0.integer.value = alarm;
  216. status = acpi_evaluate_object(battery->handle, "_BTP", &arg_list, NULL);
  217. if (ACPI_FAILURE(status))
  218. return_VALUE(-ENODEV);
  219. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
  220. battery->alarm = alarm;
  221. return_VALUE(0);
  222. }
  223. static int
  224. acpi_battery_check (
  225. struct acpi_battery *battery)
  226. {
  227. int result = 0;
  228. acpi_status status = AE_OK;
  229. acpi_handle handle = NULL;
  230. struct acpi_device *device = NULL;
  231. struct acpi_battery_info *bif = NULL;
  232. ACPI_FUNCTION_TRACE("acpi_battery_check");
  233. if (!battery)
  234. return_VALUE(-EINVAL);
  235. result = acpi_bus_get_device(battery->handle, &device);
  236. if (result)
  237. return_VALUE(result);
  238. result = acpi_bus_get_status(device);
  239. if (result)
  240. return_VALUE(result);
  241. /* Insertion? */
  242. if (!battery->flags.present && device->status.battery_present) {
  243. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
  244. /* Evalute _BIF to get certain static information */
  245. result = acpi_battery_get_info(battery, &bif);
  246. if (result)
  247. return_VALUE(result);
  248. battery->flags.power_unit = bif->power_unit;
  249. battery->trips.warning = bif->design_capacity_warning;
  250. battery->trips.low = bif->design_capacity_low;
  251. kfree(bif);
  252. /* See if alarms are supported, and if so, set default */
  253. status = acpi_get_handle(battery->handle, "_BTP", &handle);
  254. if (ACPI_SUCCESS(status)) {
  255. battery->flags.alarm = 1;
  256. acpi_battery_set_alarm(battery, battery->trips.warning);
  257. }
  258. }
  259. /* Removal? */
  260. else if (battery->flags.present && !device->status.battery_present) {
  261. ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
  262. }
  263. battery->flags.present = device->status.battery_present;
  264. return_VALUE(result);
  265. }
  266. /* --------------------------------------------------------------------------
  267. FS Interface (/proc)
  268. -------------------------------------------------------------------------- */
  269. static struct proc_dir_entry *acpi_battery_dir;
  270. static int acpi_battery_read_info(struct seq_file *seq, void *offset)
  271. {
  272. int result = 0;
  273. struct acpi_battery *battery = (struct acpi_battery *) seq->private;
  274. struct acpi_battery_info *bif = NULL;
  275. char *units = "?";
  276. ACPI_FUNCTION_TRACE("acpi_battery_read_info");
  277. if (!battery)
  278. goto end;
  279. if (battery->flags.present)
  280. seq_printf(seq, "present: yes\n");
  281. else {
  282. seq_printf(seq, "present: no\n");
  283. goto end;
  284. }
  285. /* Battery Info (_BIF) */
  286. result = acpi_battery_get_info(battery, &bif);
  287. if (result || !bif) {
  288. seq_printf(seq, "ERROR: Unable to read battery information\n");
  289. goto end;
  290. }
  291. units = bif->power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
  292. if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  293. seq_printf(seq, "design capacity: unknown\n");
  294. else
  295. seq_printf(seq, "design capacity: %d %sh\n",
  296. (u32) bif->design_capacity, units);
  297. if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  298. seq_printf(seq, "last full capacity: unknown\n");
  299. else
  300. seq_printf(seq, "last full capacity: %d %sh\n",
  301. (u32) bif->last_full_capacity, units);
  302. switch ((u32) bif->battery_technology) {
  303. case 0:
  304. seq_printf(seq, "battery technology: non-rechargeable\n");
  305. break;
  306. case 1:
  307. seq_printf(seq, "battery technology: rechargeable\n");
  308. break;
  309. default:
  310. seq_printf(seq, "battery technology: unknown\n");
  311. break;
  312. }
  313. if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  314. seq_printf(seq, "design voltage: unknown\n");
  315. else
  316. seq_printf(seq, "design voltage: %d mV\n",
  317. (u32) bif->design_voltage);
  318. seq_printf(seq, "design capacity warning: %d %sh\n",
  319. (u32) bif->design_capacity_warning, units);
  320. seq_printf(seq, "design capacity low: %d %sh\n",
  321. (u32) bif->design_capacity_low, units);
  322. seq_printf(seq, "capacity granularity 1: %d %sh\n",
  323. (u32) bif->battery_capacity_granularity_1, units);
  324. seq_printf(seq, "capacity granularity 2: %d %sh\n",
  325. (u32) bif->battery_capacity_granularity_2, units);
  326. seq_printf(seq, "model number: %s\n",
  327. bif->model_number);
  328. seq_printf(seq, "serial number: %s\n",
  329. bif->serial_number);
  330. seq_printf(seq, "battery type: %s\n",
  331. bif->battery_type);
  332. seq_printf(seq, "OEM info: %s\n",
  333. bif->oem_info);
  334. end:
  335. kfree(bif);
  336. return_VALUE(0);
  337. }
  338. static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
  339. {
  340. return single_open(file, acpi_battery_read_info, PDE(inode)->data);
  341. }
  342. static int
  343. acpi_battery_read_state (struct seq_file *seq, void *offset)
  344. {
  345. int result = 0;
  346. struct acpi_battery *battery = (struct acpi_battery *) seq->private;
  347. struct acpi_battery_status *bst = NULL;
  348. char *units = "?";
  349. ACPI_FUNCTION_TRACE("acpi_battery_read_state");
  350. if (!battery)
  351. goto end;
  352. if (battery->flags.present)
  353. seq_printf(seq, "present: yes\n");
  354. else {
  355. seq_printf(seq, "present: no\n");
  356. goto end;
  357. }
  358. /* Battery Units */
  359. units = battery->flags.power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
  360. /* Battery Status (_BST) */
  361. result = acpi_battery_get_status(battery, &bst);
  362. if (result || !bst) {
  363. seq_printf(seq, "ERROR: Unable to read battery status\n");
  364. goto end;
  365. }
  366. if (!(bst->state & 0x04))
  367. seq_printf(seq, "capacity state: ok\n");
  368. else
  369. seq_printf(seq, "capacity state: critical\n");
  370. if ((bst->state & 0x01) && (bst->state & 0x02)){
  371. seq_printf(seq, "charging state: charging/discharging\n");
  372. ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
  373. "Battery Charging and Discharging?\n"));
  374. }
  375. else if (bst->state & 0x01)
  376. seq_printf(seq, "charging state: discharging\n");
  377. else if (bst->state & 0x02)
  378. seq_printf(seq, "charging state: charging\n");
  379. else {
  380. seq_printf(seq, "charging state: charged\n");
  381. }
  382. if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
  383. seq_printf(seq, "present rate: unknown\n");
  384. else
  385. seq_printf(seq, "present rate: %d %s\n",
  386. (u32) bst->present_rate, units);
  387. if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
  388. seq_printf(seq, "remaining capacity: unknown\n");
  389. else
  390. seq_printf(seq, "remaining capacity: %d %sh\n",
  391. (u32) bst->remaining_capacity, units);
  392. if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
  393. seq_printf(seq, "present voltage: unknown\n");
  394. else
  395. seq_printf(seq, "present voltage: %d mV\n",
  396. (u32) bst->present_voltage);
  397. end:
  398. kfree(bst);
  399. return_VALUE(0);
  400. }
  401. static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
  402. {
  403. return single_open(file, acpi_battery_read_state, PDE(inode)->data);
  404. }
  405. static int
  406. acpi_battery_read_alarm (struct seq_file *seq, void *offset)
  407. {
  408. struct acpi_battery *battery = (struct acpi_battery *) seq->private;
  409. char *units = "?";
  410. ACPI_FUNCTION_TRACE("acpi_battery_read_alarm");
  411. if (!battery)
  412. goto end;
  413. if (!battery->flags.present) {
  414. seq_printf(seq, "present: no\n");
  415. goto end;
  416. }
  417. /* Battery Units */
  418. units = battery->flags.power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
  419. /* Battery Alarm */
  420. seq_printf(seq, "alarm: ");
  421. if (!battery->alarm)
  422. seq_printf(seq, "unsupported\n");
  423. else
  424. seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
  425. end:
  426. return_VALUE(0);
  427. }
  428. static ssize_t
  429. acpi_battery_write_alarm (
  430. struct file *file,
  431. const char __user *buffer,
  432. size_t count,
  433. loff_t *ppos)
  434. {
  435. int result = 0;
  436. char alarm_string[12] = {'\0'};
  437. struct seq_file *m = (struct seq_file *)file->private_data;
  438. struct acpi_battery *battery = (struct acpi_battery *)m->private;
  439. ACPI_FUNCTION_TRACE("acpi_battery_write_alarm");
  440. if (!battery || (count > sizeof(alarm_string) - 1))
  441. return_VALUE(-EINVAL);
  442. if (!battery->flags.present)
  443. return_VALUE(-ENODEV);
  444. if (copy_from_user(alarm_string, buffer, count))
  445. return_VALUE(-EFAULT);
  446. alarm_string[count] = '\0';
  447. result = acpi_battery_set_alarm(battery,
  448. simple_strtoul(alarm_string, NULL, 0));
  449. if (result)
  450. return_VALUE(result);
  451. return_VALUE(count);
  452. }
  453. static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
  454. {
  455. return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
  456. }
  457. static struct file_operations acpi_battery_info_ops = {
  458. .open = acpi_battery_info_open_fs,
  459. .read = seq_read,
  460. .llseek = seq_lseek,
  461. .release = single_release,
  462. .owner = THIS_MODULE,
  463. };
  464. static struct file_operations acpi_battery_state_ops = {
  465. .open = acpi_battery_state_open_fs,
  466. .read = seq_read,
  467. .llseek = seq_lseek,
  468. .release = single_release,
  469. .owner = THIS_MODULE,
  470. };
  471. static struct file_operations acpi_battery_alarm_ops = {
  472. .open = acpi_battery_alarm_open_fs,
  473. .read = seq_read,
  474. .write = acpi_battery_write_alarm,
  475. .llseek = seq_lseek,
  476. .release = single_release,
  477. .owner = THIS_MODULE,
  478. };
  479. static int
  480. acpi_battery_add_fs (
  481. struct acpi_device *device)
  482. {
  483. struct proc_dir_entry *entry = NULL;
  484. ACPI_FUNCTION_TRACE("acpi_battery_add_fs");
  485. if (!acpi_device_dir(device)) {
  486. acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
  487. acpi_battery_dir);
  488. if (!acpi_device_dir(device))
  489. return_VALUE(-ENODEV);
  490. acpi_device_dir(device)->owner = THIS_MODULE;
  491. }
  492. /* 'info' [R] */
  493. entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
  494. S_IRUGO, acpi_device_dir(device));
  495. if (!entry)
  496. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  497. "Unable to create '%s' fs entry\n",
  498. ACPI_BATTERY_FILE_INFO));
  499. else {
  500. entry->proc_fops = &acpi_battery_info_ops;
  501. entry->data = acpi_driver_data(device);
  502. entry->owner = THIS_MODULE;
  503. }
  504. /* 'status' [R] */
  505. entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
  506. S_IRUGO, acpi_device_dir(device));
  507. if (!entry)
  508. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  509. "Unable to create '%s' fs entry\n",
  510. ACPI_BATTERY_FILE_STATUS));
  511. else {
  512. entry->proc_fops = &acpi_battery_state_ops;
  513. entry->data = acpi_driver_data(device);
  514. entry->owner = THIS_MODULE;
  515. }
  516. /* 'alarm' [R/W] */
  517. entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
  518. S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
  519. if (!entry)
  520. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  521. "Unable to create '%s' fs entry\n",
  522. ACPI_BATTERY_FILE_ALARM));
  523. else {
  524. entry->proc_fops = &acpi_battery_alarm_ops;
  525. entry->data = acpi_driver_data(device);
  526. entry->owner = THIS_MODULE;
  527. }
  528. return_VALUE(0);
  529. }
  530. static int
  531. acpi_battery_remove_fs (
  532. struct acpi_device *device)
  533. {
  534. ACPI_FUNCTION_TRACE("acpi_battery_remove_fs");
  535. if (acpi_device_dir(device)) {
  536. remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
  537. acpi_device_dir(device));
  538. remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
  539. acpi_device_dir(device));
  540. remove_proc_entry(ACPI_BATTERY_FILE_INFO,
  541. acpi_device_dir(device));
  542. remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
  543. acpi_device_dir(device) = NULL;
  544. }
  545. return_VALUE(0);
  546. }
  547. /* --------------------------------------------------------------------------
  548. Driver Interface
  549. -------------------------------------------------------------------------- */
  550. static void
  551. acpi_battery_notify (
  552. acpi_handle handle,
  553. u32 event,
  554. void *data)
  555. {
  556. struct acpi_battery *battery = (struct acpi_battery *) data;
  557. struct acpi_device *device = NULL;
  558. ACPI_FUNCTION_TRACE("acpi_battery_notify");
  559. if (!battery)
  560. return_VOID;
  561. if (acpi_bus_get_device(handle, &device))
  562. return_VOID;
  563. switch (event) {
  564. case ACPI_BATTERY_NOTIFY_STATUS:
  565. case ACPI_BATTERY_NOTIFY_INFO:
  566. acpi_battery_check(battery);
  567. acpi_bus_generate_event(device, event, battery->flags.present);
  568. break;
  569. default:
  570. ACPI_DEBUG_PRINT((ACPI_DB_INFO,
  571. "Unsupported event [0x%x]\n", event));
  572. break;
  573. }
  574. return_VOID;
  575. }
  576. static int
  577. acpi_battery_add (
  578. struct acpi_device *device)
  579. {
  580. int result = 0;
  581. acpi_status status = 0;
  582. struct acpi_battery *battery = NULL;
  583. ACPI_FUNCTION_TRACE("acpi_battery_add");
  584. if (!device)
  585. return_VALUE(-EINVAL);
  586. battery = kmalloc(sizeof(struct acpi_battery), GFP_KERNEL);
  587. if (!battery)
  588. return_VALUE(-ENOMEM);
  589. memset(battery, 0, sizeof(struct acpi_battery));
  590. battery->handle = device->handle;
  591. strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
  592. strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
  593. acpi_driver_data(device) = battery;
  594. result = acpi_battery_check(battery);
  595. if (result)
  596. goto end;
  597. result = acpi_battery_add_fs(device);
  598. if (result)
  599. goto end;
  600. status = acpi_install_notify_handler(battery->handle,
  601. ACPI_DEVICE_NOTIFY, acpi_battery_notify, battery);
  602. if (ACPI_FAILURE(status)) {
  603. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  604. "Error installing notify handler\n"));
  605. result = -ENODEV;
  606. goto end;
  607. }
  608. printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
  609. ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
  610. device->status.battery_present?"present":"absent");
  611. end:
  612. if (result) {
  613. acpi_battery_remove_fs(device);
  614. kfree(battery);
  615. }
  616. return_VALUE(result);
  617. }
  618. static int
  619. acpi_battery_remove (
  620. struct acpi_device *device,
  621. int type)
  622. {
  623. acpi_status status = 0;
  624. struct acpi_battery *battery = NULL;
  625. ACPI_FUNCTION_TRACE("acpi_battery_remove");
  626. if (!device || !acpi_driver_data(device))
  627. return_VALUE(-EINVAL);
  628. battery = (struct acpi_battery *) acpi_driver_data(device);
  629. status = acpi_remove_notify_handler(battery->handle,
  630. ACPI_DEVICE_NOTIFY, acpi_battery_notify);
  631. if (ACPI_FAILURE(status))
  632. ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
  633. "Error removing notify handler\n"));
  634. acpi_battery_remove_fs(device);
  635. kfree(battery);
  636. return_VALUE(0);
  637. }
  638. static int __init
  639. acpi_battery_init (void)
  640. {
  641. int result = 0;
  642. ACPI_FUNCTION_TRACE("acpi_battery_init");
  643. acpi_battery_dir = proc_mkdir(ACPI_BATTERY_CLASS, acpi_root_dir);
  644. if (!acpi_battery_dir)
  645. return_VALUE(-ENODEV);
  646. acpi_battery_dir->owner = THIS_MODULE;
  647. result = acpi_bus_register_driver(&acpi_battery_driver);
  648. if (result < 0) {
  649. remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
  650. return_VALUE(-ENODEV);
  651. }
  652. return_VALUE(0);
  653. }
  654. static void __exit
  655. acpi_battery_exit (void)
  656. {
  657. ACPI_FUNCTION_TRACE("acpi_battery_exit");
  658. acpi_bus_unregister_driver(&acpi_battery_driver);
  659. remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
  660. return_VOID;
  661. }
  662. module_init(acpi_battery_init);
  663. module_exit(acpi_battery_exit);