From 901baf3790f03687e02064d702c90c25f831b7de Mon Sep 17 00:00:00 2001 From: Dario Pagani <> Date: Sun, 24 Jan 2021 10:07:24 +0100 Subject: Indented code according to Linux style in a way that passes checkpatch.cl. Replaced boilerplate code with the module_hid_driver macro --- hid-tminit.c | 112 +++++++++++++++++++++++------------------------------------ hid-tminit.h | 64 ++++++++++++++-------------------- 2 files changed, 69 insertions(+), 107 deletions(-) diff --git a/hid-tminit.c b/hid-tminit.c index f7c7814..601cd7a 100644 --- a/hid-tminit.c +++ b/hid-tminit.c @@ -2,13 +2,15 @@ /** * 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. + * + * Copyright (c) 2020-2021 Dario Pagani */ #include #include @@ -22,7 +24,8 @@ * these interrupts fix that particular issue. So far they haven't caused any * adverse effects in other wheels. */ -static void tminit_interrupts(struct hid_device *hdev){ +static void tminit_interrupts(struct hid_device *hdev) +{ int ret, trans, i, b_ep; u8 *send_buf = kmalloc(256, GFP_KERNEL); struct usb_host_endpoint *ep; @@ -30,7 +33,7 @@ static void tminit_interrupts(struct hid_device *hdev){ struct usb_interface *usbif = to_usb_interface(dev->parent); struct usb_device *usbdev = interface_to_usbdev(usbif); - if(!send_buf){ + if (!send_buf) { hid_err(hdev, "failed allocating send buffer\n"); return; } @@ -38,7 +41,7 @@ static void tminit_interrupts(struct hid_device *hdev){ ep = &usbif->cur_altsetting->endpoint[1]; b_ep = ep->desc.bEndpointAddress; - for(i = 0; i < ARRAY_SIZE(setup_arr); ++i){ + for (i = 0; i < ARRAY_SIZE(setup_arr); ++i) { memcpy(send_buf, setup_arr[i], setup_arr_sizes[i]); ret = usb_interrupt_msg(usbdev, @@ -48,7 +51,7 @@ static void tminit_interrupts(struct hid_device *hdev){ &trans, USB_CTRL_SET_TIMEOUT); - if(ret){ + if (ret) { hid_err(hdev, "setup data couldn't be sent\n"); return; } @@ -62,18 +65,16 @@ 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 || urb->status == -EPIPE) + if (urb->status == 0 || urb->status == -EPROTO || urb->status == -EPIPE) hid_info(hdev, "Success?! The wheel should have been initialized!\n"); else hid_warn(hdev, "URB to change wheel mode seems to have 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 */ @@ -85,47 +86,44 @@ static void tminit_model_handler(struct urb *urb) int i, ret; const struct th_wheel_info *twi = 0; - if(urb->status) - { + 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)) + 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)) + else if (tm_wheel->response->type == cpu_to_le16(0x47)) model = le16_to_cpu(tm_wheel->response->data.b.model); - else - { + 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) + 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) + + 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); + 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, + (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) + if (ret) hid_err(hdev, "Error %d while submitting the change URB. I am unable to initialize this wheel...\n", ret); } @@ -147,59 +145,53 @@ static void tminit_remove(struct hid_device *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. + * model type. */ -static int tminit_probe(struct hid_device *hdev, const struct hid_device_id *id){ +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) - { + 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) - { + 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) - { + if (!tm_wheel) { ret = -ENOMEM; goto error1; } tm_wheel->urb = usb_alloc_urb(0, GFP_ATOMIC); - if(!tm_wheel->urb) - { + if (!tm_wheel->urb) { ret = -ENOMEM; goto error2; } tm_wheel->model_request = kzalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); - if(! tm_wheel->model_request) - { + 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) - { + if (!tm_wheel->response) { ret = -ENOMEM; goto error4; } tm_wheel->change_request = kzalloc(sizeof(struct usb_ctrlrequest), GFP_KERNEL); - if(! tm_wheel->model_request) - { + if (!tm_wheel->model_request) { ret = -ENOMEM; goto error5; } @@ -214,7 +206,7 @@ static int tminit_probe(struct hid_device *hdev, const struct hid_device_id *id) tm_wheel->urb, tm_wheel->usb_dev, usb_rcvctrlpipe(tm_wheel->usb_dev, 0), - (char*)tm_wheel->model_request, + (char *)tm_wheel->model_request, tm_wheel->response, sizeof(struct tm_wheel_response), tminit_model_handler, @@ -222,18 +214,18 @@ static int tminit_probe(struct hid_device *hdev, const struct hid_device_id *id) ); ret = usb_submit_urb(tm_wheel->urb, GFP_ATOMIC); - if(ret) + if (ret) hid_err(hdev, "Error %d while submitting the URB. I am unable to initialize this wheel...\n", ret); return ret; error5: kfree(tm_wheel->response); -error4: kfree(tm_wheel->model_request); -error3: usb_free_urb(tm_wheel->urb); -error2: kfree(tm_wheel); -error1: hid_hw_stop(hdev); +error4: kfree(tm_wheel->model_request); +error3: usb_free_urb(tm_wheel->urb); +error2: kfree(tm_wheel); +error1: hid_hw_stop(hdev); error0: - return ret; + return ret; } static const struct hid_device_id tminit_devices[] = { @@ -250,27 +242,9 @@ static struct hid_driver tminit_driver = { .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_hid_driver(tminit_driver); -MODULE_AUTHOR("Dario Pagani <>"); +MODULE_AUTHOR("Dario Pagani "); MODULE_LICENSE("GPL"); -MODULE_DESCRIPTION("Driver to initialize Thrustmaster\'s wheels"); +MODULE_DESCRIPTION("Driver to initialize some steering wheel joysticks from Thrustmaster"); diff --git a/hid-tminit.h b/hid-tminit.h index 28ae827..729f493 100644 --- a/hid-tminit.h +++ b/hid-tminit.h @@ -1,37 +1,36 @@ +/* SPDX-License-Identifier: GPL-2.0*/ /** * These interrupts are used to prevent a nasty crash when initializing the * T300RS. Used in tminit_interrupts(). */ -u8 setup_0[] = { 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; -u8 setup_1[] = { 0x0a, 0x04, 0x90, 0x03, 0x00, 0x00, 0x00, 0x00 }; -u8 setup_2[] = { 0x0a, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 }; -u8 setup_3[] = { 0x0a, 0x04, 0x12, 0x10, 0x00, 0x00, 0x00, 0x00 }; -u8 setup_4[] = { 0x0a, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 }; -u8 *setup_arr[] = { setup_0, setup_1, setup_2, setup_3, setup_4 }; -unsigned int setup_arr_sizes[] = { +static const u8 setup_0[] = { 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; +static const u8 setup_1[] = { 0x0a, 0x04, 0x90, 0x03, 0x00, 0x00, 0x00, 0x00 }; +static const u8 setup_2[] = { 0x0a, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 }; +static const u8 setup_3[] = { 0x0a, 0x04, 0x12, 0x10, 0x00, 0x00, 0x00, 0x00 }; +static const u8 setup_4[] = { 0x0a, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 }; +static const u8 *const setup_arr[] = { setup_0, setup_1, setup_2, setup_3, setup_4 }; +static const unsigned int setup_arr_sizes[] = { ARRAY_SIZE(setup_0), ARRAY_SIZE(setup_1), ARRAY_SIZE(setup_2), ARRAY_SIZE(setup_3), - ARRAY_SIZE(setup_4) + ARRAY_SIZE(setup_4) }; /** * 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 + * must be used when preparing USB packets * and vice-versa */ -struct th_wheel_info -{ +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, + /** See when the USB control out packet is prepared... + * @TODO The TMX seems to require multiple control codes to switch. */ uint16_t switch_value; @@ -40,12 +39,8 @@ struct th_wheel_info /** * 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[] = -{ +static const struct th_wheel_info th_wheels_infos[] = { {0x0306, 0x0006, "Thrustmaster T150RS"}, {0x0206, 0x0005, "Thrustmaster T300RS"}, {0x0204, 0x0005, "Thrustmaster T300 Ferrari Alcantara Edition"}, @@ -53,15 +48,15 @@ static const struct th_wheel_info th_wheels_infos[] = {0x0407, 0x0001, "Thrustmaster TMX"} }; -const uint8_t th_wheels_infos_length = 5; +static const uint8_t th_wheels_infos_length = 5; /** * 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.... + * beign conducted yet. The position and meaning of fields are a + * just a very optimistic guess based on instinct.... */ struct __packed tm_wheel_response { @@ -72,10 +67,8 @@ struct __packed tm_wheel_response */ uint16_t type; - union - { - struct __packed - { + union { + struct __packed { uint16_t field0; uint16_t field1; /** @@ -89,8 +82,7 @@ struct __packed tm_wheel_response uint16_t field4; uint16_t field5; } a; - struct __packed - { + struct __packed { uint16_t field0; uint16_t field1; uint16_t model; @@ -98,8 +90,7 @@ struct __packed tm_wheel_response } data; }; -struct tm_wheel -{ +struct tm_wheel { struct usb_device *usb_dev; struct urb *urb; @@ -109,10 +100,8 @@ struct tm_wheel struct usb_ctrlrequest *change_request; }; -/** - * The control packet to send to wheel */ -struct usb_ctrlrequest model_request = -{ +/** The control packet to send to wheel */ +static const struct usb_ctrlrequest model_request = { .bRequestType = 0xc1, .bRequest = 73, .wValue = 0, @@ -120,8 +109,7 @@ struct usb_ctrlrequest model_request = .wLength = cpu_to_le16(0x0010) }; -struct usb_ctrlrequest change_request = -{ +static const struct usb_ctrlrequest change_request = { .bRequestType = 0x41, .bRequest = 83, .wValue = 0, // Will be filled by the driver -- cgit v1.3