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