wireless-hotkey.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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. #include <linux/kernel.h>
  9. #include <linux/module.h>
  10. #include <linux/init.h>
  11. #include <linux/input.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/acpi.h>
  14. #include <acpi/acpi_bus.h>
  15. MODULE_DESCRIPTION("Airplane mode button for AMD, HP & Xiaomi laptops");
  16. MODULE_LICENSE("GPL");
  17. MODULE_AUTHOR("Alex Hung");
  18. MODULE_ALIAS("acpi*:HPQ6001:*");
  19. MODULE_ALIAS("acpi*:WSTADEF:*");
  20. MODULE_ALIAS("acpi*:AMDI0051:*");
  21. MODULE_ALIAS("acpi*:LGEX0815:*");
  22. struct wl_button {
  23. struct input_dev *input_dev;
  24. char phys[32];
  25. };
  26. static const struct acpi_device_id wl_ids[] = {
  27. {"HPQ6001", 0},
  28. {"WSTADEF", 0},
  29. {"AMDI0051", 0},
  30. {"LGEX0815", 0},
  31. {"", 0},
  32. };
  33. static int wireless_input_setup(struct acpi_device *device)
  34. {
  35. struct wl_button *button = acpi_driver_data(device);
  36. int err;
  37. button->input_dev = input_allocate_device();
  38. if (!button->input_dev)
  39. return -ENOMEM;
  40. snprintf(button->phys, sizeof(button->phys), "%s/input0", acpi_device_hid(device));
  41. button->input_dev->name = "Wireless hotkeys";
  42. button->input_dev->phys = button->phys;
  43. button->input_dev->id.bustype = BUS_HOST;
  44. button->input_dev->evbit[0] = BIT(EV_KEY);
  45. set_bit(KEY_RFKILL, button->input_dev->keybit);
  46. err = input_register_device(button->input_dev);
  47. if (err)
  48. goto err_free_dev;
  49. return 0;
  50. err_free_dev:
  51. input_free_device(button->input_dev);
  52. return err;
  53. }
  54. static void wireless_input_destroy(struct acpi_device *device)
  55. {
  56. struct wl_button *button = acpi_driver_data(device);
  57. input_unregister_device(button->input_dev);
  58. kfree(button);
  59. }
  60. static void wl_notify(struct acpi_device *acpi_dev, u32 event)
  61. {
  62. struct wl_button *button = acpi_driver_data(acpi_dev);
  63. if (event != 0x80) {
  64. pr_info("Received unknown event (0x%x)\n", event);
  65. return;
  66. }
  67. input_report_key(button->input_dev, KEY_RFKILL, 1);
  68. input_sync(button->input_dev);
  69. input_report_key(button->input_dev, KEY_RFKILL, 0);
  70. input_sync(button->input_dev);
  71. }
  72. static int wl_add(struct acpi_device *device)
  73. {
  74. struct wl_button *button;
  75. int err;
  76. button = kzalloc_obj(struct wl_button);
  77. if (!button)
  78. return -ENOMEM;
  79. device->driver_data = button;
  80. err = wireless_input_setup(device);
  81. if (err) {
  82. pr_err("Failed to setup wireless hotkeys\n");
  83. kfree(button);
  84. }
  85. return err;
  86. }
  87. static void wl_remove(struct acpi_device *device)
  88. {
  89. wireless_input_destroy(device);
  90. }
  91. static struct acpi_driver wl_driver = {
  92. .name = "wireless-hotkey",
  93. .ids = wl_ids,
  94. .ops = {
  95. .add = wl_add,
  96. .remove = wl_remove,
  97. .notify = wl_notify,
  98. },
  99. };
  100. module_acpi_driver(wl_driver);