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_MODEL_NAME
:
254 val
->strval
= battery
->model_number
;
256 case POWER_SUPPLY_PROP_MANUFACTURER
:
257 val
->strval
= battery
->oem_info
;
259 case POWER_SUPPLY_PROP_SERIAL_NUMBER
:
260 val
->strval
= battery
->serial_number
;
268 static enum power_supply_property charge_battery_props
[] = {
269 POWER_SUPPLY_PROP_STATUS
,
270 POWER_SUPPLY_PROP_PRESENT
,
271 POWER_SUPPLY_PROP_TECHNOLOGY
,
272 POWER_SUPPLY_PROP_CYCLE_COUNT
,
273 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
274 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
275 POWER_SUPPLY_PROP_CURRENT_NOW
,
276 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN
,
277 POWER_SUPPLY_PROP_CHARGE_FULL
,
278 POWER_SUPPLY_PROP_CHARGE_NOW
,
279 POWER_SUPPLY_PROP_MODEL_NAME
,
280 POWER_SUPPLY_PROP_MANUFACTURER
,
281 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
284 static enum power_supply_property energy_battery_props
[] = {
285 POWER_SUPPLY_PROP_STATUS
,
286 POWER_SUPPLY_PROP_PRESENT
,
287 POWER_SUPPLY_PROP_TECHNOLOGY
,
288 POWER_SUPPLY_PROP_CYCLE_COUNT
,
289 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN
,
290 POWER_SUPPLY_PROP_VOLTAGE_NOW
,
291 POWER_SUPPLY_PROP_POWER_NOW
,
292 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN
,
293 POWER_SUPPLY_PROP_ENERGY_FULL
,
294 POWER_SUPPLY_PROP_ENERGY_NOW
,
295 POWER_SUPPLY_PROP_MODEL_NAME
,
296 POWER_SUPPLY_PROP_MANUFACTURER
,
297 POWER_SUPPLY_PROP_SERIAL_NUMBER
,
300 #ifdef CONFIG_ACPI_PROCFS_POWER
301 inline char *acpi_battery_units(struct acpi_battery
*battery
)
303 return (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
) ?
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 /* For buggy DSDTs that report negative 16-bit values for either
465 * charging or discharging current and/or report 0 as 65536
468 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
&&
469 battery
->rate_now
!= ACPI_BATTERY_VALUE_UNKNOWN
&&
470 (s16
)(battery
->rate_now
) < 0) {
471 battery
->rate_now
= abs((s16
)battery
->rate_now
);
472 printk_once(KERN_WARNING FW_BUG
"battery: (dis)charge rate"
476 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
)
477 && battery
->capacity_now
>= 0 && battery
->capacity_now
<= 100)
478 battery
->capacity_now
= (battery
->capacity_now
*
479 battery
->full_charge_capacity
) / 100;
483 static int acpi_battery_set_alarm(struct acpi_battery
*battery
)
485 acpi_status status
= 0;
486 union acpi_object arg0
= { .type
= ACPI_TYPE_INTEGER
};
487 struct acpi_object_list arg_list
= { 1, &arg0
};
489 if (!acpi_battery_present(battery
) ||
490 !test_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
))
493 arg0
.integer
.value
= battery
->alarm
;
495 mutex_lock(&battery
->lock
);
496 status
= acpi_evaluate_object(battery
->device
->handle
, "_BTP",
498 mutex_unlock(&battery
->lock
);
500 if (ACPI_FAILURE(status
))
503 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", battery
->alarm
));
507 static int acpi_battery_init_alarm(struct acpi_battery
*battery
)
509 acpi_status status
= AE_OK
;
510 acpi_handle handle
= NULL
;
512 /* See if alarms are supported, and if so, set default */
513 status
= acpi_get_handle(battery
->device
->handle
, "_BTP", &handle
);
514 if (ACPI_FAILURE(status
)) {
515 clear_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
518 set_bit(ACPI_BATTERY_ALARM_PRESENT
, &battery
->flags
);
520 battery
->alarm
= battery
->design_capacity_warning
;
521 return acpi_battery_set_alarm(battery
);
524 static ssize_t
acpi_battery_alarm_show(struct device
*dev
,
525 struct device_attribute
*attr
,
528 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
529 return sprintf(buf
, "%d\n", battery
->alarm
* 1000);
532 static ssize_t
acpi_battery_alarm_store(struct device
*dev
,
533 struct device_attribute
*attr
,
534 const char *buf
, size_t count
)
537 struct acpi_battery
*battery
= to_acpi_battery(dev_get_drvdata(dev
));
538 if (sscanf(buf
, "%ld\n", &x
) == 1)
539 battery
->alarm
= x
/1000;
540 if (acpi_battery_present(battery
))
541 acpi_battery_set_alarm(battery
);
545 static struct device_attribute alarm_attr
= {
546 .attr
= {.name
= "alarm", .mode
= 0644},
547 .show
= acpi_battery_alarm_show
,
548 .store
= acpi_battery_alarm_store
,
551 static int sysfs_add_battery(struct acpi_battery
*battery
)
555 if (battery
->power_unit
== ACPI_BATTERY_POWER_UNIT_MA
) {
556 battery
->bat
.properties
= charge_battery_props
;
557 battery
->bat
.num_properties
=
558 ARRAY_SIZE(charge_battery_props
);
560 battery
->bat
.properties
= energy_battery_props
;
561 battery
->bat
.num_properties
=
562 ARRAY_SIZE(energy_battery_props
);
565 battery
->bat
.name
= acpi_device_bid(battery
->device
);
566 battery
->bat
.type
= POWER_SUPPLY_TYPE_BATTERY
;
567 battery
->bat
.get_property
= acpi_battery_get_property
;
569 result
= power_supply_register(&battery
->device
->dev
, &battery
->bat
);
572 return device_create_file(battery
->bat
.dev
, &alarm_attr
);
575 static void sysfs_remove_battery(struct acpi_battery
*battery
)
577 mutex_lock(&battery
->sysfs_lock
);
578 if (!battery
->bat
.dev
) {
579 mutex_unlock(&battery
->sysfs_lock
);
583 device_remove_file(battery
->bat
.dev
, &alarm_attr
);
584 power_supply_unregister(&battery
->bat
);
585 battery
->bat
.dev
= NULL
;
586 mutex_unlock(&battery
->sysfs_lock
);
590 * According to the ACPI spec, some kinds of primary batteries can
591 * report percentage battery remaining capacity directly to OS.
592 * In this case, it reports the Last Full Charged Capacity == 100
593 * and BatteryPresentRate == 0xFFFFFFFF.
595 * Now we found some battery reports percentage remaining capacity
596 * even if it's rechargeable.
597 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
599 * Handle this correctly so that they won't break userspace.
601 static void acpi_battery_quirks(struct acpi_battery
*battery
)
603 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
))
606 if (battery
->full_charge_capacity
== 100 &&
607 battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
&&
608 battery
->capacity_now
>=0 && battery
->capacity_now
<= 100) {
609 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY
, &battery
->flags
);
610 battery
->full_charge_capacity
= battery
->design_capacity
;
611 battery
->capacity_now
= (battery
->capacity_now
*
612 battery
->full_charge_capacity
) / 100;
616 static int acpi_battery_update(struct acpi_battery
*battery
)
618 int result
, old_present
= acpi_battery_present(battery
);
619 result
= acpi_battery_get_status(battery
);
622 if (!acpi_battery_present(battery
)) {
623 sysfs_remove_battery(battery
);
624 battery
->update_time
= 0;
627 if (!battery
->update_time
||
628 old_present
!= acpi_battery_present(battery
)) {
629 result
= acpi_battery_get_info(battery
);
632 acpi_battery_init_alarm(battery
);
634 if (!battery
->bat
.dev
) {
635 result
= sysfs_add_battery(battery
);
639 result
= acpi_battery_get_state(battery
);
640 acpi_battery_quirks(battery
);
644 static void acpi_battery_refresh(struct acpi_battery
*battery
)
648 if (!battery
->bat
.dev
)
651 power_unit
= battery
->power_unit
;
653 acpi_battery_get_info(battery
);
655 if (power_unit
== battery
->power_unit
)
658 /* The battery has changed its reporting units. */
659 sysfs_remove_battery(battery
);
660 sysfs_add_battery(battery
);
663 /* --------------------------------------------------------------------------
665 -------------------------------------------------------------------------- */
667 #ifdef CONFIG_ACPI_PROCFS_POWER
668 static struct proc_dir_entry
*acpi_battery_dir
;
670 static int acpi_battery_print_info(struct seq_file
*seq
, int result
)
672 struct acpi_battery
*battery
= seq
->private;
677 seq_printf(seq
, "present: %s\n",
678 acpi_battery_present(battery
)?"yes":"no");
679 if (!acpi_battery_present(battery
))
681 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
682 seq_printf(seq
, "design capacity: unknown\n");
684 seq_printf(seq
, "design capacity: %d %sh\n",
685 battery
->design_capacity
,
686 acpi_battery_units(battery
));
688 if (battery
->full_charge_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
689 seq_printf(seq
, "last full capacity: unknown\n");
691 seq_printf(seq
, "last full capacity: %d %sh\n",
692 battery
->full_charge_capacity
,
693 acpi_battery_units(battery
));
695 seq_printf(seq
, "battery technology: %srechargeable\n",
696 (!battery
->technology
)?"non-":"");
698 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
699 seq_printf(seq
, "design voltage: unknown\n");
701 seq_printf(seq
, "design voltage: %d mV\n",
702 battery
->design_voltage
);
703 seq_printf(seq
, "design capacity warning: %d %sh\n",
704 battery
->design_capacity_warning
,
705 acpi_battery_units(battery
));
706 seq_printf(seq
, "design capacity low: %d %sh\n",
707 battery
->design_capacity_low
,
708 acpi_battery_units(battery
));
709 seq_printf(seq
, "cycle count: %i\n", battery
->cycle_count
);
710 seq_printf(seq
, "capacity granularity 1: %d %sh\n",
711 battery
->capacity_granularity_1
,
712 acpi_battery_units(battery
));
713 seq_printf(seq
, "capacity granularity 2: %d %sh\n",
714 battery
->capacity_granularity_2
,
715 acpi_battery_units(battery
));
716 seq_printf(seq
, "model number: %s\n", battery
->model_number
);
717 seq_printf(seq
, "serial number: %s\n", battery
->serial_number
);
718 seq_printf(seq
, "battery type: %s\n", battery
->type
);
719 seq_printf(seq
, "OEM info: %s\n", battery
->oem_info
);
722 seq_printf(seq
, "ERROR: Unable to read battery info\n");
726 static int acpi_battery_print_state(struct seq_file
*seq
, int result
)
728 struct acpi_battery
*battery
= seq
->private;
733 seq_printf(seq
, "present: %s\n",
734 acpi_battery_present(battery
)?"yes":"no");
735 if (!acpi_battery_present(battery
))
738 seq_printf(seq
, "capacity state: %s\n",
739 (battery
->state
& 0x04)?"critical":"ok");
740 if ((battery
->state
& 0x01) && (battery
->state
& 0x02))
742 "charging state: charging/discharging\n");
743 else if (battery
->state
& 0x01)
744 seq_printf(seq
, "charging state: discharging\n");
745 else if (battery
->state
& 0x02)
746 seq_printf(seq
, "charging state: charging\n");
748 seq_printf(seq
, "charging state: charged\n");
750 if (battery
->rate_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
751 seq_printf(seq
, "present rate: unknown\n");
753 seq_printf(seq
, "present rate: %d %s\n",
754 battery
->rate_now
, acpi_battery_units(battery
));
756 if (battery
->capacity_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
757 seq_printf(seq
, "remaining capacity: unknown\n");
759 seq_printf(seq
, "remaining capacity: %d %sh\n",
760 battery
->capacity_now
, acpi_battery_units(battery
));
761 if (battery
->voltage_now
== ACPI_BATTERY_VALUE_UNKNOWN
)
762 seq_printf(seq
, "present voltage: unknown\n");
764 seq_printf(seq
, "present voltage: %d mV\n",
765 battery
->voltage_now
);
768 seq_printf(seq
, "ERROR: Unable to read battery state\n");
773 static int acpi_battery_print_alarm(struct seq_file
*seq
, int result
)
775 struct acpi_battery
*battery
= seq
->private;
780 if (!acpi_battery_present(battery
)) {
781 seq_printf(seq
, "present: no\n");
784 seq_printf(seq
, "alarm: ");
786 seq_printf(seq
, "unsupported\n");
788 seq_printf(seq
, "%u %sh\n", battery
->alarm
,
789 acpi_battery_units(battery
));
792 seq_printf(seq
, "ERROR: Unable to read battery alarm\n");
796 static ssize_t
acpi_battery_write_alarm(struct file
*file
,
797 const char __user
* buffer
,
798 size_t count
, loff_t
* ppos
)
801 char alarm_string
[12] = { '\0' };
802 struct seq_file
*m
= file
->private_data
;
803 struct acpi_battery
*battery
= m
->private;
805 if (!battery
|| (count
> sizeof(alarm_string
) - 1))
807 if (!acpi_battery_present(battery
)) {
811 if (copy_from_user(alarm_string
, buffer
, count
)) {
815 alarm_string
[count
] = '\0';
816 battery
->alarm
= simple_strtol(alarm_string
, NULL
, 0);
817 result
= acpi_battery_set_alarm(battery
);
824 typedef int(*print_func
)(struct seq_file
*seq
, int result
);
826 static print_func acpi_print_funcs
[ACPI_BATTERY_NUMFILES
] = {
827 acpi_battery_print_info
,
828 acpi_battery_print_state
,
829 acpi_battery_print_alarm
,
832 static int acpi_battery_read(int fid
, struct seq_file
*seq
)
834 struct acpi_battery
*battery
= seq
->private;
835 int result
= acpi_battery_update(battery
);
836 return acpi_print_funcs
[fid
](seq
, result
);
839 #define DECLARE_FILE_FUNCTIONS(_name) \
840 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
842 return acpi_battery_read(_name##_tag, seq); \
844 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
846 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
849 DECLARE_FILE_FUNCTIONS(info
);
850 DECLARE_FILE_FUNCTIONS(state
);
851 DECLARE_FILE_FUNCTIONS(alarm
);
853 #undef DECLARE_FILE_FUNCTIONS
855 #define FILE_DESCRIPTION_RO(_name) \
857 .name = __stringify(_name), \
860 .open = acpi_battery_##_name##_open_fs, \
862 .llseek = seq_lseek, \
863 .release = single_release, \
864 .owner = THIS_MODULE, \
868 #define FILE_DESCRIPTION_RW(_name) \
870 .name = __stringify(_name), \
871 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
873 .open = acpi_battery_##_name##_open_fs, \
875 .llseek = seq_lseek, \
876 .write = acpi_battery_write_##_name, \
877 .release = single_release, \
878 .owner = THIS_MODULE, \
882 static const struct battery_file
{
883 struct file_operations ops
;
886 } acpi_battery_file
[] = {
887 FILE_DESCRIPTION_RO(info
),
888 FILE_DESCRIPTION_RO(state
),
889 FILE_DESCRIPTION_RW(alarm
),
892 #undef FILE_DESCRIPTION_RO
893 #undef FILE_DESCRIPTION_RW
895 static int acpi_battery_add_fs(struct acpi_device
*device
)
897 struct proc_dir_entry
*entry
= NULL
;
900 printk(KERN_WARNING PREFIX
"Deprecated procfs I/F for battery is loaded,"
901 " please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
902 if (!acpi_device_dir(device
)) {
903 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
905 if (!acpi_device_dir(device
))
909 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
) {
910 entry
= proc_create_data(acpi_battery_file
[i
].name
,
911 acpi_battery_file
[i
].mode
,
912 acpi_device_dir(device
),
913 &acpi_battery_file
[i
].ops
,
914 acpi_driver_data(device
));
921 static void acpi_battery_remove_fs(struct acpi_device
*device
)
924 if (!acpi_device_dir(device
))
926 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
)
927 remove_proc_entry(acpi_battery_file
[i
].name
,
928 acpi_device_dir(device
));
930 remove_proc_entry(acpi_device_bid(device
), acpi_battery_dir
);
931 acpi_device_dir(device
) = NULL
;
936 /* --------------------------------------------------------------------------
938 -------------------------------------------------------------------------- */
940 static void acpi_battery_notify(struct acpi_device
*device
, u32 event
)
942 struct acpi_battery
*battery
= acpi_driver_data(device
);
947 old
= battery
->bat
.dev
;
948 if (event
== ACPI_BATTERY_NOTIFY_INFO
)
949 acpi_battery_refresh(battery
);
950 acpi_battery_update(battery
);
951 acpi_bus_generate_proc_event(device
, event
,
952 acpi_battery_present(battery
));
953 acpi_bus_generate_netlink_event(device
->pnp
.device_class
,
954 dev_name(&device
->dev
), event
,
955 acpi_battery_present(battery
));
956 /* acpi_battery_update could remove power_supply object */
957 if (old
&& battery
->bat
.dev
)
958 power_supply_changed(&battery
->bat
);
961 static int battery_notify(struct notifier_block
*nb
,
962 unsigned long mode
, void *_unused
)
964 struct acpi_battery
*battery
= container_of(nb
, struct acpi_battery
,
967 case PM_POST_HIBERNATION
:
968 case PM_POST_SUSPEND
:
969 if (battery
->bat
.dev
) {
970 sysfs_remove_battery(battery
);
971 sysfs_add_battery(battery
);
979 static int acpi_battery_add(struct acpi_device
*device
)
982 struct acpi_battery
*battery
= NULL
;
986 battery
= kzalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
989 battery
->device
= device
;
990 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
991 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
992 device
->driver_data
= battery
;
993 mutex_init(&battery
->lock
);
994 mutex_init(&battery
->sysfs_lock
);
995 if (ACPI_SUCCESS(acpi_get_handle(battery
->device
->handle
,
997 set_bit(ACPI_BATTERY_XINFO_PRESENT
, &battery
->flags
);
998 result
= acpi_battery_update(battery
);
1001 #ifdef CONFIG_ACPI_PROCFS_POWER
1002 result
= acpi_battery_add_fs(device
);
1005 #ifdef CONFIG_ACPI_PROCFS_POWER
1006 acpi_battery_remove_fs(device
);
1011 printk(KERN_INFO PREFIX
"%s Slot [%s] (battery %s)\n",
1012 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
1013 device
->status
.battery_present
? "present" : "absent");
1015 battery
->pm_nb
.notifier_call
= battery_notify
;
1016 register_pm_notifier(&battery
->pm_nb
);
1021 sysfs_remove_battery(battery
);
1022 mutex_destroy(&battery
->lock
);
1023 mutex_destroy(&battery
->sysfs_lock
);
1028 static int acpi_battery_remove(struct acpi_device
*device
, int type
)
1030 struct acpi_battery
*battery
= NULL
;
1032 if (!device
|| !acpi_driver_data(device
))
1034 battery
= acpi_driver_data(device
);
1035 unregister_pm_notifier(&battery
->pm_nb
);
1036 #ifdef CONFIG_ACPI_PROCFS_POWER
1037 acpi_battery_remove_fs(device
);
1039 sysfs_remove_battery(battery
);
1040 mutex_destroy(&battery
->lock
);
1041 mutex_destroy(&battery
->sysfs_lock
);
1046 /* this is needed to learn about changes made in suspended state */
1047 static int acpi_battery_resume(struct acpi_device
*device
)
1049 struct acpi_battery
*battery
;
1052 battery
= acpi_driver_data(device
);
1053 battery
->update_time
= 0;
1054 acpi_battery_update(battery
);
1058 static struct acpi_driver acpi_battery_driver
= {
1060 .class = ACPI_BATTERY_CLASS
,
1061 .ids
= battery_device_ids
,
1062 .flags
= ACPI_DRIVER_ALL_NOTIFY_EVENTS
,
1064 .add
= acpi_battery_add
,
1065 .resume
= acpi_battery_resume
,
1066 .remove
= acpi_battery_remove
,
1067 .notify
= acpi_battery_notify
,
1071 static void __init
acpi_battery_init_async(void *unused
, async_cookie_t cookie
)
1075 #ifdef CONFIG_ACPI_PROCFS_POWER
1076 acpi_battery_dir
= acpi_lock_battery_dir();
1077 if (!acpi_battery_dir
)
1080 if (acpi_bus_register_driver(&acpi_battery_driver
) < 0) {
1081 #ifdef CONFIG_ACPI_PROCFS_POWER
1082 acpi_unlock_battery_dir(acpi_battery_dir
);
1089 static int __init
acpi_battery_init(void)
1091 async_schedule(acpi_battery_init_async
, NULL
);
1095 static void __exit
acpi_battery_exit(void)
1097 acpi_bus_unregister_driver(&acpi_battery_driver
);
1098 #ifdef CONFIG_ACPI_PROCFS_POWER
1099 acpi_unlock_battery_dir(acpi_battery_dir
);
1103 module_init(acpi_battery_init
);
1104 module_exit(acpi_battery_exit
);