xref: /openbmc/linux/drivers/acpi/battery.c (revision 565d76cb)
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  *  You should have received a copy of the GNU General Public License along
22  *  with this program; if not, write to the Free Software Foundation, Inc.,
23  *  59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
24  *
25  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
26  */
27 
28 #include <linux/kernel.h>
29 #include <linux/module.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/slab.h>
36 
37 #ifdef CONFIG_ACPI_PROCFS_POWER
38 #include <linux/proc_fs.h>
39 #include <linux/seq_file.h>
40 #include <asm/uaccess.h>
41 #endif
42 
43 #include <acpi/acpi_bus.h>
44 #include <acpi/acpi_drivers.h>
45 #include <linux/power_supply.h>
46 
47 #define PREFIX "ACPI: "
48 
49 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
50 
51 #define ACPI_BATTERY_CLASS		"battery"
52 #define ACPI_BATTERY_DEVICE_NAME	"Battery"
53 #define ACPI_BATTERY_NOTIFY_STATUS	0x80
54 #define ACPI_BATTERY_NOTIFY_INFO	0x81
55 #define ACPI_BATTERY_NOTIFY_THRESHOLD   0x82
56 
57 #define _COMPONENT		ACPI_BATTERY_COMPONENT
58 
59 ACPI_MODULE_NAME("battery");
60 
61 MODULE_AUTHOR("Paul Diefenbaugh");
62 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
63 MODULE_DESCRIPTION("ACPI Battery Driver");
64 MODULE_LICENSE("GPL");
65 
66 static unsigned int cache_time = 1000;
67 module_param(cache_time, uint, 0644);
68 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
69 
70 #ifdef CONFIG_ACPI_PROCFS_POWER
71 extern struct proc_dir_entry *acpi_lock_battery_dir(void);
72 extern void *acpi_unlock_battery_dir(struct proc_dir_entry *acpi_battery_dir);
73 
74 enum acpi_battery_files {
75 	info_tag = 0,
76 	state_tag,
77 	alarm_tag,
78 	ACPI_BATTERY_NUMFILES,
79 };
80 
81 #endif
82 
83 static const struct acpi_device_id battery_device_ids[] = {
84 	{"PNP0C0A", 0},
85 	{"", 0},
86 };
87 
88 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
89 
90 enum {
91 	ACPI_BATTERY_ALARM_PRESENT,
92 	ACPI_BATTERY_XINFO_PRESENT,
93 	/* For buggy DSDTs that report negative 16-bit values for either
94 	 * charging or discharging current and/or report 0 as 65536
95 	 * due to bad math.
96 	 */
97 	ACPI_BATTERY_QUIRK_SIGNED16_CURRENT,
98 	ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
99 };
100 
101 struct acpi_battery {
102 	struct mutex lock;
103 	struct power_supply bat;
104 	struct acpi_device *device;
105 	unsigned long update_time;
106 	int rate_now;
107 	int capacity_now;
108 	int voltage_now;
109 	int design_capacity;
110 	int full_charge_capacity;
111 	int technology;
112 	int design_voltage;
113 	int design_capacity_warning;
114 	int design_capacity_low;
115 	int cycle_count;
116 	int measurement_accuracy;
117 	int max_sampling_time;
118 	int min_sampling_time;
119 	int max_averaging_interval;
120 	int min_averaging_interval;
121 	int capacity_granularity_1;
122 	int capacity_granularity_2;
123 	int alarm;
124 	char model_number[32];
125 	char serial_number[32];
126 	char type[32];
127 	char oem_info[32];
128 	int state;
129 	int power_unit;
130 	unsigned long flags;
131 };
132 
133 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat);
134 
135 inline int acpi_battery_present(struct acpi_battery *battery)
136 {
137 	return battery->device->status.battery_present;
138 }
139 
140 static int acpi_battery_technology(struct acpi_battery *battery)
141 {
142 	if (!strcasecmp("NiCd", battery->type))
143 		return POWER_SUPPLY_TECHNOLOGY_NiCd;
144 	if (!strcasecmp("NiMH", battery->type))
145 		return POWER_SUPPLY_TECHNOLOGY_NiMH;
146 	if (!strcasecmp("LION", battery->type))
147 		return POWER_SUPPLY_TECHNOLOGY_LION;
148 	if (!strncasecmp("LI-ION", battery->type, 6))
149 		return POWER_SUPPLY_TECHNOLOGY_LION;
150 	if (!strcasecmp("LiP", battery->type))
151 		return POWER_SUPPLY_TECHNOLOGY_LIPO;
152 	return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
153 }
154 
155 static int acpi_battery_get_state(struct acpi_battery *battery);
156 
157 static int acpi_battery_is_charged(struct acpi_battery *battery)
158 {
159 	/* either charging or discharging */
160 	if (battery->state != 0)
161 		return 0;
162 
163 	/* battery not reporting charge */
164 	if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
165 	    battery->capacity_now == 0)
166 		return 0;
167 
168 	/* good batteries update full_charge as the batteries degrade */
169 	if (battery->full_charge_capacity == battery->capacity_now)
170 		return 1;
171 
172 	/* fallback to using design values for broken batteries */
173 	if (battery->design_capacity == battery->capacity_now)
174 		return 1;
175 
176 	/* we don't do any sort of metric based on percentages */
177 	return 0;
178 }
179 
180 static int acpi_battery_get_property(struct power_supply *psy,
181 				     enum power_supply_property psp,
182 				     union power_supply_propval *val)
183 {
184 	int ret = 0;
185 	struct acpi_battery *battery = to_acpi_battery(psy);
186 
187 	if (acpi_battery_present(battery)) {
188 		/* run battery update only if it is present */
189 		acpi_battery_get_state(battery);
190 	} else if (psp != POWER_SUPPLY_PROP_PRESENT)
191 		return -ENODEV;
192 	switch (psp) {
193 	case POWER_SUPPLY_PROP_STATUS:
194 		if (battery->state & 0x01)
195 			val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
196 		else if (battery->state & 0x02)
197 			val->intval = POWER_SUPPLY_STATUS_CHARGING;
198 		else if (acpi_battery_is_charged(battery))
199 			val->intval = POWER_SUPPLY_STATUS_FULL;
200 		else
201 			val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
202 		break;
203 	case POWER_SUPPLY_PROP_PRESENT:
204 		val->intval = acpi_battery_present(battery);
205 		break;
206 	case POWER_SUPPLY_PROP_TECHNOLOGY:
207 		val->intval = acpi_battery_technology(battery);
208 		break;
209 	case POWER_SUPPLY_PROP_CYCLE_COUNT:
210 		val->intval = battery->cycle_count;
211 		break;
212 	case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
213 		if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
214 			ret = -ENODEV;
215 		else
216 			val->intval = battery->design_voltage * 1000;
217 		break;
218 	case POWER_SUPPLY_PROP_VOLTAGE_NOW:
219 		if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
220 			ret = -ENODEV;
221 		else
222 			val->intval = battery->voltage_now * 1000;
223 		break;
224 	case POWER_SUPPLY_PROP_CURRENT_NOW:
225 	case POWER_SUPPLY_PROP_POWER_NOW:
226 		if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
227 			ret = -ENODEV;
228 		else
229 			val->intval = battery->rate_now * 1000;
230 		break;
231 	case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
232 	case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
233 		if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
234 			ret = -ENODEV;
235 		else
236 			val->intval = battery->design_capacity * 1000;
237 		break;
238 	case POWER_SUPPLY_PROP_CHARGE_FULL:
239 	case POWER_SUPPLY_PROP_ENERGY_FULL:
240 		if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
241 			ret = -ENODEV;
242 		else
243 			val->intval = battery->full_charge_capacity * 1000;
244 		break;
245 	case POWER_SUPPLY_PROP_CHARGE_NOW:
246 	case POWER_SUPPLY_PROP_ENERGY_NOW:
247 		if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
248 			ret = -ENODEV;
249 		else
250 			val->intval = battery->capacity_now * 1000;
251 		break;
252 	case POWER_SUPPLY_PROP_MODEL_NAME:
253 		val->strval = battery->model_number;
254 		break;
255 	case POWER_SUPPLY_PROP_MANUFACTURER:
256 		val->strval = battery->oem_info;
257 		break;
258 	case POWER_SUPPLY_PROP_SERIAL_NUMBER:
259 		val->strval = battery->serial_number;
260 		break;
261 	default:
262 		ret = -EINVAL;
263 	}
264 	return ret;
265 }
266 
267 static enum power_supply_property charge_battery_props[] = {
268 	POWER_SUPPLY_PROP_STATUS,
269 	POWER_SUPPLY_PROP_PRESENT,
270 	POWER_SUPPLY_PROP_TECHNOLOGY,
271 	POWER_SUPPLY_PROP_CYCLE_COUNT,
272 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
273 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
274 	POWER_SUPPLY_PROP_CURRENT_NOW,
275 	POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
276 	POWER_SUPPLY_PROP_CHARGE_FULL,
277 	POWER_SUPPLY_PROP_CHARGE_NOW,
278 	POWER_SUPPLY_PROP_MODEL_NAME,
279 	POWER_SUPPLY_PROP_MANUFACTURER,
280 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
281 };
282 
283 static enum power_supply_property energy_battery_props[] = {
284 	POWER_SUPPLY_PROP_STATUS,
285 	POWER_SUPPLY_PROP_PRESENT,
286 	POWER_SUPPLY_PROP_TECHNOLOGY,
287 	POWER_SUPPLY_PROP_CYCLE_COUNT,
288 	POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
289 	POWER_SUPPLY_PROP_VOLTAGE_NOW,
290 	POWER_SUPPLY_PROP_POWER_NOW,
291 	POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
292 	POWER_SUPPLY_PROP_ENERGY_FULL,
293 	POWER_SUPPLY_PROP_ENERGY_NOW,
294 	POWER_SUPPLY_PROP_MODEL_NAME,
295 	POWER_SUPPLY_PROP_MANUFACTURER,
296 	POWER_SUPPLY_PROP_SERIAL_NUMBER,
297 };
298 
299 #ifdef CONFIG_ACPI_PROCFS_POWER
300 inline char *acpi_battery_units(struct acpi_battery *battery)
301 {
302 	return (battery->power_unit)?"mA":"mW";
303 }
304 #endif
305 
306 /* --------------------------------------------------------------------------
307                                Battery Management
308    -------------------------------------------------------------------------- */
309 struct acpi_offsets {
310 	size_t offset;		/* offset inside struct acpi_sbs_battery */
311 	u8 mode;		/* int or string? */
312 };
313 
314 static struct acpi_offsets state_offsets[] = {
315 	{offsetof(struct acpi_battery, state), 0},
316 	{offsetof(struct acpi_battery, rate_now), 0},
317 	{offsetof(struct acpi_battery, capacity_now), 0},
318 	{offsetof(struct acpi_battery, voltage_now), 0},
319 };
320 
321 static struct acpi_offsets info_offsets[] = {
322 	{offsetof(struct acpi_battery, power_unit), 0},
323 	{offsetof(struct acpi_battery, design_capacity), 0},
324 	{offsetof(struct acpi_battery, full_charge_capacity), 0},
325 	{offsetof(struct acpi_battery, technology), 0},
326 	{offsetof(struct acpi_battery, design_voltage), 0},
327 	{offsetof(struct acpi_battery, design_capacity_warning), 0},
328 	{offsetof(struct acpi_battery, design_capacity_low), 0},
329 	{offsetof(struct acpi_battery, capacity_granularity_1), 0},
330 	{offsetof(struct acpi_battery, capacity_granularity_2), 0},
331 	{offsetof(struct acpi_battery, model_number), 1},
332 	{offsetof(struct acpi_battery, serial_number), 1},
333 	{offsetof(struct acpi_battery, type), 1},
334 	{offsetof(struct acpi_battery, oem_info), 1},
335 };
336 
337 static struct acpi_offsets extended_info_offsets[] = {
338 	{offsetof(struct acpi_battery, power_unit), 0},
339 	{offsetof(struct acpi_battery, design_capacity), 0},
340 	{offsetof(struct acpi_battery, full_charge_capacity), 0},
341 	{offsetof(struct acpi_battery, technology), 0},
342 	{offsetof(struct acpi_battery, design_voltage), 0},
343 	{offsetof(struct acpi_battery, design_capacity_warning), 0},
344 	{offsetof(struct acpi_battery, design_capacity_low), 0},
345 	{offsetof(struct acpi_battery, cycle_count), 0},
346 	{offsetof(struct acpi_battery, measurement_accuracy), 0},
347 	{offsetof(struct acpi_battery, max_sampling_time), 0},
348 	{offsetof(struct acpi_battery, min_sampling_time), 0},
349 	{offsetof(struct acpi_battery, max_averaging_interval), 0},
350 	{offsetof(struct acpi_battery, min_averaging_interval), 0},
351 	{offsetof(struct acpi_battery, capacity_granularity_1), 0},
352 	{offsetof(struct acpi_battery, capacity_granularity_2), 0},
353 	{offsetof(struct acpi_battery, model_number), 1},
354 	{offsetof(struct acpi_battery, serial_number), 1},
355 	{offsetof(struct acpi_battery, type), 1},
356 	{offsetof(struct acpi_battery, oem_info), 1},
357 };
358 
359 static int extract_package(struct acpi_battery *battery,
360 			   union acpi_object *package,
361 			   struct acpi_offsets *offsets, int num)
362 {
363 	int i;
364 	union acpi_object *element;
365 	if (package->type != ACPI_TYPE_PACKAGE)
366 		return -EFAULT;
367 	for (i = 0; i < num; ++i) {
368 		if (package->package.count <= i)
369 			return -EFAULT;
370 		element = &package->package.elements[i];
371 		if (offsets[i].mode) {
372 			u8 *ptr = (u8 *)battery + offsets[i].offset;
373 			if (element->type == ACPI_TYPE_STRING ||
374 			    element->type == ACPI_TYPE_BUFFER)
375 				strncpy(ptr, element->string.pointer, 32);
376 			else if (element->type == ACPI_TYPE_INTEGER) {
377 				strncpy(ptr, (u8 *)&element->integer.value,
378 					sizeof(u64));
379 				ptr[sizeof(u64)] = 0;
380 			} else
381 				*ptr = 0; /* don't have value */
382 		} else {
383 			int *x = (int *)((u8 *)battery + offsets[i].offset);
384 			*x = (element->type == ACPI_TYPE_INTEGER) ?
385 				element->integer.value : -1;
386 		}
387 	}
388 	return 0;
389 }
390 
391 static int acpi_battery_get_status(struct acpi_battery *battery)
392 {
393 	if (acpi_bus_get_status(battery->device)) {
394 		ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
395 		return -ENODEV;
396 	}
397 	return 0;
398 }
399 
400 static int acpi_battery_get_info(struct acpi_battery *battery)
401 {
402 	int result = -EFAULT;
403 	acpi_status status = 0;
404 	char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags)?
405 			"_BIX" : "_BIF";
406 
407 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
408 
409 	if (!acpi_battery_present(battery))
410 		return 0;
411 	mutex_lock(&battery->lock);
412 	status = acpi_evaluate_object(battery->device->handle, name,
413 						NULL, &buffer);
414 	mutex_unlock(&battery->lock);
415 
416 	if (ACPI_FAILURE(status)) {
417 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
418 		return -ENODEV;
419 	}
420 	if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
421 		result = extract_package(battery, buffer.pointer,
422 				extended_info_offsets,
423 				ARRAY_SIZE(extended_info_offsets));
424 	else
425 		result = extract_package(battery, buffer.pointer,
426 				info_offsets, ARRAY_SIZE(info_offsets));
427 	kfree(buffer.pointer);
428 	if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
429 		battery->full_charge_capacity = battery->design_capacity;
430 	return result;
431 }
432 
433 static int acpi_battery_get_state(struct acpi_battery *battery)
434 {
435 	int result = 0;
436 	acpi_status status = 0;
437 	struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
438 
439 	if (!acpi_battery_present(battery))
440 		return 0;
441 
442 	if (battery->update_time &&
443 	    time_before(jiffies, battery->update_time +
444 			msecs_to_jiffies(cache_time)))
445 		return 0;
446 
447 	mutex_lock(&battery->lock);
448 	status = acpi_evaluate_object(battery->device->handle, "_BST",
449 				      NULL, &buffer);
450 	mutex_unlock(&battery->lock);
451 
452 	if (ACPI_FAILURE(status)) {
453 		ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
454 		return -ENODEV;
455 	}
456 
457 	result = extract_package(battery, buffer.pointer,
458 				 state_offsets, ARRAY_SIZE(state_offsets));
459 	battery->update_time = jiffies;
460 	kfree(buffer.pointer);
461 
462 	if (test_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags) &&
463 	    battery->rate_now != -1)
464 		battery->rate_now = abs((s16)battery->rate_now);
465 
466 	if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
467 	    && battery->capacity_now >= 0 && battery->capacity_now <= 100)
468 		battery->capacity_now = (battery->capacity_now *
469 				battery->full_charge_capacity) / 100;
470 	return result;
471 }
472 
473 static int acpi_battery_set_alarm(struct acpi_battery *battery)
474 {
475 	acpi_status status = 0;
476 	union acpi_object arg0 = { .type = ACPI_TYPE_INTEGER };
477 	struct acpi_object_list arg_list = { 1, &arg0 };
478 
479 	if (!acpi_battery_present(battery) ||
480 	    !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
481 		return -ENODEV;
482 
483 	arg0.integer.value = battery->alarm;
484 
485 	mutex_lock(&battery->lock);
486 	status = acpi_evaluate_object(battery->device->handle, "_BTP",
487 				 &arg_list, NULL);
488 	mutex_unlock(&battery->lock);
489 
490 	if (ACPI_FAILURE(status))
491 		return -ENODEV;
492 
493 	ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
494 	return 0;
495 }
496 
497 static int acpi_battery_init_alarm(struct acpi_battery *battery)
498 {
499 	acpi_status status = AE_OK;
500 	acpi_handle handle = NULL;
501 
502 	/* See if alarms are supported, and if so, set default */
503 	status = acpi_get_handle(battery->device->handle, "_BTP", &handle);
504 	if (ACPI_FAILURE(status)) {
505 		clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
506 		return 0;
507 	}
508 	set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
509 	if (!battery->alarm)
510 		battery->alarm = battery->design_capacity_warning;
511 	return acpi_battery_set_alarm(battery);
512 }
513 
514 static ssize_t acpi_battery_alarm_show(struct device *dev,
515 					struct device_attribute *attr,
516 					char *buf)
517 {
518 	struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
519 	return sprintf(buf, "%d\n", battery->alarm * 1000);
520 }
521 
522 static ssize_t acpi_battery_alarm_store(struct device *dev,
523 					struct device_attribute *attr,
524 					const char *buf, size_t count)
525 {
526 	unsigned long x;
527 	struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
528 	if (sscanf(buf, "%ld\n", &x) == 1)
529 		battery->alarm = x/1000;
530 	if (acpi_battery_present(battery))
531 		acpi_battery_set_alarm(battery);
532 	return count;
533 }
534 
535 static struct device_attribute alarm_attr = {
536 	.attr = {.name = "alarm", .mode = 0644},
537 	.show = acpi_battery_alarm_show,
538 	.store = acpi_battery_alarm_store,
539 };
540 
541 static int sysfs_add_battery(struct acpi_battery *battery)
542 {
543 	int result;
544 
545 	if (battery->power_unit) {
546 		battery->bat.properties = charge_battery_props;
547 		battery->bat.num_properties =
548 			ARRAY_SIZE(charge_battery_props);
549 	} else {
550 		battery->bat.properties = energy_battery_props;
551 		battery->bat.num_properties =
552 			ARRAY_SIZE(energy_battery_props);
553 	}
554 
555 	battery->bat.name = acpi_device_bid(battery->device);
556 	battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
557 	battery->bat.get_property = acpi_battery_get_property;
558 
559 	result = power_supply_register(&battery->device->dev, &battery->bat);
560 	if (result)
561 		return result;
562 	return device_create_file(battery->bat.dev, &alarm_attr);
563 }
564 
565 static void sysfs_remove_battery(struct acpi_battery *battery)
566 {
567 	if (!battery->bat.dev)
568 		return;
569 	device_remove_file(battery->bat.dev, &alarm_attr);
570 	power_supply_unregister(&battery->bat);
571 	battery->bat.dev = NULL;
572 }
573 
574 static void acpi_battery_quirks(struct acpi_battery *battery)
575 {
576 	if (dmi_name_in_vendors("Acer") && battery->power_unit) {
577 		set_bit(ACPI_BATTERY_QUIRK_SIGNED16_CURRENT, &battery->flags);
578 	}
579 }
580 
581 /*
582  * According to the ACPI spec, some kinds of primary batteries can
583  * report percentage battery remaining capacity directly to OS.
584  * In this case, it reports the Last Full Charged Capacity == 100
585  * and BatteryPresentRate == 0xFFFFFFFF.
586  *
587  * Now we found some battery reports percentage remaining capacity
588  * even if it's rechargeable.
589  * https://bugzilla.kernel.org/show_bug.cgi?id=15979
590  *
591  * Handle this correctly so that they won't break userspace.
592  */
593 static void acpi_battery_quirks2(struct acpi_battery *battery)
594 {
595 	if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
596 		return ;
597 
598         if (battery->full_charge_capacity == 100 &&
599             battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
600             battery->capacity_now >=0 && battery->capacity_now <= 100) {
601 		set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
602 		battery->full_charge_capacity = battery->design_capacity;
603 		battery->capacity_now = (battery->capacity_now *
604 				battery->full_charge_capacity) / 100;
605 	}
606 }
607 
608 static int acpi_battery_update(struct acpi_battery *battery)
609 {
610 	int result, old_present = acpi_battery_present(battery);
611 	result = acpi_battery_get_status(battery);
612 	if (result)
613 		return result;
614 	if (!acpi_battery_present(battery)) {
615 		sysfs_remove_battery(battery);
616 		battery->update_time = 0;
617 		return 0;
618 	}
619 	if (!battery->update_time ||
620 	    old_present != acpi_battery_present(battery)) {
621 		result = acpi_battery_get_info(battery);
622 		if (result)
623 			return result;
624 		acpi_battery_quirks(battery);
625 		acpi_battery_init_alarm(battery);
626 	}
627 	if (!battery->bat.dev)
628 		sysfs_add_battery(battery);
629 	result = acpi_battery_get_state(battery);
630 	acpi_battery_quirks2(battery);
631 	return result;
632 }
633 
634 static void acpi_battery_refresh(struct acpi_battery *battery)
635 {
636 	if (!battery->bat.dev)
637 		return;
638 
639 	acpi_battery_get_info(battery);
640 	/* The battery may have changed its reporting units. */
641 	sysfs_remove_battery(battery);
642 	sysfs_add_battery(battery);
643 }
644 
645 /* --------------------------------------------------------------------------
646                               FS Interface (/proc)
647    -------------------------------------------------------------------------- */
648 
649 #ifdef CONFIG_ACPI_PROCFS_POWER
650 static struct proc_dir_entry *acpi_battery_dir;
651 
652 static int acpi_battery_print_info(struct seq_file *seq, int result)
653 {
654 	struct acpi_battery *battery = seq->private;
655 
656 	if (result)
657 		goto end;
658 
659 	seq_printf(seq, "present:                 %s\n",
660 		   acpi_battery_present(battery)?"yes":"no");
661 	if (!acpi_battery_present(battery))
662 		goto end;
663 	if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
664 		seq_printf(seq, "design capacity:         unknown\n");
665 	else
666 		seq_printf(seq, "design capacity:         %d %sh\n",
667 			   battery->design_capacity,
668 			   acpi_battery_units(battery));
669 
670 	if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
671 		seq_printf(seq, "last full capacity:      unknown\n");
672 	else
673 		seq_printf(seq, "last full capacity:      %d %sh\n",
674 			   battery->full_charge_capacity,
675 			   acpi_battery_units(battery));
676 
677 	seq_printf(seq, "battery technology:      %srechargeable\n",
678 		   (!battery->technology)?"non-":"");
679 
680 	if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
681 		seq_printf(seq, "design voltage:          unknown\n");
682 	else
683 		seq_printf(seq, "design voltage:          %d mV\n",
684 			   battery->design_voltage);
685 	seq_printf(seq, "design capacity warning: %d %sh\n",
686 		   battery->design_capacity_warning,
687 		   acpi_battery_units(battery));
688 	seq_printf(seq, "design capacity low:     %d %sh\n",
689 		   battery->design_capacity_low,
690 		   acpi_battery_units(battery));
691 	seq_printf(seq, "cycle count:		  %i\n", battery->cycle_count);
692 	seq_printf(seq, "capacity granularity 1:  %d %sh\n",
693 		   battery->capacity_granularity_1,
694 		   acpi_battery_units(battery));
695 	seq_printf(seq, "capacity granularity 2:  %d %sh\n",
696 		   battery->capacity_granularity_2,
697 		   acpi_battery_units(battery));
698 	seq_printf(seq, "model number:            %s\n", battery->model_number);
699 	seq_printf(seq, "serial number:           %s\n", battery->serial_number);
700 	seq_printf(seq, "battery type:            %s\n", battery->type);
701 	seq_printf(seq, "OEM info:                %s\n", battery->oem_info);
702       end:
703 	if (result)
704 		seq_printf(seq, "ERROR: Unable to read battery info\n");
705 	return result;
706 }
707 
708 static int acpi_battery_print_state(struct seq_file *seq, int result)
709 {
710 	struct acpi_battery *battery = seq->private;
711 
712 	if (result)
713 		goto end;
714 
715 	seq_printf(seq, "present:                 %s\n",
716 		   acpi_battery_present(battery)?"yes":"no");
717 	if (!acpi_battery_present(battery))
718 		goto end;
719 
720 	seq_printf(seq, "capacity state:          %s\n",
721 			(battery->state & 0x04)?"critical":"ok");
722 	if ((battery->state & 0x01) && (battery->state & 0x02))
723 		seq_printf(seq,
724 			   "charging state:          charging/discharging\n");
725 	else if (battery->state & 0x01)
726 		seq_printf(seq, "charging state:          discharging\n");
727 	else if (battery->state & 0x02)
728 		seq_printf(seq, "charging state:          charging\n");
729 	else
730 		seq_printf(seq, "charging state:          charged\n");
731 
732 	if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
733 		seq_printf(seq, "present rate:            unknown\n");
734 	else
735 		seq_printf(seq, "present rate:            %d %s\n",
736 			   battery->rate_now, acpi_battery_units(battery));
737 
738 	if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
739 		seq_printf(seq, "remaining capacity:      unknown\n");
740 	else
741 		seq_printf(seq, "remaining capacity:      %d %sh\n",
742 			   battery->capacity_now, acpi_battery_units(battery));
743 	if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
744 		seq_printf(seq, "present voltage:         unknown\n");
745 	else
746 		seq_printf(seq, "present voltage:         %d mV\n",
747 			   battery->voltage_now);
748       end:
749 	if (result)
750 		seq_printf(seq, "ERROR: Unable to read battery state\n");
751 
752 	return result;
753 }
754 
755 static int acpi_battery_print_alarm(struct seq_file *seq, int result)
756 {
757 	struct acpi_battery *battery = seq->private;
758 
759 	if (result)
760 		goto end;
761 
762 	if (!acpi_battery_present(battery)) {
763 		seq_printf(seq, "present:                 no\n");
764 		goto end;
765 	}
766 	seq_printf(seq, "alarm:                   ");
767 	if (!battery->alarm)
768 		seq_printf(seq, "unsupported\n");
769 	else
770 		seq_printf(seq, "%u %sh\n", battery->alarm,
771 				acpi_battery_units(battery));
772       end:
773 	if (result)
774 		seq_printf(seq, "ERROR: Unable to read battery alarm\n");
775 	return result;
776 }
777 
778 static ssize_t acpi_battery_write_alarm(struct file *file,
779 					const char __user * buffer,
780 					size_t count, loff_t * ppos)
781 {
782 	int result = 0;
783 	char alarm_string[12] = { '\0' };
784 	struct seq_file *m = file->private_data;
785 	struct acpi_battery *battery = m->private;
786 
787 	if (!battery || (count > sizeof(alarm_string) - 1))
788 		return -EINVAL;
789 	if (!acpi_battery_present(battery)) {
790 		result = -ENODEV;
791 		goto end;
792 	}
793 	if (copy_from_user(alarm_string, buffer, count)) {
794 		result = -EFAULT;
795 		goto end;
796 	}
797 	alarm_string[count] = '\0';
798 	battery->alarm = simple_strtol(alarm_string, NULL, 0);
799 	result = acpi_battery_set_alarm(battery);
800       end:
801 	if (!result)
802 		return count;
803 	return result;
804 }
805 
806 typedef int(*print_func)(struct seq_file *seq, int result);
807 
808 static print_func acpi_print_funcs[ACPI_BATTERY_NUMFILES] = {
809 	acpi_battery_print_info,
810 	acpi_battery_print_state,
811 	acpi_battery_print_alarm,
812 };
813 
814 static int acpi_battery_read(int fid, struct seq_file *seq)
815 {
816 	struct acpi_battery *battery = seq->private;
817 	int result = acpi_battery_update(battery);
818 	return acpi_print_funcs[fid](seq, result);
819 }
820 
821 #define DECLARE_FILE_FUNCTIONS(_name) \
822 static int acpi_battery_read_##_name(struct seq_file *seq, void *offset) \
823 { \
824 	return acpi_battery_read(_name##_tag, seq); \
825 } \
826 static int acpi_battery_##_name##_open_fs(struct inode *inode, struct file *file) \
827 { \
828 	return single_open(file, acpi_battery_read_##_name, PDE(inode)->data); \
829 }
830 
831 DECLARE_FILE_FUNCTIONS(info);
832 DECLARE_FILE_FUNCTIONS(state);
833 DECLARE_FILE_FUNCTIONS(alarm);
834 
835 #undef DECLARE_FILE_FUNCTIONS
836 
837 #define FILE_DESCRIPTION_RO(_name) \
838 	{ \
839 	.name = __stringify(_name), \
840 	.mode = S_IRUGO, \
841 	.ops = { \
842 		.open = acpi_battery_##_name##_open_fs, \
843 		.read = seq_read, \
844 		.llseek = seq_lseek, \
845 		.release = single_release, \
846 		.owner = THIS_MODULE, \
847 		}, \
848 	}
849 
850 #define FILE_DESCRIPTION_RW(_name) \
851 	{ \
852 	.name = __stringify(_name), \
853 	.mode = S_IFREG | S_IRUGO | S_IWUSR, \
854 	.ops = { \
855 		.open = acpi_battery_##_name##_open_fs, \
856 		.read = seq_read, \
857 		.llseek = seq_lseek, \
858 		.write = acpi_battery_write_##_name, \
859 		.release = single_release, \
860 		.owner = THIS_MODULE, \
861 		}, \
862 	}
863 
864 static struct battery_file {
865 	struct file_operations ops;
866 	mode_t mode;
867 	const char *name;
868 } acpi_battery_file[] = {
869 	FILE_DESCRIPTION_RO(info),
870 	FILE_DESCRIPTION_RO(state),
871 	FILE_DESCRIPTION_RW(alarm),
872 };
873 
874 #undef FILE_DESCRIPTION_RO
875 #undef FILE_DESCRIPTION_RW
876 
877 static int acpi_battery_add_fs(struct acpi_device *device)
878 {
879 	struct proc_dir_entry *entry = NULL;
880 	int i;
881 
882 	printk(KERN_WARNING PREFIX "Deprecated procfs I/F for battery is loaded,"
883 			" please retry with CONFIG_ACPI_PROCFS_POWER cleared\n");
884 	if (!acpi_device_dir(device)) {
885 		acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device),
886 						     acpi_battery_dir);
887 		if (!acpi_device_dir(device))
888 			return -ENODEV;
889 	}
890 
891 	for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i) {
892 		entry = proc_create_data(acpi_battery_file[i].name,
893 					 acpi_battery_file[i].mode,
894 					 acpi_device_dir(device),
895 					 &acpi_battery_file[i].ops,
896 					 acpi_driver_data(device));
897 		if (!entry)
898 			return -ENODEV;
899 	}
900 	return 0;
901 }
902 
903 static void acpi_battery_remove_fs(struct acpi_device *device)
904 {
905 	int i;
906 	if (!acpi_device_dir(device))
907 		return;
908 	for (i = 0; i < ACPI_BATTERY_NUMFILES; ++i)
909 		remove_proc_entry(acpi_battery_file[i].name,
910 				  acpi_device_dir(device));
911 
912 	remove_proc_entry(acpi_device_bid(device), acpi_battery_dir);
913 	acpi_device_dir(device) = NULL;
914 }
915 
916 #endif
917 
918 /* --------------------------------------------------------------------------
919                                  Driver Interface
920    -------------------------------------------------------------------------- */
921 
922 static void acpi_battery_notify(struct acpi_device *device, u32 event)
923 {
924 	struct acpi_battery *battery = acpi_driver_data(device);
925 	struct device *old;
926 
927 	if (!battery)
928 		return;
929 	old = battery->bat.dev;
930 	if (event == ACPI_BATTERY_NOTIFY_INFO)
931 		acpi_battery_refresh(battery);
932 	acpi_battery_update(battery);
933 	acpi_bus_generate_proc_event(device, event,
934 				     acpi_battery_present(battery));
935 	acpi_bus_generate_netlink_event(device->pnp.device_class,
936 					dev_name(&device->dev), event,
937 					acpi_battery_present(battery));
938 	/* acpi_battery_update could remove power_supply object */
939 	if (old && battery->bat.dev)
940 		power_supply_changed(&battery->bat);
941 }
942 
943 static int acpi_battery_add(struct acpi_device *device)
944 {
945 	int result = 0;
946 	struct acpi_battery *battery = NULL;
947 	acpi_handle handle;
948 	if (!device)
949 		return -EINVAL;
950 	battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
951 	if (!battery)
952 		return -ENOMEM;
953 	battery->device = device;
954 	strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
955 	strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
956 	device->driver_data = battery;
957 	mutex_init(&battery->lock);
958 	if (ACPI_SUCCESS(acpi_get_handle(battery->device->handle,
959 			"_BIX", &handle)))
960 		set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
961 	acpi_battery_update(battery);
962 #ifdef CONFIG_ACPI_PROCFS_POWER
963 	result = acpi_battery_add_fs(device);
964 #endif
965 	if (!result) {
966 		printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
967 			ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
968 			device->status.battery_present ? "present" : "absent");
969 	} else {
970 #ifdef CONFIG_ACPI_PROCFS_POWER
971 		acpi_battery_remove_fs(device);
972 #endif
973 		kfree(battery);
974 	}
975 	return result;
976 }
977 
978 static int acpi_battery_remove(struct acpi_device *device, int type)
979 {
980 	struct acpi_battery *battery = NULL;
981 
982 	if (!device || !acpi_driver_data(device))
983 		return -EINVAL;
984 	battery = acpi_driver_data(device);
985 #ifdef CONFIG_ACPI_PROCFS_POWER
986 	acpi_battery_remove_fs(device);
987 #endif
988 	sysfs_remove_battery(battery);
989 	mutex_destroy(&battery->lock);
990 	kfree(battery);
991 	return 0;
992 }
993 
994 /* this is needed to learn about changes made in suspended state */
995 static int acpi_battery_resume(struct acpi_device *device)
996 {
997 	struct acpi_battery *battery;
998 	if (!device)
999 		return -EINVAL;
1000 	battery = acpi_driver_data(device);
1001 	battery->update_time = 0;
1002 	acpi_battery_update(battery);
1003 	return 0;
1004 }
1005 
1006 static struct acpi_driver acpi_battery_driver = {
1007 	.name = "battery",
1008 	.class = ACPI_BATTERY_CLASS,
1009 	.ids = battery_device_ids,
1010 	.flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
1011 	.ops = {
1012 		.add = acpi_battery_add,
1013 		.resume = acpi_battery_resume,
1014 		.remove = acpi_battery_remove,
1015 		.notify = acpi_battery_notify,
1016 		},
1017 };
1018 
1019 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
1020 {
1021 	if (acpi_disabled)
1022 		return;
1023 #ifdef CONFIG_ACPI_PROCFS_POWER
1024 	acpi_battery_dir = acpi_lock_battery_dir();
1025 	if (!acpi_battery_dir)
1026 		return;
1027 #endif
1028 	if (acpi_bus_register_driver(&acpi_battery_driver) < 0) {
1029 #ifdef CONFIG_ACPI_PROCFS_POWER
1030 		acpi_unlock_battery_dir(acpi_battery_dir);
1031 #endif
1032 		return;
1033 	}
1034 	return;
1035 }
1036 
1037 static int __init acpi_battery_init(void)
1038 {
1039 	async_schedule(acpi_battery_init_async, NULL);
1040 	return 0;
1041 }
1042 
1043 static void __exit acpi_battery_exit(void)
1044 {
1045 	acpi_bus_unregister_driver(&acpi_battery_driver);
1046 #ifdef CONFIG_ACPI_PROCFS_POWER
1047 	acpi_unlock_battery_dir(acpi_battery_dir);
1048 #endif
1049 }
1050 
1051 module_init(acpi_battery_init);
1052 module_exit(acpi_battery_exit);
1053