Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / input / joystick / a3d.c
blob68475fad177c401aad7e91024e415180c2e1f842
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 1998-2001 Vojtech Pavlik
4 */
6 /*
7 * FP-Gaming Assassin 3D joystick 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 "FP-Gaming Assassin 3D joystick driver"
22 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
23 MODULE_DESCRIPTION(DRIVER_DESC);
24 MODULE_LICENSE("GPL");
26 #define A3D_MAX_START 600 /* 600 us */
27 #define A3D_MAX_STROBE 80 /* 80 us */
28 #define A3D_MAX_LENGTH 40 /* 40*3 bits */
30 #define A3D_MODE_A3D 1 /* Assassin 3D */
31 #define A3D_MODE_PAN 2 /* Panther */
32 #define A3D_MODE_OEM 3 /* Panther OEM version */
33 #define A3D_MODE_PXL 4 /* Panther XL */
35 static char *a3d_names[] = { NULL, "FP-Gaming Assassin 3D", "MadCatz Panther", "OEM Panther",
36 "MadCatz Panther XL", "MadCatz Panther XL w/ rudder" };
38 struct a3d {
39 struct gameport *gameport;
40 struct gameport *adc;
41 struct input_dev *dev;
42 int axes[4];
43 int buttons;
44 int mode;
45 int length;
46 int reads;
47 int bads;
48 char phys[32];
52 * a3d_read_packet() reads an Assassin 3D packet.
55 static int a3d_read_packet(struct gameport *gameport, int length, char *data)
57 unsigned long flags;
58 unsigned char u, v;
59 unsigned int t, s;
60 int i;
62 i = 0;
63 t = gameport_time(gameport, A3D_MAX_START);
64 s = gameport_time(gameport, A3D_MAX_STROBE);
66 local_irq_save(flags);
67 gameport_trigger(gameport);
68 v = gameport_read(gameport);
70 while (t > 0 && i < length) {
71 t--;
72 u = v; v = gameport_read(gameport);
73 if (~v & u & 0x10) {
74 data[i++] = v >> 5;
75 t = s;
79 local_irq_restore(flags);
81 return i;
85 * a3d_csum() computes checksum of triplet packet
88 static int a3d_csum(char *data, int count)
90 int i, csum = 0;
92 for (i = 0; i < count - 2; i++)
93 csum += data[i];
94 return (csum & 0x3f) != ((data[count - 2] << 3) | data[count - 1]);
97 static void a3d_read(struct a3d *a3d, unsigned char *data)
99 struct input_dev *dev = a3d->dev;
101 switch (a3d->mode) {
103 case A3D_MODE_A3D:
104 case A3D_MODE_OEM:
105 case A3D_MODE_PAN:
107 input_report_rel(dev, REL_X, ((data[5] << 6) | (data[6] << 3) | data[ 7]) - ((data[5] & 4) << 7));
108 input_report_rel(dev, REL_Y, ((data[8] << 6) | (data[9] << 3) | data[10]) - ((data[8] & 4) << 7));
110 input_report_key(dev, BTN_RIGHT, data[2] & 1);
111 input_report_key(dev, BTN_LEFT, data[3] & 2);
112 input_report_key(dev, BTN_MIDDLE, data[3] & 4);
114 input_sync(dev);
116 a3d->axes[0] = ((signed char)((data[11] << 6) | (data[12] << 3) | (data[13]))) + 128;
117 a3d->axes[1] = ((signed char)((data[14] << 6) | (data[15] << 3) | (data[16]))) + 128;
118 a3d->axes[2] = ((signed char)((data[17] << 6) | (data[18] << 3) | (data[19]))) + 128;
119 a3d->axes[3] = ((signed char)((data[20] << 6) | (data[21] << 3) | (data[22]))) + 128;
121 a3d->buttons = ((data[3] << 3) | data[4]) & 0xf;
123 break;
125 case A3D_MODE_PXL:
127 input_report_rel(dev, REL_X, ((data[ 9] << 6) | (data[10] << 3) | data[11]) - ((data[ 9] & 4) << 7));
128 input_report_rel(dev, REL_Y, ((data[12] << 6) | (data[13] << 3) | data[14]) - ((data[12] & 4) << 7));
130 input_report_key(dev, BTN_RIGHT, data[2] & 1);
131 input_report_key(dev, BTN_LEFT, data[3] & 2);
132 input_report_key(dev, BTN_MIDDLE, data[3] & 4);
133 input_report_key(dev, BTN_SIDE, data[7] & 2);
134 input_report_key(dev, BTN_EXTRA, data[7] & 4);
136 input_report_abs(dev, ABS_X, ((signed char)((data[15] << 6) | (data[16] << 3) | (data[17]))) + 128);
137 input_report_abs(dev, ABS_Y, ((signed char)((data[18] << 6) | (data[19] << 3) | (data[20]))) + 128);
138 input_report_abs(dev, ABS_RUDDER, ((signed char)((data[21] << 6) | (data[22] << 3) | (data[23]))) + 128);
139 input_report_abs(dev, ABS_THROTTLE, ((signed char)((data[24] << 6) | (data[25] << 3) | (data[26]))) + 128);
141 input_report_abs(dev, ABS_HAT0X, ( data[5] & 1) - ((data[5] >> 2) & 1));
142 input_report_abs(dev, ABS_HAT0Y, ((data[5] >> 1) & 1) - ((data[6] >> 2) & 1));
143 input_report_abs(dev, ABS_HAT1X, ((data[4] >> 1) & 1) - ( data[3] & 1));
144 input_report_abs(dev, ABS_HAT1Y, ((data[4] >> 2) & 1) - ( data[4] & 1));
146 input_report_key(dev, BTN_TRIGGER, data[8] & 1);
147 input_report_key(dev, BTN_THUMB, data[8] & 2);
148 input_report_key(dev, BTN_TOP, data[8] & 4);
149 input_report_key(dev, BTN_PINKIE, data[7] & 1);
151 input_sync(dev);
153 break;
159 * a3d_poll() reads and analyzes A3D joystick data.
162 static void a3d_poll(struct gameport *gameport)
164 struct a3d *a3d = gameport_get_drvdata(gameport);
165 unsigned char data[A3D_MAX_LENGTH];
167 a3d->reads++;
168 if (a3d_read_packet(a3d->gameport, a3d->length, data) != a3d->length ||
169 data[0] != a3d->mode || a3d_csum(data, a3d->length))
170 a3d->bads++;
171 else
172 a3d_read(a3d, data);
176 * a3d_adc_cooked_read() copies the acis and button data to the
177 * callers arrays. It could do the read itself, but the caller could
178 * call this more than 50 times a second, which would use too much CPU.
181 static int a3d_adc_cooked_read(struct gameport *gameport, int *axes, int *buttons)
183 struct a3d *a3d = gameport->port_data;
184 int i;
186 for (i = 0; i < 4; i++)
187 axes[i] = (a3d->axes[i] < 254) ? a3d->axes[i] : -1;
188 *buttons = a3d->buttons;
189 return 0;
193 * a3d_adc_open() is the gameport open routine. It refuses to serve
194 * any but cooked data.
197 static int a3d_adc_open(struct gameport *gameport, int mode)
199 struct a3d *a3d = gameport->port_data;
201 if (mode != GAMEPORT_MODE_COOKED)
202 return -1;
204 gameport_start_polling(a3d->gameport);
205 return 0;
209 * a3d_adc_close() is a callback from the input close routine.
212 static void a3d_adc_close(struct gameport *gameport)
214 struct a3d *a3d = gameport->port_data;
216 gameport_stop_polling(a3d->gameport);
220 * a3d_open() is a callback from the input open routine.
223 static int a3d_open(struct input_dev *dev)
225 struct a3d *a3d = input_get_drvdata(dev);
227 gameport_start_polling(a3d->gameport);
228 return 0;
232 * a3d_close() is a callback from the input close routine.
235 static void a3d_close(struct input_dev *dev)
237 struct a3d *a3d = input_get_drvdata(dev);
239 gameport_stop_polling(a3d->gameport);
243 * a3d_connect() probes for A3D joysticks.
246 static int a3d_connect(struct gameport *gameport, struct gameport_driver *drv)
248 struct a3d *a3d;
249 struct input_dev *input_dev;
250 struct gameport *adc;
251 unsigned char data[A3D_MAX_LENGTH];
252 int i;
253 int err;
255 a3d = kzalloc(sizeof(struct a3d), GFP_KERNEL);
256 input_dev = input_allocate_device();
257 if (!a3d || !input_dev) {
258 err = -ENOMEM;
259 goto fail1;
262 a3d->dev = input_dev;
263 a3d->gameport = gameport;
265 gameport_set_drvdata(gameport, a3d);
267 err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
268 if (err)
269 goto fail1;
271 i = a3d_read_packet(gameport, A3D_MAX_LENGTH, data);
273 if (!i || a3d_csum(data, i)) {
274 err = -ENODEV;
275 goto fail2;
278 a3d->mode = data[0];
280 if (!a3d->mode || a3d->mode > 5) {
281 printk(KERN_WARNING "a3d.c: Unknown A3D device detected "
282 "(%s, id=%d), contact <vojtech@ucw.cz>\n", gameport->phys, a3d->mode);
283 err = -ENODEV;
284 goto fail2;
287 gameport_set_poll_handler(gameport, a3d_poll);
288 gameport_set_poll_interval(gameport, 20);
290 snprintf(a3d->phys, sizeof(a3d->phys), "%s/input0", gameport->phys);
292 input_dev->name = a3d_names[a3d->mode];
293 input_dev->phys = a3d->phys;
294 input_dev->id.bustype = BUS_GAMEPORT;
295 input_dev->id.vendor = GAMEPORT_ID_VENDOR_MADCATZ;
296 input_dev->id.product = a3d->mode;
297 input_dev->id.version = 0x0100;
298 input_dev->dev.parent = &gameport->dev;
299 input_dev->open = a3d_open;
300 input_dev->close = a3d_close;
302 input_set_drvdata(input_dev, a3d);
304 if (a3d->mode == A3D_MODE_PXL) {
306 int axes[] = { ABS_X, ABS_Y, ABS_THROTTLE, ABS_RUDDER };
308 a3d->length = 33;
310 input_dev->evbit[0] |= BIT_MASK(EV_ABS) | BIT_MASK(EV_KEY) |
311 BIT_MASK(EV_REL);
312 input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
313 input_dev->absbit[0] |= BIT_MASK(ABS_X) | BIT_MASK(ABS_Y) |
314 BIT_MASK(ABS_THROTTLE) | BIT_MASK(ABS_RUDDER) |
315 BIT_MASK(ABS_HAT0X) | BIT_MASK(ABS_HAT0Y) |
316 BIT_MASK(ABS_HAT1X) | BIT_MASK(ABS_HAT1Y);
317 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
318 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE) |
319 BIT_MASK(BTN_SIDE) | BIT_MASK(BTN_EXTRA);
320 input_dev->keybit[BIT_WORD(BTN_JOYSTICK)] |=
321 BIT_MASK(BTN_TRIGGER) | BIT_MASK(BTN_THUMB) |
322 BIT_MASK(BTN_TOP) | BIT_MASK(BTN_PINKIE);
324 a3d_read(a3d, data);
326 for (i = 0; i < 4; i++) {
327 if (i < 2)
328 input_set_abs_params(input_dev, axes[i],
329 48, input_abs_get_val(input_dev, axes[i]) * 2 - 48, 0, 8);
330 else
331 input_set_abs_params(input_dev, axes[i], 2, 253, 0, 0);
332 input_set_abs_params(input_dev, ABS_HAT0X + i, -1, 1, 0, 0);
335 } else {
336 a3d->length = 29;
338 input_dev->evbit[0] |= BIT_MASK(EV_KEY) | BIT_MASK(EV_REL);
339 input_dev->relbit[0] |= BIT_MASK(REL_X) | BIT_MASK(REL_Y);
340 input_dev->keybit[BIT_WORD(BTN_MOUSE)] |= BIT_MASK(BTN_RIGHT) |
341 BIT_MASK(BTN_LEFT) | BIT_MASK(BTN_MIDDLE);
343 a3d_read(a3d, data);
345 if (!(a3d->adc = adc = gameport_allocate_port()))
346 printk(KERN_ERR "a3d: Not enough memory for ADC port\n");
347 else {
348 adc->port_data = a3d;
349 adc->open = a3d_adc_open;
350 adc->close = a3d_adc_close;
351 adc->cooked_read = a3d_adc_cooked_read;
352 adc->fuzz = 1;
354 gameport_set_name(adc, a3d_names[a3d->mode]);
355 gameport_set_phys(adc, "%s/gameport0", gameport->phys);
356 adc->dev.parent = &gameport->dev;
358 gameport_register_port(adc);
362 err = input_register_device(a3d->dev);
363 if (err)
364 goto fail3;
366 return 0;
368 fail3: if (a3d->adc)
369 gameport_unregister_port(a3d->adc);
370 fail2: gameport_close(gameport);
371 fail1: gameport_set_drvdata(gameport, NULL);
372 input_free_device(input_dev);
373 kfree(a3d);
374 return err;
377 static void a3d_disconnect(struct gameport *gameport)
379 struct a3d *a3d = gameport_get_drvdata(gameport);
381 input_unregister_device(a3d->dev);
382 if (a3d->adc)
383 gameport_unregister_port(a3d->adc);
384 gameport_close(gameport);
385 gameport_set_drvdata(gameport, NULL);
386 kfree(a3d);
389 static struct gameport_driver a3d_drv = {
390 .driver = {
391 .name = "adc",
392 .owner = THIS_MODULE,
394 .description = DRIVER_DESC,
395 .connect = a3d_connect,
396 .disconnect = a3d_disconnect,
399 module_gameport_driver(a3d_drv);