1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
5 * Copyright (c) 1999-2004 Vojtech Pavlik <vojtech@suse.cz>
6 * Copyright (c) 2004 Peter Nelson <rufus-kernel@hackish.org>
8 * Based on the work of:
9 * Andree Borrmann John Dahlstrom
10 * David Kuder Nathan Hand
17 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
19 #include <linux/kernel.h>
20 #include <linux/delay.h>
21 #include <linux/module.h>
22 #include <linux/init.h>
23 #include <linux/parport.h>
24 #include <linux/input.h>
25 #include <linux/mutex.h>
26 #include <linux/slab.h>
28 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
29 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
30 MODULE_LICENSE("GPL");
32 #define GC_MAX_PORTS 3
33 #define GC_MAX_DEVICES 5
36 int args
[GC_MAX_DEVICES
+ 1];
40 static struct gc_config gc_cfg
[GC_MAX_PORTS
];
42 module_param_array_named(map
, gc_cfg
[0].args
, int, &gc_cfg
[0].nargs
, 0);
43 MODULE_PARM_DESC(map
, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
44 module_param_array_named(map2
, gc_cfg
[1].args
, int, &gc_cfg
[1].nargs
, 0);
45 MODULE_PARM_DESC(map2
, "Describes second set of devices");
46 module_param_array_named(map3
, gc_cfg
[2].args
, int, &gc_cfg
[2].nargs
, 0);
47 MODULE_PARM_DESC(map3
, "Describes third set of devices");
49 /* see also gs_psx_delay parameter in PSX support section */
65 #define GC_REFRESH_TIME HZ/100
68 struct input_dev
*dev
;
75 struct gc_pad pads
[GC_MAX_DEVICES
];
76 struct timer_list timer
;
77 int pad_count
[GC_MAX
];
87 static struct gc
*gc_base
[3];
89 static const int gc_status_bit
[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
91 static const char *gc_names
[] = {
92 NULL
, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
93 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
94 "PSX DDR controller", "SNES mouse"
101 static const unsigned char gc_n64_bytes
[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
102 static const short gc_n64_btn
[] = {
103 BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
,
104 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_STOP_LENGTH 5 /* Length of encoded stop bit */
109 #define GC_N64_CMD_00 0x11111111UL
110 #define GC_N64_CMD_01 0xd1111111UL
111 #define GC_N64_CMD_03 0xdd111111UL
112 #define GC_N64_CMD_1b 0xdd1dd111UL
113 #define GC_N64_CMD_c0 0x111111ddUL
114 #define GC_N64_CMD_80 0x1111111dUL
115 #define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
116 #define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
117 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
118 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
119 /* GC_N64_DWS > 24 is known to fail */
120 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
121 #define GC_N64_POWER_R 0xfd /* power during read */
122 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
123 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
124 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
126 #define GC_N64_CLOCK 0x02 /* clock bits for read */
129 * Used for rumble code.
132 /* Send encoded command */
133 static void gc_n64_send_command(struct gc
*gc
, unsigned long cmd
,
134 unsigned char target
)
136 struct parport
*port
= gc
->pd
->port
;
139 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
140 unsigned char data
= (cmd
>> i
) & 1 ? target
: 0;
141 parport_write_data(port
, GC_N64_POWER_W
| data
);
147 static void gc_n64_send_stop_bit(struct gc
*gc
, unsigned char target
)
149 struct parport
*port
= gc
->pd
->port
;
152 for (i
= 0; i
< GC_N64_STOP_LENGTH
; i
++) {
153 unsigned char data
= (GC_N64_STOP_BIT
>> i
) & 1 ? target
: 0;
154 parport_write_data(port
, GC_N64_POWER_W
| data
);
160 * gc_n64_read_packet() reads an N64 packet.
161 * Each pad uses one bit per byte. So all pads connected to this port
162 * are read in parallel.
165 static void gc_n64_read_packet(struct gc
*gc
, unsigned char *data
)
171 * Request the pad to transmit data
174 local_irq_save(flags
);
175 gc_n64_send_command(gc
, GC_N64_REQUEST_DATA
, GC_N64_OUT
);
176 gc_n64_send_stop_bit(gc
, GC_N64_OUT
);
177 local_irq_restore(flags
);
180 * Wait for the pad response to be loaded into the 33-bit register
184 udelay(GC_N64_DELAY
);
187 * Grab data (ignoring the last bit, which is a stop bit)
190 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
191 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
);
193 data
[i
] = parport_read_status(gc
->pd
->port
);
194 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
| GC_N64_CLOCK
);
198 * We must wait 200 ms here for the controller to reinitialize before
199 * the next read request. No worries as long as gc_read is polled less
200 * frequently than this.
205 static void gc_n64_process_packet(struct gc
*gc
)
207 unsigned char data
[GC_N64_LENGTH
];
208 struct input_dev
*dev
;
212 gc_n64_read_packet(gc
, data
);
214 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
216 if (gc
->pads
[i
].type
!= GC_N64
)
219 dev
= gc
->pads
[i
].dev
;
220 s
= gc_status_bit
[i
];
222 if (s
& ~(data
[8] | data
[9])) {
226 for (j
= 0; j
< 8; j
++) {
227 if (data
[23 - j
] & s
)
229 if (data
[31 - j
] & s
)
233 input_report_abs(dev
, ABS_X
, x
);
234 input_report_abs(dev
, ABS_Y
, -y
);
236 input_report_abs(dev
, ABS_HAT0X
,
237 !(s
& data
[6]) - !(s
& data
[7]));
238 input_report_abs(dev
, ABS_HAT0Y
,
239 !(s
& data
[4]) - !(s
& data
[5]));
241 for (j
= 0; j
< 10; j
++)
242 input_report_key(dev
, gc_n64_btn
[j
],
243 s
& data
[gc_n64_bytes
[j
]]);
250 static int gc_n64_play_effect(struct input_dev
*dev
, void *data
,
251 struct ff_effect
*effect
)
255 struct gc
*gc
= input_get_drvdata(dev
);
256 struct gc_subdev
*sdev
= data
;
257 unsigned char target
= 1 << sdev
->idx
; /* select desired pin */
259 if (effect
->type
== FF_RUMBLE
) {
260 struct ff_rumble_effect
*rumble
= &effect
->u
.rumble
;
262 rumble
->strong_magnitude
|| rumble
->weak_magnitude
?
263 GC_N64_CMD_01
: GC_N64_CMD_00
;
265 local_irq_save(flags
);
267 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
268 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
269 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
270 gc_n64_send_command(gc
, GC_N64_CMD_01
, target
);
271 for (i
= 0; i
< 32; i
++)
272 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
273 gc_n64_send_stop_bit(gc
, target
);
275 udelay(GC_N64_DELAY
);
277 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
278 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
279 gc_n64_send_command(gc
, GC_N64_CMD_c0
, target
);
280 gc_n64_send_command(gc
, GC_N64_CMD_1b
, target
);
281 for (i
= 0; i
< 32; i
++)
282 gc_n64_send_command(gc
, cmd
, target
);
283 gc_n64_send_stop_bit(gc
, target
);
285 local_irq_restore(flags
);
292 static int gc_n64_init_ff(struct input_dev
*dev
, int i
)
294 struct gc_subdev
*sdev
;
297 sdev
= kmalloc(sizeof(*sdev
), GFP_KERNEL
);
303 input_set_capability(dev
, EV_FF
, FF_RUMBLE
);
305 err
= input_ff_create_memless(dev
, sdev
, gc_n64_play_effect
);
318 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
319 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
320 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
321 last 4 bits are unused */
322 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
323 16 bits are equivalent to a gamepad */
325 #define GC_NES_POWER 0xfc
326 #define GC_NES_CLOCK 0x01
327 #define GC_NES_LATCH 0x02
329 static const unsigned char gc_nes_bytes
[] = { 0, 1, 2, 3 };
330 static const unsigned char gc_snes_bytes
[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
331 static const short gc_snes_btn
[] = {
332 BTN_A
, BTN_B
, BTN_SELECT
, BTN_START
, BTN_X
, BTN_Y
, BTN_TL
, BTN_TR
336 * gc_nes_read_packet() reads a NES/SNES packet.
337 * Each pad uses one bit per byte. So all pads connected to
338 * this port are read in parallel.
341 static void gc_nes_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
345 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
| GC_NES_LATCH
);
346 udelay(GC_NES_DELAY
* 2);
347 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
349 for (i
= 0; i
< length
; i
++) {
350 udelay(GC_NES_DELAY
);
351 parport_write_data(gc
->pd
->port
, GC_NES_POWER
);
352 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
353 udelay(GC_NES_DELAY
);
354 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
358 static void gc_nes_process_packet(struct gc
*gc
)
360 unsigned char data
[GC_SNESMOUSE_LENGTH
];
362 struct input_dev
*dev
;
366 len
= gc
->pad_count
[GC_SNESMOUSE
] ? GC_SNESMOUSE_LENGTH
:
367 (gc
->pad_count
[GC_SNES
] ? GC_SNES_LENGTH
: GC_NES_LENGTH
);
369 gc_nes_read_packet(gc
, len
, data
);
371 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
375 s
= gc_status_bit
[i
];
381 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
382 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
384 for (j
= 0; j
< 4; j
++)
385 input_report_key(dev
, gc_snes_btn
[j
],
386 s
& data
[gc_nes_bytes
[j
]]);
392 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
393 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
395 for (j
= 0; j
< 8; j
++)
396 input_report_key(dev
, gc_snes_btn
[j
],
397 s
& data
[gc_snes_bytes
[j
]]);
403 * The 4 unused bits from SNES controllers appear
404 * to be ID bits so use them to make sure we are
405 * dealing with a mouse.
406 * gamepad is connected. This is important since
407 * my SNES gamepad sends 1's for bits 16-31, which
408 * cause the mouse pointer to quickly move to the
409 * upper left corner of the screen.
411 if (!(s
& data
[12]) && !(s
& data
[13]) &&
412 !(s
& data
[14]) && (s
& data
[15])) {
413 input_report_key(dev
, BTN_LEFT
, s
& data
[9]);
414 input_report_key(dev
, BTN_RIGHT
, s
& data
[8]);
417 for (j
= 0; j
< 7; j
++) {
419 if (data
[25 + j
] & s
)
423 if (data
[17 + j
] & s
)
430 input_report_rel(dev
, REL_X
, x_rel
);
436 input_report_rel(dev
, REL_Y
, y_rel
);
450 * Multisystem joystick support
453 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
454 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
457 * gc_multi_read_packet() reads a Multisystem joystick packet.
460 static void gc_multi_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
464 for (i
= 0; i
< length
; i
++) {
465 parport_write_data(gc
->pd
->port
, ~(1 << i
));
466 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
470 static void gc_multi_process_packet(struct gc
*gc
)
472 unsigned char data
[GC_MULTI2_LENGTH
];
473 int data_len
= gc
->pad_count
[GC_MULTI2
] ? GC_MULTI2_LENGTH
: GC_MULTI_LENGTH
;
475 struct input_dev
*dev
;
478 gc_multi_read_packet(gc
, data_len
, data
);
480 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
483 s
= gc_status_bit
[i
];
487 input_report_key(dev
, BTN_THUMB
, s
& data
[5]);
491 input_report_abs(dev
, ABS_X
,
492 !(s
& data
[2]) - !(s
& data
[3]));
493 input_report_abs(dev
, ABS_Y
,
494 !(s
& data
[0]) - !(s
& data
[1]));
495 input_report_key(dev
, BTN_TRIGGER
, s
& data
[4]);
508 * See documentation at:
509 * http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
510 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
514 #define GC_PSX_DELAY 25 /* 25 usec */
515 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
516 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
518 #define GC_PSX_MOUSE 1 /* Mouse */
519 #define GC_PSX_NEGCON 2 /* NegCon */
520 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
521 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
522 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
524 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
525 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
526 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
527 #define GC_PSX_SELECT 0x02 /* Pin 3 */
529 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
530 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
532 static int gc_psx_delay
= GC_PSX_DELAY
;
533 module_param_named(psx_delay
, gc_psx_delay
, uint
, 0);
534 MODULE_PARM_DESC(psx_delay
, "Delay when accessing Sony PSX controller (usecs)");
536 static const short gc_psx_abs
[] = {
537 ABS_X
, ABS_Y
, ABS_RX
, ABS_RY
, ABS_HAT0X
, ABS_HAT0Y
539 static const short gc_psx_btn
[] = {
540 BTN_TL
, BTN_TR
, BTN_TL2
, BTN_TR2
, BTN_A
, BTN_B
, BTN_X
, BTN_Y
,
541 BTN_START
, BTN_SELECT
, BTN_THUMBL
, BTN_THUMBR
543 static const short gc_psx_ddr_btn
[] = { BTN_0
, BTN_1
, BTN_2
, BTN_3
};
546 * gc_psx_command() writes 8bit command and reads 8bit data from
550 static void gc_psx_command(struct gc
*gc
, int b
, unsigned char *data
)
552 struct parport
*port
= gc
->pd
->port
;
555 memset(data
, 0, GC_MAX_DEVICES
);
557 for (i
= 0; i
< GC_PSX_LENGTH
; i
++, b
>>= 1) {
558 cmd
= (b
& 1) ? GC_PSX_COMMAND
: 0;
559 parport_write_data(port
, cmd
| GC_PSX_POWER
);
560 udelay(gc_psx_delay
);
562 read
= parport_read_status(port
) ^ 0x80;
564 for (j
= 0; j
< GC_MAX_DEVICES
; j
++) {
565 struct gc_pad
*pad
= &gc
->pads
[j
];
567 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
568 data
[j
] |= (read
& gc_status_bit
[j
]) ? (1 << i
) : 0;
571 parport_write_data(gc
->pd
->port
, cmd
| GC_PSX_CLOCK
| GC_PSX_POWER
);
572 udelay(gc_psx_delay
);
577 * gc_psx_read_packet() reads a whole psx packet and returns
578 * device identifier code.
581 static void gc_psx_read_packet(struct gc
*gc
,
582 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
],
583 unsigned char id
[GC_MAX_DEVICES
])
585 int i
, j
, max_len
= 0;
587 unsigned char data2
[GC_MAX_DEVICES
];
590 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
591 udelay(gc_psx_delay
);
592 /* Deselect, begin command */
593 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_POWER
);
594 udelay(gc_psx_delay
);
596 local_irq_save(flags
);
598 gc_psx_command(gc
, 0x01, data2
); /* Access pad */
599 gc_psx_command(gc
, 0x42, id
); /* Get device ids */
600 gc_psx_command(gc
, 0, data2
); /* Dump status */
602 /* Find the longest pad */
603 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
604 struct gc_pad
*pad
= &gc
->pads
[i
];
606 if ((pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
) &&
607 GC_PSX_LEN(id
[i
]) > max_len
&&
608 GC_PSX_LEN(id
[i
]) <= GC_PSX_BYTES
) {
609 max_len
= GC_PSX_LEN(id
[i
]);
613 /* Read in all the data */
614 for (i
= 0; i
< max_len
; i
++) {
615 gc_psx_command(gc
, 0, data2
);
616 for (j
= 0; j
< GC_MAX_DEVICES
; j
++)
617 data
[j
][i
] = data2
[j
];
620 local_irq_restore(flags
);
622 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
624 /* Set id's to the real value */
625 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
626 id
[i
] = GC_PSX_ID(id
[i
]);
629 static void gc_psx_report_one(struct gc_pad
*pad
, unsigned char psx_type
,
632 struct input_dev
*dev
= pad
->dev
;
639 input_report_key(dev
, BTN_THUMBL
, ~data
[0] & 0x04);
640 input_report_key(dev
, BTN_THUMBR
, ~data
[0] & 0x02);
646 if (pad
->type
== GC_DDR
) {
647 for (i
= 0; i
< 4; i
++)
648 input_report_key(dev
, gc_psx_ddr_btn
[i
],
649 ~data
[0] & (0x10 << i
));
651 for (i
= 0; i
< 4; i
++)
652 input_report_abs(dev
, gc_psx_abs
[i
+ 2],
655 input_report_abs(dev
, ABS_X
,
656 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
657 input_report_abs(dev
, ABS_Y
,
658 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
661 for (i
= 0; i
< 8; i
++)
662 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
664 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
665 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
673 if (pad
->type
== GC_DDR
) {
674 for (i
= 0; i
< 4; i
++)
675 input_report_key(dev
, gc_psx_ddr_btn
[i
],
676 ~data
[0] & (0x10 << i
));
678 input_report_abs(dev
, ABS_X
,
679 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
680 input_report_abs(dev
, ABS_Y
,
681 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
684 * For some reason if the extra axes are left unset
686 * for (i = 0; i < 4; i++)
687 input_report_abs(dev, gc_psx_abs[i + 2], 128);
688 * This needs to be debugged properly,
689 * maybe fuzz processing needs to be done
695 for (i
= 0; i
< 8; i
++)
696 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
698 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
699 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
705 default: /* not a pad, ignore */
710 static void gc_psx_process_packet(struct gc
*gc
)
712 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
];
713 unsigned char id
[GC_MAX_DEVICES
];
717 gc_psx_read_packet(gc
, data
, id
);
719 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
721 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
722 gc_psx_report_one(pad
, id
[i
], data
[i
]);
727 * gc_timer() initiates reads of console pads data.
730 static void gc_timer(struct timer_list
*t
)
732 struct gc
*gc
= from_timer(gc
, t
, timer
);
735 * N64 pads - must be read first, any read confuses them for 200 us
738 if (gc
->pad_count
[GC_N64
])
739 gc_n64_process_packet(gc
);
742 * NES and SNES pads or mouse
745 if (gc
->pad_count
[GC_NES
] ||
746 gc
->pad_count
[GC_SNES
] ||
747 gc
->pad_count
[GC_SNESMOUSE
]) {
748 gc_nes_process_packet(gc
);
752 * Multi and Multi2 joysticks
755 if (gc
->pad_count
[GC_MULTI
] || gc
->pad_count
[GC_MULTI2
])
756 gc_multi_process_packet(gc
);
762 if (gc
->pad_count
[GC_PSX
] || gc
->pad_count
[GC_DDR
])
763 gc_psx_process_packet(gc
);
765 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
768 static int gc_open(struct input_dev
*dev
)
770 struct gc
*gc
= input_get_drvdata(dev
);
773 err
= mutex_lock_interruptible(&gc
->mutex
);
778 parport_claim(gc
->pd
);
779 parport_write_control(gc
->pd
->port
, 0x04);
780 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
783 mutex_unlock(&gc
->mutex
);
787 static void gc_close(struct input_dev
*dev
)
789 struct gc
*gc
= input_get_drvdata(dev
);
791 mutex_lock(&gc
->mutex
);
793 del_timer_sync(&gc
->timer
);
794 parport_write_control(gc
->pd
->port
, 0x00);
795 parport_release(gc
->pd
);
797 mutex_unlock(&gc
->mutex
);
800 static int gc_setup_pad(struct gc
*gc
, int idx
, int pad_type
)
802 struct gc_pad
*pad
= &gc
->pads
[idx
];
803 struct input_dev
*input_dev
;
807 if (pad_type
< 1 || pad_type
>= GC_MAX
) {
808 pr_err("Pad type %d unknown\n", pad_type
);
812 pad
->dev
= input_dev
= input_allocate_device();
814 pr_err("Not enough memory for input device\n");
818 pad
->type
= pad_type
;
820 snprintf(pad
->phys
, sizeof(pad
->phys
),
821 "%s/input%d", gc
->pd
->port
->name
, idx
);
823 input_dev
->name
= gc_names
[pad_type
];
824 input_dev
->phys
= pad
->phys
;
825 input_dev
->id
.bustype
= BUS_PARPORT
;
826 input_dev
->id
.vendor
= 0x0001;
827 input_dev
->id
.product
= pad_type
;
828 input_dev
->id
.version
= 0x0100;
830 input_set_drvdata(input_dev
, gc
);
832 input_dev
->open
= gc_open
;
833 input_dev
->close
= gc_close
;
835 if (pad_type
!= GC_SNESMOUSE
) {
836 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
838 for (i
= 0; i
< 2; i
++)
839 input_set_abs_params(input_dev
, ABS_X
+ i
, -1, 1, 0, 0);
841 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
843 gc
->pad_count
[pad_type
]++;
848 for (i
= 0; i
< 10; i
++)
849 input_set_capability(input_dev
, EV_KEY
, gc_n64_btn
[i
]);
851 for (i
= 0; i
< 2; i
++) {
852 input_set_abs_params(input_dev
, ABS_X
+ i
, -127, 126, 0, 2);
853 input_set_abs_params(input_dev
, ABS_HAT0X
+ i
, -1, 1, 0, 0);
856 err
= gc_n64_init_ff(input_dev
, idx
);
858 pr_warn("Failed to initiate rumble for N64 device %d\n",
866 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
867 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
868 input_set_capability(input_dev
, EV_REL
, REL_X
);
869 input_set_capability(input_dev
, EV_REL
, REL_Y
);
873 for (i
= 4; i
< 8; i
++)
874 input_set_capability(input_dev
, EV_KEY
, gc_snes_btn
[i
]);
877 for (i
= 0; i
< 4; i
++)
878 input_set_capability(input_dev
, EV_KEY
, gc_snes_btn
[i
]);
882 input_set_capability(input_dev
, EV_KEY
, BTN_THUMB
);
885 input_set_capability(input_dev
, EV_KEY
, BTN_TRIGGER
);
890 for (i
= 0; i
< 6; i
++)
891 input_set_abs_params(input_dev
,
892 gc_psx_abs
[i
], 4, 252, 0, 2);
893 for (i
= 0; i
< 12; i
++)
894 input_set_capability(input_dev
, EV_KEY
, gc_psx_btn
[i
]);
900 for (i
= 0; i
< 4; i
++)
901 input_set_capability(input_dev
, EV_KEY
,
903 for (i
= 0; i
< 12; i
++)
904 input_set_capability(input_dev
, EV_KEY
, gc_psx_btn
[i
]);
909 err
= input_register_device(pad
->dev
);
916 input_free_device(pad
->dev
);
921 static void gc_attach(struct parport
*pp
)
924 struct pardevice
*pd
;
928 struct pardev_cb gc_parport_cb
;
930 for (port_idx
= 0; port_idx
< GC_MAX_PORTS
; port_idx
++) {
931 if (gc_cfg
[port_idx
].nargs
== 0 || gc_cfg
[port_idx
].args
[0] < 0)
934 if (gc_cfg
[port_idx
].args
[0] == pp
->number
)
938 if (port_idx
== GC_MAX_PORTS
) {
939 pr_debug("Not using parport%d.\n", pp
->number
);
942 pads
= gc_cfg
[port_idx
].args
+ 1;
943 n_pads
= gc_cfg
[port_idx
].nargs
- 1;
945 memset(&gc_parport_cb
, 0, sizeof(gc_parport_cb
));
946 gc_parport_cb
.flags
= PARPORT_FLAG_EXCL
;
948 pd
= parport_register_dev_model(pp
, "gamecon", &gc_parport_cb
,
951 pr_err("parport busy already - lp.o loaded?\n");
955 gc
= kzalloc(sizeof(struct gc
), GFP_KERNEL
);
957 pr_err("Not enough memory\n");
958 goto err_unreg_pardev
;
961 mutex_init(&gc
->mutex
);
963 gc
->parportno
= pp
->number
;
964 timer_setup(&gc
->timer
, gc_timer
, 0);
966 for (i
= 0; i
< n_pads
&& i
< GC_MAX_DEVICES
; i
++) {
970 if (gc_setup_pad(gc
, i
, pads
[i
]))
977 pr_err("No valid devices specified\n");
981 gc_base
[port_idx
] = gc
;
987 input_unregister_device(gc
->pads
[i
].dev
);
991 parport_unregister_device(pd
);
994 static void gc_detach(struct parport
*port
)
999 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
1000 if (gc_base
[i
] && gc_base
[i
]->parportno
== port
->number
)
1004 if (i
== GC_MAX_PORTS
)
1010 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
1011 if (gc
->pads
[i
].dev
)
1012 input_unregister_device(gc
->pads
[i
].dev
);
1013 parport_unregister_device(gc
->pd
);
1017 static struct parport_driver gc_parport_driver
= {
1019 .match_port
= gc_attach
,
1020 .detach
= gc_detach
,
1024 static int __init
gc_init(void)
1029 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
1030 if (gc_cfg
[i
].nargs
== 0 || gc_cfg
[i
].args
[0] < 0)
1033 if (gc_cfg
[i
].nargs
< 2) {
1034 pr_err("at least one device must be specified\n");
1044 return parport_register_driver(&gc_parport_driver
);
1047 static void __exit
gc_exit(void)
1049 parport_unregister_driver(&gc_parport_driver
);
1052 module_init(gc_init
);
1053 module_exit(gc_exit
);