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/proc_fs.h>
34 #include <linux/seq_file.h>
35 #include <asm/uaccess.h>
37 #include <acpi/acpi_bus.h>
38 #include <acpi/acpi_drivers.h>
40 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
42 #define ACPI_BATTERY_COMPONENT 0x00040000
43 #define ACPI_BATTERY_CLASS "battery"
44 #define ACPI_BATTERY_DEVICE_NAME "Battery"
45 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
46 #define ACPI_BATTERY_NOTIFY_INFO 0x81
48 #define _COMPONENT ACPI_BATTERY_COMPONENT
50 ACPI_MODULE_NAME("battery");
52 MODULE_AUTHOR("Paul Diefenbaugh");
53 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
54 MODULE_DESCRIPTION("ACPI Battery Driver");
55 MODULE_LICENSE("GPL");
57 static unsigned int cache_time
= 1000;
58 module_param(cache_time
, uint
, 0644);
59 MODULE_PARM_DESC(cache_time
, "cache time in milliseconds");
61 extern struct proc_dir_entry
*acpi_lock_battery_dir(void);
62 extern void *acpi_unlock_battery_dir(struct proc_dir_entry
*acpi_battery_dir
);
64 static const struct acpi_device_id battery_device_ids
[] = {
69 MODULE_DEVICE_TABLE(acpi
, battery_device_ids
);
71 enum acpi_battery_files
{
75 ACPI_BATTERY_NUMFILES
,
80 struct acpi_device
*device
;
81 unsigned long update_time
;
83 int remaining_capacity
;
86 int last_full_capacity
;
89 int design_capacity_warning
;
90 int design_capacity_low
;
91 int capacity_granularity_1
;
92 int capacity_granularity_2
;
94 char model_number
[32];
95 char serial_number
[32];
103 inline int acpi_battery_present(struct acpi_battery
*battery
)
105 return battery
->device
->status
.battery_present
;
108 inline char *acpi_battery_units(struct acpi_battery
*battery
)
110 return (battery
->power_unit
)?"mA":"mW";
113 /* --------------------------------------------------------------------------
115 -------------------------------------------------------------------------- */
116 struct acpi_offsets
{
117 size_t offset
; /* offset inside struct acpi_sbs_battery */
118 u8 mode
; /* int or string? */
121 static struct acpi_offsets state_offsets
[] = {
122 {offsetof(struct acpi_battery
, state
), 0},
123 {offsetof(struct acpi_battery
, present_rate
), 0},
124 {offsetof(struct acpi_battery
, remaining_capacity
), 0},
125 {offsetof(struct acpi_battery
, present_voltage
), 0},
128 static struct acpi_offsets info_offsets
[] = {
129 {offsetof(struct acpi_battery
, power_unit
), 0},
130 {offsetof(struct acpi_battery
, design_capacity
), 0},
131 {offsetof(struct acpi_battery
, last_full_capacity
), 0},
132 {offsetof(struct acpi_battery
, technology
), 0},
133 {offsetof(struct acpi_battery
, design_voltage
), 0},
134 {offsetof(struct acpi_battery
, design_capacity_warning
), 0},
135 {offsetof(struct acpi_battery
, design_capacity_low
), 0},
136 {offsetof(struct acpi_battery
, capacity_granularity_1
), 0},
137 {offsetof(struct acpi_battery
, capacity_granularity_2
), 0},
138 {offsetof(struct acpi_battery
, model_number
), 1},
139 {offsetof(struct acpi_battery
, serial_number
), 1},
140 {offsetof(struct acpi_battery
, type
), 1},
141 {offsetof(struct acpi_battery
, oem_info
), 1},
144 static int extract_package(struct acpi_battery
*battery
,
145 union acpi_object
*package
,
146 struct acpi_offsets
*offsets
, int num
)
149 union acpi_object
*element
;
150 if (package
->type
!= ACPI_TYPE_PACKAGE
)
152 for (i
= 0; i
< num
; ++i
) {
153 if (package
->package
.count
<= i
)
155 element
= &package
->package
.elements
[i
];
156 if (offsets
[i
].mode
) {
157 if (element
->type
!= ACPI_TYPE_STRING
&&
158 element
->type
!= ACPI_TYPE_BUFFER
)
160 strncpy((u8
*)battery
+ offsets
[i
].offset
,
161 element
->string
.pointer
, 32);
163 if (element
->type
!= ACPI_TYPE_INTEGER
)
165 x
= (int *)((u8
*)battery
+ offsets
[i
].offset
);
166 *x
= element
->integer
.value
;
172 static int acpi_battery_get_status(struct acpi_battery
*battery
)
174 if (acpi_bus_get_status(battery
->device
)) {
175 ACPI_EXCEPTION((AE_INFO
, AE_ERROR
, "Evaluating _STA"));
181 static int acpi_battery_get_info(struct acpi_battery
*battery
)
183 int result
= -EFAULT
;
184 acpi_status status
= 0;
185 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
187 if (!acpi_battery_present(battery
))
189 mutex_lock(&battery
->lock
);
190 status
= acpi_evaluate_object(battery
->device
->handle
, "_BIF",
192 mutex_unlock(&battery
->lock
);
194 if (ACPI_FAILURE(status
)) {
195 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BIF"));
199 result
= extract_package(battery
, buffer
.pointer
,
200 info_offsets
, ARRAY_SIZE(info_offsets
));
201 kfree(buffer
.pointer
);
205 static int acpi_battery_get_state(struct acpi_battery
*battery
)
208 acpi_status status
= 0;
209 struct acpi_buffer buffer
= { ACPI_ALLOCATE_BUFFER
, NULL
};
211 if (!acpi_battery_present(battery
))
214 if (battery
->update_time
&&
215 time_before(jiffies
, battery
->update_time
+
216 msecs_to_jiffies(cache_time
)))
219 mutex_lock(&battery
->lock
);
220 status
= acpi_evaluate_object(battery
->device
->handle
, "_BST",
222 mutex_unlock(&battery
->lock
);
224 if (ACPI_FAILURE(status
)) {
225 ACPI_EXCEPTION((AE_INFO
, status
, "Evaluating _BST"));
229 result
= extract_package(battery
, buffer
.pointer
,
230 state_offsets
, ARRAY_SIZE(state_offsets
));
231 battery
->update_time
= jiffies
;
232 kfree(buffer
.pointer
);
236 static int acpi_battery_set_alarm(struct acpi_battery
*battery
)
238 acpi_status status
= 0;
239 union acpi_object arg0
= { .type
= ACPI_TYPE_INTEGER
};
240 struct acpi_object_list arg_list
= { 1, &arg0
};
242 if (!acpi_battery_present(battery
)|| !battery
->alarm_present
)
245 arg0
.integer
.value
= battery
->alarm
;
247 mutex_lock(&battery
->lock
);
248 status
= acpi_evaluate_object(battery
->device
->handle
, "_BTP",
250 mutex_unlock(&battery
->lock
);
252 if (ACPI_FAILURE(status
))
255 ACPI_DEBUG_PRINT((ACPI_DB_INFO
, "Alarm set to %d\n", battery
->alarm
));
259 static int acpi_battery_init_alarm(struct acpi_battery
*battery
)
261 acpi_status status
= AE_OK
;
262 acpi_handle handle
= NULL
;
264 /* See if alarms are supported, and if so, set default */
265 status
= acpi_get_handle(battery
->device
->handle
, "_BTP", &handle
);
266 if (ACPI_FAILURE(status
)) {
267 battery
->alarm_present
= 0;
270 battery
->alarm_present
= 1;
272 battery
->alarm
= battery
->design_capacity_warning
;
273 return acpi_battery_set_alarm(battery
);
276 static int acpi_battery_update(struct acpi_battery
*battery
)
278 int saved_present
= acpi_battery_present(battery
);
279 int result
= acpi_battery_get_status(battery
);
280 if (result
|| !acpi_battery_present(battery
))
282 if (saved_present
!= acpi_battery_present(battery
) ||
283 !battery
->update_time
) {
284 battery
->update_time
= 0;
285 result
= acpi_battery_get_info(battery
);
288 acpi_battery_init_alarm(battery
);
290 return acpi_battery_get_state(battery
);
293 /* --------------------------------------------------------------------------
295 -------------------------------------------------------------------------- */
297 static struct proc_dir_entry
*acpi_battery_dir
;
299 static int acpi_battery_print_info(struct seq_file
*seq
, int result
)
301 struct acpi_battery
*battery
= seq
->private;
306 seq_printf(seq
, "present: %s\n",
307 acpi_battery_present(battery
)?"yes":"no");
308 if (!acpi_battery_present(battery
))
310 if (battery
->design_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
311 seq_printf(seq
, "design capacity: unknown\n");
313 seq_printf(seq
, "design capacity: %d %sh\n",
314 battery
->design_capacity
,
315 acpi_battery_units(battery
));
317 if (battery
->last_full_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
318 seq_printf(seq
, "last full capacity: unknown\n");
320 seq_printf(seq
, "last full capacity: %d %sh\n",
321 battery
->last_full_capacity
,
322 acpi_battery_units(battery
));
324 seq_printf(seq
, "battery technology: %srechargeable\n",
325 (!battery
->technology
)?"non-":"");
327 if (battery
->design_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
328 seq_printf(seq
, "design voltage: unknown\n");
330 seq_printf(seq
, "design voltage: %d mV\n",
331 battery
->design_voltage
);
332 seq_printf(seq
, "design capacity warning: %d %sh\n",
333 battery
->design_capacity_warning
,
334 acpi_battery_units(battery
));
335 seq_printf(seq
, "design capacity low: %d %sh\n",
336 battery
->design_capacity_low
,
337 acpi_battery_units(battery
));
338 seq_printf(seq
, "capacity granularity 1: %d %sh\n",
339 battery
->capacity_granularity_1
,
340 acpi_battery_units(battery
));
341 seq_printf(seq
, "capacity granularity 2: %d %sh\n",
342 battery
->capacity_granularity_2
,
343 acpi_battery_units(battery
));
344 seq_printf(seq
, "model number: %s\n", battery
->model_number
);
345 seq_printf(seq
, "serial number: %s\n", battery
->serial_number
);
346 seq_printf(seq
, "battery type: %s\n", battery
->type
);
347 seq_printf(seq
, "OEM info: %s\n", battery
->oem_info
);
350 seq_printf(seq
, "ERROR: Unable to read battery info\n");
354 static int acpi_battery_print_state(struct seq_file
*seq
, int result
)
356 struct acpi_battery
*battery
= seq
->private;
361 seq_printf(seq
, "present: %s\n",
362 acpi_battery_present(battery
)?"yes":"no");
363 if (!acpi_battery_present(battery
))
366 seq_printf(seq
, "capacity state: %s\n",
367 (battery
->state
& 0x04)?"critical":"ok");
368 if ((battery
->state
& 0x01) && (battery
->state
& 0x02))
370 "charging state: charging/discharging\n");
371 else if (battery
->state
& 0x01)
372 seq_printf(seq
, "charging state: discharging\n");
373 else if (battery
->state
& 0x02)
374 seq_printf(seq
, "charging state: charging\n");
376 seq_printf(seq
, "charging state: charged\n");
378 if (battery
->present_rate
== ACPI_BATTERY_VALUE_UNKNOWN
)
379 seq_printf(seq
, "present rate: unknown\n");
381 seq_printf(seq
, "present rate: %d %s\n",
382 battery
->present_rate
, acpi_battery_units(battery
));
384 if (battery
->remaining_capacity
== ACPI_BATTERY_VALUE_UNKNOWN
)
385 seq_printf(seq
, "remaining capacity: unknown\n");
387 seq_printf(seq
, "remaining capacity: %d %sh\n",
388 battery
->remaining_capacity
, acpi_battery_units(battery
));
389 if (battery
->present_voltage
== ACPI_BATTERY_VALUE_UNKNOWN
)
390 seq_printf(seq
, "present voltage: unknown\n");
392 seq_printf(seq
, "present voltage: %d mV\n",
393 battery
->present_voltage
);
396 seq_printf(seq
, "ERROR: Unable to read battery state\n");
401 static int acpi_battery_print_alarm(struct seq_file
*seq
, int result
)
403 struct acpi_battery
*battery
= seq
->private;
408 if (!acpi_battery_present(battery
)) {
409 seq_printf(seq
, "present: no\n");
412 seq_printf(seq
, "alarm: ");
414 seq_printf(seq
, "unsupported\n");
416 seq_printf(seq
, "%u %sh\n", battery
->alarm
,
417 acpi_battery_units(battery
));
420 seq_printf(seq
, "ERROR: Unable to read battery alarm\n");
424 static ssize_t
acpi_battery_write_alarm(struct file
*file
,
425 const char __user
* buffer
,
426 size_t count
, loff_t
* ppos
)
429 char alarm_string
[12] = { '\0' };
430 struct seq_file
*m
= file
->private_data
;
431 struct acpi_battery
*battery
= m
->private;
433 if (!battery
|| (count
> sizeof(alarm_string
) - 1))
439 if (!acpi_battery_present(battery
)) {
443 if (copy_from_user(alarm_string
, buffer
, count
)) {
447 alarm_string
[count
] = '\0';
448 battery
->alarm
= simple_strtol(alarm_string
, NULL
, 0);
449 result
= acpi_battery_set_alarm(battery
);
456 typedef int(*print_func
)(struct seq_file
*seq
, int result
);
458 static print_func acpi_print_funcs
[ACPI_BATTERY_NUMFILES
] = {
459 acpi_battery_print_info
,
460 acpi_battery_print_state
,
461 acpi_battery_print_alarm
,
464 static int acpi_battery_read(int fid
, struct seq_file
*seq
)
466 struct acpi_battery
*battery
= seq
->private;
467 int result
= acpi_battery_update(battery
);
468 return acpi_print_funcs
[fid
](seq
, result
);
471 #define DECLARE_FILE_FUNCTIONS(_name) \
472 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
474 return acpi_battery_read(_name##_tag, seq); \
476 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
478 return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
481 DECLARE_FILE_FUNCTIONS(info
);
482 DECLARE_FILE_FUNCTIONS(state
);
483 DECLARE_FILE_FUNCTIONS(alarm
);
485 #undef DECLARE_FILE_FUNCTIONS
487 #define FILE_DESCRIPTION_RO(_name) \
489 .name = __stringify(_name), \
492 .open = acpi_battery_##_name##_open_fs, \
494 .llseek = seq_lseek, \
495 .release = single_release, \
496 .owner = THIS_MODULE, \
500 #define FILE_DESCRIPTION_RW(_name) \
502 .name = __stringify(_name), \
503 .mode = S_IFREG | S_IRUGO | S_IWUSR, \
505 .open = acpi_battery_##_name##_open_fs, \
507 .llseek = seq_lseek, \
508 .write = acpi_battery_write_##_name, \
509 .release = single_release, \
510 .owner = THIS_MODULE, \
514 static struct battery_file
{
515 struct file_operations ops
;
518 } acpi_battery_file
[] = {
519 FILE_DESCRIPTION_RO(info
),
520 FILE_DESCRIPTION_RO(state
),
521 FILE_DESCRIPTION_RW(alarm
),
524 #undef FILE_DESCRIPTION_RO
525 #undef FILE_DESCRIPTION_RW
527 static int acpi_battery_add_fs(struct acpi_device
*device
)
529 struct proc_dir_entry
*entry
= NULL
;
532 if (!acpi_device_dir(device
)) {
533 acpi_device_dir(device
) = proc_mkdir(acpi_device_bid(device
),
535 if (!acpi_device_dir(device
))
537 acpi_device_dir(device
)->owner
= THIS_MODULE
;
540 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
) {
541 entry
= create_proc_entry(acpi_battery_file
[i
].name
,
542 acpi_battery_file
[i
].mode
, acpi_device_dir(device
));
546 entry
->proc_fops
= &acpi_battery_file
[i
].ops
;
547 entry
->data
= acpi_driver_data(device
);
548 entry
->owner
= THIS_MODULE
;
554 static void acpi_battery_remove_fs(struct acpi_device
*device
)
557 if (!acpi_device_dir(device
))
559 for (i
= 0; i
< ACPI_BATTERY_NUMFILES
; ++i
)
560 remove_proc_entry(acpi_battery_file
[i
].name
,
561 acpi_device_dir(device
));
563 remove_proc_entry(acpi_device_bid(device
), acpi_battery_dir
);
564 acpi_device_dir(device
) = NULL
;
567 /* --------------------------------------------------------------------------
569 -------------------------------------------------------------------------- */
571 static void acpi_battery_notify(acpi_handle handle
, u32 event
, void *data
)
573 struct acpi_battery
*battery
= data
;
574 struct acpi_device
*device
;
577 device
= battery
->device
;
578 acpi_battery_update(battery
);
579 acpi_bus_generate_proc_event(device
, event
,
580 acpi_battery_present(battery
));
581 acpi_bus_generate_netlink_event(device
->pnp
.device_class
,
582 device
->dev
.bus_id
, event
,
583 acpi_battery_present(battery
));
586 static int acpi_battery_add(struct acpi_device
*device
)
589 acpi_status status
= 0;
590 struct acpi_battery
*battery
= NULL
;
593 battery
= kzalloc(sizeof(struct acpi_battery
), GFP_KERNEL
);
596 battery
->device
= device
;
597 strcpy(acpi_device_name(device
), ACPI_BATTERY_DEVICE_NAME
);
598 strcpy(acpi_device_class(device
), ACPI_BATTERY_CLASS
);
599 acpi_driver_data(device
) = battery
;
600 mutex_init(&battery
->lock
);
601 acpi_battery_update(battery
);
602 result
= acpi_battery_add_fs(device
);
605 status
= acpi_install_notify_handler(device
->handle
,
607 acpi_battery_notify
, battery
);
608 if (ACPI_FAILURE(status
)) {
609 ACPI_EXCEPTION((AE_INFO
, status
, "Installing notify handler"));
613 printk(KERN_INFO PREFIX
"%s Slot [%s] (battery %s)\n",
614 ACPI_BATTERY_DEVICE_NAME
, acpi_device_bid(device
),
615 device
->status
.battery_present
? "present" : "absent");
618 acpi_battery_remove_fs(device
);
624 static int acpi_battery_remove(struct acpi_device
*device
, int type
)
626 acpi_status status
= 0;
627 struct acpi_battery
*battery
= NULL
;
629 if (!device
|| !acpi_driver_data(device
))
631 battery
= acpi_driver_data(device
);
632 status
= acpi_remove_notify_handler(device
->handle
,
634 acpi_battery_notify
);
635 acpi_battery_remove_fs(device
);
636 mutex_destroy(&battery
->lock
);
641 /* this is needed to learn about changes made in suspended state */
642 static int acpi_battery_resume(struct acpi_device
*device
)
644 struct acpi_battery
*battery
;
647 battery
= acpi_driver_data(device
);
648 battery
->update_time
= 0;
652 static struct acpi_driver acpi_battery_driver
= {
654 .class = ACPI_BATTERY_CLASS
,
655 .ids
= battery_device_ids
,
657 .add
= acpi_battery_add
,
658 .resume
= acpi_battery_resume
,
659 .remove
= acpi_battery_remove
,
663 static int __init
acpi_battery_init(void)
667 acpi_battery_dir
= acpi_lock_battery_dir();
668 if (!acpi_battery_dir
)
670 if (acpi_bus_register_driver(&acpi_battery_driver
) < 0) {
671 acpi_unlock_battery_dir(acpi_battery_dir
);
677 static void __exit
acpi_battery_exit(void)
679 acpi_bus_unregister_driver(&acpi_battery_driver
);
680 acpi_unlock_battery_dir(acpi_battery_dir
);
683 module_init(acpi_battery_init
);
684 module_exit(acpi_battery_exit
);