hid-thrustmaster.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * When connected to the machine, the Thrustmaster wheels appear as
  4. * a «generic» hid gamepad called "Thrustmaster FFB Wheel".
  5. *
  6. * When in this mode not every functionality of the wheel, like the force feedback,
  7. * are available. To enable all functionalities of a Thrustmaster wheel we have to send
  8. * to it a specific USB CONTROL request with a code different for each wheel.
  9. *
  10. * This driver tries to understand which model of Thrustmaster wheel the generic
  11. * "Thrustmaster FFB Wheel" really is and then sends the appropriate control code.
  12. *
  13. * Copyright (c) 2020-2021 Dario Pagani <[email protected]>
  14. * Copyright (c) 2020-2021 Kim Kuparinen <[email protected]>
  15. */
  16. #include <linux/hid.h>
  17. #include <linux/usb.h>
  18. #include <linux/input.h>
  19. #include <linux/slab.h>
  20. #include <linux/module.h>
  21. /*
  22. * These interrupts are used to prevent a nasty crash when initializing the
  23. * T300RS. Used in thrustmaster_interrupts().
  24. */
  25. static const u8 setup_0[] = { 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
  26. static const u8 setup_1[] = { 0x0a, 0x04, 0x90, 0x03, 0x00, 0x00, 0x00, 0x00 };
  27. static const u8 setup_2[] = { 0x0a, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 };
  28. static const u8 setup_3[] = { 0x0a, 0x04, 0x12, 0x10, 0x00, 0x00, 0x00, 0x00 };
  29. static const u8 setup_4[] = { 0x0a, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 };
  30. static const u8 *const setup_arr[] = { setup_0, setup_1, setup_2, setup_3, setup_4 };
  31. static const unsigned int setup_arr_sizes[] = {
  32. ARRAY_SIZE(setup_0),
  33. ARRAY_SIZE(setup_1),
  34. ARRAY_SIZE(setup_2),
  35. ARRAY_SIZE(setup_3),
  36. ARRAY_SIZE(setup_4)
  37. };
  38. /*
  39. * This struct contains for each type of
  40. * Thrustmaster wheel
  41. *
  42. * Note: The values are stored in the CPU
  43. * endianness, the USB protocols always use
  44. * little endian; the macro cpu_to_le[BIT]()
  45. * must be used when preparing USB packets
  46. * and vice-versa
  47. */
  48. struct tm_wheel_info {
  49. uint16_t wheel_type;
  50. /*
  51. * See when the USB control out packet is prepared...
  52. * @TODO The TMX seems to require multiple control codes to switch.
  53. */
  54. uint16_t switch_value;
  55. char const *const wheel_name;
  56. };
  57. /*
  58. * Known wheels.
  59. * Note: TMX does not work as it requires 2 control packets
  60. */
  61. static const struct tm_wheel_info tm_wheels_infos[] = {
  62. {0x0306, 0x0006, "Thrustmaster T150RS"},
  63. {0x0200, 0x0005, "Thrustmaster T300RS (Missing Attachment)"},
  64. {0x0206, 0x0005, "Thrustmaster T300RS"},
  65. {0x0209, 0x0005, "Thrustmaster T300RS (Open Wheel Attachment)"},
  66. {0x020a, 0x0005, "Thrustmaster T300RS (Sparco R383 Mod)"},
  67. {0x0204, 0x0005, "Thrustmaster T300 Ferrari Alcantara Edition"},
  68. {0x0002, 0x0002, "Thrustmaster T500RS"}
  69. //{0x0407, 0x0001, "Thrustmaster TMX"}
  70. };
  71. static const uint8_t tm_wheels_infos_length = 7;
  72. /*
  73. * This structs contains (in little endian) the response data
  74. * of the wheel to the request 73
  75. *
  76. * A sufficient research to understand what each field does is not
  77. * beign conducted yet. The position and meaning of fields are a
  78. * just a very optimistic guess based on instinct....
  79. */
  80. struct __packed tm_wheel_response
  81. {
  82. /*
  83. * Seems to be the type of packet
  84. * - 0x0049 if is data.a (15 bytes)
  85. * - 0x0047 if is data.b (7 bytes)
  86. */
  87. uint16_t type;
  88. union {
  89. struct __packed {
  90. uint16_t field0;
  91. uint16_t field1;
  92. /*
  93. * Seems to be the model code of the wheel
  94. * Read table thrustmaster_wheels to values
  95. */
  96. uint16_t model;
  97. uint16_t field2;
  98. uint16_t field3;
  99. uint16_t field4;
  100. uint16_t field5;
  101. } a;
  102. struct __packed {
  103. uint16_t field0;
  104. uint16_t field1;
  105. uint16_t model;
  106. } b;
  107. } data;
  108. };
  109. struct tm_wheel {
  110. struct usb_device *usb_dev;
  111. struct urb *urb;
  112. struct usb_ctrlrequest *model_request;
  113. struct tm_wheel_response *response;
  114. struct usb_ctrlrequest *change_request;
  115. };
  116. /* The control packet to send to wheel */
  117. static const struct usb_ctrlrequest model_request = {
  118. .bRequestType = 0xc1,
  119. .bRequest = 73,
  120. .wValue = 0,
  121. .wIndex = 0,
  122. .wLength = cpu_to_le16(0x0010)
  123. };
  124. static const struct usb_ctrlrequest change_request = {
  125. .bRequestType = 0x41,
  126. .bRequest = 83,
  127. .wValue = 0, // Will be filled by the driver
  128. .wIndex = 0,
  129. .wLength = 0
  130. };
  131. /*
  132. * On some setups initializing the T300RS crashes the kernel,
  133. * these interrupts fix that particular issue. So far they haven't caused any
  134. * adverse effects in other wheels.
  135. */
  136. static void thrustmaster_interrupts(struct hid_device *hdev)
  137. {
  138. int ret, trans, i, b_ep;
  139. u8 *send_buf = kmalloc(256, GFP_KERNEL);
  140. struct usb_host_endpoint *ep;
  141. struct device *dev = &hdev->dev;
  142. struct usb_interface *usbif = to_usb_interface(dev->parent);
  143. struct usb_device *usbdev = interface_to_usbdev(usbif);
  144. if (!send_buf) {
  145. hid_err(hdev, "failed allocating send buffer\n");
  146. return;
  147. }
  148. if (usbif->cur_altsetting->desc.bNumEndpoints < 2) {
  149. kfree(send_buf);
  150. hid_err(hdev, "Wrong number of endpoints?\n");
  151. return;
  152. }
  153. ep = &usbif->cur_altsetting->endpoint[1];
  154. b_ep = ep->desc.bEndpointAddress;
  155. for (i = 0; i < ARRAY_SIZE(setup_arr); ++i) {
  156. memcpy(send_buf, setup_arr[i], setup_arr_sizes[i]);
  157. ret = usb_interrupt_msg(usbdev,
  158. usb_sndintpipe(usbdev, b_ep),
  159. send_buf,
  160. setup_arr_sizes[i],
  161. &trans,
  162. USB_CTRL_SET_TIMEOUT);
  163. if (ret) {
  164. hid_err(hdev, "setup data couldn't be sent\n");
  165. kfree(send_buf);
  166. return;
  167. }
  168. }
  169. kfree(send_buf);
  170. }
  171. static void thrustmaster_change_handler(struct urb *urb)
  172. {
  173. struct hid_device *hdev = urb->context;
  174. // The wheel seems to kill himself before answering the host and therefore is violating the USB protocol...
  175. if (urb->status == 0 || urb->status == -EPROTO || urb->status == -EPIPE)
  176. hid_info(hdev, "Success?! The wheel should have been initialized!\n");
  177. else
  178. hid_warn(hdev, "URB to change wheel mode seems to have failed with error %d\n", urb->status);
  179. }
  180. /*
  181. * Called by the USB subsystem when the wheel responses to our request
  182. * to get [what it seems to be] the wheel's model.
  183. *
  184. * If the model id is recognized then we send an opportune USB CONTROL REQUEST
  185. * to switch the wheel to its full capabilities
  186. */
  187. static void thrustmaster_model_handler(struct urb *urb)
  188. {
  189. struct hid_device *hdev = urb->context;
  190. struct tm_wheel *tm_wheel = hid_get_drvdata(hdev);
  191. uint16_t model = 0;
  192. int i, ret;
  193. const struct tm_wheel_info *twi = NULL;
  194. if (urb->status) {
  195. hid_err(hdev, "URB to get model id failed with error %d\n", urb->status);
  196. return;
  197. }
  198. if (tm_wheel->response->type == cpu_to_le16(0x49))
  199. model = le16_to_cpu(tm_wheel->response->data.a.model);
  200. else if (tm_wheel->response->type == cpu_to_le16(0x47))
  201. model = le16_to_cpu(tm_wheel->response->data.b.model);
  202. else {
  203. hid_err(hdev, "Unknown packet type 0x%x, unable to proceed further with wheel init\n", tm_wheel->response->type);
  204. return;
  205. }
  206. for (i = 0; i < tm_wheels_infos_length && !twi; i++)
  207. if (tm_wheels_infos[i].wheel_type == model)
  208. twi = tm_wheels_infos + i;
  209. if (twi)
  210. hid_info(hdev, "Wheel with model id 0x%x is a %s\n", model, twi->wheel_name);
  211. else {
  212. hid_err(hdev, "Unknown wheel's model id 0x%x, unable to proceed further with wheel init\n", model);
  213. return;
  214. }
  215. tm_wheel->change_request->wValue = cpu_to_le16(twi->switch_value);
  216. usb_fill_control_urb(
  217. tm_wheel->urb,
  218. tm_wheel->usb_dev,
  219. usb_sndctrlpipe(tm_wheel->usb_dev, 0),
  220. (char *)tm_wheel->change_request,
  221. NULL, 0, // We do not expect any response from the wheel
  222. thrustmaster_change_handler,
  223. hdev
  224. );
  225. ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC);
  226. if (ret)
  227. hid_err(hdev, "Error %d while submitting the change URB. I am unable to initialize this wheel...\n", ret);
  228. }
  229. static void thrustmaster_remove(struct hid_device *hdev)
  230. {
  231. struct tm_wheel *tm_wheel = hid_get_drvdata(hdev);
  232. usb_kill_urb(tm_wheel->urb);
  233. kfree(tm_wheel->change_request);
  234. kfree(tm_wheel->response);
  235. kfree(tm_wheel->model_request);
  236. usb_free_urb(tm_wheel->urb);
  237. kfree(tm_wheel);
  238. hid_hw_stop(hdev);
  239. }
  240. /*
  241. * Function called by HID when a hid Thrustmaster FFB wheel is connected to the host.
  242. * This function starts the hid dev, tries to allocate the tm_wheel data structure and
  243. * finally send an USB CONTROL REQUEST to the wheel to get [what it seems to be] its
  244. * model type.
  245. */
  246. static int thrustmaster_probe(struct hid_device *hdev, const struct hid_device_id *id)
  247. {
  248. int ret = 0;
  249. struct tm_wheel *tm_wheel = NULL;
  250. if (!hid_is_usb(hdev))
  251. return -EINVAL;
  252. ret = hid_parse(hdev);
  253. if (ret) {
  254. hid_err(hdev, "parse failed with error %d\n", ret);
  255. goto error0;
  256. }
  257. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  258. if (ret) {
  259. hid_err(hdev, "hw start failed with error %d\n", ret);
  260. goto error0;
  261. }
  262. // Now we allocate the tm_wheel
  263. tm_wheel = kzalloc(sizeof(struct tm_wheel), GFP_KERNEL);
  264. if (!tm_wheel) {
  265. ret = -ENOMEM;
  266. goto error1;
  267. }
  268. tm_wheel->urb = usb_alloc_urb(0, GFP_ATOMIC);
  269. if (!tm_wheel->urb) {
  270. ret = -ENOMEM;
  271. goto error2;
  272. }
  273. tm_wheel->model_request = kmemdup(&model_request,
  274. sizeof(struct usb_ctrlrequest),
  275. GFP_KERNEL);
  276. if (!tm_wheel->model_request) {
  277. ret = -ENOMEM;
  278. goto error3;
  279. }
  280. tm_wheel->response = kzalloc(sizeof(struct tm_wheel_response), GFP_KERNEL);
  281. if (!tm_wheel->response) {
  282. ret = -ENOMEM;
  283. goto error4;
  284. }
  285. tm_wheel->change_request = kmemdup(&change_request,
  286. sizeof(struct usb_ctrlrequest),
  287. GFP_KERNEL);
  288. if (!tm_wheel->change_request) {
  289. ret = -ENOMEM;
  290. goto error5;
  291. }
  292. tm_wheel->usb_dev = interface_to_usbdev(to_usb_interface(hdev->dev.parent));
  293. hid_set_drvdata(hdev, tm_wheel);
  294. thrustmaster_interrupts(hdev);
  295. usb_fill_control_urb(
  296. tm_wheel->urb,
  297. tm_wheel->usb_dev,
  298. usb_rcvctrlpipe(tm_wheel->usb_dev, 0),
  299. (char *)tm_wheel->model_request,
  300. tm_wheel->response,
  301. sizeof(struct tm_wheel_response),
  302. thrustmaster_model_handler,
  303. hdev
  304. );
  305. ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC);
  306. if (ret) {
  307. hid_err(hdev, "Error %d while submitting the URB. I am unable to initialize this wheel...\n", ret);
  308. goto error6;
  309. }
  310. return ret;
  311. error6: kfree(tm_wheel->change_request);
  312. error5: kfree(tm_wheel->response);
  313. error4: kfree(tm_wheel->model_request);
  314. error3: usb_free_urb(tm_wheel->urb);
  315. error2: kfree(tm_wheel);
  316. error1: hid_hw_stop(hdev);
  317. error0:
  318. return ret;
  319. }
  320. static const struct hid_device_id thrustmaster_devices[] = {
  321. { HID_USB_DEVICE(0x044f, 0xb65d)},
  322. {}
  323. };
  324. MODULE_DEVICE_TABLE(hid, thrustmaster_devices);
  325. static struct hid_driver thrustmaster_driver = {
  326. .name = "hid-thrustmaster",
  327. .id_table = thrustmaster_devices,
  328. .probe = thrustmaster_probe,
  329. .remove = thrustmaster_remove,
  330. };
  331. module_hid_driver(thrustmaster_driver);
  332. MODULE_AUTHOR("Dario Pagani <[email protected]>");
  333. MODULE_LICENSE("GPL");
  334. MODULE_DESCRIPTION("Driver to initialize some steering wheel joysticks from Thrustmaster");