msi-wmi.c 7.6 KB

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