2 * acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $)
4 * Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
6 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or (at
11 * your option) any later version.
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
25 #include <linux/init.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/kernel.h>
29 #include <linux/proc_fs.h>
30 #include <linux/seq_file.h>
31 #include <asm/uaccess.h>
32 #include <linux/acpi.h>
33 #include <linux/i2c.h>
34 #include <linux/delay.h>
38 #define DEF_CAPACITY_UNIT 3
39 #define MAH_CAPACITY_UNIT 1
40 #define MWH_CAPACITY_UNIT 2
41 #define CAPACITY_UNIT DEF_CAPACITY_UNIT
43 #define REQUEST_UPDATE_MODE 1
44 #define QUEUE_UPDATE_MODE 2
46 #define DATA_TYPE_COMMON 0
47 #define DATA_TYPE_INFO 1
48 #define DATA_TYPE_STATE 2
49 #define DATA_TYPE_ALARM 3
50 #define DATA_TYPE_AC_STATE 4
52 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
53 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
54 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
55 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
57 #define ACPI_SBS_COMPONENT 0x00080000
58 #define ACPI_SBS_CLASS "sbs"
59 #define ACPI_AC_CLASS "ac_adapter"
60 #define ACPI_BATTERY_CLASS "battery"
61 #define ACPI_SBS_HID "ACPI0002"
62 #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
63 #define ACPI_SBS_FILE_INFO "info"
64 #define ACPI_SBS_FILE_STATE "state"
65 #define ACPI_SBS_FILE_ALARM "alarm"
66 #define ACPI_BATTERY_DIR_NAME "BAT%i"
67 #define ACPI_AC_DIR_NAME "AC0"
68 #define ACPI_SBC_SMBUS_ADDR 0x9
69 #define ACPI_SBSM_SMBUS_ADDR 0xa
70 #define ACPI_SB_SMBUS_ADDR 0xb
71 #define ACPI_SBS_AC_NOTIFY_STATUS 0x80
72 #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80
73 #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81
75 #define _COMPONENT ACPI_SBS_COMPONENT
78 #define MAX_SMBUS_ERR 1
80 ACPI_MODULE_NAME("sbs");
82 MODULE_AUTHOR("Rich Townsend");
83 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
84 MODULE_LICENSE("GPL");
86 static struct semaphore sbs_sem;
88 #define UPDATE_MODE QUEUE_UPDATE_MODE
89 /* REQUEST_UPDATE_MODE QUEUE_UPDATE_MODE */
90 #define UPDATE_INFO_MODE 0
91 #define UPDATE_TIME 60
92 #define UPDATE_TIME2 0
94 static int capacity_mode = CAPACITY_UNIT;
95 static int update_mode = UPDATE_MODE;
96 static int update_info_mode = UPDATE_INFO_MODE;
97 static int update_time = UPDATE_TIME;
98 static int update_time2 = UPDATE_TIME2;
100 module_param(capacity_mode, int, 0);
101 module_param(update_mode, int, 0);
102 module_param(update_info_mode, int, 0);
103 module_param(update_time, int, 0);
104 module_param(update_time2, int, 0);
106 static int acpi_sbs_add(struct acpi_device *device);
107 static int acpi_sbs_remove(struct acpi_device *device, int type);
108 static void acpi_battery_smbus_err_handler(struct acpi_ec_smbus *smbus);
109 static void acpi_sbs_update_queue(void *data);
111 static struct acpi_driver acpi_sbs_driver = {
113 .class = ACPI_SBS_CLASS,
117 .remove = acpi_sbs_remove,
121 struct acpi_battery_info {
123 s16 full_charge_capacity;
129 char manufacturer_name[I2C_SMBUS_BLOCK_MAX + 3];
130 char device_name[I2C_SMBUS_BLOCK_MAX + 3];
131 char device_chemistry[I2C_SMBUS_BLOCK_MAX + 3];
134 struct acpi_battery_state {
137 s16 remaining_capacity;
138 s16 average_time_to_empty;
139 s16 average_time_to_full;
143 struct acpi_battery_alarm {
144 s16 remaining_capacity;
147 struct acpi_battery {
152 struct acpi_sbs *sbs;
153 struct acpi_battery_info info;
154 struct acpi_battery_state state;
155 struct acpi_battery_alarm alarm;
156 struct proc_dir_entry *battery_entry;
161 struct acpi_device *device;
162 struct acpi_ec_smbus *smbus;
164 int sbsm_batteries_supported;
166 struct proc_dir_entry *ac_entry;
167 struct acpi_battery battery[MAX_SBS_BAT];
168 int update_info_mode;
172 struct timer_list update_timer;
175 static void acpi_update_delay(struct acpi_sbs *sbs);
176 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type);
178 /* --------------------------------------------------------------------------
180 -------------------------------------------------------------------------- */
182 static void acpi_battery_smbus_err_handler(struct acpi_ec_smbus *smbus)
184 union i2c_smbus_data data;
191 result = smbus->adapter.algo->
192 smbus_xfer(&smbus->adapter,
194 0, I2C_SMBUS_READ, 0x16, I2C_SMBUS_BLOCK_DATA, &data);
196 err_number = (data.word & 0x000f);
198 switch (data.word & 0x000f) {
200 err_str = "unexpected bus error";
206 err_str = "reserved command";
209 err_str = "unsupported command";
212 err_str = "access denied";
215 err_str = "overflow/underflow";
218 err_str = "bad size";
221 err_str = "unknown error";
224 err_str = "unrecognized error";
226 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
227 "%s: ret %i, err %i\n", err_str, result, err_number));
231 acpi_sbs_smbus_read_word(struct acpi_ec_smbus *smbus, int addr, int func,
233 void (*err_handler) (struct acpi_ec_smbus * smbus))
235 union i2c_smbus_data data;
239 if (err_handler == NULL) {
240 err_handler = acpi_battery_smbus_err_handler;
243 for (i = 0; i < MAX_SMBUS_ERR; i++) {
245 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
246 I2C_SMBUS_READ, func,
247 I2C_SMBUS_WORD_DATA, &data);
249 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
250 "try %i: smbus->adapter.algo->smbus_xfer() failed\n",
265 acpi_sbs_smbus_read_str(struct acpi_ec_smbus *smbus, int addr, int func,
267 void (*err_handler) (struct acpi_ec_smbus * smbus))
269 union i2c_smbus_data data;
273 if (err_handler == NULL) {
274 err_handler = acpi_battery_smbus_err_handler;
277 for (i = 0; i < MAX_SMBUS_ERR; i++) {
279 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
280 I2C_SMBUS_READ, func,
281 I2C_SMBUS_BLOCK_DATA,
284 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
285 "try %i: smbus->adapter.algo->smbus_xfer() failed\n",
291 strncpy(str, (const char *)data.block + 1,
293 str[data.block[0]] = 0;
302 acpi_sbs_smbus_write_word(struct acpi_ec_smbus *smbus, int addr, int func,
304 void (*err_handler) (struct acpi_ec_smbus * smbus))
306 union i2c_smbus_data data;
310 if (err_handler == NULL) {
311 err_handler = acpi_battery_smbus_err_handler;
316 for (i = 0; i < MAX_SMBUS_ERR; i++) {
318 smbus->adapter.algo->smbus_xfer(&smbus->adapter, addr, 0,
319 I2C_SMBUS_WRITE, func,
320 I2C_SMBUS_WORD_DATA, &data);
322 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
323 "try %i: smbus->adapter.algo"
324 "->smbus_xfer() failed\n", i));
336 /* --------------------------------------------------------------------------
337 Smart Battery System Management
338 -------------------------------------------------------------------------- */
342 static int acpi_sbs_generate_event(struct acpi_device *device,
343 int event, int state, char *bid, char *class)
346 char class_saved[20];
349 strcpy(bid_saved, acpi_device_bid(device));
350 strcpy(class_saved, acpi_device_class(device));
352 strcpy(acpi_device_bid(device), bid);
353 strcpy(acpi_device_class(device), class);
355 result = acpi_bus_generate_event(device, event, state);
357 strcpy(acpi_device_bid(device), bid_saved);
358 strcpy(acpi_device_class(device), class_saved);
363 static int acpi_battery_get_present(struct acpi_battery *battery)
369 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
370 ACPI_SBSM_SMBUS_ADDR, 0x01,
373 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
374 "acpi_sbs_smbus_read_word() failed"));
377 is_present = (state & 0x000f) & (1 << battery->id);
379 battery->battery_present = is_present;
384 static int acpi_battery_is_present(struct acpi_battery *battery)
386 return (battery->battery_present);
389 static int acpi_ac_is_present(struct acpi_sbs *sbs)
391 return (sbs->ac_present);
394 static int acpi_battery_select(struct acpi_battery *battery)
396 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
401 if (battery->sbs->sbsm_present) {
403 /* Take special care not to knobble other nibbles of
404 * state (aka selector_state), since
405 * it causes charging to halt on SBSELs */
408 acpi_sbs_smbus_read_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x01,
411 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
412 "acpi_sbs_smbus_read_word() failed\n"));
416 foo = (state & 0x0fff) | (1 << (battery->id + 12));
418 acpi_sbs_smbus_write_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x01,
421 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
422 "acpi_sbs_smbus_write_word() failed\n"));
431 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
433 struct acpi_ec_smbus *smbus = sbs->smbus;
435 s16 battery_system_info;
437 result = acpi_sbs_smbus_read_word(smbus, ACPI_SBSM_SMBUS_ADDR, 0x04,
438 &battery_system_info, NULL);
440 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
441 "acpi_sbs_smbus_read_word() failed\n"));
445 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
452 static int acpi_battery_get_info(struct acpi_battery *battery)
454 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
457 s16 specification_info;
459 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x03,
461 &acpi_battery_smbus_err_handler);
463 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
464 "acpi_sbs_smbus_read_word() failed\n"));
467 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
469 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x10,
470 &battery->info.full_charge_capacity,
471 &acpi_battery_smbus_err_handler);
473 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
474 "acpi_sbs_smbus_read_word() failed\n"));
478 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x18,
479 &battery->info.design_capacity,
480 &acpi_battery_smbus_err_handler);
486 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x19,
487 &battery->info.design_voltage,
488 &acpi_battery_smbus_err_handler);
490 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
491 "acpi_sbs_smbus_read_word() failed\n"));
495 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x1a,
497 &acpi_battery_smbus_err_handler);
499 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
500 "acpi_sbs_smbus_read_word() failed\n"));
504 switch ((specification_info & 0x0f00) >> 8) {
506 battery->info.vscale = 10;
509 battery->info.vscale = 100;
512 battery->info.vscale = 1000;
515 battery->info.vscale = 1;
518 switch ((specification_info & 0xf000) >> 12) {
520 battery->info.ipscale = 10;
523 battery->info.ipscale = 100;
526 battery->info.ipscale = 1000;
529 battery->info.ipscale = 1;
532 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x1c,
533 &battery->info.serial_number,
534 &acpi_battery_smbus_err_handler);
539 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x20,
540 battery->info.manufacturer_name,
541 &acpi_battery_smbus_err_handler);
543 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
544 "acpi_sbs_smbus_read_str() failed\n"));
548 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x21,
549 battery->info.device_name,
550 &acpi_battery_smbus_err_handler);
552 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
553 "acpi_sbs_smbus_read_str() failed\n"));
557 result = acpi_sbs_smbus_read_str(smbus, ACPI_SB_SMBUS_ADDR, 0x22,
558 battery->info.device_chemistry,
559 &acpi_battery_smbus_err_handler);
561 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
562 "acpi_sbs_smbus_read_str() failed\n"));
570 static void acpi_update_delay(struct acpi_sbs *sbs)
575 if (sbs->update_time2 > 0) {
576 msleep(sbs->update_time2 * 1000);
580 static int acpi_battery_get_state(struct acpi_battery *battery)
582 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
585 acpi_update_delay(battery->sbs);
586 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x09,
587 &battery->state.voltage,
588 &acpi_battery_smbus_err_handler);
590 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
591 "acpi_sbs_smbus_read_word() failed\n"));
595 acpi_update_delay(battery->sbs);
596 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x0a,
597 &battery->state.amperage,
598 &acpi_battery_smbus_err_handler);
600 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
601 "acpi_sbs_smbus_read_word() failed\n"));
605 acpi_update_delay(battery->sbs);
606 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x0f,
607 &battery->state.remaining_capacity,
608 &acpi_battery_smbus_err_handler);
610 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
611 "acpi_sbs_smbus_read_word() failed\n"));
615 acpi_update_delay(battery->sbs);
616 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x12,
617 &battery->state.average_time_to_empty,
618 &acpi_battery_smbus_err_handler);
620 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
621 "acpi_sbs_smbus_read_word() failed\n"));
625 acpi_update_delay(battery->sbs);
626 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x13,
627 &battery->state.average_time_to_full,
628 &acpi_battery_smbus_err_handler);
630 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
631 "acpi_sbs_smbus_read_word() failed\n"));
635 acpi_update_delay(battery->sbs);
636 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x16,
637 &battery->state.battery_status,
638 &acpi_battery_smbus_err_handler);
640 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
641 "acpi_sbs_smbus_read_word() failed\n"));
645 acpi_update_delay(battery->sbs);
651 static int acpi_battery_get_alarm(struct acpi_battery *battery)
653 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
656 result = acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
657 &battery->alarm.remaining_capacity,
658 &acpi_battery_smbus_err_handler);
660 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
661 "acpi_sbs_smbus_read_word() failed\n"));
665 acpi_update_delay(battery->sbs);
672 static int acpi_battery_set_alarm(struct acpi_battery *battery,
675 struct acpi_ec_smbus *smbus = battery->sbs->smbus;
680 result = acpi_battery_select(battery);
682 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
683 "acpi_battery_select() failed\n"));
687 /* If necessary, enable the alarm */
691 acpi_sbs_smbus_read_word(smbus, ACPI_SB_SMBUS_ADDR, 0x03,
693 &acpi_battery_smbus_err_handler);
695 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
696 "acpi_sbs_smbus_read_word() failed\n"));
701 acpi_sbs_smbus_write_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
702 battery_mode & 0xbfff,
703 &acpi_battery_smbus_err_handler);
705 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
706 "acpi_sbs_smbus_write_word() failed\n"));
711 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
712 result = acpi_sbs_smbus_write_word(smbus, ACPI_SB_SMBUS_ADDR, 0x01,
714 &acpi_battery_smbus_err_handler);
716 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
717 "acpi_sbs_smbus_write_word() failed\n"));
726 static int acpi_battery_set_mode(struct acpi_battery *battery)
731 if (capacity_mode == DEF_CAPACITY_UNIT) {
735 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
736 ACPI_SB_SMBUS_ADDR, 0x03,
737 &battery_mode, NULL);
739 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
740 "acpi_sbs_smbus_read_word() failed\n"));
744 if (capacity_mode == MAH_CAPACITY_UNIT) {
745 battery_mode &= 0x7fff;
747 battery_mode |= 0x8000;
749 result = acpi_sbs_smbus_write_word(battery->sbs->smbus,
750 ACPI_SB_SMBUS_ADDR, 0x03,
753 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
754 "acpi_sbs_smbus_write_word() failed\n"));
758 result = acpi_sbs_smbus_read_word(battery->sbs->smbus,
759 ACPI_SB_SMBUS_ADDR, 0x03,
760 &battery_mode, NULL);
762 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
763 "acpi_sbs_smbus_read_word() failed\n"));
771 static int acpi_battery_init(struct acpi_battery *battery)
775 result = acpi_battery_select(battery);
777 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
778 "acpi_battery_init() failed\n"));
782 result = acpi_battery_set_mode(battery);
784 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
785 "acpi_battery_set_mode() failed\n"));
789 result = acpi_battery_get_info(battery);
791 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
792 "acpi_battery_get_info() failed\n"));
796 result = acpi_battery_get_state(battery);
798 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
799 "acpi_battery_get_state() failed\n"));
803 result = acpi_battery_get_alarm(battery);
805 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
806 "acpi_battery_get_alarm() failed\n"));
814 static int acpi_ac_get_present(struct acpi_sbs *sbs)
816 struct acpi_ec_smbus *smbus = sbs->smbus;
820 result = acpi_sbs_smbus_read_word(smbus, ACPI_SBC_SMBUS_ADDR, 0x13,
821 &charger_status, NULL);
824 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
825 "acpi_sbs_smbus_read_word() failed\n"));
829 sbs->ac_present = (charger_status & 0x8000) >> 15;
836 /* --------------------------------------------------------------------------
837 FS Interface (/proc/acpi)
838 -------------------------------------------------------------------------- */
840 /* Generic Routines */
843 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
844 struct proc_dir_entry *parent_dir,
846 struct file_operations *info_fops,
847 struct file_operations *state_fops,
848 struct file_operations *alarm_fops, void *data)
850 struct proc_dir_entry *entry = NULL;
853 *dir = proc_mkdir(dir_name, parent_dir);
855 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
856 "proc_mkdir() failed\n"));
859 (*dir)->owner = THIS_MODULE;
864 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
866 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
867 "create_proc_entry() failed\n"));
869 entry->proc_fops = info_fops;
871 entry->owner = THIS_MODULE;
877 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
879 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
880 "create_proc_entry() failed\n"));
882 entry->proc_fops = state_fops;
884 entry->owner = THIS_MODULE;
890 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
892 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
893 "create_proc_entry() failed\n"));
895 entry->proc_fops = alarm_fops;
897 entry->owner = THIS_MODULE;
905 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
906 struct proc_dir_entry *parent_dir)
910 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
911 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
912 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
913 remove_proc_entry((*dir)->name, parent_dir);
919 /* Smart Battery Interface */
921 static struct proc_dir_entry *acpi_battery_dir = NULL;
923 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
925 struct acpi_battery *battery = seq->private;
929 if (battery->sbs->zombie) {
935 if (update_mode == REQUEST_UPDATE_MODE) {
936 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_INFO);
938 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
939 "acpi_sbs_update_run() failed\n"));
943 if (acpi_battery_is_present(battery)) {
944 seq_printf(seq, "present: yes\n");
946 seq_printf(seq, "present: no\n");
950 if (battery->info.capacity_mode) {
951 cscale = battery->info.vscale * battery->info.ipscale;
953 cscale = battery->info.ipscale;
955 seq_printf(seq, "design capacity: %i%s",
956 battery->info.design_capacity * cscale,
957 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
959 seq_printf(seq, "last full capacity: %i%s",
960 battery->info.full_charge_capacity * cscale,
961 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
963 seq_printf(seq, "battery technology: rechargeable\n");
965 seq_printf(seq, "design voltage: %i mV\n",
966 battery->info.design_voltage * battery->info.vscale);
968 seq_printf(seq, "design capacity warning: unknown\n");
969 seq_printf(seq, "design capacity low: unknown\n");
970 seq_printf(seq, "capacity granularity 1: unknown\n");
971 seq_printf(seq, "capacity granularity 2: unknown\n");
973 seq_printf(seq, "model number: %s\n",
974 battery->info.device_name);
976 seq_printf(seq, "serial number: %i\n",
977 battery->info.serial_number);
979 seq_printf(seq, "battery type: %s\n",
980 battery->info.device_chemistry);
982 seq_printf(seq, "OEM info: %s\n",
983 battery->info.manufacturer_name);
992 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
994 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
997 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
999 struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1004 if (battery->sbs->zombie) {
1010 if (update_mode == REQUEST_UPDATE_MODE) {
1011 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_STATE);
1013 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1014 "acpi_sbs_update_run() failed\n"));
1018 if (acpi_battery_is_present(battery)) {
1019 seq_printf(seq, "present: yes\n");
1021 seq_printf(seq, "present: no\n");
1025 if (battery->info.capacity_mode) {
1026 cscale = battery->info.vscale * battery->info.ipscale;
1028 cscale = battery->info.ipscale;
1031 if (battery->state.battery_status & 0x0010) {
1032 seq_printf(seq, "capacity state: critical\n");
1034 seq_printf(seq, "capacity state: ok\n");
1036 if (battery->state.amperage < 0) {
1037 seq_printf(seq, "charging state: discharging\n");
1038 foo = battery->state.remaining_capacity * cscale * 60 /
1039 (battery->state.average_time_to_empty == 0 ? 1 :
1040 battery->state.average_time_to_empty);
1041 seq_printf(seq, "present rate: %i%s\n",
1042 foo, battery->info.capacity_mode ? "0 mW" : " mA");
1043 } else if (battery->state.amperage > 0) {
1044 seq_printf(seq, "charging state: charging\n");
1045 foo = (battery->info.full_charge_capacity -
1046 battery->state.remaining_capacity) * cscale * 60 /
1047 (battery->state.average_time_to_full == 0 ? 1 :
1048 battery->state.average_time_to_full);
1049 seq_printf(seq, "present rate: %i%s\n",
1050 foo, battery->info.capacity_mode ? "0 mW" : " mA");
1052 seq_printf(seq, "charging state: charged\n");
1053 seq_printf(seq, "present rate: 0 %s\n",
1054 battery->info.capacity_mode ? "mW" : "mA");
1057 seq_printf(seq, "remaining capacity: %i%s",
1058 battery->state.remaining_capacity * cscale,
1059 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1061 seq_printf(seq, "present voltage: %i mV\n",
1062 battery->state.voltage * battery->info.vscale);
1071 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1073 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1076 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1078 struct acpi_battery *battery = seq->private;
1082 if (battery->sbs->zombie) {
1088 if (update_mode == REQUEST_UPDATE_MODE) {
1089 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM);
1091 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1092 "acpi_sbs_update_run() failed\n"));
1096 if (!acpi_battery_is_present(battery)) {
1097 seq_printf(seq, "present: no\n");
1101 if (battery->info.capacity_mode) {
1102 cscale = battery->info.vscale * battery->info.ipscale;
1104 cscale = battery->info.ipscale;
1107 seq_printf(seq, "alarm: ");
1108 if (battery->alarm.remaining_capacity) {
1109 seq_printf(seq, "%i%s",
1110 battery->alarm.remaining_capacity * cscale,
1111 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1113 seq_printf(seq, "disabled\n");
1124 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1125 size_t count, loff_t * ppos)
1127 struct seq_file *seq = file->private_data;
1128 struct acpi_battery *battery = seq->private;
1129 char alarm_string[12] = { '\0' };
1130 int result, old_alarm, new_alarm;
1132 if (battery->sbs->zombie) {
1138 if (!acpi_battery_is_present(battery)) {
1143 if (count > sizeof(alarm_string) - 1) {
1148 if (copy_from_user(alarm_string, buffer, count)) {
1153 alarm_string[count] = 0;
1155 old_alarm = battery->alarm.remaining_capacity;
1156 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1158 result = acpi_battery_set_alarm(battery, new_alarm);
1160 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1161 "acpi_battery_set_alarm() failed\n"));
1162 acpi_battery_set_alarm(battery, old_alarm);
1165 result = acpi_battery_get_alarm(battery);
1167 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1168 "acpi_battery_get_alarm() failed\n"));
1169 acpi_battery_set_alarm(battery, old_alarm);
1183 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1185 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1188 static struct file_operations acpi_battery_info_fops = {
1189 .open = acpi_battery_info_open_fs,
1191 .llseek = seq_lseek,
1192 .release = single_release,
1193 .owner = THIS_MODULE,
1196 static struct file_operations acpi_battery_state_fops = {
1197 .open = acpi_battery_state_open_fs,
1199 .llseek = seq_lseek,
1200 .release = single_release,
1201 .owner = THIS_MODULE,
1204 static struct file_operations acpi_battery_alarm_fops = {
1205 .open = acpi_battery_alarm_open_fs,
1207 .write = acpi_battery_write_alarm,
1208 .llseek = seq_lseek,
1209 .release = single_release,
1210 .owner = THIS_MODULE,
1213 /* Legacy AC Adapter Interface */
1215 static struct proc_dir_entry *acpi_ac_dir = NULL;
1217 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1219 struct acpi_sbs *sbs = seq->private;
1228 if (update_mode == REQUEST_UPDATE_MODE) {
1229 result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE);
1231 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1232 "acpi_sbs_update_run() failed\n"));
1236 seq_printf(seq, "state: %s\n",
1237 sbs->ac_present ? "on-line" : "off-line");
1244 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1246 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1249 static struct file_operations acpi_ac_state_fops = {
1250 .open = acpi_ac_state_open_fs,
1252 .llseek = seq_lseek,
1253 .release = single_release,
1254 .owner = THIS_MODULE,
1257 /* --------------------------------------------------------------------------
1259 -------------------------------------------------------------------------- */
1263 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1268 struct acpi_battery *battery;
1270 battery = &sbs->battery[id];
1274 battery->init_state = 0;
1278 result = acpi_battery_select(battery);
1280 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1281 "acpi_battery_select() failed\n"));
1285 result = acpi_battery_get_present(battery);
1287 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1288 "acpi_battery_get_present() failed\n"));
1292 is_present = acpi_battery_is_present(battery);
1295 result = acpi_battery_init(battery);
1297 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1298 "acpi_battery_init() failed\n"));
1301 battery->init_state = 1;
1304 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1306 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1309 &acpi_battery_info_fops,
1310 &acpi_battery_state_fops,
1311 &acpi_battery_alarm_fops, battery);
1313 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1314 "acpi_sbs_generic_add_fs() failed\n"));
1323 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1326 if (sbs->battery[id].battery_entry) {
1327 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1332 static int acpi_ac_add(struct acpi_sbs *sbs)
1336 result = acpi_ac_get_present(sbs);
1338 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1339 "acpi_ac_get_present() failed\n"));
1343 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1346 NULL, &acpi_ac_state_fops, NULL, sbs);
1348 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1349 "acpi_sbs_generic_add_fs() failed\n"));
1358 static void acpi_ac_remove(struct acpi_sbs *sbs)
1361 if (sbs->ac_entry) {
1362 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1366 static void acpi_sbs_update_queue_run(unsigned long data)
1368 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data);
1371 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type)
1373 struct acpi_battery *battery;
1376 int old_battery_present;
1378 int new_battery_present;
1381 int do_battery_init, do_ac_init;
1382 s16 old_remaining_capacity;
1388 old_ac_present = acpi_ac_is_present(sbs);
1390 result = acpi_ac_get_present(sbs);
1392 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1393 "acpi_ac_get_present() failed\n"));
1396 new_ac_present = acpi_ac_is_present(sbs);
1398 do_ac_init = (old_ac_present != new_ac_present);
1400 if (data_type == DATA_TYPE_AC_STATE) {
1404 for (id = 0; id < MAX_SBS_BAT; id++) {
1405 battery = &sbs->battery[id];
1406 if (battery->alive == 0) {
1410 old_remaining_capacity = battery->state.remaining_capacity;
1412 old_battery_present = acpi_battery_is_present(battery);
1414 result = acpi_battery_select(battery);
1416 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1417 "acpi_battery_select() failed\n"));
1423 result = acpi_battery_get_present(battery);
1425 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1426 "acpi_battery_get_present() failed\n"));
1432 new_battery_present = acpi_battery_is_present(battery);
1434 do_battery_init = ((old_battery_present != new_battery_present)
1435 && new_battery_present);
1440 if (do_ac_init || do_battery_init ||
1441 update_info_mode || sbs->update_info_mode) {
1442 if (sbs->update_info_mode) {
1443 sbs->update_info_mode = 0;
1445 sbs->update_info_mode = 1;
1447 result = acpi_battery_init(battery);
1449 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1450 "acpi_battery_init() "
1454 if (data_type == DATA_TYPE_INFO) {
1461 if (new_battery_present) {
1462 result = acpi_battery_get_alarm(battery);
1464 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1465 "acpi_battery_get_alarm() "
1468 if (data_type == DATA_TYPE_ALARM) {
1472 result = acpi_battery_get_state(battery);
1474 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1475 "acpi_battery_get_state() "
1482 if (data_type != DATA_TYPE_COMMON) {
1486 if (old_battery_present != new_battery_present) {
1487 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1488 result = acpi_sbs_generate_event(sbs->device,
1489 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1490 new_battery_present,
1492 ACPI_BATTERY_CLASS);
1494 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1495 "acpi_sbs_generate_event() "
1499 if (old_remaining_capacity != battery->state.remaining_capacity) {
1500 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1501 result = acpi_sbs_generate_event(sbs->device,
1502 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1503 new_battery_present,
1505 ACPI_BATTERY_CLASS);
1507 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1508 "acpi_sbs_generate_event() failed\n"));
1516 if (data_type != DATA_TYPE_COMMON) {
1520 if (old_ac_present != new_ac_present) {
1521 result = acpi_sbs_generate_event(sbs->device,
1522 ACPI_SBS_AC_NOTIFY_STATUS,
1527 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1528 "acpi_sbs_generate_event() failed\n"));
1536 static void acpi_sbs_update_queue(void *data)
1538 struct acpi_sbs *sbs = data;
1539 unsigned long delay = -1;
1546 result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON);
1548 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1549 "acpi_sbs_update_run() failed\n"));
1556 if (update_mode == REQUEST_UPDATE_MODE) {
1560 delay = jiffies + HZ * update_time;
1561 sbs->update_timer.data = (unsigned long)data;
1562 sbs->update_timer.function = acpi_sbs_update_queue_run;
1563 sbs->update_timer.expires = delay;
1564 add_timer(&sbs->update_timer);
1569 static int acpi_sbs_add(struct acpi_device *device)
1571 struct acpi_sbs *sbs = NULL;
1572 struct acpi_ec_hc *ec_hc = NULL;
1573 int result, remove_result = 0;
1574 unsigned long sbs_obj;
1576 acpi_status status = AE_OK;
1578 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1580 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "kmalloc() failed\n"));
1587 ec_hc = acpi_get_ec_hc(device);
1595 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1596 "acpi_get_ec_hc() failed: "
1597 "NO driver found for EC HC SMBus\n"));
1602 sbs->device = device;
1603 sbs->smbus = ec_hc->smbus;
1605 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1606 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1607 acpi_driver_data(device) = sbs;
1609 sbs->update_time = 0;
1610 sbs->update_time2 = 0;
1612 result = acpi_ac_add(sbs);
1614 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_ac_add() failed\n"));
1617 result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1618 if (ACPI_FAILURE(result)) {
1619 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1620 "acpi_evaluate_integer() failed\n"));
1626 result = acpi_sbsm_get_info(sbs);
1628 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1629 "acpi_sbsm_get_info() failed\n"));
1632 sbs->sbsm_present = 1;
1634 if (sbs->sbsm_present == 0) {
1635 result = acpi_battery_add(sbs, 0);
1637 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1638 "acpi_battery_add() failed\n"));
1642 for (id = 0; id < MAX_SBS_BAT; id++) {
1643 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1644 result = acpi_battery_add(sbs, id);
1646 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1647 "acpi_battery_add() "
1655 sbs->handle = device->handle;
1657 init_timer(&sbs->update_timer);
1658 if (update_mode == QUEUE_UPDATE_MODE) {
1659 status = acpi_os_execute(OSL_GPE_HANDLER,
1660 acpi_sbs_update_queue, sbs);
1661 if (status != AE_OK) {
1662 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1663 "acpi_os_execute() failed\n"));
1666 sbs->update_time = update_time;
1667 sbs->update_time2 = update_time2;
1669 printk(KERN_INFO PREFIX "%s [%s]\n",
1670 acpi_device_name(device), acpi_device_bid(device));
1674 remove_result = acpi_sbs_remove(device, 0);
1675 if (remove_result) {
1676 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1677 "acpi_sbs_remove() failed\n"));
1684 int acpi_sbs_remove(struct acpi_device *device, int type)
1686 struct acpi_sbs *sbs;
1693 sbs = (struct acpi_sbs *)acpi_driver_data(device);
1700 sbs->update_time = 0;
1701 sbs->update_time2 = 0;
1702 del_timer_sync(&sbs->update_timer);
1703 acpi_os_wait_events_complete(NULL);
1704 del_timer_sync(&sbs->update_timer);
1706 for (id = 0; id < MAX_SBS_BAT; id++) {
1707 acpi_battery_remove(sbs, id);
1710 acpi_ac_remove(sbs);
1717 static int __init acpi_sbs_init(void)
1724 init_MUTEX(&sbs_sem);
1726 if (capacity_mode != DEF_CAPACITY_UNIT
1727 && capacity_mode != MAH_CAPACITY_UNIT
1728 && capacity_mode != MWH_CAPACITY_UNIT) {
1729 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_sbs_init: "
1730 "invalid capacity_mode = %d\n",
1735 acpi_ac_dir = acpi_lock_ac_dir();
1737 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1738 "acpi_lock_ac_dir() failed\n"));
1742 acpi_battery_dir = acpi_lock_battery_dir();
1743 if (!acpi_battery_dir) {
1744 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1745 "acpi_lock_battery_dir() failed\n"));
1749 result = acpi_bus_register_driver(&acpi_sbs_driver);
1751 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1752 "acpi_bus_register_driver() failed\n"));
1759 static void __exit acpi_sbs_exit(void)
1762 acpi_bus_unregister_driver(&acpi_sbs_driver);
1764 acpi_unlock_ac_dir(acpi_ac_dir);
1766 acpi_unlock_battery_dir(acpi_battery_dir);
1767 acpi_battery_dir = NULL;
1772 module_init(acpi_sbs_init);
1773 module_exit(acpi_sbs_exit);