ieee1394: sbp2: fix sbp2_remove_device for error cases
[usb.git] / drivers / acpi / sbs.c
blob82c3a550016d82068798351123f5d2b429ef64d4
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_DEVICE_NAME "Smart Battery System"
42 #define ACPI_SBS_FILE_INFO "info"
43 #define ACPI_SBS_FILE_STATE "state"
44 #define ACPI_SBS_FILE_ALARM "alarm"
45 #define ACPI_BATTERY_DIR_NAME "BAT%i"
46 #define ACPI_AC_DIR_NAME "AC0"
47 #define ACPI_SBC_SMBUS_ADDR 0x9
48 #define ACPI_SBSM_SMBUS_ADDR 0xa
49 #define ACPI_SB_SMBUS_ADDR 0xb
50 #define ACPI_SBS_AC_NOTIFY_STATUS 0x80
51 #define ACPI_SBS_BATTERY_NOTIFY_STATUS 0x80
52 #define ACPI_SBS_BATTERY_NOTIFY_INFO 0x81
54 #define _COMPONENT ACPI_SBS_COMPONENT
56 ACPI_MODULE_NAME("sbs");
58 MODULE_AUTHOR("Rich Townsend");
59 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
60 MODULE_LICENSE("GPL");
62 #define xmsleep(t) msleep(t)
64 #define ACPI_EC_SMB_PRTCL 0x00 /* protocol, PEC */
66 #define ACPI_EC_SMB_STS 0x01 /* status */
67 #define ACPI_EC_SMB_ADDR 0x02 /* address */
68 #define ACPI_EC_SMB_CMD 0x03 /* command */
69 #define ACPI_EC_SMB_DATA 0x04 /* 32 data registers */
70 #define ACPI_EC_SMB_BCNT 0x24 /* number of data bytes */
72 #define ACPI_EC_SMB_STS_DONE 0x80
73 #define ACPI_EC_SMB_STS_STATUS 0x1f
75 #define ACPI_EC_SMB_PRTCL_WRITE 0x00
76 #define ACPI_EC_SMB_PRTCL_READ 0x01
77 #define ACPI_EC_SMB_PRTCL_WORD_DATA 0x08
78 #define ACPI_EC_SMB_PRTCL_BLOCK_DATA 0x0a
80 #define ACPI_EC_SMB_TRANSACTION_SLEEP 1
81 #define ACPI_EC_SMB_ACCESS_SLEEP1 1
82 #define ACPI_EC_SMB_ACCESS_SLEEP2 10
84 #define DEF_CAPACITY_UNIT 3
85 #define MAH_CAPACITY_UNIT 1
86 #define MWH_CAPACITY_UNIT 2
87 #define CAPACITY_UNIT DEF_CAPACITY_UNIT
89 #define REQUEST_UPDATE_MODE 1
90 #define QUEUE_UPDATE_MODE 2
92 #define DATA_TYPE_COMMON 0
93 #define DATA_TYPE_INFO 1
94 #define DATA_TYPE_STATE 2
95 #define DATA_TYPE_ALARM 3
96 #define DATA_TYPE_AC_STATE 4
98 extern struct proc_dir_entry *acpi_lock_ac_dir(void);
99 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
100 extern void acpi_unlock_ac_dir(struct proc_dir_entry *acpi_ac_dir);
101 extern void acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
103 #define MAX_SBS_BAT 4
104 #define ACPI_SBS_BLOCK_MAX 32
106 #define ACPI_SBS_SMBUS_READ 1
107 #define ACPI_SBS_SMBUS_WRITE 2
109 #define ACPI_SBS_WORD_DATA 1
110 #define ACPI_SBS_BLOCK_DATA 2
112 #define UPDATE_DELAY 10
114 /* 0 - every time, > 0 - by update_time */
115 static unsigned int update_time = 120;
117 static unsigned int capacity_mode = CAPACITY_UNIT;
119 module_param(update_time, uint, 0644);
120 module_param(capacity_mode, uint, 0444);
122 static int acpi_sbs_add(struct acpi_device *device);
123 static int acpi_sbs_remove(struct acpi_device *device, int type);
124 static int acpi_sbs_resume(struct acpi_device *device);
126 static const struct acpi_device_id sbs_device_ids[] = {
127 {"ACPI0001", 0},
128 {"ACPI0005", 0},
129 {"", 0},
131 MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
133 static struct acpi_driver acpi_sbs_driver = {
134 .name = "sbs",
135 .class = ACPI_SBS_CLASS,
136 .ids = sbs_device_ids,
137 .ops = {
138 .add = acpi_sbs_add,
139 .remove = acpi_sbs_remove,
140 .resume = acpi_sbs_resume,
144 struct acpi_ac {
145 int ac_present;
148 struct acpi_battery_info {
149 int capacity_mode;
150 s16 full_charge_capacity;
151 s16 design_capacity;
152 s16 design_voltage;
153 int vscale;
154 int ipscale;
155 s16 serial_number;
156 char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
157 char device_name[ACPI_SBS_BLOCK_MAX + 3];
158 char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
161 struct acpi_battery_state {
162 s16 voltage;
163 s16 amperage;
164 s16 remaining_capacity;
165 s16 battery_state;
168 struct acpi_battery_alarm {
169 s16 remaining_capacity;
172 struct acpi_battery {
173 int alive;
174 int id;
175 int init_state;
176 int battery_present;
177 struct acpi_sbs *sbs;
178 struct acpi_battery_info info;
179 struct acpi_battery_state state;
180 struct acpi_battery_alarm alarm;
181 struct proc_dir_entry *battery_entry;
184 struct acpi_sbs {
185 int base;
186 struct acpi_device *device;
187 struct mutex mutex;
188 int sbsm_present;
189 int sbsm_batteries_supported;
190 struct proc_dir_entry *ac_entry;
191 struct acpi_ac ac;
192 struct acpi_battery battery[MAX_SBS_BAT];
193 int zombie;
194 struct timer_list update_timer;
195 int run_cnt;
196 int update_proc_flg;
199 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type);
200 static void acpi_sbs_update_time(void *data);
202 union sbs_rw_data {
203 u16 word;
204 u8 block[ACPI_SBS_BLOCK_MAX + 2];
207 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
208 char read_write, u8 command, int size,
209 union sbs_rw_data *data);
211 /* --------------------------------------------------------------------------
212 SMBus Communication
213 -------------------------------------------------------------------------- */
215 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
217 u8 val;
218 int err;
220 err = ec_read(sbs->base + address, &val);
221 if (!err) {
222 *data = val;
224 xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
225 return (err);
228 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
230 int err;
232 err = ec_write(sbs->base + address, data);
233 return (err);
236 static int
237 acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
238 char read_write, u8 command, int size,
239 union sbs_rw_data *data)
241 unsigned char protocol, len = 0, temp[2] = { 0, 0 };
242 int i;
244 if (read_write == ACPI_SBS_SMBUS_READ) {
245 protocol = ACPI_EC_SMB_PRTCL_READ;
246 } else {
247 protocol = ACPI_EC_SMB_PRTCL_WRITE;
250 switch (size) {
252 case ACPI_SBS_WORD_DATA:
253 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
254 if (read_write == ACPI_SBS_SMBUS_WRITE) {
255 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA, data->word);
256 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + 1,
257 data->word >> 8);
259 protocol |= ACPI_EC_SMB_PRTCL_WORD_DATA;
260 break;
261 case ACPI_SBS_BLOCK_DATA:
262 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_CMD, command);
263 if (read_write == ACPI_SBS_SMBUS_WRITE) {
264 len = min_t(u8, data->block[0], 32);
265 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_BCNT, len);
266 for (i = 0; i < len; i++)
267 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_DATA + i,
268 data->block[i + 1]);
270 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA;
271 break;
272 default:
273 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
274 "unsupported transaction %d", size));
275 return (-1);
278 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1);
279 acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol);
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_SLEEP1);
285 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
287 if (~temp[0] & ACPI_EC_SMB_STS_DONE) {
288 xmsleep(ACPI_EC_SMB_ACCESS_SLEEP2);
289 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
291 if ((~temp[0] & ACPI_EC_SMB_STS_DONE)
292 || (temp[0] & ACPI_EC_SMB_STS_STATUS)) {
293 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
294 "transaction %d error", size));
295 return (-1);
298 if (read_write == ACPI_SBS_SMBUS_WRITE) {
299 return (0);
302 switch (size) {
304 case ACPI_SBS_WORD_DATA:
305 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA, temp);
306 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + 1, temp + 1);
307 data->word = (temp[1] << 8) | temp[0];
308 break;
310 case ACPI_SBS_BLOCK_DATA:
311 len = 0;
312 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_BCNT, &len);
313 len = min_t(u8, len, 32);
314 for (i = 0; i < len; i++)
315 acpi_ec_sbs_read(sbs, ACPI_EC_SMB_DATA + i,
316 data->block + i + 1);
317 data->block[0] = len;
318 break;
319 default:
320 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
321 "unsupported transaction %d", size));
322 return (-1);
325 return (0);
328 static int
329 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
331 union sbs_rw_data data;
332 int result = 0;
334 result = acpi_ec_sbs_access(sbs, addr,
335 ACPI_SBS_SMBUS_READ, func,
336 ACPI_SBS_WORD_DATA, &data);
337 if (result) {
338 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
339 "acpi_ec_sbs_access() failed"));
340 } else {
341 *word = data.word;
344 return result;
347 static int
348 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
350 union sbs_rw_data data;
351 int result = 0;
353 result = acpi_ec_sbs_access(sbs, addr,
354 ACPI_SBS_SMBUS_READ, func,
355 ACPI_SBS_BLOCK_DATA, &data);
356 if (result) {
357 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
358 "acpi_ec_sbs_access() failed"));
359 } else {
360 strncpy(str, (const char *)data.block + 1, data.block[0]);
361 str[data.block[0]] = 0;
364 return result;
367 static int
368 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
370 union sbs_rw_data data;
371 int result = 0;
373 data.word = word;
375 result = acpi_ec_sbs_access(sbs, addr,
376 ACPI_SBS_SMBUS_WRITE, func,
377 ACPI_SBS_WORD_DATA, &data);
378 if (result) {
379 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
380 "acpi_ec_sbs_access() failed"));
383 return result;
386 static int sbs_zombie(struct acpi_sbs *sbs)
388 return (sbs->zombie);
391 static int sbs_mutex_lock(struct acpi_sbs *sbs)
393 if (sbs_zombie(sbs)) {
394 return -ENODEV;
396 mutex_lock(&sbs->mutex);
397 return 0;
400 static void sbs_mutex_unlock(struct acpi_sbs *sbs)
402 mutex_unlock(&sbs->mutex);
405 /* --------------------------------------------------------------------------
406 Smart Battery System Management
407 -------------------------------------------------------------------------- */
409 static int acpi_check_update_proc(struct acpi_sbs *sbs)
411 acpi_status status = AE_OK;
413 if (update_time == 0) {
414 sbs->update_proc_flg = 0;
415 return 0;
417 if (sbs->update_proc_flg == 0) {
418 status = acpi_os_execute(OSL_GPE_HANDLER,
419 acpi_sbs_update_time, sbs);
420 if (status != AE_OK) {
421 ACPI_EXCEPTION((AE_INFO, status,
422 "acpi_os_execute() failed"));
423 return 1;
425 sbs->update_proc_flg = 1;
427 return 0;
430 static int acpi_sbs_generate_event(struct acpi_device *device,
431 int event, int state, char *bid, char *class)
433 char bid_saved[5];
434 char class_saved[20];
435 int result = 0;
437 strcpy(bid_saved, acpi_device_bid(device));
438 strcpy(class_saved, acpi_device_class(device));
440 strcpy(acpi_device_bid(device), bid);
441 strcpy(acpi_device_class(device), class);
443 result = acpi_bus_generate_event(device, event, state);
445 strcpy(acpi_device_bid(device), bid_saved);
446 strcpy(acpi_device_class(device), class_saved);
448 return result;
451 static int acpi_battery_get_present(struct acpi_battery *battery)
453 s16 state;
454 int result = 0;
455 int is_present = 0;
457 result = acpi_sbs_read_word(battery->sbs,
458 ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
459 if (result) {
460 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
461 "acpi_sbs_read_word() failed"));
463 if (!result) {
464 is_present = (state & 0x000f) & (1 << battery->id);
466 battery->battery_present = is_present;
468 return result;
471 static int acpi_battery_select(struct acpi_battery *battery)
473 struct acpi_sbs *sbs = battery->sbs;
474 int result = 0;
475 s16 state;
476 int foo;
478 if (sbs->sbsm_present) {
480 /* Take special care not to knobble other nibbles of
481 * state (aka selector_state), since
482 * it causes charging to halt on SBSELs */
484 result =
485 acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
486 if (result) {
487 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
488 "acpi_sbs_read_word() failed"));
489 goto end;
492 foo = (state & 0x0fff) | (1 << (battery->id + 12));
493 result =
494 acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
495 if (result) {
496 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
497 "acpi_sbs_write_word() failed"));
498 goto end;
502 end:
503 return result;
506 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
508 int result = 0;
509 s16 battery_system_info;
511 result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
512 &battery_system_info);
513 if (result) {
514 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
515 "acpi_sbs_read_word() failed"));
516 goto end;
518 sbs->sbsm_present = 1;
519 sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
521 end:
523 return result;
526 static int acpi_battery_get_info(struct acpi_battery *battery)
528 struct acpi_sbs *sbs = battery->sbs;
529 int result = 0;
530 s16 battery_mode;
531 s16 specification_info;
533 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
534 &battery_mode);
535 if (result) {
536 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
537 "acpi_sbs_read_word() failed"));
538 goto end;
540 battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
542 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
543 &battery->info.full_charge_capacity);
544 if (result) {
545 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
546 "acpi_sbs_read_word() failed"));
547 goto end;
550 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
551 &battery->info.design_capacity);
553 if (result) {
554 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
555 "acpi_sbs_read_word() failed"));
556 goto end;
559 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
560 &battery->info.design_voltage);
561 if (result) {
562 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
563 "acpi_sbs_read_word() failed"));
564 goto end;
567 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
568 &specification_info);
569 if (result) {
570 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
571 "acpi_sbs_read_word() failed"));
572 goto end;
575 switch ((specification_info & 0x0f00) >> 8) {
576 case 1:
577 battery->info.vscale = 10;
578 break;
579 case 2:
580 battery->info.vscale = 100;
581 break;
582 case 3:
583 battery->info.vscale = 1000;
584 break;
585 default:
586 battery->info.vscale = 1;
589 switch ((specification_info & 0xf000) >> 12) {
590 case 1:
591 battery->info.ipscale = 10;
592 break;
593 case 2:
594 battery->info.ipscale = 100;
595 break;
596 case 3:
597 battery->info.ipscale = 1000;
598 break;
599 default:
600 battery->info.ipscale = 1;
603 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
604 &battery->info.serial_number);
605 if (result) {
606 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
607 "acpi_sbs_read_word() failed"));
608 goto end;
611 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
612 battery->info.manufacturer_name);
613 if (result) {
614 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
615 "acpi_sbs_read_str() failed"));
616 goto end;
619 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
620 battery->info.device_name);
621 if (result) {
622 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
623 "acpi_sbs_read_str() failed"));
624 goto end;
627 result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
628 battery->info.device_chemistry);
629 if (result) {
630 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
631 "acpi_sbs_read_str() failed"));
632 goto end;
635 end:
636 return result;
639 static int acpi_battery_get_state(struct acpi_battery *battery)
641 struct acpi_sbs *sbs = battery->sbs;
642 int result = 0;
644 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
645 &battery->state.voltage);
646 if (result) {
647 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
648 "acpi_sbs_read_word() failed"));
649 goto end;
652 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
653 &battery->state.amperage);
654 if (result) {
655 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
656 "acpi_sbs_read_word() failed"));
657 goto end;
660 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
661 &battery->state.remaining_capacity);
662 if (result) {
663 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
664 "acpi_sbs_read_word() failed"));
665 goto end;
668 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
669 &battery->state.battery_state);
670 if (result) {
671 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
672 "acpi_sbs_read_word() failed"));
673 goto end;
676 end:
677 return result;
680 static int acpi_battery_get_alarm(struct acpi_battery *battery)
682 struct acpi_sbs *sbs = battery->sbs;
683 int result = 0;
685 result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
686 &battery->alarm.remaining_capacity);
687 if (result) {
688 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
689 "acpi_sbs_read_word() failed"));
690 goto end;
693 end:
695 return result;
698 static int acpi_battery_set_alarm(struct acpi_battery *battery,
699 unsigned long alarm)
701 struct acpi_sbs *sbs = battery->sbs;
702 int result = 0;
703 s16 battery_mode;
704 int foo;
706 result = acpi_battery_select(battery);
707 if (result) {
708 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
709 "acpi_battery_select() failed"));
710 goto end;
713 /* If necessary, enable the alarm */
715 if (alarm > 0) {
716 result =
717 acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
718 &battery_mode);
719 if (result) {
720 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
721 "acpi_sbs_read_word() failed"));
722 goto end;
725 result =
726 acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
727 battery_mode & 0xbfff);
728 if (result) {
729 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
730 "acpi_sbs_write_word() failed"));
731 goto end;
735 foo = alarm / (battery->info.capacity_mode ? 10 : 1);
736 result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
737 if (result) {
738 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
739 "acpi_sbs_write_word() failed"));
740 goto end;
743 end:
745 return result;
748 static int acpi_battery_set_mode(struct acpi_battery *battery)
750 struct acpi_sbs *sbs = battery->sbs;
751 int result = 0;
752 s16 battery_mode;
754 if (capacity_mode == DEF_CAPACITY_UNIT) {
755 goto end;
758 result = acpi_sbs_read_word(sbs,
759 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
760 if (result) {
761 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
762 "acpi_sbs_read_word() failed"));
763 goto end;
766 if (capacity_mode == MAH_CAPACITY_UNIT) {
767 battery_mode &= 0x7fff;
768 } else {
769 battery_mode |= 0x8000;
771 result = acpi_sbs_write_word(sbs,
772 ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
773 if (result) {
774 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
775 "acpi_sbs_write_word() failed"));
776 goto end;
779 result = acpi_sbs_read_word(sbs,
780 ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
781 if (result) {
782 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
783 "acpi_sbs_read_word() failed"));
784 goto end;
787 end:
788 return result;
791 static int acpi_battery_init(struct acpi_battery *battery)
793 int result = 0;
795 result = acpi_battery_select(battery);
796 if (result) {
797 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
798 "acpi_battery_select() failed"));
799 goto end;
802 result = acpi_battery_set_mode(battery);
803 if (result) {
804 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
805 "acpi_battery_set_mode() failed"));
806 goto end;
809 result = acpi_battery_get_info(battery);
810 if (result) {
811 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
812 "acpi_battery_get_info() failed"));
813 goto end;
816 result = acpi_battery_get_state(battery);
817 if (result) {
818 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
819 "acpi_battery_get_state() failed"));
820 goto end;
823 result = acpi_battery_get_alarm(battery);
824 if (result) {
825 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
826 "acpi_battery_get_alarm() failed"));
827 goto end;
830 end:
831 return result;
834 static int acpi_ac_get_present(struct acpi_sbs *sbs)
836 int result = 0;
837 s16 charger_status;
839 result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
840 &charger_status);
842 if (result) {
843 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
844 "acpi_sbs_read_word() failed"));
845 goto end;
848 sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
850 end:
852 return result;
855 /* --------------------------------------------------------------------------
856 FS Interface (/proc/acpi)
857 -------------------------------------------------------------------------- */
859 /* Generic Routines */
861 static int
862 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
863 struct proc_dir_entry *parent_dir,
864 char *dir_name,
865 struct file_operations *info_fops,
866 struct file_operations *state_fops,
867 struct file_operations *alarm_fops, void *data)
869 struct proc_dir_entry *entry = NULL;
871 if (!*dir) {
872 *dir = proc_mkdir(dir_name, parent_dir);
873 if (!*dir) {
874 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
875 "proc_mkdir() failed"));
876 return -ENODEV;
878 (*dir)->owner = THIS_MODULE;
881 /* 'info' [R] */
882 if (info_fops) {
883 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
884 if (!entry) {
885 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
886 "create_proc_entry() failed"));
887 } else {
888 entry->proc_fops = info_fops;
889 entry->data = data;
890 entry->owner = THIS_MODULE;
894 /* 'state' [R] */
895 if (state_fops) {
896 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
897 if (!entry) {
898 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
899 "create_proc_entry() failed"));
900 } else {
901 entry->proc_fops = state_fops;
902 entry->data = data;
903 entry->owner = THIS_MODULE;
907 /* 'alarm' [R/W] */
908 if (alarm_fops) {
909 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
910 if (!entry) {
911 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
912 "create_proc_entry() failed"));
913 } else {
914 entry->proc_fops = alarm_fops;
915 entry->data = data;
916 entry->owner = THIS_MODULE;
920 return 0;
923 static void
924 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
925 struct proc_dir_entry *parent_dir)
928 if (*dir) {
929 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
930 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
931 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
932 remove_proc_entry((*dir)->name, parent_dir);
933 *dir = NULL;
938 /* Smart Battery Interface */
940 static struct proc_dir_entry *acpi_battery_dir = NULL;
942 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
944 struct acpi_battery *battery = seq->private;
945 struct acpi_sbs *sbs = battery->sbs;
946 int cscale;
947 int result = 0;
949 if (sbs_mutex_lock(sbs)) {
950 return -ENODEV;
953 result = acpi_check_update_proc(sbs);
954 if (result)
955 goto end;
957 if (update_time == 0) {
958 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_INFO);
959 if (result) {
960 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
961 "acpi_sbs_update_run() failed"));
965 if (battery->battery_present) {
966 seq_printf(seq, "present: yes\n");
967 } else {
968 seq_printf(seq, "present: no\n");
969 goto end;
972 if (battery->info.capacity_mode) {
973 cscale = battery->info.vscale * battery->info.ipscale;
974 } else {
975 cscale = battery->info.ipscale;
977 seq_printf(seq, "design capacity: %i%s\n",
978 battery->info.design_capacity * cscale,
979 battery->info.capacity_mode ? "0 mWh" : " mAh");
981 seq_printf(seq, "last full capacity: %i%s\n",
982 battery->info.full_charge_capacity * cscale,
983 battery->info.capacity_mode ? "0 mWh" : " mAh");
985 seq_printf(seq, "battery technology: rechargeable\n");
987 seq_printf(seq, "design voltage: %i mV\n",
988 battery->info.design_voltage * battery->info.vscale);
990 seq_printf(seq, "design capacity warning: unknown\n");
991 seq_printf(seq, "design capacity low: unknown\n");
992 seq_printf(seq, "capacity granularity 1: unknown\n");
993 seq_printf(seq, "capacity granularity 2: unknown\n");
995 seq_printf(seq, "model number: %s\n",
996 battery->info.device_name);
998 seq_printf(seq, "serial number: %i\n",
999 battery->info.serial_number);
1001 seq_printf(seq, "battery type: %s\n",
1002 battery->info.device_chemistry);
1004 seq_printf(seq, "OEM info: %s\n",
1005 battery->info.manufacturer_name);
1007 end:
1009 sbs_mutex_unlock(sbs);
1011 return result;
1014 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1016 return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1019 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1021 struct acpi_battery *battery = seq->private;
1022 struct acpi_sbs *sbs = battery->sbs;
1023 int result = 0;
1024 int cscale;
1025 int foo;
1027 if (sbs_mutex_lock(sbs)) {
1028 return -ENODEV;
1031 result = acpi_check_update_proc(sbs);
1032 if (result)
1033 goto end;
1035 if (update_time == 0) {
1036 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_STATE);
1037 if (result) {
1038 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1039 "acpi_sbs_update_run() failed"));
1043 if (battery->battery_present) {
1044 seq_printf(seq, "present: yes\n");
1045 } else {
1046 seq_printf(seq, "present: no\n");
1047 goto end;
1050 if (battery->info.capacity_mode) {
1051 cscale = battery->info.vscale * battery->info.ipscale;
1052 } else {
1053 cscale = battery->info.ipscale;
1056 if (battery->state.battery_state & 0x0010) {
1057 seq_printf(seq, "capacity state: critical\n");
1058 } else {
1059 seq_printf(seq, "capacity state: ok\n");
1062 foo = (s16) battery->state.amperage * battery->info.ipscale;
1063 if (battery->info.capacity_mode) {
1064 foo = foo * battery->info.design_voltage / 1000;
1066 if (battery->state.amperage < 0) {
1067 seq_printf(seq, "charging state: discharging\n");
1068 seq_printf(seq, "present rate: %d %s\n",
1069 -foo, battery->info.capacity_mode ? "mW" : "mA");
1070 } else if (battery->state.amperage > 0) {
1071 seq_printf(seq, "charging state: charging\n");
1072 seq_printf(seq, "present rate: %d %s\n",
1073 foo, battery->info.capacity_mode ? "mW" : "mA");
1074 } else {
1075 seq_printf(seq, "charging state: charged\n");
1076 seq_printf(seq, "present rate: 0 %s\n",
1077 battery->info.capacity_mode ? "mW" : "mA");
1080 seq_printf(seq, "remaining capacity: %i%s\n",
1081 battery->state.remaining_capacity * cscale,
1082 battery->info.capacity_mode ? "0 mWh" : " mAh");
1084 seq_printf(seq, "present voltage: %i mV\n",
1085 battery->state.voltage * battery->info.vscale);
1087 end:
1089 sbs_mutex_unlock(sbs);
1091 return result;
1094 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1096 return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1099 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1101 struct acpi_battery *battery = seq->private;
1102 struct acpi_sbs *sbs = battery->sbs;
1103 int result = 0;
1104 int cscale;
1106 if (sbs_mutex_lock(sbs)) {
1107 return -ENODEV;
1110 result = acpi_check_update_proc(sbs);
1111 if (result)
1112 goto end;
1114 if (update_time == 0) {
1115 result = acpi_sbs_update_run(sbs, battery->id, DATA_TYPE_ALARM);
1116 if (result) {
1117 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1118 "acpi_sbs_update_run() failed"));
1122 if (!battery->battery_present) {
1123 seq_printf(seq, "present: no\n");
1124 goto end;
1127 if (battery->info.capacity_mode) {
1128 cscale = battery->info.vscale * battery->info.ipscale;
1129 } else {
1130 cscale = battery->info.ipscale;
1133 seq_printf(seq, "alarm: ");
1134 if (battery->alarm.remaining_capacity) {
1135 seq_printf(seq, "%i%s\n",
1136 battery->alarm.remaining_capacity * cscale,
1137 battery->info.capacity_mode ? "0 mWh" : " mAh");
1138 } else {
1139 seq_printf(seq, "disabled\n");
1142 end:
1144 sbs_mutex_unlock(sbs);
1146 return result;
1149 static ssize_t
1150 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1151 size_t count, loff_t * ppos)
1153 struct seq_file *seq = file->private_data;
1154 struct acpi_battery *battery = seq->private;
1155 struct acpi_sbs *sbs = battery->sbs;
1156 char alarm_string[12] = { '\0' };
1157 int result, old_alarm, new_alarm;
1159 if (sbs_mutex_lock(sbs)) {
1160 return -ENODEV;
1163 result = acpi_check_update_proc(sbs);
1164 if (result)
1165 goto end;
1167 if (!battery->battery_present) {
1168 result = -ENODEV;
1169 goto end;
1172 if (count > sizeof(alarm_string) - 1) {
1173 result = -EINVAL;
1174 goto end;
1177 if (copy_from_user(alarm_string, buffer, count)) {
1178 result = -EFAULT;
1179 goto end;
1182 alarm_string[count] = 0;
1184 old_alarm = battery->alarm.remaining_capacity;
1185 new_alarm = simple_strtoul(alarm_string, NULL, 0);
1187 result = acpi_battery_set_alarm(battery, new_alarm);
1188 if (result) {
1189 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1190 "acpi_battery_set_alarm() failed"));
1191 acpi_battery_set_alarm(battery, old_alarm);
1192 goto end;
1194 result = acpi_battery_get_alarm(battery);
1195 if (result) {
1196 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1197 "acpi_battery_get_alarm() failed"));
1198 acpi_battery_set_alarm(battery, old_alarm);
1199 goto end;
1202 end:
1203 sbs_mutex_unlock(sbs);
1205 if (result) {
1206 return result;
1207 } else {
1208 return count;
1212 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1214 return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1217 static struct file_operations acpi_battery_info_fops = {
1218 .open = acpi_battery_info_open_fs,
1219 .read = seq_read,
1220 .llseek = seq_lseek,
1221 .release = single_release,
1222 .owner = THIS_MODULE,
1225 static struct file_operations acpi_battery_state_fops = {
1226 .open = acpi_battery_state_open_fs,
1227 .read = seq_read,
1228 .llseek = seq_lseek,
1229 .release = single_release,
1230 .owner = THIS_MODULE,
1233 static struct file_operations acpi_battery_alarm_fops = {
1234 .open = acpi_battery_alarm_open_fs,
1235 .read = seq_read,
1236 .write = acpi_battery_write_alarm,
1237 .llseek = seq_lseek,
1238 .release = single_release,
1239 .owner = THIS_MODULE,
1242 /* Legacy AC Adapter Interface */
1244 static struct proc_dir_entry *acpi_ac_dir = NULL;
1246 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1248 struct acpi_sbs *sbs = seq->private;
1249 int result;
1251 if (sbs_mutex_lock(sbs)) {
1252 return -ENODEV;
1255 if (update_time == 0) {
1256 result = acpi_sbs_update_run(sbs, -1, DATA_TYPE_AC_STATE);
1257 if (result) {
1258 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1259 "acpi_sbs_update_run() failed"));
1263 seq_printf(seq, "state: %s\n",
1264 sbs->ac.ac_present ? "on-line" : "off-line");
1266 sbs_mutex_unlock(sbs);
1268 return 0;
1271 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1273 return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1276 static struct file_operations acpi_ac_state_fops = {
1277 .open = acpi_ac_state_open_fs,
1278 .read = seq_read,
1279 .llseek = seq_lseek,
1280 .release = single_release,
1281 .owner = THIS_MODULE,
1284 /* --------------------------------------------------------------------------
1285 Driver Interface
1286 -------------------------------------------------------------------------- */
1288 /* Smart Battery */
1290 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1292 int is_present;
1293 int result;
1294 char dir_name[32];
1295 struct acpi_battery *battery;
1297 battery = &sbs->battery[id];
1299 battery->alive = 0;
1301 battery->init_state = 0;
1302 battery->id = id;
1303 battery->sbs = sbs;
1305 result = acpi_battery_select(battery);
1306 if (result) {
1307 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1308 "acpi_battery_select() failed"));
1309 goto end;
1312 result = acpi_battery_get_present(battery);
1313 if (result) {
1314 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1315 "acpi_battery_get_present() failed"));
1316 goto end;
1319 is_present = battery->battery_present;
1321 if (is_present) {
1322 result = acpi_battery_init(battery);
1323 if (result) {
1324 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1325 "acpi_battery_init() failed"));
1326 goto end;
1328 battery->init_state = 1;
1331 sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1333 result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1334 acpi_battery_dir,
1335 dir_name,
1336 &acpi_battery_info_fops,
1337 &acpi_battery_state_fops,
1338 &acpi_battery_alarm_fops, battery);
1339 if (result) {
1340 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1341 "acpi_sbs_generic_add_fs() failed"));
1342 goto end;
1344 battery->alive = 1;
1346 printk(KERN_INFO PREFIX "%s [%s]: Battery Slot [%s] (battery %s)\n",
1347 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device), dir_name,
1348 sbs->battery->battery_present ? "present" : "absent");
1350 end:
1351 return result;
1354 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1357 if (sbs->battery[id].battery_entry) {
1358 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1359 acpi_battery_dir);
1363 static int acpi_ac_add(struct acpi_sbs *sbs)
1365 int result;
1367 result = acpi_ac_get_present(sbs);
1368 if (result) {
1369 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1370 "acpi_ac_get_present() failed"));
1371 goto end;
1374 result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1375 acpi_ac_dir,
1376 ACPI_AC_DIR_NAME,
1377 NULL, &acpi_ac_state_fops, NULL, sbs);
1378 if (result) {
1379 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1380 "acpi_sbs_generic_add_fs() failed"));
1381 goto end;
1384 printk(KERN_INFO PREFIX "%s [%s]: AC Adapter [%s] (%s)\n",
1385 ACPI_SBS_DEVICE_NAME, acpi_device_bid(sbs->device),
1386 ACPI_AC_DIR_NAME, sbs->ac.ac_present ? "on-line" : "off-line");
1388 end:
1390 return result;
1393 static void acpi_ac_remove(struct acpi_sbs *sbs)
1396 if (sbs->ac_entry) {
1397 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1401 static void acpi_sbs_update_time_run(unsigned long data)
1403 acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1406 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1408 struct acpi_battery *battery;
1409 int result = 0, cnt;
1410 int old_ac_present = -1;
1411 int old_battery_present = -1;
1412 int new_ac_present = -1;
1413 int new_battery_present = -1;
1414 int id_min = 0, id_max = MAX_SBS_BAT - 1;
1415 char dir_name[32];
1416 int do_battery_init = 0, do_ac_init = 0;
1417 int old_remaining_capacity = 0;
1418 int update_battery = 1;
1419 int up_tm = update_time;
1421 if (sbs_zombie(sbs)) {
1422 goto end;
1425 if (id >= 0) {
1426 id_min = id_max = id;
1429 if (data_type == DATA_TYPE_COMMON && up_tm > 0) {
1430 cnt = up_tm / (up_tm > UPDATE_DELAY ? UPDATE_DELAY : up_tm);
1431 if (sbs->run_cnt % cnt != 0) {
1432 update_battery = 0;
1436 sbs->run_cnt++;
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 int id;
1634 acpi_status status = AE_OK;
1635 unsigned long val;
1637 status =
1638 acpi_evaluate_integer(device->handle, "_EC", NULL, &val);
1639 if (ACPI_FAILURE(status)) {
1640 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Error obtaining _EC"));
1641 return -EIO;
1644 sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1645 if (!sbs) {
1646 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "kzalloc() failed"));
1647 result = -ENOMEM;
1648 goto end;
1651 mutex_init(&sbs->mutex);
1653 sbs_mutex_lock(sbs);
1655 sbs->base = 0xff & (val >> 8);
1656 sbs->device = device;
1658 strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1659 strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1660 acpi_driver_data(device) = sbs;
1662 result = acpi_ac_add(sbs);
1663 if (result) {
1664 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1665 goto end;
1668 acpi_sbsm_get_info(sbs);
1670 if (!sbs->sbsm_present) {
1671 result = acpi_battery_add(sbs, 0);
1672 if (result) {
1673 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1674 "acpi_battery_add() failed"));
1675 goto end;
1677 } else {
1678 for (id = 0; id < MAX_SBS_BAT; id++) {
1679 if ((sbs->sbsm_batteries_supported & (1 << id))) {
1680 result = acpi_battery_add(sbs, id);
1681 if (result) {
1682 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1683 "acpi_battery_add() failed"));
1684 goto end;
1690 init_timer(&sbs->update_timer);
1691 result = acpi_check_update_proc(sbs);
1692 if (result)
1693 goto end;
1695 end:
1697 sbs_mutex_unlock(sbs);
1699 if (result) {
1700 remove_result = acpi_sbs_remove(device, 0);
1701 if (remove_result) {
1702 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1703 "acpi_sbs_remove() failed"));
1707 return result;
1710 static int acpi_sbs_remove(struct acpi_device *device, int type)
1712 struct acpi_sbs *sbs;
1713 int id;
1715 if (!device) {
1716 return -EINVAL;
1719 sbs = acpi_driver_data(device);
1720 if (!sbs) {
1721 return -EINVAL;
1724 sbs_mutex_lock(sbs);
1726 sbs->zombie = 1;
1727 del_timer_sync(&sbs->update_timer);
1728 acpi_os_wait_events_complete(NULL);
1729 del_timer_sync(&sbs->update_timer);
1731 for (id = 0; id < MAX_SBS_BAT; id++) {
1732 acpi_battery_remove(sbs, id);
1735 acpi_ac_remove(sbs);
1737 sbs_mutex_unlock(sbs);
1739 mutex_destroy(&sbs->mutex);
1741 kfree(sbs);
1743 return 0;
1746 static void acpi_sbs_rmdirs(void)
1748 if (acpi_ac_dir) {
1749 acpi_unlock_ac_dir(acpi_ac_dir);
1750 acpi_ac_dir = NULL;
1752 if (acpi_battery_dir) {
1753 acpi_unlock_battery_dir(acpi_battery_dir);
1754 acpi_battery_dir = NULL;
1758 static int acpi_sbs_resume(struct acpi_device *device)
1760 struct acpi_sbs *sbs;
1762 if (!device)
1763 return -EINVAL;
1765 sbs = device->driver_data;
1767 sbs->run_cnt = 0;
1769 return 0;
1772 static int __init acpi_sbs_init(void)
1774 int result = 0;
1776 if (acpi_disabled)
1777 return -ENODEV;
1779 if (capacity_mode != DEF_CAPACITY_UNIT
1780 && capacity_mode != MAH_CAPACITY_UNIT
1781 && capacity_mode != MWH_CAPACITY_UNIT) {
1782 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1783 "invalid capacity_mode = %d", capacity_mode));
1784 return -EINVAL;
1787 acpi_ac_dir = acpi_lock_ac_dir();
1788 if (!acpi_ac_dir) {
1789 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1790 "acpi_lock_ac_dir() failed"));
1791 return -ENODEV;
1794 acpi_battery_dir = acpi_lock_battery_dir();
1795 if (!acpi_battery_dir) {
1796 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1797 "acpi_lock_battery_dir() failed"));
1798 acpi_sbs_rmdirs();
1799 return -ENODEV;
1802 result = acpi_bus_register_driver(&acpi_sbs_driver);
1803 if (result < 0) {
1804 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1805 "acpi_bus_register_driver() failed"));
1806 acpi_sbs_rmdirs();
1807 return -ENODEV;
1810 return 0;
1813 static void __exit acpi_sbs_exit(void)
1815 acpi_bus_unregister_driver(&acpi_sbs_driver);
1817 acpi_sbs_rmdirs();
1819 return;
1822 module_init(acpi_sbs_init);
1823 module_exit(acpi_sbs_exit);