blob: a936e7aedb103552e617010d7260a74664c6ff29 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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>
Tim Schmielau4e57b682005-10-30 15:03:48 -080037#include <linux/jiffies.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070038
39#define DRIVER_DESC "Gravis GrIP protocol joystick driver"
40
41MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
42MODULE_DESCRIPTION(DRIVER_DESC);
43MODULE_LICENSE("GPL");
44
45#define GRIP_MODE_GPP 1
46#define GRIP_MODE_BD 2
47#define GRIP_MODE_XT 3
48#define GRIP_MODE_DC 4
49
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
56
57struct grip {
58 struct gameport *gameport;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -050059 struct input_dev *dev[2];
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 unsigned char mode[2];
61 int reads;
62 int bads;
63 char phys[2][32];
64};
65
66static 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 };
67static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
68static 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 };
69static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
70
71static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
72static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
73static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
74static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
75
76static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
77 "Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
78static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
79static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
80static char grip_anx[] = { 0, 0, 3, 5, 5 };
81static char grip_cen[] = { 0, 0, 2, 2, 4 };
82
83/*
84 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
85 */
86
87static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
88{
89 unsigned long flags;
90 unsigned char u, v;
91 unsigned int t;
92 int i;
93
94 int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
95
96 data[0] = 0;
97 t = strobe;
98 i = 0;
99
100 local_irq_save(flags);
101
102 v = gameport_read(gameport) >> shift;
103
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;
110 }
111 } while (i < GRIP_LENGTH_GPP && t > 0);
112
113 local_irq_restore(flags);
114
115 if (i < GRIP_LENGTH_GPP) return -1;
116
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);
119
120 return -(i == GRIP_LENGTH_GPP);
121}
122
123/*
124 * grip_xt_read_packet() reads a Gravis Xterminator packet.
125 */
126
127static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
128{
129 unsigned int i, j, buf, crc;
130 unsigned char u, v, w;
131 unsigned long flags;
132 unsigned int t;
133 char status;
134
135 int strobe = gameport_time(gameport, GRIP_STROBE_XT);
136
137 data[0] = data[1] = data[2] = data[3] = 0;
138 status = buf = i = j = 0;
139 t = strobe;
140
141 local_irq_save(flags);
142
143 v = w = (gameport_read(gameport) >> shift) & 3;
144
145 do {
146 t--;
147 u = (gameport_read(gameport) >> shift) & 3;
148
149 if (u ^ v) {
150
151 if ((u ^ v) & 1) {
152 buf = (buf << 1) | (u >> 1);
153 t = strobe;
154 i++;
155 } else
156
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);
163 }
164 j++;
165 }
166 t = strobe;
167 buf = 0;
168 i = 0;
169 }
170 w = v;
171 v = u;
172 }
173
174 } while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
175
176 local_irq_restore(flags);
177
178 return -(status != 0xf);
179}
180
181/*
182 * grip_timer() repeatedly polls the joysticks and generates events.
183 */
184
185static void grip_poll(struct gameport *gameport)
186{
187 struct grip *grip = gameport_get_drvdata(gameport);
188 unsigned int data[GRIP_LENGTH_XT];
189 struct input_dev *dev;
190 int i, j;
191
192 for (i = 0; i < 2; i++) {
193
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500194 dev = grip->dev[i];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 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 = dev->private;
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 = dev->private;
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;
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500301 struct input_dev *input_dev;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302 unsigned int data[GRIP_LENGTH_XT];
303 int i, j, t;
304 int err;
305
Pekka Enberga97e1482005-09-06 15:18:33 -0700306 if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 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
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500344 for (i = 0; i < 2; i++) {
345 if (!grip->mode[i])
346 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500348 grip->dev[i] = input_dev = input_allocate_device();
349 if (!input_dev) {
350 err = -ENOMEM;
351 goto fail3;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352 }
353
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500354 sprintf(grip->phys[i], "%s/input%d", gameport->phys, i);
355
356 input_dev->name = grip_name[grip->mode[i]];
357 input_dev->phys = grip->phys[i];
358 input_dev->id.bustype = BUS_GAMEPORT;
359 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
360 input_dev->id.product = grip->mode[i];
361 input_dev->id.version = 0x0100;
362 input_dev->cdev.dev = &gameport->dev;
363 input_dev->private = grip;
364
365 input_dev->open = grip_open;
366 input_dev->close = grip_close;
367
368 input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
369
370 for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
371
372 if (j < grip_cen[grip->mode[i]])
373 input_set_abs_params(input_dev, t, 14, 52, 1, 2);
374 else if (j < grip_anx[grip->mode[i]])
375 input_set_abs_params(input_dev, t, 3, 57, 1, 0);
376 else
377 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
378 }
379
380 for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
381 if (t > 0)
382 set_bit(t, input_dev->keybit);
383
384 input_register_device(grip->dev[i]);
385 }
386
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 return 0;
388
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500389 fail3: for (i = 0; i < 2; i++)
390 if (grip->dev[i])
391 input_unregister_device(grip->dev[i]);
392 fail2: gameport_close(gameport);
393 fail1: gameport_set_drvdata(gameport, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 kfree(grip);
395 return err;
396}
397
398static void grip_disconnect(struct gameport *gameport)
399{
400 struct grip *grip = gameport_get_drvdata(gameport);
401 int i;
402
403 for (i = 0; i < 2; i++)
Dmitry Torokhov17dd3f02005-09-15 02:01:52 -0500404 if (grip->dev[i])
405 input_unregister_device(grip->dev[i]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 gameport_close(gameport);
407 gameport_set_drvdata(gameport, NULL);
408 kfree(grip);
409}
410
411static struct gameport_driver grip_drv = {
412 .driver = {
413 .name = "grip",
414 },
415 .description = DRIVER_DESC,
416 .connect = grip_connect,
417 .disconnect = grip_disconnect,
418};
419
420static int __init grip_init(void)
421{
422 gameport_register_driver(&grip_drv);
423 return 0;
424}
425
426static void __exit grip_exit(void)
427{
428 gameport_unregister_driver(&grip_drv);
429}
430
431module_init(grip_init);
432module_exit(grip_exit);