msi-wmi.c 7.8 KB

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