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