Linux 2.6.21.1
[linux/fpc-iii.git] / drivers / input / joystick / grip.c
blob17a90c436de85a35398ea2e1334c180b812954aa
1 /*
2 * $Id: grip.c,v 1.21 2002/01/22 20:27:57 vojtech Exp $
4 * Copyright (c) 1998-2001 Vojtech Pavlik
5 */
7 /*
8 * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
9 */
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.
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.
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
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
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 #include <linux/jiffies.h>
39 #define DRIVER_DESC "Gravis GrIP protocol joystick driver"
41 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42 MODULE_DESCRIPTION(DRIVER_DESC);
43 MODULE_LICENSE("GPL");
45 #define GRIP_MODE_GPP 1
46 #define GRIP_MODE_BD 2
47 #define GRIP_MODE_XT 3
48 #define GRIP_MODE_DC 4
50 #define GRIP_LENGTH_GPP 24
51 #define GRIP_STROBE_GPP 200 /* 200 us */
52 #define GRIP_LENGTH_XT 4
53 #define GRIP_STROBE_XT 64 /* 64 us */
54 #define GRIP_MAX_CHUNKS_XT 10
55 #define GRIP_MAX_BITS_XT 30
57 struct grip {
58 struct gameport *gameport;
59 struct input_dev *dev[2];
60 unsigned char mode[2];
61 int reads;
62 int bads;
63 char phys[2][32];
66 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 };
67 static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
68 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 };
69 static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
71 static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
72 static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
73 static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
74 static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
76 static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
77 "Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
78 static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
79 static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
80 static char grip_anx[] = { 0, 0, 3, 5, 5 };
81 static char grip_cen[] = { 0, 0, 2, 2, 4 };
84 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
87 static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
89 unsigned long flags;
90 unsigned char u, v;
91 unsigned int t;
92 int i;
94 int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
96 data[0] = 0;
97 t = strobe;
98 i = 0;
100 local_irq_save(flags);
102 v = gameport_read(gameport) >> shift;
104 do {
105 t--;
106 u = v; v = (gameport_read(gameport) >> shift) & 3;
107 if (~v & u & 1) {
108 data[0] |= (v >> 1) << i++;
109 t = strobe;
111 } while (i < GRIP_LENGTH_GPP && t > 0);
113 local_irq_restore(flags);
115 if (i < GRIP_LENGTH_GPP) return -1;
117 for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
118 data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
120 return -(i == GRIP_LENGTH_GPP);
124 * grip_xt_read_packet() reads a Gravis Xterminator packet.
127 static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
129 unsigned int i, j, buf, crc;
130 unsigned char u, v, w;
131 unsigned long flags;
132 unsigned int t;
133 char status;
135 int strobe = gameport_time(gameport, GRIP_STROBE_XT);
137 data[0] = data[1] = data[2] = data[3] = 0;
138 status = buf = i = j = 0;
139 t = strobe;
141 local_irq_save(flags);
143 v = w = (gameport_read(gameport) >> shift) & 3;
145 do {
146 t--;
147 u = (gameport_read(gameport) >> shift) & 3;
149 if (u ^ v) {
151 if ((u ^ v) & 1) {
152 buf = (buf << 1) | (u >> 1);
153 t = strobe;
154 i++;
155 } else
157 if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
158 if (i == 20) {
159 crc = buf ^ (buf >> 7) ^ (buf >> 14);
160 if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
161 data[buf >> 18] = buf >> 4;
162 status |= 1 << (buf >> 18);
164 j++;
166 t = strobe;
167 buf = 0;
168 i = 0;
170 w = v;
171 v = u;
174 } while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
176 local_irq_restore(flags);
178 return -(status != 0xf);
182 * grip_timer() repeatedly polls the joysticks and generates events.
185 static void grip_poll(struct gameport *gameport)
187 struct grip *grip = gameport_get_drvdata(gameport);
188 unsigned int data[GRIP_LENGTH_XT];
189 struct input_dev *dev;
190 int i, j;
192 for (i = 0; i < 2; i++) {
194 dev = grip->dev[i];
195 if (!dev)
196 continue;
198 grip->reads++;
200 switch (grip->mode[i]) {
202 case GRIP_MODE_GPP:
204 if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
205 grip->bads++;
206 break;
209 input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
210 input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
212 for (j = 0; j < 12; j++)
213 if (grip_btn_gpp[j])
214 input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
216 break;
218 case GRIP_MODE_BD:
220 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
221 grip->bads++;
222 break;
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_THROTTLE, (data[2] >> 8) & 0x3f);
229 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
230 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
232 for (j = 0; j < 5; j++)
233 input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
235 break;
237 case GRIP_MODE_XT:
239 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
240 grip->bads++;
241 break;
244 input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f);
245 input_report_abs(dev, ABS_Y, 63 - ((data[0] >> 8) & 0x3f));
246 input_report_abs(dev, ABS_BRAKE, (data[1] >> 2) & 0x3f);
247 input_report_abs(dev, ABS_GAS, (data[1] >> 8) & 0x3f);
248 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
250 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
251 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
252 input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
253 input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
255 for (j = 0; j < 11; j++)
256 input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
257 break;
259 case GRIP_MODE_DC:
261 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
262 grip->bads++;
263 break;
266 input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f);
267 input_report_abs(dev, ABS_Y, (data[0] >> 8) & 0x3f);
268 input_report_abs(dev, ABS_RX, (data[1] >> 2) & 0x3f);
269 input_report_abs(dev, ABS_RY, (data[1] >> 8) & 0x3f);
270 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
272 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
273 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
275 for (j = 0; j < 9; j++)
276 input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
277 break;
282 input_sync(dev);
286 static int grip_open(struct input_dev *dev)
288 struct grip *grip = dev->private;
290 gameport_start_polling(grip->gameport);
291 return 0;
294 static void grip_close(struct input_dev *dev)
296 struct grip *grip = dev->private;
298 gameport_stop_polling(grip->gameport);
301 static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
303 struct grip *grip;
304 struct input_dev *input_dev;
305 unsigned int data[GRIP_LENGTH_XT];
306 int i, j, t;
307 int err;
309 if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
310 return -ENOMEM;
312 grip->gameport = gameport;
314 gameport_set_drvdata(gameport, grip);
316 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
317 if (err)
318 goto fail1;
320 for (i = 0; i < 2; i++) {
321 if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
322 grip->mode[i] = GRIP_MODE_GPP;
323 continue;
325 if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
326 if (!(data[3] & 7)) {
327 grip->mode[i] = GRIP_MODE_BD;
328 continue;
330 if (!(data[2] & 0xf0)) {
331 grip->mode[i] = GRIP_MODE_XT;
332 continue;
334 grip->mode[i] = GRIP_MODE_DC;
335 continue;
339 if (!grip->mode[0] && !grip->mode[1]) {
340 err = -ENODEV;
341 goto fail2;
344 gameport_set_poll_handler(gameport, grip_poll);
345 gameport_set_poll_interval(gameport, 20);
347 for (i = 0; i < 2; i++) {
348 if (!grip->mode[i])
349 continue;
351 grip->dev[i] = input_dev = input_allocate_device();
352 if (!input_dev) {
353 err = -ENOMEM;
354 goto fail3;
357 snprintf(grip->phys[i], sizeof(grip->phys[i]),
358 "%s/input%d", gameport->phys, i);
360 input_dev->name = grip_name[grip->mode[i]];
361 input_dev->phys = grip->phys[i];
362 input_dev->id.bustype = BUS_GAMEPORT;
363 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
364 input_dev->id.product = grip->mode[i];
365 input_dev->id.version = 0x0100;
366 input_dev->cdev.dev = &gameport->dev;
367 input_dev->private = grip;
369 input_dev->open = grip_open;
370 input_dev->close = grip_close;
372 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
374 for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
376 if (j < grip_cen[grip->mode[i]])
377 input_set_abs_params(input_dev, t, 14, 52, 1, 2);
378 else if (j < grip_anx[grip->mode[i]])
379 input_set_abs_params(input_dev, t, 3, 57, 1, 0);
380 else
381 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
384 for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
385 if (t > 0)
386 set_bit(t, input_dev->keybit);
388 err = input_register_device(grip->dev[i]);
389 if (err)
390 goto fail4;
393 return 0;
395 fail4: input_free_device(grip->dev[i]);
396 fail3: while (--i >= 0)
397 if (grip->dev[i])
398 input_unregister_device(grip->dev[i]);
399 fail2: gameport_close(gameport);
400 fail1: gameport_set_drvdata(gameport, NULL);
401 kfree(grip);
402 return err;
405 static void grip_disconnect(struct gameport *gameport)
407 struct grip *grip = gameport_get_drvdata(gameport);
408 int i;
410 for (i = 0; i < 2; i++)
411 if (grip->dev[i])
412 input_unregister_device(grip->dev[i]);
413 gameport_close(gameport);
414 gameport_set_drvdata(gameport, NULL);
415 kfree(grip);
418 static struct gameport_driver grip_drv = {
419 .driver = {
420 .name = "grip",
421 .owner = THIS_MODULE,
423 .description = DRIVER_DESC,
424 .connect = grip_connect,
425 .disconnect = grip_disconnect,
428 static int __init grip_init(void)
430 gameport_register_driver(&grip_drv);
431 return 0;
434 static void __exit grip_exit(void)
436 gameport_unregister_driver(&grip_drv);
439 module_init(grip_init);
440 module_exit(grip_exit);