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