dell-wmi.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /*
  2. * Dell WMI hotkeys
  3. *
  4. * Copyright (C) 2008 Red Hat <mjg@redhat.com>
  5. *
  6. * Portions based on wistron_btns.c:
  7. * Copyright (C) 2005 Miloslav Trmac <mitr@volny.cz>
  8. * Copyright (C) 2005 Bernhard Rosenkraenzer <bero@arklinux.org>
  9. * Copyright (C) 2005 Dmitry Torokhov <dtor@mail.ru>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program; if not, write to the Free Software
  23. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  24. */
  25. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/init.h>
  29. #include <linux/slab.h>
  30. #include <linux/types.h>
  31. #include <linux/input.h>
  32. #include <linux/input/sparse-keymap.h>
  33. #include <linux/acpi.h>
  34. #include <linux/string.h>
  35. #include <linux/dmi.h>
  36. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  37. MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
  38. MODULE_LICENSE("GPL");
  39. #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
  40. static int acpi_video;
  41. MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
  42. /*
  43. * Certain keys are flagged as KE_IGNORE. All of these are either
  44. * notifications (rather than requests for change) or are also sent
  45. * via the keyboard controller so should not be sent again.
  46. */
  47. static const struct key_entry dell_wmi_legacy_keymap[] __initconst = {
  48. { KE_IGNORE, 0x003a, { KEY_CAPSLOCK } },
  49. { KE_KEY, 0xe045, { KEY_PROG1 } },
  50. { KE_KEY, 0xe009, { KEY_EJECTCD } },
  51. /* These also contain the brightness level at offset 6 */
  52. { KE_KEY, 0xe006, { KEY_BRIGHTNESSUP } },
  53. { KE_KEY, 0xe005, { KEY_BRIGHTNESSDOWN } },
  54. /* Battery health status button */
  55. { KE_KEY, 0xe007, { KEY_BATTERY } },
  56. /* This is actually for all radios. Although physically a
  57. * switch, the notification does not provide an indication of
  58. * state and so it should be reported as a key */
  59. { KE_KEY, 0xe008, { KEY_WLAN } },
  60. /* The next device is at offset 6, the active devices are at
  61. offset 8 and the attached devices at offset 10 */
  62. { KE_KEY, 0xe00b, { KEY_SWITCHVIDEOMODE } },
  63. { KE_IGNORE, 0xe00c, { KEY_KBDILLUMTOGGLE } },
  64. /* BIOS error detected */
  65. { KE_IGNORE, 0xe00d, { KEY_RESERVED } },
  66. /* Wifi Catcher */
  67. { KE_KEY, 0xe011, {KEY_PROG2 } },
  68. /* Ambient light sensor toggle */
  69. { KE_IGNORE, 0xe013, { KEY_RESERVED } },
  70. { KE_IGNORE, 0xe020, { KEY_MUTE } },
  71. /* Shortcut and audio panel keys */
  72. { KE_IGNORE, 0xe025, { KEY_RESERVED } },
  73. { KE_IGNORE, 0xe026, { KEY_RESERVED } },
  74. { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
  75. { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
  76. { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
  77. { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
  78. { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
  79. { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
  80. { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
  81. { KE_IGNORE, 0xe0f7, { KEY_MUTE } },
  82. { KE_IGNORE, 0xe0f8, { KEY_VOLUMEDOWN } },
  83. { KE_IGNORE, 0xe0f9, { KEY_VOLUMEUP } },
  84. { KE_END, 0 }
  85. };
  86. static bool dell_new_hk_type;
  87. struct dell_bios_keymap_entry {
  88. u16 scancode;
  89. u16 keycode;
  90. };
  91. struct dell_bios_hotkey_table {
  92. struct dmi_header header;
  93. struct dell_bios_keymap_entry keymap[];
  94. };
  95. static const struct dell_bios_hotkey_table *dell_bios_hotkey_table;
  96. static const u16 bios_to_linux_keycode[256] __initconst = {
  97. KEY_MEDIA, KEY_NEXTSONG, KEY_PLAYPAUSE, KEY_PREVIOUSSONG,
  98. KEY_STOPCD, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  99. KEY_WWW, KEY_UNKNOWN, KEY_VOLUMEDOWN, KEY_MUTE,
  100. KEY_VOLUMEUP, KEY_UNKNOWN, KEY_BATTERY, KEY_EJECTCD,
  101. KEY_UNKNOWN, KEY_SLEEP, KEY_PROG1, KEY_BRIGHTNESSDOWN,
  102. KEY_BRIGHTNESSUP, KEY_UNKNOWN, KEY_KBDILLUMTOGGLE,
  103. KEY_UNKNOWN, KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN,
  104. KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN, KEY_PROG2,
  105. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  106. KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN, KEY_MICMUTE,
  107. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  108. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  109. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  110. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  111. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  112. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  113. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  114. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  115. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  116. 0, 0, 0, 0, 0, 0, 0, 0, 0, KEY_PROG3
  117. };
  118. static struct input_dev *dell_wmi_input_dev;
  119. static void dell_wmi_notify(u32 value, void *context)
  120. {
  121. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  122. union acpi_object *obj;
  123. acpi_status status;
  124. status = wmi_get_event_data(value, &response);
  125. if (status != AE_OK) {
  126. pr_info("bad event status 0x%x\n", status);
  127. return;
  128. }
  129. obj = (union acpi_object *)response.pointer;
  130. if (obj && obj->type == ACPI_TYPE_BUFFER) {
  131. const struct key_entry *key;
  132. int reported_key;
  133. u16 *buffer_entry = (u16 *)obj->buffer.pointer;
  134. int buffer_size = obj->buffer.length/2;
  135. if (buffer_size >= 2 && dell_new_hk_type && buffer_entry[1] != 0x10) {
  136. pr_info("Received unknown WMI event (0x%x)\n",
  137. buffer_entry[1]);
  138. kfree(obj);
  139. return;
  140. }
  141. if (buffer_size >= 3 && (dell_new_hk_type || buffer_entry[1] == 0x0))
  142. reported_key = (int)buffer_entry[2];
  143. else if (buffer_size >= 2)
  144. reported_key = (int)buffer_entry[1] & 0xffff;
  145. else {
  146. pr_info("Received unknown WMI event\n");
  147. kfree(obj);
  148. return;
  149. }
  150. key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
  151. reported_key);
  152. if (!key) {
  153. pr_info("Unknown key %x pressed\n", reported_key);
  154. } else if ((key->keycode == KEY_BRIGHTNESSUP ||
  155. key->keycode == KEY_BRIGHTNESSDOWN) && acpi_video) {
  156. /* Don't report brightness notifications that will also
  157. * come via ACPI */
  158. ;
  159. } else {
  160. sparse_keymap_report_entry(dell_wmi_input_dev, key,
  161. 1, true);
  162. }
  163. }
  164. kfree(obj);
  165. }
  166. static const struct key_entry * __init dell_wmi_prepare_new_keymap(void)
  167. {
  168. int hotkey_num = (dell_bios_hotkey_table->header.length - 4) /
  169. sizeof(struct dell_bios_keymap_entry);
  170. struct key_entry *keymap;
  171. int i;
  172. keymap = kcalloc(hotkey_num + 1, sizeof(struct key_entry), GFP_KERNEL);
  173. if (!keymap)
  174. return NULL;
  175. for (i = 0; i < hotkey_num; i++) {
  176. const struct dell_bios_keymap_entry *bios_entry =
  177. &dell_bios_hotkey_table->keymap[i];
  178. keymap[i].type = KE_KEY;
  179. keymap[i].code = bios_entry->scancode;
  180. keymap[i].keycode = bios_entry->keycode < 256 ?
  181. bios_to_linux_keycode[bios_entry->keycode] :
  182. KEY_RESERVED;
  183. }
  184. keymap[hotkey_num].type = KE_END;
  185. return keymap;
  186. }
  187. static int __init dell_wmi_input_setup(void)
  188. {
  189. int err;
  190. dell_wmi_input_dev = input_allocate_device();
  191. if (!dell_wmi_input_dev)
  192. return -ENOMEM;
  193. dell_wmi_input_dev->name = "Dell WMI hotkeys";
  194. dell_wmi_input_dev->phys = "wmi/input0";
  195. dell_wmi_input_dev->id.bustype = BUS_HOST;
  196. if (dell_new_hk_type) {
  197. const struct key_entry *keymap = dell_wmi_prepare_new_keymap();
  198. if (!keymap) {
  199. err = -ENOMEM;
  200. goto err_free_dev;
  201. }
  202. err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
  203. /*
  204. * Sparse keymap library makes a copy of keymap so we
  205. * don't need the original one that was allocated.
  206. */
  207. kfree(keymap);
  208. } else {
  209. err = sparse_keymap_setup(dell_wmi_input_dev,
  210. dell_wmi_legacy_keymap, NULL);
  211. }
  212. if (err)
  213. goto err_free_dev;
  214. err = input_register_device(dell_wmi_input_dev);
  215. if (err)
  216. goto err_free_keymap;
  217. return 0;
  218. err_free_keymap:
  219. sparse_keymap_free(dell_wmi_input_dev);
  220. err_free_dev:
  221. input_free_device(dell_wmi_input_dev);
  222. return err;
  223. }
  224. static void dell_wmi_input_destroy(void)
  225. {
  226. sparse_keymap_free(dell_wmi_input_dev);
  227. input_unregister_device(dell_wmi_input_dev);
  228. }
  229. static void __init find_hk_type(const struct dmi_header *dm, void *dummy)
  230. {
  231. if (dm->type == 0xb2 && dm->length > 6) {
  232. dell_new_hk_type = true;
  233. dell_bios_hotkey_table =
  234. container_of(dm, struct dell_bios_hotkey_table, header);
  235. }
  236. }
  237. static int __init dell_wmi_init(void)
  238. {
  239. int err;
  240. acpi_status status;
  241. if (!wmi_has_guid(DELL_EVENT_GUID)) {
  242. pr_warn("No known WMI GUID found\n");
  243. return -ENODEV;
  244. }
  245. dmi_walk(find_hk_type, NULL);
  246. acpi_video = acpi_video_backlight_support();
  247. err = dell_wmi_input_setup();
  248. if (err)
  249. return err;
  250. status = wmi_install_notify_handler(DELL_EVENT_GUID,
  251. dell_wmi_notify, NULL);
  252. if (ACPI_FAILURE(status)) {
  253. dell_wmi_input_destroy();
  254. pr_err("Unable to register notify handler - %d\n", status);
  255. return -ENODEV;
  256. }
  257. return 0;
  258. }
  259. module_init(dell_wmi_init);
  260. static void __exit dell_wmi_exit(void)
  261. {
  262. wmi_remove_notify_handler(DELL_EVENT_GUID);
  263. dell_wmi_input_destroy();
  264. }
  265. module_exit(dell_wmi_exit);