18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) 1998-2001 Vojtech Pavlik
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/*
78c2ecf20Sopenharmony_ci * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
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/jiffies.h>
198c2ecf20Sopenharmony_ci
208c2ecf20Sopenharmony_ci#define DRIVER_DESC	"Gravis GrIP protocol joystick driver"
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
238c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
248c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#define GRIP_MODE_GPP		1
278c2ecf20Sopenharmony_ci#define GRIP_MODE_BD		2
288c2ecf20Sopenharmony_ci#define GRIP_MODE_XT		3
298c2ecf20Sopenharmony_ci#define GRIP_MODE_DC		4
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#define GRIP_LENGTH_GPP		24
328c2ecf20Sopenharmony_ci#define GRIP_STROBE_GPP		200	/* 200 us */
338c2ecf20Sopenharmony_ci#define GRIP_LENGTH_XT		4
348c2ecf20Sopenharmony_ci#define GRIP_STROBE_XT		64	/* 64 us */
358c2ecf20Sopenharmony_ci#define GRIP_MAX_CHUNKS_XT	10
368c2ecf20Sopenharmony_ci#define GRIP_MAX_BITS_XT	30
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_cistruct grip {
398c2ecf20Sopenharmony_ci	struct gameport *gameport;
408c2ecf20Sopenharmony_ci	struct input_dev *dev[2];
418c2ecf20Sopenharmony_ci	unsigned char mode[2];
428c2ecf20Sopenharmony_ci	int reads;
438c2ecf20Sopenharmony_ci	int bads;
448c2ecf20Sopenharmony_ci	char phys[2][32];
458c2ecf20Sopenharmony_ci};
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_cistatic int grip_btn_gpp[] = { BTN_START, BTN_SELECT, BTN_TR2, BTN_Y, 0, BTN_TL2, BTN_A, BTN_B, BTN_X, 0, BTN_TL, BTN_TR, -1 };
488c2ecf20Sopenharmony_cistatic int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
498c2ecf20Sopenharmony_cistatic int grip_btn_xt[] = { BTN_TRIGGER, BTN_THUMB, BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_SELECT, BTN_START, BTN_MODE, -1 };
508c2ecf20Sopenharmony_cistatic int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
518c2ecf20Sopenharmony_ci
528c2ecf20Sopenharmony_cistatic int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
538c2ecf20Sopenharmony_cistatic int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
548c2ecf20Sopenharmony_cistatic int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
558c2ecf20Sopenharmony_cistatic int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_cistatic char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
588c2ecf20Sopenharmony_ci				"Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
598c2ecf20Sopenharmony_cistatic int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
608c2ecf20Sopenharmony_cistatic int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
618c2ecf20Sopenharmony_cistatic char grip_anx[] = { 0, 0, 3, 5, 5 };
628c2ecf20Sopenharmony_cistatic char grip_cen[] = { 0, 0, 2, 2, 4 };
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci/*
658c2ecf20Sopenharmony_ci * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
668c2ecf20Sopenharmony_ci */
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_cistatic int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
698c2ecf20Sopenharmony_ci{
708c2ecf20Sopenharmony_ci	unsigned long flags;
718c2ecf20Sopenharmony_ci	unsigned char u, v;
728c2ecf20Sopenharmony_ci	unsigned int t;
738c2ecf20Sopenharmony_ci	int i;
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci	int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci	data[0] = 0;
788c2ecf20Sopenharmony_ci	t = strobe;
798c2ecf20Sopenharmony_ci	i = 0;
808c2ecf20Sopenharmony_ci
818c2ecf20Sopenharmony_ci	local_irq_save(flags);
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci	v = gameport_read(gameport) >> shift;
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci	do {
868c2ecf20Sopenharmony_ci		t--;
878c2ecf20Sopenharmony_ci		u = v; v = (gameport_read(gameport) >> shift) & 3;
888c2ecf20Sopenharmony_ci		if (~v & u & 1) {
898c2ecf20Sopenharmony_ci			data[0] |= (v >> 1) << i++;
908c2ecf20Sopenharmony_ci			t = strobe;
918c2ecf20Sopenharmony_ci		}
928c2ecf20Sopenharmony_ci	} while (i < GRIP_LENGTH_GPP && t > 0);
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci	local_irq_restore(flags);
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci	if (i < GRIP_LENGTH_GPP) return -1;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci	for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
998c2ecf20Sopenharmony_ci		data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci	return -(i == GRIP_LENGTH_GPP);
1028c2ecf20Sopenharmony_ci}
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci/*
1058c2ecf20Sopenharmony_ci * grip_xt_read_packet() reads a Gravis Xterminator packet.
1068c2ecf20Sopenharmony_ci */
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
1098c2ecf20Sopenharmony_ci{
1108c2ecf20Sopenharmony_ci	unsigned int i, j, buf, crc;
1118c2ecf20Sopenharmony_ci	unsigned char u, v, w;
1128c2ecf20Sopenharmony_ci	unsigned long flags;
1138c2ecf20Sopenharmony_ci	unsigned int t;
1148c2ecf20Sopenharmony_ci	char status;
1158c2ecf20Sopenharmony_ci
1168c2ecf20Sopenharmony_ci	int strobe = gameport_time(gameport, GRIP_STROBE_XT);
1178c2ecf20Sopenharmony_ci
1188c2ecf20Sopenharmony_ci	data[0] = data[1] = data[2] = data[3] = 0;
1198c2ecf20Sopenharmony_ci	status = buf = i = j = 0;
1208c2ecf20Sopenharmony_ci	t = strobe;
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_ci	local_irq_save(flags);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	v = w = (gameport_read(gameport) >> shift) & 3;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	do {
1278c2ecf20Sopenharmony_ci		t--;
1288c2ecf20Sopenharmony_ci		u = (gameport_read(gameport) >> shift) & 3;
1298c2ecf20Sopenharmony_ci
1308c2ecf20Sopenharmony_ci		if (u ^ v) {
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci			if ((u ^ v) & 1) {
1338c2ecf20Sopenharmony_ci				buf = (buf << 1) | (u >> 1);
1348c2ecf20Sopenharmony_ci				t = strobe;
1358c2ecf20Sopenharmony_ci				i++;
1368c2ecf20Sopenharmony_ci			} else
1378c2ecf20Sopenharmony_ci
1388c2ecf20Sopenharmony_ci			if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
1398c2ecf20Sopenharmony_ci				if (i == 20) {
1408c2ecf20Sopenharmony_ci					crc = buf ^ (buf >> 7) ^ (buf >> 14);
1418c2ecf20Sopenharmony_ci					if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
1428c2ecf20Sopenharmony_ci						data[buf >> 18] = buf >> 4;
1438c2ecf20Sopenharmony_ci						status |= 1 << (buf >> 18);
1448c2ecf20Sopenharmony_ci					}
1458c2ecf20Sopenharmony_ci					j++;
1468c2ecf20Sopenharmony_ci				}
1478c2ecf20Sopenharmony_ci				t = strobe;
1488c2ecf20Sopenharmony_ci				buf = 0;
1498c2ecf20Sopenharmony_ci				i = 0;
1508c2ecf20Sopenharmony_ci			}
1518c2ecf20Sopenharmony_ci			w = v;
1528c2ecf20Sopenharmony_ci			v = u;
1538c2ecf20Sopenharmony_ci		}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_ci	} while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	local_irq_restore(flags);
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci	return -(status != 0xf);
1608c2ecf20Sopenharmony_ci}
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci/*
1638c2ecf20Sopenharmony_ci * grip_timer() repeatedly polls the joysticks and generates events.
1648c2ecf20Sopenharmony_ci */
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_cistatic void grip_poll(struct gameport *gameport)
1678c2ecf20Sopenharmony_ci{
1688c2ecf20Sopenharmony_ci	struct grip *grip = gameport_get_drvdata(gameport);
1698c2ecf20Sopenharmony_ci	unsigned int data[GRIP_LENGTH_XT];
1708c2ecf20Sopenharmony_ci	struct input_dev *dev;
1718c2ecf20Sopenharmony_ci	int i, j;
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci		dev = grip->dev[i];
1768c2ecf20Sopenharmony_ci		if (!dev)
1778c2ecf20Sopenharmony_ci			continue;
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci		grip->reads++;
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci		switch (grip->mode[i]) {
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ci			case GRIP_MODE_GPP:
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci				if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
1868c2ecf20Sopenharmony_ci					grip->bads++;
1878c2ecf20Sopenharmony_ci					break;
1888c2ecf20Sopenharmony_ci				}
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
1918c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ci				for (j = 0; j < 12; j++)
1948c2ecf20Sopenharmony_ci					if (grip_btn_gpp[j])
1958c2ecf20Sopenharmony_ci						input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci				break;
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci			case GRIP_MODE_BD:
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
2028c2ecf20Sopenharmony_ci					grip->bads++;
2038c2ecf20Sopenharmony_ci					break;
2048c2ecf20Sopenharmony_ci				}
2058c2ecf20Sopenharmony_ci
2068c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
2078c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
2088c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
2098c2ecf20Sopenharmony_ci
2108c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
2118c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci				for (j = 0; j < 5; j++)
2148c2ecf20Sopenharmony_ci					input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci				break;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci			case GRIP_MODE_XT:
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
2218c2ecf20Sopenharmony_ci					grip->bads++;
2228c2ecf20Sopenharmony_ci					break;
2238c2ecf20Sopenharmony_ci				}
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
2268c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
2278c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_BRAKE,    (data[1] >> 2) & 0x3f);
2288c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_GAS,	    (data[1] >> 8) & 0x3f);
2298c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
2328c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
2338c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
2348c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci				for (j = 0; j < 11; j++)
2378c2ecf20Sopenharmony_ci					input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
2388c2ecf20Sopenharmony_ci				break;
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci			case GRIP_MODE_DC:
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
2438c2ecf20Sopenharmony_ci					grip->bads++;
2448c2ecf20Sopenharmony_ci					break;
2458c2ecf20Sopenharmony_ci				}
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
2488c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_Y,        (data[0] >> 8) & 0x3f);
2498c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_RX,       (data[1] >> 2) & 0x3f);
2508c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_RY,	    (data[1] >> 8) & 0x3f);
2518c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
2548c2ecf20Sopenharmony_ci				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci				for (j = 0; j < 9; j++)
2578c2ecf20Sopenharmony_ci					input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
2588c2ecf20Sopenharmony_ci				break;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci		}
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci		input_sync(dev);
2648c2ecf20Sopenharmony_ci	}
2658c2ecf20Sopenharmony_ci}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_cistatic int grip_open(struct input_dev *dev)
2688c2ecf20Sopenharmony_ci{
2698c2ecf20Sopenharmony_ci	struct grip *grip = input_get_drvdata(dev);
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci	gameport_start_polling(grip->gameport);
2728c2ecf20Sopenharmony_ci	return 0;
2738c2ecf20Sopenharmony_ci}
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_cistatic void grip_close(struct input_dev *dev)
2768c2ecf20Sopenharmony_ci{
2778c2ecf20Sopenharmony_ci	struct grip *grip = input_get_drvdata(dev);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	gameport_stop_polling(grip->gameport);
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_cistatic int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
2838c2ecf20Sopenharmony_ci{
2848c2ecf20Sopenharmony_ci	struct grip *grip;
2858c2ecf20Sopenharmony_ci	struct input_dev *input_dev;
2868c2ecf20Sopenharmony_ci	unsigned int data[GRIP_LENGTH_XT];
2878c2ecf20Sopenharmony_ci	int i, j, t;
2888c2ecf20Sopenharmony_ci	int err;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
2918c2ecf20Sopenharmony_ci		return -ENOMEM;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	grip->gameport = gameport;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	gameport_set_drvdata(gameport, grip);
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
2988c2ecf20Sopenharmony_ci	if (err)
2998c2ecf20Sopenharmony_ci		goto fail1;
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
3028c2ecf20Sopenharmony_ci		if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
3038c2ecf20Sopenharmony_ci			grip->mode[i] = GRIP_MODE_GPP;
3048c2ecf20Sopenharmony_ci			continue;
3058c2ecf20Sopenharmony_ci		}
3068c2ecf20Sopenharmony_ci		if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
3078c2ecf20Sopenharmony_ci			if (!(data[3] & 7)) {
3088c2ecf20Sopenharmony_ci				grip->mode[i] = GRIP_MODE_BD;
3098c2ecf20Sopenharmony_ci				continue;
3108c2ecf20Sopenharmony_ci			}
3118c2ecf20Sopenharmony_ci			if (!(data[2] & 0xf0)) {
3128c2ecf20Sopenharmony_ci				grip->mode[i] = GRIP_MODE_XT;
3138c2ecf20Sopenharmony_ci				continue;
3148c2ecf20Sopenharmony_ci			}
3158c2ecf20Sopenharmony_ci			grip->mode[i] = GRIP_MODE_DC;
3168c2ecf20Sopenharmony_ci			continue;
3178c2ecf20Sopenharmony_ci		}
3188c2ecf20Sopenharmony_ci	}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (!grip->mode[0] && !grip->mode[1]) {
3218c2ecf20Sopenharmony_ci		err = -ENODEV;
3228c2ecf20Sopenharmony_ci		goto fail2;
3238c2ecf20Sopenharmony_ci	}
3248c2ecf20Sopenharmony_ci
3258c2ecf20Sopenharmony_ci	gameport_set_poll_handler(gameport, grip_poll);
3268c2ecf20Sopenharmony_ci	gameport_set_poll_interval(gameport, 20);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++) {
3298c2ecf20Sopenharmony_ci		if (!grip->mode[i])
3308c2ecf20Sopenharmony_ci			continue;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci		grip->dev[i] = input_dev = input_allocate_device();
3338c2ecf20Sopenharmony_ci		if (!input_dev) {
3348c2ecf20Sopenharmony_ci			err = -ENOMEM;
3358c2ecf20Sopenharmony_ci			goto fail3;
3368c2ecf20Sopenharmony_ci		}
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci		snprintf(grip->phys[i], sizeof(grip->phys[i]),
3398c2ecf20Sopenharmony_ci			 "%s/input%d", gameport->phys, i);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci		input_dev->name = grip_name[grip->mode[i]];
3428c2ecf20Sopenharmony_ci		input_dev->phys = grip->phys[i];
3438c2ecf20Sopenharmony_ci		input_dev->id.bustype = BUS_GAMEPORT;
3448c2ecf20Sopenharmony_ci		input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
3458c2ecf20Sopenharmony_ci		input_dev->id.product = grip->mode[i];
3468c2ecf20Sopenharmony_ci		input_dev->id.version = 0x0100;
3478c2ecf20Sopenharmony_ci		input_dev->dev.parent = &gameport->dev;
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci		input_set_drvdata(input_dev, grip);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci		input_dev->open = grip_open;
3528c2ecf20Sopenharmony_ci		input_dev->close = grip_close;
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_ci		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
3558c2ecf20Sopenharmony_ci
3568c2ecf20Sopenharmony_ci		for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci			if (j < grip_cen[grip->mode[i]])
3598c2ecf20Sopenharmony_ci				input_set_abs_params(input_dev, t, 14, 52, 1, 2);
3608c2ecf20Sopenharmony_ci			else if (j < grip_anx[grip->mode[i]])
3618c2ecf20Sopenharmony_ci				input_set_abs_params(input_dev, t, 3, 57, 1, 0);
3628c2ecf20Sopenharmony_ci			else
3638c2ecf20Sopenharmony_ci				input_set_abs_params(input_dev, t, -1, 1, 0, 0);
3648c2ecf20Sopenharmony_ci		}
3658c2ecf20Sopenharmony_ci
3668c2ecf20Sopenharmony_ci		for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
3678c2ecf20Sopenharmony_ci			if (t > 0)
3688c2ecf20Sopenharmony_ci				set_bit(t, input_dev->keybit);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci		err = input_register_device(grip->dev[i]);
3718c2ecf20Sopenharmony_ci		if (err)
3728c2ecf20Sopenharmony_ci			goto fail4;
3738c2ecf20Sopenharmony_ci	}
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	return 0;
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci fail4:	input_free_device(grip->dev[i]);
3788c2ecf20Sopenharmony_ci fail3:	while (--i >= 0)
3798c2ecf20Sopenharmony_ci		if (grip->dev[i])
3808c2ecf20Sopenharmony_ci			input_unregister_device(grip->dev[i]);
3818c2ecf20Sopenharmony_ci fail2:	gameport_close(gameport);
3828c2ecf20Sopenharmony_ci fail1:	gameport_set_drvdata(gameport, NULL);
3838c2ecf20Sopenharmony_ci	kfree(grip);
3848c2ecf20Sopenharmony_ci	return err;
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic void grip_disconnect(struct gameport *gameport)
3888c2ecf20Sopenharmony_ci{
3898c2ecf20Sopenharmony_ci	struct grip *grip = gameport_get_drvdata(gameport);
3908c2ecf20Sopenharmony_ci	int i;
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci	for (i = 0; i < 2; i++)
3938c2ecf20Sopenharmony_ci		if (grip->dev[i])
3948c2ecf20Sopenharmony_ci			input_unregister_device(grip->dev[i]);
3958c2ecf20Sopenharmony_ci	gameport_close(gameport);
3968c2ecf20Sopenharmony_ci	gameport_set_drvdata(gameport, NULL);
3978c2ecf20Sopenharmony_ci	kfree(grip);
3988c2ecf20Sopenharmony_ci}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_cistatic struct gameport_driver grip_drv = {
4018c2ecf20Sopenharmony_ci	.driver		= {
4028c2ecf20Sopenharmony_ci		.name	= "grip",
4038c2ecf20Sopenharmony_ci		.owner	= THIS_MODULE,
4048c2ecf20Sopenharmony_ci	},
4058c2ecf20Sopenharmony_ci	.description	= DRIVER_DESC,
4068c2ecf20Sopenharmony_ci	.connect	= grip_connect,
4078c2ecf20Sopenharmony_ci	.disconnect	= grip_disconnect,
4088c2ecf20Sopenharmony_ci};
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_cimodule_gameport_driver(grip_drv);
411