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