ACPI: Move definition of PREFIX from acpi_bus.h to internal..h
[safe/jmp/linux-2.6] / drivers / acpi / battery.c
1 /*
2  *  battery.c - ACPI Battery Driver (Revision: 2.0)
3  *
4  *  Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5  *  Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8  *
9  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10  *
11  *  This program is free software; you can redistribute it and/or modify
12  *  it under the terms of the GNU General Public License as published by
13  *  the Free Software Foundation; either version 2 of the License, or (at
14  *  your option) any later version.
15  *
16  *  This program is distributed in the hope that it will be useful, but
17  *  WITHOUT ANY WARRANTY; without even the implied warranty of
18  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  *  General Public License for more details.
20  *
21  *  You should have received a copy of the GNU General Public License along
22  *  with this program; if not, write to the Free Software Foundation, Inc.,
23  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24  *
25  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26  */
27
28 #include <linux/kernel.h>
29 #include <linux/module.h>
30 #include <linux/init.h>
31 #include <linux/types.h>
32 #include <linux/jiffies.h>
33 #include <linux/async.h>
34
35 #ifdef CONFIG_ACPI_PROCFS_POWER
36 #include <linux/proc_fs.h>
37 #include <linux/seq_file.h>
38 #include <asm/uaccess.h>
39 #endif
40
41 #include <acpi/acpi_bus.h>
42 #include <acpi/acpi_drivers.h>
43
44 #ifdef CONFIG_ACPI_SYSFS_POWER
45 #include <linux/power_supply.h>
46 #endif
47
48 #define PREFIX "ACPI: "
49
50 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
51
52 #define ACPI_BATTERY_CLASS              "battery"
53 #define ACPI_BATTERY_DEVICE_NAME        "Battery"
54 #define ACPI_BATTERY_NOTIFY_STATUS      0x80
55 #define ACPI_BATTERY_NOTIFY_INFO        0x81
56
57 #define _COMPONENT              ACPI_BATTERY_COMPONENT
58
59 ACPI_MODULE_NAME("battery");
60
61 MODULE_AUTHOR("Paul Diefenbaugh");
62 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
63 MODULE_DESCRIPTION("ACPI Battery Driver");
64 MODULE_LICENSE("GPL");
65
66 static unsigned int cache_time = 1000;
67 module_param(cache_time, uint, 0644);
68 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
69
70 #ifdef CONFIG_ACPI_PROCFS_POWER
71 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
72 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
73
74 enum acpi_battery_files {
75         info_tag = 0,
76         state_tag,
77         alarm_tag,
78         ACPI_BATTERY_NUMFILES,
79 };
80
81 #endif
82
83 static const struct acpi_device_id battery_device_ids[] = {
84         {"PNP0C0A", 0},
85         {"", 0},
86 };
87
88 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
89
90
91 struct acpi_battery {
92         struct mutex lock;
93 #ifdef CONFIG_ACPI_SYSFS_POWER
94         struct power_supply bat;
95 #endif
96         struct acpi_device *device;
97         unsigned long update_time;
98         int rate_now;
99         int capacity_now;
100         int voltage_now;
101         int design_capacity;
102         int full_charge_capacity;
103         int technology;
104         int design_voltage;
105         int design_capacity_warning;
106         int design_capacity_low;
107         int capacity_granularity_1;
108         int capacity_granularity_2;
109         int alarm;
110         char model_number[32];
111         char serial_number[32];
112         char type[32];
113         char oem_info[32];
114         int state;
115         int power_unit;
116         u8 alarm_present;
117 };
118
119 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
120
121 inline int acpi_battery_present(struct acpi_battery *battery)
122 {
123         return battery->device->status.battery_present;
124 }
125
126 #ifdef CONFIG_ACPI_SYSFS_POWER
127 static int acpi_battery_technology(struct acpi_battery *battery)
128 {
129         if (!strcasecmp("NiCd", battery->type))
130                 return POWER_SUPPLY_TECHNOLOGY_NiCd;
131         if (!strcasecmp("NiMH", battery->type))
132                 return POWER_SUPPLY_TECHNOLOGY_NiMH;
133         if (!strcasecmp("LION", battery->type))
134                 return POWER_SUPPLY_TECHNOLOGY_LION;
135         if (!strncasecmp("LI-ION", battery->type, 6))
136                 return POWER_SUPPLY_TECHNOLOGY_LION;
137         if (!strcasecmp("LiP", battery->type))
138                 return POWER_SUPPLY_TECHNOLOGY_LIPO;
139         return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
140 }
141
142 static int acpi_battery_get_state(struct acpi_battery *battery);
143
144 static int acpi_battery_is_charged(struct acpi_battery *battery)
145 {
146         /* either charging or discharging */
147         if (battery->state != 0)
148                 return 0;
149
150         /* battery not reporting charge */
151         if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
152             battery->capacity_now == 0)
153                 return 0;
154
155         /* good batteries update full_charge as the batteries degrade */
156         if (battery->full_charge_capacity == battery->capacity_now)
157                 return 1;
158
159         /* fallback to using design values for broken batteries */
160         if (battery->design_capacity == battery->capacity_now)
161                 return 1;
162
163         /* we don't do any sort of metric based on percentages */
164         return 0;
165 }
166
167 static int acpi_battery_get_property(struct power_supply *psy,
168                                      enum power_supply_property psp,
169                                      union power_supply_propval *val)
170 {
171         struct acpi_battery *battery = to_acpi_battery(psy);
172
173         if (acpi_battery_present(battery)) {
174                 /* run battery update only if it is present */
175                 acpi_battery_get_state(battery);
176         } else if (psp != POWER_SUPPLY_PROP_PRESENT)
177                 return -ENODEV;
178         switch (psp) {
179         case POWER_SUPPLY_PROP_STATUS:
180                 if (battery->state & 0x01)
181                         val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
182                 else if (battery->state & 0x02)
183                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
184                 else if (acpi_battery_is_charged(battery))
185                         val->intval = POWER_SUPPLY_STATUS_FULL;
186                 else
187                         val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
188                 break;
189         case POWER_SUPPLY_PROP_PRESENT:
190                 val->intval = acpi_battery_present(battery);
191                 break;
192         case POWER_SUPPLY_PROP_TECHNOLOGY:
193                 val->intval = acpi_battery_technology(battery);
194                 break;
195         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
196                 val->intval = battery->design_voltage * 1000;
197                 break;
198         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
199                 val->intval = battery->voltage_now * 1000;
200                 break;
201         case POWER_SUPPLY_PROP_CURRENT_NOW:
202         case POWER_SUPPLY_PROP_POWER_NOW:
203                 val->intval = battery->rate_now * 1000;
204                 break;
205         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
206         case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
207                 val->intval = battery->design_capacity * 1000;
208                 break;
209         case POWER_SUPPLY_PROP_CHARGE_FULL:
210         case POWER_SUPPLY_PROP_ENERGY_FULL:
211                 val->intval = battery->full_charge_capacity * 1000;
212                 break;
213         case POWER_SUPPLY_PROP_CHARGE_NOW:
214         case POWER_SUPPLY_PROP_ENERGY_NOW:
215                 val->intval = battery->capacity_now * 1000;
216                 break;
217         case POWER_SUPPLY_PROP_MODEL_NAME:
218                 val->strval = battery->model_number;
219                 break;
220         case POWER_SUPPLY_PROP_MANUFACTURER:
221                 val->strval = battery->oem_info;
222                 break;
223         case POWER_SUPPLY_PROP_SERIAL_NUMBER:
224                 val->strval = battery->serial_number;
225                 break;
226         default:
227                 return -EINVAL;
228         }
229         return 0;
230 }
231
232 static enum power_supply_property charge_battery_props[] = {
233         POWER_SUPPLY_PROP_STATUS,
234         POWER_SUPPLY_PROP_PRESENT,
235         POWER_SUPPLY_PROP_TECHNOLOGY,
236         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
237         POWER_SUPPLY_PROP_VOLTAGE_NOW,
238         POWER_SUPPLY_PROP_CURRENT_NOW,
239         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
240         POWER_SUPPLY_PROP_CHARGE_FULL,
241         POWER_SUPPLY_PROP_CHARGE_NOW,
242         POWER_SUPPLY_PROP_MODEL_NAME,
243         POWER_SUPPLY_PROP_MANUFACTURER,
244         POWER_SUPPLY_PROP_SERIAL_NUMBER,
245 };
246
247 static enum power_supply_property energy_battery_props[] = {
248         POWER_SUPPLY_PROP_STATUS,
249         POWER_SUPPLY_PROP_PRESENT,
250         POWER_SUPPLY_PROP_TECHNOLOGY,
251         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
252         POWER_SUPPLY_PROP_VOLTAGE_NOW,
253         POWER_SUPPLY_PROP_CURRENT_NOW,
254         POWER_SUPPLY_PROP_POWER_NOW,
255         POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
256         POWER_SUPPLY_PROP_ENERGY_FULL,
257         POWER_SUPPLY_PROP_ENERGY_NOW,
258         POWER_SUPPLY_PROP_MODEL_NAME,
259         POWER_SUPPLY_PROP_MANUFACTURER,
260         POWER_SUPPLY_PROP_SERIAL_NUMBER,
261 };
262 #endif
263
264 #ifdef CONFIG_ACPI_PROCFS_POWER
265 inline char *acpi_battery_units(struct acpi_battery *battery)
266 {
267         return (battery->power_unit)?"mA":"mW";
268 }
269 #endif
270
271 /* --------------------------------------------------------------------------
272                                Battery Management
273    -------------------------------------------------------------------------- */
274 struct acpi_offsets {
275         size_t offset;          /* offset inside struct acpi_sbs_battery */
276         u8 mode;                /* int or string? */
277 };
278
279 static struct acpi_offsets state_offsets[] = {
280         {offsetof(struct acpi_battery, state), 0},
281         {offsetof(struct acpi_battery, rate_now), 0},
282         {offsetof(struct acpi_battery, capacity_now), 0},
283         {offsetof(struct acpi_battery, voltage_now), 0},
284 };
285
286 static struct acpi_offsets info_offsets[] = {
287         {offsetof(struct acpi_battery, power_unit), 0},
288         {offsetof(struct acpi_battery, design_capacity), 0},
289         {offsetof(struct acpi_battery, full_charge_capacity), 0},
290         {offsetof(struct acpi_battery, technology), 0},
291         {offsetof(struct acpi_battery, design_voltage), 0},
292         {offsetof(struct acpi_battery, design_capacity_warning), 0},
293         {offsetof(struct acpi_battery, design_capacity_low), 0},
294         {offsetof(struct acpi_battery, capacity_granularity_1), 0},
295         {offsetof(struct acpi_battery, capacity_granularity_2), 0},
296         {offsetof(struct acpi_battery, model_number), 1},
297         {offsetof(struct acpi_battery, serial_number), 1},
298         {offsetof(struct acpi_battery, type), 1},
299         {offsetof(struct acpi_battery, oem_info), 1},
300 };
301
302 static int extract_package(struct acpi_battery *battery,
303                            union acpi_object *package,
304                            struct acpi_offsets *offsets, int num)
305 {
306         int i;
307         union acpi_object *element;
308         if (package->type != ACPI_TYPE_PACKAGE)
309                 return -EFAULT;
310         for (i = 0; i < num; ++i) {
311                 if (package->package.count <= i)
312                         return -EFAULT;
313                 element = &package->package.elements[i];
314                 if (offsets[i].mode) {
315                         u8 *ptr = (u8 *)battery + offsets[i].offset;
316                         if (element->type == ACPI_TYPE_STRING ||
317                             element->type == ACPI_TYPE_BUFFER)
318                                 strncpy(ptr, element->string.pointer, 32);
319                         else if (element->type == ACPI_TYPE_INTEGER) {
320                                 strncpy(ptr, (u8 *)&element->integer.value,
321                                         sizeof(acpi_integer));
322                                 ptr[sizeof(acpi_integer)] = 0;
323                         } else
324                                 *ptr = 0; /* don't have value */
325                 } else {
326                         int *x = (int *)((u8 *)battery + offsets[i].offset);
327                         *x = (element->type == ACPI_TYPE_INTEGER) ?
328                                 element->integer.value : -1;
329                 }
330         }
331         return 0;
332 }
333
334 static int acpi_battery_get_status(struct acpi_battery *battery)
335 {
336         if (acpi_bus_get_status(battery->device)) {
337                 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
338                 return -ENODEV;
339         }
340         return 0;
341 }
342
343 static int acpi_battery_get_info(struct acpi_battery *battery)
344 {
345         int result = -EFAULT;
346         acpi_status status = 0;
347         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
348
349         if (!acpi_battery_present(battery))
350                 return 0;
351         mutex_lock(&battery->lock);
352         status = acpi_evaluate_object(battery->device->handle, "_BIF",
353                                       NULL, &buffer);
354         mutex_unlock(&battery->lock);
355
356         if (ACPI_FAILURE(status)) {
357                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BIF"));
358                 return -ENODEV;
359         }
360
361         result = extract_package(battery, buffer.pointer,
362                                  info_offsets, ARRAY_SIZE(info_offsets));
363         kfree(buffer.pointer);
364         return result;
365 }
366
367 static int acpi_battery_get_state(struct acpi_battery *battery)
368 {
369         int result = 0;
370         acpi_status status = 0;
371         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
372
373         if (!acpi_battery_present(battery))
374                 return 0;
375
376         if (battery->update_time &&
377             time_before(jiffies, battery->update_time +
378                         msecs_to_jiffies(cache_time)))
379                 return 0;
380
381         mutex_lock(&battery->lock);
382         status = acpi_evaluate_object(battery->device->handle, "_BST",
383                                       NULL, &buffer);
384         mutex_unlock(&battery->lock);
385
386         if (ACPI_FAILURE(status)) {
387                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
388                 return -ENODEV;
389         }
390
391         result = extract_package(battery, buffer.pointer,
392                                  state_offsets, ARRAY_SIZE(state_offsets));
393         battery->update_time = jiffies;
394         kfree(buffer.pointer);
395         return result;
396 }
397
398 static int acpi_battery_set_alarm(struct acpi_battery *battery)
399 {
400         acpi_status status = 0;
401         union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
402         struct acpi_object_list arg_list = { 1, &arg0 };
403
404         if (!acpi_battery_present(battery)|| !battery->alarm_present)
405                 return -ENODEV;
406
407         arg0.integer.value = battery->alarm;
408
409         mutex_lock(&battery->lock);
410         status = acpi_evaluate_object(battery->device->handle, "_BTP",
411                                  &arg_list, NULL);
412         mutex_unlock(&battery->lock);
413
414         if (ACPI_FAILURE(status))
415                 return -ENODEV;
416
417         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
418         return 0;
419 }
420
421 static int acpi_battery_init_alarm(struct acpi_battery *battery)
422 {
423         acpi_status status = AE_OK;
424         acpi_handle handle = NULL;
425
426         /* See if alarms are supported, and if so, set default */
427         status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
428         if (ACPI_FAILURE(status)) {
429                 battery->alarm_present = 0;
430                 return 0;
431         }
432         battery->alarm_present = 1;
433         if (!battery->alarm)
434                 battery->alarm = battery->design_capacity_warning;
435         return acpi_battery_set_alarm(battery);
436 }
437
438 #ifdef CONFIG_ACPI_SYSFS_POWER
439 static ssize_t acpi_battery_alarm_show(struct device *dev,
440                                         struct device_attribute *attr,
441                                         char *buf)
442 {
443         struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
444         return sprintf(buf, "%d\n", battery->alarm * 1000);
445 }
446
447 static ssize_t acpi_battery_alarm_store(struct device *dev,
448                                         struct device_attribute *attr,
449                                         const char *buf, size_t count)
450 {
451         unsigned long x;
452         struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
453         if (sscanf(buf, "%ld\n", &x) == 1)
454                 battery->alarm = x/1000;
455         if (acpi_battery_present(battery))
456                 acpi_battery_set_alarm(battery);
457         return count;
458 }
459
460 static struct device_attribute alarm_attr = {
461         .attr = {.name = "alarm", .mode = 0644},
462         .show = acpi_battery_alarm_show,
463         .store = acpi_battery_alarm_store,
464 };
465
466 static int sysfs_add_battery(struct acpi_battery *battery)
467 {
468         int result;
469
470         if (battery->power_unit) {
471                 battery->bat.properties = charge_battery_props;
472                 battery->bat.num_properties =
473                         ARRAY_SIZE(charge_battery_props);
474         } else {
475                 battery->bat.properties = energy_battery_props;
476                 battery->bat.num_properties =
477                         ARRAY_SIZE(energy_battery_props);
478         }
479
480         battery->bat.name = acpi_device_bid(battery->device);
481         battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
482         battery->bat.get_property = acpi_battery_get_property;
483
484         result = power_supply_register(&battery->device->dev, &battery->bat);
485         if (result)
486                 return result;
487         return device_create_file(battery->bat.dev, &alarm_attr);
488 }
489
490 static void sysfs_remove_battery(struct acpi_battery *battery)
491 {
492         if (!battery->bat.dev)
493                 return;
494         device_remove_file(battery->bat.dev, &alarm_attr);
495         power_supply_unregister(&battery->bat);
496         battery->bat.dev = NULL;
497 }
498 #endif
499
500 static int acpi_battery_update(struct acpi_battery *battery)
501 {
502         int result, old_present = acpi_battery_present(battery);
503         result = acpi_battery_get_status(battery);
504         if (result)
505                 return result;
506 #ifdef CONFIG_ACPI_SYSFS_POWER
507         if (!acpi_battery_present(battery)) {
508                 sysfs_remove_battery(battery);
509                 battery->update_time = 0;
510                 return 0;
511         }
512 #endif
513         if (!battery->update_time ||
514             old_present != acpi_battery_present(battery)) {
515                 result = acpi_battery_get_info(battery);
516                 if (result)
517                         return result;
518                 acpi_battery_init_alarm(battery);
519         }
520 #ifdef CONFIG_ACPI_SYSFS_POWER
521         if (!battery->bat.dev)
522                 sysfs_add_battery(battery);
523 #endif
524         return acpi_battery_get_state(battery);
525 }
526
527 /* --------------------------------------------------------------------------
528                               FS Interface (/proc)
529    -------------------------------------------------------------------------- */
530
531 #ifdef CONFIG_ACPI_PROCFS_POWER
532 static struct proc_dir_entry *acpi_battery_dir;
533
534 static int acpi_battery_print_info(struct seq_file *seq, int result)
535 {
536         struct acpi_battery *battery = seq->private;
537
538         if (result)
539                 goto end;
540
541         seq_printf(seq, "present:                 %s\n",
542                    acpi_battery_present(battery)?"yes":"no");
543         if (!acpi_battery_present(battery))
544                 goto end;
545         if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
546                 seq_printf(seq, "design capacity:         unknown\n");
547         else
548                 seq_printf(seq, "design capacity:         %d %sh\n",
549                            battery->design_capacity,
550                            acpi_battery_units(battery));
551
552         if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
553                 seq_printf(seq, "last full capacity:      unknown\n");
554         else
555                 seq_printf(seq, "last full capacity:      %d %sh\n",
556                            battery->full_charge_capacity,
557                            acpi_battery_units(battery));
558
559         seq_printf(seq, "battery technology:      %srechargeable\n",
560                    (!battery->technology)?"non-":"");
561
562         if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
563                 seq_printf(seq, "design voltage:          unknown\n");
564         else
565                 seq_printf(seq, "design voltage:          %d mV\n",
566                            battery->design_voltage);
567         seq_printf(seq, "design capacity warning: %d %sh\n",
568                    battery->design_capacity_warning,
569                    acpi_battery_units(battery));
570         seq_printf(seq, "design capacity low:     %d %sh\n",
571                    battery->design_capacity_low,
572                    acpi_battery_units(battery));
573         seq_printf(seq, "capacity granularity 1:  %d %sh\n",
574                    battery->capacity_granularity_1,
575                    acpi_battery_units(battery));
576         seq_printf(seq, "capacity granularity 2:  %d %sh\n",
577                    battery->capacity_granularity_2,
578                    acpi_battery_units(battery));
579         seq_printf(seq, "model number:            %s\n", battery->model_number);
580         seq_printf(seq, "serial number:           %s\n", battery->serial_number);
581         seq_printf(seq, "battery type:            %s\n", battery->type);
582         seq_printf(seq, "OEM info:                %s\n", battery->oem_info);
583       end:
584         if (result)
585                 seq_printf(seq, "ERROR: Unable to read battery info\n");
586         return result;
587 }
588
589 static int acpi_battery_print_state(struct seq_file *seq, int result)
590 {
591         struct acpi_battery *battery = seq->private;
592
593         if (result)
594                 goto end;
595
596         seq_printf(seq, "present:                 %s\n",
597                    acpi_battery_present(battery)?"yes":"no");
598         if (!acpi_battery_present(battery))
599                 goto end;
600
601         seq_printf(seq, "capacity state:          %s\n",
602                         (battery->state & 0x04)?"critical":"ok");
603         if ((battery->state & 0x01) && (battery->state & 0x02))
604                 seq_printf(seq,
605                            "charging state:          charging/discharging\n");
606         else if (battery->state & 0x01)
607                 seq_printf(seq, "charging state:          discharging\n");
608         else if (battery->state & 0x02)
609                 seq_printf(seq, "charging state:          charging\n");
610         else
611                 seq_printf(seq, "charging state:          charged\n");
612
613         if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
614                 seq_printf(seq, "present rate:            unknown\n");
615         else
616                 seq_printf(seq, "present rate:            %d %s\n",
617                            battery->rate_now, acpi_battery_units(battery));
618
619         if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
620                 seq_printf(seq, "remaining capacity:      unknown\n");
621         else
622                 seq_printf(seq, "remaining capacity:      %d %sh\n",
623                            battery->capacity_now, acpi_battery_units(battery));
624         if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
625                 seq_printf(seq, "present voltage:         unknown\n");
626         else
627                 seq_printf(seq, "present voltage:         %d mV\n",
628                            battery->voltage_now);
629       end:
630         if (result)
631                 seq_printf(seq, "ERROR: Unable to read battery state\n");
632
633         return result;
634 }
635
636 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
637 {
638         struct acpi_battery *battery = seq->private;
639
640         if (result)
641                 goto end;
642
643         if (!acpi_battery_present(battery)) {
644                 seq_printf(seq, "present:                 no\n");
645                 goto end;
646         }
647         seq_printf(seq, "alarm:                   ");
648         if (!battery->alarm)
649                 seq_printf(seq, "unsupported\n");
650         else
651                 seq_printf(seq, "%u %sh\n", battery->alarm,
652                                 acpi_battery_units(battery));
653       end:
654         if (result)
655                 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
656         return result;
657 }
658
659 static ssize_t acpi_battery_write_alarm(struct file *file,
660                                         const char __user * buffer,
661                                         size_t count, loff_t * ppos)
662 {
663         int result = 0;
664         char alarm_string[12] = { '\0' };
665         struct seq_file *m = file->private_data;
666         struct acpi_battery *battery = m->private;
667
668         if (!battery || (count > sizeof(alarm_string) - 1))
669                 return -EINVAL;
670         if (!acpi_battery_present(battery)) {
671                 result = -ENODEV;
672                 goto end;
673         }
674         if (copy_from_user(alarm_string, buffer, count)) {
675                 result = -EFAULT;
676                 goto end;
677         }
678         alarm_string[count] = '\0';
679         battery->alarm = simple_strtol(alarm_string, NULL, 0);
680         result = acpi_battery_set_alarm(battery);
681       end:
682         if (!result)
683                 return count;
684         return result;
685 }
686
687 typedef int(*print_func)(struct seq_file *seq, int result);
688
689 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
690         acpi_battery_print_info,
691         acpi_battery_print_state,
692         acpi_battery_print_alarm,
693 };
694
695 static int acpi_battery_read(int fid, struct seq_file *seq)
696 {
697         struct acpi_battery *battery = seq->private;
698         int result = acpi_battery_update(battery);
699         return acpi_print_funcs[fid](seq, result);
700 }
701
702 #define DECLARE_FILE_FUNCTIONS(_name) \
703 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
704 { \
705         return acpi_battery_read(_name##_tag, seq); \
706 } \
707 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
708 { \
709         return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
710 }
711
712 DECLARE_FILE_FUNCTIONS(info);
713 DECLARE_FILE_FUNCTIONS(state);
714 DECLARE_FILE_FUNCTIONS(alarm);
715
716 #undef DECLARE_FILE_FUNCTIONS
717
718 #define FILE_DESCRIPTION_RO(_name) \
719         { \
720         .name = __stringify(_name), \
721         .mode = S_IRUGO, \
722         .ops = { \
723                 .open = acpi_battery_##_name##_open_fs, \
724                 .read = seq_read, \
725                 .llseek = seq_lseek, \
726                 .release = single_release, \
727                 .owner = THIS_MODULE, \
728                 }, \
729         }
730
731 #define FILE_DESCRIPTION_RW(_name) \
732         { \
733         .name = __stringify(_name), \
734         .mode = S_IFREG | S_IRUGO | S_IWUSR, \
735         .ops = { \
736                 .open = acpi_battery_##_name##_open_fs, \
737                 .read = seq_read, \
738                 .llseek = seq_lseek, \
739                 .write = acpi_battery_write_##_name, \
740                 .release = single_release, \
741                 .owner = THIS_MODULE, \
742                 }, \
743         }
744
745 static struct battery_file {
746         struct file_operations ops;
747         mode_t mode;
748         const char *name;
749 } acpi_battery_file[] = {
750         FILE_DESCRIPTION_RO(info),
751         FILE_DESCRIPTION_RO(state),
752         FILE_DESCRIPTION_RW(alarm),
753 };
754
755 #undef FILE_DESCRIPTION_RO
756 #undef FILE_DESCRIPTION_RW
757
758 static int acpi_battery_add_fs(struct acpi_device *device)
759 {
760         struct proc_dir_entry *entry = NULL;
761         int i;
762
763         if (!acpi_device_dir(device)) {
764                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
765                                                      acpi_battery_dir);
766                 if (!acpi_device_dir(device))
767                         return -ENODEV;
768         }
769
770         for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
771                 entry = proc_create_data(acpi_battery_file[i].name,
772                                          acpi_battery_file[i].mode,
773                                          acpi_device_dir(device),
774                                          &acpi_battery_file[i].ops,
775                                          acpi_driver_data(device));
776                 if (!entry)
777                         return -ENODEV;
778         }
779         return 0;
780 }
781
782 static void acpi_battery_remove_fs(struct acpi_device *device)
783 {
784         int i;
785         if (!acpi_device_dir(device))
786                 return;
787         for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
788                 remove_proc_entry(acpi_battery_file[i].name,
789                                   acpi_device_dir(device));
790
791         remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
792         acpi_device_dir(device) = NULL;
793 }
794
795 #endif
796
797 /* --------------------------------------------------------------------------
798                                  Driver Interface
799    -------------------------------------------------------------------------- */
800
801 static void acpi_battery_notify(struct acpi_device *device, u32 event)
802 {
803         struct acpi_battery *battery = acpi_driver_data(device);
804
805         if (!battery)
806                 return;
807         acpi_battery_update(battery);
808         acpi_bus_generate_proc_event(device, event,
809                                      acpi_battery_present(battery));
810         acpi_bus_generate_netlink_event(device->pnp.device_class,
811                                         dev_name(&device->dev), event,
812                                         acpi_battery_present(battery));
813 #ifdef CONFIG_ACPI_SYSFS_POWER
814         /* acpi_batter_update could remove power_supply object */
815         if (battery->bat.dev)
816                 kobject_uevent(&battery->bat.dev->kobj, KOBJ_CHANGE);
817 #endif
818 }
819
820 static int acpi_battery_add(struct acpi_device *device)
821 {
822         int result = 0;
823         struct acpi_battery *battery = NULL;
824         if (!device)
825                 return -EINVAL;
826         battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
827         if (!battery)
828                 return -ENOMEM;
829         battery->device = device;
830         strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
831         strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
832         device->driver_data = battery;
833         mutex_init(&battery->lock);
834         acpi_battery_update(battery);
835 #ifdef CONFIG_ACPI_PROCFS_POWER
836         result = acpi_battery_add_fs(device);
837 #endif
838         if (!result) {
839                 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
840                         ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
841                         device->status.battery_present ? "present" : "absent");
842         } else {
843 #ifdef CONFIG_ACPI_PROCFS_POWER
844                 acpi_battery_remove_fs(device);
845 #endif
846                 kfree(battery);
847         }
848         return result;
849 }
850
851 static int acpi_battery_remove(struct acpi_device *device, int type)
852 {
853         struct acpi_battery *battery = NULL;
854
855         if (!device || !acpi_driver_data(device))
856                 return -EINVAL;
857         battery = acpi_driver_data(device);
858 #ifdef CONFIG_ACPI_PROCFS_POWER
859         acpi_battery_remove_fs(device);
860 #endif
861 #ifdef CONFIG_ACPI_SYSFS_POWER
862         sysfs_remove_battery(battery);
863 #endif
864         mutex_destroy(&battery->lock);
865         kfree(battery);
866         return 0;
867 }
868
869 /* this is needed to learn about changes made in suspended state */
870 static int acpi_battery_resume(struct acpi_device *device)
871 {
872         struct acpi_battery *battery;
873         if (!device)
874                 return -EINVAL;
875         battery = acpi_driver_data(device);
876         battery->update_time = 0;
877         acpi_battery_update(battery);
878         return 0;
879 }
880
881 static struct acpi_driver acpi_battery_driver = {
882         .name = "battery",
883         .class = ACPI_BATTERY_CLASS,
884         .ids = battery_device_ids,
885         .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
886         .ops = {
887                 .add = acpi_battery_add,
888                 .resume = acpi_battery_resume,
889                 .remove = acpi_battery_remove,
890                 .notify = acpi_battery_notify,
891                 },
892 };
893
894 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
895 {
896         if (acpi_disabled)
897                 return;
898 #ifdef CONFIG_ACPI_PROCFS_POWER
899         acpi_battery_dir = acpi_lock_battery_dir();
900         if (!acpi_battery_dir)
901                 return;
902 #endif
903         if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
904 #ifdef CONFIG_ACPI_PROCFS_POWER
905                 acpi_unlock_battery_dir(acpi_battery_dir);
906 #endif
907                 return;
908         }
909         return;
910 }
911
912 static int __init acpi_battery_init(void)
913 {
914         async_schedule(acpi_battery_init_async, NULL);
915         return 0;
916 }
917
918 static void __exit acpi_battery_exit(void)
919 {
920         acpi_bus_unregister_driver(&acpi_battery_driver);
921 #ifdef CONFIG_ACPI_PROCFS_POWER
922         acpi_unlock_battery_dir(acpi_battery_dir);
923 #endif
924 }
925
926 module_init(acpi_battery_init);
927 module_exit(acpi_battery_exit);