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 #define DRIVER_DESC "ThrustMaster DirectConnect joystick driver"
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45 MODULE_DESCRIPTION(DRIVER_DESC
);
46 MODULE_LICENSE("GPL");
48 #define TMDC_MAX_START 600 /* 600 us */
49 #define TMDC_MAX_STROBE 60 /* 60 us */
50 #define TMDC_MAX_LENGTH 13
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
58 #define TMDC_BYTE_ID 10
59 #define TMDC_BYTE_REV 11
60 #define TMDC_BYTE_DEF 12
63 #define TMDC_ABS_HAT 4
66 static unsigned char tmdc_byte_a
[16] = { 0, 1, 3, 4, 6, 7 };
67 static unsigned char tmdc_byte_d
[16] = { 2, 5, 8, 9 };
69 static signed char tmdc_abs
[TMDC_ABS
] =
70 { ABS_X
, ABS_Y
, ABS_RUDDER
, ABS_THROTTLE
, ABS_RX
, ABS_RY
, ABS_RZ
};
71 static signed char tmdc_abs_hat
[TMDC_ABS_HAT
] =
72 { ABS_HAT0X
, ABS_HAT0Y
, ABS_HAT1X
, ABS_HAT1Y
};
73 static signed char tmdc_abs_at
[TMDC_ABS
] =
74 { ABS_X
, ABS_Y
, ABS_RUDDER
, -1, ABS_THROTTLE
};
75 static signed char tmdc_abs_fm
[TMDC_ABS
] =
76 { ABS_RX
, ABS_RY
, ABS_X
, ABS_Y
};
78 static 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
};
80 static 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
};
83 static 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
};
85 static 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
};
92 } tmdc_hat_to_axis
[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
95 struct gameport
*gameport
;
96 struct input_dev dev
[2];
102 unsigned char absc
[2];
103 unsigned char btnc
[2][4];
104 unsigned char btno
[2][4];
107 unsigned char exists
;
111 * tmdc_read_packet() reads a ThrustMaster packet.
114 static int tmdc_read_packet(struct gameport
*gameport
, unsigned char data
[2][TMDC_MAX_LENGTH
])
116 unsigned char u
, v
, w
, x
;
118 int i
[2], j
[2], t
[2], p
, k
;
120 p
= gameport_time(gameport
, TMDC_MAX_STROBE
);
122 for (k
= 0; k
< 2; k
++) {
123 t
[k
] = gameport_time(gameport
, TMDC_MAX_START
);
127 local_irq_save(flags
);
128 gameport_trigger(gameport
);
130 w
= gameport_read(gameport
) >> 4;
134 w
= gameport_read(gameport
) >> 4;
136 for (k
= 0, v
= w
, u
= x
; k
< 2; k
++, v
>>= 2, u
>>= 2) {
138 if (t
[k
] <= 0 || i
[k
] >= TMDC_MAX_LENGTH
) continue;
140 if (j
[k
] == 0) { /* Start bit */
141 if (~v
& 1) t
[k
] = 0;
142 data
[k
][i
[k
]] = 0; j
[k
]++; continue;
144 if (j
[k
] == 9) { /* Stop bit */
146 j
[k
] = 0; i
[k
]++; continue;
148 data
[k
][i
[k
]] |= (~v
& 1) << (j
[k
]++ - 1); /* Data bit */
152 } while (t
[0] > 0 || t
[1] > 0);
154 local_irq_restore(flags
);
156 return (i
[0] == TMDC_MAX_LENGTH
) | ((i
[1] == TMDC_MAX_LENGTH
) << 1);
160 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
163 static void tmdc_poll(struct gameport
*gameport
)
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;
173 if ((r
= tmdc_read_packet(tmdc
->gameport
, data
)) != tmdc
->exists
)
177 for (j
= 0; j
< 2; j
++)
178 if (r
& (1 << j
) & tmdc
->exists
) {
180 if (data
[j
][TMDC_BYTE_ID
] != tmdc
->mode
[j
]) {
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
]]);
192 switch (tmdc
->mode
[j
]) {
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));
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
);
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
];
223 static int tmdc_open(struct input_dev
*dev
)
225 struct tmdc
*tmdc
= dev
->private;
227 gameport_start_polling(tmdc
->gameport
);
231 static void tmdc_close(struct input_dev
*dev
)
233 struct tmdc
*tmdc
= dev
->private;
235 gameport_stop_polling(tmdc
->gameport
);
239 * tmdc_probe() probes for ThrustMaster type joysticks.
242 static int tmdc_connect(struct gameport
*gameport
, struct gameport_driver
*drv
)
244 static struct models
{
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
}};
260 unsigned char data
[2][TMDC_MAX_LENGTH
];
265 if (!(tmdc
= kcalloc(1, sizeof(struct tmdc
), GFP_KERNEL
)))
268 tmdc
->gameport
= gameport
;
270 gameport_set_drvdata(gameport
, tmdc
);
272 err
= gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
);
276 if (!(tmdc
->exists
= tmdc_read_packet(gameport
, data
))) {
281 gameport_set_poll_handler(gameport
, tmdc_poll
);
282 gameport_set_poll_interval(gameport
, 20);
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)
326 input_set_abs_params(&tmdc
->dev
[j
], tmdc
->abs
[j
][i
], 8, 248, 2, 4);
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);
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
];
338 input_register_device(tmdc
->dev
+ j
);
339 printk(KERN_INFO
"input: %s on %s\n", tmdc
->name
[j
], gameport
->phys
);
344 fail2
: gameport_close(gameport
);
345 fail1
: gameport_set_drvdata(gameport
, NULL
);
350 static void tmdc_disconnect(struct gameport
*gameport
)
352 struct tmdc
*tmdc
= gameport_get_drvdata(gameport
);
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
);
363 static struct gameport_driver tmdc_drv
= {
367 .description
= DRIVER_DESC
,
368 .connect
= tmdc_connect
,
369 .disconnect
= tmdc_disconnect
,
372 static int __init
tmdc_init(void)
374 gameport_register_driver(&tmdc_drv
);
378 static void __exit
tmdc_exit(void)
380 gameport_unregister_driver(&tmdc_drv
);
383 module_init(tmdc_init
);
384 module_exit(tmdc_exit
);