Commit | Line | Data |
---|---|---|
1a59d1b8 | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
1da177e4 | 2 | /* |
1da177e4 LT |
3 | * Copyright (c) 1998-2001 Vojtech Pavlik |
4 | * | |
5 | * Based on the work of: | |
6 | * Trystan Larey-Williams | |
7 | */ | |
8 | ||
9 | /* | |
10 | * ThrustMaster DirectConnect (BSP) joystick family driver for Linux | |
11 | */ | |
12 | ||
1da177e4 LT |
13 | #include <linux/delay.h> |
14 | #include <linux/kernel.h> | |
15 | #include <linux/slab.h> | |
16 | #include <linux/module.h> | |
1da177e4 LT |
17 | #include <linux/gameport.h> |
18 | #include <linux/input.h> | |
4e57b681 | 19 | #include <linux/jiffies.h> |
1da177e4 LT |
20 | |
21 | #define DRIVER_DESC "ThrustMaster DirectConnect joystick driver" | |
22 | ||
23 | MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>"); | |
24 | MODULE_DESCRIPTION(DRIVER_DESC); | |
25 | MODULE_LICENSE("GPL"); | |
26 | ||
27 | #define TMDC_MAX_START 600 /* 600 us */ | |
28 | #define TMDC_MAX_STROBE 60 /* 60 us */ | |
29 | #define TMDC_MAX_LENGTH 13 | |
30 | ||
31 | #define TMDC_MODE_M3DI 1 | |
32 | #define TMDC_MODE_3DRP 3 | |
33 | #define TMDC_MODE_AT 4 | |
34 | #define TMDC_MODE_FM 8 | |
35 | #define TMDC_MODE_FGP 163 | |
36 | ||
37 | #define TMDC_BYTE_ID 10 | |
38 | #define TMDC_BYTE_REV 11 | |
39 | #define TMDC_BYTE_DEF 12 | |
40 | ||
41 | #define TMDC_ABS 7 | |
42 | #define TMDC_ABS_HAT 4 | |
43 | #define TMDC_BTN 16 | |
44 | ||
17dd3f0f DT |
45 | static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 }; |
46 | static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 }; | |
1da177e4 | 47 | |
17dd3f0f | 48 | static const signed char tmdc_abs[TMDC_ABS] = |
1da177e4 | 49 | { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ }; |
17dd3f0f | 50 | static const signed char tmdc_abs_hat[TMDC_ABS_HAT] = |
1da177e4 | 51 | { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y }; |
17dd3f0f | 52 | static const signed char tmdc_abs_at[TMDC_ABS] = |
1da177e4 | 53 | { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE }; |
17dd3f0f | 54 | static const signed char tmdc_abs_fm[TMDC_ABS] = |
1da177e4 LT |
55 | { ABS_RX, ABS_RY, ABS_X, ABS_Y }; |
56 | ||
17dd3f0f | 57 | static const short tmdc_btn_pad[TMDC_BTN] = |
1da177e4 | 58 | { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR }; |
17dd3f0f | 59 | static const short tmdc_btn_joy[TMDC_BTN] = |
1da177e4 | 60 | { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE, |
ab0c3443 | 61 | BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z }; |
17dd3f0f | 62 | static const short tmdc_btn_fm[TMDC_BTN] = |
1da177e4 | 63 | { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 }; |
17dd3f0f | 64 | static const short tmdc_btn_at[TMDC_BTN] = |
1da177e4 LT |
65 | { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4, |
66 | BTN_BASE3, BTN_BASE2, BTN_BASE }; | |
67 | ||
17dd3f0f | 68 | static const struct { |
1da177e4 LT |
69 | int x; |
70 | int y; | |
71 | } tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}}; | |
72 | ||
17dd3f0f DT |
73 | static const struct tmdc_model { |
74 | unsigned char id; | |
75 | const char *name; | |
76 | char abs; | |
77 | char hats; | |
78 | char btnc[4]; | |
79 | char btno[4]; | |
80 | const signed char *axes; | |
81 | const short *buttons; | |
82 | } tmdc_models[] = { | |
f1c80ba0 | 83 | { 1, "ThrustMaster Millennium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy }, |
17dd3f0f DT |
84 | { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad }, |
85 | { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at }, | |
86 | { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm }, | |
87 | { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad }, | |
88 | { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy } | |
89 | }; | |
90 | ||
91 | ||
92 | struct tmdc_port { | |
93 | struct input_dev *dev; | |
94 | char name[64]; | |
95 | char phys[32]; | |
96 | int mode; | |
97 | const signed char *abs; | |
98 | const short *btn; | |
99 | unsigned char absc; | |
100 | unsigned char btnc[4]; | |
101 | unsigned char btno[4]; | |
102 | }; | |
103 | ||
1da177e4 LT |
104 | struct tmdc { |
105 | struct gameport *gameport; | |
17dd3f0f DT |
106 | struct tmdc_port *port[2]; |
107 | #if 0 | |
108 | struct input_dev *dev[2]; | |
1da177e4 LT |
109 | char name[2][64]; |
110 | char phys[2][32]; | |
111 | int mode[2]; | |
112 | signed char *abs[2]; | |
113 | short *btn[2]; | |
114 | unsigned char absc[2]; | |
115 | unsigned char btnc[2][4]; | |
116 | unsigned char btno[2][4]; | |
17dd3f0f | 117 | #endif |
1da177e4 LT |
118 | int reads; |
119 | int bads; | |
120 | unsigned char exists; | |
121 | }; | |
122 | ||
123 | /* | |
124 | * tmdc_read_packet() reads a ThrustMaster packet. | |
125 | */ | |
126 | ||
127 | static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH]) | |
128 | { | |
129 | unsigned char u, v, w, x; | |
130 | unsigned long flags; | |
131 | int i[2], j[2], t[2], p, k; | |
132 | ||
133 | p = gameport_time(gameport, TMDC_MAX_STROBE); | |
134 | ||
135 | for (k = 0; k < 2; k++) { | |
136 | t[k] = gameport_time(gameport, TMDC_MAX_START); | |
137 | i[k] = j[k] = 0; | |
138 | } | |
139 | ||
140 | local_irq_save(flags); | |
141 | gameport_trigger(gameport); | |
142 | ||
143 | w = gameport_read(gameport) >> 4; | |
144 | ||
145 | do { | |
146 | x = w; | |
147 | w = gameport_read(gameport) >> 4; | |
148 | ||
149 | for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) { | |
150 | if (~v & u & 2) { | |
151 | if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue; | |
152 | t[k] = p; | |
153 | if (j[k] == 0) { /* Start bit */ | |
154 | if (~v & 1) t[k] = 0; | |
155 | data[k][i[k]] = 0; j[k]++; continue; | |
156 | } | |
157 | if (j[k] == 9) { /* Stop bit */ | |
158 | if (v & 1) t[k] = 0; | |
159 | j[k] = 0; i[k]++; continue; | |
160 | } | |
161 | data[k][i[k]] |= (~v & 1) << (j[k]++ - 1); /* Data bit */ | |
162 | } | |
163 | t[k]--; | |
164 | } | |
165 | } while (t[0] > 0 || t[1] > 0); | |
166 | ||
167 | local_irq_restore(flags); | |
168 | ||
169 | return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1); | |
170 | } | |
171 | ||
17dd3f0f DT |
172 | static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data) |
173 | { | |
174 | int i, k, l; | |
175 | ||
176 | if (data[TMDC_BYTE_ID] != port->mode) | |
177 | return -1; | |
178 | ||
179 | for (i = 0; i < port->absc; i++) { | |
180 | if (port->abs[i] < 0) | |
181 | return 0; | |
182 | ||
183 | input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]); | |
184 | } | |
185 | ||
186 | switch (port->mode) { | |
187 | ||
188 | case TMDC_MODE_M3DI: | |
189 | ||
190 | i = tmdc_byte_d[0]; | |
191 | input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1)); | |
192 | input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i] & 1)); | |
193 | break; | |
194 | ||
195 | case TMDC_MODE_AT: | |
196 | ||
197 | i = tmdc_byte_a[3]; | |
198 | input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x); | |
199 | input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y); | |
200 | break; | |
201 | ||
202 | } | |
203 | ||
204 | for (k = l = 0; k < 4; k++) { | |
205 | for (i = 0; i < port->btnc[k]; i++) | |
206 | input_report_key(port->dev, port->btn[i + l], | |
207 | ((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1)); | |
208 | l += port->btnc[k]; | |
209 | } | |
210 | ||
211 | input_sync(port->dev); | |
212 | ||
213 | return 0; | |
214 | } | |
215 | ||
1da177e4 LT |
216 | /* |
217 | * tmdc_poll() reads and analyzes ThrustMaster joystick data. | |
218 | */ | |
219 | ||
220 | static void tmdc_poll(struct gameport *gameport) | |
221 | { | |
222 | unsigned char data[2][TMDC_MAX_LENGTH]; | |
223 | struct tmdc *tmdc = gameport_get_drvdata(gameport); | |
1da177e4 | 224 | unsigned char r, bad = 0; |
17dd3f0f | 225 | int i; |
1da177e4 LT |
226 | |
227 | tmdc->reads++; | |
228 | ||
229 | if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists) | |
230 | bad = 1; | |
17dd3f0f DT |
231 | else { |
232 | for (i = 0; i < 2; i++) { | |
233 | if (r & (1 << i) & tmdc->exists) { | |
1da177e4 | 234 | |
17dd3f0f DT |
235 | if (tmdc_parse_packet(tmdc->port[i], data[i])) |
236 | bad = 1; | |
1da177e4 | 237 | } |
17dd3f0f | 238 | } |
1da177e4 LT |
239 | } |
240 | ||
241 | tmdc->bads += bad; | |
242 | } | |
243 | ||
244 | static int tmdc_open(struct input_dev *dev) | |
245 | { | |
8715c1cf | 246 | struct tmdc *tmdc = input_get_drvdata(dev); |
1da177e4 LT |
247 | |
248 | gameport_start_polling(tmdc->gameport); | |
249 | return 0; | |
250 | } | |
251 | ||
252 | static void tmdc_close(struct input_dev *dev) | |
253 | { | |
8715c1cf | 254 | struct tmdc *tmdc = input_get_drvdata(dev); |
1da177e4 LT |
255 | |
256 | gameport_stop_polling(tmdc->gameport); | |
257 | } | |
258 | ||
17dd3f0f DT |
259 | static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data) |
260 | { | |
261 | const struct tmdc_model *model; | |
262 | struct tmdc_port *port; | |
263 | struct input_dev *input_dev; | |
264 | int i, j, b = 0; | |
4d462b9e | 265 | int err; |
17dd3f0f DT |
266 | |
267 | tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL); | |
268 | input_dev = input_allocate_device(); | |
269 | if (!port || !input_dev) { | |
4d462b9e DT |
270 | err = -ENOMEM; |
271 | goto fail; | |
17dd3f0f DT |
272 | } |
273 | ||
274 | port->mode = data[TMDC_BYTE_ID]; | |
275 | ||
276 | for (model = tmdc_models; model->id && model->id != port->mode; model++) | |
277 | /* empty */; | |
278 | ||
279 | port->abs = model->axes; | |
280 | port->btn = model->buttons; | |
281 | ||
282 | if (!model->id) { | |
283 | port->absc = data[TMDC_BYTE_DEF] >> 4; | |
284 | for (i = 0; i < 4; i++) | |
285 | port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0; | |
286 | } else { | |
287 | port->absc = model->abs; | |
288 | for (i = 0; i < 4; i++) | |
289 | port->btnc[i] = model->btnc[i]; | |
290 | } | |
291 | ||
292 | for (i = 0; i < 4; i++) | |
293 | port->btno[i] = model->btno[i]; | |
294 | ||
295 | snprintf(port->name, sizeof(port->name), model->name, | |
296 | port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode); | |
297 | snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i); | |
298 | ||
299 | port->dev = input_dev; | |
300 | ||
301 | input_dev->name = port->name; | |
302 | input_dev->phys = port->phys; | |
303 | input_dev->id.bustype = BUS_GAMEPORT; | |
304 | input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER; | |
305 | input_dev->id.product = model->id; | |
306 | input_dev->id.version = 0x0100; | |
935e658e | 307 | input_dev->dev.parent = &tmdc->gameport->dev; |
8715c1cf DT |
308 | |
309 | input_set_drvdata(input_dev, tmdc); | |
17dd3f0f DT |
310 | |
311 | input_dev->open = tmdc_open; | |
312 | input_dev->close = tmdc_close; | |
313 | ||
7b19ada2 | 314 | input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS); |
17dd3f0f DT |
315 | |
316 | for (i = 0; i < port->absc && i < TMDC_ABS; i++) | |
317 | if (port->abs[i] >= 0) | |
318 | input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4); | |
319 | ||
320 | for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++) | |
321 | input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0); | |
322 | ||
323 | for (i = 0; i < 4; i++) { | |
324 | for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++) | |
325 | set_bit(port->btn[j + b], input_dev->keybit); | |
326 | b += port->btnc[i]; | |
327 | } | |
328 | ||
4d462b9e DT |
329 | err = input_register_device(port->dev); |
330 | if (err) | |
331 | goto fail; | |
17dd3f0f DT |
332 | |
333 | return 0; | |
4d462b9e DT |
334 | |
335 | fail: input_free_device(input_dev); | |
336 | kfree(port); | |
337 | return err; | |
17dd3f0f DT |
338 | } |
339 | ||
1da177e4 LT |
340 | /* |
341 | * tmdc_probe() probes for ThrustMaster type joysticks. | |
342 | */ | |
343 | ||
344 | static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv) | |
345 | { | |
1da177e4 LT |
346 | unsigned char data[2][TMDC_MAX_LENGTH]; |
347 | struct tmdc *tmdc; | |
17dd3f0f | 348 | int i; |
1da177e4 LT |
349 | int err; |
350 | ||
a97e148a | 351 | if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL))) |
1da177e4 LT |
352 | return -ENOMEM; |
353 | ||
354 | tmdc->gameport = gameport; | |
355 | ||
356 | gameport_set_drvdata(gameport, tmdc); | |
357 | ||
358 | err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW); | |
359 | if (err) | |
360 | goto fail1; | |
361 | ||
362 | if (!(tmdc->exists = tmdc_read_packet(gameport, data))) { | |
363 | err = -ENODEV; | |
364 | goto fail2; | |
365 | } | |
366 | ||
367 | gameport_set_poll_handler(gameport, tmdc_poll); | |
368 | gameport_set_poll_interval(gameport, 20); | |
369 | ||
17dd3f0f DT |
370 | for (i = 0; i < 2; i++) { |
371 | if (tmdc->exists & (1 << i)) { | |
1da177e4 | 372 | |
17dd3f0f DT |
373 | err = tmdc_setup_port(tmdc, i, data[i]); |
374 | if (err) | |
375 | goto fail3; | |
1da177e4 | 376 | } |
17dd3f0f | 377 | } |
1da177e4 LT |
378 | |
379 | return 0; | |
380 | ||
17dd3f0f DT |
381 | fail3: while (--i >= 0) { |
382 | if (tmdc->port[i]) { | |
383 | input_unregister_device(tmdc->port[i]->dev); | |
384 | kfree(tmdc->port[i]); | |
385 | } | |
386 | } | |
387 | fail2: gameport_close(gameport); | |
388 | fail1: gameport_set_drvdata(gameport, NULL); | |
1da177e4 LT |
389 | kfree(tmdc); |
390 | return err; | |
391 | } | |
392 | ||
393 | static void tmdc_disconnect(struct gameport *gameport) | |
394 | { | |
395 | struct tmdc *tmdc = gameport_get_drvdata(gameport); | |
396 | int i; | |
397 | ||
17dd3f0f DT |
398 | for (i = 0; i < 2; i++) { |
399 | if (tmdc->port[i]) { | |
400 | input_unregister_device(tmdc->port[i]->dev); | |
401 | kfree(tmdc->port[i]); | |
402 | } | |
403 | } | |
1da177e4 LT |
404 | gameport_close(gameport); |
405 | gameport_set_drvdata(gameport, NULL); | |
406 | kfree(tmdc); | |
407 | } | |
408 | ||
409 | static struct gameport_driver tmdc_drv = { | |
410 | .driver = { | |
411 | .name = "tmdc", | |
4d462b9e | 412 | .owner = THIS_MODULE, |
1da177e4 LT |
413 | }, |
414 | .description = DRIVER_DESC, | |
415 | .connect = tmdc_connect, | |
416 | .disconnect = tmdc_disconnect, | |
417 | }; | |
418 | ||
98a84131 | 419 | module_gameport_driver(tmdc_drv); |