Linux 2.6.19-rc1
[linux-2.6/next.git] / drivers / input / joystick / gamecon.c
blobfe12aa37393d9560c080f823182cd21b93339f8b
1 /*
2 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
4 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
5 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
7 * Based on the work of:
8 * Andree Borrmann John Dahlstrom
9 * David Kuder Nathan Hand
10 * Raphael Assenat
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.
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.
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
28 * Should you need to contact me, the author, you can do so either by
29 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
30 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
33 #include <linux/kernel.h>
34 #include <linux/delay.h>
35 #include <linux/module.h>
36 #include <linux/moduleparam.h>
37 #include <linux/init.h>
38 #include <linux/parport.h>
39 #include <linux/input.h>
40 #include <linux/mutex.h>
42 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
43 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
44 MODULE_LICENSE("GPL");
46 #define GC_MAX_PORTS 3
47 #define GC_MAX_DEVICES 5
49 struct gc_config {
50 int args[GC_MAX_DEVICES + 1];
51 int nargs;
54 static struct gc_config gc[GC_MAX_PORTS] __initdata;
56 module_param_array_named(map, gc[0].args, int, &gc[0].nargs, 0);
57 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
58 module_param_array_named(map2, gc[1].args, int, &gc[1].nargs, 0);
59 MODULE_PARM_DESC(map2, "Describes second set of devices");
60 module_param_array_named(map3, gc[2].args, int, &gc[2].nargs, 0);
61 MODULE_PARM_DESC(map3, "Describes third set of devices");
63 __obsolete_setup("gc=");
64 __obsolete_setup("gc_2=");
65 __obsolete_setup("gc_3=");
67 /* see also gs_psx_delay parameter in PSX support section */
69 #define GC_SNES 1
70 #define GC_NES 2
71 #define GC_NES4 3
72 #define GC_MULTI 4
73 #define GC_MULTI2 5
74 #define GC_N64 6
75 #define GC_PSX 7
76 #define GC_DDR 8
77 #define GC_SNESMOUSE 9
79 #define GC_MAX 9
81 #define GC_REFRESH_TIME HZ/100
83 struct gc {
84 struct pardevice *pd;
85 struct input_dev *dev[GC_MAX_DEVICES];
86 struct timer_list timer;
87 unsigned char pads[GC_MAX + 1];
88 int used;
89 struct mutex mutex;
90 char phys[GC_MAX_DEVICES][32];
93 static struct gc *gc_base[3];
95 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
97 static char *gc_names[] = { NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
98 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
99 "PSX DDR controller", "SNES mouse" };
101 * N64 support.
104 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
105 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 };
107 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
108 #define GC_N64_REQUEST_LENGTH 37 /* transmit request sequence is 9 bits long */
109 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
110 #define GC_N64_REQUEST 0x1dd1111111ULL /* the request data command (encoded for 000000011) */
111 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
112 /* GC_N64_DWS > 24 is known to fail */
113 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
114 #define GC_N64_POWER_R 0xfd /* power during read */
115 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
116 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
117 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
118 /* than 123 us */
119 #define GC_N64_CLOCK 0x02 /* clock bits for read */
122 * gc_n64_read_packet() reads an N64 packet.
123 * Each pad uses one bit per byte. So all pads connected to this port are read in parallel.
126 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
128 int i;
129 unsigned long flags;
132 * Request the pad to transmit data
135 local_irq_save(flags);
136 for (i = 0; i < GC_N64_REQUEST_LENGTH; i++) {
137 parport_write_data(gc->pd->port, GC_N64_POWER_W | ((GC_N64_REQUEST >> i) & 1 ? GC_N64_OUT : 0));
138 udelay(GC_N64_DWS);
140 local_irq_restore(flags);
143 * Wait for the pad response to be loaded into the 33-bit register of the adapter
146 udelay(GC_N64_DELAY);
149 * Grab data (ignoring the last bit, which is a stop bit)
152 for (i = 0; i < GC_N64_LENGTH; i++) {
153 parport_write_data(gc->pd->port, GC_N64_POWER_R);
154 data[i] = parport_read_status(gc->pd->port);
155 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
159 * We must wait 200 ms here for the controller to reinitialize before the next read request.
160 * No worries as long as gc_read is polled less frequently than this.
165 static void gc_n64_process_packet(struct gc *gc)
167 unsigned char data[GC_N64_LENGTH];
168 signed char axes[2];
169 struct input_dev *dev;
170 int i, j, s;
172 gc_n64_read_packet(gc, data);
174 for (i = 0; i < GC_MAX_DEVICES; i++) {
176 dev = gc->dev[i];
177 if (!dev)
178 continue;
180 s = gc_status_bit[i];
182 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
184 axes[0] = axes[1] = 0;
186 for (j = 0; j < 8; j++) {
187 if (data[23 - j] & s)
188 axes[0] |= 1 << j;
189 if (data[31 - j] & s)
190 axes[1] |= 1 << j;
193 input_report_abs(dev, ABS_X, axes[0]);
194 input_report_abs(dev, ABS_Y, -axes[1]);
196 input_report_abs(dev, ABS_HAT0X, !(s & data[6]) - !(s & data[7]));
197 input_report_abs(dev, ABS_HAT0Y, !(s & data[4]) - !(s & data[5]));
199 for (j = 0; j < 10; j++)
200 input_report_key(dev, gc_n64_btn[j], s & data[gc_n64_bytes[j]]);
202 input_sync(dev);
208 * NES/SNES support.
211 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
212 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
213 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
214 last 4 bits are unused */
215 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
216 16 bits are equivalent to a gamepad */
218 #define GC_NES_POWER 0xfc
219 #define GC_NES_CLOCK 0x01
220 #define GC_NES_LATCH 0x02
222 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
223 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
224 static short gc_snes_btn[] = { BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR };
227 * gc_nes_read_packet() reads a NES/SNES packet.
228 * Each pad uses one bit per byte. So all pads connected to
229 * this port are read in parallel.
232 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
234 int i;
236 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
237 udelay(GC_NES_DELAY * 2);
238 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
240 for (i = 0; i < length; i++) {
241 udelay(GC_NES_DELAY);
242 parport_write_data(gc->pd->port, GC_NES_POWER);
243 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
244 udelay(GC_NES_DELAY);
245 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
249 static void gc_nes_process_packet(struct gc *gc)
251 unsigned char data[GC_SNESMOUSE_LENGTH];
252 struct input_dev *dev;
253 int i, j, s, len;
254 char x_rel, y_rel;
256 len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
257 (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
259 gc_nes_read_packet(gc, len, data);
261 for (i = 0; i < GC_MAX_DEVICES; i++) {
263 dev = gc->dev[i];
264 if (!dev)
265 continue;
267 s = gc_status_bit[i];
269 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
270 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
271 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
274 if (s & gc->pads[GC_NES])
275 for (j = 0; j < 4; j++)
276 input_report_key(dev, gc_snes_btn[j], s & data[gc_nes_bytes[j]]);
278 if (s & gc->pads[GC_SNES])
279 for (j = 0; j < 8; j++)
280 input_report_key(dev, gc_snes_btn[j], s & data[gc_snes_bytes[j]]);
282 if (s & gc->pads[GC_SNESMOUSE]) {
284 * The 4 unused bits from SNES controllers appear to be ID bits
285 * so use them to make sure iwe are dealing with a mouse.
286 * gamepad is connected. This is important since
287 * my SNES gamepad sends 1's for bits 16-31, which
288 * cause the mouse pointer to quickly move to the
289 * upper left corner of the screen.
291 if (!(s & data[12]) && !(s & data[13]) &&
292 !(s & data[14]) && (s & data[15])) {
293 input_report_key(dev, BTN_LEFT, s & data[9]);
294 input_report_key(dev, BTN_RIGHT, s & data[8]);
296 x_rel = y_rel = 0;
297 for (j = 0; j < 7; j++) {
298 x_rel <<= 1;
299 if (data[25 + j] & s)
300 x_rel |= 1;
302 y_rel <<= 1;
303 if (data[17 + j] & s)
304 y_rel |= 1;
307 if (x_rel) {
308 if (data[24] & s)
309 x_rel = -x_rel;
310 input_report_rel(dev, REL_X, x_rel);
313 if (y_rel) {
314 if (data[16] & s)
315 y_rel = -y_rel;
316 input_report_rel(dev, REL_Y, y_rel);
320 input_sync(dev);
325 * Multisystem joystick support
328 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
329 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
332 * gc_multi_read_packet() reads a Multisystem joystick packet.
335 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
337 int i;
339 for (i = 0; i < length; i++) {
340 parport_write_data(gc->pd->port, ~(1 << i));
341 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
345 static void gc_multi_process_packet(struct gc *gc)
347 unsigned char data[GC_MULTI2_LENGTH];
348 struct input_dev *dev;
349 int i, s;
351 gc_multi_read_packet(gc, gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH, data);
353 for (i = 0; i < GC_MAX_DEVICES; i++) {
355 dev = gc->dev[i];
356 if (!dev)
357 continue;
359 s = gc_status_bit[i];
361 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
362 input_report_abs(dev, ABS_X, !(s & data[2]) - !(s & data[3]));
363 input_report_abs(dev, ABS_Y, !(s & data[0]) - !(s & data[1]));
364 input_report_key(dev, BTN_TRIGGER, s & data[4]);
367 if (s & gc->pads[GC_MULTI2])
368 input_report_key(dev, BTN_THUMB, s & data[5]);
370 input_sync(dev);
375 * PSX support
377 * See documentation at:
378 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
379 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
380 * ftp://milano.usal.es/pablo/
384 #define GC_PSX_DELAY 25 /* 25 usec */
385 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
386 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
388 #define GC_PSX_MOUSE 1 /* Mouse */
389 #define GC_PSX_NEGCON 2 /* NegCon */
390 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
391 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
392 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
394 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
395 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
396 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
397 #define GC_PSX_SELECT 0x02 /* Pin 3 */
399 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
400 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
402 static int gc_psx_delay = GC_PSX_DELAY;
403 module_param_named(psx_delay, gc_psx_delay, uint, 0);
404 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
406 __obsolete_setup("gc_psx_delay=");
408 static short gc_psx_abs[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y };
409 static short gc_psx_btn[] = { BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
410 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR };
411 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
414 * gc_psx_command() writes 8bit command and reads 8bit data from
415 * the psx pad.
418 static void gc_psx_command(struct gc *gc, int b, unsigned char data[GC_MAX_DEVICES])
420 int i, j, cmd, read;
422 for (i = 0; i < GC_MAX_DEVICES; i++)
423 data[i] = 0;
425 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
426 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
427 parport_write_data(gc->pd->port, cmd | GC_PSX_POWER);
428 udelay(gc_psx_delay);
429 read = parport_read_status(gc->pd->port) ^ 0x80;
430 for (j = 0; j < GC_MAX_DEVICES; j++)
431 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
432 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
433 udelay(gc_psx_delay);
438 * gc_psx_read_packet() reads a whole psx packet and returns
439 * device identifier code.
442 static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
443 unsigned char id[GC_MAX_DEVICES])
445 int i, j, max_len = 0;
446 unsigned long flags;
447 unsigned char data2[GC_MAX_DEVICES];
449 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER); /* Select pad */
450 udelay(gc_psx_delay);
451 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER); /* Deselect, begin command */
452 udelay(gc_psx_delay);
454 local_irq_save(flags);
456 gc_psx_command(gc, 0x01, data2); /* Access pad */
457 gc_psx_command(gc, 0x42, id); /* Get device ids */
458 gc_psx_command(gc, 0, data2); /* Dump status */
460 for (i =0; i < GC_MAX_DEVICES; i++) /* Find the longest pad */
461 if((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR]))
462 && (GC_PSX_LEN(id[i]) > max_len)
463 && (GC_PSX_LEN(id[i]) <= GC_PSX_BYTES))
464 max_len = GC_PSX_LEN(id[i]);
466 for (i = 0; i < max_len; i++) { /* Read in all the data */
467 gc_psx_command(gc, 0, data2);
468 for (j = 0; j < GC_MAX_DEVICES; j++)
469 data[j][i] = data2[j];
472 local_irq_restore(flags);
474 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
476 for(i = 0; i < GC_MAX_DEVICES; i++) /* Set id's to the real value */
477 id[i] = GC_PSX_ID(id[i]);
480 static void gc_psx_process_packet(struct gc *gc)
482 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
483 unsigned char id[GC_MAX_DEVICES];
484 struct input_dev *dev;
485 int i, j;
487 gc_psx_read_packet(gc, data, id);
489 for (i = 0; i < GC_MAX_DEVICES; i++) {
491 dev = gc->dev[i];
492 if (!dev)
493 continue;
495 switch (id[i]) {
497 case GC_PSX_RUMBLE:
499 input_report_key(dev, BTN_THUMBL, ~data[i][0] & 0x04);
500 input_report_key(dev, BTN_THUMBR, ~data[i][0] & 0x02);
502 case GC_PSX_NEGCON:
503 case GC_PSX_ANALOG:
505 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
506 for(j = 0; j < 4; j++)
507 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
508 } else {
509 for (j = 0; j < 4; j++)
510 input_report_abs(dev, gc_psx_abs[j + 2], data[i][j + 2]);
512 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
513 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
516 for (j = 0; j < 8; j++)
517 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
519 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
520 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
522 input_sync(dev);
524 break;
526 case GC_PSX_NORMAL:
527 if (gc->pads[GC_DDR] & gc_status_bit[i]) {
528 for(j = 0; j < 4; j++)
529 input_report_key(dev, gc_psx_ddr_btn[j], ~data[i][0] & (0x10 << j));
530 } else {
531 input_report_abs(dev, ABS_X, 128 + !(data[i][0] & 0x20) * 127 - !(data[i][0] & 0x80) * 128);
532 input_report_abs(dev, ABS_Y, 128 + !(data[i][0] & 0x40) * 127 - !(data[i][0] & 0x10) * 128);
534 /* for some reason if the extra axes are left unset they drift */
535 /* for (j = 0; j < 4; j++)
536 input_report_abs(dev, gc_psx_abs[j + 2], 128);
537 * This needs to be debugged properly,
538 * maybe fuzz processing needs to be done in input_sync()
539 * --vojtech
543 for (j = 0; j < 8; j++)
544 input_report_key(dev, gc_psx_btn[j], ~data[i][1] & (1 << j));
546 input_report_key(dev, BTN_START, ~data[i][0] & 0x08);
547 input_report_key(dev, BTN_SELECT, ~data[i][0] & 0x01);
549 input_sync(dev);
551 break;
553 case 0: /* not a pad, ignore */
554 break;
560 * gc_timer() initiates reads of console pads data.
563 static void gc_timer(unsigned long private)
565 struct gc *gc = (void *) private;
568 * N64 pads - must be read first, any read confuses them for 200 us
571 if (gc->pads[GC_N64])
572 gc_n64_process_packet(gc);
575 * NES and SNES pads or mouse
578 if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE])
579 gc_nes_process_packet(gc);
582 * Multi and Multi2 joysticks
585 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
586 gc_multi_process_packet(gc);
589 * PSX controllers
592 if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
593 gc_psx_process_packet(gc);
595 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
598 static int gc_open(struct input_dev *dev)
600 struct gc *gc = dev->private;
601 int err;
603 err = mutex_lock_interruptible(&gc->mutex);
604 if (err)
605 return err;
607 if (!gc->used++) {
608 parport_claim(gc->pd);
609 parport_write_control(gc->pd->port, 0x04);
610 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
613 mutex_unlock(&gc->mutex);
614 return 0;
617 static void gc_close(struct input_dev *dev)
619 struct gc *gc = dev->private;
621 mutex_lock(&gc->mutex);
622 if (!--gc->used) {
623 del_timer_sync(&gc->timer);
624 parport_write_control(gc->pd->port, 0x00);
625 parport_release(gc->pd);
627 mutex_unlock(&gc->mutex);
630 static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
632 struct input_dev *input_dev;
633 int i;
635 if (!pad_type)
636 return 0;
638 if (pad_type < 1 || pad_type > GC_MAX) {
639 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
640 return -EINVAL;
643 gc->dev[idx] = input_dev = input_allocate_device();
644 if (!input_dev) {
645 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
646 return -ENOMEM;
649 input_dev->name = gc_names[pad_type];
650 input_dev->phys = gc->phys[idx];
651 input_dev->id.bustype = BUS_PARPORT;
652 input_dev->id.vendor = 0x0001;
653 input_dev->id.product = pad_type;
654 input_dev->id.version = 0x0100;
655 input_dev->private = gc;
657 input_dev->open = gc_open;
658 input_dev->close = gc_close;
660 if (pad_type != GC_SNESMOUSE) {
661 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
663 for (i = 0; i < 2; i++)
664 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
665 } else
666 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_REL);
668 gc->pads[0] |= gc_status_bit[idx];
669 gc->pads[pad_type] |= gc_status_bit[idx];
671 switch (pad_type) {
673 case GC_N64:
674 for (i = 0; i < 10; i++)
675 set_bit(gc_n64_btn[i], input_dev->keybit);
677 for (i = 0; i < 2; i++) {
678 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
679 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
682 break;
684 case GC_SNESMOUSE:
685 set_bit(BTN_LEFT, input_dev->keybit);
686 set_bit(BTN_RIGHT, input_dev->keybit);
687 set_bit(REL_X, input_dev->relbit);
688 set_bit(REL_Y, input_dev->relbit);
689 break;
691 case GC_SNES:
692 for (i = 4; i < 8; i++)
693 set_bit(gc_snes_btn[i], input_dev->keybit);
694 case GC_NES:
695 for (i = 0; i < 4; i++)
696 set_bit(gc_snes_btn[i], input_dev->keybit);
697 break;
699 case GC_MULTI2:
700 set_bit(BTN_THUMB, input_dev->keybit);
701 case GC_MULTI:
702 set_bit(BTN_TRIGGER, input_dev->keybit);
703 break;
705 case GC_PSX:
706 for (i = 0; i < 6; i++)
707 input_set_abs_params(input_dev, gc_psx_abs[i], 4, 252, 0, 2);
708 for (i = 0; i < 12; i++)
709 set_bit(gc_psx_btn[i], input_dev->keybit);
711 break;
713 case GC_DDR:
714 for (i = 0; i < 4; i++)
715 set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
716 for (i = 0; i < 12; i++)
717 set_bit(gc_psx_btn[i], input_dev->keybit);
719 break;
722 return 0;
725 static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
727 struct gc *gc;
728 struct parport *pp;
729 struct pardevice *pd;
730 int i;
731 int err;
733 pp = parport_find_number(parport);
734 if (!pp) {
735 printk(KERN_ERR "gamecon.c: no such parport\n");
736 err = -EINVAL;
737 goto err_out;
740 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
741 if (!pd) {
742 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
743 err = -EBUSY;
744 goto err_put_pp;
747 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
748 if (!gc) {
749 printk(KERN_ERR "gamecon.c: Not enough memory\n");
750 err = -ENOMEM;
751 goto err_unreg_pardev;
754 mutex_init(&gc->mutex);
755 gc->pd = pd;
756 init_timer(&gc->timer);
757 gc->timer.data = (long) gc;
758 gc->timer.function = gc_timer;
760 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
761 if (!pads[i])
762 continue;
764 snprintf(gc->phys[i], sizeof(gc->phys[i]),
765 "%s/input%d", gc->pd->port->name, i);
766 err = gc_setup_pad(gc, i, pads[i]);
767 if (err)
768 goto err_unreg_devs;
770 err = input_register_device(gc->dev[i]);
771 if (err)
772 goto err_free_dev;
775 if (!gc->pads[0]) {
776 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
777 err = -EINVAL;
778 goto err_free_gc;
781 parport_put_port(pp);
782 return gc;
784 err_free_dev:
785 input_free_device(gc->dev[i]);
786 err_unreg_devs:
787 while (--i >= 0)
788 if (gc->dev[i])
789 input_unregister_device(gc->dev[i]);
790 err_free_gc:
791 kfree(gc);
792 err_unreg_pardev:
793 parport_unregister_device(pd);
794 err_put_pp:
795 parport_put_port(pp);
796 err_out:
797 return ERR_PTR(err);
800 static void gc_remove(struct gc *gc)
802 int i;
804 for (i = 0; i < GC_MAX_DEVICES; i++)
805 if (gc->dev[i])
806 input_unregister_device(gc->dev[i]);
807 parport_unregister_device(gc->pd);
808 kfree(gc);
811 static int __init gc_init(void)
813 int i;
814 int have_dev = 0;
815 int err = 0;
817 for (i = 0; i < GC_MAX_PORTS; i++) {
818 if (gc[i].nargs == 0 || gc[i].args[0] < 0)
819 continue;
821 if (gc[i].nargs < 2) {
822 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
823 err = -EINVAL;
824 break;
827 gc_base[i] = gc_probe(gc[i].args[0], gc[i].args + 1, gc[i].nargs - 1);
828 if (IS_ERR(gc_base[i])) {
829 err = PTR_ERR(gc_base[i]);
830 break;
833 have_dev = 1;
836 if (err) {
837 while (--i >= 0)
838 if (gc_base[i])
839 gc_remove(gc_base[i]);
840 return err;
843 return have_dev ? 0 : -ENODEV;
846 static void __exit gc_exit(void)
848 int i;
850 for (i = 0; i < GC_MAX_PORTS; i++)
851 if (gc_base[i])
852 gc_remove(gc_base[i]);
855 module_init(gc_init);
856 module_exit(gc_exit);