xref: /linux/drivers/input/joystick/grip.c (revision 4e57b6817880946a3a78d5d8cad1ace363f7e449)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  * $Id: grip.c,v 1.21 2002/01/22 20:27:57 vojtech Exp $
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Copyright (c) 1998-2001 Vojtech Pavlik
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds /*
121da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
131da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
141da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
151da177e4SLinus Torvalds  * (at your option) any later version.
161da177e4SLinus Torvalds  *
171da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
181da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
191da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
201da177e4SLinus Torvalds  * GNU General Public License for more details.
211da177e4SLinus Torvalds  *
221da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
231da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
241da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
251da177e4SLinus Torvalds  *
261da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
271da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
281da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
291da177e4SLinus Torvalds  */
301da177e4SLinus Torvalds 
311da177e4SLinus Torvalds #include <linux/kernel.h>
321da177e4SLinus Torvalds #include <linux/module.h>
331da177e4SLinus Torvalds #include <linux/init.h>
341da177e4SLinus Torvalds #include <linux/slab.h>
351da177e4SLinus Torvalds #include <linux/gameport.h>
361da177e4SLinus Torvalds #include <linux/input.h>
37*4e57b681STim Schmielau #include <linux/jiffies.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define DRIVER_DESC	"Gravis GrIP protocol joystick driver"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
421da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds #define GRIP_MODE_GPP		1
461da177e4SLinus Torvalds #define GRIP_MODE_BD		2
471da177e4SLinus Torvalds #define GRIP_MODE_XT		3
481da177e4SLinus Torvalds #define GRIP_MODE_DC		4
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds #define GRIP_LENGTH_GPP		24
511da177e4SLinus Torvalds #define GRIP_STROBE_GPP		200	/* 200 us */
521da177e4SLinus Torvalds #define GRIP_LENGTH_XT		4
531da177e4SLinus Torvalds #define GRIP_STROBE_XT		64	/* 64 us */
541da177e4SLinus Torvalds #define GRIP_MAX_CHUNKS_XT	10
551da177e4SLinus Torvalds #define GRIP_MAX_BITS_XT	30
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds struct grip {
581da177e4SLinus Torvalds 	struct gameport *gameport;
5917dd3f0fSDmitry Torokhov 	struct input_dev *dev[2];
601da177e4SLinus Torvalds 	unsigned char mode[2];
611da177e4SLinus Torvalds 	int reads;
621da177e4SLinus Torvalds 	int bads;
631da177e4SLinus Torvalds 	char phys[2][32];
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static 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 };
671da177e4SLinus Torvalds static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
681da177e4SLinus Torvalds static 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 };
691da177e4SLinus Torvalds static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
721da177e4SLinus Torvalds static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
731da177e4SLinus Torvalds static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
741da177e4SLinus Torvalds static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
751da177e4SLinus Torvalds 
761da177e4SLinus Torvalds static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
771da177e4SLinus Torvalds 				"Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
781da177e4SLinus Torvalds static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
791da177e4SLinus Torvalds static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
801da177e4SLinus Torvalds static char grip_anx[] = { 0, 0, 3, 5, 5 };
811da177e4SLinus Torvalds static char grip_cen[] = { 0, 0, 2, 2, 4 };
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds /*
841da177e4SLinus Torvalds  * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
851da177e4SLinus Torvalds  */
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
881da177e4SLinus Torvalds {
891da177e4SLinus Torvalds 	unsigned long flags;
901da177e4SLinus Torvalds 	unsigned char u, v;
911da177e4SLinus Torvalds 	unsigned int t;
921da177e4SLinus Torvalds 	int i;
931da177e4SLinus Torvalds 
941da177e4SLinus Torvalds 	int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds 	data[0] = 0;
971da177e4SLinus Torvalds 	t = strobe;
981da177e4SLinus Torvalds 	i = 0;
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds 	local_irq_save(flags);
1011da177e4SLinus Torvalds 
1021da177e4SLinus Torvalds 	v = gameport_read(gameport) >> shift;
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	do {
1051da177e4SLinus Torvalds 		t--;
1061da177e4SLinus Torvalds 		u = v; v = (gameport_read(gameport) >> shift) & 3;
1071da177e4SLinus Torvalds 		if (~v & u & 1) {
1081da177e4SLinus Torvalds 			data[0] |= (v >> 1) << i++;
1091da177e4SLinus Torvalds 			t = strobe;
1101da177e4SLinus Torvalds 		}
1111da177e4SLinus Torvalds 	} while (i < GRIP_LENGTH_GPP && t > 0);
1121da177e4SLinus Torvalds 
1131da177e4SLinus Torvalds 	local_irq_restore(flags);
1141da177e4SLinus Torvalds 
1151da177e4SLinus Torvalds 	if (i < GRIP_LENGTH_GPP) return -1;
1161da177e4SLinus Torvalds 
1171da177e4SLinus Torvalds 	for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
1181da177e4SLinus Torvalds 		data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	return -(i == GRIP_LENGTH_GPP);
1211da177e4SLinus Torvalds }
1221da177e4SLinus Torvalds 
1231da177e4SLinus Torvalds /*
1241da177e4SLinus Torvalds  * grip_xt_read_packet() reads a Gravis Xterminator packet.
1251da177e4SLinus Torvalds  */
1261da177e4SLinus Torvalds 
1271da177e4SLinus Torvalds static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
1281da177e4SLinus Torvalds {
1291da177e4SLinus Torvalds 	unsigned int i, j, buf, crc;
1301da177e4SLinus Torvalds 	unsigned char u, v, w;
1311da177e4SLinus Torvalds 	unsigned long flags;
1321da177e4SLinus Torvalds 	unsigned int t;
1331da177e4SLinus Torvalds 	char status;
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 	int strobe = gameport_time(gameport, GRIP_STROBE_XT);
1361da177e4SLinus Torvalds 
1371da177e4SLinus Torvalds 	data[0] = data[1] = data[2] = data[3] = 0;
1381da177e4SLinus Torvalds 	status = buf = i = j = 0;
1391da177e4SLinus Torvalds 	t = strobe;
1401da177e4SLinus Torvalds 
1411da177e4SLinus Torvalds 	local_irq_save(flags);
1421da177e4SLinus Torvalds 
1431da177e4SLinus Torvalds 	v = w = (gameport_read(gameport) >> shift) & 3;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	do {
1461da177e4SLinus Torvalds 		t--;
1471da177e4SLinus Torvalds 		u = (gameport_read(gameport) >> shift) & 3;
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 		if (u ^ v) {
1501da177e4SLinus Torvalds 
1511da177e4SLinus Torvalds 			if ((u ^ v) & 1) {
1521da177e4SLinus Torvalds 				buf = (buf << 1) | (u >> 1);
1531da177e4SLinus Torvalds 				t = strobe;
1541da177e4SLinus Torvalds 				i++;
1551da177e4SLinus Torvalds 			} else
1561da177e4SLinus Torvalds 
1571da177e4SLinus Torvalds 			if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
1581da177e4SLinus Torvalds 				if (i == 20) {
1591da177e4SLinus Torvalds 					crc = buf ^ (buf >> 7) ^ (buf >> 14);
1601da177e4SLinus Torvalds 					if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
1611da177e4SLinus Torvalds 						data[buf >> 18] = buf >> 4;
1621da177e4SLinus Torvalds 						status |= 1 << (buf >> 18);
1631da177e4SLinus Torvalds 					}
1641da177e4SLinus Torvalds 					j++;
1651da177e4SLinus Torvalds 				}
1661da177e4SLinus Torvalds 				t = strobe;
1671da177e4SLinus Torvalds 				buf = 0;
1681da177e4SLinus Torvalds 				i = 0;
1691da177e4SLinus Torvalds 			}
1701da177e4SLinus Torvalds 			w = v;
1711da177e4SLinus Torvalds 			v = u;
1721da177e4SLinus Torvalds 		}
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	} while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
1751da177e4SLinus Torvalds 
1761da177e4SLinus Torvalds 	local_irq_restore(flags);
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds 	return -(status != 0xf);
1791da177e4SLinus Torvalds }
1801da177e4SLinus Torvalds 
1811da177e4SLinus Torvalds /*
1821da177e4SLinus Torvalds  * grip_timer() repeatedly polls the joysticks and generates events.
1831da177e4SLinus Torvalds  */
1841da177e4SLinus Torvalds 
1851da177e4SLinus Torvalds static void grip_poll(struct gameport *gameport)
1861da177e4SLinus Torvalds {
1871da177e4SLinus Torvalds 	struct grip *grip = gameport_get_drvdata(gameport);
1881da177e4SLinus Torvalds 	unsigned int data[GRIP_LENGTH_XT];
1891da177e4SLinus Torvalds 	struct input_dev *dev;
1901da177e4SLinus Torvalds 	int i, j;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
1931da177e4SLinus Torvalds 
19417dd3f0fSDmitry Torokhov 		dev = grip->dev[i];
1951da177e4SLinus Torvalds 		grip->reads++;
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds 		switch (grip->mode[i]) {
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds 			case GRIP_MODE_GPP:
2001da177e4SLinus Torvalds 
2011da177e4SLinus Torvalds 				if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
2021da177e4SLinus Torvalds 					grip->bads++;
2031da177e4SLinus Torvalds 					break;
2041da177e4SLinus Torvalds 				}
2051da177e4SLinus Torvalds 
2061da177e4SLinus Torvalds 				input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
2071da177e4SLinus Torvalds 				input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
2081da177e4SLinus Torvalds 
2091da177e4SLinus Torvalds 				for (j = 0; j < 12; j++)
2101da177e4SLinus Torvalds 					if (grip_btn_gpp[j])
2111da177e4SLinus Torvalds 						input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds 				break;
2141da177e4SLinus Torvalds 
2151da177e4SLinus Torvalds 			case GRIP_MODE_BD:
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds 				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
2181da177e4SLinus Torvalds 					grip->bads++;
2191da177e4SLinus Torvalds 					break;
2201da177e4SLinus Torvalds 				}
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
2231da177e4SLinus Torvalds 				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
2241da177e4SLinus Torvalds 				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
2251da177e4SLinus Torvalds 
2261da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
2271da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
2281da177e4SLinus Torvalds 
2291da177e4SLinus Torvalds 				for (j = 0; j < 5; j++)
2301da177e4SLinus Torvalds 					input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 				break;
2331da177e4SLinus Torvalds 
2341da177e4SLinus Torvalds 			case GRIP_MODE_XT:
2351da177e4SLinus Torvalds 
2361da177e4SLinus Torvalds 				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
2371da177e4SLinus Torvalds 					grip->bads++;
2381da177e4SLinus Torvalds 					break;
2391da177e4SLinus Torvalds 				}
2401da177e4SLinus Torvalds 
2411da177e4SLinus Torvalds 				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
2421da177e4SLinus Torvalds 				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
2431da177e4SLinus Torvalds 				input_report_abs(dev, ABS_BRAKE,    (data[1] >> 2) & 0x3f);
2441da177e4SLinus Torvalds 				input_report_abs(dev, ABS_GAS,	    (data[1] >> 8) & 0x3f);
2451da177e4SLinus Torvalds 				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
2461da177e4SLinus Torvalds 
2471da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
2481da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
2491da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
2501da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
2511da177e4SLinus Torvalds 
2521da177e4SLinus Torvalds 				for (j = 0; j < 11; j++)
2531da177e4SLinus Torvalds 					input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
2541da177e4SLinus Torvalds 				break;
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds 			case GRIP_MODE_DC:
2571da177e4SLinus Torvalds 
2581da177e4SLinus Torvalds 				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
2591da177e4SLinus Torvalds 					grip->bads++;
2601da177e4SLinus Torvalds 					break;
2611da177e4SLinus Torvalds 				}
2621da177e4SLinus Torvalds 
2631da177e4SLinus Torvalds 				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
2641da177e4SLinus Torvalds 				input_report_abs(dev, ABS_Y,        (data[0] >> 8) & 0x3f);
2651da177e4SLinus Torvalds 				input_report_abs(dev, ABS_RX,       (data[1] >> 2) & 0x3f);
2661da177e4SLinus Torvalds 				input_report_abs(dev, ABS_RY,	    (data[1] >> 8) & 0x3f);
2671da177e4SLinus Torvalds 				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
2701da177e4SLinus Torvalds 				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 				for (j = 0; j < 9; j++)
2731da177e4SLinus Torvalds 					input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
2741da177e4SLinus Torvalds 				break;
2751da177e4SLinus Torvalds 
2761da177e4SLinus Torvalds 
2771da177e4SLinus Torvalds 		}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 		input_sync(dev);
2801da177e4SLinus Torvalds 	}
2811da177e4SLinus Torvalds }
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds static int grip_open(struct input_dev *dev)
2841da177e4SLinus Torvalds {
2851da177e4SLinus Torvalds 	struct grip *grip = dev->private;
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds 	gameport_start_polling(grip->gameport);
2881da177e4SLinus Torvalds 	return 0;
2891da177e4SLinus Torvalds }
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static void grip_close(struct input_dev *dev)
2921da177e4SLinus Torvalds {
2931da177e4SLinus Torvalds 	struct grip *grip = dev->private;
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	gameport_stop_polling(grip->gameport);
2961da177e4SLinus Torvalds }
2971da177e4SLinus Torvalds 
2981da177e4SLinus Torvalds static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
2991da177e4SLinus Torvalds {
3001da177e4SLinus Torvalds 	struct grip *grip;
30117dd3f0fSDmitry Torokhov 	struct input_dev *input_dev;
3021da177e4SLinus Torvalds 	unsigned int data[GRIP_LENGTH_XT];
3031da177e4SLinus Torvalds 	int i, j, t;
3041da177e4SLinus Torvalds 	int err;
3051da177e4SLinus Torvalds 
306a97e148aSPekka Enberg 	if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
3071da177e4SLinus Torvalds 		return -ENOMEM;
3081da177e4SLinus Torvalds 
3091da177e4SLinus Torvalds 	grip->gameport = gameport;
3101da177e4SLinus Torvalds 
3111da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, grip);
3121da177e4SLinus Torvalds 
3131da177e4SLinus Torvalds 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
3141da177e4SLinus Torvalds 	if (err)
3151da177e4SLinus Torvalds 		goto fail1;
3161da177e4SLinus Torvalds 
3171da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
3181da177e4SLinus Torvalds 		if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
3191da177e4SLinus Torvalds 			grip->mode[i] = GRIP_MODE_GPP;
3201da177e4SLinus Torvalds 			continue;
3211da177e4SLinus Torvalds 		}
3221da177e4SLinus Torvalds 		if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
3231da177e4SLinus Torvalds 			if (!(data[3] & 7)) {
3241da177e4SLinus Torvalds 				grip->mode[i] = GRIP_MODE_BD;
3251da177e4SLinus Torvalds 				continue;
3261da177e4SLinus Torvalds 			}
3271da177e4SLinus Torvalds 			if (!(data[2] & 0xf0)) {
3281da177e4SLinus Torvalds 				grip->mode[i] = GRIP_MODE_XT;
3291da177e4SLinus Torvalds 				continue;
3301da177e4SLinus Torvalds 			}
3311da177e4SLinus Torvalds 			grip->mode[i] = GRIP_MODE_DC;
3321da177e4SLinus Torvalds 			continue;
3331da177e4SLinus Torvalds 		}
3341da177e4SLinus Torvalds 	}
3351da177e4SLinus Torvalds 
3361da177e4SLinus Torvalds 	if (!grip->mode[0] && !grip->mode[1]) {
3371da177e4SLinus Torvalds 		err = -ENODEV;
3381da177e4SLinus Torvalds 		goto fail2;
3391da177e4SLinus Torvalds 	}
3401da177e4SLinus Torvalds 
3411da177e4SLinus Torvalds 	gameport_set_poll_handler(gameport, grip_poll);
3421da177e4SLinus Torvalds 	gameport_set_poll_interval(gameport, 20);
3431da177e4SLinus Torvalds 
34417dd3f0fSDmitry Torokhov 	for (i = 0; i < 2; i++) {
34517dd3f0fSDmitry Torokhov 		if (!grip->mode[i])
34617dd3f0fSDmitry Torokhov 			continue;
34717dd3f0fSDmitry Torokhov 
34817dd3f0fSDmitry Torokhov 		grip->dev[i] = input_dev = input_allocate_device();
34917dd3f0fSDmitry Torokhov 		if (!input_dev) {
35017dd3f0fSDmitry Torokhov 			err = -ENOMEM;
35117dd3f0fSDmitry Torokhov 			goto fail3;
35217dd3f0fSDmitry Torokhov 		}
3531da177e4SLinus Torvalds 
3541da177e4SLinus Torvalds 		sprintf(grip->phys[i], "%s/input%d", gameport->phys, i);
3551da177e4SLinus Torvalds 
35617dd3f0fSDmitry Torokhov 		input_dev->name = grip_name[grip->mode[i]];
35717dd3f0fSDmitry Torokhov 		input_dev->phys = grip->phys[i];
35817dd3f0fSDmitry Torokhov 		input_dev->id.bustype = BUS_GAMEPORT;
35917dd3f0fSDmitry Torokhov 		input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
36017dd3f0fSDmitry Torokhov 		input_dev->id.product = grip->mode[i];
36117dd3f0fSDmitry Torokhov 		input_dev->id.version = 0x0100;
36217dd3f0fSDmitry Torokhov 		input_dev->cdev.dev = &gameport->dev;
36317dd3f0fSDmitry Torokhov 		input_dev->private = grip;
3641da177e4SLinus Torvalds 
36517dd3f0fSDmitry Torokhov 		input_dev->open = grip_open;
36617dd3f0fSDmitry Torokhov 		input_dev->close = grip_close;
3671da177e4SLinus Torvalds 
36817dd3f0fSDmitry Torokhov 		input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
3691da177e4SLinus Torvalds 
3701da177e4SLinus Torvalds 		for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
3711da177e4SLinus Torvalds 
3721da177e4SLinus Torvalds 			if (j < grip_cen[grip->mode[i]])
37317dd3f0fSDmitry Torokhov 				input_set_abs_params(input_dev, t, 14, 52, 1, 2);
3741da177e4SLinus Torvalds 			else if (j < grip_anx[grip->mode[i]])
37517dd3f0fSDmitry Torokhov 				input_set_abs_params(input_dev, t, 3, 57, 1, 0);
3761da177e4SLinus Torvalds 			else
37717dd3f0fSDmitry Torokhov 				input_set_abs_params(input_dev, t, -1, 1, 0, 0);
3781da177e4SLinus Torvalds 		}
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 		for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
3811da177e4SLinus Torvalds 			if (t > 0)
38217dd3f0fSDmitry Torokhov 				set_bit(t, input_dev->keybit);
3831da177e4SLinus Torvalds 
38417dd3f0fSDmitry Torokhov 		input_register_device(grip->dev[i]);
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	return 0;
3881da177e4SLinus Torvalds 
38917dd3f0fSDmitry Torokhov  fail3: for (i = 0; i < 2; i++)
39017dd3f0fSDmitry Torokhov 		if (grip->dev[i])
39117dd3f0fSDmitry Torokhov 			input_unregister_device(grip->dev[i]);
3921da177e4SLinus Torvalds  fail2:	gameport_close(gameport);
3931da177e4SLinus Torvalds  fail1:	gameport_set_drvdata(gameport, NULL);
3941da177e4SLinus Torvalds 	kfree(grip);
3951da177e4SLinus Torvalds 	return err;
3961da177e4SLinus Torvalds }
3971da177e4SLinus Torvalds 
3981da177e4SLinus Torvalds static void grip_disconnect(struct gameport *gameport)
3991da177e4SLinus Torvalds {
4001da177e4SLinus Torvalds 	struct grip *grip = gameport_get_drvdata(gameport);
4011da177e4SLinus Torvalds 	int i;
4021da177e4SLinus Torvalds 
4031da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
40417dd3f0fSDmitry Torokhov 		if (grip->dev[i])
40517dd3f0fSDmitry Torokhov 			input_unregister_device(grip->dev[i]);
4061da177e4SLinus Torvalds 	gameport_close(gameport);
4071da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, NULL);
4081da177e4SLinus Torvalds 	kfree(grip);
4091da177e4SLinus Torvalds }
4101da177e4SLinus Torvalds 
4111da177e4SLinus Torvalds static struct gameport_driver grip_drv = {
4121da177e4SLinus Torvalds 	.driver		= {
4131da177e4SLinus Torvalds 		.name	= "grip",
4141da177e4SLinus Torvalds 	},
4151da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
4161da177e4SLinus Torvalds 	.connect	= grip_connect,
4171da177e4SLinus Torvalds 	.disconnect	= grip_disconnect,
4181da177e4SLinus Torvalds };
4191da177e4SLinus Torvalds 
4201da177e4SLinus Torvalds static int __init grip_init(void)
4211da177e4SLinus Torvalds {
4221da177e4SLinus Torvalds 	gameport_register_driver(&grip_drv);
4231da177e4SLinus Torvalds 	return 0;
4241da177e4SLinus Torvalds }
4251da177e4SLinus Torvalds 
4261da177e4SLinus Torvalds static void __exit grip_exit(void)
4271da177e4SLinus Torvalds {
4281da177e4SLinus Torvalds 	gameport_unregister_driver(&grip_drv);
4291da177e4SLinus Torvalds }
4301da177e4SLinus Torvalds 
4311da177e4SLinus Torvalds module_init(grip_init);
4321da177e4SLinus Torvalds module_exit(grip_exit);
433