2 * $Id: gamecon.c,v 1.1.1.1 2005/04/11 02:50:21 jack Exp $
4 * Copyright (c) 1999-2001 Vojtech Pavlik
6 * Based on the work of:
7 * Andree Borrmann John Dahlstrom
8 * David Kuder Nathan Hand
14 * NES, SNES, N64, Multi1, Multi2, PSX gamepad driver for Linux
18 * This program is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation; either version 2 of the License, or
21 * (at your option) any later version.
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
28 * You should have received a copy of the GNU General Public License
29 * along with this program; if not, write to the Free Software
30 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
32 * Should you need to contact me, the author, you can do so either by
33 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
34 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
37 #include <linux/kernel.h>
38 #include <linux/delay.h>
39 #include <linux/module.h>
40 #include <linux/init.h>
41 #include <linux/parport.h>
42 #include <linux/input.h>
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@suse.cz>");
45 MODULE_LICENSE("GPL");
46 MODULE_PARM(gc, "2-6i");
47 MODULE_PARM(gc_2,"2-6i");
48 MODULE_PARM(gc_3,"2-6i");
60 #define GC_REFRESH_TIME HZ/100
64 struct input_dev dev[5];
65 struct timer_list timer;
66 unsigned char pads[GC_MAX + 1];
70 static struct gc *gc_base[3];
72 static int gc[] __initdata = { -1, 0, 0, 0, 0, 0 };
73 static int gc_2[] __initdata = { -1, 0, 0, 0, 0, 0 };
74 static int gc_3[] __initdata = { -1, 0, 0, 0, 0, 0 };
76 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
78 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
79 "Multisystem 2-button joystick", "N64 controller", "PSX controller" };
84 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
85 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 };
87 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
88 #define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */
89 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
90 #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */
91 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
92 /* GC_N64_DWS > 24 is known to fail */
93 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
94 #define GC_N64_POWER_R 0xfd /* power during read */
95 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
96 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
97 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
99 #define GC_N64_CLOCK 0x02 /* clock bits for read */
102 * gc_n64_read_packet() reads an N64 packet.
103 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
106 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
112 * Request the pad to transmit data
117 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
118 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
121 __restore_flags(flags);
124 * Wait for the pad response to be loaded into the 33-bit register of the adapter
127 udelay(GC_N64_DELAY);
130 * Grab data (ignoring the last bit, which is a stop bit)
133 for (i = 0; i < GC_N64_LENGTH; i++) {
134 parport_write_data(gc->pd->port, GC_N64_POWER_R);
135 data[i] = parport_read_status(gc->pd->port);
136 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
140 * We must wait 200 ms here for the controller to reinitialize before the next read request.
141 * No worries as long as gc_read is polled less frequently than this.
150 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
151 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
152 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the last 4 bits are unused */
154 #define GC_NES_POWER 0xfc
155 #define GC_NES_CLOCK 0x01
156 #define GC_NES_LATCH 0x02
158 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
159 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
160 static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
163 * gc_nes_read_packet() reads a NES/SNES packet.
164 * Each pad uses one bit per byte. So all pads connected to
165 * this port are read in parallel.
168 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
172 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
173 udelay(GC_NES_DELAY * 2);
174 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
176 for (i = 0; i < length; i++) {
177 udelay(GC_NES_DELAY);
178 parport_write_data(gc->pd->port, GC_NES_POWER);
179 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
180 udelay(GC_NES_DELAY);
181 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
186 * Multisystem joystick support
189 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
190 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
193 * gc_multi_read_packet() reads a Multisystem joystick packet.
196 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
200 for (i = 0; i < length; i++) {
201 parport_write_data(gc->pd->port, ~(1 << i));
202 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
209 * See documentation at:
210 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
211 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
212 * ftp://milano.usal.es/pablo/
216 #define GC_PSX_DELAY 60 /* 60 usec */
217 #define GC_PSX_LENGTH 8 /* talk to the controller in bytes */
219 #define GC_PSX_MOUSE 1 /* Mouse */
220 #define GC_PSX_NEGCON 2 /* NegCon */
221 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
222 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
223 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
225 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
226 #define GC_PSX_COMMAND 0x01 /* Pin 1 */
227 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
228 #define GC_PSX_SELECT 0x02 /* Pin 3 */
230 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
231 #define GC_PSX_LEN(x) ((x) & 0xf) /* Low nibble is length in words */
233 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
234 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
235 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
238 * gc_psx_command() writes 8bit command and reads 8bit data from
242 static int gc_psx_command(struct gc *gc, int b)
244 int i, cmd, data = 0;
246 for (i = 0; i < 8; i++, b >>= 1) {
247 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
248 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
249 udelay(GC_PSX_DELAY);
250 data |= ((parport_read_status(gc->pd->port) ^ 0x80) & gc->pads[GC_PSX]) ? (1 << i) : 0;
251 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
252 udelay(GC_PSX_DELAY);
258 * gc_psx_read_packet() reads a whole psx packet and returns
259 * device identifier code.
262 static int gc_psx_read_packet(struct gc *gc, unsigned char *data)
267 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
268 udelay(GC_PSX_DELAY * 2);
269 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
270 udelay(GC_PSX_DELAY * 2);
275 gc_psx_command(gc, 0x01); /* Access pad */
276 id = gc_psx_command(gc, 0x42); /* Get device id */
277 if (gc_psx_command(gc, 0) == 0x5a) { /* Okay? */
278 for (i = 0; i < GC_PSX_LEN(id) * 2; i++)
279 data[i] = gc_psx_command(gc, 0);
282 __restore_flags(flags);
284 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
286 return GC_PSX_ID(id);
290 * gc_timer() reads and analyzes console pads data.
293 #define GC_MAX_LENGTH GC_N64_LENGTH
295 static void gc_timer(unsigned long private)
297 struct gc *gc = (void *) private;
298 struct input_dev *dev = gc->dev;
299 unsigned char data[GC_MAX_LENGTH];
303 * N64 pads - must be read first, any read confuses them for 200 us
306 if (gc->pads[GC_N64]) {
308 gc_n64_read_packet(gc, data);
310 for (i = 0; i < 5; i++) {
312 s = gc_status_bit[i];
314 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
317 axes[0] = axes[1] = 0;
319 for (j = 0; j < 8; j++) {
320 if (data[23 - j] & s) axes[0] |= 1 << j;
321 if (data[31 - j] & s) axes[1] |= 1 << j;
324 input_report_abs(dev + i, ABS_X, axes[0]);
325 input_report_abs(dev + i, ABS_Y, -axes[1]);
327 input_report_abs(dev + i, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
328 input_report_abs(dev + i, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
330 for (j = 0; j < 10; j++)
331 input_report_key(dev + i, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
340 if (gc->pads[GC_NES] || gc->pads[GC_SNES]) {
342 gc_nes_read_packet(gc, gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH, data);
344 for (i = 0; i < 5; i++) {
346 s = gc_status_bit[i];
348 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
349 input_report_abs(dev + i, ABS_X, !(s & data[6]) - !(s & data[7]));
350 input_report_abs(dev + i, ABS_Y, !(s & data[4]) - !(s & data[5]));
353 if (s & gc->pads[GC_NES])
354 for (j = 0; j < 4; j++)
355 input_report_key(dev + i, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
357 if (s & gc->pads[GC_SNES])
358 for (j = 0; j < 8; j++)
359 input_report_key(dev + i, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
364 * Multi and Multi2 joysticks
367 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2]) {
369 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
371 for (i = 0; i < 5; i++) {
373 s = gc_status_bit[i];
375 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
376 input_report_abs(dev + i, ABS_X, !(s & data[2]) - !(s & data[3]));
377 input_report_abs(dev + i, ABS_Y, !(s & data[0]) - !(s & data[1]));
378 input_report_key(dev + i, BTN_TRIGGER, s & data[4]);
381 if (s & gc->pads[GC_MULTI2])
382 input_report_key(dev + i, BTN_THUMB, s & data[5]);
390 if (gc->pads[GC_PSX]) {
392 for (i = 0; i < 5; i++)
393 if (gc->pads[GC_PSX] & gc_status_bit[i])
396 switch (gc_psx_read_packet(gc, data)) {
400 input_report_key(dev + i, BTN_THUMB, ~data[0] & 0x04);
401 input_report_key(dev + i, BTN_THUMB2, ~data[0] & 0x02);
406 for (j = 0; j < 4; j++)
407 input_report_abs(dev + i, gc_psx_abs[j], data[j + 2]);
409 input_report_abs(dev + i, ABS_HAT0X, !(data[0] & 0x20) - !(data[0] & 0x80));
410 input_report_abs(dev + i, ABS_HAT0Y, !(data[0] & 0x40) - !(data[0] & 0x10));
412 for (j = 0; j < 8; j++)
413 input_report_key(dev + i, gc_psx_btn[j], ~data[1] & (1 << j));
415 input_report_key(dev + i, BTN_START, ~data[0] & 0x08);
416 input_report_key(dev + i, BTN_SELECT, ~data[0] & 0x01);
422 input_report_abs(dev + i, ABS_X, 128 + !(data[0] & 0x20) * 127 - !(data[0] & 0x80) * 128);
423 input_report_abs(dev + i, ABS_Y, 128 + !(data[0] & 0x40) * 127 - !(data[0] & 0x10) * 128);
425 for (j = 0; j < 8; j++)
426 input_report_key(dev + i, gc_psx_btn[j], ~data[1] & (1 << j));
428 input_report_key(dev + i, BTN_START, ~data[0] & 0x08);
429 input_report_key(dev + i, BTN_SELECT, ~data[0] & 0x01);
435 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
438 static int gc_open(struct input_dev *dev)
440 struct gc *gc = dev->private;
442 parport_claim(gc->pd);
443 parport_write_control(gc->pd->port, 0x04);
444 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
449 static void gc_close(struct input_dev *dev)
451 struct gc *gc = dev->private;
453 del_timer(&gc->timer);
454 parport_write_control(gc->pd->port, 0x00);
455 parport_release(gc->pd);
459 static struct gc __init *gc_probe(int *config)
464 unsigned char data[32];
469 for (pp = parport_enumerate(); pp && (config[0] > 0); pp = pp->next)
473 printk(KERN_ERR "gamecon.c: no such parport\n");
477 if (!(gc = kmalloc(sizeof(struct gc), GFP_KERNEL)))
479 memset(gc, 0, sizeof(struct gc));
481 gc->pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
484 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
489 parport_claim(gc->pd);
491 init_timer(&gc->timer);
492 gc->timer.data = (long) gc;
493 gc->timer.function = gc_timer;
495 for (i = 0; i < 5; i++) {
500 if (config[i + 1] < 1 || config[i + 1] > GC_MAX) {
501 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", config[i + 1]);
505 gc->dev[i].private = gc;
506 gc->dev[i].open = gc_open;
507 gc->dev[i].close = gc_close;
509 gc->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
511 for (j = 0; j < 2; j++) {
512 set_bit(ABS_X + j, gc->dev[i].absbit);
513 gc->dev[i].absmin[ABS_X + j] = -1;
514 gc->dev[i].absmax[ABS_X + j] = 1;
517 gc->pads[0] |= gc_status_bit[i];
518 gc->pads[config[i + 1]] |= gc_status_bit[i];
520 switch(config[i + 1]) {
523 for (j = 0; j < 10; j++)
524 set_bit(gc_n64_btn[j], gc->dev[i].keybit);
526 for (j = 0; j < 2; j++) {
527 set_bit(ABS_X + j, gc->dev[i].absbit);
528 gc->dev[i].absmin[ABS_X + j] = -127;
529 gc->dev[i].absmax[ABS_X + j] = 126;
530 gc->dev[i].absflat[ABS_X + j] = 2;
531 set_bit(ABS_HAT0X + j, gc->dev[i].absbit);
532 gc->dev[i].absmin[ABS_HAT0X + j] = -1;
533 gc->dev[i].absmax[ABS_HAT0X + j] = 1;
539 for (j = 4; j < 8; j++)
540 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
542 for (j = 0; j < 4; j++)
543 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
547 set_bit(BTN_THUMB, gc->dev[i].keybit);
549 set_bit(BTN_TRIGGER, gc->dev[i].keybit);
554 psx = gc_psx_read_packet(gc, data);
562 for (j = 0; j < 6; j++) {
564 set_bit(psx, gc->dev[i].absbit);
566 gc->dev[i].absmin[psx] = 4;
567 gc->dev[i].absmax[psx] = 252;
568 gc->dev[i].absflat[psx] = 2;
570 gc->dev[i].absmin[psx] = -1;
571 gc->dev[i].absmax[psx] = 1;
575 for (j = 0; j < 12; j++)
576 set_bit(gc_psx_btn[j], gc->dev[i].keybit);
581 gc->pads[GC_PSX] &= ~gc_status_bit[i];
582 printk(KERN_ERR "gamecon.c: No PSX controller found.\n");
586 gc->pads[GC_PSX] &= ~gc_status_bit[i];
587 printk(KERN_WARNING "gamecon.c: Unsupported PSX controller %#x,"
588 " please report to <vojtech@suse.cz>.\n", psx);
593 gc->dev[i].name = gc_names[config[i + 1]];
594 gc->dev[i].idbus = BUS_PARPORT;
595 gc->dev[i].idvendor = 0x0001;
596 gc->dev[i].idproduct = config[i + 1];
597 gc->dev[i].idversion = 0x0100;
600 parport_release(gc->pd);
603 parport_unregister_device(gc->pd);
608 for (i = 0; i < 5; i++)
609 if (gc->pads[0] & gc_status_bit[i]) {
610 input_register_device(gc->dev + i);
611 printk(KERN_INFO "input%d: %s on %s\n", gc->dev[i].number, gc->dev[i].name, gc->pd->port->name);
618 int __init gc_setup(char *str)
621 get_options(str, ARRAY_SIZE(ints), ints);
622 for (i = 0; i <= ints[0] && i < 6; i++) gc[i] = ints[i + 1];
625 int __init gc_setup_2(char *str)
628 get_options(str, ARRAY_SIZE(ints), ints);
629 for (i = 0; i <= ints[0] && i < 6; i++) gc_2[i] = ints[i + 1];
632 int __init gc_setup_3(char *str)
635 get_options(str, ARRAY_SIZE(ints), ints);
636 for (i = 0; i <= ints[0] && i < 6; i++) gc_3[i] = ints[i + 1];
639 __setup("gc=", gc_setup);
640 __setup("gc_2=", gc_setup_2);
641 __setup("gc_3=", gc_setup_3);
644 int __init gc_init(void)
646 gc_base[0] = gc_probe(gc);
647 gc_base[1] = gc_probe(gc_2);
648 gc_base[2] = gc_probe(gc_3);
650 if (gc_base[0] || gc_base[1] || gc_base[2])
656 void __exit gc_exit(void)
660 for (i = 0; i < 3; i++)
662 for (j = 0; j < 5; j++)
663 if (gc_base[i]->pads[0] & gc_status_bit[j])
664 input_unregister_device(gc_base[i]->dev + j);
665 parport_unregister_device(gc_base[i]->pd);
669 module_init(gc_init);
670 module_exit(gc_exit);