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 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
35 #include <linux/kernel.h>
36 #include <linux/delay.h>
37 #include <linux/module.h>
38 #include <linux/init.h>
39 #include <linux/parport.h>
40 #include <linux/input.h>
41 #include <linux/mutex.h>
42 #include <linux/slab.h>
44 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
45 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
46 MODULE_LICENSE("GPL");
48 #define GC_MAX_PORTS 3
49 #define GC_MAX_DEVICES 5
52 int args
[GC_MAX_DEVICES
+ 1];
56 static struct gc_config gc_cfg
[GC_MAX_PORTS
];
58 module_param_array_named(map
, gc_cfg
[0].args
, int, &gc_cfg
[0].nargs
, 0);
59 MODULE_PARM_DESC(map
, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
60 module_param_array_named(map2
, gc_cfg
[1].args
, int, &gc_cfg
[1].nargs
, 0);
61 MODULE_PARM_DESC(map2
, "Describes second set of devices");
62 module_param_array_named(map3
, gc_cfg
[2].args
, int, &gc_cfg
[2].nargs
, 0);
63 MODULE_PARM_DESC(map3
, "Describes third set of devices");
65 /* see also gs_psx_delay parameter in PSX support section */
81 #define GC_REFRESH_TIME HZ/100
84 struct input_dev
*dev
;
91 struct gc_pad pads
[GC_MAX_DEVICES
];
92 struct timer_list timer
;
93 int pad_count
[GC_MAX
];
103 static struct gc
*gc_base
[3];
105 static const int gc_status_bit
[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
107 static const char *gc_names
[] = {
108 NULL
, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
109 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
110 "PSX DDR controller", "SNES mouse"
117 static const unsigned char gc_n64_bytes
[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
118 static const short gc_n64_btn
[] = {
119 BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
,
120 BTN_TL
, BTN_TR
, BTN_TRIGGER
, BTN_START
123 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
124 #define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
125 #define GC_N64_CMD_00 0x11111111UL
126 #define GC_N64_CMD_01 0xd1111111UL
127 #define GC_N64_CMD_03 0xdd111111UL
128 #define GC_N64_CMD_1b 0xdd1dd111UL
129 #define GC_N64_CMD_c0 0x111111ddUL
130 #define GC_N64_CMD_80 0x1111111dUL
131 #define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
132 #define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
133 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
134 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
135 /* GC_N64_DWS > 24 is known to fail */
136 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
137 #define GC_N64_POWER_R 0xfd /* power during read */
138 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
139 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
140 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
142 #define GC_N64_CLOCK 0x02 /* clock bits for read */
145 * Used for rumble code.
148 /* Send encoded command */
149 static void gc_n64_send_command(struct gc
*gc
, unsigned long cmd
,
150 unsigned char target
)
152 struct parport
*port
= gc
->pd
->port
;
155 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
156 unsigned char data
= (cmd
>> i
) & 1 ? target
: 0;
157 parport_write_data(port
, GC_N64_POWER_W
| data
);
163 static void gc_n64_send_stop_bit(struct gc
*gc
, unsigned char target
)
165 struct parport
*port
= gc
->pd
->port
;
168 for (i
= 0; i
< GC_N64_STOP_LENGTH
; i
++) {
169 unsigned char data
= (GC_N64_STOP_BIT
>> i
) & 1 ? target
: 0;
170 parport_write_data(port
, GC_N64_POWER_W
| data
);
176 * gc_n64_read_packet() reads an N64 packet.
177 * Each pad uses one bit per byte. So all pads connected to this port
178 * are read in parallel.
181 static void gc_n64_read_packet(struct gc
*gc
, unsigned char *data
)
187 * Request the pad to transmit data
190 local_irq_save(flags
);
191 gc_n64_send_command(gc
, GC_N64_REQUEST_DATA
, GC_N64_OUT
);
192 gc_n64_send_stop_bit(gc
, GC_N64_OUT
);
193 local_irq_restore(flags
);
196 * Wait for the pad response to be loaded into the 33-bit register
200 udelay(GC_N64_DELAY
);
203 * Grab data (ignoring the last bit, which is a stop bit)
206 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
207 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
);
209 data
[i
] = parport_read_status(gc
->pd
->port
);
210 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
| GC_N64_CLOCK
);
214 * We must wait 200 ms here for the controller to reinitialize before
215 * the next read request. No worries as long as gc_read is polled less
216 * frequently than this.
221 static void gc_n64_process_packet(struct gc
*gc
)
223 unsigned char data
[GC_N64_LENGTH
];
224 struct input_dev
*dev
;
228 gc_n64_read_packet(gc
, data
);
230 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
232 if (gc
->pads
[i
].type
!= GC_N64
)
235 dev
= gc
->pads
[i
].dev
;
236 s
= gc_status_bit
[i
];
238 if (s
& ~(data
[8] | data
[9])) {
242 for (j
= 0; j
< 8; j
++) {
243 if (data
[23 - j
] & s
)
245 if (data
[31 - j
] & s
)
249 input_report_abs(dev
, ABS_X
, x
);
250 input_report_abs(dev
, ABS_Y
, -y
);
252 input_report_abs(dev
, ABS_HAT0X
,
253 !(s
& data
[6]) - !(s
& data
[7]));
254 input_report_abs(dev
, ABS_HAT0Y
,
255 !(s
& data
[4]) - !(s
& data
[5]));
257 for (j
= 0; j
< 10; j
++)
258 input_report_key(dev
, gc_n64_btn
[j
],
259 s
& data
[gc_n64_bytes
[j
]]);
266 static int gc_n64_play_effect(struct input_dev
*dev
, void *data
,
267 struct ff_effect
*effect
)
271 struct gc
*gc
= input_get_drvdata(dev
);
272 struct gc_subdev
*sdev
= data
;
273 unsigned char target
= 1 << sdev
->idx
; /* select desired pin */
275 if (effect
->type
== FF_RUMBLE
) {
276 struct ff_rumble_effect
*rumble
= &effect
->u
.rumble
;
278 rumble
->strong_magnitude
|| rumble
->weak_magnitude
?
279 GC_N64_CMD_01
: GC_N64_CMD_00
;
281 local_irq_save(flags
);
283 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
284 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
285 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
286 gc_n64_send_command(gc
, GC_N64_CMD_01
, target
);
287 for (i
= 0; i
< 32; i
++)
288 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
289 gc_n64_send_stop_bit(gc
, target
);
291 udelay(GC_N64_DELAY
);
293 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
294 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
295 gc_n64_send_command(gc
, GC_N64_CMD_c0
, target
);
296 gc_n64_send_command(gc
, GC_N64_CMD_1b
, target
);
297 for (i
= 0; i
< 32; i
++)
298 gc_n64_send_command(gc
, cmd
, target
);
299 gc_n64_send_stop_bit(gc
, target
);
301 local_irq_restore(flags
);
308 static int gc_n64_init_ff(struct input_dev
*dev
, int i
)
310 struct gc_subdev
*sdev
;
313 sdev
= kmalloc(sizeof(*sdev
), GFP_KERNEL
);
319 input_set_capability(dev
, EV_FF
, FF_RUMBLE
);
321 err
= input_ff_create_memless(dev
, sdev
, gc_n64_play_effect
);
334 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
335 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
336 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
337 last 4 bits are unused */
338 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
339 16 bits are equivalent to a gamepad */
341 #define GC_NES_POWER 0xfc
342 #define GC_NES_CLOCK 0x01
343 #define GC_NES_LATCH 0x02
345 static const unsigned char gc_nes_bytes
[] = { 0, 1, 2, 3 };
346 static const unsigned char gc_snes_bytes
[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
347 static const short gc_snes_btn
[] = {
348 BTN_A
, BTN_B
, BTN_SELECT
, BTN_START
, BTN_X
, BTN_Y
, BTN_TL
, BTN_TR
352 * gc_nes_read_packet() reads a NES/SNES packet.
353 * Each pad uses one bit per byte. So all pads connected to
354 * this port are read in parallel.
357 static void gc_nes_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
361 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
| GC_NES_LATCH
);
362 udelay(GC_NES_DELAY
* 2);
363 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
365 for (i
= 0; i
< length
; i
++) {
366 udelay(GC_NES_DELAY
);
367 parport_write_data(gc
->pd
->port
, GC_NES_POWER
);
368 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
369 udelay(GC_NES_DELAY
);
370 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
374 static void gc_nes_process_packet(struct gc
*gc
)
376 unsigned char data
[GC_SNESMOUSE_LENGTH
];
378 struct input_dev
*dev
;
382 len
= gc
->pad_count
[GC_SNESMOUSE
] ? GC_SNESMOUSE_LENGTH
:
383 (gc
->pad_count
[GC_SNES
] ? GC_SNES_LENGTH
: GC_NES_LENGTH
);
385 gc_nes_read_packet(gc
, len
, data
);
387 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
391 s
= gc_status_bit
[i
];
397 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
398 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
400 for (j
= 0; j
< 4; j
++)
401 input_report_key(dev
, gc_snes_btn
[j
],
402 s
& data
[gc_nes_bytes
[j
]]);
408 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
409 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
411 for (j
= 0; j
< 8; j
++)
412 input_report_key(dev
, gc_snes_btn
[j
],
413 s
& data
[gc_snes_bytes
[j
]]);
419 * The 4 unused bits from SNES controllers appear
420 * to be ID bits so use them to make sure we are
421 * dealing with a mouse.
422 * gamepad is connected. This is important since
423 * my SNES gamepad sends 1's for bits 16-31, which
424 * cause the mouse pointer to quickly move to the
425 * upper left corner of the screen.
427 if (!(s
& data
[12]) && !(s
& data
[13]) &&
428 !(s
& data
[14]) && (s
& data
[15])) {
429 input_report_key(dev
, BTN_LEFT
, s
& data
[9]);
430 input_report_key(dev
, BTN_RIGHT
, s
& data
[8]);
433 for (j
= 0; j
< 7; j
++) {
435 if (data
[25 + j
] & s
)
439 if (data
[17 + j
] & s
)
446 input_report_rel(dev
, REL_X
, x_rel
);
452 input_report_rel(dev
, REL_Y
, y_rel
);
466 * Multisystem joystick support
469 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
470 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
473 * gc_multi_read_packet() reads a Multisystem joystick packet.
476 static void gc_multi_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
480 for (i
= 0; i
< length
; i
++) {
481 parport_write_data(gc
->pd
->port
, ~(1 << i
));
482 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
486 static void gc_multi_process_packet(struct gc
*gc
)
488 unsigned char data
[GC_MULTI2_LENGTH
];
489 int data_len
= gc
->pad_count
[GC_MULTI2
] ? GC_MULTI2_LENGTH
: GC_MULTI_LENGTH
;
491 struct input_dev
*dev
;
494 gc_multi_read_packet(gc
, data_len
, data
);
496 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
499 s
= gc_status_bit
[i
];
503 input_report_key(dev
, BTN_THUMB
, s
& data
[5]);
507 input_report_abs(dev
, ABS_X
,
508 !(s
& data
[2]) - !(s
& data
[3]));
509 input_report_abs(dev
, ABS_Y
,
510 !(s
& data
[0]) - !(s
& data
[1]));
511 input_report_key(dev
, BTN_TRIGGER
, s
& data
[4]);
524 * See documentation at:
525 * http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
526 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
530 #define GC_PSX_DELAY 25 /* 25 usec */
531 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
532 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
534 #define GC_PSX_MOUSE 1 /* Mouse */
535 #define GC_PSX_NEGCON 2 /* NegCon */
536 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
537 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
538 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
540 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
541 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
542 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
543 #define GC_PSX_SELECT 0x02 /* Pin 3 */
545 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
546 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
548 static int gc_psx_delay
= GC_PSX_DELAY
;
549 module_param_named(psx_delay
, gc_psx_delay
, uint
, 0);
550 MODULE_PARM_DESC(psx_delay
, "Delay when accessing Sony PSX controller (usecs)");
552 static const short gc_psx_abs
[] = {
553 ABS_X
, ABS_Y
, ABS_RX
, ABS_RY
, ABS_HAT0X
, ABS_HAT0Y
555 static const short gc_psx_btn
[] = {
556 BTN_TL
, BTN_TR
, BTN_TL2
, BTN_TR2
, BTN_A
, BTN_B
, BTN_X
, BTN_Y
,
557 BTN_START
, BTN_SELECT
, BTN_THUMBL
, BTN_THUMBR
559 static const short gc_psx_ddr_btn
[] = { BTN_0
, BTN_1
, BTN_2
, BTN_3
};
562 * gc_psx_command() writes 8bit command and reads 8bit data from
566 static void gc_psx_command(struct gc
*gc
, int b
, unsigned char *data
)
568 struct parport
*port
= gc
->pd
->port
;
571 memset(data
, 0, GC_MAX_DEVICES
);
573 for (i
= 0; i
< GC_PSX_LENGTH
; i
++, b
>>= 1) {
574 cmd
= (b
& 1) ? GC_PSX_COMMAND
: 0;
575 parport_write_data(port
, cmd
| GC_PSX_POWER
);
576 udelay(gc_psx_delay
);
578 read
= parport_read_status(port
) ^ 0x80;
580 for (j
= 0; j
< GC_MAX_DEVICES
; j
++) {
581 struct gc_pad
*pad
= &gc
->pads
[j
];
583 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
584 data
[j
] |= (read
& gc_status_bit
[j
]) ? (1 << i
) : 0;
587 parport_write_data(gc
->pd
->port
, cmd
| GC_PSX_CLOCK
| GC_PSX_POWER
);
588 udelay(gc_psx_delay
);
593 * gc_psx_read_packet() reads a whole psx packet and returns
594 * device identifier code.
597 static void gc_psx_read_packet(struct gc
*gc
,
598 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
],
599 unsigned char id
[GC_MAX_DEVICES
])
601 int i
, j
, max_len
= 0;
603 unsigned char data2
[GC_MAX_DEVICES
];
606 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
607 udelay(gc_psx_delay
);
608 /* Deselect, begin command */
609 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_POWER
);
610 udelay(gc_psx_delay
);
612 local_irq_save(flags
);
614 gc_psx_command(gc
, 0x01, data2
); /* Access pad */
615 gc_psx_command(gc
, 0x42, id
); /* Get device ids */
616 gc_psx_command(gc
, 0, data2
); /* Dump status */
618 /* Find the longest pad */
619 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
620 struct gc_pad
*pad
= &gc
->pads
[i
];
622 if ((pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
) &&
623 GC_PSX_LEN(id
[i
]) > max_len
&&
624 GC_PSX_LEN(id
[i
]) <= GC_PSX_BYTES
) {
625 max_len
= GC_PSX_LEN(id
[i
]);
629 /* Read in all the data */
630 for (i
= 0; i
< max_len
; i
++) {
631 gc_psx_command(gc
, 0, data2
);
632 for (j
= 0; j
< GC_MAX_DEVICES
; j
++)
633 data
[j
][i
] = data2
[j
];
636 local_irq_restore(flags
);
638 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
640 /* Set id's to the real value */
641 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
642 id
[i
] = GC_PSX_ID(id
[i
]);
645 static void gc_psx_report_one(struct gc_pad
*pad
, unsigned char psx_type
,
648 struct input_dev
*dev
= pad
->dev
;
655 input_report_key(dev
, BTN_THUMBL
, ~data
[0] & 0x04);
656 input_report_key(dev
, BTN_THUMBR
, ~data
[0] & 0x02);
662 if (pad
->type
== GC_DDR
) {
663 for (i
= 0; i
< 4; i
++)
664 input_report_key(dev
, gc_psx_ddr_btn
[i
],
665 ~data
[0] & (0x10 << i
));
667 for (i
= 0; i
< 4; i
++)
668 input_report_abs(dev
, gc_psx_abs
[i
+ 2],
671 input_report_abs(dev
, ABS_X
,
672 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
673 input_report_abs(dev
, ABS_Y
,
674 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
677 for (i
= 0; i
< 8; i
++)
678 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
680 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
681 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
689 if (pad
->type
== GC_DDR
) {
690 for (i
= 0; i
< 4; i
++)
691 input_report_key(dev
, gc_psx_ddr_btn
[i
],
692 ~data
[0] & (0x10 << i
));
694 input_report_abs(dev
, ABS_X
,
695 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
696 input_report_abs(dev
, ABS_Y
,
697 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
700 * For some reason if the extra axes are left unset
702 * for (i = 0; i < 4; i++)
703 input_report_abs(dev, gc_psx_abs[i + 2], 128);
704 * This needs to be debugged properly,
705 * maybe fuzz processing needs to be done
711 for (i
= 0; i
< 8; i
++)
712 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
714 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
715 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
721 default: /* not a pad, ignore */
726 static void gc_psx_process_packet(struct gc
*gc
)
728 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
];
729 unsigned char id
[GC_MAX_DEVICES
];
733 gc_psx_read_packet(gc
, data
, id
);
735 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
737 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
738 gc_psx_report_one(pad
, id
[i
], data
[i
]);
743 * gc_timer() initiates reads of console pads data.
746 static void gc_timer(struct timer_list
*t
)
748 struct gc
*gc
= from_timer(gc
, t
, timer
);
751 * N64 pads - must be read first, any read confuses them for 200 us
754 if (gc
->pad_count
[GC_N64
])
755 gc_n64_process_packet(gc
);
758 * NES and SNES pads or mouse
761 if (gc
->pad_count
[GC_NES
] ||
762 gc
->pad_count
[GC_SNES
] ||
763 gc
->pad_count
[GC_SNESMOUSE
]) {
764 gc_nes_process_packet(gc
);
768 * Multi and Multi2 joysticks
771 if (gc
->pad_count
[GC_MULTI
] || gc
->pad_count
[GC_MULTI2
])
772 gc_multi_process_packet(gc
);
778 if (gc
->pad_count
[GC_PSX
] || gc
->pad_count
[GC_DDR
])
779 gc_psx_process_packet(gc
);
781 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
784 static int gc_open(struct input_dev
*dev
)
786 struct gc
*gc
= input_get_drvdata(dev
);
789 err
= mutex_lock_interruptible(&gc
->mutex
);
794 parport_claim(gc
->pd
);
795 parport_write_control(gc
->pd
->port
, 0x04);
796 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
799 mutex_unlock(&gc
->mutex
);
803 static void gc_close(struct input_dev
*dev
)
805 struct gc
*gc
= input_get_drvdata(dev
);
807 mutex_lock(&gc
->mutex
);
809 del_timer_sync(&gc
->timer
);
810 parport_write_control(gc
->pd
->port
, 0x00);
811 parport_release(gc
->pd
);
813 mutex_unlock(&gc
->mutex
);
816 static int gc_setup_pad(struct gc
*gc
, int idx
, int pad_type
)
818 struct gc_pad
*pad
= &gc
->pads
[idx
];
819 struct input_dev
*input_dev
;
823 if (pad_type
< 1 || pad_type
>= GC_MAX
) {
824 pr_err("Pad type %d unknown\n", pad_type
);
828 pad
->dev
= input_dev
= input_allocate_device();
830 pr_err("Not enough memory for input device\n");
834 pad
->type
= pad_type
;
836 snprintf(pad
->phys
, sizeof(pad
->phys
),
837 "%s/input%d", gc
->pd
->port
->name
, idx
);
839 input_dev
->name
= gc_names
[pad_type
];
840 input_dev
->phys
= pad
->phys
;
841 input_dev
->id
.bustype
= BUS_PARPORT
;
842 input_dev
->id
.vendor
= 0x0001;
843 input_dev
->id
.product
= pad_type
;
844 input_dev
->id
.version
= 0x0100;
846 input_set_drvdata(input_dev
, gc
);
848 input_dev
->open
= gc_open
;
849 input_dev
->close
= gc_close
;
851 if (pad_type
!= GC_SNESMOUSE
) {
852 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
854 for (i
= 0; i
< 2; i
++)
855 input_set_abs_params(input_dev
, ABS_X
+ i
, -1, 1, 0, 0);
857 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
859 gc
->pad_count
[pad_type
]++;
864 for (i
= 0; i
< 10; i
++)
865 __set_bit(gc_n64_btn
[i
], input_dev
->keybit
);
867 for (i
= 0; i
< 2; i
++) {
868 input_set_abs_params(input_dev
, ABS_X
+ i
, -127, 126, 0, 2);
869 input_set_abs_params(input_dev
, ABS_HAT0X
+ i
, -1, 1, 0, 0);
872 err
= gc_n64_init_ff(input_dev
, idx
);
874 pr_warn("Failed to initiate rumble for N64 device %d\n",
882 __set_bit(BTN_LEFT
, input_dev
->keybit
);
883 __set_bit(BTN_RIGHT
, input_dev
->keybit
);
884 __set_bit(REL_X
, input_dev
->relbit
);
885 __set_bit(REL_Y
, input_dev
->relbit
);
889 for (i
= 4; i
< 8; i
++)
890 __set_bit(gc_snes_btn
[i
], input_dev
->keybit
);
893 for (i
= 0; i
< 4; i
++)
894 __set_bit(gc_snes_btn
[i
], input_dev
->keybit
);
898 __set_bit(BTN_THUMB
, input_dev
->keybit
);
901 __set_bit(BTN_TRIGGER
, input_dev
->keybit
);
905 for (i
= 0; i
< 6; i
++)
906 input_set_abs_params(input_dev
,
907 gc_psx_abs
[i
], 4, 252, 0, 2);
908 for (i
= 0; i
< 12; i
++)
909 __set_bit(gc_psx_btn
[i
], input_dev
->keybit
);
914 for (i
= 0; i
< 4; i
++)
915 __set_bit(gc_psx_ddr_btn
[i
], input_dev
->keybit
);
916 for (i
= 0; i
< 12; i
++)
917 __set_bit(gc_psx_btn
[i
], input_dev
->keybit
);
922 err
= input_register_device(pad
->dev
);
929 input_free_device(pad
->dev
);
934 static void gc_attach(struct parport
*pp
)
937 struct pardevice
*pd
;
941 struct pardev_cb gc_parport_cb
;
943 for (port_idx
= 0; port_idx
< GC_MAX_PORTS
; port_idx
++) {
944 if (gc_cfg
[port_idx
].nargs
== 0 || gc_cfg
[port_idx
].args
[0] < 0)
947 if (gc_cfg
[port_idx
].args
[0] == pp
->number
)
951 if (port_idx
== GC_MAX_PORTS
) {
952 pr_debug("Not using parport%d.\n", pp
->number
);
955 pads
= gc_cfg
[port_idx
].args
+ 1;
956 n_pads
= gc_cfg
[port_idx
].nargs
- 1;
958 memset(&gc_parport_cb
, 0, sizeof(gc_parport_cb
));
959 gc_parport_cb
.flags
= PARPORT_FLAG_EXCL
;
961 pd
= parport_register_dev_model(pp
, "gamecon", &gc_parport_cb
,
964 pr_err("parport busy already - lp.o loaded?\n");
968 gc
= kzalloc(sizeof(struct gc
), GFP_KERNEL
);
970 pr_err("Not enough memory\n");
971 goto err_unreg_pardev
;
974 mutex_init(&gc
->mutex
);
976 gc
->parportno
= pp
->number
;
977 timer_setup(&gc
->timer
, gc_timer
, 0);
979 for (i
= 0; i
< n_pads
&& i
< GC_MAX_DEVICES
; i
++) {
983 if (gc_setup_pad(gc
, i
, pads
[i
]))
990 pr_err("No valid devices specified\n");
994 gc_base
[port_idx
] = gc
;
1000 input_unregister_device(gc
->pads
[i
].dev
);
1004 parport_unregister_device(pd
);
1007 static void gc_detach(struct parport
*port
)
1012 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
1013 if (gc_base
[i
] && gc_base
[i
]->parportno
== port
->number
)
1017 if (i
== GC_MAX_PORTS
)
1023 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
1024 if (gc
->pads
[i
].dev
)
1025 input_unregister_device(gc
->pads
[i
].dev
);
1026 parport_unregister_device(gc
->pd
);
1030 static struct parport_driver gc_parport_driver
= {
1032 .match_port
= gc_attach
,
1033 .detach
= gc_detach
,
1037 static int __init
gc_init(void)
1042 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
1043 if (gc_cfg
[i
].nargs
== 0 || gc_cfg
[i
].args
[0] < 0)
1046 if (gc_cfg
[i
].nargs
< 2) {
1047 pr_err("at least one device must be specified\n");
1057 return parport_register_driver(&gc_parport_driver
);
1060 static void __exit
gc_exit(void)
1062 parport_unregister_driver(&gc_parport_driver
);
1065 module_init(gc_init
);
1066 module_exit(gc_exit
);