msi-wmi.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * MSI WMI hotkeys
  4. *
  5. * Copyright (C) 2009 Novell <[email protected]>
  6. *
  7. * Most stuff taken over from hp-wmi
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/kernel.h>
  11. #include <linux/input.h>
  12. #include <linux/input/sparse-keymap.h>
  13. #include <linux/acpi.h>
  14. #include <linux/backlight.h>
  15. #include <linux/slab.h>
  16. #include <linux/module.h>
  17. #include <acpi/video.h>
  18. MODULE_AUTHOR("Thomas Renninger <[email protected]>");
  19. MODULE_DESCRIPTION("MSI laptop WMI hotkeys driver");
  20. MODULE_LICENSE("GPL");
  21. #define DRV_NAME "msi-wmi"
  22. #define MSIWMI_BIOS_GUID "551A1F84-FBDD-4125-91DB-3EA8F44F1D45"
  23. #define MSIWMI_MSI_EVENT_GUID "B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2"
  24. #define MSIWMI_WIND_EVENT_GUID "5B3CC38A-40D9-7245-8AE6-1145B751BE3F"
  25. MODULE_ALIAS("wmi:" MSIWMI_BIOS_GUID);
  26. MODULE_ALIAS("wmi:" MSIWMI_MSI_EVENT_GUID);
  27. MODULE_ALIAS("wmi:" MSIWMI_WIND_EVENT_GUID);
  28. enum msi_scancodes {
  29. /* Generic MSI keys (not present on MSI Wind) */
  30. MSI_KEY_BRIGHTNESSUP = 0xD0,
  31. MSI_KEY_BRIGHTNESSDOWN,
  32. MSI_KEY_VOLUMEUP,
  33. MSI_KEY_VOLUMEDOWN,
  34. MSI_KEY_MUTE,
  35. /* MSI Wind keys */
  36. WIND_KEY_TOUCHPAD = 0x08, /* Fn+F3 touchpad toggle */
  37. WIND_KEY_BLUETOOTH = 0x56, /* Fn+F11 Bluetooth toggle */
  38. WIND_KEY_CAMERA, /* Fn+F6 webcam toggle */
  39. WIND_KEY_WLAN = 0x5f, /* Fn+F11 Wi-Fi toggle */
  40. WIND_KEY_TURBO, /* Fn+F10 turbo mode toggle */
  41. WIND_KEY_ECO = 0x69, /* Fn+F10 ECO mode toggle */
  42. };
  43. static struct key_entry msi_wmi_keymap[] = {
  44. { KE_KEY, MSI_KEY_BRIGHTNESSUP, {KEY_BRIGHTNESSUP} },
  45. { KE_KEY, MSI_KEY_BRIGHTNESSDOWN, {KEY_BRIGHTNESSDOWN} },
  46. { KE_KEY, MSI_KEY_VOLUMEUP, {KEY_VOLUMEUP} },
  47. { KE_KEY, MSI_KEY_VOLUMEDOWN, {KEY_VOLUMEDOWN} },
  48. { KE_KEY, MSI_KEY_MUTE, {KEY_MUTE} },
  49. /* These keys work without WMI. Ignore them to avoid double keycodes */
  50. { KE_IGNORE, WIND_KEY_TOUCHPAD, {KEY_TOUCHPAD_TOGGLE} },
  51. { KE_IGNORE, WIND_KEY_BLUETOOTH, {KEY_BLUETOOTH} },
  52. { KE_IGNORE, WIND_KEY_CAMERA, {KEY_CAMERA} },
  53. { KE_IGNORE, WIND_KEY_WLAN, {KEY_WLAN} },
  54. /* These are unknown WMI events found on MSI Wind */
  55. { KE_IGNORE, 0x00 },
  56. { KE_IGNORE, 0x62 },
  57. { KE_IGNORE, 0x63 },
  58. /* These are MSI Wind keys that should be handled via WMI */
  59. { KE_KEY, WIND_KEY_TURBO, {KEY_PROG1} },
  60. { KE_KEY, WIND_KEY_ECO, {KEY_PROG2} },
  61. { KE_END, 0 }
  62. };
  63. static ktime_t last_pressed;
  64. static const struct {
  65. const char *guid;
  66. bool quirk_last_pressed;
  67. } *event_wmi, event_wmis[] = {
  68. { MSIWMI_MSI_EVENT_GUID, true },
  69. { MSIWMI_WIND_EVENT_GUID, false },
  70. };
  71. static struct backlight_device *backlight;
  72. static int backlight_map[] = { 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF };
  73. static struct input_dev *msi_wmi_input_dev;
  74. static int msi_wmi_query_block(int instance, int *ret)
  75. {
  76. acpi_status status;
  77. union acpi_object *obj;
  78. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  79. status = wmi_query_block(MSIWMI_BIOS_GUID, instance, &output);
  80. if (ACPI_FAILURE(status))
  81. return -EIO;
  82. obj = output.pointer;
  83. if (!obj || obj->type != ACPI_TYPE_INTEGER) {
  84. if (obj) {
  85. pr_err("query block returned object "
  86. "type: %d - buffer length:%d\n", obj->type,
  87. obj->type == ACPI_TYPE_BUFFER ?
  88. obj->buffer.length : 0);
  89. }
  90. kfree(obj);
  91. return -EINVAL;
  92. }
  93. *ret = obj->integer.value;
  94. kfree(obj);
  95. return 0;
  96. }
  97. static int msi_wmi_set_block(int instance, int value)
  98. {
  99. acpi_status status;
  100. struct acpi_buffer input = { sizeof(int), &value };
  101. pr_debug("Going to set block of instance: %d - value: %d\n",
  102. instance, value);
  103. status = wmi_set_block(MSIWMI_BIOS_GUID, instance, &input);
  104. return ACPI_SUCCESS(status) ? 0 : 1;
  105. }
  106. static int bl_get(struct backlight_device *bd)
  107. {
  108. int level, err, ret;
  109. /* Instance 1 is "get backlight", cmp with DSDT */
  110. err = msi_wmi_query_block(1, &ret);
  111. if (err) {
  112. pr_err("Could not query backlight: %d\n", err);
  113. return -EINVAL;
  114. }
  115. pr_debug("Get: Query block returned: %d\n", ret);
  116. for (level = 0; level < ARRAY_SIZE(backlight_map); level++) {
  117. if (backlight_map[level] == ret) {
  118. pr_debug("Current backlight level: 0x%X - index: %d\n",
  119. backlight_map[level], level);
  120. break;
  121. }
  122. }
  123. if (level == ARRAY_SIZE(backlight_map)) {
  124. pr_err("get: Invalid brightness value: 0x%X\n", ret);
  125. return -EINVAL;
  126. }
  127. return level;
  128. }
  129. static int bl_set_status(struct backlight_device *bd)
  130. {
  131. int bright = bd->props.brightness;
  132. if (bright >= ARRAY_SIZE(backlight_map) || bright < 0)
  133. return -EINVAL;
  134. /* Instance 0 is "set backlight" */
  135. return msi_wmi_set_block(0, backlight_map[bright]);
  136. }
  137. static const struct backlight_ops msi_backlight_ops = {
  138. .get_brightness = bl_get,
  139. .update_status = bl_set_status,
  140. };
  141. static void msi_wmi_notify(u32 value, void *context)
  142. {
  143. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  144. struct key_entry *key;
  145. union acpi_object *obj;
  146. acpi_status status;
  147. status = wmi_get_event_data(value, &response);
  148. if (status != AE_OK) {
  149. pr_info("bad event status 0x%x\n", status);
  150. return;
  151. }
  152. obj = (union acpi_object *)response.pointer;
  153. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  154. int eventcode = obj->integer.value;
  155. pr_debug("Eventcode: 0x%x\n", eventcode);
  156. key = sparse_keymap_entry_from_scancode(msi_wmi_input_dev,
  157. eventcode);
  158. if (!key) {
  159. pr_info("Unknown key pressed - %x\n", eventcode);
  160. goto msi_wmi_notify_exit;
  161. }
  162. if (event_wmi->quirk_last_pressed) {
  163. ktime_t cur = ktime_get_real();
  164. ktime_t diff = ktime_sub(cur, last_pressed);
  165. /* Ignore event if any event happened in a 50 ms
  166. timeframe -> Key press may result in 10-20 GPEs */
  167. if (ktime_to_us(diff) < 1000 * 50) {
  168. pr_debug("Suppressed key event 0x%X - "
  169. "Last press was %lld us ago\n",
  170. key->code, ktime_to_us(diff));
  171. goto msi_wmi_notify_exit;
  172. }
  173. last_pressed = cur;
  174. }
  175. if (key->type == KE_KEY &&
  176. /* Brightness is served via acpi video driver */
  177. (backlight ||
  178. (key->code != MSI_KEY_BRIGHTNESSUP &&
  179. key->code != MSI_KEY_BRIGHTNESSDOWN))) {
  180. pr_debug("Send key: 0x%X - Input layer keycode: %d\n",
  181. key->code, key->keycode);
  182. sparse_keymap_report_entry(msi_wmi_input_dev, key, 1,
  183. true);
  184. }
  185. } else
  186. pr_info("Unknown event received\n");
  187. msi_wmi_notify_exit:
  188. kfree(response.pointer);
  189. }
  190. static int __init msi_wmi_backlight_setup(void)
  191. {
  192. int err;
  193. struct backlight_properties props;
  194. memset(&props, 0, sizeof(struct backlight_properties));
  195. props.type = BACKLIGHT_PLATFORM;
  196. props.max_brightness = ARRAY_SIZE(backlight_map) - 1;
  197. backlight = backlight_device_register(DRV_NAME, NULL, NULL,
  198. &msi_backlight_ops,
  199. &props);
  200. if (IS_ERR(backlight))
  201. return PTR_ERR(backlight);
  202. err = bl_get(NULL);
  203. if (err < 0) {
  204. backlight_device_unregister(backlight);
  205. return err;
  206. }
  207. backlight->props.brightness = err;
  208. return 0;
  209. }
  210. static int __init msi_wmi_input_setup(void)
  211. {
  212. int err;
  213. msi_wmi_input_dev = input_allocate_device();
  214. if (!msi_wmi_input_dev)
  215. return -ENOMEM;
  216. msi_wmi_input_dev->name = "MSI WMI hotkeys";
  217. msi_wmi_input_dev->phys = "wmi/input0";
  218. msi_wmi_input_dev->id.bustype = BUS_HOST;
  219. err = sparse_keymap_setup(msi_wmi_input_dev, msi_wmi_keymap, NULL);
  220. if (err)
  221. goto err_free_dev;
  222. err = input_register_device(msi_wmi_input_dev);
  223. if (err)
  224. goto err_free_dev;
  225. last_pressed = 0;
  226. return 0;
  227. err_free_dev:
  228. input_free_device(msi_wmi_input_dev);
  229. return err;
  230. }
  231. static int __init msi_wmi_init(void)
  232. {
  233. int err;
  234. int i;
  235. for (i = 0; i < ARRAY_SIZE(event_wmis); i++) {
  236. if (!wmi_has_guid(event_wmis[i].guid))
  237. continue;
  238. err = msi_wmi_input_setup();
  239. if (err) {
  240. pr_err("Unable to setup input device\n");
  241. return err;
  242. }
  243. err = wmi_install_notify_handler(event_wmis[i].guid,
  244. msi_wmi_notify, NULL);
  245. if (ACPI_FAILURE(err)) {
  246. pr_err("Unable to setup WMI notify handler\n");
  247. goto err_free_input;
  248. }
  249. pr_debug("Event handler installed\n");
  250. event_wmi = &event_wmis[i];
  251. break;
  252. }
  253. if (wmi_has_guid(MSIWMI_BIOS_GUID) &&
  254. acpi_video_get_backlight_type() == acpi_backlight_vendor) {
  255. err = msi_wmi_backlight_setup();
  256. if (err) {
  257. pr_err("Unable to setup backlight device\n");
  258. goto err_uninstall_handler;
  259. }
  260. pr_debug("Backlight device created\n");
  261. }
  262. if (!event_wmi && !backlight) {
  263. pr_err("This machine doesn't have neither MSI-hotkeys nor backlight through WMI\n");
  264. return -ENODEV;
  265. }
  266. return 0;
  267. err_uninstall_handler:
  268. if (event_wmi)
  269. wmi_remove_notify_handler(event_wmi->guid);
  270. err_free_input:
  271. if (event_wmi)
  272. input_unregister_device(msi_wmi_input_dev);
  273. return err;
  274. }
  275. static void __exit msi_wmi_exit(void)
  276. {
  277. if (event_wmi) {
  278. wmi_remove_notify_handler(event_wmi->guid);
  279. input_unregister_device(msi_wmi_input_dev);
  280. }
  281. backlight_device_unregister(backlight);
  282. }
  283. module_init(msi_wmi_init);
  284. module_exit(msi_wmi_exit);