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