fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / drivers / input / joystick / gamecon.c
index 02ee05a..fe12aa3 100644 (file)
@@ -1,28 +1,26 @@
 /*
- * $Id: gamecon.c,v 1.22 2002/07/01 15:42:25 vojtech Exp $
+ * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
  *
- *  Copyright (c) 1999-2001 Vojtech Pavlik
+ *  Copyright (c) 1999-2004    Vojtech Pavlik <vojtech@suse.cz>
+ *  Copyright (c) 2004         Peter Nelson <rufus-kernel@hackish.org>
  *
  *  Based on the work of:
- *     Andree Borrmann         John Dahlstrom
- *     David Kuder             Nathan Hand
- */
-
-/*
- * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
+ *     Andree Borrmann         John Dahlstrom
+ *     David Kuder             Nathan Hand
+ *     Raphael Assenat
  */
 
 /*
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or 
+ * the Free Software Foundation; either version 2 of the License, or
  * (at your option) any later version.
- * 
+ *
  * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  * GNU General Public License for more details.
- * 
+ *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 #include <linux/init.h>
 #include <linux/parport.h>
 #include <linux/input.h>
+#include <linux/mutex.h>
 
 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
 MODULE_LICENSE("GPL");
 
-static int gc[] __initdata = { -1, 0, 0, 0, 0, 0 };
-static int gc_nargs __initdata = 0;
-module_param_array_named(map, gc, int, gc_nargs, 0);
-MODULE_PARM_DESC(map, "Describers first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
+#define GC_MAX_PORTS           3
+#define GC_MAX_DEVICES         5
 
-static int gc_2[] __initdata = { -1, 0, 0, 0, 0, 0 };
-static int gc_nargs_2 __initdata = 0;
-module_param_array_named(map2, gc_2, int, gc_nargs_2, 0);
-MODULE_PARM_DESC(map2, "Describers second set of devices");
+struct gc_config {
+       int args[GC_MAX_DEVICES + 1];
+       int nargs;
+};
+
+static struct gc_config gc[GC_MAX_PORTS] __initdata;
 
-static int gc_3[] __initdata = { -1, 0, 0, 0, 0, 0 };
-static int gc_nargs_3 __initdata = 0;
-module_param_array_named(map3, gc_3, int, gc_nargs_3, 0);
-MODULE_PARM_DESC(map3, "Describers third set of devices");
+module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0);
+MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
+module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0);
+MODULE_PARM_DESC(map2, "Describes second set of devices");
+module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0);
+MODULE_PARM_DESC(map3, "Describes third set of devices");
 
 __obsolete_setup("gc=");
 __obsolete_setup("gc_2=");
@@ -70,20 +71,23 @@ __obsolete_setup("gc_3=");
 #define GC_NES4                3
 #define GC_MULTI       4
 #define GC_MULTI2      5
-#define GC_N64         6       
+#define GC_N64         6
 #define GC_PSX         7
+#define GC_DDR         8
+#define GC_SNESMOUSE   9
 
-#define GC_MAX         7
+#define GC_MAX         9
 
 #define GC_REFRESH_TIME        HZ/100
+
 struct gc {
        struct pardevice *pd;
-       struct input_dev dev[5];
+       struct input_dev *dev[GC_MAX_DEVICES];
        struct timer_list timer;
        unsigned char pads[GC_MAX + 1];
        int used;
-       char phys[5][32];
+       struct mutex mutex;
+       char phys[GC_MAX_DEVICES][32];
 };
 
 static struct gc *gc_base[3];
@@ -91,7 +95,8 @@ static struct gc *gc_base[3];
 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
 
 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
-                               "Multisystem 2-button joystick", "N64 controller", "PSX controller" };
+                               "Multisystem 2-button joystick", "N64 controller", "PSX controller",
+                               "PSX DDR controller", "SNES mouse" };
 /*
  * N64 support.
  */
@@ -104,7 +109,7 @@ static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL,
 #define GC_N64_DELAY           133             /* delay between transmit request, and response ready (us) */
 #define GC_N64_REQUEST         0x1dd1111111ULL /* the request data command (encoded for 000000011) */
 #define GC_N64_DWS             3               /* delay between write segments (required for sound playback because of ISA DMA) */
-                                               /* GC_N64_DWS > 24 is known to fail */ 
+                                               /* GC_N64_DWS > 24 is known to fail */
 #define GC_N64_POWER_W         0xe2            /* power during write (transmit request) */
 #define GC_N64_POWER_R         0xfd            /* power during read */
 #define GC_N64_OUT             0x1d            /* output bits to the 4 pads */
@@ -113,8 +118,8 @@ static short gc_n64_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL,
                                                /* than 123 us */
 #define GC_N64_CLOCK           0x02            /* clock bits for read */
 
-/* 
- * gc_n64_read_packet() reads an N64 packet. 
+/*
+ * gc_n64_read_packet() reads an N64 packet.
  * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
  */
 
@@ -157,13 +162,58 @@ static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
 
 }
 
+static void gc_n64_process_packet(struct gc *gc)
+{
+       unsigned char data[GC_N64_LENGTH];
+       signed char axes[2];
+       struct input_dev *dev;
+       int i, j, s;
+
+       gc_n64_read_packet(gc, data);
+
+       for (i = 0; i < GC_MAX_DEVICES; i++) {
+
+               dev = gc->dev[i];
+               if (!dev)
+                       continue;
+
+               s = gc_status_bit[i];
+
+               if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
+
+                       axes[0] = axes[1] = 0;
+
+                       for (j = 0; j < 8; j++) {
+                               if (data[23 - j] & s)
+                                       axes[0] |= 1 << j;
+                               if (data[31 - j] & s)
+                                       axes[1] |= 1 << j;
+                       }
+
+                       input_report_abs(dev, ABS_X,  axes[0]);
+                       input_report_abs(dev, ABS_Y, -axes[1]);
+
+                       input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
+                       input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
+
+                       for (j = 0; j < 10; j++)
+                               input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
+
+                       input_sync(dev);
+               }
+       }
+}
+
 /*
  * NES/SNES support.
  */
 
-#define GC_NES_DELAY   6       /* Delay between bits - 6us */
-#define GC_NES_LENGTH  8       /* The NES pads use 8 bits of data */
-#define GC_SNES_LENGTH 12      /* The SNES true length is 16, but the last 4 bits are unused */
+#define GC_NES_DELAY           6       /* Delay between bits - 6us */
+#define GC_NES_LENGTH          8       /* The NES pads use 8 bits of data */
+#define GC_SNES_LENGTH         12      /* The SNES true length is 16, but the
+                                          last 4 bits are unused */
+#define GC_SNESMOUSE_LENGTH    32      /* The SNES mouse uses 32 bits, the first
+                                          16 bits are equivalent to a gamepad */
 
 #define GC_NES_POWER   0xfc
 #define GC_NES_CLOCK   0x01
@@ -196,6 +246,81 @@ static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
        }
 }
 
+static void gc_nes_process_packet(struct gc *gc)
+{
+       unsigned char data[GC_SNESMOUSE_LENGTH];
+       struct input_dev *dev;
+       int i, j, s, len;
+       char x_rel, y_rel;
+
+       len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
+                       (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
+
+       gc_nes_read_packet(gc, len, data);
+
+       for (i = 0; i < GC_MAX_DEVICES; i++) {
+
+               dev = gc->dev[i];
+               if (!dev)
+                       continue;
+
+               s = gc_status_bit[i];
+
+               if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
+                       input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
+                       input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
+               }
+
+               if (s & gc->pads[GC_NES])
+                       for (j = 0; j < 4; j++)
+                               input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
+
+               if (s & gc->pads[GC_SNES])
+                       for (j = 0; j < 8; j++)
+                               input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
+
+               if (s & gc->pads[GC_SNESMOUSE]) {
+                       /*
+                        * The 4 unused bits from SNES controllers appear to be ID bits
+                        * so use them to make sure iwe are dealing with a mouse.
+                        * gamepad is connected. This is important since
+                        * my SNES gamepad sends 1's for bits 16-31, which
+                        * cause the mouse pointer to quickly move to the
+                        * upper left corner of the screen.
+                        */
+                       if (!(s & data[12]) && !(s & data[13]) &&
+                           !(s & data[14]) && (s & data[15])) {
+                               input_report_key(dev, BTN_LEFT, s & data[9]);
+                               input_report_key(dev, BTN_RIGHT, s & data[8]);
+
+                               x_rel = y_rel = 0;
+                               for (j = 0; j < 7; j++) {
+                                       x_rel <<= 1;
+                                       if (data[25 + j] & s)
+                                               x_rel |= 1;
+
+                                       y_rel <<= 1;
+                                       if (data[17 + j] & s)
+                                               y_rel |= 1;
+                               }
+
+                               if (x_rel) {
+                                       if (data[24] & s)
+                                               x_rel = -x_rel;
+                                       input_report_rel(dev, REL_X, x_rel);
+                               }
+
+                               if (y_rel) {
+                                       if (data[16] & s)
+                                               y_rel = -y_rel;
+                                       input_report_rel(dev, REL_Y, y_rel);
+                               }
+                       }
+               }
+               input_sync(dev);
+       }
+}
+
 /*
  * Multisystem joystick support
  */
@@ -217,6 +342,35 @@ static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
        }
 }
 
+static void gc_multi_process_packet(struct gc *gc)
+{
+       unsigned char data[GC_MULTI2_LENGTH];
+       struct input_dev *dev;
+       int i, s;
+
+       gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
+
+       for (i = 0; i < GC_MAX_DEVICES; i++) {
+
+               dev = gc->dev[i];
+               if (!dev)
+                       continue;
+
+               s = gc_status_bit[i];
+
+               if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
+                       input_report_abs(dev, ABS_X,  !(s & data[2]) - !(s & data[3]));
+                       input_report_abs(dev, ABS_Y,  !(s & data[0]) - !(s & data[1]));
+                       input_report_key(dev, BTN_TRIGGER, s & data[4]);
+               }
+
+               if (s & gc->pads[GC_MULTI2])
+                       input_report_key(dev, BTN_THUMB, s & data[5]);
+
+               input_sync(dev);
+       }
+}
+
 /*
  * PSX support
  *
@@ -224,11 +378,12 @@ static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
  *     http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
  *     http://www.gamesx.com/controldata/psxcont/psxcont.htm
  *     ftp://milano.usal.es/pablo/
- *     
+ *
  */
 
 #define GC_PSX_DELAY   25              /* 25 usec */
-#define GC_PSX_LENGTH  8               /* talk to the controller in bytes */
+#define GC_PSX_LENGTH  8               /* talk to the controller in bits */
+#define GC_PSX_BYTES   6               /* the maximum number of bytes to read off the controller */
 
 #define GC_PSX_MOUSE   1               /* Mouse */
 #define GC_PSX_NEGCON  2               /* NegCon */
@@ -237,12 +392,12 @@ static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
 #define GC_PSX_RUMBLE  7               /* Rumble in Red mode */
 
 #define GC_PSX_CLOCK   0x04            /* Pin 4 */
-#define GC_PSX_COMMAND 0x01            /* Pin 1 */
+#define GC_PSX_COMMAND 0x01            /* Pin 2 */
 #define GC_PSX_POWER   0xf8            /* Pins 5-9 */
 #define GC_PSX_SELECT  0x02            /* Pin 3 */
 
 #define GC_PSX_ID(x)   ((x) >> 4)      /* High nibble is device type */
-#define GC_PSX_LEN(x)  ((x) & 0xf)     /* Low nibble is length in words */
+#define GC_PSX_LEN(x)  (((x) & 0xf) << 1)      /* Low nibble is length in bytes/2 */
 
 static int gc_psx_delay = GC_PSX_DELAY;
 module_param_named(psx_delay, gc_psx_delay, uint, 0);
@@ -253,25 +408,30 @@ __obsolete_setup("gc_psx_delay=");
 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
                                BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
+static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
 
 /*
  * gc_psx_command() writes 8bit command and reads 8bit data from
  * the psx pad.
  */
 
-static int gc_psx_command(struct gc *gc, int b)
+static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
 {
-       int i, cmd, data = 0;
+       int i, j, cmd, read;
+
+       for (i = 0; i < GC_MAX_DEVICES; i++)
+               data[i] = 0;
 
-       for (i = 0; i < 8; i++, b >>= 1) {
+       for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
                cmd = (b & 1) ? GC_PSX_COMMAND : 0;
                parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
                udelay(gc_psx_delay);
-               data |= ((parport_read_status(gc->pd->port) ^ 0x80) & gc->pads[GC_PSX]) ? (1 << i) : 0;
+               read = parport_read_status(gc->pd->port) ^ 0x80;
+               for (j = 0; j < GC_MAX_DEVICES; j++)
+                       data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
                parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
                udelay(gc_psx_delay);
        }
-       return data;
 }
 
 /*
@@ -279,188 +439,158 @@ static int gc_psx_command(struct gc *gc, int b)
  * device identifier code.
  */
 
-static int gc_psx_read_packet(struct gc *gc, unsigned char *data)
+static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
+                              unsigned char id[GC_MAX_DEVICES])
 {
-       int i, id;
+       int i, j, max_len = 0;
        unsigned long flags;
+       unsigned char data2[GC_MAX_DEVICES];
 
        parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);  /* Select pad */
-       udelay(gc_psx_delay * 2);
+       udelay(gc_psx_delay);
        parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);                  /* Deselect, begin command */
-       udelay(gc_psx_delay * 2);
+       udelay(gc_psx_delay);
 
        local_irq_save(flags);
 
-       gc_psx_command(gc, 0x01);                                                       /* Access pad */
-       id = gc_psx_command(gc, 0x42);                                                  /* Get device id */
-       if (gc_psx_command(gc, 0) == 0x5a) {                                            /* Okay? */
-               for (i = 0; i < GC_PSX_LEN(id) * 2; i++)
-                       data[i] = gc_psx_command(gc, 0);
-       } else id = 0;
+       gc_psx_command(gc, 0x01, data2);                                                /* Access pad */
+       gc_psx_command(gc, 0x42, id);                                                   /* Get device ids */
+       gc_psx_command(gc, 0, data2);                                                   /* Dump status */
+
+       for (i =0; i < GC_MAX_DEVICES; i++)                                                             /* Find the longest pad */
+               if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
+                       && (GC_PSX_LEN(id[i]) > max_len)
+                       && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
+                       max_len = GC_PSX_LEN(id[i]);
+
+       for (i = 0; i < max_len; i++) {                                         /* Read in all the data */
+               gc_psx_command(gc, 0, data2);
+               for (j = 0; j < GC_MAX_DEVICES; j++)
+                       data[j][i] = data2[j];
+       }
 
        local_irq_restore(flags);
 
        parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
 
-       return GC_PSX_ID(id);
+       for(i = 0; i < GC_MAX_DEVICES; i++)                                                             /* Set id's to the real value */
+               id[i] = GC_PSX_ID(id[i]);
 }
 
-/*
- * gc_timer() reads and analyzes console pads data.
- */
-
-#define GC_MAX_LENGTH GC_N64_LENGTH
-
-static void gc_timer(unsigned long private)
+static void gc_psx_process_packet(struct gc *gc)
 {
-       struct gc *gc = (void *) private;
-       struct input_dev *dev = gc->dev;
-       unsigned char data[GC_MAX_LENGTH];
-       int i, j, s;
-
-/*
- * N64 pads - must be read first, any read confuses them for 200 us
- */
+       unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
+       unsigned char id[GC_MAX_DEVICES];
+       struct input_dev *dev;
+       int i, j;
 
-       if (gc->pads[GC_N64]) {
+       gc_psx_read_packet(gc, data, id);
 
-               gc_n64_read_packet(gc, data);
+       for (i = 0; i < GC_MAX_DEVICES; i++) {
 
-               for (i = 0; i < 5; i++) {
+               dev = gc->dev[i];
+               if (!dev)
+                       continue;
 
-                       s = gc_status_bit[i];
+               switch (id[i]) {
 
-                       if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
-       
-                               signed char axes[2];
-                               axes[0] = axes[1] = 0;
+                       case GC_PSX_RUMBLE:
 
-                               for (j = 0; j < 8; j++) {
-                                       if (data[23 - j] & s) axes[0] |= 1 << j; 
-                                       if (data[31 - j] & s) axes[1] |= 1 << j; 
-                               }
+                               input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
+                               input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
 
-                               input_report_abs(dev + i, ABS_X,  axes[0]);
-                               input_report_abs(dev + i, ABS_Y, -axes[1]);
+                       case GC_PSX_NEGCON:
+                       case GC_PSX_ANALOG:
 
-                               input_report_abs(dev + i, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
-                               input_report_abs(dev + i, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
+                               if (gc->pads[GC_DDR] & gc_status_bit[i]) {
+                                       for(j = 0; j < 4; j++)
+                                               input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
+                               } else {
+                                       for (j = 0; j < 4; j++)
+                                               input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
 
-                               for (j = 0; j < 10; j++)
-                                       input_report_key(dev + i, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
+                                       input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
+                                       input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
+                               }
 
-                               input_sync(dev + i);
-                       }
-               }
-       }
+                               for (j = 0; j < 8; j++)
+                                       input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
 
-/*
- * NES and SNES pads
- */
+                               input_report_key(dev, BTN_START,  ~data[i][0] & 0x08);
+                               input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
 
-       if (gc->pads[GC_NES] || gc->pads[GC_SNES]) {
+                               input_sync(dev);
 
-               gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
+                               break;
 
-               for (i = 0; i < 5; i++) {
+                       case GC_PSX_NORMAL:
+                               if (gc->pads[GC_DDR] & gc_status_bit[i]) {
+                                       for(j = 0; j < 4; j++)
+                                               input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
+                               } else {
+                                       input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
+                                       input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
+
+                                       /* for some reason if the extra axes are left unset they drift */
+                                       /* for (j = 0; j < 4; j++)
+                                               input_report_abs(dev, gc_psx_abs[j + 2], 128);
+                                        * This needs to be debugged properly,
+                                        * maybe fuzz processing needs to be done in input_sync()
+                                        *                               --vojtech
+                                        */
+                               }
 
-                       s = gc_status_bit[i];
+                               for (j = 0; j < 8; j++)
+                                       input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
 
-                       if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
-                               input_report_abs(dev + i, ABS_X, !(s & data[6]) - !(s & data[7]));
-                               input_report_abs(dev + i, ABS_Y, !(s & data[4]) - !(s & data[5]));
-                       }
+                               input_report_key(dev, BTN_START,  ~data[i][0] & 0x08);
+                               input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
 
-                       if (s & gc->pads[GC_NES])
-                               for (j = 0; j < 4; j++)
-                                       input_report_key(dev + i, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
+                               input_sync(dev);
 
-                       if (s & gc->pads[GC_SNES])
-                               for (j = 0; j < 8; j++)
-                                       input_report_key(dev + i, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
+                               break;
 
-                       input_sync(dev + i);
+                       case 0: /* not a pad, ignore */
+                               break;
                }
        }
+}
 
 /*
- * Multi and Multi2 joysticks
+ * gc_timer() initiates reads of console pads data.
  */
 
-       if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) {
+static void gc_timer(unsigned long private)
+{
+       struct gc *gc = (void *) private;
 
-               gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
+/*
+ * N64 pads - must be read first, any read confuses them for 200 us
+ */
 
-               for (i = 0; i < 5; i++) {
+       if (gc->pads[GC_N64])
+               gc_n64_process_packet(gc);
 
-                       s = gc_status_bit[i];
+/*
+ * NES and SNES pads or mouse
+ */
 
-                       if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
-                               input_report_abs(dev + i, ABS_X,  !(s & data[2]) - !(s & data[3]));
-                               input_report_abs(dev + i, ABS_Y,  !(s & data[0]) - !(s & data[1]));
-                               input_report_key(dev + i, BTN_TRIGGER, s & data[4]);
-                       }
+       if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE])
+               gc_nes_process_packet(gc);
 
-                       if (s & gc->pads[GC_MULTI2])
-                               input_report_key(dev + i, BTN_THUMB, s & data[5]);
+/*
+ * Multi and Multi2 joysticks
+ */
 
-                       input_sync(dev + i);
-               }
-       }
+       if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
+               gc_multi_process_packet(gc);
 
 /*
  * PSX controllers
  */
 
-       if (gc->pads[GC_PSX]) {
-
-               for (i = 0; i < 5; i++)
-                       if (gc->pads[GC_PSX] & gc_status_bit[i])
-                               break;
-
-               switch (gc_psx_read_packet(gc, data)) {
-
-                       case GC_PSX_RUMBLE:
-
-                               input_report_key(dev + i, BTN_THUMBL, ~data[0] & 0x04);
-                               input_report_key(dev + i, BTN_THUMBR, ~data[0] & 0x02);
-                               input_sync(dev + i);
-
-                       case GC_PSX_NEGCON:
-                       case GC_PSX_ANALOG:
-
-                               for (j = 0; j < 4; j++)
-                                       input_report_abs(dev + i, gc_psx_abs[j], data[j + 2]);
-
-                               input_report_abs(dev + i, ABS_HAT0X, !(data[0] & 0x20) - !(data[0] & 0x80));
-                               input_report_abs(dev + i, ABS_HAT0Y, !(data[0] & 0x40) - !(data[0] & 0x10));
-
-                               for (j = 0; j < 8; j++)
-                                       input_report_key(dev + i, gc_psx_btn[j], ~data[1] & (1 << j));
-
-                               input_report_key(dev + i, BTN_START,  ~data[0] & 0x08);
-                               input_report_key(dev + i, BTN_SELECT, ~data[0] & 0x01);
-
-                               input_sync(dev + i);
-
-                               break;
-
-                       case GC_PSX_NORMAL:
-
-                               input_report_abs(dev + i, ABS_X, 128 + !(data[0] & 0x20) * 127 - !(data[0] & 0x80) * 128);
-                               input_report_abs(dev + i, ABS_Y, 128 + !(data[0] & 0x40) * 127 - !(data[0] & 0x10) * 128);
-
-                               for (j = 0; j < 8; j++)
-                                       input_report_key(dev + i, gc_psx_btn[j], ~data[1] & (1 << j));
-
-                               input_report_key(dev + i, BTN_START,  ~data[0] & 0x08);
-                               input_report_key(dev + i, BTN_SELECT, ~data[0] & 0x01);
-
-                               input_sync(dev + i);
-
-                               break;
-               }
-       }
+       if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
+               gc_psx_process_packet(gc);
 
        mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
 }
@@ -468,216 +598,258 @@ static void gc_timer(unsigned long private)
 static int gc_open(struct input_dev *dev)
 {
        struct gc *gc = dev->private;
+       int err;
+
+       err = mutex_lock_interruptible(&gc->mutex);
+       if (err)
+               return err;
+
        if (!gc->used++) {
                parport_claim(gc->pd);
                parport_write_control(gc->pd->port, 0x04);
                mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
        }
+
+       mutex_unlock(&gc->mutex);
        return 0;
 }
 
 static void gc_close(struct input_dev *dev)
 {
        struct gc *gc = dev->private;
+
+       mutex_lock(&gc->mutex);
        if (!--gc->used) {
-               del_timer(&gc->timer);
+               del_timer_sync(&gc->timer);
                parport_write_control(gc->pd->port, 0x00);
                parport_release(gc->pd);
        }
+       mutex_unlock(&gc->mutex);
 }
 
-static struct gc __init *gc_probe(int *config, int nargs)
+static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
 {
-       struct gc *gc;
-       struct parport *pp;
-       int i, j, psx;
-       unsigned char data[32];
+       struct input_dev *input_dev;
+       int i;
 
-       if (config[0] < 0)
-               return NULL;
+       if (!pad_type)
+               return 0;
 
-       if (nargs < 2) {
-               printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
-               return NULL;
+       if (pad_type < 1 || pad_type > GC_MAX) {
+               printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
+               return -EINVAL;
        }
 
-       pp = parport_find_number(config[0]);
-
-       if (!pp) {
-               printk(KERN_ERR "gamecon.c: no such parport\n");
-               return NULL;
+       gc->dev[idx] = input_dev = input_allocate_device();
+       if (!input_dev) {
+               printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
+               return -ENOMEM;
        }
 
-       if (!(gc = kmalloc(sizeof(struct gc), GFP_KERNEL))) {
-               parport_put_port(pp);
-               return NULL;
+       input_dev->name = gc_names[pad_type];
+       input_dev->phys = gc->phys[idx];
+       input_dev->id.bustype = BUS_PARPORT;
+       input_dev->id.vendor = 0x0001;
+       input_dev->id.product = pad_type;
+       input_dev->id.version = 0x0100;
+       input_dev->private = gc;
+
+       input_dev->open = gc_open;
+       input_dev->close = gc_close;
+
+       if (pad_type != GC_SNESMOUSE) {
+               input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
+
+               for (i = 0; i < 2; i++)
+                       input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
+       } else
+               input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
+
+       gc->pads[0] |= gc_status_bit[idx];
+       gc->pads[pad_type] |= gc_status_bit[idx];
+
+       switch (pad_type) {
+
+               case GC_N64:
+                       for (i = 0; i < 10; i++)
+                               set_bit(gc_n64_btn[i], input_dev->keybit);
+
+                       for (i = 0; i < 2; i++) {
+                               input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
+                               input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
+                       }
+
+                       break;
+
+               case GC_SNESMOUSE:
+                       set_bit(BTN_LEFT, input_dev->keybit);
+                       set_bit(BTN_RIGHT, input_dev->keybit);
+                       set_bit(REL_X, input_dev->relbit);
+                       set_bit(REL_Y, input_dev->relbit);
+                       break;
+
+               case GC_SNES:
+                       for (i = 4; i < 8; i++)
+                               set_bit(gc_snes_btn[i], input_dev->keybit);
+               case GC_NES:
+                       for (i = 0; i < 4; i++)
+                               set_bit(gc_snes_btn[i], input_dev->keybit);
+                       break;
+
+               case GC_MULTI2:
+                       set_bit(BTN_THUMB, input_dev->keybit);
+               case GC_MULTI:
+                       set_bit(BTN_TRIGGER, input_dev->keybit);
+                       break;
+
+               case GC_PSX:
+                       for (i = 0; i < 6; i++)
+                               input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
+                       for (i = 0; i < 12; i++)
+                               set_bit(gc_psx_btn[i], input_dev->keybit);
+
+                       break;
+
+               case GC_DDR:
+                       for (i = 0; i < 4; i++)
+                               set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
+                       for (i = 0; i < 12; i++)
+                               set_bit(gc_psx_btn[i], input_dev->keybit);
+
+                       break;
        }
-       memset(gc, 0, sizeof(struct gc));
 
-       gc->pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
+       return 0;
+}
 
-       parport_put_port(pp);
+static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
+{
+       struct gc *gc;
+       struct parport *pp;
+       struct pardevice *pd;
+       int i;
+       int err;
+
+       pp = parport_find_number(parport);
+       if (!pp) {
+               printk(KERN_ERR "gamecon.c: no such parport\n");
+               err = -EINVAL;
+               goto err_out;
+       }
 
-       if (!gc->pd) {
+       pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
+       if (!pd) {
                printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
-               kfree(gc);
-               return NULL;
+               err = -EBUSY;
+               goto err_put_pp;
        }
 
-       parport_claim(gc->pd);
+       gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
+       if (!gc) {
+               printk(KERN_ERR "gamecon.c: Not enough memory\n");
+               err = -ENOMEM;
+               goto err_unreg_pardev;
+       }
 
+       mutex_init(&gc->mutex);
+       gc->pd = pd;
        init_timer(&gc->timer);
        gc->timer.data = (long) gc;
        gc->timer.function = gc_timer;
 
-       for (i = 0; i < nargs - 1; i++) {
-
-               if (!config[i + 1])
+       for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
+               if (!pads[i])
                        continue;
 
-               if (config[i + 1] < 1 || config[i + 1] > GC_MAX) {
-                       printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", config[i + 1]);
-                       continue;
-               }
+               snprintf(gc->phys[i], sizeof(gc->phys[i]),
+                        "%s/input%d", gc->pd->port->name, i);
+               err = gc_setup_pad(gc, i, pads[i]);
+               if (err)
+                       goto err_unreg_devs;
 
-                gc->dev[i].private = gc;
-                gc->dev[i].open = gc_open;
-                gc->dev[i].close = gc_close;
-
-                gc->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
+               err = input_register_device(gc->dev[i]);
+               if (err)
+                       goto err_free_dev;
+       }
 
-               for (j = 0; j < 2; j++) {
-                       set_bit(ABS_X + j, gc->dev[i].absbit);
-                       gc->dev[i].absmin[ABS_X + j] = -1;
-                       gc->dev[i].absmax[ABS_X + j] =  1;
-               }
+       if (!gc->pads[0]) {
+               printk(KERN_ERR "gamecon.c: No valid devices specified\n");
+               err = -EINVAL;
+               goto err_free_gc;
+       }
 
-               gc->pads[0] |= gc_status_bit[i];
-               gc->pads[config[i + 1]] |= gc_status_bit[i];
+       parport_put_port(pp);
+       return gc;
 
-               switch(config[i + 1]) {
+ err_free_dev:
+       input_free_device(gc->dev[i]);
+ err_unreg_devs:
+       while (--i >= 0)
+               if (gc->dev[i])
+                       input_unregister_device(gc->dev[i]);
+ err_free_gc:
+       kfree(gc);
+ err_unreg_pardev:
+       parport_unregister_device(pd);
+ err_put_pp:
+       parport_put_port(pp);
+ err_out:
+       return ERR_PTR(err);
+}
 
-                       case GC_N64:
-                               for (j = 0; j < 10; j++)
-                                       set_bit(gc_n64_btn[j], gc->dev[i].keybit);
+static void gc_remove(struct gc *gc)
+{
+       int i;
 
-                               for (j = 0; j < 2; j++) {
-                                       set_bit(ABS_X + j, gc->dev[i].absbit);
-                                       gc->dev[i].absmin[ABS_X + j] = -127;
-                                       gc->dev[i].absmax[ABS_X + j] =  126;
-                                       gc->dev[i].absflat[ABS_X + j] = 2;
-                                       set_bit(ABS_HAT0X + j, gc->dev[i].absbit);
-                                       gc->dev[i].absmin[ABS_HAT0X + j] = -1;
-                                       gc->dev[i].absmax[ABS_HAT0X + j] =  1;
-                               }
+       for (i = 0; i < GC_MAX_DEVICES; i++)
+               if (gc->dev[i])
+                       input_unregister_device(gc->dev[i]);
+       parport_unregister_device(gc->pd);
+       kfree(gc);
+}
 
-                               break;
+static int __init gc_init(void)
+{
+       int i;
+       int have_dev = 0;
+       int err = 0;
 
-                       case GC_SNES:
-                               for (j = 4; j < 8; j++)
-                                       set_bit(gc_snes_btn[j], gc->dev[i].keybit);
-                       case GC_NES:
-                               for (j = 0; j < 4; j++)
-                                       set_bit(gc_snes_btn[j], gc->dev[i].keybit);
-                               break;
+       for (i = 0; i < GC_MAX_PORTS; i++) {
+               if (gc[i].nargs == 0 || gc[i].args[0] < 0)
+                       continue;
 
-                       case GC_MULTI2:
-                               set_bit(BTN_THUMB, gc->dev[i].keybit);
-                       case GC_MULTI:
-                               set_bit(BTN_TRIGGER, gc->dev[i].keybit);
-                               break;
+               if (gc[i].nargs < 2) {
+                       printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
+                       err = -EINVAL;
+                       break;
+               }
 
-                       case GC_PSX:
-                               
-                               psx = gc_psx_read_packet(gc, data);
-
-                               switch(psx) {
-                                       case GC_PSX_NEGCON:
-                                       case GC_PSX_NORMAL:
-                                       case GC_PSX_ANALOG:
-                                       case GC_PSX_RUMBLE:
-
-                                               for (j = 0; j < 6; j++) {
-                                                       psx = gc_psx_abs[j];
-                                                       set_bit(psx, gc->dev[i].absbit);
-                                                       if (j < 4) {
-                                                               gc->dev[i].absmin[psx] = 4;
-                                                               gc->dev[i].absmax[psx] = 252;
-                                                               gc->dev[i].absflat[psx] = 2;
-                                                       } else {
-                                                               gc->dev[i].absmin[psx] = -1;
-                                                               gc->dev[i].absmax[psx] = 1;
-                                                       }
-                                               }
-
-                                               for (j = 0; j < 12; j++)
-                                                       set_bit(gc_psx_btn[j], gc->dev[i].keybit);
-
-                                               break;
-
-                                       case 0:
-                                               gc->pads[GC_PSX] &= ~gc_status_bit[i];
-                                               printk(KERN_ERR "gamecon.c: No PSX controller found.\n");
-                                               break;
-
-                                       default:
-                                               gc->pads[GC_PSX] &= ~gc_status_bit[i];
-                                               printk(KERN_WARNING "gamecon.c: Unsupported PSX controller %#x,"
-                                                       " please report to <vojtech@ucw.cz>.\n", psx);
-                               }
-                               break;
+               gc_base[i] = gc_probe(gc[i].args[0], gc[i].args + 1, gc[i].nargs - 1);
+               if (IS_ERR(gc_base[i])) {
+                       err = PTR_ERR(gc_base[i]);
+                       break;
                }
 
-               sprintf(gc->phys[i], "%s/input%d", gc->pd->port->name, i);
-               
-                gc->dev[i].name = gc_names[config[i + 1]];
-               gc->dev[i].phys = gc->phys[i];
-                gc->dev[i].id.bustype = BUS_PARPORT;
-                gc->dev[i].id.vendor = 0x0001;
-                gc->dev[i].id.product = config[i + 1];
-                gc->dev[i].id.version = 0x0100;
+               have_dev = 1;
        }
 
-       parport_release(gc->pd);
-
-       if (!gc->pads[0]) {
-               parport_unregister_device(gc->pd);
-               kfree(gc);
-               return NULL;
+       if (err) {
+               while (--i >= 0)
+                       if (gc_base[i])
+                               gc_remove(gc_base[i]);
+               return err;
        }
 
-       for (i = 0; i < 5; i++) 
-               if (gc->pads[0] & gc_status_bit[i]) {
-                       input_register_device(gc->dev + i);
-                       printk(KERN_INFO "input: %s on %s\n", gc->dev[i].name, gc->pd->port->name);
-               }
-
-       return gc;
+       return have_dev ? 0 : -ENODEV;
 }
 
-int __init gc_init(void)
+static void __exit gc_exit(void)
 {
-       gc_base[0] = gc_probe(gc, gc_nargs);
-       gc_base[1] = gc_probe(gc_2, gc_nargs_2);
-       gc_base[2] = gc_probe(gc_3, gc_nargs_3);
-
-       if (gc_base[0] || gc_base[1] || gc_base[2])
-               return 0;
-
-       return -ENODEV;
-}
-
-void __exit gc_exit(void)
-{
-       int i, j;
+       int i;
 
-       for (i = 0; i < 3; i++)
-               if (gc_base[i]) {
-                       for (j = 0; j < 5; j++)
-                               if (gc_base[i]->pads[0] & gc_status_bit[j])
-                                       input_unregister_device(gc_base[i]->dev + j); 
-                       parport_unregister_device(gc_base[i]->pd);
-               }
+       for (i = 0; i < GC_MAX_PORTS; i++)
+               if (gc_base[i])
+                       gc_remove(gc_base[i]);
 }
 
 module_init(gc_init);