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