ACPI: sbs: use EC rather than I2C
[safe/jmp/linux-2.6] / drivers / acpi / sbs.c
1 /*
2  *  acpi_sbs.c - ACPI Smart Battery System Driver ($Revision: 1.16 $)
3  *
4  *  Copyright (c) 2005 Rich Townsend <rhdt@bartol.udel.edu>
5  *
6  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7  *
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.
12  *
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.
17  *
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.
21  *
22  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
23  */
24
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/delay.h>
35
36 #define ACPI_SBS_COMPONENT              0x00080000
37 #define ACPI_SBS_CLASS                  "sbs"
38 #define ACPI_AC_CLASS                   "ac_adapter"
39 #define ACPI_BATTERY_CLASS              "battery"
40 #define ACPI_SBS_HID                    "ACPI0002"
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
53
54 #define _COMPONENT                      ACPI_SBS_COMPONENT
55
56 ACPI_MODULE_NAME("sbs");
57
58 MODULE_AUTHOR("Rich Townsend");
59 MODULE_DESCRIPTION("Smart Battery System ACPI interface driver");
60 MODULE_LICENSE("GPL");
61
62 #define xmsleep(t)      msleep(t)
63
64 #define ACPI_EC_SMB_PRTCL       0x00    /* protocol, PEC */
65
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 */
71
72 #define ACPI_EC_SMB_STS_DONE    0x80
73 #define ACPI_EC_SMB_STS_STATUS  0x1f
74
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
79
80 #define ACPI_EC_SMB_TRANSACTION_SLEEP   1
81 #define ACPI_EC_SMB_ACCESS_SLEEP1       1
82 #define ACPI_EC_SMB_ACCESS_SLEEP2       10
83
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
88
89 #define REQUEST_UPDATE_MODE     1
90 #define QUEUE_UPDATE_MODE       2
91
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
97
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);
102
103 #define MAX_SBS_BAT                     4
104 #define ACPI_SBS_BLOCK_MAX              32
105
106 #define ACPI_SBS_SMBUS_READ             1
107 #define ACPI_SBS_SMBUS_WRITE            2
108
109 #define ACPI_SBS_WORD_DATA              1
110 #define ACPI_SBS_BLOCK_DATA             2
111
112 static struct semaphore sbs_sem;
113
114 #define UPDATE_MODE             QUEUE_UPDATE_MODE
115 /* REQUEST_UPDATE_MODE  QUEUE_UPDATE_MODE */
116 #define UPDATE_INFO_MODE        0
117 #define UPDATE_TIME             60
118 #define UPDATE_TIME2            0
119
120 static int capacity_mode = CAPACITY_UNIT;
121 static int update_mode = UPDATE_MODE;
122 static int update_info_mode = UPDATE_INFO_MODE;
123 static int update_time = UPDATE_TIME;
124 static int update_time2 = UPDATE_TIME2;
125
126 module_param(capacity_mode, int, 0);
127 module_param(update_mode, int, 0);
128 module_param(update_info_mode, int, 0);
129 module_param(update_time, int, 0);
130 module_param(update_time2, int, 0);
131
132 static int acpi_sbs_add(struct acpi_device *device);
133 static int acpi_sbs_remove(struct acpi_device *device, int type);
134 static void acpi_sbs_update_queue(void *data);
135
136 static struct acpi_driver acpi_sbs_driver = {
137         .name = "sbs",
138         .class = ACPI_SBS_CLASS,
139         .ids = ACPI_SBS_HID,
140         .ops = {
141                 .add = acpi_sbs_add,
142                 .remove = acpi_sbs_remove,
143                 },
144 };
145
146 struct acpi_battery_info {
147         int capacity_mode;
148         s16 full_charge_capacity;
149         s16 design_capacity;
150         s16 design_voltage;
151         int vscale;
152         int ipscale;
153         s16 serial_number;
154         char manufacturer_name[ACPI_SBS_BLOCK_MAX + 3];
155         char device_name[ACPI_SBS_BLOCK_MAX + 3];
156         char device_chemistry[ACPI_SBS_BLOCK_MAX + 3];
157 };
158
159 struct acpi_battery_state {
160         s16 voltage;
161         s16 amperage;
162         s16 remaining_capacity;
163         s16 average_time_to_empty;
164         s16 average_time_to_full;
165         s16 battery_status;
166 };
167
168 struct acpi_battery_alarm {
169         s16 remaining_capacity;
170 };
171
172 struct acpi_battery {
173         int alive;
174         int battery_present;
175         int id;
176         int init_state;
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;
182 };
183
184 struct acpi_sbs {
185         acpi_handle handle;
186         int base;
187         struct acpi_device *device;
188         int sbsm_present;
189         int sbsm_batteries_supported;
190         int ac_present;
191         struct proc_dir_entry *ac_entry;
192         struct acpi_battery battery[MAX_SBS_BAT];
193         int update_info_mode;
194         int zombie;
195         int update_time;
196         int update_time2;
197         struct timer_list update_timer;
198 };
199
200 union sbs_rw_data {
201         u16 word;
202         u8 block[ACPI_SBS_BLOCK_MAX + 2];
203 };
204
205 static int acpi_ec_sbs_access(struct acpi_sbs *sbs, u16 addr,
206                               char read_write, u8 command, int size,
207                               union sbs_rw_data *data);
208 static void acpi_update_delay(struct acpi_sbs *sbs);
209 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type);
210
211 /* --------------------------------------------------------------------------
212                                SMBus Communication
213    -------------------------------------------------------------------------- */
214
215 static int acpi_ec_sbs_read(struct acpi_sbs *sbs, u8 address, u8 * data)
216 {
217         u8 val;
218         int err;
219
220         err = ec_read(sbs->base + address, &val);
221         if (!err) {
222                 *data = val;
223         }
224         xmsleep(ACPI_EC_SMB_TRANSACTION_SLEEP);
225         return (err);
226 }
227
228 static int acpi_ec_sbs_write(struct acpi_sbs *sbs, u8 address, u8 data)
229 {
230         int err;
231
232         err = ec_write(sbs->base + address, data);
233         return (err);
234 }
235
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)
240 {
241         unsigned char protocol, len = 0, temp[2] = { 0, 0 };
242         int i;
243
244         if (read_write == ACPI_SBS_SMBUS_READ) {
245                 protocol = ACPI_EC_SMB_PRTCL_READ;
246         } else {
247                 protocol = ACPI_EC_SMB_PRTCL_WRITE;
248         }
249
250         switch (size) {
251
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);
258                 }
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]);
269                 }
270                 protocol |= ACPI_EC_SMB_PRTCL_BLOCK_DATA;
271                 break;
272         default:
273                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
274                                 "unsupported transaction %d\n", size));
275                 return (-1);
276         }
277
278         acpi_ec_sbs_write(sbs, ACPI_EC_SMB_ADDR, addr << 1);
279         acpi_ec_sbs_write(sbs, ACPI_EC_SMB_PRTCL, protocol);
280
281         acpi_ec_sbs_read(sbs, ACPI_EC_SMB_STS, temp);
282
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);
286         }
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);
290         }
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\n", size));
295                 return (-1);
296         }
297
298         if (read_write == ACPI_SBS_SMBUS_WRITE) {
299                 return (0);
300         }
301
302         switch (size) {
303
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;
309
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\n", size));
322                 return (-1);
323         }
324
325         return (0);
326 }
327
328 static int
329 acpi_sbs_read_word(struct acpi_sbs *sbs, int addr, int func, u16 * word)
330 {
331         union sbs_rw_data data;
332         int result = 0;
333
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_DEBUG_PRINT((ACPI_DB_ERROR,
339                                   "acpi_ec_sbs_access() failed\n"));
340         } else {
341                 *word = data.word;
342         }
343
344         return result;
345 }
346
347 static int
348 acpi_sbs_read_str(struct acpi_sbs *sbs, int addr, int func, char *str)
349 {
350         union sbs_rw_data data;
351         int result = 0;
352
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_DEBUG_PRINT((ACPI_DB_ERROR,
358                                   "acpi_ec_sbs_access() failed\n"));
359         } else {
360                 strncpy(str, (const char *)data.block + 1, data.block[0]);
361                 str[data.block[0]] = 0;
362         }
363
364         return result;
365 }
366
367 static int
368 acpi_sbs_write_word(struct acpi_sbs *sbs, int addr, int func, int word)
369 {
370         union sbs_rw_data data;
371         int result = 0;
372
373         data.word = word;
374
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_DEBUG_PRINT((ACPI_DB_ERROR,
380                                   "acpi_ec_sbs_access() failed\n"));
381         }
382
383         return result;
384 }
385
386 /* --------------------------------------------------------------------------
387                             Smart Battery System Management
388    -------------------------------------------------------------------------- */
389
390 /* Smart Battery */
391
392 static int acpi_sbs_generate_event(struct acpi_device *device,
393                                    int event, int state, char *bid, char *class)
394 {
395         char bid_saved[5];
396         char class_saved[20];
397         int result = 0;
398
399         strcpy(bid_saved, acpi_device_bid(device));
400         strcpy(class_saved, acpi_device_class(device));
401
402         strcpy(acpi_device_bid(device), bid);
403         strcpy(acpi_device_class(device), class);
404
405         result = acpi_bus_generate_event(device, event, state);
406
407         strcpy(acpi_device_bid(device), bid_saved);
408         strcpy(acpi_device_class(device), class_saved);
409
410         return result;
411 }
412
413 static int acpi_battery_get_present(struct acpi_battery *battery)
414 {
415         s16 state;
416         int result = 0;
417         int is_present = 0;
418
419         result = acpi_sbs_read_word(battery->sbs,
420                                     ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
421         if (result) {
422                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
423                                   "acpi_sbs_read_word() failed"));
424         }
425         if (!result) {
426                 is_present = (state & 0x000f) & (1 << battery->id);
427         }
428         battery->battery_present = is_present;
429
430         return result;
431 }
432
433 static int acpi_battery_is_present(struct acpi_battery *battery)
434 {
435         return (battery->battery_present);
436 }
437
438 static int acpi_ac_is_present(struct acpi_sbs *sbs)
439 {
440         return (sbs->ac_present);
441 }
442
443 static int acpi_battery_select(struct acpi_battery *battery)
444 {
445         struct acpi_sbs *sbs = battery->sbs;
446         int result = 0;
447         s16 state;
448         int foo;
449
450         if (battery->sbs->sbsm_present) {
451
452                 /* Take special care not to knobble other nibbles of
453                  * state (aka selector_state), since
454                  * it causes charging to halt on SBSELs */
455
456                 result =
457                     acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, &state);
458                 if (result) {
459                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
460                                           "acpi_sbs_read_word() failed\n"));
461                         goto end;
462                 }
463
464                 foo = (state & 0x0fff) | (1 << (battery->id + 12));
465                 result =
466                     acpi_sbs_write_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x01, foo);
467                 if (result) {
468                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
469                                           "acpi_sbs_write_word() failed\n"));
470                         goto end;
471                 }
472         }
473
474       end:
475         return result;
476 }
477
478 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
479 {
480         int result = 0;
481         s16 battery_system_info;
482
483         result = acpi_sbs_read_word(sbs, ACPI_SBSM_SMBUS_ADDR, 0x04,
484                                     &battery_system_info);
485         if (result) {
486                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
487                                   "acpi_sbs_read_word() failed\n"));
488                 goto end;
489         }
490
491         sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
492
493       end:
494
495         return result;
496 }
497
498 static int acpi_battery_get_info(struct acpi_battery *battery)
499 {
500         struct acpi_sbs *sbs = battery->sbs;
501         int result = 0;
502         s16 battery_mode;
503         s16 specification_info;
504
505         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
506                                     &battery_mode);
507         if (result) {
508                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
509                                   "acpi_sbs_read_word() failed\n"));
510                 goto end;
511         }
512         battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
513
514         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x10,
515                                     &battery->info.full_charge_capacity);
516         if (result) {
517                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
518                                   "acpi_sbs_read_word() failed\n"));
519                 goto end;
520         }
521
522         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
523                                     &battery->info.design_capacity);
524
525         if (result) {
526                 goto end;
527         }
528
529         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x19,
530                                     &battery->info.design_voltage);
531         if (result) {
532                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
533                                   "acpi_sbs_read_word() failed\n"));
534                 goto end;
535         }
536
537         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1a,
538                                     &specification_info);
539         if (result) {
540                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
541                                   "acpi_sbs_read_word() failed\n"));
542                 goto end;
543         }
544
545         switch ((specification_info & 0x0f00) >> 8) {
546         case 1:
547                 battery->info.vscale = 10;
548                 break;
549         case 2:
550                 battery->info.vscale = 100;
551                 break;
552         case 3:
553                 battery->info.vscale = 1000;
554                 break;
555         default:
556                 battery->info.vscale = 1;
557         }
558
559         switch ((specification_info & 0xf000) >> 12) {
560         case 1:
561                 battery->info.ipscale = 10;
562                 break;
563         case 2:
564                 battery->info.ipscale = 100;
565                 break;
566         case 3:
567                 battery->info.ipscale = 1000;
568                 break;
569         default:
570                 battery->info.ipscale = 1;
571         }
572
573         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x1c,
574                                     &battery->info.serial_number);
575         if (result) {
576                 goto end;
577         }
578
579         result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x20,
580                                    battery->info.manufacturer_name);
581         if (result) {
582                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
583                                   "acpi_sbs_read_str() failed\n"));
584                 goto end;
585         }
586
587         result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x21,
588                                    battery->info.device_name);
589         if (result) {
590                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
591                                   "acpi_sbs_read_str() failed\n"));
592                 goto end;
593         }
594
595         result = acpi_sbs_read_str(sbs, ACPI_SB_SMBUS_ADDR, 0x22,
596                                    battery->info.device_chemistry);
597         if (result) {
598                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
599                                   "acpi_sbs_read_str() failed\n"));
600                 goto end;
601         }
602
603       end:
604         return result;
605 }
606
607 static void acpi_update_delay(struct acpi_sbs *sbs)
608 {
609         if (sbs->zombie) {
610                 return;
611         }
612         if (sbs->update_time2 > 0) {
613                 msleep(sbs->update_time2 * 1000);
614         }
615 }
616
617 static int acpi_battery_get_state(struct acpi_battery *battery)
618 {
619         struct acpi_sbs *sbs = battery->sbs;
620         int result = 0;
621
622         acpi_update_delay(battery->sbs);
623         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x09,
624                                     &battery->state.voltage);
625         if (result) {
626                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
627                                   "acpi_sbs_read_word() failed\n"));
628                 goto end;
629         }
630
631         acpi_update_delay(battery->sbs);
632         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0a,
633                                     &battery->state.amperage);
634         if (result) {
635                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
636                                   "acpi_sbs_read_word() failed\n"));
637                 goto end;
638         }
639
640         acpi_update_delay(battery->sbs);
641         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x0f,
642                                     &battery->state.remaining_capacity);
643         if (result) {
644                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
645                                   "acpi_sbs_read_word() failed\n"));
646                 goto end;
647         }
648
649         acpi_update_delay(battery->sbs);
650         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x12,
651                                     &battery->state.average_time_to_empty);
652         if (result) {
653                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
654                                   "acpi_sbs_read_word() failed\n"));
655                 goto end;
656         }
657
658         acpi_update_delay(battery->sbs);
659         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x13,
660                                     &battery->state.average_time_to_full);
661         if (result) {
662                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
663                                   "acpi_sbs_read_word() failed\n"));
664                 goto end;
665         }
666
667         acpi_update_delay(battery->sbs);
668         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x16,
669                                     &battery->state.battery_status);
670         if (result) {
671                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
672                                   "acpi_sbs_read_word() failed\n"));
673                 goto end;
674         }
675
676         acpi_update_delay(battery->sbs);
677
678       end:
679         return result;
680 }
681
682 static int acpi_battery_get_alarm(struct acpi_battery *battery)
683 {
684         struct acpi_sbs *sbs = battery->sbs;
685         int result = 0;
686
687         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
688                                     &battery->alarm.remaining_capacity);
689         if (result) {
690                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
691                                   "acpi_sbs_read_word() failed\n"));
692                 goto end;
693         }
694
695         acpi_update_delay(battery->sbs);
696
697       end:
698
699         return result;
700 }
701
702 static int acpi_battery_set_alarm(struct acpi_battery *battery,
703                                   unsigned long alarm)
704 {
705         struct acpi_sbs *sbs = battery->sbs;
706         int result = 0;
707         s16 battery_mode;
708         int foo;
709
710         result = acpi_battery_select(battery);
711         if (result) {
712                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
713                                   "acpi_battery_select() failed\n"));
714                 goto end;
715         }
716
717         /* If necessary, enable the alarm */
718
719         if (alarm > 0) {
720                 result =
721                     acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x03,
722                                        &battery_mode);
723                 if (result) {
724                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
725                                           "acpi_sbs_read_word() failed\n"));
726                         goto end;
727                 }
728
729                 result =
730                     acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01,
731                                         battery_mode & 0xbfff);
732                 if (result) {
733                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
734                                           "acpi_sbs_write_word() failed\n"));
735                         goto end;
736                 }
737         }
738
739         foo = alarm / (battery->info.capacity_mode ? 10 : 1);
740         result = acpi_sbs_write_word(sbs, ACPI_SB_SMBUS_ADDR, 0x01, foo);
741         if (result) {
742                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
743                                   "acpi_sbs_write_word() failed\n"));
744                 goto end;
745         }
746
747       end:
748
749         return result;
750 }
751
752 static int acpi_battery_set_mode(struct acpi_battery *battery)
753 {
754         int result = 0;
755         s16 battery_mode;
756
757         if (capacity_mode == DEF_CAPACITY_UNIT) {
758                 goto end;
759         }
760
761         result = acpi_sbs_read_word(battery->sbs,
762                                     ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
763         if (result) {
764                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
765                                   "acpi_sbs_read_word() failed\n"));
766                 goto end;
767         }
768
769         if (capacity_mode == MAH_CAPACITY_UNIT) {
770                 battery_mode &= 0x7fff;
771         } else {
772                 battery_mode |= 0x8000;
773         }
774         result = acpi_sbs_write_word(battery->sbs,
775                                      ACPI_SB_SMBUS_ADDR, 0x03, battery_mode);
776         if (result) {
777                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
778                                   "acpi_sbs_write_word() failed\n"));
779                 goto end;
780         }
781
782         result = acpi_sbs_read_word(battery->sbs,
783                                     ACPI_SB_SMBUS_ADDR, 0x03, &battery_mode);
784         if (result) {
785                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
786                                   "acpi_sbs_read_word() failed\n"));
787                 goto end;
788         }
789
790       end:
791         return result;
792 }
793
794 static int acpi_battery_init(struct acpi_battery *battery)
795 {
796         int result = 0;
797
798         result = acpi_battery_select(battery);
799         if (result) {
800                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
801                                   "acpi_battery_init() failed\n"));
802                 goto end;
803         }
804
805         result = acpi_battery_set_mode(battery);
806         if (result) {
807                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
808                                   "acpi_battery_set_mode() failed\n"));
809                 goto end;
810         }
811
812         result = acpi_battery_get_info(battery);
813         if (result) {
814                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
815                                   "acpi_battery_get_info() failed\n"));
816                 goto end;
817         }
818
819         result = acpi_battery_get_state(battery);
820         if (result) {
821                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
822                                   "acpi_battery_get_state() failed\n"));
823                 goto end;
824         }
825
826         result = acpi_battery_get_alarm(battery);
827         if (result) {
828                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
829                                   "acpi_battery_get_alarm() failed\n"));
830                 goto end;
831         }
832
833       end:
834         return result;
835 }
836
837 static int acpi_ac_get_present(struct acpi_sbs *sbs)
838 {
839         int result = 0;
840         s16 charger_status;
841
842         result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
843                                     &charger_status);
844
845         if (result) {
846                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
847                                   "acpi_sbs_read_word() failed\n"));
848                 goto end;
849         }
850
851         sbs->ac_present = (charger_status & 0x8000) >> 15;
852
853       end:
854
855         return result;
856 }
857
858 /* --------------------------------------------------------------------------
859                               FS Interface (/proc/acpi)
860    -------------------------------------------------------------------------- */
861
862 /* Generic Routines */
863
864 static int
865 acpi_sbs_generic_add_fs(struct proc_dir_entry **dir,
866                         struct proc_dir_entry *parent_dir,
867                         char *dir_name,
868                         struct file_operations *info_fops,
869                         struct file_operations *state_fops,
870                         struct file_operations *alarm_fops, void *data)
871 {
872         struct proc_dir_entry *entry = NULL;
873
874         if (!*dir) {
875                 *dir = proc_mkdir(dir_name, parent_dir);
876                 if (!*dir) {
877                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
878                                           "proc_mkdir() failed\n"));
879                         return -ENODEV;
880                 }
881                 (*dir)->owner = THIS_MODULE;
882         }
883
884         /* 'info' [R] */
885         if (info_fops) {
886                 entry = create_proc_entry(ACPI_SBS_FILE_INFO, S_IRUGO, *dir);
887                 if (!entry) {
888                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
889                                           "create_proc_entry() failed\n"));
890                 } else {
891                         entry->proc_fops = info_fops;
892                         entry->data = data;
893                         entry->owner = THIS_MODULE;
894                 }
895         }
896
897         /* 'state' [R] */
898         if (state_fops) {
899                 entry = create_proc_entry(ACPI_SBS_FILE_STATE, S_IRUGO, *dir);
900                 if (!entry) {
901                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
902                                           "create_proc_entry() failed\n"));
903                 } else {
904                         entry->proc_fops = state_fops;
905                         entry->data = data;
906                         entry->owner = THIS_MODULE;
907                 }
908         }
909
910         /* 'alarm' [R/W] */
911         if (alarm_fops) {
912                 entry = create_proc_entry(ACPI_SBS_FILE_ALARM, S_IRUGO, *dir);
913                 if (!entry) {
914                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
915                                           "create_proc_entry() failed\n"));
916                 } else {
917                         entry->proc_fops = alarm_fops;
918                         entry->data = data;
919                         entry->owner = THIS_MODULE;
920                 }
921         }
922
923         return 0;
924 }
925
926 static void
927 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
928                            struct proc_dir_entry *parent_dir)
929 {
930
931         if (*dir) {
932                 remove_proc_entry(ACPI_SBS_FILE_INFO, *dir);
933                 remove_proc_entry(ACPI_SBS_FILE_STATE, *dir);
934                 remove_proc_entry(ACPI_SBS_FILE_ALARM, *dir);
935                 remove_proc_entry((*dir)->name, parent_dir);
936                 *dir = NULL;
937         }
938
939 }
940
941 /* Smart Battery Interface */
942
943 static struct proc_dir_entry *acpi_battery_dir = NULL;
944
945 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
946 {
947         struct acpi_battery *battery = seq->private;
948         int cscale;
949         int result = 0;
950
951         if (battery->sbs->zombie) {
952                 return -ENODEV;
953         }
954
955         down(&sbs_sem);
956
957         if (update_mode == REQUEST_UPDATE_MODE) {
958                 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_INFO);
959                 if (result) {
960                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
961                                           "acpi_sbs_update_run() failed\n"));
962                 }
963         }
964
965         if (acpi_battery_is_present(battery)) {
966                 seq_printf(seq, "present:                 yes\n");
967         } else {
968                 seq_printf(seq, "present:                 no\n");
969                 goto end;
970         }
971
972         if (battery->info.capacity_mode) {
973                 cscale = battery->info.vscale * battery->info.ipscale;
974         } else {
975                 cscale = battery->info.ipscale;
976         }
977         seq_printf(seq, "design capacity:         %i%s",
978                    battery->info.design_capacity * cscale,
979                    battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
980
981         seq_printf(seq, "last full capacity:      %i%s",
982                    battery->info.full_charge_capacity * cscale,
983                    battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
984
985         seq_printf(seq, "battery technology:      rechargeable\n");
986
987         seq_printf(seq, "design voltage:          %i mV\n",
988                    battery->info.design_voltage * battery->info.vscale);
989
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");
994
995         seq_printf(seq, "model number:            %s\n",
996                    battery->info.device_name);
997
998         seq_printf(seq, "serial number:           %i\n",
999                    battery->info.serial_number);
1000
1001         seq_printf(seq, "battery type:            %s\n",
1002                    battery->info.device_chemistry);
1003
1004         seq_printf(seq, "OEM info:                %s\n",
1005                    battery->info.manufacturer_name);
1006
1007       end:
1008
1009         up(&sbs_sem);
1010
1011         return result;
1012 }
1013
1014 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
1015 {
1016         return single_open(file, acpi_battery_read_info, PDE(inode)->data);
1017 }
1018
1019 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
1020 {
1021         struct acpi_battery *battery = (struct acpi_battery *)seq->private;
1022         int result = 0;
1023         int cscale;
1024         int foo;
1025
1026         if (battery->sbs->zombie) {
1027                 return -ENODEV;
1028         }
1029
1030         down(&sbs_sem);
1031
1032         if (update_mode == REQUEST_UPDATE_MODE) {
1033                 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_STATE);
1034                 if (result) {
1035                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1036                                           "acpi_sbs_update_run() failed\n"));
1037                 }
1038         }
1039
1040         if (acpi_battery_is_present(battery)) {
1041                 seq_printf(seq, "present:                 yes\n");
1042         } else {
1043                 seq_printf(seq, "present:                 no\n");
1044                 goto end;
1045         }
1046
1047         if (battery->info.capacity_mode) {
1048                 cscale = battery->info.vscale * battery->info.ipscale;
1049         } else {
1050                 cscale = battery->info.ipscale;
1051         }
1052
1053         if (battery->state.battery_status & 0x0010) {
1054                 seq_printf(seq, "capacity state:          critical\n");
1055         } else {
1056                 seq_printf(seq, "capacity state:          ok\n");
1057         }
1058
1059         foo = (s16) battery->state.amperage * battery->info.ipscale;
1060         if (battery->info.capacity_mode) {
1061                 foo = foo * battery->info.design_voltage / 1000;
1062         }
1063         if (battery->state.amperage < 0) {
1064                 seq_printf(seq, "charging state:          discharging\n");
1065                 seq_printf(seq, "present rate:            %d %s\n",
1066                            -foo, battery->info.capacity_mode ? "mW" : "mA");
1067         } else if (battery->state.amperage > 0) {
1068                 seq_printf(seq, "charging state:          charging\n");
1069                 seq_printf(seq, "present rate:            %d %s\n",
1070                            foo, battery->info.capacity_mode ? "mW" : "mA");
1071         } else {
1072                 seq_printf(seq, "charging state:          charged\n");
1073                 seq_printf(seq, "present rate:            0 %s\n",
1074                            battery->info.capacity_mode ? "mW" : "mA");
1075         }
1076
1077         seq_printf(seq, "remaining capacity:      %i%s",
1078                    battery->state.remaining_capacity * cscale,
1079                    battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1080
1081         seq_printf(seq, "present voltage:         %i mV\n",
1082                    battery->state.voltage * battery->info.vscale);
1083
1084       end:
1085
1086         up(&sbs_sem);
1087
1088         return result;
1089 }
1090
1091 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1092 {
1093         return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1094 }
1095
1096 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1097 {
1098         struct acpi_battery *battery = seq->private;
1099         int result = 0;
1100         int cscale;
1101
1102         if (battery->sbs->zombie) {
1103                 return -ENODEV;
1104         }
1105
1106         down(&sbs_sem);
1107
1108         if (update_mode == REQUEST_UPDATE_MODE) {
1109                 result = acpi_sbs_update_run(battery->sbs, DATA_TYPE_ALARM);
1110                 if (result) {
1111                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1112                                           "acpi_sbs_update_run() failed\n"));
1113                 }
1114         }
1115
1116         if (!acpi_battery_is_present(battery)) {
1117                 seq_printf(seq, "present:                 no\n");
1118                 goto end;
1119         }
1120
1121         if (battery->info.capacity_mode) {
1122                 cscale = battery->info.vscale * battery->info.ipscale;
1123         } else {
1124                 cscale = battery->info.ipscale;
1125         }
1126
1127         seq_printf(seq, "alarm:                   ");
1128         if (battery->alarm.remaining_capacity) {
1129                 seq_printf(seq, "%i%s",
1130                            battery->alarm.remaining_capacity * cscale,
1131                            battery->info.capacity_mode ? "0 mWh\n" : " mAh\n");
1132         } else {
1133                 seq_printf(seq, "disabled\n");
1134         }
1135
1136       end:
1137
1138         up(&sbs_sem);
1139
1140         return result;
1141 }
1142
1143 static ssize_t
1144 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1145                          size_t count, loff_t * ppos)
1146 {
1147         struct seq_file *seq = file->private_data;
1148         struct acpi_battery *battery = seq->private;
1149         char alarm_string[12] = { '\0' };
1150         int result, old_alarm, new_alarm;
1151
1152         if (battery->sbs->zombie) {
1153                 return -ENODEV;
1154         }
1155
1156         down(&sbs_sem);
1157
1158         if (!acpi_battery_is_present(battery)) {
1159                 result = -ENODEV;
1160                 goto end;
1161         }
1162
1163         if (count > sizeof(alarm_string) - 1) {
1164                 result = -EINVAL;
1165                 goto end;
1166         }
1167
1168         if (copy_from_user(alarm_string, buffer, count)) {
1169                 result = -EFAULT;
1170                 goto end;
1171         }
1172
1173         alarm_string[count] = 0;
1174
1175         old_alarm = battery->alarm.remaining_capacity;
1176         new_alarm = simple_strtoul(alarm_string, NULL, 0);
1177
1178         result = acpi_battery_set_alarm(battery, new_alarm);
1179         if (result) {
1180                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1181                                   "acpi_battery_set_alarm() failed\n"));
1182                 acpi_battery_set_alarm(battery, old_alarm);
1183                 goto end;
1184         }
1185         result = acpi_battery_get_alarm(battery);
1186         if (result) {
1187                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1188                                   "acpi_battery_get_alarm() failed\n"));
1189                 acpi_battery_set_alarm(battery, old_alarm);
1190                 goto end;
1191         }
1192
1193       end:
1194         up(&sbs_sem);
1195
1196         if (result) {
1197                 return result;
1198         } else {
1199                 return count;
1200         }
1201 }
1202
1203 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1204 {
1205         return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1206 }
1207
1208 static struct file_operations acpi_battery_info_fops = {
1209         .open = acpi_battery_info_open_fs,
1210         .read = seq_read,
1211         .llseek = seq_lseek,
1212         .release = single_release,
1213         .owner = THIS_MODULE,
1214 };
1215
1216 static struct file_operations acpi_battery_state_fops = {
1217         .open = acpi_battery_state_open_fs,
1218         .read = seq_read,
1219         .llseek = seq_lseek,
1220         .release = single_release,
1221         .owner = THIS_MODULE,
1222 };
1223
1224 static struct file_operations acpi_battery_alarm_fops = {
1225         .open = acpi_battery_alarm_open_fs,
1226         .read = seq_read,
1227         .write = acpi_battery_write_alarm,
1228         .llseek = seq_lseek,
1229         .release = single_release,
1230         .owner = THIS_MODULE,
1231 };
1232
1233 /* Legacy AC Adapter Interface */
1234
1235 static struct proc_dir_entry *acpi_ac_dir = NULL;
1236
1237 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1238 {
1239         struct acpi_sbs *sbs = seq->private;
1240         int result;
1241
1242         if (sbs->zombie) {
1243                 return -ENODEV;
1244         }
1245
1246         down(&sbs_sem);
1247
1248         if (update_mode == REQUEST_UPDATE_MODE) {
1249                 result = acpi_sbs_update_run(sbs, DATA_TYPE_AC_STATE);
1250                 if (result) {
1251                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1252                                           "acpi_sbs_update_run() failed\n"));
1253                 }
1254         }
1255
1256         seq_printf(seq, "state:                   %s\n",
1257                    sbs->ac_present ? "on-line" : "off-line");
1258
1259         up(&sbs_sem);
1260
1261         return 0;
1262 }
1263
1264 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1265 {
1266         return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1267 }
1268
1269 static struct file_operations acpi_ac_state_fops = {
1270         .open = acpi_ac_state_open_fs,
1271         .read = seq_read,
1272         .llseek = seq_lseek,
1273         .release = single_release,
1274         .owner = THIS_MODULE,
1275 };
1276
1277 /* --------------------------------------------------------------------------
1278                                  Driver Interface
1279    -------------------------------------------------------------------------- */
1280
1281 /* Smart Battery */
1282
1283 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1284 {
1285         int is_present;
1286         int result;
1287         char dir_name[32];
1288         struct acpi_battery *battery;
1289
1290         battery = &sbs->battery[id];
1291
1292         battery->alive = 0;
1293
1294         battery->init_state = 0;
1295         battery->id = id;
1296         battery->sbs = sbs;
1297
1298         result = acpi_battery_select(battery);
1299         if (result) {
1300                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1301                                   "acpi_battery_select() failed\n"));
1302                 goto end;
1303         }
1304
1305         result = acpi_battery_get_present(battery);
1306         if (result) {
1307                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1308                                   "acpi_battery_get_present() failed\n"));
1309                 goto end;
1310         }
1311
1312         is_present = acpi_battery_is_present(battery);
1313
1314         if (is_present) {
1315                 result = acpi_battery_init(battery);
1316                 if (result) {
1317                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1318                                           "acpi_battery_init() failed\n"));
1319                         goto end;
1320                 }
1321                 battery->init_state = 1;
1322         }
1323
1324         sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1325
1326         result = acpi_sbs_generic_add_fs(&battery->battery_entry,
1327                                          acpi_battery_dir,
1328                                          dir_name,
1329                                          &acpi_battery_info_fops,
1330                                          &acpi_battery_state_fops,
1331                                          &acpi_battery_alarm_fops, battery);
1332         if (result) {
1333                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1334                                   "acpi_sbs_generic_add_fs() failed\n"));
1335                 goto end;
1336         }
1337         battery->alive = 1;
1338
1339       end:
1340         return result;
1341 }
1342
1343 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1344 {
1345
1346         if (sbs->battery[id].battery_entry) {
1347                 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1348                                            acpi_battery_dir);
1349         }
1350 }
1351
1352 static int acpi_ac_add(struct acpi_sbs *sbs)
1353 {
1354         int result;
1355
1356         result = acpi_ac_get_present(sbs);
1357         if (result) {
1358                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1359                                   "acpi_ac_get_present() failed\n"));
1360                 goto end;
1361         }
1362
1363         result = acpi_sbs_generic_add_fs(&sbs->ac_entry,
1364                                          acpi_ac_dir,
1365                                          ACPI_AC_DIR_NAME,
1366                                          NULL, &acpi_ac_state_fops, NULL, sbs);
1367         if (result) {
1368                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1369                                   "acpi_sbs_generic_add_fs() failed\n"));
1370                 goto end;
1371         }
1372
1373       end:
1374
1375         return result;
1376 }
1377
1378 static void acpi_ac_remove(struct acpi_sbs *sbs)
1379 {
1380
1381         if (sbs->ac_entry) {
1382                 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1383         }
1384 }
1385
1386 static void acpi_sbs_update_queue_run(unsigned long data)
1387 {
1388         acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_queue, (void *)data);
1389 }
1390
1391 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int data_type)
1392 {
1393         struct acpi_battery *battery;
1394         int result = 0;
1395         int old_ac_present;
1396         int old_battery_present;
1397         int new_ac_present;
1398         int new_battery_present;
1399         int id;
1400         char dir_name[32];
1401         int do_battery_init, do_ac_init;
1402         s16 old_remaining_capacity;
1403
1404         if (sbs->zombie) {
1405                 goto end;
1406         }
1407
1408         old_ac_present = acpi_ac_is_present(sbs);
1409
1410         result = acpi_ac_get_present(sbs);
1411         if (result) {
1412                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1413                                   "acpi_ac_get_present() failed\n"));
1414         }
1415
1416         new_ac_present = acpi_ac_is_present(sbs);
1417
1418         do_ac_init = (old_ac_present != new_ac_present);
1419
1420         if (data_type == DATA_TYPE_AC_STATE) {
1421                 goto end;
1422         }
1423
1424         for (id = 0; id < MAX_SBS_BAT; id++) {
1425                 battery = &sbs->battery[id];
1426                 if (battery->alive == 0) {
1427                         continue;
1428                 }
1429
1430                 old_remaining_capacity = battery->state.remaining_capacity;
1431
1432                 old_battery_present = acpi_battery_is_present(battery);
1433
1434                 result = acpi_battery_select(battery);
1435                 if (result) {
1436                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1437                                           "acpi_battery_select() failed\n"));
1438                 }
1439                 if (sbs->zombie) {
1440                         goto end;
1441                 }
1442
1443                 result = acpi_battery_get_present(battery);
1444                 if (result) {
1445                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1446                                           "acpi_battery_get_present() failed\n"));
1447                 }
1448                 if (sbs->zombie) {
1449                         goto end;
1450                 }
1451
1452                 new_battery_present = acpi_battery_is_present(battery);
1453
1454                 do_battery_init = ((old_battery_present != new_battery_present)
1455                                    && new_battery_present);
1456
1457                 if (sbs->zombie) {
1458                         goto end;
1459                 }
1460                 if (do_ac_init || do_battery_init ||
1461                     update_info_mode || sbs->update_info_mode) {
1462                         if (sbs->update_info_mode) {
1463                                 sbs->update_info_mode = 0;
1464                         } else {
1465                                 sbs->update_info_mode = 1;
1466                         }
1467                         result = acpi_battery_init(battery);
1468                         if (result) {
1469                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1470                                                   "acpi_battery_init() "
1471                                                   "failed\n"));
1472                         }
1473                 }
1474                 if (data_type == DATA_TYPE_INFO) {
1475                         continue;
1476                 }
1477
1478                 if (sbs->zombie) {
1479                         goto end;
1480                 }
1481                 if (new_battery_present) {
1482                         result = acpi_battery_get_alarm(battery);
1483                         if (result) {
1484                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1485                                                   "acpi_battery_get_alarm() "
1486                                                   "failed\n"));
1487                         }
1488                         if (data_type == DATA_TYPE_ALARM) {
1489                                 continue;
1490                         }
1491
1492                         result = acpi_battery_get_state(battery);
1493                         if (result) {
1494                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1495                                                   "acpi_battery_get_state() "
1496                                                   "failed\n"));
1497                         }
1498                 }
1499                 if (sbs->zombie) {
1500                         goto end;
1501                 }
1502                 if (data_type != DATA_TYPE_COMMON) {
1503                         continue;
1504                 }
1505
1506                 if (old_battery_present != new_battery_present) {
1507                         sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1508                         result = acpi_sbs_generate_event(sbs->device,
1509                                                          ACPI_SBS_BATTERY_NOTIFY_STATUS,
1510                                                          new_battery_present,
1511                                                          dir_name,
1512                                                          ACPI_BATTERY_CLASS);
1513                         if (result) {
1514                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1515                                                   "acpi_sbs_generate_event() "
1516                                                   "failed\n"));
1517                         }
1518                 }
1519                 if (old_remaining_capacity != battery->state.remaining_capacity) {
1520                         sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1521                         result = acpi_sbs_generate_event(sbs->device,
1522                                                          ACPI_SBS_BATTERY_NOTIFY_STATUS,
1523                                                          new_battery_present,
1524                                                          dir_name,
1525                                                          ACPI_BATTERY_CLASS);
1526                         if (result) {
1527                                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1528                                                   "acpi_sbs_generate_event() failed\n"));
1529                         }
1530                 }
1531
1532         }
1533         if (sbs->zombie) {
1534                 goto end;
1535         }
1536         if (data_type != DATA_TYPE_COMMON) {
1537                 goto end;
1538         }
1539
1540         if (old_ac_present != new_ac_present) {
1541                 result = acpi_sbs_generate_event(sbs->device,
1542                                                  ACPI_SBS_AC_NOTIFY_STATUS,
1543                                                  new_ac_present,
1544                                                  ACPI_AC_DIR_NAME,
1545                                                  ACPI_AC_CLASS);
1546                 if (result) {
1547                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1548                                           "acpi_sbs_generate_event() failed\n"));
1549                 }
1550         }
1551
1552       end:
1553         return result;
1554 }
1555
1556 static void acpi_sbs_update_queue(void *data)
1557 {
1558         struct acpi_sbs *sbs = data;
1559         unsigned long delay = -1;
1560         int result;
1561
1562         if (sbs->zombie) {
1563                 goto end;
1564         }
1565
1566         result = acpi_sbs_update_run(sbs, DATA_TYPE_COMMON);
1567         if (result) {
1568                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1569                                   "acpi_sbs_update_run() failed\n"));
1570         }
1571
1572         if (sbs->zombie) {
1573                 goto end;
1574         }
1575
1576         if (update_mode == REQUEST_UPDATE_MODE) {
1577                 goto end;
1578         }
1579
1580         delay = jiffies + HZ * update_time;
1581         sbs->update_timer.data = (unsigned long)data;
1582         sbs->update_timer.function = acpi_sbs_update_queue_run;
1583         sbs->update_timer.expires = delay;
1584         add_timer(&sbs->update_timer);
1585       end:
1586         ;
1587 }
1588
1589 static int acpi_sbs_add(struct acpi_device *device)
1590 {
1591         struct acpi_sbs *sbs = NULL;
1592         int result;
1593         unsigned long sbs_obj;
1594         int id;
1595         acpi_status status = AE_OK;
1596         unsigned long val;
1597
1598         status =
1599             acpi_evaluate_integer(device->parent->handle, "_EC", NULL, &val);
1600         if (ACPI_FAILURE(status)) {
1601                 ACPI_DEBUG_PRINT((ACPI_DB_WARN, "Error obtaining _EC\n"));
1602                 return -EIO;
1603         }
1604
1605         sbs = kzalloc(sizeof(struct acpi_sbs), GFP_KERNEL);
1606         if (!sbs) {
1607                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "kmalloc() failed\n"));
1608                 return -ENOMEM;
1609         }
1610         sbs->base = (val & 0xff00ull) >> 8;
1611
1612         sbs->device = device;
1613
1614         strcpy(acpi_device_name(device), ACPI_SBS_DEVICE_NAME);
1615         strcpy(acpi_device_class(device), ACPI_SBS_CLASS);
1616         acpi_driver_data(device) = sbs;
1617
1618         sbs->update_time = 0;
1619         sbs->update_time2 = 0;
1620
1621         result = acpi_ac_add(sbs);
1622         if (result) {
1623                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_ac_add() failed\n"));
1624                 goto end;
1625         }
1626         result = acpi_evaluate_integer(device->handle, "_SBS", NULL, &sbs_obj);
1627         if (ACPI_FAILURE(result)) {
1628                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1629                                   "acpi_evaluate_integer() failed\n"));
1630                 result = -EIO;
1631                 goto end;
1632         }
1633
1634         if (sbs_obj > 0) {
1635                 result = acpi_sbsm_get_info(sbs);
1636                 if (result) {
1637                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1638                                           "acpi_sbsm_get_info() failed\n"));
1639                         goto end;
1640                 }
1641                 sbs->sbsm_present = 1;
1642         }
1643         if (sbs->sbsm_present == 0) {
1644                 result = acpi_battery_add(sbs, 0);
1645                 if (result) {
1646                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1647                                           "acpi_battery_add() failed\n"));
1648                         goto end;
1649                 }
1650         } else {
1651                 for (id = 0; id < MAX_SBS_BAT; id++) {
1652                         if ((sbs->sbsm_batteries_supported & (1 << id))) {
1653                                 result = acpi_battery_add(sbs, id);
1654                                 if (result) {
1655                                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1656                                                           "acpi_battery_add() "
1657                                                           "failed\n"));
1658                                         goto end;
1659                                 }
1660                         }
1661                 }
1662         }
1663
1664         sbs->handle = device->handle;
1665
1666         init_timer(&sbs->update_timer);
1667         if (update_mode == QUEUE_UPDATE_MODE) {
1668                 status = acpi_os_execute(OSL_GPE_HANDLER,
1669                                          acpi_sbs_update_queue, sbs);
1670                 if (status != AE_OK) {
1671                         ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1672                                           "acpi_os_execute() failed\n"));
1673                 }
1674         }
1675         sbs->update_time = update_time;
1676         sbs->update_time2 = update_time2;
1677
1678         printk(KERN_INFO PREFIX "%s [%s]\n",
1679                acpi_device_name(device), acpi_device_bid(device));
1680
1681       end:
1682         if (result) {
1683                 acpi_sbs_remove(device, 0);
1684         }
1685
1686         return result;
1687 }
1688
1689 int acpi_sbs_remove(struct acpi_device *device, int type)
1690 {
1691         struct acpi_sbs *sbs;
1692         int id;
1693
1694         if (!device) {
1695                 return -EINVAL;
1696         }
1697
1698         sbs = (struct acpi_sbs *)acpi_driver_data(device);
1699
1700         if (!sbs) {
1701                 return -EINVAL;
1702         }
1703
1704         sbs->zombie = 1;
1705         sbs->update_time = 0;
1706         sbs->update_time2 = 0;
1707         del_timer_sync(&sbs->update_timer);
1708         acpi_os_wait_events_complete(NULL);
1709         del_timer_sync(&sbs->update_timer);
1710
1711         for (id = 0; id < MAX_SBS_BAT; id++) {
1712                 acpi_battery_remove(sbs, id);
1713         }
1714
1715         acpi_ac_remove(sbs);
1716
1717         acpi_driver_data(device) = NULL;
1718
1719         kfree(sbs);
1720
1721         return 0;
1722 }
1723
1724 static int __init acpi_sbs_init(void)
1725 {
1726         int result = 0;
1727
1728         if (acpi_disabled)
1729                 return -ENODEV;
1730
1731         init_MUTEX(&sbs_sem);
1732
1733         if (capacity_mode != DEF_CAPACITY_UNIT
1734             && capacity_mode != MAH_CAPACITY_UNIT
1735             && capacity_mode != MWH_CAPACITY_UNIT) {
1736                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR, "acpi_sbs_init: "
1737                                   "invalid capacity_mode = %d\n",
1738                                   capacity_mode));
1739                 return -EINVAL;
1740         }
1741
1742         acpi_ac_dir = acpi_lock_ac_dir();
1743         if (!acpi_ac_dir) {
1744                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1745                                   "acpi_lock_ac_dir() failed\n"));
1746                 return -ENODEV;
1747         }
1748
1749         acpi_battery_dir = acpi_lock_battery_dir();
1750         if (!acpi_battery_dir) {
1751                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1752                                   "acpi_lock_battery_dir() failed\n"));
1753                 return -ENODEV;
1754         }
1755
1756         result = acpi_bus_register_driver(&acpi_sbs_driver);
1757         if (result < 0) {
1758                 ACPI_DEBUG_PRINT((ACPI_DB_ERROR,
1759                                   "acpi_bus_register_driver() failed\n"));
1760                 return -ENODEV;
1761         }
1762
1763         return 0;
1764 }
1765
1766 static void __exit acpi_sbs_exit(void)
1767 {
1768
1769         acpi_bus_unregister_driver(&acpi_sbs_driver);
1770
1771         acpi_unlock_ac_dir(acpi_ac_dir);
1772         acpi_ac_dir = NULL;
1773         acpi_unlock_battery_dir(acpi_battery_dir);
1774         acpi_battery_dir = NULL;
1775
1776         return;
1777 }
1778
1779 module_init(acpi_sbs_init);
1780 module_exit(acpi_sbs_exit);