1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Airplane mode button for AMD, HP & Xiaomi laptops 4 * 5 * Copyright (C) 2014-2017 Alex Hung <alex.hung@canonical.com> 6 * Copyright (C) 2021 Advanced Micro Devices 7 */ 8 9 #include <linux/kernel.h> 10 #include <linux/module.h> 11 #include <linux/init.h> 12 #include <linux/input.h> 13 #include <linux/platform_device.h> 14 #include <linux/acpi.h> 15 #include <acpi/acpi_bus.h> 16 17 MODULE_DESCRIPTION("Airplane mode button for AMD, HP & Xiaomi laptops"); 18 MODULE_LICENSE("GPL"); 19 MODULE_AUTHOR("Alex Hung"); 20 MODULE_ALIAS("acpi*:HPQ6001:*"); 21 MODULE_ALIAS("acpi*:WSTADEF:*"); 22 MODULE_ALIAS("acpi*:AMDI0051:*"); 23 MODULE_ALIAS("acpi*:LGEX0815:*"); 24 25 struct wl_button { 26 struct input_dev *input_dev; 27 char phys[32]; 28 }; 29 30 static const struct acpi_device_id wl_ids[] = { 31 {"HPQ6001", 0}, 32 {"WSTADEF", 0}, 33 {"AMDI0051", 0}, 34 {"LGEX0815", 0}, 35 {"", 0}, 36 }; 37 38 static int wireless_input_setup(struct device *dev) 39 { 40 struct wl_button *button = dev_get_drvdata(dev); 41 int err; 42 43 button->input_dev = input_allocate_device(); 44 if (!button->input_dev) 45 return -ENOMEM; 46 47 snprintf(button->phys, sizeof(button->phys), "%s/input0", 48 acpi_device_hid(ACPI_COMPANION(dev))); 49 50 button->input_dev->name = "Wireless hotkeys"; 51 button->input_dev->phys = button->phys; 52 button->input_dev->id.bustype = BUS_HOST; 53 button->input_dev->evbit[0] = BIT(EV_KEY); 54 set_bit(KEY_RFKILL, button->input_dev->keybit); 55 56 err = input_register_device(button->input_dev); 57 if (err) 58 goto err_free_dev; 59 60 return 0; 61 62 err_free_dev: 63 input_free_device(button->input_dev); 64 return err; 65 } 66 67 static void wireless_input_destroy(struct device *dev) 68 { 69 struct wl_button *button = dev_get_drvdata(dev); 70 71 input_unregister_device(button->input_dev); 72 kfree(button); 73 } 74 75 static void wl_notify(acpi_handle handle, u32 event, void *data) 76 { 77 struct wl_button *button = data; 78 79 if (event != 0x80) { 80 pr_info("Received unknown event (0x%x)\n", event); 81 return; 82 } 83 84 input_report_key(button->input_dev, KEY_RFKILL, 1); 85 input_sync(button->input_dev); 86 input_report_key(button->input_dev, KEY_RFKILL, 0); 87 input_sync(button->input_dev); 88 } 89 90 static int wl_probe(struct platform_device *pdev) 91 { 92 struct wl_button *button; 93 int err; 94 95 button = kzalloc_obj(struct wl_button); 96 if (!button) 97 return -ENOMEM; 98 99 platform_set_drvdata(pdev, button); 100 101 err = wireless_input_setup(&pdev->dev); 102 if (err) { 103 pr_err("Failed to setup wireless hotkeys\n"); 104 kfree(button); 105 return err; 106 } 107 err = acpi_dev_install_notify_handler(ACPI_COMPANION(&pdev->dev), 108 ACPI_DEVICE_NOTIFY, wl_notify, button); 109 if (err) { 110 pr_err("Failed to install ACPI notify handler\n"); 111 wireless_input_destroy(&pdev->dev); 112 } 113 114 return err; 115 } 116 117 static void wl_remove(struct platform_device *pdev) 118 { 119 acpi_dev_remove_notify_handler(ACPI_COMPANION(&pdev->dev), 120 ACPI_DEVICE_NOTIFY, wl_notify); 121 wireless_input_destroy(&pdev->dev); 122 } 123 124 static struct platform_driver wl_driver = { 125 .probe = wl_probe, 126 .remove = wl_remove, 127 .driver = { 128 .name = "wireless-hotkey", 129 .acpi_match_table = wl_ids, 130 }, 131 }; 132 133 module_platform_driver(wl_driver); 134