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