hid-microsoft.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  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. #define MS_RDESC_3K 0x40
  28. static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  29. unsigned int *rsize)
  30. {
  31. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  32. /*
  33. * Microsoft Wireless Desktop Receiver (Model 1028) has
  34. * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
  35. */
  36. if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
  37. rdesc[559] == 0x29) {
  38. hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
  39. rdesc[557] = 0x35;
  40. rdesc[559] = 0x45;
  41. }
  42. /* the same as above (s/usage/physical/) */
  43. if ((quirks & MS_RDESC_3K) && *rsize == 106 && rdesc[94] == 0x19 &&
  44. rdesc[95] == 0x00 && rdesc[96] == 0x29 &&
  45. rdesc[97] == 0xff) {
  46. rdesc[94] = 0x35;
  47. rdesc[96] = 0x45;
  48. }
  49. return rdesc;
  50. }
  51. #define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
  52. EV_KEY, (c))
  53. static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
  54. unsigned long **bit, int *max)
  55. {
  56. struct input_dev *input = hi->input;
  57. if ((usage->hid & HID_USAGE_PAGE) == HID_UP_CONSUMER) {
  58. switch (usage->hid & HID_USAGE) {
  59. /*
  60. * Microsoft uses these 2 reserved usage ids for 2 keys on
  61. * the MS office kb labelled "Office Home" and "Task Pane".
  62. */
  63. case 0x29d:
  64. ms_map_key_clear(KEY_PROG1);
  65. return 1;
  66. case 0x29e:
  67. ms_map_key_clear(KEY_PROG2);
  68. return 1;
  69. }
  70. return 0;
  71. }
  72. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
  73. return 0;
  74. switch (usage->hid & HID_USAGE) {
  75. case 0xfd06: ms_map_key_clear(KEY_CHAT); break;
  76. case 0xfd07: ms_map_key_clear(KEY_PHONE); break;
  77. case 0xff00:
  78. /* Special keypad keys */
  79. ms_map_key_clear(KEY_KPEQUAL);
  80. set_bit(KEY_KPLEFTPAREN, input->keybit);
  81. set_bit(KEY_KPRIGHTPAREN, input->keybit);
  82. break;
  83. case 0xff01:
  84. /* Scroll wheel */
  85. hid_map_usage_clear(hi, usage, bit, max, EV_REL, REL_WHEEL);
  86. break;
  87. case 0xff02:
  88. /*
  89. * This byte contains a copy of the modifier keys byte of a
  90. * standard hid keyboard report, as send by interface 0
  91. * (this usage is found on interface 1).
  92. *
  93. * This byte only gets send when another key in the same report
  94. * changes state, and as such is useless, ignore it.
  95. */
  96. return -1;
  97. case 0xff05:
  98. set_bit(EV_REP, input->evbit);
  99. ms_map_key_clear(KEY_F13);
  100. set_bit(KEY_F14, input->keybit);
  101. set_bit(KEY_F15, input->keybit);
  102. set_bit(KEY_F16, input->keybit);
  103. set_bit(KEY_F17, input->keybit);
  104. set_bit(KEY_F18, input->keybit);
  105. break;
  106. default:
  107. return 0;
  108. }
  109. return 1;
  110. }
  111. static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
  112. unsigned long **bit, int *max)
  113. {
  114. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
  115. return 0;
  116. set_bit(EV_REP, hi->input->evbit);
  117. switch (usage->hid & HID_USAGE) {
  118. case 0xfd08: ms_map_key_clear(KEY_FORWARD); break;
  119. case 0xfd09: ms_map_key_clear(KEY_BACK); break;
  120. case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE); break;
  121. case 0xfd0e: ms_map_key_clear(KEY_CLOSE); break;
  122. case 0xfd0f: ms_map_key_clear(KEY_PLAY); break;
  123. default:
  124. return 0;
  125. }
  126. return 1;
  127. }
  128. static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  129. struct hid_field *field, struct hid_usage *usage,
  130. unsigned long **bit, int *max)
  131. {
  132. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  133. if (quirks & MS_ERGONOMY) {
  134. int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
  135. if (ret)
  136. return ret;
  137. }
  138. if ((quirks & MS_PRESENTER) &&
  139. ms_presenter_8k_quirk(hi, usage, bit, max))
  140. return 1;
  141. return 0;
  142. }
  143. static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  144. struct hid_field *field, struct hid_usage *usage,
  145. unsigned long **bit, int *max)
  146. {
  147. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  148. if (quirks & MS_DUPLICATE_USAGES)
  149. clear_bit(usage->code, *bit);
  150. return 0;
  151. }
  152. static int ms_event(struct hid_device *hdev, struct hid_field *field,
  153. struct hid_usage *usage, __s32 value)
  154. {
  155. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  156. struct input_dev *input;
  157. if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
  158. !usage->type)
  159. return 0;
  160. input = field->hidinput->input;
  161. /* Handling MS keyboards special buttons */
  162. if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff00)) {
  163. /* Special keypad keys */
  164. input_report_key(input, KEY_KPEQUAL, value & 0x01);
  165. input_report_key(input, KEY_KPLEFTPAREN, value & 0x02);
  166. input_report_key(input, KEY_KPRIGHTPAREN, value & 0x04);
  167. return 1;
  168. }
  169. if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff01)) {
  170. /* Scroll wheel */
  171. int step = ((value & 0x60) >> 5) + 1;
  172. switch (value & 0x1f) {
  173. case 0x01:
  174. input_report_rel(input, REL_WHEEL, step);
  175. break;
  176. case 0x1f:
  177. input_report_rel(input, REL_WHEEL, -step);
  178. break;
  179. }
  180. return 1;
  181. }
  182. if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
  183. static unsigned int last_key = 0;
  184. unsigned int key = 0;
  185. switch (value) {
  186. case 0x01: key = KEY_F14; break;
  187. case 0x02: key = KEY_F15; break;
  188. case 0x04: key = KEY_F16; break;
  189. case 0x08: key = KEY_F17; break;
  190. case 0x10: key = KEY_F18; break;
  191. }
  192. if (key) {
  193. input_event(input, usage->type, key, 1);
  194. last_key = key;
  195. } else
  196. input_event(input, usage->type, last_key, 0);
  197. return 1;
  198. }
  199. return 0;
  200. }
  201. static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
  202. {
  203. unsigned long quirks = id->driver_data;
  204. int ret;
  205. hid_set_drvdata(hdev, (void *)quirks);
  206. if (quirks & MS_NOGET)
  207. hdev->quirks |= HID_QUIRK_NOGET;
  208. ret = hid_parse(hdev);
  209. if (ret) {
  210. hid_err(hdev, "parse failed\n");
  211. goto err_free;
  212. }
  213. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
  214. HID_CONNECT_HIDINPUT_FORCE : 0));
  215. if (ret) {
  216. hid_err(hdev, "hw start failed\n");
  217. goto err_free;
  218. }
  219. return 0;
  220. err_free:
  221. return ret;
  222. }
  223. static const struct hid_device_id ms_devices[] = {
  224. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
  225. .driver_data = MS_HIDINPUT },
  226. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_OFFICE_KB),
  227. .driver_data = MS_ERGONOMY },
  228. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
  229. .driver_data = MS_ERGONOMY },
  230. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K_JP),
  231. .driver_data = MS_ERGONOMY },
  232. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE7K),
  233. .driver_data = MS_ERGONOMY },
  234. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
  235. .driver_data = MS_ERGONOMY | MS_RDESC },
  236. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
  237. .driver_data = MS_PRESENTER },
  238. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
  239. .driver_data = MS_ERGONOMY | MS_RDESC_3K },
  240. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
  241. .driver_data = MS_NOGET },
  242. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
  243. .driver_data = MS_DUPLICATE_USAGES },
  244. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_PRO_3),
  245. .driver_data = MS_HIDINPUT },
  246. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_PRO_3_2),
  247. .driver_data = MS_HIDINPUT },
  248. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_PRO_3_JP),
  249. .driver_data = MS_HIDINPUT },
  250. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_TYPE_COVER_3),
  251. .driver_data = MS_HIDINPUT },
  252. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_POWER_COVER),
  253. .driver_data = MS_HIDINPUT },
  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");