1/*
2 * $Id: tmdc.c,v 1.1.1.1 2007/08/03 18:52:33 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#include <linux/jiffies.h>
42
43#define DRIVER_DESC	"ThrustMaster DirectConnect joystick driver"
44
45MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
46MODULE_DESCRIPTION(DRIVER_DESC);
47MODULE_LICENSE("GPL");
48
49#define TMDC_MAX_START		600	/* 600 us */
50#define TMDC_MAX_STROBE		60	/* 60 us */
51#define TMDC_MAX_LENGTH		13
52
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
58
59#define TMDC_BYTE_ID		10
60#define TMDC_BYTE_REV		11
61#define TMDC_BYTE_DEF		12
62
63#define TMDC_ABS		7
64#define TMDC_ABS_HAT		4
65#define TMDC_BTN		16
66
67static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
68static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
69
70static const signed char tmdc_abs[TMDC_ABS] =
71	{ ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
72static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
73	{ ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
74static const signed char tmdc_abs_at[TMDC_ABS] =
75	{ ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
76static const signed char tmdc_abs_fm[TMDC_ABS] =
77	{ ABS_RX, ABS_RY, ABS_X, ABS_Y };
78
79static 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 };
81static 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 };
84static 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 };
86static 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 };
89
90static const struct {
91        int x;
92        int y;
93} tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
94
95static const struct tmdc_model {
96	unsigned char id;
97	const char *name;
98	char abs;
99	char hats;
100	char btnc[4];
101	char btno[4];
102	const signed char *axes;
103	const short *buttons;
104} tmdc_models[] = {
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 }
111};
112
113
114struct tmdc_port {
115	struct input_dev *dev;
116	char name[64];
117	char phys[32];
118	int mode;
119	const signed char *abs;
120	const short *btn;
121	unsigned char absc;
122	unsigned char btnc[4];
123	unsigned char btno[4];
124};
125
126struct tmdc {
127	struct gameport *gameport;
128	struct tmdc_port *port[2];
129	int reads;
130	int bads;
131	unsigned char exists;
132};
133
134/*
135 * tmdc_read_packet() reads a ThrustMaster packet.
136 */
137
138static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
139{
140	unsigned char u, v, w, x;
141	unsigned long flags;
142	int i[2], j[2], t[2], p, k;
143
144	p = gameport_time(gameport, TMDC_MAX_STROBE);
145
146	for (k = 0; k < 2; k++) {
147		t[k] = gameport_time(gameport, TMDC_MAX_START);
148		i[k] = j[k] = 0;
149	}
150
151	local_irq_save(flags);
152	gameport_trigger(gameport);
153
154	w = gameport_read(gameport) >> 4;
155
156	do {
157		x = w;
158		w = gameport_read(gameport) >> 4;
159
160		for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
161			if (~v & u & 2) {
162				if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
163				t[k] = p;
164				if (j[k] == 0) {				 /* Start bit */
165					if (~v & 1) t[k] = 0;
166					data[k][i[k]] = 0; j[k]++; continue;
167				}
168				if (j[k] == 9) {				/* Stop bit */
169					if (v & 1) t[k] = 0;
170					j[k] = 0; i[k]++; continue;
171				}
172				data[k][i[k]] |= (~v & 1) << (j[k]++ - 1);	/* Data bit */
173			}
174			t[k]--;
175		}
176	} while (t[0] > 0 || t[1] > 0);
177
178	local_irq_restore(flags);
179
180	return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
181}
182
183static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
184{
185	int i, k, l;
186
187	if (data[TMDC_BYTE_ID] != port->mode)
188		return -1;
189
190	for (i = 0; i < port->absc; i++) {
191		if (port->abs[i] < 0)
192			return 0;
193
194		input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
195	}
196
197	switch (port->mode) {
198
199		case TMDC_MODE_M3DI:
200
201			i = tmdc_byte_d[0];
202			input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
203			input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i]       & 1));
204			break;
205
206		case TMDC_MODE_AT:
207
208			i = tmdc_byte_a[3];
209			input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
210			input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
211			break;
212
213	}
214
215	for (k = l = 0; k < 4; k++) {
216		for (i = 0; i < port->btnc[k]; i++)
217			input_report_key(port->dev, port->btn[i + l],
218				((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
219		l += port->btnc[k];
220	}
221
222	input_sync(port->dev);
223
224	return 0;
225}
226
227/*
228 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
229 */
230
231static void tmdc_poll(struct gameport *gameport)
232{
233	unsigned char data[2][TMDC_MAX_LENGTH];
234	struct tmdc *tmdc = gameport_get_drvdata(gameport);
235	unsigned char r, bad = 0;
236	int i;
237
238	tmdc->reads++;
239
240	if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
241		bad = 1;
242	else {
243		for (i = 0; i < 2; i++) {
244			if (r & (1 << i) & tmdc->exists) {
245
246				if (tmdc_parse_packet(tmdc->port[i], data[i]))
247					bad = 1;
248			}
249		}
250	}
251
252	tmdc->bads += bad;
253}
254
255static int tmdc_open(struct input_dev *dev)
256{
257	struct tmdc *tmdc = input_get_drvdata(dev);
258
259	gameport_start_polling(tmdc->gameport);
260	return 0;
261}
262
263static void tmdc_close(struct input_dev *dev)
264{
265	struct tmdc *tmdc = input_get_drvdata(dev);
266
267	gameport_stop_polling(tmdc->gameport);
268}
269
270static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
271{
272	const struct tmdc_model *model;
273	struct tmdc_port *port;
274	struct input_dev *input_dev;
275	int i, j, b = 0;
276	int err;
277
278	tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
279	input_dev = input_allocate_device();
280	if (!port || !input_dev) {
281		err = -ENOMEM;
282		goto fail;
283	}
284
285	port->mode = data[TMDC_BYTE_ID];
286
287	for (model = tmdc_models; model->id && model->id != port->mode; model++)
288		/* empty */;
289
290	port->abs = model->axes;
291	port->btn = model->buttons;
292
293	if (!model->id) {
294		port->absc = data[TMDC_BYTE_DEF] >> 4;
295		for (i = 0; i < 4; i++)
296			port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
297	} else {
298		port->absc = model->abs;
299		for (i = 0; i < 4; i++)
300			port->btnc[i] = model->btnc[i];
301	}
302
303	for (i = 0; i < 4; i++)
304		port->btno[i] = model->btno[i];
305
306	snprintf(port->name, sizeof(port->name), model->name,
307		 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
308	snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
309
310	port->dev = input_dev;
311
312	input_dev->name = port->name;
313	input_dev->phys = port->phys;
314	input_dev->id.bustype = BUS_GAMEPORT;
315	input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
316	input_dev->id.product = model->id;
317	input_dev->id.version = 0x0100;
318	input_dev->dev.parent = &tmdc->gameport->dev;
319
320	input_set_drvdata(input_dev, tmdc);
321
322	input_dev->open = tmdc_open;
323	input_dev->close = tmdc_close;
324
325	input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
326
327	for (i = 0; i < port->absc && i < TMDC_ABS; i++)
328		if (port->abs[i] >= 0)
329			input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
330
331	for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
332		input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
333
334	for (i = 0; i < 4; i++) {
335		for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
336			set_bit(port->btn[j + b], input_dev->keybit);
337		b += port->btnc[i];
338	}
339
340	err = input_register_device(port->dev);
341	if (err)
342		goto fail;
343
344	return 0;
345
346 fail:	input_free_device(input_dev);
347	kfree(port);
348	return err;
349}
350
351/*
352 * tmdc_probe() probes for ThrustMaster type joysticks.
353 */
354
355static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
356{
357	unsigned char data[2][TMDC_MAX_LENGTH];
358	struct tmdc *tmdc;
359	int i;
360	int err;
361
362	if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
363		return -ENOMEM;
364
365	tmdc->gameport = gameport;
366
367	gameport_set_drvdata(gameport, tmdc);
368
369	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
370	if (err)
371		goto fail1;
372
373	if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
374		err = -ENODEV;
375		goto fail2;
376	}
377
378	gameport_set_poll_handler(gameport, tmdc_poll);
379	gameport_set_poll_interval(gameport, 20);
380
381	for (i = 0; i < 2; i++) {
382		if (tmdc->exists & (1 << i)) {
383
384			err = tmdc_setup_port(tmdc, i, data[i]);
385			if (err)
386				goto fail3;
387		}
388	}
389
390	return 0;
391
392 fail3: while (--i >= 0) {
393		if (tmdc->port[i]) {
394			input_unregister_device(tmdc->port[i]->dev);
395			kfree(tmdc->port[i]);
396		}
397	}
398 fail2:	gameport_close(gameport);
399 fail1:	gameport_set_drvdata(gameport, NULL);
400	kfree(tmdc);
401	return err;
402}
403
404static void tmdc_disconnect(struct gameport *gameport)
405{
406	struct tmdc *tmdc = gameport_get_drvdata(gameport);
407	int i;
408
409	for (i = 0; i < 2; i++) {
410		if (tmdc->port[i]) {
411			input_unregister_device(tmdc->port[i]->dev);
412			kfree(tmdc->port[i]);
413		}
414	}
415	gameport_close(gameport);
416	gameport_set_drvdata(gameport, NULL);
417	kfree(tmdc);
418}
419
420static struct gameport_driver tmdc_drv = {
421	.driver		= {
422		.name	= "tmdc",
423		.owner	= THIS_MODULE,
424	},
425	.description	= DRIVER_DESC,
426	.connect	= tmdc_connect,
427	.disconnect	= tmdc_disconnect,
428};
429
430static int __init tmdc_init(void)
431{
432	gameport_register_driver(&tmdc_drv);
433	return 0;
434}
435
436static void __exit tmdc_exit(void)
437{
438	gameport_unregister_driver(&tmdc_drv);
439}
440
441module_init(tmdc_init);
442module_exit(tmdc_exit);
443