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