Merge tag 'trace-printf-v6.13' of git://git.kernel.org/pub/scm/linux/kernel/git/trace...
[drm/drm-misc.git] / drivers / input / joystick / gamecon.c
blobb53cafd7a5ee120d4841bfa47e791e2057adf601
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
5 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
8 * Based on the work of:
9 * Andree Borrmann John Dahlstrom
10 * David Kuder Nathan Hand
11 * Raphael Assenat
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
27 MODULE_LICENSE("GPL");
29 #define GC_MAX_PORTS 3
30 #define GC_MAX_DEVICES 5
32 struct gc_config {
33 int args[GC_MAX_DEVICES + 1];
34 unsigned int nargs;
37 static struct gc_config gc_cfg[GC_MAX_PORTS];
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");
46 /* see also gs_psx_delay parameter in PSX support section */
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
62 #define GC_REFRESH_TIME HZ/100
64 struct gc_pad {
65 struct input_dev *dev;
66 enum gc_type type;
67 char phys[32];
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;
80 struct gc_subdev {
81 unsigned int idx;
84 static struct gc *gc_base[3];
86 static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
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"
95 * N64 support.
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
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 */
126 * Used for rumble code.
129 /* Send encoded command */
130 static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
131 unsigned char target)
133 struct parport *port = gc->pd->port;
134 int i;
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);
143 /* Send stop bit */
144 static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
146 struct parport *port = gc->pd->port;
147 int i;
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);
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.
162 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
164 int i;
165 unsigned long flags;
168 * Request the pad to transmit data
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);
177 * Wait for the pad response to be loaded into the 33-bit register
178 * of the adapter.
181 udelay(GC_N64_DELAY);
184 * Grab data (ignoring the last bit, which is a stop bit)
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);
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.
202 static void gc_n64_process_packet(struct gc *gc)
204 unsigned char data[GC_N64_LENGTH];
205 struct input_dev *dev;
206 int i, j, s;
207 signed char x, y;
209 gc_n64_read_packet(gc, data);
211 for (i = 0; i < GC_MAX_DEVICES; i++) {
213 if (gc->pads[i].type != GC_N64)
214 continue;
216 dev = gc->pads[i].dev;
217 s = gc_status_bit[i];
219 if (s & ~(data[8] | data[9])) {
221 x = y = 0;
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;
230 input_report_abs(dev, ABS_X, x);
231 input_report_abs(dev, ABS_Y, -y);
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]));
238 for (j = 0; j < 10; j++)
239 input_report_key(dev, gc_n64_btn[j],
240 s & data[gc_n64_bytes[j]]);
242 input_sync(dev);
247 static int gc_n64_play_effect(struct input_dev *dev, void *data,
248 struct ff_effect *effect)
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 */
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;
262 local_irq_save(flags);
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);
272 udelay(GC_N64_DELAY);
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);
282 local_irq_restore(flags);
286 return 0;
289 static int gc_n64_init_ff(struct input_dev *dev, int i)
291 struct gc_subdev *sdev;
292 int err;
294 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
295 if (!sdev)
296 return -ENOMEM;
298 sdev->idx = i;
300 input_set_capability(dev, EV_FF, FF_RUMBLE);
302 err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
303 if (err) {
304 kfree(sdev);
305 return err;
308 return 0;
312 * NES/SNES support.
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 */
322 #define GC_NES_POWER 0xfc
323 #define GC_NES_CLOCK 0x01
324 #define GC_NES_LATCH 0x02
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
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.
338 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
340 int i;
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);
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);
355 static void gc_nes_process_packet(struct gc *gc)
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;
363 len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
364 (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
366 gc_nes_read_packet(gc, len, data);
368 for (i = 0; i < GC_MAX_DEVICES; i++) {
370 pad = &gc->pads[i];
371 dev = pad->dev;
372 s = gc_status_bit[i];
374 switch (pad->type) {
376 case GC_NES:
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]));
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;
387 case GC_SNES:
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]));
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;
398 case GC_SNESMOUSE:
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.
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]);
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;
419 y_rel <<= 1;
420 if (data[17 + j] & s)
421 y_rel |= 1;
424 if (x_rel) {
425 if (data[24] & s)
426 x_rel = -x_rel;
427 input_report_rel(dev, REL_X, x_rel);
430 if (y_rel) {
431 if (data[16] & s)
432 y_rel = -y_rel;
433 input_report_rel(dev, REL_Y, y_rel);
436 input_sync(dev);
438 break;
440 default:
441 break;
447 * Multisystem joystick support
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 */
454 * gc_multi_read_packet() reads a Multisystem joystick packet.
457 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
459 int i;
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;
467 static void gc_multi_process_packet(struct gc *gc)
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;
475 gc_multi_read_packet(gc, data_len, data);
477 for (i = 0; i < GC_MAX_DEVICES; i++) {
478 pad = &gc->pads[i];
479 dev = pad->dev;
480 s = gc_status_bit[i];
482 switch (pad->type) {
483 case GC_MULTI2:
484 input_report_key(dev, BTN_THUMB, s & data[5]);
485 fallthrough;
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;
496 default:
497 break;
503 * PSX support
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
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 */
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 */
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 */
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 */
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)");
533 static const short gc_psx_abs[] = {
534 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
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
540 static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
543 * gc_psx_command() writes 8bit command and reads 8bit data from
544 * the psx pad.
547 static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
549 struct parport *port = gc->pd->port;
550 int i, j, cmd, read;
552 memset(data, 0, GC_MAX_DEVICES);
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);
559 read = parport_read_status(port) ^ 0x80;
561 for (j = 0; j < GC_MAX_DEVICES; j++) {
562 struct gc_pad *pad = &gc->pads[j];
564 if (pad->type == GC_PSX || pad->type == GC_DDR)
565 data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
568 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
569 udelay(gc_psx_delay);
574 * gc_psx_read_packet() reads a whole psx packet and returns
575 * device identifier code.
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])
582 int i, j, max_len = 0;
583 unsigned long flags;
584 unsigned char data2[GC_MAX_DEVICES];
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);
593 local_irq_save(flags);
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 */
599 /* Find the longest pad */
600 for (i = 0; i < GC_MAX_DEVICES; i++) {
601 struct gc_pad *pad = &gc->pads[i];
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]);
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];
617 local_irq_restore(flags);
619 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
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]);
626 static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
627 unsigned char *data)
629 struct input_dev *dev = pad->dev;
630 int i;
632 switch (psx_type) {
634 case GC_PSX_RUMBLE:
636 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
637 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
638 fallthrough;
640 case GC_PSX_NEGCON:
641 case GC_PSX_ANALOG:
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]);
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);
658 for (i = 0; i < 8; i++)
659 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
661 input_report_key(dev, BTN_START, ~data[0] & 0x08);
662 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
664 input_sync(dev);
666 break;
668 case GC_PSX_NORMAL:
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);
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
692 for (i = 0; i < 8; i++)
693 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
695 input_report_key(dev, BTN_START, ~data[0] & 0x08);
696 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
698 input_sync(dev);
700 break;
702 default: /* not a pad, ignore */
703 break;
707 static void gc_psx_process_packet(struct gc *gc)
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;
714 gc_psx_read_packet(gc, data, id);
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]);
724 * gc_timer() initiates reads of console pads data.
727 static void gc_timer(struct timer_list *t)
729 struct gc *gc = from_timer(gc, t, timer);
732 * N64 pads - must be read first, any read confuses them for 200 us
735 if (gc->pad_count[GC_N64])
736 gc_n64_process_packet(gc);
739 * NES and SNES pads or mouse
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);
749 * Multi and Multi2 joysticks
752 if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
753 gc_multi_process_packet(gc);
756 * PSX controllers
759 if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
760 gc_psx_process_packet(gc);
762 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
765 static int gc_open(struct input_dev *dev)
767 struct gc *gc = input_get_drvdata(dev);
769 scoped_guard(mutex_intr, &gc->mutex) {
770 if (!gc->used++) {
771 parport_claim(gc->pd);
772 parport_write_control(gc->pd->port, 0x04);
773 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
776 return 0;
779 return -EINTR;
782 static void gc_close(struct input_dev *dev)
784 struct gc *gc = input_get_drvdata(dev);
786 guard(mutex)(&gc->mutex);
788 if (!--gc->used) {
789 del_timer_sync(&gc->timer);
790 parport_write_control(gc->pd->port, 0x00);
791 parport_release(gc->pd);
795 static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
797 struct gc_pad *pad = &gc->pads[idx];
798 struct input_dev *input_dev;
799 int i;
800 int err;
802 if (pad_type < 1 || pad_type >= GC_MAX) {
803 pr_err("Pad type %d unknown\n", pad_type);
804 return -EINVAL;
807 pad->dev = input_dev = input_allocate_device();
808 if (!input_dev) {
809 pr_err("Not enough memory for input device\n");
810 return -ENOMEM;
813 pad->type = pad_type;
815 snprintf(pad->phys, sizeof(pad->phys),
816 "%s/input%d", gc->pd->port->name, idx);
818 input_dev->name = gc_names[pad_type];
819 input_dev->phys = pad->phys;
820 input_dev->id.bustype = BUS_PARPORT;
821 input_dev->id.vendor = 0x0001;
822 input_dev->id.product = pad_type;
823 input_dev->id.version = 0x0100;
825 input_set_drvdata(input_dev, gc);
827 input_dev->open = gc_open;
828 input_dev->close = gc_close;
830 if (pad_type != GC_SNESMOUSE) {
831 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
833 for (i = 0; i < 2; i++)
834 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
835 } else
836 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
838 gc->pad_count[pad_type]++;
840 switch (pad_type) {
842 case GC_N64:
843 for (i = 0; i < 10; i++)
844 input_set_capability(input_dev, EV_KEY, gc_n64_btn[i]);
846 for (i = 0; i < 2; i++) {
847 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
848 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
851 err = gc_n64_init_ff(input_dev, idx);
852 if (err) {
853 pr_warn("Failed to initiate rumble for N64 device %d\n",
854 idx);
855 goto err_free_dev;
858 break;
860 case GC_SNESMOUSE:
861 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
862 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
863 input_set_capability(input_dev, EV_REL, REL_X);
864 input_set_capability(input_dev, EV_REL, REL_Y);
865 break;
867 case GC_SNES:
868 for (i = 4; i < 8; i++)
869 input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
870 fallthrough;
872 case GC_NES:
873 for (i = 0; i < 4; i++)
874 input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
875 break;
877 case GC_MULTI2:
878 input_set_capability(input_dev, EV_KEY, BTN_THUMB);
879 fallthrough;
881 case GC_MULTI:
882 input_set_capability(input_dev, EV_KEY, BTN_TRIGGER);
883 break;
885 case GC_PSX:
886 for (i = 0; i < 6; i++)
887 input_set_abs_params(input_dev,
888 gc_psx_abs[i], 4, 252, 0, 2);
889 for (i = 0; i < 12; i++)
890 input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
891 break;
893 break;
895 case GC_DDR:
896 for (i = 0; i < 4; i++)
897 input_set_capability(input_dev, EV_KEY,
898 gc_psx_ddr_btn[i]);
899 for (i = 0; i < 12; i++)
900 input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
902 break;
905 err = input_register_device(pad->dev);
906 if (err)
907 goto err_free_dev;
909 return 0;
911 err_free_dev:
912 input_free_device(pad->dev);
913 pad->dev = NULL;
914 return err;
917 static void gc_attach(struct parport *pp)
919 struct gc *gc;
920 struct pardevice *pd;
921 int i, port_idx;
922 int count = 0;
923 int *pads, n_pads;
924 struct pardev_cb gc_parport_cb;
926 for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
927 if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
928 continue;
930 if (gc_cfg[port_idx].args[0] == pp->number)
931 break;
934 if (port_idx == GC_MAX_PORTS) {
935 pr_debug("Not using parport%d.\n", pp->number);
936 return;
938 pads = gc_cfg[port_idx].args + 1;
939 n_pads = gc_cfg[port_idx].nargs - 1;
941 memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
942 gc_parport_cb.flags = PARPORT_FLAG_EXCL;
944 pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
945 port_idx);
946 if (!pd) {
947 pr_err("parport busy already - lp.o loaded?\n");
948 return;
951 gc = kzalloc(sizeof(*gc), GFP_KERNEL);
952 if (!gc) {
953 pr_err("Not enough memory\n");
954 goto err_unreg_pardev;
957 mutex_init(&gc->mutex);
958 gc->pd = pd;
959 gc->parportno = pp->number;
960 timer_setup(&gc->timer, gc_timer, 0);
962 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
963 if (!pads[i])
964 continue;
966 if (gc_setup_pad(gc, i, pads[i]))
967 goto err_unreg_devs;
969 count++;
972 if (count == 0) {
973 pr_err("No valid devices specified\n");
974 goto err_free_gc;
977 gc_base[port_idx] = gc;
978 return;
980 err_unreg_devs:
981 while (--i >= 0)
982 if (gc->pads[i].dev)
983 input_unregister_device(gc->pads[i].dev);
984 err_free_gc:
985 kfree(gc);
986 err_unreg_pardev:
987 parport_unregister_device(pd);
990 static void gc_detach(struct parport *port)
992 int i;
993 struct gc *gc;
995 for (i = 0; i < GC_MAX_PORTS; i++) {
996 if (gc_base[i] && gc_base[i]->parportno == port->number)
997 break;
1000 if (i == GC_MAX_PORTS)
1001 return;
1003 gc = gc_base[i];
1004 gc_base[i] = NULL;
1006 for (i = 0; i < GC_MAX_DEVICES; i++)
1007 if (gc->pads[i].dev)
1008 input_unregister_device(gc->pads[i].dev);
1009 parport_unregister_device(gc->pd);
1010 kfree(gc);
1013 static struct parport_driver gc_parport_driver = {
1014 .name = "gamecon",
1015 .match_port = gc_attach,
1016 .detach = gc_detach,
1019 static int __init gc_init(void)
1021 int i;
1022 int have_dev = 0;
1024 for (i = 0; i < GC_MAX_PORTS; i++) {
1025 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
1026 continue;
1028 if (gc_cfg[i].nargs < 2) {
1029 pr_err("at least one device must be specified\n");
1030 return -EINVAL;
1033 have_dev = 1;
1036 if (!have_dev)
1037 return -ENODEV;
1039 return parport_register_driver(&gc_parport_driver);
1042 static void __exit gc_exit(void)
1044 parport_unregister_driver(&gc_parport_driver);
1047 module_init(gc_init);
1048 module_exit(gc_exit);