hid-microsoft.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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) 2007 Paul Walmsley
  9. * Copyright (c) 2008 Jiri Slaby
  10. */
  11. /*
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the Free
  14. * Software Foundation; either version 2 of the License, or (at your option)
  15. * any later version.
  16. */
  17. #include <linux/device.h>
  18. #include <linux/input.h>
  19. #include <linux/hid.h>
  20. #include <linux/module.h>
  21. #include "hid-ids.h"
  22. #define MS_HIDINPUT 0x01
  23. #define MS_ERGONOMY 0x02
  24. #define MS_PRESENTER 0x04
  25. #define MS_RDESC 0x08
  26. #define MS_NOGET 0x10
  27. #define MS_DUPLICATE_USAGES 0x20
  28. #define MS_RDESC_3K 0x40
  29. static __u8 *ms_report_fixup(struct hid_device *hdev, __u8 *rdesc,
  30. unsigned int *rsize)
  31. {
  32. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  33. /*
  34. * Microsoft Wireless Desktop Receiver (Model 1028) has
  35. * 'Usage Min/Max' where it ought to have 'Physical Min/Max'
  36. */
  37. if ((quirks & MS_RDESC) && *rsize == 571 && rdesc[557] == 0x19 &&
  38. rdesc[559] == 0x29) {
  39. hid_info(hdev, "fixing up Microsoft Wireless Receiver Model 1028 report descriptor\n");
  40. rdesc[557] = 0x35;
  41. rdesc[559] = 0x45;
  42. }
  43. /* the same as above (s/usage/physical/) */
  44. if ((quirks & MS_RDESC_3K) && *rsize == 106 && rdesc[94] == 0x19 &&
  45. rdesc[95] == 0x00 && rdesc[96] == 0x29 &&
  46. rdesc[97] == 0xff) {
  47. rdesc[94] = 0x35;
  48. rdesc[96] = 0x45;
  49. }
  50. return rdesc;
  51. }
  52. #define ms_map_key_clear(c) hid_map_usage_clear(hi, usage, bit, max, \
  53. EV_KEY, (c))
  54. static int ms_ergonomy_kb_quirk(struct hid_input *hi, struct hid_usage *usage,
  55. unsigned long **bit, int *max)
  56. {
  57. struct input_dev *input = hi->input;
  58. switch (usage->hid & HID_USAGE) {
  59. case 0xfd06: ms_map_key_clear(KEY_CHAT); break;
  60. case 0xfd07: ms_map_key_clear(KEY_PHONE); break;
  61. case 0xff05:
  62. set_bit(EV_REP, input->evbit);
  63. ms_map_key_clear(KEY_F13);
  64. set_bit(KEY_F14, input->keybit);
  65. set_bit(KEY_F15, input->keybit);
  66. set_bit(KEY_F16, input->keybit);
  67. set_bit(KEY_F17, input->keybit);
  68. set_bit(KEY_F18, input->keybit);
  69. default:
  70. return 0;
  71. }
  72. return 1;
  73. }
  74. static int ms_presenter_8k_quirk(struct hid_input *hi, struct hid_usage *usage,
  75. unsigned long **bit, int *max)
  76. {
  77. set_bit(EV_REP, hi->input->evbit);
  78. switch (usage->hid & HID_USAGE) {
  79. case 0xfd08: ms_map_key_clear(KEY_FORWARD); break;
  80. case 0xfd09: ms_map_key_clear(KEY_BACK); break;
  81. case 0xfd0b: ms_map_key_clear(KEY_PLAYPAUSE); break;
  82. case 0xfd0e: ms_map_key_clear(KEY_CLOSE); break;
  83. case 0xfd0f: ms_map_key_clear(KEY_PLAY); break;
  84. default:
  85. return 0;
  86. }
  87. return 1;
  88. }
  89. static int ms_input_mapping(struct hid_device *hdev, struct hid_input *hi,
  90. struct hid_field *field, struct hid_usage *usage,
  91. unsigned long **bit, int *max)
  92. {
  93. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  94. if ((usage->hid & HID_USAGE_PAGE) != HID_UP_MSVENDOR)
  95. return 0;
  96. if (quirks & MS_ERGONOMY) {
  97. int ret = ms_ergonomy_kb_quirk(hi, usage, bit, max);
  98. if (ret)
  99. return ret;
  100. }
  101. if ((quirks & MS_PRESENTER) &&
  102. ms_presenter_8k_quirk(hi, usage, bit, max))
  103. return 1;
  104. return 0;
  105. }
  106. static int ms_input_mapped(struct hid_device *hdev, struct hid_input *hi,
  107. struct hid_field *field, struct hid_usage *usage,
  108. unsigned long **bit, int *max)
  109. {
  110. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  111. if (quirks & MS_DUPLICATE_USAGES)
  112. clear_bit(usage->code, *bit);
  113. return 0;
  114. }
  115. static int ms_event(struct hid_device *hdev, struct hid_field *field,
  116. struct hid_usage *usage, __s32 value)
  117. {
  118. unsigned long quirks = (unsigned long)hid_get_drvdata(hdev);
  119. if (!(hdev->claimed & HID_CLAIMED_INPUT) || !field->hidinput ||
  120. !usage->type)
  121. return 0;
  122. /* Handling MS keyboards special buttons */
  123. if (quirks & MS_ERGONOMY && usage->hid == (HID_UP_MSVENDOR | 0xff05)) {
  124. struct input_dev *input = field->hidinput->input;
  125. static unsigned int last_key = 0;
  126. unsigned int key = 0;
  127. switch (value) {
  128. case 0x01: key = KEY_F14; break;
  129. case 0x02: key = KEY_F15; break;
  130. case 0x04: key = KEY_F16; break;
  131. case 0x08: key = KEY_F17; break;
  132. case 0x10: key = KEY_F18; break;
  133. }
  134. if (key) {
  135. input_event(input, usage->type, key, 1);
  136. last_key = key;
  137. } else
  138. input_event(input, usage->type, last_key, 0);
  139. return 1;
  140. }
  141. return 0;
  142. }
  143. static int ms_probe(struct hid_device *hdev, const struct hid_device_id *id)
  144. {
  145. unsigned long quirks = id->driver_data;
  146. int ret;
  147. hid_set_drvdata(hdev, (void *)quirks);
  148. if (quirks & MS_NOGET)
  149. hdev->quirks |= HID_QUIRK_NOGET;
  150. ret = hid_parse(hdev);
  151. if (ret) {
  152. hid_err(hdev, "parse failed\n");
  153. goto err_free;
  154. }
  155. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT | ((quirks & MS_HIDINPUT) ?
  156. HID_CONNECT_HIDINPUT_FORCE : 0));
  157. if (ret) {
  158. hid_err(hdev, "hw start failed\n");
  159. goto err_free;
  160. }
  161. return 0;
  162. err_free:
  163. return ret;
  164. }
  165. static const struct hid_device_id ms_devices[] = {
  166. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_SIDEWINDER_GV),
  167. .driver_data = MS_HIDINPUT },
  168. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_NE4K),
  169. .driver_data = MS_ERGONOMY },
  170. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_LK6K),
  171. .driver_data = MS_ERGONOMY | MS_RDESC },
  172. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_USB),
  173. .driver_data = MS_PRESENTER },
  174. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_DIGITAL_MEDIA_3K),
  175. .driver_data = MS_ERGONOMY | MS_RDESC_3K },
  176. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_WIRELESS_OPTICAL_DESKTOP_3_0),
  177. .driver_data = MS_NOGET },
  178. { HID_USB_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_COMFORT_MOUSE_4500),
  179. .driver_data = MS_DUPLICATE_USAGES },
  180. { HID_BLUETOOTH_DEVICE(USB_VENDOR_ID_MICROSOFT, USB_DEVICE_ID_MS_PRESENTER_8K_BT),
  181. .driver_data = MS_PRESENTER },
  182. { }
  183. };
  184. MODULE_DEVICE_TABLE(hid, ms_devices);
  185. static struct hid_driver ms_driver = {
  186. .name = "microsoft",
  187. .id_table = ms_devices,
  188. .report_fixup = ms_report_fixup,
  189. .input_mapping = ms_input_mapping,
  190. .input_mapped = ms_input_mapped,
  191. .event = ms_event,
  192. .probe = ms_probe,
  193. };
  194. static int __init ms_init(void)
  195. {
  196. return hid_register_driver(&ms_driver);
  197. }
  198. static void __exit ms_exit(void)
  199. {
  200. hid_unregister_driver(&ms_driver);
  201. }
  202. module_init(ms_init);
  203. module_exit(ms_exit);
  204. MODULE_LICENSE("GPL");