PageRenderTime 51ms CodeModel.GetById 17ms app.highlight 29ms RepoModel.GetById 2ms app.codeStats 0ms

/drivers/input/joystick/grip.c

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