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