xref: /linux/drivers/input/joystick/grip.c (revision 0d3b051adbb72ed81956447d0d1e54d5943ee6f5)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 1998-2001 Vojtech Pavlik
4  */
5 
6 /*
7  * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
8  */
9 
10 /*
11  */
12 
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/gameport.h>
17 #include <linux/input.h>
18 #include <linux/jiffies.h>
19 
20 #define DRIVER_DESC	"Gravis GrIP protocol joystick driver"
21 
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION(DRIVER_DESC);
24 MODULE_LICENSE("GPL");
25 
26 #define GRIP_MODE_GPP		1
27 #define GRIP_MODE_BD		2
28 #define GRIP_MODE_XT		3
29 #define GRIP_MODE_DC		4
30 
31 #define GRIP_LENGTH_GPP		24
32 #define GRIP_STROBE_GPP		200	/* 200 us */
33 #define GRIP_LENGTH_XT		4
34 #define GRIP_STROBE_XT		64	/* 64 us */
35 #define GRIP_MAX_CHUNKS_XT	10
36 #define GRIP_MAX_BITS_XT	30
37 
38 struct grip {
39 	struct gameport *gameport;
40 	struct input_dev *dev[2];
41 	unsigned char mode[2];
42 	int reads;
43 	int bads;
44 	char phys[2][32];
45 };
46 
47 static int grip_btn_gpp[] = { BTN_START, BTN_SELECT, BTN_TR2, BTN_Y, 0, BTN_TL2, BTN_A, BTN_B, BTN_X, 0, BTN_TL, BTN_TR, -1 };
48 static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
49 static int grip_btn_xt[] = { BTN_TRIGGER, BTN_THUMB, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_SELECT, BTN_START, BTN_MODE, -1 };
50 static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
51 
52 static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
53 static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
54 static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
55 static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
56 
57 static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
58 				"Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
59 static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
60 static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
61 static char grip_anx[] = { 0, 0, 3, 5, 5 };
62 static char grip_cen[] = { 0, 0, 2, 2, 4 };
63 
64 /*
65  * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
66  */
67 
68 static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
69 {
70 	unsigned long flags;
71 	unsigned char u, v;
72 	unsigned int t;
73 	int i;
74 
75 	int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
76 
77 	data[0] = 0;
78 	t = strobe;
79 	i = 0;
80 
81 	local_irq_save(flags);
82 
83 	v = gameport_read(gameport) >> shift;
84 
85 	do {
86 		t--;
87 		u = v; v = (gameport_read(gameport) >> shift) & 3;
88 		if (~v & u & 1) {
89 			data[0] |= (v >> 1) << i++;
90 			t = strobe;
91 		}
92 	} while (i < GRIP_LENGTH_GPP && t > 0);
93 
94 	local_irq_restore(flags);
95 
96 	if (i < GRIP_LENGTH_GPP) return -1;
97 
98 	for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
99 		data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
100 
101 	return -(i == GRIP_LENGTH_GPP);
102 }
103 
104 /*
105  * grip_xt_read_packet() reads a Gravis Xterminator packet.
106  */
107 
108 static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
109 {
110 	unsigned int i, j, buf, crc;
111 	unsigned char u, v, w;
112 	unsigned long flags;
113 	unsigned int t;
114 	char status;
115 
116 	int strobe = gameport_time(gameport, GRIP_STROBE_XT);
117 
118 	data[0] = data[1] = data[2] = data[3] = 0;
119 	status = buf = i = j = 0;
120 	t = strobe;
121 
122 	local_irq_save(flags);
123 
124 	v = w = (gameport_read(gameport) >> shift) & 3;
125 
126 	do {
127 		t--;
128 		u = (gameport_read(gameport) >> shift) & 3;
129 
130 		if (u ^ v) {
131 
132 			if ((u ^ v) & 1) {
133 				buf = (buf << 1) | (u >> 1);
134 				t = strobe;
135 				i++;
136 			} else
137 
138 			if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
139 				if (i == 20) {
140 					crc = buf ^ (buf >> 7) ^ (buf >> 14);
141 					if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
142 						data[buf >> 18] = buf >> 4;
143 						status |= 1 << (buf >> 18);
144 					}
145 					j++;
146 				}
147 				t = strobe;
148 				buf = 0;
149 				i = 0;
150 			}
151 			w = v;
152 			v = u;
153 		}
154 
155 	} while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
156 
157 	local_irq_restore(flags);
158 
159 	return -(status != 0xf);
160 }
161 
162 /*
163  * grip_timer() repeatedly polls the joysticks and generates events.
164  */
165 
166 static void grip_poll(struct gameport *gameport)
167 {
168 	struct grip *grip = gameport_get_drvdata(gameport);
169 	unsigned int data[GRIP_LENGTH_XT];
170 	struct input_dev *dev;
171 	int i, j;
172 
173 	for (i = 0; i < 2; i++) {
174 
175 		dev = grip->dev[i];
176 		if (!dev)
177 			continue;
178 
179 		grip->reads++;
180 
181 		switch (grip->mode[i]) {
182 
183 			case GRIP_MODE_GPP:
184 
185 				if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
186 					grip->bads++;
187 					break;
188 				}
189 
190 				input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
191 				input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
192 
193 				for (j = 0; j < 12; j++)
194 					if (grip_btn_gpp[j])
195 						input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
196 
197 				break;
198 
199 			case GRIP_MODE_BD:
200 
201 				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
202 					grip->bads++;
203 					break;
204 				}
205 
206 				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
207 				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
208 				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
209 
210 				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
211 				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
212 
213 				for (j = 0; j < 5; j++)
214 					input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
215 
216 				break;
217 
218 			case GRIP_MODE_XT:
219 
220 				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
221 					grip->bads++;
222 					break;
223 				}
224 
225 				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
226 				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
227 				input_report_abs(dev, ABS_BRAKE,    (data[1] >> 2) & 0x3f);
228 				input_report_abs(dev, ABS_GAS,	    (data[1] >> 8) & 0x3f);
229 				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
230 
231 				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
232 				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
233 				input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
234 				input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
235 
236 				for (j = 0; j < 11; j++)
237 					input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
238 				break;
239 
240 			case GRIP_MODE_DC:
241 
242 				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
243 					grip->bads++;
244 					break;
245 				}
246 
247 				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
248 				input_report_abs(dev, ABS_Y,        (data[0] >> 8) & 0x3f);
249 				input_report_abs(dev, ABS_RX,       (data[1] >> 2) & 0x3f);
250 				input_report_abs(dev, ABS_RY,	    (data[1] >> 8) & 0x3f);
251 				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
252 
253 				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
254 				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
255 
256 				for (j = 0; j < 9; j++)
257 					input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
258 				break;
259 
260 
261 		}
262 
263 		input_sync(dev);
264 	}
265 }
266 
267 static int grip_open(struct input_dev *dev)
268 {
269 	struct grip *grip = input_get_drvdata(dev);
270 
271 	gameport_start_polling(grip->gameport);
272 	return 0;
273 }
274 
275 static void grip_close(struct input_dev *dev)
276 {
277 	struct grip *grip = input_get_drvdata(dev);
278 
279 	gameport_stop_polling(grip->gameport);
280 }
281 
282 static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
283 {
284 	struct grip *grip;
285 	struct input_dev *input_dev;
286 	unsigned int data[GRIP_LENGTH_XT];
287 	int i, j, t;
288 	int err;
289 
290 	if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
291 		return -ENOMEM;
292 
293 	grip->gameport = gameport;
294 
295 	gameport_set_drvdata(gameport, grip);
296 
297 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
298 	if (err)
299 		goto fail1;
300 
301 	for (i = 0; i < 2; i++) {
302 		if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
303 			grip->mode[i] = GRIP_MODE_GPP;
304 			continue;
305 		}
306 		if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
307 			if (!(data[3] & 7)) {
308 				grip->mode[i] = GRIP_MODE_BD;
309 				continue;
310 			}
311 			if (!(data[2] & 0xf0)) {
312 				grip->mode[i] = GRIP_MODE_XT;
313 				continue;
314 			}
315 			grip->mode[i] = GRIP_MODE_DC;
316 			continue;
317 		}
318 	}
319 
320 	if (!grip->mode[0] && !grip->mode[1]) {
321 		err = -ENODEV;
322 		goto fail2;
323 	}
324 
325 	gameport_set_poll_handler(gameport, grip_poll);
326 	gameport_set_poll_interval(gameport, 20);
327 
328 	for (i = 0; i < 2; i++) {
329 		if (!grip->mode[i])
330 			continue;
331 
332 		grip->dev[i] = input_dev = input_allocate_device();
333 		if (!input_dev) {
334 			err = -ENOMEM;
335 			goto fail3;
336 		}
337 
338 		snprintf(grip->phys[i], sizeof(grip->phys[i]),
339 			 "%s/input%d", gameport->phys, i);
340 
341 		input_dev->name = grip_name[grip->mode[i]];
342 		input_dev->phys = grip->phys[i];
343 		input_dev->id.bustype = BUS_GAMEPORT;
344 		input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
345 		input_dev->id.product = grip->mode[i];
346 		input_dev->id.version = 0x0100;
347 		input_dev->dev.parent = &gameport->dev;
348 
349 		input_set_drvdata(input_dev, grip);
350 
351 		input_dev->open = grip_open;
352 		input_dev->close = grip_close;
353 
354 		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
355 
356 		for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
357 
358 			if (j < grip_cen[grip->mode[i]])
359 				input_set_abs_params(input_dev, t, 14, 52, 1, 2);
360 			else if (j < grip_anx[grip->mode[i]])
361 				input_set_abs_params(input_dev, t, 3, 57, 1, 0);
362 			else
363 				input_set_abs_params(input_dev, t, -1, 1, 0, 0);
364 		}
365 
366 		for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
367 			if (t > 0)
368 				set_bit(t, input_dev->keybit);
369 
370 		err = input_register_device(grip->dev[i]);
371 		if (err)
372 			goto fail4;
373 	}
374 
375 	return 0;
376 
377  fail4:	input_free_device(grip->dev[i]);
378  fail3:	while (--i >= 0)
379 		if (grip->dev[i])
380 			input_unregister_device(grip->dev[i]);
381  fail2:	gameport_close(gameport);
382  fail1:	gameport_set_drvdata(gameport, NULL);
383 	kfree(grip);
384 	return err;
385 }
386 
387 static void grip_disconnect(struct gameport *gameport)
388 {
389 	struct grip *grip = gameport_get_drvdata(gameport);
390 	int i;
391 
392 	for (i = 0; i < 2; i++)
393 		if (grip->dev[i])
394 			input_unregister_device(grip->dev[i]);
395 	gameport_close(gameport);
396 	gameport_set_drvdata(gameport, NULL);
397 	kfree(grip);
398 }
399 
400 static struct gameport_driver grip_drv = {
401 	.driver		= {
402 		.name	= "grip",
403 		.owner	= THIS_MODULE,
404 	},
405 	.description	= DRIVER_DESC,
406 	.connect	= grip_connect,
407 	.disconnect	= grip_disconnect,
408 };
409 
410 module_gameport_driver(grip_drv);
411