dell-wmi-aio.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*
  2. * WMI hotkeys support for Dell All-In-One series
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/kernel.h>
  20. #include <linux/module.h>
  21. #include <linux/init.h>
  22. #include <linux/types.h>
  23. #include <linux/input.h>
  24. #include <linux/input/sparse-keymap.h>
  25. #include <linux/acpi.h>
  26. #include <linux/string.h>
  27. MODULE_DESCRIPTION("WMI hotkeys driver for Dell All-In-One series");
  28. MODULE_LICENSE("GPL");
  29. #define EVENT_GUID1 "284A0E6B-380E-472A-921F-E52786257FB4"
  30. #define EVENT_GUID2 "02314822-307C-4F66-BF0E-48AEAEB26CC8"
  31. struct dell_wmi_event {
  32. u16 length;
  33. /* 0x000: A hot key pressed or an event occurred
  34. * 0x00F: A sequence of hot keys are pressed */
  35. u16 type;
  36. u16 event[];
  37. };
  38. static const char *dell_wmi_aio_guids[] = {
  39. EVENT_GUID1,
  40. EVENT_GUID2,
  41. NULL
  42. };
  43. MODULE_ALIAS("wmi:"EVENT_GUID1);
  44. MODULE_ALIAS("wmi:"EVENT_GUID2);
  45. static const struct key_entry dell_wmi_aio_keymap[] = {
  46. { KE_KEY, 0xc0, { KEY_VOLUMEUP } },
  47. { KE_KEY, 0xc1, { KEY_VOLUMEDOWN } },
  48. { KE_KEY, 0xe030, { KEY_VOLUMEUP } },
  49. { KE_KEY, 0xe02e, { KEY_VOLUMEDOWN } },
  50. { KE_KEY, 0xe020, { KEY_MUTE } },
  51. { KE_KEY, 0xe027, { KEY_DISPLAYTOGGLE } },
  52. { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
  53. { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
  54. { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
  55. { KE_END, 0 }
  56. };
  57. static struct input_dev *dell_wmi_aio_input_dev;
  58. /*
  59. * The new WMI event data format will follow the dell_wmi_event structure
  60. * So, we will check if the buffer matches the format
  61. */
  62. static bool dell_wmi_aio_event_check(u8 *buffer, int length)
  63. {
  64. struct dell_wmi_event *event = (struct dell_wmi_event *)buffer;
  65. if (event == NULL || length < 6)
  66. return false;
  67. if ((event->type == 0 || event->type == 0xf) &&
  68. event->length >= 2)
  69. return true;
  70. return false;
  71. }
  72. static void dell_wmi_aio_notify(u32 value, void *context)
  73. {
  74. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  75. union acpi_object *obj;
  76. struct dell_wmi_event *event;
  77. acpi_status status;
  78. status = wmi_get_event_data(value, &response);
  79. if (status != AE_OK) {
  80. pr_info("bad event status 0x%x\n", status);
  81. return;
  82. }
  83. obj = (union acpi_object *)response.pointer;
  84. if (obj) {
  85. unsigned int scancode = 0;
  86. switch (obj->type) {
  87. case ACPI_TYPE_INTEGER:
  88. /* Most All-In-One correctly return integer scancode */
  89. scancode = obj->integer.value;
  90. sparse_keymap_report_event(dell_wmi_aio_input_dev,
  91. scancode, 1, true);
  92. break;
  93. case ACPI_TYPE_BUFFER:
  94. if (dell_wmi_aio_event_check(obj->buffer.pointer,
  95. obj->buffer.length)) {
  96. event = (struct dell_wmi_event *)
  97. obj->buffer.pointer;
  98. scancode = event->event[0];
  99. } else {
  100. /* Broken machines return the scancode in a
  101. buffer */
  102. if (obj->buffer.pointer &&
  103. obj->buffer.length > 0)
  104. scancode = obj->buffer.pointer[0];
  105. }
  106. if (scancode)
  107. sparse_keymap_report_event(
  108. dell_wmi_aio_input_dev,
  109. scancode, 1, true);
  110. break;
  111. }
  112. }
  113. kfree(obj);
  114. }
  115. static int __init dell_wmi_aio_input_setup(void)
  116. {
  117. int err;
  118. dell_wmi_aio_input_dev = input_allocate_device();
  119. if (!dell_wmi_aio_input_dev)
  120. return -ENOMEM;
  121. dell_wmi_aio_input_dev->name = "Dell AIO WMI hotkeys";
  122. dell_wmi_aio_input_dev->phys = "wmi/input0";
  123. dell_wmi_aio_input_dev->id.bustype = BUS_HOST;
  124. err = sparse_keymap_setup(dell_wmi_aio_input_dev,
  125. dell_wmi_aio_keymap, NULL);
  126. if (err) {
  127. pr_err("Unable to setup input device keymap\n");
  128. goto err_free_dev;
  129. }
  130. err = input_register_device(dell_wmi_aio_input_dev);
  131. if (err) {
  132. pr_info("Unable to register input device\n");
  133. goto err_free_keymap;
  134. }
  135. return 0;
  136. err_free_keymap:
  137. sparse_keymap_free(dell_wmi_aio_input_dev);
  138. err_free_dev:
  139. input_free_device(dell_wmi_aio_input_dev);
  140. return err;
  141. }
  142. static const char *dell_wmi_aio_find(void)
  143. {
  144. int i;
  145. for (i = 0; dell_wmi_aio_guids[i] != NULL; i++)
  146. if (wmi_has_guid(dell_wmi_aio_guids[i]))
  147. return dell_wmi_aio_guids[i];
  148. return NULL;
  149. }
  150. static int __init dell_wmi_aio_init(void)
  151. {
  152. int err;
  153. const char *guid;
  154. guid = dell_wmi_aio_find();
  155. if (!guid) {
  156. pr_warn("No known WMI GUID found\n");
  157. return -ENXIO;
  158. }
  159. err = dell_wmi_aio_input_setup();
  160. if (err)
  161. return err;
  162. err = wmi_install_notify_handler(guid, dell_wmi_aio_notify, NULL);
  163. if (err) {
  164. pr_err("Unable to register notify handler - %d\n", err);
  165. sparse_keymap_free(dell_wmi_aio_input_dev);
  166. input_unregister_device(dell_wmi_aio_input_dev);
  167. return err;
  168. }
  169. return 0;
  170. }
  171. static void __exit dell_wmi_aio_exit(void)
  172. {
  173. const char *guid;
  174. guid = dell_wmi_aio_find();
  175. wmi_remove_notify_handler(guid);
  176. sparse_keymap_free(dell_wmi_aio_input_dev);
  177. input_unregister_device(dell_wmi_aio_input_dev);
  178. }
  179. module_init(dell_wmi_aio_init);
  180. module_exit(dell_wmi_aio_exit);