msi-wmi.c 9.1 KB

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