aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/input/joystick/tmdc.c
diff options
context:
space:
mode:
Diffstat (limited to 'drivers/input/joystick/tmdc.c')
-rw-r--r--drivers/input/joystick/tmdc.c384
1 files changed, 384 insertions, 0 deletions
diff --git a/drivers/input/joystick/tmdc.c b/drivers/input/joystick/tmdc.c
new file mode 100644
index 000000000000..aaee52ceb920
--- /dev/null
+++ b/drivers/input/joystick/tmdc.c
@@ -0,0 +1,384 @@
1/*
2 * $Id: tmdc.c,v 1.31 2002/01/22 20:29:52 vojtech Exp $
3 *
4 * Copyright (c) 1998-2001 Vojtech Pavlik
5 *
6 * Based on the work of:
7 * Trystan Larey-Williams
8 */
9
10/*
11 * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
12 */
13
14/*
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.
19 *
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.
24 *
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
28 *
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
32 */
33
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
42#define DRIVER_DESC "ThrustMaster DirectConnect joystick driver"
43
44MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45MODULE_DESCRIPTION(DRIVER_DESC);
46MODULE_LICENSE("GPL");
47
48#define TMDC_MAX_START 600 /* 600 us */
49#define TMDC_MAX_STROBE 60 /* 60 us */
50#define TMDC_MAX_LENGTH 13
51
52#define TMDC_MODE_M3DI 1
53#define TMDC_MODE_3DRP 3
54#define TMDC_MODE_AT 4
55#define TMDC_MODE_FM 8
56#define TMDC_MODE_FGP 163
57
58#define TMDC_BYTE_ID 10
59#define TMDC_BYTE_REV 11
60#define TMDC_BYTE_DEF 12
61
62#define TMDC_ABS 7
63#define TMDC_ABS_HAT 4
64#define TMDC_BTN 16
65
66static unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
67static unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
68
69static signed char tmdc_abs[TMDC_ABS] =
70 { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
71static signed char tmdc_abs_hat[TMDC_ABS_HAT] =
72 { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
73static signed char tmdc_abs_at[TMDC_ABS] =
74 { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
75static signed char tmdc_abs_fm[TMDC_ABS] =
76 { ABS_RX, ABS_RY, ABS_X, ABS_Y };
77
78static short tmdc_btn_pad[TMDC_BTN] =
79 { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
80static short tmdc_btn_joy[TMDC_BTN] =
81 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
82 BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
83static short tmdc_btn_fm[TMDC_BTN] =
84 { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
85static short tmdc_btn_at[TMDC_BTN] =
86 { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
87 BTN_BASE3, BTN_BASE2, BTN_BASE };
88
89static struct {
90 int x;
91 int y;
92} tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
93
94struct tmdc {
95 struct gameport *gameport;
96 struct input_dev dev[2];
97 char name[2][64];
98 char phys[2][32];
99 int mode[2];
100 signed char *abs[2];
101 short *btn[2];
102 unsigned char absc[2];
103 unsigned char btnc[2][4];
104 unsigned char btno[2][4];
105 int reads;
106 int bads;
107 unsigned char exists;
108};
109
110/*
111 * tmdc_read_packet() reads a ThrustMaster packet.
112 */
113
114static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
115{
116 unsigned char u, v, w, x;
117 unsigned long flags;
118 int i[2], j[2], t[2], p, k;
119
120 p = gameport_time(gameport, TMDC_MAX_STROBE);
121
122 for (k = 0; k < 2; k++) {
123 t[k] = gameport_time(gameport, TMDC_MAX_START);
124 i[k] = j[k] = 0;
125 }
126
127 local_irq_save(flags);
128 gameport_trigger(gameport);
129
130 w = gameport_read(gameport) >> 4;
131
132 do {
133 x = w;
134 w = gameport_read(gameport) >> 4;
135
136 for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
137 if (~v & u & 2) {
138 if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
139 t[k] = p;
140 if (j[k] == 0) { /* Start bit */
141 if (~v & 1) t[k] = 0;
142 data[k][i[k]] = 0; j[k]++; continue;
143 }
144 if (j[k] == 9) { /* Stop bit */
145 if (v & 1) t[k] = 0;
146 j[k] = 0; i[k]++; continue;
147 }
148 data[k][i[k]] |= (~v & 1) << (j[k]++ - 1); /* Data bit */
149 }
150 t[k]--;
151 }
152 } while (t[0] > 0 || t[1] > 0);
153
154 local_irq_restore(flags);
155
156 return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
157}
158
159/*
160 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
161 */
162
163static void tmdc_poll(struct gameport *gameport)
164{
165 unsigned char data[2][TMDC_MAX_LENGTH];
166 struct tmdc *tmdc = gameport_get_drvdata(gameport);
167 struct input_dev *dev;
168 unsigned char r, bad = 0;
169 int i, j, k, l;
170
171 tmdc->reads++;
172
173 if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
174 bad = 1;
175 else
176
177 for (j = 0; j < 2; j++)
178 if (r & (1 << j) & tmdc->exists) {
179
180 if (data[j][TMDC_BYTE_ID] != tmdc->mode[j]) {
181 bad = 1;
182 continue;
183 }
184
185 dev = tmdc->dev + j;
186
187 for (i = 0; i < tmdc->absc[j]; i++) {
188 if (tmdc->abs[j][i] < 0) continue;
189 input_report_abs(dev, tmdc->abs[j][i], data[j][tmdc_byte_a[i]]);
190 }
191
192 switch (tmdc->mode[j]) {
193
194 case TMDC_MODE_M3DI:
195
196 i = tmdc_byte_d[0];
197 input_report_abs(dev, ABS_HAT0X, ((data[j][i] >> 3) & 1) - ((data[j][i] >> 1) & 1));
198 input_report_abs(dev, ABS_HAT0Y, ((data[j][i] >> 2) & 1) - ( data[j][i] & 1));
199 break;
200
201 case TMDC_MODE_AT:
202
203 i = tmdc_byte_a[3];
204 input_report_abs(dev, ABS_HAT0X, tmdc_hat_to_axis[(data[j][i] - 141) / 25].x);
205 input_report_abs(dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[j][i] - 141) / 25].y);
206 break;
207
208 }
209
210 for (k = l = 0; k < 4; k++) {
211 for (i = 0; i < tmdc->btnc[j][k]; i++)
212 input_report_key(dev, tmdc->btn[j][i + l],
213 ((data[j][tmdc_byte_d[k]] >> (i + tmdc->btno[j][k])) & 1));
214 l += tmdc->btnc[j][k];
215 }
216
217 input_sync(dev);
218 }
219
220 tmdc->bads += bad;
221}
222
223static int tmdc_open(struct input_dev *dev)
224{
225 struct tmdc *tmdc = dev->private;
226
227 gameport_start_polling(tmdc->gameport);
228 return 0;
229}
230
231static void tmdc_close(struct input_dev *dev)
232{
233 struct tmdc *tmdc = dev->private;
234
235 gameport_stop_polling(tmdc->gameport);
236}
237
238/*
239 * tmdc_probe() probes for ThrustMaster type joysticks.
240 */
241
242static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
243{
244 static struct models {
245 unsigned char id;
246 char *name;
247 char abs;
248 char hats;
249 char btnc[4];
250 char btno[4];
251 signed char *axes;
252 short *buttons;
253 } models[] = { { 1, "ThrustMaster Millenium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
254 { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
255 { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
256 { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
257 { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
258 { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }};
259
260 unsigned char data[2][TMDC_MAX_LENGTH];
261 struct tmdc *tmdc;
262 int i, j, k, l, m;
263 int err;
264
265 if (!(tmdc = kcalloc(1, sizeof(struct tmdc), GFP_KERNEL)))
266 return -ENOMEM;
267
268 tmdc->gameport = gameport;
269
270 gameport_set_drvdata(gameport, tmdc);
271
272 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
273 if (err)
274 goto fail1;
275
276 if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
277 err = -ENODEV;
278 goto fail2;
279 }
280
281 gameport_set_poll_handler(gameport, tmdc_poll);
282 gameport_set_poll_interval(gameport, 20);
283
284 for (j = 0; j < 2; j++)
285 if (tmdc->exists & (1 << j)) {
286
287 tmdc->mode[j] = data[j][TMDC_BYTE_ID];
288
289 for (m = 0; models[m].id && models[m].id != tmdc->mode[j]; m++);
290
291 tmdc->abs[j] = models[m].axes;
292 tmdc->btn[j] = models[m].buttons;
293
294 if (!models[m].id) {
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;
298 }
299
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];
304 }
305
306 sprintf(tmdc->name[j], models[m].name, models[m].abs,
307 (data[j][TMDC_BYTE_DEF] & 0xf) << 3, tmdc->mode[j]);
308
309 sprintf(tmdc->phys[j], "%s/input%d", gameport->phys, j);
310
311 tmdc->dev[j].private = tmdc;
312 tmdc->dev[j].open = tmdc_open;
313 tmdc->dev[j].close = tmdc_close;
314
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;
321
322 tmdc->dev[j].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
323
324 for (i = 0; i < models[m].abs && i < TMDC_ABS; i++)
325 if (tmdc->abs[j][i] >= 0)
326 input_set_abs_params(&tmdc->dev[j], tmdc->abs[j][i], 8, 248, 2, 4);
327
328 for (i = 0; i < models[m].hats && i < TMDC_ABS_HAT; i++)
329 input_set_abs_params(&tmdc->dev[j], tmdc_abs_hat[i], -1, 1, 0, 0);
330
331
332 for (k = l = 0; k < 4; k++) {
333 for (i = 0; i < models[m].btnc[k] && i < TMDC_BTN; i++)
334 set_bit(tmdc->btn[j][i + l], tmdc->dev[j].keybit);
335 l += models[m].btnc[k];
336 }
337
338 input_register_device(tmdc->dev + j);
339 printk(KERN_INFO "input: %s on %s\n", tmdc->name[j], gameport->phys);
340 }
341
342 return 0;
343
344fail2: gameport_close(gameport);
345fail1: gameport_set_drvdata(gameport, NULL);
346 kfree(tmdc);
347 return err;
348}
349
350static void tmdc_disconnect(struct gameport *gameport)
351{
352 struct tmdc *tmdc = gameport_get_drvdata(gameport);
353 int i;
354
355 for (i = 0; i < 2; i++)
356 if (tmdc->exists & (1 << i))
357 input_unregister_device(tmdc->dev + i);
358 gameport_close(gameport);
359 gameport_set_drvdata(gameport, NULL);
360 kfree(tmdc);
361}
362
363static struct gameport_driver tmdc_drv = {
364 .driver = {
365 .name = "tmdc",
366 },
367 .description = DRIVER_DESC,
368 .connect = tmdc_connect,
369 .disconnect = tmdc_disconnect,
370};
371
372static int __init tmdc_init(void)
373{
374 gameport_register_driver(&tmdc_drv);
375 return 0;
376}
377
378static void __exit tmdc_exit(void)
379{
380 gameport_unregister_driver(&tmdc_drv);
381}
382
383module_init(tmdc_init);
384module_exit(tmdc_exit);