msi-wmi.c 9.3 KB

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