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