xref: /openbmc/linux/drivers/input/joystick/gamecon.c (revision 2d09ac95)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
4  *
5  *  Copyright (c) 1999-2004	Vojtech Pavlik <vojtech@suse.cz>
6  *  Copyright (c) 2004		Peter Nelson <rufus-kernel@hackish.org>
7  *
8  *  Based on the work of:
9  *	Andree Borrmann		John Dahlstrom
10  *	David Kuder		Nathan Hand
11  *	Raphael Assenat
12  */
13 
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
15 
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/parport.h>
21 #include <linux/input.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
24 
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
27 MODULE_LICENSE("GPL");
28 
29 #define GC_MAX_PORTS		3
30 #define GC_MAX_DEVICES		5
31 
32 struct gc_config {
33 	int args[GC_MAX_DEVICES + 1];
34 	unsigned int nargs;
35 };
36 
37 static struct gc_config gc_cfg[GC_MAX_PORTS];
38 
39 module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
40 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
41 module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
42 MODULE_PARM_DESC(map2, "Describes second set of devices");
43 module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
44 MODULE_PARM_DESC(map3, "Describes third set of devices");
45 
46 /* see also gs_psx_delay parameter in PSX support section */
47 
48 enum gc_type {
49 	GC_NONE = 0,
50 	GC_SNES,
51 	GC_NES,
52 	GC_NES4,
53 	GC_MULTI,
54 	GC_MULTI2,
55 	GC_N64,
56 	GC_PSX,
57 	GC_DDR,
58 	GC_SNESMOUSE,
59 	GC_MAX
60 };
61 
62 #define GC_REFRESH_TIME	HZ/100
63 
64 struct gc_pad {
65 	struct input_dev *dev;
66 	enum gc_type type;
67 	char phys[32];
68 };
69 
70 struct gc {
71 	struct pardevice *pd;
72 	struct gc_pad pads[GC_MAX_DEVICES];
73 	struct timer_list timer;
74 	int pad_count[GC_MAX];
75 	int used;
76 	int parportno;
77 	struct mutex mutex;
78 };
79 
80 struct gc_subdev {
81 	unsigned int idx;
82 };
83 
84 static struct gc *gc_base[3];
85 
86 static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
87 
88 static const char *gc_names[] = {
89 	NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
90 	"Multisystem 2-button joystick", "N64 controller", "PSX controller",
91 	"PSX DDR controller", "SNES mouse"
92 };
93 
94 /*
95  * N64 support.
96  */
97 
98 static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
99 static const short gc_n64_btn[] = {
100 	BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
101 	BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
102 };
103 
104 #define GC_N64_LENGTH		32		/* N64 bit length, not including stop bit */
105 #define GC_N64_STOP_LENGTH	5		/* Length of encoded stop bit */
106 #define GC_N64_CMD_00		0x11111111UL
107 #define GC_N64_CMD_01		0xd1111111UL
108 #define GC_N64_CMD_03		0xdd111111UL
109 #define GC_N64_CMD_1b		0xdd1dd111UL
110 #define GC_N64_CMD_c0		0x111111ddUL
111 #define GC_N64_CMD_80		0x1111111dUL
112 #define GC_N64_STOP_BIT		0x1d		/* Encoded stop bit */
113 #define GC_N64_REQUEST_DATA	GC_N64_CMD_01	/* the request data command */
114 #define GC_N64_DELAY		133		/* delay between transmit request, and response ready (us) */
115 #define GC_N64_DWS		3		/* delay between write segments (required for sound playback because of ISA DMA) */
116 						/* GC_N64_DWS > 24 is known to fail */
117 #define GC_N64_POWER_W		0xe2		/* power during write (transmit request) */
118 #define GC_N64_POWER_R		0xfd		/* power during read */
119 #define GC_N64_OUT		0x1d		/* output bits to the 4 pads */
120 						/* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
121 						/* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
122 						/* than 123 us */
123 #define GC_N64_CLOCK		0x02		/* clock bits for read */
124 
125 /*
126  * Used for rumble code.
127  */
128 
129 /* Send encoded command */
gc_n64_send_command(struct gc * gc,unsigned long cmd,unsigned char target)130 static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
131 				unsigned char target)
132 {
133 	struct parport *port = gc->pd->port;
134 	int i;
135 
136 	for (i = 0; i < GC_N64_LENGTH; i++) {
137 		unsigned char data = (cmd >> i) & 1 ? target : 0;
138 		parport_write_data(port, GC_N64_POWER_W | data);
139 		udelay(GC_N64_DWS);
140 	}
141 }
142 
143 /* Send stop bit */
gc_n64_send_stop_bit(struct gc * gc,unsigned char target)144 static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
145 {
146 	struct parport *port = gc->pd->port;
147 	int i;
148 
149 	for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
150 		unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
151 		parport_write_data(port, GC_N64_POWER_W | data);
152 		udelay(GC_N64_DWS);
153 	}
154 }
155 
156 /*
157  * gc_n64_read_packet() reads an N64 packet.
158  * Each pad uses one bit per byte. So all pads connected to this port
159  * are read in parallel.
160  */
161 
gc_n64_read_packet(struct gc * gc,unsigned char * data)162 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
163 {
164 	int i;
165 	unsigned long flags;
166 
167 /*
168  * Request the pad to transmit data
169  */
170 
171 	local_irq_save(flags);
172 	gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
173 	gc_n64_send_stop_bit(gc, GC_N64_OUT);
174 	local_irq_restore(flags);
175 
176 /*
177  * Wait for the pad response to be loaded into the 33-bit register
178  * of the adapter.
179  */
180 
181 	udelay(GC_N64_DELAY);
182 
183 /*
184  * Grab data (ignoring the last bit, which is a stop bit)
185  */
186 
187 	for (i = 0; i < GC_N64_LENGTH; i++) {
188 		parport_write_data(gc->pd->port, GC_N64_POWER_R);
189 		udelay(2);
190 		data[i] = parport_read_status(gc->pd->port);
191 		parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
192 	 }
193 
194 /*
195  * We must wait 200 ms here for the controller to reinitialize before
196  * the next read request. No worries as long as gc_read is polled less
197  * frequently than this.
198  */
199 
200 }
201 
gc_n64_process_packet(struct gc * gc)202 static void gc_n64_process_packet(struct gc *gc)
203 {
204 	unsigned char data[GC_N64_LENGTH];
205 	struct input_dev *dev;
206 	int i, j, s;
207 	signed char x, y;
208 
209 	gc_n64_read_packet(gc, data);
210 
211 	for (i = 0; i < GC_MAX_DEVICES; i++) {
212 
213 		if (gc->pads[i].type != GC_N64)
214 			continue;
215 
216 		dev = gc->pads[i].dev;
217 		s = gc_status_bit[i];
218 
219 		if (s & ~(data[8] | data[9])) {
220 
221 			x = y = 0;
222 
223 			for (j = 0; j < 8; j++) {
224 				if (data[23 - j] & s)
225 					x |= 1 << j;
226 				if (data[31 - j] & s)
227 					y |= 1 << j;
228 			}
229 
230 			input_report_abs(dev, ABS_X,  x);
231 			input_report_abs(dev, ABS_Y, -y);
232 
233 			input_report_abs(dev, ABS_HAT0X,
234 					 !(s & data[6]) - !(s & data[7]));
235 			input_report_abs(dev, ABS_HAT0Y,
236 					 !(s & data[4]) - !(s & data[5]));
237 
238 			for (j = 0; j < 10; j++)
239 				input_report_key(dev, gc_n64_btn[j],
240 						 s & data[gc_n64_bytes[j]]);
241 
242 			input_sync(dev);
243 		}
244 	}
245 }
246 
gc_n64_play_effect(struct input_dev * dev,void * data,struct ff_effect * effect)247 static int gc_n64_play_effect(struct input_dev *dev, void *data,
248 			      struct ff_effect *effect)
249 {
250 	int i;
251 	unsigned long flags;
252 	struct gc *gc = input_get_drvdata(dev);
253 	struct gc_subdev *sdev = data;
254 	unsigned char target = 1 << sdev->idx; /* select desired pin */
255 
256 	if (effect->type == FF_RUMBLE) {
257 		struct ff_rumble_effect *rumble = &effect->u.rumble;
258 		unsigned int cmd =
259 			rumble->strong_magnitude || rumble->weak_magnitude ?
260 			GC_N64_CMD_01 : GC_N64_CMD_00;
261 
262 		local_irq_save(flags);
263 
264 		/* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
265 		gc_n64_send_command(gc, GC_N64_CMD_03, target);
266 		gc_n64_send_command(gc, GC_N64_CMD_80, target);
267 		gc_n64_send_command(gc, GC_N64_CMD_01, target);
268 		for (i = 0; i < 32; i++)
269 			gc_n64_send_command(gc, GC_N64_CMD_80, target);
270 		gc_n64_send_stop_bit(gc, target);
271 
272 		udelay(GC_N64_DELAY);
273 
274 		/* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
275 		gc_n64_send_command(gc, GC_N64_CMD_03, target);
276 		gc_n64_send_command(gc, GC_N64_CMD_c0, target);
277 		gc_n64_send_command(gc, GC_N64_CMD_1b, target);
278 		for (i = 0; i < 32; i++)
279 			gc_n64_send_command(gc, cmd, target);
280 		gc_n64_send_stop_bit(gc, target);
281 
282 		local_irq_restore(flags);
283 
284 	}
285 
286 	return 0;
287 }
288 
gc_n64_init_ff(struct input_dev * dev,int i)289 static int gc_n64_init_ff(struct input_dev *dev, int i)
290 {
291 	struct gc_subdev *sdev;
292 	int err;
293 
294 	sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
295 	if (!sdev)
296 		return -ENOMEM;
297 
298 	sdev->idx = i;
299 
300 	input_set_capability(dev, EV_FF, FF_RUMBLE);
301 
302 	err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
303 	if (err) {
304 		kfree(sdev);
305 		return err;
306 	}
307 
308 	return 0;
309 }
310 
311 /*
312  * NES/SNES support.
313  */
314 
315 #define GC_NES_DELAY		6	/* Delay between bits - 6us */
316 #define GC_NES_LENGTH		8	/* The NES pads use 8 bits of data */
317 #define GC_SNES_LENGTH		12	/* The SNES true length is 16, but the
318 					   last 4 bits are unused */
319 #define GC_SNESMOUSE_LENGTH	32	/* The SNES mouse uses 32 bits, the first
320 					   16 bits are equivalent to a gamepad */
321 
322 #define GC_NES_POWER	0xfc
323 #define GC_NES_CLOCK	0x01
324 #define GC_NES_LATCH	0x02
325 
326 static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
327 static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
328 static const short gc_snes_btn[] = {
329 	BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
330 };
331 
332 /*
333  * gc_nes_read_packet() reads a NES/SNES packet.
334  * Each pad uses one bit per byte. So all pads connected to
335  * this port are read in parallel.
336  */
337 
gc_nes_read_packet(struct gc * gc,int length,unsigned char * data)338 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
339 {
340 	int i;
341 
342 	parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
343 	udelay(GC_NES_DELAY * 2);
344 	parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
345 
346 	for (i = 0; i < length; i++) {
347 		udelay(GC_NES_DELAY);
348 		parport_write_data(gc->pd->port, GC_NES_POWER);
349 		data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
350 		udelay(GC_NES_DELAY);
351 		parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
352 	}
353 }
354 
gc_nes_process_packet(struct gc * gc)355 static void gc_nes_process_packet(struct gc *gc)
356 {
357 	unsigned char data[GC_SNESMOUSE_LENGTH];
358 	struct gc_pad *pad;
359 	struct input_dev *dev;
360 	int i, j, s, len;
361 	char x_rel, y_rel;
362 
363 	len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
364 			(gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
365 
366 	gc_nes_read_packet(gc, len, data);
367 
368 	for (i = 0; i < GC_MAX_DEVICES; i++) {
369 
370 		pad = &gc->pads[i];
371 		dev = pad->dev;
372 		s = gc_status_bit[i];
373 
374 		switch (pad->type) {
375 
376 		case GC_NES:
377 
378 			input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
379 			input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
380 
381 			for (j = 0; j < 4; j++)
382 				input_report_key(dev, gc_snes_btn[j],
383 						 s & data[gc_nes_bytes[j]]);
384 			input_sync(dev);
385 			break;
386 
387 		case GC_SNES:
388 
389 			input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
390 			input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
391 
392 			for (j = 0; j < 8; j++)
393 				input_report_key(dev, gc_snes_btn[j],
394 						 s & data[gc_snes_bytes[j]]);
395 			input_sync(dev);
396 			break;
397 
398 		case GC_SNESMOUSE:
399 			/*
400 			 * The 4 unused bits from SNES controllers appear
401 			 * to be ID bits so use them to make sure we are
402 			 * dealing with a mouse.
403 			 * gamepad is connected. This is important since
404 			 * my SNES gamepad sends 1's for bits 16-31, which
405 			 * cause the mouse pointer to quickly move to the
406 			 * upper left corner of the screen.
407 			 */
408 			if (!(s & data[12]) && !(s & data[13]) &&
409 			    !(s & data[14]) && (s & data[15])) {
410 				input_report_key(dev, BTN_LEFT, s & data[9]);
411 				input_report_key(dev, BTN_RIGHT, s & data[8]);
412 
413 				x_rel = y_rel = 0;
414 				for (j = 0; j < 7; j++) {
415 					x_rel <<= 1;
416 					if (data[25 + j] & s)
417 						x_rel |= 1;
418 
419 					y_rel <<= 1;
420 					if (data[17 + j] & s)
421 						y_rel |= 1;
422 				}
423 
424 				if (x_rel) {
425 					if (data[24] & s)
426 						x_rel = -x_rel;
427 					input_report_rel(dev, REL_X, x_rel);
428 				}
429 
430 				if (y_rel) {
431 					if (data[16] & s)
432 						y_rel = -y_rel;
433 					input_report_rel(dev, REL_Y, y_rel);
434 				}
435 
436 				input_sync(dev);
437 			}
438 			break;
439 
440 		default:
441 			break;
442 		}
443 	}
444 }
445 
446 /*
447  * Multisystem joystick support
448  */
449 
450 #define GC_MULTI_LENGTH		5	/* Multi system joystick packet length is 5 */
451 #define GC_MULTI2_LENGTH	6	/* One more bit for one more button */
452 
453 /*
454  * gc_multi_read_packet() reads a Multisystem joystick packet.
455  */
456 
gc_multi_read_packet(struct gc * gc,int length,unsigned char * data)457 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
458 {
459 	int i;
460 
461 	for (i = 0; i < length; i++) {
462 		parport_write_data(gc->pd->port, ~(1 << i));
463 		data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
464 	}
465 }
466 
gc_multi_process_packet(struct gc * gc)467 static void gc_multi_process_packet(struct gc *gc)
468 {
469 	unsigned char data[GC_MULTI2_LENGTH];
470 	int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
471 	struct gc_pad *pad;
472 	struct input_dev *dev;
473 	int i, s;
474 
475 	gc_multi_read_packet(gc, data_len, data);
476 
477 	for (i = 0; i < GC_MAX_DEVICES; i++) {
478 		pad = &gc->pads[i];
479 		dev = pad->dev;
480 		s = gc_status_bit[i];
481 
482 		switch (pad->type) {
483 		case GC_MULTI2:
484 			input_report_key(dev, BTN_THUMB, s & data[5]);
485 			fallthrough;
486 
487 		case GC_MULTI:
488 			input_report_abs(dev, ABS_X,
489 					 !(s & data[2]) - !(s & data[3]));
490 			input_report_abs(dev, ABS_Y,
491 					 !(s & data[0]) - !(s & data[1]));
492 			input_report_key(dev, BTN_TRIGGER, s & data[4]);
493 			input_sync(dev);
494 			break;
495 
496 		default:
497 			break;
498 		}
499 	}
500 }
501 
502 /*
503  * PSX support
504  *
505  * See documentation at:
506  *	http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
507  *	http://www.gamesx.com/controldata/psxcont/psxcont.htm
508  *
509  */
510 
511 #define GC_PSX_DELAY	25		/* 25 usec */
512 #define GC_PSX_LENGTH	8		/* talk to the controller in bits */
513 #define GC_PSX_BYTES	6		/* the maximum number of bytes to read off the controller */
514 
515 #define GC_PSX_MOUSE	1		/* Mouse */
516 #define GC_PSX_NEGCON	2		/* NegCon */
517 #define GC_PSX_NORMAL	4		/* Digital / Analog or Rumble in Digital mode  */
518 #define GC_PSX_ANALOG	5		/* Analog in Analog mode / Rumble in Green mode */
519 #define GC_PSX_RUMBLE	7		/* Rumble in Red mode */
520 
521 #define GC_PSX_CLOCK	0x04		/* Pin 4 */
522 #define GC_PSX_COMMAND	0x01		/* Pin 2 */
523 #define GC_PSX_POWER	0xf8		/* Pins 5-9 */
524 #define GC_PSX_SELECT	0x02		/* Pin 3 */
525 
526 #define GC_PSX_ID(x)	((x) >> 4)	/* High nibble is device type */
527 #define GC_PSX_LEN(x)	(((x) & 0xf) << 1)	/* Low nibble is length in bytes/2 */
528 
529 static int gc_psx_delay = GC_PSX_DELAY;
530 module_param_named(psx_delay, gc_psx_delay, uint, 0);
531 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
532 
533 static const short gc_psx_abs[] = {
534 	ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
535 };
536 static const short gc_psx_btn[] = {
537 	BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
538 	BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
539 };
540 static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
541 
542 /*
543  * gc_psx_command() writes 8bit command and reads 8bit data from
544  * the psx pad.
545  */
546 
gc_psx_command(struct gc * gc,int b,unsigned char * data)547 static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
548 {
549 	struct parport *port = gc->pd->port;
550 	int i, j, cmd, read;
551 
552 	memset(data, 0, GC_MAX_DEVICES);
553 
554 	for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
555 		cmd = (b & 1) ? GC_PSX_COMMAND : 0;
556 		parport_write_data(port, cmd | GC_PSX_POWER);
557 		udelay(gc_psx_delay);
558 
559 		read = parport_read_status(port) ^ 0x80;
560 
561 		for (j = 0; j < GC_MAX_DEVICES; j++) {
562 			struct gc_pad *pad = &gc->pads[j];
563 
564 			if (pad->type == GC_PSX || pad->type == GC_DDR)
565 				data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
566 		}
567 
568 		parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
569 		udelay(gc_psx_delay);
570 	}
571 }
572 
573 /*
574  * gc_psx_read_packet() reads a whole psx packet and returns
575  * device identifier code.
576  */
577 
gc_psx_read_packet(struct gc * gc,unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],unsigned char id[GC_MAX_DEVICES])578 static void gc_psx_read_packet(struct gc *gc,
579 			       unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
580 			       unsigned char id[GC_MAX_DEVICES])
581 {
582 	int i, j, max_len = 0;
583 	unsigned long flags;
584 	unsigned char data2[GC_MAX_DEVICES];
585 
586 	/* Select pad */
587 	parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
588 	udelay(gc_psx_delay);
589 	/* Deselect, begin command */
590 	parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
591 	udelay(gc_psx_delay);
592 
593 	local_irq_save(flags);
594 
595 	gc_psx_command(gc, 0x01, data2);	/* Access pad */
596 	gc_psx_command(gc, 0x42, id);		/* Get device ids */
597 	gc_psx_command(gc, 0, data2);		/* Dump status */
598 
599 	/* Find the longest pad */
600 	for (i = 0; i < GC_MAX_DEVICES; i++) {
601 		struct gc_pad *pad = &gc->pads[i];
602 
603 		if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
604 		    GC_PSX_LEN(id[i]) > max_len &&
605 		    GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
606 			max_len = GC_PSX_LEN(id[i]);
607 		}
608 	}
609 
610 	/* Read in all the data */
611 	for (i = 0; i < max_len; i++) {
612 		gc_psx_command(gc, 0, data2);
613 		for (j = 0; j < GC_MAX_DEVICES; j++)
614 			data[j][i] = data2[j];
615 	}
616 
617 	local_irq_restore(flags);
618 
619 	parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
620 
621 	/* Set id's to the real value */
622 	for (i = 0; i < GC_MAX_DEVICES; i++)
623 		id[i] = GC_PSX_ID(id[i]);
624 }
625 
gc_psx_report_one(struct gc_pad * pad,unsigned char psx_type,unsigned char * data)626 static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
627 			      unsigned char *data)
628 {
629 	struct input_dev *dev = pad->dev;
630 	int i;
631 
632 	switch (psx_type) {
633 
634 	case GC_PSX_RUMBLE:
635 
636 		input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
637 		input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
638 		fallthrough;
639 
640 	case GC_PSX_NEGCON:
641 	case GC_PSX_ANALOG:
642 
643 		if (pad->type == GC_DDR) {
644 			for (i = 0; i < 4; i++)
645 				input_report_key(dev, gc_psx_ddr_btn[i],
646 						 ~data[0] & (0x10 << i));
647 		} else {
648 			for (i = 0; i < 4; i++)
649 				input_report_abs(dev, gc_psx_abs[i + 2],
650 						 data[i + 2]);
651 
652 			input_report_abs(dev, ABS_X,
653 				!!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
654 			input_report_abs(dev, ABS_Y,
655 				!!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
656 		}
657 
658 		for (i = 0; i < 8; i++)
659 			input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
660 
661 		input_report_key(dev, BTN_START,  ~data[0] & 0x08);
662 		input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
663 
664 		input_sync(dev);
665 
666 		break;
667 
668 	case GC_PSX_NORMAL:
669 
670 		if (pad->type == GC_DDR) {
671 			for (i = 0; i < 4; i++)
672 				input_report_key(dev, gc_psx_ddr_btn[i],
673 						 ~data[0] & (0x10 << i));
674 		} else {
675 			input_report_abs(dev, ABS_X,
676 				!!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
677 			input_report_abs(dev, ABS_Y,
678 				!!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
679 
680 			/*
681 			 * For some reason if the extra axes are left unset
682 			 * they drift.
683 			 * for (i = 0; i < 4; i++)
684 				input_report_abs(dev, gc_psx_abs[i + 2], 128);
685 			 * This needs to be debugged properly,
686 			 * maybe fuzz processing needs to be done
687 			 * in input_sync()
688 			 *				 --vojtech
689 			 */
690 		}
691 
692 		for (i = 0; i < 8; i++)
693 			input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
694 
695 		input_report_key(dev, BTN_START,  ~data[0] & 0x08);
696 		input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
697 
698 		input_sync(dev);
699 
700 		break;
701 
702 	default: /* not a pad, ignore */
703 		break;
704 	}
705 }
706 
gc_psx_process_packet(struct gc * gc)707 static void gc_psx_process_packet(struct gc *gc)
708 {
709 	unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
710 	unsigned char id[GC_MAX_DEVICES];
711 	struct gc_pad *pad;
712 	int i;
713 
714 	gc_psx_read_packet(gc, data, id);
715 
716 	for (i = 0; i < GC_MAX_DEVICES; i++) {
717 		pad = &gc->pads[i];
718 		if (pad->type == GC_PSX || pad->type == GC_DDR)
719 			gc_psx_report_one(pad, id[i], data[i]);
720 	}
721 }
722 
723 /*
724  * gc_timer() initiates reads of console pads data.
725  */
726 
gc_timer(struct timer_list * t)727 static void gc_timer(struct timer_list *t)
728 {
729 	struct gc *gc = from_timer(gc, t, timer);
730 
731 /*
732  * N64 pads - must be read first, any read confuses them for 200 us
733  */
734 
735 	if (gc->pad_count[GC_N64])
736 		gc_n64_process_packet(gc);
737 
738 /*
739  * NES and SNES pads or mouse
740  */
741 
742 	if (gc->pad_count[GC_NES] ||
743 	    gc->pad_count[GC_SNES] ||
744 	    gc->pad_count[GC_SNESMOUSE]) {
745 		gc_nes_process_packet(gc);
746 	}
747 
748 /*
749  * Multi and Multi2 joysticks
750  */
751 
752 	if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
753 		gc_multi_process_packet(gc);
754 
755 /*
756  * PSX controllers
757  */
758 
759 	if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
760 		gc_psx_process_packet(gc);
761 
762 	mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
763 }
764 
gc_open(struct input_dev * dev)765 static int gc_open(struct input_dev *dev)
766 {
767 	struct gc *gc = input_get_drvdata(dev);
768 	int err;
769 
770 	err = mutex_lock_interruptible(&gc->mutex);
771 	if (err)
772 		return err;
773 
774 	if (!gc->used++) {
775 		parport_claim(gc->pd);
776 		parport_write_control(gc->pd->port, 0x04);
777 		mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
778 	}
779 
780 	mutex_unlock(&gc->mutex);
781 	return 0;
782 }
783 
gc_close(struct input_dev * dev)784 static void gc_close(struct input_dev *dev)
785 {
786 	struct gc *gc = input_get_drvdata(dev);
787 
788 	mutex_lock(&gc->mutex);
789 	if (!--gc->used) {
790 		del_timer_sync(&gc->timer);
791 		parport_write_control(gc->pd->port, 0x00);
792 		parport_release(gc->pd);
793 	}
794 	mutex_unlock(&gc->mutex);
795 }
796 
gc_setup_pad(struct gc * gc,int idx,int pad_type)797 static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
798 {
799 	struct gc_pad *pad = &gc->pads[idx];
800 	struct input_dev *input_dev;
801 	int i;
802 	int err;
803 
804 	if (pad_type < 1 || pad_type >= GC_MAX) {
805 		pr_err("Pad type %d unknown\n", pad_type);
806 		return -EINVAL;
807 	}
808 
809 	pad->dev = input_dev = input_allocate_device();
810 	if (!input_dev) {
811 		pr_err("Not enough memory for input device\n");
812 		return -ENOMEM;
813 	}
814 
815 	pad->type = pad_type;
816 
817 	snprintf(pad->phys, sizeof(pad->phys),
818 		 "%s/input%d", gc->pd->port->name, idx);
819 
820 	input_dev->name = gc_names[pad_type];
821 	input_dev->phys = pad->phys;
822 	input_dev->id.bustype = BUS_PARPORT;
823 	input_dev->id.vendor = 0x0001;
824 	input_dev->id.product = pad_type;
825 	input_dev->id.version = 0x0100;
826 
827 	input_set_drvdata(input_dev, gc);
828 
829 	input_dev->open = gc_open;
830 	input_dev->close = gc_close;
831 
832 	if (pad_type != GC_SNESMOUSE) {
833 		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
834 
835 		for (i = 0; i < 2; i++)
836 			input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
837 	} else
838 		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
839 
840 	gc->pad_count[pad_type]++;
841 
842 	switch (pad_type) {
843 
844 	case GC_N64:
845 		for (i = 0; i < 10; i++)
846 			input_set_capability(input_dev, EV_KEY, gc_n64_btn[i]);
847 
848 		for (i = 0; i < 2; i++) {
849 			input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
850 			input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
851 		}
852 
853 		err = gc_n64_init_ff(input_dev, idx);
854 		if (err) {
855 			pr_warn("Failed to initiate rumble for N64 device %d\n",
856 				idx);
857 			goto err_free_dev;
858 		}
859 
860 		break;
861 
862 	case GC_SNESMOUSE:
863 		input_set_capability(input_dev, EV_KEY, BTN_LEFT);
864 		input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
865 		input_set_capability(input_dev, EV_REL, REL_X);
866 		input_set_capability(input_dev, EV_REL, REL_Y);
867 		break;
868 
869 	case GC_SNES:
870 		for (i = 4; i < 8; i++)
871 			input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
872 		fallthrough;
873 
874 	case GC_NES:
875 		for (i = 0; i < 4; i++)
876 			input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
877 		break;
878 
879 	case GC_MULTI2:
880 		input_set_capability(input_dev, EV_KEY, BTN_THUMB);
881 		fallthrough;
882 
883 	case GC_MULTI:
884 		input_set_capability(input_dev, EV_KEY, BTN_TRIGGER);
885 		break;
886 
887 	case GC_PSX:
888 		for (i = 0; i < 6; i++)
889 			input_set_abs_params(input_dev,
890 					     gc_psx_abs[i], 4, 252, 0, 2);
891 		for (i = 0; i < 12; i++)
892 			input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
893 		break;
894 
895 		break;
896 
897 	case GC_DDR:
898 		for (i = 0; i < 4; i++)
899 			input_set_capability(input_dev, EV_KEY,
900 					     gc_psx_ddr_btn[i]);
901 		for (i = 0; i < 12; i++)
902 			input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
903 
904 		break;
905 	}
906 
907 	err = input_register_device(pad->dev);
908 	if (err)
909 		goto err_free_dev;
910 
911 	return 0;
912 
913 err_free_dev:
914 	input_free_device(pad->dev);
915 	pad->dev = NULL;
916 	return err;
917 }
918 
gc_attach(struct parport * pp)919 static void gc_attach(struct parport *pp)
920 {
921 	struct gc *gc;
922 	struct pardevice *pd;
923 	int i, port_idx;
924 	int count = 0;
925 	int *pads, n_pads;
926 	struct pardev_cb gc_parport_cb;
927 
928 	for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
929 		if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
930 			continue;
931 
932 		if (gc_cfg[port_idx].args[0] == pp->number)
933 			break;
934 	}
935 
936 	if (port_idx == GC_MAX_PORTS) {
937 		pr_debug("Not using parport%d.\n", pp->number);
938 		return;
939 	}
940 	pads = gc_cfg[port_idx].args + 1;
941 	n_pads = gc_cfg[port_idx].nargs - 1;
942 
943 	memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
944 	gc_parport_cb.flags = PARPORT_FLAG_EXCL;
945 
946 	pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
947 					port_idx);
948 	if (!pd) {
949 		pr_err("parport busy already - lp.o loaded?\n");
950 		return;
951 	}
952 
953 	gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
954 	if (!gc) {
955 		pr_err("Not enough memory\n");
956 		goto err_unreg_pardev;
957 	}
958 
959 	mutex_init(&gc->mutex);
960 	gc->pd = pd;
961 	gc->parportno = pp->number;
962 	timer_setup(&gc->timer, gc_timer, 0);
963 
964 	for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
965 		if (!pads[i])
966 			continue;
967 
968 		if (gc_setup_pad(gc, i, pads[i]))
969 			goto err_unreg_devs;
970 
971 		count++;
972 	}
973 
974 	if (count == 0) {
975 		pr_err("No valid devices specified\n");
976 		goto err_free_gc;
977 	}
978 
979 	gc_base[port_idx] = gc;
980 	return;
981 
982  err_unreg_devs:
983 	while (--i >= 0)
984 		if (gc->pads[i].dev)
985 			input_unregister_device(gc->pads[i].dev);
986  err_free_gc:
987 	kfree(gc);
988  err_unreg_pardev:
989 	parport_unregister_device(pd);
990 }
991 
gc_detach(struct parport * port)992 static void gc_detach(struct parport *port)
993 {
994 	int i;
995 	struct gc *gc;
996 
997 	for (i = 0; i < GC_MAX_PORTS; i++) {
998 		if (gc_base[i] && gc_base[i]->parportno == port->number)
999 			break;
1000 	}
1001 
1002 	if (i == GC_MAX_PORTS)
1003 		return;
1004 
1005 	gc = gc_base[i];
1006 	gc_base[i] = NULL;
1007 
1008 	for (i = 0; i < GC_MAX_DEVICES; i++)
1009 		if (gc->pads[i].dev)
1010 			input_unregister_device(gc->pads[i].dev);
1011 	parport_unregister_device(gc->pd);
1012 	kfree(gc);
1013 }
1014 
1015 static struct parport_driver gc_parport_driver = {
1016 	.name = "gamecon",
1017 	.match_port = gc_attach,
1018 	.detach = gc_detach,
1019 	.devmodel = true,
1020 };
1021 
gc_init(void)1022 static int __init gc_init(void)
1023 {
1024 	int i;
1025 	int have_dev = 0;
1026 
1027 	for (i = 0; i < GC_MAX_PORTS; i++) {
1028 		if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
1029 			continue;
1030 
1031 		if (gc_cfg[i].nargs < 2) {
1032 			pr_err("at least one device must be specified\n");
1033 			return -EINVAL;
1034 		}
1035 
1036 		have_dev = 1;
1037 	}
1038 
1039 	if (!have_dev)
1040 		return -ENODEV;
1041 
1042 	return parport_register_driver(&gc_parport_driver);
1043 }
1044 
gc_exit(void)1045 static void __exit gc_exit(void)
1046 {
1047 	parport_unregister_driver(&gc_parport_driver);
1048 }
1049 
1050 module_init(gc_init);
1051 module_exit(gc_exit);
1052