Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / joystick / grip.c
blobfe798bc87950193b0d650ae12c032c1a563d526a
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 1998-2001 Vojtech Pavlik
4 */
6 /*
7 * Gravis/Kensington GrIP protocol joystick and gamepad driver for Linux
8 */
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/slab.h>
16 #include <linux/gameport.h>
17 #include <linux/input.h>
18 #include <linux/jiffies.h>
20 #define DRIVER_DESC "Gravis GrIP protocol joystick driver"
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION(DRIVER_DESC);
24 MODULE_LICENSE("GPL");
26 #define GRIP_MODE_GPP 1
27 #define GRIP_MODE_BD 2
28 #define GRIP_MODE_XT 3
29 #define GRIP_MODE_DC 4
31 #define GRIP_LENGTH_GPP 24
32 #define GRIP_STROBE_GPP 200 /* 200 us */
33 #define GRIP_LENGTH_XT 4
34 #define GRIP_STROBE_XT 64 /* 64 us */
35 #define GRIP_MAX_CHUNKS_XT 10
36 #define GRIP_MAX_BITS_XT 30
38 struct grip {
39 struct gameport *gameport;
40 struct input_dev *dev[2];
41 unsigned char mode[2];
42 int reads;
43 int bads;
44 char phys[2][32];
47 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 };
48 static int grip_btn_bd[] = { BTN_THUMB, BTN_THUMB2, BTN_TRIGGER, BTN_TOP, BTN_BASE, -1 };
49 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 };
50 static int grip_btn_dc[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_BASE5, -1 };
52 static int grip_abs_gpp[] = { ABS_X, ABS_Y, -1 };
53 static int grip_abs_bd[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
54 static int grip_abs_xt[] = { ABS_X, ABS_Y, ABS_BRAKE, ABS_GAS, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, -1 };
55 static int grip_abs_dc[] = { ABS_X, ABS_Y, ABS_RX, ABS_RY, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, -1 };
57 static char *grip_name[] = { NULL, "Gravis GamePad Pro", "Gravis Blackhawk Digital",
58 "Gravis Xterminator Digital", "Gravis Xterminator DualControl" };
59 static int *grip_abs[] = { NULL, grip_abs_gpp, grip_abs_bd, grip_abs_xt, grip_abs_dc };
60 static int *grip_btn[] = { NULL, grip_btn_gpp, grip_btn_bd, grip_btn_xt, grip_btn_dc };
61 static char grip_anx[] = { 0, 0, 3, 5, 5 };
62 static char grip_cen[] = { 0, 0, 2, 2, 4 };
65 * grip_gpp_read_packet() reads a Gravis GamePad Pro packet.
68 static int grip_gpp_read_packet(struct gameport *gameport, int shift, unsigned int *data)
70 unsigned long flags;
71 unsigned char u, v;
72 unsigned int t;
73 int i;
75 int strobe = gameport_time(gameport, GRIP_STROBE_GPP);
77 data[0] = 0;
78 t = strobe;
79 i = 0;
81 local_irq_save(flags);
83 v = gameport_read(gameport) >> shift;
85 do {
86 t--;
87 u = v; v = (gameport_read(gameport) >> shift) & 3;
88 if (~v & u & 1) {
89 data[0] |= (v >> 1) << i++;
90 t = strobe;
92 } while (i < GRIP_LENGTH_GPP && t > 0);
94 local_irq_restore(flags);
96 if (i < GRIP_LENGTH_GPP) return -1;
98 for (i = 0; i < GRIP_LENGTH_GPP && (data[0] & 0xfe4210) ^ 0x7c0000; i++)
99 data[0] = data[0] >> 1 | (data[0] & 1) << (GRIP_LENGTH_GPP - 1);
101 return -(i == GRIP_LENGTH_GPP);
105 * grip_xt_read_packet() reads a Gravis Xterminator packet.
108 static int grip_xt_read_packet(struct gameport *gameport, int shift, unsigned int *data)
110 unsigned int i, j, buf, crc;
111 unsigned char u, v, w;
112 unsigned long flags;
113 unsigned int t;
114 char status;
116 int strobe = gameport_time(gameport, GRIP_STROBE_XT);
118 data[0] = data[1] = data[2] = data[3] = 0;
119 status = buf = i = j = 0;
120 t = strobe;
122 local_irq_save(flags);
124 v = w = (gameport_read(gameport) >> shift) & 3;
126 do {
127 t--;
128 u = (gameport_read(gameport) >> shift) & 3;
130 if (u ^ v) {
132 if ((u ^ v) & 1) {
133 buf = (buf << 1) | (u >> 1);
134 t = strobe;
135 i++;
136 } else
138 if ((((u ^ v) & (v ^ w)) >> 1) & ~(u | v | w) & 1) {
139 if (i == 20) {
140 crc = buf ^ (buf >> 7) ^ (buf >> 14);
141 if (!((crc ^ (0x25cb9e70 >> ((crc >> 2) & 0x1c))) & 0xf)) {
142 data[buf >> 18] = buf >> 4;
143 status |= 1 << (buf >> 18);
145 j++;
147 t = strobe;
148 buf = 0;
149 i = 0;
151 w = v;
152 v = u;
155 } while (status != 0xf && i < GRIP_MAX_BITS_XT && j < GRIP_MAX_CHUNKS_XT && t > 0);
157 local_irq_restore(flags);
159 return -(status != 0xf);
163 * grip_timer() repeatedly polls the joysticks and generates events.
166 static void grip_poll(struct gameport *gameport)
168 struct grip *grip = gameport_get_drvdata(gameport);
169 unsigned int data[GRIP_LENGTH_XT];
170 struct input_dev *dev;
171 int i, j;
173 for (i = 0; i < 2; i++) {
175 dev = grip->dev[i];
176 if (!dev)
177 continue;
179 grip->reads++;
181 switch (grip->mode[i]) {
183 case GRIP_MODE_GPP:
185 if (grip_gpp_read_packet(grip->gameport, (i << 1) + 4, data)) {
186 grip->bads++;
187 break;
190 input_report_abs(dev, ABS_X, ((*data >> 15) & 1) - ((*data >> 16) & 1));
191 input_report_abs(dev, ABS_Y, ((*data >> 13) & 1) - ((*data >> 12) & 1));
193 for (j = 0; j < 12; j++)
194 if (grip_btn_gpp[j])
195 input_report_key(dev, grip_btn_gpp[j], (*data >> j) & 1);
197 break;
199 case GRIP_MODE_BD:
201 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
202 grip->bads++;
203 break;
206 input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f);
207 input_report_abs(dev, ABS_Y, 63 - ((data[0] >> 8) & 0x3f));
208 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
210 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
211 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
213 for (j = 0; j < 5; j++)
214 input_report_key(dev, grip_btn_bd[j], (data[3] >> (j + 4)) & 1);
216 break;
218 case GRIP_MODE_XT:
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_BRAKE, (data[1] >> 2) & 0x3f);
228 input_report_abs(dev, ABS_GAS, (data[1] >> 8) & 0x3f);
229 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
231 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
232 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
233 input_report_abs(dev, ABS_HAT1X, ((data[2] >> 5) & 1) - ((data[2] >> 4) & 1));
234 input_report_abs(dev, ABS_HAT1Y, ((data[2] >> 6) & 1) - ((data[2] >> 7) & 1));
236 for (j = 0; j < 11; j++)
237 input_report_key(dev, grip_btn_xt[j], (data[3] >> (j + 3)) & 1);
238 break;
240 case GRIP_MODE_DC:
242 if (grip_xt_read_packet(grip->gameport, (i << 1) + 4, data)) {
243 grip->bads++;
244 break;
247 input_report_abs(dev, ABS_X, (data[0] >> 2) & 0x3f);
248 input_report_abs(dev, ABS_Y, (data[0] >> 8) & 0x3f);
249 input_report_abs(dev, ABS_RX, (data[1] >> 2) & 0x3f);
250 input_report_abs(dev, ABS_RY, (data[1] >> 8) & 0x3f);
251 input_report_abs(dev, ABS_THROTTLE, (data[2] >> 8) & 0x3f);
253 input_report_abs(dev, ABS_HAT0X, ((data[2] >> 1) & 1) - ( data[2] & 1));
254 input_report_abs(dev, ABS_HAT0Y, ((data[2] >> 2) & 1) - ((data[2] >> 3) & 1));
256 for (j = 0; j < 9; j++)
257 input_report_key(dev, grip_btn_dc[j], (data[3] >> (j + 3)) & 1);
258 break;
263 input_sync(dev);
267 static int grip_open(struct input_dev *dev)
269 struct grip *grip = input_get_drvdata(dev);
271 gameport_start_polling(grip->gameport);
272 return 0;
275 static void grip_close(struct input_dev *dev)
277 struct grip *grip = input_get_drvdata(dev);
279 gameport_stop_polling(grip->gameport);
282 static int grip_connect(struct gameport *gameport, struct gameport_driver *drv)
284 struct grip *grip;
285 struct input_dev *input_dev;
286 unsigned int data[GRIP_LENGTH_XT];
287 int i, j, t;
288 int err;
290 if (!(grip = kzalloc(sizeof(struct grip), GFP_KERNEL)))
291 return -ENOMEM;
293 grip->gameport = gameport;
295 gameport_set_drvdata(gameport, grip);
297 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
298 if (err)
299 goto fail1;
301 for (i = 0; i < 2; i++) {
302 if (!grip_gpp_read_packet(gameport, (i << 1) + 4, data)) {
303 grip->mode[i] = GRIP_MODE_GPP;
304 continue;
306 if (!grip_xt_read_packet(gameport, (i << 1) + 4, data)) {
307 if (!(data[3] & 7)) {
308 grip->mode[i] = GRIP_MODE_BD;
309 continue;
311 if (!(data[2] & 0xf0)) {
312 grip->mode[i] = GRIP_MODE_XT;
313 continue;
315 grip->mode[i] = GRIP_MODE_DC;
316 continue;
320 if (!grip->mode[0] && !grip->mode[1]) {
321 err = -ENODEV;
322 goto fail2;
325 gameport_set_poll_handler(gameport, grip_poll);
326 gameport_set_poll_interval(gameport, 20);
328 for (i = 0; i < 2; i++) {
329 if (!grip->mode[i])
330 continue;
332 grip->dev[i] = input_dev = input_allocate_device();
333 if (!input_dev) {
334 err = -ENOMEM;
335 goto fail3;
338 snprintf(grip->phys[i], sizeof(grip->phys[i]),
339 "%s/input%d", gameport->phys, i);
341 input_dev->name = grip_name[grip->mode[i]];
342 input_dev->phys = grip->phys[i];
343 input_dev->id.bustype = BUS_GAMEPORT;
344 input_dev->id.vendor = GAMEPORT_ID_VENDOR_GRAVIS;
345 input_dev->id.product = grip->mode[i];
346 input_dev->id.version = 0x0100;
347 input_dev->dev.parent = &gameport->dev;
349 input_set_drvdata(input_dev, grip);
351 input_dev->open = grip_open;
352 input_dev->close = grip_close;
354 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
356 for (j = 0; (t = grip_abs[grip->mode[i]][j]) >= 0; j++) {
358 if (j < grip_cen[grip->mode[i]])
359 input_set_abs_params(input_dev, t, 14, 52, 1, 2);
360 else if (j < grip_anx[grip->mode[i]])
361 input_set_abs_params(input_dev, t, 3, 57, 1, 0);
362 else
363 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
366 for (j = 0; (t = grip_btn[grip->mode[i]][j]) >= 0; j++)
367 if (t > 0)
368 set_bit(t, input_dev->keybit);
370 err = input_register_device(grip->dev[i]);
371 if (err)
372 goto fail4;
375 return 0;
377 fail4: input_free_device(grip->dev[i]);
378 fail3: while (--i >= 0)
379 if (grip->dev[i])
380 input_unregister_device(grip->dev[i]);
381 fail2: gameport_close(gameport);
382 fail1: gameport_set_drvdata(gameport, NULL);
383 kfree(grip);
384 return err;
387 static void grip_disconnect(struct gameport *gameport)
389 struct grip *grip = gameport_get_drvdata(gameport);
390 int i;
392 for (i = 0; i < 2; i++)
393 if (grip->dev[i])
394 input_unregister_device(grip->dev[i]);
395 gameport_close(gameport);
396 gameport_set_drvdata(gameport, NULL);
397 kfree(grip);
400 static struct gameport_driver grip_drv = {
401 .driver = {
402 .name = "grip",
403 .owner = THIS_MODULE,
405 .description = DRIVER_DESC,
406 .connect = grip_connect,
407 .disconnect = grip_disconnect,
410 module_gameport_driver(grip_drv);