Linux Audio

Check our new training course

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