2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
4 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
5 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
7 * Based on the work of:
8 * Andree Borrmann John Dahlstrom
9 * David Kuder Nathan Hand
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 2 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the Free Software
25 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 * Should you need to contact me, the author, you can do so either by
28 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
29 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
32 #include <linux/kernel.h>
33 #include <linux/delay.h>
34 #include <linux/module.h>
35 #include <linux/moduleparam.h>
36 #include <linux/init.h>
37 #include <linux/parport.h>
38 #include <linux/input.h>
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
42 MODULE_LICENSE("GPL");
44 #define GC_MAX_PORTS 3
45 #define GC_MAX_DEVICES 5
48 int args[GC_MAX_DEVICES + 1];
52 static struct gc_config gc[GC_MAX_PORTS] __initdata;
54 module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0);
55 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
56 module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0);
57 MODULE_PARM_DESC(map2, "Describes second set of devices");
58 module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0);
59 MODULE_PARM_DESC(map3, "Describes third set of devices");
61 __obsolete_setup("gc=");
62 __obsolete_setup("gc_2=");
63 __obsolete_setup("gc_3=");
65 /* see also gs_psx_delay parameter in PSX support section */
78 #define GC_REFRESH_TIME HZ/100
82 struct input_dev *dev[GC_MAX_DEVICES];
83 struct timer_list timer;
84 unsigned char pads[GC_MAX + 1];
87 char phys[GC_MAX_DEVICES][32];
90 static struct gc *gc_base[3];
92 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
94 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
95 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
96 "PSX DDR controller" };
101 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
102 static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, 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_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */
106 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
107 #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */
108 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
109 /* GC_N64_DWS > 24 is known to fail */
110 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
111 #define GC_N64_POWER_R 0xfd /* power during read */
112 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
113 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
114 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
116 #define GC_N64_CLOCK 0x02 /* clock bits for read */
119 * gc_n64_read_packet() reads an N64 packet.
120 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
123 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
129 * Request the pad to transmit data
132 local_irq_save(flags);
133 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
134 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
137 local_irq_restore(flags);
140 * Wait for the pad response to be loaded into the 33-bit register of the adapter
143 udelay(GC_N64_DELAY);
146 * Grab data (ignoring the last bit, which is a stop bit)
149 for (i = 0; i < GC_N64_LENGTH; i++) {
150 parport_write_data(gc->pd->port, GC_N64_POWER_R);
151 data[i] = parport_read_status(gc->pd->port);
152 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
156 * We must wait 200 ms here for the controller to reinitialize before the next read request.
157 * No worries as long as gc_read is polled less frequently than this.
162 static void gc_n64_process_packet(struct gc *gc)
164 unsigned char data[GC_N64_LENGTH];
166 struct input_dev *dev;
169 gc_n64_read_packet(gc, data);
171 for (i = 0; i < GC_MAX_DEVICES; i++) {
177 s = gc_status_bit[i];
179 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
181 axes[0] = axes[1] = 0;
183 for (j = 0; j < 8; j++) {
184 if (data[23 - j] & s)
186 if (data[31 - j] & s)
190 input_report_abs(dev, ABS_X, axes[0]);
191 input_report_abs(dev, ABS_Y, -axes[1]);
193 input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
194 input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
196 for (j = 0; j < 10; j++)
197 input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
208 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
209 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
210 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the last 4 bits are unused */
212 #define GC_NES_POWER 0xfc
213 #define GC_NES_CLOCK 0x01
214 #define GC_NES_LATCH 0x02
216 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
217 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
218 static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
221 * gc_nes_read_packet() reads a NES/SNES packet.
222 * Each pad uses one bit per byte. So all pads connected to
223 * this port are read in parallel.
226 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
230 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
231 udelay(GC_NES_DELAY * 2);
232 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
234 for (i = 0; i < length; i++) {
235 udelay(GC_NES_DELAY);
236 parport_write_data(gc->pd->port, GC_NES_POWER);
237 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
238 udelay(GC_NES_DELAY);
239 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
243 static void gc_nes_process_packet(struct gc *gc)
245 unsigned char data[GC_SNES_LENGTH];
246 struct input_dev *dev;
249 gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
251 for (i = 0; i < GC_MAX_DEVICES; i++) {
257 s = gc_status_bit[i];
259 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
260 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
261 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
264 if (s & gc->pads[GC_NES])
265 for (j = 0; j < 4; j++)
266 input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
268 if (s & gc->pads[GC_SNES])
269 for (j = 0; j < 8; j++)
270 input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
277 * Multisystem joystick support
280 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
281 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
284 * gc_multi_read_packet() reads a Multisystem joystick packet.
287 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
291 for (i = 0; i < length; i++) {
292 parport_write_data(gc->pd->port, ~(1 << i));
293 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
297 static void gc_multi_process_packet(struct gc *gc)
299 unsigned char data[GC_MULTI2_LENGTH];
300 struct input_dev *dev;
303 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
305 for (i = 0; i < GC_MAX_DEVICES; i++) {
311 s = gc_status_bit[i];
313 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
314 input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3]));
315 input_report_abs(dev, ABS_Y, !(s & data[0]) - !(s & data[1]));
316 input_report_key(dev, BTN_TRIGGER, s & data[4]);
319 if (s & gc->pads[GC_MULTI2])
320 input_report_key(dev, BTN_THUMB, s & data[5]);
329 * See documentation at:
330 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
331 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
332 * ftp://milano.usal.es/pablo/
336 #define GC_PSX_DELAY 25 /* 25 usec */
337 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
338 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
340 #define GC_PSX_MOUSE 1 /* Mouse */
341 #define GC_PSX_NEGCON 2 /* NegCon */
342 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
343 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
344 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
346 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
347 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
348 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
349 #define GC_PSX_SELECT 0x02 /* Pin 3 */
351 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
352 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
354 static int gc_psx_delay = GC_PSX_DELAY;
355 module_param_named(psx_delay, gc_psx_delay, uint, 0);
356 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
358 __obsolete_setup("gc_psx_delay=");
360 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
361 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
362 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
363 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
366 * gc_psx_command() writes 8bit command and reads 8bit data from
370 static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
374 for (i = 0; i < GC_MAX_DEVICES; i++)
377 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
378 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
379 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
380 udelay(gc_psx_delay);
381 read = parport_read_status(gc->pd->port) ^ 0x80;
382 for (j = 0; j < GC_MAX_DEVICES; j++)
383 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
384 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
385 udelay(gc_psx_delay);
390 * gc_psx_read_packet() reads a whole psx packet and returns
391 * device identifier code.
394 static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
395 unsigned char id[GC_MAX_DEVICES])
397 int i, j, max_len = 0;
399 unsigned char data2[GC_MAX_DEVICES];
401 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
402 udelay(gc_psx_delay);
403 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
404 udelay(gc_psx_delay);
406 local_irq_save(flags);
408 gc_psx_command(gc, 0x01, data2); /* Access pad */
409 gc_psx_command(gc, 0x42, id); /* Get device ids */
410 gc_psx_command(gc, 0, data2); /* Dump status */
412 for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */
413 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
414 && (GC_PSX_LEN(id[i]) > max_len)
415 && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
416 max_len = GC_PSX_LEN(id[i]);
418 for (i = 0; i < max_len; i++) { /* Read in all the data */
419 gc_psx_command(gc, 0, data2);
420 for (j = 0; j < GC_MAX_DEVICES; j++)
421 data[j][i] = data2[j];
424 local_irq_restore(flags);
426 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
428 for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */
429 id[i] = GC_PSX_ID(id[i]);
432 static void gc_psx_process_packet(struct gc *gc)
434 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
435 unsigned char id[GC_MAX_DEVICES];
436 struct input_dev *dev;
439 gc_psx_read_packet(gc, data, id);
441 for (i = 0; i < GC_MAX_DEVICES; i++) {
451 input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
452 input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
457 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
458 for(j = 0; j < 4; j++)
459 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
461 for (j = 0; j < 4; j++)
462 input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
464 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
465 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
468 for (j = 0; j < 8; j++)
469 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
471 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
472 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
479 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
480 for(j = 0; j < 4; j++)
481 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
483 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
484 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
486 /* for some reason if the extra axes are left unset they drift */
487 /* for (j = 0; j < 4; j++)
488 input_report_abs(dev, gc_psx_abs[j + 2], 128);
489 * This needs to be debugged properly,
490 * maybe fuzz processing needs to be done in input_sync()
495 for (j = 0; j < 8; j++)
496 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
498 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
499 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
505 case 0: /* not a pad, ignore */
512 * gc_timer() initiates reads of console pads data.
515 static void gc_timer(unsigned long private)
517 struct gc *gc = (void *) private;
520 * N64 pads - must be read first, any read confuses them for 200 us
523 if (gc->pads[GC_N64])
524 gc_n64_process_packet(gc);
530 if (gc->pads[GC_NES] || gc->pads[GC_SNES])
531 gc_nes_process_packet(gc);
534 * Multi and Multi2 joysticks
537 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
538 gc_multi_process_packet(gc);
544 if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
545 gc_psx_process_packet(gc);
547 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
550 static int gc_open(struct input_dev *dev)
552 struct gc *gc = dev->private;
555 err = down_interruptible(&gc->sem);
560 parport_claim(gc->pd);
561 parport_write_control(gc->pd->port, 0x04);
562 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
569 static void gc_close(struct input_dev *dev)
571 struct gc *gc = dev->private;
575 del_timer_sync(&gc->timer);
576 parport_write_control(gc->pd->port, 0x00);
577 parport_release(gc->pd);
582 static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
584 struct input_dev *input_dev;
590 if (pad_type < 1 || pad_type > GC_MAX) {
591 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
595 gc->dev[idx] = input_dev = input_allocate_device();
597 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
601 input_dev->name = gc_names[pad_type];
602 input_dev->phys = gc->phys[idx];
603 input_dev->id.bustype = BUS_PARPORT;
604 input_dev->id.vendor = 0x0001;
605 input_dev->id.product = pad_type;
606 input_dev->id.version = 0x0100;
607 input_dev->private = gc;
609 input_dev->open = gc_open;
610 input_dev->close = gc_close;
612 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
614 for (i = 0; i < 2; i++)
615 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
617 gc->pads[0] |= gc_status_bit[idx];
618 gc->pads[pad_type] |= gc_status_bit[idx];
623 for (i = 0; i < 10; i++)
624 set_bit(gc_n64_btn[i], input_dev->keybit);
626 for (i = 0; i < 2; i++) {
627 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
628 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
634 for (i = 4; i < 8; i++)
635 set_bit(gc_snes_btn[i], input_dev->keybit);
637 for (i = 0; i < 4; i++)
638 set_bit(gc_snes_btn[i], input_dev->keybit);
642 set_bit(BTN_THUMB, input_dev->keybit);
644 set_bit(BTN_TRIGGER, input_dev->keybit);
648 for (i = 0; i < 6; i++)
649 input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
650 for (i = 0; i < 12; i++)
651 set_bit(gc_psx_btn[i], input_dev->keybit);
656 for (i = 0; i < 4; i++)
657 set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
658 for (i = 0; i < 12; i++)
659 set_bit(gc_psx_btn[i], input_dev->keybit);
667 static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
671 struct pardevice *pd;
675 pp = parport_find_number(parport);
677 printk(KERN_ERR "gamecon.c: no such parport\n");
682 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
684 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
689 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
691 printk(KERN_ERR "gamecon.c: Not enough memory\n");
693 goto err_unreg_pardev;
696 init_MUTEX(&gc->sem);
698 init_timer(&gc->timer);
699 gc->timer.data = (long) gc;
700 gc->timer.function = gc_timer;
702 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
706 sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
707 err = gc_setup_pad(gc, i, pads[i]);
711 err = input_register_device(gc->dev[i]);
717 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
722 parport_put_port(pp);
726 input_free_device(gc->dev[i]);
730 input_unregister_device(gc->dev[i]);
734 parport_unregister_device(pd);
736 parport_put_port(pp);
741 static void gc_remove(struct gc *gc)
745 for (i = 0; i < GC_MAX_DEVICES; i++)
747 input_unregister_device(gc->dev[i]);
748 parport_unregister_device(gc->pd);
752 static int __init gc_init(void)
758 for (i = 0; i < GC_MAX_PORTS; i++) {
759 if (gc[i].nargs == 0 || gc[i].args[0] < 0)
762 if (gc[i].nargs < 2) {
763 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
768 gc_base[i] = gc_probe(gc[i].args[0], gc[i].args + 1, gc[i].nargs - 1);
769 if (IS_ERR(gc_base[i])) {
770 err = PTR_ERR(gc_base[i]);
780 gc_remove(gc_base[i]);
784 return have_dev ? 0 : -ENODEV;
787 static void __exit gc_exit(void)
791 for (i = 0; i < GC_MAX_PORTS; i++)
793 gc_remove(gc_base[i]);
796 module_init(gc_init);
797 module_exit(gc_exit);