Merge branch 'release' of git://git.kernel.org/pub/scm/linux/kernel/git/aegl/linux-2.6
[pandora-kernel.git] / drivers / input / joystick / grip.c
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/init.h>
32 #include <linux/slab.h>
33 #include <linux/gameport.h>
34 #include <linux/input.h>
35 #include <linux/jiffies.h>
36
37 #define DRIVER_DESC     "Gravis GrIP protocol joystick driver"
38
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION(DRIVER_DESC);
41 MODULE_LICENSE("GPL");
42
43 #define GRIP_MODE_GPP           1
44 #define GRIP_MODE_BD            2
45 #define GRIP_MODE_XT            3
46 #define GRIP_MODE_DC            4
47
48 #define GRIP_LENGTH_GPP         24
49 #define GRIP_STROBE_GPP         200     /* 200 us */
50 #define GRIP_LENGTH_XT          4
51 #define GRIP_STROBE_XT          64      /* 64 us */
52 #define GRIP_MAX_CHUNKS_XT      10
53 #define GRIP_MAX_BITS_XT        30
54
55 struct grip {
56         struct gameport *gameport;
57         struct input_dev *dev[2];
58         unsigned char mode[2];
59         int reads;
60         int bads;
61         char phys[2][32];
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[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
77 static int *grip_btn[] = { NULL, 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
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         local_irq_save(flags);
99
100         v = gameport_read(gameport) >> shift;
101
102         do {
103                 t--;
104                 u = v; v = (gameport_read(gameport) >> shift) & 3;
105                 if (~v & u & 1) {
106                         data[0] |= (v >> 1) << i++;
107                         t = strobe;
108                 }
109         } while (i < GRIP_LENGTH_GPP && t > 0);
110
111         local_irq_restore(flags);
112
113         if (i < GRIP_LENGTH_GPP) return -1;
114
115         for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
116                 data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
117
118         return -(i == GRIP_LENGTH_GPP);
119 }
120
121 /*
122  * grip_xt_read_packet() reads a Gravis Xterminator packet.
123  */
124
125 static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
126 {
127         unsigned int i, j, buf, crc;
128         unsigned char u, v, w;
129         unsigned long flags;
130         unsigned int t;
131         char status;
132
133         int strobe = gameport_time(gameport, GRIP_STROBE_XT);
134
135         data[0] = data[1] = data[2] = data[3] = 0;
136         status = buf = i = j = 0;
137         t = strobe;
138
139         local_irq_save(flags);
140
141         v = w = (gameport_read(gameport) >> shift) & 3;
142
143         do {
144                 t--;
145                 u = (gameport_read(gameport) >> shift) & 3;
146
147                 if (u ^ v) {
148
149                         if ((u ^ v) & 1) {
150                                 buf = (buf << 1) | (u >> 1);
151                                 t = strobe;
152                                 i++;
153                         } else
154
155                         if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
156                                 if (i == 20) {
157                                         crc = buf ^ (buf >> 7) ^ (buf >> 14);
158                                         if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
159                                                 data[buf >> 18] = buf >> 4;
160                                                 status |= 1 << (buf >> 18);
161                                         }
162                                         j++;
163                                 }
164                                 t = strobe;
165                                 buf = 0;
166                                 i = 0;
167                         }
168                         w = v;
169                         v = u;
170                 }
171
172         } while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
173
174         local_irq_restore(flags);
175
176         return -(status != 0xf);
177 }
178
179 /*
180  * grip_timer() repeatedly polls the joysticks and generates events.
181  */
182
183 static void grip_poll(struct gameport *gameport)
184 {
185         struct grip *grip = gameport_get_drvdata(gameport);
186         unsigned int data[GRIP_LENGTH_XT];
187         struct input_dev *dev;
188         int i, j;
189
190         for (i = 0; i < 2; i++) {
191
192                 dev = grip->dev[i];
193                 if (!dev)
194                         continue;
195
196                 grip->reads++;
197
198                 switch (grip->mode[i]) {
199
200                         case GRIP_MODE_GPP:
201
202                                 if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
203                                         grip->bads++;
204                                         break;
205                                 }
206
207                                 input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
208                                 input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
209
210                                 for (j = 0; j < 12; j++)
211                                         if (grip_btn_gpp[j])
212                                                 input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
213
214                                 break;
215
216                         case GRIP_MODE_BD:
217
218                                 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
219                                         grip->bads++;
220                                         break;
221                                 }
222
223                                 input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
224                                 input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
225                                 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
226
227                                 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
228                                 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
229
230                                 for (j = 0; j < 5; j++)
231                                         input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
232
233                                 break;
234
235                         case GRIP_MODE_XT:
236
237                                 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
238                                         grip->bads++;
239                                         break;
240                                 }
241
242                                 input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
243                                 input_report_abs(dev, ABS_Y,  63 - ((data[0] >> 8) & 0x3f));
244                                 input_report_abs(dev, ABS_BRAKE,    (data[1] >> 2) & 0x3f);
245                                 input_report_abs(dev, ABS_GAS,      (data[1] >> 8) & 0x3f);
246                                 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
247
248                                 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
249                                 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
250                                 input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
251                                 input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
252
253                                 for (j = 0; j < 11; j++)
254                                         input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
255                                 break;
256
257                         case GRIP_MODE_DC:
258
259                                 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
260                                         grip->bads++;
261                                         break;
262                                 }
263
264                                 input_report_abs(dev, ABS_X,        (data[0] >> 2) & 0x3f);
265                                 input_report_abs(dev, ABS_Y,        (data[0] >> 8) & 0x3f);
266                                 input_report_abs(dev, ABS_RX,       (data[1] >> 2) & 0x3f);
267                                 input_report_abs(dev, ABS_RY,       (data[1] >> 8) & 0x3f);
268                                 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
269
270                                 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2]       & 1));
271                                 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
272
273                                 for (j = 0; j < 9; j++)
274                                         input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
275                                 break;
276
277
278                 }
279
280                 input_sync(dev);
281         }
282 }
283
284 static int grip_open(struct input_dev *dev)
285 {
286         struct grip *grip = input_get_drvdata(dev);
287
288         gameport_start_polling(grip->gameport);
289         return 0;
290 }
291
292 static void grip_close(struct input_dev *dev)
293 {
294         struct grip *grip = input_get_drvdata(dev);
295
296         gameport_stop_polling(grip->gameport);
297 }
298
299 static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
300 {
301         struct grip *grip;
302         struct input_dev *input_dev;
303         unsigned int data[GRIP_LENGTH_XT];
304         int i, j, t;
305         int err;
306
307         if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
308                 return -ENOMEM;
309
310         grip->gameport = gameport;
311
312         gameport_set_drvdata(gameport, grip);
313
314         err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
315         if (err)
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                 err = -ENODEV;
339                 goto fail2;
340         }
341
342         gameport_set_poll_handler(gameport, grip_poll);
343         gameport_set_poll_interval(gameport, 20);
344
345         for (i = 0; i < 2; i++) {
346                 if (!grip->mode[i])
347                         continue;
348
349                 grip->dev[i] = input_dev = input_allocate_device();
350                 if (!input_dev) {
351                         err = -ENOMEM;
352                         goto fail3;
353                 }
354
355                 snprintf(grip->phys[i], sizeof(grip->phys[i]),
356                          "%s/input%d", gameport->phys, i);
357
358                 input_dev->name = grip_name[grip->mode[i]];
359                 input_dev->phys = grip->phys[i];
360                 input_dev->id.bustype = BUS_GAMEPORT;
361                 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
362                 input_dev->id.product = grip->mode[i];
363                 input_dev->id.version = 0x0100;
364                 input_dev->dev.parent = &gameport->dev;
365
366                 input_set_drvdata(input_dev, grip);
367
368                 input_dev->open = grip_open;
369                 input_dev->close = grip_close;
370
371                 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
372
373                 for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
374
375                         if (j < grip_cen[grip->mode[i]])
376                                 input_set_abs_params(input_dev, t, 14, 52, 1, 2);
377                         else if (j < grip_anx[grip->mode[i]])
378                                 input_set_abs_params(input_dev, t, 3, 57, 1, 0);
379                         else
380                                 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
381                 }
382
383                 for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
384                         if (t > 0)
385                                 set_bit(t, input_dev->keybit);
386
387                 err = input_register_device(grip->dev[i]);
388                 if (err)
389                         goto fail4;
390         }
391
392         return 0;
393
394  fail4: input_free_device(grip->dev[i]);
395  fail3: while (--i >= 0)
396                 if (grip->dev[i])
397                         input_unregister_device(grip->dev[i]);
398  fail2: gameport_close(gameport);
399  fail1: gameport_set_drvdata(gameport, NULL);
400         kfree(grip);
401         return err;
402 }
403
404 static void grip_disconnect(struct gameport *gameport)
405 {
406         struct grip *grip = gameport_get_drvdata(gameport);
407         int i;
408
409         for (i = 0; i < 2; i++)
410                 if (grip->dev[i])
411                         input_unregister_device(grip->dev[i]);
412         gameport_close(gameport);
413         gameport_set_drvdata(gameport, NULL);
414         kfree(grip);
415 }
416
417 static struct gameport_driver grip_drv = {
418         .driver         = {
419                 .name   = "grip",
420                 .owner  = THIS_MODULE,
421         },
422         .description    = DRIVER_DESC,
423         .connect        = grip_connect,
424         .disconnect     = grip_disconnect,
425 };
426
427 static int __init grip_init(void)
428 {
429         gameport_register_driver(&grip_drv);
430         return 0;
431 }
432
433 static void __exit grip_exit(void)
434 {
435         gameport_unregister_driver(&grip_drv);
436 }
437
438 module_init(grip_init);
439 module_exit(grip_exit);