0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016 #include <linux/hid.h>
0017 #include <linux/usb.h>
0018 #include <linux/input.h>
0019 #include <linux/slab.h>
0020 #include <linux/module.h>
0021
0022
0023
0024
0025
0026 static const u8 setup_0[] = { 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
0027 static const u8 setup_1[] = { 0x0a, 0x04, 0x90, 0x03, 0x00, 0x00, 0x00, 0x00 };
0028 static const u8 setup_2[] = { 0x0a, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 };
0029 static const u8 setup_3[] = { 0x0a, 0x04, 0x12, 0x10, 0x00, 0x00, 0x00, 0x00 };
0030 static const u8 setup_4[] = { 0x0a, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 };
0031 static const u8 *const setup_arr[] = { setup_0, setup_1, setup_2, setup_3, setup_4 };
0032 static const unsigned int setup_arr_sizes[] = {
0033 ARRAY_SIZE(setup_0),
0034 ARRAY_SIZE(setup_1),
0035 ARRAY_SIZE(setup_2),
0036 ARRAY_SIZE(setup_3),
0037 ARRAY_SIZE(setup_4)
0038 };
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049 struct tm_wheel_info {
0050 uint16_t wheel_type;
0051
0052
0053
0054
0055
0056 uint16_t switch_value;
0057
0058 char const *const wheel_name;
0059 };
0060
0061
0062
0063
0064
0065 static const struct tm_wheel_info tm_wheels_infos[] = {
0066 {0x0306, 0x0006, "Thrustmaster T150RS"},
0067 {0x0200, 0x0005, "Thrustmaster T300RS (Missing Attachment)"},
0068 {0x0206, 0x0005, "Thrustmaster T300RS"},
0069 {0x0209, 0x0005, "Thrustmaster T300RS (Open Wheel Attachment)"},
0070 {0x020a, 0x0005, "Thrustmaster T300RS (Sparco R383 Mod)"},
0071 {0x0204, 0x0005, "Thrustmaster T300 Ferrari Alcantara Edition"},
0072 {0x0002, 0x0002, "Thrustmaster T500RS"}
0073
0074 };
0075
0076 static const uint8_t tm_wheels_infos_length = 7;
0077
0078
0079
0080
0081
0082
0083
0084
0085
0086 struct __packed tm_wheel_response
0087 {
0088
0089
0090
0091
0092
0093 uint16_t type;
0094
0095 union {
0096 struct __packed {
0097 uint16_t field0;
0098 uint16_t field1;
0099
0100
0101
0102
0103 uint16_t model;
0104
0105 uint16_t field2;
0106 uint16_t field3;
0107 uint16_t field4;
0108 uint16_t field5;
0109 } a;
0110 struct __packed {
0111 uint16_t field0;
0112 uint16_t field1;
0113 uint16_t model;
0114 } b;
0115 } data;
0116 };
0117
0118 struct tm_wheel {
0119 struct usb_device *usb_dev;
0120 struct urb *urb;
0121
0122 struct usb_ctrlrequest *model_request;
0123 struct tm_wheel_response *response;
0124
0125 struct usb_ctrlrequest *change_request;
0126 };
0127
0128
0129 static const struct usb_ctrlrequest model_request = {
0130 .bRequestType = 0xc1,
0131 .bRequest = 73,
0132 .wValue = 0,
0133 .wIndex = 0,
0134 .wLength = cpu_to_le16(0x0010)
0135 };
0136
0137 static const struct usb_ctrlrequest change_request = {
0138 .bRequestType = 0x41,
0139 .bRequest = 83,
0140 .wValue = 0,
0141 .wIndex = 0,
0142 .wLength = 0
0143 };
0144
0145
0146
0147
0148
0149
0150 static void thrustmaster_interrupts(struct hid_device *hdev)
0151 {
0152 int ret, trans, i, b_ep;
0153 u8 *send_buf = kmalloc(256, GFP_KERNEL);
0154 struct usb_host_endpoint *ep;
0155 struct device *dev = &hdev->dev;
0156 struct usb_interface *usbif = to_usb_interface(dev->parent);
0157 struct usb_device *usbdev = interface_to_usbdev(usbif);
0158
0159 if (!send_buf) {
0160 hid_err(hdev, "failed allocating send buffer\n");
0161 return;
0162 }
0163
0164 if (usbif->cur_altsetting->desc.bNumEndpoints < 2) {
0165 kfree(send_buf);
0166 hid_err(hdev, "Wrong number of endpoints?\n");
0167 return;
0168 }
0169
0170 ep = &usbif->cur_altsetting->endpoint[1];
0171 b_ep = ep->desc.bEndpointAddress;
0172
0173 for (i = 0; i < ARRAY_SIZE(setup_arr); ++i) {
0174 memcpy(send_buf, setup_arr[i], setup_arr_sizes[i]);
0175
0176 ret = usb_interrupt_msg(usbdev,
0177 usb_sndintpipe(usbdev, b_ep),
0178 send_buf,
0179 setup_arr_sizes[i],
0180 &trans,
0181 USB_CTRL_SET_TIMEOUT);
0182
0183 if (ret) {
0184 hid_err(hdev, "setup data couldn't be sent\n");
0185 kfree(send_buf);
0186 return;
0187 }
0188 }
0189
0190 kfree(send_buf);
0191 }
0192
0193 static void thrustmaster_change_handler(struct urb *urb)
0194 {
0195 struct hid_device *hdev = urb->context;
0196
0197
0198 if (urb->status == 0 || urb->status == -EPROTO || urb->status == -EPIPE)
0199 hid_info(hdev, "Success?! The wheel should have been initialized!\n");
0200 else
0201 hid_warn(hdev, "URB to change wheel mode seems to have failed with error %d\n", urb->status);
0202 }
0203
0204
0205
0206
0207
0208
0209
0210
0211 static void thrustmaster_model_handler(struct urb *urb)
0212 {
0213 struct hid_device *hdev = urb->context;
0214 struct tm_wheel *tm_wheel = hid_get_drvdata(hdev);
0215 uint16_t model = 0;
0216 int i, ret;
0217 const struct tm_wheel_info *twi = NULL;
0218
0219 if (urb->status) {
0220 hid_err(hdev, "URB to get model id failed with error %d\n", urb->status);
0221 return;
0222 }
0223
0224 if (tm_wheel->response->type == cpu_to_le16(0x49))
0225 model = le16_to_cpu(tm_wheel->response->data.a.model);
0226 else if (tm_wheel->response->type == cpu_to_le16(0x47))
0227 model = le16_to_cpu(tm_wheel->response->data.b.model);
0228 else {
0229 hid_err(hdev, "Unknown packet type 0x%x, unable to proceed further with wheel init\n", tm_wheel->response->type);
0230 return;
0231 }
0232
0233 for (i = 0; i < tm_wheels_infos_length && !twi; i++)
0234 if (tm_wheels_infos[i].wheel_type == model)
0235 twi = tm_wheels_infos + i;
0236
0237 if (twi)
0238 hid_info(hdev, "Wheel with model id 0x%x is a %s\n", model, twi->wheel_name);
0239 else {
0240 hid_err(hdev, "Unknown wheel's model id 0x%x, unable to proceed further with wheel init\n", model);
0241 return;
0242 }
0243
0244 tm_wheel->change_request->wValue = cpu_to_le16(twi->switch_value);
0245 usb_fill_control_urb(
0246 tm_wheel->urb,
0247 tm_wheel->usb_dev,
0248 usb_sndctrlpipe(tm_wheel->usb_dev, 0),
0249 (char *)tm_wheel->change_request,
0250 NULL, 0,
0251 thrustmaster_change_handler,
0252 hdev
0253 );
0254
0255 ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC);
0256 if (ret)
0257 hid_err(hdev, "Error %d while submitting the change URB. I am unable to initialize this wheel...\n", ret);
0258 }
0259
0260 static void thrustmaster_remove(struct hid_device *hdev)
0261 {
0262 struct tm_wheel *tm_wheel = hid_get_drvdata(hdev);
0263
0264 usb_kill_urb(tm_wheel->urb);
0265
0266 kfree(tm_wheel->change_request);
0267 kfree(tm_wheel->response);
0268 kfree(tm_wheel->model_request);
0269 usb_free_urb(tm_wheel->urb);
0270 kfree(tm_wheel);
0271
0272 hid_hw_stop(hdev);
0273 }
0274
0275
0276
0277
0278
0279
0280
0281 static int thrustmaster_probe(struct hid_device *hdev, const struct hid_device_id *id)
0282 {
0283 int ret = 0;
0284 struct tm_wheel *tm_wheel = NULL;
0285
0286 if (!hid_is_usb(hdev))
0287 return -EINVAL;
0288
0289 ret = hid_parse(hdev);
0290 if (ret) {
0291 hid_err(hdev, "parse failed with error %d\n", ret);
0292 goto error0;
0293 }
0294
0295 ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
0296 if (ret) {
0297 hid_err(hdev, "hw start failed with error %d\n", ret);
0298 goto error0;
0299 }
0300
0301
0302 tm_wheel = kzalloc(sizeof(struct tm_wheel), GFP_KERNEL);
0303 if (!tm_wheel) {
0304 ret = -ENOMEM;
0305 goto error1;
0306 }
0307
0308 tm_wheel->urb = usb_alloc_urb(0, GFP_ATOMIC);
0309 if (!tm_wheel->urb) {
0310 ret = -ENOMEM;
0311 goto error2;
0312 }
0313
0314 tm_wheel->model_request = kmemdup(&model_request,
0315 sizeof(struct usb_ctrlrequest),
0316 GFP_KERNEL);
0317 if (!tm_wheel->model_request) {
0318 ret = -ENOMEM;
0319 goto error3;
0320 }
0321
0322 tm_wheel->response = kzalloc(sizeof(struct tm_wheel_response), GFP_KERNEL);
0323 if (!tm_wheel->response) {
0324 ret = -ENOMEM;
0325 goto error4;
0326 }
0327
0328 tm_wheel->change_request = kmemdup(&change_request,
0329 sizeof(struct usb_ctrlrequest),
0330 GFP_KERNEL);
0331 if (!tm_wheel->change_request) {
0332 ret = -ENOMEM;
0333 goto error5;
0334 }
0335
0336 tm_wheel->usb_dev = interface_to_usbdev(to_usb_interface(hdev->dev.parent));
0337 hid_set_drvdata(hdev, tm_wheel);
0338
0339 thrustmaster_interrupts(hdev);
0340
0341 usb_fill_control_urb(
0342 tm_wheel->urb,
0343 tm_wheel->usb_dev,
0344 usb_rcvctrlpipe(tm_wheel->usb_dev, 0),
0345 (char *)tm_wheel->model_request,
0346 tm_wheel->response,
0347 sizeof(struct tm_wheel_response),
0348 thrustmaster_model_handler,
0349 hdev
0350 );
0351
0352 ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC);
0353 if (ret) {
0354 hid_err(hdev, "Error %d while submitting the URB. I am unable to initialize this wheel...\n", ret);
0355 goto error6;
0356 }
0357
0358 return ret;
0359
0360 error6: kfree(tm_wheel->change_request);
0361 error5: kfree(tm_wheel->response);
0362 error4: kfree(tm_wheel->model_request);
0363 error3: usb_free_urb(tm_wheel->urb);
0364 error2: kfree(tm_wheel);
0365 error1: hid_hw_stop(hdev);
0366 error0:
0367 return ret;
0368 }
0369
0370 static const struct hid_device_id thrustmaster_devices[] = {
0371 { HID_USB_DEVICE(0x044f, 0xb65d)},
0372 {}
0373 };
0374
0375 MODULE_DEVICE_TABLE(hid, thrustmaster_devices);
0376
0377 static struct hid_driver thrustmaster_driver = {
0378 .name = "hid-thrustmaster",
0379 .id_table = thrustmaster_devices,
0380 .probe = thrustmaster_probe,
0381 .remove = thrustmaster_remove,
0382 };
0383
0384 module_hid_driver(thrustmaster_driver);
0385
0386 MODULE_AUTHOR("Dario Pagani <dario.pagani.146+linuxk@gmail.com>");
0387 MODULE_LICENSE("GPL");
0388 MODULE_DESCRIPTION("Driver to initialize some steering wheel joysticks from Thrustmaster");
0389