• Home
  • History
  • Annotate
  • Line#
  • Navigate
  • Raw
  • Download
  • only in /asuswrt-rt-n18u-9.0.0.4.380.2695/release/src-rt-6.x.4708/linux/linux-2.6.36/drivers/input/joystick/
1/*
2 *  Copyright (c) 1998-2001 Vojtech Pavlik
3 *
4 *   Based on the work of:
5 *	Trystan Larey-Williams
6 */
7
8/*
9 * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
10 */
11
12/*
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
26 *
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
30 */
31
32#include <linux/delay.h>
33#include <linux/kernel.h>
34#include <linux/slab.h>
35#include <linux/module.h>
36#include <linux/init.h>
37#include <linux/gameport.h>
38#include <linux/input.h>
39#include <linux/jiffies.h>
40
41#define DRIVER_DESC	"ThrustMaster DirectConnect joystick driver"
42
43MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
44MODULE_DESCRIPTION(DRIVER_DESC);
45MODULE_LICENSE("GPL");
46
47#define TMDC_MAX_START		600	/* 600 us */
48#define TMDC_MAX_STROBE		60	/* 60 us */
49#define TMDC_MAX_LENGTH		13
50
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
56
57#define TMDC_BYTE_ID		10
58#define TMDC_BYTE_REV		11
59#define TMDC_BYTE_DEF		12
60
61#define TMDC_ABS		7
62#define TMDC_ABS_HAT		4
63#define TMDC_BTN		16
64
65static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
66static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
67
68static const signed char tmdc_abs[TMDC_ABS] =
69	{ ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
70static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
71	{ ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
72static const signed char tmdc_abs_at[TMDC_ABS] =
73	{ ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
74static const signed char tmdc_abs_fm[TMDC_ABS] =
75	{ ABS_RX, ABS_RY, ABS_X, ABS_Y };
76
77static const 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 };
79static const 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 };
82static const 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 };
84static const 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 };
87
88static const struct {
89        int x;
90        int y;
91} tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
92
93static const struct tmdc_model {
94	unsigned char id;
95	const char *name;
96	char abs;
97	char hats;
98	char btnc[4];
99	char btno[4];
100	const signed char *axes;
101	const short *buttons;
102} tmdc_models[] = {
103	{   1, "ThrustMaster Millenium 3D Inceptor",	  6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
104	{   3, "ThrustMaster Rage 3D Gamepad",		  2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
105	{   4, "ThrustMaster Attack Throttle",		  5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
106	{   8, "ThrustMaster FragMaster",		  4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
107	{ 163, "Thrustmaster Fusion GamePad",		  2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
108	{   0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
109};
110
111
112struct tmdc_port {
113	struct input_dev *dev;
114	char name[64];
115	char phys[32];
116	int mode;
117	const signed char *abs;
118	const short *btn;
119	unsigned char absc;
120	unsigned char btnc[4];
121	unsigned char btno[4];
122};
123
124struct tmdc {
125	struct gameport *gameport;
126	struct tmdc_port *port[2];
127	int reads;
128	int bads;
129	unsigned char exists;
130};
131
132/*
133 * tmdc_read_packet() reads a ThrustMaster packet.
134 */
135
136static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
137{
138	unsigned char u, v, w, x;
139	unsigned long flags;
140	int i[2], j[2], t[2], p, k;
141
142	p = gameport_time(gameport, TMDC_MAX_STROBE);
143
144	for (k = 0; k < 2; k++) {
145		t[k] = gameport_time(gameport, TMDC_MAX_START);
146		i[k] = j[k] = 0;
147	}
148
149	local_irq_save(flags);
150	gameport_trigger(gameport);
151
152	w = gameport_read(gameport) >> 4;
153
154	do {
155		x = w;
156		w = gameport_read(gameport) >> 4;
157
158		for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
159			if (~v & u & 2) {
160				if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
161				t[k] = p;
162				if (j[k] == 0) {				 /* Start bit */
163					if (~v & 1) t[k] = 0;
164					data[k][i[k]] = 0; j[k]++; continue;
165				}
166				if (j[k] == 9) {				/* Stop bit */
167					if (v & 1) t[k] = 0;
168					j[k] = 0; i[k]++; continue;
169				}
170				data[k][i[k]] |= (~v & 1) << (j[k]++ - 1);	/* Data bit */
171			}
172			t[k]--;
173		}
174	} while (t[0] > 0 || t[1] > 0);
175
176	local_irq_restore(flags);
177
178	return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
179}
180
181static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
182{
183	int i, k, l;
184
185	if (data[TMDC_BYTE_ID] != port->mode)
186		return -1;
187
188	for (i = 0; i < port->absc; i++) {
189		if (port->abs[i] < 0)
190			return 0;
191
192		input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
193	}
194
195	switch (port->mode) {
196
197		case TMDC_MODE_M3DI:
198
199			i = tmdc_byte_d[0];
200			input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
201			input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i]       & 1));
202			break;
203
204		case TMDC_MODE_AT:
205
206			i = tmdc_byte_a[3];
207			input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
208			input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
209			break;
210
211	}
212
213	for (k = l = 0; k < 4; k++) {
214		for (i = 0; i < port->btnc[k]; i++)
215			input_report_key(port->dev, port->btn[i + l],
216				((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
217		l += port->btnc[k];
218	}
219
220	input_sync(port->dev);
221
222	return 0;
223}
224
225/*
226 * tmdc_poll() reads and analyzes ThrustMaster joystick data.
227 */
228
229static void tmdc_poll(struct gameport *gameport)
230{
231	unsigned char data[2][TMDC_MAX_LENGTH];
232	struct tmdc *tmdc = gameport_get_drvdata(gameport);
233	unsigned char r, bad = 0;
234	int i;
235
236	tmdc->reads++;
237
238	if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
239		bad = 1;
240	else {
241		for (i = 0; i < 2; i++) {
242			if (r & (1 << i) & tmdc->exists) {
243
244				if (tmdc_parse_packet(tmdc->port[i], data[i]))
245					bad = 1;
246			}
247		}
248	}
249
250	tmdc->bads += bad;
251}
252
253static int tmdc_open(struct input_dev *dev)
254{
255	struct tmdc *tmdc = input_get_drvdata(dev);
256
257	gameport_start_polling(tmdc->gameport);
258	return 0;
259}
260
261static void tmdc_close(struct input_dev *dev)
262{
263	struct tmdc *tmdc = input_get_drvdata(dev);
264
265	gameport_stop_polling(tmdc->gameport);
266}
267
268static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
269{
270	const struct tmdc_model *model;
271	struct tmdc_port *port;
272	struct input_dev *input_dev;
273	int i, j, b = 0;
274	int err;
275
276	tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
277	input_dev = input_allocate_device();
278	if (!port || !input_dev) {
279		err = -ENOMEM;
280		goto fail;
281	}
282
283	port->mode = data[TMDC_BYTE_ID];
284
285	for (model = tmdc_models; model->id && model->id != port->mode; model++)
286		/* empty */;
287
288	port->abs = model->axes;
289	port->btn = model->buttons;
290
291	if (!model->id) {
292		port->absc = data[TMDC_BYTE_DEF] >> 4;
293		for (i = 0; i < 4; i++)
294			port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
295	} else {
296		port->absc = model->abs;
297		for (i = 0; i < 4; i++)
298			port->btnc[i] = model->btnc[i];
299	}
300
301	for (i = 0; i < 4; i++)
302		port->btno[i] = model->btno[i];
303
304	snprintf(port->name, sizeof(port->name), model->name,
305		 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
306	snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
307
308	port->dev = input_dev;
309
310	input_dev->name = port->name;
311	input_dev->phys = port->phys;
312	input_dev->id.bustype = BUS_GAMEPORT;
313	input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
314	input_dev->id.product = model->id;
315	input_dev->id.version = 0x0100;
316	input_dev->dev.parent = &tmdc->gameport->dev;
317
318	input_set_drvdata(input_dev, tmdc);
319
320	input_dev->open = tmdc_open;
321	input_dev->close = tmdc_close;
322
323	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
324
325	for (i = 0; i < port->absc && i < TMDC_ABS; i++)
326		if (port->abs[i] >= 0)
327			input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
328
329	for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
330		input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
331
332	for (i = 0; i < 4; i++) {
333		for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
334			set_bit(port->btn[j + b], input_dev->keybit);
335		b += port->btnc[i];
336	}
337
338	err = input_register_device(port->dev);
339	if (err)
340		goto fail;
341
342	return 0;
343
344 fail:	input_free_device(input_dev);
345	kfree(port);
346	return err;
347}
348
349/*
350 * tmdc_probe() probes for ThrustMaster type joysticks.
351 */
352
353static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
354{
355	unsigned char data[2][TMDC_MAX_LENGTH];
356	struct tmdc *tmdc;
357	int i;
358	int err;
359
360	if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
361		return -ENOMEM;
362
363	tmdc->gameport = gameport;
364
365	gameport_set_drvdata(gameport, tmdc);
366
367	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
368	if (err)
369		goto fail1;
370
371	if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
372		err = -ENODEV;
373		goto fail2;
374	}
375
376	gameport_set_poll_handler(gameport, tmdc_poll);
377	gameport_set_poll_interval(gameport, 20);
378
379	for (i = 0; i < 2; i++) {
380		if (tmdc->exists & (1 << i)) {
381
382			err = tmdc_setup_port(tmdc, i, data[i]);
383			if (err)
384				goto fail3;
385		}
386	}
387
388	return 0;
389
390 fail3: while (--i >= 0) {
391		if (tmdc->port[i]) {
392			input_unregister_device(tmdc->port[i]->dev);
393			kfree(tmdc->port[i]);
394		}
395	}
396 fail2:	gameport_close(gameport);
397 fail1:	gameport_set_drvdata(gameport, NULL);
398	kfree(tmdc);
399	return err;
400}
401
402static void tmdc_disconnect(struct gameport *gameport)
403{
404	struct tmdc *tmdc = gameport_get_drvdata(gameport);
405	int i;
406
407	for (i = 0; i < 2; i++) {
408		if (tmdc->port[i]) {
409			input_unregister_device(tmdc->port[i]->dev);
410			kfree(tmdc->port[i]);
411		}
412	}
413	gameport_close(gameport);
414	gameport_set_drvdata(gameport, NULL);
415	kfree(tmdc);
416}
417
418static struct gameport_driver tmdc_drv = {
419	.driver		= {
420		.name	= "tmdc",
421		.owner	= THIS_MODULE,
422	},
423	.description	= DRIVER_DESC,
424	.connect	= tmdc_connect,
425	.disconnect	= tmdc_disconnect,
426};
427
428static int __init tmdc_init(void)
429{
430	return gameport_register_driver(&tmdc_drv);
431}
432
433static void __exit tmdc_exit(void)
434{
435	gameport_unregister_driver(&tmdc_drv);
436}
437
438module_init(tmdc_init);
439module_exit(tmdc_exit);
440