1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * button.c - ACPI Button Driver 4 * 5 * Copyright (C) 2001, 2002 Andy Grover <andrew.grover@intel.com> 6 * Copyright (C) 2001, 2002 Paul Diefenbaugh <paul.s.diefenbaugh@intel.com> 7 */ 8 9 #define pr_fmt(fmt) "ACPI: button: " fmt 10 11 #include <linux/compiler.h> 12 #include <linux/kernel.h> 13 #include <linux/module.h> 14 #include <linux/init.h> 15 #include <linux/types.h> 16 #include <linux/proc_fs.h> 17 #include <linux/seq_file.h> 18 #include <linux/input.h> 19 #include <linux/slab.h> 20 #include <linux/acpi.h> 21 #include <linux/dmi.h> 22 #include <linux/platform_device.h> 23 #include <acpi/button.h> 24 25 #define ACPI_BUTTON_CLASS "button" 26 #define ACPI_BUTTON_FILE_STATE "state" 27 #define ACPI_BUTTON_TYPE_UNKNOWN 0x00 28 #define ACPI_BUTTON_NOTIFY_WAKE 0x02 29 #define ACPI_BUTTON_NOTIFY_STATUS 0x80 30 31 #define ACPI_BUTTON_CLASS_POWER "button/power" 32 #define ACPI_BUTTON_DEVICE_NAME_POWER "Power Button" 33 #define ACPI_BUTTON_TYPE_POWER 0x01 34 35 #define ACPI_BUTTON_CLASS_SLEEP "button/sleep" 36 #define ACPI_BUTTON_DEVICE_NAME_SLEEP "Sleep Button" 37 #define ACPI_BUTTON_TYPE_SLEEP 0x03 38 39 #define ACPI_BUTTON_CLASS_LID "button/lid" 40 #define ACPI_BUTTON_SUBCLASS_LID "lid" 41 #define ACPI_BUTTON_DEVICE_NAME_LID "Lid Switch" 42 #define ACPI_BUTTON_TYPE_LID 0x05 43 44 enum { 45 ACPI_BUTTON_LID_INIT_IGNORE, 46 ACPI_BUTTON_LID_INIT_OPEN, 47 ACPI_BUTTON_LID_INIT_METHOD, 48 ACPI_BUTTON_LID_INIT_DISABLED, 49 }; 50 51 static const char * const lid_init_state_str[] = { 52 [ACPI_BUTTON_LID_INIT_IGNORE] = "ignore", 53 [ACPI_BUTTON_LID_INIT_OPEN] = "open", 54 [ACPI_BUTTON_LID_INIT_METHOD] = "method", 55 [ACPI_BUTTON_LID_INIT_DISABLED] = "disabled", 56 }; 57 58 MODULE_AUTHOR("Paul Diefenbaugh"); 59 MODULE_DESCRIPTION("ACPI Button Driver"); 60 MODULE_LICENSE("GPL"); 61 62 static const struct acpi_device_id button_device_ids[] = { 63 {ACPI_BUTTON_HID_LID, ACPI_BUTTON_TYPE_LID}, 64 {ACPI_BUTTON_HID_SLEEP, ACPI_BUTTON_TYPE_SLEEP}, 65 {ACPI_BUTTON_HID_SLEEPF, ACPI_BUTTON_TYPE_SLEEP}, 66 {ACPI_BUTTON_HID_POWER, ACPI_BUTTON_TYPE_POWER}, 67 {ACPI_BUTTON_HID_POWERF, ACPI_BUTTON_TYPE_POWER}, 68 {"", 0}, 69 }; 70 MODULE_DEVICE_TABLE(acpi, button_device_ids); 71 72 /* Please keep this list sorted alphabetically by vendor and model */ 73 static const struct dmi_system_id dmi_lid_quirks[] = { 74 { 75 /* GP-electronic T701, _LID method points to a floating GPIO */ 76 .matches = { 77 DMI_MATCH(DMI_SYS_VENDOR, "Insyde"), 78 DMI_MATCH(DMI_PRODUCT_NAME, "T701"), 79 DMI_MATCH(DMI_BIOS_VERSION, "BYT70A.YNCHENG.WIN.007"), 80 }, 81 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED, 82 }, 83 { 84 /* Nextbook Ares 8A tablet, _LID device always reports lid closed */ 85 .matches = { 86 DMI_MATCH(DMI_SYS_VENDOR, "Insyde"), 87 DMI_MATCH(DMI_PRODUCT_NAME, "CherryTrail"), 88 DMI_MATCH(DMI_BIOS_VERSION, "M882"), 89 }, 90 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_DISABLED, 91 }, 92 { 93 /* 94 * Lenovo Yoga 9 14ITL5, initial notification of the LID device 95 * never happens. 96 */ 97 .matches = { 98 DMI_MATCH(DMI_SYS_VENDOR, "LENOVO"), 99 DMI_MATCH(DMI_PRODUCT_NAME, "82BG"), 100 }, 101 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN, 102 }, 103 { 104 /* 105 * Medion Akoya E2215T, notification of the LID device only 106 * happens on close, not on open and _LID always returns closed. 107 */ 108 .matches = { 109 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"), 110 DMI_MATCH(DMI_PRODUCT_NAME, "E2215T"), 111 }, 112 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN, 113 }, 114 { 115 /* 116 * Medion Akoya E2228T, notification of the LID device only 117 * happens on close, not on open and _LID always returns closed. 118 */ 119 .matches = { 120 DMI_MATCH(DMI_SYS_VENDOR, "MEDION"), 121 DMI_MATCH(DMI_PRODUCT_NAME, "E2228T"), 122 }, 123 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN, 124 }, 125 { 126 /* 127 * Razer Blade Stealth 13 late 2019, notification of the LID device 128 * only happens on close, not on open and _LID always returns closed. 129 */ 130 .matches = { 131 DMI_MATCH(DMI_SYS_VENDOR, "Razer"), 132 DMI_MATCH(DMI_PRODUCT_NAME, "Razer Blade Stealth 13 Late 2019"), 133 }, 134 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN, 135 }, 136 { 137 /* 138 * Samsung galaxybook2 ,initial _LID device notification returns 139 * lid closed. 140 */ 141 .matches = { 142 DMI_MATCH(DMI_SYS_VENDOR, "SAMSUNG ELECTRONICS CO., LTD."), 143 DMI_MATCH(DMI_PRODUCT_NAME, "750XED"), 144 }, 145 .driver_data = (void *)(long)ACPI_BUTTON_LID_INIT_OPEN, 146 }, 147 {} 148 }; 149 150 static int acpi_button_probe(struct platform_device *pdev); 151 static void acpi_button_remove(struct platform_device *pdev); 152 153 #ifdef CONFIG_PM_SLEEP 154 static int acpi_button_suspend(struct device *dev); 155 static int acpi_button_resume(struct device *dev); 156 #else 157 #define acpi_button_suspend NULL 158 #define acpi_button_resume NULL 159 #endif 160 static SIMPLE_DEV_PM_OPS(acpi_button_pm, acpi_button_suspend, acpi_button_resume); 161 162 static struct platform_driver acpi_button_driver = { 163 .probe = acpi_button_probe, 164 .remove = acpi_button_remove, 165 .driver = { 166 .name = "acpi-button", 167 .acpi_match_table = button_device_ids, 168 .pm = &acpi_button_pm, 169 }, 170 }; 171 172 struct acpi_button { 173 struct acpi_device *adev; 174 struct device *dev; /* physical button device */ 175 unsigned int type; 176 struct input_dev *input; 177 const char *class; /* for netlink messages */ 178 char phys[32]; /* for input device */ 179 unsigned long pushed; 180 bool last_state; 181 ktime_t last_time; 182 bool suspended; 183 bool lid_state_initialized; 184 bool gpe_enabled; 185 }; 186 187 static long lid_init_state = -1; 188 189 static unsigned long lid_report_interval __read_mostly = 500; 190 module_param(lid_report_interval, ulong, 0644); 191 MODULE_PARM_DESC(lid_report_interval, "Interval (ms) between lid key events"); 192 193 /* FS Interface (/proc) */ 194 static struct proc_dir_entry *acpi_button_dir; 195 static struct proc_dir_entry *acpi_lid_dir; 196 197 static int acpi_lid_evaluate_state(acpi_handle lid_handle) 198 { 199 unsigned long long lid_state; 200 acpi_status status; 201 202 status = acpi_evaluate_integer(lid_handle, "_LID", NULL, &lid_state); 203 if (ACPI_FAILURE(status)) 204 return -ENODEV; 205 206 return !!lid_state; 207 } 208 209 static void acpi_lid_notify_state(struct acpi_button *button, bool state) 210 { 211 struct acpi_device *device = button->adev; 212 ktime_t next_report; 213 bool do_update; 214 215 /* 216 * In lid_init_state=ignore mode, if user opens/closes lid 217 * frequently with "open" missing, and "last_time" is also updated 218 * frequently, "close" cannot be delivered to the userspace. 219 * So "last_time" is only updated after a timeout or an actual 220 * switch. 221 */ 222 do_update = lid_init_state != ACPI_BUTTON_LID_INIT_IGNORE || 223 button->last_state != state; 224 next_report = ktime_add(button->last_time, 225 ms_to_ktime(lid_report_interval)); 226 if (button->last_state == state && 227 ktime_after(ktime_get(), next_report)) { 228 /* Complain about the buggy firmware. */ 229 pr_warn_once(FW_BUG "Unexpected lid state reported by firmware\n"); 230 231 /* 232 * Send the unreliable complement switch event: 233 * 234 * On most platforms, the lid device is reliable. However 235 * there are exceptions: 236 * 1. Platforms returning initial lid state as "close" by 237 * default after booting/resuming: 238 * https://bugzilla.kernel.org/show_bug.cgi?id=89211 239 * https://bugzilla.kernel.org/show_bug.cgi?id=106151 240 * 2. Platforms never reporting "open" events: 241 * https://bugzilla.kernel.org/show_bug.cgi?id=106941 242 * On these buggy platforms, the usage model of the ACPI 243 * lid device actually is: 244 * 1. The initial returning value of _LID may not be 245 * reliable. 246 * 2. The open event may not be reliable. 247 * 3. The close event is reliable. 248 * 249 * But SW_LID is typed as input switch event, the input 250 * layer checks if the event is redundant. Hence if the 251 * state is not switched, the userspace cannot see this 252 * platform triggered reliable event. By inserting a 253 * complement switch event, it then is guaranteed that the 254 * platform triggered reliable one can always be seen by 255 * the userspace. 256 */ 257 if (lid_init_state == ACPI_BUTTON_LID_INIT_IGNORE) { 258 do_update = true; 259 /* 260 * Do generate complement switch event for "close" 261 * as "close" is reliable and wrong "open" won't 262 * trigger unexpected behaviors. 263 * Do not generate complement switch event for 264 * "open" as "open" is not reliable and wrong 265 * "close" will trigger unexpected behaviors. 266 */ 267 if (!state) { 268 input_report_switch(button->input, 269 SW_LID, state); 270 input_sync(button->input); 271 } 272 } 273 } 274 /* Send the platform triggered reliable event */ 275 if (do_update) { 276 acpi_handle_debug(device->handle, "ACPI LID %s\n", 277 state ? "open" : "closed"); 278 input_report_switch(button->input, SW_LID, !state); 279 input_sync(button->input); 280 button->last_state = state; 281 button->last_time = ktime_get(); 282 } 283 } 284 285 static int __maybe_unused acpi_button_state_seq_show(struct seq_file *seq, 286 void *offset) 287 { 288 struct acpi_button *button = seq->private; 289 int state; 290 291 state = acpi_lid_evaluate_state(button->adev->handle); 292 seq_printf(seq, "state: %s\n", 293 state < 0 ? "unsupported" : (state ? "open" : "closed")); 294 return 0; 295 } 296 297 static int acpi_lid_add_fs(struct acpi_button *button) 298 { 299 struct acpi_device *device = button->adev; 300 struct proc_dir_entry *entry = NULL; 301 302 if (acpi_button_dir || acpi_lid_dir) { 303 pr_info("More than one Lid device found!\n"); 304 return -EEXIST; 305 } 306 307 /* create /proc/acpi/button */ 308 acpi_button_dir = proc_mkdir(ACPI_BUTTON_CLASS, acpi_root_dir); 309 if (!acpi_button_dir) 310 return -ENODEV; 311 312 /* create /proc/acpi/button/lid */ 313 acpi_lid_dir = proc_mkdir(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); 314 if (!acpi_lid_dir) 315 goto remove_button_dir; 316 317 /* create /proc/acpi/button/lid/LID/ */ 318 acpi_device_dir(device) = proc_mkdir(acpi_device_bid(device), acpi_lid_dir); 319 if (!acpi_device_dir(device)) 320 goto remove_lid_dir; 321 322 /* create /proc/acpi/button/lid/LID/state */ 323 entry = proc_create_single_data(ACPI_BUTTON_FILE_STATE, S_IRUGO, 324 acpi_device_dir(device), acpi_button_state_seq_show, 325 button); 326 if (!entry) 327 goto remove_dev_dir; 328 329 return 0; 330 331 remove_dev_dir: 332 remove_proc_entry(acpi_device_bid(device), acpi_lid_dir); 333 acpi_device_dir(device) = NULL; 334 remove_lid_dir: 335 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); 336 acpi_lid_dir = NULL; 337 remove_button_dir: 338 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); 339 acpi_button_dir = NULL; 340 return -ENODEV; 341 } 342 343 static void acpi_lid_remove_fs(void *data) 344 { 345 struct acpi_button *button = data; 346 struct acpi_device *device = button->adev; 347 348 remove_proc_entry(ACPI_BUTTON_FILE_STATE, 349 acpi_device_dir(device)); 350 remove_proc_entry(acpi_device_bid(device), 351 acpi_lid_dir); 352 acpi_device_dir(device) = NULL; 353 remove_proc_entry(ACPI_BUTTON_SUBCLASS_LID, acpi_button_dir); 354 acpi_lid_dir = NULL; 355 remove_proc_entry(ACPI_BUTTON_CLASS, acpi_root_dir); 356 acpi_button_dir = NULL; 357 } 358 359 static int devm_acpi_lid_add_fs(struct device *dev, struct acpi_button *button) 360 { 361 int ret; 362 363 ret = acpi_lid_add_fs(button); 364 if (ret) 365 return ret; 366 367 return devm_add_action_or_reset(dev, acpi_lid_remove_fs, button); 368 } 369 370 static acpi_handle saved_lid_handle; 371 static DEFINE_MUTEX(acpi_lid_lock); 372 373 static void acpi_lid_save(struct acpi_device *adev) 374 { 375 guard(mutex)(&acpi_lid_lock); 376 377 saved_lid_handle = adev->handle; 378 } 379 380 static void acpi_lid_forget(struct acpi_device *adev) 381 { 382 guard(mutex)(&acpi_lid_lock); 383 384 if (saved_lid_handle == adev->handle) 385 saved_lid_handle = NULL; 386 } 387 388 /* Driver Interface */ 389 int acpi_lid_open(void) 390 { 391 guard(mutex)(&acpi_lid_lock); 392 393 if (!saved_lid_handle) 394 return -ENODEV; 395 396 return acpi_lid_evaluate_state(saved_lid_handle); 397 } 398 EXPORT_SYMBOL(acpi_lid_open); 399 400 static void acpi_lid_update_state(struct acpi_button *button, bool signal_wakeup) 401 { 402 int state; 403 404 state = acpi_lid_evaluate_state(button->adev->handle); 405 if (state < 0) 406 return; 407 408 if (state && signal_wakeup) 409 acpi_pm_wakeup_event(button->dev); 410 411 acpi_lid_notify_state(button, state); 412 } 413 414 static void acpi_lid_initialize_state(struct acpi_button *button) 415 { 416 switch (lid_init_state) { 417 case ACPI_BUTTON_LID_INIT_OPEN: 418 acpi_lid_notify_state(button, true); 419 break; 420 case ACPI_BUTTON_LID_INIT_METHOD: 421 acpi_lid_update_state(button, false); 422 break; 423 case ACPI_BUTTON_LID_INIT_IGNORE: 424 default: 425 break; 426 } 427 428 button->lid_state_initialized = true; 429 } 430 431 static void acpi_lid_notify(acpi_handle handle, u32 event, void *data) 432 { 433 struct acpi_button *button = data; 434 struct acpi_device *device = button->adev; 435 436 if (event != ACPI_BUTTON_NOTIFY_STATUS) { 437 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n", 438 event); 439 return; 440 } 441 442 if (!button->lid_state_initialized) 443 return; 444 445 acpi_lid_update_state(button, true); 446 } 447 448 static void acpi_button_notify(acpi_handle handle, u32 event, void *data) 449 { 450 struct acpi_button *button = data; 451 struct acpi_device *device = button->adev; 452 struct input_dev *input; 453 int keycode; 454 455 switch (event) { 456 case ACPI_BUTTON_NOTIFY_STATUS: 457 break; 458 case ACPI_BUTTON_NOTIFY_WAKE: 459 break; 460 default: 461 acpi_handle_debug(device->handle, "Unsupported event [0x%x]\n", 462 event); 463 return; 464 } 465 466 acpi_pm_wakeup_event(button->dev); 467 468 if (button->suspended || event == ACPI_BUTTON_NOTIFY_WAKE) 469 return; 470 471 input = button->input; 472 keycode = test_bit(KEY_SLEEP, input->keybit) ? KEY_SLEEP : KEY_POWER; 473 474 input_report_key(input, keycode, 1); 475 input_sync(input); 476 input_report_key(input, keycode, 0); 477 input_sync(input); 478 479 acpi_bus_generate_netlink_event(button->class, dev_name(&device->dev), 480 event, ++button->pushed); 481 } 482 483 static void acpi_button_notify_run(void *data) 484 { 485 acpi_button_notify(NULL, ACPI_BUTTON_NOTIFY_STATUS, data); 486 } 487 488 static u32 acpi_button_event(void *data) 489 { 490 acpi_os_execute(OSL_NOTIFY_HANDLER, acpi_button_notify_run, data); 491 return ACPI_INTERRUPT_HANDLED; 492 } 493 494 #ifdef CONFIG_PM_SLEEP 495 static int acpi_button_suspend(struct device *dev) 496 { 497 struct acpi_button *button = dev_get_drvdata(dev); 498 499 button->suspended = true; 500 return 0; 501 } 502 503 static int acpi_button_resume(struct device *dev) 504 { 505 struct acpi_button *button = dev_get_drvdata(dev); 506 struct input_dev *input; 507 508 button->suspended = false; 509 if (button->type == ACPI_BUTTON_TYPE_LID) { 510 button->last_state = !!acpi_lid_evaluate_state(ACPI_HANDLE(dev)); 511 button->last_time = ktime_get(); 512 acpi_lid_initialize_state(button); 513 } 514 515 if (button->type == ACPI_BUTTON_TYPE_POWER) { 516 input = button->input; 517 input_report_key(input, KEY_WAKEUP, 1); 518 input_sync(input); 519 input_report_key(input, KEY_WAKEUP, 0); 520 input_sync(input); 521 } 522 return 0; 523 } 524 #endif 525 526 static int acpi_lid_input_open(struct input_dev *input) 527 { 528 struct acpi_button *button = input_get_drvdata(input); 529 530 button->last_state = !!acpi_lid_evaluate_state(button->adev->handle); 531 button->last_time = ktime_get(); 532 acpi_lid_initialize_state(button); 533 534 return 0; 535 } 536 537 static acpi_notify_handler acpi_button_notify_handler(struct acpi_button *button) 538 { 539 if (button->type == ACPI_BUTTON_TYPE_LID) 540 return acpi_lid_notify; 541 542 return acpi_button_notify; 543 } 544 545 static void acpi_button_wakeup_cleanup(void *data) 546 { 547 device_init_wakeup(data, false); 548 } 549 550 static int devm_acpi_button_init_wakeup(struct device *dev) 551 { 552 device_init_wakeup(dev, true); 553 return devm_add_action_or_reset(dev, acpi_button_wakeup_cleanup, dev); 554 } 555 556 static void acpi_button_remove_event_handler(void *data) 557 { 558 struct acpi_button *button = data; 559 struct acpi_device *adev = button->adev; 560 561 switch (adev->device_type) { 562 case ACPI_BUS_TYPE_POWER_BUTTON: 563 acpi_remove_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, 564 acpi_button_event); 565 break; 566 567 case ACPI_BUS_TYPE_SLEEP_BUTTON: 568 acpi_remove_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, 569 acpi_button_event); 570 break; 571 572 default: 573 if (button->gpe_enabled) { 574 dev_dbg(button->dev, "Disabling ACPI GPE%02llx\n", 575 adev->wakeup.gpe_number); 576 acpi_disable_gpe(adev->wakeup.gpe_device, 577 adev->wakeup.gpe_number); 578 } 579 acpi_remove_notify_handler(adev->handle, ACPI_ALL_NOTIFY, 580 acpi_button_notify_handler(button)); 581 break; 582 } 583 acpi_os_wait_events_complete(); 584 } 585 586 static int acpi_button_add_fixed_event_handler(u32 event, 587 struct acpi_button *button) 588 { 589 acpi_status status; 590 591 status = acpi_install_fixed_event_handler(event, acpi_button_event, button); 592 if (ACPI_FAILURE(status)) 593 return -ENODEV; 594 595 return 0; 596 } 597 598 static int acpi_button_add_event_handler(struct acpi_button *button) 599 { 600 struct acpi_device *adev = button->adev; 601 acpi_status status; 602 603 if (adev->device_type == ACPI_BUS_TYPE_POWER_BUTTON) 604 return acpi_button_add_fixed_event_handler(ACPI_EVENT_POWER_BUTTON, 605 button); 606 607 if (adev->device_type == ACPI_BUS_TYPE_SLEEP_BUTTON) 608 return acpi_button_add_fixed_event_handler(ACPI_EVENT_SLEEP_BUTTON, 609 button); 610 611 status = acpi_install_notify_handler(adev->handle, ACPI_ALL_NOTIFY, 612 acpi_button_notify_handler(button), 613 button); 614 if (ACPI_FAILURE(status)) 615 return -ENODEV; 616 617 if (!adev->wakeup.flags.valid) 618 return 0; 619 620 /* 621 * If the wakeup GPE has a handler method, enable it in case it is also 622 * used for signaling runtime events. 623 */ 624 status = acpi_enable_gpe_cond(adev->wakeup.gpe_device, 625 adev->wakeup.gpe_number, 626 ACPI_GPE_DISPATCH_METHOD); 627 button->gpe_enabled = ACPI_SUCCESS(status); 628 if (button->gpe_enabled) 629 dev_dbg(button->dev, "Enabled ACPI GPE%02llx\n", 630 adev->wakeup.gpe_number); 631 632 return 0; 633 } 634 635 static int devm_acpi_button_add_event_handler(struct device *dev, 636 struct acpi_button *button) 637 { 638 int ret; 639 640 ret = acpi_button_add_event_handler(button); 641 if (ret) 642 return ret; 643 644 return devm_add_action_or_reset(dev, acpi_button_remove_event_handler, 645 button); 646 } 647 648 static int acpi_button_probe(struct platform_device *pdev) 649 { 650 struct device *dev = &pdev->dev; 651 struct acpi_device *device = ACPI_COMPANION(dev); 652 const struct acpi_device_id *id; 653 struct acpi_button *button; 654 struct input_dev *input; 655 u8 button_type; 656 int error = 0; 657 658 id = acpi_match_acpi_device(button_device_ids, device); 659 if (!id || strcmp(acpi_device_hid(device), id->id)) 660 return dev_err_probe(dev, -ENODEV, "Unsupported device\n"); 661 662 button_type = id->driver_data; 663 if (button_type == ACPI_BUTTON_TYPE_LID && 664 lid_init_state == ACPI_BUTTON_LID_INIT_DISABLED) 665 return -ENODEV; 666 667 button = devm_kzalloc(dev, sizeof(*button), GFP_KERNEL); 668 if (!button) 669 return -ENOMEM; 670 671 platform_set_drvdata(pdev, button); 672 673 button->dev = dev; 674 button->adev = device; 675 input = devm_input_allocate_device(dev); 676 if (!input) 677 return -ENOMEM; 678 679 button->input = input; 680 button->type = button_type; 681 682 switch (button_type) { 683 case ACPI_BUTTON_TYPE_LID: 684 button->class = ACPI_BUTTON_CLASS_LID; 685 686 input->name = ACPI_BUTTON_DEVICE_NAME_LID; 687 input_set_capability(input, EV_SW, SW_LID); 688 input->open = acpi_lid_input_open; 689 690 error = devm_acpi_lid_add_fs(dev, button); 691 if (error) 692 return error; 693 694 break; 695 696 case ACPI_BUTTON_TYPE_POWER: 697 button->class = ACPI_BUTTON_CLASS_POWER; 698 699 input->name = ACPI_BUTTON_DEVICE_NAME_POWER; 700 input_set_capability(input, EV_KEY, KEY_POWER); 701 input_set_capability(input, EV_KEY, KEY_WAKEUP); 702 break; 703 704 case ACPI_BUTTON_TYPE_SLEEP: 705 button->class = ACPI_BUTTON_CLASS_SLEEP; 706 707 input->name = ACPI_BUTTON_DEVICE_NAME_SLEEP; 708 input_set_capability(input, EV_KEY, KEY_SLEEP); 709 break; 710 711 default: 712 return dev_err_probe(dev, -ENODEV, "Unrecognized button type\n"); 713 } 714 715 snprintf(button->phys, sizeof(button->phys), "%s/button/input0", 716 acpi_device_hid(device)); 717 718 input->phys = button->phys; 719 input->id.bustype = BUS_HOST; 720 input->id.product = button_type; 721 722 input_set_drvdata(input, button); 723 error = input_register_device(input); 724 if (error) 725 return error; 726 727 error = devm_acpi_button_init_wakeup(dev); 728 if (error) 729 return error; 730 731 error = devm_acpi_button_add_event_handler(dev, button); 732 if (error) 733 return error; 734 735 if (button_type == ACPI_BUTTON_TYPE_LID) { 736 /* 737 * This assumes there's only one lid device, or if there are 738 * more we only care about the last one... 739 */ 740 acpi_lid_save(device); 741 } 742 743 pr_info("%s [%s]\n", input->name, acpi_device_bid(device)); 744 745 return 0; 746 } 747 748 static void acpi_button_remove(struct platform_device *pdev) 749 { 750 struct acpi_button *button = platform_get_drvdata(pdev); 751 752 if (button->type == ACPI_BUTTON_TYPE_LID) 753 acpi_lid_forget(button->adev); 754 } 755 756 static int param_set_lid_init_state(const char *val, 757 const struct kernel_param *kp) 758 { 759 int i; 760 761 i = sysfs_match_string(lid_init_state_str, val); 762 if (i < 0) 763 return i; 764 765 lid_init_state = i; 766 pr_info("Initial lid state set to '%s'\n", lid_init_state_str[i]); 767 return 0; 768 } 769 770 static int param_get_lid_init_state(char *buf, const struct kernel_param *kp) 771 { 772 int i, c = 0; 773 774 for (i = 0; i < ARRAY_SIZE(lid_init_state_str); i++) 775 if (i == lid_init_state) 776 c += sprintf(buf + c, "[%s] ", lid_init_state_str[i]); 777 else 778 c += sprintf(buf + c, "%s ", lid_init_state_str[i]); 779 780 buf[c - 1] = '\n'; /* Replace the final space with a newline */ 781 782 return c; 783 } 784 785 module_param_call(lid_init_state, 786 param_set_lid_init_state, param_get_lid_init_state, 787 NULL, 0644); 788 MODULE_PARM_DESC(lid_init_state, "Behavior for reporting LID initial state"); 789 790 static int __init acpi_button_init(void) 791 { 792 const struct dmi_system_id *dmi_id; 793 794 if (lid_init_state == -1) { 795 dmi_id = dmi_first_match(dmi_lid_quirks); 796 if (dmi_id) 797 lid_init_state = (long)dmi_id->driver_data; 798 else 799 lid_init_state = ACPI_BUTTON_LID_INIT_METHOD; 800 } 801 802 /* 803 * Modules such as nouveau.ko and i915.ko have a link time dependency 804 * on acpi_lid_open(), and would therefore not be loadable on ACPI 805 * capable kernels booted in non-ACPI mode if the return value of 806 * platform_driver_register() is returned from here with ACPI disabled 807 * when this driver is built as a module. 808 */ 809 if (acpi_disabled) 810 return 0; 811 812 return platform_driver_register(&acpi_button_driver); 813 } 814 815 static void __exit acpi_button_exit(void) 816 { 817 if (!acpi_disabled) 818 platform_driver_unregister(&acpi_button_driver); 819 } 820 821 module_init(acpi_button_init); 822 module_exit(acpi_button_exit); 823