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