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