2 * $Id: tmdc.c,v 1.31 2002/01/22 20:29:52 vojtech Exp $
4 * Copyright (c) 1998-2001 Vojtech Pavlik
6 * Based on the work of:
7 * Trystan Larey-Williams
11 * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
29 * Should you need to contact me, the author, you can do so either by
30 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
31 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
34 #include <linux/delay.h>
35 #include <linux/kernel.h>
36 #include <linux/slab.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/gameport.h>
40 #include <linux/input.h>
41 #include <linux/jiffies.h>
43 #define DRIVER_DESC "ThrustMaster DirectConnect joystick driver"
45 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
46 MODULE_DESCRIPTION(DRIVER_DESC);
47 MODULE_LICENSE("GPL");
49 #define TMDC_MAX_START 600 /* 600 us */
50 #define TMDC_MAX_STROBE 60 /* 60 us */
51 #define TMDC_MAX_LENGTH 13
53 #define TMDC_MODE_M3DI 1
54 #define TMDC_MODE_3DRP 3
55 #define TMDC_MODE_AT 4
56 #define TMDC_MODE_FM 8
57 #define TMDC_MODE_FGP 163
59 #define TMDC_BYTE_ID 10
60 #define TMDC_BYTE_REV 11
61 #define TMDC_BYTE_DEF 12
64 #define TMDC_ABS_HAT 4
67 static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
68 static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
70 static const signed char tmdc_abs[TMDC_ABS] =
71 { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
72 static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
73 { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
74 static const signed char tmdc_abs_at[TMDC_ABS] =
75 { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
76 static const signed char tmdc_abs_fm[TMDC_ABS] =
77 { ABS_RX, ABS_RY, ABS_X, ABS_Y };
79 static const short tmdc_btn_pad[TMDC_BTN] =
80 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
81 static const short tmdc_btn_joy[TMDC_BTN] =
82 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
83 BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
84 static const short tmdc_btn_fm[TMDC_BTN] =
85 { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
86 static const short tmdc_btn_at[TMDC_BTN] =
87 { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
88 BTN_BASE3, BTN_BASE2, BTN_BASE };
93 } tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
95 static const struct tmdc_model {
102 const signed char *axes;
103 const short *buttons;
105 { 1, "ThrustMaster Millenium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
106 { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
107 { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
108 { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
109 { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
110 { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
115 struct input_dev *dev;
119 const signed char *abs;
122 unsigned char btnc[4];
123 unsigned char btno[4];
127 struct gameport *gameport;
128 struct tmdc_port *port[2];
130 struct input_dev *dev[2];
136 unsigned char absc[2];
137 unsigned char btnc[2][4];
138 unsigned char btno[2][4];
142 unsigned char exists;
146 * tmdc_read_packet() reads a ThrustMaster packet.
149 static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
151 unsigned char u, v, w, x;
153 int i[2], j[2], t[2], p, k;
155 p = gameport_time(gameport, TMDC_MAX_STROBE);
157 for (k = 0; k < 2; k++) {
158 t[k] = gameport_time(gameport, TMDC_MAX_START);
162 local_irq_save(flags);
163 gameport_trigger(gameport);
165 w = gameport_read(gameport) >> 4;
169 w = gameport_read(gameport) >> 4;
171 for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
173 if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
175 if (j[k] == 0) { /* Start bit */
176 if (~v & 1) t[k] = 0;
177 data[k][i[k]] = 0; j[k]++; continue;
179 if (j[k] == 9) { /* Stop bit */
181 j[k] = 0; i[k]++; continue;
183 data[k][i[k]] |= (~v & 1) << (j[k]++ - 1); /* Data bit */
187 } while (t[0] > 0 || t[1] > 0);
189 local_irq_restore(flags);
191 return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
194 static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
198 if (data[TMDC_BYTE_ID] != port->mode)
201 for (i = 0; i < port->absc; i++) {
202 if (port->abs[i] < 0)
205 input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
208 switch (port->mode) {
213 input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
214 input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i] & 1));
220 input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
221 input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
226 for (k = l = 0; k < 4; k++) {
227 for (i = 0; i < port->btnc[k]; i++)
228 input_report_key(port->dev, port->btn[i + l],
229 ((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
233 input_sync(port->dev);
239 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
242 static void tmdc_poll(struct gameport *gameport)
244 unsigned char data[2][TMDC_MAX_LENGTH];
245 struct tmdc *tmdc = gameport_get_drvdata(gameport);
246 unsigned char r, bad = 0;
251 if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
254 for (i = 0; i < 2; i++) {
255 if (r & (1 << i) & tmdc->exists) {
257 if (tmdc_parse_packet(tmdc->port[i], data[i]))
266 static int tmdc_open(struct input_dev *dev)
268 struct tmdc *tmdc = dev->private;
270 gameport_start_polling(tmdc->gameport);
274 static void tmdc_close(struct input_dev *dev)
276 struct tmdc *tmdc = dev->private;
278 gameport_stop_polling(tmdc->gameport);
281 static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
283 const struct tmdc_model *model;
284 struct tmdc_port *port;
285 struct input_dev *input_dev;
289 tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
290 input_dev = input_allocate_device();
291 if (!port || !input_dev) {
296 port->mode = data[TMDC_BYTE_ID];
298 for (model = tmdc_models; model->id && model->id != port->mode; model++)
301 port->abs = model->axes;
302 port->btn = model->buttons;
305 port->absc = data[TMDC_BYTE_DEF] >> 4;
306 for (i = 0; i < 4; i++)
307 port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
309 port->absc = model->abs;
310 for (i = 0; i < 4; i++)
311 port->btnc[i] = model->btnc[i];
314 for (i = 0; i < 4; i++)
315 port->btno[i] = model->btno[i];
317 snprintf(port->name, sizeof(port->name), model->name,
318 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
319 snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
321 port->dev = input_dev;
323 input_dev->name = port->name;
324 input_dev->phys = port->phys;
325 input_dev->id.bustype = BUS_GAMEPORT;
326 input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
327 input_dev->id.product = model->id;
328 input_dev->id.version = 0x0100;
329 input_dev->cdev.dev = &tmdc->gameport->dev;
330 input_dev->private = tmdc;
332 input_dev->open = tmdc_open;
333 input_dev->close = tmdc_close;
335 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
337 for (i = 0; i < port->absc && i < TMDC_ABS; i++)
338 if (port->abs[i] >= 0)
339 input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
341 for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
342 input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
344 for (i = 0; i < 4; i++) {
345 for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
346 set_bit(port->btn[j + b], input_dev->keybit);
350 err = input_register_device(port->dev);
356 fail: input_free_device(input_dev);
362 * tmdc_probe() probes for ThrustMaster type joysticks.
365 static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
367 unsigned char data[2][TMDC_MAX_LENGTH];
372 if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
375 tmdc->gameport = gameport;
377 gameport_set_drvdata(gameport, tmdc);
379 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
383 if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
388 gameport_set_poll_handler(gameport, tmdc_poll);
389 gameport_set_poll_interval(gameport, 20);
391 for (i = 0; i < 2; i++) {
392 if (tmdc->exists & (1 << i)) {
394 err = tmdc_setup_port(tmdc, i, data[i]);
402 fail3: while (--i >= 0) {
404 input_unregister_device(tmdc->port[i]->dev);
405 kfree(tmdc->port[i]);
408 fail2: gameport_close(gameport);
409 fail1: gameport_set_drvdata(gameport, NULL);
414 static void tmdc_disconnect(struct gameport *gameport)
416 struct tmdc *tmdc = gameport_get_drvdata(gameport);
419 for (i = 0; i < 2; i++) {
421 input_unregister_device(tmdc->port[i]->dev);
422 kfree(tmdc->port[i]);
425 gameport_close(gameport);
426 gameport_set_drvdata(gameport, NULL);
430 static struct gameport_driver tmdc_drv = {
433 .owner = THIS_MODULE,
435 .description = DRIVER_DESC,
436 .connect = tmdc_connect,
437 .disconnect = tmdc_disconnect,
440 static int __init tmdc_init(void)
442 gameport_register_driver(&tmdc_drv);
446 static void __exit tmdc_exit(void)
448 gameport_unregister_driver(&tmdc_drv);
451 module_init(tmdc_init);
452 module_exit(tmdc_exit);