1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (c) 1996-2001 Vojtech Pavlik
7 * Analog joystick and gamepad driver for Linux
10 #include <linux/delay.h>
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/bitops.h>
15 #include <linux/init.h>
16 #include <linux/input.h>
17 #include <linux/gameport.h>
18 #include <linux/jiffies.h>
19 #include <linux/seq_buf.h>
20 #include <linux/timex.h>
21 #include <linux/timekeeping.h>
23 #define DRIVER_DESC "Analog joystick and gamepad driver"
25 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26 MODULE_DESCRIPTION(DRIVER_DESC
);
27 MODULE_LICENSE("GPL");
33 #define ANALOG_PORTS 16
35 static char *js
[ANALOG_PORTS
];
36 static unsigned int js_nargs
;
37 static int analog_options
[ANALOG_PORTS
];
38 module_param_array_named(map
, js
, charp
, &js_nargs
, 0);
39 MODULE_PARM_DESC(map
, "Describes analog joysticks type/capabilities");
42 * Times, feature definitions.
45 #define ANALOG_RUDDER 0x00004
46 #define ANALOG_THROTTLE 0x00008
47 #define ANALOG_AXES_STD 0x0000f
48 #define ANALOG_BTNS_STD 0x000f0
50 #define ANALOG_BTNS_CHF 0x00100
51 #define ANALOG_HAT1_CHF 0x00200
52 #define ANALOG_HAT2_CHF 0x00400
53 #define ANALOG_HAT_FCS 0x00800
54 #define ANALOG_HATS_ALL 0x00e00
55 #define ANALOG_BTN_TL 0x01000
56 #define ANALOG_BTN_TR 0x02000
57 #define ANALOG_BTN_TL2 0x04000
58 #define ANALOG_BTN_TR2 0x08000
59 #define ANALOG_BTNS_TLR 0x03000
60 #define ANALOG_BTNS_TLR2 0x0c000
61 #define ANALOG_BTNS_GAMEPAD 0x0f000
63 #define ANALOG_HBTN_CHF 0x10000
64 #define ANALOG_ANY_CHF 0x10700
65 #define ANALOG_SAITEK 0x20000
66 #define ANALOG_EXTENSIONS 0x7ff00
67 #define ANALOG_GAMEPAD 0x80000
69 #define ANALOG_MAX_TIME 3 /* 3 ms */
70 #define ANALOG_LOOP_TIME 2000 /* 2 * loop */
71 #define ANALOG_SAITEK_DELAY 200 /* 200 us */
72 #define ANALOG_SAITEK_TIME 2000 /* 2000 us */
73 #define ANALOG_AXIS_TIME 2 /* 2 * refresh */
74 #define ANALOG_INIT_RETRIES 8 /* 8 times */
75 #define ANALOG_FUZZ_BITS 2 /* 2 bit more */
76 #define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */
78 #define ANALOG_MAX_NAME_LENGTH 128
79 #define ANALOG_MAX_PHYS_LENGTH 32
81 static short analog_axes
[] = { ABS_X
, ABS_Y
, ABS_RUDDER
, ABS_THROTTLE
};
82 static short analog_hats
[] = { ABS_HAT0X
, ABS_HAT0Y
, ABS_HAT1X
, ABS_HAT1Y
, ABS_HAT2X
, ABS_HAT2Y
};
83 static short analog_pads
[] = { BTN_Y
, BTN_Z
, BTN_TL
, BTN_TR
};
84 static short analog_exts
[] = { ANALOG_HAT1_CHF
, ANALOG_HAT2_CHF
, ANALOG_HAT_FCS
};
85 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
};
86 static short analog_joy_btn
[] = { BTN_TRIGGER
, BTN_THUMB
, BTN_TOP
, BTN_TOP2
, BTN_BASE
, BTN_BASE2
,
87 BTN_BASE3
, BTN_BASE4
, BTN_BASE5
, BTN_BASE6
};
89 static unsigned char analog_chf
[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
92 struct input_dev
*dev
;
95 char name
[ANALOG_MAX_NAME_LENGTH
];
96 char phys
[ANALOG_MAX_PHYS_LENGTH
];
100 struct gameport
*gameport
;
101 struct analog analog
[2];
116 * analog_decode() decodes analog joystick data and reports input events.
119 static void analog_decode(struct analog
*analog
, int *axes
, int *initial
, int buttons
)
121 struct input_dev
*dev
= analog
->dev
;
124 if (analog
->mask
& ANALOG_HAT_FCS
)
125 for (i
= 0; i
< 4; i
++)
126 if (axes
[3] < ((initial
[3] * ((i
<< 1) + 1)) >> 3)) {
127 buttons
|= 1 << (i
+ 14);
131 for (i
= j
= 0; i
< 6; i
++)
132 if (analog
->mask
& (0x10 << i
))
133 input_report_key(dev
, analog
->buttons
[j
++], (buttons
>> i
) & 1);
135 if (analog
->mask
& ANALOG_HBTN_CHF
)
136 for (i
= 0; i
< 4; i
++)
137 input_report_key(dev
, analog
->buttons
[j
++], (buttons
>> (i
+ 10)) & 1);
139 if (analog
->mask
& ANALOG_BTN_TL
)
140 input_report_key(dev
, analog_pads
[0], axes
[2] < (initial
[2] >> 1));
141 if (analog
->mask
& ANALOG_BTN_TR
)
142 input_report_key(dev
, analog_pads
[1], axes
[3] < (initial
[3] >> 1));
143 if (analog
->mask
& ANALOG_BTN_TL2
)
144 input_report_key(dev
, analog_pads
[2], axes
[2] > (initial
[2] + (initial
[2] >> 1)));
145 if (analog
->mask
& ANALOG_BTN_TR2
)
146 input_report_key(dev
, analog_pads
[3], axes
[3] > (initial
[3] + (initial
[3] >> 1)));
148 for (i
= j
= 0; i
< 4; i
++)
149 if (analog
->mask
& (1 << i
))
150 input_report_abs(dev
, analog_axes
[j
++], axes
[i
]);
152 for (i
= j
= 0; i
< 3; i
++)
153 if (analog
->mask
& analog_exts
[i
]) {
154 input_report_abs(dev
, analog_hats
[j
++],
155 ((buttons
>> ((i
<< 2) + 7)) & 1) - ((buttons
>> ((i
<< 2) + 9)) & 1));
156 input_report_abs(dev
, analog_hats
[j
++],
157 ((buttons
>> ((i
<< 2) + 8)) & 1) - ((buttons
>> ((i
<< 2) + 6)) & 1));
164 * analog_cooked_read() reads analog joystick data.
167 static int analog_cooked_read(struct analog_port
*port
)
169 struct gameport
*gameport
= port
->gameport
;
170 ktime_t time
[4], start
, loop
, now
;
171 unsigned int loopout
, timeout
;
172 unsigned char data
[4], this, last
;
176 loopout
= (ANALOG_LOOP_TIME
* port
->loop
) / 1000;
177 timeout
= ANALOG_MAX_TIME
* NSEC_PER_MSEC
;
179 local_irq_save(flags
);
180 gameport_trigger(gameport
);
182 local_irq_restore(flags
);
193 this = gameport_read(gameport
) & port
->mask
;
195 local_irq_restore(flags
);
197 if ((last
^ this) && (ktime_sub(now
, loop
) < loopout
)) {
198 data
[i
] = last
^ this;
203 } while (this && (i
< 4) && (ktime_sub(now
, start
) < timeout
));
207 for (--i
; i
>= 0; i
--) {
209 for (j
= 0; j
< 4; j
++)
210 if (data
[i
] & (1 << j
))
211 port
->axes
[j
] = ((u32
)ktime_sub(time
[i
], start
) << ANALOG_FUZZ_BITS
) / port
->loop
;
214 return -(this != port
->mask
);
217 static int analog_button_read(struct analog_port
*port
, char saitek
, char chf
)
221 int strobe
= gameport_time(port
->gameport
, ANALOG_SAITEK_TIME
);
223 u
= gameport_read(port
->gameport
);
226 port
->buttons
= (~u
>> 4) & 0xf;
232 while ((~u
& 0xf0) && (i
< 16) && t
) {
233 port
->buttons
|= 1 << analog_chf
[(~u
>> 4) & 0xf];
234 if (!saitek
) return 0;
235 udelay(ANALOG_SAITEK_DELAY
);
237 gameport_trigger(port
->gameport
);
238 while (((u
= gameport_read(port
->gameport
)) & port
->mask
) && t
) t
--;
242 return -(!t
|| (i
== 16));
246 * analog_poll() repeatedly polls the Analog joysticks.
249 static void analog_poll(struct gameport
*gameport
)
251 struct analog_port
*port
= gameport_get_drvdata(gameport
);
254 char saitek
= !!(port
->analog
[0].mask
& ANALOG_SAITEK
);
255 char chf
= !!(port
->analog
[0].mask
& ANALOG_ANY_CHF
);
258 port
->bads
-= gameport_cooked_read(port
->gameport
, port
->axes
, &port
->buttons
);
260 port
->buttons
= port
->buttons
? (1 << analog_chf
[port
->buttons
]) : 0;
263 if (!port
->axtime
--) {
264 port
->bads
-= analog_cooked_read(port
);
265 port
->bads
-= analog_button_read(port
, saitek
, chf
);
267 port
->axtime
= ANALOG_AXIS_TIME
- 1;
270 analog_button_read(port
, saitek
, chf
);
274 for (i
= 0; i
< 2; i
++)
275 if (port
->analog
[i
].mask
)
276 analog_decode(port
->analog
+ i
, port
->axes
, port
->initial
, port
->buttons
);
280 * analog_open() is a callback from the input open routine.
283 static int analog_open(struct input_dev
*dev
)
285 struct analog_port
*port
= input_get_drvdata(dev
);
287 gameport_start_polling(port
->gameport
);
292 * analog_close() is a callback from the input close routine.
295 static void analog_close(struct input_dev
*dev
)
297 struct analog_port
*port
= input_get_drvdata(dev
);
299 gameport_stop_polling(port
->gameport
);
303 * analog_calibrate_timer() calibrates the timer and computes loop
304 * and timeout values for a joystick port.
307 static void analog_calibrate_timer(struct analog_port
*port
)
309 struct gameport
*gameport
= port
->gameport
;
310 unsigned int i
, t
, tx
;
316 for (i
= 0; i
< 50; i
++) {
317 local_irq_save(flags
);
319 for (t
= 0; t
< 50; t
++) {
320 gameport_read(gameport
);
324 local_irq_restore(flags
);
326 t
= ktime_sub(t2
, t1
) - ktime_sub(t3
, t2
);
330 port
->loop
= tx
/ 50;
334 * analog_name() constructs a name for an analog joystick.
337 static void analog_name(struct analog
*analog
)
341 seq_buf_init(&s
, analog
->name
, sizeof(analog
->name
));
342 seq_buf_printf(&s
, "Analog %d-axis %d-button",
343 hweight8(analog
->mask
& ANALOG_AXES_STD
),
344 hweight8(analog
->mask
& ANALOG_BTNS_STD
) + !!(analog
->mask
& ANALOG_BTNS_CHF
) * 2 +
345 hweight16(analog
->mask
& ANALOG_BTNS_GAMEPAD
) + !!(analog
->mask
& ANALOG_HBTN_CHF
) * 4);
347 if (analog
->mask
& ANALOG_HATS_ALL
)
348 seq_buf_printf(&s
, " %d-hat",
349 hweight16(analog
->mask
& ANALOG_HATS_ALL
));
351 if (analog
->mask
& ANALOG_HAT_FCS
)
352 seq_buf_printf(&s
, " FCS");
353 if (analog
->mask
& ANALOG_ANY_CHF
)
354 seq_buf_printf(&s
, (analog
->mask
& ANALOG_SAITEK
) ? " Saitek" : " CHF");
356 seq_buf_printf(&s
, (analog
->mask
& ANALOG_GAMEPAD
) ? " gamepad" : " joystick");
360 * analog_init_device()
363 static int analog_init_device(struct analog_port
*port
, struct analog
*analog
, int index
)
365 struct input_dev
*input_dev
;
366 int i
, j
, t
, v
, w
, x
, y
, z
;
370 snprintf(analog
->phys
, sizeof(analog
->phys
),
371 "%s/input%d", port
->gameport
->phys
, index
);
372 analog
->buttons
= (analog
->mask
& ANALOG_GAMEPAD
) ? analog_pad_btn
: analog_joy_btn
;
374 analog
->dev
= input_dev
= input_allocate_device();
378 input_dev
->name
= analog
->name
;
379 input_dev
->phys
= analog
->phys
;
380 input_dev
->id
.bustype
= BUS_GAMEPORT
;
381 input_dev
->id
.vendor
= GAMEPORT_ID_VENDOR_ANALOG
;
382 input_dev
->id
.product
= analog
->mask
>> 4;
383 input_dev
->id
.version
= 0x0100;
384 input_dev
->dev
.parent
= &port
->gameport
->dev
;
386 input_set_drvdata(input_dev
, port
);
388 input_dev
->open
= analog_open
;
389 input_dev
->close
= analog_close
;
391 input_dev
->evbit
[0] = BIT_MASK(EV_KEY
) | BIT_MASK(EV_ABS
);
393 for (i
= j
= 0; i
< 4; i
++)
394 if (analog
->mask
& (1 << i
)) {
398 y
= (port
->axes
[0] + port
->axes
[1]) >> 1;
399 z
= y
- port
->axes
[i
];
404 if ((i
== 2 || i
== 3) && (j
== 2 || j
== 3) && (z
> (y
>> 3)))
407 if (analog
->mask
& ANALOG_SAITEK
) {
408 if (i
== 2) x
= port
->axes
[i
];
413 input_set_abs_params(input_dev
, t
, v
, (x
<< 1) - v
, port
->fuzz
, w
);
417 for (i
= j
= 0; i
< 3; i
++)
418 if (analog
->mask
& analog_exts
[i
])
419 for (x
= 0; x
< 2; x
++) {
420 t
= analog_hats
[j
++];
421 input_set_abs_params(input_dev
, t
, -1, 1, 0, 0);
424 for (i
= j
= 0; i
< 4; i
++)
425 if (analog
->mask
& (0x10 << i
))
426 set_bit(analog
->buttons
[j
++], input_dev
->keybit
);
428 if (analog
->mask
& ANALOG_BTNS_CHF
)
429 for (i
= 0; i
< 2; i
++)
430 set_bit(analog
->buttons
[j
++], input_dev
->keybit
);
432 if (analog
->mask
& ANALOG_HBTN_CHF
)
433 for (i
= 0; i
< 4; i
++)
434 set_bit(analog
->buttons
[j
++], input_dev
->keybit
);
436 for (i
= 0; i
< 4; i
++)
437 if (analog
->mask
& (ANALOG_BTN_TL
<< i
))
438 set_bit(analog_pads
[i
], input_dev
->keybit
);
440 analog_decode(analog
, port
->axes
, port
->initial
, port
->buttons
);
442 error
= input_register_device(analog
->dev
);
444 input_free_device(analog
->dev
);
452 * analog_init_devices() sets up device-specific values and registers the input devices.
455 static int analog_init_masks(struct analog_port
*port
)
458 struct analog
*analog
= port
->analog
;
464 if ((port
->mask
& 3) != 3 && port
->mask
!= 0xc) {
465 printk(KERN_WARNING
"analog.c: Unknown joystick device found "
466 "(data=%#x, %s), probably not analog joystick.\n",
467 port
->mask
, port
->gameport
->phys
);
472 i
= analog_options
[0]; /* FIXME !!! - need to specify options for different ports */
474 analog
[0].mask
= i
& 0xfffff;
476 analog
[0].mask
&= ~(ANALOG_AXES_STD
| ANALOG_HAT_FCS
| ANALOG_BTNS_GAMEPAD
)
477 | port
->mask
| ((port
->mask
<< 8) & ANALOG_HAT_FCS
)
478 | ((port
->mask
<< 10) & ANALOG_BTNS_TLR
) | ((port
->mask
<< 12) & ANALOG_BTNS_TLR2
);
480 analog
[0].mask
&= ~(ANALOG_HAT2_CHF
)
481 | ((analog
[0].mask
& ANALOG_HBTN_CHF
) ? 0 : ANALOG_HAT2_CHF
);
483 analog
[0].mask
&= ~(ANALOG_THROTTLE
| ANALOG_BTN_TR
| ANALOG_BTN_TR2
)
484 | ((~analog
[0].mask
& ANALOG_HAT_FCS
) >> 8)
485 | ((~analog
[0].mask
& ANALOG_HAT_FCS
) << 2)
486 | ((~analog
[0].mask
& ANALOG_HAT_FCS
) << 4);
488 analog
[0].mask
&= ~(ANALOG_THROTTLE
| ANALOG_RUDDER
)
489 | (((~analog
[0].mask
& ANALOG_BTNS_TLR
) >> 10)
490 & ((~analog
[0].mask
& ANALOG_BTNS_TLR2
) >> 12));
492 analog
[1].mask
= ((i
>> 20) & 0xff) | ((i
>> 12) & 0xf0000);
494 analog
[1].mask
&= (analog
[0].mask
& ANALOG_EXTENSIONS
) ? ANALOG_GAMEPAD
495 : (((ANALOG_BTNS_STD
| port
->mask
) & ~analog
[0].mask
) | ANALOG_GAMEPAD
);
499 for (i
= 0; i
< 4; i
++) max
[i
] = port
->axes
[i
] << 1;
501 if ((analog
[0].mask
& 0x7) == 0x7) max
[2] = (max
[0] + max
[1]) >> 1;
502 if ((analog
[0].mask
& 0xb) == 0xb) max
[3] = (max
[0] + max
[1]) >> 1;
503 if ((analog
[0].mask
& ANALOG_BTN_TL
) && !(analog
[0].mask
& ANALOG_BTN_TL2
)) max
[2] >>= 1;
504 if ((analog
[0].mask
& ANALOG_BTN_TR
) && !(analog
[0].mask
& ANALOG_BTN_TR2
)) max
[3] >>= 1;
505 if ((analog
[0].mask
& ANALOG_HAT_FCS
)) max
[3] >>= 1;
507 gameport_calibrate(port
->gameport
, port
->axes
, max
);
510 for (i
= 0; i
< 4; i
++)
511 port
->initial
[i
] = port
->axes
[i
];
513 return -!(analog
[0].mask
|| analog
[1].mask
);
516 static int analog_init_port(struct gameport
*gameport
, struct gameport_driver
*drv
, struct analog_port
*port
)
520 port
->gameport
= gameport
;
522 gameport_set_drvdata(gameport
, port
);
524 if (!gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
)) {
526 analog_calibrate_timer(port
);
528 gameport_trigger(gameport
);
529 t
= gameport_read(gameport
);
530 msleep(ANALOG_MAX_TIME
);
531 port
->mask
= (gameport_read(gameport
) ^ t
) & t
& 0xf;
532 port
->fuzz
= (NSEC_PER_MSEC
* ANALOG_FUZZ_MAGIC
) / port
->loop
/ 1000 + ANALOG_FUZZ_BITS
;
534 for (i
= 0; i
< ANALOG_INIT_RETRIES
; i
++) {
535 if (!analog_cooked_read(port
))
537 msleep(ANALOG_MAX_TIME
);
542 msleep(ANALOG_MAX_TIME
);
543 t
= gameport_time(gameport
, ANALOG_MAX_TIME
* 1000);
544 gameport_trigger(gameport
);
545 while ((gameport_read(port
->gameport
) & port
->mask
) && (u
< t
))
547 udelay(ANALOG_SAITEK_DELAY
);
548 t
= gameport_time(gameport
, ANALOG_SAITEK_TIME
);
549 gameport_trigger(gameport
);
550 while ((gameport_read(port
->gameport
) & port
->mask
) && (v
< t
))
553 if (v
< (u
>> 1)) { /* FIXME - more than one port */
554 analog_options
[0] |= /* FIXME - more than one port */
555 ANALOG_SAITEK
| ANALOG_BTNS_CHF
| ANALOG_HBTN_CHF
| ANALOG_HAT1_CHF
;
559 gameport_close(gameport
);
562 if (!gameport_open(gameport
, drv
, GAMEPORT_MODE_COOKED
)) {
564 for (i
= 0; i
< ANALOG_INIT_RETRIES
; i
++)
565 if (!gameport_cooked_read(gameport
, port
->axes
, &port
->buttons
))
567 for (i
= 0; i
< 4; i
++)
568 if (port
->axes
[i
] != -1)
569 port
->mask
|= 1 << i
;
571 port
->fuzz
= gameport
->fuzz
;
576 return gameport_open(gameport
, drv
, GAMEPORT_MODE_RAW
);
579 static int analog_connect(struct gameport
*gameport
, struct gameport_driver
*drv
)
581 struct analog_port
*port
;
585 port
= kzalloc(sizeof(*port
), GFP_KERNEL
);
589 err
= analog_init_port(gameport
, drv
, port
);
593 err
= analog_init_masks(port
);
597 gameport_set_poll_handler(gameport
, analog_poll
);
598 gameport_set_poll_interval(gameport
, 10);
600 for (i
= 0; i
< 2; i
++)
601 if (port
->analog
[i
].mask
) {
602 err
= analog_init_device(port
, port
->analog
+ i
, i
);
609 fail3
: while (--i
>= 0)
610 if (port
->analog
[i
].mask
)
611 input_unregister_device(port
->analog
[i
].dev
);
612 fail2
: gameport_close(gameport
);
613 fail1
: gameport_set_drvdata(gameport
, NULL
);
618 static void analog_disconnect(struct gameport
*gameport
)
620 struct analog_port
*port
= gameport_get_drvdata(gameport
);
623 for (i
= 0; i
< 2; i
++)
624 if (port
->analog
[i
].mask
)
625 input_unregister_device(port
->analog
[i
].dev
);
626 gameport_close(gameport
);
627 gameport_set_drvdata(gameport
, NULL
);
628 printk(KERN_INFO
"analog.c: %d out of %d reads (%d%%) on %s failed\n",
629 port
->bads
, port
->reads
, port
->reads
? (port
->bads
* 100 / port
->reads
) : 0,
630 port
->gameport
->phys
);
634 struct analog_types
{
639 static struct analog_types analog_types
[] = {
640 { "none", 0x00000000 },
641 { "auto", 0x000000ff },
642 { "2btn", 0x0000003f },
643 { "y-joy", 0x0cc00033 },
644 { "y-pad", 0x8cc80033 },
645 { "fcs", 0x000008f7 },
646 { "chf", 0x000002ff },
647 { "fullchf", 0x000007ff },
648 { "gamepad", 0x000830f3 },
649 { "gamepad8", 0x0008f0f3 },
653 static void analog_parse_options(void)
658 for (i
= 0; i
< js_nargs
; i
++) {
660 for (j
= 0; analog_types
[j
].name
; j
++)
661 if (!strcmp(analog_types
[j
].name
, js
[i
])) {
662 analog_options
[i
] = analog_types
[j
].value
;
665 if (analog_types
[j
].name
) continue;
667 analog_options
[i
] = simple_strtoul(js
[i
], &end
, 0);
668 if (end
!= js
[i
]) continue;
670 analog_options
[i
] = 0xff;
671 if (!strlen(js
[i
])) continue;
673 printk(KERN_WARNING
"analog.c: Bad config for port %d - \"%s\"\n", i
, js
[i
]);
676 for (; i
< ANALOG_PORTS
; i
++)
677 analog_options
[i
] = 0xff;
681 * The gameport device structure.
684 static struct gameport_driver analog_drv
= {
688 .description
= DRIVER_DESC
,
689 .connect
= analog_connect
,
690 .disconnect
= analog_disconnect
,
693 static int __init
analog_init(void)
695 analog_parse_options();
696 return gameport_register_driver(&analog_drv
);
699 static void __exit
analog_exit(void)
701 gameport_unregister_driver(&analog_drv
);
704 module_init(analog_init
);
705 module_exit(analog_exit
);