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