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 <asm/uaccess.h>
33 #include <acpi/acpi_bus.h>
34 #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_DRIVER_NAME "ACPI Battery Driver"
46 #define ACPI_BATTERY_DEVICE_NAME "Battery"
47 #define ACPI_BATTERY_FILE_INFO "info"
48 #define ACPI_BATTERY_FILE_STATUS "state"
49 #define ACPI_BATTERY_FILE_ALARM "alarm"
50 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
51 #define ACPI_BATTERY_NOTIFY_INFO 0x81
52 #define ACPI_BATTERY_UNITS_WATTS "mW"
53 #define ACPI_BATTERY_UNITS_AMPS "mA"
56 #define _COMPONENT ACPI_BATTERY_COMPONENT
57 ACPI_MODULE_NAME ("acpi_battery")
59 MODULE_AUTHOR("Paul Diefenbaugh");
60 MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME
);
61 MODULE_LICENSE("GPL");
63 static int acpi_battery_add (struct acpi_device
*device
);
64 static int acpi_battery_remove (struct acpi_device
*device
, int type
);
66 static struct acpi_driver acpi_battery_driver
= {
67 .name
= ACPI_BATTERY_DRIVER_NAME
,
68 .class = ACPI_BATTERY_CLASS
,
69 .ids
= ACPI_BATTERY_HID
,
71 .add
= acpi_battery_add
,
72 .remove
= acpi_battery_remove
,
76 struct acpi_battery_status
{
78 acpi_integer present_rate
;
79 acpi_integer remaining_capacity
;
80 acpi_integer present_voltage
;
83 struct acpi_battery_info
{
84 acpi_integer power_unit
;
85 acpi_integer design_capacity
;
86 acpi_integer last_full_capacity
;
87 acpi_integer battery_technology
;
88 acpi_integer design_voltage
;
89 acpi_integer design_capacity_warning
;
90 acpi_integer design_capacity_low
;
91 acpi_integer battery_capacity_granularity_1
;
92 acpi_integer battery_capacity_granularity_2
;
93 acpi_string model_number
;
94 acpi_string serial_number
;
95 acpi_string battery_type
;
99 struct acpi_battery_flags
{
100 u8 present
:1; /* Bay occupied? */
101 u8 power_unit
:1; /* 0=watts, 1=apms */
102 u8 alarm
:1; /* _BTP present? */
106 struct acpi_battery_trips
{
107 unsigned long warning
;
111 struct acpi_battery
{
113 struct acpi_battery_flags flags
;
114 struct acpi_battery_trips trips
;
116 struct acpi_battery_info
*info
;
120 /* --------------------------------------------------------------------------
122 -------------------------------------------------------------------------- */
125 acpi_battery_get_info (
126 struct acpi_battery
*battery
,
127 struct acpi_battery_info
**bif
)
130 acpi_status status
= 0;
131 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
132 struct acpi_buffer format
= {sizeof(ACPI_BATTERY_FORMAT_BIF
),
133 ACPI_BATTERY_FORMAT_BIF
};
134 struct acpi_buffer data
= {0, NULL
};
135 union acpi_object
*package
= NULL
;
137 ACPI_FUNCTION_TRACE("acpi_battery_get_info");
139 if (!battery
|| !bif
)
140 return_VALUE(-EINVAL
);
144 status
= acpi_evaluate_object(battery
->handle
, "_BIF", NULL
, &buffer
);
145 if (ACPI_FAILURE(status
)) {
146 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error evaluating _BIF\n"));
147 return_VALUE(-ENODEV
);
150 package
= (union acpi_object
*) buffer
.pointer
;
152 /* Extract Package Data */
154 status
= acpi_extract_package(package
, &format
, &data
);
155 if (status
!= AE_BUFFER_OVERFLOW
) {
156 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error extracting _BIF\n"));
161 data
.pointer
= kmalloc(data
.length
, GFP_KERNEL
);
166 memset(data
.pointer
, 0, data
.length
);
168 status
= acpi_extract_package(package
, &format
, &data
);
169 if (ACPI_FAILURE(status
)) {
170 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error extracting _BIF\n"));
177 acpi_os_free(buffer
.pointer
);
180 (*bif
) = (struct acpi_battery_info
*) data
.pointer
;
182 return_VALUE(result
);
186 acpi_battery_get_status (
187 struct acpi_battery
*battery
,
188 struct acpi_battery_status
**bst
)
191 acpi_status status
= 0;
192 struct acpi_buffer buffer
= {ACPI_ALLOCATE_BUFFER
, NULL
};
193 struct acpi_buffer format
= {sizeof(ACPI_BATTERY_FORMAT_BST
),
194 ACPI_BATTERY_FORMAT_BST
};
195 struct acpi_buffer data
= {0, NULL
};
196 union acpi_object
*package
= NULL
;
198 ACPI_FUNCTION_TRACE("acpi_battery_get_status");
200 if (!battery
|| !bst
)
201 return_VALUE(-EINVAL
);
205 status
= acpi_evaluate_object(battery
->handle
, "_BST", NULL
, &buffer
);
206 if (ACPI_FAILURE(status
)) {
207 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error evaluating _BST\n"));
208 return_VALUE(-ENODEV
);
211 package
= (union acpi_object
*) buffer
.pointer
;
213 /* Extract Package Data */
215 status
= acpi_extract_package(package
, &format
, &data
);
216 if (status
!= AE_BUFFER_OVERFLOW
) {
217 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error extracting _BST\n"));
222 data
.pointer
= kmalloc(data
.length
, GFP_KERNEL
);
227 memset(data
.pointer
, 0, data
.length
);
229 status
= acpi_extract_package(package
, &format
, &data
);
230 if (ACPI_FAILURE(status
)) {
231 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
, "Error extracting _BST\n"));
238 acpi_os_free(buffer
.pointer
);
241 (*bst
) = (struct acpi_battery_status
*) data
.pointer
;
243 return_VALUE(result
);
248 acpi_battery_set_alarm (
249 struct acpi_battery
*battery
,
252 acpi_status status
= 0;
253 union acpi_object arg0
= {ACPI_TYPE_INTEGER
};
254 struct acpi_object_list arg_list
= {1, &arg0
};
256 ACPI_FUNCTION_TRACE("acpi_battery_set_alarm");
259 return_VALUE(-EINVAL
);
261 if (!battery
->flags
.alarm
)
262 return_VALUE(-ENODEV
);
264 arg0
.integer
.value
= alarm
;
266 status
= acpi_evaluate_object(battery
->handle
, "_BTP", &arg_list
, NULL
);
267 if (ACPI_FAILURE(status
))
268 return_VALUE(-ENODEV
);
270 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", (u32
) alarm
));
272 battery
->alarm
= alarm
;
280 struct acpi_battery
*battery
)
283 acpi_status status
= AE_OK
;
284 acpi_handle handle
= NULL
;
285 struct acpi_device
*device
= NULL
;
286 struct acpi_battery_info
*bif
= NULL
;
288 ACPI_FUNCTION_TRACE("acpi_battery_check");
291 return_VALUE(-EINVAL
);
293 result
= acpi_bus_get_device(battery
->handle
, &device
);
295 return_VALUE(result
);
297 result
= acpi_bus_get_status(device
);
299 return_VALUE(result
);
303 if (!battery
->flags
.present
&& device
->status
.battery_present
) {
305 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Battery inserted\n"));
307 /* Evalute _BIF to get certain static information */
309 result
= acpi_battery_get_info(battery
, &bif
);
311 return_VALUE(result
);
313 battery
->flags
.power_unit
= bif
->power_unit
;
314 battery
->trips
.warning
= bif
->design_capacity_warning
;
315 battery
->trips
.low
= bif
->design_capacity_low
;
318 /* See if alarms are supported, and if so, set default */
320 status
= acpi_get_handle(battery
->handle
, "_BTP", &handle
);
321 if (ACPI_SUCCESS(status
)) {
322 battery
->flags
.alarm
= 1;
323 acpi_battery_set_alarm(battery
, battery
->trips
.warning
);
329 else if (battery
->flags
.present
&& !device
->status
.battery_present
) {
330 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Battery removed\n"));
333 battery
->flags
.present
= device
->status
.battery_present
;
335 return_VALUE(result
);
339 /* --------------------------------------------------------------------------
341 -------------------------------------------------------------------------- */
343 struct proc_dir_entry
*acpi_battery_dir
;
346 acpi_battery_read_info (
355 struct acpi_battery
*battery
= (struct acpi_battery
*) data
;
356 struct acpi_battery_info
*bif
= NULL
;
361 ACPI_FUNCTION_TRACE("acpi_battery_read_info");
363 if (!battery
|| (off
!= 0))
366 if (battery
->flags
.present
)
367 p
+= sprintf(p
, "present: yes\n");
369 p
+= sprintf(p
, "present: no\n");
373 /* Battery Info (_BIF) */
375 result
= acpi_battery_get_info(battery
, &bif
);
376 if (result
|| !bif
) {
377 p
+= sprintf(p
, "ERROR: Unable to read battery information\n");
381 units
= bif
->power_unit
? ACPI_BATTERY_UNITS_AMPS
: ACPI_BATTERY_UNITS_WATTS
;
383 if (bif
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
384 p
+= sprintf(p
, "design capacity: unknown\n");
386 p
+= sprintf(p
, "design capacity: %d %sh\n",
387 (u32
) bif
->design_capacity
, units
);
389 if (bif
->last_full_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
390 p
+= sprintf(p
, "last full capacity: unknown\n");
392 p
+= sprintf(p
, "last full capacity: %d %sh\n",
393 (u32
) bif
->last_full_capacity
, units
);
395 switch ((u32
) bif
->battery_technology
) {
397 p
+= sprintf(p
, "battery technology: non-rechargeable\n");
400 p
+= sprintf(p
, "battery technology: rechargeable\n");
403 p
+= sprintf(p
, "battery technology: unknown\n");
407 if (bif
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
408 p
+= sprintf(p
, "design voltage: unknown\n");
410 p
+= sprintf(p
, "design voltage: %d mV\n",
411 (u32
) bif
->design_voltage
);
413 p
+= sprintf(p
, "design capacity warning: %d %sh\n",
414 (u32
) bif
->design_capacity_warning
, units
);
415 p
+= sprintf(p
, "design capacity low: %d %sh\n",
416 (u32
) bif
->design_capacity_low
, units
);
417 p
+= sprintf(p
, "capacity granularity 1: %d %sh\n",
418 (u32
) bif
->battery_capacity_granularity_1
, units
);
419 p
+= sprintf(p
, "capacity granularity 2: %d %sh\n",
420 (u32
) bif
->battery_capacity_granularity_2
, units
);
421 p
+= sprintf(p
, "model number: %s\n",
423 p
+= sprintf(p
, "serial number: %s\n",
425 p
+= sprintf(p
, "battery type: %s\n",
427 p
+= sprintf(p
, "OEM info: %s\n",
434 if (len
<= off
+count
) *eof
= 1;
437 if (len
>count
) len
= count
;
445 acpi_battery_read_state (
454 struct acpi_battery
*battery
= (struct acpi_battery
*) data
;
455 struct acpi_battery_status
*bst
= NULL
;
460 ACPI_FUNCTION_TRACE("acpi_battery_read_state");
462 if (!battery
|| (off
!= 0))
465 if (battery
->flags
.present
)
466 p
+= sprintf(p
, "present: yes\n");
468 p
+= sprintf(p
, "present: no\n");
474 units
= battery
->flags
.power_unit
? ACPI_BATTERY_UNITS_AMPS
: ACPI_BATTERY_UNITS_WATTS
;
476 /* Battery Status (_BST) */
478 result
= acpi_battery_get_status(battery
, &bst
);
479 if (result
|| !bst
) {
480 p
+= sprintf(p
, "ERROR: Unable to read battery status\n");
484 if (!(bst
->state
& 0x04))
485 p
+= sprintf(p
, "capacity state: ok\n");
487 p
+= sprintf(p
, "capacity state: critical\n");
489 if ((bst
->state
& 0x01) && (bst
->state
& 0x02)){
490 p
+= sprintf(p
, "charging state: charging/discharging\n");
491 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR
,
492 "Battery Charging and Discharging?\n"));
494 else if (bst
->state
& 0x01)
495 p
+= sprintf(p
, "charging state: discharging\n");
496 else if (bst
->state
& 0x02)
497 p
+= sprintf(p
, "charging state: charging\n");
499 p
+= sprintf(p
, "charging state: charged\n");
502 if (bst
->present_rate
== ACPI_BATTERY_VALUE_UNKNOWN
)
503 p
+= sprintf(p
, "present rate: unknown\n");
505 p
+= sprintf(p
, "present rate: %d %s\n",
506 (u32
) bst
->present_rate
, units
);
508 if (bst
->remaining_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
509 p
+= sprintf(p
, "remaining capacity: unknown\n");
511 p
+= sprintf(p
, "remaining capacity: %d %sh\n",
512 (u32
) bst
->remaining_capacity
, units
);
514 if (bst
->present_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
515 p
+= sprintf(p
, "present voltage: unknown\n");
517 p
+= sprintf(p
, "present voltage: %d mV\n",
518 (u32
) bst
->present_voltage
);
524 if (len
<= off
+count
) *eof
= 1;
527 if (len
>count
) len
= count
;
535 acpi_battery_read_alarm (
543 struct acpi_battery
*battery
= (struct acpi_battery
*) data
;
548 ACPI_FUNCTION_TRACE("acpi_battery_read_alarm");
550 if (!battery
|| (off
!= 0))
553 if (!battery
->flags
.present
) {
554 p
+= sprintf(p
, "present: no\n");
560 units
= battery
->flags
.power_unit
? ACPI_BATTERY_UNITS_AMPS
: ACPI_BATTERY_UNITS_WATTS
;
564 p
+= sprintf(p
, "alarm: ");
566 p
+= sprintf(p
, "unsupported\n");
568 p
+= sprintf(p
, "%d %sh\n", (u32
) battery
->alarm
, units
);
572 if (len
<= off
+count
) *eof
= 1;
575 if (len
>count
) len
= count
;
583 acpi_battery_write_alarm (
585 const char __user
*buffer
,
590 struct acpi_battery
*battery
= (struct acpi_battery
*) data
;
591 char alarm_string
[12] = {'\0'};
593 ACPI_FUNCTION_TRACE("acpi_battery_write_alarm");
595 if (!battery
|| (count
> sizeof(alarm_string
) - 1))
596 return_VALUE(-EINVAL
);
598 if (!battery
->flags
.present
)
599 return_VALUE(-ENODEV
);
601 if (copy_from_user(alarm_string
, buffer
, count
))
602 return_VALUE(-EFAULT
);
604 alarm_string
[count
] = '\0';
606 result
= acpi_battery_set_alarm(battery
,
607 simple_strtoul(alarm_string
, NULL
, 0));
609 return_VALUE(result
);
616 acpi_battery_add_fs (
617 struct acpi_device
*device
)
619 struct proc_dir_entry
*entry
= NULL
;
621 ACPI_FUNCTION_TRACE("acpi_battery_add_fs");
623 if (!acpi_device_dir(device
)) {
624 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
626 if (!acpi_device_dir(device
))
627 return_VALUE(-ENODEV
);
628 acpi_device_dir(device
)->owner
= THIS_MODULE
;
632 entry
= create_proc_entry(ACPI_BATTERY_FILE_INFO
,
633 S_IRUGO
, acpi_device_dir(device
));
635 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
636 "Unable to create '%s' fs entry\n",
637 ACPI_BATTERY_FILE_INFO
));
639 entry
->read_proc
= acpi_battery_read_info
;
640 entry
->data
= acpi_driver_data(device
);
641 entry
->owner
= THIS_MODULE
;
645 entry
= create_proc_entry(ACPI_BATTERY_FILE_STATUS
,
646 S_IRUGO
, acpi_device_dir(device
));
648 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
649 "Unable to create '%s' fs entry\n",
650 ACPI_BATTERY_FILE_STATUS
));
652 entry
->read_proc
= acpi_battery_read_state
;
653 entry
->data
= acpi_driver_data(device
);
654 entry
->owner
= THIS_MODULE
;
658 entry
= create_proc_entry(ACPI_BATTERY_FILE_ALARM
,
659 S_IFREG
|S_IRUGO
|S_IWUSR
, acpi_device_dir(device
));
661 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
662 "Unable to create '%s' fs entry\n",
663 ACPI_BATTERY_FILE_ALARM
));
665 entry
->read_proc
= acpi_battery_read_alarm
;
666 entry
->write_proc
= acpi_battery_write_alarm
;
667 entry
->data
= acpi_driver_data(device
);
668 entry
->owner
= THIS_MODULE
;
676 acpi_battery_remove_fs (
677 struct acpi_device
*device
)
679 ACPI_FUNCTION_TRACE("acpi_battery_remove_fs");
681 if (acpi_device_dir(device
)) {
682 remove_proc_entry(ACPI_BATTERY_FILE_ALARM
,
683 acpi_device_dir(device
));
684 remove_proc_entry(ACPI_BATTERY_FILE_STATUS
,
685 acpi_device_dir(device
));
686 remove_proc_entry(ACPI_BATTERY_FILE_INFO
,
687 acpi_device_dir(device
));
689 remove_proc_entry(acpi_device_bid(device
), acpi_battery_dir
);
690 acpi_device_dir(device
) = NULL
;
697 /* --------------------------------------------------------------------------
699 -------------------------------------------------------------------------- */
702 acpi_battery_notify (
707 struct acpi_battery
*battery
= (struct acpi_battery
*) data
;
708 struct acpi_device
*device
= NULL
;
710 ACPI_FUNCTION_TRACE("acpi_battery_notify");
715 if (acpi_bus_get_device(handle
, &device
))
719 case ACPI_BATTERY_NOTIFY_STATUS
:
720 case ACPI_BATTERY_NOTIFY_INFO
:
721 acpi_battery_check(battery
);
722 acpi_bus_generate_event(device
, event
, battery
->flags
.present
);
725 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
726 "Unsupported event [0x%x]\n", event
));
736 struct acpi_device
*device
)
739 acpi_status status
= 0;
740 struct acpi_battery
*battery
= NULL
;
742 ACPI_FUNCTION_TRACE("acpi_battery_add");
745 return_VALUE(-EINVAL
);
747 battery
= kmalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
749 return_VALUE(-ENOMEM
);
750 memset(battery
, 0, sizeof(struct acpi_battery
));
752 battery
->handle
= device
->handle
;
753 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
754 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
755 acpi_driver_data(device
) = battery
;
757 result
= acpi_battery_check(battery
);
761 result
= acpi_battery_add_fs(device
);
765 status
= acpi_install_notify_handler(battery
->handle
,
766 ACPI_DEVICE_NOTIFY
, acpi_battery_notify
, battery
);
767 if (ACPI_FAILURE(status
)) {
768 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
769 "Error installing notify handler\n"));
774 printk(KERN_INFO PREFIX
"%s Slot [%s] (battery %s)\n",
775 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
776 device
->status
.battery_present
?"present":"absent");
780 acpi_battery_remove_fs(device
);
784 return_VALUE(result
);
789 acpi_battery_remove (
790 struct acpi_device
*device
,
793 acpi_status status
= 0;
794 struct acpi_battery
*battery
= NULL
;
796 ACPI_FUNCTION_TRACE("acpi_battery_remove");
798 if (!device
|| !acpi_driver_data(device
))
799 return_VALUE(-EINVAL
);
801 battery
= (struct acpi_battery
*) acpi_driver_data(device
);
803 status
= acpi_remove_notify_handler(battery
->handle
,
804 ACPI_DEVICE_NOTIFY
, acpi_battery_notify
);
805 if (ACPI_FAILURE(status
))
806 ACPI_DEBUG_PRINT((ACPI_DB_ERROR
,
807 "Error removing notify handler\n"));
809 acpi_battery_remove_fs(device
);
818 acpi_battery_init (void)
822 ACPI_FUNCTION_TRACE("acpi_battery_init");
824 acpi_battery_dir
= proc_mkdir(ACPI_BATTERY_CLASS
, acpi_root_dir
);
825 if (!acpi_battery_dir
)
826 return_VALUE(-ENODEV
);
827 acpi_battery_dir
->owner
= THIS_MODULE
;
829 result
= acpi_bus_register_driver(&acpi_battery_driver
);
831 remove_proc_entry(ACPI_BATTERY_CLASS
, acpi_root_dir
);
832 return_VALUE(-ENODEV
);
840 acpi_battery_exit (void)
842 ACPI_FUNCTION_TRACE("acpi_battery_exit");
844 acpi_bus_unregister_driver(&acpi_battery_driver
);
846 remove_proc_entry(ACPI_BATTERY_CLASS
, acpi_root_dir
);
852 module_init(acpi_battery_init
);
853 module_exit(acpi_battery_exit
);