msi-wmi.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/acpi.h>
  25. #include <linux/backlight.h>
  26. MODULE_AUTHOR("Thomas Renninger <trenn@suse.de>");
  27. MODULE_DESCRIPTION("MSI laptop WMI hotkeys driver");
  28. MODULE_LICENSE("GPL");
  29. MODULE_ALIAS("wmi:551A1F84-FBDD-4125-91DB-3EA8F44F1D45");
  30. MODULE_ALIAS("wmi:B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2");
  31. /* Temporary workaround until the WMI sysfs interface goes in
  32. { "svn", DMI_SYS_VENDOR },
  33. { "pn", DMI_PRODUCT_NAME },
  34. { "pvr", DMI_PRODUCT_VERSION },
  35. { "rvn", DMI_BOARD_VENDOR },
  36. { "rn", DMI_BOARD_NAME },
  37. */
  38. MODULE_ALIAS("dmi:*:svnMICRO-STARINTERNATIONAL*:pnMS-6638:*");
  39. #define DRV_NAME "msi-wmi"
  40. #define DRV_PFX DRV_NAME ": "
  41. #define MSIWMI_BIOS_GUID "551A1F84-FBDD-4125-91DB-3EA8F44F1D45"
  42. #define MSIWMI_EVENT_GUID "B6F3EEF2-3D2F-49DC-9DE3-85BCE18C62F2"
  43. #define dprintk(msg...) pr_debug(DRV_PFX msg)
  44. struct key_entry {
  45. char type; /* See KE_* below */
  46. u16 code;
  47. u16 keycode;
  48. ktime_t last_pressed;
  49. };
  50. /*
  51. * KE_KEY the only used key type, but keep this, others might also
  52. * show up in the future. Compare with hp-wmi.c
  53. */
  54. enum { KE_KEY, KE_END };
  55. static struct key_entry msi_wmi_keymap[] = {
  56. { KE_KEY, 0xd0, KEY_BRIGHTNESSUP, {0, } },
  57. { KE_KEY, 0xd1, KEY_BRIGHTNESSDOWN, {0, } },
  58. { KE_KEY, 0xd2, KEY_VOLUMEUP, {0, } },
  59. { KE_KEY, 0xd3, KEY_VOLUMEDOWN, {0, } },
  60. { KE_END, 0}
  61. };
  62. struct backlight_device *backlight;
  63. static int backlight_map[] = { 0x00, 0x33, 0x66, 0x99, 0xCC, 0xFF };
  64. static struct input_dev *msi_wmi_input_dev;
  65. static int msi_wmi_query_block(int instance, int *ret)
  66. {
  67. acpi_status status;
  68. union acpi_object *obj;
  69. struct acpi_buffer output = { ACPI_ALLOCATE_BUFFER, NULL };
  70. status = wmi_query_block(MSIWMI_BIOS_GUID, instance, &output);
  71. obj = output.pointer;
  72. if (!obj || obj->type != ACPI_TYPE_INTEGER) {
  73. if (obj) {
  74. printk(KERN_ERR DRV_PFX "query block returned object "
  75. "type: %d - buffer length:%d\n", obj->type,
  76. obj->type == ACPI_TYPE_BUFFER ?
  77. obj->buffer.length : 0);
  78. }
  79. kfree(obj);
  80. return -EINVAL;
  81. }
  82. *ret = obj->integer.value;
  83. kfree(obj);
  84. return 0;
  85. }
  86. static int msi_wmi_set_block(int instance, int value)
  87. {
  88. acpi_status status;
  89. struct acpi_buffer input = { sizeof(int), &value };
  90. dprintk("Going to set block of instance: %d - value: %d\n",
  91. instance, value);
  92. status = wmi_set_block(MSIWMI_BIOS_GUID, instance, &input);
  93. return ACPI_SUCCESS(status) ? 0 : 1;
  94. }
  95. static int bl_get(struct backlight_device *bd)
  96. {
  97. int level, err, ret = 0;
  98. /* Instance 1 is "get backlight", cmp with DSDT */
  99. err = msi_wmi_query_block(1, &ret);
  100. if (err)
  101. printk(KERN_ERR DRV_PFX "Could not query backlight: %d\n", err);
  102. dprintk("Get: Query block returned: %d\n", ret);
  103. for (level = 0; level < ARRAY_SIZE(backlight_map); level++) {
  104. if (backlight_map[level] == ret) {
  105. dprintk("Current backlight level: 0x%X - index: %d\n",
  106. backlight_map[level], level);
  107. break;
  108. }
  109. }
  110. if (level == ARRAY_SIZE(backlight_map)) {
  111. printk(KERN_ERR DRV_PFX "get: Invalid brightness value: 0x%X\n",
  112. ret);
  113. return -EINVAL;
  114. }
  115. return level;
  116. }
  117. static int bl_set_status(struct backlight_device *bd)
  118. {
  119. int bright = bd->props.brightness;
  120. if (bright >= ARRAY_SIZE(backlight_map) || bright < 0)
  121. return -EINVAL;
  122. /* Instance 0 is "set backlight" */
  123. return msi_wmi_set_block(0, backlight_map[bright]);
  124. }
  125. static struct backlight_ops msi_backlight_ops = {
  126. .get_brightness = bl_get,
  127. .update_status = bl_set_status,
  128. };
  129. static struct key_entry *msi_wmi_get_entry_by_scancode(int code)
  130. {
  131. struct key_entry *key;
  132. for (key = msi_wmi_keymap; key->type != KE_END; key++)
  133. if (code == key->code)
  134. return key;
  135. return NULL;
  136. }
  137. static struct key_entry *msi_wmi_get_entry_by_keycode(int keycode)
  138. {
  139. struct key_entry *key;
  140. for (key = msi_wmi_keymap; key->type != KE_END; key++)
  141. if (key->type == KE_KEY && keycode == key->keycode)
  142. return key;
  143. return NULL;
  144. }
  145. static int msi_wmi_getkeycode(struct input_dev *dev, int scancode, int *keycode)
  146. {
  147. struct key_entry *key = msi_wmi_get_entry_by_scancode(scancode);
  148. if (key && key->type == KE_KEY) {
  149. *keycode = key->keycode;
  150. return 0;
  151. }
  152. return -EINVAL;
  153. }
  154. static int msi_wmi_setkeycode(struct input_dev *dev, int scancode, int keycode)
  155. {
  156. struct key_entry *key;
  157. int old_keycode;
  158. if (keycode < 0 || keycode > KEY_MAX)
  159. return -EINVAL;
  160. key = msi_wmi_get_entry_by_scancode(scancode);
  161. if (key && key->type == KE_KEY) {
  162. old_keycode = key->keycode;
  163. key->keycode = keycode;
  164. set_bit(keycode, dev->keybit);
  165. if (!msi_wmi_get_entry_by_keycode(old_keycode))
  166. clear_bit(old_keycode, dev->keybit);
  167. return 0;
  168. }
  169. return -EINVAL;
  170. }
  171. static void msi_wmi_notify(u32 value, void *context)
  172. {
  173. struct acpi_buffer response = { ACPI_ALLOCATE_BUFFER, NULL };
  174. static struct key_entry *key;
  175. union acpi_object *obj;
  176. ktime_t cur;
  177. wmi_get_event_data(value, &response);
  178. obj = (union acpi_object *)response.pointer;
  179. if (obj && obj->type == ACPI_TYPE_INTEGER) {
  180. int eventcode = obj->integer.value;
  181. dprintk("Eventcode: 0x%x\n", eventcode);
  182. key = msi_wmi_get_entry_by_scancode(eventcode);
  183. if (key) {
  184. cur = ktime_get_real();
  185. /* Ignore event if the same event happened in a 50 ms
  186. timeframe -> Key press may result in 10-20 GPEs */
  187. if (ktime_to_us(ktime_sub(cur, key->last_pressed))
  188. < 1000 * 50) {
  189. dprintk("Suppressed key event 0x%X - "
  190. "Last press was %lld us ago\n",
  191. key->code,
  192. ktime_to_us(ktime_sub(cur,
  193. key->last_pressed)));
  194. return;
  195. }
  196. key->last_pressed = cur;
  197. switch (key->type) {
  198. case KE_KEY:
  199. /* Brightness is served via acpi video driver */
  200. if (!backlight &&
  201. (key->keycode == KEY_BRIGHTNESSUP ||
  202. key->keycode == KEY_BRIGHTNESSDOWN))
  203. break;
  204. dprintk("Send key: 0x%X - "
  205. "Input layer keycode: %d\n", key->code,
  206. key->keycode);
  207. input_report_key(msi_wmi_input_dev,
  208. key->keycode, 1);
  209. input_sync(msi_wmi_input_dev);
  210. input_report_key(msi_wmi_input_dev,
  211. key->keycode, 0);
  212. input_sync(msi_wmi_input_dev);
  213. break;
  214. }
  215. } else
  216. printk(KERN_INFO "Unknown key pressed - %x\n",
  217. eventcode);
  218. } else
  219. printk(KERN_INFO DRV_PFX "Unknown event received\n");
  220. kfree(response.pointer);
  221. }
  222. static int __init msi_wmi_input_setup(void)
  223. {
  224. struct key_entry *key;
  225. int err;
  226. msi_wmi_input_dev = input_allocate_device();
  227. if (!msi_wmi_input_dev)
  228. return -ENOMEM;
  229. msi_wmi_input_dev->name = "MSI WMI hotkeys";
  230. msi_wmi_input_dev->phys = "wmi/input0";
  231. msi_wmi_input_dev->id.bustype = BUS_HOST;
  232. msi_wmi_input_dev->getkeycode = msi_wmi_getkeycode;
  233. msi_wmi_input_dev->setkeycode = msi_wmi_setkeycode;
  234. for (key = msi_wmi_keymap; key->type != KE_END; key++) {
  235. switch (key->type) {
  236. case KE_KEY:
  237. set_bit(EV_KEY, msi_wmi_input_dev->evbit);
  238. set_bit(key->keycode, msi_wmi_input_dev->keybit);
  239. break;
  240. }
  241. }
  242. err = input_register_device(msi_wmi_input_dev);
  243. if (err) {
  244. input_free_device(msi_wmi_input_dev);
  245. return err;
  246. }
  247. return 0;
  248. }
  249. static int __init msi_wmi_init(void)
  250. {
  251. int err;
  252. if (!wmi_has_guid(MSIWMI_EVENT_GUID)) {
  253. printk(KERN_ERR
  254. "This machine doesn't have MSI-hotkeys through WMI\n");
  255. return -ENODEV;
  256. }
  257. err = wmi_install_notify_handler(MSIWMI_EVENT_GUID,
  258. msi_wmi_notify, NULL);
  259. if (err)
  260. return -EINVAL;
  261. err = msi_wmi_input_setup();
  262. if (err)
  263. goto err_uninstall_notifier;
  264. if (!acpi_video_backlight_support()) {
  265. backlight = backlight_device_register(DRV_NAME,
  266. NULL, NULL, &msi_backlight_ops);
  267. if (IS_ERR(backlight))
  268. goto err_free_input;
  269. backlight->props.max_brightness = ARRAY_SIZE(backlight_map) - 1;
  270. err = bl_get(NULL);
  271. if (err < 0)
  272. goto err_free_backlight;
  273. backlight->props.brightness = err;
  274. }
  275. dprintk("Event handler installed\n");
  276. return 0;
  277. err_free_backlight:
  278. backlight_device_unregister(backlight);
  279. err_free_input:
  280. input_unregister_device(msi_wmi_input_dev);
  281. err_uninstall_notifier:
  282. wmi_remove_notify_handler(MSIWMI_EVENT_GUID);
  283. return err;
  284. }
  285. static void __exit msi_wmi_exit(void)
  286. {
  287. if (wmi_has_guid(MSIWMI_EVENT_GUID)) {
  288. wmi_remove_notify_handler(MSIWMI_EVENT_GUID);
  289. input_unregister_device(msi_wmi_input_dev);
  290. backlight_device_unregister(backlight);
  291. }
  292. }
  293. module_init(msi_wmi_init);
  294. module_exit(msi_wmi_exit);