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_FILE_INFO "info"
47 #define ACPI_BATTERY_FILE_STATUS "state"
48 #define ACPI_BATTERY_FILE_ALARM "alarm"
49 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
50 #define ACPI_BATTERY_NOTIFY_INFO 0x81
51 #define ACPI_BATTERY_UNITS_WATTS "mW"
52 #define ACPI_BATTERY_UNITS_AMPS "mA"
54 #define _COMPONENT ACPI_BATTERY_COMPONENT
55 ACPI_MODULE_NAME("battery");
57 MODULE_AUTHOR("Paul Diefenbaugh");
58 MODULE_DESCRIPTION("ACPI Battery Driver");
59 MODULE_LICENSE("GPL");
61 extern struct proc_dir_entry
*acpi_lock_battery_dir(void);
62 extern void *acpi_unlock_battery_dir(struct proc_dir_entry
*acpi_battery_dir
);
64 static int acpi_battery_add(struct acpi_device
*device
);
65 static int acpi_battery_remove(struct acpi_device
*device
, int type
);
66 static int acpi_battery_resume(struct acpi_device
*device
);
68 static struct acpi_driver acpi_battery_driver
= {
70 .class = ACPI_BATTERY_CLASS
,
71 .ids
= ACPI_BATTERY_HID
,
73 .add
= acpi_battery_add
,
74 .resume
= acpi_battery_resume
,
75 .remove
= acpi_battery_remove
,
79 struct acpi_battery_status
{
81 acpi_integer present_rate
;
82 acpi_integer remaining_capacity
;
83 acpi_integer present_voltage
;
86 struct acpi_battery_info
{
87 acpi_integer power_unit
;
88 acpi_integer design_capacity
;
89 acpi_integer last_full_capacity
;
90 acpi_integer battery_technology
;
91 acpi_integer design_voltage
;
92 acpi_integer design_capacity_warning
;
93 acpi_integer design_capacity_low
;
94 acpi_integer battery_capacity_granularity_1
;
95 acpi_integer battery_capacity_granularity_2
;
96 acpi_string model_number
;
97 acpi_string serial_number
;
98 acpi_string battery_type
;
102 struct acpi_battery_flags
{
103 u8 present
:1; /* Bay occupied? */
104 u8 power_unit
:1; /* 0=watts, 1=apms */
105 u8 alarm
:1; /* _BTP present? */
109 struct acpi_battery_trips
{
110 unsigned long warning
;
114 struct acpi_battery
{
115 struct acpi_device
* device
;
116 struct acpi_battery_flags flags
;
117 struct acpi_battery_trips trips
;
119 struct acpi_battery_info
*info
;
122 /* --------------------------------------------------------------------------
124 -------------------------------------------------------------------------- */
127 acpi_battery_get_info(struct acpi_battery
*battery
,
128 struct acpi_battery_info
**bif
)
131 acpi_status status
= 0;
132 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
133 struct acpi_buffer format
= { sizeof(ACPI_BATTERY_FORMAT_BIF
),
134 ACPI_BATTERY_FORMAT_BIF
136 struct acpi_buffer data
= { 0, NULL
};
137 union acpi_object
*package
= NULL
;
140 if (!battery
|| !bif
)
145 status
= acpi_evaluate_object(battery
->device
->handle
, "_BIF", NULL
, &buffer
);
146 if (ACPI_FAILURE(status
)) {
147 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BIF"));
151 package
= buffer
.pointer
;
153 /* Extract Package Data */
155 status
= acpi_extract_package(package
, &format
, &data
);
156 if (status
!= AE_BUFFER_OVERFLOW
) {
157 ACPI_EXCEPTION((AE_INFO
, status
, "Extracting _BIF"));
162 data
.pointer
= kzalloc(data
.length
, GFP_KERNEL
);
168 status
= acpi_extract_package(package
, &format
, &data
);
169 if (ACPI_FAILURE(status
)) {
170 ACPI_EXCEPTION((AE_INFO
, status
, "Extracting _BIF"));
177 kfree(buffer
.pointer
);
180 (*bif
) = data
.pointer
;
186 acpi_battery_get_status(struct acpi_battery
*battery
,
187 struct acpi_battery_status
**bst
)
190 acpi_status status
= 0;
191 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
192 struct acpi_buffer format
= { sizeof(ACPI_BATTERY_FORMAT_BST
),
193 ACPI_BATTERY_FORMAT_BST
195 struct acpi_buffer data
= { 0, NULL
};
196 union acpi_object
*package
= NULL
;
199 if (!battery
|| !bst
)
204 status
= acpi_evaluate_object(battery
->device
->handle
, "_BST", NULL
, &buffer
);
205 if (ACPI_FAILURE(status
)) {
206 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BST"));
210 package
= buffer
.pointer
;
212 /* Extract Package Data */
214 status
= acpi_extract_package(package
, &format
, &data
);
215 if (status
!= AE_BUFFER_OVERFLOW
) {
216 ACPI_EXCEPTION((AE_INFO
, status
, "Extracting _BST"));
221 data
.pointer
= kzalloc(data
.length
, GFP_KERNEL
);
227 status
= acpi_extract_package(package
, &format
, &data
);
228 if (ACPI_FAILURE(status
)) {
229 ACPI_EXCEPTION((AE_INFO
, status
, "Extracting _BST"));
236 kfree(buffer
.pointer
);
239 (*bst
) = data
.pointer
;
245 acpi_battery_set_alarm(struct acpi_battery
*battery
, unsigned long alarm
)
247 acpi_status status
= 0;
248 union acpi_object arg0
= { ACPI_TYPE_INTEGER
};
249 struct acpi_object_list arg_list
= { 1, &arg0
};
255 if (!battery
->flags
.alarm
)
258 arg0
.integer
.value
= alarm
;
260 status
= acpi_evaluate_object(battery
->device
->handle
, "_BTP", &arg_list
, NULL
);
261 if (ACPI_FAILURE(status
))
264 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", (u32
) alarm
));
266 battery
->alarm
= alarm
;
271 static int acpi_battery_check(struct acpi_battery
*battery
)
274 acpi_status status
= AE_OK
;
275 acpi_handle handle
= NULL
;
276 struct acpi_device
*device
= NULL
;
277 struct acpi_battery_info
*bif
= NULL
;
283 device
= battery
->device
;
285 result
= acpi_bus_get_status(device
);
291 if (!battery
->flags
.present
&& device
->status
.battery_present
) {
293 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Battery inserted\n"));
295 /* Evalute _BIF to get certain static information */
297 result
= acpi_battery_get_info(battery
, &bif
);
301 battery
->flags
.power_unit
= bif
->power_unit
;
302 battery
->trips
.warning
= bif
->design_capacity_warning
;
303 battery
->trips
.low
= bif
->design_capacity_low
;
306 /* See if alarms are supported, and if so, set default */
308 status
= acpi_get_handle(battery
->device
->handle
, "_BTP", &handle
);
309 if (ACPI_SUCCESS(status
)) {
310 battery
->flags
.alarm
= 1;
311 acpi_battery_set_alarm(battery
, battery
->trips
.warning
);
317 else if (battery
->flags
.present
&& !device
->status
.battery_present
) {
318 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Battery removed\n"));
321 battery
->flags
.present
= device
->status
.battery_present
;
326 static void acpi_battery_check_present(struct acpi_battery
*battery
)
328 if (!battery
->flags
.present
) {
329 acpi_battery_check(battery
);
333 /* --------------------------------------------------------------------------
335 -------------------------------------------------------------------------- */
337 static struct proc_dir_entry
*acpi_battery_dir
;
338 static int acpi_battery_read_info(struct seq_file
*seq
, void *offset
)
341 struct acpi_battery
*battery
= seq
->private;
342 struct acpi_battery_info
*bif
= NULL
;
349 acpi_battery_check_present(battery
);
351 if (battery
->flags
.present
)
352 seq_printf(seq
, "present: yes\n");
354 seq_printf(seq
, "present: no\n");
358 /* Battery Info (_BIF) */
360 result
= acpi_battery_get_info(battery
, &bif
);
361 if (result
|| !bif
) {
362 seq_printf(seq
, "ERROR: Unable to read battery information\n");
368 power_unit
? ACPI_BATTERY_UNITS_AMPS
: ACPI_BATTERY_UNITS_WATTS
;
370 if (bif
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
371 seq_printf(seq
, "design capacity: unknown\n");
373 seq_printf(seq
, "design capacity: %d %sh\n",
374 (u32
) bif
->design_capacity
, units
);
376 if (bif
->last_full_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
377 seq_printf(seq
, "last full capacity: unknown\n");
379 seq_printf(seq
, "last full capacity: %d %sh\n",
380 (u32
) bif
->last_full_capacity
, units
);
382 switch ((u32
) bif
->battery_technology
) {
384 seq_printf(seq
, "battery technology: non-rechargeable\n");
387 seq_printf(seq
, "battery technology: rechargeable\n");
390 seq_printf(seq
, "battery technology: unknown\n");
394 if (bif
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
395 seq_printf(seq
, "design voltage: unknown\n");
397 seq_printf(seq
, "design voltage: %d mV\n",
398 (u32
) bif
->design_voltage
);
400 seq_printf(seq
, "design capacity warning: %d %sh\n",
401 (u32
) bif
->design_capacity_warning
, units
);
402 seq_printf(seq
, "design capacity low: %d %sh\n",
403 (u32
) bif
->design_capacity_low
, units
);
404 seq_printf(seq
, "capacity granularity 1: %d %sh\n",
405 (u32
) bif
->battery_capacity_granularity_1
, units
);
406 seq_printf(seq
, "capacity granularity 2: %d %sh\n",
407 (u32
) bif
->battery_capacity_granularity_2
, units
);
408 seq_printf(seq
, "model number: %s\n", bif
->model_number
);
409 seq_printf(seq
, "serial number: %s\n", bif
->serial_number
);
410 seq_printf(seq
, "battery type: %s\n", bif
->battery_type
);
411 seq_printf(seq
, "OEM info: %s\n", bif
->oem_info
);
419 static int acpi_battery_info_open_fs(struct inode
*inode
, struct file
*file
)
421 return single_open(file
, acpi_battery_read_info
, PDE(inode
)->data
);
424 static int acpi_battery_read_state(struct seq_file
*seq
, void *offset
)
427 struct acpi_battery
*battery
= seq
->private;
428 struct acpi_battery_status
*bst
= NULL
;
435 acpi_battery_check_present(battery
);
437 if (battery
->flags
.present
)
438 seq_printf(seq
, "present: yes\n");
440 seq_printf(seq
, "present: no\n");
448 power_unit
? ACPI_BATTERY_UNITS_AMPS
: ACPI_BATTERY_UNITS_WATTS
;
450 /* Battery Status (_BST) */
452 result
= acpi_battery_get_status(battery
, &bst
);
453 if (result
|| !bst
) {
454 seq_printf(seq
, "ERROR: Unable to read battery status\n");
458 if (!(bst
->state
& 0x04))
459 seq_printf(seq
, "capacity state: ok\n");
461 seq_printf(seq
, "capacity state: critical\n");
463 if ((bst
->state
& 0x01) && (bst
->state
& 0x02)) {
465 "charging state: charging/discharging\n");
466 } else if (bst
->state
& 0x01)
467 seq_printf(seq
, "charging state: discharging\n");
468 else if (bst
->state
& 0x02)
469 seq_printf(seq
, "charging state: charging\n");
471 seq_printf(seq
, "charging state: charged\n");
474 if (bst
->present_rate
== ACPI_BATTERY_VALUE_UNKNOWN
)
475 seq_printf(seq
, "present rate: unknown\n");
477 seq_printf(seq
, "present rate: %d %s\n",
478 (u32
) bst
->present_rate
, units
);
480 if (bst
->remaining_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
481 seq_printf(seq
, "remaining capacity: unknown\n");
483 seq_printf(seq
, "remaining capacity: %d %sh\n",
484 (u32
) bst
->remaining_capacity
, units
);
486 if (bst
->present_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
487 seq_printf(seq
, "present voltage: unknown\n");
489 seq_printf(seq
, "present voltage: %d mV\n",
490 (u32
) bst
->present_voltage
);
498 static int acpi_battery_state_open_fs(struct inode
*inode
, struct file
*file
)
500 return single_open(file
, acpi_battery_read_state
, PDE(inode
)->data
);
503 static int acpi_battery_read_alarm(struct seq_file
*seq
, void *offset
)
505 struct acpi_battery
*battery
= seq
->private;
512 acpi_battery_check_present(battery
);
514 if (!battery
->flags
.present
) {
515 seq_printf(seq
, "present: no\n");
523 power_unit
? ACPI_BATTERY_UNITS_AMPS
: ACPI_BATTERY_UNITS_WATTS
;
527 seq_printf(seq
, "alarm: ");
529 seq_printf(seq
, "unsupported\n");
531 seq_printf(seq
, "%d %sh\n", (u32
) battery
->alarm
, units
);
538 acpi_battery_write_alarm(struct file
*file
,
539 const char __user
* buffer
,
540 size_t count
, loff_t
* ppos
)
543 char alarm_string
[12] = { '\0' };
544 struct seq_file
*m
= file
->private_data
;
545 struct acpi_battery
*battery
= m
->private;
548 if (!battery
|| (count
> sizeof(alarm_string
) - 1))
551 acpi_battery_check_present(battery
);
553 if (!battery
->flags
.present
)
556 if (copy_from_user(alarm_string
, buffer
, count
))
559 alarm_string
[count
] = '\0';
561 result
= acpi_battery_set_alarm(battery
,
562 simple_strtoul(alarm_string
, NULL
, 0));
569 static int acpi_battery_alarm_open_fs(struct inode
*inode
, struct file
*file
)
571 return single_open(file
, acpi_battery_read_alarm
, PDE(inode
)->data
);
574 static const struct file_operations acpi_battery_info_ops
= {
575 .open
= acpi_battery_info_open_fs
,
578 .release
= single_release
,
579 .owner
= THIS_MODULE
,
582 static const struct file_operations acpi_battery_state_ops
= {
583 .open
= acpi_battery_state_open_fs
,
586 .release
= single_release
,
587 .owner
= THIS_MODULE
,
590 static const struct file_operations acpi_battery_alarm_ops
= {
591 .open
= acpi_battery_alarm_open_fs
,
593 .write
= acpi_battery_write_alarm
,
595 .release
= single_release
,
596 .owner
= THIS_MODULE
,
599 static int acpi_battery_add_fs(struct acpi_device
*device
)
601 struct proc_dir_entry
*entry
= NULL
;
604 if (!acpi_device_dir(device
)) {
605 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
607 if (!acpi_device_dir(device
))
609 acpi_device_dir(device
)->owner
= THIS_MODULE
;
613 entry
= create_proc_entry(ACPI_BATTERY_FILE_INFO
,
614 S_IRUGO
, acpi_device_dir(device
));
618 entry
->proc_fops
= &acpi_battery_info_ops
;
619 entry
->data
= acpi_driver_data(device
);
620 entry
->owner
= THIS_MODULE
;
624 entry
= create_proc_entry(ACPI_BATTERY_FILE_STATUS
,
625 S_IRUGO
, acpi_device_dir(device
));
629 entry
->proc_fops
= &acpi_battery_state_ops
;
630 entry
->data
= acpi_driver_data(device
);
631 entry
->owner
= THIS_MODULE
;
635 entry
= create_proc_entry(ACPI_BATTERY_FILE_ALARM
,
636 S_IFREG
| S_IRUGO
| S_IWUSR
,
637 acpi_device_dir(device
));
641 entry
->proc_fops
= &acpi_battery_alarm_ops
;
642 entry
->data
= acpi_driver_data(device
);
643 entry
->owner
= THIS_MODULE
;
649 static int acpi_battery_remove_fs(struct acpi_device
*device
)
652 if (acpi_device_dir(device
)) {
653 remove_proc_entry(ACPI_BATTERY_FILE_ALARM
,
654 acpi_device_dir(device
));
655 remove_proc_entry(ACPI_BATTERY_FILE_STATUS
,
656 acpi_device_dir(device
));
657 remove_proc_entry(ACPI_BATTERY_FILE_INFO
,
658 acpi_device_dir(device
));
660 remove_proc_entry(acpi_device_bid(device
), acpi_battery_dir
);
661 acpi_device_dir(device
) = NULL
;
667 /* --------------------------------------------------------------------------
669 -------------------------------------------------------------------------- */
671 static void acpi_battery_notify(acpi_handle handle
, u32 event
, void *data
)
673 struct acpi_battery
*battery
= data
;
674 struct acpi_device
*device
= NULL
;
680 device
= battery
->device
;
683 case ACPI_BATTERY_NOTIFY_STATUS
:
684 case ACPI_BATTERY_NOTIFY_INFO
:
685 case ACPI_NOTIFY_BUS_CHECK
:
686 case ACPI_NOTIFY_DEVICE_CHECK
:
687 acpi_battery_check(battery
);
688 acpi_bus_generate_event(device
, event
, battery
->flags
.present
);
691 ACPI_DEBUG_PRINT((ACPI_DB_INFO
,
692 "Unsupported event [0x%x]\n", event
));
699 static int acpi_battery_add(struct acpi_device
*device
)
702 acpi_status status
= 0;
703 struct acpi_battery
*battery
= NULL
;
709 battery
= kzalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
713 battery
->device
= device
;
714 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
715 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
716 acpi_driver_data(device
) = battery
;
718 result
= acpi_battery_check(battery
);
722 result
= acpi_battery_add_fs(device
);
726 status
= acpi_install_notify_handler(device
->handle
,
728 acpi_battery_notify
, battery
);
729 if (ACPI_FAILURE(status
)) {
734 printk(KERN_INFO PREFIX
"%s Slot [%s] (battery %s)\n",
735 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
736 device
->status
.battery_present
? "present" : "absent");
740 acpi_battery_remove_fs(device
);
747 static int acpi_battery_remove(struct acpi_device
*device
, int type
)
749 acpi_status status
= 0;
750 struct acpi_battery
*battery
= NULL
;
753 if (!device
|| !acpi_driver_data(device
))
756 battery
= acpi_driver_data(device
);
758 status
= acpi_remove_notify_handler(device
->handle
,
760 acpi_battery_notify
);
762 acpi_battery_remove_fs(device
);
769 /* this is needed to learn about changes made in suspended state */
770 static int acpi_battery_resume(struct acpi_device
*device
)
772 struct acpi_battery
*battery
;
777 battery
= device
->driver_data
;
778 return acpi_battery_check(battery
);
781 static int __init
acpi_battery_init(void)
788 acpi_battery_dir
= acpi_lock_battery_dir();
789 if (!acpi_battery_dir
)
792 result
= acpi_bus_register_driver(&acpi_battery_driver
);
794 acpi_unlock_battery_dir(acpi_battery_dir
);
801 static void __exit
acpi_battery_exit(void)
804 acpi_bus_unregister_driver(&acpi_battery_driver
);
806 acpi_unlock_battery_dir(acpi_battery_dir
);
811 module_init(acpi_battery_init
);
812 module_exit(acpi_battery_exit
);