Pull ec into release branch
[linux-2.6] / drivers / acpi / battery.c
1 /*
2  *  acpi_battery.c - ACPI Battery Driver ($Revision: 37 $)
3  *
4  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
5  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
6  *
7  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
8  *
9  *  This program is free software; you can redistribute it and/or modify
10  *  it under the terms of the GNU General Public License as published by
11  *  the Free Software Foundation; either version 2 of the License, or (at
12  *  your option) any later version.
13  *
14  *  This program is distributed in the hope that it will be useful, but
15  *  WITHOUT ANY WARRANTY; without even the implied warranty of
16  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  *  General Public License for more details.
18  *
19  *  You should have received a copy of the GNU General Public License along
20  *  with this program; if not, write to the Free Software Foundation, Inc.,
21  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
22  *
23  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
24  */
25
26 #include <linux/kernel.h>
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/types.h>
30 #include <linux/proc_fs.h>
31 #include <linux/seq_file.h>
32 #include <asm/uaccess.h>
33
34 #include <acpi/acpi_bus.h>
35 #include <acpi/acpi_drivers.h>
36
37 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
38
39 #define ACPI_BATTERY_FORMAT_BIF "NNNNNNNNNSSSS"
40 #define ACPI_BATTERY_FORMAT_BST "NNNN"
41
42 #define ACPI_BATTERY_COMPONENT          0x00040000
43 #define ACPI_BATTERY_CLASS              "battery"
44 #define ACPI_BATTERY_HID                "PNP0C0A"
45 #define ACPI_BATTERY_DEVICE_NAME        "Battery"
46 #define ACPI_BATTERY_NOTIFY_STATUS      0x80
47 #define ACPI_BATTERY_NOTIFY_INFO        0x81
48 #define ACPI_BATTERY_UNITS_WATTS        "mW"
49 #define ACPI_BATTERY_UNITS_AMPS         "mA"
50
51 #define _COMPONENT              ACPI_BATTERY_COMPONENT
52
53 #define ACPI_BATTERY_UPDATE_TIME        0
54
55 #define ACPI_BATTERY_NONE_UPDATE        0
56 #define ACPI_BATTERY_EASY_UPDATE        1
57 #define ACPI_BATTERY_INIT_UPDATE        2
58
59 ACPI_MODULE_NAME("battery");
60
61 MODULE_AUTHOR("Paul Diefenbaugh");
62 MODULE_DESCRIPTION("ACPI Battery Driver");
63 MODULE_LICENSE("GPL");
64
65 static unsigned int update_time = ACPI_BATTERY_UPDATE_TIME;
66
67 /* 0 - every time, > 0 - by update_time */
68 module_param(update_time, uint, 0644);
69
70 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
71 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
72
73 static int acpi_battery_add(struct acpi_device *device);
74 static int acpi_battery_remove(struct acpi_device *device, int type);
75 static int acpi_battery_resume(struct acpi_device *device);
76
77 static struct acpi_driver acpi_battery_driver = {
78         .name = "battery",
79         .class = ACPI_BATTERY_CLASS,
80         .ids = ACPI_BATTERY_HID,
81         .ops = {
82                 .add = acpi_battery_add,
83                 .resume = acpi_battery_resume,
84                 .remove = acpi_battery_remove,
85                 },
86 };
87
88 struct acpi_battery_state {
89         acpi_integer state;
90         acpi_integer present_rate;
91         acpi_integer remaining_capacity;
92         acpi_integer present_voltage;
93 };
94
95 struct acpi_battery_info {
96         acpi_integer power_unit;
97         acpi_integer design_capacity;
98         acpi_integer last_full_capacity;
99         acpi_integer battery_technology;
100         acpi_integer design_voltage;
101         acpi_integer design_capacity_warning;
102         acpi_integer design_capacity_low;
103         acpi_integer battery_capacity_granularity_1;
104         acpi_integer battery_capacity_granularity_2;
105         acpi_string model_number;
106         acpi_string serial_number;
107         acpi_string battery_type;
108         acpi_string oem_info;
109 };
110
111 enum acpi_battery_files{
112         ACPI_BATTERY_INFO = 0,
113         ACPI_BATTERY_STATE,
114         ACPI_BATTERY_ALARM,
115         ACPI_BATTERY_NUMFILES,
116 };
117
118 struct acpi_battery_flags {
119         u8 battery_present_prev;
120         u8 alarm_present;
121         u8 init_update;
122         u8 update[ACPI_BATTERY_NUMFILES];
123         u8 power_unit;
124 };
125
126 struct acpi_battery {
127         struct mutex mutex;
128         struct acpi_device *device;
129         struct acpi_battery_flags flags;
130         struct acpi_buffer bif_data;
131         struct acpi_buffer bst_data;
132         unsigned long alarm;
133         unsigned long update_time[ACPI_BATTERY_NUMFILES];
134 };
135
136 inline int acpi_battery_present(struct acpi_battery *battery)
137 {
138         return battery->device->status.battery_present;
139 }
140 inline char *acpi_battery_power_units(struct acpi_battery *battery)
141 {
142         if (battery->flags.power_unit)
143                 return ACPI_BATTERY_UNITS_AMPS;
144         else
145                 return ACPI_BATTERY_UNITS_WATTS;
146 }
147
148 inline acpi_handle acpi_battery_handle(struct acpi_battery *battery)
149 {
150         return battery->device->handle;
151 }
152
153 /* --------------------------------------------------------------------------
154                                Battery Management
155    -------------------------------------------------------------------------- */
156
157 static void acpi_battery_check_result(struct acpi_battery *battery, int result)
158 {
159         if (!battery)
160                 return;
161
162         if (result) {
163                 battery->flags.init_update = 1;
164         }
165 }
166
167 static int acpi_battery_extract_package(struct acpi_battery *battery,
168                                         union acpi_object *package,
169                                         struct acpi_buffer *format,
170                                         struct acpi_buffer *data,
171                                         char *package_name)
172 {
173         acpi_status status = AE_OK;
174         struct acpi_buffer data_null = { 0, NULL };
175
176         status = acpi_extract_package(package, format, &data_null);
177         if (status != AE_BUFFER_OVERFLOW) {
178                 ACPI_EXCEPTION((AE_INFO, status, "Extracting size %s",
179                                 package_name));
180                 return -ENODEV;
181         }
182
183         if (data_null.length != data->length) {
184                 kfree(data->pointer);
185                 data->pointer = kzalloc(data_null.length, GFP_KERNEL);
186                 if (!data->pointer) {
187                         ACPI_EXCEPTION((AE_INFO, AE_NO_MEMORY, "kzalloc()"));
188                         return -ENOMEM;
189                 }
190                 data->length = data_null.length;
191         }
192
193         status = acpi_extract_package(package, format, data);
194         if (ACPI_FAILURE(status)) {
195                 ACPI_EXCEPTION((AE_INFO, status, "Extracting %s",
196                                 package_name));
197                 return -ENODEV;
198         }
199
200         return 0;
201 }
202
203 static int acpi_battery_get_status(struct acpi_battery *battery)
204 {
205         int result = 0;
206
207         result = acpi_bus_get_status(battery->device);
208         if (result) {
209                 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
210                 return -ENODEV;
211         }
212         return result;
213 }
214
215 static int acpi_battery_get_info(struct acpi_battery *battery)
216 {
217         int result = 0;
218         acpi_status status = 0;
219         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
220         struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BIF),
221                 ACPI_BATTERY_FORMAT_BIF
222         };
223         union acpi_object *package = NULL;
224         struct acpi_buffer *data = NULL;
225         struct acpi_battery_info *bif = NULL;
226
227         battery->update_time[ACPI_BATTERY_INFO] = get_seconds();
228
229         if (!acpi_battery_present(battery))
230                 return 0;
231
232         /* Evaluate _BIF */
233
234         status =
235             acpi_evaluate_object(acpi_battery_handle(battery), "_BIF", NULL,
236                                  &buffer);
237         if (ACPI_FAILURE(status)) {
238                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
239                 return -ENODEV;
240         }
241
242         package = buffer.pointer;
243
244         data = &battery->bif_data;
245
246         /* Extract Package Data */
247
248         result =
249             acpi_battery_extract_package(battery, package, &format, data,
250                                          "_BIF");
251         if (result)
252                 goto end;
253
254       end:
255
256         kfree(buffer.pointer);
257
258         if (!result) {
259                 bif = data->pointer;
260                 battery->flags.power_unit = bif->power_unit;
261         }
262
263         return result;
264 }
265
266 static int acpi_battery_get_state(struct acpi_battery *battery)
267 {
268         int result = 0;
269         acpi_status status = 0;
270         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
271         struct acpi_buffer format = { sizeof(ACPI_BATTERY_FORMAT_BST),
272                 ACPI_BATTERY_FORMAT_BST
273         };
274         union acpi_object *package = NULL;
275         struct acpi_buffer *data = NULL;
276
277         battery->update_time[ACPI_BATTERY_STATE] = get_seconds();
278
279         if (!acpi_battery_present(battery))
280                 return 0;
281
282         /* Evaluate _BST */
283
284         status =
285             acpi_evaluate_object(acpi_battery_handle(battery), "_BST", NULL,
286                                  &buffer);
287         if (ACPI_FAILURE(status)) {
288                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
289                 return -ENODEV;
290         }
291
292         package = buffer.pointer;
293
294         data = &battery->bst_data;
295
296         /* Extract Package Data */
297
298         result =
299             acpi_battery_extract_package(battery, package, &format, data,
300                                          "_BST");
301         if (result)
302                 goto end;
303
304       end:
305         kfree(buffer.pointer);
306
307         return result;
308 }
309
310 static int acpi_battery_get_alarm(struct acpi_battery *battery)
311 {
312         battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
313
314         return 0;
315 }
316
317 static int acpi_battery_set_alarm(struct acpi_battery *battery,
318                                   unsigned long alarm)
319 {
320         acpi_status status = 0;
321         union acpi_object arg0 = { ACPI_TYPE_INTEGER };
322         struct acpi_object_list arg_list = { 1, &arg0 };
323
324         battery->update_time[ACPI_BATTERY_ALARM] = get_seconds();
325
326         if (!acpi_battery_present(battery))
327                 return -ENODEV;
328
329         if (!battery->flags.alarm_present)
330                 return -ENODEV;
331
332         arg0.integer.value = alarm;
333
334         status =
335             acpi_evaluate_object(acpi_battery_handle(battery), "_BTP",
336                                  &arg_list, NULL);
337         if (ACPI_FAILURE(status))
338                 return -ENODEV;
339
340         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", (u32) alarm));
341
342         battery->alarm = alarm;
343
344         return 0;
345 }
346
347 static int acpi_battery_init_alarm(struct acpi_battery *battery)
348 {
349         int result = 0;
350         acpi_status status = AE_OK;
351         acpi_handle handle = NULL;
352         struct acpi_battery_info *bif = battery->bif_data.pointer;
353         unsigned long alarm = battery->alarm;
354
355         /* See if alarms are supported, and if so, set default */
356
357         status = acpi_get_handle(acpi_battery_handle(battery), "_BTP", &handle);
358         if (ACPI_SUCCESS(status)) {
359                 battery->flags.alarm_present = 1;
360                 if (!alarm && bif) {
361                         alarm = bif->design_capacity_warning;
362                 }
363                 result = acpi_battery_set_alarm(battery, alarm);
364                 if (result)
365                         goto end;
366         } else {
367                 battery->flags.alarm_present = 0;
368         }
369
370       end:
371
372         return result;
373 }
374
375 static int acpi_battery_init_update(struct acpi_battery *battery)
376 {
377         int result = 0;
378
379         result = acpi_battery_get_status(battery);
380         if (result)
381                 return result;
382
383         battery->flags.battery_present_prev = acpi_battery_present(battery);
384
385         if (acpi_battery_present(battery)) {
386                 result = acpi_battery_get_info(battery);
387                 if (result)
388                         return result;
389                 result = acpi_battery_get_state(battery);
390                 if (result)
391                         return result;
392
393                 acpi_battery_init_alarm(battery);
394         }
395
396         return result;
397 }
398
399 static int acpi_battery_update(struct acpi_battery *battery,
400                                int update, int *update_result_ptr)
401 {
402         int result = 0;
403         int update_result = ACPI_BATTERY_NONE_UPDATE;
404
405         if (!acpi_battery_present(battery)) {
406                 update = 1;
407         }
408
409         if (battery->flags.init_update) {
410                 result = acpi_battery_init_update(battery);
411                 if (result)
412                         goto end;
413                 update_result = ACPI_BATTERY_INIT_UPDATE;
414         } else if (update) {
415                 result = acpi_battery_get_status(battery);
416                 if (result)
417                         goto end;
418                 if ((!battery->flags.battery_present_prev & acpi_battery_present(battery))
419                     || (battery->flags.battery_present_prev & !acpi_battery_present(battery))) {
420                         result = acpi_battery_init_update(battery);
421                         if (result)
422                                 goto end;
423                         update_result = ACPI_BATTERY_INIT_UPDATE;
424                 } else {
425                         update_result = ACPI_BATTERY_EASY_UPDATE;
426                 }
427         }
428
429       end:
430
431         battery->flags.init_update = (result != 0);
432
433         *update_result_ptr = update_result;
434
435         return result;
436 }
437
438 static void acpi_battery_notify_update(struct acpi_battery *battery)
439 {
440         acpi_battery_get_status(battery);
441
442         if (battery->flags.init_update) {
443                 return;
444         }
445
446         if ((!battery->flags.battery_present_prev &
447              acpi_battery_present(battery)) ||
448             (battery->flags.battery_present_prev &
449              !acpi_battery_present(battery))) {
450                 battery->flags.init_update = 1;
451         } else {
452                 battery->flags.update[ACPI_BATTERY_INFO] = 1;
453                 battery->flags.update[ACPI_BATTERY_STATE] = 1;
454                 battery->flags.update[ACPI_BATTERY_ALARM] = 1;
455         }
456 }
457
458 /* --------------------------------------------------------------------------
459                               FS Interface (/proc)
460    -------------------------------------------------------------------------- */
461
462 static struct proc_dir_entry *acpi_battery_dir;
463
464 static int acpi_battery_print_info(struct seq_file *seq, int result)
465 {
466         struct acpi_battery *battery = seq->private;
467         struct acpi_battery_info *bif = NULL;
468         char *units = "?";
469
470         if (result)
471                 goto end;
472
473         if (acpi_battery_present(battery))
474                 seq_printf(seq, "present:                 yes\n");
475         else {
476                 seq_printf(seq, "present:                 no\n");
477                 goto end;
478         }
479
480         bif = battery->bif_data.pointer;
481         if (!bif) {
482                 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BIF buffer is NULL"));
483                 result = -ENODEV;
484                 goto end;
485         }
486
487         /* Battery Units */
488
489         units = acpi_battery_power_units(battery);
490
491         if (bif->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
492                 seq_printf(seq, "design capacity:         unknown\n");
493         else
494                 seq_printf(seq, "design capacity:         %d %sh\n",
495                            (u32) bif->design_capacity, units);
496
497         if (bif->last_full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
498                 seq_printf(seq, "last full capacity:      unknown\n");
499         else
500                 seq_printf(seq, "last full capacity:      %d %sh\n",
501                            (u32) bif->last_full_capacity, units);
502
503         switch ((u32) bif->battery_technology) {
504         case 0:
505                 seq_printf(seq, "battery technology:      non-rechargeable\n");
506                 break;
507         case 1:
508                 seq_printf(seq, "battery technology:      rechargeable\n");
509                 break;
510         default:
511                 seq_printf(seq, "battery technology:      unknown\n");
512                 break;
513         }
514
515         if (bif->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
516                 seq_printf(seq, "design voltage:          unknown\n");
517         else
518                 seq_printf(seq, "design voltage:          %d mV\n",
519                            (u32) bif->design_voltage);
520         seq_printf(seq, "design capacity warning: %d %sh\n",
521                    (u32) bif->design_capacity_warning, units);
522         seq_printf(seq, "design capacity low:     %d %sh\n",
523                    (u32) bif->design_capacity_low, units);
524         seq_printf(seq, "capacity granularity 1:  %d %sh\n",
525                    (u32) bif->battery_capacity_granularity_1, units);
526         seq_printf(seq, "capacity granularity 2:  %d %sh\n",
527                    (u32) bif->battery_capacity_granularity_2, units);
528         seq_printf(seq, "model number:            %s\n", bif->model_number);
529         seq_printf(seq, "serial number:           %s\n", bif->serial_number);
530         seq_printf(seq, "battery type:            %s\n", bif->battery_type);
531         seq_printf(seq, "OEM info:                %s\n", bif->oem_info);
532
533       end:
534
535         if (result)
536                 seq_printf(seq, "ERROR: Unable to read battery info\n");
537
538         return result;
539 }
540
541 static int acpi_battery_print_state(struct seq_file *seq, int result)
542 {
543         struct acpi_battery *battery = seq->private;
544         struct acpi_battery_state *bst = NULL;
545         char *units = "?";
546
547         if (result)
548                 goto end;
549
550         if (acpi_battery_present(battery))
551                 seq_printf(seq, "present:                 yes\n");
552         else {
553                 seq_printf(seq, "present:                 no\n");
554                 goto end;
555         }
556
557         bst = battery->bst_data.pointer;
558         if (!bst) {
559                 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "BST buffer is NULL"));
560                 result = -ENODEV;
561                 goto end;
562         }
563
564         /* Battery Units */
565
566         units = acpi_battery_power_units(battery);
567
568         if (!(bst->state & 0x04))
569                 seq_printf(seq, "capacity state:          ok\n");
570         else
571                 seq_printf(seq, "capacity state:          critical\n");
572
573         if ((bst->state & 0x01) && (bst->state & 0x02)) {
574                 seq_printf(seq,
575                            "charging state:          charging/discharging\n");
576         } else if (bst->state & 0x01)
577                 seq_printf(seq, "charging state:          discharging\n");
578         else if (bst->state & 0x02)
579                 seq_printf(seq, "charging state:          charging\n");
580         else {
581                 seq_printf(seq, "charging state:          charged\n");
582         }
583
584         if (bst->present_rate == ACPI_BATTERY_VALUE_UNKNOWN)
585                 seq_printf(seq, "present rate:            unknown\n");
586         else
587                 seq_printf(seq, "present rate:            %d %s\n",
588                            (u32) bst->present_rate, units);
589
590         if (bst->remaining_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
591                 seq_printf(seq, "remaining capacity:      unknown\n");
592         else
593                 seq_printf(seq, "remaining capacity:      %d %sh\n",
594                            (u32) bst->remaining_capacity, units);
595
596         if (bst->present_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
597                 seq_printf(seq, "present voltage:         unknown\n");
598         else
599                 seq_printf(seq, "present voltage:         %d mV\n",
600                            (u32) bst->present_voltage);
601
602       end:
603
604         if (result) {
605                 seq_printf(seq, "ERROR: Unable to read battery state\n");
606         }
607
608         return result;
609 }
610
611 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
612 {
613         struct acpi_battery *battery = seq->private;
614         char *units = "?";
615
616         if (result)
617                 goto end;
618
619         if (!acpi_battery_present(battery)) {
620                 seq_printf(seq, "present:                 no\n");
621                 goto end;
622         }
623
624         /* Battery Units */
625
626         units = acpi_battery_power_units(battery);
627
628         seq_printf(seq, "alarm:                   ");
629         if (!battery->alarm)
630                 seq_printf(seq, "unsupported\n");
631         else
632                 seq_printf(seq, "%lu %sh\n", battery->alarm, units);
633
634       end:
635
636         if (result)
637                 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
638
639         return result;
640 }
641
642 static ssize_t
643 acpi_battery_write_alarm(struct file *file,
644                          const char __user * buffer,
645                          size_t count, loff_t * ppos)
646 {
647         int result = 0;
648         char alarm_string[12] = { '\0' };
649         struct seq_file *m = file->private_data;
650         struct acpi_battery *battery = m->private;
651         int update_result = ACPI_BATTERY_NONE_UPDATE;
652
653         if (!battery || (count > sizeof(alarm_string) - 1))
654                 return -EINVAL;
655
656         mutex_lock(&battery->mutex);
657
658         result = acpi_battery_update(battery, 1, &update_result);
659         if (result) {
660                 result = -ENODEV;
661                 goto end;
662         }
663
664         if (!acpi_battery_present(battery)) {
665                 result = -ENODEV;
666                 goto end;
667         }
668
669         if (copy_from_user(alarm_string, buffer, count)) {
670                 result = -EFAULT;
671                 goto end;
672         }
673
674         alarm_string[count] = '\0';
675
676         result = acpi_battery_set_alarm(battery,
677                                         simple_strtoul(alarm_string, NULL, 0));
678         if (result)
679                 goto end;
680
681       end:
682
683         acpi_battery_check_result(battery, result);
684
685         if (!result)
686                 result = count;
687
688         mutex_unlock(&battery->mutex);
689
690         return result;
691 }
692
693 typedef int(*print_func)(struct seq_file *seq, int result);
694 typedef int(*get_func)(struct acpi_battery *battery);
695
696 static struct acpi_read_mux {
697         print_func print;
698         get_func get;
699 } acpi_read_funcs[ACPI_BATTERY_NUMFILES] = {
700         {.get = acpi_battery_get_info, .print = acpi_battery_print_info},
701         {.get = acpi_battery_get_state, .print = acpi_battery_print_state},
702         {.get = acpi_battery_get_alarm, .print = acpi_battery_print_alarm},
703 };
704
705 static int acpi_battery_read(int fid, struct seq_file *seq)
706 {
707         struct acpi_battery *battery = seq->private;
708         int result = 0;
709         int update_result = ACPI_BATTERY_NONE_UPDATE;
710         int update = 0;
711
712         mutex_lock(&battery->mutex);
713
714         update = (get_seconds() - battery->update_time[fid] >= update_time);
715         update = (update | battery->flags.update[fid]);
716
717         result = acpi_battery_update(battery, update, &update_result);
718         if (result)
719                 goto end;
720
721         if (update_result == ACPI_BATTERY_EASY_UPDATE) {
722                 result = acpi_read_funcs[fid].get(battery);
723                 if (result)
724                         goto end;
725         }
726
727       end:
728         result = acpi_read_funcs[fid].print(seq, result);
729         acpi_battery_check_result(battery, result);
730         battery->flags.update[fid] = result;
731         mutex_unlock(&battery->mutex);
732         return result;
733 }
734
735 static int acpi_battery_read_info(struct seq_file *seq, void *offset)
736 {
737         return acpi_battery_read(ACPI_BATTERY_INFO, seq);
738 }
739
740 static int acpi_battery_read_state(struct seq_file *seq, void *offset)
741 {
742         return acpi_battery_read(ACPI_BATTERY_STATE, seq);
743 }
744
745 static int acpi_battery_read_alarm(struct seq_file *seq, void *offset)
746 {
747         return acpi_battery_read(ACPI_BATTERY_ALARM, seq);
748 }
749
750 static int acpi_battery_info_open_fs(struct inode *inode, struct file *file)
751 {
752         return single_open(file, acpi_battery_read_info, PDE(inode)->data);
753 }
754
755 static int acpi_battery_state_open_fs(struct inode *inode, struct file *file)
756 {
757         return single_open(file, acpi_battery_read_state, PDE(inode)->data);
758 }
759
760 static int acpi_battery_alarm_open_fs(struct inode *inode, struct file *file)
761 {
762         return single_open(file, acpi_battery_read_alarm, PDE(inode)->data);
763 }
764
765 static struct battery_file {
766         struct file_operations ops;
767         mode_t mode;
768         char *name;
769 } acpi_battery_file[] = {
770         {
771         .name = "info",
772         .mode = S_IRUGO,
773         .ops = {
774         .open = acpi_battery_info_open_fs,
775         .read = seq_read,
776         .llseek = seq_lseek,
777         .release = single_release,
778         .owner = THIS_MODULE,
779         },
780         },
781         {
782         .name = "state",
783         .mode = S_IRUGO,
784         .ops = {
785         .open = acpi_battery_state_open_fs,
786         .read = seq_read,
787         .llseek = seq_lseek,
788         .release = single_release,
789         .owner = THIS_MODULE,
790         },
791         },
792         {
793         .name = "alarm",
794         .mode = S_IFREG | S_IRUGO | S_IWUSR,
795         .ops = {
796         .open = acpi_battery_alarm_open_fs,
797         .read = seq_read,
798         .write = acpi_battery_write_alarm,
799         .llseek = seq_lseek,
800         .release = single_release,
801         .owner = THIS_MODULE,
802         },
803         },
804 };
805
806 static int acpi_battery_add_fs(struct acpi_device *device)
807 {
808         struct proc_dir_entry *entry = NULL;
809         int i;
810
811         if (!acpi_device_dir(device)) {
812                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
813                                                      acpi_battery_dir);
814                 if (!acpi_device_dir(device))
815                         return -ENODEV;
816                 acpi_device_dir(device)->owner = THIS_MODULE;
817         }
818
819         for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
820                 entry = create_proc_entry(acpi_battery_file[i].name,
821                                   acpi_battery_file[i].mode, acpi_device_dir(device));
822                 if (!entry)
823                         return -ENODEV;
824                 else {
825                         entry->proc_fops = &acpi_battery_file[i].ops;
826                         entry->data = acpi_driver_data(device);
827                         entry->owner = THIS_MODULE;
828                 }
829         }
830
831         return 0;
832 }
833
834 static int acpi_battery_remove_fs(struct acpi_device *device)
835 {
836         int i;
837         if (acpi_device_dir(device)) {
838                 for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
839                         remove_proc_entry(acpi_battery_file[i].name,
840                                   acpi_device_dir(device));
841                 }
842                 remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
843                 acpi_device_dir(device) = NULL;
844         }
845
846         return 0;
847 }
848
849 /* --------------------------------------------------------------------------
850                                  Driver Interface
851    -------------------------------------------------------------------------- */
852
853 static void acpi_battery_notify(acpi_handle handle, u32 event, void *data)
854 {
855         struct acpi_battery *battery = data;
856         struct acpi_device *device = NULL;
857
858         if (!battery)
859                 return;
860
861         device = battery->device;
862
863         switch (event) {
864         case ACPI_BATTERY_NOTIFY_STATUS:
865         case ACPI_BATTERY_NOTIFY_INFO:
866         case ACPI_NOTIFY_BUS_CHECK:
867         case ACPI_NOTIFY_DEVICE_CHECK:
868                 device = battery->device;
869                 acpi_battery_notify_update(battery);
870                 acpi_bus_generate_event(device, event,
871                                         acpi_battery_present(battery));
872                 break;
873         default:
874                 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
875                                   "Unsupported event [0x%x]\n", event));
876                 break;
877         }
878
879         return;
880 }
881
882 static int acpi_battery_add(struct acpi_device *device)
883 {
884         int result = 0;
885         acpi_status status = 0;
886         struct acpi_battery *battery = NULL;
887
888         if (!device)
889                 return -EINVAL;
890
891         battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
892         if (!battery)
893                 return -ENOMEM;
894
895         mutex_init(&battery->mutex);
896
897         mutex_lock(&battery->mutex);
898
899         battery->device = device;
900         strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
901         strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
902         acpi_driver_data(device) = battery;
903
904         result = acpi_battery_get_status(battery);
905         if (result)
906                 goto end;
907
908         battery->flags.init_update = 1;
909
910         result = acpi_battery_add_fs(device);
911         if (result)
912                 goto end;
913
914         status = acpi_install_notify_handler(device->handle,
915                                              ACPI_ALL_NOTIFY,
916                                              acpi_battery_notify, battery);
917         if (ACPI_FAILURE(status)) {
918                 ACPI_EXCEPTION((AE_INFO, status, "Installing notify handler"));
919                 result = -ENODEV;
920                 goto end;
921         }
922
923         printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
924                ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
925                device->status.battery_present ? "present" : "absent");
926
927       end:
928
929         if (result) {
930                 acpi_battery_remove_fs(device);
931                 kfree(battery);
932         }
933
934         mutex_unlock(&battery->mutex);
935
936         return result;
937 }
938
939 static int acpi_battery_remove(struct acpi_device *device, int type)
940 {
941         acpi_status status = 0;
942         struct acpi_battery *battery = NULL;
943
944         if (!device || !acpi_driver_data(device))
945                 return -EINVAL;
946
947         battery = acpi_driver_data(device);
948
949         mutex_lock(&battery->mutex);
950
951         status = acpi_remove_notify_handler(device->handle,
952                                             ACPI_ALL_NOTIFY,
953                                             acpi_battery_notify);
954
955         acpi_battery_remove_fs(device);
956
957         kfree(battery->bif_data.pointer);
958
959         kfree(battery->bst_data.pointer);
960
961         mutex_unlock(&battery->mutex);
962
963         mutex_destroy(&battery->mutex);
964
965         kfree(battery);
966
967         return 0;
968 }
969
970 /* this is needed to learn about changes made in suspended state */
971 static int acpi_battery_resume(struct acpi_device *device)
972 {
973         struct acpi_battery *battery;
974
975         if (!device)
976                 return -EINVAL;
977
978         battery = device->driver_data;
979
980         battery->flags.init_update = 1;
981
982         return 0;
983 }
984
985 static int __init acpi_battery_init(void)
986 {
987         int result;
988
989         if (acpi_disabled)
990                 return -ENODEV;
991
992         acpi_battery_dir = acpi_lock_battery_dir();
993         if (!acpi_battery_dir)
994                 return -ENODEV;
995
996         result = acpi_bus_register_driver(&acpi_battery_driver);
997         if (result < 0) {
998                 acpi_unlock_battery_dir(acpi_battery_dir);
999                 return -ENODEV;
1000         }
1001
1002         return 0;
1003 }
1004
1005 static void __exit acpi_battery_exit(void)
1006 {
1007         acpi_bus_unregister_driver(&acpi_battery_driver);
1008
1009         acpi_unlock_battery_dir(acpi_battery_dir);
1010
1011         return;
1012 }
1013
1014 module_init(acpi_battery_init);
1015 module_exit(acpi_battery_exit);