Linux kernel & device driver programming

Cross-Referenced Linux and Device Driver Code

[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ]
Version: [ 2.6.11.8 ] [ 2.6.25 ] [ 2.6.25.8 ] [ 2.6.31.13 ] Architecture: [ i386 ]
  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 <asm/timex.h>
 42 
 43 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
 44 MODULE_DESCRIPTION("Analog joystick and gamepad driver");
 45 MODULE_LICENSE("GPL");
 46 
 47 /*
 48  * Option parsing.
 49  */
 50 
 51 #define ANALOG_PORTS            16
 52 
 53 static char *js[ANALOG_PORTS];
 54 static 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 __obsolete_setup("js=");
 60 
 61 /*
 62  * Times, feature definitions.
 63  */
 64 
 65 #define ANALOG_RUDDER           0x00004
 66 #define ANALOG_THROTTLE         0x00008
 67 #define ANALOG_AXES_STD         0x0000f
 68 #define ANALOG_BTNS_STD         0x000f0
 69 
 70 #define ANALOG_BTNS_CHF         0x00100
 71 #define ANALOG_HAT1_CHF         0x00200
 72 #define ANALOG_HAT2_CHF         0x00400
 73 #define ANALOG_HAT_FCS          0x00800
 74 #define ANALOG_HATS_ALL         0x00e00
 75 #define ANALOG_BTN_TL           0x01000
 76 #define ANALOG_BTN_TR           0x02000
 77 #define ANALOG_BTN_TL2          0x04000
 78 #define ANALOG_BTN_TR2          0x08000
 79 #define ANALOG_BTNS_TLR         0x03000
 80 #define ANALOG_BTNS_TLR2        0x0c000
 81 #define ANALOG_BTNS_GAMEPAD     0x0f000
 82 
 83 #define ANALOG_HBTN_CHF         0x10000
 84 #define ANALOG_ANY_CHF          0x10700
 85 #define ANALOG_SAITEK           0x20000
 86 #define ANALOG_EXTENSIONS       0x7ff00
 87 #define ANALOG_GAMEPAD          0x80000
 88 
 89 #define ANALOG_MAX_TIME         3       /* 3 ms */
 90 #define ANALOG_LOOP_TIME        2000    /* 2 * loop */
 91 #define ANALOG_REFRESH_TIME     HZ/100  /* 10 ms */
 92 #define ANALOG_SAITEK_DELAY     200     /* 200 us */
 93 #define ANALOG_SAITEK_TIME      2000    /* 2000 us */
 94 #define ANALOG_AXIS_TIME        2       /* 2 * refresh */
 95 #define ANALOG_INIT_RETRIES     8       /* 8 times */
 96 #define ANALOG_FUZZ_BITS        2       /* 2 bit more */
 97 #define ANALOG_FUZZ_MAGIC       36      /* 36 u*ms/loop */
 98 
 99 #define ANALOG_MAX_NAME_LENGTH  128
100 #define ANALOG_MAX_PHYS_LENGTH  32
101 
102 static short analog_axes[] = { ABS_X, ABS_Y, ABS_RUDDER, ABS_THROTTLE };
103 static short analog_hats[] = { ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
104 static short analog_pads[] = { BTN_Y, BTN_Z, BTN_TL, BTN_TR };
105 static short analog_exts[] = { ANALOG_HAT1_CHF, ANALOG_HAT2_CHF, ANALOG_HAT_FCS };
106 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 };
107 static short analog_joy_btn[] = { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2,
108                                   BTN_BASE3, BTN_BASE4, BTN_BASE5, BTN_BASE6 };
109 
110 static unsigned char analog_chf[] = { 0xf, 0x0, 0x1, 0x9, 0x2, 0x4, 0xc, 0x8, 0x3, 0x5, 0xb, 0x7, 0xd, 0xe, 0xa, 0x6 };
111 
112 struct analog {
113         struct input_dev dev;
114         int mask;
115         short *buttons;
116         char name[ANALOG_MAX_NAME_LENGTH];
117         char phys[ANALOG_MAX_PHYS_LENGTH];
118 };
119 
120 struct analog_port {
121         struct gameport *gameport;
122         struct timer_list timer;
123         struct analog analog[2];
124         unsigned char mask;
125         char saitek;
126         char cooked;
127         int bads;
128         int reads;
129         int speed;
130         int loop;
131         int fuzz;
132         int axes[4];
133         int buttons;
134         int initial[4];
135         int used;
136         int axtime;
137 };
138 
139 /*
140  * Time macros.
141  */
142 
143 #ifdef __i386__
144 #define GET_TIME(x)     do { if (cpu_has_tsc) rdtscl(x); else x = get_time_pit(); } while (0)
145 #define DELTA(x,y)      (cpu_has_tsc ? ((y) - (x)) : ((x) - (y) + ((x) < (y) ? CLOCK_TICK_RATE / HZ : 0)))
146 #define TIME_NAME       (cpu_has_tsc?"TSC":"PIT")
147 static unsigned int get_time_pit(void)
148 {
149         extern spinlock_t i8253_lock;
150         unsigned long flags;
151         unsigned int count;
152 
153         spin_lock_irqsave(&i8253_lock, flags);
154         outb_p(0x00, 0x43);
155         count = inb_p(0x40);
156         count |= inb_p(0x40) << 8;
157         spin_unlock_irqrestore(&i8253_lock, flags);
158 
159         return count;
160 }
161 #elif defined(__x86_64__)
162 #define GET_TIME(x)     rdtscl(x)
163 #define DELTA(x,y)      ((y)-(x))
164 #define TIME_NAME       "TSC"
165 #elif defined(__alpha__)
166 #define GET_TIME(x)     do { x = get_cycles(); } while (0)
167 #define DELTA(x,y)      ((y)-(x))
168 #define TIME_NAME       "PCC"
169 #else
170 #define FAKE_TIME
171 static unsigned long analog_faketime = 0;
172 #define GET_TIME(x)     do { x = analog_faketime++; } while(0)
173 #define DELTA(x,y)      ((y)-(x))
174 #define TIME_NAME       "Unreliable"
175 #warning Precise timer not defined for this architecture.
176 #endif
177 
178 /*
179  * analog_decode() decodes analog joystick data and reports input events.
180  */
181 
182 static void analog_decode(struct analog *analog, int *axes, int *initial, int buttons)
183 {
184         struct input_dev *dev = &analog->dev;
185         int i, j;
186 
187         if (analog->mask & ANALOG_HAT_FCS)
188                 for (i = 0; i < 4; i++)
189                         if (axes[3] < ((initial[3] * ((i << 1) + 1)) >> 3)) {
190                                 buttons |= 1 << (i + 14);
191                                 break;
192                         }
193 
194         for (i = j = 0; i < 6; i++)
195                 if (analog->mask & (0x10 << i))
196                         input_report_key(dev, analog->buttons[j++], (buttons >> i) & 1);
197 
198         if (analog->mask & ANALOG_HBTN_CHF)
199                 for (i = 0; i < 4; i++)
200                         input_report_key(dev, analog->buttons[j++], (buttons >> (i + 10)) & 1);
201 
202         if (analog->mask & ANALOG_BTN_TL)
203                 input_report_key(dev, analog_pads[0], axes[2] < (initial[2] >> 1));
204         if (analog->mask & ANALOG_BTN_TR)
205                 input_report_key(dev, analog_pads[1], axes[3] < (initial[3] >> 1));
206         if (analog->mask & ANALOG_BTN_TL2)
207                 input_report_key(dev, analog_pads[2], axes[2] > (initial[2] + (initial[2] >> 1)));
208         if (analog->mask & ANALOG_BTN_TR2)
209                 input_report_key(dev, analog_pads[3], axes[3] > (initial[3] + (initial[3] >> 1)));
210 
211         for (i = j = 0; i < 4; i++)
212                 if (analog->mask & (1 << i))
213                         input_report_abs(dev, analog_axes[j++], axes[i]);
214 
215         for (i = j = 0; i < 3; i++)
216                 if (analog->mask & analog_exts[i]) {
217                         input_report_abs(dev, analog_hats[j++],
218                                 ((buttons >> ((i << 2) + 7)) & 1) - ((buttons >> ((i << 2) + 9)) & 1));
219                         input_report_abs(dev, analog_hats[j++],
220                                 ((buttons >> ((i << 2) + 8)) & 1) - ((buttons >> ((i << 2) + 6)) & 1));
221                 }
222 
223         input_sync(dev);
224 }
225 
226 /*
227  * analog_cooked_read() reads analog joystick data.
228  */
229 
230 static int analog_cooked_read(struct analog_port *port)
231 {
232         struct gameport *gameport = port->gameport;
233         unsigned int time[4], start, loop, now, loopout, timeout;
234         unsigned char data[4], this, last;
235         unsigned long flags;
236         int i, j;
237 
238         loopout = (ANALOG_LOOP_TIME * port->loop) / 1000;
239         timeout = ANALOG_MAX_TIME * port->speed;
240 
241         local_irq_save(flags);
242         gameport_trigger(gameport);
243         GET_TIME(now);
244         local_irq_restore(flags);
245 
246         start = now;
247         this = port->mask;
248         i = 0;
249 
250         do {
251                 loop = now;
252                 last = this;
253 
254                 local_irq_disable();
255                 this = gameport_read(gameport) & port->mask;
256                 GET_TIME(now);
257                 local_irq_restore(flags);
258 
259                 if ((last ^ this) && (DELTA(loop, now) < loopout)) {
260                         data[i] = last ^ this;
261                         time[i] = now;
262                         i++;
263                 }
264 
265         } while (this && (i < 4) && (DELTA(start, now) < timeout));
266 
267         this <<= 4;
268 
269         for (--i; i >= 0; i--) {
270                 this |= data[i];
271                 for (j = 0; j < 4; j++)
272                         if (data[i] & (1 << j))
273                                 port->axes[j] = (DELTA(start, time[i]) << ANALOG_FUZZ_BITS) / port->loop;
274         }
275 
276         return -(this != port->mask);
277 }
278 
279 static int analog_button_read(struct analog_port *port, char saitek, char chf)
280 {
281         unsigned char u;
282         int t = 1, i = 0;
283         int strobe = gameport_time(port->gameport, ANALOG_SAITEK_TIME);
284 
285         u = gameport_read(port->gameport);
286 
287         if (!chf) {
288                 port->buttons = (~u >> 4) & 0xf;
289                 return 0;
290         }
291 
292         port->buttons = 0;
293 
294         while ((~u & 0xf0) && (i < 16) && t) {
295                 port->buttons |= 1 << analog_chf[(~u >> 4) & 0xf];
296                 if (!saitek) return 0;
297                 udelay(ANALOG_SAITEK_DELAY);
298                 t = strobe;
299                 gameport_trigger(port->gameport);
300                 while (((u = gameport_read(port->gameport)) & port->mask) && t) t--;
301                 i++;
302         }
303 
304         return -(!t || (i == 16));
305 }
306 
307 /*
308  * analog_timer() repeatedly polls the Analog joysticks.
309  */
310 
311 static void analog_timer(unsigned long data)
312 {
313         struct analog_port *port = (void *) data;
314         int i;
315 
316         char saitek = !!(port->analog[0].mask & ANALOG_SAITEK);
317         char chf = !!(port->analog[0].mask & ANALOG_ANY_CHF);
318 
319         if (port->cooked) {
320                 port->bads -= gameport_cooked_read(port->gameport, port->axes, &port->buttons);
321                 if (chf)
322                         port->buttons = port->buttons ? (1 << analog_chf[port->buttons]) : 0;
323                 port->reads++;
324         } else {
325                 if (!port->axtime--) {
326                         port->bads -= analog_cooked_read(port);
327                         port->bads -= analog_button_read(port, saitek, chf);
328                         port->reads++;
329                         port->axtime = ANALOG_AXIS_TIME - 1;
330                 } else {
331                         if (!saitek)
332                                 analog_button_read(port, saitek, chf);
333                 }
334         }
335 
336         for (i = 0; i < 2; i++)
337                 if (port->analog[i].mask)
338                         analog_decode(port->analog + i, port->axes, port->initial, port->buttons);
339 
340         mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME);
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         if (!port->used++)
351                 mod_timer(&port->timer, jiffies + ANALOG_REFRESH_TIME);
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         if (!--port->used)
363                 del_timer(&port->timer);
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         udelay(1000);
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         sprintf(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                 sprintf(analog->name, "%s %d-hat",
418                         analog->name, hweight16(analog->mask & ANALOG_HATS_ALL));
419 
420         if (analog->mask & ANALOG_HAT_FCS)
421                         strcat(analog->name, " FCS");
422         if (analog->mask & ANALOG_ANY_CHF)
423                         strcat(analog->name, (analog->mask & ANALOG_SAITEK) ? " Saitek" : " CHF");
424 
425         strcat(analog->name, (analog->mask & ANALOG_GAMEPAD) ? " gamepad": " joystick");
426 }
427 
428 /*
429  * analog_init_device()
430  */
431 
432 static void analog_init_device(struct analog_port *port, struct analog *analog, int index)
433 {
434         int i, j, t, v, w, x, y, z;
435 
436         analog_name(analog);
437         sprintf(analog->phys, "%s/input%d", port->gameport->phys, index);
438         analog->buttons = (analog->mask & ANALOG_GAMEPAD) ? analog_pad_btn : analog_joy_btn;
439 
440         init_input_dev(&analog->dev);
441 
442         analog->dev.name = analog->name;
443         analog->dev.phys = analog->phys;
444         analog->dev.id.bustype = BUS_GAMEPORT;
445         analog->dev.id.vendor = GAMEPORT_ID_VENDOR_ANALOG;
446         analog->dev.id.product = analog->mask >> 4;
447         analog->dev.id.version = 0x0100;
448 
449         analog->dev.open = analog_open;
450         analog->dev.close = analog_close;
451         analog->dev.private = port;
452         analog->dev.evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
453 
454         for (i = j = 0; i < 4; i++)
455                 if (analog->mask & (1 << i)) {
456 
457                         t = analog_axes[j];
458                         x = port->axes[i];
459                         y = (port->axes[0] + port->axes[1]) >> 1;
460                         z = y - port->axes[i];
461                         z = z > 0 ? z : -z;
462                         v = (x >> 3);
463                         w = (x >> 3);
464 
465                         set_bit(t, analog->dev.absbit);
466 
467                         if ((i == 2 || i == 3) && (j == 2 || j == 3) && (z > (y >> 3)))
468                                 x = y;
469 
470                         if (analog->mask & ANALOG_SAITEK) {
471                                 if (i == 2) x = port->axes[i];
472                                 v = x - (x >> 2);
473                                 w = (x >> 4);
474                         }
475 
476                         analog->dev.absmax[t] = (x << 1) - v;
477                         analog->dev.absmin[t] = v;
478                         analog->dev.absfuzz[t] = port->fuzz;
479                         analog->dev.absflat[t] = w;
480 
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                                 set_bit(t, analog->dev.absbit);
489                                 analog->dev.absmax[t] = 1;
490                                 analog->dev.absmin[t] = -1;
491                         }
492 
493         for (i = j = 0; i < 4; i++)
494                 if (analog->mask & (0x10 << i))
495                         set_bit(analog->buttons[j++], analog->dev.keybit);
496 
497         if (analog->mask & ANALOG_BTNS_CHF)
498                 for (i = 0; i < 2; i++)
499                         set_bit(analog->buttons[j++], analog->dev.keybit);
500 
501         if (analog->mask & ANALOG_HBTN_CHF)
502                 for (i = 0; i < 4; i++)
503                         set_bit(analog->buttons[j++], analog->dev.keybit);
504 
505         for (i = 0; i < 4; i++)
506                 if (analog->mask & (ANALOG_BTN_TL << i))
507                         set_bit(analog_pads[i], analog->dev.keybit);
508 
509         analog_decode(analog, port->axes, port->initial, port->buttons);
510 
511         input_register_device(&analog->dev);
512 
513         printk(KERN_INFO "input: %s at %s", analog->name, port->gameport->phys);
514 
515         if (port->cooked)
516                 printk(" [ADC port]\n");
517         else
518                 printk(" [%s timer, %d %sHz clock, %d ns res]\n", TIME_NAME,
519                 port->speed > 10000 ? (port->speed + 800) / 1000 : port->speed,
520                 port->speed > 10000 ? "M" : "k",
521                 port->speed > 10000 ? (port->loop * 1000) / (port->speed / 1000)
522                                     : (port->loop * 1000000) / port->speed);
523 }
524 
525 /*
526  * analog_init_devices() sets up device-specific values and registers the input devices.
527  */
528 
529 static int analog_init_masks(struct analog_port *port)
530 {
531         int i;
532         struct analog *analog = port->analog;
533         int max[4];
534 
535         if (!port->mask)
536                 return -1;
537 
538         if ((port->mask & 3) != 3 && port->mask != 0xc) {
539                 printk(KERN_WARNING "analog.c: Unknown joystick device found  "
540                         "(data=%#x, %s), probably not analog joystick.\n",
541                         port->mask, port->gameport->phys);
542                 return -1;
543         }
544 
545 
546         i = analog_options[0]; /* FIXME !!! - need to specify options for different ports */
547 
548         analog[0].mask = i & 0xfffff;
549 
550         analog[0].mask &= ~(ANALOG_AXES_STD | ANALOG_HAT_FCS | ANALOG_BTNS_GAMEPAD)
551                         | port->mask | ((port->mask << 8) & ANALOG_HAT_FCS)
552                         | ((port->mask << 10) & ANALOG_BTNS_TLR) | ((port->mask << 12) & ANALOG_BTNS_TLR2);
553 
554         analog[0].mask &= ~(ANALOG_HAT2_CHF)
555                         | ((analog[0].mask & ANALOG_HBTN_CHF) ? 0 : ANALOG_HAT2_CHF);
556 
557         analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_BTN_TR | ANALOG_BTN_TR2)
558                         | ((~analog[0].mask & ANALOG_HAT_FCS) >> 8)
559                         | ((~analog[0].mask & ANALOG_HAT_FCS) << 2)
560                         | ((~analog[0].mask & ANALOG_HAT_FCS) << 4);
561 
562         analog[0].mask &= ~(ANALOG_THROTTLE | ANALOG_RUDDER)
563                         | (((~analog[0].mask & ANALOG_BTNS_TLR ) >> 10)
564                         &  ((~analog[0].mask & ANALOG_BTNS_TLR2) >> 12));
565 
566         analog[1].mask = ((i >> 20) & 0xff) | ((i >> 12) & 0xf0000);
567 
568         analog[1].mask &= (analog[0].mask & ANALOG_EXTENSIONS) ? ANALOG_GAMEPAD
569                         : (((ANALOG_BTNS_STD | port->mask) & ~analog[0].mask) | ANALOG_GAMEPAD);
570 
571         if (port->cooked) {
572 
573                 for (i = 0; i < 4; i++) max[i] = port->axes[i] << 1;
574 
575                 if ((analog[0].mask & 0x7) == 0x7) max[2] = (max[0] + max[1]) >> 1;
576                 if ((analog[0].mask & 0xb) == 0xb) max[3] = (max[0] + max[1]) >> 1;
577                 if ((analog[0].mask & ANALOG_BTN_TL) && !(analog[0].mask & ANALOG_BTN_TL2)) max[2] >>= 1;
578                 if ((analog[0].mask & ANALOG_BTN_TR) && !(analog[0].mask & ANALOG_BTN_TR2)) max[3] >>= 1;
579                 if ((analog[0].mask & ANALOG_HAT_FCS)) max[3] >>= 1;
580 
581                 gameport_calibrate(port->gameport, port->axes, max);
582         }
583 
584         for (i = 0; i < 4; i++)
585                 port->initial[i] = port->axes[i];
586 
587         return -!(analog[0].mask || analog[1].mask);
588 }
589 
590 static int analog_init_port(struct gameport *gameport, struct gameport_dev *dev, struct analog_port *port)
591 {
592         int i, t, u, v;
593 
594         gameport->private = port;
595         port->gameport = gameport;
596         init_timer(&port->timer);
597         port->timer.data = (long) port;
598         port->timer.function = analog_timer;
599 
600         if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW)) {
601 
602                 analog_calibrate_timer(port);
603 
604                 gameport_trigger(gameport);
605                 t = gameport_read(gameport);
606                 msleep(ANALOG_MAX_TIME);
607                 port->mask = (gameport_read(gameport) ^ t) & t & 0xf;
608                 port->fuzz = (port->speed * ANALOG_FUZZ_MAGIC) / port->loop / 1000 + ANALOG_FUZZ_BITS;
609 
610                 for (i = 0; i < ANALOG_INIT_RETRIES; i++) {
611                         if (!analog_cooked_read(port)) break;
612                         msleep(ANALOG_MAX_TIME);
613                 }
614 
615                 u = v = 0;
616 
617                 msleep(ANALOG_MAX_TIME);
618                 t = gameport_time(gameport, ANALOG_MAX_TIME * 1000);
619                 gameport_trigger(gameport);
620                 while ((gameport_read(port->gameport) & port->mask) && (u < t)) u++;
621                 udelay(ANALOG_SAITEK_DELAY);
622                 t = gameport_time(gameport, ANALOG_SAITEK_TIME);
623                 gameport_trigger(gameport);
624                 while ((gameport_read(port->gameport) & port->mask) && (v < t)) v++;
625 
626                 if (v < (u >> 1)) { /* FIXME - more than one port */
627                         analog_options[0] |= /* FIXME - more than one port */
628                                 ANALOG_SAITEK | ANALOG_BTNS_CHF | ANALOG_HBTN_CHF | ANALOG_HAT1_CHF;
629                         return 0;
630                 }
631 
632                 gameport_close(gameport);
633         }
634 
635         if (!gameport_open(gameport, dev, GAMEPORT_MODE_COOKED)) {
636 
637                 for (i = 0; i < ANALOG_INIT_RETRIES; i++)
638                         if (!gameport_cooked_read(gameport, port->axes, &port->buttons))
639                                 break;
640                 for (i = 0; i < 4; i++)
641                         if (port->axes[i] != -1) port->mask |= 1 << i;
642 
643                 port->fuzz = gameport->fuzz;
644                 port->cooked = 1;
645                 return 0;
646         }
647 
648         if (!gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
649                 return 0;
650 
651         return -1;
652 }
653 
654 static void analog_connect(struct gameport *gameport, struct gameport_dev *dev)
655 {
656         struct analog_port *port;
657         int i;
658 
659         if (!(port = kmalloc(sizeof(struct analog_port), GFP_KERNEL)))
660                 return;
661         memset(port, 0, sizeof(struct analog_port));
662 
663         if (analog_init_port(gameport, dev, port)) {
664                 kfree(port);
665                 return;
666         }
667 
668         if (analog_init_masks(port)) {
669                 gameport_close(gameport);
670                 kfree(port);
671                 return;
672         }
673 
674         for (i = 0; i < 2; i++)
675                 if (port->analog[i].mask)
676                         analog_init_device(port, port->analog + i, i);
677 }
678 
679 static void analog_disconnect(struct gameport *gameport)
680 {
681         int i;
682 
683         struct analog_port *port = gameport->private;
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         printk(KERN_INFO "analog.c: %d out of %d reads (%d%%) on %s failed\n",
689                 port->bads, port->reads, port->reads ? (port->bads * 100 / port->reads) : 0,
690                 port->gameport->phys);
691         kfree(port);
692 }
693 
694 struct analog_types {
695         char *name;
696         int value;
697 };
698 
699 struct analog_types analog_types[] = {
700         { "none",       0x00000000 },
701         { "auto",       0x000000ff },
702         { "2btn",       0x0000003f },
703         { "y-joy",      0x0cc00033 },
704         { "y-pad",      0x8cc80033 },
705         { "fcs",        0x000008f7 },
706         { "chf",        0x000002ff },
707         { "fullchf",    0x000007ff },
708         { "gamepad",    0x000830f3 },
709         { "gamepad8",   0x0008f0f3 },
710         { NULL, 0 }
711 };
712 
713 static void analog_parse_options(void)
714 {
715         int i, j;
716         char *end;
717 
718         for (i = 0; i < js_nargs; i++) {
719 
720                 for (j = 0; analog_types[j].name; j++)
721                         if (!strcmp(analog_types[j].name, js[i])) {
722                                 analog_options[i] = analog_types[j].value;
723                                 break;
724                         }
725                 if (analog_types[j].name) continue;
726 
727                 analog_options[i] = simple_strtoul(js[i], &end, 0);
728                 if (end != js[i]) continue;
729 
730                 analog_options[i] = 0xff;
731                 if (!strlen(js[i])) continue;
732 
733                 printk(KERN_WARNING "analog.c: Bad config for port %d - \"%s\"\n", i, js[i]);
734         }
735 
736         for (; i < ANALOG_PORTS; i++)
737                 analog_options[i] = 0xff;
738 }
739 
740 /*
741  * The gameport device structure.
742  */
743 
744 static struct gameport_dev analog_dev = {
745         .connect =      analog_connect,
746         .disconnect =   analog_disconnect,
747 };
748 
749 int __init analog_init(void)
750 {
751         analog_parse_options();
752         gameport_register_device(&analog_dev);
753         return 0;
754 }
755 
756 void __exit analog_exit(void)
757 {
758         gameport_unregister_device(&analog_dev);
759 }
760 
761 module_init(analog_init);
762 module_exit(analog_exit);
763 
  This page was automatically generated by the LXR engine.