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 ]

Diff markup

Differences between /linux/drivers/input/joystick/sidewinder.c (Version 2.6.31.13) and /linux/drivers/input/joystick/sidewinder.c (Version 2.6.25.8)


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