peaq-wmi.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * PEAQ 2-in-1 WMI hotkey driver
  4. * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
  5. */
  6. #include <linux/acpi.h>
  7. #include <linux/dmi.h>
  8. #include <linux/input.h>
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #define PEAQ_DOLBY_BUTTON_GUID "ABBC0F6F-8EA1-11D1-00A0-C90629100000"
  12. #define PEAQ_DOLBY_BUTTON_METHOD_ID 5
  13. #define PEAQ_POLL_INTERVAL_MS 250
  14. #define PEAQ_POLL_IGNORE_MS 500
  15. #define PEAQ_POLL_MAX_MS 1000
  16. MODULE_ALIAS("wmi:"PEAQ_DOLBY_BUTTON_GUID);
  17. static struct input_dev *peaq_poll_dev;
  18. /*
  19. * The Dolby button (yes really a Dolby button) causes an ACPI variable to get
  20. * set on both press and release. The WMI method checks and clears that flag.
  21. * So for a press + release we will get back One from the WMI method either once
  22. * (if polling after the release) or twice (polling between press and release).
  23. * We ignore events for 0.5s after the first event to avoid reporting 2 presses.
  24. */
  25. static void peaq_wmi_poll(struct input_dev *input_dev)
  26. {
  27. static unsigned long last_event_time;
  28. static bool had_events;
  29. union acpi_object obj;
  30. acpi_status status;
  31. u32 dummy = 0;
  32. struct acpi_buffer input = { sizeof(dummy), &dummy };
  33. struct acpi_buffer output = { sizeof(obj), &obj };
  34. status = wmi_evaluate_method(PEAQ_DOLBY_BUTTON_GUID, 0,
  35. PEAQ_DOLBY_BUTTON_METHOD_ID,
  36. &input, &output);
  37. if (ACPI_FAILURE(status))
  38. return;
  39. if (obj.type != ACPI_TYPE_INTEGER) {
  40. dev_err(&input_dev->dev,
  41. "Error WMBC did not return an integer\n");
  42. return;
  43. }
  44. if (!obj.integer.value)
  45. return;
  46. if (had_events && time_before(jiffies, last_event_time +
  47. msecs_to_jiffies(PEAQ_POLL_IGNORE_MS)))
  48. return;
  49. input_event(input_dev, EV_KEY, KEY_SOUND, 1);
  50. input_sync(input_dev);
  51. input_event(input_dev, EV_KEY, KEY_SOUND, 0);
  52. input_sync(input_dev);
  53. last_event_time = jiffies;
  54. had_events = true;
  55. }
  56. /* Some other devices (Shuttle XS35) use the same WMI GUID for other purposes */
  57. static const struct dmi_system_id peaq_dmi_table[] __initconst = {
  58. {
  59. .matches = {
  60. DMI_MATCH(DMI_SYS_VENDOR, "PEAQ"),
  61. DMI_MATCH(DMI_PRODUCT_NAME, "PEAQ PMM C1010 MD99187"),
  62. },
  63. },
  64. {}
  65. };
  66. static int __init peaq_wmi_init(void)
  67. {
  68. int err;
  69. /* WMI GUID is not unique, also check for a DMI match */
  70. if (!dmi_check_system(peaq_dmi_table))
  71. return -ENODEV;
  72. if (!wmi_has_guid(PEAQ_DOLBY_BUTTON_GUID))
  73. return -ENODEV;
  74. peaq_poll_dev = input_allocate_device();
  75. if (!peaq_poll_dev)
  76. return -ENOMEM;
  77. peaq_poll_dev->name = "PEAQ WMI hotkeys";
  78. peaq_poll_dev->phys = "wmi/input0";
  79. peaq_poll_dev->id.bustype = BUS_HOST;
  80. input_set_capability(peaq_poll_dev, EV_KEY, KEY_SOUND);
  81. err = input_setup_polling(peaq_poll_dev, peaq_wmi_poll);
  82. if (err)
  83. goto err_out;
  84. input_set_poll_interval(peaq_poll_dev, PEAQ_POLL_INTERVAL_MS);
  85. input_set_max_poll_interval(peaq_poll_dev, PEAQ_POLL_MAX_MS);
  86. err = input_register_device(peaq_poll_dev);
  87. if (err)
  88. goto err_out;
  89. return 0;
  90. err_out:
  91. input_free_device(peaq_poll_dev);
  92. return err;
  93. }
  94. static void __exit peaq_wmi_exit(void)
  95. {
  96. input_unregister_device(peaq_poll_dev);
  97. }
  98. module_init(peaq_wmi_init);
  99. module_exit(peaq_wmi_exit);
  100. MODULE_DESCRIPTION("PEAQ 2-in-1 WMI hotkey driver");
  101. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  102. MODULE_LICENSE("GPL");