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