hid-microsoft.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. * HID driver for some microsoft "special" devices
  3. *
  4. * Copyright (c) 1999 Andreas Gal
  5. * Copyright (c) 2000-2005 Vojtech Pavlik <vojtech@suse.cz>
  6. * Copyright (c) 2005 Michael Haboustak <mike-@cinci.rr.com> for Concept2, Inc
  7. * Copyright (c) 2006-2007 Jiri Kosina
  8. * Copyright (c) 2008 Jiri Slaby
  9. */
  10. /*
  11. * This program is free software; you can redistribute it and/or modify it
  12. * under the terms of the GNU General Public License as published by the Free
  13. * Software Foundation; either version 2 of the License, or (at your option)
  14. * any later version.
  15. */
  16. #include <linux/device.h>
  17. #include <linux/input.h>
  18. #include <linux/hid.h>
  19. #include <linux/module.h>
  20. #include "hid-ids.h"
  21. #define MS_HIDINPUT 0x01
  22. #define MS_ERGONOMY 0x02
  23. #define MS_PRESENTER 0x04
  24. #define MS_RDESC 0x08
  25. #define MS_NOGET 0x10
  26. #define MS_DUPLICATE_USAGES 0x20
  27. static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  28. unsigned int *rsize)
  29. {
  30. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  31. /*
  32. * Microsoft Wireless Desktop Receiver (Model 1028) has
  33. * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
  34. */
  35. if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
  36. rdesc[559] == 0x29) {
  37. hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
  38. rdesc[557] = 0x35;
  39. rdesc[559] = 0x45;
  40. }
  41. return rdesc;
  42. }
  43. #define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
  44. EV_KEY, (c))
  45. static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
  46. unsigned long **bit, int *max)
  47. {
  48. struct input_dev *input = hi->input;
  49. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
  50. switch (usage->hid & HID_USAGE) {
  51. /*
  52. * Microsoft uses these 2 reserved usage ids for 2 keys on
  53. * the MS office kb labelled "Office Home" and "Task Pane".
  54. */
  55. case 0x29d:
  56. ms_map_key_clear(KEY_PROG1);
  57. return 1;
  58. case 0x29e:
  59. ms_map_key_clear(KEY_PROG2);
  60. return 1;
  61. }
  62. return 0;
  63. }
  64. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
  65. return 0;
  66. switch (usage->hid & HID_USAGE) {
  67. case 0xfd06: ms_map_key_clear(KEY_CHAT); break;
  68. case 0xfd07: ms_map_key_clear(KEY_PHONE); break;
  69. case 0xff00:
  70. /* Special keypad keys */
  71. ms_map_key_clear(KEY_KPEQUAL);
  72. set_bit(KEY_KPLEFTPAREN, input->keybit);
  73. set_bit(KEY_KPRIGHTPAREN, input->keybit);
  74. break;
  75. case 0xff01:
  76. /* Scroll wheel */
  77. hid_map_usage_clear(hi, usage, bit, max, EV_REL, REL_WHEEL);
  78. break;
  79. case 0xff02:
  80. /*
  81. * This byte contains a copy of the modifier keys byte of a
  82. * standard hid keyboard report, as send by interface 0
  83. * (this usage is found on interface 1).
  84. *
  85. * This byte only gets send when another key in the same report
  86. * changes state, and as such is useless, ignore it.
  87. */
  88. return -1;
  89. case 0xff05:
  90. set_bit(EV_REP, input->evbit);
  91. ms_map_key_clear(KEY_F13);
  92. set_bit(KEY_F14, input->keybit);
  93. set_bit(KEY_F15, input->keybit);
  94. set_bit(KEY_F16, input->keybit);
  95. set_bit(KEY_F17, input->keybit);
  96. set_bit(KEY_F18, input->keybit);
  97. break;
  98. default:
  99. return 0;
  100. }
  101. return 1;
  102. }
  103. static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
  104. unsigned long **bit, int *max)
  105. {
  106. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
  107. return 0;
  108. set_bit(EV_REP, hi->input->evbit);
  109. switch (usage->hid & HID_USAGE) {
  110. case 0xfd08: ms_map_key_clear(KEY_FORWARD); break;
  111. case 0xfd09: ms_map_key_clear(KEY_BACK); break;
  112. case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE); break;
  113. case 0xfd0e: ms_map_key_clear(KEY_CLOSE); break;
  114. case 0xfd0f: ms_map_key_clear(KEY_PLAY); break;
  115. default:
  116. return 0;
  117. }
  118. return 1;
  119. }
  120. static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  121. struct hid_field *field, struct hid_usage *usage,
  122. unsigned long **bit, int *max)
  123. {
  124. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  125. if (quirks & MS_ERGONOMY) {
  126. int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
  127. if (ret)
  128. return ret;
  129. }
  130. if ((quirks & MS_PRESENTER) &&
  131. ms_presenter_8k_quirk(hi, usage, bit, max))
  132. return 1;
  133. return 0;
  134. }
  135. static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  136. struct hid_field *field, struct hid_usage *usage,
  137. unsigned long **bit, int *max)
  138. {
  139. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  140. if (quirks & MS_DUPLICATE_USAGES)
  141. clear_bit(usage->code, *bit);
  142. return 0;
  143. }
  144. static int ms_event(struct hid_device *hdev, struct hid_field *field,
  145. struct hid_usage *usage, __s32 value)
  146. {
  147. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  148. struct input_dev *input;
  149. if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
  150. !usage->type)
  151. return 0;
  152. input = field->hidinput->input;
  153. /* Handling MS keyboards special buttons */
  154. if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff00)) {
  155. /* Special keypad keys */
  156. input_report_key(input, KEY_KPEQUAL, value & 0x01);
  157. input_report_key(input, KEY_KPLEFTPAREN, value & 0x02);
  158. input_report_key(input, KEY_KPRIGHTPAREN, value & 0x04);
  159. return 1;
  160. }
  161. if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff01)) {
  162. /* Scroll wheel */
  163. int step = ((value & 0x60) >> 5) + 1;
  164. switch (value & 0x1f) {
  165. case 0x01:
  166. input_report_rel(input, REL_WHEEL, step);
  167. break;
  168. case 0x1f:
  169. input_report_rel(input, REL_WHEEL, -step);
  170. break;
  171. }
  172. return 1;
  173. }
  174. if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
  175. static unsigned int last_key = 0;
  176. unsigned int key = 0;
  177. switch (value) {
  178. case 0x01: key = KEY_F14; break;
  179. case 0x02: key = KEY_F15; break;
  180. case 0x04: key = KEY_F16; break;
  181. case 0x08: key = KEY_F17; break;
  182. case 0x10: key = KEY_F18; break;
  183. }
  184. if (key) {
  185. input_event(input, usage->type, key, 1);
  186. last_key = key;
  187. } else
  188. input_event(input, usage->type, last_key, 0);
  189. return 1;
  190. }
  191. return 0;
  192. }
  193. static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
  194. {
  195. unsigned long quirks = id->driver_data;
  196. int ret;
  197. hid_set_drvdata(hdev, (void *)quirks);
  198. if (quirks & MS_NOGET)
  199. hdev->quirks |= HID_QUIRK_NOGET;
  200. ret = hid_parse(hdev);
  201. if (ret) {
  202. hid_err(hdev, "parse failed\n");
  203. goto err_free;
  204. }
  205. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
  206. HID_CONNECT_HIDINPUT_FORCE : 0));
  207. if (ret) {
  208. hid_err(hdev, "hw start failed\n");
  209. goto err_free;
  210. }
  211. return 0;
  212. err_free:
  213. return ret;
  214. }
  215. static const struct hid_device_id ms_devices[] = {
  216. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
  217. .driver_data = MS_HIDINPUT },
  218. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_OFFICE_KB),
  219. .driver_data = MS_ERGONOMY },
  220. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
  221. .driver_data = MS_ERGONOMY },
  222. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP),
  223. .driver_data = MS_ERGONOMY },
  224. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE7K),
  225. .driver_data = MS_ERGONOMY },
  226. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
  227. .driver_data = MS_ERGONOMY | MS_RDESC },
  228. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
  229. .driver_data = MS_PRESENTER },
  230. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
  231. .driver_data = MS_ERGONOMY },
  232. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_7K),
  233. .driver_data = MS_ERGONOMY },
  234. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_600),
  235. .driver_data = MS_ERGONOMY },
  236. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3KV1),
  237. .driver_data = MS_ERGONOMY },
  238. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
  239. .driver_data = MS_NOGET },
  240. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
  241. .driver_data = MS_DUPLICATE_USAGES },
  242. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_PRO_3),
  243. .driver_data = MS_HIDINPUT },
  244. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_PRO_3_2),
  245. .driver_data = MS_HIDINPUT },
  246. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_PRO_3_JP),
  247. .driver_data = MS_HIDINPUT },
  248. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_PRO_4_JP),
  249. .driver_data = MS_HIDINPUT },
  250. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_POWER_COVER),
  251. .driver_data = MS_HIDINPUT },
  252. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_KEYBOARD),
  253. .driver_data = MS_ERGONOMY},
  254. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
  255. .driver_data = MS_PRESENTER },
  256. { }
  257. };
  258. MODULE_DEVICE_TABLE(hid, ms_devices);
  259. static struct hid_driver ms_driver = {
  260. .name = "microsoft",
  261. .id_table = ms_devices,
  262. .report_fixup = ms_report_fixup,
  263. .input_mapping = ms_input_mapping,
  264. .input_mapped = ms_input_mapped,
  265. .event = ms_event,
  266. .probe = ms_probe,
  267. };
  268. module_hid_driver(ms_driver);
  269. MODULE_LICENSE("GPL");