ACPI: Clean up inclusions of ACPI header files
[deliverable/linux.git] / drivers / acpi / battery.c
1 /*
2 * battery.c - ACPI Battery Driver (Revision: 2.0)
3 *
4 * Copyright (C) 2007 Alexey Starikovskiy <astarikovskiy@suse.de>
5 * Copyright (C) 2004-2007 Vladimir Lebedev <vladimir.p.lebedev@intel.com>
6 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com>
7 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com>
8 *
9 * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or (at
14 * your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but
17 * WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
19 * General Public License for more details.
20 *
21 * 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 #include <linux/suspend.h>
37 #include <asm/unaligned.h>
38
39 #include <linux/acpi.h>
40 #include <linux/power_supply.h>
41
42 #define PREFIX "ACPI: "
43
44 #define ACPI_BATTERY_VALUE_UNKNOWN 0xFFFFFFFF
45
46 #define ACPI_BATTERY_CLASS "battery"
47 #define ACPI_BATTERY_DEVICE_NAME "Battery"
48 #define ACPI_BATTERY_NOTIFY_STATUS 0x80
49 #define ACPI_BATTERY_NOTIFY_INFO 0x81
50 #define ACPI_BATTERY_NOTIFY_THRESHOLD 0x82
51
52 /* Battery power unit: 0 means mW, 1 means mA */
53 #define ACPI_BATTERY_POWER_UNIT_MA 1
54
55 #define _COMPONENT ACPI_BATTERY_COMPONENT
56
57 ACPI_MODULE_NAME("battery");
58
59 MODULE_AUTHOR("Paul Diefenbaugh");
60 MODULE_AUTHOR("Alexey Starikovskiy <astarikovskiy@suse.de>");
61 MODULE_DESCRIPTION("ACPI Battery Driver");
62 MODULE_LICENSE("GPL");
63
64 static unsigned int cache_time = 1000;
65 module_param(cache_time, uint, 0644);
66 MODULE_PARM_DESC(cache_time, "cache time in milliseconds");
67
68 static const struct acpi_device_id battery_device_ids[] = {
69 {"PNP0C0A", 0},
70 {"", 0},
71 };
72
73 MODULE_DEVICE_TABLE(acpi, battery_device_ids);
74
75 enum {
76 ACPI_BATTERY_ALARM_PRESENT,
77 ACPI_BATTERY_XINFO_PRESENT,
78 ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY,
79 /* On Lenovo Thinkpad models from 2010 and 2011, the power unit
80 switches between mWh and mAh depending on whether the system
81 is running on battery or not. When mAh is the unit, most
82 reported values are incorrect and need to be adjusted by
83 10000/design_voltage. Verified on x201, t410, t410s, and x220.
84 Pre-2010 and 2012 models appear to always report in mWh and
85 are thus unaffected (tested with t42, t61, t500, x200, x300,
86 and x230). Also, in mid-2012 Lenovo issued a BIOS update for
87 the 2011 models that fixes the issue (tested on x220 with a
88 post-1.29 BIOS), but as of Nov. 2012, no such update is
89 available for the 2010 models. */
90 ACPI_BATTERY_QUIRK_THINKPAD_MAH,
91 };
92
93 struct acpi_battery {
94 struct mutex lock;
95 struct mutex sysfs_lock;
96 struct power_supply bat;
97 struct acpi_device *device;
98 struct notifier_block pm_nb;
99 unsigned long update_time;
100 int revision;
101 int rate_now;
102 int capacity_now;
103 int voltage_now;
104 int design_capacity;
105 int full_charge_capacity;
106 int technology;
107 int design_voltage;
108 int design_capacity_warning;
109 int design_capacity_low;
110 int cycle_count;
111 int measurement_accuracy;
112 int max_sampling_time;
113 int min_sampling_time;
114 int max_averaging_interval;
115 int min_averaging_interval;
116 int capacity_granularity_1;
117 int capacity_granularity_2;
118 int alarm;
119 char model_number[32];
120 char serial_number[32];
121 char type[32];
122 char oem_info[32];
123 int state;
124 int power_unit;
125 unsigned long flags;
126 };
127
128 #define to_acpi_battery(x) container_of(x, struct acpi_battery, bat)
129
130 static inline int acpi_battery_present(struct acpi_battery *battery)
131 {
132 return battery->device->status.battery_present;
133 }
134
135 static int acpi_battery_technology(struct acpi_battery *battery)
136 {
137 if (!strcasecmp("NiCd", battery->type))
138 return POWER_SUPPLY_TECHNOLOGY_NiCd;
139 if (!strcasecmp("NiMH", battery->type))
140 return POWER_SUPPLY_TECHNOLOGY_NiMH;
141 if (!strcasecmp("LION", battery->type))
142 return POWER_SUPPLY_TECHNOLOGY_LION;
143 if (!strncasecmp("LI-ION", battery->type, 6))
144 return POWER_SUPPLY_TECHNOLOGY_LION;
145 if (!strcasecmp("LiP", battery->type))
146 return POWER_SUPPLY_TECHNOLOGY_LIPO;
147 return POWER_SUPPLY_TECHNOLOGY_UNKNOWN;
148 }
149
150 static int acpi_battery_get_state(struct acpi_battery *battery);
151
152 static int acpi_battery_is_charged(struct acpi_battery *battery)
153 {
154 /* either charging or discharging */
155 if (battery->state != 0)
156 return 0;
157
158 /* battery not reporting charge */
159 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN ||
160 battery->capacity_now == 0)
161 return 0;
162
163 /* good batteries update full_charge as the batteries degrade */
164 if (battery->full_charge_capacity == battery->capacity_now)
165 return 1;
166
167 /* fallback to using design values for broken batteries */
168 if (battery->design_capacity == battery->capacity_now)
169 return 1;
170
171 /* we don't do any sort of metric based on percentages */
172 return 0;
173 }
174
175 static int acpi_battery_get_property(struct power_supply *psy,
176 enum power_supply_property psp,
177 union power_supply_propval *val)
178 {
179 int ret = 0;
180 struct acpi_battery *battery = to_acpi_battery(psy);
181
182 if (acpi_battery_present(battery)) {
183 /* run battery update only if it is present */
184 acpi_battery_get_state(battery);
185 } else if (psp != POWER_SUPPLY_PROP_PRESENT)
186 return -ENODEV;
187 switch (psp) {
188 case POWER_SUPPLY_PROP_STATUS:
189 if (battery->state & 0x01)
190 val->intval = POWER_SUPPLY_STATUS_DISCHARGING;
191 else if (battery->state & 0x02)
192 val->intval = POWER_SUPPLY_STATUS_CHARGING;
193 else if (acpi_battery_is_charged(battery))
194 val->intval = POWER_SUPPLY_STATUS_FULL;
195 else
196 val->intval = POWER_SUPPLY_STATUS_UNKNOWN;
197 break;
198 case POWER_SUPPLY_PROP_PRESENT:
199 val->intval = acpi_battery_present(battery);
200 break;
201 case POWER_SUPPLY_PROP_TECHNOLOGY:
202 val->intval = acpi_battery_technology(battery);
203 break;
204 case POWER_SUPPLY_PROP_CYCLE_COUNT:
205 val->intval = battery->cycle_count;
206 break;
207 case POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN:
208 if (battery->design_voltage == ACPI_BATTERY_VALUE_UNKNOWN)
209 ret = -ENODEV;
210 else
211 val->intval = battery->design_voltage * 1000;
212 break;
213 case POWER_SUPPLY_PROP_VOLTAGE_NOW:
214 if (battery->voltage_now == ACPI_BATTERY_VALUE_UNKNOWN)
215 ret = -ENODEV;
216 else
217 val->intval = battery->voltage_now * 1000;
218 break;
219 case POWER_SUPPLY_PROP_CURRENT_NOW:
220 case POWER_SUPPLY_PROP_POWER_NOW:
221 if (battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN)
222 ret = -ENODEV;
223 else
224 val->intval = battery->rate_now * 1000;
225 break;
226 case POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN:
227 case POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN:
228 if (battery->design_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
229 ret = -ENODEV;
230 else
231 val->intval = battery->design_capacity * 1000;
232 break;
233 case POWER_SUPPLY_PROP_CHARGE_FULL:
234 case POWER_SUPPLY_PROP_ENERGY_FULL:
235 if (battery->full_charge_capacity == ACPI_BATTERY_VALUE_UNKNOWN)
236 ret = -ENODEV;
237 else
238 val->intval = battery->full_charge_capacity * 1000;
239 break;
240 case POWER_SUPPLY_PROP_CHARGE_NOW:
241 case POWER_SUPPLY_PROP_ENERGY_NOW:
242 if (battery->capacity_now == ACPI_BATTERY_VALUE_UNKNOWN)
243 ret = -ENODEV;
244 else
245 val->intval = battery->capacity_now * 1000;
246 break;
247 case POWER_SUPPLY_PROP_CAPACITY:
248 if (battery->capacity_now && battery->full_charge_capacity)
249 val->intval = battery->capacity_now * 100/
250 battery->full_charge_capacity;
251 else
252 val->intval = 0;
253 break;
254 case POWER_SUPPLY_PROP_MODEL_NAME:
255 val->strval = battery->model_number;
256 break;
257 case POWER_SUPPLY_PROP_MANUFACTURER:
258 val->strval = battery->oem_info;
259 break;
260 case POWER_SUPPLY_PROP_SERIAL_NUMBER:
261 val->strval = battery->serial_number;
262 break;
263 default:
264 ret = -EINVAL;
265 }
266 return ret;
267 }
268
269 static enum power_supply_property charge_battery_props[] = {
270 POWER_SUPPLY_PROP_STATUS,
271 POWER_SUPPLY_PROP_PRESENT,
272 POWER_SUPPLY_PROP_TECHNOLOGY,
273 POWER_SUPPLY_PROP_CYCLE_COUNT,
274 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
275 POWER_SUPPLY_PROP_VOLTAGE_NOW,
276 POWER_SUPPLY_PROP_CURRENT_NOW,
277 POWER_SUPPLY_PROP_CHARGE_FULL_DESIGN,
278 POWER_SUPPLY_PROP_CHARGE_FULL,
279 POWER_SUPPLY_PROP_CHARGE_NOW,
280 POWER_SUPPLY_PROP_CAPACITY,
281 POWER_SUPPLY_PROP_MODEL_NAME,
282 POWER_SUPPLY_PROP_MANUFACTURER,
283 POWER_SUPPLY_PROP_SERIAL_NUMBER,
284 };
285
286 static enum power_supply_property energy_battery_props[] = {
287 POWER_SUPPLY_PROP_STATUS,
288 POWER_SUPPLY_PROP_PRESENT,
289 POWER_SUPPLY_PROP_TECHNOLOGY,
290 POWER_SUPPLY_PROP_CYCLE_COUNT,
291 POWER_SUPPLY_PROP_VOLTAGE_MIN_DESIGN,
292 POWER_SUPPLY_PROP_VOLTAGE_NOW,
293 POWER_SUPPLY_PROP_POWER_NOW,
294 POWER_SUPPLY_PROP_ENERGY_FULL_DESIGN,
295 POWER_SUPPLY_PROP_ENERGY_FULL,
296 POWER_SUPPLY_PROP_ENERGY_NOW,
297 POWER_SUPPLY_PROP_CAPACITY,
298 POWER_SUPPLY_PROP_MODEL_NAME,
299 POWER_SUPPLY_PROP_MANUFACTURER,
300 POWER_SUPPLY_PROP_SERIAL_NUMBER,
301 };
302
303 /* --------------------------------------------------------------------------
304 Battery Management
305 -------------------------------------------------------------------------- */
306 struct acpi_offsets {
307 size_t offset; /* offset inside struct acpi_sbs_battery */
308 u8 mode; /* int or string? */
309 };
310
311 static struct acpi_offsets state_offsets[] = {
312 {offsetof(struct acpi_battery, state), 0},
313 {offsetof(struct acpi_battery, rate_now), 0},
314 {offsetof(struct acpi_battery, capacity_now), 0},
315 {offsetof(struct acpi_battery, voltage_now), 0},
316 };
317
318 static struct acpi_offsets info_offsets[] = {
319 {offsetof(struct acpi_battery, power_unit), 0},
320 {offsetof(struct acpi_battery, design_capacity), 0},
321 {offsetof(struct acpi_battery, full_charge_capacity), 0},
322 {offsetof(struct acpi_battery, technology), 0},
323 {offsetof(struct acpi_battery, design_voltage), 0},
324 {offsetof(struct acpi_battery, design_capacity_warning), 0},
325 {offsetof(struct acpi_battery, design_capacity_low), 0},
326 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
327 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
328 {offsetof(struct acpi_battery, model_number), 1},
329 {offsetof(struct acpi_battery, serial_number), 1},
330 {offsetof(struct acpi_battery, type), 1},
331 {offsetof(struct acpi_battery, oem_info), 1},
332 };
333
334 static struct acpi_offsets extended_info_offsets[] = {
335 {offsetof(struct acpi_battery, revision), 0},
336 {offsetof(struct acpi_battery, power_unit), 0},
337 {offsetof(struct acpi_battery, design_capacity), 0},
338 {offsetof(struct acpi_battery, full_charge_capacity), 0},
339 {offsetof(struct acpi_battery, technology), 0},
340 {offsetof(struct acpi_battery, design_voltage), 0},
341 {offsetof(struct acpi_battery, design_capacity_warning), 0},
342 {offsetof(struct acpi_battery, design_capacity_low), 0},
343 {offsetof(struct acpi_battery, cycle_count), 0},
344 {offsetof(struct acpi_battery, measurement_accuracy), 0},
345 {offsetof(struct acpi_battery, max_sampling_time), 0},
346 {offsetof(struct acpi_battery, min_sampling_time), 0},
347 {offsetof(struct acpi_battery, max_averaging_interval), 0},
348 {offsetof(struct acpi_battery, min_averaging_interval), 0},
349 {offsetof(struct acpi_battery, capacity_granularity_1), 0},
350 {offsetof(struct acpi_battery, capacity_granularity_2), 0},
351 {offsetof(struct acpi_battery, model_number), 1},
352 {offsetof(struct acpi_battery, serial_number), 1},
353 {offsetof(struct acpi_battery, type), 1},
354 {offsetof(struct acpi_battery, oem_info), 1},
355 };
356
357 static int extract_package(struct acpi_battery *battery,
358 union acpi_object *package,
359 struct acpi_offsets *offsets, int num)
360 {
361 int i;
362 union acpi_object *element;
363 if (package->type != ACPI_TYPE_PACKAGE)
364 return -EFAULT;
365 for (i = 0; i < num; ++i) {
366 if (package->package.count <= i)
367 return -EFAULT;
368 element = &package->package.elements[i];
369 if (offsets[i].mode) {
370 u8 *ptr = (u8 *)battery + offsets[i].offset;
371 if (element->type == ACPI_TYPE_STRING ||
372 element->type == ACPI_TYPE_BUFFER)
373 strncpy(ptr, element->string.pointer, 32);
374 else if (element->type == ACPI_TYPE_INTEGER) {
375 strncpy(ptr, (u8 *)&element->integer.value,
376 sizeof(u64));
377 ptr[sizeof(u64)] = 0;
378 } else
379 *ptr = 0; /* don't have value */
380 } else {
381 int *x = (int *)((u8 *)battery + offsets[i].offset);
382 *x = (element->type == ACPI_TYPE_INTEGER) ?
383 element->integer.value : -1;
384 }
385 }
386 return 0;
387 }
388
389 static int acpi_battery_get_status(struct acpi_battery *battery)
390 {
391 if (acpi_bus_get_status(battery->device)) {
392 ACPI_EXCEPTION((AE_INFO, AE_ERROR, "Evaluating _STA"));
393 return -ENODEV;
394 }
395 return 0;
396 }
397
398 static int acpi_battery_get_info(struct acpi_battery *battery)
399 {
400 int result = -EFAULT;
401 acpi_status status = 0;
402 char *name = test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags) ?
403 "_BIX" : "_BIF";
404
405 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
406
407 if (!acpi_battery_present(battery))
408 return 0;
409 mutex_lock(&battery->lock);
410 status = acpi_evaluate_object(battery->device->handle, name,
411 NULL, &buffer);
412 mutex_unlock(&battery->lock);
413
414 if (ACPI_FAILURE(status)) {
415 ACPI_EXCEPTION((AE_INFO, status, "Evaluating %s", name));
416 return -ENODEV;
417 }
418 if (test_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags))
419 result = extract_package(battery, buffer.pointer,
420 extended_info_offsets,
421 ARRAY_SIZE(extended_info_offsets));
422 else
423 result = extract_package(battery, buffer.pointer,
424 info_offsets, ARRAY_SIZE(info_offsets));
425 kfree(buffer.pointer);
426 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
427 battery->full_charge_capacity = battery->design_capacity;
428 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
429 battery->power_unit && battery->design_voltage) {
430 battery->design_capacity = battery->design_capacity *
431 10000 / battery->design_voltage;
432 battery->full_charge_capacity = battery->full_charge_capacity *
433 10000 / battery->design_voltage;
434 battery->design_capacity_warning =
435 battery->design_capacity_warning *
436 10000 / battery->design_voltage;
437 /* Curiously, design_capacity_low, unlike the rest of them,
438 is correct. */
439 /* capacity_granularity_* equal 1 on the systems tested, so
440 it's impossible to tell if they would need an adjustment
441 or not if their values were higher. */
442 }
443 return result;
444 }
445
446 static int acpi_battery_get_state(struct acpi_battery *battery)
447 {
448 int result = 0;
449 acpi_status status = 0;
450 struct acpi_buffer buffer = { ACPI_ALLOCATE_BUFFER, NULL };
451
452 if (!acpi_battery_present(battery))
453 return 0;
454
455 if (battery->update_time &&
456 time_before(jiffies, battery->update_time +
457 msecs_to_jiffies(cache_time)))
458 return 0;
459
460 mutex_lock(&battery->lock);
461 status = acpi_evaluate_object(battery->device->handle, "_BST",
462 NULL, &buffer);
463 mutex_unlock(&battery->lock);
464
465 if (ACPI_FAILURE(status)) {
466 ACPI_EXCEPTION((AE_INFO, status, "Evaluating _BST"));
467 return -ENODEV;
468 }
469
470 result = extract_package(battery, buffer.pointer,
471 state_offsets, ARRAY_SIZE(state_offsets));
472 battery->update_time = jiffies;
473 kfree(buffer.pointer);
474
475 /* For buggy DSDTs that report negative 16-bit values for either
476 * charging or discharging current and/or report 0 as 65536
477 * due to bad math.
478 */
479 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA &&
480 battery->rate_now != ACPI_BATTERY_VALUE_UNKNOWN &&
481 (s16)(battery->rate_now) < 0) {
482 battery->rate_now = abs((s16)battery->rate_now);
483 printk_once(KERN_WARNING FW_BUG "battery: (dis)charge rate"
484 " invalid.\n");
485 }
486
487 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags)
488 && battery->capacity_now >= 0 && battery->capacity_now <= 100)
489 battery->capacity_now = (battery->capacity_now *
490 battery->full_charge_capacity) / 100;
491 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags) &&
492 battery->power_unit && battery->design_voltage) {
493 battery->capacity_now = battery->capacity_now *
494 10000 / battery->design_voltage;
495 }
496 return result;
497 }
498
499 static int acpi_battery_set_alarm(struct acpi_battery *battery)
500 {
501 acpi_status status = 0;
502
503 if (!acpi_battery_present(battery) ||
504 !test_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags))
505 return -ENODEV;
506
507 mutex_lock(&battery->lock);
508 status = acpi_execute_simple_method(battery->device->handle, "_BTP",
509 battery->alarm);
510 mutex_unlock(&battery->lock);
511
512 if (ACPI_FAILURE(status))
513 return -ENODEV;
514
515 ACPI_DEBUG_PRINT((ACPI_DB_INFO, "Alarm set to %d\n", battery->alarm));
516 return 0;
517 }
518
519 static int acpi_battery_init_alarm(struct acpi_battery *battery)
520 {
521 /* See if alarms are supported, and if so, set default */
522 if (!acpi_has_method(battery->device->handle, "_BTP")) {
523 clear_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
524 return 0;
525 }
526 set_bit(ACPI_BATTERY_ALARM_PRESENT, &battery->flags);
527 if (!battery->alarm)
528 battery->alarm = battery->design_capacity_warning;
529 return acpi_battery_set_alarm(battery);
530 }
531
532 static ssize_t acpi_battery_alarm_show(struct device *dev,
533 struct device_attribute *attr,
534 char *buf)
535 {
536 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
537 return sprintf(buf, "%d\n", battery->alarm * 1000);
538 }
539
540 static ssize_t acpi_battery_alarm_store(struct device *dev,
541 struct device_attribute *attr,
542 const char *buf, size_t count)
543 {
544 unsigned long x;
545 struct acpi_battery *battery = to_acpi_battery(dev_get_drvdata(dev));
546 if (sscanf(buf, "%ld\n", &x) == 1)
547 battery->alarm = x/1000;
548 if (acpi_battery_present(battery))
549 acpi_battery_set_alarm(battery);
550 return count;
551 }
552
553 static struct device_attribute alarm_attr = {
554 .attr = {.name = "alarm", .mode = 0644},
555 .show = acpi_battery_alarm_show,
556 .store = acpi_battery_alarm_store,
557 };
558
559 static int sysfs_add_battery(struct acpi_battery *battery)
560 {
561 int result;
562
563 if (battery->power_unit == ACPI_BATTERY_POWER_UNIT_MA) {
564 battery->bat.properties = charge_battery_props;
565 battery->bat.num_properties =
566 ARRAY_SIZE(charge_battery_props);
567 } else {
568 battery->bat.properties = energy_battery_props;
569 battery->bat.num_properties =
570 ARRAY_SIZE(energy_battery_props);
571 }
572
573 battery->bat.name = acpi_device_bid(battery->device);
574 battery->bat.type = POWER_SUPPLY_TYPE_BATTERY;
575 battery->bat.get_property = acpi_battery_get_property;
576
577 result = power_supply_register(&battery->device->dev, &battery->bat);
578 if (result)
579 return result;
580 return device_create_file(battery->bat.dev, &alarm_attr);
581 }
582
583 static void sysfs_remove_battery(struct acpi_battery *battery)
584 {
585 mutex_lock(&battery->sysfs_lock);
586 if (!battery->bat.dev) {
587 mutex_unlock(&battery->sysfs_lock);
588 return;
589 }
590
591 device_remove_file(battery->bat.dev, &alarm_attr);
592 power_supply_unregister(&battery->bat);
593 battery->bat.dev = NULL;
594 mutex_unlock(&battery->sysfs_lock);
595 }
596
597 static void find_battery(const struct dmi_header *dm, void *private)
598 {
599 struct acpi_battery *battery = (struct acpi_battery *)private;
600 /* Note: the hardcoded offsets below have been extracted from
601 the source code of dmidecode. */
602 if (dm->type == DMI_ENTRY_PORTABLE_BATTERY && dm->length >= 8) {
603 const u8 *dmi_data = (const u8 *)(dm + 1);
604 int dmi_capacity = get_unaligned((const u16 *)(dmi_data + 6));
605 if (dm->length >= 18)
606 dmi_capacity *= dmi_data[17];
607 if (battery->design_capacity * battery->design_voltage / 1000
608 != dmi_capacity &&
609 battery->design_capacity * 10 == dmi_capacity)
610 set_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
611 &battery->flags);
612 }
613 }
614
615 /*
616 * According to the ACPI spec, some kinds of primary batteries can
617 * report percentage battery remaining capacity directly to OS.
618 * In this case, it reports the Last Full Charged Capacity == 100
619 * and BatteryPresentRate == 0xFFFFFFFF.
620 *
621 * Now we found some battery reports percentage remaining capacity
622 * even if it's rechargeable.
623 * https://bugzilla.kernel.org/show_bug.cgi?id=15979
624 *
625 * Handle this correctly so that they won't break userspace.
626 */
627 static void acpi_battery_quirks(struct acpi_battery *battery)
628 {
629 if (test_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags))
630 return;
631
632 if (battery->full_charge_capacity == 100 &&
633 battery->rate_now == ACPI_BATTERY_VALUE_UNKNOWN &&
634 battery->capacity_now >= 0 && battery->capacity_now <= 100) {
635 set_bit(ACPI_BATTERY_QUIRK_PERCENTAGE_CAPACITY, &battery->flags);
636 battery->full_charge_capacity = battery->design_capacity;
637 battery->capacity_now = (battery->capacity_now *
638 battery->full_charge_capacity) / 100;
639 }
640
641 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH, &battery->flags))
642 return;
643
644 if (battery->power_unit && dmi_name_in_vendors("LENOVO")) {
645 const char *s;
646 s = dmi_get_system_info(DMI_PRODUCT_VERSION);
647 if (s && !strnicmp(s, "ThinkPad", 8)) {
648 dmi_walk(find_battery, battery);
649 if (test_bit(ACPI_BATTERY_QUIRK_THINKPAD_MAH,
650 &battery->flags) &&
651 battery->design_voltage) {
652 battery->design_capacity =
653 battery->design_capacity *
654 10000 / battery->design_voltage;
655 battery->full_charge_capacity =
656 battery->full_charge_capacity *
657 10000 / battery->design_voltage;
658 battery->design_capacity_warning =
659 battery->design_capacity_warning *
660 10000 / battery->design_voltage;
661 battery->capacity_now = battery->capacity_now *
662 10000 / battery->design_voltage;
663 }
664 }
665 }
666 }
667
668 static int acpi_battery_update(struct acpi_battery *battery)
669 {
670 int result, old_present = acpi_battery_present(battery);
671 result = acpi_battery_get_status(battery);
672 if (result)
673 return result;
674 if (!acpi_battery_present(battery)) {
675 sysfs_remove_battery(battery);
676 battery->update_time = 0;
677 return 0;
678 }
679 if (!battery->update_time ||
680 old_present != acpi_battery_present(battery)) {
681 result = acpi_battery_get_info(battery);
682 if (result)
683 return result;
684 acpi_battery_init_alarm(battery);
685 }
686 if (!battery->bat.dev) {
687 result = sysfs_add_battery(battery);
688 if (result)
689 return result;
690 }
691 result = acpi_battery_get_state(battery);
692 acpi_battery_quirks(battery);
693 return result;
694 }
695
696 static void acpi_battery_refresh(struct acpi_battery *battery)
697 {
698 int power_unit;
699
700 if (!battery->bat.dev)
701 return;
702
703 power_unit = battery->power_unit;
704
705 acpi_battery_get_info(battery);
706
707 if (power_unit == battery->power_unit)
708 return;
709
710 /* The battery has changed its reporting units. */
711 sysfs_remove_battery(battery);
712 sysfs_add_battery(battery);
713 }
714
715 /* --------------------------------------------------------------------------
716 Driver Interface
717 -------------------------------------------------------------------------- */
718
719 static void acpi_battery_notify(struct acpi_device *device, u32 event)
720 {
721 struct acpi_battery *battery = acpi_driver_data(device);
722 struct device *old;
723
724 if (!battery)
725 return;
726 old = battery->bat.dev;
727 if (event == ACPI_BATTERY_NOTIFY_INFO)
728 acpi_battery_refresh(battery);
729 acpi_battery_update(battery);
730 acpi_bus_generate_netlink_event(device->pnp.device_class,
731 dev_name(&device->dev), event,
732 acpi_battery_present(battery));
733 /* acpi_battery_update could remove power_supply object */
734 if (old && battery->bat.dev)
735 power_supply_changed(&battery->bat);
736 }
737
738 static int battery_notify(struct notifier_block *nb,
739 unsigned long mode, void *_unused)
740 {
741 struct acpi_battery *battery = container_of(nb, struct acpi_battery,
742 pm_nb);
743 switch (mode) {
744 case PM_POST_HIBERNATION:
745 case PM_POST_SUSPEND:
746 if (battery->bat.dev) {
747 sysfs_remove_battery(battery);
748 sysfs_add_battery(battery);
749 }
750 break;
751 }
752
753 return 0;
754 }
755
756 static int acpi_battery_add(struct acpi_device *device)
757 {
758 int result = 0;
759 struct acpi_battery *battery = NULL;
760
761 if (!device)
762 return -EINVAL;
763 battery = kzalloc(sizeof(struct acpi_battery), GFP_KERNEL);
764 if (!battery)
765 return -ENOMEM;
766 battery->device = device;
767 strcpy(acpi_device_name(device), ACPI_BATTERY_DEVICE_NAME);
768 strcpy(acpi_device_class(device), ACPI_BATTERY_CLASS);
769 device->driver_data = battery;
770 mutex_init(&battery->lock);
771 mutex_init(&battery->sysfs_lock);
772 if (acpi_has_method(battery->device->handle, "_BIX"))
773 set_bit(ACPI_BATTERY_XINFO_PRESENT, &battery->flags);
774 result = acpi_battery_update(battery);
775 if (result)
776 goto fail;
777
778 printk(KERN_INFO PREFIX "%s Slot [%s] (battery %s)\n",
779 ACPI_BATTERY_DEVICE_NAME, acpi_device_bid(device),
780 device->status.battery_present ? "present" : "absent");
781
782 battery->pm_nb.notifier_call = battery_notify;
783 register_pm_notifier(&battery->pm_nb);
784
785 return result;
786
787 fail:
788 sysfs_remove_battery(battery);
789 mutex_destroy(&battery->lock);
790 mutex_destroy(&battery->sysfs_lock);
791 kfree(battery);
792 return result;
793 }
794
795 static int acpi_battery_remove(struct acpi_device *device)
796 {
797 struct acpi_battery *battery = NULL;
798
799 if (!device || !acpi_driver_data(device))
800 return -EINVAL;
801 battery = acpi_driver_data(device);
802 unregister_pm_notifier(&battery->pm_nb);
803 sysfs_remove_battery(battery);
804 mutex_destroy(&battery->lock);
805 mutex_destroy(&battery->sysfs_lock);
806 kfree(battery);
807 return 0;
808 }
809
810 #ifdef CONFIG_PM_SLEEP
811 /* this is needed to learn about changes made in suspended state */
812 static int acpi_battery_resume(struct device *dev)
813 {
814 struct acpi_battery *battery;
815
816 if (!dev)
817 return -EINVAL;
818
819 battery = acpi_driver_data(to_acpi_device(dev));
820 if (!battery)
821 return -EINVAL;
822
823 battery->update_time = 0;
824 acpi_battery_update(battery);
825 return 0;
826 }
827 #endif
828
829 static SIMPLE_DEV_PM_OPS(acpi_battery_pm, NULL, acpi_battery_resume);
830
831 static struct acpi_driver acpi_battery_driver = {
832 .name = "battery",
833 .class = ACPI_BATTERY_CLASS,
834 .ids = battery_device_ids,
835 .flags = ACPI_DRIVER_ALL_NOTIFY_EVENTS,
836 .ops = {
837 .add = acpi_battery_add,
838 .remove = acpi_battery_remove,
839 .notify = acpi_battery_notify,
840 },
841 .drv.pm = &acpi_battery_pm,
842 };
843
844 static void __init acpi_battery_init_async(void *unused, async_cookie_t cookie)
845 {
846 if (acpi_disabled)
847 return;
848 acpi_bus_register_driver(&acpi_battery_driver);
849 }
850
851 static int __init acpi_battery_init(void)
852 {
853 async_schedule(acpi_battery_init_async, NULL);
854 return 0;
855 }
856
857 static void __exit acpi_battery_exit(void)
858 {
859 acpi_bus_unregister_driver(&acpi_battery_driver);
860 }
861
862 module_init(acpi_battery_init);
863 module_exit(acpi_battery_exit);
This page took 0.051196 seconds and 5 git commands to generate.