OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [linux/] [linux-2.4/] [drivers/] [char/] [joystick/] [sidewinder.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.