peaq-wmi.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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/input-polldev.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #define PEAQ_DOLBY_BUTTON_GUID "ABBC0F6F-8EA1-11D1-00A0-C90629100000"
  14. #define PEAQ_DOLBY_BUTTON_METHOD_ID 5
  15. #define PEAQ_POLL_INTERVAL_MS 250
  16. #define PEAQ_POLL_IGNORE_MS 500
  17. #define PEAQ_POLL_MAX_MS 1000
  18. MODULE_ALIAS("wmi:"PEAQ_DOLBY_BUTTON_GUID);
  19. static unsigned int peaq_ignore_events_counter;
  20. static struct input_polled_dev *peaq_poll_dev;
  21. /*
  22. * The Dolby button (yes really a Dolby button) causes an ACPI variable to get
  23. * set on both press and release. The WMI method checks and clears that flag.
  24. * So for a press + release we will get back One from the WMI method either once
  25. * (if polling after the release) or twice (polling between press and release).
  26. * We ignore events for 0.5s after the first event to avoid reporting 2 presses.
  27. */
  28. static void peaq_wmi_poll(struct input_polled_dev *dev)
  29. {
  30. union acpi_object obj;
  31. acpi_status status;
  32. u32 dummy = 0;
  33. struct acpi_buffer input = { sizeof(dummy), &dummy };
  34. struct acpi_buffer output = { sizeof(obj), &obj };
  35. status = wmi_evaluate_method(PEAQ_DOLBY_BUTTON_GUID, 0,
  36. PEAQ_DOLBY_BUTTON_METHOD_ID,
  37. &input, &output);
  38. if (ACPI_FAILURE(status))
  39. return;
  40. if (obj.type != ACPI_TYPE_INTEGER) {
  41. dev_err(&peaq_poll_dev->input->dev,
  42. "Error WMBC did not return an integer\n");
  43. return;
  44. }
  45. if (peaq_ignore_events_counter && peaq_ignore_events_counter--)
  46. return;
  47. if (obj.integer.value) {
  48. input_event(peaq_poll_dev->input, EV_KEY, KEY_SOUND, 1);
  49. input_sync(peaq_poll_dev->input);
  50. input_event(peaq_poll_dev->input, EV_KEY, KEY_SOUND, 0);
  51. input_sync(peaq_poll_dev->input);
  52. peaq_ignore_events_counter = max(1u,
  53. PEAQ_POLL_IGNORE_MS / peaq_poll_dev->poll_interval);
  54. }
  55. }
  56. static int __init peaq_wmi_init(void)
  57. {
  58. if (!wmi_has_guid(PEAQ_DOLBY_BUTTON_GUID))
  59. return -ENODEV;
  60. peaq_poll_dev = input_allocate_polled_device();
  61. if (!peaq_poll_dev)
  62. return -ENOMEM;
  63. peaq_poll_dev->poll = peaq_wmi_poll;
  64. peaq_poll_dev->poll_interval = PEAQ_POLL_INTERVAL_MS;
  65. peaq_poll_dev->poll_interval_max = PEAQ_POLL_MAX_MS;
  66. peaq_poll_dev->input->name = "PEAQ WMI hotkeys";
  67. peaq_poll_dev->input->phys = "wmi/input0";
  68. peaq_poll_dev->input->id.bustype = BUS_HOST;
  69. input_set_capability(peaq_poll_dev->input, EV_KEY, KEY_SOUND);
  70. return input_register_polled_device(peaq_poll_dev);
  71. }
  72. static void __exit peaq_wmi_exit(void)
  73. {
  74. if (!wmi_has_guid(PEAQ_DOLBY_BUTTON_GUID))
  75. return;
  76. input_unregister_polled_device(peaq_poll_dev);
  77. }
  78. module_init(peaq_wmi_init);
  79. module_exit(peaq_wmi_exit);
  80. MODULE_DESCRIPTION("PEAQ 2-in-1 WMI hotkey driver");
  81. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  82. MODULE_LICENSE("GPL");