2 * $Id: gamecon.c,v 1.4 2000/05/29 21:08:45 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * Based on the work of:
7 * Andree Borrmann John Dahlstrom
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_PARM(gc
, "2-6i");
46 MODULE_PARM(gc_2
,"2-6i");
47 MODULE_PARM(gc_3
,"2-6i");
59 #define GC_REFRESH_TIME HZ/100
63 struct input_dev dev
[5];
64 struct timer_list timer
;
65 unsigned char pads
[GC_PSX
+ 1];
69 static struct gc
*gc_base
[3];
71 static int gc
[] __initdata
= { -1, 0, 0, 0, 0, 0 };
72 static int gc_2
[] __initdata
= { -1, 0, 0, 0, 0, 0 };
73 static int gc_3
[] __initdata
= { -1, 0, 0, 0, 0, 0 };
75 static int gc_status_bit
[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
77 static char *gc_names
[] = { NULL
, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
78 "Multisystem 2-button joystick", "N64 controller", "PSX pad",
79 "PSX NegCon", "PSX Analog contoller" };
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_START
, BTN_SELECT
, 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;
210 #define GC_PSX_DELAY 10
211 #define GC_PSX_LENGTH 8 /* talk to the controller in bytes */
213 #define GC_PSX_MOUSE 0x12 /* PSX Mouse */
214 #define GC_PSX_NEGCON 0x23 /* NegCon pad */
215 #define GC_PSX_NORMAL 0x41 /* Standard Digital controller */
216 #define GC_PSX_ANALOGR 0x73 /* Analog controller in Red mode */
217 #define GC_PSX_ANALOGG 0x53 /* Analog controller in Green mode */
219 #define GC_PSX_CLOCK 0x04 /* Pin 3 */
220 #define GC_PSX_COMMAND 0x01 /* Pin 1 */
221 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
222 #define GC_PSX_SELECT 0x02 /* Pin 2 */
223 #define GC_PSX_NOPOWER 0x04
225 static short gc_psx_abs
[] = { ABS_X
, ABS_Y
, ABS_RX
, ABS_RY
, ABS_HAT0X
, ABS_HAT0Y
};
226 static short gc_psx_btn
[] = { BTN_TL
, BTN_TR
, BTN_TL2
, BTN_TR2
, BTN_A
, BTN_B
, BTN_X
, BTN_Y
,
227 BTN_START
, BTN_SELECT
, BTN_THUMB
, BTN_THUMB2
};
230 * gc_psx_command() writes 8bit command and reads 8bit data from
234 static int gc_psx_command(struct gc
*gc
, int b
)
238 cmd
= (b
& 1) ? GC_PSX_COMMAND
: 0;
239 for (i
= 0; i
< 8; i
++) {
240 parport_write_data(gc
->pd
->port
, cmd
| GC_PSX_POWER
);
241 udelay(GC_PSX_DELAY
);
242 ret
|= ((parport_read_status(gc
->pd
->port
) ^ 0x80) & gc
->pads
[GC_PSX
]) ? (1 << i
) : 0;
243 cmd
= (b
& 1) ? GC_PSX_COMMAND
: 0;
244 parport_write_data(gc
->pd
->port
, cmd
| GC_PSX_CLOCK
| GC_PSX_POWER
);
245 udelay(GC_PSX_DELAY
);
252 * gc_psx_read_packet() reads a whole psx packet and returns
253 * device identifier code.
256 static int gc_psx_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
264 parport_write_data(gc
->pd
->port
, GC_PSX_POWER
);
266 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
); /* Select pad */
267 udelay(GC_PSX_DELAY
* 2);
268 gc_psx_command(gc
, 0x01); /* Access pad */
269 ret
= gc_psx_command(gc
, 0x42); /* Get device id */
270 if (gc_psx_command(gc
, 0) == 'Z') /* okay? */
271 for (i
= 0; i
< length
; i
++)
272 data
[i
] = gc_psx_command(gc
, 0);
275 parport_write_data(gc
->pd
->port
, GC_PSX_SELECT
| GC_PSX_CLOCK
| GC_PSX_POWER
);
276 __restore_flags(flags
);
282 * gc_timer() reads and analyzes console pads data.
285 #define GC_MAX_LENGTH GC_N64_LENGTH
287 static void gc_timer(unsigned long private)
289 struct gc
*gc
= (void *) private;
290 struct input_dev
*dev
= gc
->dev
;
291 unsigned char data
[GC_MAX_LENGTH
];
295 * N64 pads - must be read first, any read confuses them for 200 us
298 if (gc
->pads
[GC_N64
]) {
300 gc_n64_read_packet(gc
, data
);
302 for (i
= 0; i
< 5; i
++) {
304 s
= gc_status_bit
[i
];
306 if (s
& gc
->pads
[GC_N64
] & ~(data
[8] | data
[9])) {
309 axes
[0] = axes
[1] = 0;
311 for (j
= 0; j
< 8; j
++) {
312 if (data
[23 - j
] & s
) axes
[0] |= 1 << j
;
313 if (data
[31 - j
] & s
) axes
[1] |= 1 << j
;
316 input_report_abs(dev
+ i
, ABS_X
, axes
[0]);
317 input_report_abs(dev
+ i
, ABS_Y
, -axes
[1]);
319 input_report_abs(dev
+ i
, ABS_HAT0X
, !!(s
& data
[7]) - !!(s
& data
[6]));
320 input_report_abs(dev
+ i
, ABS_HAT0Y
, !!(s
& data
[5]) - !!(s
& data
[4]));
322 for (j
= 0; j
< 10; j
++)
323 input_report_key(dev
+ i
, gc_n64_btn
[j
], s
& data
[gc_n64_bytes
[j
]]);
332 if (gc
->pads
[GC_NES
] || gc
->pads
[GC_SNES
]) {
334 gc_nes_read_packet(gc
, gc
->pads
[GC_SNES
] ? GC_SNES_LENGTH
: GC_NES_LENGTH
, data
);
336 for (i
= 0; i
< 5; i
++) {
338 s
= gc_status_bit
[i
];
340 if (s
& (gc
->pads
[GC_NES
] | gc
->pads
[GC_SNES
])) {
341 input_report_abs(dev
+ i
, ABS_X
, !!(s
& data
[7]) - !!(s
& data
[6]));
342 input_report_abs(dev
+ i
, ABS_Y
, !!(s
& data
[5]) - !!(s
& data
[4]));
345 if (s
& gc
->pads
[GC_NES
])
346 for (j
= 0; j
< 4; j
++)
347 input_report_key(dev
+ i
, gc_snes_btn
[j
], s
& data
[gc_nes_bytes
[j
]]);
349 if (s
& gc
->pads
[GC_SNES
])
350 for (j
= 0; j
< 8; j
++)
351 input_report_key(dev
+ i
, gc_snes_btn
[j
], s
& data
[gc_snes_bytes
[j
]]);
356 * Multi and Multi2 joysticks
359 if (gc
->pads
[GC_MULTI
] || gc
->pads
[GC_MULTI2
]) {
361 gc_multi_read_packet(gc
, gc
->pads
[GC_MULTI2
] ? GC_MULTI2_LENGTH
: GC_MULTI_LENGTH
, data
);
363 for (i
= 0; i
< 5; i
++) {
365 s
= gc_status_bit
[i
];
367 if (s
& (gc
->pads
[GC_MULTI
] | gc
->pads
[GC_MULTI2
])) {
368 input_report_abs(dev
+ i
, ABS_X
, !!(s
& data
[3]) - !!(s
& data
[2]));
369 input_report_abs(dev
+ i
, ABS_Y
, !!(s
& data
[1]) - !!(s
& data
[0]));
370 input_report_key(dev
+ i
, BTN_TRIGGER
, s
& data
[4]);
373 if (s
& gc
->pads
[GC_MULTI2
])
374 input_report_key(dev
+ i
, BTN_THUMB
, s
& data
[5]);
382 if (gc
->pads
[GC_PSX
]) {
384 for (i
= 0; i
< 5; i
++)
385 if (gc
->pads
[GC_PSX
] & gc_status_bit
[i
])
388 switch (gc_psx_read_packet(gc
, 6, data
)) {
392 for (j
= 0; j
< 4; j
++)
393 input_report_abs(dev
+ i
, gc_psx_abs
[j
], data
[j
+ 2]);
395 input_report_abs(dev
+ i
, ABS_HAT0X
, !!(data
[0]&0x20) - !!(data
[0]&0x80));
396 input_report_abs(dev
+ i
, ABS_HAT0Y
, !!(data
[0]&0x40) - !!(data
[0]&0x10));
398 for (j
= 0; j
< 8; j
++)
399 input_report_key(dev
+ i
, gc_psx_btn
[j
], ~data
[1] & (1 << i
));
401 input_report_key(dev
+ i
, BTN_START
, ~data
[0] & 0x08);
402 input_report_key(dev
+ i
, BTN_SELECT
, ~data
[0] & 0x01);
408 input_report_key(dev
+ i
, BTN_THUMB
, ~data
[0] & 0x04);
409 input_report_key(dev
+ i
, BTN_THUMB2
, ~data
[0] & 0x02);
414 input_report_abs(dev
+ i
, ABS_X
, 128 + !!(data
[0] & 0x20) * 127 - !!(data
[0] & 0x80) * 128);
415 input_report_abs(dev
+ i
, ABS_Y
, 128 + !!(data
[0] & 0x40) * 127 - !!(data
[0] & 0x10) * 128);
417 for (j
= 0; j
< 8; j
++)
418 input_report_key(dev
+ i
, gc_psx_btn
[j
], ~data
[1] & (1 << i
));
420 input_report_key(dev
+ i
, BTN_START
, ~data
[0] & 0x08);
421 input_report_key(dev
+ i
, BTN_SELECT
, ~data
[0] & 0x01);
427 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
430 static int gc_open(struct input_dev
*dev
)
432 struct gc
*gc
= dev
->private;
434 parport_claim(gc
->pd
);
435 parport_write_control(gc
->pd
->port
, 0x04);
436 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
441 static void gc_close(struct input_dev
*dev
)
443 struct gc
*gc
= dev
->private;
445 del_timer(&gc
->timer
);
446 parport_write_control(gc
->pd
->port
, 0x00);
447 parport_release(gc
->pd
);
453 static struct gc __init
*gc_probe(int *config
)
458 unsigned char data
[2];
463 for (pp
= parport_enumerate(); pp
&& (config
[0] > 0); pp
= pp
->next
)
467 printk(KERN_ERR
"gamecon.c: no such parport\n");
471 if (!(gc
= kmalloc(sizeof(struct gc
), GFP_KERNEL
)))
473 memset(gc
, 0, sizeof(struct gc
));
475 gc
->pd
= parport_register_device(pp
, "gamecon", NULL
, NULL
, NULL
, PARPORT_DEV_EXCL
, NULL
);
478 printk(KERN_ERR
"gamecon.c: parport busy already - lp.o loaded?\n");
483 parport_claim(gc
->pd
);
485 init_timer(&gc
->timer
);
486 gc
->timer
.data
= (long) gc
;
487 gc
->timer
.function
= gc_timer
;
489 for (i
= 0; i
< 5; i
++) {
494 if (config
[i
+ 1] < 1 || config
[i
+ 1] > GC_MAX
) {
495 printk(KERN_WARNING
"gamecon.c: Pad type %d unknown\n", config
[i
+ 1]);
499 gc
->dev
[i
].private = gc
;
500 gc
->dev
[i
].open
= gc_open
;
501 gc
->dev
[i
].close
= gc_close
;
503 gc
->dev
[i
].evbit
[0] = BIT(EV_KEY
) | BIT(EV_ABS
);
505 for (j
= 0; j
< 2; j
++) {
506 set_bit(ABS_X
+ j
, gc
->dev
[i
].absbit
);
507 gc
->dev
[i
].absmin
[ABS_X
+ j
] = -1;
508 gc
->dev
[i
].absmax
[ABS_X
+ j
] = 1;
511 gc
->pads
[0] |= gc_status_bit
[i
];
512 gc
->pads
[config
[i
+ 1]] |= gc_status_bit
[i
];
514 switch(config
[i
+ 1]) {
517 for (j
= 0; j
< 10; j
++)
518 set_bit(gc_n64_btn
[j
], gc
->dev
[j
].keybit
);
520 for (j
= 0; j
< 2; j
++) {
521 set_bit(ABS_X
+ j
, gc
->dev
[i
].absbit
);
522 gc
->dev
[i
].absmin
[ABS_X
+ j
] = -127;
523 gc
->dev
[i
].absmax
[ABS_X
+ j
] = 126;
524 gc
->dev
[i
].absflat
[ABS_X
+ j
] = 2;
525 set_bit(ABS_HAT0X
+ j
, gc
->dev
[i
].absbit
);
526 gc
->dev
[i
].absmin
[ABS_HAT0X
+ j
] = -1;
527 gc
->dev
[i
].absmax
[ABS_HAT0X
+ j
] = 1;
533 for (j
= 0; j
< 8; j
++)
534 set_bit(gc_snes_btn
[j
], gc
->dev
[j
].keybit
);
538 for (j
= 0; j
< 4; j
++)
539 set_bit(gc_snes_btn
[j
], gc
->dev
[j
].keybit
);
543 set_bit(BTN_THUMB
, gc
->dev
[i
].keybit
);
546 set_bit(BTN_TRIGGER
, gc
->dev
[i
].keybit
);
551 psx
= gc_psx_read_packet(gc
, 2, data
);
564 for (j
= 0; j
< 6; j
++) {
566 set_bit(psx
, gc
->dev
[i
].absbit
);
567 gc
->dev
[i
].absmin
[psx
] = 4;
568 gc
->dev
[i
].absmax
[psx
] = 252;
569 gc
->dev
[i
].absflat
[psx
] = 2;
574 gc
->pads
[GC_PSX
] &= ~gc_status_bit
[i
];
576 printk(KERN_ERR
"gamecon.c: No PSX controller found.\n");
580 gc
->pads
[GC_PSX
] &= ~gc_status_bit
[i
];
582 printk(KERN_WARNING
"gamecon.c: Unsupported PSX controller %#x,"
583 " please report to <vojtech@suse.cz>.\n", psx
);
586 for (j
= 0; j
< pbtn
; j
++)
587 set_bit(gc_psx_btn
[j
], gc
->dev
[i
].keybit
);
592 gc
->dev
[i
].name
= gc_names
[config
[i
+ 1]];
593 gc
->dev
[i
].idbus
= BUS_PARPORT
;
594 gc
->dev
[i
].idvendor
= 0x0001;
595 gc
->dev
[i
].idproduct
= config
[i
+ 1];
596 gc
->dev
[i
].idversion
= 0x0100;
599 parport_release(gc
->pd
);
602 parport_unregister_device(gc
->pd
);
607 for (i
= 0; i
< 5; i
++)
608 if (gc
->pads
[0] & gc_status_bit
[i
]) {
609 input_register_device(gc
->dev
+ i
);
610 printk(KERN_INFO
"input%d: %s on %s\n", gc
->dev
[i
].number
, gc
->dev
[i
].name
, gc
->pd
->port
->name
);
617 int __init
gc_setup(char *str
)
620 get_options(str
, ARRAY_SIZE(ints
), ints
);
621 for (i
= 0; i
<= ints
[0] && i
< 6; i
++) gc
[i
] = ints
[i
+ 1];
624 int __init
gc_setup_2(char *str
)
627 get_options(str
, ARRAY_SIZE(ints
), ints
);
628 for (i
= 0; i
<= ints
[0] && i
< 6; i
++) gc_2
[i
] = ints
[i
+ 1];
631 int __init
gc_setup_3(char *str
)
634 get_options(str
, ARRAY_SIZE(ints
), ints
);
635 for (i
= 0; i
<= ints
[0] && i
< 6; i
++) gc_3
[i
] = ints
[i
+ 1];
638 __setup("gc=", gc_setup
);
639 __setup("gc_2=", gc_setup_2
);
640 __setup("gc_3=", gc_setup_3
);
643 int __init
gc_init(void)
645 gc_base
[0] = gc_probe(gc
);
646 gc_base
[1] = gc_probe(gc_2
);
647 gc_base
[2] = gc_probe(gc_3
);
649 if (gc_base
[0] || gc_base
[1] || gc_base
[2])
655 void __exit
gc_exit(void)
659 for (i
= 0; i
< 3; i
++) {
660 for (j
= 0; j
< 5; j
++)
661 if (gc_base
[i
]->pads
[0] & gc_status_bit
[j
])
662 input_unregister_device(gc_base
[i
]->dev
+ j
);
663 parport_unregister_device(gc_base
[i
]->pd
);
667 module_init(gc_init
);
668 module_exit(gc_exit
);