peaq-wmi.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * PEAQ 2-in-1 WMI hotkey driver
  3. * Copyright (C) 2017 Hans de Goede <hdegoede@redhat.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License version 2 as
  7. * published by the Free Software Foundation.
  8. */
  9. #include <linux/acpi.h>
  10. #include <linux/dmi.h>
  11. #include <linux/input-polldev.h>
  12. #include <linux/kernel.h>
  13. #include <linux/module.h>
  14. #define PEAQ_DOLBY_BUTTON_GUID "ABBC0F6F-8EA1-11D1-00A0-C90629100000"
  15. #define PEAQ_DOLBY_BUTTON_METHOD_ID 5
  16. #define PEAQ_POLL_INTERVAL_MS 250
  17. #define PEAQ_POLL_IGNORE_MS 500
  18. #define PEAQ_POLL_MAX_MS 1000
  19. MODULE_ALIAS("wmi:"PEAQ_DOLBY_BUTTON_GUID);
  20. static unsigned int peaq_ignore_events_counter;
  21. static struct input_polled_dev *peaq_poll_dev;
  22. /*
  23. * The Dolby button (yes really a Dolby button) causes an ACPI variable to get
  24. * set on both press and release. The WMI method checks and clears that flag.
  25. * So for a press + release we will get back One from the WMI method either once
  26. * (if polling after the release) or twice (polling between press and release).
  27. * We ignore events for 0.5s after the first event to avoid reporting 2 presses.
  28. */
  29. static void peaq_wmi_poll(struct input_polled_dev *dev)
  30. {
  31. union acpi_object obj;
  32. acpi_status status;
  33. u32 dummy = 0;
  34. struct acpi_buffer input = { sizeof(dummy), &dummy };
  35. struct acpi_buffer output = { sizeof(obj), &obj };
  36. status = wmi_evaluate_method(PEAQ_DOLBY_BUTTON_GUID, 0,
  37. PEAQ_DOLBY_BUTTON_METHOD_ID,
  38. &input, &output);
  39. if (ACPI_FAILURE(status))
  40. return;
  41. if (obj.type != ACPI_TYPE_INTEGER) {
  42. dev_err(&peaq_poll_dev->input->dev,
  43. "Error WMBC did not return an integer\n");
  44. return;
  45. }
  46. if (peaq_ignore_events_counter && peaq_ignore_events_counter--)
  47. return;
  48. if (obj.integer.value) {
  49. input_event(peaq_poll_dev->input, EV_KEY, KEY_SOUND, 1);
  50. input_sync(peaq_poll_dev->input);
  51. input_event(peaq_poll_dev->input, EV_KEY, KEY_SOUND, 0);
  52. input_sync(peaq_poll_dev->input);
  53. peaq_ignore_events_counter = max(1u,
  54. PEAQ_POLL_IGNORE_MS / peaq_poll_dev->poll_interval);
  55. }
  56. }
  57. /* Some other devices (Shuttle XS35) use the same WMI GUID for other purposes */
  58. static const struct dmi_system_id peaq_dmi_table[] __initconst = {
  59. {
  60. .matches = {
  61. DMI_MATCH(DMI_SYS_VENDOR, "PEAQ"),
  62. DMI_MATCH(DMI_PRODUCT_NAME, "PEAQ PMM C1010 MD99187"),
  63. },
  64. },
  65. {}
  66. };
  67. static int __init peaq_wmi_init(void)
  68. {
  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_polled_device();
  75. if (!peaq_poll_dev)
  76. return -ENOMEM;
  77. peaq_poll_dev->poll = peaq_wmi_poll;
  78. peaq_poll_dev->poll_interval = PEAQ_POLL_INTERVAL_MS;
  79. peaq_poll_dev->poll_interval_max = PEAQ_POLL_MAX_MS;
  80. peaq_poll_dev->input->name = "PEAQ WMI hotkeys";
  81. peaq_poll_dev->input->phys = "wmi/input0";
  82. peaq_poll_dev->input->id.bustype = BUS_HOST;
  83. input_set_capability(peaq_poll_dev->input, EV_KEY, KEY_SOUND);
  84. return input_register_polled_device(peaq_poll_dev);
  85. }
  86. static void __exit peaq_wmi_exit(void)
  87. {
  88. input_unregister_polled_device(peaq_poll_dev);
  89. }
  90. module_init(peaq_wmi_init);
  91. module_exit(peaq_wmi_exit);
  92. MODULE_DESCRIPTION("PEAQ 2-in-1 WMI hotkey driver");
  93. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  94. MODULE_LICENSE("GPL");