2 * battery.c - ACPI Battery Driver (Revision: 2.0)
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
25 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/jiffies.h>
33 #include <linux/async.h>
35 #ifdef CONFIG_ACPI_PROCFS_POWER
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <asm/uaccess.h>
41 #include <acpi/acpi_bus.h>
42 #include <acpi/acpi_drivers.h>
44 #ifdef CONFIG_ACPI_SYSFS_POWER
45 #include <linux/power_supply.h>
48 #define PREFIX "ACPI: "
50 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
52 #define ACPI_BATTERY_CLASS "battery"
53 #define ACPI_BATTERY_DEVICE_NAME "Battery"
54 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
55 #define ACPI_BATTERY_NOTIFY_INFO 0x81
57 #define _COMPONENT ACPI_BATTERY_COMPONENT
59 ACPI_MODULE_NAME("battery");
61 MODULE_AUTHOR("Paul Diefenbaugh");
62 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
63 MODULE_DESCRIPTION("ACPI Battery Driver");
64 MODULE_LICENSE("GPL");
66 static unsigned int cache_time = 1000;
67 module_param(cache_time, uint, 0644);
68 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
70 #ifdef CONFIG_ACPI_PROCFS_POWER
71 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
72 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
74 enum acpi_battery_files {
78 ACPI_BATTERY_NUMFILES,
83 static const struct acpi_device_id battery_device_ids[] = {
88 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
93 #ifdef CONFIG_ACPI_SYSFS_POWER
94 struct power_supply bat;
96 struct acpi_device *device;
97 unsigned long update_time;
102 int full_charge_capacity;
105 int design_capacity_warning;
106 int design_capacity_low;
107 int capacity_granularity_1;
108 int capacity_granularity_2;
110 char model_number[32];
111 char serial_number[32];
119 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
121 inline int acpi_battery_present(struct acpi_battery *battery)
123 return battery->device->status.battery_present;
126 #ifdef CONFIG_ACPI_SYSFS_POWER
127 static int acpi_battery_technology(struct acpi_battery *battery)
129 if (!strcasecmp("NiCd", battery->type))
130 return POWER_SUPPLY_TECHNOLOGY_NiCd;
131 if (!strcasecmp("NiMH", battery->type))
132 return POWER_SUPPLY_TECHNOLOGY_NiMH;
133 if (!strcasecmp("LION", battery->type))
134 return POWER_SUPPLY_TECHNOLOGY_LION;
135 if (!strncasecmp("LI-ION", battery->type, 6))
136 return POWER_SUPPLY_TECHNOLOGY_LION;
137 if (!strcasecmp("LiP", battery->type))
138 return POWER_SUPPLY_TECHNOLOGY_LIPO;
139 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
142 static int acpi_battery_get_state(struct acpi_battery *battery);
144 static int acpi_battery_is_charged(struct acpi_battery *battery)
146 /* either charging or discharging */
147 if (battery->state != 0)
150 /* battery not reporting charge */
151 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
152 battery->capacity_now == 0)
155 /* good batteries update full_charge as the batteries degrade */
156 if (battery->full_charge_capacity == battery->capacity_now)
159 /* fallback to using design values for broken batteries */
160 if (battery->design_capacity == battery->capacity_now)
163 /* we don't do any sort of metric based on percentages */
167 static int acpi_battery_get_property(struct power_supply *psy,
168 enum power_supply_property psp,
169 union power_supply_propval *val)
171 struct acpi_battery *battery = to_acpi_battery(psy);
173 if (acpi_battery_present(battery)) {
174 /* run battery update only if it is present */
175 acpi_battery_get_state(battery);
176 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
179 case POWER_SUPPLY_PROP_STATUS:
180 if (battery->state & 0x01)
181 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
182 else if (battery->state & 0x02)
183 val->intval = POWER_SUPPLY_STATUS_CHARGING;
184 else if (acpi_battery_is_charged(battery))
185 val->intval = POWER_SUPPLY_STATUS_FULL;
187 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
189 case POWER_SUPPLY_PROP_PRESENT:
190 val->intval = acpi_battery_present(battery);
192 case POWER_SUPPLY_PROP_TECHNOLOGY:
193 val->intval = acpi_battery_technology(battery);
195 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
196 val->intval = battery->design_voltage * 1000;
198 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
199 val->intval = battery->voltage_now * 1000;
201 case POWER_SUPPLY_PROP_CURRENT_NOW:
202 case POWER_SUPPLY_PROP_POWER_NOW:
203 val->intval = battery->rate_now * 1000;
205 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
206 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
207 val->intval = battery->design_capacity * 1000;
209 case POWER_SUPPLY_PROP_CHARGE_FULL:
210 case POWER_SUPPLY_PROP_ENERGY_FULL:
211 val->intval = battery->full_charge_capacity * 1000;
213 case POWER_SUPPLY_PROP_CHARGE_NOW:
214 case POWER_SUPPLY_PROP_ENERGY_NOW:
215 val->intval = battery->capacity_now * 1000;
217 case POWER_SUPPLY_PROP_MODEL_NAME:
218 val->strval = battery->model_number;
220 case POWER_SUPPLY_PROP_MANUFACTURER:
221 val->strval = battery->oem_info;
223 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
224 val->strval = battery->serial_number;
232 static enum power_supply_property charge_battery_props[] = {
233 POWER_SUPPLY_PROP_STATUS,
234 POWER_SUPPLY_PROP_PRESENT,
235 POWER_SUPPLY_PROP_TECHNOLOGY,
236 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
237 POWER_SUPPLY_PROP_VOLTAGE_NOW,
238 POWER_SUPPLY_PROP_CURRENT_NOW,
239 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
240 POWER_SUPPLY_PROP_CHARGE_FULL,
241 POWER_SUPPLY_PROP_CHARGE_NOW,
242 POWER_SUPPLY_PROP_MODEL_NAME,
243 POWER_SUPPLY_PROP_MANUFACTURER,
244 POWER_SUPPLY_PROP_SERIAL_NUMBER,
247 static enum power_supply_property energy_battery_props[] = {
248 POWER_SUPPLY_PROP_STATUS,
249 POWER_SUPPLY_PROP_PRESENT,
250 POWER_SUPPLY_PROP_TECHNOLOGY,
251 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
252 POWER_SUPPLY_PROP_VOLTAGE_NOW,
253 POWER_SUPPLY_PROP_CURRENT_NOW,
254 POWER_SUPPLY_PROP_POWER_NOW,
255 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
256 POWER_SUPPLY_PROP_ENERGY_FULL,
257 POWER_SUPPLY_PROP_ENERGY_NOW,
258 POWER_SUPPLY_PROP_MODEL_NAME,
259 POWER_SUPPLY_PROP_MANUFACTURER,
260 POWER_SUPPLY_PROP_SERIAL_NUMBER,
264 #ifdef CONFIG_ACPI_PROCFS_POWER
265 inline char *acpi_battery_units(struct acpi_battery *battery)
267 return (battery->power_unit)?"mA":"mW";
271 /* --------------------------------------------------------------------------
273 -------------------------------------------------------------------------- */
274 struct acpi_offsets {
275 size_t offset; /* offset inside struct acpi_sbs_battery */
276 u8 mode; /* int or string? */
279 static struct acpi_offsets state_offsets[] = {
280 {offsetof(struct acpi_battery, state), 0},
281 {offsetof(struct acpi_battery, rate_now), 0},
282 {offsetof(struct acpi_battery, capacity_now), 0},
283 {offsetof(struct acpi_battery, voltage_now), 0},
286 static struct acpi_offsets info_offsets[] = {
287 {offsetof(struct acpi_battery, power_unit), 0},
288 {offsetof(struct acpi_battery, design_capacity), 0},
289 {offsetof(struct acpi_battery, full_charge_capacity), 0},
290 {offsetof(struct acpi_battery, technology), 0},
291 {offsetof(struct acpi_battery, design_voltage), 0},
292 {offsetof(struct acpi_battery, design_capacity_warning), 0},
293 {offsetof(struct acpi_battery, design_capacity_low), 0},
294 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
295 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
296 {offsetof(struct acpi_battery, model_number), 1},
297 {offsetof(struct acpi_battery, serial_number), 1},
298 {offsetof(struct acpi_battery, type), 1},
299 {offsetof(struct acpi_battery, oem_info), 1},
302 static int extract_package(struct acpi_battery *battery,
303 union acpi_object *package,
304 struct acpi_offsets *offsets, int num)
307 union acpi_object *element;
308 if (package->type != ACPI_TYPE_PACKAGE)
310 for (i = 0; i < num; ++i) {
311 if (package->package.count <= i)
313 element = &package->package.elements[i];
314 if (offsets[i].mode) {
315 u8 *ptr = (u8 *)battery + offsets[i].offset;
316 if (element->type == ACPI_TYPE_STRING ||
317 element->type == ACPI_TYPE_BUFFER)
318 strncpy(ptr, element->string.pointer, 32);
319 else if (element->type == ACPI_TYPE_INTEGER) {
320 strncpy(ptr, (u8 *)&element->integer.value,
321 sizeof(acpi_integer));
322 ptr[sizeof(acpi_integer)] = 0;
324 *ptr = 0; /* don't have value */
326 int *x = (int *)((u8 *)battery + offsets[i].offset);
327 *x = (element->type == ACPI_TYPE_INTEGER) ?
328 element->integer.value : -1;
334 static int acpi_battery_get_status(struct acpi_battery *battery)
336 if (acpi_bus_get_status(battery->device)) {
337 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
343 static int acpi_battery_get_info(struct acpi_battery *battery)
345 int result = -EFAULT;
346 acpi_status status = 0;
347 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
349 if (!acpi_battery_present(battery))
351 mutex_lock(&battery->lock);
352 status = acpi_evaluate_object(battery->device->handle, "_BIF",
354 mutex_unlock(&battery->lock);
356 if (ACPI_FAILURE(status)) {
357 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
361 result = extract_package(battery, buffer.pointer,
362 info_offsets, ARRAY_SIZE(info_offsets));
363 kfree(buffer.pointer);
367 static int acpi_battery_get_state(struct acpi_battery *battery)
370 acpi_status status = 0;
371 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
373 if (!acpi_battery_present(battery))
376 if (battery->update_time &&
377 time_before(jiffies, battery->update_time +
378 msecs_to_jiffies(cache_time)))
381 mutex_lock(&battery->lock);
382 status = acpi_evaluate_object(battery->device->handle, "_BST",
384 mutex_unlock(&battery->lock);
386 if (ACPI_FAILURE(status)) {
387 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
391 result = extract_package(battery, buffer.pointer,
392 state_offsets, ARRAY_SIZE(state_offsets));
393 battery->update_time = jiffies;
394 kfree(buffer.pointer);
398 static int acpi_battery_set_alarm(struct acpi_battery *battery)
400 acpi_status status = 0;
401 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
402 struct acpi_object_list arg_list = { 1, &arg0 };
404 if (!acpi_battery_present(battery)|| !battery->alarm_present)
407 arg0.integer.value = battery->alarm;
409 mutex_lock(&battery->lock);
410 status = acpi_evaluate_object(battery->device->handle, "_BTP",
412 mutex_unlock(&battery->lock);
414 if (ACPI_FAILURE(status))
417 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
421 static int acpi_battery_init_alarm(struct acpi_battery *battery)
423 acpi_status status = AE_OK;
424 acpi_handle handle = NULL;
426 /* See if alarms are supported, and if so, set default */
427 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
428 if (ACPI_FAILURE(status)) {
429 battery->alarm_present = 0;
432 battery->alarm_present = 1;
434 battery->alarm = battery->design_capacity_warning;
435 return acpi_battery_set_alarm(battery);
438 #ifdef CONFIG_ACPI_SYSFS_POWER
439 static ssize_t acpi_battery_alarm_show(struct device *dev,
440 struct device_attribute *attr,
443 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
444 return sprintf(buf, "%d\n", battery->alarm * 1000);
447 static ssize_t acpi_battery_alarm_store(struct device *dev,
448 struct device_attribute *attr,
449 const char *buf, size_t count)
452 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
453 if (sscanf(buf, "%ld\n", &x) == 1)
454 battery->alarm = x/1000;
455 if (acpi_battery_present(battery))
456 acpi_battery_set_alarm(battery);
460 static struct device_attribute alarm_attr = {
461 .attr = {.name = "alarm", .mode = 0644},
462 .show = acpi_battery_alarm_show,
463 .store = acpi_battery_alarm_store,
466 static int sysfs_add_battery(struct acpi_battery *battery)
470 if (battery->power_unit) {
471 battery->bat.properties = charge_battery_props;
472 battery->bat.num_properties =
473 ARRAY_SIZE(charge_battery_props);
475 battery->bat.properties = energy_battery_props;
476 battery->bat.num_properties =
477 ARRAY_SIZE(energy_battery_props);
480 battery->bat.name = acpi_device_bid(battery->device);
481 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
482 battery->bat.get_property = acpi_battery_get_property;
484 result = power_supply_register(&battery->device->dev, &battery->bat);
487 return device_create_file(battery->bat.dev, &alarm_attr);
490 static void sysfs_remove_battery(struct acpi_battery *battery)
492 if (!battery->bat.dev)
494 device_remove_file(battery->bat.dev, &alarm_attr);
495 power_supply_unregister(&battery->bat);
496 battery->bat.dev = NULL;
500 static int acpi_battery_update(struct acpi_battery *battery)
502 int result, old_present = acpi_battery_present(battery);
503 result = acpi_battery_get_status(battery);
506 #ifdef CONFIG_ACPI_SYSFS_POWER
507 if (!acpi_battery_present(battery)) {
508 sysfs_remove_battery(battery);
509 battery->update_time = 0;
513 if (!battery->update_time ||
514 old_present != acpi_battery_present(battery)) {
515 result = acpi_battery_get_info(battery);
518 acpi_battery_init_alarm(battery);
520 #ifdef CONFIG_ACPI_SYSFS_POWER
521 if (!battery->bat.dev)
522 sysfs_add_battery(battery);
524 return acpi_battery_get_state(battery);
527 /* --------------------------------------------------------------------------
529 -------------------------------------------------------------------------- */
531 #ifdef CONFIG_ACPI_PROCFS_POWER
532 static struct proc_dir_entry *acpi_battery_dir;
534 static int acpi_battery_print_info(struct seq_file *seq, int result)
536 struct acpi_battery *battery = seq->private;
541 seq_printf(seq, "present: %s\n",
542 acpi_battery_present(battery)?"yes":"no");
543 if (!acpi_battery_present(battery))
545 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
546 seq_printf(seq, "design capacity: unknown\n");
548 seq_printf(seq, "design capacity: %d %sh\n",
549 battery->design_capacity,
550 acpi_battery_units(battery));
552 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
553 seq_printf(seq, "last full capacity: unknown\n");
555 seq_printf(seq, "last full capacity: %d %sh\n",
556 battery->full_charge_capacity,
557 acpi_battery_units(battery));
559 seq_printf(seq, "battery technology: %srechargeable\n",
560 (!battery->technology)?"non-":"");
562 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
563 seq_printf(seq, "design voltage: unknown\n");
565 seq_printf(seq, "design voltage: %d mV\n",
566 battery->design_voltage);
567 seq_printf(seq, "design capacity warning: %d %sh\n",
568 battery->design_capacity_warning,
569 acpi_battery_units(battery));
570 seq_printf(seq, "design capacity low: %d %sh\n",
571 battery->design_capacity_low,
572 acpi_battery_units(battery));
573 seq_printf(seq, "capacity granularity 1: %d %sh\n",
574 battery->capacity_granularity_1,
575 acpi_battery_units(battery));
576 seq_printf(seq, "capacity granularity 2: %d %sh\n",
577 battery->capacity_granularity_2,
578 acpi_battery_units(battery));
579 seq_printf(seq, "model number: %s\n", battery->model_number);
580 seq_printf(seq, "serial number: %s\n", battery->serial_number);
581 seq_printf(seq, "battery type: %s\n", battery->type);
582 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
585 seq_printf(seq, "ERROR: Unable to read battery info\n");
589 static int acpi_battery_print_state(struct seq_file *seq, int result)
591 struct acpi_battery *battery = seq->private;
596 seq_printf(seq, "present: %s\n",
597 acpi_battery_present(battery)?"yes":"no");
598 if (!acpi_battery_present(battery))
601 seq_printf(seq, "capacity state: %s\n",
602 (battery->state & 0x04)?"critical":"ok");
603 if ((battery->state & 0x01) && (battery->state & 0x02))
605 "charging state: charging/discharging\n");
606 else if (battery->state & 0x01)
607 seq_printf(seq, "charging state: discharging\n");
608 else if (battery->state & 0x02)
609 seq_printf(seq, "charging state: charging\n");
611 seq_printf(seq, "charging state: charged\n");
613 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
614 seq_printf(seq, "present rate: unknown\n");
616 seq_printf(seq, "present rate: %d %s\n",
617 battery->rate_now, acpi_battery_units(battery));
619 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
620 seq_printf(seq, "remaining capacity: unknown\n");
622 seq_printf(seq, "remaining capacity: %d %sh\n",
623 battery->capacity_now, acpi_battery_units(battery));
624 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
625 seq_printf(seq, "present voltage: unknown\n");
627 seq_printf(seq, "present voltage: %d mV\n",
628 battery->voltage_now);
631 seq_printf(seq, "ERROR: Unable to read battery state\n");
636 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
638 struct acpi_battery *battery = seq->private;
643 if (!acpi_battery_present(battery)) {
644 seq_printf(seq, "present: no\n");
647 seq_printf(seq, "alarm: ");
649 seq_printf(seq, "unsupported\n");
651 seq_printf(seq, "%u %sh\n", battery->alarm,
652 acpi_battery_units(battery));
655 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
659 static ssize_t acpi_battery_write_alarm(struct file *file,
660 const char __user * buffer,
661 size_t count, loff_t * ppos)
664 char alarm_string[12] = { '\0' };
665 struct seq_file *m = file->private_data;
666 struct acpi_battery *battery = m->private;
668 if (!battery || (count > sizeof(alarm_string) - 1))
670 if (!acpi_battery_present(battery)) {
674 if (copy_from_user(alarm_string, buffer, count)) {
678 alarm_string[count] = '\0';
679 battery->alarm = simple_strtol(alarm_string, NULL, 0);
680 result = acpi_battery_set_alarm(battery);
687 typedef int(*print_func)(struct seq_file *seq, int result);
689 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
690 acpi_battery_print_info,
691 acpi_battery_print_state,
692 acpi_battery_print_alarm,
695 static int acpi_battery_read(int fid, struct seq_file *seq)
697 struct acpi_battery *battery = seq->private;
698 int result = acpi_battery_update(battery);
699 return acpi_print_funcs[fid](seq, result);
702 #define DECLARE_FILE_FUNCTIONS(_name) \
703 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
705 return acpi_battery_read(_name##_tag, seq); \
707 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
709 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
712 DECLARE_FILE_FUNCTIONS(info);
713 DECLARE_FILE_FUNCTIONS(state);
714 DECLARE_FILE_FUNCTIONS(alarm);
716 #undef DECLARE_FILE_FUNCTIONS
718 #define FILE_DESCRIPTION_RO(_name) \
720 .name = __stringify(_name), \
723 .open = acpi_battery_##_name##_open_fs, \
725 .llseek = seq_lseek, \
726 .release = single_release, \
727 .owner = THIS_MODULE, \
731 #define FILE_DESCRIPTION_RW(_name) \
733 .name = __stringify(_name), \
734 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
736 .open = acpi_battery_##_name##_open_fs, \
738 .llseek = seq_lseek, \
739 .write = acpi_battery_write_##_name, \
740 .release = single_release, \
741 .owner = THIS_MODULE, \
745 static struct battery_file {
746 struct file_operations ops;
749 } acpi_battery_file[] = {
750 FILE_DESCRIPTION_RO(info),
751 FILE_DESCRIPTION_RO(state),
752 FILE_DESCRIPTION_RW(alarm),
755 #undef FILE_DESCRIPTION_RO
756 #undef FILE_DESCRIPTION_RW
758 static int acpi_battery_add_fs(struct acpi_device *device)
760 struct proc_dir_entry *entry = NULL;
763 if (!acpi_device_dir(device)) {
764 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
766 if (!acpi_device_dir(device))
770 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
771 entry = proc_create_data(acpi_battery_file[i].name,
772 acpi_battery_file[i].mode,
773 acpi_device_dir(device),
774 &acpi_battery_file[i].ops,
775 acpi_driver_data(device));
782 static void acpi_battery_remove_fs(struct acpi_device *device)
785 if (!acpi_device_dir(device))
787 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
788 remove_proc_entry(acpi_battery_file[i].name,
789 acpi_device_dir(device));
791 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
792 acpi_device_dir(device) = NULL;
797 /* --------------------------------------------------------------------------
799 -------------------------------------------------------------------------- */
801 static void acpi_battery_notify(struct acpi_device *device, u32 event)
803 struct acpi_battery *battery = acpi_driver_data(device);
807 acpi_battery_update(battery);
808 acpi_bus_generate_proc_event(device, event,
809 acpi_battery_present(battery));
810 acpi_bus_generate_netlink_event(device->pnp.device_class,
811 dev_name(&device->dev), event,
812 acpi_battery_present(battery));
813 #ifdef CONFIG_ACPI_SYSFS_POWER
814 /* acpi_batter_update could remove power_supply object */
815 if (battery->bat.dev)
816 kobject_uevent(&battery->bat.dev->kobj, KOBJ_CHANGE);
820 static int acpi_battery_add(struct acpi_device *device)
823 struct acpi_battery *battery = NULL;
826 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
829 battery->device = device;
830 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
831 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
832 device->driver_data = battery;
833 mutex_init(&battery->lock);
834 acpi_battery_update(battery);
835 #ifdef CONFIG_ACPI_PROCFS_POWER
836 result = acpi_battery_add_fs(device);
839 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
840 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
841 device->status.battery_present ? "present" : "absent");
843 #ifdef CONFIG_ACPI_PROCFS_POWER
844 acpi_battery_remove_fs(device);
851 static int acpi_battery_remove(struct acpi_device *device, int type)
853 struct acpi_battery *battery = NULL;
855 if (!device || !acpi_driver_data(device))
857 battery = acpi_driver_data(device);
858 #ifdef CONFIG_ACPI_PROCFS_POWER
859 acpi_battery_remove_fs(device);
861 #ifdef CONFIG_ACPI_SYSFS_POWER
862 sysfs_remove_battery(battery);
864 mutex_destroy(&battery->lock);
869 /* this is needed to learn about changes made in suspended state */
870 static int acpi_battery_resume(struct acpi_device *device)
872 struct acpi_battery *battery;
875 battery = acpi_driver_data(device);
876 battery->update_time = 0;
877 acpi_battery_update(battery);
881 static struct acpi_driver acpi_battery_driver = {
883 .class = ACPI_BATTERY_CLASS,
884 .ids = battery_device_ids,
885 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
887 .add = acpi_battery_add,
888 .resume = acpi_battery_resume,
889 .remove = acpi_battery_remove,
890 .notify = acpi_battery_notify,
894 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
898 #ifdef CONFIG_ACPI_PROCFS_POWER
899 acpi_battery_dir = acpi_lock_battery_dir();
900 if (!acpi_battery_dir)
903 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
904 #ifdef CONFIG_ACPI_PROCFS_POWER
905 acpi_unlock_battery_dir(acpi_battery_dir);
912 static int __init acpi_battery_init(void)
914 async_schedule(acpi_battery_init_async, NULL);
918 static void __exit acpi_battery_exit(void)
920 acpi_bus_unregister_driver(&acpi_battery_driver);
921 #ifdef CONFIG_ACPI_PROCFS_POWER
922 acpi_unlock_battery_dir(acpi_battery_dir);
926 module_init(acpi_battery_init);
927 module_exit(acpi_battery_exit);