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