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>
45 #include <linux/power_supply.h>
47 #define PREFIX "ACPI: "
49 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51 #define ACPI_BATTERY_CLASS "battery"
52 #define ACPI_BATTERY_DEVICE_NAME "Battery"
53 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
54 #define ACPI_BATTERY_NOTIFY_INFO 0x81
55 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
57 #define _COMPONENT ACPI_BATTERY_COMPONENT
59 ACPI_MODULE_NAME("battery");
61 MODULE_AUTHOR("Paul Diefenbaugh");
62 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
63 MODULE_DESCRIPTION("ACPI Battery Driver");
64 MODULE_LICENSE("GPL");
66 static unsigned int cache_time = 1000;
67 module_param(cache_time, uint, 0644);
68 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
70 #ifdef CONFIG_ACPI_PROCFS_POWER
71 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
72 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
74 enum acpi_battery_files {
78 ACPI_BATTERY_NUMFILES,
83 static const struct acpi_device_id battery_device_ids[] = {
88 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
91 ACPI_BATTERY_ALARM_PRESENT,
92 ACPI_BATTERY_XINFO_PRESENT,
93 /* For buggy DSDTs that report negative 16-bit values for either
94 * charging or discharging current and/or report 0 as 65536
97 ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
100 struct acpi_battery {
102 struct power_supply bat;
103 struct acpi_device *device;
104 unsigned long update_time;
109 int full_charge_capacity;
112 int design_capacity_warning;
113 int design_capacity_low;
115 int measurement_accuracy;
116 int max_sampling_time;
117 int min_sampling_time;
118 int max_averaging_interval;
119 int min_averaging_interval;
120 int capacity_granularity_1;
121 int capacity_granularity_2;
123 char model_number[32];
124 char serial_number[32];
132 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
134 inline int acpi_battery_present(struct acpi_battery *battery)
136 return battery->device->status.battery_present;
139 static int acpi_battery_technology(struct acpi_battery *battery)
141 if (!strcasecmp("NiCd", battery->type))
142 return POWER_SUPPLY_TECHNOLOGY_NiCd;
143 if (!strcasecmp("NiMH", battery->type))
144 return POWER_SUPPLY_TECHNOLOGY_NiMH;
145 if (!strcasecmp("LION", battery->type))
146 return POWER_SUPPLY_TECHNOLOGY_LION;
147 if (!strncasecmp("LI-ION", battery->type, 6))
148 return POWER_SUPPLY_TECHNOLOGY_LION;
149 if (!strcasecmp("LiP", battery->type))
150 return POWER_SUPPLY_TECHNOLOGY_LIPO;
151 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
154 static int acpi_battery_get_state(struct acpi_battery *battery);
156 static int acpi_battery_is_charged(struct acpi_battery *battery)
158 /* either charging or discharging */
159 if (battery->state != 0)
162 /* battery not reporting charge */
163 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
164 battery->capacity_now == 0)
167 /* good batteries update full_charge as the batteries degrade */
168 if (battery->full_charge_capacity == battery->capacity_now)
171 /* fallback to using design values for broken batteries */
172 if (battery->design_capacity == battery->capacity_now)
175 /* we don't do any sort of metric based on percentages */
179 static int acpi_battery_get_property(struct power_supply *psy,
180 enum power_supply_property psp,
181 union power_supply_propval *val)
183 struct acpi_battery *battery = to_acpi_battery(psy);
185 if (acpi_battery_present(battery)) {
186 /* run battery update only if it is present */
187 acpi_battery_get_state(battery);
188 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
191 case POWER_SUPPLY_PROP_STATUS:
192 if (battery->state & 0x01)
193 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
194 else if (battery->state & 0x02)
195 val->intval = POWER_SUPPLY_STATUS_CHARGING;
196 else if (acpi_battery_is_charged(battery))
197 val->intval = POWER_SUPPLY_STATUS_FULL;
199 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
201 case POWER_SUPPLY_PROP_PRESENT:
202 val->intval = acpi_battery_present(battery);
204 case POWER_SUPPLY_PROP_TECHNOLOGY:
205 val->intval = acpi_battery_technology(battery);
207 case POWER_SUPPLY_PROP_CYCLE_COUNT:
208 val->intval = battery->cycle_count;
210 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
211 val->intval = battery->design_voltage * 1000;
213 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
214 val->intval = battery->voltage_now * 1000;
216 case POWER_SUPPLY_PROP_CURRENT_NOW:
217 case POWER_SUPPLY_PROP_POWER_NOW:
218 val->intval = battery->rate_now * 1000;
220 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
221 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
222 val->intval = battery->design_capacity * 1000;
224 case POWER_SUPPLY_PROP_CHARGE_FULL:
225 case POWER_SUPPLY_PROP_ENERGY_FULL:
226 val->intval = battery->full_charge_capacity * 1000;
228 case POWER_SUPPLY_PROP_CHARGE_NOW:
229 case POWER_SUPPLY_PROP_ENERGY_NOW:
230 val->intval = battery->capacity_now * 1000;
232 case POWER_SUPPLY_PROP_MODEL_NAME:
233 val->strval = battery->model_number;
235 case POWER_SUPPLY_PROP_MANUFACTURER:
236 val->strval = battery->oem_info;
238 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
239 val->strval = battery->serial_number;
247 static enum power_supply_property charge_battery_props[] = {
248 POWER_SUPPLY_PROP_STATUS,
249 POWER_SUPPLY_PROP_PRESENT,
250 POWER_SUPPLY_PROP_TECHNOLOGY,
251 POWER_SUPPLY_PROP_CYCLE_COUNT,
252 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
253 POWER_SUPPLY_PROP_VOLTAGE_NOW,
254 POWER_SUPPLY_PROP_CURRENT_NOW,
255 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
256 POWER_SUPPLY_PROP_CHARGE_FULL,
257 POWER_SUPPLY_PROP_CHARGE_NOW,
258 POWER_SUPPLY_PROP_MODEL_NAME,
259 POWER_SUPPLY_PROP_MANUFACTURER,
260 POWER_SUPPLY_PROP_SERIAL_NUMBER,
263 static enum power_supply_property energy_battery_props[] = {
264 POWER_SUPPLY_PROP_STATUS,
265 POWER_SUPPLY_PROP_PRESENT,
266 POWER_SUPPLY_PROP_TECHNOLOGY,
267 POWER_SUPPLY_PROP_CYCLE_COUNT,
268 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
269 POWER_SUPPLY_PROP_VOLTAGE_NOW,
270 POWER_SUPPLY_PROP_POWER_NOW,
271 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
272 POWER_SUPPLY_PROP_ENERGY_FULL,
273 POWER_SUPPLY_PROP_ENERGY_NOW,
274 POWER_SUPPLY_PROP_MODEL_NAME,
275 POWER_SUPPLY_PROP_MANUFACTURER,
276 POWER_SUPPLY_PROP_SERIAL_NUMBER,
279 #ifdef CONFIG_ACPI_PROCFS_POWER
280 inline char *acpi_battery_units(struct acpi_battery *battery)
282 return (battery->power_unit)?"mA":"mW";
286 /* --------------------------------------------------------------------------
288 -------------------------------------------------------------------------- */
289 struct acpi_offsets {
290 size_t offset; /* offset inside struct acpi_sbs_battery */
291 u8 mode; /* int or string? */
294 static struct acpi_offsets state_offsets[] = {
295 {offsetof(struct acpi_battery, state), 0},
296 {offsetof(struct acpi_battery, rate_now), 0},
297 {offsetof(struct acpi_battery, capacity_now), 0},
298 {offsetof(struct acpi_battery, voltage_now), 0},
301 static struct acpi_offsets info_offsets[] = {
302 {offsetof(struct acpi_battery, power_unit), 0},
303 {offsetof(struct acpi_battery, design_capacity), 0},
304 {offsetof(struct acpi_battery, full_charge_capacity), 0},
305 {offsetof(struct acpi_battery, technology), 0},
306 {offsetof(struct acpi_battery, design_voltage), 0},
307 {offsetof(struct acpi_battery, design_capacity_warning), 0},
308 {offsetof(struct acpi_battery, design_capacity_low), 0},
309 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
310 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
311 {offsetof(struct acpi_battery, model_number), 1},
312 {offsetof(struct acpi_battery, serial_number), 1},
313 {offsetof(struct acpi_battery, type), 1},
314 {offsetof(struct acpi_battery, oem_info), 1},
317 static struct acpi_offsets extended_info_offsets[] = {
318 {offsetof(struct acpi_battery, power_unit), 0},
319 {offsetof(struct acpi_battery, design_capacity), 0},
320 {offsetof(struct acpi_battery, full_charge_capacity), 0},
321 {offsetof(struct acpi_battery, technology), 0},
322 {offsetof(struct acpi_battery, design_voltage), 0},
323 {offsetof(struct acpi_battery, design_capacity_warning), 0},
324 {offsetof(struct acpi_battery, design_capacity_low), 0},
325 {offsetof(struct acpi_battery, cycle_count), 0},
326 {offsetof(struct acpi_battery, measurement_accuracy), 0},
327 {offsetof(struct acpi_battery, max_sampling_time), 0},
328 {offsetof(struct acpi_battery, min_sampling_time), 0},
329 {offsetof(struct acpi_battery, max_averaging_interval), 0},
330 {offsetof(struct acpi_battery, min_averaging_interval), 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 int extract_package(struct acpi_battery *battery,
340 union acpi_object *package,
341 struct acpi_offsets *offsets, int num)
344 union acpi_object *element;
345 if (package->type != ACPI_TYPE_PACKAGE)
347 for (i = 0; i < num; ++i) {
348 if (package->package.count <= i)
350 element = &package->package.elements[i];
351 if (offsets[i].mode) {
352 u8 *ptr = (u8 *)battery + offsets[i].offset;
353 if (element->type == ACPI_TYPE_STRING ||
354 element->type == ACPI_TYPE_BUFFER)
355 strncpy(ptr, element->string.pointer, 32);
356 else if (element->type == ACPI_TYPE_INTEGER) {
357 strncpy(ptr, (u8 *)&element->integer.value,
359 ptr[sizeof(u64)] = 0;
361 *ptr = 0; /* don't have value */
363 int *x = (int *)((u8 *)battery + offsets[i].offset);
364 *x = (element->type == ACPI_TYPE_INTEGER) ?
365 element->integer.value : -1;
371 static int acpi_battery_get_status(struct acpi_battery *battery)
373 if (acpi_bus_get_status(battery->device)) {
374 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
380 static int acpi_battery_get_info(struct acpi_battery *battery)
382 int result = -EFAULT;
383 acpi_status status = 0;
384 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
387 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
389 if (!acpi_battery_present(battery))
391 mutex_lock(&battery->lock);
392 status = acpi_evaluate_object(battery->device->handle, name,
394 mutex_unlock(&battery->lock);
396 if (ACPI_FAILURE(status)) {
397 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
400 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
401 result = extract_package(battery, buffer.pointer,
402 extended_info_offsets,
403 ARRAY_SIZE(extended_info_offsets));
405 result = extract_package(battery, buffer.pointer,
406 info_offsets, ARRAY_SIZE(info_offsets));
407 kfree(buffer.pointer);
411 static int acpi_battery_get_state(struct acpi_battery *battery)
414 acpi_status status = 0;
415 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
417 if (!acpi_battery_present(battery))
420 if (battery->update_time &&
421 time_before(jiffies, battery->update_time +
422 msecs_to_jiffies(cache_time)))
425 mutex_lock(&battery->lock);
426 status = acpi_evaluate_object(battery->device->handle, "_BST",
428 mutex_unlock(&battery->lock);
430 if (ACPI_FAILURE(status)) {
431 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
435 result = extract_package(battery, buffer.pointer,
436 state_offsets, ARRAY_SIZE(state_offsets));
437 battery->update_time = jiffies;
438 kfree(buffer.pointer);
440 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
441 battery->rate_now != -1)
442 battery->rate_now = abs((s16)battery->rate_now);
447 static int acpi_battery_set_alarm(struct acpi_battery *battery)
449 acpi_status status = 0;
450 union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
451 struct acpi_object_list arg_list = { 1, &arg0 };
453 if (!acpi_battery_present(battery) ||
454 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
457 arg0.integer.value = battery->alarm;
459 mutex_lock(&battery->lock);
460 status = acpi_evaluate_object(battery->device->handle, "_BTP",
462 mutex_unlock(&battery->lock);
464 if (ACPI_FAILURE(status))
467 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
471 static int acpi_battery_init_alarm(struct acpi_battery *battery)
473 acpi_status status = AE_OK;
474 acpi_handle handle = NULL;
476 /* See if alarms are supported, and if so, set default */
477 status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
478 if (ACPI_FAILURE(status)) {
479 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
482 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
484 battery->alarm = battery->design_capacity_warning;
485 return acpi_battery_set_alarm(battery);
488 static ssize_t acpi_battery_alarm_show(struct device *dev,
489 struct device_attribute *attr,
492 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
493 return sprintf(buf, "%d\n", battery->alarm * 1000);
496 static ssize_t acpi_battery_alarm_store(struct device *dev,
497 struct device_attribute *attr,
498 const char *buf, size_t count)
501 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
502 if (sscanf(buf, "%ld\n", &x) == 1)
503 battery->alarm = x/1000;
504 if (acpi_battery_present(battery))
505 acpi_battery_set_alarm(battery);
509 static struct device_attribute alarm_attr = {
510 .attr = {.name = "alarm", .mode = 0644},
511 .show = acpi_battery_alarm_show,
512 .store = acpi_battery_alarm_store,
515 static int sysfs_add_battery(struct acpi_battery *battery)
519 if (battery->power_unit) {
520 battery->bat.properties = charge_battery_props;
521 battery->bat.num_properties =
522 ARRAY_SIZE(charge_battery_props);
524 battery->bat.properties = energy_battery_props;
525 battery->bat.num_properties =
526 ARRAY_SIZE(energy_battery_props);
529 battery->bat.name = acpi_device_bid(battery->device);
530 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
531 battery->bat.get_property = acpi_battery_get_property;
533 result = power_supply_register(&battery->device->dev, &battery->bat);
536 return device_create_file(battery->bat.dev, &alarm_attr);
539 static void sysfs_remove_battery(struct acpi_battery *battery)
541 if (!battery->bat.dev)
543 device_remove_file(battery->bat.dev, &alarm_attr);
544 power_supply_unregister(&battery->bat);
545 battery->bat.dev = NULL;
548 static void acpi_battery_quirks(struct acpi_battery *battery)
550 if (dmi_name_in_vendors("Acer") && battery->power_unit) {
551 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
555 static int acpi_battery_update(struct acpi_battery *battery)
557 int result, old_present = acpi_battery_present(battery);
558 result = acpi_battery_get_status(battery);
561 if (!acpi_battery_present(battery)) {
562 sysfs_remove_battery(battery);
563 battery->update_time = 0;
566 if (!battery->update_time ||
567 old_present != acpi_battery_present(battery)) {
568 result = acpi_battery_get_info(battery);
571 acpi_battery_quirks(battery);
572 acpi_battery_init_alarm(battery);
574 if (!battery->bat.dev)
575 sysfs_add_battery(battery);
576 return acpi_battery_get_state(battery);
579 /* --------------------------------------------------------------------------
581 -------------------------------------------------------------------------- */
583 #ifdef CONFIG_ACPI_PROCFS_POWER
584 static struct proc_dir_entry *acpi_battery_dir;
586 static int acpi_battery_print_info(struct seq_file *seq, int result)
588 struct acpi_battery *battery = seq->private;
593 seq_printf(seq, "present: %s\n",
594 acpi_battery_present(battery)?"yes":"no");
595 if (!acpi_battery_present(battery))
597 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
598 seq_printf(seq, "design capacity: unknown\n");
600 seq_printf(seq, "design capacity: %d %sh\n",
601 battery->design_capacity,
602 acpi_battery_units(battery));
604 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
605 seq_printf(seq, "last full capacity: unknown\n");
607 seq_printf(seq, "last full capacity: %d %sh\n",
608 battery->full_charge_capacity,
609 acpi_battery_units(battery));
611 seq_printf(seq, "battery technology: %srechargeable\n",
612 (!battery->technology)?"non-":"");
614 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
615 seq_printf(seq, "design voltage: unknown\n");
617 seq_printf(seq, "design voltage: %d mV\n",
618 battery->design_voltage);
619 seq_printf(seq, "design capacity warning: %d %sh\n",
620 battery->design_capacity_warning,
621 acpi_battery_units(battery));
622 seq_printf(seq, "design capacity low: %d %sh\n",
623 battery->design_capacity_low,
624 acpi_battery_units(battery));
625 seq_printf(seq, "cycle count: %i\n", battery->cycle_count);
626 seq_printf(seq, "capacity granularity 1: %d %sh\n",
627 battery->capacity_granularity_1,
628 acpi_battery_units(battery));
629 seq_printf(seq, "capacity granularity 2: %d %sh\n",
630 battery->capacity_granularity_2,
631 acpi_battery_units(battery));
632 seq_printf(seq, "model number: %s\n", battery->model_number);
633 seq_printf(seq, "serial number: %s\n", battery->serial_number);
634 seq_printf(seq, "battery type: %s\n", battery->type);
635 seq_printf(seq, "OEM info: %s\n", battery->oem_info);
638 seq_printf(seq, "ERROR: Unable to read battery info\n");
642 static int acpi_battery_print_state(struct seq_file *seq, int result)
644 struct acpi_battery *battery = seq->private;
649 seq_printf(seq, "present: %s\n",
650 acpi_battery_present(battery)?"yes":"no");
651 if (!acpi_battery_present(battery))
654 seq_printf(seq, "capacity state: %s\n",
655 (battery->state & 0x04)?"critical":"ok");
656 if ((battery->state & 0x01) && (battery->state & 0x02))
658 "charging state: charging/discharging\n");
659 else if (battery->state & 0x01)
660 seq_printf(seq, "charging state: discharging\n");
661 else if (battery->state & 0x02)
662 seq_printf(seq, "charging state: charging\n");
664 seq_printf(seq, "charging state: charged\n");
666 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
667 seq_printf(seq, "present rate: unknown\n");
669 seq_printf(seq, "present rate: %d %s\n",
670 battery->rate_now, acpi_battery_units(battery));
672 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
673 seq_printf(seq, "remaining capacity: unknown\n");
675 seq_printf(seq, "remaining capacity: %d %sh\n",
676 battery->capacity_now, acpi_battery_units(battery));
677 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
678 seq_printf(seq, "present voltage: unknown\n");
680 seq_printf(seq, "present voltage: %d mV\n",
681 battery->voltage_now);
684 seq_printf(seq, "ERROR: Unable to read battery state\n");
689 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
691 struct acpi_battery *battery = seq->private;
696 if (!acpi_battery_present(battery)) {
697 seq_printf(seq, "present: no\n");
700 seq_printf(seq, "alarm: ");
702 seq_printf(seq, "unsupported\n");
704 seq_printf(seq, "%u %sh\n", battery->alarm,
705 acpi_battery_units(battery));
708 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
712 static ssize_t acpi_battery_write_alarm(struct file *file,
713 const char __user * buffer,
714 size_t count, loff_t * ppos)
717 char alarm_string[12] = { '\0' };
718 struct seq_file *m = file->private_data;
719 struct acpi_battery *battery = m->private;
721 if (!battery || (count > sizeof(alarm_string) - 1))
723 if (!acpi_battery_present(battery)) {
727 if (copy_from_user(alarm_string, buffer, count)) {
731 alarm_string[count] = '\0';
732 battery->alarm = simple_strtol(alarm_string, NULL, 0);
733 result = acpi_battery_set_alarm(battery);
740 typedef int(*print_func)(struct seq_file *seq, int result);
742 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
743 acpi_battery_print_info,
744 acpi_battery_print_state,
745 acpi_battery_print_alarm,
748 static int acpi_battery_read(int fid, struct seq_file *seq)
750 struct acpi_battery *battery = seq->private;
751 int result = acpi_battery_update(battery);
752 return acpi_print_funcs[fid](seq, result);
755 #define DECLARE_FILE_FUNCTIONS(_name) \
756 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
758 return acpi_battery_read(_name##_tag, seq); \
760 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
762 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
765 DECLARE_FILE_FUNCTIONS(info);
766 DECLARE_FILE_FUNCTIONS(state);
767 DECLARE_FILE_FUNCTIONS(alarm);
769 #undef DECLARE_FILE_FUNCTIONS
771 #define FILE_DESCRIPTION_RO(_name) \
773 .name = __stringify(_name), \
776 .open = acpi_battery_##_name##_open_fs, \
778 .llseek = seq_lseek, \
779 .release = single_release, \
780 .owner = THIS_MODULE, \
784 #define FILE_DESCRIPTION_RW(_name) \
786 .name = __stringify(_name), \
787 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
789 .open = acpi_battery_##_name##_open_fs, \
791 .llseek = seq_lseek, \
792 .write = acpi_battery_write_##_name, \
793 .release = single_release, \
794 .owner = THIS_MODULE, \
798 static struct battery_file {
799 struct file_operations ops;
802 } acpi_battery_file[] = {
803 FILE_DESCRIPTION_RO(info),
804 FILE_DESCRIPTION_RO(state),
805 FILE_DESCRIPTION_RW(alarm),
808 #undef FILE_DESCRIPTION_RO
809 #undef FILE_DESCRIPTION_RW
811 static int acpi_battery_add_fs(struct acpi_device *device)
813 struct proc_dir_entry *entry = NULL;
816 if (!acpi_device_dir(device)) {
817 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
819 if (!acpi_device_dir(device))
823 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
824 entry = proc_create_data(acpi_battery_file[i].name,
825 acpi_battery_file[i].mode,
826 acpi_device_dir(device),
827 &acpi_battery_file[i].ops,
828 acpi_driver_data(device));
835 static void acpi_battery_remove_fs(struct acpi_device *device)
838 if (!acpi_device_dir(device))
840 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
841 remove_proc_entry(acpi_battery_file[i].name,
842 acpi_device_dir(device));
844 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
845 acpi_device_dir(device) = NULL;
850 /* --------------------------------------------------------------------------
852 -------------------------------------------------------------------------- */
854 static void acpi_battery_notify(struct acpi_device *device, u32 event)
856 struct acpi_battery *battery = acpi_driver_data(device);
861 old = battery->bat.dev;
862 acpi_battery_update(battery);
863 acpi_bus_generate_proc_event(device, event,
864 acpi_battery_present(battery));
865 acpi_bus_generate_netlink_event(device->pnp.device_class,
866 dev_name(&device->dev), event,
867 acpi_battery_present(battery));
868 /* acpi_battery_update could remove power_supply object */
869 if (old && battery->bat.dev)
870 power_supply_changed(&battery->bat);
873 static int acpi_battery_add(struct acpi_device *device)
876 struct acpi_battery *battery = NULL;
880 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
883 battery->device = device;
884 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
885 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
886 device->driver_data = battery;
887 mutex_init(&battery->lock);
888 if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
890 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
891 acpi_battery_update(battery);
892 #ifdef CONFIG_ACPI_PROCFS_POWER
893 result = acpi_battery_add_fs(device);
896 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
897 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
898 device->status.battery_present ? "present" : "absent");
900 #ifdef CONFIG_ACPI_PROCFS_POWER
901 acpi_battery_remove_fs(device);
908 static int acpi_battery_remove(struct acpi_device *device, int type)
910 struct acpi_battery *battery = NULL;
912 if (!device || !acpi_driver_data(device))
914 battery = acpi_driver_data(device);
915 #ifdef CONFIG_ACPI_PROCFS_POWER
916 acpi_battery_remove_fs(device);
918 sysfs_remove_battery(battery);
919 mutex_destroy(&battery->lock);
924 /* this is needed to learn about changes made in suspended state */
925 static int acpi_battery_resume(struct acpi_device *device)
927 struct acpi_battery *battery;
930 battery = acpi_driver_data(device);
931 battery->update_time = 0;
932 acpi_battery_update(battery);
936 static struct acpi_driver acpi_battery_driver = {
938 .class = ACPI_BATTERY_CLASS,
939 .ids = battery_device_ids,
940 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
942 .add = acpi_battery_add,
943 .resume = acpi_battery_resume,
944 .remove = acpi_battery_remove,
945 .notify = acpi_battery_notify,
949 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
953 #ifdef CONFIG_ACPI_PROCFS_POWER
954 acpi_battery_dir = acpi_lock_battery_dir();
955 if (!acpi_battery_dir)
958 if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
959 #ifdef CONFIG_ACPI_PROCFS_POWER
960 acpi_unlock_battery_dir(acpi_battery_dir);
967 static int __init acpi_battery_init(void)
969 async_schedule(acpi_battery_init_async, NULL);
973 static void __exit acpi_battery_exit(void)
975 acpi_bus_unregister_driver(&acpi_battery_driver);
976 #ifdef CONFIG_ACPI_PROCFS_POWER
977 acpi_unlock_battery_dir(acpi_battery_dir);
981 module_init(acpi_battery_init);
982 module_exit(acpi_battery_exit);