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
29 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
31 #include <linux/kernel.h>
32 #include <linux/delay.h>
33 #include <linux/module.h>
34 #include <linux/init.h>
35 #include <linux/parport.h>
36 #include <linux/input.h>
37 #include <linux/mutex.h>
38 #include <linux/slab.h>
40 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
41 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
42 MODULE_LICENSE("GPL");
44 #define GC_MAX_PORTS 3
45 #define GC_MAX_DEVICES 5
48 int args
[GC_MAX_DEVICES
+ 1];
52 static struct gc_config gc_cfg
[GC_MAX_PORTS
];
54 module_param_array_named(map
, gc_cfg
[0].args
, int, &gc_cfg
[0].nargs
, 0);
55 MODULE_PARM_DESC(map
, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
56 module_param_array_named(map2
, gc_cfg
[1].args
, int, &gc_cfg
[1].nargs
, 0);
57 MODULE_PARM_DESC(map2
, "Describes second set of devices");
58 module_param_array_named(map3
, gc_cfg
[2].args
, int, &gc_cfg
[2].nargs
, 0);
59 MODULE_PARM_DESC(map3
, "Describes third set of devices");
61 /* see also gs_psx_delay parameter in PSX support section */
77 #define GC_REFRESH_TIME HZ/100
80 struct input_dev
*dev
;
87 struct gc_pad pads
[GC_MAX_DEVICES
];
88 struct timer_list timer
;
89 int pad_count
[GC_MAX
];
99 static struct gc
*gc_base
[3];
101 static const int gc_status_bit
[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
103 static const char *gc_names
[] = {
104 NULL
, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
105 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
106 "PSX DDR controller", "SNES mouse"
113 static const unsigned char gc_n64_bytes
[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
114 static const short gc_n64_btn
[] = {
115 BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
,
116 BTN_TL
, BTN_TR
, BTN_TRIGGER
, BTN_START
119 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
120 #define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
121 #define GC_N64_CMD_00 0x11111111UL
122 #define GC_N64_CMD_01 0xd1111111UL
123 #define GC_N64_CMD_03 0xdd111111UL
124 #define GC_N64_CMD_1b 0xdd1dd111UL
125 #define GC_N64_CMD_c0 0x111111ddUL
126 #define GC_N64_CMD_80 0x1111111dUL
127 #define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
128 #define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
129 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
130 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
131 /* GC_N64_DWS > 24 is known to fail */
132 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
133 #define GC_N64_POWER_R 0xfd /* power during read */
134 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
135 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
136 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
138 #define GC_N64_CLOCK 0x02 /* clock bits for read */
141 * Used for rumble code.
144 /* Send encoded command */
145 static void gc_n64_send_command(struct gc
*gc
, unsigned long cmd
,
146 unsigned char target
)
148 struct parport
*port
= gc
->pd
->port
;
151 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
152 unsigned char data
= (cmd
>> i
) & 1 ? target
: 0;
153 parport_write_data(port
, GC_N64_POWER_W
| data
);
159 static void gc_n64_send_stop_bit(struct gc
*gc
, unsigned char target
)
161 struct parport
*port
= gc
->pd
->port
;
164 for (i
= 0; i
< GC_N64_STOP_LENGTH
; i
++) {
165 unsigned char data
= (GC_N64_STOP_BIT
>> i
) & 1 ? target
: 0;
166 parport_write_data(port
, GC_N64_POWER_W
| data
);
172 * gc_n64_read_packet() reads an N64 packet.
173 * Each pad uses one bit per byte. So all pads connected to this port
174 * are read in parallel.
177 static void gc_n64_read_packet(struct gc
*gc
, unsigned char *data
)
183 * Request the pad to transmit data
186 local_irq_save(flags
);
187 gc_n64_send_command(gc
, GC_N64_REQUEST_DATA
, GC_N64_OUT
);
188 gc_n64_send_stop_bit(gc
, GC_N64_OUT
);
189 local_irq_restore(flags
);
192 * Wait for the pad response to be loaded into the 33-bit register
196 udelay(GC_N64_DELAY
);
199 * Grab data (ignoring the last bit, which is a stop bit)
202 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
203 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
);
205 data
[i
] = parport_read_status(gc
->pd
->port
);
206 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
| GC_N64_CLOCK
);
210 * We must wait 200 ms here for the controller to reinitialize before
211 * the next read request. No worries as long as gc_read is polled less
212 * frequently than this.
217 static void gc_n64_process_packet(struct gc
*gc
)
219 unsigned char data
[GC_N64_LENGTH
];
220 struct input_dev
*dev
;
224 gc_n64_read_packet(gc
, data
);
226 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
228 if (gc
->pads
[i
].type
!= GC_N64
)
231 dev
= gc
->pads
[i
].dev
;
232 s
= gc_status_bit
[i
];
234 if (s
& ~(data
[8] | data
[9])) {
238 for (j
= 0; j
< 8; j
++) {
239 if (data
[23 - j
] & s
)
241 if (data
[31 - j
] & s
)
245 input_report_abs(dev
, ABS_X
, x
);
246 input_report_abs(dev
, ABS_Y
, -y
);
248 input_report_abs(dev
, ABS_HAT0X
,
249 !(s
& data
[6]) - !(s
& data
[7]));
250 input_report_abs(dev
, ABS_HAT0Y
,
251 !(s
& data
[4]) - !(s
& data
[5]));
253 for (j
= 0; j
< 10; j
++)
254 input_report_key(dev
, gc_n64_btn
[j
],
255 s
& data
[gc_n64_bytes
[j
]]);
262 static int gc_n64_play_effect(struct input_dev
*dev
, void *data
,
263 struct ff_effect
*effect
)
267 struct gc
*gc
= input_get_drvdata(dev
);
268 struct gc_subdev
*sdev
= data
;
269 unsigned char target
= 1 << sdev
->idx
; /* select desired pin */
271 if (effect
->type
== FF_RUMBLE
) {
272 struct ff_rumble_effect
*rumble
= &effect
->u
.rumble
;
274 rumble
->strong_magnitude
|| rumble
->weak_magnitude
?
275 GC_N64_CMD_01
: GC_N64_CMD_00
;
277 local_irq_save(flags
);
279 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
280 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
281 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
282 gc_n64_send_command(gc
, GC_N64_CMD_01
, target
);
283 for (i
= 0; i
< 32; i
++)
284 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
285 gc_n64_send_stop_bit(gc
, target
);
287 udelay(GC_N64_DELAY
);
289 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
290 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
291 gc_n64_send_command(gc
, GC_N64_CMD_c0
, target
);
292 gc_n64_send_command(gc
, GC_N64_CMD_1b
, target
);
293 for (i
= 0; i
< 32; i
++)
294 gc_n64_send_command(gc
, cmd
, target
);
295 gc_n64_send_stop_bit(gc
, target
);
297 local_irq_restore(flags
);
304 static int gc_n64_init_ff(struct input_dev
*dev
, int i
)
306 struct gc_subdev
*sdev
;
309 sdev
= kmalloc(sizeof(*sdev
), GFP_KERNEL
);
315 input_set_capability(dev
, EV_FF
, FF_RUMBLE
);
317 err
= input_ff_create_memless(dev
, sdev
, gc_n64_play_effect
);
330 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
331 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
332 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
333 last 4 bits are unused */
334 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
335 16 bits are equivalent to a gamepad */
337 #define GC_NES_POWER 0xfc
338 #define GC_NES_CLOCK 0x01
339 #define GC_NES_LATCH 0x02
341 static const unsigned char gc_nes_bytes
[] = { 0, 1, 2, 3 };
342 static const unsigned char gc_snes_bytes
[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
343 static const short gc_snes_btn
[] = {
344 BTN_A
, BTN_B
, BTN_SELECT
, BTN_START
, BTN_X
, BTN_Y
, BTN_TL
, BTN_TR
348 * gc_nes_read_packet() reads a NES/SNES packet.
349 * Each pad uses one bit per byte. So all pads connected to
350 * this port are read in parallel.
353 static void gc_nes_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
357 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
| GC_NES_LATCH
);
358 udelay(GC_NES_DELAY
* 2);
359 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
361 for (i
= 0; i
< length
; i
++) {
362 udelay(GC_NES_DELAY
);
363 parport_write_data(gc
->pd
->port
, GC_NES_POWER
);
364 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
365 udelay(GC_NES_DELAY
);
366 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
370 static void gc_nes_process_packet(struct gc
*gc
)
372 unsigned char data
[GC_SNESMOUSE_LENGTH
];
374 struct input_dev
*dev
;
378 len
= gc
->pad_count
[GC_SNESMOUSE
] ? GC_SNESMOUSE_LENGTH
:
379 (gc
->pad_count
[GC_SNES
] ? GC_SNES_LENGTH
: GC_NES_LENGTH
);
381 gc_nes_read_packet(gc
, len
, data
);
383 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
387 s
= gc_status_bit
[i
];
393 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
394 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
396 for (j
= 0; j
< 4; j
++)
397 input_report_key(dev
, gc_snes_btn
[j
],
398 s
& data
[gc_nes_bytes
[j
]]);
404 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
405 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
407 for (j
= 0; j
< 8; j
++)
408 input_report_key(dev
, gc_snes_btn
[j
],
409 s
& data
[gc_snes_bytes
[j
]]);
415 * The 4 unused bits from SNES controllers appear
416 * to be ID bits so use them to make sure we are
417 * dealing with a mouse.
418 * gamepad is connected. This is important since
419 * my SNES gamepad sends 1's for bits 16-31, which
420 * cause the mouse pointer to quickly move to the
421 * upper left corner of the screen.
423 if (!(s
& data
[12]) && !(s
& data
[13]) &&
424 !(s
& data
[14]) && (s
& data
[15])) {
425 input_report_key(dev
, BTN_LEFT
, s
& data
[9]);
426 input_report_key(dev
, BTN_RIGHT
, s
& data
[8]);
429 for (j
= 0; j
< 7; j
++) {
431 if (data
[25 + j
] & s
)
435 if (data
[17 + j
] & s
)
442 input_report_rel(dev
, REL_X
, x_rel
);
448 input_report_rel(dev
, REL_Y
, y_rel
);
462 * Multisystem joystick support
465 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
466 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
469 * gc_multi_read_packet() reads a Multisystem joystick packet.
472 static void gc_multi_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
476 for (i
= 0; i
< length
; i
++) {
477 parport_write_data(gc
->pd
->port
, ~(1 << i
));
478 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
482 static void gc_multi_process_packet(struct gc
*gc
)
484 unsigned char data
[GC_MULTI2_LENGTH
];
485 int data_len
= gc
->pad_count
[GC_MULTI2
] ? GC_MULTI2_LENGTH
: GC_MULTI_LENGTH
;
487 struct input_dev
*dev
;
490 gc_multi_read_packet(gc
, data_len
, data
);
492 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
495 s
= gc_status_bit
[i
];
499 input_report_key(dev
, BTN_THUMB
, s
& data
[5]);
503 input_report_abs(dev
, ABS_X
,
504 !(s
& data
[2]) - !(s
& data
[3]));
505 input_report_abs(dev
, ABS_Y
,
506 !(s
& data
[0]) - !(s
& data
[1]));
507 input_report_key(dev
, BTN_TRIGGER
, s
& data
[4]);
520 * See documentation at:
521 * http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
522 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
526 #define GC_PSX_DELAY 25 /* 25 usec */
527 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
528 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
530 #define GC_PSX_MOUSE 1 /* Mouse */
531 #define GC_PSX_NEGCON 2 /* NegCon */
532 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
533 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
534 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
536 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
537 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
538 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
539 #define GC_PSX_SELECT 0x02 /* Pin 3 */
541 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
542 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
544 static int gc_psx_delay
= GC_PSX_DELAY
;
545 module_param_named(psx_delay
, gc_psx_delay
, uint
, 0);
546 MODULE_PARM_DESC(psx_delay
, "Delay when accessing Sony PSX controller (usecs)");
548 static const short gc_psx_abs
[] = {
549 ABS_X
, ABS_Y
, ABS_RX
, ABS_RY
, ABS_HAT0X
, ABS_HAT0Y
551 static const short gc_psx_btn
[] = {
552 BTN_TL
, BTN_TR
, BTN_TL2
, BTN_TR2
, BTN_A
, BTN_B
, BTN_X
, BTN_Y
,
553 BTN_START
, BTN_SELECT
, BTN_THUMBL
, BTN_THUMBR
555 static const short gc_psx_ddr_btn
[] = { BTN_0
, BTN_1
, BTN_2
, BTN_3
};
558 * gc_psx_command() writes 8bit command and reads 8bit data from
562 static void gc_psx_command(struct gc
*gc
, int b
, unsigned char *data
)
564 struct parport
*port
= gc
->pd
->port
;
567 memset(data
, 0, GC_MAX_DEVICES
);
569 for (i
= 0; i
< GC_PSX_LENGTH
; i
++, b
>>= 1) {
570 cmd
= (b
& 1) ? GC_PSX_COMMAND
: 0;
571 parport_write_data(port
, cmd
| GC_PSX_POWER
);
572 udelay(gc_psx_delay
);
574 read
= parport_read_status(port
) ^ 0x80;
576 for (j
= 0; j
< GC_MAX_DEVICES
; j
++) {
577 struct gc_pad
*pad
= &gc
->pads
[j
];
579 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
580 data
[j
] |= (read
& gc_status_bit
[j
]) ? (1 << i
) : 0;
583 parport_write_data(gc
->pd
->port
, cmd
| GC_PSX_CLOCK
| GC_PSX_POWER
);
584 udelay(gc_psx_delay
);
589 * gc_psx_read_packet() reads a whole psx packet and returns
590 * device identifier code.
593 static void gc_psx_read_packet(struct gc
*gc
,
594 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
],
595 unsigned char id
[GC_MAX_DEVICES
])
597 int i
, j
, max_len
= 0;
599 unsigned char data2
[GC_MAX_DEVICES
];
602 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
603 udelay(gc_psx_delay
);
604 /* Deselect, begin command */
605 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_POWER
);
606 udelay(gc_psx_delay
);
608 local_irq_save(flags
);
610 gc_psx_command(gc
, 0x01, data2
); /* Access pad */
611 gc_psx_command(gc
, 0x42, id
); /* Get device ids */
612 gc_psx_command(gc
, 0, data2
); /* Dump status */
614 /* Find the longest pad */
615 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
616 struct gc_pad
*pad
= &gc
->pads
[i
];
618 if ((pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
) &&
619 GC_PSX_LEN(id
[i
]) > max_len
&&
620 GC_PSX_LEN(id
[i
]) <= GC_PSX_BYTES
) {
621 max_len
= GC_PSX_LEN(id
[i
]);
625 /* Read in all the data */
626 for (i
= 0; i
< max_len
; i
++) {
627 gc_psx_command(gc
, 0, data2
);
628 for (j
= 0; j
< GC_MAX_DEVICES
; j
++)
629 data
[j
][i
] = data2
[j
];
632 local_irq_restore(flags
);
634 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
636 /* Set id's to the real value */
637 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
638 id
[i
] = GC_PSX_ID(id
[i
]);
641 static void gc_psx_report_one(struct gc_pad
*pad
, unsigned char psx_type
,
644 struct input_dev
*dev
= pad
->dev
;
651 input_report_key(dev
, BTN_THUMBL
, ~data
[0] & 0x04);
652 input_report_key(dev
, BTN_THUMBR
, ~data
[0] & 0x02);
658 if (pad
->type
== GC_DDR
) {
659 for (i
= 0; i
< 4; i
++)
660 input_report_key(dev
, gc_psx_ddr_btn
[i
],
661 ~data
[0] & (0x10 << i
));
663 for (i
= 0; i
< 4; i
++)
664 input_report_abs(dev
, gc_psx_abs
[i
+ 2],
667 input_report_abs(dev
, ABS_X
,
668 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
669 input_report_abs(dev
, ABS_Y
,
670 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
673 for (i
= 0; i
< 8; i
++)
674 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
676 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
677 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
685 if (pad
->type
== GC_DDR
) {
686 for (i
= 0; i
< 4; i
++)
687 input_report_key(dev
, gc_psx_ddr_btn
[i
],
688 ~data
[0] & (0x10 << i
));
690 input_report_abs(dev
, ABS_X
,
691 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
692 input_report_abs(dev
, ABS_Y
,
693 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
696 * For some reason if the extra axes are left unset
698 * for (i = 0; i < 4; i++)
699 input_report_abs(dev, gc_psx_abs[i + 2], 128);
700 * This needs to be debugged properly,
701 * maybe fuzz processing needs to be done
707 for (i
= 0; i
< 8; i
++)
708 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
710 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
711 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
717 default: /* not a pad, ignore */
722 static void gc_psx_process_packet(struct gc
*gc
)
724 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
];
725 unsigned char id
[GC_MAX_DEVICES
];
729 gc_psx_read_packet(gc
, data
, id
);
731 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
733 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
734 gc_psx_report_one(pad
, id
[i
], data
[i
]);
739 * gc_timer() initiates reads of console pads data.
742 static void gc_timer(struct timer_list
*t
)
744 struct gc
*gc
= from_timer(gc
, t
, timer
);
747 * N64 pads - must be read first, any read confuses them for 200 us
750 if (gc
->pad_count
[GC_N64
])
751 gc_n64_process_packet(gc
);
754 * NES and SNES pads or mouse
757 if (gc
->pad_count
[GC_NES
] ||
758 gc
->pad_count
[GC_SNES
] ||
759 gc
->pad_count
[GC_SNESMOUSE
]) {
760 gc_nes_process_packet(gc
);
764 * Multi and Multi2 joysticks
767 if (gc
->pad_count
[GC_MULTI
] || gc
->pad_count
[GC_MULTI2
])
768 gc_multi_process_packet(gc
);
774 if (gc
->pad_count
[GC_PSX
] || gc
->pad_count
[GC_DDR
])
775 gc_psx_process_packet(gc
);
777 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
780 static int gc_open(struct input_dev
*dev
)
782 struct gc
*gc
= input_get_drvdata(dev
);
785 err
= mutex_lock_interruptible(&gc
->mutex
);
790 parport_claim(gc
->pd
);
791 parport_write_control(gc
->pd
->port
, 0x04);
792 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
795 mutex_unlock(&gc
->mutex
);
799 static void gc_close(struct input_dev
*dev
)
801 struct gc
*gc
= input_get_drvdata(dev
);
803 mutex_lock(&gc
->mutex
);
805 del_timer_sync(&gc
->timer
);
806 parport_write_control(gc
->pd
->port
, 0x00);
807 parport_release(gc
->pd
);
809 mutex_unlock(&gc
->mutex
);
812 static int gc_setup_pad(struct gc
*gc
, int idx
, int pad_type
)
814 struct gc_pad
*pad
= &gc
->pads
[idx
];
815 struct input_dev
*input_dev
;
819 if (pad_type
< 1 || pad_type
>= GC_MAX
) {
820 pr_err("Pad type %d unknown\n", pad_type
);
824 pad
->dev
= input_dev
= input_allocate_device();
826 pr_err("Not enough memory for input device\n");
830 pad
->type
= pad_type
;
832 snprintf(pad
->phys
, sizeof(pad
->phys
),
833 "%s/input%d", gc
->pd
->port
->name
, idx
);
835 input_dev
->name
= gc_names
[pad_type
];
836 input_dev
->phys
= pad
->phys
;
837 input_dev
->id
.bustype
= BUS_PARPORT
;
838 input_dev
->id
.vendor
= 0x0001;
839 input_dev
->id
.product
= pad_type
;
840 input_dev
->id
.version
= 0x0100;
842 input_set_drvdata(input_dev
, gc
);
844 input_dev
->open
= gc_open
;
845 input_dev
->close
= gc_close
;
847 if (pad_type
!= GC_SNESMOUSE
) {
848 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
850 for (i
= 0; i
< 2; i
++)
851 input_set_abs_params(input_dev
, ABS_X
+ i
, -1, 1, 0, 0);
853 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
855 gc
->pad_count
[pad_type
]++;
860 for (i
= 0; i
< 10; i
++)
861 input_set_capability(input_dev
, EV_KEY
, gc_n64_btn
[i
]);
863 for (i
= 0; i
< 2; i
++) {
864 input_set_abs_params(input_dev
, ABS_X
+ i
, -127, 126, 0, 2);
865 input_set_abs_params(input_dev
, ABS_HAT0X
+ i
, -1, 1, 0, 0);
868 err
= gc_n64_init_ff(input_dev
, idx
);
870 pr_warn("Failed to initiate rumble for N64 device %d\n",
878 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
879 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
880 input_set_capability(input_dev
, EV_REL
, REL_X
);
881 input_set_capability(input_dev
, EV_REL
, REL_Y
);
885 for (i
= 4; i
< 8; i
++)
886 input_set_capability(input_dev
, EV_KEY
, gc_snes_btn
[i
]);
889 for (i
= 0; i
< 4; i
++)
890 input_set_capability(input_dev
, EV_KEY
, gc_snes_btn
[i
]);
894 input_set_capability(input_dev
, EV_KEY
, BTN_THUMB
);
897 input_set_capability(input_dev
, EV_KEY
, BTN_TRIGGER
);
902 for (i
= 0; i
< 6; i
++)
903 input_set_abs_params(input_dev
,
904 gc_psx_abs
[i
], 4, 252, 0, 2);
905 for (i
= 0; i
< 12; i
++)
906 input_set_capability(input_dev
, EV_KEY
, gc_psx_btn
[i
]);
912 for (i
= 0; i
< 4; i
++)
913 input_set_capability(input_dev
, EV_KEY
,
915 for (i
= 0; i
< 12; i
++)
916 input_set_capability(input_dev
, EV_KEY
, gc_psx_btn
[i
]);
921 err
= input_register_device(pad
->dev
);
928 input_free_device(pad
->dev
);
933 static void gc_attach(struct parport
*pp
)
936 struct pardevice
*pd
;
940 struct pardev_cb gc_parport_cb
;
942 for (port_idx
= 0; port_idx
< GC_MAX_PORTS
; port_idx
++) {
943 if (gc_cfg
[port_idx
].nargs
== 0 || gc_cfg
[port_idx
].args
[0] < 0)
946 if (gc_cfg
[port_idx
].args
[0] == pp
->number
)
950 if (port_idx
== GC_MAX_PORTS
) {
951 pr_debug("Not using parport%d.\n", pp
->number
);
954 pads
= gc_cfg
[port_idx
].args
+ 1;
955 n_pads
= gc_cfg
[port_idx
].nargs
- 1;
957 memset(&gc_parport_cb
, 0, sizeof(gc_parport_cb
));
958 gc_parport_cb
.flags
= PARPORT_FLAG_EXCL
;
960 pd
= parport_register_dev_model(pp
, "gamecon", &gc_parport_cb
,
963 pr_err("parport busy already - lp.o loaded?\n");
967 gc
= kzalloc(sizeof(struct gc
), GFP_KERNEL
);
969 pr_err("Not enough memory\n");
970 goto err_unreg_pardev
;
973 mutex_init(&gc
->mutex
);
975 gc
->parportno
= pp
->number
;
976 timer_setup(&gc
->timer
, gc_timer
, 0);
978 for (i
= 0; i
< n_pads
&& i
< GC_MAX_DEVICES
; i
++) {
982 if (gc_setup_pad(gc
, i
, pads
[i
]))
989 pr_err("No valid devices specified\n");
993 gc_base
[port_idx
] = gc
;
999 input_unregister_device(gc
->pads
[i
].dev
);
1003 parport_unregister_device(pd
);
1006 static void gc_detach(struct parport
*port
)
1011 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
1012 if (gc_base
[i
] && gc_base
[i
]->parportno
== port
->number
)
1016 if (i
== GC_MAX_PORTS
)
1022 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
1023 if (gc
->pads
[i
].dev
)
1024 input_unregister_device(gc
->pads
[i
].dev
);
1025 parport_unregister_device(gc
->pd
);
1029 static struct parport_driver gc_parport_driver
= {
1031 .match_port
= gc_attach
,
1032 .detach
= gc_detach
,
1036 static int __init
gc_init(void)
1041 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
1042 if (gc_cfg
[i
].nargs
== 0 || gc_cfg
[i
].args
[0] < 0)
1045 if (gc_cfg
[i
].nargs
< 2) {
1046 pr_err("at least one device must be specified\n");
1056 return parport_register_driver(&gc_parport_driver
);
1059 static void __exit
gc_exit(void)
1061 parport_unregister_driver(&gc_parport_driver
);
1064 module_init(gc_init
);
1065 module_exit(gc_exit
);