patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / drivers / input / joystick / sidewinder.c
1 /*
2  * $Id: sidewinder.c,v 1.29 2002/01/22 20:28:51 vojtech Exp $
3  *
4  *  Copyright (c) 1998-2001 Vojtech Pavlik
5  */
6
7 /*
8  * Microsoft SideWinder joystick family 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/delay.h>
32 #include <linux/kernel.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/init.h>
36 #include <linux/input.h>
37 #include <linux/gameport.h>
38
39 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40 MODULE_DESCRIPTION("Microsoft SideWinder joystick family driver");
41 MODULE_LICENSE("GPL");
42
43 /*
44  * These are really magic values. Changing them can make a problem go away,
45  * as well as break everything.
46  */
47
48 #define SW_DEBUG
49
50 #define SW_START        400     /* The time we wait for the first bit [400 us] */
51 #define SW_STROBE       45      /* Max time per bit [45 us] */
52 #define SW_TIMEOUT      4000    /* Wait for everything to settle [4 ms] */
53 #define SW_KICK         45      /* Wait after A0 fall till kick [45 us] */
54 #define SW_END          8       /* Number of bits before end of packet to kick */
55 #define SW_FAIL         16      /* Number of packet read errors to fail and reinitialize */
56 #define SW_BAD          2       /* Number of packet read errors to switch off 3d Pro optimization */
57 #define SW_OK           64      /* Number of packet read successes to switch optimization back on */
58 #define SW_LENGTH       512     /* Max number of bits in a packet */
59 #define SW_REFRESH      HZ/50   /* Time to wait between updates of joystick data [20 ms] */
60
61 #ifdef SW_DEBUG
62 #define dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg)
63 #else
64 #define dbg(format, arg...) do {} while (0)
65 #endif
66
67 /*
68  * SideWinder joystick types ...
69  */
70
71 #define SW_ID_3DP       0
72 #define SW_ID_GP        1
73 #define SW_ID_PP        2
74 #define SW_ID_FFP       3
75 #define SW_ID_FSP       4
76 #define SW_ID_FFW       5
77
78 /*
79  * Names, buttons, axes ...
80  */
81
82 static char *sw_name[] = {      "3D Pro", "GamePad", "Precision Pro", "Force Feedback Pro", "FreeStyle Pro",
83                                 "Force Feedback Wheel" };
84
85 static char sw_abs[][7] = {
86         { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
87         { ABS_X, ABS_Y },
88         { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
89         { ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
90         { ABS_X, ABS_Y,         ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
91         { ABS_RX, ABS_RUDDER,   ABS_THROTTLE }};
92
93 static char sw_bit[][7] = {
94         { 10, 10,  9, 10,  1,  1 },
95         {  1,  1                 },
96         { 10, 10,  6,  7,  1,  1 },
97         { 10, 10,  6,  7,  1,  1 },
98         { 10, 10,  6,  1,  1     },
99         { 10,  7,  7,  1,  1     }};
100
101 static short sw_btn[][12] = {
102         { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_MODE },
103         { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE },
104         { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
105         { BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
106         { BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT },
107         { BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 }};
108
109 static struct {
110         int x;
111         int y;
112 } sw_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
113
114 struct sw {
115         struct gameport *gameport;
116         struct timer_list timer;
117         struct input_dev dev[4];
118         char name[64];
119         char phys[4][32];
120         int length;
121         int type;
122         int bits;
123         int number;
124         int fail;
125         int ok;
126         int reads;
127         int bads;
128         int used;
129 };
130
131 /*
132  * sw_read_packet() is a function which reads either a data packet, or an
133  * identification packet from a SideWinder joystick. The protocol is very,
134  * very, very braindamaged. Microsoft patented it in US patent #5628686.
135  */
136
137 static int sw_read_packet(struct gameport *gameport, unsigned char *buf, int length, int id)
138 {
139         unsigned long flags;
140         int timeout, bitout, sched, i, kick, start, strobe;
141         unsigned char pending, u, v;
142
143         i = -id;                                                /* Don't care about data, only want ID */
144         timeout = id ? gameport_time(gameport, SW_TIMEOUT) : 0; /* Set up global timeout for ID packet */
145         kick = id ? gameport_time(gameport, SW_KICK) : 0;       /* Set up kick timeout for ID packet */
146         start = gameport_time(gameport, SW_START);
147         strobe = gameport_time(gameport, SW_STROBE);
148         bitout = start;
149         pending = 0;
150         sched = 0;
151
152         local_irq_save(flags);                                  /* Quiet, please */
153
154         gameport_trigger(gameport);                             /* Trigger */
155         v = gameport_read(gameport);
156
157         do {
158                 bitout--;
159                 u = v;
160                 v = gameport_read(gameport);
161         } while (!(~v & u & 0x10) && (bitout > 0));             /* Wait for first falling edge on clock */
162
163         if (bitout > 0) bitout = strobe;                        /* Extend time if not timed out */
164
165         while ((timeout > 0 || bitout > 0) && (i < length)) {
166
167                 timeout--;
168                 bitout--;                                       /* Decrement timers */
169                 sched--;
170
171                 u = v;
172                 v = gameport_read(gameport);
173
174                 if ((~u & v & 0x10) && (bitout > 0)) {          /* Rising edge on clock - data bit */
175                         if (i >= 0)                             /* Want this data */
176                                 buf[i] = v >> 5;                /* Store it */
177                         i++;                                    /* Advance index */
178                         bitout = strobe;                        /* Extend timeout for next bit */
179                 }
180
181                 if (kick && (~v & u & 0x01)) {                  /* Falling edge on axis 0 */
182                         sched = kick;                           /* Schedule second trigger */
183                         kick = 0;                               /* Don't schedule next time on falling edge */
184                         pending = 1;                            /* Mark schedule */
185                 }
186
187                 if (pending && sched < 0 && (i > -SW_END)) {    /* Second trigger time */
188                         gameport_trigger(gameport);             /* Trigger */
189                         bitout = start;                         /* Long bit timeout */
190                         pending = 0;                            /* Unmark schedule */
191                         timeout = 0;                            /* Switch from global to bit timeouts */
192                 }
193         }
194
195         local_irq_restore(flags);                                       /* Done - relax */
196
197 #ifdef SW_DEBUG
198         {
199                 int j;
200                 printk(KERN_DEBUG "sidewinder.c: Read %d triplets. [", i);
201                 for (j = 0; j < i; j++) printk("%d", buf[j]);
202                 printk("]\n");
203         }
204 #endif
205
206         return i;
207 }
208
209 /*
210  * sw_get_bits() and GB() compose bits from the triplet buffer into a __u64.
211  * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
212  * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
213  * is number of bits per triplet.
214  */
215
216 #define GB(pos,num) sw_get_bits(buf, pos, num, sw->bits)
217
218 static __u64 sw_get_bits(unsigned char *buf, int pos, int num, char bits)
219 {
220         __u64 data = 0;
221         int tri = pos % bits;                                           /* Start position */
222         int i   = pos / bits;
223         int bit = 0;
224
225         while (num--) {
226                 data |= (__u64)((buf[i] >> tri++) & 1) << bit++;        /* Transfer bit */
227                 if (tri == bits) {
228                         i++;                                            /* Next triplet */
229                         tri = 0;
230                 }
231         }
232
233         return data;
234 }
235
236 /*
237  * sw_init_digital() initializes a SideWinder 3D Pro joystick
238  * into digital mode.
239  */
240
241 static void sw_init_digital(struct gameport *gameport)
242 {
243         int seq[] = { 140, 140+725, 140+300, 0 };
244         unsigned long flags;
245         int i, t;
246
247         local_irq_save(flags);
248
249         i = 0;
250         do {
251                 gameport_trigger(gameport);                     /* Trigger */
252                 t = gameport_time(gameport, SW_TIMEOUT);
253                 while ((gameport_read(gameport) & 1) && t) t--; /* Wait for axis to fall back to 0 */
254                 udelay(seq[i]);                                 /* Delay magic time */
255         } while (seq[++i]);
256
257         gameport_trigger(gameport);                             /* Last trigger */
258
259         local_irq_restore(flags);
260 }
261
262 /*
263  * sw_parity() computes parity of __u64
264  */
265
266 static int sw_parity(__u64 t)
267 {
268         int x = t ^ (t >> 32);
269         x ^= x >> 16;
270         x ^= x >> 8;
271         x ^= x >> 4;
272         x ^= x >> 2;
273         x ^= x >> 1;
274         return x & 1;
275 }
276
277 /*
278  * sw_ccheck() checks synchronization bits and computes checksum of nibbles.
279  */
280
281 static int sw_check(__u64 t)
282 {
283         unsigned char sum = 0;
284
285         if ((t & 0x8080808080808080ULL) ^ 0x80)                 /* Sync */
286                 return -1;
287
288         while (t) {                                             /* Sum */
289                 sum += t & 0xf;
290                 t >>= 4;
291         }
292
293         return sum & 0xf;
294 }
295
296 /*
297  * sw_parse() analyzes SideWinder joystick data, and writes the results into
298  * the axes and buttons arrays.
299  */
300
301 static int sw_parse(unsigned char *buf, struct sw *sw)
302 {
303         int hat, i, j;
304         struct input_dev *dev = sw->dev;
305
306         switch (sw->type) {
307
308                 case SW_ID_3DP:
309
310                         if (sw_check(GB(0,64)) || (hat = (GB(6,1) << 3) | GB(60,3)) > 8) return -1;
311
312                         input_report_abs(dev, ABS_X,        (GB( 3,3) << 7) | GB(16,7));
313                         input_report_abs(dev, ABS_Y,        (GB( 0,3) << 7) | GB(24,7));
314                         input_report_abs(dev, ABS_RZ,       (GB(35,2) << 7) | GB(40,7));
315                         input_report_abs(dev, ABS_THROTTLE, (GB(32,3) << 7) | GB(48,7));
316
317                         input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
318                         input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
319
320                         for (j = 0; j < 7; j++)
321                                 input_report_key(dev, sw_btn[SW_ID_3DP][j], !GB(j+8,1));
322
323                         input_report_key(dev, BTN_BASE4, !GB(38,1));
324                         input_report_key(dev, BTN_BASE5, !GB(37,1));
325
326                         input_sync(dev);
327
328                         return 0;
329
330                 case SW_ID_GP:
331
332                         for (i = 0; i < sw->number; i ++) {
333
334                                 if (sw_parity(GB(i*15,15))) return -1;
335
336                                 input_report_abs(dev + i, ABS_X, GB(i*15+3,1) - GB(i*15+2,1));
337                                 input_report_abs(dev + i, ABS_Y, GB(i*15+0,1) - GB(i*15+1,1));
338
339                                 for (j = 0; j < 10; j++)
340                                         input_report_key(dev + i, sw_btn[SW_ID_GP][j], !GB(i*15+j+4,1));
341
342                                 input_sync(dev + i);
343                         }
344
345                         return 0;
346
347                 case SW_ID_PP:
348                 case SW_ID_FFP:
349
350                         if (!sw_parity(GB(0,48)) || (hat = GB(42,4)) > 8) return -1;
351
352                         input_report_abs(dev, ABS_X,        GB( 9,10));
353                         input_report_abs(dev, ABS_Y,        GB(19,10));
354                         input_report_abs(dev, ABS_RZ,       GB(36, 6));
355                         input_report_abs(dev, ABS_THROTTLE, GB(29, 7));
356
357                         input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
358                         input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
359
360                         for (j = 0; j < 9; j++)
361                                 input_report_key(dev, sw_btn[SW_ID_PP][j], !GB(j,1));
362
363                         input_sync(dev);
364
365                         return 0;
366
367                 case SW_ID_FSP:
368
369                         if (!sw_parity(GB(0,43)) || (hat = GB(28,4)) > 8) return -1;
370
371                         input_report_abs(dev, ABS_X,        GB( 0,10));
372                         input_report_abs(dev, ABS_Y,        GB(16,10));
373                         input_report_abs(dev, ABS_THROTTLE, GB(32, 6));
374
375                         input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
376                         input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
377
378                         for (j = 0; j < 6; j++)
379                                 input_report_key(dev, sw_btn[SW_ID_FSP][j], !GB(j+10,1));
380
381                         input_report_key(dev, BTN_TR,     !GB(26,1));
382                         input_report_key(dev, BTN_START,  !GB(27,1));
383                         input_report_key(dev, BTN_MODE,   !GB(38,1));
384                         input_report_key(dev, BTN_SELECT, !GB(39,1));
385
386                         input_sync(dev);
387
388                         return 0;
389
390                 case SW_ID_FFW:
391
392                         if (!sw_parity(GB(0,33))) return -1;
393
394                         input_report_abs(dev, ABS_RX,       GB( 0,10));
395                         input_report_abs(dev, ABS_RUDDER,   GB(10, 6));
396                         input_report_abs(dev, ABS_THROTTLE, GB(16, 6));
397
398                         for (j = 0; j < 8; j++)
399                                 input_report_key(dev, sw_btn[SW_ID_FFW][j], !GB(j+22,1));
400
401                         input_sync(dev);
402
403                         return 0;
404         }
405
406         return -1;
407 }
408
409 /*
410  * sw_read() reads SideWinder joystick data, and reinitializes
411  * the joystick in case of persistent problems. This is the function that is
412  * called from the generic code to poll the joystick.
413  */
414
415 static int sw_read(struct sw *sw)
416 {
417         unsigned char buf[SW_LENGTH];
418         int i;
419
420         i = sw_read_packet(sw->gameport, buf, sw->length, 0);
421
422         if (sw->type == SW_ID_3DP && sw->length == 66 && i != 66) {             /* Broken packet, try to fix */
423
424                 if (i == 64 && !sw_check(sw_get_bits(buf,0,64,1))) {            /* Last init failed, 1 bit mode */
425                         printk(KERN_WARNING "sidewinder.c: Joystick in wrong mode on %s"
426                                 " - going to reinitialize.\n", sw->gameport->phys);
427                         sw->fail = SW_FAIL;                                     /* Reinitialize */
428                         i = 128;                                                /* Bogus value */
429                 }
430
431                 if (i < 66 && GB(0,64) == GB(i*3-66,64))                        /* 1 == 3 */
432                         i = 66;                                                 /* Everything is fine */
433
434                 if (i < 66 && GB(0,64) == GB(66,64))                            /* 1 == 2 */
435                         i = 66;                                                 /* Everything is fine */
436
437                 if (i < 66 && GB(i*3-132,64) == GB(i*3-66,64)) {                /* 2 == 3 */
438                         memmove(buf, buf + i - 22, 22);                         /* Move data */
439                         i = 66;                                                 /* Carry on */
440                 }
441         }
442
443         if (i == sw->length && !sw_parse(buf, sw)) {                            /* Parse data */
444
445                 sw->fail = 0;
446                 sw->ok++;
447
448                 if (sw->type == SW_ID_3DP && sw->length == 66                   /* Many packets OK */
449                         && sw->ok > SW_OK) {
450
451                         printk(KERN_INFO "sidewinder.c: No more trouble on %s"
452                                 " - enabling optimization again.\n", sw->gameport->phys);
453                         sw->length = 22;
454                 }
455
456                 return 0;
457         }
458
459         sw->ok = 0;
460         sw->fail++;
461
462         if (sw->type == SW_ID_3DP && sw->length == 22 && sw->fail > SW_BAD) {   /* Consecutive bad packets */
463
464                 printk(KERN_INFO "sidewinder.c: Many bit errors on %s"
465                         " - disabling optimization.\n", sw->gameport->phys);
466                 sw->length = 66;
467         }
468
469         if (sw->fail < SW_FAIL) return -1;                                      /* Not enough, don't reinitialize yet */
470
471         printk(KERN_WARNING "sidewinder.c: Too many bit errors on %s"
472                 " - reinitializing joystick.\n", sw->gameport->phys);
473
474         if (!i && sw->type == SW_ID_3DP) {                                      /* 3D Pro can be in analog mode */
475                 udelay(3 * SW_TIMEOUT);
476                 sw_init_digital(sw->gameport);
477         }
478
479         udelay(SW_TIMEOUT);
480         i = sw_read_packet(sw->gameport, buf, SW_LENGTH, 0);                    /* Read normal data packet */
481         udelay(SW_TIMEOUT);
482         sw_read_packet(sw->gameport, buf, SW_LENGTH, i);                        /* Read ID packet, this initializes the stick */
483
484         sw->fail = SW_FAIL;
485
486         return -1;
487 }
488
489 static void sw_timer(unsigned long private)
490 {
491         struct sw *sw = (void *) private;
492
493         sw->reads++;
494         if (sw_read(sw)) sw->bads++;
495         mod_timer(&sw->timer, jiffies + SW_REFRESH);
496 }
497
498 static int sw_open(struct input_dev *dev)
499 {
500         struct sw *sw = dev->private;
501         if (!sw->used++)
502                 mod_timer(&sw->timer, jiffies + SW_REFRESH);
503         return 0;
504 }
505
506 static void sw_close(struct input_dev *dev)
507 {
508         struct sw *sw = dev->private;
509         if (!--sw->used)
510                 del_timer(&sw->timer);
511 }
512
513 /*
514  * sw_print_packet() prints the contents of a SideWinder packet.
515  */
516
517 static void sw_print_packet(char *name, int length, unsigned char *buf, char bits)
518 {
519         int i;
520
521         printk(KERN_INFO "sidewinder.c: %s packet, %d bits. [", name, length);
522         for (i = (((length + 3) >> 2) - 1); i >= 0; i--)
523                 printk("%x", (int)sw_get_bits(buf, i << 2, 4, bits));
524         printk("]\n");
525 }
526
527 /*
528  * sw_3dp_id() translates the 3DP id into a human legible string.
529  * Unfortunately I don't know how to do this for the other SW types.
530  */
531
532 static void sw_3dp_id(unsigned char *buf, char *comment)
533 {
534         int i;
535         char pnp[8], rev[9];
536
537         for (i = 0; i < 7; i++)                                         /* ASCII PnP ID */
538                 pnp[i] = sw_get_bits(buf, 24+8*i, 8, 1);
539
540         for (i = 0; i < 8; i++)                                         /* ASCII firmware revision */
541                 rev[i] = sw_get_bits(buf, 88+8*i, 8, 1);
542
543         pnp[7] = rev[8] = 0;
544
545         sprintf(comment, " [PnP %d.%02d id %s rev %s]",
546                 (int) ((sw_get_bits(buf, 8, 6, 1) << 6) |               /* Two 6-bit values */
547                         sw_get_bits(buf, 16, 6, 1)) / 100,
548                 (int) ((sw_get_bits(buf, 8, 6, 1) << 6) |
549                         sw_get_bits(buf, 16, 6, 1)) % 100,
550                  pnp, rev);
551 }
552
553 /*
554  * sw_guess_mode() checks the upper two button bits for toggling -
555  * indication of that the joystick is in 3-bit mode. This is documented
556  * behavior for 3DP ID packet, and for example the FSP does this in
557  * normal packets instead. Fun ...
558  */
559
560 static int sw_guess_mode(unsigned char *buf, int len)
561 {
562         int i;
563         unsigned char xor = 0;
564         for (i = 1; i < len; i++) xor |= (buf[i - 1] ^ buf[i]) & 6;
565         return !!xor * 2 + 1;
566 }
567
568 /*
569  * sw_connect() probes for SideWinder type joysticks.
570  */
571
572 static void sw_connect(struct gameport *gameport, struct gameport_dev *dev)
573 {
574         struct sw *sw;
575         int i, j, k, l;
576         unsigned char *buf = NULL;      /* [SW_LENGTH] */
577         unsigned char *idbuf = NULL;    /* [SW_LENGTH] */
578         unsigned char m = 1;
579         char comment[40];
580
581         comment[0] = 0;
582
583         if (!(sw = kmalloc(sizeof(struct sw), GFP_KERNEL))) return;
584         memset(sw, 0, sizeof(struct sw));
585
586         buf = kmalloc(SW_LENGTH, GFP_KERNEL);
587         idbuf = kmalloc(SW_LENGTH, GFP_KERNEL);
588         if (!buf || !idbuf)
589                 goto fail1;
590
591         gameport->private = sw;
592
593         sw->gameport = gameport;
594         init_timer(&sw->timer);
595         sw->timer.data = (long) sw;
596         sw->timer.function = sw_timer;
597
598         if (gameport_open(gameport, dev, GAMEPORT_MODE_RAW))
599                 goto fail1;
600
601         dbg("Init 0: Opened %s, io %#x, speed %d",
602                 gameport->phys, gameport->io, gameport->speed);
603
604         i = sw_read_packet(gameport, buf, SW_LENGTH, 0);                /* Read normal packet */
605         udelay(SW_TIMEOUT);
606         dbg("Init 1: Mode %d. Length %d.", m , i);
607
608         if (!i) {                                                       /* No data. 3d Pro analog mode? */
609                 sw_init_digital(gameport);                              /* Switch to digital */
610                 udelay(SW_TIMEOUT);
611                 i = sw_read_packet(gameport, buf, SW_LENGTH, 0);        /* Retry reading packet */
612                 udelay(SW_TIMEOUT);
613                 dbg("Init 1b: Length %d.", i);
614                 if (!i) goto fail2;                                     /* No data -> FAIL */
615         }
616
617         j = sw_read_packet(gameport, idbuf, SW_LENGTH, i);              /* Read ID. This initializes the stick */
618         m |= sw_guess_mode(idbuf, j);                                   /* ID packet should carry mode info [3DP] */
619         dbg("Init 2: Mode %d. ID Length %d.", m , j);
620
621         if (!j) {                                                       /* Read ID failed. Happens in 1-bit mode on PP */
622                 udelay(SW_TIMEOUT);
623                 i = sw_read_packet(gameport, buf, SW_LENGTH, 0);        /* Retry reading packet */
624                 dbg("Init 2b: Mode %d. Length %d.", m, i);
625                 if (!i) goto fail2;
626                 udelay(SW_TIMEOUT);
627                 j = sw_read_packet(gameport, idbuf, SW_LENGTH, i);      /* Retry reading ID */
628                 dbg("Init 2c: ID Length %d.", j);
629         }
630
631         sw->type = -1;
632         k = SW_FAIL;                                                    /* Try SW_FAIL times */
633         l = 0;
634
635         do {
636                 k--;
637                 udelay(SW_TIMEOUT);
638                 i = sw_read_packet(gameport, buf, SW_LENGTH, 0);        /* Read data packet */
639                 dbg("Init 3: Mode %d. Length %d. Last %d. Tries %d.", m, i, l, k);
640
641                 if (i > l) {                                            /* Longer? As we can only lose bits, it makes */
642                                                                         /* no sense to try detection for a packet shorter */
643                         l = i;                                          /* than the previous one */
644
645                         sw->number = 1;
646                         sw->gameport = gameport;
647                         sw->length = i;
648                         sw->bits = m;
649
650                         dbg("Init 3a: Case %d.\n", i * m);
651
652                         switch (i * m) {
653                                 case 60:
654                                         sw->number++;
655                                 case 45:                                /* Ambiguous packet length */
656                                         if (j <= 40) {                  /* ID length less or eq 40 -> FSP */
657                                 case 43:
658                                                 sw->type = SW_ID_FSP;
659                                                 break;
660                                         }
661                                         sw->number++;
662                                 case 30:
663                                         sw->number++;
664                                 case 15:
665                                         sw->type = SW_ID_GP;
666                                         break;
667                                 case 33:
668                                 case 31:
669                                         sw->type = SW_ID_FFW;
670                                         break;
671                                 case 48:                                /* Ambiguous */
672                                         if (j == 14) {                  /* ID length 14*3 -> FFP */
673                                                 sw->type = SW_ID_FFP;
674                                                 sprintf(comment, " [AC %s]", sw_get_bits(idbuf,38,1,3) ? "off" : "on");
675                                         } else
676                                         sw->type = SW_ID_PP;
677                                         break;
678                                 case 66:
679                                         sw->bits = 3;
680                                 case 198:
681                                         sw->length = 22;
682                                 case 64:
683                                         sw->type = SW_ID_3DP;
684                                         if (j == 160) sw_3dp_id(idbuf, comment);
685                                         break;
686                         }
687                 }
688
689         } while (k && (sw->type == -1));
690
691         if (sw->type == -1) {
692                 printk(KERN_WARNING "sidewinder.c: unknown joystick device detected "
693                         "on %s, contact <vojtech@ucw.cz>\n", gameport->phys);
694                 sw_print_packet("ID", j * 3, idbuf, 3);
695                 sw_print_packet("Data", i * m, buf, m);
696                 goto fail2;
697         }
698
699 #ifdef SW_DEBUG
700         sw_print_packet("ID", j * 3, idbuf, 3);
701         sw_print_packet("Data", i * m, buf, m);
702 #endif
703
704         k = i;
705         l = j;
706
707         for (i = 0; i < sw->number; i++) {
708                 int bits, code;
709
710                 sprintf(sw->name, "Microsoft SideWinder %s", sw_name[sw->type]);
711                 sprintf(sw->phys[i], "%s/input%d", gameport->phys, i);
712
713                 sw->dev[i].private = sw;
714
715                 sw->dev[i].open = sw_open;
716                 sw->dev[i].close = sw_close;
717
718                 sw->dev[i].name = sw->name;
719                 sw->dev[i].phys = sw->phys[i];
720                 sw->dev[i].id.bustype = BUS_GAMEPORT;
721                 sw->dev[i].id.vendor = GAMEPORT_ID_VENDOR_MICROSOFT;
722                 sw->dev[i].id.product = sw->type;
723                 sw->dev[i].id.version = 0x0100;
724
725                 sw->dev[i].evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
726
727                 for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
728                         code = sw_abs[sw->type][j];
729                         set_bit(code, sw->dev[i].absbit);
730                         sw->dev[i].absmax[code] = (1 << bits) - 1;
731                         sw->dev[i].absmin[code] = (bits == 1) ? -1 : 0;
732                         sw->dev[i].absfuzz[code] = ((bits >> 1) >= 2) ? (1 << ((bits >> 1) - 2)) : 0;
733                         if (code != ABS_THROTTLE)
734                                 sw->dev[i].absflat[code] = (bits >= 5) ? (1 << (bits - 5)) : 0;
735                 }
736
737                 for (j = 0; (code = sw_btn[sw->type][j]); j++)
738                         set_bit(code, sw->dev[i].keybit);
739
740                 input_register_device(sw->dev + i);
741                 printk(KERN_INFO "input: %s%s on %s [%d-bit id %d data %d]\n",
742                         sw->name, comment, gameport->phys, m, l, k);
743         }
744
745         return;
746 fail2:  gameport_close(gameport);
747 fail1:  kfree(sw);
748         kfree(buf);
749         kfree(idbuf);
750 }
751
752 static void sw_disconnect(struct gameport *gameport)
753 {
754         int i;
755
756         struct sw *sw = gameport->private;
757         for (i = 0; i < sw->number; i++)
758                 input_unregister_device(sw->dev + i);
759         gameport_close(gameport);
760         kfree(sw);
761 }
762
763 static struct gameport_dev sw_dev = {
764         .connect =      sw_connect,
765         .disconnect =   sw_disconnect,
766 };
767
768 int __init sw_init(void)
769 {
770         gameport_register_device(&sw_dev);
771         return 0;
772 }
773
774 void __exit sw_exit(void)
775 {
776         gameport_unregister_device(&sw_dev);
777 }
778
779 module_init(sw_init);
780 module_exit(sw_exit);