blob: 3a7d1bb46472b7c616c22ace3706029ea2c70edd [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050066static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
67static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
Linus Torvalds1da177e2005-04-16 15:20:36 -070068
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050069static const signed char tmdc_abs[TMDC_ABS] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070070 { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050071static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050073static const signed char tmdc_abs_at[TMDC_ABS] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070074 { ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050075static const signed char tmdc_abs_fm[TMDC_ABS] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 { ABS_RX, ABS_RY, ABS_X, ABS_Y };
77
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050078static const short tmdc_btn_pad[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 { 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 -050080static const short tmdc_btn_joy[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
Dmitry Torokhovab0c3442005-05-29 02:28:55 -050082 BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050083static const short tmdc_btn_fm[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 { 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 -050085static const short tmdc_btn_at[TMDC_BTN] =
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
87 BTN_BASE3, BTN_BASE2, BTN_BASE };
88
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050089static const struct {
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int x;
91 int y;
92} tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
93
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050094static const struct tmdc_model {
95 unsigned char id;
96 const char *name;
97 char abs;
98 char hats;
99 char btnc[4];
100 char btno[4];
101 const signed char *axes;
102 const short *buttons;
103} tmdc_models[] = {
104 { 1, "ThrustMaster Millenium 3D Inceptor", 6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
105 { 3, "ThrustMaster Rage 3D Gamepad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
106 { 4, "ThrustMaster Attack Throttle", 5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
107 { 8, "ThrustMaster FragMaster", 4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
108 { 163, "Thrustmaster Fusion GamePad", 2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
109 { 0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
110};
111
112
113struct tmdc_port {
114 struct input_dev *dev;
115 char name[64];
116 char phys[32];
117 int mode;
118 const signed char *abs;
119 const short *btn;
120 unsigned char absc;
121 unsigned char btnc[4];
122 unsigned char btno[4];
123};
124
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125struct tmdc {
126 struct gameport *gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500127 struct tmdc_port *port[2];
128#if 0
129 struct input_dev *dev[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 char name[2][64];
131 char phys[2][32];
132 int mode[2];
133 signed char *abs[2];
134 short *btn[2];
135 unsigned char absc[2];
136 unsigned char btnc[2][4];
137 unsigned char btno[2][4];
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500138#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 int reads;
140 int bads;
141 unsigned char exists;
142};
143
144/*
145 * tmdc_read_packet() reads a ThrustMaster packet.
146 */
147
148static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
149{
150 unsigned char u, v, w, x;
151 unsigned long flags;
152 int i[2], j[2], t[2], p, k;
153
154 p = gameport_time(gameport, TMDC_MAX_STROBE);
155
156 for (k = 0; k < 2; k++) {
157 t[k] = gameport_time(gameport, TMDC_MAX_START);
158 i[k] = j[k] = 0;
159 }
160
161 local_irq_save(flags);
162 gameport_trigger(gameport);
163
164 w = gameport_read(gameport) >> 4;
165
166 do {
167 x = w;
168 w = gameport_read(gameport) >> 4;
169
170 for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
171 if (~v & u & 2) {
172 if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
173 t[k] = p;
174 if (j[k] == 0) { /* Start bit */
175 if (~v & 1) t[k] = 0;
176 data[k][i[k]] = 0; j[k]++; continue;
177 }
178 if (j[k] == 9) { /* Stop bit */
179 if (v & 1) t[k] = 0;
180 j[k] = 0; i[k]++; continue;
181 }
182 data[k][i[k]] |= (~v & 1) << (j[k]++ - 1); /* Data bit */
183 }
184 t[k]--;
185 }
186 } while (t[0] > 0 || t[1] > 0);
187
188 local_irq_restore(flags);
189
190 return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
191}
192
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500193static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
194{
195 int i, k, l;
196
197 if (data[TMDC_BYTE_ID] != port->mode)
198 return -1;
199
200 for (i = 0; i < port->absc; i++) {
201 if (port->abs[i] < 0)
202 return 0;
203
204 input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
205 }
206
207 switch (port->mode) {
208
209 case TMDC_MODE_M3DI:
210
211 i = tmdc_byte_d[0];
212 input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
213 input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i] & 1));
214 break;
215
216 case TMDC_MODE_AT:
217
218 i = tmdc_byte_a[3];
219 input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
220 input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
221 break;
222
223 }
224
225 for (k = l = 0; k < 4; k++) {
226 for (i = 0; i < port->btnc[k]; i++)
227 input_report_key(port->dev, port->btn[i + l],
228 ((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
229 l += port->btnc[k];
230 }
231
232 input_sync(port->dev);
233
234 return 0;
235}
236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237/*
238 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
239 */
240
241static void tmdc_poll(struct gameport *gameport)
242{
243 unsigned char data[2][TMDC_MAX_LENGTH];
244 struct tmdc *tmdc = gameport_get_drvdata(gameport);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 unsigned char r, bad = 0;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500246 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
248 tmdc->reads++;
249
250 if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
251 bad = 1;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500252 else {
253 for (i = 0; i < 2; i++) {
254 if (r & (1 << i) & tmdc->exists) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500256 if (tmdc_parse_packet(tmdc->port[i], data[i]))
257 bad = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500259 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260 }
261
262 tmdc->bads += bad;
263}
264
265static int tmdc_open(struct input_dev *dev)
266{
267 struct tmdc *tmdc = dev->private;
268
269 gameport_start_polling(tmdc->gameport);
270 return 0;
271}
272
273static void tmdc_close(struct input_dev *dev)
274{
275 struct tmdc *tmdc = dev->private;
276
277 gameport_stop_polling(tmdc->gameport);
278}
279
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500280static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
281{
282 const struct tmdc_model *model;
283 struct tmdc_port *port;
284 struct input_dev *input_dev;
285 int i, j, b = 0;
286
287 tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
288 input_dev = input_allocate_device();
289 if (!port || !input_dev) {
290 kfree(port);
291 input_free_device(input_dev);
292 return -ENOMEM;
293 }
294
295 port->mode = data[TMDC_BYTE_ID];
296
297 for (model = tmdc_models; model->id && model->id != port->mode; model++)
298 /* empty */;
299
300 port->abs = model->axes;
301 port->btn = model->buttons;
302
303 if (!model->id) {
304 port->absc = data[TMDC_BYTE_DEF] >> 4;
305 for (i = 0; i < 4; i++)
306 port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
307 } else {
308 port->absc = model->abs;
309 for (i = 0; i < 4; i++)
310 port->btnc[i] = model->btnc[i];
311 }
312
313 for (i = 0; i < 4; i++)
314 port->btno[i] = model->btno[i];
315
316 snprintf(port->name, sizeof(port->name), model->name,
317 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
318 snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
319
320 port->dev = input_dev;
321
322 input_dev->name = port->name;
323 input_dev->phys = port->phys;
324 input_dev->id.bustype = BUS_GAMEPORT;
325 input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
326 input_dev->id.product = model->id;
327 input_dev->id.version = 0x0100;
328 input_dev->cdev.dev = &tmdc->gameport->dev;
329 input_dev->private = tmdc;
330
331 input_dev->open = tmdc_open;
332 input_dev->close = tmdc_close;
333
334 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
335
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
349 input_register_device(port->dev);
350
351 return 0;
352}
353
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354/*
355 * tmdc_probe() probes for ThrustMaster type joysticks.
356 */
357
358static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
359{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 unsigned char data[2][TMDC_MAX_LENGTH];
361 struct tmdc *tmdc;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500362 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 int err;
364
Pekka Enberga97e1482005-09-06 15:18:33 -0700365 if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 return -ENOMEM;
367
368 tmdc->gameport = gameport;
369
370 gameport_set_drvdata(gameport, tmdc);
371
372 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
373 if (err)
374 goto fail1;
375
376 if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
377 err = -ENODEV;
378 goto fail2;
379 }
380
381 gameport_set_poll_handler(gameport, tmdc_poll);
382 gameport_set_poll_interval(gameport, 20);
383
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500384 for (i = 0; i < 2; i++) {
385 if (tmdc->exists & (1 << i)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500387 err = tmdc_setup_port(tmdc, i, data[i]);
388 if (err)
389 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500391 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392
393 return 0;
394
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500395 fail3: while (--i >= 0) {
396 if (tmdc->port[i]) {
397 input_unregister_device(tmdc->port[i]->dev);
398 kfree(tmdc->port[i]);
399 }
400 }
401 fail2: gameport_close(gameport);
402 fail1: gameport_set_drvdata(gameport, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 kfree(tmdc);
404 return err;
405}
406
407static void tmdc_disconnect(struct gameport *gameport)
408{
409 struct tmdc *tmdc = gameport_get_drvdata(gameport);
410 int i;
411
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500412 for (i = 0; i < 2; i++) {
413 if (tmdc->port[i]) {
414 input_unregister_device(tmdc->port[i]->dev);
415 kfree(tmdc->port[i]);
416 }
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 gameport_close(gameport);
419 gameport_set_drvdata(gameport, NULL);
420 kfree(tmdc);
421}
422
423static struct gameport_driver tmdc_drv = {
424 .driver = {
425 .name = "tmdc",
426 },
427 .description = DRIVER_DESC,
428 .connect = tmdc_connect,
429 .disconnect = tmdc_disconnect,
430};
431
432static int __init tmdc_init(void)
433{
434 gameport_register_driver(&tmdc_drv);
435 return 0;
436}
437
438static void __exit tmdc_exit(void)
439{
440 gameport_unregister_driver(&tmdc_drv);
441}
442
443module_init(tmdc_init);
444module_exit(tmdc_exit);