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