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