Merge branch 'release' of git://lm-sensors.org/kernel/mhoffman/hwmon-2.6
[sfrench/cifs-2.6.git] / 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/jiffies.h>
35 #include <linux/delay.h>
36
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
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 #define UPDATE_DELAY    10
113
114 /* 0 - every time, > 0 - by update_time */
115 static unsigned int update_time = 120;
116
117 static unsigned int capacity_mode = CAPACITY_UNIT;
118
119 module_param(update_time, uint, 0644);
120 module_param(capacity_mode, uint, 0444);
121
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);
125
126 static const struct acpi_device_id sbs_device_ids[] = {
127         {"ACPI0001", 0},
128         {"ACPI0005", 0},
129         {"", 0},
130 };
131 MODULE_DEVICE_TABLE(acpi, sbs_device_ids);
132
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,
141                 },
142 };
143
144 struct acpi_ac {
145         int ac_present;
146 };
147
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];
159 };
160
161 struct acpi_battery_state {
162         s16 voltage;
163         s16 amperage;
164         s16 remaining_capacity;
165         s16 battery_state;
166 };
167
168 struct acpi_battery_alarm {
169         s16 remaining_capacity;
170 };
171
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;
182 };
183
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;
197 };
198
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);
201
202 union sbs_rw_data {
203         u16 word;
204         u8 block[ACPI_SBS_BLOCK_MAX + 2];
205 };
206
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);
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", 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", 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", 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_EXCEPTION((AE_INFO, AE_ERROR,
339                                 "acpi_ec_sbs_access() failed"));
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_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;
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_EXCEPTION((AE_INFO, AE_ERROR,
380                                 "acpi_ec_sbs_access() failed"));
381         }
382
383         return result;
384 }
385
386 static int sbs_zombie(struct acpi_sbs *sbs)
387 {
388         return (sbs->zombie);
389 }
390
391 static int sbs_mutex_lock(struct acpi_sbs *sbs)
392 {
393         if (sbs_zombie(sbs)) {
394                 return -ENODEV;
395         }
396         mutex_lock(&sbs->mutex);
397         return 0;
398 }
399
400 static void sbs_mutex_unlock(struct acpi_sbs *sbs)
401 {
402         mutex_unlock(&sbs->mutex);
403 }
404
405 /* --------------------------------------------------------------------------
406                             Smart Battery System Management
407    -------------------------------------------------------------------------- */
408
409 static int acpi_check_update_proc(struct acpi_sbs *sbs)
410 {
411         acpi_status status = AE_OK;
412
413         if (update_time == 0) {
414                 sbs->update_proc_flg = 0;
415                 return 0;
416         }
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;
424                 }
425                 sbs->update_proc_flg = 1;
426         }
427         return 0;
428 }
429
430 static int acpi_sbs_generate_event(struct acpi_device *device,
431                                    int event, int state, char *bid, char *class)
432 {
433         char bid_saved[5];
434         char class_saved[20];
435         int result = 0;
436
437         strcpy(bid_saved, acpi_device_bid(device));
438         strcpy(class_saved, acpi_device_class(device));
439
440         strcpy(acpi_device_bid(device), bid);
441         strcpy(acpi_device_class(device), class);
442
443         result = acpi_bus_generate_event(device, event, state);
444
445         strcpy(acpi_device_bid(device), bid_saved);
446         strcpy(acpi_device_class(device), class_saved);
447
448         return result;
449 }
450
451 static int acpi_battery_get_present(struct acpi_battery *battery)
452 {
453         s16 state;
454         int result = 0;
455         int is_present = 0;
456
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"));
462         }
463         if (!result) {
464                 is_present = (state & 0x000f) & (1 << battery->id);
465         }
466         battery->battery_present = is_present;
467
468         return result;
469 }
470
471 static int acpi_battery_select(struct acpi_battery *battery)
472 {
473         struct acpi_sbs *sbs = battery->sbs;
474         int result = 0;
475         s16 state;
476         int foo;
477
478         if (sbs->sbsm_present) {
479
480                 /* Take special care not to knobble other nibbles of
481                  * state (aka selector_state), since
482                  * it causes charging to halt on SBSELs */
483
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;
490                 }
491
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;
499                 }
500         }
501
502       end:
503         return result;
504 }
505
506 static int acpi_sbsm_get_info(struct acpi_sbs *sbs)
507 {
508         int result = 0;
509         s16 battery_system_info;
510
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;
517         }
518         sbs->sbsm_present = 1;
519         sbs->sbsm_batteries_supported = battery_system_info & 0x000f;
520
521       end:
522
523         return result;
524 }
525
526 static int acpi_battery_get_info(struct acpi_battery *battery)
527 {
528         struct acpi_sbs *sbs = battery->sbs;
529         int result = 0;
530         s16 battery_mode;
531         s16 specification_info;
532
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;
539         }
540         battery->info.capacity_mode = (battery_mode & 0x8000) >> 15;
541
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;
548         }
549
550         result = acpi_sbs_read_word(sbs, ACPI_SB_SMBUS_ADDR, 0x18,
551                                     &battery->info.design_capacity);
552
553         if (result) {
554                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
555                                 "acpi_sbs_read_word() failed"));
556                 goto end;
557         }
558
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;
565         }
566
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;
573         }
574
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;
587         }
588
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;
601         }
602
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;
609         }
610
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;
617         }
618
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;
625         }
626
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;
633         }
634
635       end:
636         return result;
637 }
638
639 static int acpi_battery_get_state(struct acpi_battery *battery)
640 {
641         struct acpi_sbs *sbs = battery->sbs;
642         int result = 0;
643
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;
650         }
651
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;
658         }
659
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;
666         }
667
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;
674         }
675
676       end:
677         return result;
678 }
679
680 static int acpi_battery_get_alarm(struct acpi_battery *battery)
681 {
682         struct acpi_sbs *sbs = battery->sbs;
683         int result = 0;
684
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;
691         }
692
693       end:
694
695         return result;
696 }
697
698 static int acpi_battery_set_alarm(struct acpi_battery *battery,
699                                   unsigned long alarm)
700 {
701         struct acpi_sbs *sbs = battery->sbs;
702         int result = 0;
703         s16 battery_mode;
704         int foo;
705
706         result = acpi_battery_select(battery);
707         if (result) {
708                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
709                                 "acpi_battery_select() failed"));
710                 goto end;
711         }
712
713         /* If necessary, enable the alarm */
714
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;
723                 }
724
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;
732                 }
733         }
734
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;
741         }
742
743       end:
744
745         return result;
746 }
747
748 static int acpi_battery_set_mode(struct acpi_battery *battery)
749 {
750         struct acpi_sbs *sbs = battery->sbs;
751         int result = 0;
752         s16 battery_mode;
753
754         if (capacity_mode == DEF_CAPACITY_UNIT) {
755                 goto end;
756         }
757
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;
764         }
765
766         if (capacity_mode == MAH_CAPACITY_UNIT) {
767                 battery_mode &= 0x7fff;
768         } else {
769                 battery_mode |= 0x8000;
770         }
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;
777         }
778
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;
785         }
786
787       end:
788         return result;
789 }
790
791 static int acpi_battery_init(struct acpi_battery *battery)
792 {
793         int result = 0;
794
795         result = acpi_battery_select(battery);
796         if (result) {
797                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
798                                 "acpi_battery_select() failed"));
799                 goto end;
800         }
801
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;
807         }
808
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;
814         }
815
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;
821         }
822
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;
828         }
829
830       end:
831         return result;
832 }
833
834 static int acpi_ac_get_present(struct acpi_sbs *sbs)
835 {
836         int result = 0;
837         s16 charger_status;
838
839         result = acpi_sbs_read_word(sbs, ACPI_SBC_SMBUS_ADDR, 0x13,
840                                     &charger_status);
841
842         if (result) {
843                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
844                                 "acpi_sbs_read_word() failed"));
845                 goto end;
846         }
847
848         sbs->ac.ac_present = (charger_status & 0x8000) >> 15;
849
850       end:
851
852         return result;
853 }
854
855 /* --------------------------------------------------------------------------
856                               FS Interface (/proc/acpi)
857    -------------------------------------------------------------------------- */
858
859 /* Generic Routines */
860
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)
868 {
869         struct proc_dir_entry *entry = NULL;
870
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;
877                 }
878                 (*dir)->owner = THIS_MODULE;
879         }
880
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;
891                 }
892         }
893
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;
904                 }
905         }
906
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;
917                 }
918         }
919
920         return 0;
921 }
922
923 static void
924 acpi_sbs_generic_remove_fs(struct proc_dir_entry **dir,
925                            struct proc_dir_entry *parent_dir)
926 {
927
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;
934         }
935
936 }
937
938 /* Smart Battery Interface */
939
940 static struct proc_dir_entry *acpi_battery_dir = NULL;
941
942 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
943 {
944         struct acpi_battery *battery = seq->private;
945         struct acpi_sbs *sbs = battery->sbs;
946         int cscale;
947         int result = 0;
948
949         if (sbs_mutex_lock(sbs)) {
950                 return -ENODEV;
951         }
952
953         result = acpi_check_update_proc(sbs);
954         if (result)
955                 goto end;
956
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"));
962                 }
963         }
964
965         if (battery->battery_present) {
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\n",
978                    battery->info.design_capacity * cscale,
979                    battery->info.capacity_mode ? "0 mWh" : " mAh");
980
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");
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         sbs_mutex_unlock(sbs);
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 = seq->private;
1022         struct acpi_sbs *sbs = battery->sbs;
1023         int result = 0;
1024         int cscale;
1025         int foo;
1026
1027         if (sbs_mutex_lock(sbs)) {
1028                 return -ENODEV;
1029         }
1030
1031         result = acpi_check_update_proc(sbs);
1032         if (result)
1033                 goto end;
1034
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"));
1040                 }
1041         }
1042
1043         if (battery->battery_present) {
1044                 seq_printf(seq, "present:                 yes\n");
1045         } else {
1046                 seq_printf(seq, "present:                 no\n");
1047                 goto end;
1048         }
1049
1050         if (battery->info.capacity_mode) {
1051                 cscale = battery->info.vscale * battery->info.ipscale;
1052         } else {
1053                 cscale = battery->info.ipscale;
1054         }
1055
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");
1060         }
1061
1062         foo = (s16) battery->state.amperage * battery->info.ipscale;
1063         if (battery->info.capacity_mode) {
1064                 foo = foo * battery->info.design_voltage / 1000;
1065         }
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");
1078         }
1079
1080         seq_printf(seq, "remaining capacity:      %i%s\n",
1081                    battery->state.remaining_capacity * cscale,
1082                    battery->info.capacity_mode ? "0 mWh" : " mAh");
1083
1084         seq_printf(seq, "present voltage:         %i mV\n",
1085                    battery->state.voltage * battery->info.vscale);
1086
1087       end:
1088
1089         sbs_mutex_unlock(sbs);
1090
1091         return result;
1092 }
1093
1094 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
1095 {
1096         return single_open(file, acpi_battery_read_state, PDE(inode)->data);
1097 }
1098
1099 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
1100 {
1101         struct acpi_battery *battery = seq->private;
1102         struct acpi_sbs *sbs = battery->sbs;
1103         int result = 0;
1104         int cscale;
1105
1106         if (sbs_mutex_lock(sbs)) {
1107                 return -ENODEV;
1108         }
1109
1110         result = acpi_check_update_proc(sbs);
1111         if (result)
1112                 goto end;
1113
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"));
1119                 }
1120         }
1121
1122         if (!battery->battery_present) {
1123                 seq_printf(seq, "present:                 no\n");
1124                 goto end;
1125         }
1126
1127         if (battery->info.capacity_mode) {
1128                 cscale = battery->info.vscale * battery->info.ipscale;
1129         } else {
1130                 cscale = battery->info.ipscale;
1131         }
1132
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");
1140         }
1141
1142       end:
1143
1144         sbs_mutex_unlock(sbs);
1145
1146         return result;
1147 }
1148
1149 static ssize_t
1150 acpi_battery_write_alarm(struct file *file, const char __user * buffer,
1151                          size_t count, loff_t * ppos)
1152 {
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;
1158
1159         if (sbs_mutex_lock(sbs)) {
1160                 return -ENODEV;
1161         }
1162
1163         result = acpi_check_update_proc(sbs);
1164         if (result)
1165                 goto end;
1166
1167         if (!battery->battery_present) {
1168                 result = -ENODEV;
1169                 goto end;
1170         }
1171
1172         if (count > sizeof(alarm_string) - 1) {
1173                 result = -EINVAL;
1174                 goto end;
1175         }
1176
1177         if (copy_from_user(alarm_string, buffer, count)) {
1178                 result = -EFAULT;
1179                 goto end;
1180         }
1181
1182         alarm_string[count] = 0;
1183
1184         old_alarm = battery->alarm.remaining_capacity;
1185         new_alarm = simple_strtoul(alarm_string, NULL, 0);
1186
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;
1193         }
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;
1200         }
1201
1202       end:
1203         sbs_mutex_unlock(sbs);
1204
1205         if (result) {
1206                 return result;
1207         } else {
1208                 return count;
1209         }
1210 }
1211
1212 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
1213 {
1214         return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
1215 }
1216
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,
1223 };
1224
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,
1231 };
1232
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,
1240 };
1241
1242 /* Legacy AC Adapter Interface */
1243
1244 static struct proc_dir_entry *acpi_ac_dir = NULL;
1245
1246 static int acpi_ac_read_state(struct seq_file *seq, void *offset)
1247 {
1248         struct acpi_sbs *sbs = seq->private;
1249         int result;
1250
1251         if (sbs_mutex_lock(sbs)) {
1252                 return -ENODEV;
1253         }
1254
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"));
1260                 }
1261         }
1262
1263         seq_printf(seq, "state:                   %s\n",
1264                    sbs->ac.ac_present ? "on-line" : "off-line");
1265
1266         sbs_mutex_unlock(sbs);
1267
1268         return 0;
1269 }
1270
1271 static int acpi_ac_state_open_fs(struct inode *inode, struct file *file)
1272 {
1273         return single_open(file, acpi_ac_read_state, PDE(inode)->data);
1274 }
1275
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,
1282 };
1283
1284 /* --------------------------------------------------------------------------
1285                                  Driver Interface
1286    -------------------------------------------------------------------------- */
1287
1288 /* Smart Battery */
1289
1290 static int acpi_battery_add(struct acpi_sbs *sbs, int id)
1291 {
1292         int is_present;
1293         int result;
1294         char dir_name[32];
1295         struct acpi_battery *battery;
1296
1297         battery = &sbs->battery[id];
1298
1299         battery->alive = 0;
1300
1301         battery->init_state = 0;
1302         battery->id = id;
1303         battery->sbs = sbs;
1304
1305         result = acpi_battery_select(battery);
1306         if (result) {
1307                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1308                                 "acpi_battery_select() failed"));
1309                 goto end;
1310         }
1311
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;
1317         }
1318
1319         is_present = battery->battery_present;
1320
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;
1327                 }
1328                 battery->init_state = 1;
1329         }
1330
1331         sprintf(dir_name, ACPI_BATTERY_DIR_NAME, id);
1332
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;
1343         }
1344         battery->alive = 1;
1345
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");
1349
1350       end:
1351         return result;
1352 }
1353
1354 static void acpi_battery_remove(struct acpi_sbs *sbs, int id)
1355 {
1356
1357         if (sbs->battery[id].battery_entry) {
1358                 acpi_sbs_generic_remove_fs(&(sbs->battery[id].battery_entry),
1359                                            acpi_battery_dir);
1360         }
1361 }
1362
1363 static int acpi_ac_add(struct acpi_sbs *sbs)
1364 {
1365         int result;
1366
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;
1372         }
1373
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;
1382         }
1383
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");
1387
1388       end:
1389
1390         return result;
1391 }
1392
1393 static void acpi_ac_remove(struct acpi_sbs *sbs)
1394 {
1395
1396         if (sbs->ac_entry) {
1397                 acpi_sbs_generic_remove_fs(&sbs->ac_entry, acpi_ac_dir);
1398         }
1399 }
1400
1401 static void acpi_sbs_update_time_run(unsigned long data)
1402 {
1403         acpi_os_execute(OSL_GPE_HANDLER, acpi_sbs_update_time, (void *)data);
1404 }
1405
1406 static int acpi_sbs_update_run(struct acpi_sbs *sbs, int id, int data_type)
1407 {
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;
1420
1421         if (sbs_zombie(sbs)) {
1422                 goto end;
1423         }
1424
1425         if (id >= 0) {
1426                 id_min = id_max = id;
1427         }
1428
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;
1433                 }
1434         }
1435
1436         sbs->run_cnt++;
1437
1438         old_ac_present = sbs->ac.ac_present;
1439
1440         result = acpi_ac_get_present(sbs);
1441         if (result) {
1442                 ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1443                                 "acpi_ac_get_present() failed"));
1444         }
1445
1446         new_ac_present = sbs->ac.ac_present;
1447
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;
1451         }
1452
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"));
1462                 }
1463         }
1464
1465         if (data_type == DATA_TYPE_COMMON) {
1466                 if (!do_ac_init && !update_battery) {
1467                         goto end;
1468                 }
1469         }
1470
1471         if (data_type == DATA_TYPE_AC_STATE && !do_ac_init) {
1472                 goto end;
1473         }
1474
1475         for (id = id_min; id <= id_max; id++) {
1476                 battery = &sbs->battery[id];
1477                 if (battery->alive == 0) {
1478                         continue;
1479                 }
1480
1481                 old_remaining_capacity = battery->state.remaining_capacity;
1482
1483                 old_battery_present = battery->battery_present;
1484
1485                 result = acpi_battery_select(battery);
1486                 if (result) {
1487                         ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1488                                         "acpi_battery_select() failed"));
1489                 }
1490
1491                 result = acpi_battery_get_present(battery);
1492                 if (result) {
1493                         ACPI_EXCEPTION((AE_INFO, AE_ERROR,
1494                                         "acpi_battery_get_present() failed"));
1495                 }
1496
1497                 new_battery_present = battery->battery_present;
1498
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"));
1509                         }
1510                 }
1511                 if (sbs_zombie(sbs)) {
1512                         goto end;
1513                 }
1514
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"));
1522                         }
1523                 }
1524                 if (data_type == DATA_TYPE_INFO) {
1525                         continue;
1526                 }
1527                 if (sbs_zombie(sbs)) {
1528                         goto end;
1529                 }
1530
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"));
1538                         }
1539                 }
1540                 if (data_type == DATA_TYPE_STATE) {
1541                         goto event;
1542                 }
1543                 if (sbs_zombie(sbs)) {
1544                         goto end;
1545                 }
1546
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"));
1555                         }
1556                 }
1557                 if (data_type == DATA_TYPE_ALARM) {
1558                         continue;
1559                 }
1560                 if (sbs_zombie(sbs)) {
1561                         goto end;
1562                 }
1563
1564               event:
1565
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"));
1579                         }
1580                 }
1581         }
1582
1583       end:
1584
1585         return result;
1586 }
1587
1588 static void acpi_sbs_update_time(void *data)
1589 {
1590         struct acpi_sbs *sbs = data;
1591         unsigned long delay = -1;
1592         int result;
1593         unsigned int up_tm = update_time;
1594
1595         if (sbs_mutex_lock(sbs))
1596                 return;
1597
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"));
1602         }
1603
1604         if (sbs_zombie(sbs)) {
1605                 goto end;
1606         }
1607
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);
1621                 }
1622         }
1623
1624       end:
1625
1626         sbs_mutex_unlock(sbs);
1627 }
1628
1629 static int acpi_sbs_add(struct acpi_device *device)
1630 {
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;
1636
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;
1642         }
1643
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;
1649         }
1650
1651         mutex_init(&sbs->mutex);
1652
1653         sbs_mutex_lock(sbs);
1654
1655         sbs->base = 0xff & (val >> 8);
1656         sbs->device = device;
1657
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;
1661
1662         result = acpi_ac_add(sbs);
1663         if (result) {
1664                 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "acpi_ac_add() failed"));
1665                 goto end;
1666         }
1667
1668         acpi_sbsm_get_info(sbs);
1669
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;
1676                 }
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;
1685                                 }
1686                         }
1687                 }
1688         }
1689
1690         init_timer(&sbs->update_timer);
1691         result = acpi_check_update_proc(sbs);
1692         if (result)
1693                 goto end;
1694
1695       end:
1696
1697         sbs_mutex_unlock(sbs);
1698
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"));
1704                 }
1705         }
1706
1707         return result;
1708 }
1709
1710 static int acpi_sbs_remove(struct acpi_device *device, int type)
1711 {
1712         struct acpi_sbs *sbs;
1713         int id;
1714
1715         if (!device) {
1716                 return -EINVAL;
1717         }
1718
1719         sbs = acpi_driver_data(device);
1720         if (!sbs) {
1721                 return -EINVAL;
1722         }
1723
1724         sbs_mutex_lock(sbs);
1725
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);
1730
1731         for (id = 0; id < MAX_SBS_BAT; id++) {
1732                 acpi_battery_remove(sbs, id);
1733         }
1734
1735         acpi_ac_remove(sbs);
1736
1737         sbs_mutex_unlock(sbs);
1738
1739         mutex_destroy(&sbs->mutex);
1740
1741         kfree(sbs);
1742
1743         return 0;
1744 }
1745
1746 static void acpi_sbs_rmdirs(void)
1747 {
1748         if (acpi_ac_dir) {
1749                 acpi_unlock_ac_dir(acpi_ac_dir);
1750                 acpi_ac_dir = NULL;
1751         }
1752         if (acpi_battery_dir) {
1753                 acpi_unlock_battery_dir(acpi_battery_dir);
1754                 acpi_battery_dir = NULL;
1755         }
1756 }
1757
1758 static int acpi_sbs_resume(struct acpi_device *device)
1759 {
1760         struct acpi_sbs *sbs;
1761
1762         if (!device)
1763                 return -EINVAL;
1764
1765         sbs = device->driver_data;
1766
1767         sbs->run_cnt = 0;
1768
1769         return 0;
1770 }
1771
1772 static int __init acpi_sbs_init(void)
1773 {
1774         int result = 0;
1775
1776         if (acpi_disabled)
1777                 return -ENODEV;
1778
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;
1785         }
1786
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;
1792         }
1793
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;
1800         }
1801
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;
1808         }
1809
1810         return 0;
1811 }
1812
1813 static void __exit acpi_sbs_exit(void)
1814 {
1815         acpi_bus_unregister_driver(&acpi_sbs_driver);
1816
1817         acpi_sbs_rmdirs();
1818
1819         return;
1820 }
1821
1822 module_init(acpi_sbs_init);
1823 module_exit(acpi_sbs_exit);