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
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/init.h>
37 #include <linux/parport.h>
38 #include <linux/input.h>
39 #include <linux/mutex.h>
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
43 MODULE_LICENSE("GPL");
45 #define GC_MAX_PORTS 3
46 #define GC_MAX_DEVICES 5
49 int args[GC_MAX_DEVICES + 1];
53 static struct gc_config gc_cfg[GC_MAX_PORTS] __initdata;
55 module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
56 MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
57 module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
58 MODULE_PARM_DESC(map2, "Describes second set of devices");
59 module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
60 MODULE_PARM_DESC(map3, "Describes third set of devices");
62 /* see also gs_psx_delay parameter in PSX support section */
72 #define GC_SNESMOUSE 9
76 #define GC_REFRESH_TIME HZ/100
80 struct input_dev *dev[GC_MAX_DEVICES];
81 struct timer_list timer;
82 unsigned char pads[GC_MAX + 1];
85 char phys[GC_MAX_DEVICES][32];
92 static struct gc *gc_base[3];
94 static int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
96 static char *gc_names[] = {
97 NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
98 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
99 "PSX DDR controller", "SNES mouse"
106 static unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
107 static short gc_n64_btn[] = {
108 BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
109 BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
112 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
113 #define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
114 #define GC_N64_CMD_00 0x11111111UL
115 #define GC_N64_CMD_01 0xd1111111UL
116 #define GC_N64_CMD_03 0xdd111111UL
117 #define GC_N64_CMD_1b 0xdd1dd111UL
118 #define GC_N64_CMD_c0 0x111111ddUL
119 #define GC_N64_CMD_80 0x1111111dUL
120 #define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
121 #define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
122 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
123 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
124 /* GC_N64_DWS > 24 is known to fail */
125 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
126 #define GC_N64_POWER_R 0xfd /* power during read */
127 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
128 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
129 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
131 #define GC_N64_CLOCK 0x02 /* clock bits for read */
134 * Used for rumble code.
137 /* Send encoded command */
138 static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
139 unsigned char target)
141 struct parport *port = gc->pd->port;
144 for (i = 0; i < GC_N64_LENGTH; i++) {
145 unsigned char data = (cmd >> i) & 1 ? target : 0;
146 parport_write_data(port, GC_N64_POWER_W | data);
152 static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
154 struct parport *port = gc->pd->port;
157 for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
158 unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
159 parport_write_data(port, GC_N64_POWER_W | data);
165 * gc_n64_read_packet() reads an N64 packet.
166 * Each pad uses one bit per byte. So all pads connected to this port
167 * are read in parallel.
170 static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
176 * Request the pad to transmit data
179 local_irq_save(flags);
180 gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
181 gc_n64_send_stop_bit(gc, GC_N64_OUT);
182 local_irq_restore(flags);
185 * Wait for the pad response to be loaded into the 33-bit register
189 udelay(GC_N64_DELAY);
192 * Grab data (ignoring the last bit, which is a stop bit)
195 for (i = 0; i < GC_N64_LENGTH; i++) {
196 parport_write_data(gc->pd->port, GC_N64_POWER_R);
198 data[i] = parport_read_status(gc->pd->port);
199 parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
203 * We must wait 200 ms here for the controller to reinitialize before
204 * the next read request. No worries as long as gc_read is polled less
205 * frequently than this.
210 static void gc_n64_process_packet(struct gc *gc)
212 unsigned char data[GC_N64_LENGTH];
213 struct input_dev *dev;
217 gc_n64_read_packet(gc, data);
219 for (i = 0; i < GC_MAX_DEVICES; i++) {
225 s = gc_status_bit[i];
227 if (s & gc->pads[GC_N64] & ~(data[8] | data[9])) {
231 for (j = 0; j < 8; j++) {
232 if (data[23 - j] & s)
234 if (data[31 - j] & s)
238 input_report_abs(dev, ABS_X, x);
239 input_report_abs(dev, ABS_Y, -y);
241 input_report_abs(dev, ABS_HAT0X,
242 !(s & data[6]) - !(s & data[7]));
243 input_report_abs(dev, ABS_HAT0Y,
244 !(s & data[4]) - !(s & data[5]));
246 for (j = 0; j < 10; j++)
247 input_report_key(dev, gc_n64_btn[j],
248 s & data[gc_n64_bytes[j]]);
255 static int gc_n64_play_effect(struct input_dev *dev, void *data,
256 struct ff_effect *effect)
260 struct gc *gc = input_get_drvdata(dev);
261 struct gc_subdev *sdev = data;
262 unsigned char target = 1 << sdev->idx; /* select desired pin */
264 if (effect->type == FF_RUMBLE) {
265 struct ff_rumble_effect *rumble = &effect->u.rumble;
267 rumble->strong_magnitude || rumble->weak_magnitude ?
268 GC_N64_CMD_01 : GC_N64_CMD_00;
270 local_irq_save(flags);
272 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
273 gc_n64_send_command(gc, GC_N64_CMD_03, target);
274 gc_n64_send_command(gc, GC_N64_CMD_80, target);
275 gc_n64_send_command(gc, GC_N64_CMD_01, target);
276 for (i = 0; i < 32; i++)
277 gc_n64_send_command(gc, GC_N64_CMD_80, target);
278 gc_n64_send_stop_bit(gc, target);
280 udelay(GC_N64_DELAY);
282 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
283 gc_n64_send_command(gc, GC_N64_CMD_03, target);
284 gc_n64_send_command(gc, GC_N64_CMD_c0, target);
285 gc_n64_send_command(gc, GC_N64_CMD_1b, target);
286 for (i = 0; i < 32; i++)
287 gc_n64_send_command(gc, cmd, target);
288 gc_n64_send_stop_bit(gc, target);
290 local_irq_restore(flags);
297 static int __init gc_n64_init_ff(struct input_dev *dev, int i)
299 struct gc_subdev *sdev;
302 sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
308 input_set_capability(dev, EV_FF, FF_RUMBLE);
310 err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
323 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
324 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
325 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
326 last 4 bits are unused */
327 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
328 16 bits are equivalent to a gamepad */
330 #define GC_NES_POWER 0xfc
331 #define GC_NES_CLOCK 0x01
332 #define GC_NES_LATCH 0x02
334 static unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
335 static unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
336 static short gc_snes_btn[] = {
337 BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
341 * gc_nes_read_packet() reads a NES/SNES packet.
342 * Each pad uses one bit per byte. So all pads connected to
343 * this port are read in parallel.
346 static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
350 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
351 udelay(GC_NES_DELAY * 2);
352 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
354 for (i = 0; i < length; i++) {
355 udelay(GC_NES_DELAY);
356 parport_write_data(gc->pd->port, GC_NES_POWER);
357 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
358 udelay(GC_NES_DELAY);
359 parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
363 static void gc_nes_process_packet(struct gc *gc)
365 unsigned char data[GC_SNESMOUSE_LENGTH];
366 struct input_dev *dev;
370 len = gc->pads[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
371 (gc->pads[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
373 gc_nes_read_packet(gc, len, data);
375 for (i = 0; i < GC_MAX_DEVICES; i++) {
381 s = gc_status_bit[i];
383 if (s & (gc->pads[GC_NES] | gc->pads[GC_SNES])) {
384 input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
385 input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
388 if (s & gc->pads[GC_NES])
389 for (j = 0; j < 4; j++)
390 input_report_key(dev, gc_snes_btn[j],
391 s & data[gc_nes_bytes[j]]);
393 if (s & gc->pads[GC_SNES])
394 for (j = 0; j < 8; j++)
395 input_report_key(dev, gc_snes_btn[j],
396 s & data[gc_snes_bytes[j]]);
398 if (s & gc->pads[GC_SNESMOUSE]) {
400 * The 4 unused bits from SNES controllers appear
401 * to be ID bits so use them to make sure we are
402 * dealing with a mouse.
403 * gamepad is connected. This is important since
404 * my SNES gamepad sends 1's for bits 16-31, which
405 * cause the mouse pointer to quickly move to the
406 * upper left corner of the screen.
408 if (!(s & data[12]) && !(s & data[13]) &&
409 !(s & data[14]) && (s & data[15])) {
410 input_report_key(dev, BTN_LEFT, s & data[9]);
411 input_report_key(dev, BTN_RIGHT, s & data[8]);
414 for (j = 0; j < 7; j++) {
416 if (data[25 + j] & s)
420 if (data[17 + j] & s)
427 input_report_rel(dev, REL_X, x_rel);
433 input_report_rel(dev, REL_Y, y_rel);
442 * Multisystem joystick support
445 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
446 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
449 * gc_multi_read_packet() reads a Multisystem joystick packet.
452 static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
456 for (i = 0; i < length; i++) {
457 parport_write_data(gc->pd->port, ~(1 << i));
458 data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
462 static void gc_multi_process_packet(struct gc *gc)
464 unsigned char data[GC_MULTI2_LENGTH];
465 int data_len = gc->pads[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
466 struct input_dev *dev;
469 gc_multi_read_packet(gc, data_len, data);
471 for (i = 0; i < GC_MAX_DEVICES; i++) {
477 s = gc_status_bit[i];
479 if (s & (gc->pads[GC_MULTI] | gc->pads[GC_MULTI2])) {
480 input_report_abs(dev, ABS_X,
481 !(s & data[2]) - !(s & data[3]));
482 input_report_abs(dev, ABS_Y,
483 !(s & data[0]) - !(s & data[1]));
484 input_report_key(dev, BTN_TRIGGER, s & data[4]);
487 if (s & gc->pads[GC_MULTI2])
488 input_report_key(dev, BTN_THUMB, s & data[5]);
497 * See documentation at:
498 * http://www.dim.com/~mackys/psxmemcard/ps-eng2.txt
499 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
500 * ftp://milano.usal.es/pablo/
504 #define GC_PSX_DELAY 25 /* 25 usec */
505 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
506 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
508 #define GC_PSX_MOUSE 1 /* Mouse */
509 #define GC_PSX_NEGCON 2 /* NegCon */
510 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
511 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
512 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
514 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
515 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
516 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
517 #define GC_PSX_SELECT 0x02 /* Pin 3 */
519 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
520 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
522 static int gc_psx_delay = GC_PSX_DELAY;
523 module_param_named(psx_delay, gc_psx_delay, uint, 0);
524 MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
526 static short gc_psx_abs[] = {
527 ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
529 static short gc_psx_btn[] = {
530 BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
531 BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
533 static short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
536 * gc_psx_command() writes 8bit command and reads 8bit data from
540 static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
542 struct parport *port = gc->pd->port;
545 memset(data, 0, GC_MAX_DEVICES);
547 for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
548 cmd = (b & 1) ? GC_PSX_COMMAND : 0;
549 parport_write_data(port, cmd | GC_PSX_POWER);
550 udelay(gc_psx_delay);
551 read = parport_read_status(port) ^ 0x80;
552 for (j = 0; j < GC_MAX_DEVICES; j++)
553 data[j] |= (read & gc_status_bit[j] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) ? (1 << i) : 0;
554 parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
555 udelay(gc_psx_delay);
560 * gc_psx_read_packet() reads a whole psx packet and returns
561 * device identifier code.
564 static void gc_psx_read_packet(struct gc *gc, unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
565 unsigned char id[GC_MAX_DEVICES])
567 int i, j, max_len = 0;
569 unsigned char data2[GC_MAX_DEVICES];
572 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
573 udelay(gc_psx_delay);
574 /* Deselect, begin command */
575 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
576 udelay(gc_psx_delay);
578 local_irq_save(flags);
580 gc_psx_command(gc, 0x01, data2); /* Access pad */
581 gc_psx_command(gc, 0x42, id); /* Get device ids */
582 gc_psx_command(gc, 0, data2); /* Dump status */
584 /* Find the longest pad */
585 for (i = 0; i < GC_MAX_DEVICES; i++)
586 if ((gc_status_bit[i] & (gc->pads[GC_PSX] | gc->pads[GC_DDR])) &&
587 GC_PSX_LEN(id[i]) > max_len &&
588 GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
589 max_len = GC_PSX_LEN(id[i]);
592 /* Read in all the data */
593 for (i = 0; i < max_len; i++) {
594 gc_psx_command(gc, 0, data2);
595 for (j = 0; j < GC_MAX_DEVICES; j++)
596 data[j][i] = data2[j];
599 local_irq_restore(flags);
601 parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
603 /* Set id's to the real value */
604 for (i = 0; i < GC_MAX_DEVICES; i++)
605 id[i] = GC_PSX_ID(id[i]);
608 static void gc_psx_report_one(struct gc *gc, struct input_dev *dev,
609 unsigned char pad_type, unsigned char status_bit,
618 input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
619 input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
624 if (gc->pads[GC_DDR] & status_bit) {
625 for (i = 0; i < 4; i++)
626 input_report_key(dev, gc_psx_ddr_btn[i],
627 ~data[0] & (0x10 << i));
629 for (i = 0; i < 4; i++)
630 input_report_abs(dev, gc_psx_abs[i + 2],
633 input_report_abs(dev, ABS_X, 128 + !(data[0] & 0x20) * 127 - !(data[0] & 0x80) * 128);
634 input_report_abs(dev, ABS_Y, 128 + !(data[0] & 0x40) * 127 - !(data[0] & 0x10) * 128);
637 for (i = 0; i < 8; i++)
638 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
640 input_report_key(dev, BTN_START, ~data[0] & 0x08);
641 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
648 if (gc->pads[GC_DDR] & status_bit) {
649 for (i = 0; i < 4; i++)
650 input_report_key(dev, gc_psx_ddr_btn[i],
651 ~data[0] & (0x10 << i));
653 input_report_abs(dev, ABS_X, 128 + !(data[0] & 0x20) * 127 - !(data[0] & 0x80) * 128);
654 input_report_abs(dev, ABS_Y, 128 + !(data[0] & 0x40) * 127 - !(data[0] & 0x10) * 128);
657 * For some reason if the extra axes are left unset
659 * for (i = 0; i < 4; i++)
660 input_report_abs(dev, gc_psx_abs[i + 2], 128);
661 * This needs to be debugged properly,
662 * maybe fuzz processing needs to be done
668 for (i = 0; i < 8; i++)
669 input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
671 input_report_key(dev, BTN_START, ~data[0] & 0x08);
672 input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
678 case 0: /* not a pad, ignore */
683 static void gc_psx_process_packet(struct gc *gc)
685 unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
686 unsigned char id[GC_MAX_DEVICES];
689 gc_psx_read_packet(gc, data, id);
691 for (i = 0; i < GC_MAX_DEVICES; i++) {
694 gc_psx_report_one(gc, gc->dev[i],
695 id[i], gc_status_bit[i], data[i]);
700 * gc_timer() initiates reads of console pads data.
703 static void gc_timer(unsigned long private)
705 struct gc *gc = (void *) private;
708 * N64 pads - must be read first, any read confuses them for 200 us
711 if (gc->pads[GC_N64])
712 gc_n64_process_packet(gc);
715 * NES and SNES pads or mouse
718 if (gc->pads[GC_NES] || gc->pads[GC_SNES] || gc->pads[GC_SNESMOUSE])
719 gc_nes_process_packet(gc);
722 * Multi and Multi2 joysticks
725 if (gc->pads[GC_MULTI] || gc->pads[GC_MULTI2])
726 gc_multi_process_packet(gc);
732 if (gc->pads[GC_PSX] || gc->pads[GC_DDR])
733 gc_psx_process_packet(gc);
735 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
738 static int gc_open(struct input_dev *dev)
740 struct gc *gc = input_get_drvdata(dev);
743 err = mutex_lock_interruptible(&gc->mutex);
748 parport_claim(gc->pd);
749 parport_write_control(gc->pd->port, 0x04);
750 mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
753 mutex_unlock(&gc->mutex);
757 static void gc_close(struct input_dev *dev)
759 struct gc *gc = input_get_drvdata(dev);
761 mutex_lock(&gc->mutex);
763 del_timer_sync(&gc->timer);
764 parport_write_control(gc->pd->port, 0x00);
765 parport_release(gc->pd);
767 mutex_unlock(&gc->mutex);
770 static int __init gc_setup_pad(struct gc *gc, int idx, int pad_type)
772 struct input_dev *input_dev;
779 if (pad_type < 1 || pad_type > GC_MAX) {
780 printk(KERN_WARNING "gamecon.c: Pad type %d unknown\n", pad_type);
784 gc->dev[idx] = input_dev = input_allocate_device();
786 printk(KERN_ERR "gamecon.c: Not enough memory for input device\n");
790 input_dev->name = gc_names[pad_type];
791 input_dev->phys = gc->phys[idx];
792 input_dev->id.bustype = BUS_PARPORT;
793 input_dev->id.vendor = 0x0001;
794 input_dev->id.product = pad_type;
795 input_dev->id.version = 0x0100;
797 input_set_drvdata(input_dev, gc);
799 input_dev->open = gc_open;
800 input_dev->close = gc_close;
802 if (pad_type != GC_SNESMOUSE) {
803 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
805 for (i = 0; i < 2; i++)
806 input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
808 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
810 gc->pads[0] |= gc_status_bit[idx];
811 gc->pads[pad_type] |= gc_status_bit[idx];
816 for (i = 0; i < 10; i++)
817 __set_bit(gc_n64_btn[i], input_dev->keybit);
819 for (i = 0; i < 2; i++) {
820 input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
821 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
824 err = gc_n64_init_ff(input_dev, idx);
826 printk(KERN_WARNING "gamecon.c: Failed to initiate rumble for N64 device %d\n", idx);
827 input_free_device(input_dev);
834 __set_bit(BTN_LEFT, input_dev->keybit);
835 __set_bit(BTN_RIGHT, input_dev->keybit);
836 __set_bit(REL_X, input_dev->relbit);
837 __set_bit(REL_Y, input_dev->relbit);
841 for (i = 4; i < 8; i++)
842 __set_bit(gc_snes_btn[i], input_dev->keybit);
844 for (i = 0; i < 4; i++)
845 __set_bit(gc_snes_btn[i], input_dev->keybit);
849 __set_bit(BTN_THUMB, input_dev->keybit);
851 __set_bit(BTN_TRIGGER, input_dev->keybit);
855 for (i = 0; i < 6; i++)
856 input_set_abs_params(input_dev,
857 gc_psx_abs[i], 4, 252, 0, 2);
858 for (i = 0; i < 12; i++)
859 __set_bit(gc_psx_btn[i], input_dev->keybit);
864 for (i = 0; i < 4; i++)
865 __set_bit(gc_psx_ddr_btn[i], input_dev->keybit);
866 for (i = 0; i < 12; i++)
867 __set_bit(gc_psx_btn[i], input_dev->keybit);
875 static struct gc __init *gc_probe(int parport, int *pads, int n_pads)
879 struct pardevice *pd;
883 pp = parport_find_number(parport);
885 printk(KERN_ERR "gamecon.c: no such parport\n");
890 pd = parport_register_device(pp, "gamecon", NULL, NULL, NULL, PARPORT_DEV_EXCL, NULL);
892 printk(KERN_ERR "gamecon.c: parport busy already - lp.o loaded?\n");
897 gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
899 printk(KERN_ERR "gamecon.c: Not enough memory\n");
901 goto err_unreg_pardev;
904 mutex_init(&gc->mutex);
906 setup_timer(&gc->timer, gc_timer, (long) gc);
908 for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
912 snprintf(gc->phys[i], sizeof(gc->phys[i]),
913 "%s/input%d", gc->pd->port->name, i);
914 err = gc_setup_pad(gc, i, pads[i]);
918 err = input_register_device(gc->dev[i]);
924 printk(KERN_ERR "gamecon.c: No valid devices specified\n");
929 parport_put_port(pp);
933 input_free_device(gc->dev[i]);
937 input_unregister_device(gc->dev[i]);
941 parport_unregister_device(pd);
943 parport_put_port(pp);
948 static void gc_remove(struct gc *gc)
952 for (i = 0; i < GC_MAX_DEVICES; i++)
954 input_unregister_device(gc->dev[i]);
955 parport_unregister_device(gc->pd);
959 static int __init gc_init(void)
965 for (i = 0; i < GC_MAX_PORTS; i++) {
966 if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
969 if (gc_cfg[i].nargs < 2) {
970 printk(KERN_ERR "gamecon.c: at least one device must be specified\n");
975 gc_base[i] = gc_probe(gc_cfg[i].args[0],
976 gc_cfg[i].args + 1, gc_cfg[i].nargs - 1);
977 if (IS_ERR(gc_base[i])) {
978 err = PTR_ERR(gc_base[i]);
988 gc_remove(gc_base[i]);
992 return have_dev ? 0 : -ENODEV;
995 static void __exit gc_exit(void)
999 for (i = 0; i < GC_MAX_PORTS; i++)
1001 gc_remove(gc_base[i]);
1004 module_init(gc_init);
1005 module_exit(gc_exit);