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 static int gc[] __initdata = { -1, 0, 0, 0, 0, 0 };
45 static int gc_nargs __initdata = 0;
46 module_param_array_named(map, gc, int, &gc_nargs, 0);
47 MODULE_PARM_DESC(map, "Describers first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
49 static int gc_2[] __initdata = { -1, 0, 0, 0, 0, 0 };
50 static int gc_nargs_2 __initdata = 0;
51 module_param_array_named(map2, gc_2, int, &gc_nargs_2, 0);
52 MODULE_PARM_DESC(map2, "Describers second set of devices");
54 static int gc_3[] __initdata = { -1, 0, 0, 0, 0, 0 };
55 static int gc_nargs_3 __initdata = 0;
56 module_param_array_named(map3, gc_3, int, &gc_nargs_3, 0);
57 MODULE_PARM_DESC(map3, "Describers third set of devices");
59 __obsolete_setup("gc=");
60 __obsolete_setup("gc_2=");
61 __obsolete_setup("gc_3=");
63 /* see also gs_psx_delay parameter in PSX support section */
76 #define GC_REFRESH_TIME HZ/100
80 struct input_dev dev[5];
81 struct timer_list timer;
82 unsigned char pads[GC_MAX + 1];
87 static struct gc *gc_base[3];
89 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
91 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
92 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
93 "PSX DDR controller" };
98 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
99 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 };
101 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
102 #define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */
103 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
104 #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */
105 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
106 /* GC_N64_DWS > 24 is known to fail */
107 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
108 #define GC_N64_POWER_R 0xfd /* power during read */
109 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
110 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
111 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
113 #define GC_N64_CLOCK 0x02 /* clock bits for read */
116 * gc_n64_read_packet() reads an N64 packet.
117 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
120 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
126 * Request the pad to transmit data
129 local_irq_save(flags);
130 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
131 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
134 local_irq_restore(flags);
137 * Wait for the pad response to be loaded into the 33-bit register of the adapter
140 udelay(GC_N64_DELAY);
143 * Grab data (ignoring the last bit, which is a stop bit)
146 for (i = 0; i < GC_N64_LENGTH; i++) {
147 parport_write_data(gc->pd->port, GC_N64_POWER_R);
148 data[i] = parport_read_status(gc->pd->port);
149 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
153 * We must wait 200 ms here for the controller to reinitialize before the next read request.
154 * No worries as long as gc_read is polled less frequently than this.
163 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
164 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
165 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the last 4 bits are unused */
167 #define GC_NES_POWER 0xfc
168 #define GC_NES_CLOCK 0x01
169 #define GC_NES_LATCH 0x02
171 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
172 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
173 static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
176 * gc_nes_read_packet() reads a NES/SNES packet.
177 * Each pad uses one bit per byte. So all pads connected to
178 * this port are read in parallel.
181 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
185 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
186 udelay(GC_NES_DELAY * 2);
187 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
189 for (i = 0; i < length; i++) {
190 udelay(GC_NES_DELAY);
191 parport_write_data(gc->pd->port, GC_NES_POWER);
192 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
193 udelay(GC_NES_DELAY);
194 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
199 * Multisystem joystick support
202 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
203 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
206 * gc_multi_read_packet() reads a Multisystem joystick packet.
209 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
213 for (i = 0; i < length; i++) {
214 parport_write_data(gc->pd->port, ~(1 << i));
215 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
222 * See documentation at:
223 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
224 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
225 * ftp://milano.usal.es/pablo/
229 #define GC_PSX_DELAY 25 /* 25 usec */
230 #define GC_PSX_LENGTH 8 /* talk to the controller in bytes */
232 #define GC_PSX_MOUSE 1 /* Mouse */
233 #define GC_PSX_NEGCON 2 /* NegCon */
234 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
235 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
236 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
238 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
239 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
240 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
241 #define GC_PSX_SELECT 0x02 /* Pin 3 */
243 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
244 #define GC_PSX_LEN(x) ((x) & 0xf) /* Low nibble is length in words */
246 static int gc_psx_delay = GC_PSX_DELAY;
247 module_param_named(psx_delay, gc_psx_delay, uint, 0);
248 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
250 __obsolete_setup("gc_psx_delay=");
252 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
253 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
254 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
255 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
258 * gc_psx_command() writes 8bit command and reads 8bit data from
262 static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_PSX_LENGTH])
265 for (i = 0; i < 5; i++)
268 for (i = 0; i < 8; i++, b >>= 1) {
269 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
270 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
271 udelay(gc_psx_delay);
272 read = parport_read_status(gc->pd->port) ^ 0x80;
273 for (j = 0; j < 5; j++)
274 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
275 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
276 udelay(gc_psx_delay);
281 * gc_psx_read_packet() reads a whole psx packet and returns
282 * device identifier code.
285 static void gc_psx_read_packet(struct gc *gc, unsigned char data[5][GC_PSX_LENGTH], unsigned char id[5])
287 int i, j, max_len = 0;
289 unsigned char data2[5];
291 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
292 udelay(gc_psx_delay);
293 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
294 udelay(gc_psx_delay);
296 local_irq_save(flags);
298 gc_psx_command(gc, 0x01, data2); /* Access pad */
299 gc_psx_command(gc, 0x42, id); /* Get device ids */
300 gc_psx_command(gc, 0, data2); /* Dump status */
302 for (i =0; i < 5; i++) /* Find the longest pad */
303 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) && (GC_PSX_LEN(id[i]) > max_len))
304 max_len = GC_PSX_LEN(id[i]);
306 for (i = 0; i < max_len * 2; i++) { /* Read in all the data */
307 gc_psx_command(gc, 0, data2);
308 for (j = 0; j < 5; j++)
309 data[j][i] = data2[j];
312 local_irq_restore(flags);
314 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
316 for(i = 0; i < 5; i++) /* Set id's to the real value */
317 id[i] = GC_PSX_ID(id[i]);
321 * gc_timer() reads and analyzes console pads data.
324 #define GC_MAX_LENGTH GC_N64_LENGTH
326 static void gc_timer(unsigned long private)
328 struct gc *gc = (void *) private;
329 struct input_dev *dev = gc->dev;
330 unsigned char data[GC_MAX_LENGTH];
331 unsigned char data_psx[5][GC_PSX_LENGTH];
335 * N64 pads - must be read first, any read confuses them for 200 us
338 if (gc->pads[GC_N64]) {
340 gc_n64_read_packet(gc, data);
342 for (i = 0; i < 5; i++) {
344 s = gc_status_bit[i];
346 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
349 axes[0] = axes[1] = 0;
351 for (j = 0; j < 8; j++) {
352 if (data[23 - j] & s) axes[0] |= 1 << j;
353 if (data[31 - j] & s) axes[1] |= 1 << j;
356 input_report_abs(dev + i, ABS_X, axes[0]);
357 input_report_abs(dev + i, ABS_Y, -axes[1]);
359 input_report_abs(dev + i, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
360 input_report_abs(dev + i, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
362 for (j = 0; j < 10; j++)
363 input_report_key(dev + i, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
374 if (gc->pads[GC_NES] || gc->pads[GC_SNES]) {
376 gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
378 for (i = 0; i < 5; i++) {
380 s = gc_status_bit[i];
382 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
383 input_report_abs(dev + i, ABS_X, !(s & data[6]) - !(s & data[7]));
384 input_report_abs(dev + i, ABS_Y, !(s & data[4]) - !(s & data[5]));
387 if (s & gc->pads[GC_NES])
388 for (j = 0; j < 4; j++)
389 input_report_key(dev + i, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
391 if (s & gc->pads[GC_SNES])
392 for (j = 0; j < 8; j++)
393 input_report_key(dev + i, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
400 * Multi and Multi2 joysticks
403 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) {
405 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
407 for (i = 0; i < 5; i++) {
409 s = gc_status_bit[i];
411 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
412 input_report_abs(dev + i, ABS_X, !(s & data[2]) - !(s & data[3]));
413 input_report_abs(dev + i, ABS_Y, !(s & data[0]) - !(s & data[1]));
414 input_report_key(dev + i, BTN_TRIGGER, s & data[4]);
417 if (s & gc->pads[GC_MULTI2])
418 input_report_key(dev + i, BTN_THUMB, s & data[5]);
428 if (gc->pads[GC_PSX] || gc->pads[GC_DDR]) {
430 gc_psx_read_packet(gc, data_psx, data);
432 for (i = 0; i < 5; i++) {
437 input_report_key(dev + i, BTN_THUMBL, ~data_psx[i][0] & 0x04);
438 input_report_key(dev + i, BTN_THUMBR, ~data_psx[i][0] & 0x02);
443 if(gc->pads[GC_DDR] & gc_status_bit[i]) {
444 for(j = 0; j < 4; j++)
445 input_report_key(dev + i, gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
447 for (j = 0; j < 4; j++)
448 input_report_abs(dev + i, gc_psx_abs[j+2], data_psx[i][j + 2]);
450 input_report_abs(dev + i, ABS_X, 128 + !(data_psx[i][0] & 0x20) * 127 - !(data_psx[i][0] & 0x80) * 128);
451 input_report_abs(dev + i, ABS_Y, 128 + !(data_psx[i][0] & 0x40) * 127 - !(data_psx[i][0] & 0x10) * 128);
454 for (j = 0; j < 8; j++)
455 input_report_key(dev + i, gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
457 input_report_key(dev + i, BTN_START, ~data_psx[i][0] & 0x08);
458 input_report_key(dev + i, BTN_SELECT, ~data_psx[i][0] & 0x01);
465 if(gc->pads[GC_DDR] & gc_status_bit[i]) {
466 for(j = 0; j < 4; j++)
467 input_report_key(dev + i, gc_psx_ddr_btn[j], ~data_psx[i][0] & (0x10 << j));
469 input_report_abs(dev + i, ABS_X, 128 + !(data_psx[i][0] & 0x20) * 127 - !(data_psx[i][0] & 0x80) * 128);
470 input_report_abs(dev + i, ABS_Y, 128 + !(data_psx[i][0] & 0x40) * 127 - !(data_psx[i][0] & 0x10) * 128);
472 /* for some reason if the extra axes are left unset they drift */
473 /* for (j = 0; j < 4; j++)
474 input_report_abs(dev + i, gc_psx_abs[j+2], 128);
475 * This needs to be debugged properly,
476 * maybe fuzz processing needs to be done in input_sync()
481 for (j = 0; j < 8; j++)
482 input_report_key(dev + i, gc_psx_btn[j], ~data_psx[i][1] & (1 << j));
484 input_report_key(dev + i, BTN_START, ~data_psx[i][0] & 0x08);
485 input_report_key(dev + i, BTN_SELECT, ~data_psx[i][0] & 0x01);
491 case 0: /* not a pad, ignore */
497 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
500 static int gc_open(struct input_dev *dev)
502 struct gc *gc = dev->private;
504 parport_claim(gc->pd);
505 parport_write_control(gc->pd->port, 0x04);
506 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
511 static void gc_close(struct input_dev *dev)
513 struct gc *gc = dev->private;
515 del_timer(&gc->timer);
516 parport_write_control(gc->pd->port, 0x00);
517 parport_release(gc->pd);
521 static struct gc __init *gc_probe(int *config, int nargs)
531 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
535 pp = parport_find_number(config[0]);
538 printk(KERN_ERR "gamecon.c: no such parport\n");
542 if (!(gc = kmalloc(sizeof(struct gc), GFP_KERNEL))) {
543 parport_put_port(pp);
546 memset(gc, 0, sizeof(struct gc));
548 gc->pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
550 parport_put_port(pp);
553 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
558 parport_claim(gc->pd);
560 init_timer(&gc->timer);
561 gc->timer.data = (long) gc;
562 gc->timer.function = gc_timer;
564 for (i = 0; i < nargs - 1; i++) {
569 if (config[i + 1] < 1 || config[i + 1] > GC_MAX) {
570 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", config[i + 1]);
574 gc->dev[i].private = gc;
575 gc->dev[i].open = gc_open;
576 gc->dev[i].close = gc_close;
578 gc->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
580 for (j = 0; j < 2; j++) {
581 set_bit(ABS_X + j, gc->dev[i].absbit);
582 gc->dev[i].absmin[ABS_X + j] = -1;
583 gc->dev[i].absmax[ABS_X + j] = 1;
586 gc->pads[0] |= gc_status_bit[i];
587 gc->pads[config[i + 1]] |= gc_status_bit[i];
589 switch(config[i + 1]) {
592 for (j = 0; j < 10; j++)
593 set_bit(gc_n64_btn[j], gc->dev[i].keybit);
595 for (j = 0; j < 2; j++) {
596 set_bit(ABS_X + j, gc->dev[i].absbit);
597 gc->dev[i].absmin[ABS_X + j] = -127;
598 gc->dev[i].absmax[ABS_X + j] = 126;
599 gc->dev[i].absflat[ABS_X + j] = 2;
600 set_bit(ABS_HAT0X + j, gc->dev[i].absbit);
601 gc->dev[i].absmin[ABS_HAT0X + j] = -1;
602 gc->dev[i].absmax[ABS_HAT0X + j] = 1;
608 for (j = 4; j < 8; j++)
609 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
611 for (j = 0; j < 4; j++)
612 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
616 set_bit(BTN_THUMB, gc->dev[i].keybit);
618 set_bit(BTN_TRIGGER, gc->dev[i].keybit);
623 if(config[i + 1] == GC_DDR) {
624 for (j = 0; j < 4; j++)
625 set_bit(gc_psx_ddr_btn[j], gc->dev[i].keybit);
627 for (j = 0; j < 6; j++) {
628 set_bit(gc_psx_abs[j], gc->dev[i].absbit);
629 gc->dev[i].absmin[gc_psx_abs[j]] = 4;
630 gc->dev[i].absmax[gc_psx_abs[j]] = 252;
631 gc->dev[i].absflat[gc_psx_abs[j]] = 2;
635 for (j = 0; j < 12; j++)
636 set_bit(gc_psx_btn[j], gc->dev[i].keybit);
641 sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
643 gc->dev[i].name = gc_names[config[i + 1]];
644 gc->dev[i].phys = gc->phys[i];
645 gc->dev[i].id.bustype = BUS_PARPORT;
646 gc->dev[i].id.vendor = 0x0001;
647 gc->dev[i].id.product = config[i + 1];
648 gc->dev[i].id.version = 0x0100;
651 parport_release(gc->pd);
654 parport_unregister_device(gc->pd);
659 for (i = 0; i < 5; i++)
660 if (gc->pads[0] & gc_status_bit[i]) {
661 input_register_device(gc->dev + i);
662 printk(KERN_INFO "input: %s on %s\n", gc->dev[i].name, gc->pd->port->name);
668 int __init gc_init(void)
670 gc_base[0] = gc_probe(gc, gc_nargs);
671 gc_base[1] = gc_probe(gc_2, gc_nargs_2);
672 gc_base[2] = gc_probe(gc_3, gc_nargs_3);
674 if (gc_base[0] || gc_base[1] || gc_base[2])
680 void __exit gc_exit(void)
684 for (i = 0; i < 3; i++)
686 for (j = 0; j < 5; j++)
687 if (gc_base[i]->pads[0] & gc_status_bit[j])
688 input_unregister_device(gc_base[i]->dev + j);
689 parport_unregister_device(gc_base[i]->pd);
693 module_init(gc_init);
694 module_exit(gc_exit);