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>
36 #include <linux/suspend.h>
38 #ifdef CONFIG_ACPI_PROCFS_POWER
39 #include <linux/proc_fs.h>
40 #include <linux/seq_file.h>
41 #include <asm/uaccess.h>
44 #include <acpi/acpi_bus.h>
45 #include <acpi/acpi_drivers.h>
46 #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
56 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
58 /* Battery power unit: 0 means mW, 1 means mA */
59 #define ACPI_BATTERY_POWER_UNIT_MA 1
61 #define _COMPONENT ACPI_BATTERY_COMPONENT
63 ACPI_MODULE_NAME("battery");
65 MODULE_AUTHOR("Paul Diefenbaugh");
66 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
67 MODULE_DESCRIPTION("ACPI Battery Driver");
68 MODULE_LICENSE("GPL");
70 static unsigned int cache_time = 1000;
71 module_param(cache_time, uint, 0644);
72 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
74 #ifdef CONFIG_ACPI_PROCFS_POWER
75 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
76 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
78 enum acpi_battery_files {
82 ACPI_BATTERY_NUMFILES,
87 static const struct acpi_device_id battery_device_ids[] = {
92 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
95 ACPI_BATTERY_ALARM_PRESENT,
96 ACPI_BATTERY_XINFO_PRESENT,
97 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
100 struct acpi_battery {
102 struct mutex sysfs_lock;
103 struct power_supply bat;
104 struct acpi_device *device;
105 struct notifier_block pm_nb;
106 unsigned long update_time;
111 int full_charge_capacity;
114 int design_capacity_warning;
115 int design_capacity_low;
117 int measurement_accuracy;
118 int max_sampling_time;
119 int min_sampling_time;
120 int max_averaging_interval;
121 int min_averaging_interval;
122 int capacity_granularity_1;
123 int capacity_granularity_2;
125 char model_number[32];
126 char serial_number[32];
134 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
136 inline int acpi_battery_present(struct acpi_battery *battery)
138 return battery->device->status.battery_present;
141 static int acpi_battery_technology(struct acpi_battery *battery)
143 if (!strcasecmp("NiCd", battery->type))
144 return POWER_SUPPLY_TECHNOLOGY_NiCd;
145 if (!strcasecmp("NiMH", battery->type))
146 return POWER_SUPPLY_TECHNOLOGY_NiMH;
147 if (!strcasecmp("LION", battery->type))
148 return POWER_SUPPLY_TECHNOLOGY_LION;
149 if (!strncasecmp("LI-ION", battery->type, 6))
150 return POWER_SUPPLY_TECHNOLOGY_LION;
151 if (!strcasecmp("LiP", battery->type))
152 return POWER_SUPPLY_TECHNOLOGY_LIPO;
153 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
156 static int acpi_battery_get_state(struct acpi_battery *battery);
158 static int acpi_battery_is_charged(struct acpi_battery *battery)
160 /* either charging or discharging */
161 if (battery->state != 0)
164 /* battery not reporting charge */
165 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
166 battery->capacity_now == 0)
169 /* good batteries update full_charge as the batteries degrade */
170 if (battery->full_charge_capacity == battery->capacity_now)
173 /* fallback to using design values for broken batteries */
174 if (battery->design_capacity == battery->capacity_now)
177 /* we don't do any sort of metric based on percentages */
181 static int acpi_battery_get_property(struct power_supply *psy,
182 enum power_supply_property psp,
183 union power_supply_propval *val)
186 struct acpi_battery *battery = to_acpi_battery(psy);
188 if (acpi_battery_present(battery)) {
189 /* run battery update only if it is present */
190 acpi_battery_get_state(battery);
191 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
194 case POWER_SUPPLY_PROP_STATUS:
195 if (battery->state & 0x01)
196 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
197 else if (battery->state & 0x02)
198 val->intval = POWER_SUPPLY_STATUS_CHARGING;
199 else if (acpi_battery_is_charged(battery))
200 val->intval = POWER_SUPPLY_STATUS_FULL;
202 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
204 case POWER_SUPPLY_PROP_PRESENT:
205 val->intval = acpi_battery_present(battery);
207 case POWER_SUPPLY_PROP_TECHNOLOGY:
208 val->intval = acpi_battery_technology(battery);
210 case POWER_SUPPLY_PROP_CYCLE_COUNT:
211 val->intval = battery->cycle_count;
213 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
214 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
217 val->intval = battery->design_voltage * 1000;
219 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
220 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
223 val->intval = battery->voltage_now * 1000;
225 case POWER_SUPPLY_PROP_CURRENT_NOW:
226 case POWER_SUPPLY_PROP_POWER_NOW:
227 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
230 val->intval = battery->rate_now * 1000;
232 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
233 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
234 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
237 val->intval = battery->design_capacity * 1000;
239 case POWER_SUPPLY_PROP_CHARGE_FULL:
240 case POWER_SUPPLY_PROP_ENERGY_FULL:
241 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
244 val->intval = battery->full_charge_capacity * 1000;
246 case POWER_SUPPLY_PROP_CHARGE_NOW:
247 case POWER_SUPPLY_PROP_ENERGY_NOW:
248 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
251 val->intval = battery->capacity_now * 1000;
253 case POWER_SUPPLY_PROP_CAPACITY:
254 if (battery->capacity_now && battery->full_charge_capacity)
255 val->intval = battery->capacity_now * 100/
256 battery->full_charge_capacity;
260 case POWER_SUPPLY_PROP_MODEL_NAME:
261 val->strval = battery->model_number;
263 case POWER_SUPPLY_PROP_MANUFACTURER:
264 val->strval = battery->oem_info;
266 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
267 val->strval = battery->serial_number;
275 static enum power_supply_property charge_battery_props[] = {
276 POWER_SUPPLY_PROP_STATUS,
277 POWER_SUPPLY_PROP_PRESENT,
278 POWER_SUPPLY_PROP_TECHNOLOGY,
279 POWER_SUPPLY_PROP_CYCLE_COUNT,
280 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
281 POWER_SUPPLY_PROP_VOLTAGE_NOW,
282 POWER_SUPPLY_PROP_CURRENT_NOW,
283 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
284 POWER_SUPPLY_PROP_CHARGE_FULL,
285 POWER_SUPPLY_PROP_CHARGE_NOW,
286 POWER_SUPPLY_PROP_CAPACITY,
287 POWER_SUPPLY_PROP_MODEL_NAME,
288 POWER_SUPPLY_PROP_MANUFACTURER,
289 POWER_SUPPLY_PROP_SERIAL_NUMBER,
292 static enum power_supply_property energy_battery_props[] = {
293 POWER_SUPPLY_PROP_STATUS,
294 POWER_SUPPLY_PROP_PRESENT,
295 POWER_SUPPLY_PROP_TECHNOLOGY,
296 POWER_SUPPLY_PROP_CYCLE_COUNT,
297 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
298 POWER_SUPPLY_PROP_VOLTAGE_NOW,
299 POWER_SUPPLY_PROP_POWER_NOW,
300 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
301 POWER_SUPPLY_PROP_ENERGY_FULL,
302 POWER_SUPPLY_PROP_ENERGY_NOW,
303 POWER_SUPPLY_PROP_CAPACITY,
304 POWER_SUPPLY_PROP_MODEL_NAME,
305 POWER_SUPPLY_PROP_MANUFACTURER,
306 POWER_SUPPLY_PROP_SERIAL_NUMBER,
309 #ifdef CONFIG_ACPI_PROCFS_POWER
310 inline char *acpi_battery_units(struct acpi_battery *battery)
312 return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
317 /* --------------------------------------------------------------------------
319 -------------------------------------------------------------------------- */
320 struct acpi_offsets {
321 size_t offset; /* offset inside struct acpi_sbs_battery */
322 u8 mode; /* int or string? */
325 static struct acpi_offsets state_offsets[] = {
326 {offsetof(struct acpi_battery, state), 0},
327 {offsetof(struct acpi_battery, rate_now), 0},
328 {offsetof(struct acpi_battery, capacity_now), 0},
329 {offsetof(struct acpi_battery, voltage_now), 0},
332 static struct acpi_offsets info_offsets[] = {
333 {offsetof(struct acpi_battery, power_unit), 0},
334 {offsetof(struct acpi_battery, design_capacity), 0},
335 {offsetof(struct acpi_battery, full_charge_capacity), 0},
336 {offsetof(struct acpi_battery, technology), 0},
337 {offsetof(struct acpi_battery, design_voltage), 0},
338 {offsetof(struct acpi_battery, design_capacity_warning), 0},
339 {offsetof(struct acpi_battery, design_capacity_low), 0},
340 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
341 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
342 {offsetof(struct acpi_battery, model_number), 1},
343 {offsetof(struct acpi_battery, serial_number), 1},
344 {offsetof(struct acpi_battery, type), 1},
345 {offsetof(struct acpi_battery, oem_info), 1},
348 static struct acpi_offsets extended_info_offsets[] = {
349 {offsetof(struct acpi_battery, power_unit), 0},
350 {offsetof(struct acpi_battery, design_capacity), 0},
351 {offsetof(struct acpi_battery, full_charge_capacity), 0},
352 {offsetof(struct acpi_battery, technology), 0},
353 {offsetof(struct acpi_battery, design_voltage), 0},
354 {offsetof(struct acpi_battery, design_capacity_warning), 0},
355 {offsetof(struct acpi_battery, design_capacity_low), 0},
356 {offsetof(struct acpi_battery, cycle_count), 0},
357 {offsetof(struct acpi_battery, measurement_accuracy), 0},
358 {offsetof(struct acpi_battery, max_sampling_time), 0},
359 {offsetof(struct acpi_battery, min_sampling_time), 0},
360 {offsetof(struct acpi_battery, max_averaging_interval), 0},
361 {offsetof(struct acpi_battery, min_averaging_interval), 0},
362 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
363 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
364 {offsetof(struct acpi_battery, model_number), 1},
365 {offsetof(struct acpi_battery, serial_number), 1},
366 {offsetof(struct acpi_battery, type), 1},
367 {offsetof(struct acpi_battery, oem_info), 1},
370 static int extract_package(struct acpi_battery *battery,
371 union acpi_object *package,
372 struct acpi_offsets *offsets, int num)
375 union acpi_object *element;
376 if (package->type != ACPI_TYPE_PACKAGE)
378 for (i = 0; i < num; ++i) {
379 if (package->package.count <= i)
381 element = &package->package.elements[i];
382 if (offsets[i].mode) {
383 u8 *ptr = (u8 *)battery + offsets[i].offset;
384 if (element->type == ACPI_TYPE_STRING ||
385 element->type == ACPI_TYPE_BUFFER)
386 strncpy(ptr, element->string.pointer, 32);
387 else if (element->type == ACPI_TYPE_INTEGER) {
388 strncpy(ptr, (u8 *)&element->integer.value,
390 ptr[sizeof(u64)] = 0;
392 *ptr = 0; /* don't have value */
394 int *x = (int *)((u8 *)battery + offsets[i].offset);
395 *x = (element->type == ACPI_TYPE_INTEGER) ?
396 element->integer.value : -1;
402 static int acpi_battery_get_status(struct acpi_battery *battery)
404 if (acpi_bus_get_status(battery->device)) {
405 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
411 static int acpi_battery_get_info(struct acpi_battery *battery)
413 int result = -EFAULT;
414 acpi_status status = 0;
415 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
418 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
420 if (!acpi_battery_present(battery))
422 mutex_lock(&battery->lock);
423 status = acpi_evaluate_object(battery->device->handle, name,
425 mutex_unlock(&battery->lock);
427 if (ACPI_FAILURE(status)) {
428 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
431 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
432 result = extract_package(battery, buffer.pointer,
433 extended_info_offsets,
434 ARRAY_SIZE(extended_info_offsets));
436 result = extract_package(battery, buffer.pointer,
437 info_offsets, ARRAY_SIZE(info_offsets));
438 kfree(buffer.pointer);
439 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
440 battery->full_charge_capacity = battery->design_capacity;
444 static int acpi_battery_get_state(struct acpi_battery *battery)
447 acpi_status status = 0;
448 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
450 if (!acpi_battery_present(battery))
453 if (battery->update_time &&
454 time_before(jiffies, battery->update_time +
455 msecs_to_jiffies(cache_time)))
458 mutex_lock(&battery->lock);
459 status = acpi_evaluate_object(battery->device->handle, "_BST",
461 mutex_unlock(&battery->lock);
463 if (ACPI_FAILURE(status)) {
464 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
468 result = extract_package(battery, buffer.pointer,
469 state_offsets, ARRAY_SIZE(state_offsets));
470 battery->update_time = jiffies;
471 kfree(buffer.pointer);
473 /* For buggy DSDTs that report negative 16-bit values for either
474 * charging or discharging current and/or report 0 as 65536
477 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
478 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
479 (s16)(battery->rate_now) < 0) {
480 battery->rate_now = abs((s16)battery->rate_now);
481 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
485 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
486 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
487 battery->capacity_now = (battery->capacity_now *
488 battery->full_charge_capacity) / 100;
492 static int acpi_battery_set_alarm(struct acpi_battery *battery)
494 acpi_status status = 0;
495 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
496 struct acpi_object_list arg_list = { 1, &arg0 };
498 if (!acpi_battery_present(battery) ||
499 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
502 arg0.integer.value = battery->alarm;
504 mutex_lock(&battery->lock);
505 status = acpi_evaluate_object(battery->device->handle, "_BTP",
507 mutex_unlock(&battery->lock);
509 if (ACPI_FAILURE(status))
512 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
516 static int acpi_battery_init_alarm(struct acpi_battery *battery)
518 acpi_status status = AE_OK;
519 acpi_handle handle = NULL;
521 /* See if alarms are supported, and if so, set default */
522 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
523 if (ACPI_FAILURE(status)) {
524 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
527 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
529 battery->alarm = battery->design_capacity_warning;
530 return acpi_battery_set_alarm(battery);
533 static ssize_t acpi_battery_alarm_show(struct device *dev,
534 struct device_attribute *attr,
537 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
538 return sprintf(buf, "%d\n", battery->alarm * 1000);
541 static ssize_t acpi_battery_alarm_store(struct device *dev,
542 struct device_attribute *attr,
543 const char *buf, size_t count)
546 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
547 if (sscanf(buf, "%ld\n", &x) == 1)
548 battery->alarm = x/1000;
549 if (acpi_battery_present(battery))
550 acpi_battery_set_alarm(battery);
554 static struct device_attribute alarm_attr = {
555 .attr = {.name = "alarm", .mode = 0644},
556 .show = acpi_battery_alarm_show,
557 .store = acpi_battery_alarm_store,
560 static int sysfs_add_battery(struct acpi_battery *battery)
564 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
565 battery->bat.properties = charge_battery_props;
566 battery->bat.num_properties =
567 ARRAY_SIZE(charge_battery_props);
569 battery->bat.properties = energy_battery_props;
570 battery->bat.num_properties =
571 ARRAY_SIZE(energy_battery_props);
574 battery->bat.name = acpi_device_bid(battery->device);
575 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
576 battery->bat.get_property = acpi_battery_get_property;
578 result = power_supply_register(&battery->device->dev, &battery->bat);
581 return device_create_file(battery->bat.dev, &alarm_attr);
584 static void sysfs_remove_battery(struct acpi_battery *battery)
586 mutex_lock(&battery->sysfs_lock);
587 if (!battery->bat.dev) {
588 mutex_unlock(&battery->sysfs_lock);
592 device_remove_file(battery->bat.dev, &alarm_attr);
593 power_supply_unregister(&battery->bat);
594 battery->bat.dev = NULL;
595 mutex_unlock(&battery->sysfs_lock);
599 * According to the ACPI spec, some kinds of primary batteries can
600 * report percentage battery remaining capacity directly to OS.
601 * In this case, it reports the Last Full Charged Capacity == 100
602 * and BatteryPresentRate == 0xFFFFFFFF.
604 * Now we found some battery reports percentage remaining capacity
605 * even if it's rechargeable.
606 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
608 * Handle this correctly so that they won't break userspace.
610 static void acpi_battery_quirks(struct acpi_battery *battery)
612 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
615 if (battery->full_charge_capacity == 100 &&
616 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
617 battery->capacity_now >=0 && battery->capacity_now <= 100) {
618 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
619 battery->full_charge_capacity = battery->design_capacity;
620 battery->capacity_now = (battery->capacity_now *
621 battery->full_charge_capacity) / 100;
625 static int acpi_battery_update(struct acpi_battery *battery)
627 int result, old_present = acpi_battery_present(battery);
628 result = acpi_battery_get_status(battery);
631 if (!acpi_battery_present(battery)) {
632 sysfs_remove_battery(battery);
633 battery->update_time = 0;
636 if (!battery->update_time ||
637 old_present != acpi_battery_present(battery)) {
638 result = acpi_battery_get_info(battery);
641 acpi_battery_init_alarm(battery);
643 if (!battery->bat.dev) {
644 result = sysfs_add_battery(battery);
648 result = acpi_battery_get_state(battery);
649 acpi_battery_quirks(battery);
653 static void acpi_battery_refresh(struct acpi_battery *battery)
657 if (!battery->bat.dev)
660 power_unit = battery->power_unit;
662 acpi_battery_get_info(battery);
664 if (power_unit == battery->power_unit)
667 /* The battery has changed its reporting units. */
668 sysfs_remove_battery(battery);
669 sysfs_add_battery(battery);
672 /* --------------------------------------------------------------------------
674 -------------------------------------------------------------------------- */
676 #ifdef CONFIG_ACPI_PROCFS_POWER
677 static struct proc_dir_entry *acpi_battery_dir;
679 static int acpi_battery_print_info(struct seq_file *seq, int result)
681 struct acpi_battery *battery = seq->private;
686 seq_printf(seq, "present: %s\n",
687 acpi_battery_present(battery)?"yes":"no");
688 if (!acpi_battery_present(battery))
690 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
691 seq_printf(seq, "design capacity: unknown\n");
693 seq_printf(seq, "design capacity: %d %sh\n",
694 battery->design_capacity,
695 acpi_battery_units(battery));
697 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
698 seq_printf(seq, "last full capacity: unknown\n");
700 seq_printf(seq, "last full capacity: %d %sh\n",
701 battery->full_charge_capacity,
702 acpi_battery_units(battery));
704 seq_printf(seq, "battery technology: %srechargeable\n",
705 (!battery->technology)?"non-":"");
707 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
708 seq_printf(seq, "design voltage: unknown\n");
710 seq_printf(seq, "design voltage: %d mV\n",
711 battery->design_voltage);
712 seq_printf(seq, "design capacity warning: %d %sh\n",
713 battery->design_capacity_warning,
714 acpi_battery_units(battery));
715 seq_printf(seq, "design capacity low: %d %sh\n",
716 battery->design_capacity_low,
717 acpi_battery_units(battery));
718 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
719 seq_printf(seq, "capacity granularity 1: %d %sh\n",
720 battery->capacity_granularity_1,
721 acpi_battery_units(battery));
722 seq_printf(seq, "capacity granularity 2: %d %sh\n",
723 battery->capacity_granularity_2,
724 acpi_battery_units(battery));
725 seq_printf(seq, "model number: %s\n", battery->model_number);
726 seq_printf(seq, "serial number: %s\n", battery->serial_number);
727 seq_printf(seq, "battery type: %s\n", battery->type);
728 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
731 seq_printf(seq, "ERROR: Unable to read battery info\n");
735 static int acpi_battery_print_state(struct seq_file *seq, int result)
737 struct acpi_battery *battery = seq->private;
742 seq_printf(seq, "present: %s\n",
743 acpi_battery_present(battery)?"yes":"no");
744 if (!acpi_battery_present(battery))
747 seq_printf(seq, "capacity state: %s\n",
748 (battery->state & 0x04)?"critical":"ok");
749 if ((battery->state & 0x01) && (battery->state & 0x02))
751 "charging state: charging/discharging\n");
752 else if (battery->state & 0x01)
753 seq_printf(seq, "charging state: discharging\n");
754 else if (battery->state & 0x02)
755 seq_printf(seq, "charging state: charging\n");
757 seq_printf(seq, "charging state: charged\n");
759 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
760 seq_printf(seq, "present rate: unknown\n");
762 seq_printf(seq, "present rate: %d %s\n",
763 battery->rate_now, acpi_battery_units(battery));
765 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
766 seq_printf(seq, "remaining capacity: unknown\n");
768 seq_printf(seq, "remaining capacity: %d %sh\n",
769 battery->capacity_now, acpi_battery_units(battery));
770 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
771 seq_printf(seq, "present voltage: unknown\n");
773 seq_printf(seq, "present voltage: %d mV\n",
774 battery->voltage_now);
777 seq_printf(seq, "ERROR: Unable to read battery state\n");
782 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
784 struct acpi_battery *battery = seq->private;
789 if (!acpi_battery_present(battery)) {
790 seq_printf(seq, "present: no\n");
793 seq_printf(seq, "alarm: ");
795 seq_printf(seq, "unsupported\n");
797 seq_printf(seq, "%u %sh\n", battery->alarm,
798 acpi_battery_units(battery));
801 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
805 static ssize_t acpi_battery_write_alarm(struct file *file,
806 const char __user * buffer,
807 size_t count, loff_t * ppos)
810 char alarm_string[12] = { '\0' };
811 struct seq_file *m = file->private_data;
812 struct acpi_battery *battery = m->private;
814 if (!battery || (count > sizeof(alarm_string) - 1))
816 if (!acpi_battery_present(battery)) {
820 if (copy_from_user(alarm_string, buffer, count)) {
824 alarm_string[count] = '\0';
825 battery->alarm = simple_strtol(alarm_string, NULL, 0);
826 result = acpi_battery_set_alarm(battery);
833 typedef int(*print_func)(struct seq_file *seq, int result);
835 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
836 acpi_battery_print_info,
837 acpi_battery_print_state,
838 acpi_battery_print_alarm,
841 static int acpi_battery_read(int fid, struct seq_file *seq)
843 struct acpi_battery *battery = seq->private;
844 int result = acpi_battery_update(battery);
845 return acpi_print_funcs[fid](seq, result);
848 #define DECLARE_FILE_FUNCTIONS(_name) \
849 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
851 return acpi_battery_read(_name##_tag, seq); \
853 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
855 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
858 DECLARE_FILE_FUNCTIONS(info);
859 DECLARE_FILE_FUNCTIONS(state);
860 DECLARE_FILE_FUNCTIONS(alarm);
862 #undef DECLARE_FILE_FUNCTIONS
864 #define FILE_DESCRIPTION_RO(_name) \
866 .name = __stringify(_name), \
869 .open = acpi_battery_##_name##_open_fs, \
871 .llseek = seq_lseek, \
872 .release = single_release, \
873 .owner = THIS_MODULE, \
877 #define FILE_DESCRIPTION_RW(_name) \
879 .name = __stringify(_name), \
880 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
882 .open = acpi_battery_##_name##_open_fs, \
884 .llseek = seq_lseek, \
885 .write = acpi_battery_write_##_name, \
886 .release = single_release, \
887 .owner = THIS_MODULE, \
891 static const struct battery_file {
892 struct file_operations ops;
895 } acpi_battery_file[] = {
896 FILE_DESCRIPTION_RO(info),
897 FILE_DESCRIPTION_RO(state),
898 FILE_DESCRIPTION_RW(alarm),
901 #undef FILE_DESCRIPTION_RO
902 #undef FILE_DESCRIPTION_RW
904 static int acpi_battery_add_fs(struct acpi_device *device)
906 struct proc_dir_entry *entry = NULL;
909 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
910 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
911 if (!acpi_device_dir(device)) {
912 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
914 if (!acpi_device_dir(device))
918 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
919 entry = proc_create_data(acpi_battery_file[i].name,
920 acpi_battery_file[i].mode,
921 acpi_device_dir(device),
922 &acpi_battery_file[i].ops,
923 acpi_driver_data(device));
930 static void acpi_battery_remove_fs(struct acpi_device *device)
933 if (!acpi_device_dir(device))
935 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
936 remove_proc_entry(acpi_battery_file[i].name,
937 acpi_device_dir(device));
939 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
940 acpi_device_dir(device) = NULL;
945 /* --------------------------------------------------------------------------
947 -------------------------------------------------------------------------- */
949 static void acpi_battery_notify(struct acpi_device *device, u32 event)
951 struct acpi_battery *battery = acpi_driver_data(device);
956 old = battery->bat.dev;
957 if (event == ACPI_BATTERY_NOTIFY_INFO)
958 acpi_battery_refresh(battery);
959 acpi_battery_update(battery);
960 acpi_bus_generate_proc_event(device, event,
961 acpi_battery_present(battery));
962 acpi_bus_generate_netlink_event(device->pnp.device_class,
963 dev_name(&device->dev), event,
964 acpi_battery_present(battery));
965 /* acpi_battery_update could remove power_supply object */
966 if (old && battery->bat.dev)
967 power_supply_changed(&battery->bat);
970 static int battery_notify(struct notifier_block *nb,
971 unsigned long mode, void *_unused)
973 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
976 case PM_POST_HIBERNATION:
977 case PM_POST_SUSPEND:
978 if (battery->bat.dev) {
979 sysfs_remove_battery(battery);
980 sysfs_add_battery(battery);
988 static int acpi_battery_add(struct acpi_device *device)
991 struct acpi_battery *battery = NULL;
995 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
998 battery->device = device;
999 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1000 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1001 device->driver_data = battery;
1002 mutex_init(&battery->lock);
1003 mutex_init(&battery->sysfs_lock);
1004 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
1006 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1007 result = acpi_battery_update(battery);
1010 #ifdef CONFIG_ACPI_PROCFS_POWER
1011 result = acpi_battery_add_fs(device);
1014 #ifdef CONFIG_ACPI_PROCFS_POWER
1015 acpi_battery_remove_fs(device);
1020 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
1021 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1022 device->status.battery_present ? "present" : "absent");
1024 battery->pm_nb.notifier_call = battery_notify;
1025 register_pm_notifier(&battery->pm_nb);
1030 sysfs_remove_battery(battery);
1031 mutex_destroy(&battery->lock);
1032 mutex_destroy(&battery->sysfs_lock);
1037 static int acpi_battery_remove(struct acpi_device *device, int type)
1039 struct acpi_battery *battery = NULL;
1041 if (!device || !acpi_driver_data(device))
1043 battery = acpi_driver_data(device);
1044 unregister_pm_notifier(&battery->pm_nb);
1045 #ifdef CONFIG_ACPI_PROCFS_POWER
1046 acpi_battery_remove_fs(device);
1048 sysfs_remove_battery(battery);
1049 mutex_destroy(&battery->lock);
1050 mutex_destroy(&battery->sysfs_lock);
1055 /* this is needed to learn about changes made in suspended state */
1056 static int acpi_battery_resume(struct device *dev)
1058 struct acpi_battery *battery;
1063 battery = acpi_driver_data(to_acpi_device(dev));
1067 battery->update_time = 0;
1068 acpi_battery_update(battery);
1072 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1074 static struct acpi_driver acpi_battery_driver = {
1076 .class = ACPI_BATTERY_CLASS,
1077 .ids = battery_device_ids,
1078 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1080 .add = acpi_battery_add,
1081 .remove = acpi_battery_remove,
1082 .notify = acpi_battery_notify,
1084 .drv.pm = &acpi_battery_pm,
1087 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1091 #ifdef CONFIG_ACPI_PROCFS_POWER
1092 acpi_battery_dir = acpi_lock_battery_dir();
1093 if (!acpi_battery_dir)
1096 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
1097 #ifdef CONFIG_ACPI_PROCFS_POWER
1098 acpi_unlock_battery_dir(acpi_battery_dir);
1105 static int __init acpi_battery_init(void)
1107 async_schedule(acpi_battery_init_async, NULL);
1111 static void __exit acpi_battery_exit(void)
1113 acpi_bus_unregister_driver(&acpi_battery_driver);
1114 #ifdef CONFIG_ACPI_PROCFS_POWER
1115 acpi_unlock_battery_dir(acpi_battery_dir);
1119 module_init(acpi_battery_init);
1120 module_exit(acpi_battery_exit);