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