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