forked from Kimplul/hid-tmff2
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhid-tmt248.c
310 lines (264 loc) · 8.22 KB
/
hid-tmt248.c
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
// SPDX-License-Identifier: GPL-2.0
#include <linux/usb.h>
#include <linux/hid.h>
#include "hid-tmff2.h"
#define T248_MAX_EFFECTS 16
#define T248_BUFFER_LENGTH 63
static const u8 setup_0[64] = { 0x42, 0x01 };
static const u8 setup_1[64] = { 0x0a, 0x04, 0x90, 0x03 };
static const u8 setup_2[64] = { 0x0a, 0x04, 0x00, 0x0c };
static const u8 setup_3[64] = { 0x0a, 0x04, 0x12, 0x10 };
static const u8 setup_4[64] = { 0x0a, 0x04, 0x00, 0x06 };
static const u8 setup_5[64] = { 0x0a, 0x04, 0x00, 0x0e };
static const u8 setup_6[64] = { 0x0a, 0x04, 0x00, 0x0e, 0x01 };
static const u8 *const setup_arr[] = { setup_0, setup_1, setup_2, setup_3, setup_4, setup_5, setup_6 };
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_5),
ARRAY_SIZE(setup_6)
};
static const unsigned long t248_params =
PARAM_SPRING_LEVEL
| PARAM_DAMPER_LEVEL
| PARAM_FRICTION_LEVEL
| PARAM_RANGE
| PARAM_GAIN
;
static const signed short t248_effects[] = {
FF_CONSTANT,
FF_RAMP,
FF_SPRING,
FF_DAMPER,
FF_FRICTION,
FF_INERTIA,
FF_PERIODIC,
FF_SINE,
FF_TRIANGLE,
FF_SQUARE,
FF_SAW_UP,
FF_SAW_DOWN,
FF_AUTOCENTER,
FF_GAIN,
-1
};
/* TODO: sort through this stuff */
static u8 t248_pc_rdesc_fixed[] = {
0x05, 0x01, /* Usage page (Generic Desktop) */
0x09, 0x04, /* Usage (Joystick) */
0xa1, 0x01, /* Collection (Application) */
0x09, 0x01, /* Usage (Pointer) */
0xa1, 0x00, /* Collection (Physical) */
0x85, 0x07, /* Report ID (7) */
0x09, 0x30, /* Usage (X) */
0x15, 0x00, /* Logical minimum (0) */
0x27, 0xff, 0xff, 0x00, 0x00, /* Logical maximum (65535) */
0x35, 0x00, /* Physical minimum (0) */
0x47, 0xff, 0xff, 0x00, 0x00, /* Physical maximum (65535) */
0x75, 0x10, /* Report size (16) */
0x95, 0x01, /* Report count (1) */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x31, /* Usage (Y) TODO: clutch? */
0x26, 0xff, 0x03, /* Logical maximum (1023) */
0x46, 0xff, 0x03, /* Physical maximum (1023) */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x35, /* Usage (Rz) TODO: brake? */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x36, /* Usage (Slider) */
0x81, 0x02, /* Input (Variable, Absolute) */
0x75, 0x08, /* Report size (8) */
0x26, 0xff, 0x00, /* Logical maximum (255) */
0x46, 0xff, 0x00, /* Physical maximum (255) */
0x09, 0x40, /* Usage (Vx) TODO: what is this? */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x41, /* Usage (Vy) TODO: --||-- */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x33, /* Usage (Rx) TODO: --||-- */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x34, /* Usage (Ry) TODO: --||-- */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x32, /* Usage (Z) TODO: --||-- (gas?) */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x37, /* Usage (Dial) */
0x81, 0x02, /* Input (Variable, Absolute) */
0x05, 0x09, /* Usage page (Button) */
0x19, 0x01, /* Usage minimum (1) */
0x29, 0x1a, /* Usage maximum (13) */
0x25, 0x01, /* Logical maximum (1) */
0x45, 0x01, /* Physical maximum (1) */
0x75, 0x01, /* Report size (1) */
0x95, 0x1a, /* Report count (26) */
0x81, 0x02, /* Input (Variable, Absolute) */
0x75, 0x06, /* Report size (6) */
0x95, 0x01, /* Report count (1) */
0x81, 0x03, /* Usage (Variable, Absolute, Constant) */
0x05, 0x01, /* Usage page (Generic Desktop) */
0x09, 0x39, /* Usage (Hat Switch) */
0x25, 0x07, /* Logical maximum (7) */
0x46, 0x3b, 0x01, /* Physical maximum (315) */
0x55, 0x00, /* Unit exponent (0) */
0x65, 0x14, /* Unit (Eng rot, Angular Pos) */
0x75, 0x04, /* Report size (4) */
0x81, 0x42, /* Input (Variable, Absolute, NullState) */
0x65, 0x00, /* Input (None) */
0x81, 0x03, /* Input (Variable, Absolute, Constant) */
0x85, 0x60, /* Report ID (96), prev 10 */
0x06, 0x00, 0xff, /* Usage page (Vendor 1) */
0x09, 0x60, /* Usage (96), prev 10 */
0x75, 0x08, /* Report size (8) */
0x95, 0x3f, /* Report count (63) */
0x26, 0xff, 0x00, /* Logical maximum (256) */
0x46, 0xff, 0x00, /* Physical maximum (256) */
0x91, 0x02, /* Output (Variable, Absolute) */
0x85, 0x02, /* Report ID (2) */
0x09, 0x02, /* Usage (2) */
0x81, 0x02, /* Input (Variable, Absolute) */
0x09, 0x14, /* Usage (20) */
0x85, 0x14, /* Report ID (20) */
0x81, 0x02, /* Input (Variable, Absolute) */
0xc0, /* End collection */
0xc0, /* End collection */
};
static int t248_interrupts(struct t300rs_device_entry *t248)
{
u8 *send_buf = kmalloc(256, GFP_KERNEL);
struct usb_interface *usbif = to_usb_interface(t248->hdev->dev.parent);
struct usb_host_endpoint *ep;
int ret, trans, b_ep, i;
if (!send_buf) {
hid_err(t248->hdev, "failed allocating send buffer\n");
return -ENOMEM;
}
ep = &usbif->cur_altsetting->endpoint[1];
b_ep = ep->desc.bEndpointAddress;
for (i = 0; i < ARRAY_SIZE(setup_arr); ++i) {
memcpy(send_buf, setup_arr[i], setup_arr_sizes[i]);
ret = usb_interrupt_msg(t248->usbdev,
usb_sndintpipe(t248->usbdev, b_ep),
send_buf, setup_arr_sizes[i],
&trans,
USB_CTRL_SET_TIMEOUT);
if (ret) {
hid_err(t248->hdev, "setup data couldn't be sent\n");
goto err;
}
}
err:
kfree(send_buf);
return ret;
}
int t248_wheel_init(struct tmff2_device_entry *tmff2)
{
struct t300rs_device_entry *t248 = kzalloc(sizeof(struct t300rs_device_entry), GFP_KERNEL);
struct list_head *report_list;
int ret;
if (!t248) {
ret = -ENOMEM;
goto t248_err;
}
t248->hdev = tmff2->hdev;
t248->input_dev = tmff2->input_dev;
t248->usbdev = to_usb_device(tmff2->hdev->dev.parent->parent);
t248->buffer_length = T248_BUFFER_LENGTH;
t248->send_buffer = kzalloc(t248->buffer_length, GFP_KERNEL);
if (!t248->send_buffer) {
ret = -ENOMEM;
goto send_err;
}
report_list = &t248->hdev->report_enum[HID_OUTPUT_REPORT].report_list;
t248->report = list_entry(report_list->next, struct hid_report, list);
t248->ff_field = t248->report->field[0];
t248->open = t248->input_dev->open;
t248->close = t248->input_dev->close;
if ((ret = t248_interrupts(t248)))
goto interrupt_err;
/* everything went OK */
tmff2->data = t248;
tmff2->params = t248_params;
tmff2->max_effects = T248_MAX_EFFECTS;
memcpy(tmff2->supported_effects, t248_effects, sizeof(t248_effects));
hid_info(t248->hdev, "force feedback for T248\n");
return 0;
interrupt_err:
send_err:
kfree(t248);
t248_err:
hid_err(tmff2->hdev, "failed initializing T248\n");
return ret;
}
int t248_wheel_destroy(void *data)
{
struct t300rs_device_entry *t300rs = data;
if (!t300rs)
return -ENODEV;
kfree(t300rs->send_buffer);
kfree(t300rs);
return 0;
}
int t248_set_range(void *data, uint16_t value)
{
struct t300rs_device_entry *t248 = data;
if (value < 140) {
hid_info(t248->hdev, "value %i too small, clamping to 140\n", value);
value = 140;
}
if (value > 900) {
hid_info(t248->hdev, "value %i too large, clamping to 900\n", value);
value = 900;
}
return t300rs_set_range(data, value);
}
static int t248_open(void *data)
{
struct t300rs_device_entry *t248 = data;
if (!t248)
return -ENODEV;
t248->send_buffer[0] = 0x01;
t248->send_buffer[1] = 0x04;
t300rs_send_int(t248);
t248->send_buffer[0] = 0x01;
t248->send_buffer[1] = 0x05;
t300rs_send_int(t248);
return t248->open(t248->input_dev);
}
static int t248_close(void *data)
{
struct t300rs_device_entry *t248 = data;
if (!t248)
return -ENODEV;
t248->send_buffer[0] = 0x01;
t248->send_buffer[1] = 0x05;
t300rs_send_int(t248);
t248->send_buffer[0] = 0x01;
t248->send_buffer[1] = 0x00;
t300rs_send_int(t248);
t248->close(t248->input_dev);
return 0;
}
static __u8 *t248_wheel_fixup(struct hid_device *hdev, __u8 *rdesc,
unsigned int *rsize)
{
rdesc = t248_pc_rdesc_fixed;
*rsize = sizeof(t248_pc_rdesc_fixed);
return rdesc;
}
int t248_populate_api(struct tmff2_device_entry *tmff2)
{
tmff2->play_effect = t300rs_play_effect;
tmff2->upload_effect = t300rs_upload_effect;
tmff2->update_effect = t300rs_update_effect;
tmff2->stop_effect = t300rs_stop_effect;
tmff2->set_gain = t300rs_set_gain;
tmff2->set_autocenter = t300rs_set_autocenter;
/* T248 only has 900 degree range, instead of T300RS 1080 */
tmff2->set_range = t248_set_range;
tmff2->wheel_fixup = t248_wheel_fixup;
tmff2->open = t248_open;
tmff2->close = t248_close;
tmff2->wheel_init = t248_wheel_init;
tmff2->wheel_destroy = t248_wheel_destroy;
return 0;
}