📄 gamecon.c
字号:
/* * $Id: gamecon.c,v 1.5 2000/06/25 09:56:58 vojtech Exp $ * * Copyright (c) 1999-2000 Vojtech Pavlik * * Based on the work of: * Andree Borrmann John Dahlstrom * David Kuder * * Sponsored by SuSE *//* * NES, SNES, N64, Multi1, Multi2, PSX gamepad driver for Linux *//* * 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 * (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 * * Should you need to contact me, the author, you can do so either by * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail: * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic */#include <linux/kernel.h>#include <linux/delay.h>#include <linux/module.h>#include <linux/init.h>#include <linux/parport.h>#include <linux/input.h>MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");MODULE_PARM(gc, "2-6i");MODULE_PARM(gc_2,"2-6i");MODULE_PARM(gc_3,"2-6i");#define GC_SNES 1#define GC_NES 2#define GC_NES4 3#define GC_MULTI 4#define GC_MULTI2 5#define GC_N64 6 #define GC_PSX 7#define GC_MAX 7#define GC_REFRESH_TIME HZ/100 struct gc { struct pardevice *pd; struct input_dev dev[5]; struct timer_list timer; unsigned char pads[GC_PSX + 1]; int used;};static struct gc *gc_base[3];static int gc[] __initdata = { -1, 0, 0, 0, 0, 0 };static int gc_2[] __initdata = { -1, 0, 0, 0, 0, 0 };static int gc_3[] __initdata = { -1, 0, 0, 0, 0, 0 };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 pad", "PSX NegCon", "PSX Analog contoller" };/* * N64 support. */static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };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 };#define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */#define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */#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 */ #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 */ /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */ /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */ /* than 123 us */#define GC_N64_CLOCK 0x02 /* clock bits for read *//* * 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. */static void gc_n64_read_packet(struct gc *gc, unsigned char *data){ int i; unsigned long flags;/* * Request the pad to transmit data */ __save_flags(flags); __cli(); for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) { parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0)); udelay(GC_N64_DWS); } __restore_flags(flags);/* * Wait for the pad response to be loaded into the 33-bit register of the adapter */ udelay(GC_N64_DELAY);/* * Grab data (ignoring the last bit, which is a stop bit) */ for (i = 0; i < GC_N64_LENGTH; i++) { parport_write_data(gc->pd->port, GC_N64_POWER_R); data[i] = parport_read_status(gc->pd->port); parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK); }/* * We must wait 200 ms here for the controller to reinitialize before the next read request. * No worries as long as gc_read is polled less frequently than this. */}/* * 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_POWER 0xfc#define GC_NES_CLOCK 0x01#define GC_NES_LATCH 0x02static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };/* * gc_nes_read_packet() reads a NES/SNES packet. * Each pad uses one bit per byte. So all pads connected to * this port are read in parallel. */static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data){ int i; parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH); udelay(GC_NES_DELAY * 2); parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK); for (i = 0; i < length; i++) { udelay(GC_NES_DELAY); parport_write_data(gc->pd->port, GC_NES_POWER); data[i] = parport_read_status(gc->pd->port) ^ 0x7f; udelay(GC_NES_DELAY); parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK); }}/* * Multisystem joystick support */#define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */#define GC_MULTI2_LENGTH 6 /* One more bit for one more button *//* * gc_multi_read_packet() reads a Multisystem joystick packet. */static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data){ int i; for (i = 0; i < length; i++) { parport_write_data(gc->pd->port, ~(1 << i)); data[i] = parport_read_status(gc->pd->port) ^ 0x7f; }}/* * PSX support */#define GC_PSX_DELAY 10#define GC_PSX_LENGTH 8 /* talk to the controller in bytes */#define GC_PSX_MOUSE 0x12 /* PSX Mouse */#define GC_PSX_NEGCON 0x23 /* NegCon pad */#define GC_PSX_NORMAL 0x41 /* Standard Digital controller */#define GC_PSX_ANALOGR 0x73 /* Analog controller in Red mode */#define GC_PSX_ANALOGG 0x53 /* Analog controller in Green mode */#define GC_PSX_CLOCK 0x04 /* Pin 3 */#define GC_PSX_COMMAND 0x01 /* Pin 1 */#define GC_PSX_POWER 0xf8 /* Pins 5-9 */#define GC_PSX_SELECT 0x02 /* Pin 2 */#define GC_PSX_NOPOWER 0x04static 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_THUMB, BTN_THUMB2 };/* * gc_psx_command() writes 8bit command and reads 8bit data from * the psx pad. */static int gc_psx_command(struct gc *gc, int b){ int i, cmd, ret = 0; cmd = (b & 1) ? GC_PSX_COMMAND : 0; for (i = 0; i < 8; i++) { parport_write_data(gc->pd->port, cmd | GC_PSX_POWER); udelay(GC_PSX_DELAY); ret |= ((parport_read_status(gc->pd->port) ^ 0x80) & gc->pads[GC_PSX]) ? (1 << i) : 0; cmd = (b & 1) ? GC_PSX_COMMAND : 0; parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER); udelay(GC_PSX_DELAY); b >>= 1; } return ret;}/* * gc_psx_read_packet() reads a whole psx packet and returns * device identifier code. */static int gc_psx_read_packet(struct gc *gc, int length, unsigned char *data){ int i, ret; unsigned long flags; __save_flags(flags); __cli(); parport_write_data(gc->pd->port, GC_PSX_POWER); parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */ udelay(GC_PSX_DELAY * 2); gc_psx_command(gc, 0x01); /* Access pad */ ret = gc_psx_command(gc, 0x42); /* Get device id */ if (gc_psx_command(gc, 0) == 'Z') /* okay? */ for (i = 0; i < length; i++) data[i] = gc_psx_command(gc, 0); else ret = -1; parport_write_data(gc->pd->port, GC_PSX_SELECT | GC_PSX_CLOCK | GC_PSX_POWER); __restore_flags(flags); return ret;}/* * gc_timer() reads and analyzes console pads data. */#define GC_MAX_LENGTH GC_N64_LENGTHstatic void gc_timer(unsigned long private){ 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 */ if (gc->pads[GC_N64]) { gc_n64_read_packet(gc, data); for (i = 0; i < 5; i++) { s = gc_status_bit[i]; if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) { signed char axes[2]; 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 + i, ABS_X, axes[0]); input_report_abs(dev + i, ABS_Y, -axes[1]); input_report_abs(dev + i, ABS_HAT0X, !!(s & data[7]) - !!(s & data[6])); input_report_abs(dev + i, ABS_HAT0Y, !!(s & data[5]) - !!(s & data[4])); for (j = 0; j < 10; j++) input_report_key(dev + i, gc_n64_btn[j], s & data[gc_n64_bytes[j]]); } } }/* * NES and SNES pads */ if (gc->pads[GC_NES] || gc->pads[GC_SNES]) { gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -