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