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