blob: 5ef9bcdb0345bff2ebacac80ea73ea318403dd03 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002 * Copyright (c) 1998-2001 Vojtech Pavlik
3 *
4 * Based on the work of:
5 * Trystan Larey-Williams
6 */
7
8/*
9 * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
30 */
31
32#include <linux/delay.h>
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/gameport.h>
38#include <linux/input.h>
Tim Schmielau4e57b682005-10-30 15:03:48 -080039#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define DRIVER_DESC "ThrustMaster DirectConnect joystick driver"
42
43MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE("GPL");
46
47#define TMDC_MAX_START 600 /* 600 us */
48#define TMDC_MAX_STROBE 60 /* 60 us */
49#define TMDC_MAX_LENGTH 13
50
51#define TMDC_MODE_M3DI 1
52#define TMDC_MODE_3DRP 3
53#define TMDC_MODE_AT 4
54#define TMDC_MODE_FM 8
55#define TMDC_MODE_FGP 163
56
57#define TMDC_BYTE_ID 10
58#define TMDC_BYTE_REV 11
59#define TMDC_BYTE_DEF 12
60
61#define TMDC_ABS 7
62#define TMDC_ABS_HAT 4
63#define TMDC_BTN 16
64
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050065static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
66static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070067
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050068static const signed char tmdc_abs[TMDC_ABS] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050070static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070071 { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050072static const signed char tmdc_abs_at[TMDC_ABS] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050074static const signed char tmdc_abs_fm[TMDC_ABS] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 { ABS_RX, ABS_RY, ABS_X, ABS_Y };
76
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050077static const short tmdc_btn_pad[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070078 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050079static const short tmdc_btn_joy[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
Dmitry Torokhovab0c3442005-05-29 02:28:55 -050081 BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050082static const short tmdc_btn_fm[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050084static const short tmdc_btn_at[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
86 BTN_BASE3, BTN_BASE2, BTN_BASE };
87
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050088static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 int x;
90 int y;
91} tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
92
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050093static const struct tmdc_model {
94 unsigned char id;
95 const char *name;
96 char abs;
97 char hats;
98 char btnc[4];
99 char btno[4];
100 const signed char *axes;
101 const short *buttons;
102} tmdc_models[] = {
103 { 1, "ThrustMaster Millenium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
104 { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
105 { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
106 { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
107 { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
108 { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
109};
110
111
112struct tmdc_port {
113 struct input_dev *dev;
114 char name[64];
115 char phys[32];
116 int mode;
117 const signed char *abs;
118 const short *btn;
119 unsigned char absc;
120 unsigned char btnc[4];
121 unsigned char btno[4];
122};
123
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124struct tmdc {
125 struct gameport *gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500126 struct tmdc_port *port[2];
127#if 0
128 struct input_dev *dev[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 char name[2][64];
130 char phys[2][32];
131 int mode[2];
132 signed char *abs[2];
133 short *btn[2];
134 unsigned char absc[2];
135 unsigned char btnc[2][4];
136 unsigned char btno[2][4];
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500137#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 int reads;
139 int bads;
140 unsigned char exists;
141};
142
143/*
144 * tmdc_read_packet() reads a ThrustMaster packet.
145 */
146
147static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
148{
149 unsigned char u, v, w, x;
150 unsigned long flags;
151 int i[2], j[2], t[2], p, k;
152
153 p = gameport_time(gameport, TMDC_MAX_STROBE);
154
155 for (k = 0; k < 2; k++) {
156 t[k] = gameport_time(gameport, TMDC_MAX_START);
157 i[k] = j[k] = 0;
158 }
159
160 local_irq_save(flags);
161 gameport_trigger(gameport);
162
163 w = gameport_read(gameport) >> 4;
164
165 do {
166 x = w;
167 w = gameport_read(gameport) >> 4;
168
169 for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
170 if (~v & u & 2) {
171 if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
172 t[k] = p;
173 if (j[k] == 0) { /* Start bit */
174 if (~v & 1) t[k] = 0;
175 data[k][i[k]] = 0; j[k]++; continue;
176 }
177 if (j[k] == 9) { /* Stop bit */
178 if (v & 1) t[k] = 0;
179 j[k] = 0; i[k]++; continue;
180 }
181 data[k][i[k]] |= (~v & 1) << (j[k]++ - 1); /* Data bit */
182 }
183 t[k]--;
184 }
185 } while (t[0] > 0 || t[1] > 0);
186
187 local_irq_restore(flags);
188
189 return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
190}
191
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500192static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
193{
194 int i, k, l;
195
196 if (data[TMDC_BYTE_ID] != port->mode)
197 return -1;
198
199 for (i = 0; i < port->absc; i++) {
200 if (port->abs[i] < 0)
201 return 0;
202
203 input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
204 }
205
206 switch (port->mode) {
207
208 case TMDC_MODE_M3DI:
209
210 i = tmdc_byte_d[0];
211 input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
212 input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i] & 1));
213 break;
214
215 case TMDC_MODE_AT:
216
217 i = tmdc_byte_a[3];
218 input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
219 input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
220 break;
221
222 }
223
224 for (k = l = 0; k < 4; k++) {
225 for (i = 0; i < port->btnc[k]; i++)
226 input_report_key(port->dev, port->btn[i + l],
227 ((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
228 l += port->btnc[k];
229 }
230
231 input_sync(port->dev);
232
233 return 0;
234}
235
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236/*
237 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
238 */
239
240static void tmdc_poll(struct gameport *gameport)
241{
242 unsigned char data[2][TMDC_MAX_LENGTH];
243 struct tmdc *tmdc = gameport_get_drvdata(gameport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 unsigned char r, bad = 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500245 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246
247 tmdc->reads++;
248
249 if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
250 bad = 1;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500251 else {
252 for (i = 0; i < 2; i++) {
253 if (r & (1 << i) & tmdc->exists) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500255 if (tmdc_parse_packet(tmdc->port[i], data[i]))
256 bad = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500258 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
260
261 tmdc->bads += bad;
262}
263
264static int tmdc_open(struct input_dev *dev)
265{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400266 struct tmdc *tmdc = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
268 gameport_start_polling(tmdc->gameport);
269 return 0;
270}
271
272static void tmdc_close(struct input_dev *dev)
273{
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400274 struct tmdc *tmdc = input_get_drvdata(dev);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 gameport_stop_polling(tmdc->gameport);
277}
278
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500279static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
280{
281 const struct tmdc_model *model;
282 struct tmdc_port *port;
283 struct input_dev *input_dev;
284 int i, j, b = 0;
Dmitry Torokhov4d462b92006-01-29 21:52:26 -0500285 int err;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500286
287 tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
288 input_dev = input_allocate_device();
289 if (!port || !input_dev) {
Dmitry Torokhov4d462b92006-01-29 21:52:26 -0500290 err = -ENOMEM;
291 goto fail;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500292 }
293
294 port->mode = data[TMDC_BYTE_ID];
295
296 for (model = tmdc_models; model->id && model->id != port->mode; model++)
297 /* empty */;
298
299 port->abs = model->axes;
300 port->btn = model->buttons;
301
302 if (!model->id) {
303 port->absc = data[TMDC_BYTE_DEF] >> 4;
304 for (i = 0; i < 4; i++)
305 port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
306 } else {
307 port->absc = model->abs;
308 for (i = 0; i < 4; i++)
309 port->btnc[i] = model->btnc[i];
310 }
311
312 for (i = 0; i < 4; i++)
313 port->btno[i] = model->btno[i];
314
315 snprintf(port->name, sizeof(port->name), model->name,
316 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
317 snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
318
319 port->dev = input_dev;
320
321 input_dev->name = port->name;
322 input_dev->phys = port->phys;
323 input_dev->id.bustype = BUS_GAMEPORT;
324 input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
325 input_dev->id.product = model->id;
326 input_dev->id.version = 0x0100;
Dmitry Torokhov935e6582007-04-12 01:35:26 -0400327 input_dev->dev.parent = &tmdc->gameport->dev;
Dmitry Torokhov8715c1c2007-04-12 01:34:14 -0400328
329 input_set_drvdata(input_dev, tmdc);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500330
331 input_dev->open = tmdc_open;
332 input_dev->close = tmdc_close;
333
Jiri Slaby7b19ada2007-10-18 23:40:32 -0700334 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500335
336 for (i = 0; i < port->absc && i < TMDC_ABS; i++)
337 if (port->abs[i] >= 0)
338 input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
339
340 for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
341 input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
342
343 for (i = 0; i < 4; i++) {
344 for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
345 set_bit(port->btn[j + b], input_dev->keybit);
346 b += port->btnc[i];
347 }
348
Dmitry Torokhov4d462b92006-01-29 21:52:26 -0500349 err = input_register_device(port->dev);
350 if (err)
351 goto fail;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500352
353 return 0;
Dmitry Torokhov4d462b92006-01-29 21:52:26 -0500354
355 fail: input_free_device(input_dev);
356 kfree(port);
357 return err;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500358}
359
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360/*
361 * tmdc_probe() probes for ThrustMaster type joysticks.
362 */
363
364static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
365{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 unsigned char data[2][TMDC_MAX_LENGTH];
367 struct tmdc *tmdc;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500368 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 int err;
370
Pekka Enberga97e1482005-09-06 15:18:33 -0700371 if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 return -ENOMEM;
373
374 tmdc->gameport = gameport;
375
376 gameport_set_drvdata(gameport, tmdc);
377
378 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
379 if (err)
380 goto fail1;
381
382 if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
383 err = -ENODEV;
384 goto fail2;
385 }
386
387 gameport_set_poll_handler(gameport, tmdc_poll);
388 gameport_set_poll_interval(gameport, 20);
389
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500390 for (i = 0; i < 2; i++) {
391 if (tmdc->exists & (1 << i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500393 err = tmdc_setup_port(tmdc, i, data[i]);
394 if (err)
395 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500397 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398
399 return 0;
400
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500401 fail3: while (--i >= 0) {
402 if (tmdc->port[i]) {
403 input_unregister_device(tmdc->port[i]->dev);
404 kfree(tmdc->port[i]);
405 }
406 }
407 fail2: gameport_close(gameport);
408 fail1: gameport_set_drvdata(gameport, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 kfree(tmdc);
410 return err;
411}
412
413static void tmdc_disconnect(struct gameport *gameport)
414{
415 struct tmdc *tmdc = gameport_get_drvdata(gameport);
416 int i;
417
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500418 for (i = 0; i < 2; i++) {
419 if (tmdc->port[i]) {
420 input_unregister_device(tmdc->port[i]->dev);
421 kfree(tmdc->port[i]);
422 }
423 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 gameport_close(gameport);
425 gameport_set_drvdata(gameport, NULL);
426 kfree(tmdc);
427}
428
429static struct gameport_driver tmdc_drv = {
430 .driver = {
431 .name = "tmdc",
Dmitry Torokhov4d462b92006-01-29 21:52:26 -0500432 .owner = THIS_MODULE,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 },
434 .description = DRIVER_DESC,
435 .connect = tmdc_connect,
436 .disconnect = tmdc_disconnect,
437};
438
Axel Lin98a84132012-04-03 23:52:27 -0700439module_gameport_driver(tmdc_drv);