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");
1037 foo = (s16) battery->state.amperage * battery->info.ipscale;
1038 if (battery->info.capacity_mode) {
1039 foo = foo * battery->info.design_voltage / 1000;
1041 if (battery->state.amperage < 0) {
1042 seq_printf(seq, "charging state: discharging\n");
1043 seq_printf(seq, "present rate: %d %s\n",
1044 -foo, battery->info.capacity_mode ? "mW" : "mA");
1045 } else if (battery->state.amperage > 0) {
1046 seq_printf(seq, "charging state: charging\n");
1047 seq_printf(seq, "present rate: %d %s\n",
1048 foo, battery->info.capacity_mode ? "mW" : "mA");
1050 seq_printf(seq, "charging state: charged\n");
1051 seq_printf(seq, "present rate: 0 %s\n",
1052 battery->info.capacity_mode ? "mW" : "mA");
1055 seq_printf(seq, "remaining capacity: %i%s",
1056 battery->state.remaining_capacity * cscale,
1057 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1059 seq_printf(seq, "present voltage: %i mV\n",
1060 battery->state.voltage * battery->info.vscale);
1069 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1071 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1074 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1076 struct acpi_battery *battery = seq->private;
1080 if (battery->sbs->zombie) {
1086 if (update_mode == REQUEST_UPDATE_MODE) {
1087 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM);
1089 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1090 "acpi_sbs_update_run() failed\n"));
1094 if (!acpi_battery_is_present(battery)) {
1095 seq_printf(seq, "present: no\n");
1099 if (battery->info.capacity_mode) {
1100 cscale = battery->info.vscale * battery->info.ipscale;
1102 cscale = battery->info.ipscale;
1105 seq_printf(seq, "alarm: ");
1106 if (battery->alarm.remaining_capacity) {
1107 seq_printf(seq, "%i%s",
1108 battery->alarm.remaining_capacity * cscale,
1109 battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1111 seq_printf(seq, "disabled\n");
1122 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1123 size_t count, loff_t * ppos)
1125 struct seq_file *seq = file->private_data;
1126 struct acpi_battery *battery = seq->private;
1127 char alarm_string[12] = { '\0' };
1128 int result, old_alarm, new_alarm;
1130 if (battery->sbs->zombie) {
1136 if (!acpi_battery_is_present(battery)) {
1141 if (count > sizeof(alarm_string) - 1) {
1146 if (copy_from_user(alarm_string, buffer, count)) {
1151 alarm_string[count] = 0;
1153 old_alarm = battery->alarm.remaining_capacity;
1154 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1156 result = acpi_battery_set_alarm(battery, new_alarm);
1158 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1159 "acpi_battery_set_alarm() failed\n"));
1160 acpi_battery_set_alarm(battery, old_alarm);
1163 result = acpi_battery_get_alarm(battery);
1165 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1166 "acpi_battery_get_alarm() failed\n"));
1167 acpi_battery_set_alarm(battery, old_alarm);
1181 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1183 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1186 static struct file_operations acpi_battery_info_fops = {
1187 .open = acpi_battery_info_open_fs,
1189 .llseek = seq_lseek,
1190 .release = single_release,
1191 .owner = THIS_MODULE,
1194 static struct file_operations acpi_battery_state_fops = {
1195 .open = acpi_battery_state_open_fs,
1197 .llseek = seq_lseek,
1198 .release = single_release,
1199 .owner = THIS_MODULE,
1202 static struct file_operations acpi_battery_alarm_fops = {
1203 .open = acpi_battery_alarm_open_fs,
1205 .write = acpi_battery_write_alarm,
1206 .llseek = seq_lseek,
1207 .release = single_release,
1208 .owner = THIS_MODULE,
1211 /* Legacy AC Adapter Interface */
1213 static struct proc_dir_entry *acpi_ac_dir = NULL;
1215 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1217 struct acpi_sbs *sbs = seq->private;
1226 if (update_mode == REQUEST_UPDATE_MODE) {
1227 result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE);
1229 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1230 "acpi_sbs_update_run() failed\n"));
1234 seq_printf(seq, "state: %s\n",
1235 sbs->ac_present ? "on-line" : "off-line");
1242 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1244 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1247 static struct file_operations acpi_ac_state_fops = {
1248 .open = acpi_ac_state_open_fs,
1250 .llseek = seq_lseek,
1251 .release = single_release,
1252 .owner = THIS_MODULE,
1255 /* --------------------------------------------------------------------------
1257 -------------------------------------------------------------------------- */
1261 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1266 struct acpi_battery *battery;
1268 battery = &sbs->battery[id];
1272 battery->init_state = 0;
1276 result = acpi_battery_select(battery);
1278 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1279 "acpi_battery_select() failed\n"));
1283 result = acpi_battery_get_present(battery);
1285 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1286 "acpi_battery_get_present() failed\n"));
1290 is_present = acpi_battery_is_present(battery);
1293 result = acpi_battery_init(battery);
1295 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1296 "acpi_battery_init() failed\n"));
1299 battery->init_state = 1;
1302 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1304 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1307 &acpi_battery_info_fops,
1308 &acpi_battery_state_fops,
1309 &acpi_battery_alarm_fops, battery);
1311 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1312 "acpi_sbs_generic_add_fs() failed\n"));
1321 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1324 if (sbs->battery[id].battery_entry) {
1325 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1330 static int acpi_ac_add(struct acpi_sbs *sbs)
1334 result = acpi_ac_get_present(sbs);
1336 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1337 "acpi_ac_get_present() failed\n"));
1341 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1344 NULL, &acpi_ac_state_fops, NULL, sbs);
1346 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1347 "acpi_sbs_generic_add_fs() failed\n"));
1356 static void acpi_ac_remove(struct acpi_sbs *sbs)
1359 if (sbs->ac_entry) {
1360 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1364 static void acpi_sbs_update_queue_run(unsigned long data)
1366 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data);
1369 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type)
1371 struct acpi_battery *battery;
1374 int old_battery_present;
1376 int new_battery_present;
1379 int do_battery_init, do_ac_init;
1380 s16 old_remaining_capacity;
1386 old_ac_present = acpi_ac_is_present(sbs);
1388 result = acpi_ac_get_present(sbs);
1390 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1391 "acpi_ac_get_present() failed\n"));
1394 new_ac_present = acpi_ac_is_present(sbs);
1396 do_ac_init = (old_ac_present != new_ac_present);
1398 if (data_type == DATA_TYPE_AC_STATE) {
1402 for (id = 0; id < MAX_SBS_BAT; id++) {
1403 battery = &sbs->battery[id];
1404 if (battery->alive == 0) {
1408 old_remaining_capacity = battery->state.remaining_capacity;
1410 old_battery_present = acpi_battery_is_present(battery);
1412 result = acpi_battery_select(battery);
1414 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1415 "acpi_battery_select() failed\n"));
1421 result = acpi_battery_get_present(battery);
1423 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1424 "acpi_battery_get_present() failed\n"));
1430 new_battery_present = acpi_battery_is_present(battery);
1432 do_battery_init = ((old_battery_present != new_battery_present)
1433 && new_battery_present);
1438 if (do_ac_init || do_battery_init ||
1439 update_info_mode || sbs->update_info_mode) {
1440 if (sbs->update_info_mode) {
1441 sbs->update_info_mode = 0;
1443 sbs->update_info_mode = 1;
1445 result = acpi_battery_init(battery);
1447 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1448 "acpi_battery_init() "
1452 if (data_type == DATA_TYPE_INFO) {
1459 if (new_battery_present) {
1460 result = acpi_battery_get_alarm(battery);
1462 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1463 "acpi_battery_get_alarm() "
1466 if (data_type == DATA_TYPE_ALARM) {
1470 result = acpi_battery_get_state(battery);
1472 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1473 "acpi_battery_get_state() "
1480 if (data_type != DATA_TYPE_COMMON) {
1484 if (old_battery_present != new_battery_present) {
1485 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1486 result = acpi_sbs_generate_event(sbs->device,
1487 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1488 new_battery_present,
1490 ACPI_BATTERY_CLASS);
1492 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1493 "acpi_sbs_generate_event() "
1497 if (old_remaining_capacity != battery->state.remaining_capacity) {
1498 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1499 result = acpi_sbs_generate_event(sbs->device,
1500 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1501 new_battery_present,
1503 ACPI_BATTERY_CLASS);
1505 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1506 "acpi_sbs_generate_event() failed\n"));
1514 if (data_type != DATA_TYPE_COMMON) {
1518 if (old_ac_present != new_ac_present) {
1519 result = acpi_sbs_generate_event(sbs->device,
1520 ACPI_SBS_AC_NOTIFY_STATUS,
1525 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1526 "acpi_sbs_generate_event() failed\n"));
1534 static void acpi_sbs_update_queue(void *data)
1536 struct acpi_sbs *sbs = data;
1537 unsigned long delay = -1;
1544 result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON);
1546 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1547 "acpi_sbs_update_run() failed\n"));
1554 if (update_mode == REQUEST_UPDATE_MODE) {
1558 delay = jiffies + HZ * update_time;
1559 sbs->update_timer.data = (unsigned long)data;
1560 sbs->update_timer.function = acpi_sbs_update_queue_run;
1561 sbs->update_timer.expires = delay;
1562 add_timer(&sbs->update_timer);
1567 static int acpi_sbs_add(struct acpi_device *device)
1569 struct acpi_sbs *sbs = NULL;
1570 struct acpi_ec_hc *ec_hc = NULL;
1571 int result, remove_result = 0;
1572 unsigned long sbs_obj;
1574 acpi_status status = AE_OK;
1576 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1578 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "kmalloc() failed\n"));
1585 ec_hc = acpi_get_ec_hc(device);
1593 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1594 "acpi_get_ec_hc() failed: "
1595 "NO driver found for EC HC SMBus\n"));
1600 sbs->device = device;
1601 sbs->smbus = ec_hc->smbus;
1603 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1604 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1605 acpi_driver_data(device) = sbs;
1607 sbs->update_time = 0;
1608 sbs->update_time2 = 0;
1610 result = acpi_ac_add(sbs);
1612 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_ac_add() failed\n"));
1615 result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1616 if (ACPI_FAILURE(result)) {
1617 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1618 "acpi_evaluate_integer() failed\n"));
1624 result = acpi_sbsm_get_info(sbs);
1626 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1627 "acpi_sbsm_get_info() failed\n"));
1630 sbs->sbsm_present = 1;
1632 if (sbs->sbsm_present == 0) {
1633 result = acpi_battery_add(sbs, 0);
1635 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1636 "acpi_battery_add() failed\n"));
1640 for (id = 0; id < MAX_SBS_BAT; id++) {
1641 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1642 result = acpi_battery_add(sbs, id);
1644 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1645 "acpi_battery_add() "
1653 sbs->handle = device->handle;
1655 init_timer(&sbs->update_timer);
1656 if (update_mode == QUEUE_UPDATE_MODE) {
1657 status = acpi_os_execute(OSL_GPE_HANDLER,
1658 acpi_sbs_update_queue, sbs);
1659 if (status != AE_OK) {
1660 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1661 "acpi_os_execute() failed\n"));
1664 sbs->update_time = update_time;
1665 sbs->update_time2 = update_time2;
1667 printk(KERN_INFO PREFIX "%s [%s]\n",
1668 acpi_device_name(device), acpi_device_bid(device));
1672 remove_result = acpi_sbs_remove(device, 0);
1673 if (remove_result) {
1674 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1675 "acpi_sbs_remove() failed\n"));
1682 int acpi_sbs_remove(struct acpi_device *device, int type)
1684 struct acpi_sbs *sbs;
1691 sbs = (struct acpi_sbs *)acpi_driver_data(device);
1698 sbs->update_time = 0;
1699 sbs->update_time2 = 0;
1700 del_timer_sync(&sbs->update_timer);
1701 acpi_os_wait_events_complete(NULL);
1702 del_timer_sync(&sbs->update_timer);
1704 for (id = 0; id < MAX_SBS_BAT; id++) {
1705 acpi_battery_remove(sbs, id);
1708 acpi_ac_remove(sbs);
1715 static int __init acpi_sbs_init(void)
1722 init_MUTEX(&sbs_sem);
1724 if (capacity_mode != DEF_CAPACITY_UNIT
1725 && capacity_mode != MAH_CAPACITY_UNIT
1726 && capacity_mode != MWH_CAPACITY_UNIT) {
1727 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_sbs_init: "
1728 "invalid capacity_mode = %d\n",
1733 acpi_ac_dir = acpi_lock_ac_dir();
1735 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1736 "acpi_lock_ac_dir() failed\n"));
1740 acpi_battery_dir = acpi_lock_battery_dir();
1741 if (!acpi_battery_dir) {
1742 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1743 "acpi_lock_battery_dir() failed\n"));
1747 result = acpi_bus_register_driver(&acpi_sbs_driver);
1749 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1750 "acpi_bus_register_driver() failed\n"));
1757 static void __exit acpi_sbs_exit(void)
1760 acpi_bus_unregister_driver(&acpi_sbs_driver);
1762 acpi_unlock_ac_dir(acpi_ac_dir);
1764 acpi_unlock_battery_dir(acpi_battery_dir);
1765 acpi_battery_dir = NULL;
1770 module_init(acpi_sbs_init);
1771 module_exit(acpi_sbs_exit);