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/timer.h>
34 #include <linux/jiffies.h>
35 #include <linux/delay.h>
37 #define ACPI_SBS_COMPONENT 0x00080000
38 #define ACPI_SBS_CLASS "sbs"
39 #define ACPI_AC_CLASS "ac_adapter"
40 #define ACPI_BATTERY_CLASS "battery"
41 #define ACPI_SBS_HID "ACPI0002"
42 #define ACPI_SBS_DEVICE_NAME "Smart Battery System"
43 #define ACPI_SBS_FILE_INFO "info"
44 #define ACPI_SBS_FILE_STATE "state"
45 #define ACPI_SBS_FILE_ALARM "alarm"
46 #define ACPI_BATTERY_DIR_NAME "BAT%i"
47 #define ACPI_AC_DIR_NAME "AC0"
48 #define ACPI_SBC_SMBUS_ADDR 0x9
49 #define ACPI_SBSM_SMBUS_ADDR 0xa
50 #define ACPI_SB_SMBUS_ADDR 0xb
51 #define ACPI_SBS_AC_NOTIFY_STATUS 0x80
52 #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80
53 #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81
55 #define _COMPONENT ACPI_SBS_COMPONENT
57 ACPI_MODULE_NAME("sbs");
59 MODULE_AUTHOR("Rich Townsend");
60 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
61 MODULE_LICENSE("GPL");
63 #define xmsleep(t) msleep(t)
65 #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */
67 #define ACPI_EC_SMB_STS 0x01 /* status */
68 #define ACPI_EC_SMB_ADDR 0x02 /* address */
69 #define ACPI_EC_SMB_CMD 0x03 /* command */
70 #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */
71 #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */
73 #define ACPI_EC_SMB_STS_DONE 0x80
74 #define ACPI_EC_SMB_STS_STATUS 0x1f
76 #define ACPI_EC_SMB_PRTCL_WRITE 0x00
77 #define ACPI_EC_SMB_PRTCL_READ 0x01
78 #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08
79 #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a
81 #define ACPI_EC_SMB_TRANSACTION_SLEEP 1
82 #define ACPI_EC_SMB_ACCESS_SLEEP1 1
83 #define ACPI_EC_SMB_ACCESS_SLEEP2 10
85 #define DEF_CAPACITY_UNIT 3
86 #define MAH_CAPACITY_UNIT 1
87 #define MWH_CAPACITY_UNIT 2
88 #define CAPACITY_UNIT DEF_CAPACITY_UNIT
90 #define REQUEST_UPDATE_MODE 1
91 #define QUEUE_UPDATE_MODE 2
93 #define DATA_TYPE_COMMON 0
94 #define DATA_TYPE_INFO 1
95 #define DATA_TYPE_STATE 2
96 #define DATA_TYPE_ALARM 3
97 #define DATA_TYPE_AC_STATE 4
99 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
100 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
101 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
102 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
104 #define MAX_SBS_BAT 4
105 #define ACPI_SBS_BLOCK_MAX 32
107 #define ACPI_SBS_SMBUS_READ 1
108 #define ACPI_SBS_SMBUS_WRITE 2
110 #define ACPI_SBS_WORD_DATA 1
111 #define ACPI_SBS_BLOCK_DATA 2
113 #define UPDATE_DELAY 10
115 /* 0 - every time, > 0 - by update_time */
116 static unsigned int update_time = 120;
118 static unsigned int capacity_mode = CAPACITY_UNIT;
120 module_param(update_time, uint, 0644);
121 module_param(capacity_mode, uint, 0444);
123 static int acpi_sbs_add(struct acpi_device *device);
124 static int acpi_sbs_remove(struct acpi_device *device, int type);
125 static int acpi_sbs_resume(struct acpi_device *device);
127 static struct acpi_driver acpi_sbs_driver = {
129 .class = ACPI_SBS_CLASS,
130 .ids = "ACPI0001,ACPI0005",
133 .remove = acpi_sbs_remove,
134 .resume = acpi_sbs_resume,
142 struct acpi_battery_info {
144 s16 full_charge_capacity;
150 char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
151 char device_name[ACPI_SBS_BLOCK_MAX + 3];
152 char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
155 struct acpi_battery_state {
158 s16 remaining_capacity;
162 struct acpi_battery_alarm {
163 s16 remaining_capacity;
166 struct acpi_battery {
171 struct acpi_sbs *sbs;
172 struct acpi_battery_info info;
173 struct acpi_battery_state state;
174 struct acpi_battery_alarm alarm;
175 struct proc_dir_entry *battery_entry;
180 struct acpi_device *device;
183 int sbsm_batteries_supported;
184 struct proc_dir_entry *ac_entry;
186 struct acpi_battery battery[MAX_SBS_BAT];
188 struct timer_list update_timer;
193 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
194 static void acpi_sbs_update_time(void *data);
198 u8 block[ACPI_SBS_BLOCK_MAX + 2];
201 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
202 char read_write, u8 command, int size,
203 union sbs_rw_data *data);
205 /* --------------------------------------------------------------------------
207 -------------------------------------------------------------------------- */
209 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
214 err = ec_read(sbs->base + address, &val);
218 xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
222 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
226 err = ec_write(sbs->base + address, data);
231 acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
232 char read_write, u8 command, int size,
233 union sbs_rw_data *data)
235 unsigned char protocol, len = 0, temp[2] = { 0, 0 };
238 if (read_write == ACPI_SBS_SMBUS_READ) {
239 protocol = ACPI_EC_SMB_PRTCL_READ;
241 protocol = ACPI_EC_SMB_PRTCL_WRITE;
246 case ACPI_SBS_WORD_DATA:
247 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
248 if (read_write == ACPI_SBS_SMBUS_WRITE) {
249 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word);
250 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1,
253 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA;
255 case ACPI_SBS_BLOCK_DATA:
256 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
257 if (read_write == ACPI_SBS_SMBUS_WRITE) {
258 len = min_t(u8, data->block[0], 32);
259 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len);
260 for (i = 0; i < len; i++)
261 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i,
264 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA;
267 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
268 "unsupported transaction %d", size));
272 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1);
273 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol);
275 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
277 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
278 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP1);
279 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
281 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
282 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
283 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
285 if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
286 || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
287 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
288 "transaction %d error", size));
292 if (read_write == ACPI_SBS_SMBUS_WRITE) {
298 case ACPI_SBS_WORD_DATA:
299 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp);
300 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1);
301 data->word = (temp[1] << 8) | temp[0];
304 case ACPI_SBS_BLOCK_DATA:
306 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len);
307 len = min_t(u8, len, 32);
308 for (i = 0; i < len; i++)
309 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i,
310 data->block + i + 1);
311 data->block[0] = len;
314 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
315 "unsupported transaction %d", size));
323 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
325 union sbs_rw_data data;
328 result = acpi_ec_sbs_access(sbs, addr,
329 ACPI_SBS_SMBUS_READ, func,
330 ACPI_SBS_WORD_DATA, &data);
332 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
333 "acpi_ec_sbs_access() failed"));
342 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
344 union sbs_rw_data data;
347 result = acpi_ec_sbs_access(sbs, addr,
348 ACPI_SBS_SMBUS_READ, func,
349 ACPI_SBS_BLOCK_DATA, &data);
351 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
352 "acpi_ec_sbs_access() failed"));
354 strncpy(str, (const char *)data.block + 1, data.block[0]);
355 str[data.block[0]] = 0;
362 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
364 union sbs_rw_data data;
369 result = acpi_ec_sbs_access(sbs, addr,
370 ACPI_SBS_SMBUS_WRITE, func,
371 ACPI_SBS_WORD_DATA, &data);
373 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
374 "acpi_ec_sbs_access() failed"));
380 static int sbs_zombie(struct acpi_sbs *sbs)
382 return (sbs->zombie);
385 static int sbs_mutex_lock(struct acpi_sbs *sbs)
387 if (sbs_zombie(sbs)) {
390 mutex_lock(&sbs->mutex);
394 static void sbs_mutex_unlock(struct acpi_sbs *sbs)
396 mutex_unlock(&sbs->mutex);
399 /* --------------------------------------------------------------------------
400 Smart Battery System Management
401 -------------------------------------------------------------------------- */
403 static int acpi_check_update_proc(struct acpi_sbs *sbs)
405 acpi_status status = AE_OK;
407 if (update_time == 0) {
408 sbs->update_proc_flg = 0;
411 if (sbs->update_proc_flg == 0) {
412 status = acpi_os_execute(OSL_GPE_HANDLER,
413 acpi_sbs_update_time, sbs);
414 if (status != AE_OK) {
415 ACPI_EXCEPTION((AE_INFO, status,
416 "acpi_os_execute() failed"));
419 sbs->update_proc_flg = 1;
424 static int acpi_sbs_generate_event(struct acpi_device *device,
425 int event, int state, char *bid, char *class)
428 char class_saved[20];
431 strcpy(bid_saved, acpi_device_bid(device));
432 strcpy(class_saved, acpi_device_class(device));
434 strcpy(acpi_device_bid(device), bid);
435 strcpy(acpi_device_class(device), class);
437 result = acpi_bus_generate_event(device, event, state);
439 strcpy(acpi_device_bid(device), bid_saved);
440 strcpy(acpi_device_class(device), class_saved);
445 static int acpi_battery_get_present(struct acpi_battery *battery)
451 result = acpi_sbs_read_word(battery->sbs,
452 ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
454 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
455 "acpi_sbs_read_word() failed"));
458 is_present = (state & 0x000f) & (1 << battery->id);
460 battery->battery_present = is_present;
465 static int acpi_battery_select(struct acpi_battery *battery)
467 struct acpi_sbs *sbs = battery->sbs;
472 if (sbs->sbsm_present) {
474 /* Take special care not to knobble other nibbles of
475 * state (aka selector_state), since
476 * it causes charging to halt on SBSELs */
479 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
481 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
482 "acpi_sbs_read_word() failed"));
486 foo = (state & 0x0fff) | (1 << (battery->id + 12));
488 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
490 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
491 "acpi_sbs_write_word() failed"));
500 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
503 s16 battery_system_info;
505 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
506 &battery_system_info);
508 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
509 "acpi_sbs_read_word() failed"));
512 sbs->sbsm_present = 1;
513 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
520 static int acpi_battery_get_info(struct acpi_battery *battery)
522 struct acpi_sbs *sbs = battery->sbs;
525 s16 specification_info;
527 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
530 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
531 "acpi_sbs_read_word() failed"));
534 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
536 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
537 &battery->info.full_charge_capacity);
539 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
540 "acpi_sbs_read_word() failed"));
544 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
545 &battery->info.design_capacity);
548 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
549 "acpi_sbs_read_word() failed"));
553 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
554 &battery->info.design_voltage);
556 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
557 "acpi_sbs_read_word() failed"));
561 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
562 &specification_info);
564 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
565 "acpi_sbs_read_word() failed"));
569 switch ((specification_info & 0x0f00) >> 8) {
571 battery->info.vscale = 10;
574 battery->info.vscale = 100;
577 battery->info.vscale = 1000;
580 battery->info.vscale = 1;
583 switch ((specification_info & 0xf000) >> 12) {
585 battery->info.ipscale = 10;
588 battery->info.ipscale = 100;
591 battery->info.ipscale = 1000;
594 battery->info.ipscale = 1;
597 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
598 &battery->info.serial_number);
600 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
601 "acpi_sbs_read_word() failed"));
605 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
606 battery->info.manufacturer_name);
608 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
609 "acpi_sbs_read_str() failed"));
613 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
614 battery->info.device_name);
616 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
617 "acpi_sbs_read_str() failed"));
621 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
622 battery->info.device_chemistry);
624 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
625 "acpi_sbs_read_str() failed"));
633 static int acpi_battery_get_state(struct acpi_battery *battery)
635 struct acpi_sbs *sbs = battery->sbs;
638 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
639 &battery->state.voltage);
641 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
642 "acpi_sbs_read_word() failed"));
646 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
647 &battery->state.amperage);
649 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
650 "acpi_sbs_read_word() failed"));
654 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
655 &battery->state.remaining_capacity);
657 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
658 "acpi_sbs_read_word() failed"));
662 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
663 &battery->state.battery_state);
665 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
666 "acpi_sbs_read_word() failed"));
674 static int acpi_battery_get_alarm(struct acpi_battery *battery)
676 struct acpi_sbs *sbs = battery->sbs;
679 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
680 &battery->alarm.remaining_capacity);
682 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
683 "acpi_sbs_read_word() failed"));
692 static int acpi_battery_set_alarm(struct acpi_battery *battery,
695 struct acpi_sbs *sbs = battery->sbs;
700 result = acpi_battery_select(battery);
702 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
703 "acpi_battery_select() failed"));
707 /* If necessary, enable the alarm */
711 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
714 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
715 "acpi_sbs_read_word() failed"));
720 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
721 battery_mode & 0xbfff);
723 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
724 "acpi_sbs_write_word() failed"));
729 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
730 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
732 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
733 "acpi_sbs_write_word() failed"));
742 static int acpi_battery_set_mode(struct acpi_battery *battery)
744 struct acpi_sbs *sbs = battery->sbs;
748 if (capacity_mode == DEF_CAPACITY_UNIT) {
752 result = acpi_sbs_read_word(sbs,
753 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
755 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
756 "acpi_sbs_read_word() failed"));
760 if (capacity_mode == MAH_CAPACITY_UNIT) {
761 battery_mode &= 0x7fff;
763 battery_mode |= 0x8000;
765 result = acpi_sbs_write_word(sbs,
766 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
768 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
769 "acpi_sbs_write_word() failed"));
773 result = acpi_sbs_read_word(sbs,
774 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
776 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
777 "acpi_sbs_read_word() failed"));
785 static int acpi_battery_init(struct acpi_battery *battery)
789 result = acpi_battery_select(battery);
791 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
792 "acpi_battery_select() failed"));
796 result = acpi_battery_set_mode(battery);
798 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
799 "acpi_battery_set_mode() failed"));
803 result = acpi_battery_get_info(battery);
805 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
806 "acpi_battery_get_info() failed"));
810 result = acpi_battery_get_state(battery);
812 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
813 "acpi_battery_get_state() failed"));
817 result = acpi_battery_get_alarm(battery);
819 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
820 "acpi_battery_get_alarm() failed"));
828 static int acpi_ac_get_present(struct acpi_sbs *sbs)
833 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
837 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
838 "acpi_sbs_read_word() failed"));
842 sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
849 /* --------------------------------------------------------------------------
850 FS Interface (/proc/acpi)
851 -------------------------------------------------------------------------- */
853 /* Generic Routines */
856 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
857 struct proc_dir_entry *parent_dir,
859 struct file_operations *info_fops,
860 struct file_operations *state_fops,
861 struct file_operations *alarm_fops, void *data)
863 struct proc_dir_entry *entry = NULL;
866 *dir = proc_mkdir(dir_name, parent_dir);
868 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
869 "proc_mkdir() failed"));
872 (*dir)->owner = THIS_MODULE;
877 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
879 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
880 "create_proc_entry() failed"));
882 entry->proc_fops = info_fops;
884 entry->owner = THIS_MODULE;
890 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
892 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
893 "create_proc_entry() failed"));
895 entry->proc_fops = state_fops;
897 entry->owner = THIS_MODULE;
903 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
905 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
906 "create_proc_entry() failed"));
908 entry->proc_fops = alarm_fops;
910 entry->owner = THIS_MODULE;
918 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
919 struct proc_dir_entry *parent_dir)
923 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
924 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
925 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
926 remove_proc_entry((*dir)->name, parent_dir);
932 /* Smart Battery Interface */
934 static struct proc_dir_entry *acpi_battery_dir = NULL;
936 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
938 struct acpi_battery *battery = seq->private;
939 struct acpi_sbs *sbs = battery->sbs;
943 if (sbs_mutex_lock(sbs)) {
947 result = acpi_check_update_proc(sbs);
951 if (update_time == 0) {
952 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
954 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
955 "acpi_sbs_update_run() failed"));
959 if (battery->battery_present) {
960 seq_printf(seq, "present: yes\n");
962 seq_printf(seq, "present: no\n");
966 if (battery->info.capacity_mode) {
967 cscale = battery->info.vscale * battery->info.ipscale;
969 cscale = battery->info.ipscale;
971 seq_printf(seq, "design capacity: %i%s\n",
972 battery->info.design_capacity * cscale,
973 battery->info.capacity_mode ? "0 mWh" : " mAh");
975 seq_printf(seq, "last full capacity: %i%s\n",
976 battery->info.full_charge_capacity * cscale,
977 battery->info.capacity_mode ? "0 mWh" : " mAh");
979 seq_printf(seq, "battery technology: rechargeable\n");
981 seq_printf(seq, "design voltage: %i mV\n",
982 battery->info.design_voltage * battery->info.vscale);
984 seq_printf(seq, "design capacity warning: unknown\n");
985 seq_printf(seq, "design capacity low: unknown\n");
986 seq_printf(seq, "capacity granularity 1: unknown\n");
987 seq_printf(seq, "capacity granularity 2: unknown\n");
989 seq_printf(seq, "model number: %s\n",
990 battery->info.device_name);
992 seq_printf(seq, "serial number: %i\n",
993 battery->info.serial_number);
995 seq_printf(seq, "battery type: %s\n",
996 battery->info.device_chemistry);
998 seq_printf(seq, "OEM info: %s\n",
999 battery->info.manufacturer_name);
1003 sbs_mutex_unlock(sbs);
1008 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1010 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1013 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1015 struct acpi_battery *battery = seq->private;
1016 struct acpi_sbs *sbs = battery->sbs;
1021 if (sbs_mutex_lock(sbs)) {
1025 result = acpi_check_update_proc(sbs);
1029 if (update_time == 0) {
1030 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1032 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1033 "acpi_sbs_update_run() failed"));
1037 if (battery->battery_present) {
1038 seq_printf(seq, "present: yes\n");
1040 seq_printf(seq, "present: no\n");
1044 if (battery->info.capacity_mode) {
1045 cscale = battery->info.vscale * battery->info.ipscale;
1047 cscale = battery->info.ipscale;
1050 if (battery->state.battery_state & 0x0010) {
1051 seq_printf(seq, "capacity state: critical\n");
1053 seq_printf(seq, "capacity state: ok\n");
1056 foo = (s16) battery->state.amperage * battery->info.ipscale;
1057 if (battery->info.capacity_mode) {
1058 foo = foo * battery->info.design_voltage / 1000;
1060 if (battery->state.amperage < 0) {
1061 seq_printf(seq, "charging state: discharging\n");
1062 seq_printf(seq, "present rate: %d %s\n",
1063 -foo, battery->info.capacity_mode ? "mW" : "mA");
1064 } else if (battery->state.amperage > 0) {
1065 seq_printf(seq, "charging state: charging\n");
1066 seq_printf(seq, "present rate: %d %s\n",
1067 foo, battery->info.capacity_mode ? "mW" : "mA");
1069 seq_printf(seq, "charging state: charged\n");
1070 seq_printf(seq, "present rate: 0 %s\n",
1071 battery->info.capacity_mode ? "mW" : "mA");
1074 seq_printf(seq, "remaining capacity: %i%s\n",
1075 battery->state.remaining_capacity * cscale,
1076 battery->info.capacity_mode ? "0 mWh" : " mAh");
1078 seq_printf(seq, "present voltage: %i mV\n",
1079 battery->state.voltage * battery->info.vscale);
1083 sbs_mutex_unlock(sbs);
1088 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1090 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1093 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1095 struct acpi_battery *battery = seq->private;
1096 struct acpi_sbs *sbs = battery->sbs;
1100 if (sbs_mutex_lock(sbs)) {
1104 result = acpi_check_update_proc(sbs);
1108 if (update_time == 0) {
1109 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1111 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1112 "acpi_sbs_update_run() failed"));
1116 if (!battery->battery_present) {
1117 seq_printf(seq, "present: no\n");
1121 if (battery->info.capacity_mode) {
1122 cscale = battery->info.vscale * battery->info.ipscale;
1124 cscale = battery->info.ipscale;
1127 seq_printf(seq, "alarm: ");
1128 if (battery->alarm.remaining_capacity) {
1129 seq_printf(seq, "%i%s\n",
1130 battery->alarm.remaining_capacity * cscale,
1131 battery->info.capacity_mode ? "0 mWh" : " mAh");
1133 seq_printf(seq, "disabled\n");
1138 sbs_mutex_unlock(sbs);
1144 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1145 size_t count, loff_t * ppos)
1147 struct seq_file *seq = file->private_data;
1148 struct acpi_battery *battery = seq->private;
1149 struct acpi_sbs *sbs = battery->sbs;
1150 char alarm_string[12] = { '\0' };
1151 int result, old_alarm, new_alarm;
1153 if (sbs_mutex_lock(sbs)) {
1157 result = acpi_check_update_proc(sbs);
1161 if (!battery->battery_present) {
1166 if (count > sizeof(alarm_string) - 1) {
1171 if (copy_from_user(alarm_string, buffer, count)) {
1176 alarm_string[count] = 0;
1178 old_alarm = battery->alarm.remaining_capacity;
1179 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1181 result = acpi_battery_set_alarm(battery, new_alarm);
1183 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1184 "acpi_battery_set_alarm() failed"));
1185 acpi_battery_set_alarm(battery, old_alarm);
1188 result = acpi_battery_get_alarm(battery);
1190 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1191 "acpi_battery_get_alarm() failed"));
1192 acpi_battery_set_alarm(battery, old_alarm);
1197 sbs_mutex_unlock(sbs);
1206 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1208 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1211 static struct file_operations acpi_battery_info_fops = {
1212 .open = acpi_battery_info_open_fs,
1214 .llseek = seq_lseek,
1215 .release = single_release,
1216 .owner = THIS_MODULE,
1219 static struct file_operations acpi_battery_state_fops = {
1220 .open = acpi_battery_state_open_fs,
1222 .llseek = seq_lseek,
1223 .release = single_release,
1224 .owner = THIS_MODULE,
1227 static struct file_operations acpi_battery_alarm_fops = {
1228 .open = acpi_battery_alarm_open_fs,
1230 .write = acpi_battery_write_alarm,
1231 .llseek = seq_lseek,
1232 .release = single_release,
1233 .owner = THIS_MODULE,
1236 /* Legacy AC Adapter Interface */
1238 static struct proc_dir_entry *acpi_ac_dir = NULL;
1240 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1242 struct acpi_sbs *sbs = seq->private;
1245 if (sbs_mutex_lock(sbs)) {
1249 if (update_time == 0) {
1250 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1252 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1253 "acpi_sbs_update_run() failed"));
1257 seq_printf(seq, "state: %s\n",
1258 sbs->ac.ac_present ? "on-line" : "off-line");
1260 sbs_mutex_unlock(sbs);
1265 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1267 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1270 static struct file_operations acpi_ac_state_fops = {
1271 .open = acpi_ac_state_open_fs,
1273 .llseek = seq_lseek,
1274 .release = single_release,
1275 .owner = THIS_MODULE,
1278 /* --------------------------------------------------------------------------
1280 -------------------------------------------------------------------------- */
1284 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1289 struct acpi_battery *battery;
1291 battery = &sbs->battery[id];
1295 battery->init_state = 0;
1299 result = acpi_battery_select(battery);
1301 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1302 "acpi_battery_select() failed"));
1306 result = acpi_battery_get_present(battery);
1308 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1309 "acpi_battery_get_present() failed"));
1313 is_present = battery->battery_present;
1316 result = acpi_battery_init(battery);
1318 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1319 "acpi_battery_init() failed"));
1322 battery->init_state = 1;
1325 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1327 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1330 &acpi_battery_info_fops,
1331 &acpi_battery_state_fops,
1332 &acpi_battery_alarm_fops, battery);
1334 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1335 "acpi_sbs_generic_add_fs() failed"));
1340 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
1341 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1342 sbs->battery->battery_present ? "present" : "absent");
1348 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1351 if (sbs->battery[id].battery_entry) {
1352 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1357 static int acpi_ac_add(struct acpi_sbs *sbs)
1361 result = acpi_ac_get_present(sbs);
1363 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1364 "acpi_ac_get_present() failed"));
1368 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1371 NULL, &acpi_ac_state_fops, NULL, sbs);
1373 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1374 "acpi_sbs_generic_add_fs() failed"));
1378 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
1379 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1380 ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
1387 static void acpi_ac_remove(struct acpi_sbs *sbs)
1390 if (sbs->ac_entry) {
1391 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1395 static void acpi_sbs_update_time_run(unsigned long data)
1397 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1400 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1402 struct acpi_battery *battery;
1403 int result = 0, cnt;
1404 int old_ac_present = -1;
1405 int old_battery_present = -1;
1406 int new_ac_present = -1;
1407 int new_battery_present = -1;
1408 int id_min = 0, id_max = MAX_SBS_BAT - 1;
1410 int do_battery_init = 0, do_ac_init = 0;
1411 int old_remaining_capacity = 0;
1412 int update_ac = 1, update_battery = 1;
1413 int up_tm = update_time;
1415 if (sbs_zombie(sbs)) {
1420 id_min = id_max = id;
1423 if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
1424 cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1425 if (sbs->run_cnt % cnt != 0) {
1432 if (!update_ac && !update_battery) {
1436 old_ac_present = sbs->ac.ac_present;
1438 result = acpi_ac_get_present(sbs);
1440 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1441 "acpi_ac_get_present() failed"));
1444 new_ac_present = sbs->ac.ac_present;
1446 do_ac_init = (old_ac_present != new_ac_present);
1447 if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
1452 result = acpi_sbs_generate_event(sbs->device,
1453 ACPI_SBS_AC_NOTIFY_STATUS,
1458 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1459 "acpi_sbs_generate_event() failed"));
1463 if (data_type == DATA_TYPE_COMMON) {
1464 if (!do_ac_init && !update_battery) {
1469 if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1473 for (id = id_min; id <= id_max; id++) {
1474 battery = &sbs->battery[id];
1475 if (battery->alive == 0) {
1479 old_remaining_capacity = battery->state.remaining_capacity;
1481 old_battery_present = battery->battery_present;
1483 result = acpi_battery_select(battery);
1485 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1486 "acpi_battery_select() failed"));
1489 result = acpi_battery_get_present(battery);
1491 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1492 "acpi_battery_get_present() failed"));
1495 new_battery_present = battery->battery_present;
1497 do_battery_init = ((old_battery_present != new_battery_present)
1498 && new_battery_present);
1499 if (!new_battery_present)
1501 if (do_ac_init || do_battery_init) {
1502 result = acpi_battery_init(battery);
1504 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1505 "acpi_battery_init() "
1509 if (sbs_zombie(sbs)) {
1513 if ((data_type == DATA_TYPE_COMMON
1514 || data_type == DATA_TYPE_INFO)
1515 && new_battery_present) {
1516 result = acpi_battery_get_info(battery);
1518 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1519 "acpi_battery_get_info() failed"));
1522 if (data_type == DATA_TYPE_INFO) {
1525 if (sbs_zombie(sbs)) {
1529 if ((data_type == DATA_TYPE_COMMON
1530 || data_type == DATA_TYPE_STATE)
1531 && new_battery_present) {
1532 result = acpi_battery_get_state(battery);
1534 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1535 "acpi_battery_get_state() failed"));
1538 if (data_type == DATA_TYPE_STATE) {
1541 if (sbs_zombie(sbs)) {
1545 if ((data_type == DATA_TYPE_COMMON
1546 || data_type == DATA_TYPE_ALARM)
1547 && new_battery_present) {
1548 result = acpi_battery_get_alarm(battery);
1550 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1551 "acpi_battery_get_alarm() "
1555 if (data_type == DATA_TYPE_ALARM) {
1558 if (sbs_zombie(sbs)) {
1564 if (old_battery_present != new_battery_present || do_ac_init ||
1565 old_remaining_capacity !=
1566 battery->state.remaining_capacity) {
1567 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1568 result = acpi_sbs_generate_event(sbs->device,
1569 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1570 new_battery_present,
1572 ACPI_BATTERY_CLASS);
1574 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1575 "acpi_sbs_generate_event() "
1586 static void acpi_sbs_update_time(void *data)
1588 struct acpi_sbs *sbs = data;
1589 unsigned long delay = -1;
1591 unsigned int up_tm = update_time;
1593 if (sbs_mutex_lock(sbs))
1596 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1598 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1599 "acpi_sbs_update_run() failed"));
1602 if (sbs_zombie(sbs)) {
1607 if (timer_pending(&sbs->update_timer))
1608 del_timer(&sbs->update_timer);
1610 delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1611 delay = jiffies + HZ * delay;
1612 if (timer_pending(&sbs->update_timer)) {
1613 mod_timer(&sbs->update_timer, delay);
1615 sbs->update_timer.data = (unsigned long)data;
1616 sbs->update_timer.function = acpi_sbs_update_time_run;
1617 sbs->update_timer.expires = delay;
1618 add_timer(&sbs->update_timer);
1624 sbs_mutex_unlock(sbs);
1627 static int acpi_sbs_add(struct acpi_device *device)
1629 struct acpi_sbs *sbs = NULL;
1630 int result = 0, remove_result = 0;
1632 acpi_status status = AE_OK;
1636 acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
1637 if (ACPI_FAILURE(status)) {
1638 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
1642 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1644 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
1649 mutex_init(&sbs->mutex);
1651 sbs_mutex_lock(sbs);
1653 sbs->base = 0xff & (val >> 8);
1654 sbs->device = device;
1656 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1657 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1658 acpi_driver_data(device) = sbs;
1660 result = acpi_ac_add(sbs);
1662 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1666 acpi_sbsm_get_info(sbs);
1668 if (!sbs->sbsm_present) {
1669 result = acpi_battery_add(sbs, 0);
1671 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1672 "acpi_battery_add() failed"));
1676 for (id = 0; id < MAX_SBS_BAT; id++) {
1677 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1678 result = acpi_battery_add(sbs, id);
1680 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1681 "acpi_battery_add() failed"));
1688 init_timer(&sbs->update_timer);
1689 result = acpi_check_update_proc(sbs);
1695 sbs_mutex_unlock(sbs);
1698 remove_result = acpi_sbs_remove(device, 0);
1699 if (remove_result) {
1700 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1701 "acpi_sbs_remove() failed"));
1708 static int acpi_sbs_remove(struct acpi_device *device, int type)
1710 struct acpi_sbs *sbs;
1717 sbs = acpi_driver_data(device);
1722 sbs_mutex_lock(sbs);
1725 del_timer_sync(&sbs->update_timer);
1726 acpi_os_wait_events_complete(NULL);
1727 del_timer_sync(&sbs->update_timer);
1729 for (id = 0; id < MAX_SBS_BAT; id++) {
1730 acpi_battery_remove(sbs, id);
1733 acpi_ac_remove(sbs);
1735 sbs_mutex_unlock(sbs);
1737 mutex_destroy(&sbs->mutex);
1744 static void acpi_sbs_rmdirs(void)
1747 acpi_unlock_ac_dir(acpi_ac_dir);
1750 if (acpi_battery_dir) {
1751 acpi_unlock_battery_dir(acpi_battery_dir);
1752 acpi_battery_dir = NULL;
1756 static int acpi_sbs_resume(struct acpi_device *device)
1758 struct acpi_sbs *sbs;
1763 sbs = device->driver_data;
1770 static int __init acpi_sbs_init(void)
1777 if (capacity_mode != DEF_CAPACITY_UNIT
1778 && capacity_mode != MAH_CAPACITY_UNIT
1779 && capacity_mode != MWH_CAPACITY_UNIT) {
1780 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1781 "invalid capacity_mode = %d", capacity_mode));
1785 acpi_ac_dir = acpi_lock_ac_dir();
1787 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1788 "acpi_lock_ac_dir() failed"));
1792 acpi_battery_dir = acpi_lock_battery_dir();
1793 if (!acpi_battery_dir) {
1794 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1795 "acpi_lock_battery_dir() failed"));
1800 result = acpi_bus_register_driver(&acpi_sbs_driver);
1802 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1803 "acpi_bus_register_driver() failed"));
1811 static void __exit acpi_sbs_exit(void)
1813 acpi_bus_unregister_driver(&acpi_sbs_driver);
1820 module_init(acpi_sbs_init);
1821 module_exit(acpi_sbs_exit);