1 /*
2 * $Id: grip.c,v 1.14 2000/06/06 21:13:36 vojtech Exp $
3 *
4 * Copyright (c) 1998-2000 Vojtech Pavlik
5 *
6 * Sponsored by SuSE
7 */
8
9 /*
10 * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
11 */
12
13 /*
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
18 *
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 *
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, write to the Free Software
26 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 *
28 * Should you need to contact me, the author, you can do so either by
29 * e-mail - mail your message to <vojtech@suse.cz>, or by paper mail:
30 * Vojtech Pavlik, Ucitelska 1576, Prague 8, 182 00 Czech Republic
31 */
32
33 #include <linux/kernel.h>
34 #include <linux/module.h>
35 #include <linux/init.h>
36 #include <linux/slab.h>
37 #include <linux/gameport.h>
38 #include <linux/input.h>
39
40 #define GRIP_MODE_GPP 1
41 #define GRIP_MODE_BD 2
42 #define GRIP_MODE_XT 3
43 #define GRIP_MODE_DC 4
44
45 #define GRIP_LENGTH_GPP 24
46 #define GRIP_STROBE_GPP 200 /* 200 us */
47 #define GRIP_LENGTH_XT 4
48 #define GRIP_STROBE_XT 64 /* 64 us */
49 #define GRIP_MAX_CHUNKS_XT 10
50 #define GRIP_MAX_BITS_XT 30
51
52 #define GRIP_REFRESH_TIME HZ/50 /* 20 ms */
53
54 struct grip {
55 struct gameport *gameport;
56 struct timer_list timer;
57 struct input_dev dev[2];
58 unsigned char mode[2];
59 int used;
60 int reads;
61 int bads;
62 };
63
64 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 };
65 static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
66 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 };
67 static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
68
69 static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
70 static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
71 static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
72 static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
73
74 static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
75 "Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
76 static int *grip_abs[] = { 0, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
77 static int *grip_btn[] = { 0, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
78 static char grip_anx[] = { 0, 0, 3, 5, 5 };
79 static char grip_cen[] = { 0, 0, 2, 2, 4 };
80
81 /*
82 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
83 */
84
grip_gpp_read_packet(struct gameport * gameport,int shift,unsigned int * data)85 static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
86 {
87 unsigned long flags;
88 unsigned char u, v;
89 unsigned int t;
90 int i;
91
92 int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
93
94 data[0] = 0;
95 t = strobe;
96 i = 0;
97
98 __save_flags(flags);
99 __cli();
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 __restore_flags(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
grip_xt_read_packet(struct gameport * gameport,int shift,unsigned int * data)126 static 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 __save_flags(flags);
141 __cli();
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 __restore_flags(flags);
177
178 return -(status != 0xf);
179 }
180
181 /*
182 * grip_timer() repeatedly polls the joysticks and generates events.
183 */
184
grip_timer(unsigned long private)185 static void grip_timer(unsigned long private)
186 {
187 struct grip *grip = (void*) private;
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
194 dev = grip->dev + i;
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
280 mod_timer(&grip->timer, jiffies + GRIP_REFRESH_TIME);
281 }
282
grip_open(struct input_dev * dev)283 static int grip_open(struct input_dev *dev)
284 {
285 struct grip *grip = dev->private;
286 if (!grip->used++)
287 mod_timer(&grip->timer, jiffies + GRIP_REFRESH_TIME);
288 return 0;
289 }
290
grip_close(struct input_dev * dev)291 static void grip_close(struct input_dev *dev)
292 {
293 struct grip *grip = dev->private;
294 if (!--grip->used)
295 del_timer(&grip->timer);
296 }
297
grip_connect(struct gameport * gameport,struct gameport_dev * dev)298 static void grip_connect(struct gameport *gameport, struct gameport_dev *dev)
299 {
300 struct grip *grip;
301 unsigned int data[GRIP_LENGTH_XT];
302 int i, j, t;
303
304 if (!(grip = kmalloc(sizeof(struct grip), GFP_KERNEL)))
305 return;
306 memset(grip, 0, sizeof(struct grip));
307
308 gameport->private = grip;
309
310 grip->gameport = gameport;
311 init_timer(&grip->timer);
312 grip->timer.data = (long) grip;
313 grip->timer.function = grip_timer;
314
315 if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
316 goto fail1;
317
318 for (i = 0; i < 2; i++) {
319 if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
320 grip->mode[i] = GRIP_MODE_GPP;
321 continue;
322 }
323 if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
324 if (!(data[3] & 7)) {
325 grip->mode[i] = GRIP_MODE_BD;
326 continue;
327 }
328 if (!(data[2] & 0xf0)) {
329 grip->mode[i] = GRIP_MODE_XT;
330 continue;
331 }
332 grip->mode[i] = GRIP_MODE_DC;
333 continue;
334 }
335 }
336
337 if (!grip->mode[0] && !grip->mode[1])
338 goto fail2;
339
340 for (i = 0; i < 2; i++)
341 if (grip->mode[i]) {
342
343 grip->dev[i].private = grip;
344
345 grip->dev[i].open = grip_open;
346 grip->dev[i].close = grip_close;
347
348 grip->dev[i].name = grip_name[grip->mode[i]];
349 grip->dev[i].idbus = BUS_GAMEPORT;
350 grip->dev[i].idvendor = GAMEPORT_ID_VENDOR_GRAVIS;
351 grip->dev[i].idproduct = grip->mode[i];
352 grip->dev[i].idversion = 0x0100;
353
354 grip->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
355
356 for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
357
358 set_bit(t, grip->dev[i].absbit);
359
360 if (j < grip_cen[grip->mode[i]]) {
361 grip->dev[i].absmin[t] = 14;
362 grip->dev[i].absmax[t] = 52;
363 grip->dev[i].absfuzz[t] = 1;
364 grip->dev[i].absflat[t] = 2;
365 continue;
366 }
367
368 if (j < grip_anx[grip->mode[i]]) {
369 grip->dev[i].absmin[t] = 3;
370 grip->dev[i].absmax[t] = 57;
371 grip->dev[i].absfuzz[t] = 1;
372 continue;
373 }
374
375 grip->dev[i].absmin[t] = -1;
376 grip->dev[i].absmax[t] = 1;
377 }
378
379 for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
380 if (t > 0)
381 set_bit(t, grip->dev[i].keybit);
382
383 input_register_device(grip->dev + i);
384
385 printk(KERN_INFO "input%d: %s on gameport%d.%d\n",
386 grip->dev[i].number, grip_name[grip->mode[i]], gameport->number, i);
387 }
388
389 return;
390 fail2: gameport_close(gameport);
391 fail1: kfree(grip);
392 }
393
grip_disconnect(struct gameport * gameport)394 static void grip_disconnect(struct gameport *gameport)
395 {
396 int i;
397
398 struct grip *grip = gameport->private;
399 for (i = 0; i < 2; i++)
400 if (grip->mode[i])
401 input_unregister_device(grip->dev + i);
402 gameport_close(gameport);
403 kfree(grip);
404 }
405
406 static struct gameport_dev grip_dev = {
407 connect: grip_connect,
408 disconnect: grip_disconnect,
409 };
410
grip_init(void)411 int __init grip_init(void)
412 {
413 gameport_register_device(&grip_dev);
414 return 0;
415 }
416
grip_exit(void)417 void __exit grip_exit(void)
418 {
419 gameport_unregister_device(&grip_dev);
420 }
421
422 module_init(grip_init);
423 module_exit(grip_exit);
424
425 MODULE_LICENSE("GPL");
426