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>
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION("ThrustMaster DirectConnect joystick driver");
44 MODULE_LICENSE("GPL");
46 #define TMDC_MAX_START 400 /* 400 us */
47 #define TMDC_MAX_STROBE 45 /* 45 us */
48 #define TMDC_MAX_LENGTH 13
49 #define TMDC_REFRESH_TIME HZ/50 /* 20 ms */
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
57 #define TMDC_BYTE_ID 10
58 #define TMDC_BYTE_REV 11
59 #define TMDC_BYTE_DEF 12
62 #define TMDC_ABS_HAT 4
65 static unsigned char tmdc_byte_a
[16] = { 0, 1, 3, 4, 6, 7 };
66 static unsigned char tmdc_byte_d
[16] = { 2, 5, 8, 9 };
68 static signed char tmdc_abs
[TMDC_ABS
] =
69 { ABS_X
, ABS_Y
, ABS_RUDDER
, ABS_THROTTLE
, ABS_RX
, ABS_RY
, ABS_RZ
};
70 static signed char tmdc_abs_hat
[TMDC_ABS_HAT
] =
71 { ABS_HAT0X
, ABS_HAT0Y
, ABS_HAT1X
, ABS_HAT1Y
};
72 static signed char tmdc_abs_at
[TMDC_ABS
] =
73 { ABS_X
, ABS_Y
, ABS_RUDDER
, -1, ABS_THROTTLE
};
74 static signed char tmdc_abs_fm
[TMDC_ABS
] =
75 { ABS_RX
, ABS_RY
, ABS_X
, ABS_Y
};
77 static short tmdc_btn_pad
[TMDC_BTN
] =
78 { BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
, BTN_START
, BTN_SELECT
, BTN_TL
, BTN_TR
};
79 static short tmdc_btn_joy
[TMDC_BTN
] =
80 { BTN_TRIGGER
, BTN_THUMB
, BTN_TOP
, BTN_TOP2
, BTN_BASE
, BTN_BASE2
, BTN_THUMB2
, BTN_PINKIE
,
81 BTN_BASE3
, BTN_BASE4
, BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
};
82 static short tmdc_btn_fm
[TMDC_BTN
] =
83 { BTN_TRIGGER
, BTN_C
, BTN_B
, BTN_A
, BTN_THUMB
, BTN_X
, BTN_Y
, BTN_Z
, BTN_TOP
, BTN_TOP2
};
84 static short tmdc_btn_at
[TMDC_BTN
] =
85 { BTN_TRIGGER
, BTN_THUMB2
, BTN_PINKIE
, BTN_THUMB
, BTN_BASE6
, BTN_BASE5
, BTN_BASE4
,
86 BTN_BASE3
, BTN_BASE2
, BTN_BASE
};
91 } tmdc_hat_to_axis
[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
94 struct gameport
*gameport
;
95 struct timer_list timer
;
96 struct input_dev dev
[2];
102 unsigned char absc
[2];
103 unsigned char btnc
[2][4];
104 unsigned char btno
[2][4];
108 unsigned char exists
;
112 * tmdc_read_packet() reads a ThrustMaster packet.
115 static int tmdc_read_packet(struct gameport
*gameport
, unsigned char data
[2][TMDC_MAX_LENGTH
])
117 unsigned char u
, v
, w
, x
;
119 int i
[2], j
[2], t
[2], p
, k
;
121 p
= gameport_time(gameport
, TMDC_MAX_STROBE
);
123 for (k
= 0; k
< 2; k
++) {
124 t
[k
] = gameport_time(gameport
, TMDC_MAX_START
);
128 local_irq_save(flags
);
129 gameport_trigger(gameport
);
131 w
= gameport_read(gameport
) >> 4;
135 w
= gameport_read(gameport
) >> 4;
137 for (k
= 0, v
= w
, u
= x
; k
< 2; k
++, v
>>= 2, u
>>= 2) {
139 if (t
[k
] <= 0 || i
[k
] >= TMDC_MAX_LENGTH
) continue;
141 if (j
[k
] == 0) { /* Start bit */
142 if (~v
& 1) t
[k
] = 0;
143 data
[k
][i
[k
]] = 0; j
[k
]++; continue;
145 if (j
[k
] == 9) { /* Stop bit */
147 j
[k
] = 0; i
[k
]++; continue;
149 data
[k
][i
[k
]] |= (~v
& 1) << (j
[k
]++ - 1); /* Data bit */
153 } while (t
[0] > 0 || t
[1] > 0);
155 local_irq_restore(flags
);
157 return (i
[0] == TMDC_MAX_LENGTH
) | ((i
[1] == TMDC_MAX_LENGTH
) << 1);
161 * tmdc_read() reads and analyzes ThrustMaster joystick data.
164 static void tmdc_timer(unsigned long private)
166 unsigned char data
[2][TMDC_MAX_LENGTH
];
167 struct tmdc
*tmdc
= (void *) private;
168 struct input_dev
*dev
;
169 unsigned char r
, bad
= 0;
174 if ((r
= tmdc_read_packet(tmdc
->gameport
, data
)) != tmdc
->exists
)
178 for (j
= 0; j
< 2; j
++)
179 if (r
& (1 << j
) & tmdc
->exists
) {
181 if (data
[j
][TMDC_BYTE_ID
] != tmdc
->mode
[j
]) {
188 for (i
= 0; i
< tmdc
->absc
[j
]; i
++) {
189 if (tmdc
->abs
[j
][i
] < 0) continue;
190 input_report_abs(dev
, tmdc
->abs
[j
][i
], data
[j
][tmdc_byte_a
[i
]]);
193 switch (tmdc
->mode
[j
]) {
198 input_report_abs(dev
, ABS_HAT0X
, ((data
[j
][i
] >> 3) & 1) - ((data
[j
][i
] >> 1) & 1));
199 input_report_abs(dev
, ABS_HAT0Y
, ((data
[j
][i
] >> 2) & 1) - ( data
[j
][i
] & 1));
205 input_report_abs(dev
, ABS_HAT0X
, tmdc_hat_to_axis
[(data
[j
][i
] - 141) / 25].x
);
206 input_report_abs(dev
, ABS_HAT0Y
, tmdc_hat_to_axis
[(data
[j
][i
] - 141) / 25].y
);
211 for (k
= l
= 0; k
< 4; k
++) {
212 for (i
= 0; i
< tmdc
->btnc
[j
][k
]; i
++)
213 input_report_key(dev
, tmdc
->btn
[j
][i
+ l
],
214 ((data
[j
][tmdc_byte_d
[k
]] >> (i
+ tmdc
->btno
[j
][k
])) & 1));
215 l
+= tmdc
->btnc
[j
][k
];
223 mod_timer(&tmdc
->timer
, jiffies
+ TMDC_REFRESH_TIME
);
226 static int tmdc_open(struct input_dev
*dev
)
228 struct tmdc
*tmdc
= dev
->private;
230 mod_timer(&tmdc
->timer
, jiffies
+ TMDC_REFRESH_TIME
);
234 static void tmdc_close(struct input_dev
*dev
)
236 struct tmdc
*tmdc
= dev
->private;
238 del_timer(&tmdc
->timer
);
242 * tmdc_probe() probes for ThrustMaster type joysticks.
245 static void tmdc_connect(struct gameport
*gameport
, struct gameport_dev
*dev
)
256 } models
[] = { { 1, "ThrustMaster Millenium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs
, tmdc_btn_joy
},
257 { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs
, tmdc_btn_pad
},
258 { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at
, tmdc_btn_at
},
259 { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm
, tmdc_btn_fm
},
260 { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs
, tmdc_btn_pad
},
261 { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs
, tmdc_btn_joy
}};
263 unsigned char data
[2][TMDC_MAX_LENGTH
];
267 if (!(tmdc
= kmalloc(sizeof(struct tmdc
), GFP_KERNEL
)))
269 memset(tmdc
, 0, sizeof(struct tmdc
));
271 gameport
->private = tmdc
;
273 tmdc
->gameport
= gameport
;
274 init_timer(&tmdc
->timer
);
275 tmdc
->timer
.data
= (long) tmdc
;
276 tmdc
->timer
.function
= tmdc_timer
;
278 if (gameport_open(gameport
, dev
, GAMEPORT_MODE_RAW
))
281 if (!(tmdc
->exists
= tmdc_read_packet(gameport
, data
)))
284 for (j
= 0; j
< 2; j
++)
285 if (tmdc
->exists
& (1 << j
)) {
287 tmdc
->mode
[j
] = data
[j
][TMDC_BYTE_ID
];
289 for (m
= 0; models
[m
].id
&& models
[m
].id
!= tmdc
->mode
[j
]; m
++);
291 tmdc
->abs
[j
] = models
[m
].axes
;
292 tmdc
->btn
[j
] = models
[m
].buttons
;
295 models
[m
].abs
= data
[j
][TMDC_BYTE_DEF
] >> 4;
296 for (k
= 0; k
< 4; k
++)
297 models
[m
].btnc
[k
] = k
< (data
[j
][TMDC_BYTE_DEF
] & 0xf) ? 8 : 0;
300 tmdc
->absc
[j
] = models
[m
].abs
;
301 for (k
= 0; k
< 4; k
++) {
302 tmdc
->btnc
[j
][k
] = models
[m
].btnc
[k
];
303 tmdc
->btno
[j
][k
] = models
[m
].btno
[k
];
306 sprintf(tmdc
->name
[j
], models
[m
].name
, models
[m
].abs
,
307 (data
[j
][TMDC_BYTE_DEF
] & 0xf) << 3, tmdc
->mode
[j
]);
309 sprintf(tmdc
->phys
[j
], "%s/input%d", gameport
->phys
, j
);
311 tmdc
->dev
[j
].private = tmdc
;
312 tmdc
->dev
[j
].open
= tmdc_open
;
313 tmdc
->dev
[j
].close
= tmdc_close
;
315 tmdc
->dev
[j
].name
= tmdc
->name
[j
];
316 tmdc
->dev
[j
].phys
= tmdc
->phys
[j
];
317 tmdc
->dev
[j
].id
.bustype
= BUS_GAMEPORT
;
318 tmdc
->dev
[j
].id
.vendor
= GAMEPORT_ID_VENDOR_THRUSTMASTER
;
319 tmdc
->dev
[j
].id
.product
= models
[m
].id
;
320 tmdc
->dev
[j
].id
.version
= 0x0100;
322 tmdc
->dev
[j
].evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
324 for (i
= 0; i
< models
[m
].abs
&& i
< TMDC_ABS
; i
++) {
325 if (tmdc
->abs
[j
][i
] < 0) continue;
326 set_bit(tmdc
->abs
[j
][i
], tmdc
->dev
[j
].absbit
);
327 tmdc
->dev
[j
].absmin
[tmdc
->abs
[j
][i
]] = 8;
328 tmdc
->dev
[j
].absmax
[tmdc
->abs
[j
][i
]] = 248;
329 tmdc
->dev
[j
].absfuzz
[tmdc
->abs
[j
][i
]] = 2;
330 tmdc
->dev
[j
].absflat
[tmdc
->abs
[j
][i
]] = 4;
333 for (i
= 0; i
< models
[m
].hats
&& i
< TMDC_ABS_HAT
; i
++) {
334 set_bit(tmdc_abs_hat
[i
], tmdc
->dev
[j
].absbit
);
335 tmdc
->dev
[j
].absmin
[tmdc_abs_hat
[i
]] = -1;
336 tmdc
->dev
[j
].absmax
[tmdc_abs_hat
[i
]] = 1;
339 for (k
= l
= 0; k
< 4; k
++) {
340 for (i
= 0; i
< models
[m
].btnc
[k
] && i
< TMDC_BTN
; i
++)
341 set_bit(tmdc
->btn
[j
][i
+ l
], tmdc
->dev
[j
].keybit
);
342 l
+= models
[m
].btnc
[k
];
345 input_register_device(tmdc
->dev
+ j
);
346 printk(KERN_INFO
"input: %s on %s\n", tmdc
->name
[j
], gameport
->phys
);
350 fail2
: gameport_close(gameport
);
354 static void tmdc_disconnect(struct gameport
*gameport
)
356 struct tmdc
*tmdc
= gameport
->private;
358 for (i
= 0; i
< 2; i
++)
359 if (tmdc
->exists
& (1 << i
))
360 input_unregister_device(tmdc
->dev
+ i
);
361 gameport_close(gameport
);
365 static struct gameport_dev tmdc_dev
= {
366 .connect
= tmdc_connect
,
367 .disconnect
= tmdc_disconnect
,
370 int __init
tmdc_init(void)
372 gameport_register_device(&tmdc_dev
);
376 void __exit
tmdc_exit(void)
378 gameport_unregister_device(&tmdc_dev
);
381 module_init(tmdc_init
);
382 module_exit(tmdc_exit
);