linux/drivers/input/joystick/gamecon.c
<<
>>
Prefs
   1/*
   2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
   3 *
   4 *  Copyright (c) 1999-2004     Vojtech Pavlik <vojtech@suse.cz>
   5 *  Copyright (c) 2004          Peter Nelson <rufus-kernel@hackish.org>
   6 *
   7 *  Based on the work of:
   8 *      Andree Borrmann         John Dahlstrom
   9 *      David Kuder             Nathan Hand
  10 *      Raphael Assenat
  11 */
  12
  13/*
  14 * This program is free software; you can redistribute it and/or modify
  15 * it under the terms of the GNU General Public License as published by
  16 * the Free Software Foundation; either version 2 of the License, or
  17 * (at your option) any later version.
  18 *
  19 * This program is distributed in the hope that it will be useful,
  20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22 * GNU General Public License for more details.
  23 *
  24 * You should have received a copy of the GNU General Public License
  25 * along with this program; if not, write to the Free Software
  26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  27 */
  28
  29#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  30
  31#include <linux/kernel.h>
  32#include <linux/delay.h>
  33#include <linux/module.h>
  34#include <linux/init.h>
  35#include <linux/parport.h>
  36#include <linux/input.h>
  37#include <linux/mutex.h>
  38#include <linux/slab.h>
  39
  40MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
  41MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
  42MODULE_LICENSE("GPL");
  43
  44#define GC_MAX_PORTS            3
  45#define GC_MAX_DEVICES          5
  46
  47struct gc_config {
  48        int args[GC_MAX_DEVICES + 1];
  49        unsigned int nargs;
  50};
  51
  52static struct gc_config gc_cfg[GC_MAX_PORTS];
  53
  54module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
  55MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
  56module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
  57MODULE_PARM_DESC(map2, "Describes second set of devices");
  58module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
  59MODULE_PARM_DESC(map3, "Describes third set of devices");
  60
  61/* see also gs_psx_delay parameter in PSX support section */
  62
  63enum gc_type {
  64        GC_NONE = 0,
  65        GC_SNES,
  66        GC_NES,
  67        GC_NES4,
  68        GC_MULTI,
  69        GC_MULTI2,
  70        GC_N64,
  71        GC_PSX,
  72        GC_DDR,
  73        GC_SNESMOUSE,
  74        GC_MAX
  75};
  76
  77#define GC_REFRESH_TIME HZ/100
  78
  79struct gc_pad {
  80        struct input_dev *dev;
  81        enum gc_type type;
  82        char phys[32];
  83};
  84
  85struct gc {
  86        struct pardevice *pd;
  87        struct gc_pad pads[GC_MAX_DEVICES];
  88        struct timer_list timer;
  89        int pad_count[GC_MAX];
  90        int used;
  91        int parportno;
  92        struct mutex mutex;
  93};
  94
  95struct gc_subdev {
  96        unsigned int idx;
  97};
  98
  99static struct gc *gc_base[3];
 100
 101static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
 102
 103static const char *gc_names[] = {
 104        NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
 105        "Multisystem 2-button joystick", "N64 controller", "PSX controller",
 106        "PSX DDR controller", "SNES mouse"
 107};
 108
 109/*
 110 * N64 support.
 111 */
 112
 113static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
 114static const short gc_n64_btn[] = {
 115        BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
 116        BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
 117};
 118
 119#define GC_N64_LENGTH           32              /* N64 bit length, not including stop bit */
 120#define GC_N64_STOP_LENGTH      5               /* Length of encoded stop bit */
 121#define GC_N64_CMD_00           0x11111111UL
 122#define GC_N64_CMD_01           0xd1111111UL
 123#define GC_N64_CMD_03           0xdd111111UL
 124#define GC_N64_CMD_1b           0xdd1dd111UL
 125#define GC_N64_CMD_c0           0x111111ddUL
 126#define GC_N64_CMD_80           0x1111111dUL
 127#define GC_N64_STOP_BIT         0x1d            /* Encoded stop bit */
 128#define GC_N64_REQUEST_DATA     GC_N64_CMD_01   /* the request data command */
 129#define GC_N64_DELAY            133             /* delay between transmit request, and response ready (us) */
 130#define GC_N64_DWS              3               /* delay between write segments (required for sound playback because of ISA DMA) */
 131                                                /* GC_N64_DWS > 24 is known to fail */
 132#define GC_N64_POWER_W          0xe2            /* power during write (transmit request) */
 133#define GC_N64_POWER_R          0xfd            /* power during read */
 134#define GC_N64_OUT              0x1d            /* output bits to the 4 pads */
 135                                                /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
 136                                                /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
 137                                                /* than 123 us */
 138#define GC_N64_CLOCK            0x02            /* clock bits for read */
 139
 140/*
 141 * Used for rumble code.
 142 */
 143
 144/* Send encoded command */
 145static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
 146                                unsigned char target)
 147{
 148        struct parport *port = gc->pd->port;
 149        int i;
 150
 151        for (i = 0; i < GC_N64_LENGTH; i++) {
 152                unsigned char data = (cmd >> i) & 1 ? target : 0;
 153                parport_write_data(port, GC_N64_POWER_W | data);
 154                udelay(GC_N64_DWS);
 155        }
 156}
 157
 158/* Send stop bit */
 159static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
 160{
 161        struct parport *port = gc->pd->port;
 162        int i;
 163
 164        for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
 165                unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
 166                parport_write_data(port, GC_N64_POWER_W | data);
 167                udelay(GC_N64_DWS);
 168        }
 169}
 170
 171/*
 172 * gc_n64_read_packet() reads an N64 packet.
 173 * Each pad uses one bit per byte. So all pads connected to this port
 174 * are read in parallel.
 175 */
 176
 177static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
 178{
 179        int i;
 180        unsigned long flags;
 181
 182/*
 183 * Request the pad to transmit data
 184 */
 185
 186        local_irq_save(flags);
 187        gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
 188        gc_n64_send_stop_bit(gc, GC_N64_OUT);
 189        local_irq_restore(flags);
 190
 191/*
 192 * Wait for the pad response to be loaded into the 33-bit register
 193 * of the adapter.
 194 */
 195
 196        udelay(GC_N64_DELAY);
 197
 198/*
 199 * Grab data (ignoring the last bit, which is a stop bit)
 200 */
 201
 202        for (i = 0; i < GC_N64_LENGTH; i++) {
 203                parport_write_data(gc->pd->port, GC_N64_POWER_R);
 204                udelay(2);
 205                data[i] = parport_read_status(gc->pd->port);
 206                parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
 207         }
 208
 209/*
 210 * We must wait 200 ms here for the controller to reinitialize before
 211 * the next read request. No worries as long as gc_read is polled less
 212 * frequently than this.
 213 */
 214
 215}
 216
 217static void gc_n64_process_packet(struct gc *gc)
 218{
 219        unsigned char data[GC_N64_LENGTH];
 220        struct input_dev *dev;
 221        int i, j, s;
 222        signed char x, y;
 223
 224        gc_n64_read_packet(gc, data);
 225
 226        for (i = 0; i < GC_MAX_DEVICES; i++) {
 227
 228                if (gc->pads[i].type != GC_N64)
 229                        continue;
 230
 231                dev = gc->pads[i].dev;
 232                s = gc_status_bit[i];
 233
 234                if (s & ~(data[8] | data[9])) {
 235
 236                        x = y = 0;
 237
 238                        for (j = 0; j < 8; j++) {
 239                                if (data[23 - j] & s)
 240                                        x |= 1 << j;
 241                                if (data[31 - j] & s)
 242                                        y |= 1 << j;
 243                        }
 244
 245                        input_report_abs(dev, ABS_X,  x);
 246                        input_report_abs(dev, ABS_Y, -y);
 247
 248                        input_report_abs(dev, ABS_HAT0X,
 249                                         !(s & data[6]) - !(s & data[7]));
 250                        input_report_abs(dev, ABS_HAT0Y,
 251                                         !(s & data[4]) - !(s & data[5]));
 252
 253                        for (j = 0; j < 10; j++)
 254                                input_report_key(dev, gc_n64_btn[j],
 255                                                 s & data[gc_n64_bytes[j]]);
 256
 257                        input_sync(dev);
 258                }
 259        }
 260}
 261
 262static int gc_n64_play_effect(struct input_dev *dev, void *data,
 263                              struct ff_effect *effect)
 264{
 265        int i;
 266        unsigned long flags;
 267        struct gc *gc = input_get_drvdata(dev);
 268        struct gc_subdev *sdev = data;
 269        unsigned char target = 1 << sdev->idx; /* select desired pin */
 270
 271        if (effect->type == FF_RUMBLE) {
 272                struct ff_rumble_effect *rumble = &effect->u.rumble;
 273                unsigned int cmd =
 274                        rumble->strong_magnitude || rumble->weak_magnitude ?
 275                        GC_N64_CMD_01 : GC_N64_CMD_00;
 276
 277                local_irq_save(flags);
 278
 279                /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
 280                gc_n64_send_command(gc, GC_N64_CMD_03, target);
 281                gc_n64_send_command(gc, GC_N64_CMD_80, target);
 282                gc_n64_send_command(gc, GC_N64_CMD_01, target);
 283                for (i = 0; i < 32; i++)
 284                        gc_n64_send_command(gc, GC_N64_CMD_80, target);
 285                gc_n64_send_stop_bit(gc, target);
 286
 287                udelay(GC_N64_DELAY);
 288
 289                /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
 290                gc_n64_send_command(gc, GC_N64_CMD_03, target);
 291                gc_n64_send_command(gc, GC_N64_CMD_c0, target);
 292                gc_n64_send_command(gc, GC_N64_CMD_1b, target);
 293                for (i = 0; i < 32; i++)
 294                        gc_n64_send_command(gc, cmd, target);
 295                gc_n64_send_stop_bit(gc, target);
 296
 297                local_irq_restore(flags);
 298
 299        }
 300
 301        return 0;
 302}
 303
 304static int gc_n64_init_ff(struct input_dev *dev, int i)
 305{
 306        struct gc_subdev *sdev;
 307        int err;
 308
 309        sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
 310        if (!sdev)
 311                return -ENOMEM;
 312
 313        sdev->idx = i;
 314
 315        input_set_capability(dev, EV_FF, FF_RUMBLE);
 316
 317        err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
 318        if (err) {
 319                kfree(sdev);
 320                return err;
 321        }
 322
 323        return 0;
 324}
 325
 326/*
 327 * NES/SNES support.
 328 */
 329
 330#define GC_NES_DELAY            6       /* Delay between bits - 6us */
 331#define GC_NES_LENGTH           8       /* The NES pads use 8 bits of data */
 332#define GC_SNES_LENGTH          12      /* The SNES true length is 16, but the
 333                                           last 4 bits are unused */
 334#define GC_SNESMOUSE_LENGTH     32      /* The SNES mouse uses 32 bits, the first
 335                                           16 bits are equivalent to a gamepad */
 336
 337#define GC_NES_POWER    0xfc
 338#define GC_NES_CLOCK    0x01
 339#define GC_NES_LATCH    0x02
 340
 341static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
 342static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
 343static const short gc_snes_btn[] = {
 344        BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
 345};
 346
 347/*
 348 * gc_nes_read_packet() reads a NES/SNES packet.
 349 * Each pad uses one bit per byte. So all pads connected to
 350 * this port are read in parallel.
 351 */
 352
 353static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
 354{
 355        int i;
 356
 357        parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
 358        udelay(GC_NES_DELAY * 2);
 359        parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
 360
 361        for (i = 0; i < length; i++) {
 362                udelay(GC_NES_DELAY);
 363                parport_write_data(gc->pd->port, GC_NES_POWER);
 364                data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
 365                udelay(GC_NES_DELAY);
 366                parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
 367        }
 368}
 369
 370static void gc_nes_process_packet(struct gc *gc)
 371{
 372        unsigned char data[GC_SNESMOUSE_LENGTH];
 373        struct gc_pad *pad;
 374        struct input_dev *dev;
 375        int i, j, s, len;
 376        char x_rel, y_rel;
 377
 378        len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
 379                        (gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
 380
 381        gc_nes_read_packet(gc, len, data);
 382
 383        for (i = 0; i < GC_MAX_DEVICES; i++) {
 384
 385                pad = &gc->pads[i];
 386                dev = pad->dev;
 387                s = gc_status_bit[i];
 388
 389                switch (pad->type) {
 390
 391                case GC_NES:
 392
 393                        input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
 394                        input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
 395
 396                        for (j = 0; j < 4; j++)
 397                                input_report_key(dev, gc_snes_btn[j],
 398                                                 s & data[gc_nes_bytes[j]]);
 399                        input_sync(dev);
 400                        break;
 401
 402                case GC_SNES:
 403
 404                        input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
 405                        input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
 406
 407                        for (j = 0; j < 8; j++)
 408                                input_report_key(dev, gc_snes_btn[j],
 409                                                 s & data[gc_snes_bytes[j]]);
 410                        input_sync(dev);
 411                        break;
 412
 413                case GC_SNESMOUSE:
 414                        /*
 415                         * The 4 unused bits from SNES controllers appear
 416                         * to be ID bits so use them to make sure we are
 417                         * dealing with a mouse.
 418                         * gamepad is connected. This is important since
 419                         * my SNES gamepad sends 1's for bits 16-31, which
 420                         * cause the mouse pointer to quickly move to the
 421                         * upper left corner of the screen.
 422                         */
 423                        if (!(s & data[12]) && !(s & data[13]) &&
 424                            !(s & data[14]) && (s & data[15])) {
 425                                input_report_key(dev, BTN_LEFT, s & data[9]);
 426                                input_report_key(dev, BTN_RIGHT, s & data[8]);
 427
 428                                x_rel = y_rel = 0;
 429                                for (j = 0; j < 7; j++) {
 430                                        x_rel <<= 1;
 431                                        if (data[25 + j] & s)
 432                                                x_rel |= 1;
 433
 434                                        y_rel <<= 1;
 435                                        if (data[17 + j] & s)
 436                                                y_rel |= 1;
 437                                }
 438
 439                                if (x_rel) {
 440                                        if (data[24] & s)
 441                                                x_rel = -x_rel;
 442                                        input_report_rel(dev, REL_X, x_rel);
 443                                }
 444
 445                                if (y_rel) {
 446                                        if (data[16] & s)
 447                                                y_rel = -y_rel;
 448                                        input_report_rel(dev, REL_Y, y_rel);
 449                                }
 450
 451                                input_sync(dev);
 452                        }
 453                        break;
 454
 455                default:
 456                        break;
 457                }
 458        }
 459}
 460
 461/*
 462 * Multisystem joystick support
 463 */
 464
 465#define GC_MULTI_LENGTH         5       /* Multi system joystick packet length is 5 */
 466#define GC_MULTI2_LENGTH        6       /* One more bit for one more button */
 467
 468/*
 469 * gc_multi_read_packet() reads a Multisystem joystick packet.
 470 */
 471
 472static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
 473{
 474        int i;
 475
 476        for (i = 0; i < length; i++) {
 477                parport_write_data(gc->pd->port, ~(1 << i));
 478                data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
 479        }
 480}
 481
 482static void gc_multi_process_packet(struct gc *gc)
 483{
 484        unsigned char data[GC_MULTI2_LENGTH];
 485        int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
 486        struct gc_pad *pad;
 487        struct input_dev *dev;
 488        int i, s;
 489
 490        gc_multi_read_packet(gc, data_len, data);
 491
 492        for (i = 0; i < GC_MAX_DEVICES; i++) {
 493                pad = &gc->pads[i];
 494                dev = pad->dev;
 495                s = gc_status_bit[i];
 496
 497                switch (pad->type) {
 498                case GC_MULTI2:
 499                        input_report_key(dev, BTN_THUMB, s & data[5]);
 500                        /* fall through */
 501
 502                case GC_MULTI:
 503                        input_report_abs(dev, ABS_X,
 504                                         !(s & data[2]) - !(s & data[3]));
 505                        input_report_abs(dev, ABS_Y,
 506                                         !(s & data[0]) - !(s & data[1]));
 507                        input_report_key(dev, BTN_TRIGGER, s & data[4]);
 508                        input_sync(dev);
 509                        break;
 510
 511                default:
 512                        break;
 513                }
 514        }
 515}
 516
 517/*
 518 * PSX support
 519 *
 520 * See documentation at:
 521 *      http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt 
 522 *      http://www.gamesx.com/controldata/psxcont/psxcont.htm
 523 *
 524 */
 525
 526#define GC_PSX_DELAY    25              /* 25 usec */
 527#define GC_PSX_LENGTH   8               /* talk to the controller in bits */
 528#define GC_PSX_BYTES    6               /* the maximum number of bytes to read off the controller */
 529
 530#define GC_PSX_MOUSE    1               /* Mouse */
 531#define GC_PSX_NEGCON   2               /* NegCon */
 532#define GC_PSX_NORMAL   4               /* Digital / Analog or Rumble in Digital mode  */
 533#define GC_PSX_ANALOG   5               /* Analog in Analog mode / Rumble in Green mode */
 534#define GC_PSX_RUMBLE   7               /* Rumble in Red mode */
 535
 536#define GC_PSX_CLOCK    0x04            /* Pin 4 */
 537#define GC_PSX_COMMAND  0x01            /* Pin 2 */
 538#define GC_PSX_POWER    0xf8            /* Pins 5-9 */
 539#define GC_PSX_SELECT   0x02            /* Pin 3 */
 540
 541#define GC_PSX_ID(x)    ((x) >> 4)      /* High nibble is device type */
 542#define GC_PSX_LEN(x)   (((x) & 0xf) << 1)      /* Low nibble is length in bytes/2 */
 543
 544static int gc_psx_delay = GC_PSX_DELAY;
 545module_param_named(psx_delay, gc_psx_delay, uint, 0);
 546MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
 547
 548static const short gc_psx_abs[] = {
 549        ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
 550};
 551static const short gc_psx_btn[] = {
 552        BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
 553        BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
 554};
 555static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
 556
 557/*
 558 * gc_psx_command() writes 8bit command and reads 8bit data from
 559 * the psx pad.
 560 */
 561
 562static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
 563{
 564        struct parport *port = gc->pd->port;
 565        int i, j, cmd, read;
 566
 567        memset(data, 0, GC_MAX_DEVICES);
 568
 569        for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
 570                cmd = (b & 1) ? GC_PSX_COMMAND : 0;
 571                parport_write_data(port, cmd | GC_PSX_POWER);
 572                udelay(gc_psx_delay);
 573
 574                read = parport_read_status(port) ^ 0x80;
 575
 576                for (j = 0; j < GC_MAX_DEVICES; j++) {
 577                        struct gc_pad *pad = &gc->pads[j];
 578
 579                        if (pad->type == GC_PSX || pad->type == GC_DDR)
 580                                data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
 581                }
 582
 583                parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
 584                udelay(gc_psx_delay);
 585        }
 586}
 587
 588/*
 589 * gc_psx_read_packet() reads a whole psx packet and returns
 590 * device identifier code.
 591 */
 592
 593static void gc_psx_read_packet(struct gc *gc,
 594                               unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
 595                               unsigned char id[GC_MAX_DEVICES])
 596{
 597        int i, j, max_len = 0;
 598        unsigned long flags;
 599        unsigned char data2[GC_MAX_DEVICES];
 600
 601        /* Select pad */
 602        parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
 603        udelay(gc_psx_delay);
 604        /* Deselect, begin command */
 605        parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
 606        udelay(gc_psx_delay);
 607
 608        local_irq_save(flags);
 609
 610        gc_psx_command(gc, 0x01, data2);        /* Access pad */
 611        gc_psx_command(gc, 0x42, id);           /* Get device ids */
 612        gc_psx_command(gc, 0, data2);           /* Dump status */
 613
 614        /* Find the longest pad */
 615        for (i = 0; i < GC_MAX_DEVICES; i++) {
 616                struct gc_pad *pad = &gc->pads[i];
 617
 618                if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
 619                    GC_PSX_LEN(id[i]) > max_len &&
 620                    GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
 621                        max_len = GC_PSX_LEN(id[i]);
 622                }
 623        }
 624
 625        /* Read in all the data */
 626        for (i = 0; i < max_len; i++) {
 627                gc_psx_command(gc, 0, data2);
 628                for (j = 0; j < GC_MAX_DEVICES; j++)
 629                        data[j][i] = data2[j];
 630        }
 631
 632        local_irq_restore(flags);
 633
 634        parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
 635
 636        /* Set id's to the real value */
 637        for (i = 0; i < GC_MAX_DEVICES; i++)
 638                id[i] = GC_PSX_ID(id[i]);
 639}
 640
 641static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
 642                              unsigned char *data)
 643{
 644        struct input_dev *dev = pad->dev;
 645        int i;
 646
 647        switch (psx_type) {
 648
 649        case GC_PSX_RUMBLE:
 650
 651                input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
 652                input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
 653                /* fall through */
 654
 655        case GC_PSX_NEGCON:
 656        case GC_PSX_ANALOG:
 657
 658                if (pad->type == GC_DDR) {
 659                        for (i = 0; i < 4; i++)
 660                                input_report_key(dev, gc_psx_ddr_btn[i],
 661                                                 ~data[0] & (0x10 << i));
 662                } else {
 663                        for (i = 0; i < 4; i++)
 664                                input_report_abs(dev, gc_psx_abs[i + 2],
 665                                                 data[i + 2]);
 666
 667                        input_report_abs(dev, ABS_X,
 668                                !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
 669                        input_report_abs(dev, ABS_Y,
 670                                !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
 671                }
 672
 673                for (i = 0; i < 8; i++)
 674                        input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
 675
 676                input_report_key(dev, BTN_START,  ~data[0] & 0x08);
 677                input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
 678
 679                input_sync(dev);
 680
 681                break;
 682
 683        case GC_PSX_NORMAL:
 684
 685                if (pad->type == GC_DDR) {
 686                        for (i = 0; i < 4; i++)
 687                                input_report_key(dev, gc_psx_ddr_btn[i],
 688                                                 ~data[0] & (0x10 << i));
 689                } else {
 690                        input_report_abs(dev, ABS_X,
 691                                !!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
 692                        input_report_abs(dev, ABS_Y,
 693                                !!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
 694
 695                        /*
 696                         * For some reason if the extra axes are left unset
 697                         * they drift.
 698                         * for (i = 0; i < 4; i++)
 699                                input_report_abs(dev, gc_psx_abs[i + 2], 128);
 700                         * This needs to be debugged properly,
 701                         * maybe fuzz processing needs to be done
 702                         * in input_sync()
 703                         *                               --vojtech
 704                         */
 705                }
 706
 707                for (i = 0; i < 8; i++)
 708                        input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
 709
 710                input_report_key(dev, BTN_START,  ~data[0] & 0x08);
 711                input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
 712
 713                input_sync(dev);
 714
 715                break;
 716
 717        default: /* not a pad, ignore */
 718                break;
 719        }
 720}
 721
 722static void gc_psx_process_packet(struct gc *gc)
 723{
 724        unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
 725        unsigned char id[GC_MAX_DEVICES];
 726        struct gc_pad *pad;
 727        int i;
 728
 729        gc_psx_read_packet(gc, data, id);
 730
 731        for (i = 0; i < GC_MAX_DEVICES; i++) {
 732                pad = &gc->pads[i];
 733                if (pad->type == GC_PSX || pad->type == GC_DDR)
 734                        gc_psx_report_one(pad, id[i], data[i]);
 735        }
 736}
 737
 738/*
 739 * gc_timer() initiates reads of console pads data.
 740 */
 741
 742static void gc_timer(struct timer_list *t)
 743{
 744        struct gc *gc = from_timer(gc, t, timer);
 745
 746/*
 747 * N64 pads - must be read first, any read confuses them for 200 us
 748 */
 749
 750        if (gc->pad_count[GC_N64])
 751                gc_n64_process_packet(gc);
 752
 753/*
 754 * NES and SNES pads or mouse
 755 */
 756
 757        if (gc->pad_count[GC_NES] ||
 758            gc->pad_count[GC_SNES] ||
 759            gc->pad_count[GC_SNESMOUSE]) {
 760                gc_nes_process_packet(gc);
 761        }
 762
 763/*
 764 * Multi and Multi2 joysticks
 765 */
 766
 767        if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
 768                gc_multi_process_packet(gc);
 769
 770/*
 771 * PSX controllers
 772 */
 773
 774        if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
 775                gc_psx_process_packet(gc);
 776
 777        mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
 778}
 779
 780static int gc_open(struct input_dev *dev)
 781{
 782        struct gc *gc = input_get_drvdata(dev);
 783        int err;
 784
 785        err = mutex_lock_interruptible(&gc->mutex);
 786        if (err)
 787                return err;
 788
 789        if (!gc->used++) {
 790                parport_claim(gc->pd);
 791                parport_write_control(gc->pd->port, 0x04);
 792                mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
 793        }
 794
 795        mutex_unlock(&gc->mutex);
 796        return 0;
 797}
 798
 799static void gc_close(struct input_dev *dev)
 800{
 801        struct gc *gc = input_get_drvdata(dev);
 802
 803        mutex_lock(&gc->mutex);
 804        if (!--gc->used) {
 805                del_timer_sync(&gc->timer);
 806                parport_write_control(gc->pd->port, 0x00);
 807                parport_release(gc->pd);
 808        }
 809        mutex_unlock(&gc->mutex);
 810}
 811
 812static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
 813{
 814        struct gc_pad *pad = &gc->pads[idx];
 815        struct input_dev *input_dev;
 816        int i;
 817        int err;
 818
 819        if (pad_type < 1 || pad_type >= GC_MAX) {
 820                pr_err("Pad type %d unknown\n", pad_type);
 821                return -EINVAL;
 822        }
 823
 824        pad->dev = input_dev = input_allocate_device();
 825        if (!input_dev) {
 826                pr_err("Not enough memory for input device\n");
 827                return -ENOMEM;
 828        }
 829
 830        pad->type = pad_type;
 831
 832        snprintf(pad->phys, sizeof(pad->phys),
 833                 "%s/input%d", gc->pd->port->name, idx);
 834
 835        input_dev->name = gc_names[pad_type];
 836        input_dev->phys = pad->phys;
 837        input_dev->id.bustype = BUS_PARPORT;
 838        input_dev->id.vendor = 0x0001;
 839        input_dev->id.product = pad_type;
 840        input_dev->id.version = 0x0100;
 841
 842        input_set_drvdata(input_dev, gc);
 843
 844        input_dev->open = gc_open;
 845        input_dev->close = gc_close;
 846
 847        if (pad_type != GC_SNESMOUSE) {
 848                input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
 849
 850                for (i = 0; i < 2; i++)
 851                        input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
 852        } else
 853                input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
 854
 855        gc->pad_count[pad_type]++;
 856
 857        switch (pad_type) {
 858
 859        case GC_N64:
 860                for (i = 0; i < 10; i++)
 861                        input_set_capability(input_dev, EV_KEY, gc_n64_btn[i]);
 862
 863                for (i = 0; i < 2; i++) {
 864                        input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
 865                        input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
 866                }
 867
 868                err = gc_n64_init_ff(input_dev, idx);
 869                if (err) {
 870                        pr_warn("Failed to initiate rumble for N64 device %d\n",
 871                                idx);
 872                        goto err_free_dev;
 873                }
 874
 875                break;
 876
 877        case GC_SNESMOUSE:
 878                input_set_capability(input_dev, EV_KEY, BTN_LEFT);
 879                input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
 880                input_set_capability(input_dev, EV_REL, REL_X);
 881                input_set_capability(input_dev, EV_REL, REL_Y);
 882                break;
 883
 884        case GC_SNES:
 885                for (i = 4; i < 8; i++)
 886                        input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
 887                /* fall through */
 888        case GC_NES:
 889                for (i = 0; i < 4; i++)
 890                        input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
 891                break;
 892
 893        case GC_MULTI2:
 894                input_set_capability(input_dev, EV_KEY, BTN_THUMB);
 895                /* fall through */
 896        case GC_MULTI:
 897                input_set_capability(input_dev, EV_KEY, BTN_TRIGGER);
 898                /* fall through */
 899                break;
 900
 901        case GC_PSX:
 902                for (i = 0; i < 6; i++)
 903                        input_set_abs_params(input_dev,
 904                                             gc_psx_abs[i], 4, 252, 0, 2);
 905                for (i = 0; i < 12; i++)
 906                        input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
 907                break;
 908
 909                break;
 910
 911        case GC_DDR:
 912                for (i = 0; i < 4; i++)
 913                        input_set_capability(input_dev, EV_KEY,
 914                                             gc_psx_ddr_btn[i]);
 915                for (i = 0; i < 12; i++)
 916                        input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
 917
 918                break;
 919        }
 920
 921        err = input_register_device(pad->dev);
 922        if (err)
 923                goto err_free_dev;
 924
 925        return 0;
 926
 927err_free_dev:
 928        input_free_device(pad->dev);
 929        pad->dev = NULL;
 930        return err;
 931}
 932
 933static void gc_attach(struct parport *pp)
 934{
 935        struct gc *gc;
 936        struct pardevice *pd;
 937        int i, port_idx;
 938        int count = 0;
 939        int *pads, n_pads;
 940        struct pardev_cb gc_parport_cb;
 941
 942        for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
 943                if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
 944                        continue;
 945
 946                if (gc_cfg[port_idx].args[0] == pp->number)
 947                        break;
 948        }
 949
 950        if (port_idx == GC_MAX_PORTS) {
 951                pr_debug("Not using parport%d.\n", pp->number);
 952                return;
 953        }
 954        pads = gc_cfg[port_idx].args + 1;
 955        n_pads = gc_cfg[port_idx].nargs - 1;
 956
 957        memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
 958        gc_parport_cb.flags = PARPORT_FLAG_EXCL;
 959
 960        pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
 961                                        port_idx);
 962        if (!pd) {
 963                pr_err("parport busy already - lp.o loaded?\n");
 964                return;
 965        }
 966
 967        gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
 968        if (!gc) {
 969                pr_err("Not enough memory\n");
 970                goto err_unreg_pardev;
 971        }
 972
 973        mutex_init(&gc->mutex);
 974        gc->pd = pd;
 975        gc->parportno = pp->number;
 976        timer_setup(&gc->timer, gc_timer, 0);
 977
 978        for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
 979                if (!pads[i])
 980                        continue;
 981
 982                if (gc_setup_pad(gc, i, pads[i]))
 983                        goto err_unreg_devs;
 984
 985                count++;
 986        }
 987
 988        if (count == 0) {
 989                pr_err("No valid devices specified\n");
 990                goto err_free_gc;
 991        }
 992
 993        gc_base[port_idx] = gc;
 994        return;
 995
 996 err_unreg_devs:
 997        while (--i >= 0)
 998                if (gc->pads[i].dev)
 999                        input_unregister_device(gc->pads[i].dev);
1000 err_free_gc:
1001        kfree(gc);
1002 err_unreg_pardev:
1003        parport_unregister_device(pd);
1004}
1005
1006static void gc_detach(struct parport *port)
1007{
1008        int i;
1009        struct gc *gc;
1010
1011        for (i = 0; i < GC_MAX_PORTS; i++) {
1012                if (gc_base[i] && gc_base[i]->parportno == port->number)
1013                        break;
1014        }
1015
1016        if (i == GC_MAX_PORTS)
1017                return;
1018
1019        gc = gc_base[i];
1020        gc_base[i] = NULL;
1021
1022        for (i = 0; i < GC_MAX_DEVICES; i++)
1023                if (gc->pads[i].dev)
1024                        input_unregister_device(gc->pads[i].dev);
1025        parport_unregister_device(gc->pd);
1026        kfree(gc);
1027}
1028
1029static struct parport_driver gc_parport_driver = {
1030        .name = "gamecon",
1031        .match_port = gc_attach,
1032        .detach = gc_detach,
1033        .devmodel = true,
1034};
1035
1036static int __init gc_init(void)
1037{
1038        int i;
1039        int have_dev = 0;
1040
1041        for (i = 0; i < GC_MAX_PORTS; i++) {
1042                if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
1043                        continue;
1044
1045                if (gc_cfg[i].nargs < 2) {
1046                        pr_err("at least one device must be specified\n");
1047                        return -EINVAL;
1048                }
1049
1050                have_dev = 1;
1051        }
1052
1053        if (!have_dev)
1054                return -ENODEV;
1055
1056        return parport_register_driver(&gc_parport_driver);
1057}
1058
1059static void __exit gc_exit(void)
1060{
1061        parport_unregister_driver(&gc_parport_driver);
1062}
1063
1064module_init(gc_init);
1065module_exit(gc_exit);
1066