Linux 2.4.0-test7-pre5
[davej-history.git] / drivers / char / joystick / gamecon.c
blobc1d37248f099ae8e3c747727c459e5e569d00619
1 /*
2 * $Id: gamecon.c,v 1.5 2000/06/25 09:56:58 vojtech Exp $
4 * Copyright (c) 1999-2000 Vojtech Pavlik
6 * Based on the work of:
7 * Andree Borrmann John Dahlstrom
8 * David Kuder
10 * Sponsored by SuSE
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");
49 #define GC_SNES 1
50 #define GC_NES 2
51 #define GC_NES4 3
52 #define GC_MULTI 4
53 #define GC_MULTI2 5
54 #define GC_N64 6
55 #define GC_PSX 7
57 #define GC_MAX 7
59 #define GC_REFRESH_TIME HZ/100
61 struct gc {
62 struct pardevice *pd;
63 struct input_dev dev[5];
64 struct timer_list timer;
65 unsigned char pads[GC_PSX + 1];
66 int used;
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" };
81 * N64 support.
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 */
98 /* than 123 us */
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)
108 int i;
109 unsigned long flags;
112 * Request the pad to transmit data
115 __save_flags(flags);
116 __cli();
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));
119 udelay(GC_N64_DWS);
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.
147 * NES/SNES support.
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)
170 int i;
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)
198 int i;
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;
207 * PSX support
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
231 * the psx pad.
234 static int gc_psx_command(struct gc *gc, int b)
236 int i, cmd, ret = 0;
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);
246 b >>= 1;
248 return ret;
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)
258 int i, ret;
259 unsigned long flags;
261 __save_flags(flags);
262 __cli();
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);
273 else ret = -1;
275 parport_write_data(gc->pd->port, GC_PSX_SELECT | GC_PSX_CLOCK | GC_PSX_POWER);
276 __restore_flags(flags);
278 return ret;
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];
292 int i, j, s;
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])) {
308 signed char axes[2];
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]]);
329 * NES and SNES pads
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]);
379 * PSX controllers
382 if (gc->pads[GC_PSX]) {
384 for (i = 0; i < 5; i++)
385 if (gc->pads[GC_PSX] & gc_status_bit[i])
386 break;
388 switch (gc_psx_read_packet(gc, 6, data)) {
390 case GC_PSX_ANALOGG:
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);
404 break;
406 case GC_PSX_ANALOGR:
408 input_report_key(dev + i, BTN_THUMB, ~data[0] & 0x04);
409 input_report_key(dev + i, BTN_THUMB2, ~data[0] & 0x02);
411 case GC_PSX_NORMAL:
412 case GC_PSX_NEGCON:
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);
423 break;
427 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
430 static int gc_open(struct input_dev *dev)
432 struct gc *gc = dev->private;
433 if (!gc->used++) {
434 parport_claim(gc->pd);
435 parport_write_control(gc->pd->port, 0x04);
436 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
438 return 0;
441 static void gc_close(struct input_dev *dev)
443 struct gc *gc = dev->private;
444 if (!--gc->used) {
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)
455 struct gc *gc;
456 struct parport *pp;
457 int i, j, psx, pbtn;
458 unsigned char data[2];
460 if (config[0] < 0)
461 return NULL;
463 for (pp = parport_enumerate(); pp && (config[0] > 0); pp = pp->next)
464 config[0]--;
466 if (!pp) {
467 printk(KERN_ERR "gamecon.c: no such parport\n");
468 return NULL;
471 if (!(gc = kmalloc(sizeof(struct gc), GFP_KERNEL)))
472 return NULL;
473 memset(gc, 0, sizeof(struct gc));
475 gc->pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
477 if (!gc->pd) {
478 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
479 kfree(gc);
480 return NULL;
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++) {
491 if (!config[i + 1])
492 continue;
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]);
496 continue;
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]) {
516 case GC_N64:
517 for (j = 0; j < 10; j++)
518 set_bit(gc_n64_btn[j], gc->dev[i].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;
530 break;
532 case GC_SNES:
533 for (j = 4; j < 8; j++)
534 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
535 case GC_NES:
536 for (j = 0; j < 4; j++)
537 set_bit(gc_snes_btn[j], gc->dev[i].keybit);
538 break;
540 case GC_MULTI2:
541 set_bit(BTN_THUMB, gc->dev[i].keybit);
542 case GC_MULTI:
543 set_bit(BTN_TRIGGER, gc->dev[i].keybit);
544 break;
546 case GC_PSX:
548 psx = gc_psx_read_packet(gc, 2, data);
550 switch(psx) {
551 case GC_PSX_NEGCON:
552 config[i + 1] += 1;
553 case GC_PSX_NORMAL:
554 pbtn = 10;
555 break;
557 case GC_PSX_ANALOGG:
558 case GC_PSX_ANALOGR:
559 config[i + 1] += 2;
560 pbtn = 12;
561 for (j = 0; j < 6; j++) {
562 psx = gc_psx_abs[j];
563 set_bit(psx, gc->dev[i].absbit);
564 gc->dev[i].absmin[psx] = 4;
565 gc->dev[i].absmax[psx] = 252;
566 gc->dev[i].absflat[psx] = 2;
568 break;
570 case -1:
571 gc->pads[GC_PSX] &= ~gc_status_bit[i];
572 pbtn = 0;
573 printk(KERN_ERR "gamecon.c: No PSX controller found.\n");
574 break;
576 default:
577 gc->pads[GC_PSX] &= ~gc_status_bit[i];
578 pbtn = 0;
579 printk(KERN_WARNING "gamecon.c: Unsupported PSX controller %#x,"
580 " please report to <vojtech@suse.cz>.\n", psx);
583 for (j = 0; j < pbtn; j++)
584 set_bit(gc_psx_btn[j], gc->dev[i].keybit);
586 break;
589 gc->dev[i].name = gc_names[config[i + 1]];
590 gc->dev[i].idbus = BUS_PARPORT;
591 gc->dev[i].idvendor = 0x0001;
592 gc->dev[i].idproduct = config[i + 1];
593 gc->dev[i].idversion = 0x0100;
596 parport_release(gc->pd);
598 if (!gc->pads[0]) {
599 parport_unregister_device(gc->pd);
600 kfree(gc);
601 return NULL;
604 for (i = 0; i < 5; i++)
605 if (gc->pads[0] & gc_status_bit[i]) {
606 input_register_device(gc->dev + i);
607 printk(KERN_INFO "input%d: %s on %s\n", gc->dev[i].number, gc->dev[i].name, gc->pd->port->name);
610 return gc;
613 #ifndef MODULE
614 int __init gc_setup(char *str)
616 int i, ints[7];
617 get_options(str, ARRAY_SIZE(ints), ints);
618 for (i = 0; i <= ints[0] && i < 6; i++) gc[i] = ints[i + 1];
619 return 1;
621 int __init gc_setup_2(char *str)
623 int i, ints[7];
624 get_options(str, ARRAY_SIZE(ints), ints);
625 for (i = 0; i <= ints[0] && i < 6; i++) gc_2[i] = ints[i + 1];
626 return 1;
628 int __init gc_setup_3(char *str)
630 int i, ints[7];
631 get_options(str, ARRAY_SIZE(ints), ints);
632 for (i = 0; i <= ints[0] && i < 6; i++) gc_3[i] = ints[i + 1];
633 return 1;
635 __setup("gc=", gc_setup);
636 __setup("gc_2=", gc_setup_2);
637 __setup("gc_3=", gc_setup_3);
638 #endif
640 int __init gc_init(void)
642 gc_base[0] = gc_probe(gc);
643 gc_base[1] = gc_probe(gc_2);
644 gc_base[2] = gc_probe(gc_3);
646 if (gc_base[0] || gc_base[1] || gc_base[2])
647 return 0;
649 return -ENODEV;
652 void __exit gc_exit(void)
654 int i, j;
656 for (i = 0; i < 3; i++)
657 if (gc_base[i]) {
658 for (j = 0; j < 5; j++)
659 if (gc_base[i]->pads[0] & gc_status_bit[j])
660 input_unregister_device(gc_base[i]->dev + j);
661 parport_unregister_device(gc_base[i]->pd);
665 module_init(gc_init);
666 module_exit(gc_exit);