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
,
98 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
,
101 struct acpi_battery
{
103 struct power_supply bat
;
104 struct acpi_device
*device
;
105 unsigned long update_time
;
110 int full_charge_capacity
;
113 int design_capacity_warning
;
114 int design_capacity_low
;
116 int measurement_accuracy
;
117 int max_sampling_time
;
118 int min_sampling_time
;
119 int max_averaging_interval
;
120 int min_averaging_interval
;
121 int capacity_granularity_1
;
122 int capacity_granularity_2
;
124 char model_number
[32];
125 char serial_number
[32];
133 static int acpi_battery_update(struct acpi_battery
*battery
);
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_update(battery
))
192 if (acpi_battery_present(battery
)) {
193 /* run battery update only if it is present */
194 acpi_battery_get_state(battery
);
195 } else if (psp
!= POWER_SUPPLY_PROP_PRESENT
)
198 case POWER_SUPPLY_PROP_STATUS
:
199 if (battery
->state
& 0x01)
200 val
->intval
= POWER_SUPPLY_STATUS_DISCHARGING
;
201 else if (battery
->state
& 0x02)
202 val
->intval
= POWER_SUPPLY_STATUS_CHARGING
;
203 else if (acpi_battery_is_charged(battery
))
204 val
->intval
= POWER_SUPPLY_STATUS_FULL
;
206 val
->intval
= POWER_SUPPLY_STATUS_UNKNOWN
;
208 case POWER_SUPPLY_PROP_PRESENT
:
209 val
->intval
= acpi_battery_present(battery
);
211 case POWER_SUPPLY_PROP_TECHNOLOGY
:
212 val
->intval
= acpi_battery_technology(battery
);
214 case POWER_SUPPLY_PROP_CYCLE_COUNT
:
215 val
->intval
= battery
->cycle_count
;
217 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
:
218 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
221 val
->intval
= battery
->design_voltage
* 1000;
223 case POWER_SUPPLY_PROP_VOLTAGE_NOW
:
224 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
227 val
->intval
= battery
->voltage_now
* 1000;
229 case POWER_SUPPLY_PROP_CURRENT_NOW
:
230 case POWER_SUPPLY_PROP_POWER_NOW
:
231 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
234 val
->intval
= battery
->rate_now
* 1000;
236 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
:
237 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
:
238 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
241 val
->intval
= battery
->design_capacity
* 1000;
243 case POWER_SUPPLY_PROP_CHARGE_FULL
:
244 case POWER_SUPPLY_PROP_ENERGY_FULL
:
245 if (battery
->full_charge_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
248 val
->intval
= battery
->full_charge_capacity
* 1000;
250 case POWER_SUPPLY_PROP_CHARGE_NOW
:
251 case POWER_SUPPLY_PROP_ENERGY_NOW
:
252 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
255 val
->intval
= battery
->capacity_now
* 1000;
257 case POWER_SUPPLY_PROP_MODEL_NAME
:
258 val
->strval
= battery
->model_number
;
260 case POWER_SUPPLY_PROP_MANUFACTURER
:
261 val
->strval
= battery
->oem_info
;
263 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
264 val
->strval
= battery
->serial_number
;
272 static enum power_supply_property charge_battery_props
[] = {
273 POWER_SUPPLY_PROP_STATUS
,
274 POWER_SUPPLY_PROP_PRESENT
,
275 POWER_SUPPLY_PROP_TECHNOLOGY
,
276 POWER_SUPPLY_PROP_CYCLE_COUNT
,
277 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
278 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
279 POWER_SUPPLY_PROP_CURRENT_NOW
,
280 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
281 POWER_SUPPLY_PROP_CHARGE_FULL
,
282 POWER_SUPPLY_PROP_CHARGE_NOW
,
283 POWER_SUPPLY_PROP_MODEL_NAME
,
284 POWER_SUPPLY_PROP_MANUFACTURER
,
285 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
288 static enum power_supply_property energy_battery_props
[] = {
289 POWER_SUPPLY_PROP_STATUS
,
290 POWER_SUPPLY_PROP_PRESENT
,
291 POWER_SUPPLY_PROP_TECHNOLOGY
,
292 POWER_SUPPLY_PROP_CYCLE_COUNT
,
293 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
294 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
295 POWER_SUPPLY_PROP_POWER_NOW
,
296 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
297 POWER_SUPPLY_PROP_ENERGY_FULL
,
298 POWER_SUPPLY_PROP_ENERGY_NOW
,
299 POWER_SUPPLY_PROP_MODEL_NAME
,
300 POWER_SUPPLY_PROP_MANUFACTURER
,
301 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
304 #ifdef CONFIG_ACPI_PROCFS_POWER
305 inline char *acpi_battery_units(struct acpi_battery
*battery
)
307 return (battery
->power_unit
)?"mA":"mW";
311 /* --------------------------------------------------------------------------
313 -------------------------------------------------------------------------- */
314 struct acpi_offsets
{
315 size_t offset
; /* offset inside struct acpi_sbs_battery */
316 u8 mode
; /* int or string? */
319 static struct acpi_offsets state_offsets
[] = {
320 {offsetof(struct acpi_battery
, state
), 0},
321 {offsetof(struct acpi_battery
, rate_now
), 0},
322 {offsetof(struct acpi_battery
, capacity_now
), 0},
323 {offsetof(struct acpi_battery
, voltage_now
), 0},
326 static struct acpi_offsets info_offsets
[] = {
327 {offsetof(struct acpi_battery
, power_unit
), 0},
328 {offsetof(struct acpi_battery
, design_capacity
), 0},
329 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
330 {offsetof(struct acpi_battery
, technology
), 0},
331 {offsetof(struct acpi_battery
, design_voltage
), 0},
332 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
333 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
334 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
335 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
336 {offsetof(struct acpi_battery
, model_number
), 1},
337 {offsetof(struct acpi_battery
, serial_number
), 1},
338 {offsetof(struct acpi_battery
, type
), 1},
339 {offsetof(struct acpi_battery
, oem_info
), 1},
342 static struct acpi_offsets extended_info_offsets
[] = {
343 {offsetof(struct acpi_battery
, power_unit
), 0},
344 {offsetof(struct acpi_battery
, design_capacity
), 0},
345 {offsetof(struct acpi_battery
, full_charge_capacity
), 0},
346 {offsetof(struct acpi_battery
, technology
), 0},
347 {offsetof(struct acpi_battery
, design_voltage
), 0},
348 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
349 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
350 {offsetof(struct acpi_battery
, cycle_count
), 0},
351 {offsetof(struct acpi_battery
, measurement_accuracy
), 0},
352 {offsetof(struct acpi_battery
, max_sampling_time
), 0},
353 {offsetof(struct acpi_battery
, min_sampling_time
), 0},
354 {offsetof(struct acpi_battery
, max_averaging_interval
), 0},
355 {offsetof(struct acpi_battery
, min_averaging_interval
), 0},
356 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
357 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
358 {offsetof(struct acpi_battery
, model_number
), 1},
359 {offsetof(struct acpi_battery
, serial_number
), 1},
360 {offsetof(struct acpi_battery
, type
), 1},
361 {offsetof(struct acpi_battery
, oem_info
), 1},
364 static int extract_package(struct acpi_battery
*battery
,
365 union acpi_object
*package
,
366 struct acpi_offsets
*offsets
, int num
)
369 union acpi_object
*element
;
370 if (package
->type
!= ACPI_TYPE_PACKAGE
)
372 for (i
= 0; i
< num
; ++i
) {
373 if (package
->package
.count
<= i
)
375 element
= &package
->package
.elements
[i
];
376 if (offsets
[i
].mode
) {
377 u8
*ptr
= (u8
*)battery
+ offsets
[i
].offset
;
378 if (element
->type
== ACPI_TYPE_STRING
||
379 element
->type
== ACPI_TYPE_BUFFER
)
380 strncpy(ptr
, element
->string
.pointer
, 32);
381 else if (element
->type
== ACPI_TYPE_INTEGER
) {
382 strncpy(ptr
, (u8
*)&element
->integer
.value
,
384 ptr
[sizeof(u64
)] = 0;
386 *ptr
= 0; /* don't have value */
388 int *x
= (int *)((u8
*)battery
+ offsets
[i
].offset
);
389 *x
= (element
->type
== ACPI_TYPE_INTEGER
) ?
390 element
->integer
.value
: -1;
396 static int acpi_battery_get_status(struct acpi_battery
*battery
)
398 if (acpi_bus_get_status(battery
->device
)) {
399 ACPI_EXCEPTION((AE_INFO
, AE_ERROR
, "Evaluating _STA"));
405 static int acpi_battery_get_info(struct acpi_battery
*battery
)
407 int result
= -EFAULT
;
408 acpi_status status
= 0;
409 char *name
= test_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
)?
412 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
414 if (!acpi_battery_present(battery
))
416 mutex_lock(&battery
->lock
);
417 status
= acpi_evaluate_object(battery
->device
->handle
, name
,
419 mutex_unlock(&battery
->lock
);
421 if (ACPI_FAILURE(status
)) {
422 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating %s", name
));
425 if (test_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
))
426 result
= extract_package(battery
, buffer
.pointer
,
427 extended_info_offsets
,
428 ARRAY_SIZE(extended_info_offsets
));
430 result
= extract_package(battery
, buffer
.pointer
,
431 info_offsets
, ARRAY_SIZE(info_offsets
));
432 kfree(buffer
.pointer
);
433 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
434 battery
->full_charge_capacity
= battery
->design_capacity
;
438 static int acpi_battery_get_state(struct acpi_battery
*battery
)
441 acpi_status status
= 0;
442 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
444 if (!acpi_battery_present(battery
))
447 if (battery
->update_time
&&
448 time_before(jiffies
, battery
->update_time
+
449 msecs_to_jiffies(cache_time
)))
452 mutex_lock(&battery
->lock
);
453 status
= acpi_evaluate_object(battery
->device
->handle
, "_BST",
455 mutex_unlock(&battery
->lock
);
457 if (ACPI_FAILURE(status
)) {
458 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BST"));
462 result
= extract_package(battery
, buffer
.pointer
,
463 state_offsets
, ARRAY_SIZE(state_offsets
));
464 battery
->update_time
= jiffies
;
465 kfree(buffer
.pointer
);
467 if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT
, &battery
->flags
) &&
468 battery
->rate_now
!= -1)
469 battery
->rate_now
= abs((s16
)battery
->rate_now
);
471 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
)
472 && battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100)
473 battery
->capacity_now
= (battery
->capacity_now
*
474 battery
->full_charge_capacity
) / 100;
478 static int acpi_battery_set_alarm(struct acpi_battery
*battery
)
480 acpi_status status
= 0;
481 union acpi_object arg0
= { .type
= ACPI_TYPE_INTEGER
};
482 struct acpi_object_list arg_list
= { 1, &arg0
};
484 if (!acpi_battery_present(battery
) ||
485 !test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
))
488 arg0
.integer
.value
= battery
->alarm
;
490 mutex_lock(&battery
->lock
);
491 status
= acpi_evaluate_object(battery
->device
->handle
, "_BTP",
493 mutex_unlock(&battery
->lock
);
495 if (ACPI_FAILURE(status
))
498 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", battery
->alarm
));
502 static int acpi_battery_init_alarm(struct acpi_battery
*battery
)
504 acpi_status status
= AE_OK
;
505 acpi_handle handle
= NULL
;
507 /* See if alarms are supported, and if so, set default */
508 status
= acpi_get_handle(battery
->device
->handle
, "_BTP", &handle
);
509 if (ACPI_FAILURE(status
)) {
510 clear_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
513 set_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
515 battery
->alarm
= battery
->design_capacity_warning
;
516 return acpi_battery_set_alarm(battery
);
519 static ssize_t
acpi_battery_alarm_show(struct device
*dev
,
520 struct device_attribute
*attr
,
523 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
524 return sprintf(buf
, "%d\n", battery
->alarm
* 1000);
527 static ssize_t
acpi_battery_alarm_store(struct device
*dev
,
528 struct device_attribute
*attr
,
529 const char *buf
, size_t count
)
532 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
533 if (sscanf(buf
, "%ld\n", &x
) == 1)
534 battery
->alarm
= x
/1000;
535 if (acpi_battery_present(battery
))
536 acpi_battery_set_alarm(battery
);
540 static struct device_attribute alarm_attr
= {
541 .attr
= {.name
= "alarm", .mode
= 0644},
542 .show
= acpi_battery_alarm_show
,
543 .store
= acpi_battery_alarm_store
,
546 static int sysfs_add_battery(struct acpi_battery
*battery
)
550 if (battery
->power_unit
) {
551 battery
->bat
.properties
= charge_battery_props
;
552 battery
->bat
.num_properties
=
553 ARRAY_SIZE(charge_battery_props
);
555 battery
->bat
.properties
= energy_battery_props
;
556 battery
->bat
.num_properties
=
557 ARRAY_SIZE(energy_battery_props
);
560 battery
->bat
.name
= acpi_device_bid(battery
->device
);
561 battery
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
562 battery
->bat
.get_property
= acpi_battery_get_property
;
564 result
= power_supply_register(&battery
->device
->dev
, &battery
->bat
);
567 return device_create_file(battery
->bat
.dev
, &alarm_attr
);
570 static void sysfs_remove_battery(struct acpi_battery
*battery
)
572 if (!battery
->bat
.dev
)
574 device_remove_file(battery
->bat
.dev
, &alarm_attr
);
575 power_supply_unregister(&battery
->bat
);
576 battery
->bat
.dev
= NULL
;
579 static void acpi_battery_quirks(struct acpi_battery
*battery
)
581 if (dmi_name_in_vendors("Acer") && battery
->power_unit
) {
582 set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT
, &battery
->flags
);
587 * According to the ACPI spec, some kinds of primary batteries can
588 * report percentage battery remaining capacity directly to OS.
589 * In this case, it reports the Last Full Charged Capacity == 100
590 * and BatteryPresentRate == 0xFFFFFFFF.
592 * Now we found some battery reports percentage remaining capacity
593 * even if it's rechargeable.
594 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
596 * Handle this correctly so that they won't break userspace.
598 static void acpi_battery_quirks2(struct acpi_battery
*battery
)
600 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
603 if (battery
->full_charge_capacity
== 100 &&
604 battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
&&
605 battery
->capacity_now
>=0 && battery
->capacity_now
<= 100) {
606 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
);
607 battery
->full_charge_capacity
= battery
->design_capacity
;
608 battery
->capacity_now
= (battery
->capacity_now
*
609 battery
->full_charge_capacity
) / 100;
613 static int acpi_battery_update(struct acpi_battery
*battery
)
615 int result
, old_present
= acpi_battery_present(battery
);
616 result
= acpi_battery_get_status(battery
);
619 if (!acpi_battery_present(battery
)) {
620 sysfs_remove_battery(battery
);
621 battery
->update_time
= 0;
624 if (!battery
->update_time
||
625 old_present
!= acpi_battery_present(battery
)) {
626 result
= acpi_battery_get_info(battery
);
629 acpi_battery_quirks(battery
);
630 acpi_battery_init_alarm(battery
);
632 if (!battery
->bat
.dev
)
633 sysfs_add_battery(battery
);
634 result
= acpi_battery_get_state(battery
);
635 acpi_battery_quirks2(battery
);
639 /* --------------------------------------------------------------------------
641 -------------------------------------------------------------------------- */
643 #ifdef CONFIG_ACPI_PROCFS_POWER
644 static struct proc_dir_entry
*acpi_battery_dir
;
646 static int acpi_battery_print_info(struct seq_file
*seq
, int result
)
648 struct acpi_battery
*battery
= seq
->private;
653 seq_printf(seq
, "present: %s\n",
654 acpi_battery_present(battery
)?"yes":"no");
655 if (!acpi_battery_present(battery
))
657 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
658 seq_printf(seq
, "design capacity: unknown\n");
660 seq_printf(seq
, "design capacity: %d %sh\n",
661 battery
->design_capacity
,
662 acpi_battery_units(battery
));
664 if (battery
->full_charge_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
665 seq_printf(seq
, "last full capacity: unknown\n");
667 seq_printf(seq
, "last full capacity: %d %sh\n",
668 battery
->full_charge_capacity
,
669 acpi_battery_units(battery
));
671 seq_printf(seq
, "battery technology: %srechargeable\n",
672 (!battery
->technology
)?"non-":"");
674 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
675 seq_printf(seq
, "design voltage: unknown\n");
677 seq_printf(seq
, "design voltage: %d mV\n",
678 battery
->design_voltage
);
679 seq_printf(seq
, "design capacity warning: %d %sh\n",
680 battery
->design_capacity_warning
,
681 acpi_battery_units(battery
));
682 seq_printf(seq
, "design capacity low: %d %sh\n",
683 battery
->design_capacity_low
,
684 acpi_battery_units(battery
));
685 seq_printf(seq
, "cycle count: %i\n", battery
->cycle_count
);
686 seq_printf(seq
, "capacity granularity 1: %d %sh\n",
687 battery
->capacity_granularity_1
,
688 acpi_battery_units(battery
));
689 seq_printf(seq
, "capacity granularity 2: %d %sh\n",
690 battery
->capacity_granularity_2
,
691 acpi_battery_units(battery
));
692 seq_printf(seq
, "model number: %s\n", battery
->model_number
);
693 seq_printf(seq
, "serial number: %s\n", battery
->serial_number
);
694 seq_printf(seq
, "battery type: %s\n", battery
->type
);
695 seq_printf(seq
, "OEM info: %s\n", battery
->oem_info
);
698 seq_printf(seq
, "ERROR: Unable to read battery info\n");
702 static int acpi_battery_print_state(struct seq_file
*seq
, int result
)
704 struct acpi_battery
*battery
= seq
->private;
709 seq_printf(seq
, "present: %s\n",
710 acpi_battery_present(battery
)?"yes":"no");
711 if (!acpi_battery_present(battery
))
714 seq_printf(seq
, "capacity state: %s\n",
715 (battery
->state
& 0x04)?"critical":"ok");
716 if ((battery
->state
& 0x01) && (battery
->state
& 0x02))
718 "charging state: charging/discharging\n");
719 else if (battery
->state
& 0x01)
720 seq_printf(seq
, "charging state: discharging\n");
721 else if (battery
->state
& 0x02)
722 seq_printf(seq
, "charging state: charging\n");
724 seq_printf(seq
, "charging state: charged\n");
726 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
727 seq_printf(seq
, "present rate: unknown\n");
729 seq_printf(seq
, "present rate: %d %s\n",
730 battery
->rate_now
, acpi_battery_units(battery
));
732 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
733 seq_printf(seq
, "remaining capacity: unknown\n");
735 seq_printf(seq
, "remaining capacity: %d %sh\n",
736 battery
->capacity_now
, acpi_battery_units(battery
));
737 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
738 seq_printf(seq
, "present voltage: unknown\n");
740 seq_printf(seq
, "present voltage: %d mV\n",
741 battery
->voltage_now
);
744 seq_printf(seq
, "ERROR: Unable to read battery state\n");
749 static int acpi_battery_print_alarm(struct seq_file
*seq
, int result
)
751 struct acpi_battery
*battery
= seq
->private;
756 if (!acpi_battery_present(battery
)) {
757 seq_printf(seq
, "present: no\n");
760 seq_printf(seq
, "alarm: ");
762 seq_printf(seq
, "unsupported\n");
764 seq_printf(seq
, "%u %sh\n", battery
->alarm
,
765 acpi_battery_units(battery
));
768 seq_printf(seq
, "ERROR: Unable to read battery alarm\n");
772 static ssize_t
acpi_battery_write_alarm(struct file
*file
,
773 const char __user
* buffer
,
774 size_t count
, loff_t
* ppos
)
777 char alarm_string
[12] = { '\0' };
778 struct seq_file
*m
= file
->private_data
;
779 struct acpi_battery
*battery
= m
->private;
781 if (!battery
|| (count
> sizeof(alarm_string
) - 1))
783 if (!acpi_battery_present(battery
)) {
787 if (copy_from_user(alarm_string
, buffer
, count
)) {
791 alarm_string
[count
] = '\0';
792 battery
->alarm
= simple_strtol(alarm_string
, NULL
, 0);
793 result
= acpi_battery_set_alarm(battery
);
800 typedef int(*print_func
)(struct seq_file
*seq
, int result
);
802 static print_func acpi_print_funcs
[ACPI_BATTERY_NUMFILES
] = {
803 acpi_battery_print_info
,
804 acpi_battery_print_state
,
805 acpi_battery_print_alarm
,
808 static int acpi_battery_read(int fid
, struct seq_file
*seq
)
810 struct acpi_battery
*battery
= seq
->private;
811 int result
= acpi_battery_update(battery
);
812 return acpi_print_funcs
[fid
](seq
, result
);
815 #define DECLARE_FILE_FUNCTIONS(_name) \
816 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
818 return acpi_battery_read(_name##_tag, seq); \
820 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
822 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
825 DECLARE_FILE_FUNCTIONS(info
);
826 DECLARE_FILE_FUNCTIONS(state
);
827 DECLARE_FILE_FUNCTIONS(alarm
);
829 #undef DECLARE_FILE_FUNCTIONS
831 #define FILE_DESCRIPTION_RO(_name) \
833 .name = __stringify(_name), \
836 .open = acpi_battery_##_name##_open_fs, \
838 .llseek = seq_lseek, \
839 .release = single_release, \
840 .owner = THIS_MODULE, \
844 #define FILE_DESCRIPTION_RW(_name) \
846 .name = __stringify(_name), \
847 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
849 .open = acpi_battery_##_name##_open_fs, \
851 .llseek = seq_lseek, \
852 .write = acpi_battery_write_##_name, \
853 .release = single_release, \
854 .owner = THIS_MODULE, \
858 static struct battery_file
{
859 struct file_operations ops
;
862 } acpi_battery_file
[] = {
863 FILE_DESCRIPTION_RO(info
),
864 FILE_DESCRIPTION_RO(state
),
865 FILE_DESCRIPTION_RW(alarm
),
868 #undef FILE_DESCRIPTION_RO
869 #undef FILE_DESCRIPTION_RW
871 static int acpi_battery_add_fs(struct acpi_device
*device
)
873 struct proc_dir_entry
*entry
= NULL
;
876 if (!acpi_device_dir(device
)) {
877 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
879 if (!acpi_device_dir(device
))
883 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
) {
884 entry
= proc_create_data(acpi_battery_file
[i
].name
,
885 acpi_battery_file
[i
].mode
,
886 acpi_device_dir(device
),
887 &acpi_battery_file
[i
].ops
,
888 acpi_driver_data(device
));
895 static void acpi_battery_remove_fs(struct acpi_device
*device
)
898 if (!acpi_device_dir(device
))
900 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
)
901 remove_proc_entry(acpi_battery_file
[i
].name
,
902 acpi_device_dir(device
));
904 remove_proc_entry(acpi_device_bid(device
), acpi_battery_dir
);
905 acpi_device_dir(device
) = NULL
;
910 /* --------------------------------------------------------------------------
912 -------------------------------------------------------------------------- */
914 static void acpi_battery_notify(struct acpi_device
*device
, u32 event
)
916 struct acpi_battery
*battery
= acpi_driver_data(device
);
921 old
= battery
->bat
.dev
;
922 acpi_battery_update(battery
);
923 acpi_bus_generate_proc_event(device
, event
,
924 acpi_battery_present(battery
));
925 acpi_bus_generate_netlink_event(device
->pnp
.device_class
,
926 dev_name(&device
->dev
), event
,
927 acpi_battery_present(battery
));
928 /* acpi_battery_update could remove power_supply object */
929 if (old
&& battery
->bat
.dev
)
930 power_supply_changed(&battery
->bat
);
933 static int acpi_battery_add(struct acpi_device
*device
)
936 struct acpi_battery
*battery
= NULL
;
940 battery
= kzalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
943 battery
->device
= device
;
944 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
945 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
946 device
->driver_data
= battery
;
947 mutex_init(&battery
->lock
);
948 if (ACPI_SUCCESS(acpi_get_handle(battery
->device
->handle
,
950 set_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
);
951 acpi_battery_update(battery
);
952 #ifdef CONFIG_ACPI_PROCFS_POWER
953 result
= acpi_battery_add_fs(device
);
956 printk(KERN_INFO PREFIX
"%s Slot [%s] (battery %s)\n",
957 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
958 device
->status
.battery_present
? "present" : "absent");
960 #ifdef CONFIG_ACPI_PROCFS_POWER
961 acpi_battery_remove_fs(device
);
968 static int acpi_battery_remove(struct acpi_device
*device
, int type
)
970 struct acpi_battery
*battery
= NULL
;
972 if (!device
|| !acpi_driver_data(device
))
974 battery
= acpi_driver_data(device
);
975 #ifdef CONFIG_ACPI_PROCFS_POWER
976 acpi_battery_remove_fs(device
);
978 sysfs_remove_battery(battery
);
979 mutex_destroy(&battery
->lock
);
984 /* this is needed to learn about changes made in suspended state */
985 static int acpi_battery_resume(struct acpi_device
*device
)
987 struct acpi_battery
*battery
;
990 battery
= acpi_driver_data(device
);
991 battery
->update_time
= 0;
992 acpi_battery_update(battery
);
996 static struct acpi_driver acpi_battery_driver
= {
998 .class = ACPI_BATTERY_CLASS
,
999 .ids
= battery_device_ids
,
1000 .flags
= ACPI_DRIVER_ALL_NOTIFY_EVENTS
,
1002 .add
= acpi_battery_add
,
1003 .resume
= acpi_battery_resume
,
1004 .remove
= acpi_battery_remove
,
1005 .notify
= acpi_battery_notify
,
1009 static void __init
acpi_battery_init_async(void *unused
, async_cookie_t cookie
)
1013 #ifdef CONFIG_ACPI_PROCFS_POWER
1014 acpi_battery_dir
= acpi_lock_battery_dir();
1015 if (!acpi_battery_dir
)
1018 if (acpi_bus_register_driver(&acpi_battery_driver
) < 0) {
1019 #ifdef CONFIG_ACPI_PROCFS_POWER
1020 acpi_unlock_battery_dir(acpi_battery_dir
);
1027 static int __init
acpi_battery_init(void)
1029 async_schedule(acpi_battery_init_async
, NULL
);
1033 static void __exit
acpi_battery_exit(void)
1035 acpi_bus_unregister_driver(&acpi_battery_driver
);
1036 #ifdef CONFIG_ACPI_PROCFS_POWER
1037 acpi_unlock_battery_dir(acpi_battery_dir
);
1041 module_init(acpi_battery_init
);
1042 module_exit(acpi_battery_exit
);