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
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
|
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Force feedback support for various HID compliant devices by ThrustMaster:
* ThrustMaster FireStorm Dual Power 2
* and possibly others whose device ids haven't been added.
*
* Modified to support ThrustMaster devices by Zinx Verituse
* on 2003-01-25 from the Logitech force feedback driver,
* which is by Johann Deneux.
*
* Copyright (c) 2003 Zinx Verituse <zinx@epicsol.org>
* Copyright (c) 2002 Johann Deneux
*/
/*
*/
#include <linux/hid.h>
#include <linux/usb.h>
#include <linux/input.h>
#include <linux/slab.h>
#include <linux/module.h>
#define USB_VENDOR_ID_THRUSTMASTER 0x044f
#define THRUSTMASTER_DEVICE_ID_2_IN_1_DT 0xb320
#define URB_FUNCTION_SYNC_RESET_PIPE_AND_CLEAR_STALL 0x001e
static const signed short ff_rumble[] = {
FF_RUMBLE,
-1
};
static const signed short ff_constant[] = {
FF_CONSTANT,
-1
};
static const signed short ff_joystick[] = {
FF_CONSTANT,
-1
};
#ifdef CONFIG_THRUSTMASTER_FF
/* Usages for thrustmaster devices I know about */
#define THRUSTMASTER_USAGE_FF (HID_UP_GENDESK | 0xbb)
static u8 setup_0[] = { 0x42, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
static u8 setup_1[] = { 0x0a, 0x04, 0x90, 0x03, 0x00, 0x00, 0x00, 0x00 };
static u8 setup_2[] = { 0x0a, 0x04, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x00 };
static u8 setup_3[] = { 0x0a, 0x04, 0x12, 0x10, 0x00, 0x00, 0x00, 0x00 };
static u8 setup_4[] = { 0x0a, 0x04, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00 };
static u8 *setup_arr[] = { setup_0, setup_1, setup_2, setup_3, setup_4 };
static 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)
};
static u8 hw_rq_in[] = { 0xc1, 0x49, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00 };
static u8 hw_rq_out[] = { 0x41, 0x53, 0x05, 0x00, 0x00, 0x00, 0x00, 0x00 };
static u8 ff_constant_array[] = {
0x60, 0x00, 0x01, 0x0a, 0xff, 0xdf, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, };
struct tmff_device {
struct hid_report *report;
struct hid_field *ff_field;
spinlock_t report_lock;
};
static spinlock_t *report_lock;
static int flags;
struct api_context {
struct completion done;
int status;
};
static struct api_context ctx;
/* Changes values from 0 to 0xffff into values from minimum to maximum */
static inline int tmff_scale_u16(unsigned int in, int minimum, int maximum)
{
int ret;
ret = (in * (maximum - minimum) / 0xffff) + minimum;
if (ret < minimum)
return minimum;
if (ret > maximum)
return maximum;
return ret;
}
/* Changes values from -0x80 to 0x7f into values from minimum to maximum */
static inline int tmff_scale_s8(int in, int minimum, int maximum)
{
int ret;
ret = (((in + 0x80) * (maximum - minimum)) / 0xff) + minimum;
if (ret < minimum)
return minimum;
if (ret > maximum)
return maximum;
return ret;
}
static void tmff_ctrl(struct urb *urb){
if(urb->status){
hid_warn(urb->dev, "urb status %d received\n", urb->status);
}
usb_free_urb(urb);
spin_unlock_irqrestore(report_lock, flags);
}
static int usb_start_wait_urb(struct urb *urb, int timeout, int *actual_length)
{
unsigned long expire;
int retval;
init_completion(&ctx.done);
urb->context = &ctx;
urb->actual_length = 0;
retval = usb_submit_urb(urb, GFP_ATOMIC);
if (unlikely(retval))
goto out;
expire = timeout ? msecs_to_jiffies(timeout) : MAX_SCHEDULE_TIMEOUT;
if (!wait_for_completion_timeout(&ctx.done, expire)) {
usb_kill_urb(urb);
retval = (ctx.status == -ENOENT ? -ETIMEDOUT : ctx.status);
dev_dbg(&urb->dev->dev,
"%s timed out on ep%d%s len=%u/%u\n",
current->comm,
usb_endpoint_num(&urb->ep->desc),
usb_urb_dir_in(urb) ? "in" : "out",
urb->actual_length,
urb->transfer_buffer_length);
} else
retval = ctx.status;
out:
if (actual_length)
*actual_length = urb->actual_length;
usb_free_urb(urb);
return retval;
}
static int tmff_play(struct input_dev *dev, void *data,
struct ff_effect *effect)
{
spin_lock_irqsave(report_lock, flags);
printk("Reached debug point 1");
struct hid_device *hid = input_get_drvdata(dev);
struct tmff_device *tmff = data;
struct hid_field *ff_field = tmff->ff_field;
int x, y;
int left, right; /* Rumbling */
printk("Reached debug point 2");
int trans, b_ep, err;
unsigned long flags;
struct device *d_dev = &hid->dev;
struct usb_interface *usbif = to_usb_interface(d_dev->parent);
struct usb_device *usbdev = interface_to_usbdev(usbif);
struct usb_host_endpoint *ep;
struct urb *urb = usb_alloc_urb(0, GFP_ATOMIC);
printk("Reached debug point 3");
u8 *send_buf = kmalloc(1024, GFP_ATOMIC);
memcpy(send_buf, ff_constant_array, ARRAY_SIZE(ff_constant_array));
ep = &usbif->cur_altsetting->endpoint[1];
b_ep = ep->desc.bEndpointAddress;
printk("Reached debug point 4");
switch (effect->type) {
case FF_CONSTANT:
printk("Reached debug point 5");
x = tmff_scale_s8(effect->u.ramp.start_level,
ff_field->logical_minimum,
ff_field->logical_maximum);
y = tmff_scale_s8(effect->u.ramp.end_level,
ff_field->logical_minimum,
ff_field->logical_maximum);
printk("(x, y)=(%04x, %04x)\n", x, y);
//send_buf[4] = x;
//send_buf[5] = y;
printk("Reached debug point 6");
usb_fill_int_urb(
urb,
usbdev,
usb_sndintpipe(usbdev, 1),
send_buf,
ARRAY_SIZE(ff_constant_array),
tmff_ctrl,
hid,
ep->desc.bInterval
);
printk("Reached debug point yeet");
/*err = usb_start_wait_urb(urb, 1, &trans);
if(err){
hid_err(hid, "Failed sending thing with ERRNO: %i", err);
}*/
usb_submit_urb(urb, GFP_KERNEL);
printk("Reached debug point wooot");
/*usb_interrupt_msg(usbdev,
usb_sndintpipe(usbdev, b_ep),
send_buf,
ARRAY_SIZE(ff_constant_array),
&trans,
USB_CTRL_SET_TIMEOUT);*/
printk("Reached debug point 7");
break;
}
printk("Reached debug point X");
kfree(send_buf);
return 0;
}
static int tmff_init_t300rs(struct hid_device *hid){
/* wow this is slow */
int a = 0;
return 0;
}
static int tmff_clear_init(struct hid_device *hid){
/* die */
int trans, b_ep, i, err;
u8 *send_buf, *rq_buf;
struct device *dev = &hid->dev;
struct usb_interface *usbif = to_usb_interface(dev->parent);
struct usb_device *usbdev = interface_to_usbdev(usbif);
struct usb_host_endpoint *ep;
struct urb *urb;
ep = &usbif->cur_altsetting->endpoint[1];
b_ep = ep->desc.bEndpointAddress;
send_buf = kmalloc(256, GFP_KERNEL); /* overkill but whatever */
rq_buf = kmalloc(256, GFP_KERNEL);
for(i = 0; i < ARRAY_SIZE(setup_arr); ++i){
memcpy(send_buf, setup_arr[i], setup_arr_sizes[i]);
err = usb_interrupt_msg(usbdev,
usb_sndintpipe(usbdev, b_ep),
send_buf,
setup_arr_sizes[i],
&trans,
USB_CTRL_SET_TIMEOUT);
if(err){
hid_err(hid, "Setup data at index %i couldn't be sent, ERRNO: %i\n", i, err);
goto error;
}
}
urb = usb_alloc_urb(0, GFP_KERNEL);
memcpy(send_buf, hw_rq_in, 8);
memcpy(rq_buf, hw_rq_out, 8);
usb_fill_control_urb(urb,
usbdev,
usb_sndctrlpipe(usbdev, 0),
rq_buf,
send_buf,
0,
tmff_ctrl,
hid
);
err = usb_start_wait_urb(urb, USB_CTRL_SET_TIMEOUT, &trans);
if(err != 0){
hid_err(hid, "Failed sending ctrl out with ERRNO: %i", err);
goto error;
}
hid_info(hid, "URB submission didn't fail?");
error:
kfree(rq_buf);
kfree(send_buf);
return err;
}
static int tmff_delete(struct kref *kref){
/* reserved for future use */
/* currently the errors we get aren't pretty */
return 0;
};
static int tmff_init(struct hid_device *hid, const signed short *ff_bits)
{
struct tmff_device *tmff;
struct hid_report *report;
struct list_head *report_list;
struct hid_input *hidinput = list_entry(hid->inputs.next,
struct hid_input, list);
struct input_dev *input_dev = hidinput->input;
int error;
int i;
if(hid->product == 0xb65d)
return tmff_clear_init(hid);
tmff = kzalloc(sizeof(struct tmff_device), GFP_KERNEL);
if (!tmff)
return -ENOMEM;
spin_lock_init(&tmff->report_lock);
report_lock = &tmff->report_lock;
/* Find the report to use */
report_list = &hid->report_enum[HID_OUTPUT_REPORT].report_list;
list_for_each_entry(report, report_list, list) {
int fieldnum;
for (fieldnum = 0; fieldnum < report->maxfield; ++fieldnum) {
struct hid_field *field = report->field[fieldnum];
if (field->maxusage <= 0)
continue;
switch (field->usage[0].hid) {
case 0xff00000a:
case THRUSTMASTER_USAGE_FF:
if (field->report_count < 2) {
hid_warn(hid, "ignoring FF field with report_count < 2\n");
continue;
}
if (field->logical_maximum ==
field->logical_minimum) {
hid_warn(hid, "ignoring FF field with logical_maximum == logical_minimum\n");
continue;
}
if (tmff->report && tmff->report != report) {
hid_warn(hid, "ignoring FF field in other report\n");
continue;
}
if (tmff->ff_field && tmff->ff_field != field) {
hid_warn(hid, "ignoring duplicate FF field\n");
continue;
}
tmff->report = report;
tmff->ff_field = field;
for (i = 0; ff_bits[i] >= 0; i++)
set_bit(ff_bits[i], input_dev->ffbit);
break;
default:
hid_warn(hid, "ignoring unknown output usage %08x\n",
field->usage[0].hid);
continue;
}
}
}
if (!tmff->report) {
hid_err(hid, "can't find FF field in output reports\n");
error = -ENODEV;
goto fail;
}
error = input_ff_create_memless(input_dev, tmff, tmff_play);
if (error)
goto fail;
hid_info(hid, "force feedback for ThrustMaster devices by Zinx Verituse <zinx@epicsol.org>\n");
return 0;
fail:
kfree(tmff);
return error;
}
#else
static inline int tmff_init(struct hid_device *hid, const signed short *ff_bits)
{
return 0;
}
#endif
static int tm_probe(struct hid_device *hdev, const struct hid_device_id *id)
{
int ret;
printk("We have been retarded");
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;
}
tmff_init(hdev, (void *)id->driver_data);
return 0;
err:
return ret;
}
static const struct hid_device_id tm_devices[] = {
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb300),
.driver_data = (unsigned long)ff_rumble },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb304), /* FireStorm Dual Power 2 (and 3) */
.driver_data = (unsigned long)ff_rumble },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, THRUSTMASTER_DEVICE_ID_2_IN_1_DT), /* Dual Trigger 2-in-1 */
.driver_data = (unsigned long)ff_rumble },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb323), /* Dual Trigger 3-in-1 (PC Mode) */
.driver_data = (unsigned long)ff_rumble },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb324), /* Dual Trigger 3-in-1 (PS3 Mode) */
.driver_data = (unsigned long)ff_rumble },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb605), /* NASCAR PRO FF2 Wheel */
.driver_data = (unsigned long)ff_joystick },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb651), /* FGT Rumble Force Wheel */
.driver_data = (unsigned long)ff_rumble },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb653), /* RGT Force Feedback CLUTCH Raging Wheel */
.driver_data = (unsigned long)ff_joystick },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb654), /* FGT Force Feedback Wheel */
.driver_data = (unsigned long)ff_joystick },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65a), /* F430 Force Feedback Wheel */
.driver_data = (unsigned long)ff_joystick },
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb65d),
.driver_data = (unsigned long)ff_constant }, /* die */
{ HID_USB_DEVICE(USB_VENDOR_ID_THRUSTMASTER, 0xb66e),
.driver_data = (unsigned long)ff_constant },
{ }
};
MODULE_DEVICE_TABLE(hid, tm_devices);
static struct hid_driver tm_driver = {
.name = "thrustmaster",
.id_table = tm_devices,
.probe = tm_probe,
};
module_hid_driver(tm_driver);
MODULE_LICENSE("GPL");
|