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 #define _COMPONENT ACPI_BATTERY_COMPONENT
60 ACPI_MODULE_NAME("battery");
62 MODULE_AUTHOR("Paul Diefenbaugh");
63 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
64 MODULE_DESCRIPTION("ACPI Battery Driver");
65 MODULE_LICENSE("GPL");
67 static unsigned int cache_time = 1000;
68 module_param(cache_time, uint, 0644);
69 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
71 #ifdef CONFIG_ACPI_PROCFS_POWER
72 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
73 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
75 enum acpi_battery_files {
79 ACPI_BATTERY_NUMFILES,
84 static const struct acpi_device_id battery_device_ids[] = {
89 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
92 ACPI_BATTERY_ALARM_PRESENT,
93 ACPI_BATTERY_XINFO_PRESENT,
94 /* For buggy DSDTs that report negative 16-bit values for either
95 * charging or discharging current and/or report 0 as 65536
98 ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
99 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
102 struct acpi_battery {
104 struct power_supply bat;
105 struct acpi_device *device;
106 struct notifier_block pm_nb;
107 unsigned long update_time;
112 int full_charge_capacity;
115 int design_capacity_warning;
116 int design_capacity_low;
118 int measurement_accuracy;
119 int max_sampling_time;
120 int min_sampling_time;
121 int max_averaging_interval;
122 int min_averaging_interval;
123 int capacity_granularity_1;
124 int capacity_granularity_2;
126 char model_number[32];
127 char serial_number[32];
135 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
137 inline int acpi_battery_present(struct acpi_battery *battery)
139 return battery->device->status.battery_present;
142 static int acpi_battery_technology(struct acpi_battery *battery)
144 if (!strcasecmp("NiCd", battery->type))
145 return POWER_SUPPLY_TECHNOLOGY_NiCd;
146 if (!strcasecmp("NiMH", battery->type))
147 return POWER_SUPPLY_TECHNOLOGY_NiMH;
148 if (!strcasecmp("LION", battery->type))
149 return POWER_SUPPLY_TECHNOLOGY_LION;
150 if (!strncasecmp("LI-ION", battery->type, 6))
151 return POWER_SUPPLY_TECHNOLOGY_LION;
152 if (!strcasecmp("LiP", battery->type))
153 return POWER_SUPPLY_TECHNOLOGY_LIPO;
154 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
157 static int acpi_battery_get_state(struct acpi_battery *battery);
159 static int acpi_battery_is_charged(struct acpi_battery *battery)
161 /* either charging or discharging */
162 if (battery->state != 0)
165 /* battery not reporting charge */
166 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
167 battery->capacity_now == 0)
170 /* good batteries update full_charge as the batteries degrade */
171 if (battery->full_charge_capacity == battery->capacity_now)
174 /* fallback to using design values for broken batteries */
175 if (battery->design_capacity == battery->capacity_now)
178 /* we don't do any sort of metric based on percentages */
182 static int acpi_battery_get_property(struct power_supply *psy,
183 enum power_supply_property psp,
184 union power_supply_propval *val)
187 struct acpi_battery *battery = to_acpi_battery(psy);
189 if (acpi_battery_present(battery)) {
190 /* run battery update only if it is present */
191 acpi_battery_get_state(battery);
192 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
195 case POWER_SUPPLY_PROP_STATUS:
196 if (battery->state & 0x01)
197 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
198 else if (battery->state & 0x02)
199 val->intval = POWER_SUPPLY_STATUS_CHARGING;
200 else if (acpi_battery_is_charged(battery))
201 val->intval = POWER_SUPPLY_STATUS_FULL;
203 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
205 case POWER_SUPPLY_PROP_PRESENT:
206 val->intval = acpi_battery_present(battery);
208 case POWER_SUPPLY_PROP_TECHNOLOGY:
209 val->intval = acpi_battery_technology(battery);
211 case POWER_SUPPLY_PROP_CYCLE_COUNT:
212 val->intval = battery->cycle_count;
214 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
215 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
218 val->intval = battery->design_voltage * 1000;
220 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
221 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
224 val->intval = battery->voltage_now * 1000;
226 case POWER_SUPPLY_PROP_CURRENT_NOW:
227 case POWER_SUPPLY_PROP_POWER_NOW:
228 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
231 val->intval = battery->rate_now * 1000;
233 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
234 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
235 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
238 val->intval = battery->design_capacity * 1000;
240 case POWER_SUPPLY_PROP_CHARGE_FULL:
241 case POWER_SUPPLY_PROP_ENERGY_FULL:
242 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
245 val->intval = battery->full_charge_capacity * 1000;
247 case POWER_SUPPLY_PROP_CHARGE_NOW:
248 case POWER_SUPPLY_PROP_ENERGY_NOW:
249 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
252 val->intval = battery->capacity_now * 1000;
254 case POWER_SUPPLY_PROP_MODEL_NAME:
255 val->strval = battery->model_number;
257 case POWER_SUPPLY_PROP_MANUFACTURER:
258 val->strval = battery->oem_info;
260 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
261 val->strval = battery->serial_number;
269 static enum power_supply_property charge_battery_props[] = {
270 POWER_SUPPLY_PROP_STATUS,
271 POWER_SUPPLY_PROP_PRESENT,
272 POWER_SUPPLY_PROP_TECHNOLOGY,
273 POWER_SUPPLY_PROP_CYCLE_COUNT,
274 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
275 POWER_SUPPLY_PROP_VOLTAGE_NOW,
276 POWER_SUPPLY_PROP_CURRENT_NOW,
277 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
278 POWER_SUPPLY_PROP_CHARGE_FULL,
279 POWER_SUPPLY_PROP_CHARGE_NOW,
280 POWER_SUPPLY_PROP_MODEL_NAME,
281 POWER_SUPPLY_PROP_MANUFACTURER,
282 POWER_SUPPLY_PROP_SERIAL_NUMBER,
285 static enum power_supply_property energy_battery_props[] = {
286 POWER_SUPPLY_PROP_STATUS,
287 POWER_SUPPLY_PROP_PRESENT,
288 POWER_SUPPLY_PROP_TECHNOLOGY,
289 POWER_SUPPLY_PROP_CYCLE_COUNT,
290 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
291 POWER_SUPPLY_PROP_VOLTAGE_NOW,
292 POWER_SUPPLY_PROP_POWER_NOW,
293 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
294 POWER_SUPPLY_PROP_ENERGY_FULL,
295 POWER_SUPPLY_PROP_ENERGY_NOW,
296 POWER_SUPPLY_PROP_MODEL_NAME,
297 POWER_SUPPLY_PROP_MANUFACTURER,
298 POWER_SUPPLY_PROP_SERIAL_NUMBER,
301 #ifdef CONFIG_ACPI_PROCFS_POWER
302 inline char *acpi_battery_units(struct acpi_battery *battery)
304 return (battery->power_unit)?"mA":"mW";
308 /* --------------------------------------------------------------------------
310 -------------------------------------------------------------------------- */
311 struct acpi_offsets {
312 size_t offset; /* offset inside struct acpi_sbs_battery */
313 u8 mode; /* int or string? */
316 static struct acpi_offsets state_offsets[] = {
317 {offsetof(struct acpi_battery, state), 0},
318 {offsetof(struct acpi_battery, rate_now), 0},
319 {offsetof(struct acpi_battery, capacity_now), 0},
320 {offsetof(struct acpi_battery, voltage_now), 0},
323 static struct acpi_offsets info_offsets[] = {
324 {offsetof(struct acpi_battery, power_unit), 0},
325 {offsetof(struct acpi_battery, design_capacity), 0},
326 {offsetof(struct acpi_battery, full_charge_capacity), 0},
327 {offsetof(struct acpi_battery, technology), 0},
328 {offsetof(struct acpi_battery, design_voltage), 0},
329 {offsetof(struct acpi_battery, design_capacity_warning), 0},
330 {offsetof(struct acpi_battery, design_capacity_low), 0},
331 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
332 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
333 {offsetof(struct acpi_battery, model_number), 1},
334 {offsetof(struct acpi_battery, serial_number), 1},
335 {offsetof(struct acpi_battery, type), 1},
336 {offsetof(struct acpi_battery, oem_info), 1},
339 static struct acpi_offsets extended_info_offsets[] = {
340 {offsetof(struct acpi_battery, power_unit), 0},
341 {offsetof(struct acpi_battery, design_capacity), 0},
342 {offsetof(struct acpi_battery, full_charge_capacity), 0},
343 {offsetof(struct acpi_battery, technology), 0},
344 {offsetof(struct acpi_battery, design_voltage), 0},
345 {offsetof(struct acpi_battery, design_capacity_warning), 0},
346 {offsetof(struct acpi_battery, design_capacity_low), 0},
347 {offsetof(struct acpi_battery, cycle_count), 0},
348 {offsetof(struct acpi_battery, measurement_accuracy), 0},
349 {offsetof(struct acpi_battery, max_sampling_time), 0},
350 {offsetof(struct acpi_battery, min_sampling_time), 0},
351 {offsetof(struct acpi_battery, max_averaging_interval), 0},
352 {offsetof(struct acpi_battery, min_averaging_interval), 0},
353 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
354 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
355 {offsetof(struct acpi_battery, model_number), 1},
356 {offsetof(struct acpi_battery, serial_number), 1},
357 {offsetof(struct acpi_battery, type), 1},
358 {offsetof(struct acpi_battery, oem_info), 1},
361 static int extract_package(struct acpi_battery *battery,
362 union acpi_object *package,
363 struct acpi_offsets *offsets, int num)
366 union acpi_object *element;
367 if (package->type != ACPI_TYPE_PACKAGE)
369 for (i = 0; i < num; ++i) {
370 if (package->package.count <= i)
372 element = &package->package.elements[i];
373 if (offsets[i].mode) {
374 u8 *ptr = (u8 *)battery + offsets[i].offset;
375 if (element->type == ACPI_TYPE_STRING ||
376 element->type == ACPI_TYPE_BUFFER)
377 strncpy(ptr, element->string.pointer, 32);
378 else if (element->type == ACPI_TYPE_INTEGER) {
379 strncpy(ptr, (u8 *)&element->integer.value,
381 ptr[sizeof(u64)] = 0;
383 *ptr = 0; /* don't have value */
385 int *x = (int *)((u8 *)battery + offsets[i].offset);
386 *x = (element->type == ACPI_TYPE_INTEGER) ?
387 element->integer.value : -1;
393 static int acpi_battery_get_status(struct acpi_battery *battery)
395 if (acpi_bus_get_status(battery->device)) {
396 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
402 static int acpi_battery_get_info(struct acpi_battery *battery)
404 int result = -EFAULT;
405 acpi_status status = 0;
406 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
409 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
411 if (!acpi_battery_present(battery))
413 mutex_lock(&battery->lock);
414 status = acpi_evaluate_object(battery->device->handle, name,
416 mutex_unlock(&battery->lock);
418 if (ACPI_FAILURE(status)) {
419 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
422 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
423 result = extract_package(battery, buffer.pointer,
424 extended_info_offsets,
425 ARRAY_SIZE(extended_info_offsets));
427 result = extract_package(battery, buffer.pointer,
428 info_offsets, ARRAY_SIZE(info_offsets));
429 kfree(buffer.pointer);
430 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
431 battery->full_charge_capacity = battery->design_capacity;
435 static int acpi_battery_get_state(struct acpi_battery *battery)
438 acpi_status status = 0;
439 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
441 if (!acpi_battery_present(battery))
444 if (battery->update_time &&
445 time_before(jiffies, battery->update_time +
446 msecs_to_jiffies(cache_time)))
449 mutex_lock(&battery->lock);
450 status = acpi_evaluate_object(battery->device->handle, "_BST",
452 mutex_unlock(&battery->lock);
454 if (ACPI_FAILURE(status)) {
455 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
459 result = extract_package(battery, buffer.pointer,
460 state_offsets, ARRAY_SIZE(state_offsets));
461 battery->update_time = jiffies;
462 kfree(buffer.pointer);
464 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
465 battery->rate_now != -1)
466 battery->rate_now = abs((s16)battery->rate_now);
468 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
469 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
470 battery->capacity_now = (battery->capacity_now *
471 battery->full_charge_capacity) / 100;
475 static int acpi_battery_set_alarm(struct acpi_battery *battery)
477 acpi_status status = 0;
478 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
479 struct acpi_object_list arg_list = { 1, &arg0 };
481 if (!acpi_battery_present(battery) ||
482 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
485 arg0.integer.value = battery->alarm;
487 mutex_lock(&battery->lock);
488 status = acpi_evaluate_object(battery->device->handle, "_BTP",
490 mutex_unlock(&battery->lock);
492 if (ACPI_FAILURE(status))
495 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
499 static int acpi_battery_init_alarm(struct acpi_battery *battery)
501 acpi_status status = AE_OK;
502 acpi_handle handle = NULL;
504 /* See if alarms are supported, and if so, set default */
505 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
506 if (ACPI_FAILURE(status)) {
507 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
510 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
512 battery->alarm = battery->design_capacity_warning;
513 return acpi_battery_set_alarm(battery);
516 static ssize_t acpi_battery_alarm_show(struct device *dev,
517 struct device_attribute *attr,
520 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
521 return sprintf(buf, "%d\n", battery->alarm * 1000);
524 static ssize_t acpi_battery_alarm_store(struct device *dev,
525 struct device_attribute *attr,
526 const char *buf, size_t count)
529 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
530 if (sscanf(buf, "%ld\n", &x) == 1)
531 battery->alarm = x/1000;
532 if (acpi_battery_present(battery))
533 acpi_battery_set_alarm(battery);
537 static struct device_attribute alarm_attr = {
538 .attr = {.name = "alarm", .mode = 0644},
539 .show = acpi_battery_alarm_show,
540 .store = acpi_battery_alarm_store,
543 static int sysfs_add_battery(struct acpi_battery *battery)
547 if (battery->power_unit) {
548 battery->bat.properties = charge_battery_props;
549 battery->bat.num_properties =
550 ARRAY_SIZE(charge_battery_props);
552 battery->bat.properties = energy_battery_props;
553 battery->bat.num_properties =
554 ARRAY_SIZE(energy_battery_props);
557 battery->bat.name = acpi_device_bid(battery->device);
558 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
559 battery->bat.get_property = acpi_battery_get_property;
561 result = power_supply_register(&battery->device->dev, &battery->bat);
564 return device_create_file(battery->bat.dev, &alarm_attr);
567 static void sysfs_remove_battery(struct acpi_battery *battery)
569 if (!battery->bat.dev)
571 device_remove_file(battery->bat.dev, &alarm_attr);
572 power_supply_unregister(&battery->bat);
573 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);
584 * According to the ACPI spec, some kinds of primary batteries can
585 * report percentage battery remaining capacity directly to OS.
586 * In this case, it reports the Last Full Charged Capacity == 100
587 * and BatteryPresentRate == 0xFFFFFFFF.
589 * Now we found some battery reports percentage remaining capacity
590 * even if it's rechargeable.
591 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
593 * Handle this correctly so that they won't break userspace.
595 static void acpi_battery_quirks2(struct acpi_battery *battery)
597 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
600 if (battery->full_charge_capacity == 100 &&
601 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
602 battery->capacity_now >=0 && battery->capacity_now <= 100) {
603 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
604 battery->full_charge_capacity = battery->design_capacity;
605 battery->capacity_now = (battery->capacity_now *
606 battery->full_charge_capacity) / 100;
610 static int acpi_battery_update(struct acpi_battery *battery)
612 int result, old_present = acpi_battery_present(battery);
613 result = acpi_battery_get_status(battery);
616 if (!acpi_battery_present(battery)) {
617 sysfs_remove_battery(battery);
618 battery->update_time = 0;
621 if (!battery->update_time ||
622 old_present != acpi_battery_present(battery)) {
623 result = acpi_battery_get_info(battery);
626 acpi_battery_quirks(battery);
627 acpi_battery_init_alarm(battery);
629 if (!battery->bat.dev)
630 sysfs_add_battery(battery);
631 result = acpi_battery_get_state(battery);
632 acpi_battery_quirks2(battery);
636 static void acpi_battery_refresh(struct acpi_battery *battery)
640 if (!battery->bat.dev)
643 power_unit = battery->power_unit;
645 acpi_battery_get_info(battery);
647 if (power_unit == battery->power_unit)
650 /* The battery has changed its reporting units. */
651 sysfs_remove_battery(battery);
652 sysfs_add_battery(battery);
655 /* --------------------------------------------------------------------------
657 -------------------------------------------------------------------------- */
659 #ifdef CONFIG_ACPI_PROCFS_POWER
660 static struct proc_dir_entry *acpi_battery_dir;
662 static int acpi_battery_print_info(struct seq_file *seq, int result)
664 struct acpi_battery *battery = seq->private;
669 seq_printf(seq, "present: %s\n",
670 acpi_battery_present(battery)?"yes":"no");
671 if (!acpi_battery_present(battery))
673 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
674 seq_printf(seq, "design capacity: unknown\n");
676 seq_printf(seq, "design capacity: %d %sh\n",
677 battery->design_capacity,
678 acpi_battery_units(battery));
680 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
681 seq_printf(seq, "last full capacity: unknown\n");
683 seq_printf(seq, "last full capacity: %d %sh\n",
684 battery->full_charge_capacity,
685 acpi_battery_units(battery));
687 seq_printf(seq, "battery technology: %srechargeable\n",
688 (!battery->technology)?"non-":"");
690 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
691 seq_printf(seq, "design voltage: unknown\n");
693 seq_printf(seq, "design voltage: %d mV\n",
694 battery->design_voltage);
695 seq_printf(seq, "design capacity warning: %d %sh\n",
696 battery->design_capacity_warning,
697 acpi_battery_units(battery));
698 seq_printf(seq, "design capacity low: %d %sh\n",
699 battery->design_capacity_low,
700 acpi_battery_units(battery));
701 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
702 seq_printf(seq, "capacity granularity 1: %d %sh\n",
703 battery->capacity_granularity_1,
704 acpi_battery_units(battery));
705 seq_printf(seq, "capacity granularity 2: %d %sh\n",
706 battery->capacity_granularity_2,
707 acpi_battery_units(battery));
708 seq_printf(seq, "model number: %s\n", battery->model_number);
709 seq_printf(seq, "serial number: %s\n", battery->serial_number);
710 seq_printf(seq, "battery type: %s\n", battery->type);
711 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
714 seq_printf(seq, "ERROR: Unable to read battery info\n");
718 static int acpi_battery_print_state(struct seq_file *seq, int result)
720 struct acpi_battery *battery = seq->private;
725 seq_printf(seq, "present: %s\n",
726 acpi_battery_present(battery)?"yes":"no");
727 if (!acpi_battery_present(battery))
730 seq_printf(seq, "capacity state: %s\n",
731 (battery->state & 0x04)?"critical":"ok");
732 if ((battery->state & 0x01) && (battery->state & 0x02))
734 "charging state: charging/discharging\n");
735 else if (battery->state & 0x01)
736 seq_printf(seq, "charging state: discharging\n");
737 else if (battery->state & 0x02)
738 seq_printf(seq, "charging state: charging\n");
740 seq_printf(seq, "charging state: charged\n");
742 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
743 seq_printf(seq, "present rate: unknown\n");
745 seq_printf(seq, "present rate: %d %s\n",
746 battery->rate_now, acpi_battery_units(battery));
748 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
749 seq_printf(seq, "remaining capacity: unknown\n");
751 seq_printf(seq, "remaining capacity: %d %sh\n",
752 battery->capacity_now, acpi_battery_units(battery));
753 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
754 seq_printf(seq, "present voltage: unknown\n");
756 seq_printf(seq, "present voltage: %d mV\n",
757 battery->voltage_now);
760 seq_printf(seq, "ERROR: Unable to read battery state\n");
765 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
767 struct acpi_battery *battery = seq->private;
772 if (!acpi_battery_present(battery)) {
773 seq_printf(seq, "present: no\n");
776 seq_printf(seq, "alarm: ");
778 seq_printf(seq, "unsupported\n");
780 seq_printf(seq, "%u %sh\n", battery->alarm,
781 acpi_battery_units(battery));
784 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
788 static ssize_t acpi_battery_write_alarm(struct file *file,
789 const char __user * buffer,
790 size_t count, loff_t * ppos)
793 char alarm_string[12] = { '\0' };
794 struct seq_file *m = file->private_data;
795 struct acpi_battery *battery = m->private;
797 if (!battery || (count > sizeof(alarm_string) - 1))
799 if (!acpi_battery_present(battery)) {
803 if (copy_from_user(alarm_string, buffer, count)) {
807 alarm_string[count] = '\0';
808 battery->alarm = simple_strtol(alarm_string, NULL, 0);
809 result = acpi_battery_set_alarm(battery);
816 typedef int(*print_func)(struct seq_file *seq, int result);
818 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
819 acpi_battery_print_info,
820 acpi_battery_print_state,
821 acpi_battery_print_alarm,
824 static int acpi_battery_read(int fid, struct seq_file *seq)
826 struct acpi_battery *battery = seq->private;
827 int result = acpi_battery_update(battery);
828 return acpi_print_funcs[fid](seq, result);
831 #define DECLARE_FILE_FUNCTIONS(_name) \
832 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
834 return acpi_battery_read(_name##_tag, seq); \
836 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
838 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
841 DECLARE_FILE_FUNCTIONS(info);
842 DECLARE_FILE_FUNCTIONS(state);
843 DECLARE_FILE_FUNCTIONS(alarm);
845 #undef DECLARE_FILE_FUNCTIONS
847 #define FILE_DESCRIPTION_RO(_name) \
849 .name = __stringify(_name), \
852 .open = acpi_battery_##_name##_open_fs, \
854 .llseek = seq_lseek, \
855 .release = single_release, \
856 .owner = THIS_MODULE, \
860 #define FILE_DESCRIPTION_RW(_name) \
862 .name = __stringify(_name), \
863 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
865 .open = acpi_battery_##_name##_open_fs, \
867 .llseek = seq_lseek, \
868 .write = acpi_battery_write_##_name, \
869 .release = single_release, \
870 .owner = THIS_MODULE, \
874 static struct battery_file {
875 struct file_operations ops;
878 } acpi_battery_file[] = {
879 FILE_DESCRIPTION_RO(info),
880 FILE_DESCRIPTION_RO(state),
881 FILE_DESCRIPTION_RW(alarm),
884 #undef FILE_DESCRIPTION_RO
885 #undef FILE_DESCRIPTION_RW
887 static int acpi_battery_add_fs(struct acpi_device *device)
889 struct proc_dir_entry *entry = NULL;
892 printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
893 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
894 if (!acpi_device_dir(device)) {
895 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
897 if (!acpi_device_dir(device))
901 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
902 entry = proc_create_data(acpi_battery_file[i].name,
903 acpi_battery_file[i].mode,
904 acpi_device_dir(device),
905 &acpi_battery_file[i].ops,
906 acpi_driver_data(device));
913 static void acpi_battery_remove_fs(struct acpi_device *device)
916 if (!acpi_device_dir(device))
918 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
919 remove_proc_entry(acpi_battery_file[i].name,
920 acpi_device_dir(device));
922 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
923 acpi_device_dir(device) = NULL;
928 /* --------------------------------------------------------------------------
930 -------------------------------------------------------------------------- */
932 static void acpi_battery_notify(struct acpi_device *device, u32 event)
934 struct acpi_battery *battery = acpi_driver_data(device);
939 old = battery->bat.dev;
940 if (event == ACPI_BATTERY_NOTIFY_INFO)
941 acpi_battery_refresh(battery);
942 acpi_battery_update(battery);
943 acpi_bus_generate_proc_event(device, event,
944 acpi_battery_present(battery));
945 acpi_bus_generate_netlink_event(device->pnp.device_class,
946 dev_name(&device->dev), event,
947 acpi_battery_present(battery));
948 /* acpi_battery_update could remove power_supply object */
949 if (old && battery->bat.dev)
950 power_supply_changed(&battery->bat);
953 static int battery_notify(struct notifier_block *nb,
954 unsigned long mode, void *_unused)
956 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
959 case PM_POST_SUSPEND:
960 sysfs_remove_battery(battery);
961 sysfs_add_battery(battery);
968 static int acpi_battery_add(struct acpi_device *device)
971 struct acpi_battery *battery = NULL;
975 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
978 battery->device = device;
979 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
980 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
981 device->driver_data = battery;
982 mutex_init(&battery->lock);
983 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
985 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
986 acpi_battery_update(battery);
987 #ifdef CONFIG_ACPI_PROCFS_POWER
988 result = acpi_battery_add_fs(device);
991 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
992 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
993 device->status.battery_present ? "present" : "absent");
995 #ifdef CONFIG_ACPI_PROCFS_POWER
996 acpi_battery_remove_fs(device);
1001 battery->pm_nb.notifier_call = battery_notify;
1002 register_pm_notifier(&battery->pm_nb);
1007 static int acpi_battery_remove(struct acpi_device *device, int type)
1009 struct acpi_battery *battery = NULL;
1011 if (!device || !acpi_driver_data(device))
1013 battery = acpi_driver_data(device);
1014 unregister_pm_notifier(&battery->pm_nb);
1015 #ifdef CONFIG_ACPI_PROCFS_POWER
1016 acpi_battery_remove_fs(device);
1018 sysfs_remove_battery(battery);
1019 mutex_destroy(&battery->lock);
1024 /* this is needed to learn about changes made in suspended state */
1025 static int acpi_battery_resume(struct acpi_device *device)
1027 struct acpi_battery *battery;
1030 battery = acpi_driver_data(device);
1031 battery->update_time = 0;
1032 acpi_battery_update(battery);
1036 static struct acpi_driver acpi_battery_driver = {
1038 .class = ACPI_BATTERY_CLASS,
1039 .ids = battery_device_ids,
1040 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1042 .add = acpi_battery_add,
1043 .resume = acpi_battery_resume,
1044 .remove = acpi_battery_remove,
1045 .notify = acpi_battery_notify,
1049 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1053 #ifdef CONFIG_ACPI_PROCFS_POWER
1054 acpi_battery_dir = acpi_lock_battery_dir();
1055 if (!acpi_battery_dir)
1058 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
1059 #ifdef CONFIG_ACPI_PROCFS_POWER
1060 acpi_unlock_battery_dir(acpi_battery_dir);
1067 static int __init acpi_battery_init(void)
1069 async_schedule(acpi_battery_init_async, NULL);
1073 static void __exit acpi_battery_exit(void)
1075 acpi_bus_unregister_driver(&acpi_battery_driver);
1076 #ifdef CONFIG_ACPI_PROCFS_POWER
1077 acpi_unlock_battery_dir(acpi_battery_dir);
1081 module_init(acpi_battery_init);
1082 module_exit(acpi_battery_exit);