aboutsummaryrefslogtreecommitdiff
path: root/hid-tminit.c
blob: be62ee896d4f908145aa13c18566c978bef0887b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
#include "hid-tminit.h"

static void tminit_callback(struct urb *urb){
    if(urb->status){
        hid_info(urb->dev, "urb status %d received\n", urb->status);
    }

    usb_free_urb(urb);
}

static int tminit_send_int(struct hid_device *hdev, struct usb_device *usbdev, struct usb_interface *usbif, u8 *send_buffer, int *trans){
    struct urb *urb = usb_alloc_urb(0, GFP_ATOMIC);
    struct usb_host_endpoint *ep;

    ep = &usbif->cur_altsetting->endpoint[1];

    usb_fill_int_urb(
            urb,
            usbdev,
            usb_sndintpipe(usbdev, 1),
            send_buffer,
            9,
            tminit_callback,
            hdev,
            ep->desc.bInterval
            );

    return usb_submit_urb(urb, GFP_ATOMIC);
}

static int tminit_send_int_in(struct hid_device *hdev, struct usb_device *usbdev, struct usb_interface *usbif, u8 *send_buffer, int *trans){
    struct urb *urb = usb_alloc_urb(0, GFP_ATOMIC);
    struct usb_host_endpoint *ep;

    ep = &usbif->cur_altsetting->endpoint[1];

    usb_fill_int_urb(
            urb,
            usbdev,
            usb_rcvintpipe(usbdev, 2),
            send_buffer,
            27,
            tminit_callback,
            hdev,
            ep->desc.bInterval
            );

    return usb_submit_urb(urb, GFP_ATOMIC);
}
/* for some godawful reason these interrupts are absolutely necessary, otherwise
 * the whole kernel crashes. I have no idea why.
 * */
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;
    struct device *dev = &hdev->dev;
    struct usb_interface *usbif = to_usb_interface(dev->parent);
    struct usb_device *usbdev = interface_to_usbdev(usbif);

    ep = &usbif->cur_altsetting->endpoint[1];
    b_ep = ep->desc.bEndpointAddress;

    memcpy(send_buf, setup_arr[0], setup_arr_sizes[0]);
    ret = usb_interrupt_msg(usbdev,
            usb_sndintpipe(usbdev, b_ep),
            send_buf,
            setup_arr_sizes[0],
            &trans,
            USB_CTRL_SET_TIMEOUT
            );

    if(ret){
        hid_err(hdev, "setup int couldn't be sent: %i\n", ret);
    }

    for(i = 0; i < 4; ++i){
        ret = tminit_send_int_in(hdev, usbdev, usbif, send_buf, trans);

        if(ret){
            hid_err(hdev, "setup int in couldn't be sent: %i\n", ret);
        }
    }

    msleep(100);

    for(i = 1; i < ARRAY_SIZE(setup_arr); ++i){
        memcpy(send_buf, setup_arr[i], setup_arr_sizes[i]);

        ret = usb_interrupt_msg(usbdev,
                usb_sndintpipe(usbdev, b_ep),
                send_buf,
                setup_arr_sizes[i],
                &trans,
                USB_CTRL_SET_TIMEOUT
                );

        if(ret){
            hid_err(hdev, "setup data couldn't be sent\n");
            return;
        }

        msleep(10);
        
    }

    kzfree(send_buf);
}


void tminit_controls(struct hid_device *hdev){
    int i = 0, ret;
    struct usb_host_endpoint *ep;
    struct usb_host_endpoint *ip;
    struct device *dev = &hdev->dev;
    struct usb_interface *usbif = to_usb_interface(dev->parent);
    struct usb_device *usbdev = interface_to_usbdev(usbif);

    u8 *transfer = kzalloc(64, GFP_ATOMIC);

    ret = usb_control_msg(usbdev,
            usb_rcvctrlpipe(usbdev, 0),
            86,
            0xc1,
            0,
            0,
            transfer,
            8,
            USB_CTRL_SET_TIMEOUT);

    if(ret < 0){
        hid_err(hdev, "failed retrieveing ctrl 86: %i", ret);
    }

    ret = usb_control_msg(usbdev,
            usb_rcvctrlpipe(usbdev, 0),
            73,
            0xc1,
            0,
            0,
            transfer,
            16,
            USB_CTRL_SET_TIMEOUT);

    if(ret < 0){
        hid_err(hdev, "failed retrieving ctrl 73: %i", ret);
    }

    ret = usb_control_msg(usbdev,
            usb_rcvctrlpipe(usbdev, 0),
            66,
            0xc1,
            0,
            0,
            transfer,
            8,
            USB_CTRL_SET_TIMEOUT);

    if(ret < 0){
        hid_err(hdev, "failed retreiving ctrl 66: %i", ret);
    }

    ret = usb_control_msg(usbdev,
            usb_rcvctrlpipe(usbdev, 0),
            78,
            0xc1,
            0,
            0,
            transfer,
            8,
            USB_CTRL_SET_TIMEOUT);

    if(ret < 0){
        hid_err(hdev, "failed retrieving ctrl 78: %i", ret);
    }

    ret = usb_control_msg(usbdev,
            usb_rcvctrlpipe(usbdev, 0),
            86,
            0xc1,
            0,
            0,
            transfer,
            8,
            USB_CTRL_SET_TIMEOUT);

    if(ret < 0){
        hid_err(hdev, "failed retrieving 86: %i", ret);
    }
}

int tminit(struct hid_device *hdev){
    struct urb *urb;
    u8 *setup_packet, *transfer_buffer;
    struct device *dev = &hdev->dev;
    struct usb_interface *usbif = to_usb_interface(dev->parent);
    struct usb_device *usbdev = interface_to_usbdev(usbif);
    int ret;
    u8 *transfer = kzalloc(64, GFP_ATOMIC);

    tminit_controls(hdev);

    tminit_interrupts(hdev);

    setup_packet = kmalloc(8, GFP_ATOMIC);
    transfer_buffer = kmalloc(8, GFP_ATOMIC);

    memcpy(setup_packet, hw_rq_out, 8);
    memcpy(transfer_buffer, hw_rq_in, 8);

    // this is really ugly but it'll work for now I suppose
    msleep(200);

    ret = usb_control_msg(usbdev,
            usb_rcvctrlpipe(usbdev, 0),
            73,
            0xc1,
            0,
            0,
            transfer,
            16,
            USB_CTRL_SET_TIMEOUT);

    if(ret < 0){
        hid_err(hdev, "failed retrieving 73 after interrupts: %i", ret);
    }


    urb = usb_alloc_urb(0, GFP_ATOMIC);

    usb_fill_control_urb(urb,
            usbdev,
            usb_sndctrlpipe(usbdev, 0),
            setup_packet,
            transfer_buffer,
            8,
            tminit_callback,
            hdev);

    /* we sort of have to go on faith that the message is sent, because the
     * wheel usually completely dies as soon as it receives the message.
     * No need to even check the return value.
     */
    ret = usb_submit_urb(urb, GFP_ATOMIC);
    kfree(setup_packet);
    kfree(transfer_buffer);
    kfree(transfer);
    return ret;
}

static void tminit_remove(struct hid_device *hdev){
    /* we are dead, hopefully without any serious side effects */
    hid_hw_stop(hdev);
}

static int tminit_probe(struct hid_device *hdev, const struct hid_device_id *id){
    int ret;

    ret = hid_parse(hdev);
    if(ret){
        hid_err(hdev, "parse failed\n");
        goto err;
    }

    ret = hid_hw_start(hdev, HID_CONNECT_DEFAULT & ~HID_CONNECT_FF);
    if(ret){
        hid_err(hdev, "hw start failed\n");
        goto err;
    }

    ret = tminit(hdev);
    if(ret){
        hid_err(hdev, "tminit exited, error might be intended behaviour\n");
        goto err;
    }

err:
    return ret; 
}

static const struct hid_device_id tminit_devices[] = {
    { HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65d)},
    {}
};

MODULE_DEVICE_TABLE(hid, tminit_devices);

static struct hid_driver tminit_driver = {
    .name = "thrustmaster init",
    .id_table = tminit_devices,
    .probe = tminit_probe,
    .remove = tminit_remove,
};
module_hid_driver(tminit_driver);

MODULE_LICENSE("GPL");