diff options
| -rw-r--r-- | .editorconfig | 17 | ||||
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | Makefile | 8 | ||||
| -rw-r--r-- | README.md | 15 | ||||
| -rw-r--r-- | hid-tminit.c | 230 | ||||
| -rw-r--r-- | hid-tminit.h | 112 |
6 files changed, 384 insertions, 2 deletions
diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..ceb6e23 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,17 @@ +[*] +charset = utf-8 +insert_final_newline = true + +# The settings for C (*.c and *.h) files are mirrored in .clang-format. Keep +# them in sync. +[*.{c,h,sh,perl,pl,pm,txt}] +indent_style = tab +tab_width = 8 + +[Makefile] +indent_style = tab +tab_width = 8 + +[*.py] +indent_style = space +indent_size = 4 diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..107f6d4 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +build/* +.vscode +.idea + diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..d666a97 --- /dev/null +++ b/Makefile @@ -0,0 +1,8 @@ +obj-m += hid-tminit.o + +all: + make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules +install: + make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) modules_install +clean: + make -C /lib/modules/$(shell uname -r)/build M=$(shell pwd) clean @@ -1,4 +1,15 @@ # hid-tminit -Linux driver to properly initialize Thrustmaster Wheels +Linux driver to properly initialize Thrustmaster Wheels, based on [this](https://github.com/Kimplul/hid-tmff2) driver written by Kimplul -Based on [this](https://github.com/Kimplul/hid-tmff2) driver written by Kimplul +When connected to the machine, the Thrustmaster wheels appear as +a «generic» hid gamepad called `Thrustmaster FFB Wheel`. + +When in this mode not every functionality of the wheel, like the force feedback, +are available. To enable all functionalities of a Thrustmaster wheel we have to send +to it a specific USB CONTROL request with a code different for each wheel. + +This driver tries to understand which model of Thrustmaster wheel the generic +`Thrustmaster FFB Wheel` really is and then sends the appropriate control code. + +## Wheels supported +You can check the table of wheels supported in `hid-tminit.h`, they are stored in the array `th_wheels_infos[]` diff --git a/hid-tminit.c b/hid-tminit.c new file mode 100644 index 0000000..219d78c --- /dev/null +++ b/hid-tminit.c @@ -0,0 +1,230 @@ +// SPDX-License-Identifier: GPL-2.0 +/** + * When connected to the machine, the Thrustmaster wheels appear as + * a «generic» hid gamepad called "Thrustmaster FFB Wheel". + * + * When in this mode not every functionality of the wheel, like the force feedback, + * are available. To enable all functionalities of a Thrustmaster wheel we have to send + * to it a specific USB CONTROL request with a code different for each wheel. + * + * This driver tries to understand which model of Thrustmaster wheel the generic + * "Thrustmaster FFB Wheel" really is and then sends the appropriate control code. + */ +#include <linux/hid.h> +#include <linux/usb.h> +#include <linux/input.h> +#include <linux/slab.h> +#include <linux/module.h> +#include "hid-tminit.h" + +static void tminit_change_handler(struct urb *urb) +{ + struct hid_device *hdev = urb->context; + + // The wheel seems to kill himself before answering the host and therefore is violating the USB protocol... + if(urb->status == 0 || urb->status == -EPROTO) + hid_info(hdev, "Success?! The wheel should have been initialized!\n"); + else + hid_err(hdev, "URB to change wheel mode failed with error %d\n", urb->status); +} + +/** + * Called by the USB subsystem when the wheel respons to our request + * to get [what it seems to be] the wheel's model. + * + * If the model id is recognized then we send an opportune USB CONTROL REQUEST + * to switch the wheel to its full capabilities + */ +static void tminit_model_handler(struct urb *urb) +{ + struct hid_device *hdev = urb->context; + struct tm_wheel *tm_wheel = hid_get_drvdata(hdev); + uint16_t model = 0; + int i, ret; + const struct th_wheel_info *twi = 0; + + if(urb->status) + { + hid_err(hdev, "URB to get model id failed with error %d\n", urb->status); + return; + } + + if(tm_wheel->response->type == cpu_to_le16(0x49)) + model = le16_to_cpu(tm_wheel->response->data.a.model); + else if(tm_wheel->response->type == cpu_to_le16(0x47)) + model = le16_to_cpu(tm_wheel->response->data.b.model); + else + { + hid_err(hdev, "Unknow packet type 0x%x, unable to proceed further with wheel init\n", tm_wheel->response->type); + return; + } + + for(i = 0; i < th_wheels_infos_length && !twi; i++) + if(th_wheels_infos[i].wheel_type == model) + twi = th_wheels_infos + i; + + if(twi) + hid_info(hdev, "Wheel with model id 0x%x is a %s\n", model, twi->wheel_name); + else + { + hid_err(hdev, "Unknown wheel's model id 0x%x, unable to proceed further with wheel init\n",model); + return; + } + + tm_wheel->change_request->wValue = cpu_to_le16(twi->switch_value); + usb_fill_control_urb( + tm_wheel->urb, + tm_wheel->usb_dev, + usb_sndctrlpipe(tm_wheel->usb_dev, 0), + (char*)tm_wheel->change_request, + 0, 0, // We do not expect any response from the wheel + tminit_change_handler, + hdev + ); + + ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC); + if(ret) + hid_err(hdev, "Error %d while submitting the change URB. I am unable to initialize this wheel...\n", ret); +} + +static void tminit_remove(struct hid_device *hdev) +{ + struct tm_wheel *tm_wheel = hid_get_drvdata(hdev); + + usb_kill_urb(tm_wheel->urb); + + kzfree(tm_wheel->response); + kzfree(tm_wheel->model_request); + usb_free_urb(tm_wheel->urb); + kzfree(tm_wheel); + + hid_hw_stop(hdev); +} + +/** + * Function called by HID when a hid Thrustmaster FFB wheel is connected to the host. + * This function starts the hid dev, tries to allocate the tm_wheel data structure and + * finally send an USB CONTROL REQUEST to the wheel to get [what it seems to be] its + * model type. + */ +static int tminit_probe(struct hid_device *hdev, const struct hid_device_id *id){ + int ret = 0; + struct tm_wheel *tm_wheel = 0; + + ret = hid_parse(hdev); + if(ret) + { + hid_err(hdev, "parse failed with error %d\n", ret); + goto error0; + } + + ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF); + if(ret) + { + hid_err(hdev, "hw start failed with error %d\n", ret); + goto error0; + } + + // Now we allocate the tm_wheel + tm_wheel = kzalloc(sizeof(struct tm_wheel), GFP_KERNEL); + if(!tm_wheel) + { + ret = -ENOMEM; + goto error1; + } + + tm_wheel->urb = usb_alloc_urb(0, GFP_ATOMIC); + if(!tm_wheel->urb) + { + ret = -ENOMEM; + goto error2; + } + + tm_wheel->model_request = kzalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); + if(! tm_wheel->model_request) + { + ret = -ENOMEM; + goto error3; + } + memcpy(tm_wheel->model_request, &model_request, sizeof(struct usb_ctrlrequest)); + + tm_wheel->response = kzalloc(sizeof(struct tm_wheel_response), GFP_KERNEL); + if(! tm_wheel->response) + { + ret = -ENOMEM; + goto error4; + } + + tm_wheel->change_request = kzalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); + if(! tm_wheel->model_request) + { + ret = -ENOMEM; + goto error5; + } + memcpy(tm_wheel->change_request, &change_request, sizeof(struct usb_ctrlrequest)); + + tm_wheel->usb_dev = interface_to_usbdev(to_usb_interface(hdev->dev.parent)); + hid_set_drvdata(hdev, tm_wheel); + + usb_fill_control_urb( + tm_wheel->urb, + tm_wheel->usb_dev, + usb_sndctrlpipe(tm_wheel->usb_dev, 0), + (char*)tm_wheel->model_request, + tm_wheel->response, + sizeof(struct tm_wheel_response), + tminit_model_handler, + hdev + ); + + ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC); + if(ret) + hid_err(hdev, "Error %d while submitting the URB. I am unable to initialize this wheel...\n", ret); + + return ret; + +error5: kzfree(tm_wheel->response); +error4: kzfree(tm_wheel->model_request); +error3: usb_free_urb(tm_wheel->urb); +error2: kzfree(tm_wheel); +error1: hid_hw_stop(hdev); +error0: + return ret; +} + +static const struct hid_device_id tminit_devices[] = { + { HID_USB_DEVICE(0x044f, 0xb65d)}, + {} +}; + +MODULE_DEVICE_TABLE(hid, tminit_devices); + +static struct hid_driver tminit_driver = { + .name = "hid-tminit", + .id_table = tminit_devices, + .probe = tminit_probe, + .remove = tminit_remove, +}; + +static int __init tminit_init(void) +{ + int errno; + + errno = hid_register_driver(&tminit_driver); + + if(errno) + printk(KERN_ERR "hid-tminit: error %d while registering the hid driver\n", errno); + + return errno; +} + +static void __exit tminit_exit(void) +{ + hid_unregister_driver(&tminit_driver); +} + +module_init(tminit_init); +module_exit(tminit_exit); + +MODULE_AUTHOR("Dario Pagani <>"); +MODULE_LICENSE("GPL"); diff --git a/hid-tminit.h b/hid-tminit.h new file mode 100644 index 0000000..820ca86 --- /dev/null +++ b/hid-tminit.h @@ -0,0 +1,112 @@ +/** + * This struct contains for each type of + * Thrustmaster wheel + * + * Note: The values are stored in the CPU + * endianess, the USB protocols always use + * little endian; the macro cpu_to_le[BIT]() + * must be used when preparing USB packets + * and vice-versa + */ +struct th_wheel_info +{ + uint16_t wheel_type; + + /** See when the USB control out packet is prepared... + * @TODO The TMX seems to require to control codes to switch. + * Probabilly this field needs to be converted to an array, + */ + uint16_t switch_value; + + char const *const wheel_name; +}; + +/** + * All wheel I know. TO BE TESTED + * I can't find of a clever way to store them, but I do + * not think a O(n) cycle for each wheel attached to the + * machine is too bad... + */ +static const struct th_wheel_info th_wheels_infos[] = +{ + {0x0306, 0x0006, "Thrustmaster T150RS"}, + {0x0206, 0x0005, "Thrustmaster T300RS"}, + {0x0002, 0x0002, "Thrustmaster T500RS"}, + {0x0407, 0x0001, "Thrustmaster TMX"} +}; + +const uint8_t th_wheels_infos_length = 4; + +/** + * This structs contains (in little endian) the response data + * of the wheel to the request 73 + * + * A sufficient research to understand what each field does is not + * beign conducted yet. The position and meaning of fields are a + * just a very optimistic guess based on instinct.... + */ +struct __packed tm_wheel_response +{ + /** + * Seems to be the type of packet + * - 0x0049 if is data.a (15 bytes) + * - 0x0047 if is data.b (7 bytes) + */ + uint16_t type; + + union + { + struct __packed + { + uint16_t field0; + uint16_t field1; + /** + * Seems to be the model code of the wheel + * Read table thrustmaster_wheels to values + */ + uint16_t model; + + uint16_t field2; + uint16_t field3; + uint16_t field4; + uint16_t field5; + } a; + struct __packed + { + uint16_t field0; + uint16_t field1; + uint16_t model; + } b; + } data; +}; + +struct tm_wheel +{ + struct usb_device *usb_dev; + struct urb *urb; + + struct usb_ctrlrequest *model_request; + struct tm_wheel_response *response; + + struct usb_ctrlrequest *change_request; +}; + +/** + * The control packet to send to wheel */ +struct usb_ctrlrequest model_request = +{ + .bRequestType = 0xc1, + .bRequest = 73, + .wValue = 0, + .wIndex = 0, + .wLength = cpu_to_le16(0x0010) +}; + +struct usb_ctrlrequest change_request = +{ + .bRequestType = 0x41, + .bRequest = 83, + .wValue = 0, // Will be filled by the driver + .wIndex = 0, + .wLength = 0 +}; |
