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
14 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
16 #include <linux/kernel.h>
17 #include <linux/delay.h>
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/parport.h>
21 #include <linux/input.h>
22 #include <linux/mutex.h>
23 #include <linux/slab.h>
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26 MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
27 MODULE_LICENSE("GPL");
29 #define GC_MAX_PORTS 3
30 #define GC_MAX_DEVICES 5
33 int args
[GC_MAX_DEVICES
+ 1];
37 static struct gc_config gc_cfg
[GC_MAX_PORTS
];
39 module_param_array_named(map
, gc_cfg
[0].args
, int, &gc_cfg
[0].nargs
, 0);
40 MODULE_PARM_DESC(map
, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
41 module_param_array_named(map2
, gc_cfg
[1].args
, int, &gc_cfg
[1].nargs
, 0);
42 MODULE_PARM_DESC(map2
, "Describes second set of devices");
43 module_param_array_named(map3
, gc_cfg
[2].args
, int, &gc_cfg
[2].nargs
, 0);
44 MODULE_PARM_DESC(map3
, "Describes third set of devices");
46 /* see also gs_psx_delay parameter in PSX support section */
62 #define GC_REFRESH_TIME HZ/100
65 struct input_dev
*dev
;
72 struct gc_pad pads
[GC_MAX_DEVICES
];
73 struct timer_list timer
;
74 int pad_count
[GC_MAX
];
84 static struct gc
*gc_base
[3];
86 static const int gc_status_bit
[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
88 static const char *gc_names
[] = {
89 NULL
, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
90 "Multisystem 2-button joystick", "N64 controller", "PSX controller",
91 "PSX DDR controller", "SNES mouse"
98 static const unsigned char gc_n64_bytes
[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
99 static const short gc_n64_btn
[] = {
100 BTN_A
, BTN_B
, BTN_C
, BTN_X
, BTN_Y
, BTN_Z
,
101 BTN_TL
, BTN_TR
, BTN_TRIGGER
, BTN_START
104 #define GC_N64_LENGTH 32 /* N64 bit length, not including stop bit */
105 #define GC_N64_STOP_LENGTH 5 /* Length of encoded stop bit */
106 #define GC_N64_CMD_00 0x11111111UL
107 #define GC_N64_CMD_01 0xd1111111UL
108 #define GC_N64_CMD_03 0xdd111111UL
109 #define GC_N64_CMD_1b 0xdd1dd111UL
110 #define GC_N64_CMD_c0 0x111111ddUL
111 #define GC_N64_CMD_80 0x1111111dUL
112 #define GC_N64_STOP_BIT 0x1d /* Encoded stop bit */
113 #define GC_N64_REQUEST_DATA GC_N64_CMD_01 /* the request data command */
114 #define GC_N64_DELAY 133 /* delay between transmit request, and response ready (us) */
115 #define GC_N64_DWS 3 /* delay between write segments (required for sound playback because of ISA DMA) */
116 /* GC_N64_DWS > 24 is known to fail */
117 #define GC_N64_POWER_W 0xe2 /* power during write (transmit request) */
118 #define GC_N64_POWER_R 0xfd /* power during read */
119 #define GC_N64_OUT 0x1d /* output bits to the 4 pads */
120 /* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
121 /* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
123 #define GC_N64_CLOCK 0x02 /* clock bits for read */
126 * Used for rumble code.
129 /* Send encoded command */
130 static void gc_n64_send_command(struct gc
*gc
, unsigned long cmd
,
131 unsigned char target
)
133 struct parport
*port
= gc
->pd
->port
;
136 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
137 unsigned char data
= (cmd
>> i
) & 1 ? target
: 0;
138 parport_write_data(port
, GC_N64_POWER_W
| data
);
144 static void gc_n64_send_stop_bit(struct gc
*gc
, unsigned char target
)
146 struct parport
*port
= gc
->pd
->port
;
149 for (i
= 0; i
< GC_N64_STOP_LENGTH
; i
++) {
150 unsigned char data
= (GC_N64_STOP_BIT
>> i
) & 1 ? target
: 0;
151 parport_write_data(port
, GC_N64_POWER_W
| data
);
157 * gc_n64_read_packet() reads an N64 packet.
158 * Each pad uses one bit per byte. So all pads connected to this port
159 * are read in parallel.
162 static void gc_n64_read_packet(struct gc
*gc
, unsigned char *data
)
168 * Request the pad to transmit data
171 local_irq_save(flags
);
172 gc_n64_send_command(gc
, GC_N64_REQUEST_DATA
, GC_N64_OUT
);
173 gc_n64_send_stop_bit(gc
, GC_N64_OUT
);
174 local_irq_restore(flags
);
177 * Wait for the pad response to be loaded into the 33-bit register
181 udelay(GC_N64_DELAY
);
184 * Grab data (ignoring the last bit, which is a stop bit)
187 for (i
= 0; i
< GC_N64_LENGTH
; i
++) {
188 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
);
190 data
[i
] = parport_read_status(gc
->pd
->port
);
191 parport_write_data(gc
->pd
->port
, GC_N64_POWER_R
| GC_N64_CLOCK
);
195 * We must wait 200 ms here for the controller to reinitialize before
196 * the next read request. No worries as long as gc_read is polled less
197 * frequently than this.
202 static void gc_n64_process_packet(struct gc
*gc
)
204 unsigned char data
[GC_N64_LENGTH
];
205 struct input_dev
*dev
;
209 gc_n64_read_packet(gc
, data
);
211 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
213 if (gc
->pads
[i
].type
!= GC_N64
)
216 dev
= gc
->pads
[i
].dev
;
217 s
= gc_status_bit
[i
];
219 if (s
& ~(data
[8] | data
[9])) {
223 for (j
= 0; j
< 8; j
++) {
224 if (data
[23 - j
] & s
)
226 if (data
[31 - j
] & s
)
230 input_report_abs(dev
, ABS_X
, x
);
231 input_report_abs(dev
, ABS_Y
, -y
);
233 input_report_abs(dev
, ABS_HAT0X
,
234 !(s
& data
[6]) - !(s
& data
[7]));
235 input_report_abs(dev
, ABS_HAT0Y
,
236 !(s
& data
[4]) - !(s
& data
[5]));
238 for (j
= 0; j
< 10; j
++)
239 input_report_key(dev
, gc_n64_btn
[j
],
240 s
& data
[gc_n64_bytes
[j
]]);
247 static int gc_n64_play_effect(struct input_dev
*dev
, void *data
,
248 struct ff_effect
*effect
)
252 struct gc
*gc
= input_get_drvdata(dev
);
253 struct gc_subdev
*sdev
= data
;
254 unsigned char target
= 1 << sdev
->idx
; /* select desired pin */
256 if (effect
->type
== FF_RUMBLE
) {
257 struct ff_rumble_effect
*rumble
= &effect
->u
.rumble
;
259 rumble
->strong_magnitude
|| rumble
->weak_magnitude
?
260 GC_N64_CMD_01
: GC_N64_CMD_00
;
262 local_irq_save(flags
);
264 /* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
265 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
266 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
267 gc_n64_send_command(gc
, GC_N64_CMD_01
, target
);
268 for (i
= 0; i
< 32; i
++)
269 gc_n64_send_command(gc
, GC_N64_CMD_80
, target
);
270 gc_n64_send_stop_bit(gc
, target
);
272 udelay(GC_N64_DELAY
);
274 /* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
275 gc_n64_send_command(gc
, GC_N64_CMD_03
, target
);
276 gc_n64_send_command(gc
, GC_N64_CMD_c0
, target
);
277 gc_n64_send_command(gc
, GC_N64_CMD_1b
, target
);
278 for (i
= 0; i
< 32; i
++)
279 gc_n64_send_command(gc
, cmd
, target
);
280 gc_n64_send_stop_bit(gc
, target
);
282 local_irq_restore(flags
);
289 static int gc_n64_init_ff(struct input_dev
*dev
, int i
)
291 struct gc_subdev
*sdev
;
294 sdev
= kmalloc(sizeof(*sdev
), GFP_KERNEL
);
300 input_set_capability(dev
, EV_FF
, FF_RUMBLE
);
302 err
= input_ff_create_memless(dev
, sdev
, gc_n64_play_effect
);
315 #define GC_NES_DELAY 6 /* Delay between bits - 6us */
316 #define GC_NES_LENGTH 8 /* The NES pads use 8 bits of data */
317 #define GC_SNES_LENGTH 12 /* The SNES true length is 16, but the
318 last 4 bits are unused */
319 #define GC_SNESMOUSE_LENGTH 32 /* The SNES mouse uses 32 bits, the first
320 16 bits are equivalent to a gamepad */
322 #define GC_NES_POWER 0xfc
323 #define GC_NES_CLOCK 0x01
324 #define GC_NES_LATCH 0x02
326 static const unsigned char gc_nes_bytes
[] = { 0, 1, 2, 3 };
327 static const unsigned char gc_snes_bytes
[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
328 static const short gc_snes_btn
[] = {
329 BTN_A
, BTN_B
, BTN_SELECT
, BTN_START
, BTN_X
, BTN_Y
, BTN_TL
, BTN_TR
333 * gc_nes_read_packet() reads a NES/SNES packet.
334 * Each pad uses one bit per byte. So all pads connected to
335 * this port are read in parallel.
338 static void gc_nes_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
342 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
| GC_NES_LATCH
);
343 udelay(GC_NES_DELAY
* 2);
344 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
346 for (i
= 0; i
< length
; i
++) {
347 udelay(GC_NES_DELAY
);
348 parport_write_data(gc
->pd
->port
, GC_NES_POWER
);
349 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
350 udelay(GC_NES_DELAY
);
351 parport_write_data(gc
->pd
->port
, GC_NES_POWER
| GC_NES_CLOCK
);
355 static void gc_nes_process_packet(struct gc
*gc
)
357 unsigned char data
[GC_SNESMOUSE_LENGTH
];
359 struct input_dev
*dev
;
363 len
= gc
->pad_count
[GC_SNESMOUSE
] ? GC_SNESMOUSE_LENGTH
:
364 (gc
->pad_count
[GC_SNES
] ? GC_SNES_LENGTH
: GC_NES_LENGTH
);
366 gc_nes_read_packet(gc
, len
, data
);
368 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
372 s
= gc_status_bit
[i
];
378 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
379 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
381 for (j
= 0; j
< 4; j
++)
382 input_report_key(dev
, gc_snes_btn
[j
],
383 s
& data
[gc_nes_bytes
[j
]]);
389 input_report_abs(dev
, ABS_X
, !(s
& data
[6]) - !(s
& data
[7]));
390 input_report_abs(dev
, ABS_Y
, !(s
& data
[4]) - !(s
& data
[5]));
392 for (j
= 0; j
< 8; j
++)
393 input_report_key(dev
, gc_snes_btn
[j
],
394 s
& data
[gc_snes_bytes
[j
]]);
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
);
447 * Multisystem joystick support
450 #define GC_MULTI_LENGTH 5 /* Multi system joystick packet length is 5 */
451 #define GC_MULTI2_LENGTH 6 /* One more bit for one more button */
454 * gc_multi_read_packet() reads a Multisystem joystick packet.
457 static void gc_multi_read_packet(struct gc
*gc
, int length
, unsigned char *data
)
461 for (i
= 0; i
< length
; i
++) {
462 parport_write_data(gc
->pd
->port
, ~(1 << i
));
463 data
[i
] = parport_read_status(gc
->pd
->port
) ^ 0x7f;
467 static void gc_multi_process_packet(struct gc
*gc
)
469 unsigned char data
[GC_MULTI2_LENGTH
];
470 int data_len
= gc
->pad_count
[GC_MULTI2
] ? GC_MULTI2_LENGTH
: GC_MULTI_LENGTH
;
472 struct input_dev
*dev
;
475 gc_multi_read_packet(gc
, data_len
, data
);
477 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
480 s
= gc_status_bit
[i
];
484 input_report_key(dev
, BTN_THUMB
, s
& data
[5]);
488 input_report_abs(dev
, ABS_X
,
489 !(s
& data
[2]) - !(s
& data
[3]));
490 input_report_abs(dev
, ABS_Y
,
491 !(s
& data
[0]) - !(s
& data
[1]));
492 input_report_key(dev
, BTN_TRIGGER
, s
& data
[4]);
505 * See documentation at:
506 * http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt
507 * http://www.gamesx.com/controldata/psxcont/psxcont.htm
511 #define GC_PSX_DELAY 25 /* 25 usec */
512 #define GC_PSX_LENGTH 8 /* talk to the controller in bits */
513 #define GC_PSX_BYTES 6 /* the maximum number of bytes to read off the controller */
515 #define GC_PSX_MOUSE 1 /* Mouse */
516 #define GC_PSX_NEGCON 2 /* NegCon */
517 #define GC_PSX_NORMAL 4 /* Digital / Analog or Rumble in Digital mode */
518 #define GC_PSX_ANALOG 5 /* Analog in Analog mode / Rumble in Green mode */
519 #define GC_PSX_RUMBLE 7 /* Rumble in Red mode */
521 #define GC_PSX_CLOCK 0x04 /* Pin 4 */
522 #define GC_PSX_COMMAND 0x01 /* Pin 2 */
523 #define GC_PSX_POWER 0xf8 /* Pins 5-9 */
524 #define GC_PSX_SELECT 0x02 /* Pin 3 */
526 #define GC_PSX_ID(x) ((x) >> 4) /* High nibble is device type */
527 #define GC_PSX_LEN(x) (((x) & 0xf) << 1) /* Low nibble is length in bytes/2 */
529 static int gc_psx_delay
= GC_PSX_DELAY
;
530 module_param_named(psx_delay
, gc_psx_delay
, uint
, 0);
531 MODULE_PARM_DESC(psx_delay
, "Delay when accessing Sony PSX controller (usecs)");
533 static const short gc_psx_abs
[] = {
534 ABS_X
, ABS_Y
, ABS_RX
, ABS_RY
, ABS_HAT0X
, ABS_HAT0Y
536 static const short gc_psx_btn
[] = {
537 BTN_TL
, BTN_TR
, BTN_TL2
, BTN_TR2
, BTN_A
, BTN_B
, BTN_X
, BTN_Y
,
538 BTN_START
, BTN_SELECT
, BTN_THUMBL
, BTN_THUMBR
540 static const short gc_psx_ddr_btn
[] = { BTN_0
, BTN_1
, BTN_2
, BTN_3
};
543 * gc_psx_command() writes 8bit command and reads 8bit data from
547 static void gc_psx_command(struct gc
*gc
, int b
, unsigned char *data
)
549 struct parport
*port
= gc
->pd
->port
;
552 memset(data
, 0, GC_MAX_DEVICES
);
554 for (i
= 0; i
< GC_PSX_LENGTH
; i
++, b
>>= 1) {
555 cmd
= (b
& 1) ? GC_PSX_COMMAND
: 0;
556 parport_write_data(port
, cmd
| GC_PSX_POWER
);
557 udelay(gc_psx_delay
);
559 read
= parport_read_status(port
) ^ 0x80;
561 for (j
= 0; j
< GC_MAX_DEVICES
; j
++) {
562 struct gc_pad
*pad
= &gc
->pads
[j
];
564 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
565 data
[j
] |= (read
& gc_status_bit
[j
]) ? (1 << i
) : 0;
568 parport_write_data(gc
->pd
->port
, cmd
| GC_PSX_CLOCK
| GC_PSX_POWER
);
569 udelay(gc_psx_delay
);
574 * gc_psx_read_packet() reads a whole psx packet and returns
575 * device identifier code.
578 static void gc_psx_read_packet(struct gc
*gc
,
579 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
],
580 unsigned char id
[GC_MAX_DEVICES
])
582 int i
, j
, max_len
= 0;
584 unsigned char data2
[GC_MAX_DEVICES
];
587 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
588 udelay(gc_psx_delay
);
589 /* Deselect, begin command */
590 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_POWER
);
591 udelay(gc_psx_delay
);
593 local_irq_save(flags
);
595 gc_psx_command(gc
, 0x01, data2
); /* Access pad */
596 gc_psx_command(gc
, 0x42, id
); /* Get device ids */
597 gc_psx_command(gc
, 0, data2
); /* Dump status */
599 /* Find the longest pad */
600 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
601 struct gc_pad
*pad
= &gc
->pads
[i
];
603 if ((pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
) &&
604 GC_PSX_LEN(id
[i
]) > max_len
&&
605 GC_PSX_LEN(id
[i
]) <= GC_PSX_BYTES
) {
606 max_len
= GC_PSX_LEN(id
[i
]);
610 /* Read in all the data */
611 for (i
= 0; i
< max_len
; i
++) {
612 gc_psx_command(gc
, 0, data2
);
613 for (j
= 0; j
< GC_MAX_DEVICES
; j
++)
614 data
[j
][i
] = data2
[j
];
617 local_irq_restore(flags
);
619 parport_write_data(gc
->pd
->port
, GC_PSX_CLOCK
| GC_PSX_SELECT
| GC_PSX_POWER
);
621 /* Set id's to the real value */
622 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
623 id
[i
] = GC_PSX_ID(id
[i
]);
626 static void gc_psx_report_one(struct gc_pad
*pad
, unsigned char psx_type
,
629 struct input_dev
*dev
= pad
->dev
;
636 input_report_key(dev
, BTN_THUMBL
, ~data
[0] & 0x04);
637 input_report_key(dev
, BTN_THUMBR
, ~data
[0] & 0x02);
643 if (pad
->type
== GC_DDR
) {
644 for (i
= 0; i
< 4; i
++)
645 input_report_key(dev
, gc_psx_ddr_btn
[i
],
646 ~data
[0] & (0x10 << i
));
648 for (i
= 0; i
< 4; i
++)
649 input_report_abs(dev
, gc_psx_abs
[i
+ 2],
652 input_report_abs(dev
, ABS_X
,
653 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
654 input_report_abs(dev
, ABS_Y
,
655 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
658 for (i
= 0; i
< 8; i
++)
659 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
661 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
662 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
670 if (pad
->type
== GC_DDR
) {
671 for (i
= 0; i
< 4; i
++)
672 input_report_key(dev
, gc_psx_ddr_btn
[i
],
673 ~data
[0] & (0x10 << i
));
675 input_report_abs(dev
, ABS_X
,
676 !!(data
[0] & 0x80) * 128 + !(data
[0] & 0x20) * 127);
677 input_report_abs(dev
, ABS_Y
,
678 !!(data
[0] & 0x10) * 128 + !(data
[0] & 0x40) * 127);
681 * For some reason if the extra axes are left unset
683 * for (i = 0; i < 4; i++)
684 input_report_abs(dev, gc_psx_abs[i + 2], 128);
685 * This needs to be debugged properly,
686 * maybe fuzz processing needs to be done
692 for (i
= 0; i
< 8; i
++)
693 input_report_key(dev
, gc_psx_btn
[i
], ~data
[1] & (1 << i
));
695 input_report_key(dev
, BTN_START
, ~data
[0] & 0x08);
696 input_report_key(dev
, BTN_SELECT
, ~data
[0] & 0x01);
702 default: /* not a pad, ignore */
707 static void gc_psx_process_packet(struct gc
*gc
)
709 unsigned char data
[GC_MAX_DEVICES
][GC_PSX_BYTES
];
710 unsigned char id
[GC_MAX_DEVICES
];
714 gc_psx_read_packet(gc
, data
, id
);
716 for (i
= 0; i
< GC_MAX_DEVICES
; i
++) {
718 if (pad
->type
== GC_PSX
|| pad
->type
== GC_DDR
)
719 gc_psx_report_one(pad
, id
[i
], data
[i
]);
724 * gc_timer() initiates reads of console pads data.
727 static void gc_timer(struct timer_list
*t
)
729 struct gc
*gc
= from_timer(gc
, t
, timer
);
732 * N64 pads - must be read first, any read confuses them for 200 us
735 if (gc
->pad_count
[GC_N64
])
736 gc_n64_process_packet(gc
);
739 * NES and SNES pads or mouse
742 if (gc
->pad_count
[GC_NES
] ||
743 gc
->pad_count
[GC_SNES
] ||
744 gc
->pad_count
[GC_SNESMOUSE
]) {
745 gc_nes_process_packet(gc
);
749 * Multi and Multi2 joysticks
752 if (gc
->pad_count
[GC_MULTI
] || gc
->pad_count
[GC_MULTI2
])
753 gc_multi_process_packet(gc
);
759 if (gc
->pad_count
[GC_PSX
] || gc
->pad_count
[GC_DDR
])
760 gc_psx_process_packet(gc
);
762 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
765 static int gc_open(struct input_dev
*dev
)
767 struct gc
*gc
= input_get_drvdata(dev
);
769 scoped_guard(mutex_intr
, &gc
->mutex
) {
771 parport_claim(gc
->pd
);
772 parport_write_control(gc
->pd
->port
, 0x04);
773 mod_timer(&gc
->timer
, jiffies
+ GC_REFRESH_TIME
);
782 static void gc_close(struct input_dev
*dev
)
784 struct gc
*gc
= input_get_drvdata(dev
);
786 guard(mutex
)(&gc
->mutex
);
789 del_timer_sync(&gc
->timer
);
790 parport_write_control(gc
->pd
->port
, 0x00);
791 parport_release(gc
->pd
);
795 static int gc_setup_pad(struct gc
*gc
, int idx
, int pad_type
)
797 struct gc_pad
*pad
= &gc
->pads
[idx
];
798 struct input_dev
*input_dev
;
802 if (pad_type
< 1 || pad_type
>= GC_MAX
) {
803 pr_err("Pad type %d unknown\n", pad_type
);
807 pad
->dev
= input_dev
= input_allocate_device();
809 pr_err("Not enough memory for input device\n");
813 pad
->type
= pad_type
;
815 snprintf(pad
->phys
, sizeof(pad
->phys
),
816 "%s/input%d", gc
->pd
->port
->name
, idx
);
818 input_dev
->name
= gc_names
[pad_type
];
819 input_dev
->phys
= pad
->phys
;
820 input_dev
->id
.bustype
= BUS_PARPORT
;
821 input_dev
->id
.vendor
= 0x0001;
822 input_dev
->id
.product
= pad_type
;
823 input_dev
->id
.version
= 0x0100;
825 input_set_drvdata(input_dev
, gc
);
827 input_dev
->open
= gc_open
;
828 input_dev
->close
= gc_close
;
830 if (pad_type
!= GC_SNESMOUSE
) {
831 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
833 for (i
= 0; i
< 2; i
++)
834 input_set_abs_params(input_dev
, ABS_X
+ i
, -1, 1, 0, 0);
836 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_REL
);
838 gc
->pad_count
[pad_type
]++;
843 for (i
= 0; i
< 10; i
++)
844 input_set_capability(input_dev
, EV_KEY
, gc_n64_btn
[i
]);
846 for (i
= 0; i
< 2; i
++) {
847 input_set_abs_params(input_dev
, ABS_X
+ i
, -127, 126, 0, 2);
848 input_set_abs_params(input_dev
, ABS_HAT0X
+ i
, -1, 1, 0, 0);
851 err
= gc_n64_init_ff(input_dev
, idx
);
853 pr_warn("Failed to initiate rumble for N64 device %d\n",
861 input_set_capability(input_dev
, EV_KEY
, BTN_LEFT
);
862 input_set_capability(input_dev
, EV_KEY
, BTN_RIGHT
);
863 input_set_capability(input_dev
, EV_REL
, REL_X
);
864 input_set_capability(input_dev
, EV_REL
, REL_Y
);
868 for (i
= 4; i
< 8; i
++)
869 input_set_capability(input_dev
, EV_KEY
, gc_snes_btn
[i
]);
873 for (i
= 0; i
< 4; i
++)
874 input_set_capability(input_dev
, EV_KEY
, gc_snes_btn
[i
]);
878 input_set_capability(input_dev
, EV_KEY
, BTN_THUMB
);
882 input_set_capability(input_dev
, EV_KEY
, BTN_TRIGGER
);
886 for (i
= 0; i
< 6; i
++)
887 input_set_abs_params(input_dev
,
888 gc_psx_abs
[i
], 4, 252, 0, 2);
889 for (i
= 0; i
< 12; i
++)
890 input_set_capability(input_dev
, EV_KEY
, gc_psx_btn
[i
]);
896 for (i
= 0; i
< 4; i
++)
897 input_set_capability(input_dev
, EV_KEY
,
899 for (i
= 0; i
< 12; i
++)
900 input_set_capability(input_dev
, EV_KEY
, gc_psx_btn
[i
]);
905 err
= input_register_device(pad
->dev
);
912 input_free_device(pad
->dev
);
917 static void gc_attach(struct parport
*pp
)
920 struct pardevice
*pd
;
924 struct pardev_cb gc_parport_cb
;
926 for (port_idx
= 0; port_idx
< GC_MAX_PORTS
; port_idx
++) {
927 if (gc_cfg
[port_idx
].nargs
== 0 || gc_cfg
[port_idx
].args
[0] < 0)
930 if (gc_cfg
[port_idx
].args
[0] == pp
->number
)
934 if (port_idx
== GC_MAX_PORTS
) {
935 pr_debug("Not using parport%d.\n", pp
->number
);
938 pads
= gc_cfg
[port_idx
].args
+ 1;
939 n_pads
= gc_cfg
[port_idx
].nargs
- 1;
941 memset(&gc_parport_cb
, 0, sizeof(gc_parport_cb
));
942 gc_parport_cb
.flags
= PARPORT_FLAG_EXCL
;
944 pd
= parport_register_dev_model(pp
, "gamecon", &gc_parport_cb
,
947 pr_err("parport busy already - lp.o loaded?\n");
951 gc
= kzalloc(sizeof(*gc
), GFP_KERNEL
);
953 pr_err("Not enough memory\n");
954 goto err_unreg_pardev
;
957 mutex_init(&gc
->mutex
);
959 gc
->parportno
= pp
->number
;
960 timer_setup(&gc
->timer
, gc_timer
, 0);
962 for (i
= 0; i
< n_pads
&& i
< GC_MAX_DEVICES
; i
++) {
966 if (gc_setup_pad(gc
, i
, pads
[i
]))
973 pr_err("No valid devices specified\n");
977 gc_base
[port_idx
] = gc
;
983 input_unregister_device(gc
->pads
[i
].dev
);
987 parport_unregister_device(pd
);
990 static void gc_detach(struct parport
*port
)
995 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
996 if (gc_base
[i
] && gc_base
[i
]->parportno
== port
->number
)
1000 if (i
== GC_MAX_PORTS
)
1006 for (i
= 0; i
< GC_MAX_DEVICES
; i
++)
1007 if (gc
->pads
[i
].dev
)
1008 input_unregister_device(gc
->pads
[i
].dev
);
1009 parport_unregister_device(gc
->pd
);
1013 static struct parport_driver gc_parport_driver
= {
1015 .match_port
= gc_attach
,
1016 .detach
= gc_detach
,
1019 static int __init
gc_init(void)
1024 for (i
= 0; i
< GC_MAX_PORTS
; i
++) {
1025 if (gc_cfg
[i
].nargs
== 0 || gc_cfg
[i
].args
[0] < 0)
1028 if (gc_cfg
[i
].nargs
< 2) {
1029 pr_err("at least one device must be specified\n");
1039 return parport_register_driver(&gc_parport_driver
);
1042 static void __exit
gc_exit(void)
1044 parport_unregister_driver(&gc_parport_driver
);
1047 module_init(gc_init
);
1048 module_exit(gc_exit
);