Loading...
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 1996-2001 Vojtech Pavlik
4 */
5
6/*
7 * Analog joystick and gamepad driver for Linux
8 */
9
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>
22
23#define DRIVER_DESC "Analog joystick and gamepad driver"
24
25MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
26MODULE_DESCRIPTION(DRIVER_DESC);
27MODULE_LICENSE("GPL");
28
29/*
30 * Option parsing.
31 */
32
33#define ANALOG_PORTS 16
34
35static char *js[ANALOG_PORTS];
36static unsigned int js_nargs;
37static int analog_options[ANALOG_PORTS];
38module_param_array_named(map, js, charp, &js_nargs, 0);
39MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
40
41/*
42 * Times, feature definitions.
43 */
44
45#define ANALOG_RUDDER 0x00004
46#define ANALOG_THROTTLE 0x00008
47#define ANALOG_AXES_STD 0x0000f
48#define ANALOG_BTNS_STD 0x000f0
49
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
62
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
68
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 */
77
78#define ANALOG_MAX_NAME_LENGTH 128
79#define ANALOG_MAX_PHYS_LENGTH 32
80
81static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
82static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
83static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
84static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
85static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE };
86static 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 };
88
89static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
90
91struct analog {
92 struct input_dev *dev;
93 int mask;
94 short *buttons;
95 char name[ANALOG_MAX_NAME_LENGTH];
96 char phys[ANALOG_MAX_PHYS_LENGTH];
97};
98
99struct analog_port {
100 struct gameport *gameport;
101 struct analog analog[2];
102 unsigned char mask;
103 char saitek;
104 char cooked;
105 int bads;
106 int reads;
107 int loop;
108 int fuzz;
109 int axes[4];
110 int buttons;
111 int initial[4];
112 int axtime;
113};
114
115/*
116 * analog_decode() decodes analog joystick data and reports input events.
117 */
118
119static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
120{
121 struct input_dev *dev = analog->dev;
122 int i, j;
123
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);
128 break;
129 }
130
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);
134
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);
138
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)));
147
148 for (i = j = 0; i < 4; i++)
149 if (analog->mask & (1 << i))
150 input_report_abs(dev, analog_axes[j++], axes[i]);
151
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));
158 }
159
160 input_sync(dev);
161}
162
163/*
164 * analog_cooked_read() reads analog joystick data.
165 */
166
167static int analog_cooked_read(struct analog_port *port)
168{
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;
173 unsigned long flags;
174 int i, j;
175
176 loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
177 timeout = ANALOG_MAX_TIME * NSEC_PER_MSEC;
178
179 local_irq_save(flags);
180 gameport_trigger(gameport);
181 now = ktime_get();
182 local_irq_restore(flags);
183
184 start = now;
185 this = port->mask;
186 i = 0;
187
188 do {
189 loop = now;
190 last = this;
191
192 local_irq_disable();
193 this = gameport_read(gameport) & port->mask;
194 now = ktime_get();
195 local_irq_restore(flags);
196
197 if ((last ^ this) && (ktime_sub(now, loop) < loopout)) {
198 data[i] = last ^ this;
199 time[i] = now;
200 i++;
201 }
202
203 } while (this && (i < 4) && (ktime_sub(now, start) < timeout));
204
205 this <<= 4;
206
207 for (--i; i >= 0; i--) {
208 this |= data[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;
212 }
213
214 return -(this != port->mask);
215}
216
217static int analog_button_read(struct analog_port *port, char saitek, char chf)
218{
219 unsigned char u;
220 int t = 1, i = 0;
221 int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
222
223 u = gameport_read(port->gameport);
224
225 if (!chf) {
226 port->buttons = (~u >> 4) & 0xf;
227 return 0;
228 }
229
230 port->buttons = 0;
231
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);
236 t = strobe;
237 gameport_trigger(port->gameport);
238 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
239 i++;
240 }
241
242 return -(!t || (i == 16));
243}
244
245/*
246 * analog_poll() repeatedly polls the Analog joysticks.
247 */
248
249static void analog_poll(struct gameport *gameport)
250{
251 struct analog_port *port = gameport_get_drvdata(gameport);
252 int i;
253
254 char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
255 char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
256
257 if (port->cooked) {
258 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
259 if (chf)
260 port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
261 port->reads++;
262 } else {
263 if (!port->axtime--) {
264 port->bads -= analog_cooked_read(port);
265 port->bads -= analog_button_read(port, saitek, chf);
266 port->reads++;
267 port->axtime = ANALOG_AXIS_TIME - 1;
268 } else {
269 if (!saitek)
270 analog_button_read(port, saitek, chf);
271 }
272 }
273
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);
277}
278
279/*
280 * analog_open() is a callback from the input open routine.
281 */
282
283static int analog_open(struct input_dev *dev)
284{
285 struct analog_port *port = input_get_drvdata(dev);
286
287 gameport_start_polling(port->gameport);
288 return 0;
289}
290
291/*
292 * analog_close() is a callback from the input close routine.
293 */
294
295static void analog_close(struct input_dev *dev)
296{
297 struct analog_port *port = input_get_drvdata(dev);
298
299 gameport_stop_polling(port->gameport);
300}
301
302/*
303 * analog_calibrate_timer() calibrates the timer and computes loop
304 * and timeout values for a joystick port.
305 */
306
307static void analog_calibrate_timer(struct analog_port *port)
308{
309 struct gameport *gameport = port->gameport;
310 unsigned int i, t, tx;
311 ktime_t t1, t2, t3;
312 unsigned long flags;
313
314 tx = ~0;
315
316 for (i = 0; i < 50; i++) {
317 local_irq_save(flags);
318 t1 = ktime_get();
319 for (t = 0; t < 50; t++) {
320 gameport_read(gameport);
321 t2 = ktime_get();
322 }
323 t3 = ktime_get();
324 local_irq_restore(flags);
325 udelay(i);
326 t = ktime_sub(t2, t1) - ktime_sub(t3, t2);
327 if (t < tx) tx = t;
328 }
329
330 port->loop = tx / 50;
331}
332
333/*
334 * analog_name() constructs a name for an analog joystick.
335 */
336
337static void analog_name(struct analog *analog)
338{
339 struct seq_buf s;
340
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);
346
347 if (analog->mask & ANALOG_HATS_ALL)
348 seq_buf_printf(&s, " %d-hat",
349 hweight16(analog->mask & ANALOG_HATS_ALL));
350
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");
355
356 seq_buf_printf(&s, (analog->mask & ANALOG_GAMEPAD) ? " gamepad" : " joystick");
357}
358
359/*
360 * analog_init_device()
361 */
362
363static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
364{
365 struct input_dev *input_dev;
366 int i, j, t, v, w, x, y, z;
367 int error;
368
369 analog_name(analog);
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;
373
374 analog->dev = input_dev = input_allocate_device();
375 if (!input_dev)
376 return -ENOMEM;
377
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;
385
386 input_set_drvdata(input_dev, port);
387
388 input_dev->open = analog_open;
389 input_dev->close = analog_close;
390
391 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
392
393 for (i = j = 0; i < 4; i++)
394 if (analog->mask & (1 << i)) {
395
396 t = analog_axes[j];
397 x = port->axes[i];
398 y = (port->axes[0] + port->axes[1]) >> 1;
399 z = y - port->axes[i];
400 z = z > 0 ? z : -z;
401 v = (x >> 3);
402 w = (x >> 3);
403
404 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
405 x = y;
406
407 if (analog->mask & ANALOG_SAITEK) {
408 if (i == 2) x = port->axes[i];
409 v = x - (x >> 2);
410 w = (x >> 4);
411 }
412
413 input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
414 j++;
415 }
416
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);
422 }
423
424 for (i = j = 0; i < 4; i++)
425 if (analog->mask & (0x10 << i))
426 set_bit(analog->buttons[j++], input_dev->keybit);
427
428 if (analog->mask & ANALOG_BTNS_CHF)
429 for (i = 0; i < 2; i++)
430 set_bit(analog->buttons[j++], input_dev->keybit);
431
432 if (analog->mask & ANALOG_HBTN_CHF)
433 for (i = 0; i < 4; i++)
434 set_bit(analog->buttons[j++], input_dev->keybit);
435
436 for (i = 0; i < 4; i++)
437 if (analog->mask & (ANALOG_BTN_TL << i))
438 set_bit(analog_pads[i], input_dev->keybit);
439
440 analog_decode(analog, port->axes, port->initial, port->buttons);
441
442 error = input_register_device(analog->dev);
443 if (error) {
444 input_free_device(analog->dev);
445 return error;
446 }
447
448 return 0;
449}
450
451/*
452 * analog_init_devices() sets up device-specific values and registers the input devices.
453 */
454
455static int analog_init_masks(struct analog_port *port)
456{
457 int i;
458 struct analog *analog = port->analog;
459 int max[4];
460
461 if (!port->mask)
462 return -1;
463
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);
468 return -1;
469 }
470
471
472 i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
473
474 analog[0].mask = i & 0xfffff;
475
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);
479
480 analog[0].mask &= ~(ANALOG_HAT2_CHF)
481 | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
482
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);
487
488 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
489 | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
490 & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
491
492 analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
493
494 analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
495 : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
496
497 if (port->cooked) {
498
499 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
500
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;
506
507 gameport_calibrate(port->gameport, port->axes, max);
508 }
509
510 for (i = 0; i < 4; i++)
511 port->initial[i] = port->axes[i];
512
513 return -!(analog[0].mask || analog[1].mask);
514}
515
516static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
517{
518 int i, t, u, v;
519
520 port->gameport = gameport;
521
522 gameport_set_drvdata(gameport, port);
523
524 if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
525
526 analog_calibrate_timer(port);
527
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;
533
534 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
535 if (!analog_cooked_read(port))
536 break;
537 msleep(ANALOG_MAX_TIME);
538 }
539
540 u = v = 0;
541
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))
546 u++;
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))
551 v++;
552
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;
556 return 0;
557 }
558
559 gameport_close(gameport);
560 }
561
562 if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
563
564 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
565 if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
566 break;
567 for (i = 0; i < 4; i++)
568 if (port->axes[i] != -1)
569 port->mask |= 1 << i;
570
571 port->fuzz = gameport->fuzz;
572 port->cooked = 1;
573 return 0;
574 }
575
576 return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
577}
578
579static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
580{
581 struct analog_port *port;
582 int i;
583 int err;
584
585 if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
586 return -ENOMEM;
587
588 err = analog_init_port(gameport, drv, port);
589 if (err)
590 goto fail1;
591
592 err = analog_init_masks(port);
593 if (err)
594 goto fail2;
595
596 gameport_set_poll_handler(gameport, analog_poll);
597 gameport_set_poll_interval(gameport, 10);
598
599 for (i = 0; i < 2; i++)
600 if (port->analog[i].mask) {
601 err = analog_init_device(port, port->analog + i, i);
602 if (err)
603 goto fail3;
604 }
605
606 return 0;
607
608 fail3: while (--i >= 0)
609 if (port->analog[i].mask)
610 input_unregister_device(port->analog[i].dev);
611 fail2: gameport_close(gameport);
612 fail1: gameport_set_drvdata(gameport, NULL);
613 kfree(port);
614 return err;
615}
616
617static void analog_disconnect(struct gameport *gameport)
618{
619 struct analog_port *port = gameport_get_drvdata(gameport);
620 int i;
621
622 for (i = 0; i < 2; i++)
623 if (port->analog[i].mask)
624 input_unregister_device(port->analog[i].dev);
625 gameport_close(gameport);
626 gameport_set_drvdata(gameport, NULL);
627 printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
628 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
629 port->gameport->phys);
630 kfree(port);
631}
632
633struct analog_types {
634 char *name;
635 int value;
636};
637
638static struct analog_types analog_types[] = {
639 { "none", 0x00000000 },
640 { "auto", 0x000000ff },
641 { "2btn", 0x0000003f },
642 { "y-joy", 0x0cc00033 },
643 { "y-pad", 0x8cc80033 },
644 { "fcs", 0x000008f7 },
645 { "chf", 0x000002ff },
646 { "fullchf", 0x000007ff },
647 { "gamepad", 0x000830f3 },
648 { "gamepad8", 0x0008f0f3 },
649 { NULL, 0 }
650};
651
652static void analog_parse_options(void)
653{
654 int i, j;
655 char *end;
656
657 for (i = 0; i < js_nargs; i++) {
658
659 for (j = 0; analog_types[j].name; j++)
660 if (!strcmp(analog_types[j].name, js[i])) {
661 analog_options[i] = analog_types[j].value;
662 break;
663 }
664 if (analog_types[j].name) continue;
665
666 analog_options[i] = simple_strtoul(js[i], &end, 0);
667 if (end != js[i]) continue;
668
669 analog_options[i] = 0xff;
670 if (!strlen(js[i])) continue;
671
672 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
673 }
674
675 for (; i < ANALOG_PORTS; i++)
676 analog_options[i] = 0xff;
677}
678
679/*
680 * The gameport device structure.
681 */
682
683static struct gameport_driver analog_drv = {
684 .driver = {
685 .name = "analog",
686 },
687 .description = DRIVER_DESC,
688 .connect = analog_connect,
689 .disconnect = analog_disconnect,
690};
691
692static int __init analog_init(void)
693{
694 analog_parse_options();
695 return gameport_register_driver(&analog_drv);
696}
697
698static void __exit analog_exit(void)
699{
700 gameport_unregister_driver(&analog_drv);
701}
702
703module_init(analog_init);
704module_exit(analog_exit);
1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright (c) 1996-2001 Vojtech Pavlik
4 */
5
6/*
7 * Analog joystick and gamepad driver for Linux
8 */
9
10/*
11 */
12
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/timex.h>
23#include <linux/timekeeping.h>
24
25#define DRIVER_DESC "Analog joystick and gamepad driver"
26
27MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
28MODULE_DESCRIPTION(DRIVER_DESC);
29MODULE_LICENSE("GPL");
30
31static bool use_ktime = true;
32module_param(use_ktime, bool, 0400);
33MODULE_PARM_DESC(use_ktime, "Use ktime for measuring I/O speed");
34
35/*
36 * Option parsing.
37 */
38
39#define ANALOG_PORTS 16
40
41static char *js[ANALOG_PORTS];
42static unsigned int js_nargs;
43static int analog_options[ANALOG_PORTS];
44module_param_array_named(map, js, charp, &js_nargs, 0);
45MODULE_PARM_DESC(map, "Describes analog joysticks type/capabilities");
46
47/*
48 * Times, feature definitions.
49 */
50
51#define ANALOG_RUDDER 0x00004
52#define ANALOG_THROTTLE 0x00008
53#define ANALOG_AXES_STD 0x0000f
54#define ANALOG_BTNS_STD 0x000f0
55
56#define ANALOG_BTNS_CHF 0x00100
57#define ANALOG_HAT1_CHF 0x00200
58#define ANALOG_HAT2_CHF 0x00400
59#define ANALOG_HAT_FCS 0x00800
60#define ANALOG_HATS_ALL 0x00e00
61#define ANALOG_BTN_TL 0x01000
62#define ANALOG_BTN_TR 0x02000
63#define ANALOG_BTN_TL2 0x04000
64#define ANALOG_BTN_TR2 0x08000
65#define ANALOG_BTNS_TLR 0x03000
66#define ANALOG_BTNS_TLR2 0x0c000
67#define ANALOG_BTNS_GAMEPAD 0x0f000
68
69#define ANALOG_HBTN_CHF 0x10000
70#define ANALOG_ANY_CHF 0x10700
71#define ANALOG_SAITEK 0x20000
72#define ANALOG_EXTENSIONS 0x7ff00
73#define ANALOG_GAMEPAD 0x80000
74
75#define ANALOG_MAX_TIME 3 /* 3 ms */
76#define ANALOG_LOOP_TIME 2000 /* 2 * loop */
77#define ANALOG_SAITEK_DELAY 200 /* 200 us */
78#define ANALOG_SAITEK_TIME 2000 /* 2000 us */
79#define ANALOG_AXIS_TIME 2 /* 2 * refresh */
80#define ANALOG_INIT_RETRIES 8 /* 8 times */
81#define ANALOG_FUZZ_BITS 2 /* 2 bit more */
82#define ANALOG_FUZZ_MAGIC 36 /* 36 u*ms/loop */
83
84#define ANALOG_MAX_NAME_LENGTH 128
85#define ANALOG_MAX_PHYS_LENGTH 32
86
87static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
88static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
89static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
90static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
91static short analog_pad_btn[] = { BTN_A, BTN_B, BTN_C, BTN_X, BTN_TL2, BTN_TR2, BTN_SELECT, BTN_START, BTN_MODE, BTN_BASE };
92static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
93 BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
94
95static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
96
97struct analog {
98 struct input_dev *dev;
99 int mask;
100 short *buttons;
101 char name[ANALOG_MAX_NAME_LENGTH];
102 char phys[ANALOG_MAX_PHYS_LENGTH];
103};
104
105struct analog_port {
106 struct gameport *gameport;
107 struct analog analog[2];
108 unsigned char mask;
109 char saitek;
110 char cooked;
111 int bads;
112 int reads;
113 int speed;
114 int loop;
115 int fuzz;
116 int axes[4];
117 int buttons;
118 int initial[4];
119 int axtime;
120};
121
122/*
123 * Time macros.
124 */
125
126#ifdef __i386__
127
128#include <linux/i8253.h>
129
130#define GET_TIME(x) do { if (boot_cpu_has(X86_FEATURE_TSC)) x = (unsigned int)rdtsc(); else x = get_time_pit(); } while (0)
131#define DELTA(x,y) (boot_cpu_has(X86_FEATURE_TSC) ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? PIT_TICK_RATE / HZ : 0)))
132#define TIME_NAME (boot_cpu_has(X86_FEATURE_TSC)?"TSC":"PIT")
133static unsigned int get_time_pit(void)
134{
135 unsigned long flags;
136 unsigned int count;
137
138 raw_spin_lock_irqsave(&i8253_lock, flags);
139 outb_p(0x00, 0x43);
140 count = inb_p(0x40);
141 count |= inb_p(0x40) << 8;
142 raw_spin_unlock_irqrestore(&i8253_lock, flags);
143
144 return count;
145}
146#elif defined(__x86_64__)
147#define GET_TIME(x) do { x = (unsigned int)rdtsc(); } while (0)
148#define DELTA(x,y) ((y)-(x))
149#define TIME_NAME "TSC"
150#elif defined(__alpha__) || defined(CONFIG_ARM) || defined(CONFIG_ARM64) || defined(CONFIG_PPC) || defined(CONFIG_RISCV)
151#define GET_TIME(x) do { x = get_cycles(); } while (0)
152#define DELTA(x,y) ((y)-(x))
153#define TIME_NAME "get_cycles"
154#else
155#define FAKE_TIME
156static unsigned long analog_faketime = 0;
157#define GET_TIME(x) do { x = analog_faketime++; } while(0)
158#define DELTA(x,y) ((y)-(x))
159#define TIME_NAME "Unreliable"
160#warning Precise timer not defined for this architecture.
161#endif
162
163static inline u64 get_time(void)
164{
165 if (use_ktime) {
166 return ktime_get_ns();
167 } else {
168 unsigned int x;
169 GET_TIME(x);
170 return x;
171 }
172}
173
174static inline unsigned int delta(u64 x, u64 y)
175{
176 if (use_ktime)
177 return y - x;
178 else
179 return DELTA((unsigned int)x, (unsigned int)y);
180}
181
182/*
183 * analog_decode() decodes analog joystick data and reports input events.
184 */
185
186static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
187{
188 struct input_dev *dev = analog->dev;
189 int i, j;
190
191 if (analog->mask & ANALOG_HAT_FCS)
192 for (i = 0; i < 4; i++)
193 if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
194 buttons |= 1 << (i + 14);
195 break;
196 }
197
198 for (i = j = 0; i < 6; i++)
199 if (analog->mask & (0x10 << i))
200 input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
201
202 if (analog->mask & ANALOG_HBTN_CHF)
203 for (i = 0; i < 4; i++)
204 input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
205
206 if (analog->mask & ANALOG_BTN_TL)
207 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
208 if (analog->mask & ANALOG_BTN_TR)
209 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
210 if (analog->mask & ANALOG_BTN_TL2)
211 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
212 if (analog->mask & ANALOG_BTN_TR2)
213 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
214
215 for (i = j = 0; i < 4; i++)
216 if (analog->mask & (1 << i))
217 input_report_abs(dev, analog_axes[j++], axes[i]);
218
219 for (i = j = 0; i < 3; i++)
220 if (analog->mask & analog_exts[i]) {
221 input_report_abs(dev, analog_hats[j++],
222 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
223 input_report_abs(dev, analog_hats[j++],
224 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
225 }
226
227 input_sync(dev);
228}
229
230/*
231 * analog_cooked_read() reads analog joystick data.
232 */
233
234static int analog_cooked_read(struct analog_port *port)
235{
236 struct gameport *gameport = port->gameport;
237 u64 time[4], start, loop, now;
238 unsigned int loopout, timeout;
239 unsigned char data[4], this, last;
240 unsigned long flags;
241 int i, j;
242
243 loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
244 timeout = ANALOG_MAX_TIME * port->speed;
245
246 local_irq_save(flags);
247 gameport_trigger(gameport);
248 now = get_time();
249 local_irq_restore(flags);
250
251 start = now;
252 this = port->mask;
253 i = 0;
254
255 do {
256 loop = now;
257 last = this;
258
259 local_irq_disable();
260 this = gameport_read(gameport) & port->mask;
261 now = get_time();
262 local_irq_restore(flags);
263
264 if ((last ^ this) && (delta(loop, now) < loopout)) {
265 data[i] = last ^ this;
266 time[i] = now;
267 i++;
268 }
269
270 } while (this && (i < 4) && (delta(start, now) < timeout));
271
272 this <<= 4;
273
274 for (--i; i >= 0; i--) {
275 this |= data[i];
276 for (j = 0; j < 4; j++)
277 if (data[i] & (1 << j))
278 port->axes[j] = (delta(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
279 }
280
281 return -(this != port->mask);
282}
283
284static int analog_button_read(struct analog_port *port, char saitek, char chf)
285{
286 unsigned char u;
287 int t = 1, i = 0;
288 int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
289
290 u = gameport_read(port->gameport);
291
292 if (!chf) {
293 port->buttons = (~u >> 4) & 0xf;
294 return 0;
295 }
296
297 port->buttons = 0;
298
299 while ((~u & 0xf0) && (i < 16) && t) {
300 port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
301 if (!saitek) return 0;
302 udelay(ANALOG_SAITEK_DELAY);
303 t = strobe;
304 gameport_trigger(port->gameport);
305 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
306 i++;
307 }
308
309 return -(!t || (i == 16));
310}
311
312/*
313 * analog_poll() repeatedly polls the Analog joysticks.
314 */
315
316static void analog_poll(struct gameport *gameport)
317{
318 struct analog_port *port = gameport_get_drvdata(gameport);
319 int i;
320
321 char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
322 char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
323
324 if (port->cooked) {
325 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
326 if (chf)
327 port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
328 port->reads++;
329 } else {
330 if (!port->axtime--) {
331 port->bads -= analog_cooked_read(port);
332 port->bads -= analog_button_read(port, saitek, chf);
333 port->reads++;
334 port->axtime = ANALOG_AXIS_TIME - 1;
335 } else {
336 if (!saitek)
337 analog_button_read(port, saitek, chf);
338 }
339 }
340
341 for (i = 0; i < 2; i++)
342 if (port->analog[i].mask)
343 analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
344}
345
346/*
347 * analog_open() is a callback from the input open routine.
348 */
349
350static int analog_open(struct input_dev *dev)
351{
352 struct analog_port *port = input_get_drvdata(dev);
353
354 gameport_start_polling(port->gameport);
355 return 0;
356}
357
358/*
359 * analog_close() is a callback from the input close routine.
360 */
361
362static void analog_close(struct input_dev *dev)
363{
364 struct analog_port *port = input_get_drvdata(dev);
365
366 gameport_stop_polling(port->gameport);
367}
368
369/*
370 * analog_calibrate_timer() calibrates the timer and computes loop
371 * and timeout values for a joystick port.
372 */
373
374static void analog_calibrate_timer(struct analog_port *port)
375{
376 struct gameport *gameport = port->gameport;
377 unsigned int i, t, tx;
378 u64 t1, t2, t3;
379 unsigned long flags;
380
381 if (use_ktime) {
382 port->speed = 1000000;
383 } else {
384 local_irq_save(flags);
385 t1 = get_time();
386#ifdef FAKE_TIME
387 analog_faketime += 830;
388#endif
389 mdelay(1);
390 t2 = get_time();
391 t3 = get_time();
392 local_irq_restore(flags);
393
394 port->speed = delta(t1, t2) - delta(t2, t3);
395 }
396
397 tx = ~0;
398
399 for (i = 0; i < 50; i++) {
400 local_irq_save(flags);
401 t1 = get_time();
402 for (t = 0; t < 50; t++) {
403 gameport_read(gameport);
404 t2 = get_time();
405 }
406 t3 = get_time();
407 local_irq_restore(flags);
408 udelay(i);
409 t = delta(t1, t2) - delta(t2, t3);
410 if (t < tx) tx = t;
411 }
412
413 port->loop = tx / 50;
414}
415
416/*
417 * analog_name() constructs a name for an analog joystick.
418 */
419
420static void analog_name(struct analog *analog)
421{
422 snprintf(analog->name, sizeof(analog->name), "Analog %d-axis %d-button",
423 hweight8(analog->mask & ANALOG_AXES_STD),
424 hweight8(analog->mask & ANALOG_BTNS_STD) + !!(analog->mask & ANALOG_BTNS_CHF) * 2 +
425 hweight16(analog->mask & ANALOG_BTNS_GAMEPAD) + !!(analog->mask & ANALOG_HBTN_CHF) * 4);
426
427 if (analog->mask & ANALOG_HATS_ALL)
428 snprintf(analog->name, sizeof(analog->name), "%s %d-hat",
429 analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
430
431 if (analog->mask & ANALOG_HAT_FCS)
432 strlcat(analog->name, " FCS", sizeof(analog->name));
433 if (analog->mask & ANALOG_ANY_CHF)
434 strlcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF",
435 sizeof(analog->name));
436
437 strlcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick",
438 sizeof(analog->name));
439}
440
441/*
442 * analog_init_device()
443 */
444
445static int analog_init_device(struct analog_port *port, struct analog *analog, int index)
446{
447 struct input_dev *input_dev;
448 int i, j, t, v, w, x, y, z;
449 int error;
450
451 analog_name(analog);
452 snprintf(analog->phys, sizeof(analog->phys),
453 "%s/input%d", port->gameport->phys, index);
454 analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
455
456 analog->dev = input_dev = input_allocate_device();
457 if (!input_dev)
458 return -ENOMEM;
459
460 input_dev->name = analog->name;
461 input_dev->phys = analog->phys;
462 input_dev->id.bustype = BUS_GAMEPORT;
463 input_dev->id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
464 input_dev->id.product = analog->mask >> 4;
465 input_dev->id.version = 0x0100;
466 input_dev->dev.parent = &port->gameport->dev;
467
468 input_set_drvdata(input_dev, port);
469
470 input_dev->open = analog_open;
471 input_dev->close = analog_close;
472
473 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
474
475 for (i = j = 0; i < 4; i++)
476 if (analog->mask & (1 << i)) {
477
478 t = analog_axes[j];
479 x = port->axes[i];
480 y = (port->axes[0] + port->axes[1]) >> 1;
481 z = y - port->axes[i];
482 z = z > 0 ? z : -z;
483 v = (x >> 3);
484 w = (x >> 3);
485
486 if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
487 x = y;
488
489 if (analog->mask & ANALOG_SAITEK) {
490 if (i == 2) x = port->axes[i];
491 v = x - (x >> 2);
492 w = (x >> 4);
493 }
494
495 input_set_abs_params(input_dev, t, v, (x << 1) - v, port->fuzz, w);
496 j++;
497 }
498
499 for (i = j = 0; i < 3; i++)
500 if (analog->mask & analog_exts[i])
501 for (x = 0; x < 2; x++) {
502 t = analog_hats[j++];
503 input_set_abs_params(input_dev, t, -1, 1, 0, 0);
504 }
505
506 for (i = j = 0; i < 4; i++)
507 if (analog->mask & (0x10 << i))
508 set_bit(analog->buttons[j++], input_dev->keybit);
509
510 if (analog->mask & ANALOG_BTNS_CHF)
511 for (i = 0; i < 2; i++)
512 set_bit(analog->buttons[j++], input_dev->keybit);
513
514 if (analog->mask & ANALOG_HBTN_CHF)
515 for (i = 0; i < 4; i++)
516 set_bit(analog->buttons[j++], input_dev->keybit);
517
518 for (i = 0; i < 4; i++)
519 if (analog->mask & (ANALOG_BTN_TL << i))
520 set_bit(analog_pads[i], input_dev->keybit);
521
522 analog_decode(analog, port->axes, port->initial, port->buttons);
523
524 error = input_register_device(analog->dev);
525 if (error) {
526 input_free_device(analog->dev);
527 return error;
528 }
529
530 return 0;
531}
532
533/*
534 * analog_init_devices() sets up device-specific values and registers the input devices.
535 */
536
537static int analog_init_masks(struct analog_port *port)
538{
539 int i;
540 struct analog *analog = port->analog;
541 int max[4];
542
543 if (!port->mask)
544 return -1;
545
546 if ((port->mask & 3) != 3 && port->mask != 0xc) {
547 printk(KERN_WARNING "analog.c: Unknown joystick device found "
548 "(data=%#x, %s), probably not analog joystick.\n",
549 port->mask, port->gameport->phys);
550 return -1;
551 }
552
553
554 i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
555
556 analog[0].mask = i & 0xfffff;
557
558 analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
559 | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
560 | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
561
562 analog[0].mask &= ~(ANALOG_HAT2_CHF)
563 | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
564
565 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
566 | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
567 | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
568 | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
569
570 analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
571 | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
572 & ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
573
574 analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
575
576 analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
577 : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
578
579 if (port->cooked) {
580
581 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
582
583 if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
584 if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
585 if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
586 if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
587 if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
588
589 gameport_calibrate(port->gameport, port->axes, max);
590 }
591
592 for (i = 0; i < 4; i++)
593 port->initial[i] = port->axes[i];
594
595 return -!(analog[0].mask || analog[1].mask);
596}
597
598static int analog_init_port(struct gameport *gameport, struct gameport_driver *drv, struct analog_port *port)
599{
600 int i, t, u, v;
601
602 port->gameport = gameport;
603
604 gameport_set_drvdata(gameport, port);
605
606 if (!gameport_open(gameport, drv, GAMEPORT_MODE_RAW)) {
607
608 analog_calibrate_timer(port);
609
610 gameport_trigger(gameport);
611 t = gameport_read(gameport);
612 msleep(ANALOG_MAX_TIME);
613 port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
614 port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
615
616 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
617 if (!analog_cooked_read(port))
618 break;
619 msleep(ANALOG_MAX_TIME);
620 }
621
622 u = v = 0;
623
624 msleep(ANALOG_MAX_TIME);
625 t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
626 gameport_trigger(gameport);
627 while ((gameport_read(port->gameport) & port->mask) && (u < t))
628 u++;
629 udelay(ANALOG_SAITEK_DELAY);
630 t = gameport_time(gameport, ANALOG_SAITEK_TIME);
631 gameport_trigger(gameport);
632 while ((gameport_read(port->gameport) & port->mask) && (v < t))
633 v++;
634
635 if (v < (u >> 1)) { /* FIXME - more than one port */
636 analog_options[0] |= /* FIXME - more than one port */
637 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
638 return 0;
639 }
640
641 gameport_close(gameport);
642 }
643
644 if (!gameport_open(gameport, drv, GAMEPORT_MODE_COOKED)) {
645
646 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
647 if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
648 break;
649 for (i = 0; i < 4; i++)
650 if (port->axes[i] != -1)
651 port->mask |= 1 << i;
652
653 port->fuzz = gameport->fuzz;
654 port->cooked = 1;
655 return 0;
656 }
657
658 return gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
659}
660
661static int analog_connect(struct gameport *gameport, struct gameport_driver *drv)
662{
663 struct analog_port *port;
664 int i;
665 int err;
666
667 if (!(port = kzalloc(sizeof(struct analog_port), GFP_KERNEL)))
668 return - ENOMEM;
669
670 err = analog_init_port(gameport, drv, port);
671 if (err)
672 goto fail1;
673
674 err = analog_init_masks(port);
675 if (err)
676 goto fail2;
677
678 gameport_set_poll_handler(gameport, analog_poll);
679 gameport_set_poll_interval(gameport, 10);
680
681 for (i = 0; i < 2; i++)
682 if (port->analog[i].mask) {
683 err = analog_init_device(port, port->analog + i, i);
684 if (err)
685 goto fail3;
686 }
687
688 return 0;
689
690 fail3: while (--i >= 0)
691 if (port->analog[i].mask)
692 input_unregister_device(port->analog[i].dev);
693 fail2: gameport_close(gameport);
694 fail1: gameport_set_drvdata(gameport, NULL);
695 kfree(port);
696 return err;
697}
698
699static void analog_disconnect(struct gameport *gameport)
700{
701 struct analog_port *port = gameport_get_drvdata(gameport);
702 int i;
703
704 for (i = 0; i < 2; i++)
705 if (port->analog[i].mask)
706 input_unregister_device(port->analog[i].dev);
707 gameport_close(gameport);
708 gameport_set_drvdata(gameport, NULL);
709 printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
710 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
711 port->gameport->phys);
712 kfree(port);
713}
714
715struct analog_types {
716 char *name;
717 int value;
718};
719
720static struct analog_types analog_types[] = {
721 { "none", 0x00000000 },
722 { "auto", 0x000000ff },
723 { "2btn", 0x0000003f },
724 { "y-joy", 0x0cc00033 },
725 { "y-pad", 0x8cc80033 },
726 { "fcs", 0x000008f7 },
727 { "chf", 0x000002ff },
728 { "fullchf", 0x000007ff },
729 { "gamepad", 0x000830f3 },
730 { "gamepad8", 0x0008f0f3 },
731 { NULL, 0 }
732};
733
734static void analog_parse_options(void)
735{
736 int i, j;
737 char *end;
738
739 for (i = 0; i < js_nargs; i++) {
740
741 for (j = 0; analog_types[j].name; j++)
742 if (!strcmp(analog_types[j].name, js[i])) {
743 analog_options[i] = analog_types[j].value;
744 break;
745 }
746 if (analog_types[j].name) continue;
747
748 analog_options[i] = simple_strtoul(js[i], &end, 0);
749 if (end != js[i]) continue;
750
751 analog_options[i] = 0xff;
752 if (!strlen(js[i])) continue;
753
754 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
755 }
756
757 for (; i < ANALOG_PORTS; i++)
758 analog_options[i] = 0xff;
759}
760
761/*
762 * The gameport device structure.
763 */
764
765static struct gameport_driver analog_drv = {
766 .driver = {
767 .name = "analog",
768 },
769 .description = DRIVER_DESC,
770 .connect = analog_connect,
771 .disconnect = analog_disconnect,
772};
773
774static int __init analog_init(void)
775{
776 analog_parse_options();
777 return gameport_register_driver(&analog_drv);
778}
779
780static void __exit analog_exit(void)
781{
782 gameport_unregister_driver(&analog_drv);
783}
784
785module_init(analog_init);
786module_exit(analog_exit);