1 // SPDX-License-Identifier: GPL-2.0
3 * When connected to the machine, the Thrustmaster wheels appear as
4 * a «generic» hid gamepad called "Thrustmaster FFB Wheel".
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.
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.
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>
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>
23 * These interrupts are used to prevent a nasty crash when initializing the
24 * T300RS. Used in thrustmaster_interrupts().
26 static const u8 setup_0
[] = { 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
27 static const u8 setup_1
[] = { 0x0a, 0x04, 0x90, 0x03, 0x00, 0x00, 0x00, 0x00 };
28 static const u8 setup_2
[] = { 0x0a, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 };
29 static const u8 setup_3
[] = { 0x0a, 0x04, 0x12, 0x10, 0x00, 0x00, 0x00, 0x00 };
30 static const u8 setup_4
[] = { 0x0a, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 };
31 static const u8
*const setup_arr
[] = { setup_0
, setup_1
, setup_2
, setup_3
, setup_4
};
32 static const unsigned int setup_arr_sizes
[] = {
40 * This struct contains for each type of
43 * Note: The values are stored in the CPU
44 * endianness, the USB protocols always use
45 * little endian; the macro cpu_to_le[BIT]()
46 * must be used when preparing USB packets
49 struct tm_wheel_info
{
53 * See when the USB control out packet is prepared...
54 * @TODO The TMX seems to require multiple control codes to switch.
56 uint16_t switch_value
;
58 char const *const wheel_name
;
63 * Note: TMX does not work as it requires 2 control packets
65 static const struct tm_wheel_info tm_wheels_infos
[] = {
66 {0x0306, 0x0006, "Thrustmaster T150RS"},
67 {0x0200, 0x0005, "Thrustmaster T300RS (Missing Attachment)"},
68 {0x0206, 0x0005, "Thrustmaster T300RS"},
69 {0x0209, 0x0005, "Thrustmaster T300RS (Open Wheel Attachment)"},
70 {0x020a, 0x0005, "Thrustmaster T300RS (Sparco R383 Mod)"},
71 {0x0204, 0x0005, "Thrustmaster T300 Ferrari Alcantara Edition"},
72 {0x0002, 0x0002, "Thrustmaster T500RS"}
73 //{0x0407, 0x0001, "Thrustmaster TMX"}
76 static const uint8_t tm_wheels_infos_length
= 7;
79 * This structs contains (in little endian) the response data
80 * of the wheel to the request 73
82 * A sufficient research to understand what each field does is not
83 * beign conducted yet. The position and meaning of fields are a
84 * just a very optimistic guess based on instinct....
86 struct __packed tm_wheel_response
89 * Seems to be the type of packet
90 * - 0x0049 if is data.a (15 bytes)
91 * - 0x0047 if is data.b (7 bytes)
100 * Seems to be the model code of the wheel
101 * Read table thrustmaster_wheels to values
119 struct usb_device
*usb_dev
;
122 struct usb_ctrlrequest
*model_request
;
123 struct tm_wheel_response
*response
;
125 struct usb_ctrlrequest
*change_request
;
128 /* The control packet to send to wheel */
129 static const struct usb_ctrlrequest model_request
= {
130 .bRequestType
= 0xc1,
134 .wLength
= cpu_to_le16(0x0010)
137 static const struct usb_ctrlrequest change_request
= {
138 .bRequestType
= 0x41,
140 .wValue
= 0, // Will be filled by the driver
146 * On some setups initializing the T300RS crashes the kernel,
147 * these interrupts fix that particular issue. So far they haven't caused any
148 * adverse effects in other wheels.
150 static void thrustmaster_interrupts(struct hid_device
*hdev
)
152 int ret
, trans
, i
, b_ep
;
153 u8
*send_buf
= kmalloc(256, GFP_KERNEL
);
154 struct usb_host_endpoint
*ep
;
155 struct device
*dev
= &hdev
->dev
;
156 struct usb_interface
*usbif
= to_usb_interface(dev
->parent
);
157 struct usb_device
*usbdev
= interface_to_usbdev(usbif
);
160 hid_err(hdev
, "failed allocating send buffer\n");
164 if (usbif
->cur_altsetting
->desc
.bNumEndpoints
< 2) {
166 hid_err(hdev
, "Wrong number of endpoints?\n");
170 ep
= &usbif
->cur_altsetting
->endpoint
[1];
171 b_ep
= ep
->desc
.bEndpointAddress
;
173 for (i
= 0; i
< ARRAY_SIZE(setup_arr
); ++i
) {
174 memcpy(send_buf
, setup_arr
[i
], setup_arr_sizes
[i
]);
176 ret
= usb_interrupt_msg(usbdev
,
177 usb_sndintpipe(usbdev
, b_ep
),
181 USB_CTRL_SET_TIMEOUT
);
184 hid_err(hdev
, "setup data couldn't be sent\n");
193 static void thrustmaster_change_handler(struct urb
*urb
)
195 struct hid_device
*hdev
= urb
->context
;
197 // The wheel seems to kill himself before answering the host and therefore is violating the USB protocol...
198 if (urb
->status
== 0 || urb
->status
== -EPROTO
|| urb
->status
== -EPIPE
)
199 hid_info(hdev
, "Success?! The wheel should have been initialized!\n");
201 hid_warn(hdev
, "URB to change wheel mode seems to have failed with error %d\n", urb
->status
);
205 * Called by the USB subsystem when the wheel responses to our request
206 * to get [what it seems to be] the wheel's model.
208 * If the model id is recognized then we send an opportune USB CONTROL REQUEST
209 * to switch the wheel to its full capabilities
211 static void thrustmaster_model_handler(struct urb
*urb
)
213 struct hid_device
*hdev
= urb
->context
;
214 struct tm_wheel
*tm_wheel
= hid_get_drvdata(hdev
);
217 const struct tm_wheel_info
*twi
= NULL
;
220 hid_err(hdev
, "URB to get model id failed with error %d\n", urb
->status
);
224 if (tm_wheel
->response
->type
== cpu_to_le16(0x49))
225 model
= le16_to_cpu(tm_wheel
->response
->data
.a
.model
);
226 else if (tm_wheel
->response
->type
== cpu_to_le16(0x47))
227 model
= le16_to_cpu(tm_wheel
->response
->data
.b
.model
);
229 hid_err(hdev
, "Unknown packet type 0x%x, unable to proceed further with wheel init\n", tm_wheel
->response
->type
);
233 for (i
= 0; i
< tm_wheels_infos_length
&& !twi
; i
++)
234 if (tm_wheels_infos
[i
].wheel_type
== model
)
235 twi
= tm_wheels_infos
+ i
;
238 hid_info(hdev
, "Wheel with model id 0x%x is a %s\n", model
, twi
->wheel_name
);
240 hid_err(hdev
, "Unknown wheel's model id 0x%x, unable to proceed further with wheel init\n", model
);
244 tm_wheel
->change_request
->wValue
= cpu_to_le16(twi
->switch_value
);
245 usb_fill_control_urb(
248 usb_sndctrlpipe(tm_wheel
->usb_dev
, 0),
249 (char *)tm_wheel
->change_request
,
250 NULL
, 0, // We do not expect any response from the wheel
251 thrustmaster_change_handler
,
255 ret
= usb_submit_urb(tm_wheel
->urb
, GFP_ATOMIC
);
257 hid_err(hdev
, "Error %d while submitting the change URB. I am unable to initialize this wheel...\n", ret
);
260 static void thrustmaster_remove(struct hid_device
*hdev
)
262 struct tm_wheel
*tm_wheel
= hid_get_drvdata(hdev
);
264 usb_kill_urb(tm_wheel
->urb
);
266 kfree(tm_wheel
->change_request
);
267 kfree(tm_wheel
->response
);
268 kfree(tm_wheel
->model_request
);
269 usb_free_urb(tm_wheel
->urb
);
276 * Function called by HID when a hid Thrustmaster FFB wheel is connected to the host.
277 * This function starts the hid dev, tries to allocate the tm_wheel data structure and
278 * finally send an USB CONTROL REQUEST to the wheel to get [what it seems to be] its
281 static int thrustmaster_probe(struct hid_device
*hdev
, const struct hid_device_id
*id
)
284 struct tm_wheel
*tm_wheel
= NULL
;
286 if (!hid_is_usb(hdev
))
289 ret
= hid_parse(hdev
);
291 hid_err(hdev
, "parse failed with error %d\n", ret
);
295 ret
= hid_hw_start(hdev
, HID_CONNECT_DEFAULT
& ~HID_CONNECT_FF
);
297 hid_err(hdev
, "hw start failed with error %d\n", ret
);
301 // Now we allocate the tm_wheel
302 tm_wheel
= kzalloc(sizeof(struct tm_wheel
), GFP_KERNEL
);
308 tm_wheel
->urb
= usb_alloc_urb(0, GFP_ATOMIC
);
309 if (!tm_wheel
->urb
) {
314 tm_wheel
->model_request
= kmemdup(&model_request
,
315 sizeof(struct usb_ctrlrequest
),
317 if (!tm_wheel
->model_request
) {
322 tm_wheel
->response
= kzalloc(sizeof(struct tm_wheel_response
), GFP_KERNEL
);
323 if (!tm_wheel
->response
) {
328 tm_wheel
->change_request
= kmemdup(&change_request
,
329 sizeof(struct usb_ctrlrequest
),
331 if (!tm_wheel
->change_request
) {
336 tm_wheel
->usb_dev
= interface_to_usbdev(to_usb_interface(hdev
->dev
.parent
));
337 hid_set_drvdata(hdev
, tm_wheel
);
339 thrustmaster_interrupts(hdev
);
341 usb_fill_control_urb(
344 usb_rcvctrlpipe(tm_wheel
->usb_dev
, 0),
345 (char *)tm_wheel
->model_request
,
347 sizeof(struct tm_wheel_response
),
348 thrustmaster_model_handler
,
352 ret
= usb_submit_urb(tm_wheel
->urb
, GFP_ATOMIC
);
354 hid_err(hdev
, "Error %d while submitting the URB. I am unable to initialize this wheel...\n", ret
);
360 error6
: kfree(tm_wheel
->change_request
);
361 error5
: kfree(tm_wheel
->response
);
362 error4
: kfree(tm_wheel
->model_request
);
363 error3
: usb_free_urb(tm_wheel
->urb
);
364 error2
: kfree(tm_wheel
);
365 error1
: hid_hw_stop(hdev
);
370 static const struct hid_device_id thrustmaster_devices
[] = {
371 { HID_USB_DEVICE(0x044f, 0xb65d)},
375 MODULE_DEVICE_TABLE(hid
, thrustmaster_devices
);
377 static struct hid_driver thrustmaster_driver
= {
378 .name
= "hid-thrustmaster",
379 .id_table
= thrustmaster_devices
,
380 .probe
= thrustmaster_probe
,
381 .remove
= thrustmaster_remove
,
384 module_hid_driver(thrustmaster_driver
);
386 MODULE_AUTHOR("Dario Pagani <dario.pagani.146+linuxk@gmail.com>");
387 MODULE_LICENSE("GPL");
388 MODULE_DESCRIPTION("Driver to initialize some steering wheel joysticks from Thrustmaster");