/drivers/platform/x86/dell-wmi.c

https://bitbucket.org/slukk/jb-tsm-kernel-4.2 · C · 307 lines · 207 code · 55 blank · 45 comment · 32 complexity · c2078e543cc480436f83ee34bb3116c8 MD5 · raw file

  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 <acpi/acpi_drivers.h>
  34. #include <linux/acpi.h>
  35. #include <linux/string.h>
  36. #include <linux/dmi.h>
  37. MODULE_AUTHOR("Matthew Garrett <mjg@redhat.com>");
  38. MODULE_DESCRIPTION("Dell laptop WMI hotkeys driver");
  39. MODULE_LICENSE("GPL");
  40. #define DELL_EVENT_GUID "9DBB5994-A997-11DA-B012-B622A1EF5492"
  41. static int acpi_video;
  42. MODULE_ALIAS("wmi:"DELL_EVENT_GUID);
  43. /*
  44. * Certain keys are flagged as KE_IGNORE. All of these are either
  45. * notifications (rather than requests for change) or are also sent
  46. * via the keyboard controller so should not be sent again.
  47. */
  48. static const struct key_entry dell_wmi_legacy_keymap[] __initconst = {
  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. { KE_IGNORE, 0xe02e, { KEY_VOLUMEDOWN } },
  72. { KE_IGNORE, 0xe030, { KEY_VOLUMEUP } },
  73. { KE_IGNORE, 0xe033, { KEY_KBDILLUMUP } },
  74. { KE_IGNORE, 0xe034, { KEY_KBDILLUMDOWN } },
  75. { KE_IGNORE, 0xe03a, { KEY_CAPSLOCK } },
  76. { KE_IGNORE, 0xe045, { KEY_NUMLOCK } },
  77. { KE_IGNORE, 0xe046, { KEY_SCROLLLOCK } },
  78. { KE_END, 0 }
  79. };
  80. static bool dell_new_hk_type;
  81. struct dell_bios_keymap_entry {
  82. u16 scancode;
  83. u16 keycode;
  84. };
  85. struct dell_bios_hotkey_table {
  86. struct dmi_header header;
  87. struct dell_bios_keymap_entry keymap[];
  88. };
  89. static const struct dell_bios_hotkey_table *dell_bios_hotkey_table;
  90. static const u16 bios_to_linux_keycode[256] __initconst = {
  91. KEY_MEDIA, KEY_NEXTSONG, KEY_PLAYPAUSE, KEY_PREVIOUSSONG,
  92. KEY_STOPCD, KEY_UNKNOWN, KEY_UNKNOWN, KEY_UNKNOWN,
  93. KEY_WWW, KEY_UNKNOWN, KEY_VOLUMEDOWN, KEY_MUTE,
  94. KEY_VOLUMEUP, KEY_UNKNOWN, KEY_BATTERY, KEY_EJECTCD,
  95. KEY_UNKNOWN, KEY_SLEEP, KEY_PROG1, KEY_BRIGHTNESSDOWN,
  96. KEY_BRIGHTNESSUP, KEY_UNKNOWN, KEY_KBDILLUMTOGGLE,
  97. KEY_UNKNOWN, KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN,
  98. KEY_SWITCHVIDEOMODE, KEY_UNKNOWN, KEY_UNKNOWN, KEY_PROG2,
  99. KEY_UNKNOWN, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  100. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  101. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  102. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  103. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  104. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  105. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  106. 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
  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,
  109. KEY_PROG3
  110. };
  111. static struct input_dev *dell_wmi_input_dev;
  112. static void dell_wmi_notify(u32 value, void *context)
  113. {
  114. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  115. union acpi_object *obj;
  116. acpi_status status;
  117. status = wmi_get_event_data(value, &response);
  118. if (status != AE_OK) {
  119. pr_info("bad event status 0x%x\n", status);
  120. return;
  121. }
  122. obj = (union acpi_object *)response.pointer;
  123. if (obj && obj->type == ACPI_TYPE_BUFFER) {
  124. const struct key_entry *key;
  125. int reported_key;
  126. u16 *buffer_entry = (u16 *)obj->buffer.pointer;
  127. if (dell_new_hk_type && (buffer_entry[1] != 0x10)) {
  128. pr_info("Received unknown WMI event (0x%x)\n",
  129. buffer_entry[1]);
  130. kfree(obj);
  131. return;
  132. }
  133. if (dell_new_hk_type || buffer_entry[1] == 0x0)
  134. reported_key = (int)buffer_entry[2];
  135. else
  136. reported_key = (int)buffer_entry[1] & 0xffff;
  137. key = sparse_keymap_entry_from_scancode(dell_wmi_input_dev,
  138. reported_key);
  139. if (!key) {
  140. pr_info("Unknown key %x pressed\n", reported_key);
  141. } else if ((key->keycode == KEY_BRIGHTNESSUP ||
  142. key->keycode == KEY_BRIGHTNESSDOWN) && acpi_video) {
  143. /* Don't report brightness notifications that will also
  144. * come via ACPI */
  145. ;
  146. } else {
  147. sparse_keymap_report_entry(dell_wmi_input_dev, key,
  148. 1, true);
  149. }
  150. }
  151. kfree(obj);
  152. }
  153. static const struct key_entry * __init dell_wmi_prepare_new_keymap(void)
  154. {
  155. int hotkey_num = (dell_bios_hotkey_table->header.length - 4) /
  156. sizeof(struct dell_bios_keymap_entry);
  157. struct key_entry *keymap;
  158. int i;
  159. keymap = kcalloc(hotkey_num + 1, sizeof(struct key_entry), GFP_KERNEL);
  160. if (!keymap)
  161. return NULL;
  162. for (i = 0; i < hotkey_num; i++) {
  163. const struct dell_bios_keymap_entry *bios_entry =
  164. &dell_bios_hotkey_table->keymap[i];
  165. keymap[i].type = KE_KEY;
  166. keymap[i].code = bios_entry->scancode;
  167. keymap[i].keycode = bios_entry->keycode < 256 ?
  168. bios_to_linux_keycode[bios_entry->keycode] :
  169. KEY_RESERVED;
  170. }
  171. keymap[hotkey_num].type = KE_END;
  172. return keymap;
  173. }
  174. static int __init dell_wmi_input_setup(void)
  175. {
  176. int err;
  177. dell_wmi_input_dev = input_allocate_device();
  178. if (!dell_wmi_input_dev)
  179. return -ENOMEM;
  180. dell_wmi_input_dev->name = "Dell WMI hotkeys";
  181. dell_wmi_input_dev->phys = "wmi/input0";
  182. dell_wmi_input_dev->id.bustype = BUS_HOST;
  183. if (dell_new_hk_type) {
  184. const struct key_entry *keymap = dell_wmi_prepare_new_keymap();
  185. if (!keymap) {
  186. err = -ENOMEM;
  187. goto err_free_dev;
  188. }
  189. err = sparse_keymap_setup(dell_wmi_input_dev, keymap, NULL);
  190. /*
  191. * Sparse keymap library makes a copy of keymap so we
  192. * don't need the original one that was allocated.
  193. */
  194. kfree(keymap);
  195. } else {
  196. err = sparse_keymap_setup(dell_wmi_input_dev,
  197. dell_wmi_legacy_keymap, NULL);
  198. }
  199. if (err)
  200. goto err_free_dev;
  201. err = input_register_device(dell_wmi_input_dev);
  202. if (err)
  203. goto err_free_keymap;
  204. return 0;
  205. err_free_keymap:
  206. sparse_keymap_free(dell_wmi_input_dev);
  207. err_free_dev:
  208. input_free_device(dell_wmi_input_dev);
  209. return err;
  210. }
  211. static void dell_wmi_input_destroy(void)
  212. {
  213. sparse_keymap_free(dell_wmi_input_dev);
  214. input_unregister_device(dell_wmi_input_dev);
  215. }
  216. static void __init find_hk_type(const struct dmi_header *dm, void *dummy)
  217. {
  218. if (dm->type == 0xb2 && dm->length > 6) {
  219. dell_new_hk_type = true;
  220. dell_bios_hotkey_table =
  221. container_of(dm, struct dell_bios_hotkey_table, header);
  222. }
  223. }
  224. static int __init dell_wmi_init(void)
  225. {
  226. int err;
  227. acpi_status status;
  228. if (!wmi_has_guid(DELL_EVENT_GUID)) {
  229. pr_warn("No known WMI GUID found\n");
  230. return -ENODEV;
  231. }
  232. dmi_walk(find_hk_type, NULL);
  233. acpi_video = acpi_video_backlight_support();
  234. err = dell_wmi_input_setup();
  235. if (err)
  236. return err;
  237. status = wmi_install_notify_handler(DELL_EVENT_GUID,
  238. dell_wmi_notify, NULL);
  239. if (ACPI_FAILURE(status)) {
  240. dell_wmi_input_destroy();
  241. pr_err("Unable to register notify handler - %d\n", status);
  242. return -ENODEV;
  243. }
  244. return 0;
  245. }
  246. module_init(dell_wmi_init);
  247. static void __exit dell_wmi_exit(void)
  248. {
  249. wmi_remove_notify_handler(DELL_EVENT_GUID);
  250. dell_wmi_input_destroy();
  251. }
  252. module_exit(dell_wmi_exit);