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