Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / joystick / gamecon.c
blobd37645e496ff17838d443699d549e42ec0f5d4c7
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
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/parport.h>
24 #include <linux/input.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
30 MODULE_LICENSE("GPL");
32 #define GC_MAX_PORTS 3
33 #define GC_MAX_DEVICES 5
35 struct gc_config {
36 int args[GC_MAX_DEVICES + 1];
37 unsigned int nargs;
40 static struct gc_config gc_cfg[GC_MAX_PORTS];
42 module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
43 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
44 module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
45 MODULE_PARM_DESC(map2, "Describes second set of devices");
46 module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
47 MODULE_PARM_DESC(map3, "Describes third set of devices");
49 /* see also gs_psx_delay parameter in PSX support section */
51 enum gc_type {
52 GC_NONE = 0,
53 GC_SNES,
54 GC_NES,
55 GC_NES4,
56 GC_MULTI,
57 GC_MULTI2,
58 GC_N64,
59 GC_PSX,
60 GC_DDR,
61 GC_SNESMOUSE,
62 GC_MAX
65 #define GC_REFRESH_TIME HZ/100
67 struct gc_pad {
68 struct input_dev *dev;
69 enum gc_type type;
70 char phys[32];
73 struct gc {
74 struct pardevice *pd;
75 struct gc_pad pads[GC_MAX_DEVICES];
76 struct timer_list timer;
77 int pad_count[GC_MAX];
78 int used;
79 int parportno;
80 struct mutex mutex;
83 struct gc_subdev {
84 unsigned int idx;
87 static struct gc *gc_base[3];
89 static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
91 static const char *gc_names[] = {
92 NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
93 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
94 "PSX DDR controller", "SNES mouse"
98 * N64 support.
101 static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
102 static const short gc_n64_btn[] = {
103 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
104 BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
107 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
108 #define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
109 #define GC_N64_CMD_00 0x11111111UL
110 #define GC_N64_CMD_01 0xd1111111UL
111 #define GC_N64_CMD_03 0xdd111111UL
112 #define GC_N64_CMD_1b 0xdd1dd111UL
113 #define GC_N64_CMD_c0 0x111111ddUL
114 #define GC_N64_CMD_80 0x1111111dUL
115 #define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
116 #define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
117 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
118 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
119 /* GC_N64_DWS > 24 is known to fail */
120 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
121 #define GC_N64_POWER_R 0xfd /* power during read */
122 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
123 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
124 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
125 /* than 123 us */
126 #define GC_N64_CLOCK 0x02 /* clock bits for read */
129 * Used for rumble code.
132 /* Send encoded command */
133 static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
134 unsigned char target)
136 struct parport *port = gc->pd->port;
137 int i;
139 for (i = 0; i < GC_N64_LENGTH; i++) {
140 unsigned char data = (cmd >> i) & 1 ? target : 0;
141 parport_write_data(port, GC_N64_POWER_W | data);
142 udelay(GC_N64_DWS);
146 /* Send stop bit */
147 static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
149 struct parport *port = gc->pd->port;
150 int i;
152 for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
153 unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
154 parport_write_data(port, GC_N64_POWER_W | data);
155 udelay(GC_N64_DWS);
160 * gc_n64_read_packet() reads an N64 packet.
161 * Each pad uses one bit per byte. So all pads connected to this port
162 * are read in parallel.
165 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
167 int i;
168 unsigned long flags;
171 * Request the pad to transmit data
174 local_irq_save(flags);
175 gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
176 gc_n64_send_stop_bit(gc, GC_N64_OUT);
177 local_irq_restore(flags);
180 * Wait for the pad response to be loaded into the 33-bit register
181 * of the adapter.
184 udelay(GC_N64_DELAY);
187 * Grab data (ignoring the last bit, which is a stop bit)
190 for (i = 0; i < GC_N64_LENGTH; i++) {
191 parport_write_data(gc->pd->port, GC_N64_POWER_R);
192 udelay(2);
193 data[i] = parport_read_status(gc->pd->port);
194 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
198 * We must wait 200 ms here for the controller to reinitialize before
199 * the next read request. No worries as long as gc_read is polled less
200 * frequently than this.
205 static void gc_n64_process_packet(struct gc *gc)
207 unsigned char data[GC_N64_LENGTH];
208 struct input_dev *dev;
209 int i, j, s;
210 signed char x, y;
212 gc_n64_read_packet(gc, data);
214 for (i = 0; i < GC_MAX_DEVICES; i++) {
216 if (gc->pads[i].type != GC_N64)
217 continue;
219 dev = gc->pads[i].dev;
220 s = gc_status_bit[i];
222 if (s & ~(data[8] | data[9])) {
224 x = y = 0;
226 for (j = 0; j < 8; j++) {
227 if (data[23 - j] & s)
228 x |= 1 << j;
229 if (data[31 - j] & s)
230 y |= 1 << j;
233 input_report_abs(dev, ABS_X, x);
234 input_report_abs(dev, ABS_Y, -y);
236 input_report_abs(dev, ABS_HAT0X,
237 !(s & data[6]) - !(s & data[7]));
238 input_report_abs(dev, ABS_HAT0Y,
239 !(s & data[4]) - !(s & data[5]));
241 for (j = 0; j < 10; j++)
242 input_report_key(dev, gc_n64_btn[j],
243 s & data[gc_n64_bytes[j]]);
245 input_sync(dev);
250 static int gc_n64_play_effect(struct input_dev *dev, void *data,
251 struct ff_effect *effect)
253 int i;
254 unsigned long flags;
255 struct gc *gc = input_get_drvdata(dev);
256 struct gc_subdev *sdev = data;
257 unsigned char target = 1 << sdev->idx; /* select desired pin */
259 if (effect->type == FF_RUMBLE) {
260 struct ff_rumble_effect *rumble = &effect->u.rumble;
261 unsigned int cmd =
262 rumble->strong_magnitude || rumble->weak_magnitude ?
263 GC_N64_CMD_01 : GC_N64_CMD_00;
265 local_irq_save(flags);
267 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
268 gc_n64_send_command(gc, GC_N64_CMD_03, target);
269 gc_n64_send_command(gc, GC_N64_CMD_80, target);
270 gc_n64_send_command(gc, GC_N64_CMD_01, target);
271 for (i = 0; i < 32; i++)
272 gc_n64_send_command(gc, GC_N64_CMD_80, target);
273 gc_n64_send_stop_bit(gc, target);
275 udelay(GC_N64_DELAY);
277 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
278 gc_n64_send_command(gc, GC_N64_CMD_03, target);
279 gc_n64_send_command(gc, GC_N64_CMD_c0, target);
280 gc_n64_send_command(gc, GC_N64_CMD_1b, target);
281 for (i = 0; i < 32; i++)
282 gc_n64_send_command(gc, cmd, target);
283 gc_n64_send_stop_bit(gc, target);
285 local_irq_restore(flags);
289 return 0;
292 static int gc_n64_init_ff(struct input_dev *dev, int i)
294 struct gc_subdev *sdev;
295 int err;
297 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
298 if (!sdev)
299 return -ENOMEM;
301 sdev->idx = i;
303 input_set_capability(dev, EV_FF, FF_RUMBLE);
305 err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
306 if (err) {
307 kfree(sdev);
308 return err;
311 return 0;
315 * NES/SNES support.
318 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
319 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
320 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
321 last 4 bits are unused */
322 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
323 16 bits are equivalent to a gamepad */
325 #define GC_NES_POWER 0xfc
326 #define GC_NES_CLOCK 0x01
327 #define GC_NES_LATCH 0x02
329 static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
330 static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
331 static const short gc_snes_btn[] = {
332 BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
336 * gc_nes_read_packet() reads a NES/SNES packet.
337 * Each pad uses one bit per byte. So all pads connected to
338 * this port are read in parallel.
341 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
343 int i;
345 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
346 udelay(GC_NES_DELAY * 2);
347 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
349 for (i = 0; i < length; i++) {
350 udelay(GC_NES_DELAY);
351 parport_write_data(gc->pd->port, GC_NES_POWER);
352 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
353 udelay(GC_NES_DELAY);
354 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
358 static void gc_nes_process_packet(struct gc *gc)
360 unsigned char data[GC_SNESMOUSE_LENGTH];
361 struct gc_pad *pad;
362 struct input_dev *dev;
363 int i, j, s, len;
364 char x_rel, y_rel;
366 len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
367 (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
369 gc_nes_read_packet(gc, len, data);
371 for (i = 0; i < GC_MAX_DEVICES; i++) {
373 pad = &gc->pads[i];
374 dev = pad->dev;
375 s = gc_status_bit[i];
377 switch (pad->type) {
379 case GC_NES:
381 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
382 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
384 for (j = 0; j < 4; j++)
385 input_report_key(dev, gc_snes_btn[j],
386 s & data[gc_nes_bytes[j]]);
387 input_sync(dev);
388 break;
390 case GC_SNES:
392 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
393 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
395 for (j = 0; j < 8; j++)
396 input_report_key(dev, gc_snes_btn[j],
397 s & data[gc_snes_bytes[j]]);
398 input_sync(dev);
399 break;
401 case GC_SNESMOUSE:
403 * The 4 unused bits from SNES controllers appear
404 * to be ID bits so use them to make sure we are
405 * dealing with a mouse.
406 * gamepad is connected. This is important since
407 * my SNES gamepad sends 1's for bits 16-31, which
408 * cause the mouse pointer to quickly move to the
409 * upper left corner of the screen.
411 if (!(s & data[12]) && !(s & data[13]) &&
412 !(s & data[14]) && (s & data[15])) {
413 input_report_key(dev, BTN_LEFT, s & data[9]);
414 input_report_key(dev, BTN_RIGHT, s & data[8]);
416 x_rel = y_rel = 0;
417 for (j = 0; j < 7; j++) {
418 x_rel <<= 1;
419 if (data[25 + j] & s)
420 x_rel |= 1;
422 y_rel <<= 1;
423 if (data[17 + j] & s)
424 y_rel |= 1;
427 if (x_rel) {
428 if (data[24] & s)
429 x_rel = -x_rel;
430 input_report_rel(dev, REL_X, x_rel);
433 if (y_rel) {
434 if (data[16] & s)
435 y_rel = -y_rel;
436 input_report_rel(dev, REL_Y, y_rel);
439 input_sync(dev);
441 break;
443 default:
444 break;
450 * Multisystem joystick support
453 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
454 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
457 * gc_multi_read_packet() reads a Multisystem joystick packet.
460 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
462 int i;
464 for (i = 0; i < length; i++) {
465 parport_write_data(gc->pd->port, ~(1 << i));
466 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
470 static void gc_multi_process_packet(struct gc *gc)
472 unsigned char data[GC_MULTI2_LENGTH];
473 int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
474 struct gc_pad *pad;
475 struct input_dev *dev;
476 int i, s;
478 gc_multi_read_packet(gc, data_len, data);
480 for (i = 0; i < GC_MAX_DEVICES; i++) {
481 pad = &gc->pads[i];
482 dev = pad->dev;
483 s = gc_status_bit[i];
485 switch (pad->type) {
486 case GC_MULTI2:
487 input_report_key(dev, BTN_THUMB, s & data[5]);
488 fallthrough;
490 case GC_MULTI:
491 input_report_abs(dev, ABS_X,
492 !(s & data[2]) - !(s & data[3]));
493 input_report_abs(dev, ABS_Y,
494 !(s & data[0]) - !(s & data[1]));
495 input_report_key(dev, BTN_TRIGGER, s & data[4]);
496 input_sync(dev);
497 break;
499 default:
500 break;
506 * PSX support
508 * See documentation at:
509 * http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
510 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
514 #define GC_PSX_DELAY 25 /* 25 usec */
515 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
516 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
518 #define GC_PSX_MOUSE 1 /* Mouse */
519 #define GC_PSX_NEGCON 2 /* NegCon */
520 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
521 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
522 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
524 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
525 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
526 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
527 #define GC_PSX_SELECT 0x02 /* Pin 3 */
529 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
530 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
532 static int gc_psx_delay = GC_PSX_DELAY;
533 module_param_named(psx_delay, gc_psx_delay, uint, 0);
534 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
536 static const short gc_psx_abs[] = {
537 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
539 static const short gc_psx_btn[] = {
540 BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
541 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
543 static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
546 * gc_psx_command() writes 8bit command and reads 8bit data from
547 * the psx pad.
550 static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
552 struct parport *port = gc->pd->port;
553 int i, j, cmd, read;
555 memset(data, 0, GC_MAX_DEVICES);
557 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
558 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
559 parport_write_data(port, cmd | GC_PSX_POWER);
560 udelay(gc_psx_delay);
562 read = parport_read_status(port) ^ 0x80;
564 for (j = 0; j < GC_MAX_DEVICES; j++) {
565 struct gc_pad *pad = &gc->pads[j];
567 if (pad->type == GC_PSX || pad->type == GC_DDR)
568 data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
571 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
572 udelay(gc_psx_delay);
577 * gc_psx_read_packet() reads a whole psx packet and returns
578 * device identifier code.
581 static void gc_psx_read_packet(struct gc *gc,
582 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
583 unsigned char id[GC_MAX_DEVICES])
585 int i, j, max_len = 0;
586 unsigned long flags;
587 unsigned char data2[GC_MAX_DEVICES];
589 /* Select pad */
590 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
591 udelay(gc_psx_delay);
592 /* Deselect, begin command */
593 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
594 udelay(gc_psx_delay);
596 local_irq_save(flags);
598 gc_psx_command(gc, 0x01, data2); /* Access pad */
599 gc_psx_command(gc, 0x42, id); /* Get device ids */
600 gc_psx_command(gc, 0, data2); /* Dump status */
602 /* Find the longest pad */
603 for (i = 0; i < GC_MAX_DEVICES; i++) {
604 struct gc_pad *pad = &gc->pads[i];
606 if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
607 GC_PSX_LEN(id[i]) > max_len &&
608 GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
609 max_len = GC_PSX_LEN(id[i]);
613 /* Read in all the data */
614 for (i = 0; i < max_len; i++) {
615 gc_psx_command(gc, 0, data2);
616 for (j = 0; j < GC_MAX_DEVICES; j++)
617 data[j][i] = data2[j];
620 local_irq_restore(flags);
622 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
624 /* Set id's to the real value */
625 for (i = 0; i < GC_MAX_DEVICES; i++)
626 id[i] = GC_PSX_ID(id[i]);
629 static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
630 unsigned char *data)
632 struct input_dev *dev = pad->dev;
633 int i;
635 switch (psx_type) {
637 case GC_PSX_RUMBLE:
639 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
640 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
641 fallthrough;
643 case GC_PSX_NEGCON:
644 case GC_PSX_ANALOG:
646 if (pad->type == GC_DDR) {
647 for (i = 0; i < 4; i++)
648 input_report_key(dev, gc_psx_ddr_btn[i],
649 ~data[0] & (0x10 << i));
650 } else {
651 for (i = 0; i < 4; i++)
652 input_report_abs(dev, gc_psx_abs[i + 2],
653 data[i + 2]);
655 input_report_abs(dev, ABS_X,
656 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
657 input_report_abs(dev, ABS_Y,
658 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
661 for (i = 0; i < 8; i++)
662 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
664 input_report_key(dev, BTN_START, ~data[0] & 0x08);
665 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
667 input_sync(dev);
669 break;
671 case GC_PSX_NORMAL:
673 if (pad->type == GC_DDR) {
674 for (i = 0; i < 4; i++)
675 input_report_key(dev, gc_psx_ddr_btn[i],
676 ~data[0] & (0x10 << i));
677 } else {
678 input_report_abs(dev, ABS_X,
679 !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
680 input_report_abs(dev, ABS_Y,
681 !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
684 * For some reason if the extra axes are left unset
685 * they drift.
686 * for (i = 0; i < 4; i++)
687 input_report_abs(dev, gc_psx_abs[i + 2], 128);
688 * This needs to be debugged properly,
689 * maybe fuzz processing needs to be done
690 * in input_sync()
691 * --vojtech
695 for (i = 0; i < 8; i++)
696 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
698 input_report_key(dev, BTN_START, ~data[0] & 0x08);
699 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
701 input_sync(dev);
703 break;
705 default: /* not a pad, ignore */
706 break;
710 static void gc_psx_process_packet(struct gc *gc)
712 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
713 unsigned char id[GC_MAX_DEVICES];
714 struct gc_pad *pad;
715 int i;
717 gc_psx_read_packet(gc, data, id);
719 for (i = 0; i < GC_MAX_DEVICES; i++) {
720 pad = &gc->pads[i];
721 if (pad->type == GC_PSX || pad->type == GC_DDR)
722 gc_psx_report_one(pad, id[i], data[i]);
727 * gc_timer() initiates reads of console pads data.
730 static void gc_timer(struct timer_list *t)
732 struct gc *gc = from_timer(gc, t, timer);
735 * N64 pads - must be read first, any read confuses them for 200 us
738 if (gc->pad_count[GC_N64])
739 gc_n64_process_packet(gc);
742 * NES and SNES pads or mouse
745 if (gc->pad_count[GC_NES] ||
746 gc->pad_count[GC_SNES] ||
747 gc->pad_count[GC_SNESMOUSE]) {
748 gc_nes_process_packet(gc);
752 * Multi and Multi2 joysticks
755 if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
756 gc_multi_process_packet(gc);
759 * PSX controllers
762 if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
763 gc_psx_process_packet(gc);
765 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
768 static int gc_open(struct input_dev *dev)
770 struct gc *gc = input_get_drvdata(dev);
771 int err;
773 err = mutex_lock_interruptible(&gc->mutex);
774 if (err)
775 return err;
777 if (!gc->used++) {
778 parport_claim(gc->pd);
779 parport_write_control(gc->pd->port, 0x04);
780 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
783 mutex_unlock(&gc->mutex);
784 return 0;
787 static void gc_close(struct input_dev *dev)
789 struct gc *gc = input_get_drvdata(dev);
791 mutex_lock(&gc->mutex);
792 if (!--gc->used) {
793 del_timer_sync(&gc->timer);
794 parport_write_control(gc->pd->port, 0x00);
795 parport_release(gc->pd);
797 mutex_unlock(&gc->mutex);
800 static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
802 struct gc_pad *pad = &gc->pads[idx];
803 struct input_dev *input_dev;
804 int i;
805 int err;
807 if (pad_type < 1 || pad_type >= GC_MAX) {
808 pr_err("Pad type %d unknown\n", pad_type);
809 return -EINVAL;
812 pad->dev = input_dev = input_allocate_device();
813 if (!input_dev) {
814 pr_err("Not enough memory for input device\n");
815 return -ENOMEM;
818 pad->type = pad_type;
820 snprintf(pad->phys, sizeof(pad->phys),
821 "%s/input%d", gc->pd->port->name, idx);
823 input_dev->name = gc_names[pad_type];
824 input_dev->phys = pad->phys;
825 input_dev->id.bustype = BUS_PARPORT;
826 input_dev->id.vendor = 0x0001;
827 input_dev->id.product = pad_type;
828 input_dev->id.version = 0x0100;
830 input_set_drvdata(input_dev, gc);
832 input_dev->open = gc_open;
833 input_dev->close = gc_close;
835 if (pad_type != GC_SNESMOUSE) {
836 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
838 for (i = 0; i < 2; i++)
839 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
840 } else
841 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
843 gc->pad_count[pad_type]++;
845 switch (pad_type) {
847 case GC_N64:
848 for (i = 0; i < 10; i++)
849 input_set_capability(input_dev, EV_KEY, gc_n64_btn[i]);
851 for (i = 0; i < 2; i++) {
852 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
853 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
856 err = gc_n64_init_ff(input_dev, idx);
857 if (err) {
858 pr_warn("Failed to initiate rumble for N64 device %d\n",
859 idx);
860 goto err_free_dev;
863 break;
865 case GC_SNESMOUSE:
866 input_set_capability(input_dev, EV_KEY, BTN_LEFT);
867 input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
868 input_set_capability(input_dev, EV_REL, REL_X);
869 input_set_capability(input_dev, EV_REL, REL_Y);
870 break;
872 case GC_SNES:
873 for (i = 4; i < 8; i++)
874 input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
875 fallthrough;
877 case GC_NES:
878 for (i = 0; i < 4; i++)
879 input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
880 break;
882 case GC_MULTI2:
883 input_set_capability(input_dev, EV_KEY, BTN_THUMB);
884 fallthrough;
886 case GC_MULTI:
887 input_set_capability(input_dev, EV_KEY, BTN_TRIGGER);
888 break;
890 case GC_PSX:
891 for (i = 0; i < 6; i++)
892 input_set_abs_params(input_dev,
893 gc_psx_abs[i], 4, 252, 0, 2);
894 for (i = 0; i < 12; i++)
895 input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
896 break;
898 break;
900 case GC_DDR:
901 for (i = 0; i < 4; i++)
902 input_set_capability(input_dev, EV_KEY,
903 gc_psx_ddr_btn[i]);
904 for (i = 0; i < 12; i++)
905 input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
907 break;
910 err = input_register_device(pad->dev);
911 if (err)
912 goto err_free_dev;
914 return 0;
916 err_free_dev:
917 input_free_device(pad->dev);
918 pad->dev = NULL;
919 return err;
922 static void gc_attach(struct parport *pp)
924 struct gc *gc;
925 struct pardevice *pd;
926 int i, port_idx;
927 int count = 0;
928 int *pads, n_pads;
929 struct pardev_cb gc_parport_cb;
931 for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
932 if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
933 continue;
935 if (gc_cfg[port_idx].args[0] == pp->number)
936 break;
939 if (port_idx == GC_MAX_PORTS) {
940 pr_debug("Not using parport%d.\n", pp->number);
941 return;
943 pads = gc_cfg[port_idx].args + 1;
944 n_pads = gc_cfg[port_idx].nargs - 1;
946 memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
947 gc_parport_cb.flags = PARPORT_FLAG_EXCL;
949 pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
950 port_idx);
951 if (!pd) {
952 pr_err("parport busy already - lp.o loaded?\n");
953 return;
956 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
957 if (!gc) {
958 pr_err("Not enough memory\n");
959 goto err_unreg_pardev;
962 mutex_init(&gc->mutex);
963 gc->pd = pd;
964 gc->parportno = pp->number;
965 timer_setup(&gc->timer, gc_timer, 0);
967 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
968 if (!pads[i])
969 continue;
971 if (gc_setup_pad(gc, i, pads[i]))
972 goto err_unreg_devs;
974 count++;
977 if (count == 0) {
978 pr_err("No valid devices specified\n");
979 goto err_free_gc;
982 gc_base[port_idx] = gc;
983 return;
985 err_unreg_devs:
986 while (--i >= 0)
987 if (gc->pads[i].dev)
988 input_unregister_device(gc->pads[i].dev);
989 err_free_gc:
990 kfree(gc);
991 err_unreg_pardev:
992 parport_unregister_device(pd);
995 static void gc_detach(struct parport *port)
997 int i;
998 struct gc *gc;
1000 for (i = 0; i < GC_MAX_PORTS; i++) {
1001 if (gc_base[i] && gc_base[i]->parportno == port->number)
1002 break;
1005 if (i == GC_MAX_PORTS)
1006 return;
1008 gc = gc_base[i];
1009 gc_base[i] = NULL;
1011 for (i = 0; i < GC_MAX_DEVICES; i++)
1012 if (gc->pads[i].dev)
1013 input_unregister_device(gc->pads[i].dev);
1014 parport_unregister_device(gc->pd);
1015 kfree(gc);
1018 static struct parport_driver gc_parport_driver = {
1019 .name = "gamecon",
1020 .match_port = gc_attach,
1021 .detach = gc_detach,
1022 .devmodel = true,
1025 static int __init gc_init(void)
1027 int i;
1028 int have_dev = 0;
1030 for (i = 0; i < GC_MAX_PORTS; i++) {
1031 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
1032 continue;
1034 if (gc_cfg[i].nargs < 2) {
1035 pr_err("at least one device must be specified\n");
1036 return -EINVAL;
1039 have_dev = 1;
1042 if (!have_dev)
1043 return -ENODEV;
1045 return parport_register_driver(&gc_parport_driver);
1048 static void __exit gc_exit(void)
1050 parport_unregister_driver(&gc_parport_driver);
1053 module_init(gc_init);
1054 module_exit(gc_exit);