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_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"
55 #define _COMPONENT ACPI_BATTERY_COMPONENT
56 ACPI_MODULE_NAME("acpi_battery")
58 MODULE_AUTHOR("Paul Diefenbaugh");
59 MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME);
60 MODULE_LICENSE("GPL");
62 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
63 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
65 static int acpi_battery_add(struct acpi_device *device);
66 static int acpi_battery_remove(struct acpi_device *device, int type);
67 static int acpi_battery_resume(struct acpi_device *device);
69 static struct acpi_driver acpi_battery_driver = {
70 .name = ACPI_BATTERY_DRIVER_NAME,
71 .class = ACPI_BATTERY_CLASS,
72 .ids = ACPI_BATTERY_HID,
74 .add = acpi_battery_add,
75 .resume = acpi_battery_resume,
76 .remove = acpi_battery_remove,
80 struct acpi_battery_status {
82 acpi_integer present_rate;
83 acpi_integer remaining_capacity;
84 acpi_integer present_voltage;
87 struct acpi_battery_info {
88 acpi_integer power_unit;
89 acpi_integer design_capacity;
90 acpi_integer last_full_capacity;
91 acpi_integer battery_technology;
92 acpi_integer design_voltage;
93 acpi_integer design_capacity_warning;
94 acpi_integer design_capacity_low;
95 acpi_integer battery_capacity_granularity_1;
96 acpi_integer battery_capacity_granularity_2;
97 acpi_string model_number;
98 acpi_string serial_number;
99 acpi_string battery_type;
100 acpi_string oem_info;
103 struct acpi_battery_flags {
104 u8 present:1; /* Bay occupied? */
105 u8 power_unit:1; /* 0=watts, 1=apms */
106 u8 alarm:1; /* _BTP present? */
110 struct acpi_battery_trips {
111 unsigned long warning;
115 struct acpi_battery {
116 struct acpi_device * device;
117 struct acpi_battery_flags flags;
118 struct acpi_battery_trips trips;
120 struct acpi_battery_info *info;
123 /* --------------------------------------------------------------------------
125 -------------------------------------------------------------------------- */
128 acpi_battery_get_info(struct acpi_battery *battery,
129 struct acpi_battery_info **bif)
132 acpi_status status = 0;
133 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
134 struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
135 ACPI_BATTERY_FORMAT_BIF
137 struct acpi_buffer data = { 0, NULL };
138 union acpi_object *package = NULL;
141 if (!battery || !bif)
146 status = acpi_evaluate_object(battery->device->handle, "_BIF", NULL, &buffer);
147 if (ACPI_FAILURE(status)) {
148 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
152 package = buffer.pointer;
154 /* Extract Package Data */
156 status = acpi_extract_package(package, &format, &data);
157 if (status != AE_BUFFER_OVERFLOW) {
158 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
163 data.pointer = kzalloc(data.length, GFP_KERNEL);
169 status = acpi_extract_package(package, &format, &data);
170 if (ACPI_FAILURE(status)) {
171 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BIF"));
178 kfree(buffer.pointer);
181 (*bif) = data.pointer;
187 acpi_battery_get_status(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
196 struct acpi_buffer data = { 0, NULL };
197 union acpi_object *package = NULL;
200 if (!battery || !bst)
205 status = acpi_evaluate_object(battery->device->handle, "_BST", NULL, &buffer);
206 if (ACPI_FAILURE(status)) {
207 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
211 package = buffer.pointer;
213 /* Extract Package Data */
215 status = acpi_extract_package(package, &format, &data);
216 if (status != AE_BUFFER_OVERFLOW) {
217 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
222 data.pointer = kzalloc(data.length, GFP_KERNEL);
228 status = acpi_extract_package(package, &format, &data);
229 if (ACPI_FAILURE(status)) {
230 ACPI_EXCEPTION((AE_INFO, status, "Extracting _BST"));
237 kfree(buffer.pointer);
240 (*bst) = data.pointer;
246 acpi_battery_set_alarm(struct acpi_battery *battery, unsigned long alarm)
248 acpi_status status = 0;
249 union acpi_object arg0 = { ACPI_TYPE_INTEGER };
250 struct acpi_object_list arg_list = { 1, &arg0 };
256 if (!battery->flags.alarm)
259 arg0.integer.value = alarm;
261 status = acpi_evaluate_object(battery->device->handle, "_BTP", &arg_list, NULL);
262 if (ACPI_FAILURE(status))
265 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
267 battery->alarm = alarm;
272 static int acpi_battery_check(struct acpi_battery *battery)
275 acpi_status status = AE_OK;
276 acpi_handle handle = NULL;
277 struct acpi_device *device = NULL;
278 struct acpi_battery_info *bif = NULL;
284 device = battery->device;
286 result = acpi_bus_get_status(device);
292 if (!battery->flags.present && device->status.battery_present) {
294 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
296 /* Evalute _BIF to get certain static information */
298 result = acpi_battery_get_info(battery, &bif);
302 battery->flags.power_unit = bif->power_unit;
303 battery->trips.warning = bif->design_capacity_warning;
304 battery->trips.low = bif->design_capacity_low;
307 /* See if alarms are supported, and if so, set default */
309 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
310 if (ACPI_SUCCESS(status)) {
311 battery->flags.alarm = 1;
312 acpi_battery_set_alarm(battery, battery->trips.warning);
318 else if (battery->flags.present && !device->status.battery_present) {
319 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
322 battery->flags.present = device->status.battery_present;
327 static void acpi_battery_check_present(struct acpi_battery *battery)
329 if (!battery->flags.present) {
330 acpi_battery_check(battery);
334 /* --------------------------------------------------------------------------
336 -------------------------------------------------------------------------- */
338 static struct proc_dir_entry *acpi_battery_dir;
339 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
342 struct acpi_battery *battery = seq->private;
343 struct acpi_battery_info *bif = NULL;
350 acpi_battery_check_present(battery);
352 if (battery->flags.present)
353 seq_printf(seq, "present: yes\n");
355 seq_printf(seq, "present: no\n");
359 /* Battery Info (_BIF) */
361 result = acpi_battery_get_info(battery, &bif);
362 if (result || !bif) {
363 seq_printf(seq, "ERROR: Unable to read battery information\n");
369 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
371 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
372 seq_printf(seq, "design capacity: unknown\n");
374 seq_printf(seq, "design capacity: %d %sh\n",
375 (u32) bif->design_capacity, units);
377 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
378 seq_printf(seq, "last full capacity: unknown\n");
380 seq_printf(seq, "last full capacity: %d %sh\n",
381 (u32) bif->last_full_capacity, units);
383 switch ((u32) bif->battery_technology) {
385 seq_printf(seq, "battery technology: non-rechargeable\n");
388 seq_printf(seq, "battery technology: rechargeable\n");
391 seq_printf(seq, "battery technology: unknown\n");
395 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
396 seq_printf(seq, "design voltage: unknown\n");
398 seq_printf(seq, "design voltage: %d mV\n",
399 (u32) bif->design_voltage);
401 seq_printf(seq, "design capacity warning: %d %sh\n",
402 (u32) bif->design_capacity_warning, units);
403 seq_printf(seq, "design capacity low: %d %sh\n",
404 (u32) bif->design_capacity_low, units);
405 seq_printf(seq, "capacity granularity 1: %d %sh\n",
406 (u32) bif->battery_capacity_granularity_1, units);
407 seq_printf(seq, "capacity granularity 2: %d %sh\n",
408 (u32) bif->battery_capacity_granularity_2, units);
409 seq_printf(seq, "model number: %s\n", bif->model_number);
410 seq_printf(seq, "serial number: %s\n", bif->serial_number);
411 seq_printf(seq, "battery type: %s\n", bif->battery_type);
412 seq_printf(seq, "OEM info: %s\n", bif->oem_info);
420 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
422 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
425 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
428 struct acpi_battery *battery = seq->private;
429 struct acpi_battery_status *bst = NULL;
436 acpi_battery_check_present(battery);
438 if (battery->flags.present)
439 seq_printf(seq, "present: yes\n");
441 seq_printf(seq, "present: no\n");
449 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
451 /* Battery Status (_BST) */
453 result = acpi_battery_get_status(battery, &bst);
454 if (result || !bst) {
455 seq_printf(seq, "ERROR: Unable to read battery status\n");
459 if (!(bst->state & 0x04))
460 seq_printf(seq, "capacity state: ok\n");
462 seq_printf(seq, "capacity state: critical\n");
464 if ((bst->state & 0x01) && (bst->state & 0x02)) {
466 "charging state: charging/discharging\n");
467 } else if (bst->state & 0x01)
468 seq_printf(seq, "charging state: discharging\n");
469 else if (bst->state & 0x02)
470 seq_printf(seq, "charging state: charging\n");
472 seq_printf(seq, "charging state: charged\n");
475 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
476 seq_printf(seq, "present rate: unknown\n");
478 seq_printf(seq, "present rate: %d %s\n",
479 (u32) bst->present_rate, units);
481 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
482 seq_printf(seq, "remaining capacity: unknown\n");
484 seq_printf(seq, "remaining capacity: %d %sh\n",
485 (u32) bst->remaining_capacity, units);
487 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
488 seq_printf(seq, "present voltage: unknown\n");
490 seq_printf(seq, "present voltage: %d mV\n",
491 (u32) bst->present_voltage);
499 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
501 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
504 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
506 struct acpi_battery *battery = seq->private;
513 acpi_battery_check_present(battery);
515 if (!battery->flags.present) {
516 seq_printf(seq, "present: no\n");
524 power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
528 seq_printf(seq, "alarm: ");
530 seq_printf(seq, "unsupported\n");
532 seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
539 acpi_battery_write_alarm(struct file *file,
540 const char __user * buffer,
541 size_t count, loff_t * ppos)
544 char alarm_string[12] = { '\0' };
545 struct seq_file *m = file->private_data;
546 struct acpi_battery *battery = m->private;
549 if (!battery || (count > sizeof(alarm_string) - 1))
552 acpi_battery_check_present(battery);
554 if (!battery->flags.present)
557 if (copy_from_user(alarm_string, buffer, count))
560 alarm_string[count] = '\0';
562 result = acpi_battery_set_alarm(battery,
563 simple_strtoul(alarm_string, NULL, 0));
570 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
572 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
575 static const struct file_operations acpi_battery_info_ops = {
576 .open = acpi_battery_info_open_fs,
579 .release = single_release,
580 .owner = THIS_MODULE,
583 static const struct file_operations acpi_battery_state_ops = {
584 .open = acpi_battery_state_open_fs,
587 .release = single_release,
588 .owner = THIS_MODULE,
591 static const struct file_operations acpi_battery_alarm_ops = {
592 .open = acpi_battery_alarm_open_fs,
594 .write = acpi_battery_write_alarm,
596 .release = single_release,
597 .owner = THIS_MODULE,
600 static int acpi_battery_add_fs(struct acpi_device *device)
602 struct proc_dir_entry *entry = NULL;
605 if (!acpi_device_dir(device)) {
606 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
608 if (!acpi_device_dir(device))
610 acpi_device_dir(device)->owner = THIS_MODULE;
614 entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
615 S_IRUGO, acpi_device_dir(device));
619 entry->proc_fops = &acpi_battery_info_ops;
620 entry->data = acpi_driver_data(device);
621 entry->owner = THIS_MODULE;
625 entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
626 S_IRUGO, acpi_device_dir(device));
630 entry->proc_fops = &acpi_battery_state_ops;
631 entry->data = acpi_driver_data(device);
632 entry->owner = THIS_MODULE;
636 entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
637 S_IFREG | S_IRUGO | S_IWUSR,
638 acpi_device_dir(device));
642 entry->proc_fops = &acpi_battery_alarm_ops;
643 entry->data = acpi_driver_data(device);
644 entry->owner = THIS_MODULE;
650 static int acpi_battery_remove_fs(struct acpi_device *device)
653 if (acpi_device_dir(device)) {
654 remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
655 acpi_device_dir(device));
656 remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
657 acpi_device_dir(device));
658 remove_proc_entry(ACPI_BATTERY_FILE_INFO,
659 acpi_device_dir(device));
661 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
662 acpi_device_dir(device) = NULL;
668 /* --------------------------------------------------------------------------
670 -------------------------------------------------------------------------- */
672 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
674 struct acpi_battery *battery = data;
675 struct acpi_device *device = NULL;
681 device = battery->device;
684 case ACPI_BATTERY_NOTIFY_STATUS:
685 case ACPI_BATTERY_NOTIFY_INFO:
686 case ACPI_NOTIFY_BUS_CHECK:
687 case ACPI_NOTIFY_DEVICE_CHECK:
688 acpi_battery_check(battery);
689 acpi_bus_generate_event(device, event, battery->flags.present);
692 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
693 "Unsupported event [0x%x]\n", event));
700 static int acpi_battery_add(struct acpi_device *device)
703 acpi_status status = 0;
704 struct acpi_battery *battery = NULL;
710 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
714 battery->device = device;
715 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
716 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
717 acpi_driver_data(device) = battery;
719 result = acpi_battery_check(battery);
723 result = acpi_battery_add_fs(device);
727 status = acpi_install_notify_handler(device->handle,
729 acpi_battery_notify, battery);
730 if (ACPI_FAILURE(status)) {
735 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
736 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
737 device->status.battery_present ? "present" : "absent");
741 acpi_battery_remove_fs(device);
748 static int acpi_battery_remove(struct acpi_device *device, int type)
750 acpi_status status = 0;
751 struct acpi_battery *battery = NULL;
754 if (!device || !acpi_driver_data(device))
757 battery = acpi_driver_data(device);
759 status = acpi_remove_notify_handler(device->handle,
761 acpi_battery_notify);
763 acpi_battery_remove_fs(device);
770 /* this is needed to learn about changes made in suspended state */
771 static int acpi_battery_resume(struct acpi_device *device)
773 struct acpi_battery *battery;
778 battery = device->driver_data;
779 return acpi_battery_check(battery);
782 static int __init acpi_battery_init(void)
789 acpi_battery_dir = acpi_lock_battery_dir();
790 if (!acpi_battery_dir)
793 result = acpi_bus_register_driver(&acpi_battery_driver);
795 acpi_unlock_battery_dir(acpi_battery_dir);
802 static void __exit acpi_battery_exit(void)
805 acpi_bus_unregister_driver(&acpi_battery_driver);
807 acpi_unlock_battery_dir(acpi_battery_dir);
812 module_init(acpi_battery_init);
813 module_exit(acpi_battery_exit);