RT-AC66 3.0.0.4.374.130 core
[tomato.git] / release / src-rt-6.x / linux / linux-2.6 / drivers / acpi / sbs.c
blobc1bae106833cef03d6f7faee83e5d42c9fda36a9
1 /*
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 = {
128 .name = "sbs",
129 .class = ACPI_SBS_CLASS,
130 .ids = ACPI_SBS_HID,
131 .ops = {
132 .add = acpi_sbs_add,
133 .remove = acpi_sbs_remove,
134 .resume = acpi_sbs_resume,
138 struct acpi_ac {
139 int ac_present;
142 struct acpi_battery_info {
143 int capacity_mode;
144 s16 full_charge_capacity;
145 s16 design_capacity;
146 s16 design_voltage;
147 int vscale;
148 int ipscale;
149 s16 serial_number;
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 {
156 s16 voltage;
157 s16 amperage;
158 s16 remaining_capacity;
159 s16 battery_state;
162 struct acpi_battery_alarm {
163 s16 remaining_capacity;
166 struct acpi_battery {
167 int alive;
168 int id;
169 int init_state;
170 int battery_present;
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;
178 struct acpi_sbs {
179 acpi_handle handle;
180 int base;
181 struct acpi_device *device;
182 struct acpi_ec_smbus *smbus;
183 struct mutex mutex;
184 int sbsm_present;
185 int sbsm_batteries_supported;
186 struct proc_dir_entry *ac_entry;
187 struct acpi_ac ac;
188 struct acpi_battery battery[MAX_SBS_BAT];
189 int zombie;
190 struct timer_list update_timer;
191 int run_cnt;
192 int update_proc_flg;
195 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
196 static void acpi_sbs_update_time(void *data);
198 union sbs_rw_data {
199 u16 word;
200 u8 block[ACPI_SBS_BLOCK_MAX + 2];
203 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
204 char read_write, u8 command, int size,
205 union sbs_rw_data *data);
207 /* --------------------------------------------------------------------------
208 SMBus Communication
209 -------------------------------------------------------------------------- */
211 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
213 u8 val;
214 int err;
216 err = ec_read(sbs->base + address, &val);
217 if (!err) {
218 *data = val;
220 xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
221 return (err);
224 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
226 int err;
228 err = ec_write(sbs->base + address, data);
229 return (err);
232 static int
233 acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
234 char read_write, u8 command, int size,
235 union sbs_rw_data *data)
237 unsigned char protocol, len = 0, temp[2] = { 0, 0 };
238 int i;
240 if (read_write == ACPI_SBS_SMBUS_READ) {
241 protocol = ACPI_EC_SMB_PRTCL_READ;
242 } else {
243 protocol = ACPI_EC_SMB_PRTCL_WRITE;
246 switch (size) {
248 case ACPI_SBS_WORD_DATA:
249 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
250 if (read_write == ACPI_SBS_SMBUS_WRITE) {
251 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word);
252 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1,
253 data->word >> 8);
255 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA;
256 break;
257 case ACPI_SBS_BLOCK_DATA:
258 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
259 if (read_write == ACPI_SBS_SMBUS_WRITE) {
260 len = min_t(u8, data->block[0], 32);
261 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len);
262 for (i = 0; i < len; i++)
263 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i,
264 data->block[i + 1]);
266 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA;
267 break;
268 default:
269 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
270 "unsupported transaction %d", size));
271 return (-1);
274 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1);
275 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol);
277 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
279 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
280 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP1);
281 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
283 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
284 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
285 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
287 if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
288 || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
289 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
290 "transaction %d error", size));
291 return (-1);
294 if (read_write == ACPI_SBS_SMBUS_WRITE) {
295 return (0);
298 switch (size) {
300 case ACPI_SBS_WORD_DATA:
301 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp);
302 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1);
303 data->word = (temp[1] << 8) | temp[0];
304 break;
306 case ACPI_SBS_BLOCK_DATA:
307 len = 0;
308 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len);
309 len = min_t(u8, len, 32);
310 for (i = 0; i < len; i++)
311 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i,
312 data->block + i + 1);
313 data->block[0] = len;
314 break;
315 default:
316 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
317 "unsupported transaction %d", size));
318 return (-1);
321 return (0);
324 static int
325 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
327 union sbs_rw_data data;
328 int result = 0;
330 result = acpi_ec_sbs_access(sbs, addr,
331 ACPI_SBS_SMBUS_READ, func,
332 ACPI_SBS_WORD_DATA, &data);
333 if (result) {
334 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
335 "acpi_ec_sbs_access() failed"));
336 } else {
337 *word = data.word;
340 return result;
343 static int
344 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
346 union sbs_rw_data data;
347 int result = 0;
349 result = acpi_ec_sbs_access(sbs, addr,
350 ACPI_SBS_SMBUS_READ, func,
351 ACPI_SBS_BLOCK_DATA, &data);
352 if (result) {
353 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
354 "acpi_ec_sbs_access() failed"));
355 } else {
356 strncpy(str, (const char *)data.block + 1, data.block[0]);
357 str[data.block[0]] = 0;
360 return result;
363 static int
364 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
366 union sbs_rw_data data;
367 int result = 0;
369 data.word = word;
371 result = acpi_ec_sbs_access(sbs, addr,
372 ACPI_SBS_SMBUS_WRITE, func,
373 ACPI_SBS_WORD_DATA, &data);
374 if (result) {
375 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
376 "acpi_ec_sbs_access() failed"));
379 return result;
382 static int sbs_zombie(struct acpi_sbs *sbs)
384 return (sbs->zombie);
387 static int sbs_mutex_lock(struct acpi_sbs *sbs)
389 if (sbs_zombie(sbs)) {
390 return -ENODEV;
392 mutex_lock(&sbs->mutex);
393 return 0;
396 static void sbs_mutex_unlock(struct acpi_sbs *sbs)
398 mutex_unlock(&sbs->mutex);
401 /* --------------------------------------------------------------------------
402 Smart Battery System Management
403 -------------------------------------------------------------------------- */
405 static int acpi_check_update_proc(struct acpi_sbs *sbs)
407 acpi_status status = AE_OK;
409 if (update_time == 0) {
410 sbs->update_proc_flg = 0;
411 return 0;
413 if (sbs->update_proc_flg == 0) {
414 status = acpi_os_execute(OSL_GPE_HANDLER,
415 acpi_sbs_update_time, sbs);
416 if (status != AE_OK) {
417 ACPI_EXCEPTION((AE_INFO, status,
418 "acpi_os_execute() failed"));
419 return 1;
421 sbs->update_proc_flg = 1;
423 return 0;
426 static int acpi_sbs_generate_event(struct acpi_device *device,
427 int event, int state, char *bid, char *class)
429 char bid_saved[5];
430 char class_saved[20];
431 int result = 0;
433 strcpy(bid_saved, acpi_device_bid(device));
434 strcpy(class_saved, acpi_device_class(device));
436 strcpy(acpi_device_bid(device), bid);
437 strcpy(acpi_device_class(device), class);
439 result = acpi_bus_generate_event(device, event, state);
441 strcpy(acpi_device_bid(device), bid_saved);
442 strcpy(acpi_device_class(device), class_saved);
444 return result;
447 static int acpi_battery_get_present(struct acpi_battery *battery)
449 s16 state;
450 int result = 0;
451 int is_present = 0;
453 result = acpi_sbs_read_word(battery->sbs,
454 ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
455 if (result) {
456 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
457 "acpi_sbs_read_word() failed"));
459 if (!result) {
460 is_present = (state & 0x000f) & (1 << battery->id);
462 battery->battery_present = is_present;
464 return result;
467 static int acpi_battery_select(struct acpi_battery *battery)
469 struct acpi_sbs *sbs = battery->sbs;
470 int result = 0;
471 s16 state;
472 int foo;
474 if (sbs->sbsm_present) {
476 /* Take special care not to knobble other nibbles of
477 * state (aka selector_state), since
478 * it causes charging to halt on SBSELs */
480 result =
481 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
482 if (result) {
483 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
484 "acpi_sbs_read_word() failed"));
485 goto end;
488 foo = (state & 0x0fff) | (1 << (battery->id + 12));
489 result =
490 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
491 if (result) {
492 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
493 "acpi_sbs_write_word() failed"));
494 goto end;
498 end:
499 return result;
502 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
504 int result = 0;
505 s16 battery_system_info;
507 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
508 &battery_system_info);
509 if (result) {
510 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
511 "acpi_sbs_read_word() failed"));
512 goto end;
515 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
517 end:
519 return result;
522 static int acpi_battery_get_info(struct acpi_battery *battery)
524 struct acpi_sbs *sbs = battery->sbs;
525 int result = 0;
526 s16 battery_mode;
527 s16 specification_info;
529 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
530 &battery_mode);
531 if (result) {
532 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
533 "acpi_sbs_read_word() failed"));
534 goto end;
536 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
538 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
539 &battery->info.full_charge_capacity);
540 if (result) {
541 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
542 "acpi_sbs_read_word() failed"));
543 goto end;
546 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
547 &battery->info.design_capacity);
549 if (result) {
550 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
551 "acpi_sbs_read_word() failed"));
552 goto end;
555 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
556 &battery->info.design_voltage);
557 if (result) {
558 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
559 "acpi_sbs_read_word() failed"));
560 goto end;
563 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
564 &specification_info);
565 if (result) {
566 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
567 "acpi_sbs_read_word() failed"));
568 goto end;
571 switch ((specification_info & 0x0f00) >> 8) {
572 case 1:
573 battery->info.vscale = 10;
574 break;
575 case 2:
576 battery->info.vscale = 100;
577 break;
578 case 3:
579 battery->info.vscale = 1000;
580 break;
581 default:
582 battery->info.vscale = 1;
585 switch ((specification_info & 0xf000) >> 12) {
586 case 1:
587 battery->info.ipscale = 10;
588 break;
589 case 2:
590 battery->info.ipscale = 100;
591 break;
592 case 3:
593 battery->info.ipscale = 1000;
594 break;
595 default:
596 battery->info.ipscale = 1;
599 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
600 &battery->info.serial_number);
601 if (result) {
602 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
603 "acpi_sbs_read_word() failed"));
604 goto end;
607 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
608 battery->info.manufacturer_name);
609 if (result) {
610 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
611 "acpi_sbs_read_str() failed"));
612 goto end;
615 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
616 battery->info.device_name);
617 if (result) {
618 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
619 "acpi_sbs_read_str() failed"));
620 goto end;
623 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
624 battery->info.device_chemistry);
625 if (result) {
626 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
627 "acpi_sbs_read_str() failed"));
628 goto end;
631 end:
632 return result;
635 static int acpi_battery_get_state(struct acpi_battery *battery)
637 struct acpi_sbs *sbs = battery->sbs;
638 int result = 0;
640 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
641 &battery->state.voltage);
642 if (result) {
643 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
644 "acpi_sbs_read_word() failed"));
645 goto end;
648 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
649 &battery->state.amperage);
650 if (result) {
651 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
652 "acpi_sbs_read_word() failed"));
653 goto end;
656 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
657 &battery->state.remaining_capacity);
658 if (result) {
659 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
660 "acpi_sbs_read_word() failed"));
661 goto end;
664 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
665 &battery->state.battery_state);
666 if (result) {
667 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
668 "acpi_sbs_read_word() failed"));
669 goto end;
672 end:
673 return result;
676 static int acpi_battery_get_alarm(struct acpi_battery *battery)
678 struct acpi_sbs *sbs = battery->sbs;
679 int result = 0;
681 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
682 &battery->alarm.remaining_capacity);
683 if (result) {
684 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
685 "acpi_sbs_read_word() failed"));
686 goto end;
689 end:
691 return result;
694 static int acpi_battery_set_alarm(struct acpi_battery *battery,
695 unsigned long alarm)
697 struct acpi_sbs *sbs = battery->sbs;
698 int result = 0;
699 s16 battery_mode;
700 int foo;
702 result = acpi_battery_select(battery);
703 if (result) {
704 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
705 "acpi_battery_select() failed"));
706 goto end;
709 /* If necessary, enable the alarm */
711 if (alarm > 0) {
712 result =
713 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
714 &battery_mode);
715 if (result) {
716 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
717 "acpi_sbs_read_word() failed"));
718 goto end;
721 result =
722 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
723 battery_mode & 0xbfff);
724 if (result) {
725 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
726 "acpi_sbs_write_word() failed"));
727 goto end;
731 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
732 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
733 if (result) {
734 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
735 "acpi_sbs_write_word() failed"));
736 goto end;
739 end:
741 return result;
744 static int acpi_battery_set_mode(struct acpi_battery *battery)
746 struct acpi_sbs *sbs = battery->sbs;
747 int result = 0;
748 s16 battery_mode;
750 if (capacity_mode == DEF_CAPACITY_UNIT) {
751 goto end;
754 result = acpi_sbs_read_word(sbs,
755 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
756 if (result) {
757 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
758 "acpi_sbs_read_word() failed"));
759 goto end;
762 if (capacity_mode == MAH_CAPACITY_UNIT) {
763 battery_mode &= 0x7fff;
764 } else {
765 battery_mode |= 0x8000;
767 result = acpi_sbs_write_word(sbs,
768 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
769 if (result) {
770 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
771 "acpi_sbs_write_word() failed"));
772 goto end;
775 result = acpi_sbs_read_word(sbs,
776 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
777 if (result) {
778 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
779 "acpi_sbs_read_word() failed"));
780 goto end;
783 end:
784 return result;
787 static int acpi_battery_init(struct acpi_battery *battery)
789 int result = 0;
791 result = acpi_battery_select(battery);
792 if (result) {
793 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
794 "acpi_battery_select() failed"));
795 goto end;
798 result = acpi_battery_set_mode(battery);
799 if (result) {
800 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
801 "acpi_battery_set_mode() failed"));
802 goto end;
805 result = acpi_battery_get_info(battery);
806 if (result) {
807 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
808 "acpi_battery_get_info() failed"));
809 goto end;
812 result = acpi_battery_get_state(battery);
813 if (result) {
814 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
815 "acpi_battery_get_state() failed"));
816 goto end;
819 result = acpi_battery_get_alarm(battery);
820 if (result) {
821 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
822 "acpi_battery_get_alarm() failed"));
823 goto end;
826 end:
827 return result;
830 static int acpi_ac_get_present(struct acpi_sbs *sbs)
832 int result = 0;
833 s16 charger_status;
835 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
836 &charger_status);
838 if (result) {
839 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
840 "acpi_sbs_read_word() failed"));
841 goto end;
844 sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
846 end:
848 return result;
851 /* --------------------------------------------------------------------------
852 FS Interface (/proc/acpi)
853 -------------------------------------------------------------------------- */
855 /* Generic Routines */
857 static int
858 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
859 struct proc_dir_entry *parent_dir,
860 char *dir_name,
861 struct file_operations *info_fops,
862 struct file_operations *state_fops,
863 struct file_operations *alarm_fops, void *data)
865 struct proc_dir_entry *entry = NULL;
867 if (!*dir) {
868 *dir = proc_mkdir(dir_name, parent_dir);
869 if (!*dir) {
870 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
871 "proc_mkdir() failed"));
872 return -ENODEV;
874 (*dir)->owner = THIS_MODULE;
877 /* 'info' [R] */
878 if (info_fops) {
879 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
880 if (!entry) {
881 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
882 "create_proc_entry() failed"));
883 } else {
884 entry->proc_fops = info_fops;
885 entry->data = data;
886 entry->owner = THIS_MODULE;
890 /* 'state' [R] */
891 if (state_fops) {
892 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
893 if (!entry) {
894 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
895 "create_proc_entry() failed"));
896 } else {
897 entry->proc_fops = state_fops;
898 entry->data = data;
899 entry->owner = THIS_MODULE;
903 /* 'alarm' [R/W] */
904 if (alarm_fops) {
905 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
906 if (!entry) {
907 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
908 "create_proc_entry() failed"));
909 } else {
910 entry->proc_fops = alarm_fops;
911 entry->data = data;
912 entry->owner = THIS_MODULE;
916 return 0;
919 static void
920 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
921 struct proc_dir_entry *parent_dir)
924 if (*dir) {
925 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
926 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
927 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
928 remove_proc_entry((*dir)->name, parent_dir);
929 *dir = NULL;
934 /* Smart Battery Interface */
936 static struct proc_dir_entry *acpi_battery_dir = NULL;
938 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
940 struct acpi_battery *battery = seq->private;
941 struct acpi_sbs *sbs = battery->sbs;
942 int cscale;
943 int result = 0;
945 if (sbs_mutex_lock(sbs)) {
946 return -ENODEV;
949 result = acpi_check_update_proc(sbs);
950 if (result)
951 goto end;
953 if (update_time == 0) {
954 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
955 if (result) {
956 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
957 "acpi_sbs_update_run() failed"));
961 if (battery->battery_present) {
962 seq_printf(seq, "present: yes\n");
963 } else {
964 seq_printf(seq, "present: no\n");
965 goto end;
968 if (battery->info.capacity_mode) {
969 cscale = battery->info.vscale * battery->info.ipscale;
970 } else {
971 cscale = battery->info.ipscale;
973 seq_printf(seq, "design capacity: %i%s\n",
974 battery->info.design_capacity * cscale,
975 battery->info.capacity_mode ? "0 mWh" : " mAh");
977 seq_printf(seq, "last full capacity: %i%s\n",
978 battery->info.full_charge_capacity * cscale,
979 battery->info.capacity_mode ? "0 mWh" : " mAh");
981 seq_printf(seq, "battery technology: rechargeable\n");
983 seq_printf(seq, "design voltage: %i mV\n",
984 battery->info.design_voltage * battery->info.vscale);
986 seq_printf(seq, "design capacity warning: unknown\n");
987 seq_printf(seq, "design capacity low: unknown\n");
988 seq_printf(seq, "capacity granularity 1: unknown\n");
989 seq_printf(seq, "capacity granularity 2: unknown\n");
991 seq_printf(seq, "model number: %s\n",
992 battery->info.device_name);
994 seq_printf(seq, "serial number: %i\n",
995 battery->info.serial_number);
997 seq_printf(seq, "battery type: %s\n",
998 battery->info.device_chemistry);
1000 seq_printf(seq, "OEM info: %s\n",
1001 battery->info.manufacturer_name);
1003 end:
1005 sbs_mutex_unlock(sbs);
1007 return result;
1010 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1012 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1015 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1017 struct acpi_battery *battery = seq->private;
1018 struct acpi_sbs *sbs = battery->sbs;
1019 int result = 0;
1020 int cscale;
1021 int foo;
1023 if (sbs_mutex_lock(sbs)) {
1024 return -ENODEV;
1027 result = acpi_check_update_proc(sbs);
1028 if (result)
1029 goto end;
1031 if (update_time == 0) {
1032 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1033 if (result) {
1034 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1035 "acpi_sbs_update_run() failed"));
1039 if (battery->battery_present) {
1040 seq_printf(seq, "present: yes\n");
1041 } else {
1042 seq_printf(seq, "present: no\n");
1043 goto end;
1046 if (battery->info.capacity_mode) {
1047 cscale = battery->info.vscale * battery->info.ipscale;
1048 } else {
1049 cscale = battery->info.ipscale;
1052 if (battery->state.battery_state & 0x0010) {
1053 seq_printf(seq, "capacity state: critical\n");
1054 } else {
1055 seq_printf(seq, "capacity state: ok\n");
1058 foo = (s16) battery->state.amperage * battery->info.ipscale;
1059 if (battery->info.capacity_mode) {
1060 foo = foo * battery->info.design_voltage / 1000;
1062 if (battery->state.amperage < 0) {
1063 seq_printf(seq, "charging state: discharging\n");
1064 seq_printf(seq, "present rate: %d %s\n",
1065 -foo, battery->info.capacity_mode ? "mW" : "mA");
1066 } else if (battery->state.amperage > 0) {
1067 seq_printf(seq, "charging state: charging\n");
1068 seq_printf(seq, "present rate: %d %s\n",
1069 foo, battery->info.capacity_mode ? "mW" : "mA");
1070 } else {
1071 seq_printf(seq, "charging state: charged\n");
1072 seq_printf(seq, "present rate: 0 %s\n",
1073 battery->info.capacity_mode ? "mW" : "mA");
1076 seq_printf(seq, "remaining capacity: %i%s\n",
1077 battery->state.remaining_capacity * cscale,
1078 battery->info.capacity_mode ? "0 mWh" : " mAh");
1080 seq_printf(seq, "present voltage: %i mV\n",
1081 battery->state.voltage * battery->info.vscale);
1083 end:
1085 sbs_mutex_unlock(sbs);
1087 return result;
1090 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1092 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1095 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1097 struct acpi_battery *battery = seq->private;
1098 struct acpi_sbs *sbs = battery->sbs;
1099 int result = 0;
1100 int cscale;
1102 if (sbs_mutex_lock(sbs)) {
1103 return -ENODEV;
1106 result = acpi_check_update_proc(sbs);
1107 if (result)
1108 goto end;
1110 if (update_time == 0) {
1111 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1112 if (result) {
1113 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1114 "acpi_sbs_update_run() failed"));
1118 if (!battery->battery_present) {
1119 seq_printf(seq, "present: no\n");
1120 goto end;
1123 if (battery->info.capacity_mode) {
1124 cscale = battery->info.vscale * battery->info.ipscale;
1125 } else {
1126 cscale = battery->info.ipscale;
1129 seq_printf(seq, "alarm: ");
1130 if (battery->alarm.remaining_capacity) {
1131 seq_printf(seq, "%i%s\n",
1132 battery->alarm.remaining_capacity * cscale,
1133 battery->info.capacity_mode ? "0 mWh" : " mAh");
1134 } else {
1135 seq_printf(seq, "disabled\n");
1138 end:
1140 sbs_mutex_unlock(sbs);
1142 return result;
1145 static ssize_t
1146 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1147 size_t count, loff_t * ppos)
1149 struct seq_file *seq = file->private_data;
1150 struct acpi_battery *battery = seq->private;
1151 struct acpi_sbs *sbs = battery->sbs;
1152 char alarm_string[12] = { '\0' };
1153 int result, old_alarm, new_alarm;
1155 if (sbs_mutex_lock(sbs)) {
1156 return -ENODEV;
1159 result = acpi_check_update_proc(sbs);
1160 if (result)
1161 goto end;
1163 if (!battery->battery_present) {
1164 result = -ENODEV;
1165 goto end;
1168 if (count > sizeof(alarm_string) - 1) {
1169 result = -EINVAL;
1170 goto end;
1173 if (copy_from_user(alarm_string, buffer, count)) {
1174 result = -EFAULT;
1175 goto end;
1178 alarm_string[count] = 0;
1180 old_alarm = battery->alarm.remaining_capacity;
1181 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1183 result = acpi_battery_set_alarm(battery, new_alarm);
1184 if (result) {
1185 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1186 "acpi_battery_set_alarm() failed"));
1187 acpi_battery_set_alarm(battery, old_alarm);
1188 goto end;
1190 result = acpi_battery_get_alarm(battery);
1191 if (result) {
1192 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1193 "acpi_battery_get_alarm() failed"));
1194 acpi_battery_set_alarm(battery, old_alarm);
1195 goto end;
1198 end:
1199 sbs_mutex_unlock(sbs);
1201 if (result) {
1202 return result;
1203 } else {
1204 return count;
1208 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1210 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1213 static struct file_operations acpi_battery_info_fops = {
1214 .open = acpi_battery_info_open_fs,
1215 .read = seq_read,
1216 .llseek = seq_lseek,
1217 .release = single_release,
1218 .owner = THIS_MODULE,
1221 static struct file_operations acpi_battery_state_fops = {
1222 .open = acpi_battery_state_open_fs,
1223 .read = seq_read,
1224 .llseek = seq_lseek,
1225 .release = single_release,
1226 .owner = THIS_MODULE,
1229 static struct file_operations acpi_battery_alarm_fops = {
1230 .open = acpi_battery_alarm_open_fs,
1231 .read = seq_read,
1232 .write = acpi_battery_write_alarm,
1233 .llseek = seq_lseek,
1234 .release = single_release,
1235 .owner = THIS_MODULE,
1238 /* Legacy AC Adapter Interface */
1240 static struct proc_dir_entry *acpi_ac_dir = NULL;
1242 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1244 struct acpi_sbs *sbs = seq->private;
1245 int result;
1247 if (sbs_mutex_lock(sbs)) {
1248 return -ENODEV;
1251 if (update_time == 0) {
1252 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1253 if (result) {
1254 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1255 "acpi_sbs_update_run() failed"));
1259 seq_printf(seq, "state: %s\n",
1260 sbs->ac.ac_present ? "on-line" : "off-line");
1262 sbs_mutex_unlock(sbs);
1264 return 0;
1267 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1269 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1272 static struct file_operations acpi_ac_state_fops = {
1273 .open = acpi_ac_state_open_fs,
1274 .read = seq_read,
1275 .llseek = seq_lseek,
1276 .release = single_release,
1277 .owner = THIS_MODULE,
1280 /* --------------------------------------------------------------------------
1281 Driver Interface
1282 -------------------------------------------------------------------------- */
1284 /* Smart Battery */
1286 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1288 int is_present;
1289 int result;
1290 char dir_name[32];
1291 struct acpi_battery *battery;
1293 battery = &sbs->battery[id];
1295 battery->alive = 0;
1297 battery->init_state = 0;
1298 battery->id = id;
1299 battery->sbs = sbs;
1301 result = acpi_battery_select(battery);
1302 if (result) {
1303 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1304 "acpi_battery_select() failed"));
1305 goto end;
1308 result = acpi_battery_get_present(battery);
1309 if (result) {
1310 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1311 "acpi_battery_get_present() failed"));
1312 goto end;
1315 is_present = battery->battery_present;
1317 if (is_present) {
1318 result = acpi_battery_init(battery);
1319 if (result) {
1320 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1321 "acpi_battery_init() failed"));
1322 goto end;
1324 battery->init_state = 1;
1327 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1329 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1330 acpi_battery_dir,
1331 dir_name,
1332 &acpi_battery_info_fops,
1333 &acpi_battery_state_fops,
1334 &acpi_battery_alarm_fops, battery);
1335 if (result) {
1336 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1337 "acpi_sbs_generic_add_fs() failed"));
1338 goto end;
1340 battery->alive = 1;
1342 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
1343 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1344 sbs->battery->battery_present ? "present" : "absent");
1346 end:
1347 return result;
1350 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1353 if (sbs->battery[id].battery_entry) {
1354 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1355 acpi_battery_dir);
1359 static int acpi_ac_add(struct acpi_sbs *sbs)
1361 int result;
1363 result = acpi_ac_get_present(sbs);
1364 if (result) {
1365 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1366 "acpi_ac_get_present() failed"));
1367 goto end;
1370 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1371 acpi_ac_dir,
1372 ACPI_AC_DIR_NAME,
1373 NULL, &acpi_ac_state_fops, NULL, sbs);
1374 if (result) {
1375 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1376 "acpi_sbs_generic_add_fs() failed"));
1377 goto end;
1380 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
1381 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1382 ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
1384 end:
1386 return result;
1389 static void acpi_ac_remove(struct acpi_sbs *sbs)
1392 if (sbs->ac_entry) {
1393 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1397 static void acpi_sbs_update_time_run(unsigned long data)
1399 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1402 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1404 struct acpi_battery *battery;
1405 int result = 0, cnt;
1406 int old_ac_present = -1;
1407 int old_battery_present = -1;
1408 int new_ac_present = -1;
1409 int new_battery_present = -1;
1410 int id_min = 0, id_max = MAX_SBS_BAT - 1;
1411 char dir_name[32];
1412 int do_battery_init = 0, do_ac_init = 0;
1413 int old_remaining_capacity = 0;
1414 int update_ac = 1, update_battery = 1;
1415 int up_tm = update_time;
1417 if (sbs_zombie(sbs)) {
1418 goto end;
1421 if (id >= 0) {
1422 id_min = id_max = id;
1425 if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
1426 cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1427 if (sbs->run_cnt % cnt != 0) {
1428 update_battery = 0;
1432 sbs->run_cnt++;
1434 if (!update_ac && !update_battery) {
1435 goto end;
1438 old_ac_present = sbs->ac.ac_present;
1440 result = acpi_ac_get_present(sbs);
1441 if (result) {
1442 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1443 "acpi_ac_get_present() failed"));
1446 new_ac_present = sbs->ac.ac_present;
1448 do_ac_init = (old_ac_present != new_ac_present);
1449 if (sbs->run_cnt == 1 && data_type == DATA_TYPE_COMMON) {
1450 do_ac_init = 1;
1453 if (do_ac_init) {
1454 result = acpi_sbs_generate_event(sbs->device,
1455 ACPI_SBS_AC_NOTIFY_STATUS,
1456 new_ac_present,
1457 ACPI_AC_DIR_NAME,
1458 ACPI_AC_CLASS);
1459 if (result) {
1460 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1461 "acpi_sbs_generate_event() failed"));
1465 if (data_type == DATA_TYPE_COMMON) {
1466 if (!do_ac_init && !update_battery) {
1467 goto end;
1471 if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1472 goto end;
1475 for (id = id_min; id <= id_max; id++) {
1476 battery = &sbs->battery[id];
1477 if (battery->alive == 0) {
1478 continue;
1481 old_remaining_capacity = battery->state.remaining_capacity;
1483 old_battery_present = battery->battery_present;
1485 result = acpi_battery_select(battery);
1486 if (result) {
1487 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1488 "acpi_battery_select() failed"));
1491 result = acpi_battery_get_present(battery);
1492 if (result) {
1493 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1494 "acpi_battery_get_present() failed"));
1497 new_battery_present = battery->battery_present;
1499 do_battery_init = ((old_battery_present != new_battery_present)
1500 && new_battery_present);
1501 if (!new_battery_present)
1502 goto event;
1503 if (do_ac_init || do_battery_init) {
1504 result = acpi_battery_init(battery);
1505 if (result) {
1506 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1507 "acpi_battery_init() "
1508 "failed"));
1511 if (sbs_zombie(sbs)) {
1512 goto end;
1515 if ((data_type == DATA_TYPE_COMMON
1516 || data_type == DATA_TYPE_INFO)
1517 && new_battery_present) {
1518 result = acpi_battery_get_info(battery);
1519 if (result) {
1520 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1521 "acpi_battery_get_info() failed"));
1524 if (data_type == DATA_TYPE_INFO) {
1525 continue;
1527 if (sbs_zombie(sbs)) {
1528 goto end;
1531 if ((data_type == DATA_TYPE_COMMON
1532 || data_type == DATA_TYPE_STATE)
1533 && new_battery_present) {
1534 result = acpi_battery_get_state(battery);
1535 if (result) {
1536 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1537 "acpi_battery_get_state() failed"));
1540 if (data_type == DATA_TYPE_STATE) {
1541 goto event;
1543 if (sbs_zombie(sbs)) {
1544 goto end;
1547 if ((data_type == DATA_TYPE_COMMON
1548 || data_type == DATA_TYPE_ALARM)
1549 && new_battery_present) {
1550 result = acpi_battery_get_alarm(battery);
1551 if (result) {
1552 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1553 "acpi_battery_get_alarm() "
1554 "failed"));
1557 if (data_type == DATA_TYPE_ALARM) {
1558 continue;
1560 if (sbs_zombie(sbs)) {
1561 goto end;
1564 event:
1566 if (old_battery_present != new_battery_present || do_ac_init ||
1567 old_remaining_capacity !=
1568 battery->state.remaining_capacity) {
1569 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1570 result = acpi_sbs_generate_event(sbs->device,
1571 ACPI_SBS_BATTERY_NOTIFY_STATUS,
1572 new_battery_present,
1573 dir_name,
1574 ACPI_BATTERY_CLASS);
1575 if (result) {
1576 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1577 "acpi_sbs_generate_event() "
1578 "failed"));
1583 end:
1585 return result;
1588 static void acpi_sbs_update_time(void *data)
1590 struct acpi_sbs *sbs = data;
1591 unsigned long delay = -1;
1592 int result;
1593 unsigned int up_tm = update_time;
1595 if (sbs_mutex_lock(sbs))
1596 return;
1598 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_COMMON);
1599 if (result) {
1600 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1601 "acpi_sbs_update_run() failed"));
1604 if (sbs_zombie(sbs)) {
1605 goto end;
1608 if (!up_tm) {
1609 if (timer_pending(&sbs->update_timer))
1610 del_timer(&sbs->update_timer);
1611 } else {
1612 delay = (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1613 delay = jiffies + HZ * delay;
1614 if (timer_pending(&sbs->update_timer)) {
1615 mod_timer(&sbs->update_timer, delay);
1616 } else {
1617 sbs->update_timer.data = (unsigned long)data;
1618 sbs->update_timer.function = acpi_sbs_update_time_run;
1619 sbs->update_timer.expires = delay;
1620 add_timer(&sbs->update_timer);
1624 end:
1626 sbs_mutex_unlock(sbs);
1629 static int acpi_sbs_add(struct acpi_device *device)
1631 struct acpi_sbs *sbs = NULL;
1632 int result = 0, remove_result = 0;
1633 unsigned long sbs_obj;
1634 int id;
1635 acpi_status status = AE_OK;
1636 unsigned long val;
1638 status =
1639 acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val);
1640 if (ACPI_FAILURE(status)) {
1641 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
1642 return -EIO;
1645 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1646 if (!sbs) {
1647 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
1648 result = -ENOMEM;
1649 goto end;
1652 mutex_init(&sbs->mutex);
1654 sbs_mutex_lock(sbs);
1656 sbs->base = (val & 0xff00ull) >> 8;
1657 sbs->device = device;
1659 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1660 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1661 acpi_driver_data(device) = sbs;
1663 result = acpi_ac_add(sbs);
1664 if (result) {
1665 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1666 goto end;
1668 status = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1669 if (status) {
1670 ACPI_EXCEPTION((AE_INFO, status,
1671 "acpi_evaluate_integer() failed"));
1672 result = -EIO;
1673 goto end;
1675 if (sbs_obj > 0) {
1676 result = acpi_sbsm_get_info(sbs);
1677 if (result) {
1678 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1679 "acpi_sbsm_get_info() failed"));
1680 goto end;
1682 sbs->sbsm_present = 1;
1685 if (sbs->sbsm_present == 0) {
1686 result = acpi_battery_add(sbs, 0);
1687 if (result) {
1688 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1689 "acpi_battery_add() failed"));
1690 goto end;
1692 } else {
1693 for (id = 0; id < MAX_SBS_BAT; id++) {
1694 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1695 result = acpi_battery_add(sbs, id);
1696 if (result) {
1697 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1698 "acpi_battery_add() failed"));
1699 goto end;
1705 sbs->handle = device->handle;
1707 init_timer(&sbs->update_timer);
1708 result = acpi_check_update_proc(sbs);
1709 if (result)
1710 goto end;
1712 end:
1714 sbs_mutex_unlock(sbs);
1716 if (result) {
1717 remove_result = acpi_sbs_remove(device, 0);
1718 if (remove_result) {
1719 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1720 "acpi_sbs_remove() failed"));
1724 return result;
1727 static int acpi_sbs_remove(struct acpi_device *device, int type)
1729 struct acpi_sbs *sbs;
1730 int id;
1732 if (!device) {
1733 return -EINVAL;
1736 sbs = acpi_driver_data(device);
1737 if (!sbs) {
1738 return -EINVAL;
1741 sbs_mutex_lock(sbs);
1743 sbs->zombie = 1;
1744 del_timer_sync(&sbs->update_timer);
1745 acpi_os_wait_events_complete(NULL);
1746 del_timer_sync(&sbs->update_timer);
1748 for (id = 0; id < MAX_SBS_BAT; id++) {
1749 acpi_battery_remove(sbs, id);
1752 acpi_ac_remove(sbs);
1754 sbs_mutex_unlock(sbs);
1756 mutex_destroy(&sbs->mutex);
1758 kfree(sbs);
1760 return 0;
1763 static void acpi_sbs_rmdirs(void)
1765 if (acpi_ac_dir) {
1766 acpi_unlock_ac_dir(acpi_ac_dir);
1767 acpi_ac_dir = NULL;
1769 if (acpi_battery_dir) {
1770 acpi_unlock_battery_dir(acpi_battery_dir);
1771 acpi_battery_dir = NULL;
1775 static int acpi_sbs_resume(struct acpi_device *device)
1777 struct acpi_sbs *sbs;
1779 if (!device)
1780 return -EINVAL;
1782 sbs = device->driver_data;
1784 sbs->run_cnt = 0;
1786 return 0;
1789 static int __init acpi_sbs_init(void)
1791 int result = 0;
1793 if (acpi_disabled)
1794 return -ENODEV;
1796 if (capacity_mode != DEF_CAPACITY_UNIT
1797 && capacity_mode != MAH_CAPACITY_UNIT
1798 && capacity_mode != MWH_CAPACITY_UNIT) {
1799 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1800 "invalid capacity_mode = %d", capacity_mode));
1801 return -EINVAL;
1804 acpi_ac_dir = acpi_lock_ac_dir();
1805 if (!acpi_ac_dir) {
1806 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1807 "acpi_lock_ac_dir() failed"));
1808 return -ENODEV;
1811 acpi_battery_dir = acpi_lock_battery_dir();
1812 if (!acpi_battery_dir) {
1813 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1814 "acpi_lock_battery_dir() failed"));
1815 acpi_sbs_rmdirs();
1816 return -ENODEV;
1819 result = acpi_bus_register_driver(&acpi_sbs_driver);
1820 if (result < 0) {
1821 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1822 "acpi_bus_register_driver() failed"));
1823 acpi_sbs_rmdirs();
1824 return -ENODEV;
1827 return 0;
1830 static void __exit acpi_sbs_exit(void)
1832 acpi_bus_unregister_driver(&acpi_sbs_driver);
1834 acpi_sbs_rmdirs();
1836 return;
1839 module_init(acpi_sbs_init);
1840 module_exit(acpi_sbs_exit);