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,
101 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
104 struct acpi_battery {
106 #ifdef CONFIG_ACPI_SYSFS_POWER
107 struct power_supply bat;
109 struct acpi_device *device;
110 unsigned long update_time;
115 int full_charge_capacity;
118 int design_capacity_warning;
119 int design_capacity_low;
121 int measurement_accuracy;
122 int max_sampling_time;
123 int min_sampling_time;
124 int max_averaging_interval;
125 int min_averaging_interval;
126 int capacity_granularity_1;
127 int capacity_granularity_2;
129 char model_number[32];
130 char serial_number[32];
138 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
140 inline int acpi_battery_present(struct acpi_battery *battery)
142 return battery->device->status.battery_present;
145 #ifdef CONFIG_ACPI_SYSFS_POWER
146 static int acpi_battery_technology(struct acpi_battery *battery)
148 if (!strcasecmp("NiCd", battery->type))
149 return POWER_SUPPLY_TECHNOLOGY_NiCd;
150 if (!strcasecmp("NiMH", battery->type))
151 return POWER_SUPPLY_TECHNOLOGY_NiMH;
152 if (!strcasecmp("LION", battery->type))
153 return POWER_SUPPLY_TECHNOLOGY_LION;
154 if (!strncasecmp("LI-ION", battery->type, 6))
155 return POWER_SUPPLY_TECHNOLOGY_LION;
156 if (!strcasecmp("LiP", battery->type))
157 return POWER_SUPPLY_TECHNOLOGY_LIPO;
158 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
161 static int acpi_battery_get_state(struct acpi_battery *battery);
163 static int acpi_battery_is_charged(struct acpi_battery *battery)
165 /* either charging or discharging */
166 if (battery->state != 0)
169 /* battery not reporting charge */
170 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
171 battery->capacity_now == 0)
174 /* good batteries update full_charge as the batteries degrade */
175 if (battery->full_charge_capacity == battery->capacity_now)
178 /* fallback to using design values for broken batteries */
179 if (battery->design_capacity == battery->capacity_now)
182 /* we don't do any sort of metric based on percentages */
186 static int acpi_battery_get_property(struct power_supply *psy,
187 enum power_supply_property psp,
188 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 val->intval = battery->design_voltage * 1000;
220 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
221 val->intval = battery->voltage_now * 1000;
223 case POWER_SUPPLY_PROP_CURRENT_NOW:
224 case POWER_SUPPLY_PROP_POWER_NOW:
225 val->intval = battery->rate_now * 1000;
227 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
228 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
229 val->intval = battery->design_capacity * 1000;
231 case POWER_SUPPLY_PROP_CHARGE_FULL:
232 case POWER_SUPPLY_PROP_ENERGY_FULL:
233 val->intval = battery->full_charge_capacity * 1000;
235 case POWER_SUPPLY_PROP_CHARGE_NOW:
236 case POWER_SUPPLY_PROP_ENERGY_NOW:
237 val->intval = battery->capacity_now * 1000;
239 case POWER_SUPPLY_PROP_MODEL_NAME:
240 val->strval = battery->model_number;
242 case POWER_SUPPLY_PROP_MANUFACTURER:
243 val->strval = battery->oem_info;
245 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
246 val->strval = battery->serial_number;
254 static enum power_supply_property charge_battery_props[] = {
255 POWER_SUPPLY_PROP_STATUS,
256 POWER_SUPPLY_PROP_PRESENT,
257 POWER_SUPPLY_PROP_TECHNOLOGY,
258 POWER_SUPPLY_PROP_CYCLE_COUNT,
259 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
260 POWER_SUPPLY_PROP_VOLTAGE_NOW,
261 POWER_SUPPLY_PROP_CURRENT_NOW,
262 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
263 POWER_SUPPLY_PROP_CHARGE_FULL,
264 POWER_SUPPLY_PROP_CHARGE_NOW,
265 POWER_SUPPLY_PROP_MODEL_NAME,
266 POWER_SUPPLY_PROP_MANUFACTURER,
267 POWER_SUPPLY_PROP_SERIAL_NUMBER,
270 static enum power_supply_property energy_battery_props[] = {
271 POWER_SUPPLY_PROP_STATUS,
272 POWER_SUPPLY_PROP_PRESENT,
273 POWER_SUPPLY_PROP_TECHNOLOGY,
274 POWER_SUPPLY_PROP_CYCLE_COUNT,
275 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
276 POWER_SUPPLY_PROP_VOLTAGE_NOW,
277 POWER_SUPPLY_PROP_POWER_NOW,
278 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
279 POWER_SUPPLY_PROP_ENERGY_FULL,
280 POWER_SUPPLY_PROP_ENERGY_NOW,
281 POWER_SUPPLY_PROP_MODEL_NAME,
282 POWER_SUPPLY_PROP_MANUFACTURER,
283 POWER_SUPPLY_PROP_SERIAL_NUMBER,
287 #ifdef CONFIG_ACPI_PROCFS_POWER
288 inline char *acpi_battery_units(struct acpi_battery *battery)
290 return (battery->power_unit)?"mA":"mW";
294 /* --------------------------------------------------------------------------
296 -------------------------------------------------------------------------- */
297 struct acpi_offsets {
298 size_t offset; /* offset inside struct acpi_sbs_battery */
299 u8 mode; /* int or string? */
302 static struct acpi_offsets state_offsets[] = {
303 {offsetof(struct acpi_battery, state), 0},
304 {offsetof(struct acpi_battery, rate_now), 0},
305 {offsetof(struct acpi_battery, capacity_now), 0},
306 {offsetof(struct acpi_battery, voltage_now), 0},
309 static struct acpi_offsets info_offsets[] = {
310 {offsetof(struct acpi_battery, power_unit), 0},
311 {offsetof(struct acpi_battery, design_capacity), 0},
312 {offsetof(struct acpi_battery, full_charge_capacity), 0},
313 {offsetof(struct acpi_battery, technology), 0},
314 {offsetof(struct acpi_battery, design_voltage), 0},
315 {offsetof(struct acpi_battery, design_capacity_warning), 0},
316 {offsetof(struct acpi_battery, design_capacity_low), 0},
317 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
318 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
319 {offsetof(struct acpi_battery, model_number), 1},
320 {offsetof(struct acpi_battery, serial_number), 1},
321 {offsetof(struct acpi_battery, type), 1},
322 {offsetof(struct acpi_battery, oem_info), 1},
325 static struct acpi_offsets extended_info_offsets[] = {
326 {offsetof(struct acpi_battery, power_unit), 0},
327 {offsetof(struct acpi_battery, design_capacity), 0},
328 {offsetof(struct acpi_battery, full_charge_capacity), 0},
329 {offsetof(struct acpi_battery, technology), 0},
330 {offsetof(struct acpi_battery, design_voltage), 0},
331 {offsetof(struct acpi_battery, design_capacity_warning), 0},
332 {offsetof(struct acpi_battery, design_capacity_low), 0},
333 {offsetof(struct acpi_battery, cycle_count), 0},
334 {offsetof(struct acpi_battery, measurement_accuracy), 0},
335 {offsetof(struct acpi_battery, max_sampling_time), 0},
336 {offsetof(struct acpi_battery, min_sampling_time), 0},
337 {offsetof(struct acpi_battery, max_averaging_interval), 0},
338 {offsetof(struct acpi_battery, min_averaging_interval), 0},
339 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
340 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
341 {offsetof(struct acpi_battery, model_number), 1},
342 {offsetof(struct acpi_battery, serial_number), 1},
343 {offsetof(struct acpi_battery, type), 1},
344 {offsetof(struct acpi_battery, oem_info), 1},
347 static int extract_package(struct acpi_battery *battery,
348 union acpi_object *package,
349 struct acpi_offsets *offsets, int num)
352 union acpi_object *element;
353 if (package->type != ACPI_TYPE_PACKAGE)
355 for (i = 0; i < num; ++i) {
356 if (package->package.count <= i)
358 element = &package->package.elements[i];
359 if (offsets[i].mode) {
360 u8 *ptr = (u8 *)battery + offsets[i].offset;
361 if (element->type == ACPI_TYPE_STRING ||
362 element->type == ACPI_TYPE_BUFFER)
363 strncpy(ptr, element->string.pointer, 32);
364 else if (element->type == ACPI_TYPE_INTEGER) {
365 strncpy(ptr, (u8 *)&element->integer.value,
367 ptr[sizeof(u64)] = 0;
369 *ptr = 0; /* don't have value */
371 int *x = (int *)((u8 *)battery + offsets[i].offset);
372 *x = (element->type == ACPI_TYPE_INTEGER) ?
373 element->integer.value : -1;
379 static int acpi_battery_get_status(struct acpi_battery *battery)
381 if (acpi_bus_get_status(battery->device)) {
382 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
388 static int acpi_battery_get_info(struct acpi_battery *battery)
390 int result = -EFAULT;
391 acpi_status status = 0;
392 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
395 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
397 if (!acpi_battery_present(battery))
399 mutex_lock(&battery->lock);
400 status = acpi_evaluate_object(battery->device->handle, name,
402 mutex_unlock(&battery->lock);
404 if (ACPI_FAILURE(status)) {
405 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
408 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
409 result = extract_package(battery, buffer.pointer,
410 extended_info_offsets,
411 ARRAY_SIZE(extended_info_offsets));
413 result = extract_package(battery, buffer.pointer,
414 info_offsets, ARRAY_SIZE(info_offsets));
415 kfree(buffer.pointer);
416 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
417 battery->full_charge_capacity = battery->design_capacity;
421 static int acpi_battery_get_state(struct acpi_battery *battery)
424 acpi_status status = 0;
425 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
427 if (!acpi_battery_present(battery))
430 if (battery->update_time &&
431 time_before(jiffies, battery->update_time +
432 msecs_to_jiffies(cache_time)))
435 mutex_lock(&battery->lock);
436 status = acpi_evaluate_object(battery->device->handle, "_BST",
438 mutex_unlock(&battery->lock);
440 if (ACPI_FAILURE(status)) {
441 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
445 result = extract_package(battery, buffer.pointer,
446 state_offsets, ARRAY_SIZE(state_offsets));
447 battery->update_time = jiffies;
448 kfree(buffer.pointer);
450 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
451 battery->rate_now != -1)
452 battery->rate_now = abs((s16)battery->rate_now);
454 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
455 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
456 battery->capacity_now = (battery->capacity_now *
457 battery->full_charge_capacity) / 100;
461 static int acpi_battery_set_alarm(struct acpi_battery *battery)
463 acpi_status status = 0;
464 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
465 struct acpi_object_list arg_list = { 1, &arg0 };
467 if (!acpi_battery_present(battery) ||
468 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
471 arg0.integer.value = battery->alarm;
473 mutex_lock(&battery->lock);
474 status = acpi_evaluate_object(battery->device->handle, "_BTP",
476 mutex_unlock(&battery->lock);
478 if (ACPI_FAILURE(status))
481 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
485 static int acpi_battery_init_alarm(struct acpi_battery *battery)
487 acpi_status status = AE_OK;
488 acpi_handle handle = NULL;
490 /* See if alarms are supported, and if so, set default */
491 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
492 if (ACPI_FAILURE(status)) {
493 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
496 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
498 battery->alarm = battery->design_capacity_warning;
499 return acpi_battery_set_alarm(battery);
502 #ifdef CONFIG_ACPI_SYSFS_POWER
503 static ssize_t acpi_battery_alarm_show(struct device *dev,
504 struct device_attribute *attr,
507 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
508 return sprintf(buf, "%d\n", battery->alarm * 1000);
511 static ssize_t acpi_battery_alarm_store(struct device *dev,
512 struct device_attribute *attr,
513 const char *buf, size_t count)
516 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
517 if (sscanf(buf, "%ld\n", &x) == 1)
518 battery->alarm = x/1000;
519 if (acpi_battery_present(battery))
520 acpi_battery_set_alarm(battery);
524 static struct device_attribute alarm_attr = {
525 .attr = {.name = "alarm", .mode = 0644},
526 .show = acpi_battery_alarm_show,
527 .store = acpi_battery_alarm_store,
530 static int sysfs_add_battery(struct acpi_battery *battery)
534 if (battery->power_unit) {
535 battery->bat.properties = charge_battery_props;
536 battery->bat.num_properties =
537 ARRAY_SIZE(charge_battery_props);
539 battery->bat.properties = energy_battery_props;
540 battery->bat.num_properties =
541 ARRAY_SIZE(energy_battery_props);
544 battery->bat.name = acpi_device_bid(battery->device);
545 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
546 battery->bat.get_property = acpi_battery_get_property;
548 result = power_supply_register(&battery->device->dev, &battery->bat);
551 return device_create_file(battery->bat.dev, &alarm_attr);
554 static void sysfs_remove_battery(struct acpi_battery *battery)
556 if (!battery->bat.dev)
558 device_remove_file(battery->bat.dev, &alarm_attr);
559 power_supply_unregister(&battery->bat);
560 battery->bat.dev = NULL;
564 static void acpi_battery_quirks(struct acpi_battery *battery)
566 if (dmi_name_in_vendors("Acer") && battery->power_unit) {
567 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
572 * According to the ACPI spec, some kinds of primary batteries can
573 * report percentage battery remaining capacity directly to OS.
574 * In this case, it reports the Last Full Charged Capacity == 100
575 * and BatteryPresentRate == 0xFFFFFFFF.
577 * Now we found some battery reports percentage remaining capacity
578 * even if it's rechargeable.
579 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
581 * Handle this correctly so that they won't break userspace.
583 static void acpi_battery_quirks2(struct acpi_battery *battery)
585 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
588 if (battery->full_charge_capacity == 100 &&
589 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
590 battery->capacity_now >=0 && battery->capacity_now <= 100) {
591 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
592 battery->full_charge_capacity = battery->design_capacity;
593 battery->capacity_now = (battery->capacity_now *
594 battery->full_charge_capacity) / 100;
598 static int acpi_battery_update(struct acpi_battery *battery)
600 int result, old_present = acpi_battery_present(battery);
601 result = acpi_battery_get_status(battery);
604 if (!acpi_battery_present(battery)) {
605 #ifdef CONFIG_ACPI_SYSFS_POWER
606 sysfs_remove_battery(battery);
608 battery->update_time = 0;
611 if (!battery->update_time ||
612 old_present != acpi_battery_present(battery)) {
613 result = acpi_battery_get_info(battery);
616 acpi_battery_quirks(battery);
617 acpi_battery_init_alarm(battery);
619 #ifdef CONFIG_ACPI_SYSFS_POWER
620 if (!battery->bat.dev)
621 sysfs_add_battery(battery);
623 result = acpi_battery_get_state(battery);
624 acpi_battery_quirks2(battery);
628 /* --------------------------------------------------------------------------
630 -------------------------------------------------------------------------- */
632 #ifdef CONFIG_ACPI_PROCFS_POWER
633 static struct proc_dir_entry *acpi_battery_dir;
635 static int acpi_battery_print_info(struct seq_file *seq, int result)
637 struct acpi_battery *battery = seq->private;
642 seq_printf(seq, "present: %s\n",
643 acpi_battery_present(battery)?"yes":"no");
644 if (!acpi_battery_present(battery))
646 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
647 seq_printf(seq, "design capacity: unknown\n");
649 seq_printf(seq, "design capacity: %d %sh\n",
650 battery->design_capacity,
651 acpi_battery_units(battery));
653 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
654 seq_printf(seq, "last full capacity: unknown\n");
656 seq_printf(seq, "last full capacity: %d %sh\n",
657 battery->full_charge_capacity,
658 acpi_battery_units(battery));
660 seq_printf(seq, "battery technology: %srechargeable\n",
661 (!battery->technology)?"non-":"");
663 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
664 seq_printf(seq, "design voltage: unknown\n");
666 seq_printf(seq, "design voltage: %d mV\n",
667 battery->design_voltage);
668 seq_printf(seq, "design capacity warning: %d %sh\n",
669 battery->design_capacity_warning,
670 acpi_battery_units(battery));
671 seq_printf(seq, "design capacity low: %d %sh\n",
672 battery->design_capacity_low,
673 acpi_battery_units(battery));
674 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
675 seq_printf(seq, "capacity granularity 1: %d %sh\n",
676 battery->capacity_granularity_1,
677 acpi_battery_units(battery));
678 seq_printf(seq, "capacity granularity 2: %d %sh\n",
679 battery->capacity_granularity_2,
680 acpi_battery_units(battery));
681 seq_printf(seq, "model number: %s\n", battery->model_number);
682 seq_printf(seq, "serial number: %s\n", battery->serial_number);
683 seq_printf(seq, "battery type: %s\n", battery->type);
684 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
687 seq_printf(seq, "ERROR: Unable to read battery info\n");
691 static int acpi_battery_print_state(struct seq_file *seq, int result)
693 struct acpi_battery *battery = seq->private;
698 seq_printf(seq, "present: %s\n",
699 acpi_battery_present(battery)?"yes":"no");
700 if (!acpi_battery_present(battery))
703 seq_printf(seq, "capacity state: %s\n",
704 (battery->state & 0x04)?"critical":"ok");
705 if ((battery->state & 0x01) && (battery->state & 0x02))
707 "charging state: charging/discharging\n");
708 else if (battery->state & 0x01)
709 seq_printf(seq, "charging state: discharging\n");
710 else if (battery->state & 0x02)
711 seq_printf(seq, "charging state: charging\n");
713 seq_printf(seq, "charging state: charged\n");
715 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
716 seq_printf(seq, "present rate: unknown\n");
718 seq_printf(seq, "present rate: %d %s\n",
719 battery->rate_now, acpi_battery_units(battery));
721 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
722 seq_printf(seq, "remaining capacity: unknown\n");
724 seq_printf(seq, "remaining capacity: %d %sh\n",
725 battery->capacity_now, acpi_battery_units(battery));
726 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
727 seq_printf(seq, "present voltage: unknown\n");
729 seq_printf(seq, "present voltage: %d mV\n",
730 battery->voltage_now);
733 seq_printf(seq, "ERROR: Unable to read battery state\n");
738 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
740 struct acpi_battery *battery = seq->private;
745 if (!acpi_battery_present(battery)) {
746 seq_printf(seq, "present: no\n");
749 seq_printf(seq, "alarm: ");
751 seq_printf(seq, "unsupported\n");
753 seq_printf(seq, "%u %sh\n", battery->alarm,
754 acpi_battery_units(battery));
757 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
761 static ssize_t acpi_battery_write_alarm(struct file *file,
762 const char __user * buffer,
763 size_t count, loff_t * ppos)
766 char alarm_string[12] = { '\0' };
767 struct seq_file *m = file->private_data;
768 struct acpi_battery *battery = m->private;
770 if (!battery || (count > sizeof(alarm_string) - 1))
772 if (!acpi_battery_present(battery)) {
776 if (copy_from_user(alarm_string, buffer, count)) {
780 alarm_string[count] = '\0';
781 battery->alarm = simple_strtol(alarm_string, NULL, 0);
782 result = acpi_battery_set_alarm(battery);
789 typedef int(*print_func)(struct seq_file *seq, int result);
791 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
792 acpi_battery_print_info,
793 acpi_battery_print_state,
794 acpi_battery_print_alarm,
797 static int acpi_battery_read(int fid, struct seq_file *seq)
799 struct acpi_battery *battery = seq->private;
800 int result = acpi_battery_update(battery);
801 return acpi_print_funcs[fid](seq, result);
804 #define DECLARE_FILE_FUNCTIONS(_name) \
805 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
807 return acpi_battery_read(_name##_tag, seq); \
809 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
811 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
814 DECLARE_FILE_FUNCTIONS(info);
815 DECLARE_FILE_FUNCTIONS(state);
816 DECLARE_FILE_FUNCTIONS(alarm);
818 #undef DECLARE_FILE_FUNCTIONS
820 #define FILE_DESCRIPTION_RO(_name) \
822 .name = __stringify(_name), \
825 .open = acpi_battery_##_name##_open_fs, \
827 .llseek = seq_lseek, \
828 .release = single_release, \
829 .owner = THIS_MODULE, \
833 #define FILE_DESCRIPTION_RW(_name) \
835 .name = __stringify(_name), \
836 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
838 .open = acpi_battery_##_name##_open_fs, \
840 .llseek = seq_lseek, \
841 .write = acpi_battery_write_##_name, \
842 .release = single_release, \
843 .owner = THIS_MODULE, \
847 static struct battery_file {
848 struct file_operations ops;
851 } acpi_battery_file[] = {
852 FILE_DESCRIPTION_RO(info),
853 FILE_DESCRIPTION_RO(state),
854 FILE_DESCRIPTION_RW(alarm),
857 #undef FILE_DESCRIPTION_RO
858 #undef FILE_DESCRIPTION_RW
860 static int acpi_battery_add_fs(struct acpi_device *device)
862 struct proc_dir_entry *entry = NULL;
865 if (!acpi_device_dir(device)) {
866 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
868 if (!acpi_device_dir(device))
872 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
873 entry = proc_create_data(acpi_battery_file[i].name,
874 acpi_battery_file[i].mode,
875 acpi_device_dir(device),
876 &acpi_battery_file[i].ops,
877 acpi_driver_data(device));
884 static void acpi_battery_remove_fs(struct acpi_device *device)
887 if (!acpi_device_dir(device))
889 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
890 remove_proc_entry(acpi_battery_file[i].name,
891 acpi_device_dir(device));
893 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
894 acpi_device_dir(device) = NULL;
899 /* --------------------------------------------------------------------------
901 -------------------------------------------------------------------------- */
903 static void acpi_battery_notify(struct acpi_device *device, u32 event)
905 struct acpi_battery *battery = acpi_driver_data(device);
906 #ifdef CONFIG_ACPI_SYSFS_POWER
912 #ifdef CONFIG_ACPI_SYSFS_POWER
913 old = battery->bat.dev;
915 acpi_battery_update(battery);
916 acpi_bus_generate_proc_event(device, event,
917 acpi_battery_present(battery));
918 acpi_bus_generate_netlink_event(device->pnp.device_class,
919 dev_name(&device->dev), event,
920 acpi_battery_present(battery));
921 #ifdef CONFIG_ACPI_SYSFS_POWER
922 /* acpi_battery_update could remove power_supply object */
923 if (old && battery->bat.dev)
924 power_supply_changed(&battery->bat);
928 static int acpi_battery_add(struct acpi_device *device)
931 struct acpi_battery *battery = NULL;
935 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
938 battery->device = device;
939 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
940 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
941 device->driver_data = battery;
942 mutex_init(&battery->lock);
943 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
945 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
946 acpi_battery_update(battery);
947 #ifdef CONFIG_ACPI_PROCFS_POWER
948 result = acpi_battery_add_fs(device);
951 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
952 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
953 device->status.battery_present ? "present" : "absent");
955 #ifdef CONFIG_ACPI_PROCFS_POWER
956 acpi_battery_remove_fs(device);
963 static int acpi_battery_remove(struct acpi_device *device, int type)
965 struct acpi_battery *battery = NULL;
967 if (!device || !acpi_driver_data(device))
969 battery = acpi_driver_data(device);
970 #ifdef CONFIG_ACPI_PROCFS_POWER
971 acpi_battery_remove_fs(device);
973 #ifdef CONFIG_ACPI_SYSFS_POWER
974 sysfs_remove_battery(battery);
976 mutex_destroy(&battery->lock);
981 /* this is needed to learn about changes made in suspended state */
982 static int acpi_battery_resume(struct acpi_device *device)
984 struct acpi_battery *battery;
987 battery = acpi_driver_data(device);
988 battery->update_time = 0;
989 acpi_battery_update(battery);
993 static struct acpi_driver acpi_battery_driver = {
995 .class = ACPI_BATTERY_CLASS,
996 .ids = battery_device_ids,
997 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
999 .add = acpi_battery_add,
1000 .resume = acpi_battery_resume,
1001 .remove = acpi_battery_remove,
1002 .notify = acpi_battery_notify,
1006 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1010 #ifdef CONFIG_ACPI_PROCFS_POWER
1011 acpi_battery_dir = acpi_lock_battery_dir();
1012 if (!acpi_battery_dir)
1015 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
1016 #ifdef CONFIG_ACPI_PROCFS_POWER
1017 acpi_unlock_battery_dir(acpi_battery_dir);
1024 static int __init acpi_battery_init(void)
1026 async_schedule(acpi_battery_init_async, NULL);
1030 static void __exit acpi_battery_exit(void)
1032 acpi_bus_unregister_driver(&acpi_battery_driver);
1033 #ifdef CONFIG_ACPI_PROCFS_POWER
1034 acpi_unlock_battery_dir(acpi_battery_dir);
1038 module_init(acpi_battery_init);
1039 module_exit(acpi_battery_exit);