18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Driver for the Gravis Grip Multiport, a gamepad "hub" that
48c2ecf20Sopenharmony_ci *  connects up to four 9-pin digital gamepads/joysticks.
58c2ecf20Sopenharmony_ci *  Driver tested on SMP and UP kernel versions 2.4.18-4 and 2.4.18-5.
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci *  Thanks to Chris Gassib for helpful advice.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci *  Copyright (c)      2002 Brian Bonnlander, Bill Soudan
108c2ecf20Sopenharmony_ci *  Copyright (c) 1998-2000 Vojtech Pavlik
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/kernel.h>
148c2ecf20Sopenharmony_ci#include <linux/module.h>
158c2ecf20Sopenharmony_ci#include <linux/slab.h>
168c2ecf20Sopenharmony_ci#include <linux/gameport.h>
178c2ecf20Sopenharmony_ci#include <linux/input.h>
188c2ecf20Sopenharmony_ci#include <linux/delay.h>
198c2ecf20Sopenharmony_ci#include <linux/proc_fs.h>
208c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#define DRIVER_DESC	"Gravis Grip Multiport driver"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ciMODULE_AUTHOR("Brian Bonnlander");
258c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
268c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#ifdef GRIP_DEBUG
298c2ecf20Sopenharmony_ci#define dbg(format, arg...) printk(KERN_ERR __FILE__ ": " format "\n" , ## arg)
308c2ecf20Sopenharmony_ci#else
318c2ecf20Sopenharmony_ci#define dbg(format, arg...) do {} while (0)
328c2ecf20Sopenharmony_ci#endif
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define GRIP_MAX_PORTS	4
358c2ecf20Sopenharmony_ci/*
368c2ecf20Sopenharmony_ci * Grip multiport state
378c2ecf20Sopenharmony_ci */
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_cistruct grip_port {
408c2ecf20Sopenharmony_ci	struct input_dev *dev;
418c2ecf20Sopenharmony_ci	int mode;
428c2ecf20Sopenharmony_ci	int registered;
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	/* individual gamepad states */
458c2ecf20Sopenharmony_ci	int buttons;
468c2ecf20Sopenharmony_ci	int xaxes;
478c2ecf20Sopenharmony_ci	int yaxes;
488c2ecf20Sopenharmony_ci	int dirty;     /* has the state been updated? */
498c2ecf20Sopenharmony_ci};
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_cistruct grip_mp {
528c2ecf20Sopenharmony_ci	struct gameport *gameport;
538c2ecf20Sopenharmony_ci	struct grip_port *port[GRIP_MAX_PORTS];
548c2ecf20Sopenharmony_ci	int reads;
558c2ecf20Sopenharmony_ci	int bads;
568c2ecf20Sopenharmony_ci};
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci/*
598c2ecf20Sopenharmony_ci * Multiport packet interpretation
608c2ecf20Sopenharmony_ci */
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci#define PACKET_FULL          0x80000000       /* packet is full                        */
638c2ecf20Sopenharmony_ci#define PACKET_IO_FAST       0x40000000       /* 3 bits per gameport read              */
648c2ecf20Sopenharmony_ci#define PACKET_IO_SLOW       0x20000000       /* 1 bit per gameport read               */
658c2ecf20Sopenharmony_ci#define PACKET_MP_MORE       0x04000000       /* multiport wants to send more          */
668c2ecf20Sopenharmony_ci#define PACKET_MP_DONE       0x02000000       /* multiport done sending                */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/*
698c2ecf20Sopenharmony_ci * Packet status code interpretation
708c2ecf20Sopenharmony_ci */
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define IO_GOT_PACKET        0x0100           /* Got a packet                           */
738c2ecf20Sopenharmony_ci#define IO_MODE_FAST         0x0200           /* Used 3 data bits per gameport read     */
748c2ecf20Sopenharmony_ci#define IO_SLOT_CHANGE       0x0800           /* Multiport physical slot status changed */
758c2ecf20Sopenharmony_ci#define IO_DONE              0x1000           /* Multiport is done sending packets      */
768c2ecf20Sopenharmony_ci#define IO_RETRY             0x4000           /* Try again later to get packet          */
778c2ecf20Sopenharmony_ci#define IO_RESET             0x8000           /* Force multiport to resend all packets  */
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci/*
808c2ecf20Sopenharmony_ci * Gamepad configuration data.  Other 9-pin digital joystick devices
818c2ecf20Sopenharmony_ci * may work with the multiport, so this may not be an exhaustive list!
828c2ecf20Sopenharmony_ci * Commodore 64 joystick remains untested.
838c2ecf20Sopenharmony_ci */
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define GRIP_INIT_DELAY         2000          /*  2 ms */
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci#define GRIP_MODE_NONE		0
888c2ecf20Sopenharmony_ci#define GRIP_MODE_RESET         1
898c2ecf20Sopenharmony_ci#define GRIP_MODE_GP		2
908c2ecf20Sopenharmony_ci#define GRIP_MODE_C64		3
918c2ecf20Sopenharmony_ci
928c2ecf20Sopenharmony_cistatic const int grip_btn_gp[]  = { BTN_TR, BTN_TL, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, -1 };
938c2ecf20Sopenharmony_cistatic const int grip_btn_c64[] = { BTN_JOYSTICK, -1 };
948c2ecf20Sopenharmony_ci
958c2ecf20Sopenharmony_cistatic const int grip_abs_gp[]  = { ABS_X, ABS_Y, -1 };
968c2ecf20Sopenharmony_cistatic const int grip_abs_c64[] = { ABS_X, ABS_Y, -1 };
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic const int *grip_abs[] = { NULL, NULL, grip_abs_gp, grip_abs_c64 };
998c2ecf20Sopenharmony_cistatic const int *grip_btn[] = { NULL, NULL, grip_btn_gp, grip_btn_c64 };
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_cistatic const char *grip_name[] = { NULL, NULL, "Gravis Grip Pad", "Commodore 64 Joystick" };
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic const int init_seq[] = {
1048c2ecf20Sopenharmony_ci	1, 0, 1, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1058c2ecf20Sopenharmony_ci	1, 0, 1, 0, 1, 0, 1, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1,
1068c2ecf20Sopenharmony_ci	1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1,
1078c2ecf20Sopenharmony_ci	0, 1, 1, 0, 1, 1, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 1, 1 };
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* Maps multiport directional values to X,Y axis values (each axis encoded in 3 bits) */
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic const int axis_map[] = { 5, 9, 1, 5, 6, 10, 2, 6, 4, 8, 0, 4, 5, 9, 1, 5 };
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_cistatic int register_slot(int i, struct grip_mp *grip);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci/*
1168c2ecf20Sopenharmony_ci * Returns whether an odd or even number of bits are on in pkt.
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int bit_parity(u32 pkt)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	int x = pkt ^ (pkt >> 16);
1228c2ecf20Sopenharmony_ci	x ^= x >> 8;
1238c2ecf20Sopenharmony_ci	x ^= x >> 4;
1248c2ecf20Sopenharmony_ci	x ^= x >> 2;
1258c2ecf20Sopenharmony_ci	x ^= x >> 1;
1268c2ecf20Sopenharmony_ci	return x & 1;
1278c2ecf20Sopenharmony_ci}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci/*
1308c2ecf20Sopenharmony_ci * Poll gameport; return true if all bits set in 'onbits' are on and
1318c2ecf20Sopenharmony_ci * all bits set in 'offbits' are off.
1328c2ecf20Sopenharmony_ci */
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_cistatic inline int poll_until(u8 onbits, u8 offbits, int u_sec, struct gameport* gp, u8 *data)
1358c2ecf20Sopenharmony_ci{
1368c2ecf20Sopenharmony_ci	int i, nloops;
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci	nloops = gameport_time(gp, u_sec);
1398c2ecf20Sopenharmony_ci	for (i = 0; i < nloops; i++) {
1408c2ecf20Sopenharmony_ci		*data = gameport_read(gp);
1418c2ecf20Sopenharmony_ci		if ((*data & onbits) == onbits &&
1428c2ecf20Sopenharmony_ci		    (~(*data) & offbits) == offbits)
1438c2ecf20Sopenharmony_ci			return 1;
1448c2ecf20Sopenharmony_ci	}
1458c2ecf20Sopenharmony_ci	dbg("gameport timed out after %d microseconds.\n", u_sec);
1468c2ecf20Sopenharmony_ci	return 0;
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci/*
1508c2ecf20Sopenharmony_ci * Gets a 28-bit packet from the multiport.
1518c2ecf20Sopenharmony_ci *
1528c2ecf20Sopenharmony_ci * After getting a packet successfully, commands encoded by sendcode may
1538c2ecf20Sopenharmony_ci * be sent to the multiport.
1548c2ecf20Sopenharmony_ci *
1558c2ecf20Sopenharmony_ci * The multiport clock value is reflected in gameport bit B4.
1568c2ecf20Sopenharmony_ci *
1578c2ecf20Sopenharmony_ci * Returns a packet status code indicating whether packet is valid, the transfer
1588c2ecf20Sopenharmony_ci * mode, and any error conditions.
1598c2ecf20Sopenharmony_ci *
1608c2ecf20Sopenharmony_ci * sendflags:      current I/O status
1618c2ecf20Sopenharmony_ci * sendcode:   data to send to the multiport if sendflags is nonzero
1628c2ecf20Sopenharmony_ci */
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_cistatic int mp_io(struct gameport* gameport, int sendflags, int sendcode, u32 *packet)
1658c2ecf20Sopenharmony_ci{
1668c2ecf20Sopenharmony_ci	u8  raw_data;            /* raw data from gameport */
1678c2ecf20Sopenharmony_ci	u8  data_mask;           /* packet data bits from raw_data */
1688c2ecf20Sopenharmony_ci	u32 pkt;                 /* packet temporary storage */
1698c2ecf20Sopenharmony_ci	int bits_per_read;       /* num packet bits per gameport read */
1708c2ecf20Sopenharmony_ci	int portvals = 0;        /* used for port value sanity check */
1718c2ecf20Sopenharmony_ci	int i;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	/* Gameport bits B0, B4, B5 should first be off, then B4 should come on. */
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	*packet = 0;
1768c2ecf20Sopenharmony_ci	raw_data = gameport_read(gameport);
1778c2ecf20Sopenharmony_ci	if (raw_data & 1)
1788c2ecf20Sopenharmony_ci		return IO_RETRY;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	for (i = 0; i < 64; i++) {
1818c2ecf20Sopenharmony_ci		raw_data = gameport_read(gameport);
1828c2ecf20Sopenharmony_ci		portvals |= 1 << ((raw_data >> 4) & 3); /* Demux B4, B5 */
1838c2ecf20Sopenharmony_ci	}
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	if (portvals == 1) {                            /* B4, B5 off */
1868c2ecf20Sopenharmony_ci		raw_data = gameport_read(gameport);
1878c2ecf20Sopenharmony_ci		portvals = raw_data & 0xf0;
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci		if (raw_data & 0x31)
1908c2ecf20Sopenharmony_ci			return IO_RESET;
1918c2ecf20Sopenharmony_ci		gameport_trigger(gameport);
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci		if (!poll_until(0x10, 0, 308, gameport, &raw_data))
1948c2ecf20Sopenharmony_ci			return IO_RESET;
1958c2ecf20Sopenharmony_ci	} else
1968c2ecf20Sopenharmony_ci		return IO_RETRY;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	/* Determine packet transfer mode and prepare for packet construction. */
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	if (raw_data & 0x20) {                 /* 3 data bits/read */
2018c2ecf20Sopenharmony_ci		portvals |= raw_data >> 4;     /* Compare B4-B7 before & after trigger */
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci		if (portvals != 0xb)
2048c2ecf20Sopenharmony_ci			return 0;
2058c2ecf20Sopenharmony_ci		data_mask = 7;
2068c2ecf20Sopenharmony_ci		bits_per_read = 3;
2078c2ecf20Sopenharmony_ci		pkt = (PACKET_FULL | PACKET_IO_FAST) >> 28;
2088c2ecf20Sopenharmony_ci	} else {                                 /* 1 data bit/read */
2098c2ecf20Sopenharmony_ci		data_mask = 1;
2108c2ecf20Sopenharmony_ci		bits_per_read = 1;
2118c2ecf20Sopenharmony_ci		pkt = (PACKET_FULL | PACKET_IO_SLOW) >> 28;
2128c2ecf20Sopenharmony_ci	}
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	/* Construct a packet.  Final data bits must be zero. */
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	while (1) {
2178c2ecf20Sopenharmony_ci		if (!poll_until(0, 0x10, 77, gameport, &raw_data))
2188c2ecf20Sopenharmony_ci			return IO_RESET;
2198c2ecf20Sopenharmony_ci		raw_data = (raw_data >> 5) & data_mask;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci		if (pkt & PACKET_FULL)
2228c2ecf20Sopenharmony_ci			break;
2238c2ecf20Sopenharmony_ci		pkt = (pkt << bits_per_read) | raw_data;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci		if (!poll_until(0x10, 0, 77, gameport, &raw_data))
2268c2ecf20Sopenharmony_ci			return IO_RESET;
2278c2ecf20Sopenharmony_ci	}
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci	if (raw_data)
2308c2ecf20Sopenharmony_ci		return IO_RESET;
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	/* If 3 bits/read used, drop from 30 bits to 28. */
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci	if (bits_per_read == 3) {
2358c2ecf20Sopenharmony_ci		pkt = (pkt & 0xffff0000) | ((pkt << 1) & 0xffff);
2368c2ecf20Sopenharmony_ci		pkt = (pkt >> 2) | 0xf0000000;
2378c2ecf20Sopenharmony_ci	}
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_ci	if (bit_parity(pkt) == 1)
2408c2ecf20Sopenharmony_ci		return IO_RESET;
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	/* Acknowledge packet receipt */
2438c2ecf20Sopenharmony_ci
2448c2ecf20Sopenharmony_ci	if (!poll_until(0x30, 0, 77, gameport, &raw_data))
2458c2ecf20Sopenharmony_ci		return IO_RESET;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	raw_data = gameport_read(gameport);
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	if (raw_data & 1)
2508c2ecf20Sopenharmony_ci		return IO_RESET;
2518c2ecf20Sopenharmony_ci
2528c2ecf20Sopenharmony_ci	gameport_trigger(gameport);
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	if (!poll_until(0, 0x20, 77, gameport, &raw_data))
2558c2ecf20Sopenharmony_ci		return IO_RESET;
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci        /* Return if we just wanted the packet or multiport wants to send more */
2588c2ecf20Sopenharmony_ci
2598c2ecf20Sopenharmony_ci	*packet = pkt;
2608c2ecf20Sopenharmony_ci	if ((sendflags == 0) || ((sendflags & IO_RETRY) && !(pkt & PACKET_MP_DONE)))
2618c2ecf20Sopenharmony_ci		return IO_GOT_PACKET;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	if (pkt & PACKET_MP_MORE)
2648c2ecf20Sopenharmony_ci		return IO_GOT_PACKET | IO_RETRY;
2658c2ecf20Sopenharmony_ci
2668c2ecf20Sopenharmony_ci	/* Multiport is done sending packets and is ready to receive data */
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_ci	if (!poll_until(0x20, 0, 77, gameport, &raw_data))
2698c2ecf20Sopenharmony_ci		return IO_GOT_PACKET | IO_RESET;
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	raw_data = gameport_read(gameport);
2728c2ecf20Sopenharmony_ci	if (raw_data & 1)
2738c2ecf20Sopenharmony_ci		return IO_GOT_PACKET | IO_RESET;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	/* Trigger gameport based on bits in sendcode */
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	gameport_trigger(gameport);
2788c2ecf20Sopenharmony_ci	do {
2798c2ecf20Sopenharmony_ci		if (!poll_until(0x20, 0x10, 116, gameport, &raw_data))
2808c2ecf20Sopenharmony_ci			return IO_GOT_PACKET | IO_RESET;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci		if (!poll_until(0x30, 0, 193, gameport, &raw_data))
2838c2ecf20Sopenharmony_ci			return IO_GOT_PACKET | IO_RESET;
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_ci		if (raw_data & 1)
2868c2ecf20Sopenharmony_ci			return IO_GOT_PACKET | IO_RESET;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci		if (sendcode & 1)
2898c2ecf20Sopenharmony_ci			gameport_trigger(gameport);
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_ci		sendcode >>= 1;
2928c2ecf20Sopenharmony_ci	} while (sendcode);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return IO_GOT_PACKET | IO_MODE_FAST;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci/*
2988c2ecf20Sopenharmony_ci * Disables and restores interrupts for mp_io(), which does the actual I/O.
2998c2ecf20Sopenharmony_ci */
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_cistatic int multiport_io(struct gameport* gameport, int sendflags, int sendcode, u32 *packet)
3028c2ecf20Sopenharmony_ci{
3038c2ecf20Sopenharmony_ci	int status;
3048c2ecf20Sopenharmony_ci	unsigned long flags;
3058c2ecf20Sopenharmony_ci
3068c2ecf20Sopenharmony_ci	local_irq_save(flags);
3078c2ecf20Sopenharmony_ci	status = mp_io(gameport, sendflags, sendcode, packet);
3088c2ecf20Sopenharmony_ci	local_irq_restore(flags);
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci	return status;
3118c2ecf20Sopenharmony_ci}
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci/*
3148c2ecf20Sopenharmony_ci * Puts multiport into digital mode.  Multiport LED turns green.
3158c2ecf20Sopenharmony_ci *
3168c2ecf20Sopenharmony_ci * Returns true if a valid digital packet was received, false otherwise.
3178c2ecf20Sopenharmony_ci */
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_cistatic int dig_mode_start(struct gameport *gameport, u32 *packet)
3208c2ecf20Sopenharmony_ci{
3218c2ecf20Sopenharmony_ci	int i;
3228c2ecf20Sopenharmony_ci	int flags, tries = 0, bads = 0;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(init_seq); i++) {     /* Send magic sequence */
3258c2ecf20Sopenharmony_ci		if (init_seq[i])
3268c2ecf20Sopenharmony_ci			gameport_trigger(gameport);
3278c2ecf20Sopenharmony_ci		udelay(GRIP_INIT_DELAY);
3288c2ecf20Sopenharmony_ci	}
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci	for (i = 0; i < 16; i++)            /* Wait for multiport to settle */
3318c2ecf20Sopenharmony_ci		udelay(GRIP_INIT_DELAY);
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci	while (tries < 64 && bads < 8) {    /* Reset multiport and try getting a packet */
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		flags = multiport_io(gameport, IO_RESET, 0x27, packet);
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci		if (flags & IO_MODE_FAST)
3388c2ecf20Sopenharmony_ci			return 1;
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci		if (flags & IO_RETRY)
3418c2ecf20Sopenharmony_ci			tries++;
3428c2ecf20Sopenharmony_ci		else
3438c2ecf20Sopenharmony_ci			bads++;
3448c2ecf20Sopenharmony_ci	}
3458c2ecf20Sopenharmony_ci	return 0;
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_ci/*
3498c2ecf20Sopenharmony_ci * Packet structure: B0-B15   => gamepad state
3508c2ecf20Sopenharmony_ci *                   B16-B20  => gamepad device type
3518c2ecf20Sopenharmony_ci *                   B21-B24  => multiport slot index (1-4)
3528c2ecf20Sopenharmony_ci *
3538c2ecf20Sopenharmony_ci * Known device types: 0x1f (grip pad), 0x0 (no device).  Others may exist.
3548c2ecf20Sopenharmony_ci *
3558c2ecf20Sopenharmony_ci * Returns the packet status.
3568c2ecf20Sopenharmony_ci */
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_cistatic int get_and_decode_packet(struct grip_mp *grip, int flags)
3598c2ecf20Sopenharmony_ci{
3608c2ecf20Sopenharmony_ci	struct grip_port *port;
3618c2ecf20Sopenharmony_ci	u32 packet;
3628c2ecf20Sopenharmony_ci	int joytype = 0;
3638c2ecf20Sopenharmony_ci	int slot;
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	/* Get a packet and check for validity */
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	flags &= IO_RESET | IO_RETRY;
3688c2ecf20Sopenharmony_ci	flags = multiport_io(grip->gameport, flags, 0, &packet);
3698c2ecf20Sopenharmony_ci	grip->reads++;
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	if (packet & PACKET_MP_DONE)
3728c2ecf20Sopenharmony_ci		flags |= IO_DONE;
3738c2ecf20Sopenharmony_ci
3748c2ecf20Sopenharmony_ci	if (flags && !(flags & IO_GOT_PACKET)) {
3758c2ecf20Sopenharmony_ci		grip->bads++;
3768c2ecf20Sopenharmony_ci		return flags;
3778c2ecf20Sopenharmony_ci	}
3788c2ecf20Sopenharmony_ci
3798c2ecf20Sopenharmony_ci	/* Ignore non-gamepad packets, e.g. multiport hardware version */
3808c2ecf20Sopenharmony_ci
3818c2ecf20Sopenharmony_ci	slot = ((packet >> 21) & 0xf) - 1;
3828c2ecf20Sopenharmony_ci	if ((slot < 0) || (slot > 3))
3838c2ecf20Sopenharmony_ci		return flags;
3848c2ecf20Sopenharmony_ci
3858c2ecf20Sopenharmony_ci	port = grip->port[slot];
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci	/*
3888c2ecf20Sopenharmony_ci	 * Handle "reset" packets, which occur at startup, and when gamepads
3898c2ecf20Sopenharmony_ci	 * are removed or plugged in.  May contain configuration of a new gamepad.
3908c2ecf20Sopenharmony_ci	 */
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	joytype = (packet >> 16) & 0x1f;
3938c2ecf20Sopenharmony_ci	if (!joytype) {
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci		if (port->registered) {
3968c2ecf20Sopenharmony_ci			printk(KERN_INFO "grip_mp: removing %s, slot %d\n",
3978c2ecf20Sopenharmony_ci			       grip_name[port->mode], slot);
3988c2ecf20Sopenharmony_ci			input_unregister_device(port->dev);
3998c2ecf20Sopenharmony_ci			port->registered = 0;
4008c2ecf20Sopenharmony_ci		}
4018c2ecf20Sopenharmony_ci		dbg("Reset: grip multiport slot %d\n", slot);
4028c2ecf20Sopenharmony_ci		port->mode = GRIP_MODE_RESET;
4038c2ecf20Sopenharmony_ci		flags |= IO_SLOT_CHANGE;
4048c2ecf20Sopenharmony_ci		return flags;
4058c2ecf20Sopenharmony_ci	}
4068c2ecf20Sopenharmony_ci
4078c2ecf20Sopenharmony_ci	/* Interpret a grip pad packet */
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_ci	if (joytype == 0x1f) {
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci		int dir = (packet >> 8) & 0xf;          /* eight way directional value */
4128c2ecf20Sopenharmony_ci		port->buttons = (~packet) & 0xff;
4138c2ecf20Sopenharmony_ci		port->yaxes = ((axis_map[dir] >> 2) & 3) - 1;
4148c2ecf20Sopenharmony_ci		port->xaxes = (axis_map[dir] & 3) - 1;
4158c2ecf20Sopenharmony_ci		port->dirty = 1;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci		if (port->mode == GRIP_MODE_RESET)
4188c2ecf20Sopenharmony_ci			flags |= IO_SLOT_CHANGE;
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci		port->mode = GRIP_MODE_GP;
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci		if (!port->registered) {
4238c2ecf20Sopenharmony_ci			dbg("New Grip pad in multiport slot %d.\n", slot);
4248c2ecf20Sopenharmony_ci			if (register_slot(slot, grip)) {
4258c2ecf20Sopenharmony_ci				port->mode = GRIP_MODE_RESET;
4268c2ecf20Sopenharmony_ci				port->dirty = 0;
4278c2ecf20Sopenharmony_ci			}
4288c2ecf20Sopenharmony_ci		}
4298c2ecf20Sopenharmony_ci		return flags;
4308c2ecf20Sopenharmony_ci	}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_ci	/* Handle non-grip device codes.  For now, just print diagnostics. */
4338c2ecf20Sopenharmony_ci
4348c2ecf20Sopenharmony_ci	{
4358c2ecf20Sopenharmony_ci		static int strange_code = 0;
4368c2ecf20Sopenharmony_ci		if (strange_code != joytype) {
4378c2ecf20Sopenharmony_ci			printk(KERN_INFO "Possible non-grip pad/joystick detected.\n");
4388c2ecf20Sopenharmony_ci			printk(KERN_INFO "Got joy type 0x%x and packet 0x%x.\n", joytype, packet);
4398c2ecf20Sopenharmony_ci			strange_code = joytype;
4408c2ecf20Sopenharmony_ci		}
4418c2ecf20Sopenharmony_ci	}
4428c2ecf20Sopenharmony_ci	return flags;
4438c2ecf20Sopenharmony_ci}
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci/*
4468c2ecf20Sopenharmony_ci * Returns true if all multiport slot states appear valid.
4478c2ecf20Sopenharmony_ci */
4488c2ecf20Sopenharmony_ci
4498c2ecf20Sopenharmony_cistatic int slots_valid(struct grip_mp *grip)
4508c2ecf20Sopenharmony_ci{
4518c2ecf20Sopenharmony_ci	int flags, slot, invalid = 0, active = 0;
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	flags = get_and_decode_packet(grip, 0);
4548c2ecf20Sopenharmony_ci	if (!(flags & IO_GOT_PACKET))
4558c2ecf20Sopenharmony_ci		return 0;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci	for (slot = 0; slot < 4; slot++) {
4588c2ecf20Sopenharmony_ci		if (grip->port[slot]->mode == GRIP_MODE_RESET)
4598c2ecf20Sopenharmony_ci			invalid = 1;
4608c2ecf20Sopenharmony_ci		if (grip->port[slot]->mode != GRIP_MODE_NONE)
4618c2ecf20Sopenharmony_ci			active = 1;
4628c2ecf20Sopenharmony_ci	}
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	/* Return true if no active slot but multiport sent all its data */
4658c2ecf20Sopenharmony_ci	if (!active)
4668c2ecf20Sopenharmony_ci		return (flags & IO_DONE) ? 1 : 0;
4678c2ecf20Sopenharmony_ci
4688c2ecf20Sopenharmony_ci	/* Return false if invalid device code received */
4698c2ecf20Sopenharmony_ci	return invalid ? 0 : 1;
4708c2ecf20Sopenharmony_ci}
4718c2ecf20Sopenharmony_ci
4728c2ecf20Sopenharmony_ci/*
4738c2ecf20Sopenharmony_ci * Returns whether the multiport was placed into digital mode and
4748c2ecf20Sopenharmony_ci * able to communicate its state successfully.
4758c2ecf20Sopenharmony_ci */
4768c2ecf20Sopenharmony_ci
4778c2ecf20Sopenharmony_cistatic int multiport_init(struct grip_mp *grip)
4788c2ecf20Sopenharmony_ci{
4798c2ecf20Sopenharmony_ci	int dig_mode, initialized = 0, tries = 0;
4808c2ecf20Sopenharmony_ci	u32 packet;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	dig_mode = dig_mode_start(grip->gameport, &packet);
4838c2ecf20Sopenharmony_ci	while (!dig_mode && tries < 4) {
4848c2ecf20Sopenharmony_ci		dig_mode = dig_mode_start(grip->gameport, &packet);
4858c2ecf20Sopenharmony_ci		tries++;
4868c2ecf20Sopenharmony_ci	}
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	if (dig_mode)
4898c2ecf20Sopenharmony_ci		dbg("multiport_init(): digital mode activated.\n");
4908c2ecf20Sopenharmony_ci	else {
4918c2ecf20Sopenharmony_ci		dbg("multiport_init(): unable to activate digital mode.\n");
4928c2ecf20Sopenharmony_ci		return 0;
4938c2ecf20Sopenharmony_ci	}
4948c2ecf20Sopenharmony_ci
4958c2ecf20Sopenharmony_ci	/* Get packets, store multiport state, and check state's validity */
4968c2ecf20Sopenharmony_ci	for (tries = 0; tries < 4096; tries++) {
4978c2ecf20Sopenharmony_ci		if (slots_valid(grip)) {
4988c2ecf20Sopenharmony_ci			initialized = 1;
4998c2ecf20Sopenharmony_ci			break;
5008c2ecf20Sopenharmony_ci		}
5018c2ecf20Sopenharmony_ci	}
5028c2ecf20Sopenharmony_ci	dbg("multiport_init(): initialized == %d\n", initialized);
5038c2ecf20Sopenharmony_ci	return initialized;
5048c2ecf20Sopenharmony_ci}
5058c2ecf20Sopenharmony_ci
5068c2ecf20Sopenharmony_ci/*
5078c2ecf20Sopenharmony_ci * Reports joystick state to the linux input layer.
5088c2ecf20Sopenharmony_ci */
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_cistatic void report_slot(struct grip_mp *grip, int slot)
5118c2ecf20Sopenharmony_ci{
5128c2ecf20Sopenharmony_ci	struct grip_port *port = grip->port[slot];
5138c2ecf20Sopenharmony_ci	int i;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	/* Store button states with linux input driver */
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++)
5188c2ecf20Sopenharmony_ci		input_report_key(port->dev, grip_btn_gp[i], (port->buttons >> i) & 1);
5198c2ecf20Sopenharmony_ci
5208c2ecf20Sopenharmony_ci	/* Store axis states with linux driver */
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci	input_report_abs(port->dev, ABS_X, port->xaxes);
5238c2ecf20Sopenharmony_ci	input_report_abs(port->dev, ABS_Y, port->yaxes);
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	/* Tell the receiver of the events to process them */
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	input_sync(port->dev);
5288c2ecf20Sopenharmony_ci
5298c2ecf20Sopenharmony_ci	port->dirty = 0;
5308c2ecf20Sopenharmony_ci}
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci/*
5338c2ecf20Sopenharmony_ci * Get the multiport state.
5348c2ecf20Sopenharmony_ci */
5358c2ecf20Sopenharmony_ci
5368c2ecf20Sopenharmony_cistatic void grip_poll(struct gameport *gameport)
5378c2ecf20Sopenharmony_ci{
5388c2ecf20Sopenharmony_ci	struct grip_mp *grip = gameport_get_drvdata(gameport);
5398c2ecf20Sopenharmony_ci	int i, npkts, flags;
5408c2ecf20Sopenharmony_ci
5418c2ecf20Sopenharmony_ci	for (npkts = 0; npkts < 4; npkts++) {
5428c2ecf20Sopenharmony_ci		flags = IO_RETRY;
5438c2ecf20Sopenharmony_ci		for (i = 0; i < 32; i++) {
5448c2ecf20Sopenharmony_ci			flags = get_and_decode_packet(grip, flags);
5458c2ecf20Sopenharmony_ci			if ((flags & IO_GOT_PACKET) || !(flags & IO_RETRY))
5468c2ecf20Sopenharmony_ci				break;
5478c2ecf20Sopenharmony_ci		}
5488c2ecf20Sopenharmony_ci		if (flags & IO_DONE)
5498c2ecf20Sopenharmony_ci			break;
5508c2ecf20Sopenharmony_ci	}
5518c2ecf20Sopenharmony_ci
5528c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++)
5538c2ecf20Sopenharmony_ci		if (grip->port[i]->dirty)
5548c2ecf20Sopenharmony_ci			report_slot(grip, i);
5558c2ecf20Sopenharmony_ci}
5568c2ecf20Sopenharmony_ci
5578c2ecf20Sopenharmony_ci/*
5588c2ecf20Sopenharmony_ci * Called when a joystick device file is opened
5598c2ecf20Sopenharmony_ci */
5608c2ecf20Sopenharmony_ci
5618c2ecf20Sopenharmony_cistatic int grip_open(struct input_dev *dev)
5628c2ecf20Sopenharmony_ci{
5638c2ecf20Sopenharmony_ci	struct grip_mp *grip = input_get_drvdata(dev);
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	gameport_start_polling(grip->gameport);
5668c2ecf20Sopenharmony_ci	return 0;
5678c2ecf20Sopenharmony_ci}
5688c2ecf20Sopenharmony_ci
5698c2ecf20Sopenharmony_ci/*
5708c2ecf20Sopenharmony_ci * Called when a joystick device file is closed
5718c2ecf20Sopenharmony_ci */
5728c2ecf20Sopenharmony_ci
5738c2ecf20Sopenharmony_cistatic void grip_close(struct input_dev *dev)
5748c2ecf20Sopenharmony_ci{
5758c2ecf20Sopenharmony_ci	struct grip_mp *grip = input_get_drvdata(dev);
5768c2ecf20Sopenharmony_ci
5778c2ecf20Sopenharmony_ci	gameport_stop_polling(grip->gameport);
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci/*
5818c2ecf20Sopenharmony_ci * Tell the linux input layer about a newly plugged-in gamepad.
5828c2ecf20Sopenharmony_ci */
5838c2ecf20Sopenharmony_ci
5848c2ecf20Sopenharmony_cistatic int register_slot(int slot, struct grip_mp *grip)
5858c2ecf20Sopenharmony_ci{
5868c2ecf20Sopenharmony_ci	struct grip_port *port = grip->port[slot];
5878c2ecf20Sopenharmony_ci	struct input_dev *input_dev;
5888c2ecf20Sopenharmony_ci	int j, t;
5898c2ecf20Sopenharmony_ci	int err;
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	port->dev = input_dev = input_allocate_device();
5928c2ecf20Sopenharmony_ci	if (!input_dev)
5938c2ecf20Sopenharmony_ci		return -ENOMEM;
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	input_dev->name = grip_name[port->mode];
5968c2ecf20Sopenharmony_ci	input_dev->id.bustype = BUS_GAMEPORT;
5978c2ecf20Sopenharmony_ci	input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
5988c2ecf20Sopenharmony_ci	input_dev->id.product = 0x0100 + port->mode;
5998c2ecf20Sopenharmony_ci	input_dev->id.version = 0x0100;
6008c2ecf20Sopenharmony_ci	input_dev->dev.parent = &grip->gameport->dev;
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	input_set_drvdata(input_dev, grip);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	input_dev->open = grip_open;
6058c2ecf20Sopenharmony_ci	input_dev->close = grip_close;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
6088c2ecf20Sopenharmony_ci
6098c2ecf20Sopenharmony_ci	for (j = 0; (t = grip_abs[port->mode][j]) >= 0; j++)
6108c2ecf20Sopenharmony_ci		input_set_abs_params(input_dev, t, -1, 1, 0, 0);
6118c2ecf20Sopenharmony_ci
6128c2ecf20Sopenharmony_ci	for (j = 0; (t = grip_btn[port->mode][j]) >= 0; j++)
6138c2ecf20Sopenharmony_ci		if (t > 0)
6148c2ecf20Sopenharmony_ci			set_bit(t, input_dev->keybit);
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	err = input_register_device(port->dev);
6178c2ecf20Sopenharmony_ci	if (err) {
6188c2ecf20Sopenharmony_ci		input_free_device(port->dev);
6198c2ecf20Sopenharmony_ci		return err;
6208c2ecf20Sopenharmony_ci	}
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	port->registered = 1;
6238c2ecf20Sopenharmony_ci
6248c2ecf20Sopenharmony_ci	if (port->dirty)	            /* report initial state, if any */
6258c2ecf20Sopenharmony_ci		report_slot(grip, slot);
6268c2ecf20Sopenharmony_ci
6278c2ecf20Sopenharmony_ci	return 0;
6288c2ecf20Sopenharmony_ci}
6298c2ecf20Sopenharmony_ci
6308c2ecf20Sopenharmony_cistatic int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	struct grip_mp *grip;
6338c2ecf20Sopenharmony_ci	int err;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	if (!(grip = kzalloc(sizeof(struct grip_mp), GFP_KERNEL)))
6368c2ecf20Sopenharmony_ci		return -ENOMEM;
6378c2ecf20Sopenharmony_ci
6388c2ecf20Sopenharmony_ci	grip->gameport = gameport;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	gameport_set_drvdata(gameport, grip);
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_ci	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
6438c2ecf20Sopenharmony_ci	if (err)
6448c2ecf20Sopenharmony_ci		goto fail1;
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	gameport_set_poll_handler(gameport, grip_poll);
6478c2ecf20Sopenharmony_ci	gameport_set_poll_interval(gameport, 20);
6488c2ecf20Sopenharmony_ci
6498c2ecf20Sopenharmony_ci	if (!multiport_init(grip)) {
6508c2ecf20Sopenharmony_ci		err = -ENODEV;
6518c2ecf20Sopenharmony_ci		goto fail2;
6528c2ecf20Sopenharmony_ci	}
6538c2ecf20Sopenharmony_ci
6548c2ecf20Sopenharmony_ci	if (!grip->port[0]->mode && !grip->port[1]->mode && !grip->port[2]->mode && !grip->port[3]->mode) {
6558c2ecf20Sopenharmony_ci		/* nothing plugged in */
6568c2ecf20Sopenharmony_ci		err = -ENODEV;
6578c2ecf20Sopenharmony_ci		goto fail2;
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci
6608c2ecf20Sopenharmony_ci	return 0;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_cifail2:	gameport_close(gameport);
6638c2ecf20Sopenharmony_cifail1:	gameport_set_drvdata(gameport, NULL);
6648c2ecf20Sopenharmony_ci	kfree(grip);
6658c2ecf20Sopenharmony_ci	return err;
6668c2ecf20Sopenharmony_ci}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_cistatic void grip_disconnect(struct gameport *gameport)
6698c2ecf20Sopenharmony_ci{
6708c2ecf20Sopenharmony_ci	struct grip_mp *grip = gameport_get_drvdata(gameport);
6718c2ecf20Sopenharmony_ci	int i;
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	for (i = 0; i < 4; i++)
6748c2ecf20Sopenharmony_ci		if (grip->port[i]->registered)
6758c2ecf20Sopenharmony_ci			input_unregister_device(grip->port[i]->dev);
6768c2ecf20Sopenharmony_ci	gameport_close(gameport);
6778c2ecf20Sopenharmony_ci	gameport_set_drvdata(gameport, NULL);
6788c2ecf20Sopenharmony_ci	kfree(grip);
6798c2ecf20Sopenharmony_ci}
6808c2ecf20Sopenharmony_ci
6818c2ecf20Sopenharmony_cistatic struct gameport_driver grip_drv = {
6828c2ecf20Sopenharmony_ci	.driver		= {
6838c2ecf20Sopenharmony_ci		.name	= "grip_mp",
6848c2ecf20Sopenharmony_ci	},
6858c2ecf20Sopenharmony_ci	.description	= DRIVER_DESC,
6868c2ecf20Sopenharmony_ci	.connect	= grip_connect,
6878c2ecf20Sopenharmony_ci	.disconnect	= grip_disconnect,
6888c2ecf20Sopenharmony_ci};
6898c2ecf20Sopenharmony_ci
6908c2ecf20Sopenharmony_cimodule_gameport_driver(grip_drv);
691