drm: ioremap return value checks
[usb.git] / drivers / acpi / battery.c
blobcad932de383d02c3f3426f3784a9ff2190a1e4b2
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_HID "PNP0C0A"
45 #define ACPI_BATTERY_DEVICE_NAME "Battery"
46 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
47 #define ACPI_BATTERY_NOTIFY_INFO 0x81
48 #define ACPI_BATTERY_UNITS_WATTS "mW"
49 #define ACPI_BATTERY_UNITS_AMPS "mA"
51 #define _COMPONENT ACPI_BATTERY_COMPONENT
53 #define ACPI_BATTERY_UPDATE_TIME 0
55 #define ACPI_BATTERY_NONE_UPDATE 0
56 #define ACPI_BATTERY_EASY_UPDATE 1
57 #define ACPI_BATTERY_INIT_UPDATE 2
59 ACPI_MODULE_NAME("battery");
61 MODULE_AUTHOR("Paul Diefenbaugh");
62 MODULE_DESCRIPTION("ACPI Battery Driver");
63 MODULE_LICENSE("GPL");
65 static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME;
67 /* 0 - every time, > 0 - by update_time */
68 module_param(update_time, uint, 0644);
70 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
71 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
73 static int acpi_battery_add(struct acpi_device *device);
74 static int acpi_battery_remove(struct acpi_device *device, int type);
75 static int acpi_battery_resume(struct acpi_device *device);
77 static struct acpi_driver acpi_battery_driver = {
78 .name = "battery",
79 .class = ACPI_BATTERY_CLASS,
80 .ids = ACPI_BATTERY_HID,
81 .ops = {
82 .add = acpi_battery_add,
83 .resume = acpi_battery_resume,
84 .remove = acpi_battery_remove,
88 struct acpi_battery_state {
89 acpi_integer state;
90 acpi_integer present_rate;
91 acpi_integer remaining_capacity;
92 acpi_integer present_voltage;
95 struct acpi_battery_info {
96 acpi_integer power_unit;
97 acpi_integer design_capacity;
98 acpi_integer last_full_capacity;
99 acpi_integer battery_technology;
100 acpi_integer design_voltage;
101 acpi_integer design_capacity_warning;
102 acpi_integer design_capacity_low;
103 acpi_integer battery_capacity_granularity_1;
104 acpi_integer battery_capacity_granularity_2;
105 acpi_string model_number;
106 acpi_string serial_number;
107 acpi_string battery_type;
108 acpi_string oem_info;
111 enum acpi_battery_files{
112 ACPI_BATTERY_INFO = 0,
113 ACPI_BATTERY_STATE,
114 ACPI_BATTERY_ALARM,
115 ACPI_BATTERY_NUMFILES,
118 struct acpi_battery_flags {
119 u8 battery_present_prev;
120 u8 alarm_present;
121 u8 init_update;
122 u8 update[ACPI_BATTERY_NUMFILES];
123 u8 power_unit;
126 struct acpi_battery {
127 struct mutex mutex;
128 struct acpi_device *device;
129 struct acpi_battery_flags flags;
130 struct acpi_buffer bif_data;
131 struct acpi_buffer bst_data;
132 unsigned long alarm;
133 unsigned long update_time[ACPI_BATTERY_NUMFILES];
136 inline int acpi_battery_present(struct acpi_battery *battery)
138 return battery->device->status.battery_present;
140 inline char *acpi_battery_power_units(struct acpi_battery *battery)
142 if (battery->flags.power_unit)
143 return ACPI_BATTERY_UNITS_AMPS;
144 else
145 return ACPI_BATTERY_UNITS_WATTS;
148 inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
150 return battery->device->handle;
153 /* --------------------------------------------------------------------------
154 Battery Management
155 -------------------------------------------------------------------------- */
157 static void acpi_battery_check_result(struct acpi_battery *battery, int result)
159 if (!battery)
160 return;
162 if (result) {
163 battery->flags.init_update = 1;
167 static int acpi_battery_extract_package(struct acpi_battery *battery,
168 union acpi_object *package,
169 struct acpi_buffer *format,
170 struct acpi_buffer *data,
171 char *package_name)
173 acpi_status status = AE_OK;
174 struct acpi_buffer data_null = { 0, NULL };
176 status = acpi_extract_package(package, format, &data_null);
177 if (status != AE_BUFFER_OVERFLOW) {
178 ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s",
179 package_name));
180 return -ENODEV;
183 if (data_null.length != data->length) {
184 kfree(data->pointer);
185 data->pointer = kzalloc(data_null.length, GFP_KERNEL);
186 if (!data->pointer) {
187 ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()"));
188 return -ENOMEM;
190 data->length = data_null.length;
193 status = acpi_extract_package(package, format, data);
194 if (ACPI_FAILURE(status)) {
195 ACPI_EXCEPTION((AE_INFO, status, "Extracting %s",
196 package_name));
197 return -ENODEV;
200 return 0;
203 static int acpi_battery_get_status(struct acpi_battery *battery)
205 int result = 0;
207 result = acpi_bus_get_status(battery->device);
208 if (result) {
209 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
210 return -ENODEV;
212 return result;
215 static int acpi_battery_get_info(struct acpi_battery *battery)
217 int result = 0;
218 acpi_status status = 0;
219 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
220 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
221 ACPI_BATTERY_FORMAT_BIF
223 union acpi_object *package = NULL;
224 struct acpi_buffer *data = NULL;
225 struct acpi_battery_info *bif = NULL;
227 battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
229 if (!acpi_battery_present(battery))
230 return 0;
232 /* Evaluate _BIF */
234 status =
235 acpi_evaluate_object(acpi_battery_handle(battery), "_BIF", NULL,
236 &buffer);
237 if (ACPI_FAILURE(status)) {
238 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
239 return -ENODEV;
242 package = buffer.pointer;
244 data = &battery->bif_data;
246 /* Extract Package Data */
248 result =
249 acpi_battery_extract_package(battery, package, &format, data,
250 "_BIF");
251 if (result)
252 goto end;
254 end:
256 kfree(buffer.pointer);
258 if (!result) {
259 bif = data->pointer;
260 battery->flags.power_unit = bif->power_unit;
263 return result;
266 static int acpi_battery_get_state(struct acpi_battery *battery)
268 int result = 0;
269 acpi_status status = 0;
270 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
271 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
272 ACPI_BATTERY_FORMAT_BST
274 union acpi_object *package = NULL;
275 struct acpi_buffer *data = NULL;
277 battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
279 if (!acpi_battery_present(battery))
280 return 0;
282 /* Evaluate _BST */
284 status =
285 acpi_evaluate_object(acpi_battery_handle(battery), "_BST", NULL,
286 &buffer);
287 if (ACPI_FAILURE(status)) {
288 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
289 return -ENODEV;
292 package = buffer.pointer;
294 data = &battery->bst_data;
296 /* Extract Package Data */
298 result =
299 acpi_battery_extract_package(battery, package, &format, data,
300 "_BST");
301 if (result)
302 goto end;
304 end:
305 kfree(buffer.pointer);
307 return result;
310 static int acpi_battery_get_alarm(struct acpi_battery *battery)
312 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
314 return 0;
317 static int acpi_battery_set_alarm(struct acpi_battery *battery,
318 unsigned long alarm)
320 acpi_status status = 0;
321 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
322 struct acpi_object_list arg_list = { 1, &arg0 };
324 battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
326 if (!acpi_battery_present(battery))
327 return -ENODEV;
329 if (!battery->flags.alarm_present)
330 return -ENODEV;
332 arg0.integer.value = alarm;
334 status =
335 acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
336 &arg_list, NULL);
337 if (ACPI_FAILURE(status))
338 return -ENODEV;
340 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
342 battery->alarm = alarm;
344 return 0;
347 static int acpi_battery_init_alarm(struct acpi_battery *battery)
349 int result = 0;
350 acpi_status status = AE_OK;
351 acpi_handle handle = NULL;
352 struct acpi_battery_info *bif = battery->bif_data.pointer;
353 unsigned long alarm = battery->alarm;
355 /* See if alarms are supported, and if so, set default */
357 status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
358 if (ACPI_SUCCESS(status)) {
359 battery->flags.alarm_present = 1;
360 if (!alarm && bif) {
361 alarm = bif->design_capacity_warning;
363 result = acpi_battery_set_alarm(battery, alarm);
364 if (result)
365 goto end;
366 } else {
367 battery->flags.alarm_present = 0;
370 end:
372 return result;
375 static int acpi_battery_init_update(struct acpi_battery *battery)
377 int result = 0;
379 result = acpi_battery_get_status(battery);
380 if (result)
381 return result;
383 battery->flags.battery_present_prev = acpi_battery_present(battery);
385 if (acpi_battery_present(battery)) {
386 result = acpi_battery_get_info(battery);
387 if (result)
388 return result;
389 result = acpi_battery_get_state(battery);
390 if (result)
391 return result;
393 acpi_battery_init_alarm(battery);
396 return result;
399 static int acpi_battery_update(struct acpi_battery *battery,
400 int update, int *update_result_ptr)
402 int result = 0;
403 int update_result = ACPI_BATTERY_NONE_UPDATE;
405 if (!acpi_battery_present(battery)) {
406 update = 1;
409 if (battery->flags.init_update) {
410 result = acpi_battery_init_update(battery);
411 if (result)
412 goto end;
413 update_result = ACPI_BATTERY_INIT_UPDATE;
414 } else if (update) {
415 result = acpi_battery_get_status(battery);
416 if (result)
417 goto end;
418 if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
419 || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
420 result = acpi_battery_init_update(battery);
421 if (result)
422 goto end;
423 update_result = ACPI_BATTERY_INIT_UPDATE;
424 } else {
425 update_result = ACPI_BATTERY_EASY_UPDATE;
429 end:
431 battery->flags.init_update = (result != 0);
433 *update_result_ptr = update_result;
435 return result;
438 static void acpi_battery_notify_update(struct acpi_battery *battery)
440 acpi_battery_get_status(battery);
442 if (battery->flags.init_update) {
443 return;
446 if ((!battery->flags.battery_present_prev &
447 acpi_battery_present(battery)) ||
448 (battery->flags.battery_present_prev &
449 !acpi_battery_present(battery))) {
450 battery->flags.init_update = 1;
451 } else {
452 battery->flags.update[ACPI_BATTERY_INFO] = 1;
453 battery->flags.update[ACPI_BATTERY_STATE] = 1;
454 battery->flags.update[ACPI_BATTERY_ALARM] = 1;
458 /* --------------------------------------------------------------------------
459 FS Interface (/proc)
460 -------------------------------------------------------------------------- */
462 static struct proc_dir_entry *acpi_battery_dir;
464 static int acpi_battery_print_info(struct seq_file *seq, int result)
466 struct acpi_battery *battery = seq->private;
467 struct acpi_battery_info *bif = NULL;
468 char *units = "?";
470 if (result)
471 goto end;
473 if (acpi_battery_present(battery))
474 seq_printf(seq, "present: yes\n");
475 else {
476 seq_printf(seq, "present: no\n");
477 goto end;
480 bif = battery->bif_data.pointer;
481 if (!bif) {
482 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
483 result = -ENODEV;
484 goto end;
487 /* Battery Units */
489 units = acpi_battery_power_units(battery);
491 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
492 seq_printf(seq, "design capacity: unknown\n");
493 else
494 seq_printf(seq, "design capacity: %d %sh\n",
495 (u32) bif->design_capacity, units);
497 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
498 seq_printf(seq, "last full capacity: unknown\n");
499 else
500 seq_printf(seq, "last full capacity: %d %sh\n",
501 (u32) bif->last_full_capacity, units);
503 switch ((u32) bif->battery_technology) {
504 case 0:
505 seq_printf(seq, "battery technology: non-rechargeable\n");
506 break;
507 case 1:
508 seq_printf(seq, "battery technology: rechargeable\n");
509 break;
510 default:
511 seq_printf(seq, "battery technology: unknown\n");
512 break;
515 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
516 seq_printf(seq, "design voltage: unknown\n");
517 else
518 seq_printf(seq, "design voltage: %d mV\n",
519 (u32) bif->design_voltage);
520 seq_printf(seq, "design capacity warning: %d %sh\n",
521 (u32) bif->design_capacity_warning, units);
522 seq_printf(seq, "design capacity low: %d %sh\n",
523 (u32) bif->design_capacity_low, units);
524 seq_printf(seq, "capacity granularity 1: %d %sh\n",
525 (u32) bif->battery_capacity_granularity_1, units);
526 seq_printf(seq, "capacity granularity 2: %d %sh\n",
527 (u32) bif->battery_capacity_granularity_2, units);
528 seq_printf(seq, "model number: %s\n", bif->model_number);
529 seq_printf(seq, "serial number: %s\n", bif->serial_number);
530 seq_printf(seq, "battery type: %s\n", bif->battery_type);
531 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
533 end:
535 if (result)
536 seq_printf(seq, "ERROR: Unable to read battery info\n");
538 return result;
541 static int acpi_battery_print_state(struct seq_file *seq, int result)
543 struct acpi_battery *battery = seq->private;
544 struct acpi_battery_state *bst = NULL;
545 char *units = "?";
547 if (result)
548 goto end;
550 if (acpi_battery_present(battery))
551 seq_printf(seq, "present: yes\n");
552 else {
553 seq_printf(seq, "present: no\n");
554 goto end;
557 bst = battery->bst_data.pointer;
558 if (!bst) {
559 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
560 result = -ENODEV;
561 goto end;
564 /* Battery Units */
566 units = acpi_battery_power_units(battery);
568 if (!(bst->state & 0x04))
569 seq_printf(seq, "capacity state: ok\n");
570 else
571 seq_printf(seq, "capacity state: critical\n");
573 if ((bst->state & 0x01) && (bst->state & 0x02)) {
574 seq_printf(seq,
575 "charging state: charging/discharging\n");
576 } else if (bst->state & 0x01)
577 seq_printf(seq, "charging state: discharging\n");
578 else if (bst->state & 0x02)
579 seq_printf(seq, "charging state: charging\n");
580 else {
581 seq_printf(seq, "charging state: charged\n");
584 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
585 seq_printf(seq, "present rate: unknown\n");
586 else
587 seq_printf(seq, "present rate: %d %s\n",
588 (u32) bst->present_rate, units);
590 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
591 seq_printf(seq, "remaining capacity: unknown\n");
592 else
593 seq_printf(seq, "remaining capacity: %d %sh\n",
594 (u32) bst->remaining_capacity, units);
596 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
597 seq_printf(seq, "present voltage: unknown\n");
598 else
599 seq_printf(seq, "present voltage: %d mV\n",
600 (u32) bst->present_voltage);
602 end:
604 if (result) {
605 seq_printf(seq, "ERROR: Unable to read battery state\n");
608 return result;
611 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
613 struct acpi_battery *battery = seq->private;
614 char *units = "?";
616 if (result)
617 goto end;
619 if (!acpi_battery_present(battery)) {
620 seq_printf(seq, "present: no\n");
621 goto end;
624 /* Battery Units */
626 units = acpi_battery_power_units(battery);
628 seq_printf(seq, "alarm: ");
629 if (!battery->alarm)
630 seq_printf(seq, "unsupported\n");
631 else
632 seq_printf(seq, "%lu %sh\n", battery->alarm, units);
634 end:
636 if (result)
637 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
639 return result;
642 static ssize_t
643 acpi_battery_write_alarm(struct file *file,
644 const char __user * buffer,
645 size_t count, loff_t * ppos)
647 int result = 0;
648 char alarm_string[12] = { '\0' };
649 struct seq_file *m = file->private_data;
650 struct acpi_battery *battery = m->private;
651 int update_result = ACPI_BATTERY_NONE_UPDATE;
653 if (!battery || (count > sizeof(alarm_string) - 1))
654 return -EINVAL;
656 mutex_lock(&battery->mutex);
658 result = acpi_battery_update(battery, 1, &update_result);
659 if (result) {
660 result = -ENODEV;
661 goto end;
664 if (!acpi_battery_present(battery)) {
665 result = -ENODEV;
666 goto end;
669 if (copy_from_user(alarm_string, buffer, count)) {
670 result = -EFAULT;
671 goto end;
674 alarm_string[count] = '\0';
676 result = acpi_battery_set_alarm(battery,
677 simple_strtoul(alarm_string, NULL, 0));
678 if (result)
679 goto end;
681 end:
683 acpi_battery_check_result(battery, result);
685 if (!result)
686 result = count;
688 mutex_unlock(&battery->mutex);
690 return result;
693 typedef int(*print_func)(struct seq_file *seq, int result);
694 typedef int(*get_func)(struct acpi_battery *battery);
696 static struct acpi_read_mux {
697 print_func print;
698 get_func get;
699 } acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
700 {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
701 {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
702 {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
705 static int acpi_battery_read(int fid, struct seq_file *seq)
707 struct acpi_battery *battery = seq->private;
708 int result = 0;
709 int update_result = ACPI_BATTERY_NONE_UPDATE;
710 int update = 0;
712 mutex_lock(&battery->mutex);
714 update = (get_seconds() - battery->update_time[fid] >= update_time);
715 update = (update | battery->flags.update[fid]);
717 result = acpi_battery_update(battery, update, &update_result);
718 if (result)
719 goto end;
721 if (update_result == ACPI_BATTERY_EASY_UPDATE) {
722 result = acpi_read_funcs[fid].get(battery);
723 if (result)
724 goto end;
727 end:
728 result = acpi_read_funcs[fid].print(seq, result);
729 acpi_battery_check_result(battery, result);
730 battery->flags.update[fid] = result;
731 mutex_unlock(&battery->mutex);
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->mutex);
897 mutex_lock(&battery->mutex);
899 battery->device = device;
900 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
901 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
902 acpi_driver_data(device) = battery;
904 result = acpi_battery_get_status(battery);
905 if (result)
906 goto end;
908 battery->flags.init_update = 1;
910 result = acpi_battery_add_fs(device);
911 if (result)
912 goto end;
914 status = acpi_install_notify_handler(device->handle,
915 ACPI_ALL_NOTIFY,
916 acpi_battery_notify, battery);
917 if (ACPI_FAILURE(status)) {
918 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
919 result = -ENODEV;
920 goto end;
923 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
924 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
925 device->status.battery_present ? "present" : "absent");
927 end:
929 if (result) {
930 acpi_battery_remove_fs(device);
931 kfree(battery);
934 mutex_unlock(&battery->mutex);
936 return result;
939 static int acpi_battery_remove(struct acpi_device *device, int type)
941 acpi_status status = 0;
942 struct acpi_battery *battery = NULL;
944 if (!device || !acpi_driver_data(device))
945 return -EINVAL;
947 battery = acpi_driver_data(device);
949 mutex_lock(&battery->mutex);
951 status = acpi_remove_notify_handler(device->handle,
952 ACPI_ALL_NOTIFY,
953 acpi_battery_notify);
955 acpi_battery_remove_fs(device);
957 kfree(battery->bif_data.pointer);
959 kfree(battery->bst_data.pointer);
961 mutex_unlock(&battery->mutex);
963 mutex_destroy(&battery->mutex);
965 kfree(battery);
967 return 0;
970 /* this is needed to learn about changes made in suspended state */
971 static int acpi_battery_resume(struct acpi_device *device)
973 struct acpi_battery *battery;
975 if (!device)
976 return -EINVAL;
978 battery = device->driver_data;
980 battery->flags.init_update = 1;
982 return 0;
985 static int __init acpi_battery_init(void)
987 int result;
989 if (acpi_disabled)
990 return -ENODEV;
992 acpi_battery_dir = acpi_lock_battery_dir();
993 if (!acpi_battery_dir)
994 return -ENODEV;
996 result = acpi_bus_register_driver(&acpi_battery_driver);
997 if (result < 0) {
998 acpi_unlock_battery_dir(acpi_battery_dir);
999 return -ENODEV;
1002 return 0;
1005 static void __exit acpi_battery_exit(void)
1007 acpi_bus_unregister_driver(&acpi_battery_driver);
1009 acpi_unlock_battery_dir(acpi_battery_dir);
1011 return;
1014 module_init(acpi_battery_init);
1015 module_exit(acpi_battery_exit);