1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1996-2001 Vojtech Pavlik
7 * Analog joystick and gamepad driver for Linux
13 #include <linux/delay.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/slab.h>
17 #include <linux/bitops.h>
18 #include <linux/init.h>
19 #include <linux/input.h>
20 #include <linux/gameport.h>
21 #include <linux/jiffies.h>
22 #include <linux/seq_buf.h>
23 #include <linux/timex.h>
24 #include <linux/timekeeping.h>
26 #define DRIVER_DESC "Analog joystick and gamepad driver"
29 MODULE_DESCRIPTION(DRIVER_DESC);
30 MODULE_LICENSE("GPL");
36 #define ANALOG_PORTS 16
38 static char *js[ANALOG_PORTS];
39 static unsigned int js_nargs;
40 static int analog_options[ANALOG_PORTS];
41 module_param_array_named(map, js, charp, &js_nargs, 0);
42 MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
45 * Times, feature definitions.
48 #define ANALOG_RUDDER 0x00004
49 #define ANALOG_THROTTLE 0x00008
50 #define ANALOG_AXES_STD 0x0000f
51 #define ANALOG_BTNS_STD 0x000f0
53 #define ANALOG_BTNS_CHF 0x00100
54 #define ANALOG_HAT1_CHF 0x00200
55 #define ANALOG_HAT2_CHF 0x00400
56 #define ANALOG_HAT_FCS 0x00800
57 #define ANALOG_HATS_ALL 0x00e00
58 #define ANALOG_BTN_TL 0x01000
59 #define ANALOG_BTN_TR 0x02000
60 #define ANALOG_BTN_TL2 0x04000
61 #define ANALOG_BTN_TR2 0x08000
62 #define ANALOG_BTNS_TLR 0x03000
63 #define ANALOG_BTNS_TLR2 0x0c000
64 #define ANALOG_BTNS_GAMEPAD 0x0f000
66 #define ANALOG_HBTN_CHF 0x10000
67 #define ANALOG_ANY_CHF 0x10700
68 #define ANALOG_SAITEK 0x20000
69 #define ANALOG_EXTENSIONS 0x7ff00
70 #define ANALOG_GAMEPAD 0x80000
72 #define ANALOG_MAX_TIME 3 /* 3 ms */
73 #define ANALOG_LOOP_TIME 2000 /* 2 * loop */
74 #define ANALOG_SAITEK_DELAY 200 /* 200 us */
75 #define ANALOG_SAITEK_TIME 2000 /* 2000 us */
76 #define ANALOG_AXIS_TIME 2 /* 2 * refresh */
77 #define ANALOG_INIT_RETRIES 8 /* 8 times */
78 #define ANALOG_FUZZ_BITS 2 /* 2 bit more */
79 #define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */
81 #define ANALOG_MAX_NAME_LENGTH 128
82 #define ANALOG_MAX_PHYS_LENGTH 32
84 static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
85 static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
86 static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
87 static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
88 static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE };
89 static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
90 BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
92 static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
95 struct input_dev *dev;
98 char name[ANALOG_MAX_NAME_LENGTH];
99 char phys[ANALOG_MAX_PHYS_LENGTH];
103 struct gameport *gameport;
104 struct analog analog[2];
119 * analog_decode() decodes analog joystick data and reports input events.
122 static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
124 struct input_dev *dev = analog->dev;
127 if (analog->mask & ANALOG_HAT_FCS)
128 for (i = 0; i < 4; i++)
129 if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
130 buttons |= 1 << (i + 14);
134 for (i = j = 0; i < 6; i++)
135 if (analog->mask & (0x10 << i))
136 input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
138 if (analog->mask & ANALOG_HBTN_CHF)
139 for (i = 0; i < 4; i++)
140 input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
142 if (analog->mask & ANALOG_BTN_TL)
143 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
144 if (analog->mask & ANALOG_BTN_TR)
145 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
146 if (analog->mask & ANALOG_BTN_TL2)
147 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
148 if (analog->mask & ANALOG_BTN_TR2)
149 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
151 for (i = j = 0; i < 4; i++)
152 if (analog->mask & (1 << i))
153 input_report_abs(dev, analog_axes[j++], axes[i]);
155 for (i = j = 0; i < 3; i++)
156 if (analog->mask & analog_exts[i]) {
157 input_report_abs(dev, analog_hats[j++],
158 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
159 input_report_abs(dev, analog_hats[j++],
160 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
167 * analog_cooked_read() reads analog joystick data.
170 static int analog_cooked_read(struct analog_port *port)
172 struct gameport *gameport = port->gameport;
173 ktime_t time[4], start, loop, now;
174 unsigned int loopout, timeout;
175 unsigned char data[4], this, last;
179 loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
180 timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
182 local_irq_save(flags);
183 gameport_trigger(gameport);
185 local_irq_restore(flags);
196 this = gameport_read(gameport) & port->mask;
198 local_irq_restore(flags);
200 if ((last ^ this) && (ktime_sub(now, loop) < loopout)) {
201 data[i] = last ^ this;
206 } while (this && (i < 4) && (ktime_sub(now, start) < timeout));
210 for (--i; i >= 0; i--) {
212 for (j = 0; j < 4; j++)
213 if (data[i] & (1 << j))
214 port->axes[j] = ((u32)ktime_sub(time[i], start) << ANALOG_FUZZ_BITS) / port->loop;
217 return -(this != port->mask);
220 static int analog_button_read(struct analog_port *port, char saitek, char chf)
224 int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
226 u = gameport_read(port->gameport);
229 port->buttons = (~u >> 4) & 0xf;
235 while ((~u & 0xf0) && (i < 16) && t) {
236 port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
237 if (!saitek) return 0;
238 udelay(ANALOG_SAITEK_DELAY);
240 gameport_trigger(port->gameport);
241 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
245 return -(!t || (i == 16));
249 * analog_poll() repeatedly polls the Analog joysticks.
252 static void analog_poll(struct gameport *gameport)
254 struct analog_port *port = gameport_get_drvdata(gameport);
257 char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
258 char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
261 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
263 port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
266 if (!port->axtime--) {
267 port->bads -= analog_cooked_read(port);
268 port->bads -= analog_button_read(port, saitek, chf);
270 port->axtime = ANALOG_AXIS_TIME - 1;
273 analog_button_read(port, saitek, chf);
277 for (i = 0; i < 2; i++)
278 if (port->analog[i].mask)
279 analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
283 * analog_open() is a callback from the input open routine.
286 static int analog_open(struct input_dev *dev)
288 struct analog_port *port = input_get_drvdata(dev);
290 gameport_start_polling(port->gameport);
295 * analog_close() is a callback from the input close routine.
298 static void analog_close(struct input_dev *dev)
300 struct analog_port *port = input_get_drvdata(dev);
302 gameport_stop_polling(port->gameport);
306 * analog_calibrate_timer() calibrates the timer and computes loop
307 * and timeout values for a joystick port.
310 static void analog_calibrate_timer(struct analog_port *port)
312 struct gameport *gameport = port->gameport;
313 unsigned int i, t, tx;
319 for (i = 0; i < 50; i++) {
320 local_irq_save(flags);
322 for (t = 0; t < 50; t++) {
323 gameport_read(gameport);
327 local_irq_restore(flags);
329 t = ktime_sub(t2, t1) - ktime_sub(t3, t2);
333 port->loop = tx / 50;
337 * analog_name() constructs a name for an analog joystick.
340 static void analog_name(struct analog *analog)
344 seq_buf_init(&s, analog->name, sizeof(analog->name));
345 seq_buf_printf(&s, "Analog %d-axis %d-button",
346 hweight8(analog->mask & ANALOG_AXES_STD),
347 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
348 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
350 if (analog->mask & ANALOG_HATS_ALL)
351 seq_buf_printf(&s, " %d-hat",
352 hweight16(analog->mask & ANALOG_HATS_ALL));
354 if (analog->mask & ANALOG_HAT_FCS)
355 seq_buf_printf(&s, " FCS");
356 if (analog->mask & ANALOG_ANY_CHF)
357 seq_buf_printf(&s, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
359 seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
363 * analog_init_device()
366 static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
368 struct input_dev *input_dev;
369 int i, j, t, v, w, x, y, z;
373 snprintf(analog->phys, sizeof(analog->phys),
374 "%s/input%d", port->gameport->phys, index);
375 analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
377 analog->dev = input_dev = input_allocate_device();
381 input_dev->name = analog->name;
382 input_dev->phys = analog->phys;
383 input_dev->id.bustype = BUS_GAMEPORT;
384 input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
385 input_dev->id.product = analog->mask >> 4;
386 input_dev->id.version = 0x0100;
387 input_dev->dev.parent = &port->gameport->dev;
389 input_set_drvdata(input_dev, port);
391 input_dev->open = analog_open;
392 input_dev->close = analog_close;
394 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
396 for (i = j = 0; i < 4; i++)
397 if (analog->mask & (1 << i)) {
401 y = (port->axes[0] + port->axes[1]) >> 1;
402 z = y - port->axes[i];
407 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
410 if (analog->mask & ANALOG_SAITEK) {
411 if (i == 2) x = port->axes[i];
416 input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
420 for (i = j = 0; i < 3; i++)
421 if (analog->mask & analog_exts[i])
422 for (x = 0; x < 2; x++) {
423 t = analog_hats[j++];
424 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
427 for (i = j = 0; i < 4; i++)
428 if (analog->mask & (0x10 << i))
429 set_bit(analog->buttons[j++], input_dev->keybit);
431 if (analog->mask & ANALOG_BTNS_CHF)
432 for (i = 0; i < 2; i++)
433 set_bit(analog->buttons[j++], input_dev->keybit);
435 if (analog->mask & ANALOG_HBTN_CHF)
436 for (i = 0; i < 4; i++)
437 set_bit(analog->buttons[j++], input_dev->keybit);
439 for (i = 0; i < 4; i++)
440 if (analog->mask & (ANALOG_BTN_TL << i))
441 set_bit(analog_pads[i], input_dev->keybit);
443 analog_decode(analog, port->axes, port->initial, port->buttons);
445 error = input_register_device(analog->dev);
447 input_free_device(analog->dev);
455 * analog_init_devices() sets up device-specific values and registers the input devices.
458 static int analog_init_masks(struct analog_port *port)
461 struct analog *analog = port->analog;
467 if ((port->mask & 3) != 3 && port->mask != 0xc) {
468 printk(KERN_WARNING "analog.c: Unknown joystick device found "
469 "(data=%#x, %s), probably not analog joystick.\n",
470 port->mask, port->gameport->phys);
475 i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
477 analog[0].mask = i & 0xfffff;
479 analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
480 | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
481 | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
483 analog[0].mask &= ~(ANALOG_HAT2_CHF)
484 | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
486 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
487 | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
488 | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
489 | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
491 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
492 | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
493 & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
495 analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
497 analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
498 : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
502 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
504 if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
505 if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
506 if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
507 if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
508 if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
510 gameport_calibrate(port->gameport, port->axes, max);
513 for (i = 0; i < 4; i++)
514 port->initial[i] = port->axes[i];
516 return -!(analog[0].mask || analog[1].mask);
519 static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
523 port->gameport = gameport;
525 gameport_set_drvdata(gameport, port);
527 if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
529 analog_calibrate_timer(port);
531 gameport_trigger(gameport);
532 t = gameport_read(gameport);
533 msleep(ANALOG_MAX_TIME);
534 port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
535 port->fuzz = (NSEC_PER_MSEC * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
537 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
538 if (!analog_cooked_read(port))
540 msleep(ANALOG_MAX_TIME);
545 msleep(ANALOG_MAX_TIME);
546 t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
547 gameport_trigger(gameport);
548 while ((gameport_read(port->gameport) & port->mask) && (u < t))
550 udelay(ANALOG_SAITEK_DELAY);
551 t = gameport_time(gameport, ANALOG_SAITEK_TIME);
552 gameport_trigger(gameport);
553 while ((gameport_read(port->gameport) & port->mask) && (v < t))
556 if (v < (u >> 1)) { /* FIXME - more than one port */
557 analog_options[0] |= /* FIXME - more than one port */
558 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
562 gameport_close(gameport);
565 if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
567 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
568 if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
570 for (i = 0; i < 4; i++)
571 if (port->axes[i] != -1)
572 port->mask |= 1 << i;
574 port->fuzz = gameport->fuzz;
579 return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
582 static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
584 struct analog_port *port;
588 if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
591 err = analog_init_port(gameport, drv, port);
595 err = analog_init_masks(port);
599 gameport_set_poll_handler(gameport, analog_poll);
600 gameport_set_poll_interval(gameport, 10);
602 for (i = 0; i < 2; i++)
603 if (port->analog[i].mask) {
604 err = analog_init_device(port, port->analog + i, i);
611 fail3: while (--i >= 0)
612 if (port->analog[i].mask)
613 input_unregister_device(port->analog[i].dev);
614 fail2: gameport_close(gameport);
615 fail1: gameport_set_drvdata(gameport, NULL);
620 static void analog_disconnect(struct gameport *gameport)
622 struct analog_port *port = gameport_get_drvdata(gameport);
625 for (i = 0; i < 2; i++)
626 if (port->analog[i].mask)
627 input_unregister_device(port->analog[i].dev);
628 gameport_close(gameport);
629 gameport_set_drvdata(gameport, NULL);
630 printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
631 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
632 port->gameport->phys);
636 struct analog_types {
641 static struct analog_types analog_types[] = {
642 { "none", 0x00000000 },
643 { "auto", 0x000000ff },
644 { "2btn", 0x0000003f },
645 { "y-joy", 0x0cc00033 },
646 { "y-pad", 0x8cc80033 },
647 { "fcs", 0x000008f7 },
648 { "chf", 0x000002ff },
649 { "fullchf", 0x000007ff },
650 { "gamepad", 0x000830f3 },
651 { "gamepad8", 0x0008f0f3 },
655 static void analog_parse_options(void)
660 for (i = 0; i < js_nargs; i++) {
662 for (j = 0; analog_types[j].name; j++)
663 if (!strcmp(analog_types[j].name, js[i])) {
664 analog_options[i] = analog_types[j].value;
667 if (analog_types[j].name) continue;
669 analog_options[i] = simple_strtoul(js[i], &end, 0);
670 if (end != js[i]) continue;
672 analog_options[i] = 0xff;
673 if (!strlen(js[i])) continue;
675 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
678 for (; i < ANALOG_PORTS; i++)
679 analog_options[i] = 0xff;
683 * The gameport device structure.
686 static struct gameport_driver analog_drv = {
690 .description = DRIVER_DESC,
691 .connect = analog_connect,
692 .disconnect = analog_disconnect,
695 static int __init analog_init(void)
697 analog_parse_options();
698 return gameport_register_driver(&analog_drv);
701 static void __exit analog_exit(void)
703 gameport_unregister_driver(&analog_drv);
706 module_init(analog_init);
707 module_exit(analog_exit);