]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/acpi/battery.c
85a6c3c3631c091b2516dc685aae96a30d5e9683
[linux.git] / drivers / acpi / battery.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  battery.c - ACPI Battery Driver (Revision: 2.0)
4  *
5  *  Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
6  *  Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
7  *  Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
8  *  Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
9  */
10
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12
13 #include <linux/async.h>
14 #include <linux/delay.h>
15 #include <linux/dmi.h>
16 #include <linux/jiffies.h>
17 #include <linux/kernel.h>
18 #include <linux/list.h>
19 #include <linux/module.h>
20 #include <linux/mutex.h>
21 #include <linux/slab.h>
22 #include <linux/suspend.h>
23 #include <linux/types.h>
24
25 #include <asm/unaligned.h>
26
27 #ifdef CONFIG_ACPI_PROCFS_POWER
28 #include <linux/proc_fs.h>
29 #include <linux/seq_file.h>
30 #include <linux/uaccess.h>
31 #endif
32
33 #include <linux/acpi.h>
34 #include <linux/power_supply.h>
35
36 #include <acpi/battery.h>
37
38 #define PREFIX "ACPI: "
39
40 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
41 #define ACPI_BATTERY_CAPACITY_VALID(capacity) \
42         ((capacity) != 0 && (capacity) != ACPI_BATTERY_VALUE_UNKNOWN)
43
44 #define ACPI_BATTERY_DEVICE_NAME        "Battery"
45
46 /* Battery power unit: 0 means mW, 1 means mA */
47 #define ACPI_BATTERY_POWER_UNIT_MA      1
48
49 #define ACPI_BATTERY_STATE_DISCHARGING  0x1
50 #define ACPI_BATTERY_STATE_CHARGING     0x2
51 #define ACPI_BATTERY_STATE_CRITICAL     0x4
52
53 #define _COMPONENT              ACPI_BATTERY_COMPONENT
54
55 ACPI_MODULE_NAME("battery");
56
57 MODULE_AUTHOR("Paul Diefenbaugh");
58 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
59 MODULE_DESCRIPTION("ACPI Battery Driver");
60 MODULE_LICENSE("GPL");
61
62 static async_cookie_t async_cookie;
63 static bool battery_driver_registered;
64 static int battery_bix_broken_package;
65 static int battery_notification_delay_ms;
66 static int battery_ac_is_broken;
67 static int battery_check_pmic = 1;
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 #endif
76
77 static const struct acpi_device_id battery_device_ids[] = {
78         {"PNP0C0A", 0},
79         {"", 0},
80 };
81
82 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
83
84 /* Lists of PMIC ACPI HIDs with an (often better) native battery driver */
85 static const char * const acpi_battery_blacklist[] = {
86         "INT33F4", /* X-Powers AXP288 PMIC */
87 };
88
89 enum {
90         ACPI_BATTERY_ALARM_PRESENT,
91         ACPI_BATTERY_XINFO_PRESENT,
92         ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
93         /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
94            switches between mWh and mAh depending on whether the system
95            is running on battery or not.  When mAh is the unit, most
96            reported values are incorrect and need to be adjusted by
97            10000/design_voltage.  Verified on x201, t410, t410s, and x220.
98            Pre-2010 and 2012 models appear to always report in mWh and
99            are thus unaffected (tested with t42, t61, t500, x200, x300,
100            and x230).  Also, in mid-2012 Lenovo issued a BIOS update for
101            the 2011 models that fixes the issue (tested on x220 with a
102            post-1.29 BIOS), but as of Nov. 2012, no such update is
103            available for the 2010 models.  */
104         ACPI_BATTERY_QUIRK_THINKPAD_MAH,
105         /* for batteries reporting current capacity with design capacity
106          * on a full charge, but showing degradation in full charge cap.
107          */
108         ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE,
109 };
110
111 struct acpi_battery {
112         struct mutex lock;
113         struct mutex sysfs_lock;
114         struct power_supply *bat;
115         struct power_supply_desc bat_desc;
116         struct acpi_device *device;
117         struct notifier_block pm_nb;
118         struct list_head list;
119         unsigned long update_time;
120         int revision;
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) power_supply_get_drvdata(x)
149
150 static 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         /* charging, discharging or critical low */
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 bool acpi_battery_is_degraded(struct acpi_battery *battery)
196 {
197         return ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity) &&
198                 ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity) &&
199                 battery->full_charge_capacity < battery->design_capacity;
200 }
201
202 static int acpi_battery_handle_discharging(struct acpi_battery *battery)
203 {
204         /*
205          * Some devices wrongly report discharging if the battery's charge level
206          * was above the device's start charging threshold atm the AC adapter
207          * was plugged in and the device thus did not start a new charge cycle.
208          */
209         if ((battery_ac_is_broken || power_supply_is_system_supplied()) &&
210             battery->rate_now == 0)
211                 return POWER_SUPPLY_STATUS_NOT_CHARGING;
212
213         return POWER_SUPPLY_STATUS_DISCHARGING;
214 }
215
216 static int acpi_battery_get_property(struct power_supply *psy,
217                                      enum power_supply_property psp,
218                                      union power_supply_propval *val)
219 {
220         int full_capacity = ACPI_BATTERY_VALUE_UNKNOWN, ret = 0;
221         struct acpi_battery *battery = to_acpi_battery(psy);
222
223         if (acpi_battery_present(battery)) {
224                 /* run battery update only if it is present */
225                 acpi_battery_get_state(battery);
226         } else if (psp != POWER_SUPPLY_PROP_PRESENT)
227                 return -ENODEV;
228         switch (psp) {
229         case POWER_SUPPLY_PROP_STATUS:
230                 if (battery->state & ACPI_BATTERY_STATE_DISCHARGING)
231                         val->intval = acpi_battery_handle_discharging(battery);
232                 else if (battery->state & ACPI_BATTERY_STATE_CHARGING)
233                         val->intval = POWER_SUPPLY_STATUS_CHARGING;
234                 else if (acpi_battery_is_charged(battery))
235                         val->intval = POWER_SUPPLY_STATUS_FULL;
236                 else
237                         val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
238                 break;
239         case POWER_SUPPLY_PROP_PRESENT:
240                 val->intval = acpi_battery_present(battery);
241                 break;
242         case POWER_SUPPLY_PROP_TECHNOLOGY:
243                 val->intval = acpi_battery_technology(battery);
244                 break;
245         case POWER_SUPPLY_PROP_CYCLE_COUNT:
246                 val->intval = battery->cycle_count;
247                 break;
248         case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
249                 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
250                         ret = -ENODEV;
251                 else
252                         val->intval = battery->design_voltage * 1000;
253                 break;
254         case POWER_SUPPLY_PROP_VOLTAGE_NOW:
255                 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
256                         ret = -ENODEV;
257                 else
258                         val->intval = battery->voltage_now * 1000;
259                 break;
260         case POWER_SUPPLY_PROP_CURRENT_NOW:
261         case POWER_SUPPLY_PROP_POWER_NOW:
262                 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
263                         ret = -ENODEV;
264                 else
265                         val->intval = battery->rate_now * 1000;
266                 break;
267         case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
268         case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
269                 if (!ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
270                         ret = -ENODEV;
271                 else
272                         val->intval = battery->design_capacity * 1000;
273                 break;
274         case POWER_SUPPLY_PROP_CHARGE_FULL:
275         case POWER_SUPPLY_PROP_ENERGY_FULL:
276                 if (!ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
277                         ret = -ENODEV;
278                 else
279                         val->intval = battery->full_charge_capacity * 1000;
280                 break;
281         case POWER_SUPPLY_PROP_CHARGE_NOW:
282         case POWER_SUPPLY_PROP_ENERGY_NOW:
283                 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
284                         ret = -ENODEV;
285                 else
286                         val->intval = battery->capacity_now * 1000;
287                 break;
288         case POWER_SUPPLY_PROP_CAPACITY:
289                 if (ACPI_BATTERY_CAPACITY_VALID(battery->full_charge_capacity))
290                         full_capacity = battery->full_charge_capacity;
291                 else if (ACPI_BATTERY_CAPACITY_VALID(battery->design_capacity))
292                         full_capacity = battery->design_capacity;
293
294                 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
295                     full_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
296                         ret = -ENODEV;
297                 else
298                         val->intval = battery->capacity_now * 100/
299                                         full_capacity;
300                 break;
301         case POWER_SUPPLY_PROP_CAPACITY_LEVEL:
302                 if (battery->state & ACPI_BATTERY_STATE_CRITICAL)
303                         val->intval = POWER_SUPPLY_CAPACITY_LEVEL_CRITICAL;
304                 else if (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
305                         (battery->capacity_now <= battery->alarm))
306                         val->intval = POWER_SUPPLY_CAPACITY_LEVEL_LOW;
307                 else if (acpi_battery_is_charged(battery))
308                         val->intval = POWER_SUPPLY_CAPACITY_LEVEL_FULL;
309                 else
310                         val->intval = POWER_SUPPLY_CAPACITY_LEVEL_NORMAL;
311                 break;
312         case POWER_SUPPLY_PROP_MODEL_NAME:
313                 val->strval = battery->model_number;
314                 break;
315         case POWER_SUPPLY_PROP_MANUFACTURER:
316                 val->strval = battery->oem_info;
317                 break;
318         case POWER_SUPPLY_PROP_SERIAL_NUMBER:
319                 val->strval = battery->serial_number;
320                 break;
321         default:
322                 ret = -EINVAL;
323         }
324         return ret;
325 }
326
327 static enum power_supply_property charge_battery_props[] = {
328         POWER_SUPPLY_PROP_STATUS,
329         POWER_SUPPLY_PROP_PRESENT,
330         POWER_SUPPLY_PROP_TECHNOLOGY,
331         POWER_SUPPLY_PROP_CYCLE_COUNT,
332         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
333         POWER_SUPPLY_PROP_VOLTAGE_NOW,
334         POWER_SUPPLY_PROP_CURRENT_NOW,
335         POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
336         POWER_SUPPLY_PROP_CHARGE_FULL,
337         POWER_SUPPLY_PROP_CHARGE_NOW,
338         POWER_SUPPLY_PROP_CAPACITY,
339         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
340         POWER_SUPPLY_PROP_MODEL_NAME,
341         POWER_SUPPLY_PROP_MANUFACTURER,
342         POWER_SUPPLY_PROP_SERIAL_NUMBER,
343 };
344
345 static enum power_supply_property energy_battery_props[] = {
346         POWER_SUPPLY_PROP_STATUS,
347         POWER_SUPPLY_PROP_PRESENT,
348         POWER_SUPPLY_PROP_TECHNOLOGY,
349         POWER_SUPPLY_PROP_CYCLE_COUNT,
350         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
351         POWER_SUPPLY_PROP_VOLTAGE_NOW,
352         POWER_SUPPLY_PROP_POWER_NOW,
353         POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
354         POWER_SUPPLY_PROP_ENERGY_FULL,
355         POWER_SUPPLY_PROP_ENERGY_NOW,
356         POWER_SUPPLY_PROP_CAPACITY,
357         POWER_SUPPLY_PROP_CAPACITY_LEVEL,
358         POWER_SUPPLY_PROP_MODEL_NAME,
359         POWER_SUPPLY_PROP_MANUFACTURER,
360         POWER_SUPPLY_PROP_SERIAL_NUMBER,
361 };
362
363 static enum power_supply_property energy_battery_full_cap_broken_props[] = {
364         POWER_SUPPLY_PROP_STATUS,
365         POWER_SUPPLY_PROP_PRESENT,
366         POWER_SUPPLY_PROP_TECHNOLOGY,
367         POWER_SUPPLY_PROP_CYCLE_COUNT,
368         POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
369         POWER_SUPPLY_PROP_VOLTAGE_NOW,
370         POWER_SUPPLY_PROP_POWER_NOW,
371         POWER_SUPPLY_PROP_ENERGY_NOW,
372         POWER_SUPPLY_PROP_MODEL_NAME,
373         POWER_SUPPLY_PROP_MANUFACTURER,
374         POWER_SUPPLY_PROP_SERIAL_NUMBER,
375 };
376
377 /* --------------------------------------------------------------------------
378                                Battery Management
379    -------------------------------------------------------------------------- */
380 struct acpi_offsets {
381         size_t offset;          /* offset inside struct acpi_sbs_battery */
382         u8 mode;                /* int or string? */
383 };
384
385 static const struct acpi_offsets state_offsets[] = {
386         {offsetof(struct acpi_battery, state), 0},
387         {offsetof(struct acpi_battery, rate_now), 0},
388         {offsetof(struct acpi_battery, capacity_now), 0},
389         {offsetof(struct acpi_battery, voltage_now), 0},
390 };
391
392 static const struct acpi_offsets info_offsets[] = {
393         {offsetof(struct acpi_battery, power_unit), 0},
394         {offsetof(struct acpi_battery, design_capacity), 0},
395         {offsetof(struct acpi_battery, full_charge_capacity), 0},
396         {offsetof(struct acpi_battery, technology), 0},
397         {offsetof(struct acpi_battery, design_voltage), 0},
398         {offsetof(struct acpi_battery, design_capacity_warning), 0},
399         {offsetof(struct acpi_battery, design_capacity_low), 0},
400         {offsetof(struct acpi_battery, capacity_granularity_1), 0},
401         {offsetof(struct acpi_battery, capacity_granularity_2), 0},
402         {offsetof(struct acpi_battery, model_number), 1},
403         {offsetof(struct acpi_battery, serial_number), 1},
404         {offsetof(struct acpi_battery, type), 1},
405         {offsetof(struct acpi_battery, oem_info), 1},
406 };
407
408 static const struct acpi_offsets extended_info_offsets[] = {
409         {offsetof(struct acpi_battery, revision), 0},
410         {offsetof(struct acpi_battery, power_unit), 0},
411         {offsetof(struct acpi_battery, design_capacity), 0},
412         {offsetof(struct acpi_battery, full_charge_capacity), 0},
413         {offsetof(struct acpi_battery, technology), 0},
414         {offsetof(struct acpi_battery, design_voltage), 0},
415         {offsetof(struct acpi_battery, design_capacity_warning), 0},
416         {offsetof(struct acpi_battery, design_capacity_low), 0},
417         {offsetof(struct acpi_battery, cycle_count), 0},
418         {offsetof(struct acpi_battery, measurement_accuracy), 0},
419         {offsetof(struct acpi_battery, max_sampling_time), 0},
420         {offsetof(struct acpi_battery, min_sampling_time), 0},
421         {offsetof(struct acpi_battery, max_averaging_interval), 0},
422         {offsetof(struct acpi_battery, min_averaging_interval), 0},
423         {offsetof(struct acpi_battery, capacity_granularity_1), 0},
424         {offsetof(struct acpi_battery, capacity_granularity_2), 0},
425         {offsetof(struct acpi_battery, model_number), 1},
426         {offsetof(struct acpi_battery, serial_number), 1},
427         {offsetof(struct acpi_battery, type), 1},
428         {offsetof(struct acpi_battery, oem_info), 1},
429 };
430
431 static int extract_package(struct acpi_battery *battery,
432                            union acpi_object *package,
433                            const struct acpi_offsets *offsets, int num)
434 {
435         int i;
436         union acpi_object *element;
437         if (package->type != ACPI_TYPE_PACKAGE)
438                 return -EFAULT;
439         for (i = 0; i < num; ++i) {
440                 if (package->package.count <= i)
441                         return -EFAULT;
442                 element = &package->package.elements[i];
443                 if (offsets[i].mode) {
444                         u8 *ptr = (u8 *)battery + offsets[i].offset;
445                         if (element->type == ACPI_TYPE_STRING ||
446                             element->type == ACPI_TYPE_BUFFER)
447                                 strncpy(ptr, element->string.pointer, 32);
448                         else if (element->type == ACPI_TYPE_INTEGER) {
449                                 strncpy(ptr, (u8 *)&element->integer.value,
450                                         sizeof(u64));
451                                 ptr[sizeof(u64)] = 0;
452                         } else
453                                 *ptr = 0; /* don't have value */
454                 } else {
455                         int *x = (int *)((u8 *)battery + offsets[i].offset);
456                         *x = (element->type == ACPI_TYPE_INTEGER) ?
457                                 element->integer.value : -1;
458                 }
459         }
460         return 0;
461 }
462
463 static int acpi_battery_get_status(struct acpi_battery *battery)
464 {
465         if (acpi_bus_get_status(battery->device)) {
466                 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
467                 return -ENODEV;
468         }
469         return 0;
470 }
471
472
473 static int extract_battery_info(const int use_bix,
474                          struct acpi_battery *battery,
475                          const struct acpi_buffer *buffer)
476 {
477         int result = -EFAULT;
478
479         if (use_bix && battery_bix_broken_package)
480                 result = extract_package(battery, buffer->pointer,
481                                 extended_info_offsets + 1,
482                                 ARRAY_SIZE(extended_info_offsets) - 1);
483         else if (use_bix)
484                 result = extract_package(battery, buffer->pointer,
485                                 extended_info_offsets,
486                                 ARRAY_SIZE(extended_info_offsets));
487         else
488                 result = extract_package(battery, buffer->pointer,
489                                 info_offsets, ARRAY_SIZE(info_offsets));
490         if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
491                 battery->full_charge_capacity = battery->design_capacity;
492         if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
493             battery->power_unit && battery->design_voltage) {
494                 battery->design_capacity = battery->design_capacity *
495                     10000 / battery->design_voltage;
496                 battery->full_charge_capacity = battery->full_charge_capacity *
497                     10000 / battery->design_voltage;
498                 battery->design_capacity_warning =
499                     battery->design_capacity_warning *
500                     10000 / battery->design_voltage;
501                 /* Curiously, design_capacity_low, unlike the rest of them,
502                    is correct.  */
503                 /* capacity_granularity_* equal 1 on the systems tested, so
504                    it's impossible to tell if they would need an adjustment
505                    or not if their values were higher.  */
506         }
507         if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
508             battery->capacity_now > battery->full_charge_capacity)
509                 battery->capacity_now = battery->full_charge_capacity;
510
511         return result;
512 }
513
514 static int acpi_battery_get_info(struct acpi_battery *battery)
515 {
516         const int xinfo = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
517         int use_bix;
518         int result = -ENODEV;
519
520         if (!acpi_battery_present(battery))
521                 return 0;
522
523
524         for (use_bix = xinfo ? 1 : 0; use_bix >= 0; use_bix--) {
525                 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
526                 acpi_status status = AE_ERROR;
527
528                 mutex_lock(&battery->lock);
529                 status = acpi_evaluate_object(battery->device->handle,
530                                               use_bix ? "_BIX":"_BIF",
531                                               NULL, &buffer);
532                 mutex_unlock(&battery->lock);
533
534                 if (ACPI_FAILURE(status)) {
535                         ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s",
536                                         use_bix ? "_BIX":"_BIF"));
537                 } else {
538                         result = extract_battery_info(use_bix,
539                                                       battery,
540                                                       &buffer);
541
542                         kfree(buffer.pointer);
543                         break;
544                 }
545         }
546
547         if (!result && !use_bix && xinfo)
548                 pr_warn(FW_BUG "The _BIX method is broken, using _BIF.\n");
549
550         return result;
551 }
552
553 static int acpi_battery_get_state(struct acpi_battery *battery)
554 {
555         int result = 0;
556         acpi_status status = 0;
557         struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
558
559         if (!acpi_battery_present(battery))
560                 return 0;
561
562         if (battery->update_time &&
563             time_before(jiffies, battery->update_time +
564                         msecs_to_jiffies(cache_time)))
565                 return 0;
566
567         mutex_lock(&battery->lock);
568         status = acpi_evaluate_object(battery->device->handle, "_BST",
569                                       NULL, &buffer);
570         mutex_unlock(&battery->lock);
571
572         if (ACPI_FAILURE(status)) {
573                 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
574                 return -ENODEV;
575         }
576
577         result = extract_package(battery, buffer.pointer,
578                                  state_offsets, ARRAY_SIZE(state_offsets));
579         battery->update_time = jiffies;
580         kfree(buffer.pointer);
581
582         /* For buggy DSDTs that report negative 16-bit values for either
583          * charging or discharging current and/or report 0 as 65536
584          * due to bad math.
585          */
586         if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
587                 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
588                 (s16)(battery->rate_now) < 0) {
589                 battery->rate_now = abs((s16)battery->rate_now);
590                 pr_warn_once(FW_BUG "battery: (dis)charge rate invalid.\n");
591         }
592
593         if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
594             && battery->capacity_now >= 0 && battery->capacity_now <= 100)
595                 battery->capacity_now = (battery->capacity_now *
596                                 battery->full_charge_capacity) / 100;
597         if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
598             battery->power_unit && battery->design_voltage) {
599                 battery->capacity_now = battery->capacity_now *
600                     10000 / battery->design_voltage;
601         }
602         if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags) &&
603             battery->capacity_now > battery->full_charge_capacity)
604                 battery->capacity_now = battery->full_charge_capacity;
605
606         return result;
607 }
608
609 static int acpi_battery_set_alarm(struct acpi_battery *battery)
610 {
611         acpi_status status = 0;
612
613         if (!acpi_battery_present(battery) ||
614             !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
615                 return -ENODEV;
616
617         mutex_lock(&battery->lock);
618         status = acpi_execute_simple_method(battery->device->handle, "_BTP",
619                                             battery->alarm);
620         mutex_unlock(&battery->lock);
621
622         if (ACPI_FAILURE(status))
623                 return -ENODEV;
624
625         ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
626         return 0;
627 }
628
629 static int acpi_battery_init_alarm(struct acpi_battery *battery)
630 {
631         /* See if alarms are supported, and if so, set default */
632         if (!acpi_has_method(battery->device->handle, "_BTP")) {
633                 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
634                 return 0;
635         }
636         set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
637         if (!battery->alarm)
638                 battery->alarm = battery->design_capacity_warning;
639         return acpi_battery_set_alarm(battery);
640 }
641
642 static ssize_t acpi_battery_alarm_show(struct device *dev,
643                                         struct device_attribute *attr,
644                                         char *buf)
645 {
646         struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
647         return sprintf(buf, "%d\n", battery->alarm * 1000);
648 }
649
650 static ssize_t acpi_battery_alarm_store(struct device *dev,
651                                         struct device_attribute *attr,
652                                         const char *buf, size_t count)
653 {
654         unsigned long x;
655         struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
656         if (sscanf(buf, "%lu\n", &x) == 1)
657                 battery->alarm = x/1000;
658         if (acpi_battery_present(battery))
659                 acpi_battery_set_alarm(battery);
660         return count;
661 }
662
663 static const struct device_attribute alarm_attr = {
664         .attr = {.name = "alarm", .mode = 0644},
665         .show = acpi_battery_alarm_show,
666         .store = acpi_battery_alarm_store,
667 };
668
669 /*
670  * The Battery Hooking API
671  *
672  * This API is used inside other drivers that need to expose
673  * platform-specific behaviour within the generic driver in a
674  * generic way.
675  *
676  */
677
678 static LIST_HEAD(acpi_battery_list);
679 static LIST_HEAD(battery_hook_list);
680 static DEFINE_MUTEX(hook_mutex);
681
682 static void __battery_hook_unregister(struct acpi_battery_hook *hook, int lock)
683 {
684         struct acpi_battery *battery;
685         /*
686          * In order to remove a hook, we first need to
687          * de-register all the batteries that are registered.
688          */
689         if (lock)
690                 mutex_lock(&hook_mutex);
691         list_for_each_entry(battery, &acpi_battery_list, list) {
692                 hook->remove_battery(battery->bat);
693         }
694         list_del(&hook->list);
695         if (lock)
696                 mutex_unlock(&hook_mutex);
697         pr_info("extension unregistered: %s\n", hook->name);
698 }
699
700 void battery_hook_unregister(struct acpi_battery_hook *hook)
701 {
702         __battery_hook_unregister(hook, 1);
703 }
704 EXPORT_SYMBOL_GPL(battery_hook_unregister);
705
706 void battery_hook_register(struct acpi_battery_hook *hook)
707 {
708         struct acpi_battery *battery;
709
710         mutex_lock(&hook_mutex);
711         INIT_LIST_HEAD(&hook->list);
712         list_add(&hook->list, &battery_hook_list);
713         /*
714          * Now that the driver is registered, we need
715          * to notify the hook that a battery is available
716          * for each battery, so that the driver may add
717          * its attributes.
718          */
719         list_for_each_entry(battery, &acpi_battery_list, list) {
720                 if (hook->add_battery(battery->bat)) {
721                         /*
722                          * If a add-battery returns non-zero,
723                          * the registration of the extension has failed,
724                          * and we will not add it to the list of loaded
725                          * hooks.
726                          */
727                         pr_err("extension failed to load: %s", hook->name);
728                         __battery_hook_unregister(hook, 0);
729                         goto end;
730                 }
731         }
732         pr_info("new extension: %s\n", hook->name);
733 end:
734         mutex_unlock(&hook_mutex);
735 }
736 EXPORT_SYMBOL_GPL(battery_hook_register);
737
738 /*
739  * This function gets called right after the battery sysfs
740  * attributes have been added, so that the drivers that
741  * define custom sysfs attributes can add their own.
742 */
743 static void battery_hook_add_battery(struct acpi_battery *battery)
744 {
745         struct acpi_battery_hook *hook_node, *tmp;
746
747         mutex_lock(&hook_mutex);
748         INIT_LIST_HEAD(&battery->list);
749         list_add(&battery->list, &acpi_battery_list);
750         /*
751          * Since we added a new battery to the list, we need to
752          * iterate over the hooks and call add_battery for each
753          * hook that was registered. This usually happens
754          * when a battery gets hotplugged or initialized
755          * during the battery module initialization.
756          */
757         list_for_each_entry_safe(hook_node, tmp, &battery_hook_list, list) {
758                 if (hook_node->add_battery(battery->bat)) {
759                         /*
760                          * The notification of the extensions has failed, to
761                          * prevent further errors we will unload the extension.
762                          */
763                         pr_err("error in extension, unloading: %s",
764                                         hook_node->name);
765                         __battery_hook_unregister(hook_node, 0);
766                 }
767         }
768         mutex_unlock(&hook_mutex);
769 }
770
771 static void battery_hook_remove_battery(struct acpi_battery *battery)
772 {
773         struct acpi_battery_hook *hook;
774
775         mutex_lock(&hook_mutex);
776         /*
777          * Before removing the hook, we need to remove all
778          * custom attributes from the battery.
779          */
780         list_for_each_entry(hook, &battery_hook_list, list) {
781                 hook->remove_battery(battery->bat);
782         }
783         /* Then, just remove the battery from the list */
784         list_del(&battery->list);
785         mutex_unlock(&hook_mutex);
786 }
787
788 static void __exit battery_hook_exit(void)
789 {
790         struct acpi_battery_hook *hook;
791         struct acpi_battery_hook *ptr;
792         /*
793          * At this point, the acpi_bus_unregister_driver()
794          * has called remove for all batteries. We just
795          * need to remove the hooks.
796          */
797         list_for_each_entry_safe(hook, ptr, &battery_hook_list, list) {
798                 __battery_hook_unregister(hook, 1);
799         }
800         mutex_destroy(&hook_mutex);
801 }
802
803 static int sysfs_add_battery(struct acpi_battery *battery)
804 {
805         struct power_supply_config psy_cfg = { .drv_data = battery, };
806
807         if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
808                 battery->bat_desc.properties = charge_battery_props;
809                 battery->bat_desc.num_properties =
810                         ARRAY_SIZE(charge_battery_props);
811         } else if (!ACPI_BATTERY_CAPACITY_VALID(
812                                         battery->full_charge_capacity)) {
813                 battery->bat_desc.properties =
814                         energy_battery_full_cap_broken_props;
815                 battery->bat_desc.num_properties =
816                         ARRAY_SIZE(energy_battery_full_cap_broken_props);
817         } else {
818                 battery->bat_desc.properties = energy_battery_props;
819                 battery->bat_desc.num_properties =
820                         ARRAY_SIZE(energy_battery_props);
821         }
822
823         battery->bat_desc.name = acpi_device_bid(battery->device);
824         battery->bat_desc.type = POWER_SUPPLY_TYPE_BATTERY;
825         battery->bat_desc.get_property = acpi_battery_get_property;
826
827         battery->bat = power_supply_register_no_ws(&battery->device->dev,
828                                 &battery->bat_desc, &psy_cfg);
829
830         if (IS_ERR(battery->bat)) {
831                 int result = PTR_ERR(battery->bat);
832
833                 battery->bat = NULL;
834                 return result;
835         }
836         battery_hook_add_battery(battery);
837         return device_create_file(&battery->bat->dev, &alarm_attr);
838 }
839
840 static void sysfs_remove_battery(struct acpi_battery *battery)
841 {
842         mutex_lock(&battery->sysfs_lock);
843         if (!battery->bat) {
844                 mutex_unlock(&battery->sysfs_lock);
845                 return;
846         }
847         battery_hook_remove_battery(battery);
848         device_remove_file(&battery->bat->dev, &alarm_attr);
849         power_supply_unregister(battery->bat);
850         battery->bat = NULL;
851         mutex_unlock(&battery->sysfs_lock);
852 }
853
854 static void find_battery(const struct dmi_header *dm, void *private)
855 {
856         struct acpi_battery *battery = (struct acpi_battery *)private;
857         /* Note: the hardcoded offsets below have been extracted from
858            the source code of dmidecode.  */
859         if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
860                 const u8 *dmi_data = (const u8 *)(dm + 1);
861                 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
862                 if (dm->length >= 18)
863                         dmi_capacity *= dmi_data[17];
864                 if (battery->design_capacity * battery->design_voltage / 1000
865                     != dmi_capacity &&
866                     battery->design_capacity * 10 == dmi_capacity)
867                         set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
868                                 &battery->flags);
869         }
870 }
871
872 /*
873  * According to the ACPI spec, some kinds of primary batteries can
874  * report percentage battery remaining capacity directly to OS.
875  * In this case, it reports the Last Full Charged Capacity == 100
876  * and BatteryPresentRate == 0xFFFFFFFF.
877  *
878  * Now we found some battery reports percentage remaining capacity
879  * even if it's rechargeable.
880  * https://bugzilla.kernel.org/show_bug.cgi?id=15979
881  *
882  * Handle this correctly so that they won't break userspace.
883  */
884 static void acpi_battery_quirks(struct acpi_battery *battery)
885 {
886         if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
887                 return;
888
889         if (battery->full_charge_capacity == 100 &&
890                 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
891                 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
892                 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
893                 battery->full_charge_capacity = battery->design_capacity;
894                 battery->capacity_now = (battery->capacity_now *
895                                 battery->full_charge_capacity) / 100;
896         }
897
898         if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
899                 return;
900
901         if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
902                 const char *s;
903                 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
904                 if (s && !strncasecmp(s, "ThinkPad", 8)) {
905                         dmi_walk(find_battery, battery);
906                         if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
907                                      &battery->flags) &&
908                             battery->design_voltage) {
909                                 battery->design_capacity =
910                                     battery->design_capacity *
911                                     10000 / battery->design_voltage;
912                                 battery->full_charge_capacity =
913                                     battery->full_charge_capacity *
914                                     10000 / battery->design_voltage;
915                                 battery->design_capacity_warning =
916                                     battery->design_capacity_warning *
917                                     10000 / battery->design_voltage;
918                                 battery->capacity_now = battery->capacity_now *
919                                     10000 / battery->design_voltage;
920                         }
921                 }
922         }
923
924         if (test_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags))
925                 return;
926
927         if (acpi_battery_is_degraded(battery) &&
928             battery->capacity_now > battery->full_charge_capacity) {
929                 set_bit(ACPI_BATTERY_QUIRK_DEGRADED_FULL_CHARGE, &battery->flags);
930                 battery->capacity_now = battery->full_charge_capacity;
931         }
932 }
933
934 static int acpi_battery_update(struct acpi_battery *battery, bool resume)
935 {
936         int result = acpi_battery_get_status(battery);
937
938         if (result)
939                 return result;
940
941         if (!acpi_battery_present(battery)) {
942                 sysfs_remove_battery(battery);
943                 battery->update_time = 0;
944                 return 0;
945         }
946
947         if (resume)
948                 return 0;
949
950         if (!battery->update_time) {
951                 result = acpi_battery_get_info(battery);
952                 if (result)
953                         return result;
954                 acpi_battery_init_alarm(battery);
955         }
956
957         result = acpi_battery_get_state(battery);
958         if (result)
959                 return result;
960         acpi_battery_quirks(battery);
961
962         if (!battery->bat) {
963                 result = sysfs_add_battery(battery);
964                 if (result)
965                         return result;
966         }
967
968         /*
969          * Wakeup the system if battery is critical low
970          * or lower than the alarm level
971          */
972         if ((battery->state & ACPI_BATTERY_STATE_CRITICAL) ||
973             (test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags) &&
974             (battery->capacity_now <= battery->alarm)))
975                 acpi_pm_wakeup_event(&battery->device->dev);
976
977         return result;
978 }
979
980 static void acpi_battery_refresh(struct acpi_battery *battery)
981 {
982         int power_unit;
983
984         if (!battery->bat)
985                 return;
986
987         power_unit = battery->power_unit;
988
989         acpi_battery_get_info(battery);
990
991         if (power_unit == battery->power_unit)
992                 return;
993
994         /* The battery has changed its reporting units. */
995         sysfs_remove_battery(battery);
996         sysfs_add_battery(battery);
997 }
998
999 /* --------------------------------------------------------------------------
1000                               FS Interface (/proc)
1001    -------------------------------------------------------------------------- */
1002
1003 #ifdef CONFIG_ACPI_PROCFS_POWER
1004 static struct proc_dir_entry *acpi_battery_dir;
1005
1006 static const char *acpi_battery_units(const struct acpi_battery *battery)
1007 {
1008         return (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) ?
1009                 "mA" : "mW";
1010 }
1011
1012 static int acpi_battery_info_proc_show(struct seq_file *seq, void *offset)
1013 {
1014         struct acpi_battery *battery = seq->private;
1015         int result = acpi_battery_update(battery, false);
1016
1017         if (result)
1018                 goto end;
1019
1020         seq_printf(seq, "present:                 %s\n",
1021                    acpi_battery_present(battery) ? "yes" : "no");
1022         if (!acpi_battery_present(battery))
1023                 goto end;
1024         if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1025                 seq_printf(seq, "design capacity:         unknown\n");
1026         else
1027                 seq_printf(seq, "design capacity:         %d %sh\n",
1028                            battery->design_capacity,
1029                            acpi_battery_units(battery));
1030
1031         if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
1032                 seq_printf(seq, "last full capacity:      unknown\n");
1033         else
1034                 seq_printf(seq, "last full capacity:      %d %sh\n",
1035                            battery->full_charge_capacity,
1036                            acpi_battery_units(battery));
1037
1038         seq_printf(seq, "battery technology:      %srechargeable\n",
1039                    battery->technology ? "" : "non-");
1040
1041         if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
1042                 seq_printf(seq, "design voltage:          unknown\n");
1043         else
1044                 seq_printf(seq, "design voltage:          %d mV\n",
1045                            battery->design_voltage);
1046         seq_printf(seq, "design capacity warning: %d %sh\n",
1047                    battery->design_capacity_warning,
1048                    acpi_battery_units(battery));
1049         seq_printf(seq, "design capacity low:     %d %sh\n",
1050                    battery->design_capacity_low,
1051                    acpi_battery_units(battery));
1052         seq_printf(seq, "cycle count:             %i\n", battery->cycle_count);
1053         seq_printf(seq, "capacity granularity 1:  %d %sh\n",
1054                    battery->capacity_granularity_1,
1055                    acpi_battery_units(battery));
1056         seq_printf(seq, "capacity granularity 2:  %d %sh\n",
1057                    battery->capacity_granularity_2,
1058                    acpi_battery_units(battery));
1059         seq_printf(seq, "model number:            %s\n", battery->model_number);
1060         seq_printf(seq, "serial number:           %s\n", battery->serial_number);
1061         seq_printf(seq, "battery type:            %s\n", battery->type);
1062         seq_printf(seq, "OEM info:                %s\n", battery->oem_info);
1063       end:
1064         if (result)
1065                 seq_printf(seq, "ERROR: Unable to read battery info\n");
1066         return result;
1067 }
1068
1069 static int acpi_battery_state_proc_show(struct seq_file *seq, void *offset)
1070 {
1071         struct acpi_battery *battery = seq->private;
1072         int result = acpi_battery_update(battery, false);
1073
1074         if (result)
1075                 goto end;
1076
1077         seq_printf(seq, "present:                 %s\n",
1078                    acpi_battery_present(battery) ? "yes" : "no");
1079         if (!acpi_battery_present(battery))
1080                 goto end;
1081
1082         seq_printf(seq, "capacity state:          %s\n",
1083                         (battery->state & 0x04) ? "critical" : "ok");
1084         if ((battery->state & 0x01) && (battery->state & 0x02))
1085                 seq_printf(seq,
1086                            "charging state:          charging/discharging\n");
1087         else if (battery->state & 0x01)
1088                 seq_printf(seq, "charging state:          discharging\n");
1089         else if (battery->state & 0x02)
1090                 seq_printf(seq, "charging state:          charging\n");
1091         else
1092                 seq_printf(seq, "charging state:          charged\n");
1093
1094         if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
1095                 seq_printf(seq, "present rate:            unknown\n");
1096         else
1097                 seq_printf(seq, "present rate:            %d %s\n",
1098                            battery->rate_now, acpi_battery_units(battery));
1099
1100         if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
1101                 seq_printf(seq, "remaining capacity:      unknown\n");
1102         else
1103                 seq_printf(seq, "remaining capacity:      %d %sh\n",
1104                            battery->capacity_now, acpi_battery_units(battery));
1105         if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
1106                 seq_printf(seq, "present voltage:         unknown\n");
1107         else
1108                 seq_printf(seq, "present voltage:         %d mV\n",
1109                            battery->voltage_now);
1110       end:
1111         if (result)
1112                 seq_printf(seq, "ERROR: Unable to read battery state\n");
1113
1114         return result;
1115 }
1116
1117 static int acpi_battery_alarm_proc_show(struct seq_file *seq, void *offset)
1118 {
1119         struct acpi_battery *battery = seq->private;
1120         int result = acpi_battery_update(battery, false);
1121
1122         if (result)
1123                 goto end;
1124
1125         if (!acpi_battery_present(battery)) {
1126                 seq_printf(seq, "present:                 no\n");
1127                 goto end;
1128         }
1129         seq_printf(seq, "alarm:                   ");
1130         if (battery->alarm) {
1131                 seq_printf(seq, "%u %sh\n", battery->alarm,
1132                                 acpi_battery_units(battery));
1133         } else {
1134                 seq_printf(seq, "unsupported\n");
1135         }
1136       end:
1137         if (result)
1138                 seq_printf(seq, "ERROR: Unable to read battery alarm\n");
1139         return result;
1140 }
1141
1142 static ssize_t acpi_battery_write_alarm(struct file *file,
1143                                         const char __user * buffer,
1144                                         size_t count, loff_t * ppos)
1145 {
1146         int result = 0;
1147         char alarm_string[12] = { '\0' };
1148         struct seq_file *m = file->private_data;
1149         struct acpi_battery *battery = m->private;
1150
1151         if (!battery || (count > sizeof(alarm_string) - 1))
1152                 return -EINVAL;
1153         if (!acpi_battery_present(battery)) {
1154                 result = -ENODEV;
1155                 goto end;
1156         }
1157         if (copy_from_user(alarm_string, buffer, count)) {
1158                 result = -EFAULT;
1159                 goto end;
1160         }
1161         alarm_string[count] = '\0';
1162         if (kstrtoint(alarm_string, 0, &battery->alarm)) {
1163                 result = -EINVAL;
1164                 goto end;
1165         }
1166         result = acpi_battery_set_alarm(battery);
1167       end:
1168         if (result)
1169                 return result;
1170         return count;
1171 }
1172
1173 static int acpi_battery_alarm_proc_open(struct inode *inode, struct file *file)
1174 {
1175         return single_open(file, acpi_battery_alarm_proc_show, PDE_DATA(inode));
1176 }
1177
1178 static const struct file_operations acpi_battery_alarm_fops = {
1179         .owner          = THIS_MODULE,
1180         .open           = acpi_battery_alarm_proc_open,
1181         .read           = seq_read,
1182         .write          = acpi_battery_write_alarm,
1183         .llseek         = seq_lseek,
1184         .release        = single_release,
1185 };
1186
1187 static int acpi_battery_add_fs(struct acpi_device *device)
1188 {
1189         pr_warn(PREFIX "Deprecated procfs I/F for battery is loaded, please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
1190         if (!acpi_device_dir(device)) {
1191                 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
1192                                                      acpi_battery_dir);
1193                 if (!acpi_device_dir(device))
1194                         return -ENODEV;
1195         }
1196
1197         if (!proc_create_single_data("info", S_IRUGO, acpi_device_dir(device),
1198                         acpi_battery_info_proc_show, acpi_driver_data(device)))
1199                 return -ENODEV;
1200         if (!proc_create_single_data("state", S_IRUGO, acpi_device_dir(device),
1201                         acpi_battery_state_proc_show, acpi_driver_data(device)))
1202                 return -ENODEV;
1203         if (!proc_create_data("alarm", S_IFREG | S_IRUGO | S_IWUSR,
1204                         acpi_device_dir(device), &acpi_battery_alarm_fops,
1205                         acpi_driver_data(device)))
1206                 return -ENODEV;
1207         return 0;
1208 }
1209
1210 static void acpi_battery_remove_fs(struct acpi_device *device)
1211 {
1212         if (!acpi_device_dir(device))
1213                 return;
1214         remove_proc_subtree(acpi_device_bid(device), acpi_battery_dir);
1215         acpi_device_dir(device) = NULL;
1216 }
1217
1218 #endif
1219
1220 /* --------------------------------------------------------------------------
1221                                  Driver Interface
1222    -------------------------------------------------------------------------- */
1223
1224 static void acpi_battery_notify(struct acpi_device *device, u32 event)
1225 {
1226         struct acpi_battery *battery = acpi_driver_data(device);
1227         struct power_supply *old;
1228
1229         if (!battery)
1230                 return;
1231         old = battery->bat;
1232         /*
1233         * On Acer Aspire V5-573G notifications are sometimes triggered too
1234         * early. For example, when AC is unplugged and notification is
1235         * triggered, battery state is still reported as "Full", and changes to
1236         * "Discharging" only after short delay, without any notification.
1237         */
1238         if (battery_notification_delay_ms > 0)
1239                 msleep(battery_notification_delay_ms);
1240         if (event == ACPI_BATTERY_NOTIFY_INFO)
1241                 acpi_battery_refresh(battery);
1242         acpi_battery_update(battery, false);
1243         acpi_bus_generate_netlink_event(device->pnp.device_class,
1244                                         dev_name(&device->dev), event,
1245                                         acpi_battery_present(battery));
1246         acpi_notifier_call_chain(device, event, acpi_battery_present(battery));
1247         /* acpi_battery_update could remove power_supply object */
1248         if (old && battery->bat)
1249                 power_supply_changed(battery->bat);
1250 }
1251
1252 static int battery_notify(struct notifier_block *nb,
1253                                unsigned long mode, void *_unused)
1254 {
1255         struct acpi_battery *battery = container_of(nb, struct acpi_battery,
1256                                                     pm_nb);
1257         int result;
1258
1259         switch (mode) {
1260         case PM_POST_HIBERNATION:
1261         case PM_POST_SUSPEND:
1262                 if (!acpi_battery_present(battery))
1263                         return 0;
1264
1265                 if (battery->bat) {
1266                         acpi_battery_refresh(battery);
1267                 } else {
1268                         result = acpi_battery_get_info(battery);
1269                         if (result)
1270                                 return result;
1271
1272                         result = sysfs_add_battery(battery);
1273                         if (result)
1274                                 return result;
1275                 }
1276
1277                 acpi_battery_init_alarm(battery);
1278                 acpi_battery_get_state(battery);
1279                 break;
1280         }
1281
1282         return 0;
1283 }
1284
1285 static int __init
1286 battery_bix_broken_package_quirk(const struct dmi_system_id *d)
1287 {
1288         battery_bix_broken_package = 1;
1289         return 0;
1290 }
1291
1292 static int __init
1293 battery_notification_delay_quirk(const struct dmi_system_id *d)
1294 {
1295         battery_notification_delay_ms = 1000;
1296         return 0;
1297 }
1298
1299 static int __init
1300 battery_ac_is_broken_quirk(const struct dmi_system_id *d)
1301 {
1302         battery_ac_is_broken = 1;
1303         return 0;
1304 }
1305
1306 static int __init
1307 battery_do_not_check_pmic_quirk(const struct dmi_system_id *d)
1308 {
1309         battery_check_pmic = 0;
1310         return 0;
1311 }
1312
1313 static const struct dmi_system_id bat_dmi_table[] __initconst = {
1314         {
1315                 /* NEC LZ750/LS */
1316                 .callback = battery_bix_broken_package_quirk,
1317                 .matches = {
1318                         DMI_MATCH(DMI_SYS_VENDOR, "NEC"),
1319                         DMI_MATCH(DMI_PRODUCT_NAME, "PC-LZ750LS"),
1320                 },
1321         },
1322         {
1323                 /* Acer Aspire V5-573G */
1324                 .callback = battery_notification_delay_quirk,
1325                 .matches = {
1326                         DMI_MATCH(DMI_SYS_VENDOR, "Acer"),
1327                         DMI_MATCH(DMI_PRODUCT_NAME, "Aspire V5-573G"),
1328                 },
1329         },
1330         {
1331                 /* Point of View mobii wintab p800w */
1332                 .callback = battery_ac_is_broken_quirk,
1333                 .matches = {
1334                         DMI_MATCH(DMI_BOARD_VENDOR, "AMI Corporation"),
1335                         DMI_MATCH(DMI_BOARD_NAME, "Aptio CRB"),
1336                         DMI_MATCH(DMI_BIOS_VERSION, "3BAIR1013"),
1337                         /* Above matches are too generic, add bios-date match */
1338                         DMI_MATCH(DMI_BIOS_DATE, "08/22/2014"),
1339                 },
1340         },
1341         {
1342                 /* ECS EF20EA */
1343                 .callback = battery_do_not_check_pmic_quirk,
1344                 .matches = {
1345                         DMI_MATCH(DMI_PRODUCT_NAME, "EF20EA"),
1346                 },
1347         },
1348         {
1349                 /* Lenovo Ideapad Miix 320 */
1350                 .callback = battery_do_not_check_pmic_quirk,
1351                 .matches = {
1352                   DMI_EXACT_MATCH(DMI_SYS_VENDOR, "LENOVO"),
1353                   DMI_EXACT_MATCH(DMI_PRODUCT_NAME, "80XF"),
1354                   DMI_EXACT_MATCH(DMI_PRODUCT_VERSION, "Lenovo MIIX 320-10ICR"),
1355                 },
1356         },
1357         {},
1358 };
1359
1360 /*
1361  * Some machines'(E,G Lenovo Z480) ECs are not stable
1362  * during boot up and this causes battery driver fails to be
1363  * probed due to failure of getting battery information
1364  * from EC sometimes. After several retries, the operation
1365  * may work. So add retry code here and 20ms sleep between
1366  * every retries.
1367  */
1368 static int acpi_battery_update_retry(struct acpi_battery *battery)
1369 {
1370         int retry, ret;
1371
1372         for (retry = 5; retry; retry--) {
1373                 ret = acpi_battery_update(battery, false);
1374                 if (!ret)
1375                         break;
1376
1377                 msleep(20);
1378         }
1379         return ret;
1380 }
1381
1382 static int acpi_battery_add(struct acpi_device *device)
1383 {
1384         int result = 0;
1385         struct acpi_battery *battery = NULL;
1386
1387         if (!device)
1388                 return -EINVAL;
1389
1390         if (device->dep_unmet)
1391                 return -EPROBE_DEFER;
1392
1393         battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
1394         if (!battery)
1395                 return -ENOMEM;
1396         battery->device = device;
1397         strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
1398         strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
1399         device->driver_data = battery;
1400         mutex_init(&battery->lock);
1401         mutex_init(&battery->sysfs_lock);
1402         if (acpi_has_method(battery->device->handle, "_BIX"))
1403                 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
1404
1405         result = acpi_battery_update_retry(battery);
1406         if (result)
1407                 goto fail;
1408
1409 #ifdef CONFIG_ACPI_PROCFS_POWER
1410         result = acpi_battery_add_fs(device);
1411         if (result) {
1412                 acpi_battery_remove_fs(device);
1413                 goto fail;
1414         }
1415 #endif
1416
1417         pr_info(PREFIX "%s Slot [%s] (battery %s)\n",
1418                 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
1419                 device->status.battery_present ? "present" : "absent");
1420
1421         battery->pm_nb.notifier_call = battery_notify;
1422         register_pm_notifier(&battery->pm_nb);
1423
1424         device_init_wakeup(&device->dev, 1);
1425
1426         return result;
1427
1428 fail:
1429         sysfs_remove_battery(battery);
1430         mutex_destroy(&battery->lock);
1431         mutex_destroy(&battery->sysfs_lock);
1432         kfree(battery);
1433         return result;
1434 }
1435
1436 static int acpi_battery_remove(struct acpi_device *device)
1437 {
1438         struct acpi_battery *battery = NULL;
1439
1440         if (!device || !acpi_driver_data(device))
1441                 return -EINVAL;
1442         device_init_wakeup(&device->dev, 0);
1443         battery = acpi_driver_data(device);
1444         unregister_pm_notifier(&battery->pm_nb);
1445 #ifdef CONFIG_ACPI_PROCFS_POWER
1446         acpi_battery_remove_fs(device);
1447 #endif
1448         sysfs_remove_battery(battery);
1449         mutex_destroy(&battery->lock);
1450         mutex_destroy(&battery->sysfs_lock);
1451         kfree(battery);
1452         return 0;
1453 }
1454
1455 #ifdef CONFIG_PM_SLEEP
1456 /* this is needed to learn about changes made in suspended state */
1457 static int acpi_battery_resume(struct device *dev)
1458 {
1459         struct acpi_battery *battery;
1460
1461         if (!dev)
1462                 return -EINVAL;
1463
1464         battery = acpi_driver_data(to_acpi_device(dev));
1465         if (!battery)
1466                 return -EINVAL;
1467
1468         battery->update_time = 0;
1469         acpi_battery_update(battery, true);
1470         return 0;
1471 }
1472 #else
1473 #define acpi_battery_resume NULL
1474 #endif
1475
1476 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
1477
1478 static struct acpi_driver acpi_battery_driver = {
1479         .name = "battery",
1480         .class = ACPI_BATTERY_CLASS,
1481         .ids = battery_device_ids,
1482         .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1483         .ops = {
1484                 .add = acpi_battery_add,
1485                 .remove = acpi_battery_remove,
1486                 .notify = acpi_battery_notify,
1487                 },
1488         .drv.pm = &acpi_battery_pm,
1489 };
1490
1491 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1492 {
1493         unsigned int i;
1494         int result;
1495
1496         dmi_check_system(bat_dmi_table);
1497
1498         if (battery_check_pmic) {
1499                 for (i = 0; i < ARRAY_SIZE(acpi_battery_blacklist); i++)
1500                         if (acpi_dev_present(acpi_battery_blacklist[i], "1", -1)) {
1501                                 pr_info(PREFIX ACPI_BATTERY_DEVICE_NAME
1502                                         ": found native %s PMIC, not loading\n",
1503                                         acpi_battery_blacklist[i]);
1504                                 return;
1505                         }
1506         }
1507
1508 #ifdef CONFIG_ACPI_PROCFS_POWER
1509         acpi_battery_dir = acpi_lock_battery_dir();
1510         if (!acpi_battery_dir)
1511                 return;
1512 #endif
1513         result = acpi_bus_register_driver(&acpi_battery_driver);
1514 #ifdef CONFIG_ACPI_PROCFS_POWER
1515         if (result < 0)
1516                 acpi_unlock_battery_dir(acpi_battery_dir);
1517 #endif
1518         battery_driver_registered = (result == 0);
1519 }
1520
1521 static int __init acpi_battery_init(void)
1522 {
1523         if (acpi_disabled)
1524                 return -ENODEV;
1525
1526         async_cookie = async_schedule(acpi_battery_init_async, NULL);
1527         return 0;
1528 }
1529
1530 static void __exit acpi_battery_exit(void)
1531 {
1532         async_synchronize_cookie(async_cookie + 1);
1533         if (battery_driver_registered) {
1534                 acpi_bus_unregister_driver(&acpi_battery_driver);
1535                 battery_hook_exit();
1536         }
1537 #ifdef CONFIG_ACPI_PROCFS_POWER
1538         if (acpi_battery_dir)
1539                 acpi_unlock_battery_dir(acpi_battery_dir);
1540 #endif
1541 }
1542
1543 module_init(acpi_battery_init);
1544 module_exit(acpi_battery_exit);