[x86 setup] Don't use EDD to get the MBR signature
[linux-2.6/linux-loongson.git] / drivers / acpi / battery.c
blobd7b499fe0cd932fec5f8c6a9c7237cd38b962acc
1 /*
2 * acpi_battery.c - ACPI Battery Driver ($Revision: 37 $)
4 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
7 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
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.
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.
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.
23 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <asm/uaccess.h>
34 #include <acpi/acpi_bus.h>
35 #include <acpi/acpi_drivers.h>
37 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
39 #define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS"
40 #define ACPI_BATTERY_FORMAT_BST "NNNN"
42 #define ACPI_BATTERY_COMPONENT 0x00040000
43 #define ACPI_BATTERY_CLASS "battery"
44 #define ACPI_BATTERY_DEVICE_NAME "Battery"
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"
50 #define _COMPONENT ACPI_BATTERY_COMPONENT
52 #define ACPI_BATTERY_UPDATE_TIME 0
54 #define ACPI_BATTERY_NONE_UPDATE 0
55 #define ACPI_BATTERY_EASY_UPDATE 1
56 #define ACPI_BATTERY_INIT_UPDATE 2
58 ACPI_MODULE_NAME("battery");
60 MODULE_AUTHOR("Paul Diefenbaugh");
61 MODULE_DESCRIPTION("ACPI Battery Driver");
62 MODULE_LICENSE("GPL");
64 static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME;
66 /* 0 - every time, > 0 - by update_time */
67 module_param(update_time, uint, 0644);
69 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
70 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
72 static int acpi_battery_add(struct acpi_device *device);
73 static int acpi_battery_remove(struct acpi_device *device, int type);
74 static int acpi_battery_resume(struct acpi_device *device);
76 static const struct acpi_device_id battery_device_ids[] = {
77 {"PNP0C0A", 0},
78 {"", 0},
80 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
82 static struct acpi_driver acpi_battery_driver = {
83 .name = "battery",
84 .class = ACPI_BATTERY_CLASS,
85 .ids = battery_device_ids,
86 .ops = {
87 .add = acpi_battery_add,
88 .resume = acpi_battery_resume,
89 .remove = acpi_battery_remove,
93 struct acpi_battery_state {
94 acpi_integer state;
95 acpi_integer present_rate;
96 acpi_integer remaining_capacity;
97 acpi_integer present_voltage;
100 struct acpi_battery_info {
101 acpi_integer power_unit;
102 acpi_integer design_capacity;
103 acpi_integer last_full_capacity;
104 acpi_integer battery_technology;
105 acpi_integer design_voltage;
106 acpi_integer design_capacity_warning;
107 acpi_integer design_capacity_low;
108 acpi_integer battery_capacity_granularity_1;
109 acpi_integer battery_capacity_granularity_2;
110 acpi_string model_number;
111 acpi_string serial_number;
112 acpi_string battery_type;
113 acpi_string oem_info;
116 enum acpi_battery_files {
117 ACPI_BATTERY_INFO = 0,
118 ACPI_BATTERY_STATE,
119 ACPI_BATTERY_ALARM,
120 ACPI_BATTERY_NUMFILES,
123 struct acpi_battery_flags {
124 u8 battery_present_prev;
125 u8 alarm_present;
126 u8 init_update;
127 u8 update[ACPI_BATTERY_NUMFILES];
128 u8 power_unit;
131 struct acpi_battery {
132 struct acpi_device *device;
133 struct acpi_battery_flags flags;
134 struct acpi_buffer bif_data;
135 struct acpi_buffer bst_data;
136 struct mutex lock;
137 unsigned long alarm;
138 unsigned long update_time[ACPI_BATTERY_NUMFILES];
142 inline int acpi_battery_present(struct acpi_battery *battery)
144 return battery->device->status.battery_present;
146 inline char *acpi_battery_power_units(struct acpi_battery *battery)
148 if (battery->flags.power_unit)
149 return ACPI_BATTERY_UNITS_AMPS;
150 else
151 return ACPI_BATTERY_UNITS_WATTS;
154 inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
156 return battery->device->handle;
159 /* --------------------------------------------------------------------------
160 Battery Management
161 -------------------------------------------------------------------------- */
163 static void acpi_battery_check_result(struct acpi_battery *battery, int result)
165 if (!battery)
166 return;
168 if (result) {
169 battery->flags.init_update = 1;
173 static int acpi_battery_extract_package(struct acpi_battery *battery,
174 union acpi_object *package,
175 struct acpi_buffer *format,
176 struct acpi_buffer *data,
177 char *package_name)
179 acpi_status status = AE_OK;
180 struct acpi_buffer data_null = { 0, NULL };
182 status = acpi_extract_package(package, format, &data_null);
183 if (status != AE_BUFFER_OVERFLOW) {
184 ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s",
185 package_name));
186 return -ENODEV;
189 if (data_null.length != data->length) {
190 kfree(data->pointer);
191 data->pointer = kzalloc(data_null.length, GFP_KERNEL);
192 if (!data->pointer) {
193 ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()"));
194 return -ENOMEM;
196 data->length = data_null.length;
199 status = acpi_extract_package(package, format, data);
200 if (ACPI_FAILURE(status)) {
201 ACPI_EXCEPTION((AE_INFO, status, "Extracting %s",
202 package_name));
203 return -ENODEV;
206 return 0;
209 static int acpi_battery_get_status(struct acpi_battery *battery)
211 int result = 0;
213 result = acpi_bus_get_status(battery->device);
214 if (result) {
215 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
216 return -ENODEV;
218 return result;
221 static int acpi_battery_get_info(struct acpi_battery *battery)
223 int result = 0;
224 acpi_status status = 0;
225 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
226 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
227 ACPI_BATTERY_FORMAT_BIF
229 union acpi_object *package = NULL;
230 struct acpi_buffer *data = NULL;
231 struct acpi_battery_info *bif = NULL;
233 battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
235 if (!acpi_battery_present(battery))
236 return 0;
238 /* Evaluate _BIF */
239 mutex_lock(&battery->lock);
240 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BIF",
241 NULL, &buffer);
242 mutex_unlock(&battery->lock);
243 if (ACPI_FAILURE(status)) {
244 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
245 return -ENODEV;
248 package = buffer.pointer;
250 data = &battery->bif_data;
252 /* Extract Package Data */
254 result =
255 acpi_battery_extract_package(battery, package, &format, data,
256 "_BIF");
257 if (result)
258 goto end;
260 end:
262 kfree(buffer.pointer);
264 if (!result) {
265 bif = data->pointer;
266 battery->flags.power_unit = bif->power_unit;
269 return result;
272 static int acpi_battery_get_state(struct acpi_battery *battery)
274 int result = 0;
275 acpi_status status = 0;
276 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
277 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
278 ACPI_BATTERY_FORMAT_BST
280 union acpi_object *package = NULL;
281 struct acpi_buffer *data = NULL;
283 battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
285 if (!acpi_battery_present(battery))
286 return 0;
288 /* Evaluate _BST */
289 mutex_lock(&battery->lock);
290 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BST",
291 NULL, &buffer);
292 mutex_unlock(&battery->lock);
293 if (ACPI_FAILURE(status)) {
294 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
295 return -ENODEV;
298 package = buffer.pointer;
300 data = &battery->bst_data;
302 /* Extract Package Data */
304 result =
305 acpi_battery_extract_package(battery, package, &format, data,
306 "_BST");
307 if (result)
308 goto end;
310 end:
311 kfree(buffer.pointer);
313 return result;
316 static int acpi_battery_get_alarm(struct acpi_battery *battery)
318 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
320 return 0;
323 static int acpi_battery_set_alarm(struct acpi_battery *battery,
324 unsigned long alarm)
326 acpi_status status = 0;
327 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
328 struct acpi_object_list arg_list = { 1, &arg0 };
330 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
332 if (!acpi_battery_present(battery))
333 return -ENODEV;
335 if (!battery->flags.alarm_present)
336 return -ENODEV;
338 arg0.integer.value = alarm;
340 mutex_lock(&battery->lock);
341 status = acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
342 &arg_list, NULL);
343 mutex_unlock(&battery->lock);
344 if (ACPI_FAILURE(status))
345 return -ENODEV;
347 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
349 battery->alarm = alarm;
351 return 0;
354 static int acpi_battery_init_alarm(struct acpi_battery *battery)
356 int result = 0;
357 acpi_status status = AE_OK;
358 acpi_handle handle = NULL;
359 struct acpi_battery_info *bif = battery->bif_data.pointer;
360 unsigned long alarm = battery->alarm;
362 /* See if alarms are supported, and if so, set default */
364 status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
365 if (ACPI_SUCCESS(status)) {
366 battery->flags.alarm_present = 1;
367 if (!alarm && bif) {
368 alarm = bif->design_capacity_warning;
370 result = acpi_battery_set_alarm(battery, alarm);
371 if (result)
372 goto end;
373 } else {
374 battery->flags.alarm_present = 0;
377 end:
379 return result;
382 static int acpi_battery_init_update(struct acpi_battery *battery)
384 int result = 0;
386 result = acpi_battery_get_status(battery);
387 if (result)
388 return result;
390 battery->flags.battery_present_prev = acpi_battery_present(battery);
392 if (acpi_battery_present(battery)) {
393 result = acpi_battery_get_info(battery);
394 if (result)
395 return result;
396 result = acpi_battery_get_state(battery);
397 if (result)
398 return result;
400 acpi_battery_init_alarm(battery);
403 return result;
406 static int acpi_battery_update(struct acpi_battery *battery,
407 int update, int *update_result_ptr)
409 int result = 0;
410 int update_result = ACPI_BATTERY_NONE_UPDATE;
412 if (!acpi_battery_present(battery)) {
413 update = 1;
416 if (battery->flags.init_update) {
417 result = acpi_battery_init_update(battery);
418 if (result)
419 goto end;
420 update_result = ACPI_BATTERY_INIT_UPDATE;
421 } else if (update) {
422 result = acpi_battery_get_status(battery);
423 if (result)
424 goto end;
425 if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
426 || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
427 result = acpi_battery_init_update(battery);
428 if (result)
429 goto end;
430 update_result = ACPI_BATTERY_INIT_UPDATE;
431 } else {
432 update_result = ACPI_BATTERY_EASY_UPDATE;
436 end:
438 battery->flags.init_update = (result != 0);
440 *update_result_ptr = update_result;
442 return result;
445 static void acpi_battery_notify_update(struct acpi_battery *battery)
447 acpi_battery_get_status(battery);
449 if (battery->flags.init_update) {
450 return;
453 if ((!battery->flags.battery_present_prev &
454 acpi_battery_present(battery)) ||
455 (battery->flags.battery_present_prev &
456 !acpi_battery_present(battery))) {
457 battery->flags.init_update = 1;
458 } else {
459 battery->flags.update[ACPI_BATTERY_INFO] = 1;
460 battery->flags.update[ACPI_BATTERY_STATE] = 1;
461 battery->flags.update[ACPI_BATTERY_ALARM] = 1;
465 /* --------------------------------------------------------------------------
466 FS Interface (/proc)
467 -------------------------------------------------------------------------- */
469 static struct proc_dir_entry *acpi_battery_dir;
471 static int acpi_battery_print_info(struct seq_file *seq, int result)
473 struct acpi_battery *battery = seq->private;
474 struct acpi_battery_info *bif = NULL;
475 char *units = "?";
477 if (result)
478 goto end;
480 if (acpi_battery_present(battery))
481 seq_printf(seq, "present: yes\n");
482 else {
483 seq_printf(seq, "present: no\n");
484 goto end;
487 bif = battery->bif_data.pointer;
488 if (!bif) {
489 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
490 result = -ENODEV;
491 goto end;
494 /* Battery Units */
496 units = acpi_battery_power_units(battery);
498 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
499 seq_printf(seq, "design capacity: unknown\n");
500 else
501 seq_printf(seq, "design capacity: %d %sh\n",
502 (u32) bif->design_capacity, units);
504 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
505 seq_printf(seq, "last full capacity: unknown\n");
506 else
507 seq_printf(seq, "last full capacity: %d %sh\n",
508 (u32) bif->last_full_capacity, units);
510 switch ((u32) bif->battery_technology) {
511 case 0:
512 seq_printf(seq, "battery technology: non-rechargeable\n");
513 break;
514 case 1:
515 seq_printf(seq, "battery technology: rechargeable\n");
516 break;
517 default:
518 seq_printf(seq, "battery technology: unknown\n");
519 break;
522 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
523 seq_printf(seq, "design voltage: unknown\n");
524 else
525 seq_printf(seq, "design voltage: %d mV\n",
526 (u32) bif->design_voltage);
527 seq_printf(seq, "design capacity warning: %d %sh\n",
528 (u32) bif->design_capacity_warning, units);
529 seq_printf(seq, "design capacity low: %d %sh\n",
530 (u32) bif->design_capacity_low, units);
531 seq_printf(seq, "capacity granularity 1: %d %sh\n",
532 (u32) bif->battery_capacity_granularity_1, units);
533 seq_printf(seq, "capacity granularity 2: %d %sh\n",
534 (u32) bif->battery_capacity_granularity_2, units);
535 seq_printf(seq, "model number: %s\n", bif->model_number);
536 seq_printf(seq, "serial number: %s\n", bif->serial_number);
537 seq_printf(seq, "battery type: %s\n", bif->battery_type);
538 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
540 end:
542 if (result)
543 seq_printf(seq, "ERROR: Unable to read battery info\n");
545 return result;
548 static int acpi_battery_print_state(struct seq_file *seq, int result)
550 struct acpi_battery *battery = seq->private;
551 struct acpi_battery_state *bst = NULL;
552 char *units = "?";
554 if (result)
555 goto end;
557 if (acpi_battery_present(battery))
558 seq_printf(seq, "present: yes\n");
559 else {
560 seq_printf(seq, "present: no\n");
561 goto end;
564 bst = battery->bst_data.pointer;
565 if (!bst) {
566 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
567 result = -ENODEV;
568 goto end;
571 /* Battery Units */
573 units = acpi_battery_power_units(battery);
575 if (!(bst->state & 0x04))
576 seq_printf(seq, "capacity state: ok\n");
577 else
578 seq_printf(seq, "capacity state: critical\n");
580 if ((bst->state & 0x01) && (bst->state & 0x02)) {
581 seq_printf(seq,
582 "charging state: charging/discharging\n");
583 } else if (bst->state & 0x01)
584 seq_printf(seq, "charging state: discharging\n");
585 else if (bst->state & 0x02)
586 seq_printf(seq, "charging state: charging\n");
587 else {
588 seq_printf(seq, "charging state: charged\n");
591 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
592 seq_printf(seq, "present rate: unknown\n");
593 else
594 seq_printf(seq, "present rate: %d %s\n",
595 (u32) bst->present_rate, units);
597 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
598 seq_printf(seq, "remaining capacity: unknown\n");
599 else
600 seq_printf(seq, "remaining capacity: %d %sh\n",
601 (u32) bst->remaining_capacity, units);
603 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
604 seq_printf(seq, "present voltage: unknown\n");
605 else
606 seq_printf(seq, "present voltage: %d mV\n",
607 (u32) bst->present_voltage);
609 end:
611 if (result) {
612 seq_printf(seq, "ERROR: Unable to read battery state\n");
615 return result;
618 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
620 struct acpi_battery *battery = seq->private;
621 char *units = "?";
623 if (result)
624 goto end;
626 if (!acpi_battery_present(battery)) {
627 seq_printf(seq, "present: no\n");
628 goto end;
631 /* Battery Units */
633 units = acpi_battery_power_units(battery);
635 seq_printf(seq, "alarm: ");
636 if (!battery->alarm)
637 seq_printf(seq, "unsupported\n");
638 else
639 seq_printf(seq, "%lu %sh\n", battery->alarm, units);
641 end:
643 if (result)
644 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
646 return result;
649 static ssize_t
650 acpi_battery_write_alarm(struct file *file,
651 const char __user * buffer,
652 size_t count, loff_t * ppos)
654 int result = 0;
655 char alarm_string[12] = { '\0' };
656 struct seq_file *m = file->private_data;
657 struct acpi_battery *battery = m->private;
658 int update_result = ACPI_BATTERY_NONE_UPDATE;
660 if (!battery || (count > sizeof(alarm_string) - 1))
661 return -EINVAL;
663 result = acpi_battery_update(battery, 1, &update_result);
664 if (result) {
665 result = -ENODEV;
666 goto end;
669 if (!acpi_battery_present(battery)) {
670 result = -ENODEV;
671 goto end;
674 if (copy_from_user(alarm_string, buffer, count)) {
675 result = -EFAULT;
676 goto end;
679 alarm_string[count] = '\0';
681 result = acpi_battery_set_alarm(battery,
682 simple_strtoul(alarm_string, NULL, 0));
683 if (result)
684 goto end;
686 end:
688 acpi_battery_check_result(battery, result);
690 if (!result)
691 return count;
693 return result;
696 typedef int(*print_func)(struct seq_file *seq, int result);
697 typedef int(*get_func)(struct acpi_battery *battery);
699 static struct acpi_read_mux {
700 print_func print;
701 get_func get;
702 } acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
703 {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
704 {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
705 {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
708 static int acpi_battery_read(int fid, struct seq_file *seq)
710 struct acpi_battery *battery = seq->private;
711 int result = 0;
712 int update_result = ACPI_BATTERY_NONE_UPDATE;
713 int update = 0;
715 update = (get_seconds() - battery->update_time[fid] >= update_time);
716 update = (update | battery->flags.update[fid]);
718 result = acpi_battery_update(battery, update, &update_result);
719 if (result)
720 goto end;
722 if (update_result == ACPI_BATTERY_EASY_UPDATE) {
723 result = acpi_read_funcs[fid].get(battery);
724 if (result)
725 goto end;
728 end:
729 result = acpi_read_funcs[fid].print(seq, result);
730 acpi_battery_check_result(battery, result);
731 battery->flags.update[fid] = result;
732 return result;
735 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
737 return acpi_battery_read(ACPI_BATTERY_INFO, seq);
740 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
742 return acpi_battery_read(ACPI_BATTERY_STATE, seq);
745 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
747 return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
750 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
752 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
755 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
757 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
760 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
762 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
765 static struct battery_file {
766 struct file_operations ops;
767 mode_t mode;
768 char *name;
769 } acpi_battery_file[] = {
771 .name = "info",
772 .mode = S_IRUGO,
773 .ops = {
774 .open = acpi_battery_info_open_fs,
775 .read = seq_read,
776 .llseek = seq_lseek,
777 .release = single_release,
778 .owner = THIS_MODULE,
782 .name = "state",
783 .mode = S_IRUGO,
784 .ops = {
785 .open = acpi_battery_state_open_fs,
786 .read = seq_read,
787 .llseek = seq_lseek,
788 .release = single_release,
789 .owner = THIS_MODULE,
793 .name = "alarm",
794 .mode = S_IFREG | S_IRUGO | S_IWUSR,
795 .ops = {
796 .open = acpi_battery_alarm_open_fs,
797 .read = seq_read,
798 .write = acpi_battery_write_alarm,
799 .llseek = seq_lseek,
800 .release = single_release,
801 .owner = THIS_MODULE,
806 static int acpi_battery_add_fs(struct acpi_device *device)
808 struct proc_dir_entry *entry = NULL;
809 int i;
811 if (!acpi_device_dir(device)) {
812 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
813 acpi_battery_dir);
814 if (!acpi_device_dir(device))
815 return -ENODEV;
816 acpi_device_dir(device)->owner = THIS_MODULE;
819 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
820 entry = create_proc_entry(acpi_battery_file[i].name,
821 acpi_battery_file[i].mode, acpi_device_dir(device));
822 if (!entry)
823 return -ENODEV;
824 else {
825 entry->proc_fops = &acpi_battery_file[i].ops;
826 entry->data = acpi_driver_data(device);
827 entry->owner = THIS_MODULE;
831 return 0;
834 static int acpi_battery_remove_fs(struct acpi_device *device)
836 int i;
837 if (acpi_device_dir(device)) {
838 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
839 remove_proc_entry(acpi_battery_file[i].name,
840 acpi_device_dir(device));
842 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
843 acpi_device_dir(device) = NULL;
846 return 0;
849 /* --------------------------------------------------------------------------
850 Driver Interface
851 -------------------------------------------------------------------------- */
853 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
855 struct acpi_battery *battery = data;
856 struct acpi_device *device = NULL;
858 if (!battery)
859 return;
861 device = battery->device;
863 switch (event) {
864 case ACPI_BATTERY_NOTIFY_STATUS:
865 case ACPI_BATTERY_NOTIFY_INFO:
866 case ACPI_NOTIFY_BUS_CHECK:
867 case ACPI_NOTIFY_DEVICE_CHECK:
868 device = battery->device;
869 acpi_battery_notify_update(battery);
870 acpi_bus_generate_event(device, event,
871 acpi_battery_present(battery));
872 break;
873 default:
874 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
875 "Unsupported event [0x%x]\n", event));
876 break;
879 return;
882 static int acpi_battery_add(struct acpi_device *device)
884 int result = 0;
885 acpi_status status = 0;
886 struct acpi_battery *battery = NULL;
888 if (!device)
889 return -EINVAL;
891 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
892 if (!battery)
893 return -ENOMEM;
895 mutex_init(&battery->lock);
896 battery->device = device;
897 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
898 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
899 acpi_driver_data(device) = battery;
901 result = acpi_battery_get_status(battery);
902 if (result)
903 goto end;
905 battery->flags.init_update = 1;
907 result = acpi_battery_add_fs(device);
908 if (result)
909 goto end;
911 status = acpi_install_notify_handler(device->handle,
912 ACPI_ALL_NOTIFY,
913 acpi_battery_notify, battery);
914 if (ACPI_FAILURE(status)) {
915 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
916 result = -ENODEV;
917 goto end;
920 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
921 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
922 device->status.battery_present ? "present" : "absent");
924 end:
926 if (result) {
927 acpi_battery_remove_fs(device);
928 kfree(battery);
932 return result;
935 static int acpi_battery_remove(struct acpi_device *device, int type)
937 acpi_status status = 0;
938 struct acpi_battery *battery = NULL;
940 if (!device || !acpi_driver_data(device))
941 return -EINVAL;
943 battery = acpi_driver_data(device);
945 status = acpi_remove_notify_handler(device->handle,
946 ACPI_ALL_NOTIFY,
947 acpi_battery_notify);
949 acpi_battery_remove_fs(device);
951 kfree(battery->bif_data.pointer);
953 kfree(battery->bst_data.pointer);
955 mutex_destroy(&battery->lock);
957 kfree(battery);
959 return 0;
962 /* this is needed to learn about changes made in suspended state */
963 static int acpi_battery_resume(struct acpi_device *device)
965 struct acpi_battery *battery;
967 if (!device)
968 return -EINVAL;
970 battery = device->driver_data;
972 battery->flags.init_update = 1;
974 return 0;
977 static int __init acpi_battery_init(void)
979 int result;
981 if (acpi_disabled)
982 return -ENODEV;
984 acpi_battery_dir = acpi_lock_battery_dir();
985 if (!acpi_battery_dir)
986 return -ENODEV;
988 result = acpi_bus_register_driver(&acpi_battery_driver);
989 if (result < 0) {
990 acpi_unlock_battery_dir(acpi_battery_dir);
991 return -ENODEV;
994 return 0;
997 static void __exit acpi_battery_exit(void)
999 acpi_bus_unregister_driver(&acpi_battery_driver);
1001 acpi_unlock_battery_dir(acpi_battery_dir);
1003 return;
1006 module_init(acpi_battery_init);
1007 module_exit(acpi_battery_exit);