Orange Pi5 kernel

Deprecated Linux kernel 5.10.110 for OrangePi 5/5B/5+ boards

3 Commits   0 Branches   0 Tags
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    1) // SPDX-License-Identifier: GPL-2.0-or-later
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    3)  * NES, SNES, N64, MultiSystem, PSX gamepad driver for Linux
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    4)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    5)  *  Copyright (c) 1999-2004	Vojtech Pavlik <vojtech@suse.cz>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    6)  *  Copyright (c) 2004		Peter Nelson <rufus-kernel@hackish.org>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    7)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    8)  *  Based on the work of:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300    9)  *	Andree Borrmann		John Dahlstrom
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   10)  *	David Kuder		Nathan Hand
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   11)  *	Raphael Assenat
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   12)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   13) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   14) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   15)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   16) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   17) #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   18) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   19) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   20) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   21) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   22) #include <linux/init.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   23) #include <linux/parport.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   24) #include <linux/input.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   25) #include <linux/mutex.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   26) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   27) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   28) MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   29) MODULE_DESCRIPTION("NES, SNES, N64, MultiSystem, PSX gamepad driver");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   30) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   31) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   32) #define GC_MAX_PORTS		3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   33) #define GC_MAX_DEVICES		5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   34) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   35) struct gc_config {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   36) 	int args[GC_MAX_DEVICES + 1];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   37) 	unsigned int nargs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   38) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   39) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   40) static struct gc_config gc_cfg[GC_MAX_PORTS];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   41) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   42) module_param_array_named(map, gc_cfg[0].args, int, &gc_cfg[0].nargs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   43) MODULE_PARM_DESC(map, "Describes first set of devices (<parport#>,<pad1>,<pad2>,..<pad5>)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   44) module_param_array_named(map2, gc_cfg[1].args, int, &gc_cfg[1].nargs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   45) MODULE_PARM_DESC(map2, "Describes second set of devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   46) module_param_array_named(map3, gc_cfg[2].args, int, &gc_cfg[2].nargs, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   47) MODULE_PARM_DESC(map3, "Describes third set of devices");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   48) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   49) /* see also gs_psx_delay parameter in PSX support section */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   50) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   51) enum gc_type {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   52) 	GC_NONE = 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   53) 	GC_SNES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   54) 	GC_NES,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   55) 	GC_NES4,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   56) 	GC_MULTI,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   57) 	GC_MULTI2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   58) 	GC_N64,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   59) 	GC_PSX,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   60) 	GC_DDR,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   61) 	GC_SNESMOUSE,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   62) 	GC_MAX
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   63) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   64) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   65) #define GC_REFRESH_TIME	HZ/100
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   66) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   67) struct gc_pad {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   68) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   69) 	enum gc_type type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   70) 	char phys[32];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   71) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   72) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   73) struct gc {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   74) 	struct pardevice *pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   75) 	struct gc_pad pads[GC_MAX_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   76) 	struct timer_list timer;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   77) 	int pad_count[GC_MAX];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   78) 	int used;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   79) 	int parportno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   80) 	struct mutex mutex;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   81) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   82) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   83) struct gc_subdev {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   84) 	unsigned int idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   85) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   86) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   87) static struct gc *gc_base[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   88) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   89) static const int gc_status_bit[] = { 0x40, 0x80, 0x20, 0x10, 0x08 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   90) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   91) static const char *gc_names[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   92) 	NULL, "SNES pad", "NES pad", "NES FourPort", "Multisystem joystick",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   93) 	"Multisystem 2-button joystick", "N64 controller", "PSX controller",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   94) 	"PSX DDR controller", "SNES mouse"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   95) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   96) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   97) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   98)  * N64 support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300   99)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  100) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  101) static const unsigned char gc_n64_bytes[] = { 0, 1, 13, 15, 14, 12, 10, 11, 2, 3 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  102) static const short gc_n64_btn[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  103) 	BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  104) 	BTN_TL, BTN_TR, BTN_TRIGGER, BTN_START
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  105) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  106) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  107) #define GC_N64_LENGTH		32		/* N64 bit length, not including stop bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  108) #define GC_N64_STOP_LENGTH	5		/* Length of encoded stop bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  109) #define GC_N64_CMD_00		0x11111111UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  110) #define GC_N64_CMD_01		0xd1111111UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  111) #define GC_N64_CMD_03		0xdd111111UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  112) #define GC_N64_CMD_1b		0xdd1dd111UL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  113) #define GC_N64_CMD_c0		0x111111ddUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  114) #define GC_N64_CMD_80		0x1111111dUL
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  115) #define GC_N64_STOP_BIT		0x1d		/* Encoded stop bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  116) #define GC_N64_REQUEST_DATA	GC_N64_CMD_01	/* the request data command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  117) #define GC_N64_DELAY		133		/* delay between transmit request, and response ready (us) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  118) #define GC_N64_DWS		3		/* delay between write segments (required for sound playback because of ISA DMA) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  119) 						/* GC_N64_DWS > 24 is known to fail */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  120) #define GC_N64_POWER_W		0xe2		/* power during write (transmit request) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  121) #define GC_N64_POWER_R		0xfd		/* power during read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  122) #define GC_N64_OUT		0x1d		/* output bits to the 4 pads */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  123) 						/* Reading the main axes of any N64 pad is known to fail if the corresponding bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  124) 						/* in GC_N64_OUT is pulled low on the output port (by any routine) for more */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  125) 						/* than 123 us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  126) #define GC_N64_CLOCK		0x02		/* clock bits for read */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  127) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  128) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  129)  * Used for rumble code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  130)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  131) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  132) /* Send encoded command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  133) static void gc_n64_send_command(struct gc *gc, unsigned long cmd,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  134) 				unsigned char target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  135) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  136) 	struct parport *port = gc->pd->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  137) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  138) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  139) 	for (i = 0; i < GC_N64_LENGTH; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  140) 		unsigned char data = (cmd >> i) & 1 ? target : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  141) 		parport_write_data(port, GC_N64_POWER_W | data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  142) 		udelay(GC_N64_DWS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  143) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  144) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  145) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  146) /* Send stop bit */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  147) static void gc_n64_send_stop_bit(struct gc *gc, unsigned char target)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  148) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  149) 	struct parport *port = gc->pd->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  150) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  151) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  152) 	for (i = 0; i < GC_N64_STOP_LENGTH; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  153) 		unsigned char data = (GC_N64_STOP_BIT >> i) & 1 ? target : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  154) 		parport_write_data(port, GC_N64_POWER_W | data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  155) 		udelay(GC_N64_DWS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  156) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  157) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  158) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  159) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  160)  * gc_n64_read_packet() reads an N64 packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  161)  * Each pad uses one bit per byte. So all pads connected to this port
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  162)  * are read in parallel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  163)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  164) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  165) static void gc_n64_read_packet(struct gc *gc, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  166) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  167) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  168) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  169) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  170) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  171)  * Request the pad to transmit data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  172)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  173) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  174) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  175) 	gc_n64_send_command(gc, GC_N64_REQUEST_DATA, GC_N64_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  176) 	gc_n64_send_stop_bit(gc, GC_N64_OUT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  177) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  178) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  179) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  180)  * Wait for the pad response to be loaded into the 33-bit register
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  181)  * of the adapter.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  182)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  183) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  184) 	udelay(GC_N64_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  185) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  186) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  187)  * Grab data (ignoring the last bit, which is a stop bit)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  188)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  189) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  190) 	for (i = 0; i < GC_N64_LENGTH; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  191) 		parport_write_data(gc->pd->port, GC_N64_POWER_R);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  192) 		udelay(2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  193) 		data[i] = parport_read_status(gc->pd->port);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  194) 		parport_write_data(gc->pd->port, GC_N64_POWER_R | GC_N64_CLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  195) 	 }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  196) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  197) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  198)  * We must wait 200 ms here for the controller to reinitialize before
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  199)  * the next read request. No worries as long as gc_read is polled less
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  200)  * frequently than this.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  201)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  202) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  203) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  204) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  205) static void gc_n64_process_packet(struct gc *gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  206) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  207) 	unsigned char data[GC_N64_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  208) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  209) 	int i, j, s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  210) 	signed char x, y;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  211) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  212) 	gc_n64_read_packet(gc, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  213) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  214) 	for (i = 0; i < GC_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  215) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  216) 		if (gc->pads[i].type != GC_N64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  217) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  218) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  219) 		dev = gc->pads[i].dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  220) 		s = gc_status_bit[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  221) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  222) 		if (s & ~(data[8] | data[9])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  223) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  224) 			x = y = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  225) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  226) 			for (j = 0; j < 8; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  227) 				if (data[23 - j] & s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  228) 					x |= 1 << j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  229) 				if (data[31 - j] & s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  230) 					y |= 1 << j;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  231) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  232) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  233) 			input_report_abs(dev, ABS_X,  x);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  234) 			input_report_abs(dev, ABS_Y, -y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  235) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  236) 			input_report_abs(dev, ABS_HAT0X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  237) 					 !(s & data[6]) - !(s & data[7]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  238) 			input_report_abs(dev, ABS_HAT0Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  239) 					 !(s & data[4]) - !(s & data[5]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  240) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  241) 			for (j = 0; j < 10; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  242) 				input_report_key(dev, gc_n64_btn[j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  243) 						 s & data[gc_n64_bytes[j]]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  244) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  245) 			input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  246) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  247) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  249) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  250) static int gc_n64_play_effect(struct input_dev *dev, void *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  251) 			      struct ff_effect *effect)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  252) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  253) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  254) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  255) 	struct gc *gc = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  256) 	struct gc_subdev *sdev = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  257) 	unsigned char target = 1 << sdev->idx; /* select desired pin */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  258) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  259) 	if (effect->type == FF_RUMBLE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  260) 		struct ff_rumble_effect *rumble = &effect->u.rumble;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  261) 		unsigned int cmd =
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  262) 			rumble->strong_magnitude || rumble->weak_magnitude ?
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  263) 			GC_N64_CMD_01 : GC_N64_CMD_00;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  264) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  265) 		local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  266) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  267) 		/* Init Rumble - 0x03, 0x80, 0x01, (34)0x80 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  268) 		gc_n64_send_command(gc, GC_N64_CMD_03, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  269) 		gc_n64_send_command(gc, GC_N64_CMD_80, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  270) 		gc_n64_send_command(gc, GC_N64_CMD_01, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  271) 		for (i = 0; i < 32; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  272) 			gc_n64_send_command(gc, GC_N64_CMD_80, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  273) 		gc_n64_send_stop_bit(gc, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  274) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  275) 		udelay(GC_N64_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  276) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  277) 		/* Now start or stop it - 0x03, 0xc0, 0zx1b, (32)0x01/0x00 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  278) 		gc_n64_send_command(gc, GC_N64_CMD_03, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  279) 		gc_n64_send_command(gc, GC_N64_CMD_c0, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  280) 		gc_n64_send_command(gc, GC_N64_CMD_1b, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  281) 		for (i = 0; i < 32; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  282) 			gc_n64_send_command(gc, cmd, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  283) 		gc_n64_send_stop_bit(gc, target);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  284) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  285) 		local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  286) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  287) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  288) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  289) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  290) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  291) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  292) static int gc_n64_init_ff(struct input_dev *dev, int i)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  293) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  294) 	struct gc_subdev *sdev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  295) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  296) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  297) 	sdev = kmalloc(sizeof(*sdev), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  298) 	if (!sdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  299) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  300) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  301) 	sdev->idx = i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  302) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  303) 	input_set_capability(dev, EV_FF, FF_RUMBLE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  304) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  305) 	err = input_ff_create_memless(dev, sdev, gc_n64_play_effect);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  306) 	if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  307) 		kfree(sdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  308) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  309) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  310) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  311) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  312) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  313) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  314) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  315)  * NES/SNES support.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  316)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  317) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  318) #define GC_NES_DELAY		6	/* Delay between bits - 6us */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  319) #define GC_NES_LENGTH		8	/* The NES pads use 8 bits of data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  320) #define GC_SNES_LENGTH		12	/* The SNES true length is 16, but the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  321) 					   last 4 bits are unused */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  322) #define GC_SNESMOUSE_LENGTH	32	/* The SNES mouse uses 32 bits, the first
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  323) 					   16 bits are equivalent to a gamepad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  324) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  325) #define GC_NES_POWER	0xfc
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  326) #define GC_NES_CLOCK	0x01
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  327) #define GC_NES_LATCH	0x02
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  328) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  329) static const unsigned char gc_nes_bytes[] = { 0, 1, 2, 3 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  330) static const unsigned char gc_snes_bytes[] = { 8, 0, 2, 3, 9, 1, 10, 11 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  331) static const short gc_snes_btn[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  332) 	BTN_A, BTN_B, BTN_SELECT, BTN_START, BTN_X, BTN_Y, BTN_TL, BTN_TR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  333) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  334) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  335) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  336)  * gc_nes_read_packet() reads a NES/SNES packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  337)  * Each pad uses one bit per byte. So all pads connected to
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  338)  * this port are read in parallel.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  339)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  340) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  341) static void gc_nes_read_packet(struct gc *gc, int length, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  342) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  343) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  344) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  345) 	parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK | GC_NES_LATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  346) 	udelay(GC_NES_DELAY * 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  347) 	parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  348) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  349) 	for (i = 0; i < length; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  350) 		udelay(GC_NES_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  351) 		parport_write_data(gc->pd->port, GC_NES_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  352) 		data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  353) 		udelay(GC_NES_DELAY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  354) 		parport_write_data(gc->pd->port, GC_NES_POWER | GC_NES_CLOCK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  355) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  356) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  357) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  358) static void gc_nes_process_packet(struct gc *gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  359) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  360) 	unsigned char data[GC_SNESMOUSE_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  361) 	struct gc_pad *pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  362) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  363) 	int i, j, s, len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  364) 	char x_rel, y_rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  365) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  366) 	len = gc->pad_count[GC_SNESMOUSE] ? GC_SNESMOUSE_LENGTH :
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  367) 			(gc->pad_count[GC_SNES] ? GC_SNES_LENGTH : GC_NES_LENGTH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  368) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  369) 	gc_nes_read_packet(gc, len, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  370) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  371) 	for (i = 0; i < GC_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  372) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  373) 		pad = &gc->pads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  374) 		dev = pad->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  375) 		s = gc_status_bit[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  376) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  377) 		switch (pad->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  378) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  379) 		case GC_NES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  380) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  381) 			input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  382) 			input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  383) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  384) 			for (j = 0; j < 4; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  385) 				input_report_key(dev, gc_snes_btn[j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  386) 						 s & data[gc_nes_bytes[j]]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  387) 			input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  388) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  389) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  390) 		case GC_SNES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  391) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  392) 			input_report_abs(dev, ABS_X, !(s & data[6]) - !(s & data[7]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  393) 			input_report_abs(dev, ABS_Y, !(s & data[4]) - !(s & data[5]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  394) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  395) 			for (j = 0; j < 8; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  396) 				input_report_key(dev, gc_snes_btn[j],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  397) 						 s & data[gc_snes_bytes[j]]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  398) 			input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  399) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  400) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  401) 		case GC_SNESMOUSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  402) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  403) 			 * The 4 unused bits from SNES controllers appear
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  404) 			 * to be ID bits so use them to make sure we are
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  405) 			 * dealing with a mouse.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  406) 			 * gamepad is connected. This is important since
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  407) 			 * my SNES gamepad sends 1's for bits 16-31, which
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  408) 			 * cause the mouse pointer to quickly move to the
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  409) 			 * upper left corner of the screen.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  410) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  411) 			if (!(s & data[12]) && !(s & data[13]) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  412) 			    !(s & data[14]) && (s & data[15])) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  413) 				input_report_key(dev, BTN_LEFT, s & data[9]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  414) 				input_report_key(dev, BTN_RIGHT, s & data[8]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  415) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  416) 				x_rel = y_rel = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  417) 				for (j = 0; j < 7; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  418) 					x_rel <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  419) 					if (data[25 + j] & s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  420) 						x_rel |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  421) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  422) 					y_rel <<= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  423) 					if (data[17 + j] & s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  424) 						y_rel |= 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  425) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  426) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  427) 				if (x_rel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  428) 					if (data[24] & s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  429) 						x_rel = -x_rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  430) 					input_report_rel(dev, REL_X, x_rel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  431) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  432) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  433) 				if (y_rel) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  434) 					if (data[16] & s)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  435) 						y_rel = -y_rel;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  436) 					input_report_rel(dev, REL_Y, y_rel);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  437) 				}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  438) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  439) 				input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  440) 			}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  441) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  442) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  443) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  444) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  445) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  446) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  447) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  448) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  449) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  450)  * Multisystem joystick support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  451)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  452) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  453) #define GC_MULTI_LENGTH		5	/* Multi system joystick packet length is 5 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  454) #define GC_MULTI2_LENGTH	6	/* One more bit for one more button */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  455) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  456) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  457)  * gc_multi_read_packet() reads a Multisystem joystick packet.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  458)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  459) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  460) static void gc_multi_read_packet(struct gc *gc, int length, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  461) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  462) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  463) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  464) 	for (i = 0; i < length; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  465) 		parport_write_data(gc->pd->port, ~(1 << i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  466) 		data[i] = parport_read_status(gc->pd->port) ^ 0x7f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  467) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  468) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  469) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  470) static void gc_multi_process_packet(struct gc *gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  471) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  472) 	unsigned char data[GC_MULTI2_LENGTH];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  473) 	int data_len = gc->pad_count[GC_MULTI2] ? GC_MULTI2_LENGTH : GC_MULTI_LENGTH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  474) 	struct gc_pad *pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  475) 	struct input_dev *dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  476) 	int i, s;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  477) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  478) 	gc_multi_read_packet(gc, data_len, data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  479) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  480) 	for (i = 0; i < GC_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  481) 		pad = &gc->pads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  482) 		dev = pad->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  483) 		s = gc_status_bit[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  484) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  485) 		switch (pad->type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  486) 		case GC_MULTI2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  487) 			input_report_key(dev, BTN_THUMB, s & data[5]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  488) 			fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  489) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  490) 		case GC_MULTI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  491) 			input_report_abs(dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  492) 					 !(s & data[2]) - !(s & data[3]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  493) 			input_report_abs(dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  494) 					 !(s & data[0]) - !(s & data[1]));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  495) 			input_report_key(dev, BTN_TRIGGER, s & data[4]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  496) 			input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  497) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  498) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  499) 		default:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  500) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  501) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  502) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  503) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  504) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  505) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  506)  * PSX support
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  507)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  508)  * See documentation at:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  509)  *	http://www.geocities.co.jp/Playtown/2004/psx/ps_eng.txt	
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  510)  *	http://www.gamesx.com/controldata/psxcont/psxcont.htm
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  511)  *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  512)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  513) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  514) #define GC_PSX_DELAY	25		/* 25 usec */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  515) #define GC_PSX_LENGTH	8		/* talk to the controller in bits */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  516) #define GC_PSX_BYTES	6		/* the maximum number of bytes to read off the controller */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  517) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  518) #define GC_PSX_MOUSE	1		/* Mouse */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  519) #define GC_PSX_NEGCON	2		/* NegCon */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  520) #define GC_PSX_NORMAL	4		/* Digital / Analog or Rumble in Digital mode  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  521) #define GC_PSX_ANALOG	5		/* Analog in Analog mode / Rumble in Green mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  522) #define GC_PSX_RUMBLE	7		/* Rumble in Red mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  523) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  524) #define GC_PSX_CLOCK	0x04		/* Pin 4 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  525) #define GC_PSX_COMMAND	0x01		/* Pin 2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  526) #define GC_PSX_POWER	0xf8		/* Pins 5-9 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  527) #define GC_PSX_SELECT	0x02		/* Pin 3 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  528) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  529) #define GC_PSX_ID(x)	((x) >> 4)	/* High nibble is device type */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  530) #define GC_PSX_LEN(x)	(((x) & 0xf) << 1)	/* Low nibble is length in bytes/2 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  531) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  532) static int gc_psx_delay = GC_PSX_DELAY;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  533) module_param_named(psx_delay, gc_psx_delay, uint, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  534) MODULE_PARM_DESC(psx_delay, "Delay when accessing Sony PSX controller (usecs)");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  535) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  536) static const short gc_psx_abs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  537) 	ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_HAT0X, ABS_HAT0Y
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  538) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  539) static const short gc_psx_btn[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  540) 	BTN_TL, BTN_TR, BTN_TL2, BTN_TR2, BTN_A, BTN_B, BTN_X, BTN_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  541) 	BTN_START, BTN_SELECT, BTN_THUMBL, BTN_THUMBR
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  542) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  543) static const short gc_psx_ddr_btn[] = { BTN_0, BTN_1, BTN_2, BTN_3 };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  544) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  545) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  546)  * gc_psx_command() writes 8bit command and reads 8bit data from
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  547)  * the psx pad.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  548)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  549) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  550) static void gc_psx_command(struct gc *gc, int b, unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  551) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  552) 	struct parport *port = gc->pd->port;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  553) 	int i, j, cmd, read;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  554) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  555) 	memset(data, 0, GC_MAX_DEVICES);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  556) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  557) 	for (i = 0; i < GC_PSX_LENGTH; i++, b >>= 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  558) 		cmd = (b & 1) ? GC_PSX_COMMAND : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  559) 		parport_write_data(port, cmd | GC_PSX_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  560) 		udelay(gc_psx_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  561) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  562) 		read = parport_read_status(port) ^ 0x80;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  563) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  564) 		for (j = 0; j < GC_MAX_DEVICES; j++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  565) 			struct gc_pad *pad = &gc->pads[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  566) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  567) 			if (pad->type == GC_PSX || pad->type == GC_DDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  568) 				data[j] |= (read & gc_status_bit[j]) ? (1 << i) : 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  569) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  570) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  571) 		parport_write_data(gc->pd->port, cmd | GC_PSX_CLOCK | GC_PSX_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  572) 		udelay(gc_psx_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  573) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  574) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  575) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  576) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  577)  * gc_psx_read_packet() reads a whole psx packet and returns
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  578)  * device identifier code.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  579)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  580) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  581) static void gc_psx_read_packet(struct gc *gc,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  582) 			       unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  583) 			       unsigned char id[GC_MAX_DEVICES])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  584) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  585) 	int i, j, max_len = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  586) 	unsigned long flags;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  587) 	unsigned char data2[GC_MAX_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  588) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  589) 	/* Select pad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  590) 	parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  591) 	udelay(gc_psx_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  592) 	/* Deselect, begin command */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  593) 	parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  594) 	udelay(gc_psx_delay);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  595) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  596) 	local_irq_save(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  597) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  598) 	gc_psx_command(gc, 0x01, data2);	/* Access pad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  599) 	gc_psx_command(gc, 0x42, id);		/* Get device ids */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  600) 	gc_psx_command(gc, 0, data2);		/* Dump status */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  601) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  602) 	/* Find the longest pad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  603) 	for (i = 0; i < GC_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  604) 		struct gc_pad *pad = &gc->pads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  605) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  606) 		if ((pad->type == GC_PSX || pad->type == GC_DDR) &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  607) 		    GC_PSX_LEN(id[i]) > max_len &&
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  608) 		    GC_PSX_LEN(id[i]) <= GC_PSX_BYTES) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  609) 			max_len = GC_PSX_LEN(id[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  610) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  611) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  612) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  613) 	/* Read in all the data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  614) 	for (i = 0; i < max_len; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  615) 		gc_psx_command(gc, 0, data2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  616) 		for (j = 0; j < GC_MAX_DEVICES; j++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  617) 			data[j][i] = data2[j];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  618) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  619) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  620) 	local_irq_restore(flags);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  621) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  622) 	parport_write_data(gc->pd->port, GC_PSX_CLOCK | GC_PSX_SELECT | GC_PSX_POWER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  623) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  624) 	/* Set id's to the real value */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  625) 	for (i = 0; i < GC_MAX_DEVICES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  626) 		id[i] = GC_PSX_ID(id[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  627) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  628) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  629) static void gc_psx_report_one(struct gc_pad *pad, unsigned char psx_type,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  630) 			      unsigned char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  631) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  632) 	struct input_dev *dev = pad->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  633) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  634) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  635) 	switch (psx_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  636) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  637) 	case GC_PSX_RUMBLE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  638) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  639) 		input_report_key(dev, BTN_THUMBL, ~data[0] & 0x04);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  640) 		input_report_key(dev, BTN_THUMBR, ~data[0] & 0x02);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  641) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  642) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  643) 	case GC_PSX_NEGCON:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  644) 	case GC_PSX_ANALOG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  645) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  646) 		if (pad->type == GC_DDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  647) 			for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  648) 				input_report_key(dev, gc_psx_ddr_btn[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  649) 						 ~data[0] & (0x10 << i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  650) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  651) 			for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  652) 				input_report_abs(dev, gc_psx_abs[i + 2],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  653) 						 data[i + 2]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  654) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  655) 			input_report_abs(dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  656) 				!!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  657) 			input_report_abs(dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  658) 				!!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  659) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  660) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  661) 		for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  662) 			input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  663) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  664) 		input_report_key(dev, BTN_START,  ~data[0] & 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  665) 		input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  666) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  667) 		input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  668) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  669) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  670) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  671) 	case GC_PSX_NORMAL:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  672) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  673) 		if (pad->type == GC_DDR) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  674) 			for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  675) 				input_report_key(dev, gc_psx_ddr_btn[i],
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  676) 						 ~data[0] & (0x10 << i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  677) 		} else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  678) 			input_report_abs(dev, ABS_X,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  679) 				!!(data[0] & 0x80) * 128 + !(data[0] & 0x20) * 127);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  680) 			input_report_abs(dev, ABS_Y,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  681) 				!!(data[0] & 0x10) * 128 + !(data[0] & 0x40) * 127);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  682) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  683) 			/*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  684) 			 * For some reason if the extra axes are left unset
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  685) 			 * they drift.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  686) 			 * for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  687) 				input_report_abs(dev, gc_psx_abs[i + 2], 128);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  688) 			 * This needs to be debugged properly,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  689) 			 * maybe fuzz processing needs to be done
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  690) 			 * in input_sync()
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  691) 			 *				 --vojtech
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  692) 			 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  693) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  694) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  695) 		for (i = 0; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  696) 			input_report_key(dev, gc_psx_btn[i], ~data[1] & (1 << i));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  697) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  698) 		input_report_key(dev, BTN_START,  ~data[0] & 0x08);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  699) 		input_report_key(dev, BTN_SELECT, ~data[0] & 0x01);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  700) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  701) 		input_sync(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  702) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  703) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  704) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  705) 	default: /* not a pad, ignore */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  706) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  707) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  708) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  709) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  710) static void gc_psx_process_packet(struct gc *gc)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  711) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  712) 	unsigned char data[GC_MAX_DEVICES][GC_PSX_BYTES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  713) 	unsigned char id[GC_MAX_DEVICES];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  714) 	struct gc_pad *pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  715) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  716) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  717) 	gc_psx_read_packet(gc, data, id);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  718) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  719) 	for (i = 0; i < GC_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  720) 		pad = &gc->pads[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  721) 		if (pad->type == GC_PSX || pad->type == GC_DDR)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  722) 			gc_psx_report_one(pad, id[i], data[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  723) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  724) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  725) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  726) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  727)  * gc_timer() initiates reads of console pads data.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  728)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  729) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  730) static void gc_timer(struct timer_list *t)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  731) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  732) 	struct gc *gc = from_timer(gc, t, timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  733) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  734) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  735)  * N64 pads - must be read first, any read confuses them for 200 us
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  736)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  737) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  738) 	if (gc->pad_count[GC_N64])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  739) 		gc_n64_process_packet(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  740) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  741) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  742)  * NES and SNES pads or mouse
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  743)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  744) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  745) 	if (gc->pad_count[GC_NES] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  746) 	    gc->pad_count[GC_SNES] ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  747) 	    gc->pad_count[GC_SNESMOUSE]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  748) 		gc_nes_process_packet(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  749) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  750) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  751) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  752)  * Multi and Multi2 joysticks
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  753)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  754) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  755) 	if (gc->pad_count[GC_MULTI] || gc->pad_count[GC_MULTI2])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  756) 		gc_multi_process_packet(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  757) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  758) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  759)  * PSX controllers
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  760)  */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  761) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  762) 	if (gc->pad_count[GC_PSX] || gc->pad_count[GC_DDR])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  763) 		gc_psx_process_packet(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  764) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  765) 	mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  766) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  767) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  768) static int gc_open(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  769) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  770) 	struct gc *gc = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  771) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  772) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  773) 	err = mutex_lock_interruptible(&gc->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  774) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  775) 		return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  776) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  777) 	if (!gc->used++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  778) 		parport_claim(gc->pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  779) 		parport_write_control(gc->pd->port, 0x04);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  780) 		mod_timer(&gc->timer, jiffies + GC_REFRESH_TIME);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  781) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  782) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  783) 	mutex_unlock(&gc->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  784) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  785) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  786) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  787) static void gc_close(struct input_dev *dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  788) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  789) 	struct gc *gc = input_get_drvdata(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  790) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  791) 	mutex_lock(&gc->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  792) 	if (!--gc->used) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  793) 		del_timer_sync(&gc->timer);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  794) 		parport_write_control(gc->pd->port, 0x00);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  795) 		parport_release(gc->pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  796) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  797) 	mutex_unlock(&gc->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  798) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  799) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  800) static int gc_setup_pad(struct gc *gc, int idx, int pad_type)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  801) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  802) 	struct gc_pad *pad = &gc->pads[idx];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  803) 	struct input_dev *input_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  804) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  805) 	int err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  806) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  807) 	if (pad_type < 1 || pad_type >= GC_MAX) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  808) 		pr_err("Pad type %d unknown\n", pad_type);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  809) 		return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  810) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  811) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  812) 	pad->dev = input_dev = input_allocate_device();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  813) 	if (!input_dev) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  814) 		pr_err("Not enough memory for input device\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  815) 		return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  816) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  817) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  818) 	pad->type = pad_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  819) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  820) 	snprintf(pad->phys, sizeof(pad->phys),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  821) 		 "%s/input%d", gc->pd->port->name, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  822) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  823) 	input_dev->name = gc_names[pad_type];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  824) 	input_dev->phys = pad->phys;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  825) 	input_dev->id.bustype = BUS_PARPORT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  826) 	input_dev->id.vendor = 0x0001;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  827) 	input_dev->id.product = pad_type;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  828) 	input_dev->id.version = 0x0100;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  829) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  830) 	input_set_drvdata(input_dev, gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  831) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  832) 	input_dev->open = gc_open;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  833) 	input_dev->close = gc_close;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  834) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  835) 	if (pad_type != GC_SNESMOUSE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  836) 		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  837) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  838) 		for (i = 0; i < 2; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  839) 			input_set_abs_params(input_dev, ABS_X + i, -1, 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  840) 	} else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  841) 		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  842) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  843) 	gc->pad_count[pad_type]++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  844) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  845) 	switch (pad_type) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  846) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  847) 	case GC_N64:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  848) 		for (i = 0; i < 10; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  849) 			input_set_capability(input_dev, EV_KEY, gc_n64_btn[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  850) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  851) 		for (i = 0; i < 2; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  852) 			input_set_abs_params(input_dev, ABS_X + i, -127, 126, 0, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  853) 			input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  854) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  855) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  856) 		err = gc_n64_init_ff(input_dev, idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  857) 		if (err) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  858) 			pr_warn("Failed to initiate rumble for N64 device %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  859) 				idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  860) 			goto err_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  861) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  862) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  863) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  864) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  865) 	case GC_SNESMOUSE:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  866) 		input_set_capability(input_dev, EV_KEY, BTN_LEFT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  867) 		input_set_capability(input_dev, EV_KEY, BTN_RIGHT);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  868) 		input_set_capability(input_dev, EV_REL, REL_X);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  869) 		input_set_capability(input_dev, EV_REL, REL_Y);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  870) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  871) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  872) 	case GC_SNES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  873) 		for (i = 4; i < 8; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  874) 			input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  875) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  876) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  877) 	case GC_NES:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  878) 		for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  879) 			input_set_capability(input_dev, EV_KEY, gc_snes_btn[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  880) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  881) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  882) 	case GC_MULTI2:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  883) 		input_set_capability(input_dev, EV_KEY, BTN_THUMB);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  884) 		fallthrough;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  885) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  886) 	case GC_MULTI:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  887) 		input_set_capability(input_dev, EV_KEY, BTN_TRIGGER);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  888) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  889) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  890) 	case GC_PSX:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  891) 		for (i = 0; i < 6; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  892) 			input_set_abs_params(input_dev,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  893) 					     gc_psx_abs[i], 4, 252, 0, 2);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  894) 		for (i = 0; i < 12; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  895) 			input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  896) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  897) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  898) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  899) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  900) 	case GC_DDR:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  901) 		for (i = 0; i < 4; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  902) 			input_set_capability(input_dev, EV_KEY,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  903) 					     gc_psx_ddr_btn[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  904) 		for (i = 0; i < 12; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  905) 			input_set_capability(input_dev, EV_KEY, gc_psx_btn[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  906) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  907) 		break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  908) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  909) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  910) 	err = input_register_device(pad->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  911) 	if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  912) 		goto err_free_dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  913) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  914) 	return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  915) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  916) err_free_dev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  917) 	input_free_device(pad->dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  918) 	pad->dev = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  919) 	return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  920) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  921) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  922) static void gc_attach(struct parport *pp)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  923) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  924) 	struct gc *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  925) 	struct pardevice *pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  926) 	int i, port_idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  927) 	int count = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  928) 	int *pads, n_pads;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  929) 	struct pardev_cb gc_parport_cb;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  930) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  931) 	for (port_idx = 0; port_idx < GC_MAX_PORTS; port_idx++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  932) 		if (gc_cfg[port_idx].nargs == 0 || gc_cfg[port_idx].args[0] < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  933) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  934) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  935) 		if (gc_cfg[port_idx].args[0] == pp->number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  936) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  937) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  938) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  939) 	if (port_idx == GC_MAX_PORTS) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  940) 		pr_debug("Not using parport%d.\n", pp->number);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  941) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  942) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  943) 	pads = gc_cfg[port_idx].args + 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  944) 	n_pads = gc_cfg[port_idx].nargs - 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  945) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  946) 	memset(&gc_parport_cb, 0, sizeof(gc_parport_cb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  947) 	gc_parport_cb.flags = PARPORT_FLAG_EXCL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  948) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  949) 	pd = parport_register_dev_model(pp, "gamecon", &gc_parport_cb,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  950) 					port_idx);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  951) 	if (!pd) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  952) 		pr_err("parport busy already - lp.o loaded?\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  953) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  954) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  955) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  956) 	gc = kzalloc(sizeof(struct gc), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  957) 	if (!gc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  958) 		pr_err("Not enough memory\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  959) 		goto err_unreg_pardev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  960) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  961) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  962) 	mutex_init(&gc->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  963) 	gc->pd = pd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  964) 	gc->parportno = pp->number;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  965) 	timer_setup(&gc->timer, gc_timer, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  966) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  967) 	for (i = 0; i < n_pads && i < GC_MAX_DEVICES; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  968) 		if (!pads[i])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  969) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  970) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  971) 		if (gc_setup_pad(gc, i, pads[i]))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  972) 			goto err_unreg_devs;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  973) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  974) 		count++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  975) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  976) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  977) 	if (count == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  978) 		pr_err("No valid devices specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  979) 		goto err_free_gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  980) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  981) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  982) 	gc_base[port_idx] = gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  983) 	return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  984) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  985)  err_unreg_devs:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  986) 	while (--i >= 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  987) 		if (gc->pads[i].dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  988) 			input_unregister_device(gc->pads[i].dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  989)  err_free_gc:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  990) 	kfree(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  991)  err_unreg_pardev:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  992) 	parport_unregister_device(pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  993) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  994) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  995) static void gc_detach(struct parport *port)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  996) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  997) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  998) 	struct gc *gc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300  999) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1000) 	for (i = 0; i < GC_MAX_PORTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1001) 		if (gc_base[i] && gc_base[i]->parportno == port->number)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1002) 			break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1003) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1004) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1005) 	if (i == GC_MAX_PORTS)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1006) 		return;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1007) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1008) 	gc = gc_base[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1009) 	gc_base[i] = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1010) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1011) 	for (i = 0; i < GC_MAX_DEVICES; i++)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1012) 		if (gc->pads[i].dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1013) 			input_unregister_device(gc->pads[i].dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1014) 	parport_unregister_device(gc->pd);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1015) 	kfree(gc);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1016) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1017) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1018) static struct parport_driver gc_parport_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1019) 	.name = "gamecon",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1020) 	.match_port = gc_attach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1021) 	.detach = gc_detach,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1022) 	.devmodel = true,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1023) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1024) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1025) static int __init gc_init(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1026) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1027) 	int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1028) 	int have_dev = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1029) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1030) 	for (i = 0; i < GC_MAX_PORTS; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1031) 		if (gc_cfg[i].nargs == 0 || gc_cfg[i].args[0] < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1032) 			continue;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1033) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1034) 		if (gc_cfg[i].nargs < 2) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1035) 			pr_err("at least one device must be specified\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1036) 			return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1037) 		}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1038) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1039) 		have_dev = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1040) 	}
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1041) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1042) 	if (!have_dev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1043) 		return -ENODEV;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1044) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1045) 	return parport_register_driver(&gc_parport_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1046) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1047) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1048) static void __exit gc_exit(void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1049) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1050) 	parport_unregister_driver(&gc_parport_driver);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1051) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1052) 
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1053) module_init(gc_init);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1054) module_exit(gc_exit);