xref: /openbmc/linux/drivers/input/joystick/tmdc.c (revision c6acb1e7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 1998-2001 Vojtech Pavlik
4  *
5  *   Based on the work of:
6  *	Trystan Larey-Williams
7  */
8 
9 /*
10  * ThrustMaster DirectConnect (BSP) joystick family driver for Linux
11  */
12 
13 /*
14  */
15 
16 #include <linux/delay.h>
17 #include <linux/kernel.h>
18 #include <linux/slab.h>
19 #include <linux/module.h>
20 #include <linux/gameport.h>
21 #include <linux/input.h>
22 #include <linux/jiffies.h>
23 
24 #define DRIVER_DESC	"ThrustMaster DirectConnect joystick driver"
25 
26 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
27 MODULE_DESCRIPTION(DRIVER_DESC);
28 MODULE_LICENSE("GPL");
29 
30 #define TMDC_MAX_START		600	/* 600 us */
31 #define TMDC_MAX_STROBE		60	/* 60 us */
32 #define TMDC_MAX_LENGTH		13
33 
34 #define TMDC_MODE_M3DI		1
35 #define TMDC_MODE_3DRP		3
36 #define TMDC_MODE_AT		4
37 #define TMDC_MODE_FM		8
38 #define TMDC_MODE_FGP		163
39 
40 #define TMDC_BYTE_ID		10
41 #define TMDC_BYTE_REV		11
42 #define TMDC_BYTE_DEF		12
43 
44 #define TMDC_ABS		7
45 #define TMDC_ABS_HAT		4
46 #define TMDC_BTN		16
47 
48 static const unsigned char tmdc_byte_a[16] = { 0, 1, 3, 4, 6, 7 };
49 static const unsigned char tmdc_byte_d[16] = { 2, 5, 8, 9 };
50 
51 static const signed char tmdc_abs[TMDC_ABS] =
52 	{ ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE, ABS_RX, ABS_RY, ABS_RZ };
53 static const signed char tmdc_abs_hat[TMDC_ABS_HAT] =
54 	{ ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y };
55 static const signed char tmdc_abs_at[TMDC_ABS] =
56 	{ ABS_X, ABS_Y, ABS_RUDDER, -1, ABS_THROTTLE };
57 static const signed char tmdc_abs_fm[TMDC_ABS] =
58 	{ ABS_RX, ABS_RY, ABS_X, ABS_Y };
59 
60 static const short tmdc_btn_pad[TMDC_BTN] =
61 	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_START, BTN_SELECT, BTN_TL, BTN_TR };
62 static const short tmdc_btn_joy[TMDC_BTN] =
63 	{ BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_THUMB2, BTN_PINKIE,
64 	  BTN_BASE3, BTN_BASE4, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z };
65 static const short tmdc_btn_fm[TMDC_BTN] =
66         { BTN_TRIGGER, BTN_C, BTN_B, BTN_A, BTN_THUMB, BTN_X, BTN_Y, BTN_Z, BTN_TOP, BTN_TOP2 };
67 static const short tmdc_btn_at[TMDC_BTN] =
68         { BTN_TRIGGER, BTN_THUMB2, BTN_PINKIE, BTN_THUMB, BTN_BASE6, BTN_BASE5, BTN_BASE4,
69           BTN_BASE3, BTN_BASE2, BTN_BASE };
70 
71 static const struct {
72         int x;
73         int y;
74 } tmdc_hat_to_axis[] = {{ 0, 0}, { 1, 0}, { 0,-1}, {-1, 0}, { 0, 1}};
75 
76 static const struct tmdc_model {
77 	unsigned char id;
78 	const char *name;
79 	char abs;
80 	char hats;
81 	char btnc[4];
82 	char btno[4];
83 	const signed char *axes;
84 	const short *buttons;
85 } tmdc_models[] = {
86 	{   1, "ThrustMaster Millennium 3D Inceptor",	  6, 2, { 4, 2 }, { 4, 6 }, tmdc_abs, tmdc_btn_joy },
87 	{   3, "ThrustMaster Rage 3D Gamepad",		  2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
88 	{   4, "ThrustMaster Attack Throttle",		  5, 2, { 4, 6 }, { 4, 2 }, tmdc_abs_at, tmdc_btn_at },
89 	{   8, "ThrustMaster FragMaster",		  4, 0, { 8, 2 }, { 0, 0 }, tmdc_abs_fm, tmdc_btn_fm },
90 	{ 163, "Thrustmaster Fusion GamePad",		  2, 0, { 8, 2 }, { 0, 0 }, tmdc_abs, tmdc_btn_pad },
91 	{   0, "Unknown %d-axis, %d-button TM device %d", 0, 0, { 0, 0 }, { 0, 0 }, tmdc_abs, tmdc_btn_joy }
92 };
93 
94 
95 struct tmdc_port {
96 	struct input_dev *dev;
97 	char name[64];
98 	char phys[32];
99 	int mode;
100 	const signed char *abs;
101 	const short *btn;
102 	unsigned char absc;
103 	unsigned char btnc[4];
104 	unsigned char btno[4];
105 };
106 
107 struct tmdc {
108 	struct gameport *gameport;
109 	struct tmdc_port *port[2];
110 #if 0
111 	struct input_dev *dev[2];
112 	char name[2][64];
113 	char phys[2][32];
114 	int mode[2];
115 	signed char *abs[2];
116 	short *btn[2];
117 	unsigned char absc[2];
118 	unsigned char btnc[2][4];
119 	unsigned char btno[2][4];
120 #endif
121 	int reads;
122 	int bads;
123 	unsigned char exists;
124 };
125 
126 /*
127  * tmdc_read_packet() reads a ThrustMaster packet.
128  */
129 
130 static int tmdc_read_packet(struct gameport *gameport, unsigned char data[2][TMDC_MAX_LENGTH])
131 {
132 	unsigned char u, v, w, x;
133 	unsigned long flags;
134 	int i[2], j[2], t[2], p, k;
135 
136 	p = gameport_time(gameport, TMDC_MAX_STROBE);
137 
138 	for (k = 0; k < 2; k++) {
139 		t[k] = gameport_time(gameport, TMDC_MAX_START);
140 		i[k] = j[k] = 0;
141 	}
142 
143 	local_irq_save(flags);
144 	gameport_trigger(gameport);
145 
146 	w = gameport_read(gameport) >> 4;
147 
148 	do {
149 		x = w;
150 		w = gameport_read(gameport) >> 4;
151 
152 		for (k = 0, v = w, u = x; k < 2; k++, v >>= 2, u >>= 2) {
153 			if (~v & u & 2) {
154 				if (t[k] <= 0 || i[k] >= TMDC_MAX_LENGTH) continue;
155 				t[k] = p;
156 				if (j[k] == 0) {				 /* Start bit */
157 					if (~v & 1) t[k] = 0;
158 					data[k][i[k]] = 0; j[k]++; continue;
159 				}
160 				if (j[k] == 9) {				/* Stop bit */
161 					if (v & 1) t[k] = 0;
162 					j[k] = 0; i[k]++; continue;
163 				}
164 				data[k][i[k]] |= (~v & 1) << (j[k]++ - 1);	/* Data bit */
165 			}
166 			t[k]--;
167 		}
168 	} while (t[0] > 0 || t[1] > 0);
169 
170 	local_irq_restore(flags);
171 
172 	return (i[0] == TMDC_MAX_LENGTH) | ((i[1] == TMDC_MAX_LENGTH) << 1);
173 }
174 
175 static int tmdc_parse_packet(struct tmdc_port *port, unsigned char *data)
176 {
177 	int i, k, l;
178 
179 	if (data[TMDC_BYTE_ID] != port->mode)
180 		return -1;
181 
182 	for (i = 0; i < port->absc; i++) {
183 		if (port->abs[i] < 0)
184 			return 0;
185 
186 		input_report_abs(port->dev, port->abs[i], data[tmdc_byte_a[i]]);
187 	}
188 
189 	switch (port->mode) {
190 
191 		case TMDC_MODE_M3DI:
192 
193 			i = tmdc_byte_d[0];
194 			input_report_abs(port->dev, ABS_HAT0X, ((data[i] >> 3) & 1) - ((data[i] >> 1) & 1));
195 			input_report_abs(port->dev, ABS_HAT0Y, ((data[i] >> 2) & 1) - ( data[i]       & 1));
196 			break;
197 
198 		case TMDC_MODE_AT:
199 
200 			i = tmdc_byte_a[3];
201 			input_report_abs(port->dev, ABS_HAT0X, tmdc_hat_to_axis[(data[i] - 141) / 25].x);
202 			input_report_abs(port->dev, ABS_HAT0Y, tmdc_hat_to_axis[(data[i] - 141) / 25].y);
203 			break;
204 
205 	}
206 
207 	for (k = l = 0; k < 4; k++) {
208 		for (i = 0; i < port->btnc[k]; i++)
209 			input_report_key(port->dev, port->btn[i + l],
210 				((data[tmdc_byte_d[k]] >> (i + port->btno[k])) & 1));
211 		l += port->btnc[k];
212 	}
213 
214 	input_sync(port->dev);
215 
216 	return 0;
217 }
218 
219 /*
220  * tmdc_poll() reads and analyzes ThrustMaster joystick data.
221  */
222 
223 static void tmdc_poll(struct gameport *gameport)
224 {
225 	unsigned char data[2][TMDC_MAX_LENGTH];
226 	struct tmdc *tmdc = gameport_get_drvdata(gameport);
227 	unsigned char r, bad = 0;
228 	int i;
229 
230 	tmdc->reads++;
231 
232 	if ((r = tmdc_read_packet(tmdc->gameport, data)) != tmdc->exists)
233 		bad = 1;
234 	else {
235 		for (i = 0; i < 2; i++) {
236 			if (r & (1 << i) & tmdc->exists) {
237 
238 				if (tmdc_parse_packet(tmdc->port[i], data[i]))
239 					bad = 1;
240 			}
241 		}
242 	}
243 
244 	tmdc->bads += bad;
245 }
246 
247 static int tmdc_open(struct input_dev *dev)
248 {
249 	struct tmdc *tmdc = input_get_drvdata(dev);
250 
251 	gameport_start_polling(tmdc->gameport);
252 	return 0;
253 }
254 
255 static void tmdc_close(struct input_dev *dev)
256 {
257 	struct tmdc *tmdc = input_get_drvdata(dev);
258 
259 	gameport_stop_polling(tmdc->gameport);
260 }
261 
262 static int tmdc_setup_port(struct tmdc *tmdc, int idx, unsigned char *data)
263 {
264 	const struct tmdc_model *model;
265 	struct tmdc_port *port;
266 	struct input_dev *input_dev;
267 	int i, j, b = 0;
268 	int err;
269 
270 	tmdc->port[idx] = port = kzalloc(sizeof (struct tmdc_port), GFP_KERNEL);
271 	input_dev = input_allocate_device();
272 	if (!port || !input_dev) {
273 		err = -ENOMEM;
274 		goto fail;
275 	}
276 
277 	port->mode = data[TMDC_BYTE_ID];
278 
279 	for (model = tmdc_models; model->id && model->id != port->mode; model++)
280 		/* empty */;
281 
282 	port->abs = model->axes;
283 	port->btn = model->buttons;
284 
285 	if (!model->id) {
286 		port->absc = data[TMDC_BYTE_DEF] >> 4;
287 		for (i = 0; i < 4; i++)
288 			port->btnc[i] = i < (data[TMDC_BYTE_DEF] & 0xf) ? 8 : 0;
289 	} else {
290 		port->absc = model->abs;
291 		for (i = 0; i < 4; i++)
292 			port->btnc[i] = model->btnc[i];
293 	}
294 
295 	for (i = 0; i < 4; i++)
296 		port->btno[i] = model->btno[i];
297 
298 	snprintf(port->name, sizeof(port->name), model->name,
299 		 port->absc, (data[TMDC_BYTE_DEF] & 0xf) << 3, port->mode);
300 	snprintf(port->phys, sizeof(port->phys), "%s/input%d", tmdc->gameport->phys, i);
301 
302 	port->dev = input_dev;
303 
304 	input_dev->name = port->name;
305 	input_dev->phys = port->phys;
306 	input_dev->id.bustype = BUS_GAMEPORT;
307 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_THRUSTMASTER;
308 	input_dev->id.product = model->id;
309 	input_dev->id.version = 0x0100;
310 	input_dev->dev.parent = &tmdc->gameport->dev;
311 
312 	input_set_drvdata(input_dev, tmdc);
313 
314 	input_dev->open = tmdc_open;
315 	input_dev->close = tmdc_close;
316 
317 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
318 
319 	for (i = 0; i < port->absc && i < TMDC_ABS; i++)
320 		if (port->abs[i] >= 0)
321 			input_set_abs_params(input_dev, port->abs[i], 8, 248, 2, 4);
322 
323 	for (i = 0; i < model->hats && i < TMDC_ABS_HAT; i++)
324 		input_set_abs_params(input_dev, tmdc_abs_hat[i], -1, 1, 0, 0);
325 
326 	for (i = 0; i < 4; i++) {
327 		for (j = 0; j < port->btnc[i] && j < TMDC_BTN; j++)
328 			set_bit(port->btn[j + b], input_dev->keybit);
329 		b += port->btnc[i];
330 	}
331 
332 	err = input_register_device(port->dev);
333 	if (err)
334 		goto fail;
335 
336 	return 0;
337 
338  fail:	input_free_device(input_dev);
339 	kfree(port);
340 	return err;
341 }
342 
343 /*
344  * tmdc_probe() probes for ThrustMaster type joysticks.
345  */
346 
347 static int tmdc_connect(struct gameport *gameport, struct gameport_driver *drv)
348 {
349 	unsigned char data[2][TMDC_MAX_LENGTH];
350 	struct tmdc *tmdc;
351 	int i;
352 	int err;
353 
354 	if (!(tmdc = kzalloc(sizeof(struct tmdc), GFP_KERNEL)))
355 		return -ENOMEM;
356 
357 	tmdc->gameport = gameport;
358 
359 	gameport_set_drvdata(gameport, tmdc);
360 
361 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
362 	if (err)
363 		goto fail1;
364 
365 	if (!(tmdc->exists = tmdc_read_packet(gameport, data))) {
366 		err = -ENODEV;
367 		goto fail2;
368 	}
369 
370 	gameport_set_poll_handler(gameport, tmdc_poll);
371 	gameport_set_poll_interval(gameport, 20);
372 
373 	for (i = 0; i < 2; i++) {
374 		if (tmdc->exists & (1 << i)) {
375 
376 			err = tmdc_setup_port(tmdc, i, data[i]);
377 			if (err)
378 				goto fail3;
379 		}
380 	}
381 
382 	return 0;
383 
384  fail3: while (--i >= 0) {
385 		if (tmdc->port[i]) {
386 			input_unregister_device(tmdc->port[i]->dev);
387 			kfree(tmdc->port[i]);
388 		}
389 	}
390  fail2:	gameport_close(gameport);
391  fail1:	gameport_set_drvdata(gameport, NULL);
392 	kfree(tmdc);
393 	return err;
394 }
395 
396 static void tmdc_disconnect(struct gameport *gameport)
397 {
398 	struct tmdc *tmdc = gameport_get_drvdata(gameport);
399 	int i;
400 
401 	for (i = 0; i < 2; i++) {
402 		if (tmdc->port[i]) {
403 			input_unregister_device(tmdc->port[i]->dev);
404 			kfree(tmdc->port[i]);
405 		}
406 	}
407 	gameport_close(gameport);
408 	gameport_set_drvdata(gameport, NULL);
409 	kfree(tmdc);
410 }
411 
412 static struct gameport_driver tmdc_drv = {
413 	.driver		= {
414 		.name	= "tmdc",
415 		.owner	= THIS_MODULE,
416 	},
417 	.description	= DRIVER_DESC,
418 	.connect	= tmdc_connect,
419 	.disconnect	= tmdc_disconnect,
420 };
421 
422 module_gameport_driver(tmdc_drv);
423