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