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>
38 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
40 #define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS"
41 #define ACPI_BATTERY_FORMAT_BST "NNNN"
43 #define ACPI_BATTERY_COMPONENT 0x00040000
44 #define ACPI_BATTERY_CLASS "battery"
45 #define ACPI_BATTERY_HID "PNP0C0A"
46 #define ACPI_BATTERY_DRIVER_NAME "ACPI Battery Driver"
47 #define ACPI_BATTERY_DEVICE_NAME "Battery"
48 #define ACPI_BATTERY_FILE_INFO "info"
49 #define ACPI_BATTERY_FILE_STATUS "state"
50 #define ACPI_BATTERY_FILE_ALARM "alarm"
51 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
52 #define ACPI_BATTERY_NOTIFY_INFO 0x81
53 #define ACPI_BATTERY_UNITS_WATTS "mW"
54 #define ACPI_BATTERY_UNITS_AMPS "mA"
57 #define _COMPONENT ACPI_BATTERY_COMPONENT
58 ACPI_MODULE_NAME ("acpi_battery")
60 MODULE_AUTHOR("Paul Diefenbaugh");
61 MODULE_DESCRIPTION(ACPI_BATTERY_DRIVER_NAME);
62 MODULE_LICENSE("GPL");
64 static int acpi_battery_add (struct acpi_device *device);
65 static int acpi_battery_remove (struct acpi_device *device, int type);
67 static struct acpi_driver acpi_battery_driver = {
68 .name = ACPI_BATTERY_DRIVER_NAME,
69 .class = ACPI_BATTERY_CLASS,
70 .ids = ACPI_BATTERY_HID,
72 .add = acpi_battery_add,
73 .remove = acpi_battery_remove,
77 struct acpi_battery_status {
79 acpi_integer present_rate;
80 acpi_integer remaining_capacity;
81 acpi_integer present_voltage;
84 struct acpi_battery_info {
85 acpi_integer power_unit;
86 acpi_integer design_capacity;
87 acpi_integer last_full_capacity;
88 acpi_integer battery_technology;
89 acpi_integer design_voltage;
90 acpi_integer design_capacity_warning;
91 acpi_integer design_capacity_low;
92 acpi_integer battery_capacity_granularity_1;
93 acpi_integer battery_capacity_granularity_2;
94 acpi_string model_number;
95 acpi_string serial_number;
96 acpi_string battery_type;
100 struct acpi_battery_flags {
101 u8 present:1; /* Bay occupied? */
102 u8 power_unit:1; /* 0=watts, 1=apms */
103 u8 alarm:1; /* _BTP present? */
107 struct acpi_battery_trips {
108 unsigned long warning;
112 struct acpi_battery {
114 struct acpi_battery_flags flags;
115 struct acpi_battery_trips trips;
117 struct acpi_battery_info *info;
121 /* --------------------------------------------------------------------------
123 -------------------------------------------------------------------------- */
126 acpi_battery_get_info (
127 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};
135 struct acpi_buffer data = {0, NULL};
136 union acpi_object *package = NULL;
138 ACPI_FUNCTION_TRACE("acpi_battery_get_info");
140 if (!battery || !bif)
141 return_VALUE(-EINVAL);
145 status = acpi_evaluate_object(battery->handle, "_BIF", NULL, &buffer);
146 if (ACPI_FAILURE(status)) {
147 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BIF\n"));
148 return_VALUE(-ENODEV);
151 package = (union acpi_object *) buffer.pointer;
153 /* Extract Package Data */
155 status = acpi_extract_package(package, &format, &data);
156 if (status != AE_BUFFER_OVERFLOW) {
157 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BIF\n"));
162 data.pointer = kmalloc(data.length, GFP_KERNEL);
167 memset(data.pointer, 0, data.length);
169 status = acpi_extract_package(package, &format, &data);
170 if (ACPI_FAILURE(status)) {
171 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BIF\n"));
178 acpi_os_free(buffer.pointer);
181 (*bif) = (struct acpi_battery_info *) data.pointer;
183 return_VALUE(result);
187 acpi_battery_get_status (
188 struct acpi_battery *battery,
189 struct acpi_battery_status **bst)
192 acpi_status status = 0;
193 struct acpi_buffer buffer = {ACPI_ALLOCATE_BUFFER, NULL};
194 struct acpi_buffer format = {sizeof(ACPI_BATTERY_FORMAT_BST),
195 ACPI_BATTERY_FORMAT_BST};
196 struct acpi_buffer data = {0, NULL};
197 union acpi_object *package = NULL;
199 ACPI_FUNCTION_TRACE("acpi_battery_get_status");
201 if (!battery || !bst)
202 return_VALUE(-EINVAL);
206 status = acpi_evaluate_object(battery->handle, "_BST", NULL, &buffer);
207 if (ACPI_FAILURE(status)) {
208 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error evaluating _BST\n"));
209 return_VALUE(-ENODEV);
212 package = (union acpi_object *) buffer.pointer;
214 /* Extract Package Data */
216 status = acpi_extract_package(package, &format, &data);
217 if (status != AE_BUFFER_OVERFLOW) {
218 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BST\n"));
223 data.pointer = kmalloc(data.length, GFP_KERNEL);
228 memset(data.pointer, 0, data.length);
230 status = acpi_extract_package(package, &format, &data);
231 if (ACPI_FAILURE(status)) {
232 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "Error extracting _BST\n"));
239 acpi_os_free(buffer.pointer);
242 (*bst) = (struct acpi_battery_status *) data.pointer;
244 return_VALUE(result);
249 acpi_battery_set_alarm (
250 struct acpi_battery *battery,
253 acpi_status status = 0;
254 union acpi_object arg0 = {ACPI_TYPE_INTEGER};
255 struct acpi_object_list arg_list = {1, &arg0};
257 ACPI_FUNCTION_TRACE("acpi_battery_set_alarm");
260 return_VALUE(-EINVAL);
262 if (!battery->flags.alarm)
263 return_VALUE(-ENODEV);
265 arg0.integer.value = alarm;
267 status = acpi_evaluate_object(battery->handle, "_BTP", &arg_list, NULL);
268 if (ACPI_FAILURE(status))
269 return_VALUE(-ENODEV);
271 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
273 battery->alarm = alarm;
281 struct acpi_battery *battery)
284 acpi_status status = AE_OK;
285 acpi_handle handle = NULL;
286 struct acpi_device *device = NULL;
287 struct acpi_battery_info *bif = NULL;
289 ACPI_FUNCTION_TRACE("acpi_battery_check");
292 return_VALUE(-EINVAL);
294 result = acpi_bus_get_device(battery->handle, &device);
296 return_VALUE(result);
298 result = acpi_bus_get_status(device);
300 return_VALUE(result);
304 if (!battery->flags.present && device->status.battery_present) {
306 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery inserted\n"));
308 /* Evalute _BIF to get certain static information */
310 result = acpi_battery_get_info(battery, &bif);
312 return_VALUE(result);
314 battery->flags.power_unit = bif->power_unit;
315 battery->trips.warning = bif->design_capacity_warning;
316 battery->trips.low = bif->design_capacity_low;
319 /* See if alarms are supported, and if so, set default */
321 status = acpi_get_handle(battery->handle, "_BTP", &handle);
322 if (ACPI_SUCCESS(status)) {
323 battery->flags.alarm = 1;
324 acpi_battery_set_alarm(battery, battery->trips.warning);
330 else if (battery->flags.present && !device->status.battery_present) {
331 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Battery removed\n"));
334 battery->flags.present = device->status.battery_present;
336 return_VALUE(result);
340 /* --------------------------------------------------------------------------
342 -------------------------------------------------------------------------- */
344 static struct proc_dir_entry *acpi_battery_dir;
345 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
348 struct acpi_battery *battery = (struct acpi_battery *) seq->private;
349 struct acpi_battery_info *bif = NULL;
352 ACPI_FUNCTION_TRACE("acpi_battery_read_info");
357 if (battery->flags.present)
358 seq_printf(seq, "present: yes\n");
360 seq_printf(seq, "present: no\n");
364 /* Battery Info (_BIF) */
366 result = acpi_battery_get_info(battery, &bif);
367 if (result || !bif) {
368 seq_printf(seq, "ERROR: Unable to read battery information\n");
372 units = bif->power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
374 if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
375 seq_printf(seq, "design capacity: unknown\n");
377 seq_printf(seq, "design capacity: %d %sh\n",
378 (u32) bif->design_capacity, units);
380 if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
381 seq_printf(seq, "last full capacity: unknown\n");
383 seq_printf(seq, "last full capacity: %d %sh\n",
384 (u32) bif->last_full_capacity, units);
386 switch ((u32) bif->battery_technology) {
388 seq_printf(seq, "battery technology: non-rechargeable\n");
391 seq_printf(seq, "battery technology: rechargeable\n");
394 seq_printf(seq, "battery technology: unknown\n");
398 if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
399 seq_printf(seq, "design voltage: unknown\n");
401 seq_printf(seq, "design voltage: %d mV\n",
402 (u32) bif->design_voltage);
404 seq_printf(seq, "design capacity warning: %d %sh\n",
405 (u32) bif->design_capacity_warning, units);
406 seq_printf(seq, "design capacity low: %d %sh\n",
407 (u32) bif->design_capacity_low, units);
408 seq_printf(seq, "capacity granularity 1: %d %sh\n",
409 (u32) bif->battery_capacity_granularity_1, units);
410 seq_printf(seq, "capacity granularity 2: %d %sh\n",
411 (u32) bif->battery_capacity_granularity_2, units);
412 seq_printf(seq, "model number: %s\n",
414 seq_printf(seq, "serial number: %s\n",
416 seq_printf(seq, "battery type: %s\n",
418 seq_printf(seq, "OEM info: %s\n",
427 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
429 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
434 acpi_battery_read_state (struct seq_file *seq, void *offset)
437 struct acpi_battery *battery = (struct acpi_battery *) seq->private;
438 struct acpi_battery_status *bst = NULL;
441 ACPI_FUNCTION_TRACE("acpi_battery_read_state");
446 if (battery->flags.present)
447 seq_printf(seq, "present: yes\n");
449 seq_printf(seq, "present: no\n");
455 units = battery->flags.power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
457 /* Battery Status (_BST) */
459 result = acpi_battery_get_status(battery, &bst);
460 if (result || !bst) {
461 seq_printf(seq, "ERROR: Unable to read battery status\n");
465 if (!(bst->state & 0x04))
466 seq_printf(seq, "capacity state: ok\n");
468 seq_printf(seq, "capacity state: critical\n");
470 if ((bst->state & 0x01) && (bst->state & 0x02)){
471 seq_printf(seq, "charging state: charging/discharging\n");
472 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
473 "Battery Charging and Discharging?\n"));
475 else if (bst->state & 0x01)
476 seq_printf(seq, "charging state: discharging\n");
477 else if (bst->state & 0x02)
478 seq_printf(seq, "charging state: charging\n");
480 seq_printf(seq, "charging state: charged\n");
483 if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
484 seq_printf(seq, "present rate: unknown\n");
486 seq_printf(seq, "present rate: %d %s\n",
487 (u32) bst->present_rate, units);
489 if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
490 seq_printf(seq, "remaining capacity: unknown\n");
492 seq_printf(seq, "remaining capacity: %d %sh\n",
493 (u32) bst->remaining_capacity, units);
495 if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
496 seq_printf(seq, "present voltage: unknown\n");
498 seq_printf(seq, "present voltage: %d mV\n",
499 (u32) bst->present_voltage);
507 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
509 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
514 acpi_battery_read_alarm (struct seq_file *seq, void *offset)
516 struct acpi_battery *battery = (struct acpi_battery *) seq->private;
519 ACPI_FUNCTION_TRACE("acpi_battery_read_alarm");
524 if (!battery->flags.present) {
525 seq_printf(seq, "present: no\n");
531 units = battery->flags.power_unit ? ACPI_BATTERY_UNITS_AMPS : ACPI_BATTERY_UNITS_WATTS;
535 seq_printf(seq, "alarm: ");
537 seq_printf(seq, "unsupported\n");
539 seq_printf(seq, "%d %sh\n", (u32) battery->alarm, units);
547 acpi_battery_write_alarm (
549 const char __user *buffer,
554 char alarm_string[12] = {'\0'};
555 struct seq_file *m = (struct seq_file *)file->private_data;
556 struct acpi_battery *battery = (struct acpi_battery *)m->private;
558 ACPI_FUNCTION_TRACE("acpi_battery_write_alarm");
560 if (!battery || (count > sizeof(alarm_string) - 1))
561 return_VALUE(-EINVAL);
563 if (!battery->flags.present)
564 return_VALUE(-ENODEV);
566 if (copy_from_user(alarm_string, buffer, count))
567 return_VALUE(-EFAULT);
569 alarm_string[count] = '\0';
571 result = acpi_battery_set_alarm(battery,
572 simple_strtoul(alarm_string, NULL, 0));
574 return_VALUE(result);
579 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
581 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
584 static struct file_operations acpi_battery_info_ops = {
585 .open = acpi_battery_info_open_fs,
588 .release = single_release,
589 .owner = THIS_MODULE,
592 static struct file_operations acpi_battery_state_ops = {
593 .open = acpi_battery_state_open_fs,
596 .release = single_release,
597 .owner = THIS_MODULE,
600 static struct file_operations acpi_battery_alarm_ops = {
601 .open = acpi_battery_alarm_open_fs,
603 .write = acpi_battery_write_alarm,
605 .release = single_release,
606 .owner = THIS_MODULE,
610 acpi_battery_add_fs (
611 struct acpi_device *device)
613 struct proc_dir_entry *entry = NULL;
615 ACPI_FUNCTION_TRACE("acpi_battery_add_fs");
617 if (!acpi_device_dir(device)) {
618 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
620 if (!acpi_device_dir(device))
621 return_VALUE(-ENODEV);
622 acpi_device_dir(device)->owner = THIS_MODULE;
626 entry = create_proc_entry(ACPI_BATTERY_FILE_INFO,
627 S_IRUGO, acpi_device_dir(device));
629 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
630 "Unable to create '%s' fs entry\n",
631 ACPI_BATTERY_FILE_INFO));
633 entry->proc_fops = &acpi_battery_info_ops;
634 entry->data = acpi_driver_data(device);
635 entry->owner = THIS_MODULE;
639 entry = create_proc_entry(ACPI_BATTERY_FILE_STATUS,
640 S_IRUGO, acpi_device_dir(device));
642 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
643 "Unable to create '%s' fs entry\n",
644 ACPI_BATTERY_FILE_STATUS));
646 entry->proc_fops = &acpi_battery_state_ops;
647 entry->data = acpi_driver_data(device);
648 entry->owner = THIS_MODULE;
652 entry = create_proc_entry(ACPI_BATTERY_FILE_ALARM,
653 S_IFREG|S_IRUGO|S_IWUSR, acpi_device_dir(device));
655 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
656 "Unable to create '%s' fs entry\n",
657 ACPI_BATTERY_FILE_ALARM));
659 entry->proc_fops = &acpi_battery_alarm_ops;
660 entry->data = acpi_driver_data(device);
661 entry->owner = THIS_MODULE;
669 acpi_battery_remove_fs (
670 struct acpi_device *device)
672 ACPI_FUNCTION_TRACE("acpi_battery_remove_fs");
674 if (acpi_device_dir(device)) {
675 remove_proc_entry(ACPI_BATTERY_FILE_ALARM,
676 acpi_device_dir(device));
677 remove_proc_entry(ACPI_BATTERY_FILE_STATUS,
678 acpi_device_dir(device));
679 remove_proc_entry(ACPI_BATTERY_FILE_INFO,
680 acpi_device_dir(device));
682 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
683 acpi_device_dir(device) = NULL;
690 /* --------------------------------------------------------------------------
692 -------------------------------------------------------------------------- */
695 acpi_battery_notify (
700 struct acpi_battery *battery = (struct acpi_battery *) data;
701 struct acpi_device *device = NULL;
703 ACPI_FUNCTION_TRACE("acpi_battery_notify");
708 if (acpi_bus_get_device(handle, &device))
712 case ACPI_BATTERY_NOTIFY_STATUS:
713 case ACPI_BATTERY_NOTIFY_INFO:
714 acpi_battery_check(battery);
715 acpi_bus_generate_event(device, event, battery->flags.present);
718 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
719 "Unsupported event [0x%x]\n", event));
729 struct acpi_device *device)
732 acpi_status status = 0;
733 struct acpi_battery *battery = NULL;
735 ACPI_FUNCTION_TRACE("acpi_battery_add");
738 return_VALUE(-EINVAL);
740 battery = kmalloc(sizeof(struct acpi_battery), GFP_KERNEL);
742 return_VALUE(-ENOMEM);
743 memset(battery, 0, sizeof(struct acpi_battery));
745 battery->handle = device->handle;
746 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
747 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
748 acpi_driver_data(device) = battery;
750 result = acpi_battery_check(battery);
754 result = acpi_battery_add_fs(device);
758 status = acpi_install_notify_handler(battery->handle,
759 ACPI_DEVICE_NOTIFY, acpi_battery_notify, battery);
760 if (ACPI_FAILURE(status)) {
761 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
762 "Error installing notify handler\n"));
767 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
768 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
769 device->status.battery_present?"present":"absent");
773 acpi_battery_remove_fs(device);
777 return_VALUE(result);
782 acpi_battery_remove (
783 struct acpi_device *device,
786 acpi_status status = 0;
787 struct acpi_battery *battery = NULL;
789 ACPI_FUNCTION_TRACE("acpi_battery_remove");
791 if (!device || !acpi_driver_data(device))
792 return_VALUE(-EINVAL);
794 battery = (struct acpi_battery *) acpi_driver_data(device);
796 status = acpi_remove_notify_handler(battery->handle,
797 ACPI_DEVICE_NOTIFY, acpi_battery_notify);
798 if (ACPI_FAILURE(status))
799 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
800 "Error removing notify handler\n"));
802 acpi_battery_remove_fs(device);
811 acpi_battery_init (void)
815 ACPI_FUNCTION_TRACE("acpi_battery_init");
817 acpi_battery_dir = proc_mkdir(ACPI_BATTERY_CLASS, acpi_root_dir);
818 if (!acpi_battery_dir)
819 return_VALUE(-ENODEV);
820 acpi_battery_dir->owner = THIS_MODULE;
822 result = acpi_bus_register_driver(&acpi_battery_driver);
824 remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
825 return_VALUE(-ENODEV);
833 acpi_battery_exit (void)
835 ACPI_FUNCTION_TRACE("acpi_battery_exit");
837 acpi_bus_unregister_driver(&acpi_battery_driver);
839 remove_proc_entry(ACPI_BATTERY_CLASS, acpi_root_dir);
845 module_init(acpi_battery_init);
846 module_exit(acpi_battery_exit);