msi-wmi.c 7.8 KB

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