msi-wmi.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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. MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
  31. MODULE_DESCRIPTION("MSI laptop WMI hotkeys driver");
  32. MODULE_LICENSE("GPL");
  33. #define DRV_NAME "msi-wmi"
  34. #define MSIWMI_BIOS_GUID "551A1F84-FBDD-4125-91DB-3EA8F44F1D45"
  35. #define MSIWMI_EVENT_GUID "B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2"
  36. MODULE_ALIAS("wmi:" MSIWMI_BIOS_GUID);
  37. MODULE_ALIAS("wmi:" MSIWMI_EVENT_GUID);
  38. enum msi_scancodes {
  39. MSI_KEY_BRIGHTNESSUP = 0xD0,
  40. MSI_KEY_BRIGHTNESSDOWN,
  41. MSI_KEY_VOLUMEUP,
  42. MSI_KEY_VOLUMEDOWN,
  43. MSI_KEY_MUTE,
  44. };
  45. static struct key_entry msi_wmi_keymap[] = {
  46. { KE_KEY, MSI_KEY_BRIGHTNESSUP, {KEY_BRIGHTNESSUP} },
  47. { KE_KEY, MSI_KEY_BRIGHTNESSDOWN, {KEY_BRIGHTNESSDOWN} },
  48. { KE_KEY, MSI_KEY_VOLUMEUP, {KEY_VOLUMEUP} },
  49. { KE_KEY, MSI_KEY_VOLUMEDOWN, {KEY_VOLUMEDOWN} },
  50. { KE_KEY, MSI_KEY_MUTE, {KEY_MUTE} },
  51. { KE_END, 0 }
  52. };
  53. static ktime_t last_pressed;
  54. static bool quirk_last_pressed;
  55. static const char *event_wmi_guid;
  56. static struct backlight_device *backlight;
  57. static int backlight_map[] = { 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF };
  58. static struct input_dev *msi_wmi_input_dev;
  59. static int msi_wmi_query_block(int instance, int *ret)
  60. {
  61. acpi_status status;
  62. union acpi_object *obj;
  63. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  64. status = wmi_query_block(MSIWMI_BIOS_GUID, instance, &output);
  65. obj = output.pointer;
  66. if (!obj || obj->type != ACPI_TYPE_INTEGER) {
  67. if (obj) {
  68. pr_err("query block returned object "
  69. "type: %d - buffer length:%d\n", obj->type,
  70. obj->type == ACPI_TYPE_BUFFER ?
  71. obj->buffer.length : 0);
  72. }
  73. kfree(obj);
  74. return -EINVAL;
  75. }
  76. *ret = obj->integer.value;
  77. kfree(obj);
  78. return 0;
  79. }
  80. static int msi_wmi_set_block(int instance, int value)
  81. {
  82. acpi_status status;
  83. struct acpi_buffer input = { sizeof(int), &value };
  84. pr_debug("Going to set block of instance: %d - value: %d\n",
  85. instance, value);
  86. status = wmi_set_block(MSIWMI_BIOS_GUID, instance, &input);
  87. return ACPI_SUCCESS(status) ? 0 : 1;
  88. }
  89. static int bl_get(struct backlight_device *bd)
  90. {
  91. int level, err, ret;
  92. /* Instance 1 is "get backlight", cmp with DSDT */
  93. err = msi_wmi_query_block(1, &ret);
  94. if (err) {
  95. pr_err("Could not query backlight: %d\n", err);
  96. return -EINVAL;
  97. }
  98. pr_debug("Get: Query block returned: %d\n", ret);
  99. for (level = 0; level < ARRAY_SIZE(backlight_map); level++) {
  100. if (backlight_map[level] == ret) {
  101. pr_debug("Current backlight level: 0x%X - index: %d\n",
  102. backlight_map[level], level);
  103. break;
  104. }
  105. }
  106. if (level == ARRAY_SIZE(backlight_map)) {
  107. pr_err("get: Invalid brightness value: 0x%X\n", ret);
  108. return -EINVAL;
  109. }
  110. return level;
  111. }
  112. static int bl_set_status(struct backlight_device *bd)
  113. {
  114. int bright = bd->props.brightness;
  115. if (bright >= ARRAY_SIZE(backlight_map) || bright < 0)
  116. return -EINVAL;
  117. /* Instance 0 is "set backlight" */
  118. return msi_wmi_set_block(0, backlight_map[bright]);
  119. }
  120. static const struct backlight_ops msi_backlight_ops = {
  121. .get_brightness = bl_get,
  122. .update_status = bl_set_status,
  123. };
  124. static void msi_wmi_notify(u32 value, void *context)
  125. {
  126. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  127. static struct key_entry *key;
  128. union acpi_object *obj;
  129. acpi_status status;
  130. status = wmi_get_event_data(value, &response);
  131. if (status != AE_OK) {
  132. pr_info("bad event status 0x%x\n", status);
  133. return;
  134. }
  135. obj = (union acpi_object *)response.pointer;
  136. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  137. int eventcode = obj->integer.value;
  138. pr_debug("Eventcode: 0x%x\n", eventcode);
  139. key = sparse_keymap_entry_from_scancode(msi_wmi_input_dev,
  140. eventcode);
  141. if (!key) {
  142. pr_info("Unknown key pressed - %x\n", eventcode);
  143. goto msi_wmi_notify_exit;
  144. }
  145. if (quirk_last_pressed) {
  146. ktime_t cur = ktime_get_real();
  147. ktime_t diff = ktime_sub(cur, last_pressed);
  148. /* Ignore event if any event happened in a 50 ms
  149. timeframe -> Key press may result in 10-20 GPEs */
  150. if (ktime_to_us(diff) < 1000 * 50) {
  151. pr_debug("Suppressed key event 0x%X - "
  152. "Last press was %lld us ago\n",
  153. key->code, ktime_to_us(diff));
  154. goto msi_wmi_notify_exit;
  155. }
  156. last_pressed = cur;
  157. }
  158. if (key->type == KE_KEY &&
  159. /* Brightness is served via acpi video driver */
  160. (backlight ||
  161. (key->code != MSI_KEY_BRIGHTNESSUP &&
  162. key->code != MSI_KEY_BRIGHTNESSDOWN))) {
  163. pr_debug("Send key: 0x%X - Input layer keycode: %d\n",
  164. key->code, key->keycode);
  165. sparse_keymap_report_entry(msi_wmi_input_dev, key, 1,
  166. true);
  167. }
  168. } else
  169. pr_info("Unknown event received\n");
  170. msi_wmi_notify_exit:
  171. kfree(response.pointer);
  172. }
  173. static int __init msi_wmi_backlight_setup(void)
  174. {
  175. int err;
  176. struct backlight_properties props;
  177. memset(&props, 0, sizeof(struct backlight_properties));
  178. props.type = BACKLIGHT_PLATFORM;
  179. props.max_brightness = ARRAY_SIZE(backlight_map) - 1;
  180. backlight = backlight_device_register(DRV_NAME, NULL, NULL,
  181. &msi_backlight_ops,
  182. &props);
  183. if (IS_ERR(backlight))
  184. return PTR_ERR(backlight);
  185. err = bl_get(NULL);
  186. if (err < 0) {
  187. backlight_device_unregister(backlight);
  188. return err;
  189. }
  190. backlight->props.brightness = err;
  191. return 0;
  192. }
  193. static int __init msi_wmi_input_setup(void)
  194. {
  195. int err;
  196. msi_wmi_input_dev = input_allocate_device();
  197. if (!msi_wmi_input_dev)
  198. return -ENOMEM;
  199. msi_wmi_input_dev->name = "MSI WMI hotkeys";
  200. msi_wmi_input_dev->phys = "wmi/input0";
  201. msi_wmi_input_dev->id.bustype = BUS_HOST;
  202. err = sparse_keymap_setup(msi_wmi_input_dev, msi_wmi_keymap, NULL);
  203. if (err)
  204. goto err_free_dev;
  205. err = input_register_device(msi_wmi_input_dev);
  206. if (err)
  207. goto err_free_keymap;
  208. last_pressed = ktime_set(0, 0);
  209. return 0;
  210. err_free_keymap:
  211. sparse_keymap_free(msi_wmi_input_dev);
  212. err_free_dev:
  213. input_free_device(msi_wmi_input_dev);
  214. return err;
  215. }
  216. static int __init msi_wmi_init(void)
  217. {
  218. int err;
  219. if (wmi_has_guid(MSIWMI_EVENT_GUID)) {
  220. err = msi_wmi_input_setup();
  221. if (err) {
  222. pr_err("Unable to setup input device\n");
  223. return err;
  224. }
  225. err = wmi_install_notify_handler(MSIWMI_EVENT_GUID,
  226. msi_wmi_notify, NULL);
  227. if (ACPI_FAILURE(err)) {
  228. pr_err("Unable to setup WMI notify handler\n");
  229. goto err_free_input;
  230. }
  231. pr_debug("Event handler installed\n");
  232. event_wmi_guid = MSIWMI_EVENT_GUID;
  233. quirk_last_pressed = true;
  234. }
  235. if (wmi_has_guid(MSIWMI_BIOS_GUID) && !acpi_video_backlight_support()) {
  236. err = msi_wmi_backlight_setup();
  237. if (err) {
  238. pr_err("Unable to setup backlight device\n");
  239. goto err_uninstall_handler;
  240. }
  241. pr_debug("Backlight device created\n");
  242. }
  243. if (!event_wmi_guid && !backlight) {
  244. pr_err("This machine doesn't have neither MSI-hotkeys nor backlight through WMI\n");
  245. return -ENODEV;
  246. }
  247. return 0;
  248. err_uninstall_handler:
  249. if (event_wmi_guid)
  250. wmi_remove_notify_handler(event_wmi_guid);
  251. err_free_input:
  252. if (event_wmi_guid) {
  253. sparse_keymap_free(msi_wmi_input_dev);
  254. input_unregister_device(msi_wmi_input_dev);
  255. }
  256. return err;
  257. }
  258. static void __exit msi_wmi_exit(void)
  259. {
  260. if (event_wmi_guid) {
  261. wmi_remove_notify_handler(event_wmi_guid);
  262. sparse_keymap_free(msi_wmi_input_dev);
  263. input_unregister_device(msi_wmi_input_dev);
  264. }
  265. if (backlight)
  266. backlight_device_unregister(backlight);
  267. }
  268. module_init(msi_wmi_init);
  269. module_exit(msi_wmi_exit);