hid-thrustmaster.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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 <dario.pagani.146+linuxk@gmail.com>
  14. * Copyright (c) 2020-2021 Kim Kuparinen <kimi.h.kuparinen@gmail.com>
  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. /* Are the expected endpoints present? */
  156. u8 ep_addr[2] = {b_ep, 0};
  157. if (!usb_check_int_endpoints(usbif, ep_addr)) {
  158. kfree(send_buf);
  159. hid_err(hdev, "Unexpected non-int endpoint\n");
  160. return;
  161. }
  162. for (i = 0; i < ARRAY_SIZE(setup_arr); ++i) {
  163. memcpy(send_buf, setup_arr[i], setup_arr_sizes[i]);
  164. ret = usb_interrupt_msg(usbdev,
  165. usb_sndintpipe(usbdev, b_ep),
  166. send_buf,
  167. setup_arr_sizes[i],
  168. &trans,
  169. USB_CTRL_SET_TIMEOUT);
  170. if (ret) {
  171. hid_err(hdev, "setup data couldn't be sent\n");
  172. kfree(send_buf);
  173. return;
  174. }
  175. }
  176. kfree(send_buf);
  177. }
  178. static void thrustmaster_change_handler(struct urb *urb)
  179. {
  180. struct hid_device *hdev = urb->context;
  181. // The wheel seems to kill himself before answering the host and therefore is violating the USB protocol...
  182. if (urb->status == 0 || urb->status == -EPROTO || urb->status == -EPIPE)
  183. hid_info(hdev, "Success?! The wheel should have been initialized!\n");
  184. else
  185. hid_warn(hdev, "URB to change wheel mode seems to have failed with error %d\n", urb->status);
  186. }
  187. /*
  188. * Called by the USB subsystem when the wheel responses to our request
  189. * to get [what it seems to be] the wheel's model.
  190. *
  191. * If the model id is recognized then we send an opportune USB CONTROL REQUEST
  192. * to switch the wheel to its full capabilities
  193. */
  194. static void thrustmaster_model_handler(struct urb *urb)
  195. {
  196. struct hid_device *hdev = urb->context;
  197. struct tm_wheel *tm_wheel = hid_get_drvdata(hdev);
  198. uint16_t model = 0;
  199. int i, ret;
  200. const struct tm_wheel_info *twi = NULL;
  201. if (urb->status) {
  202. hid_err(hdev, "URB to get model id failed with error %d\n", urb->status);
  203. return;
  204. }
  205. if (tm_wheel->response->type == cpu_to_le16(0x49))
  206. model = le16_to_cpu(tm_wheel->response->data.a.model);
  207. else if (tm_wheel->response->type == cpu_to_le16(0x47))
  208. model = le16_to_cpu(tm_wheel->response->data.b.model);
  209. else {
  210. hid_err(hdev, "Unknown packet type 0x%x, unable to proceed further with wheel init\n", tm_wheel->response->type);
  211. return;
  212. }
  213. for (i = 0; i < tm_wheels_infos_length && !twi; i++)
  214. if (tm_wheels_infos[i].wheel_type == model)
  215. twi = tm_wheels_infos + i;
  216. if (twi)
  217. hid_info(hdev, "Wheel with model id 0x%x is a %s\n", model, twi->wheel_name);
  218. else {
  219. hid_err(hdev, "Unknown wheel's model id 0x%x, unable to proceed further with wheel init\n", model);
  220. return;
  221. }
  222. tm_wheel->change_request->wValue = cpu_to_le16(twi->switch_value);
  223. usb_fill_control_urb(
  224. tm_wheel->urb,
  225. tm_wheel->usb_dev,
  226. usb_sndctrlpipe(tm_wheel->usb_dev, 0),
  227. (char *)tm_wheel->change_request,
  228. NULL, 0, // We do not expect any response from the wheel
  229. thrustmaster_change_handler,
  230. hdev
  231. );
  232. ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC);
  233. if (ret)
  234. hid_err(hdev, "Error %d while submitting the change URB. I am unable to initialize this wheel...\n", ret);
  235. }
  236. static void thrustmaster_remove(struct hid_device *hdev)
  237. {
  238. struct tm_wheel *tm_wheel = hid_get_drvdata(hdev);
  239. usb_kill_urb(tm_wheel->urb);
  240. kfree(tm_wheel->change_request);
  241. kfree(tm_wheel->response);
  242. kfree(tm_wheel->model_request);
  243. usb_free_urb(tm_wheel->urb);
  244. kfree(tm_wheel);
  245. hid_hw_stop(hdev);
  246. }
  247. /*
  248. * Function called by HID when a hid Thrustmaster FFB wheel is connected to the host.
  249. * This function starts the hid dev, tries to allocate the tm_wheel data structure and
  250. * finally send an USB CONTROL REQUEST to the wheel to get [what it seems to be] its
  251. * model type.
  252. */
  253. static int thrustmaster_probe(struct hid_device *hdev, const struct hid_device_id *id)
  254. {
  255. int ret = 0;
  256. struct tm_wheel *tm_wheel = NULL;
  257. if (!hid_is_usb(hdev))
  258. return -EINVAL;
  259. ret = hid_parse(hdev);
  260. if (ret) {
  261. hid_err(hdev, "parse failed with error %d\n", ret);
  262. goto error0;
  263. }
  264. ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
  265. if (ret) {
  266. hid_err(hdev, "hw start failed with error %d\n", ret);
  267. goto error0;
  268. }
  269. // Now we allocate the tm_wheel
  270. tm_wheel = kzalloc_obj(struct tm_wheel);
  271. if (!tm_wheel) {
  272. ret = -ENOMEM;
  273. goto error1;
  274. }
  275. tm_wheel->urb = usb_alloc_urb(0, GFP_ATOMIC);
  276. if (!tm_wheel->urb) {
  277. ret = -ENOMEM;
  278. goto error2;
  279. }
  280. tm_wheel->model_request = kmemdup(&model_request,
  281. sizeof(struct usb_ctrlrequest),
  282. GFP_KERNEL);
  283. if (!tm_wheel->model_request) {
  284. ret = -ENOMEM;
  285. goto error3;
  286. }
  287. tm_wheel->response = kzalloc_obj(struct tm_wheel_response);
  288. if (!tm_wheel->response) {
  289. ret = -ENOMEM;
  290. goto error4;
  291. }
  292. tm_wheel->change_request = kmemdup(&change_request,
  293. sizeof(struct usb_ctrlrequest),
  294. GFP_KERNEL);
  295. if (!tm_wheel->change_request) {
  296. ret = -ENOMEM;
  297. goto error5;
  298. }
  299. tm_wheel->usb_dev = interface_to_usbdev(to_usb_interface(hdev->dev.parent));
  300. hid_set_drvdata(hdev, tm_wheel);
  301. thrustmaster_interrupts(hdev);
  302. usb_fill_control_urb(
  303. tm_wheel->urb,
  304. tm_wheel->usb_dev,
  305. usb_rcvctrlpipe(tm_wheel->usb_dev, 0),
  306. (char *)tm_wheel->model_request,
  307. tm_wheel->response,
  308. sizeof(struct tm_wheel_response),
  309. thrustmaster_model_handler,
  310. hdev
  311. );
  312. ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC);
  313. if (ret) {
  314. hid_err(hdev, "Error %d while submitting the URB. I am unable to initialize this wheel...\n", ret);
  315. goto error6;
  316. }
  317. return ret;
  318. error6: kfree(tm_wheel->change_request);
  319. error5: kfree(tm_wheel->response);
  320. error4: kfree(tm_wheel->model_request);
  321. error3: usb_free_urb(tm_wheel->urb);
  322. error2: kfree(tm_wheel);
  323. error1: hid_hw_stop(hdev);
  324. error0:
  325. return ret;
  326. }
  327. static const struct hid_device_id thrustmaster_devices[] = {
  328. { HID_USB_DEVICE(0x044f, 0xb65d)},
  329. {}
  330. };
  331. MODULE_DEVICE_TABLE(hid, thrustmaster_devices);
  332. static struct hid_driver thrustmaster_driver = {
  333. .name = "hid-thrustmaster",
  334. .id_table = thrustmaster_devices,
  335. .probe = thrustmaster_probe,
  336. .remove = thrustmaster_remove,
  337. };
  338. module_hid_driver(thrustmaster_driver);
  339. MODULE_AUTHOR("Dario Pagani <dario.pagani.146+linuxk@gmail.com>");
  340. MODULE_LICENSE("GPL");
  341. MODULE_DESCRIPTION("Driver to initialize some steering wheel joysticks from Thrustmaster");