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>
34 #include <linux/dmi.h>
35 #include <linux/slab.h>
37 #ifdef CONFIG_ACPI_PROCFS_POWER
38 #include <linux/proc_fs.h>
39 #include <linux/seq_file.h>
40 #include <asm/uaccess.h>
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
46 #ifdef CONFIG_ACPI_SYSFS_POWER
47 #include <linux/power_supply.h>
50 #define PREFIX "ACPI: "
52 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
54 #define ACPI_BATTERY_CLASS "battery"
55 #define ACPI_BATTERY_DEVICE_NAME "Battery"
56 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
57 #define ACPI_BATTERY_NOTIFY_INFO 0x81
58 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
60 #define _COMPONENT ACPI_BATTERY_COMPONENT
62 ACPI_MODULE_NAME("battery");
64 MODULE_AUTHOR("Paul Diefenbaugh");
65 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
66 MODULE_DESCRIPTION("ACPI Battery Driver");
67 MODULE_LICENSE("GPL");
69 static unsigned int cache_time = 1000;
70 module_param(cache_time, uint, 0644);
71 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
73 #ifdef CONFIG_ACPI_PROCFS_POWER
74 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
75 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
77 enum acpi_battery_files {
81 ACPI_BATTERY_NUMFILES,
86 static const struct acpi_device_id battery_device_ids[] = {
91 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
94 ACPI_BATTERY_ALARM_PRESENT,
95 ACPI_BATTERY_XINFO_PRESENT,
96 /* For buggy DSDTs that report negative 16-bit values for either
97 * charging or discharging current and/or report 0 as 65536
100 ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
103 struct acpi_battery {
105 #ifdef CONFIG_ACPI_SYSFS_POWER
106 struct power_supply bat;
108 struct acpi_device *device;
109 unsigned long update_time;
114 int full_charge_capacity;
117 int design_capacity_warning;
118 int design_capacity_low;
120 int measurement_accuracy;
121 int max_sampling_time;
122 int min_sampling_time;
123 int max_averaging_interval;
124 int min_averaging_interval;
125 int capacity_granularity_1;
126 int capacity_granularity_2;
128 char model_number[32];
129 char serial_number[32];
137 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
139 inline int acpi_battery_present(struct acpi_battery *battery)
141 return battery->device->status.battery_present;
144 #ifdef CONFIG_ACPI_SYSFS_POWER
145 static int acpi_battery_technology(struct acpi_battery *battery)
147 if (!strcasecmp("NiCd", battery->type))
148 return POWER_SUPPLY_TECHNOLOGY_NiCd;
149 if (!strcasecmp("NiMH", battery->type))
150 return POWER_SUPPLY_TECHNOLOGY_NiMH;
151 if (!strcasecmp("LION", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_LION;
153 if (!strncasecmp("LI-ION", battery->type, 6))
154 return POWER_SUPPLY_TECHNOLOGY_LION;
155 if (!strcasecmp("LiP", battery->type))
156 return POWER_SUPPLY_TECHNOLOGY_LIPO;
157 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
160 static int acpi_battery_get_state(struct acpi_battery *battery);
162 static int acpi_battery_is_charged(struct acpi_battery *battery)
164 /* either charging or discharging */
165 if (battery->state != 0)
168 /* battery not reporting charge */
169 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
170 battery->capacity_now == 0)
173 /* good batteries update full_charge as the batteries degrade */
174 if (battery->full_charge_capacity == battery->capacity_now)
177 /* fallback to using design values for broken batteries */
178 if (battery->design_capacity == battery->capacity_now)
181 /* we don't do any sort of metric based on percentages */
185 static int acpi_battery_get_property(struct power_supply *psy,
186 enum power_supply_property psp,
187 union power_supply_propval *val)
190 struct acpi_battery *battery = to_acpi_battery(psy);
192 if (acpi_battery_present(battery)) {
193 /* run battery update only if it is present */
194 acpi_battery_get_state(battery);
195 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
198 case POWER_SUPPLY_PROP_STATUS:
199 if (battery->state & 0x01)
200 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
201 else if (battery->state & 0x02)
202 val->intval = POWER_SUPPLY_STATUS_CHARGING;
203 else if (acpi_battery_is_charged(battery))
204 val->intval = POWER_SUPPLY_STATUS_FULL;
206 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
208 case POWER_SUPPLY_PROP_PRESENT:
209 val->intval = acpi_battery_present(battery);
211 case POWER_SUPPLY_PROP_TECHNOLOGY:
212 val->intval = acpi_battery_technology(battery);
214 case POWER_SUPPLY_PROP_CYCLE_COUNT:
215 val->intval = battery->cycle_count;
217 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
218 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
221 val->intval = battery->design_voltage * 1000;
223 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
224 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
227 val->intval = battery->voltage_now * 1000;
229 case POWER_SUPPLY_PROP_CURRENT_NOW:
230 case POWER_SUPPLY_PROP_POWER_NOW:
231 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
234 val->intval = battery->rate_now * 1000;
236 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
237 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
238 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
241 val->intval = battery->design_capacity * 1000;
243 case POWER_SUPPLY_PROP_CHARGE_FULL:
244 case POWER_SUPPLY_PROP_ENERGY_FULL:
245 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
248 val->intval = battery->full_charge_capacity * 1000;
250 case POWER_SUPPLY_PROP_CHARGE_NOW:
251 case POWER_SUPPLY_PROP_ENERGY_NOW:
252 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
255 val->intval = battery->capacity_now * 1000;
257 case POWER_SUPPLY_PROP_MODEL_NAME:
258 val->strval = battery->model_number;
260 case POWER_SUPPLY_PROP_MANUFACTURER:
261 val->strval = battery->oem_info;
263 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
264 val->strval = battery->serial_number;
272 static enum power_supply_property charge_battery_props[] = {
273 POWER_SUPPLY_PROP_STATUS,
274 POWER_SUPPLY_PROP_PRESENT,
275 POWER_SUPPLY_PROP_TECHNOLOGY,
276 POWER_SUPPLY_PROP_CYCLE_COUNT,
277 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
278 POWER_SUPPLY_PROP_VOLTAGE_NOW,
279 POWER_SUPPLY_PROP_CURRENT_NOW,
280 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
281 POWER_SUPPLY_PROP_CHARGE_FULL,
282 POWER_SUPPLY_PROP_CHARGE_NOW,
283 POWER_SUPPLY_PROP_MODEL_NAME,
284 POWER_SUPPLY_PROP_MANUFACTURER,
285 POWER_SUPPLY_PROP_SERIAL_NUMBER,
288 static enum power_supply_property energy_battery_props[] = {
289 POWER_SUPPLY_PROP_STATUS,
290 POWER_SUPPLY_PROP_PRESENT,
291 POWER_SUPPLY_PROP_TECHNOLOGY,
292 POWER_SUPPLY_PROP_CYCLE_COUNT,
293 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
294 POWER_SUPPLY_PROP_VOLTAGE_NOW,
295 POWER_SUPPLY_PROP_POWER_NOW,
296 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
297 POWER_SUPPLY_PROP_ENERGY_FULL,
298 POWER_SUPPLY_PROP_ENERGY_NOW,
299 POWER_SUPPLY_PROP_MODEL_NAME,
300 POWER_SUPPLY_PROP_MANUFACTURER,
301 POWER_SUPPLY_PROP_SERIAL_NUMBER,
305 #ifdef CONFIG_ACPI_PROCFS_POWER
306 inline char *acpi_battery_units(struct acpi_battery *battery)
308 return (battery->power_unit)?"mA":"mW";
312 /* --------------------------------------------------------------------------
314 -------------------------------------------------------------------------- */
315 struct acpi_offsets {
316 size_t offset; /* offset inside struct acpi_sbs_battery */
317 u8 mode; /* int or string? */
320 static struct acpi_offsets state_offsets[] = {
321 {offsetof(struct acpi_battery, state), 0},
322 {offsetof(struct acpi_battery, rate_now), 0},
323 {offsetof(struct acpi_battery, capacity_now), 0},
324 {offsetof(struct acpi_battery, voltage_now), 0},
327 static struct acpi_offsets info_offsets[] = {
328 {offsetof(struct acpi_battery, power_unit), 0},
329 {offsetof(struct acpi_battery, design_capacity), 0},
330 {offsetof(struct acpi_battery, full_charge_capacity), 0},
331 {offsetof(struct acpi_battery, technology), 0},
332 {offsetof(struct acpi_battery, design_voltage), 0},
333 {offsetof(struct acpi_battery, design_capacity_warning), 0},
334 {offsetof(struct acpi_battery, design_capacity_low), 0},
335 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
336 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
337 {offsetof(struct acpi_battery, model_number), 1},
338 {offsetof(struct acpi_battery, serial_number), 1},
339 {offsetof(struct acpi_battery, type), 1},
340 {offsetof(struct acpi_battery, oem_info), 1},
343 static struct acpi_offsets extended_info_offsets[] = {
344 {offsetof(struct acpi_battery, power_unit), 0},
345 {offsetof(struct acpi_battery, design_capacity), 0},
346 {offsetof(struct acpi_battery, full_charge_capacity), 0},
347 {offsetof(struct acpi_battery, technology), 0},
348 {offsetof(struct acpi_battery, design_voltage), 0},
349 {offsetof(struct acpi_battery, design_capacity_warning), 0},
350 {offsetof(struct acpi_battery, design_capacity_low), 0},
351 {offsetof(struct acpi_battery, cycle_count), 0},
352 {offsetof(struct acpi_battery, measurement_accuracy), 0},
353 {offsetof(struct acpi_battery, max_sampling_time), 0},
354 {offsetof(struct acpi_battery, min_sampling_time), 0},
355 {offsetof(struct acpi_battery, max_averaging_interval), 0},
356 {offsetof(struct acpi_battery, min_averaging_interval), 0},
357 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
358 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
359 {offsetof(struct acpi_battery, model_number), 1},
360 {offsetof(struct acpi_battery, serial_number), 1},
361 {offsetof(struct acpi_battery, type), 1},
362 {offsetof(struct acpi_battery, oem_info), 1},
365 static int extract_package(struct acpi_battery *battery,
366 union acpi_object *package,
367 struct acpi_offsets *offsets, int num)
370 union acpi_object *element;
371 if (package->type != ACPI_TYPE_PACKAGE)
373 for (i = 0; i < num; ++i) {
374 if (package->package.count <= i)
376 element = &package->package.elements[i];
377 if (offsets[i].mode) {
378 u8 *ptr = (u8 *)battery + offsets[i].offset;
379 if (element->type == ACPI_TYPE_STRING ||
380 element->type == ACPI_TYPE_BUFFER)
381 strncpy(ptr, element->string.pointer, 32);
382 else if (element->type == ACPI_TYPE_INTEGER) {
383 strncpy(ptr, (u8 *)&element->integer.value,
385 ptr[sizeof(u64)] = 0;
387 *ptr = 0; /* don't have value */
389 int *x = (int *)((u8 *)battery + offsets[i].offset);
390 *x = (element->type == ACPI_TYPE_INTEGER) ?
391 element->integer.value : -1;
397 static int acpi_battery_get_status(struct acpi_battery *battery)
399 if (acpi_bus_get_status(battery->device)) {
400 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
406 static int acpi_battery_get_info(struct acpi_battery *battery)
408 int result = -EFAULT;
409 acpi_status status = 0;
410 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
413 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
415 if (!acpi_battery_present(battery))
417 mutex_lock(&battery->lock);
418 status = acpi_evaluate_object(battery->device->handle, name,
420 mutex_unlock(&battery->lock);
422 if (ACPI_FAILURE(status)) {
423 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
426 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
427 result = extract_package(battery, buffer.pointer,
428 extended_info_offsets,
429 ARRAY_SIZE(extended_info_offsets));
431 result = extract_package(battery, buffer.pointer,
432 info_offsets, ARRAY_SIZE(info_offsets));
433 kfree(buffer.pointer);
437 static int acpi_battery_get_state(struct acpi_battery *battery)
440 acpi_status status = 0;
441 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
443 if (!acpi_battery_present(battery))
446 if (battery->update_time &&
447 time_before(jiffies, battery->update_time +
448 msecs_to_jiffies(cache_time)))
451 mutex_lock(&battery->lock);
452 status = acpi_evaluate_object(battery->device->handle, "_BST",
454 mutex_unlock(&battery->lock);
456 if (ACPI_FAILURE(status)) {
457 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
461 result = extract_package(battery, buffer.pointer,
462 state_offsets, ARRAY_SIZE(state_offsets));
463 battery->update_time = jiffies;
464 kfree(buffer.pointer);
466 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
467 battery->rate_now != -1)
468 battery->rate_now = abs((s16)battery->rate_now);
473 static int acpi_battery_set_alarm(struct acpi_battery *battery)
475 acpi_status status = 0;
476 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
477 struct acpi_object_list arg_list = { 1, &arg0 };
479 if (!acpi_battery_present(battery) ||
480 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
483 arg0.integer.value = battery->alarm;
485 mutex_lock(&battery->lock);
486 status = acpi_evaluate_object(battery->device->handle, "_BTP",
488 mutex_unlock(&battery->lock);
490 if (ACPI_FAILURE(status))
493 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
497 static int acpi_battery_init_alarm(struct acpi_battery *battery)
499 acpi_status status = AE_OK;
500 acpi_handle handle = NULL;
502 /* See if alarms are supported, and if so, set default */
503 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
504 if (ACPI_FAILURE(status)) {
505 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
508 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
510 battery->alarm = battery->design_capacity_warning;
511 return acpi_battery_set_alarm(battery);
514 #ifdef CONFIG_ACPI_SYSFS_POWER
515 static ssize_t acpi_battery_alarm_show(struct device *dev,
516 struct device_attribute *attr,
519 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
520 return sprintf(buf, "%d\n", battery->alarm * 1000);
523 static ssize_t acpi_battery_alarm_store(struct device *dev,
524 struct device_attribute *attr,
525 const char *buf, size_t count)
528 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
529 if (sscanf(buf, "%ld\n", &x) == 1)
530 battery->alarm = x/1000;
531 if (acpi_battery_present(battery))
532 acpi_battery_set_alarm(battery);
536 static struct device_attribute alarm_attr = {
537 .attr = {.name = "alarm", .mode = 0644},
538 .show = acpi_battery_alarm_show,
539 .store = acpi_battery_alarm_store,
542 static int sysfs_add_battery(struct acpi_battery *battery)
546 if (battery->power_unit) {
547 battery->bat.properties = charge_battery_props;
548 battery->bat.num_properties =
549 ARRAY_SIZE(charge_battery_props);
551 battery->bat.properties = energy_battery_props;
552 battery->bat.num_properties =
553 ARRAY_SIZE(energy_battery_props);
556 battery->bat.name = acpi_device_bid(battery->device);
557 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
558 battery->bat.get_property = acpi_battery_get_property;
560 result = power_supply_register(&battery->device->dev, &battery->bat);
563 return device_create_file(battery->bat.dev, &alarm_attr);
566 static void sysfs_remove_battery(struct acpi_battery *battery)
568 if (!battery->bat.dev)
570 device_remove_file(battery->bat.dev, &alarm_attr);
571 power_supply_unregister(&battery->bat);
572 battery->bat.dev = NULL;
576 static void acpi_battery_quirks(struct acpi_battery *battery)
578 if (dmi_name_in_vendors("Acer") && battery->power_unit) {
579 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
583 static int acpi_battery_update(struct acpi_battery *battery)
585 int result, old_present = acpi_battery_present(battery);
586 result = acpi_battery_get_status(battery);
589 if (!acpi_battery_present(battery)) {
590 #ifdef CONFIG_ACPI_SYSFS_POWER
591 sysfs_remove_battery(battery);
593 battery->update_time = 0;
596 if (!battery->update_time ||
597 old_present != acpi_battery_present(battery)) {
598 result = acpi_battery_get_info(battery);
601 acpi_battery_quirks(battery);
602 acpi_battery_init_alarm(battery);
604 #ifdef CONFIG_ACPI_SYSFS_POWER
605 if (!battery->bat.dev)
606 sysfs_add_battery(battery);
608 return acpi_battery_get_state(battery);
611 /* --------------------------------------------------------------------------
613 -------------------------------------------------------------------------- */
615 #ifdef CONFIG_ACPI_PROCFS_POWER
616 static struct proc_dir_entry *acpi_battery_dir;
618 static int acpi_battery_print_info(struct seq_file *seq, int result)
620 struct acpi_battery *battery = seq->private;
625 seq_printf(seq, "present: %s\n",
626 acpi_battery_present(battery)?"yes":"no");
627 if (!acpi_battery_present(battery))
629 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
630 seq_printf(seq, "design capacity: unknown\n");
632 seq_printf(seq, "design capacity: %d %sh\n",
633 battery->design_capacity,
634 acpi_battery_units(battery));
636 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
637 seq_printf(seq, "last full capacity: unknown\n");
639 seq_printf(seq, "last full capacity: %d %sh\n",
640 battery->full_charge_capacity,
641 acpi_battery_units(battery));
643 seq_printf(seq, "battery technology: %srechargeable\n",
644 (!battery->technology)?"non-":"");
646 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
647 seq_printf(seq, "design voltage: unknown\n");
649 seq_printf(seq, "design voltage: %d mV\n",
650 battery->design_voltage);
651 seq_printf(seq, "design capacity warning: %d %sh\n",
652 battery->design_capacity_warning,
653 acpi_battery_units(battery));
654 seq_printf(seq, "design capacity low: %d %sh\n",
655 battery->design_capacity_low,
656 acpi_battery_units(battery));
657 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
658 seq_printf(seq, "capacity granularity 1: %d %sh\n",
659 battery->capacity_granularity_1,
660 acpi_battery_units(battery));
661 seq_printf(seq, "capacity granularity 2: %d %sh\n",
662 battery->capacity_granularity_2,
663 acpi_battery_units(battery));
664 seq_printf(seq, "model number: %s\n", battery->model_number);
665 seq_printf(seq, "serial number: %s\n", battery->serial_number);
666 seq_printf(seq, "battery type: %s\n", battery->type);
667 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
670 seq_printf(seq, "ERROR: Unable to read battery info\n");
674 static int acpi_battery_print_state(struct seq_file *seq, int result)
676 struct acpi_battery *battery = seq->private;
681 seq_printf(seq, "present: %s\n",
682 acpi_battery_present(battery)?"yes":"no");
683 if (!acpi_battery_present(battery))
686 seq_printf(seq, "capacity state: %s\n",
687 (battery->state & 0x04)?"critical":"ok");
688 if ((battery->state & 0x01) && (battery->state & 0x02))
690 "charging state: charging/discharging\n");
691 else if (battery->state & 0x01)
692 seq_printf(seq, "charging state: discharging\n");
693 else if (battery->state & 0x02)
694 seq_printf(seq, "charging state: charging\n");
696 seq_printf(seq, "charging state: charged\n");
698 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
699 seq_printf(seq, "present rate: unknown\n");
701 seq_printf(seq, "present rate: %d %s\n",
702 battery->rate_now, acpi_battery_units(battery));
704 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
705 seq_printf(seq, "remaining capacity: unknown\n");
707 seq_printf(seq, "remaining capacity: %d %sh\n",
708 battery->capacity_now, acpi_battery_units(battery));
709 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
710 seq_printf(seq, "present voltage: unknown\n");
712 seq_printf(seq, "present voltage: %d mV\n",
713 battery->voltage_now);
716 seq_printf(seq, "ERROR: Unable to read battery state\n");
721 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
723 struct acpi_battery *battery = seq->private;
728 if (!acpi_battery_present(battery)) {
729 seq_printf(seq, "present: no\n");
732 seq_printf(seq, "alarm: ");
734 seq_printf(seq, "unsupported\n");
736 seq_printf(seq, "%u %sh\n", battery->alarm,
737 acpi_battery_units(battery));
740 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
744 static ssize_t acpi_battery_write_alarm(struct file *file,
745 const char __user * buffer,
746 size_t count, loff_t * ppos)
749 char alarm_string[12] = { '\0' };
750 struct seq_file *m = file->private_data;
751 struct acpi_battery *battery = m->private;
753 if (!battery || (count > sizeof(alarm_string) - 1))
755 if (!acpi_battery_present(battery)) {
759 if (copy_from_user(alarm_string, buffer, count)) {
763 alarm_string[count] = '\0';
764 battery->alarm = simple_strtol(alarm_string, NULL, 0);
765 result = acpi_battery_set_alarm(battery);
772 typedef int(*print_func)(struct seq_file *seq, int result);
774 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
775 acpi_battery_print_info,
776 acpi_battery_print_state,
777 acpi_battery_print_alarm,
780 static int acpi_battery_read(int fid, struct seq_file *seq)
782 struct acpi_battery *battery = seq->private;
783 int result = acpi_battery_update(battery);
784 return acpi_print_funcs[fid](seq, result);
787 #define DECLARE_FILE_FUNCTIONS(_name) \
788 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
790 return acpi_battery_read(_name##_tag, seq); \
792 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
794 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
797 DECLARE_FILE_FUNCTIONS(info);
798 DECLARE_FILE_FUNCTIONS(state);
799 DECLARE_FILE_FUNCTIONS(alarm);
801 #undef DECLARE_FILE_FUNCTIONS
803 #define FILE_DESCRIPTION_RO(_name) \
805 .name = __stringify(_name), \
808 .open = acpi_battery_##_name##_open_fs, \
810 .llseek = seq_lseek, \
811 .release = single_release, \
812 .owner = THIS_MODULE, \
816 #define FILE_DESCRIPTION_RW(_name) \
818 .name = __stringify(_name), \
819 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
821 .open = acpi_battery_##_name##_open_fs, \
823 .llseek = seq_lseek, \
824 .write = acpi_battery_write_##_name, \
825 .release = single_release, \
826 .owner = THIS_MODULE, \
830 static struct battery_file {
831 struct file_operations ops;
834 } acpi_battery_file[] = {
835 FILE_DESCRIPTION_RO(info),
836 FILE_DESCRIPTION_RO(state),
837 FILE_DESCRIPTION_RW(alarm),
840 #undef FILE_DESCRIPTION_RO
841 #undef FILE_DESCRIPTION_RW
843 static int acpi_battery_add_fs(struct acpi_device *device)
845 struct proc_dir_entry *entry = NULL;
848 if (!acpi_device_dir(device)) {
849 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
851 if (!acpi_device_dir(device))
855 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
856 entry = proc_create_data(acpi_battery_file[i].name,
857 acpi_battery_file[i].mode,
858 acpi_device_dir(device),
859 &acpi_battery_file[i].ops,
860 acpi_driver_data(device));
867 static void acpi_battery_remove_fs(struct acpi_device *device)
870 if (!acpi_device_dir(device))
872 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
873 remove_proc_entry(acpi_battery_file[i].name,
874 acpi_device_dir(device));
876 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
877 acpi_device_dir(device) = NULL;
882 /* --------------------------------------------------------------------------
884 -------------------------------------------------------------------------- */
886 static void acpi_battery_notify(struct acpi_device *device, u32 event)
888 struct acpi_battery *battery = acpi_driver_data(device);
889 #ifdef CONFIG_ACPI_SYSFS_POWER
895 #ifdef CONFIG_ACPI_SYSFS_POWER
896 old = battery->bat.dev;
898 acpi_battery_update(battery);
899 acpi_bus_generate_proc_event(device, event,
900 acpi_battery_present(battery));
901 acpi_bus_generate_netlink_event(device->pnp.device_class,
902 dev_name(&device->dev), event,
903 acpi_battery_present(battery));
904 #ifdef CONFIG_ACPI_SYSFS_POWER
905 /* acpi_battery_update could remove power_supply object */
906 if (old && battery->bat.dev)
907 power_supply_changed(&battery->bat);
911 static int acpi_battery_add(struct acpi_device *device)
914 struct acpi_battery *battery = NULL;
918 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
921 battery->device = device;
922 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
923 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
924 device->driver_data = battery;
925 mutex_init(&battery->lock);
926 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
928 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
929 acpi_battery_update(battery);
930 #ifdef CONFIG_ACPI_PROCFS_POWER
931 result = acpi_battery_add_fs(device);
934 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
935 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
936 device->status.battery_present ? "present" : "absent");
938 #ifdef CONFIG_ACPI_PROCFS_POWER
939 acpi_battery_remove_fs(device);
946 static int acpi_battery_remove(struct acpi_device *device, int type)
948 struct acpi_battery *battery = NULL;
950 if (!device || !acpi_driver_data(device))
952 battery = acpi_driver_data(device);
953 #ifdef CONFIG_ACPI_PROCFS_POWER
954 acpi_battery_remove_fs(device);
956 #ifdef CONFIG_ACPI_SYSFS_POWER
957 sysfs_remove_battery(battery);
959 mutex_destroy(&battery->lock);
964 /* this is needed to learn about changes made in suspended state */
965 static int acpi_battery_resume(struct acpi_device *device)
967 struct acpi_battery *battery;
970 battery = acpi_driver_data(device);
971 battery->update_time = 0;
972 acpi_battery_update(battery);
976 static struct acpi_driver acpi_battery_driver = {
978 .class = ACPI_BATTERY_CLASS,
979 .ids = battery_device_ids,
980 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
982 .add = acpi_battery_add,
983 .resume = acpi_battery_resume,
984 .remove = acpi_battery_remove,
985 .notify = acpi_battery_notify,
989 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
993 #ifdef CONFIG_ACPI_PROCFS_POWER
994 acpi_battery_dir = acpi_lock_battery_dir();
995 if (!acpi_battery_dir)
998 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
999 #ifdef CONFIG_ACPI_PROCFS_POWER
1000 acpi_unlock_battery_dir(acpi_battery_dir);
1007 static int __init acpi_battery_init(void)
1009 async_schedule(acpi_battery_init_async, NULL);
1013 static void __exit acpi_battery_exit(void)
1015 acpi_bus_unregister_driver(&acpi_battery_driver);
1016 #ifdef CONFIG_ACPI_PROCFS_POWER
1017 acpi_unlock_battery_dir(acpi_battery_dir);
1021 module_init(acpi_battery_init);
1022 module_exit(acpi_battery_exit);