Linux Audio

Check our new training course

Yocto / OpenEmbedded training

Feb 10-13, 2025
Register
Loading...
v3.15
 
  1/*
  2 *  Copyright (c) 1998-2001 Vojtech Pavlik
  3 */
  4
  5/*
  6 * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
  7 */
  8
  9/*
 10 * This program is free software; you can redistribute it and/or modify
 11 * it under the terms of the GNU General Public License as published by
 12 * the Free Software Foundation; either version 2 of the License, or
 13 * (at your option) any later version.
 14 *
 15 * This program is distributed in the hope that it will be useful,
 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 18 * GNU General Public License for more details.
 19 *
 20 * You should have received a copy of the GNU General Public License
 21 * along with this program; if not, write to the Free Software
 22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 23 *
 24 * Should you need to contact me, the author, you can do so either by
 25 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
 26 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
 27 */
 28
 29#include <linux/kernel.h>
 30#include <linux/module.h>
 31#include <linux/slab.h>
 32#include <linux/gameport.h>
 33#include <linux/input.h>
 34#include <linux/jiffies.h>
 35
 36#define DRIVER_DESC	"Gravis GrIP protocol joystick driver"
 37
 38MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 39MODULE_DESCRIPTION(DRIVER_DESC);
 40MODULE_LICENSE("GPL");
 41
 42#define GRIP_MODE_GPP		1
 43#define GRIP_MODE_BD		2
 44#define GRIP_MODE_XT		3
 45#define GRIP_MODE_DC		4
 46
 47#define GRIP_LENGTH_GPP		24
 48#define GRIP_STROBE_GPP		200	/* 200 us */
 49#define GRIP_LENGTH_XT		4
 50#define GRIP_STROBE_XT		64	/* 64 us */
 51#define GRIP_MAX_CHUNKS_XT	10
 52#define GRIP_MAX_BITS_XT	30
 53
 54struct grip {
 55	struct gameport *gameport;
 56	struct input_dev *dev[2];
 57	unsigned char mode[2];
 58	int reads;
 59	int bads;
 60	char phys[2][32];
 61};
 62
 63static 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 };
 64static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
 65static 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 };
 66static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
 67
 68static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
 69static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
 70static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
 71static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
 72
 73static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
 74				"Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
 75static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
 76static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
 77static char grip_anx[] = { 0, 0, 3, 5, 5 };
 78static char grip_cen[] = { 0, 0, 2, 2, 4 };
 79
 80/*
 81 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
 82 */
 83
 84static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
 85{
 86	unsigned long flags;
 87	unsigned char u, v;
 88	unsigned int t;
 89	int i;
 90
 91	int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
 92
 93	data[0] = 0;
 94	t = strobe;
 95	i = 0;
 96
 97	local_irq_save(flags);
 98
 99	v = gameport_read(gameport) >> shift;
100
101	do {
102		t--;
103		u = v; v = (gameport_read(gameport) >> shift) & 3;
104		if (~v & u & 1) {
105			data[0] |= (v >> 1) << i++;
106			t = strobe;
107		}
108	} while (i < GRIP_LENGTH_GPP && t > 0);
109
110	local_irq_restore(flags);
111
112	if (i < GRIP_LENGTH_GPP) return -1;
113
114	for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
115		data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
116
117	return -(i == GRIP_LENGTH_GPP);
118}
119
120/*
121 * grip_xt_read_packet() reads a Gravis Xterminator packet.
122 */
123
124static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
125{
126	unsigned int i, j, buf, crc;
127	unsigned char u, v, w;
128	unsigned long flags;
129	unsigned int t;
130	char status;
131
132	int strobe = gameport_time(gameport, GRIP_STROBE_XT);
133
134	data[0] = data[1] = data[2] = data[3] = 0;
135	status = buf = i = j = 0;
136	t = strobe;
137
138	local_irq_save(flags);
139
140	v = w = (gameport_read(gameport) >> shift) & 3;
141
142	do {
143		t--;
144		u = (gameport_read(gameport) >> shift) & 3;
145
146		if (u ^ v) {
147
148			if ((u ^ v) & 1) {
149				buf = (buf << 1) | (u >> 1);
150				t = strobe;
151				i++;
152			} else
153
154			if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
155				if (i == 20) {
156					crc = buf ^ (buf >> 7) ^ (buf >> 14);
157					if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
158						data[buf >> 18] = buf >> 4;
159						status |= 1 << (buf >> 18);
160					}
161					j++;
162				}
163				t = strobe;
164				buf = 0;
165				i = 0;
166			}
167			w = v;
168			v = u;
169		}
170
171	} while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
172
173	local_irq_restore(flags);
174
175	return -(status != 0xf);
176}
177
178/*
179 * grip_timer() repeatedly polls the joysticks and generates events.
180 */
181
182static void grip_poll(struct gameport *gameport)
183{
184	struct grip *grip = gameport_get_drvdata(gameport);
185	unsigned int data[GRIP_LENGTH_XT];
186	struct input_dev *dev;
187	int i, j;
188
189	for (i = 0; i < 2; i++) {
190
191		dev = grip->dev[i];
192		if (!dev)
193			continue;
194
195		grip->reads++;
196
197		switch (grip->mode[i]) {
198
199			case GRIP_MODE_GPP:
200
201				if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
202					grip->bads++;
203					break;
204				}
205
206				input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
207				input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
208
209				for (j = 0; j < 12; j++)
210					if (grip_btn_gpp[j])
211						input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
212
213				break;
214
215			case GRIP_MODE_BD:
216
217				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
218					grip->bads++;
219					break;
220				}
221
222				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
223				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
224				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
225
226				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
227				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
228
229				for (j = 0; j < 5; j++)
230					input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
231
232				break;
233
234			case GRIP_MODE_XT:
235
236				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
237					grip->bads++;
238					break;
239				}
240
241				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
242				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
243				input_report_abs(dev, ABS_BRAKE,    (data[1] >> 2) & 0x3f);
244				input_report_abs(dev, ABS_GAS,	    (data[1] >> 8) & 0x3f);
245				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
246
247				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
248				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
249				input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
250				input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
251
252				for (j = 0; j < 11; j++)
253					input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
254				break;
255
256			case GRIP_MODE_DC:
257
258				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
259					grip->bads++;
260					break;
261				}
262
263				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
264				input_report_abs(dev, ABS_Y,        (data[0] >> 8) & 0x3f);
265				input_report_abs(dev, ABS_RX,       (data[1] >> 2) & 0x3f);
266				input_report_abs(dev, ABS_RY,	    (data[1] >> 8) & 0x3f);
267				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
268
269				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
270				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
271
272				for (j = 0; j < 9; j++)
273					input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
274				break;
275
276
277		}
278
279		input_sync(dev);
280	}
281}
282
283static int grip_open(struct input_dev *dev)
284{
285	struct grip *grip = input_get_drvdata(dev);
286
287	gameport_start_polling(grip->gameport);
288	return 0;
289}
290
291static void grip_close(struct input_dev *dev)
292{
293	struct grip *grip = input_get_drvdata(dev);
294
295	gameport_stop_polling(grip->gameport);
296}
297
298static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
299{
300	struct grip *grip;
301	struct input_dev *input_dev;
302	unsigned int data[GRIP_LENGTH_XT];
303	int i, j, t;
304	int err;
305
306	if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
307		return -ENOMEM;
308
309	grip->gameport = gameport;
310
311	gameport_set_drvdata(gameport, grip);
312
313	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
314	if (err)
315		goto fail1;
316
317	for (i = 0; i < 2; i++) {
318		if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
319			grip->mode[i] = GRIP_MODE_GPP;
320			continue;
321		}
322		if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
323			if (!(data[3] & 7)) {
324				grip->mode[i] = GRIP_MODE_BD;
325				continue;
326			}
327			if (!(data[2] & 0xf0)) {
328				grip->mode[i] = GRIP_MODE_XT;
329				continue;
330			}
331			grip->mode[i] = GRIP_MODE_DC;
332			continue;
333		}
334	}
335
336	if (!grip->mode[0] && !grip->mode[1]) {
337		err = -ENODEV;
338		goto fail2;
339	}
340
341	gameport_set_poll_handler(gameport, grip_poll);
342	gameport_set_poll_interval(gameport, 20);
343
344	for (i = 0; i < 2; i++) {
345		if (!grip->mode[i])
346			continue;
347
348		grip->dev[i] = input_dev = input_allocate_device();
349		if (!input_dev) {
350			err = -ENOMEM;
351			goto fail3;
352		}
353
354		snprintf(grip->phys[i], sizeof(grip->phys[i]),
355			 "%s/input%d", gameport->phys, i);
356
357		input_dev->name = grip_name[grip->mode[i]];
358		input_dev->phys = grip->phys[i];
359		input_dev->id.bustype = BUS_GAMEPORT;
360		input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
361		input_dev->id.product = grip->mode[i];
362		input_dev->id.version = 0x0100;
363		input_dev->dev.parent = &gameport->dev;
364
365		input_set_drvdata(input_dev, grip);
366
367		input_dev->open = grip_open;
368		input_dev->close = grip_close;
369
370		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
371
372		for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
373
374			if (j < grip_cen[grip->mode[i]])
375				input_set_abs_params(input_dev, t, 14, 52, 1, 2);
376			else if (j < grip_anx[grip->mode[i]])
377				input_set_abs_params(input_dev, t, 3, 57, 1, 0);
378			else
379				input_set_abs_params(input_dev, t, -1, 1, 0, 0);
380		}
381
382		for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
383			if (t > 0)
384				set_bit(t, input_dev->keybit);
385
386		err = input_register_device(grip->dev[i]);
387		if (err)
388			goto fail4;
389	}
390
391	return 0;
392
393 fail4:	input_free_device(grip->dev[i]);
394 fail3:	while (--i >= 0)
395		if (grip->dev[i])
396			input_unregister_device(grip->dev[i]);
397 fail2:	gameport_close(gameport);
398 fail1:	gameport_set_drvdata(gameport, NULL);
399	kfree(grip);
400	return err;
401}
402
403static void grip_disconnect(struct gameport *gameport)
404{
405	struct grip *grip = gameport_get_drvdata(gameport);
406	int i;
407
408	for (i = 0; i < 2; i++)
409		if (grip->dev[i])
410			input_unregister_device(grip->dev[i]);
411	gameport_close(gameport);
412	gameport_set_drvdata(gameport, NULL);
413	kfree(grip);
414}
415
416static struct gameport_driver grip_drv = {
417	.driver		= {
418		.name	= "grip",
419		.owner	= THIS_MODULE,
420	},
421	.description	= DRIVER_DESC,
422	.connect	= grip_connect,
423	.disconnect	= grip_disconnect,
424};
425
426module_gameport_driver(grip_drv);
v5.9
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 *  Copyright (c) 1998-2001 Vojtech Pavlik
  4 */
  5
  6/*
  7 * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
  8 */
  9
 10/*
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 11 */
 12
 13#include <linux/kernel.h>
 14#include <linux/module.h>
 15#include <linux/slab.h>
 16#include <linux/gameport.h>
 17#include <linux/input.h>
 18#include <linux/jiffies.h>
 19
 20#define DRIVER_DESC	"Gravis GrIP protocol joystick driver"
 21
 22MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 23MODULE_DESCRIPTION(DRIVER_DESC);
 24MODULE_LICENSE("GPL");
 25
 26#define GRIP_MODE_GPP		1
 27#define GRIP_MODE_BD		2
 28#define GRIP_MODE_XT		3
 29#define GRIP_MODE_DC		4
 30
 31#define GRIP_LENGTH_GPP		24
 32#define GRIP_STROBE_GPP		200	/* 200 us */
 33#define GRIP_LENGTH_XT		4
 34#define GRIP_STROBE_XT		64	/* 64 us */
 35#define GRIP_MAX_CHUNKS_XT	10
 36#define GRIP_MAX_BITS_XT	30
 37
 38struct grip {
 39	struct gameport *gameport;
 40	struct input_dev *dev[2];
 41	unsigned char mode[2];
 42	int reads;
 43	int bads;
 44	char phys[2][32];
 45};
 46
 47static 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 };
 48static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
 49static 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 };
 50static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
 51
 52static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
 53static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
 54static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
 55static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
 56
 57static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
 58				"Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
 59static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
 60static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
 61static char grip_anx[] = { 0, 0, 3, 5, 5 };
 62static char grip_cen[] = { 0, 0, 2, 2, 4 };
 63
 64/*
 65 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
 66 */
 67
 68static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
 69{
 70	unsigned long flags;
 71	unsigned char u, v;
 72	unsigned int t;
 73	int i;
 74
 75	int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
 76
 77	data[0] = 0;
 78	t = strobe;
 79	i = 0;
 80
 81	local_irq_save(flags);
 82
 83	v = gameport_read(gameport) >> shift;
 84
 85	do {
 86		t--;
 87		u = v; v = (gameport_read(gameport) >> shift) & 3;
 88		if (~v & u & 1) {
 89			data[0] |= (v >> 1) << i++;
 90			t = strobe;
 91		}
 92	} while (i < GRIP_LENGTH_GPP && t > 0);
 93
 94	local_irq_restore(flags);
 95
 96	if (i < GRIP_LENGTH_GPP) return -1;
 97
 98	for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
 99		data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
100
101	return -(i == GRIP_LENGTH_GPP);
102}
103
104/*
105 * grip_xt_read_packet() reads a Gravis Xterminator packet.
106 */
107
108static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
109{
110	unsigned int i, j, buf, crc;
111	unsigned char u, v, w;
112	unsigned long flags;
113	unsigned int t;
114	char status;
115
116	int strobe = gameport_time(gameport, GRIP_STROBE_XT);
117
118	data[0] = data[1] = data[2] = data[3] = 0;
119	status = buf = i = j = 0;
120	t = strobe;
121
122	local_irq_save(flags);
123
124	v = w = (gameport_read(gameport) >> shift) & 3;
125
126	do {
127		t--;
128		u = (gameport_read(gameport) >> shift) & 3;
129
130		if (u ^ v) {
131
132			if ((u ^ v) & 1) {
133				buf = (buf << 1) | (u >> 1);
134				t = strobe;
135				i++;
136			} else
137
138			if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
139				if (i == 20) {
140					crc = buf ^ (buf >> 7) ^ (buf >> 14);
141					if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
142						data[buf >> 18] = buf >> 4;
143						status |= 1 << (buf >> 18);
144					}
145					j++;
146				}
147				t = strobe;
148				buf = 0;
149				i = 0;
150			}
151			w = v;
152			v = u;
153		}
154
155	} while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
156
157	local_irq_restore(flags);
158
159	return -(status != 0xf);
160}
161
162/*
163 * grip_timer() repeatedly polls the joysticks and generates events.
164 */
165
166static void grip_poll(struct gameport *gameport)
167{
168	struct grip *grip = gameport_get_drvdata(gameport);
169	unsigned int data[GRIP_LENGTH_XT];
170	struct input_dev *dev;
171	int i, j;
172
173	for (i = 0; i < 2; i++) {
174
175		dev = grip->dev[i];
176		if (!dev)
177			continue;
178
179		grip->reads++;
180
181		switch (grip->mode[i]) {
182
183			case GRIP_MODE_GPP:
184
185				if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
186					grip->bads++;
187					break;
188				}
189
190				input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
191				input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
192
193				for (j = 0; j < 12; j++)
194					if (grip_btn_gpp[j])
195						input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
196
197				break;
198
199			case GRIP_MODE_BD:
200
201				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
202					grip->bads++;
203					break;
204				}
205
206				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
207				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
208				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
209
210				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
211				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
212
213				for (j = 0; j < 5; j++)
214					input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
215
216				break;
217
218			case GRIP_MODE_XT:
219
220				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
221					grip->bads++;
222					break;
223				}
224
225				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
226				input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
227				input_report_abs(dev, ABS_BRAKE,    (data[1] >> 2) & 0x3f);
228				input_report_abs(dev, ABS_GAS,	    (data[1] >> 8) & 0x3f);
229				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
230
231				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
232				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
233				input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
234				input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
235
236				for (j = 0; j < 11; j++)
237					input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
238				break;
239
240			case GRIP_MODE_DC:
241
242				if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
243					grip->bads++;
244					break;
245				}
246
247				input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
248				input_report_abs(dev, ABS_Y,        (data[0] >> 8) & 0x3f);
249				input_report_abs(dev, ABS_RX,       (data[1] >> 2) & 0x3f);
250				input_report_abs(dev, ABS_RY,	    (data[1] >> 8) & 0x3f);
251				input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
252
253				input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
254				input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
255
256				for (j = 0; j < 9; j++)
257					input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
258				break;
259
260
261		}
262
263		input_sync(dev);
264	}
265}
266
267static int grip_open(struct input_dev *dev)
268{
269	struct grip *grip = input_get_drvdata(dev);
270
271	gameport_start_polling(grip->gameport);
272	return 0;
273}
274
275static void grip_close(struct input_dev *dev)
276{
277	struct grip *grip = input_get_drvdata(dev);
278
279	gameport_stop_polling(grip->gameport);
280}
281
282static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
283{
284	struct grip *grip;
285	struct input_dev *input_dev;
286	unsigned int data[GRIP_LENGTH_XT];
287	int i, j, t;
288	int err;
289
290	if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
291		return -ENOMEM;
292
293	grip->gameport = gameport;
294
295	gameport_set_drvdata(gameport, grip);
296
297	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
298	if (err)
299		goto fail1;
300
301	for (i = 0; i < 2; i++) {
302		if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
303			grip->mode[i] = GRIP_MODE_GPP;
304			continue;
305		}
306		if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
307			if (!(data[3] & 7)) {
308				grip->mode[i] = GRIP_MODE_BD;
309				continue;
310			}
311			if (!(data[2] & 0xf0)) {
312				grip->mode[i] = GRIP_MODE_XT;
313				continue;
314			}
315			grip->mode[i] = GRIP_MODE_DC;
316			continue;
317		}
318	}
319
320	if (!grip->mode[0] && !grip->mode[1]) {
321		err = -ENODEV;
322		goto fail2;
323	}
324
325	gameport_set_poll_handler(gameport, grip_poll);
326	gameport_set_poll_interval(gameport, 20);
327
328	for (i = 0; i < 2; i++) {
329		if (!grip->mode[i])
330			continue;
331
332		grip->dev[i] = input_dev = input_allocate_device();
333		if (!input_dev) {
334			err = -ENOMEM;
335			goto fail3;
336		}
337
338		snprintf(grip->phys[i], sizeof(grip->phys[i]),
339			 "%s/input%d", gameport->phys, i);
340
341		input_dev->name = grip_name[grip->mode[i]];
342		input_dev->phys = grip->phys[i];
343		input_dev->id.bustype = BUS_GAMEPORT;
344		input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
345		input_dev->id.product = grip->mode[i];
346		input_dev->id.version = 0x0100;
347		input_dev->dev.parent = &gameport->dev;
348
349		input_set_drvdata(input_dev, grip);
350
351		input_dev->open = grip_open;
352		input_dev->close = grip_close;
353
354		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
355
356		for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
357
358			if (j < grip_cen[grip->mode[i]])
359				input_set_abs_params(input_dev, t, 14, 52, 1, 2);
360			else if (j < grip_anx[grip->mode[i]])
361				input_set_abs_params(input_dev, t, 3, 57, 1, 0);
362			else
363				input_set_abs_params(input_dev, t, -1, 1, 0, 0);
364		}
365
366		for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
367			if (t > 0)
368				set_bit(t, input_dev->keybit);
369
370		err = input_register_device(grip->dev[i]);
371		if (err)
372			goto fail4;
373	}
374
375	return 0;
376
377 fail4:	input_free_device(grip->dev[i]);
378 fail3:	while (--i >= 0)
379		if (grip->dev[i])
380			input_unregister_device(grip->dev[i]);
381 fail2:	gameport_close(gameport);
382 fail1:	gameport_set_drvdata(gameport, NULL);
383	kfree(grip);
384	return err;
385}
386
387static void grip_disconnect(struct gameport *gameport)
388{
389	struct grip *grip = gameport_get_drvdata(gameport);
390	int i;
391
392	for (i = 0; i < 2; i++)
393		if (grip->dev[i])
394			input_unregister_device(grip->dev[i]);
395	gameport_close(gameport);
396	gameport_set_drvdata(gameport, NULL);
397	kfree(grip);
398}
399
400static struct gameport_driver grip_drv = {
401	.driver		= {
402		.name	= "grip",
403		.owner	= THIS_MODULE,
404	},
405	.description	= DRIVER_DESC,
406	.connect	= grip_connect,
407	.disconnect	= grip_disconnect,
408};
409
410module_gameport_driver(grip_drv);