18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci *  Copyright (c) 1998-2005 Vojtech Pavlik
48c2ecf20Sopenharmony_ci */
58c2ecf20Sopenharmony_ci
68c2ecf20Sopenharmony_ci/*
78c2ecf20Sopenharmony_ci * Microsoft SideWinder joystick family driver for Linux
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci/*
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/delay.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/module.h>
168c2ecf20Sopenharmony_ci#include <linux/slab.h>
178c2ecf20Sopenharmony_ci#include <linux/input.h>
188c2ecf20Sopenharmony_ci#include <linux/gameport.h>
198c2ecf20Sopenharmony_ci#include <linux/jiffies.h>
208c2ecf20Sopenharmony_ci
218c2ecf20Sopenharmony_ci#define DRIVER_DESC	"Microsoft SideWinder joystick family driver"
228c2ecf20Sopenharmony_ci
238c2ecf20Sopenharmony_ciMODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
248c2ecf20Sopenharmony_ciMODULE_DESCRIPTION(DRIVER_DESC);
258c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/*
288c2ecf20Sopenharmony_ci * These are really magic values. Changing them can make a problem go away,
298c2ecf20Sopenharmony_ci * as well as break everything.
308c2ecf20Sopenharmony_ci */
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci#undef SW_DEBUG
338c2ecf20Sopenharmony_ci#undef SW_DEBUG_DATA
348c2ecf20Sopenharmony_ci
358c2ecf20Sopenharmony_ci#define SW_START	600	/* The time we wait for the first bit [600 us] */
368c2ecf20Sopenharmony_ci#define SW_STROBE	60	/* Max time per bit [60 us] */
378c2ecf20Sopenharmony_ci#define SW_TIMEOUT	6	/* Wait for everything to settle [6 ms] */
388c2ecf20Sopenharmony_ci#define SW_KICK		45	/* Wait after A0 fall till kick [45 us] */
398c2ecf20Sopenharmony_ci#define SW_END		8	/* Number of bits before end of packet to kick */
408c2ecf20Sopenharmony_ci#define SW_FAIL		16	/* Number of packet read errors to fail and reinitialize */
418c2ecf20Sopenharmony_ci#define SW_BAD		2	/* Number of packet read errors to switch off 3d Pro optimization */
428c2ecf20Sopenharmony_ci#define SW_OK		64	/* Number of packet read successes to switch optimization back on */
438c2ecf20Sopenharmony_ci#define SW_LENGTH	512	/* Max number of bits in a packet */
448c2ecf20Sopenharmony_ci
458c2ecf20Sopenharmony_ci#ifdef SW_DEBUG
468c2ecf20Sopenharmony_ci#define dbg(format, arg...) printk(KERN_DEBUG __FILE__ ": " format "\n" , ## arg)
478c2ecf20Sopenharmony_ci#else
488c2ecf20Sopenharmony_ci#define dbg(format, arg...) do {} while (0)
498c2ecf20Sopenharmony_ci#endif
508c2ecf20Sopenharmony_ci
518c2ecf20Sopenharmony_ci/*
528c2ecf20Sopenharmony_ci * SideWinder joystick types ...
538c2ecf20Sopenharmony_ci */
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define SW_ID_3DP	0
568c2ecf20Sopenharmony_ci#define SW_ID_GP	1
578c2ecf20Sopenharmony_ci#define SW_ID_PP	2
588c2ecf20Sopenharmony_ci#define SW_ID_FFP	3
598c2ecf20Sopenharmony_ci#define SW_ID_FSP	4
608c2ecf20Sopenharmony_ci#define SW_ID_FFW	5
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci/*
638c2ecf20Sopenharmony_ci * Names, buttons, axes ...
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci
668c2ecf20Sopenharmony_cistatic char *sw_name[] = {	"3D Pro", "GamePad", "Precision Pro", "Force Feedback Pro", "FreeStyle Pro",
678c2ecf20Sopenharmony_ci				"Force Feedback Wheel" };
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_cistatic char sw_abs[][7] = {
708c2ecf20Sopenharmony_ci	{ ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
718c2ecf20Sopenharmony_ci	{ ABS_X, ABS_Y },
728c2ecf20Sopenharmony_ci	{ ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
738c2ecf20Sopenharmony_ci	{ ABS_X, ABS_Y, ABS_RZ, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
748c2ecf20Sopenharmony_ci	{ ABS_X, ABS_Y,         ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y },
758c2ecf20Sopenharmony_ci	{ ABS_RX, ABS_RUDDER,   ABS_THROTTLE }};
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_cistatic char sw_bit[][7] = {
788c2ecf20Sopenharmony_ci	{ 10, 10,  9, 10,  1,  1 },
798c2ecf20Sopenharmony_ci	{  1,  1                 },
808c2ecf20Sopenharmony_ci	{ 10, 10,  6,  7,  1,  1 },
818c2ecf20Sopenharmony_ci	{ 10, 10,  6,  7,  1,  1 },
828c2ecf20Sopenharmony_ci	{ 10, 10,  6,  1,  1     },
838c2ecf20Sopenharmony_ci	{ 10,  7,  7,  1,  1     }};
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_cistatic short sw_btn[][12] = {
868c2ecf20Sopenharmony_ci	{ BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_MODE },
878c2ecf20Sopenharmony_ci	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE },
888c2ecf20Sopenharmony_ci	{ BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
898c2ecf20Sopenharmony_ci	{ BTN_TRIGGER, BTN_THUMB, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_SELECT },
908c2ecf20Sopenharmony_ci	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z, BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT },
918c2ecf20Sopenharmony_ci	{ BTN_TRIGGER, BTN_TOP, BTN_THUMB, BTN_THUMB2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4 }};
928c2ecf20Sopenharmony_ci
938c2ecf20Sopenharmony_cistatic struct {
948c2ecf20Sopenharmony_ci	int x;
958c2ecf20Sopenharmony_ci	int y;
968c2ecf20Sopenharmony_ci} sw_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct sw {
998c2ecf20Sopenharmony_ci	struct gameport *gameport;
1008c2ecf20Sopenharmony_ci	struct input_dev *dev[4];
1018c2ecf20Sopenharmony_ci	char name[64];
1028c2ecf20Sopenharmony_ci	char phys[4][32];
1038c2ecf20Sopenharmony_ci	int length;
1048c2ecf20Sopenharmony_ci	int type;
1058c2ecf20Sopenharmony_ci	int bits;
1068c2ecf20Sopenharmony_ci	int number;
1078c2ecf20Sopenharmony_ci	int fail;
1088c2ecf20Sopenharmony_ci	int ok;
1098c2ecf20Sopenharmony_ci	int reads;
1108c2ecf20Sopenharmony_ci	int bads;
1118c2ecf20Sopenharmony_ci};
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/*
1148c2ecf20Sopenharmony_ci * sw_read_packet() is a function which reads either a data packet, or an
1158c2ecf20Sopenharmony_ci * identification packet from a SideWinder joystick. The protocol is very,
1168c2ecf20Sopenharmony_ci * very, very braindamaged. Microsoft patented it in US patent #5628686.
1178c2ecf20Sopenharmony_ci */
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int sw_read_packet(struct gameport *gameport, unsigned char *buf, int length, int id)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	unsigned long flags;
1228c2ecf20Sopenharmony_ci	int timeout, bitout, sched, i, kick, start, strobe;
1238c2ecf20Sopenharmony_ci	unsigned char pending, u, v;
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci	i = -id;						/* Don't care about data, only want ID */
1268c2ecf20Sopenharmony_ci	timeout = id ? gameport_time(gameport, SW_TIMEOUT * 1000) : 0; /* Set up global timeout for ID packet */
1278c2ecf20Sopenharmony_ci	kick = id ? gameport_time(gameport, SW_KICK) : 0;	/* Set up kick timeout for ID packet */
1288c2ecf20Sopenharmony_ci	start = gameport_time(gameport, SW_START);
1298c2ecf20Sopenharmony_ci	strobe = gameport_time(gameport, SW_STROBE);
1308c2ecf20Sopenharmony_ci	bitout = start;
1318c2ecf20Sopenharmony_ci	pending = 0;
1328c2ecf20Sopenharmony_ci	sched = 0;
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci        local_irq_save(flags);					/* Quiet, please */
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	gameport_trigger(gameport);				/* Trigger */
1378c2ecf20Sopenharmony_ci	v = gameport_read(gameport);
1388c2ecf20Sopenharmony_ci
1398c2ecf20Sopenharmony_ci	do {
1408c2ecf20Sopenharmony_ci		bitout--;
1418c2ecf20Sopenharmony_ci		u = v;
1428c2ecf20Sopenharmony_ci		v = gameport_read(gameport);
1438c2ecf20Sopenharmony_ci	} while (!(~v & u & 0x10) && (bitout > 0));		/* Wait for first falling edge on clock */
1448c2ecf20Sopenharmony_ci
1458c2ecf20Sopenharmony_ci	if (bitout > 0)
1468c2ecf20Sopenharmony_ci		bitout = strobe;				/* Extend time if not timed out */
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	while ((timeout > 0 || bitout > 0) && (i < length)) {
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci		timeout--;
1518c2ecf20Sopenharmony_ci		bitout--;					/* Decrement timers */
1528c2ecf20Sopenharmony_ci		sched--;
1538c2ecf20Sopenharmony_ci
1548c2ecf20Sopenharmony_ci		u = v;
1558c2ecf20Sopenharmony_ci		v = gameport_read(gameport);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci		if ((~u & v & 0x10) && (bitout > 0)) {		/* Rising edge on clock - data bit */
1588c2ecf20Sopenharmony_ci			if (i >= 0)				/* Want this data */
1598c2ecf20Sopenharmony_ci				buf[i] = v >> 5;		/* Store it */
1608c2ecf20Sopenharmony_ci			i++;					/* Advance index */
1618c2ecf20Sopenharmony_ci			bitout = strobe;			/* Extend timeout for next bit */
1628c2ecf20Sopenharmony_ci		}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci		if (kick && (~v & u & 0x01)) {			/* Falling edge on axis 0 */
1658c2ecf20Sopenharmony_ci			sched = kick;				/* Schedule second trigger */
1668c2ecf20Sopenharmony_ci			kick = 0;				/* Don't schedule next time on falling edge */
1678c2ecf20Sopenharmony_ci			pending = 1;				/* Mark schedule */
1688c2ecf20Sopenharmony_ci		}
1698c2ecf20Sopenharmony_ci
1708c2ecf20Sopenharmony_ci		if (pending && sched < 0 && (i > -SW_END)) {	/* Second trigger time */
1718c2ecf20Sopenharmony_ci			gameport_trigger(gameport);		/* Trigger */
1728c2ecf20Sopenharmony_ci			bitout = start;				/* Long bit timeout */
1738c2ecf20Sopenharmony_ci			pending = 0;				/* Unmark schedule */
1748c2ecf20Sopenharmony_ci			timeout = 0;				/* Switch from global to bit timeouts */
1758c2ecf20Sopenharmony_ci		}
1768c2ecf20Sopenharmony_ci	}
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	local_irq_restore(flags);					/* Done - relax */
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci#ifdef SW_DEBUG_DATA
1818c2ecf20Sopenharmony_ci	{
1828c2ecf20Sopenharmony_ci		int j;
1838c2ecf20Sopenharmony_ci		printk(KERN_DEBUG "sidewinder.c: Read %d triplets. [", i);
1848c2ecf20Sopenharmony_ci		for (j = 0; j < i; j++) printk("%d", buf[j]);
1858c2ecf20Sopenharmony_ci		printk("]\n");
1868c2ecf20Sopenharmony_ci	}
1878c2ecf20Sopenharmony_ci#endif
1888c2ecf20Sopenharmony_ci
1898c2ecf20Sopenharmony_ci	return i;
1908c2ecf20Sopenharmony_ci}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci/*
1938c2ecf20Sopenharmony_ci * sw_get_bits() and GB() compose bits from the triplet buffer into a __u64.
1948c2ecf20Sopenharmony_ci * Parameter 'pos' is bit number inside packet where to start at, 'num' is number
1958c2ecf20Sopenharmony_ci * of bits to be read, 'shift' is offset in the resulting __u64 to start at, bits
1968c2ecf20Sopenharmony_ci * is number of bits per triplet.
1978c2ecf20Sopenharmony_ci */
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_ci#define GB(pos,num) sw_get_bits(buf, pos, num, sw->bits)
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_cistatic __u64 sw_get_bits(unsigned char *buf, int pos, int num, char bits)
2028c2ecf20Sopenharmony_ci{
2038c2ecf20Sopenharmony_ci	__u64 data = 0;
2048c2ecf20Sopenharmony_ci	int tri = pos % bits;						/* Start position */
2058c2ecf20Sopenharmony_ci	int i   = pos / bits;
2068c2ecf20Sopenharmony_ci	int bit = 0;
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	while (num--) {
2098c2ecf20Sopenharmony_ci		data |= (__u64)((buf[i] >> tri++) & 1) << bit++;	/* Transfer bit */
2108c2ecf20Sopenharmony_ci		if (tri == bits) {
2118c2ecf20Sopenharmony_ci			i++;						/* Next triplet */
2128c2ecf20Sopenharmony_ci			tri = 0;
2138c2ecf20Sopenharmony_ci		}
2148c2ecf20Sopenharmony_ci	}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	return data;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci/*
2208c2ecf20Sopenharmony_ci * sw_init_digital() initializes a SideWinder 3D Pro joystick
2218c2ecf20Sopenharmony_ci * into digital mode.
2228c2ecf20Sopenharmony_ci */
2238c2ecf20Sopenharmony_ci
2248c2ecf20Sopenharmony_cistatic void sw_init_digital(struct gameport *gameport)
2258c2ecf20Sopenharmony_ci{
2268c2ecf20Sopenharmony_ci	static const int seq[] = { 140, 140+725, 140+300, 0 };
2278c2ecf20Sopenharmony_ci	unsigned long flags;
2288c2ecf20Sopenharmony_ci	int i, t;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci        local_irq_save(flags);
2318c2ecf20Sopenharmony_ci
2328c2ecf20Sopenharmony_ci	i = 0;
2338c2ecf20Sopenharmony_ci        do {
2348c2ecf20Sopenharmony_ci                gameport_trigger(gameport);			/* Trigger */
2358c2ecf20Sopenharmony_ci		t = gameport_time(gameport, SW_TIMEOUT * 1000);
2368c2ecf20Sopenharmony_ci		while ((gameport_read(gameport) & 1) && t) t--;	/* Wait for axis to fall back to 0 */
2378c2ecf20Sopenharmony_ci                udelay(seq[i]);					/* Delay magic time */
2388c2ecf20Sopenharmony_ci        } while (seq[++i]);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci	gameport_trigger(gameport);				/* Last trigger */
2418c2ecf20Sopenharmony_ci
2428c2ecf20Sopenharmony_ci	local_irq_restore(flags);
2438c2ecf20Sopenharmony_ci}
2448c2ecf20Sopenharmony_ci
2458c2ecf20Sopenharmony_ci/*
2468c2ecf20Sopenharmony_ci * sw_parity() computes parity of __u64
2478c2ecf20Sopenharmony_ci */
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_cistatic int sw_parity(__u64 t)
2508c2ecf20Sopenharmony_ci{
2518c2ecf20Sopenharmony_ci	int x = t ^ (t >> 32);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	x ^= x >> 16;
2548c2ecf20Sopenharmony_ci	x ^= x >> 8;
2558c2ecf20Sopenharmony_ci	x ^= x >> 4;
2568c2ecf20Sopenharmony_ci	x ^= x >> 2;
2578c2ecf20Sopenharmony_ci	x ^= x >> 1;
2588c2ecf20Sopenharmony_ci	return x & 1;
2598c2ecf20Sopenharmony_ci}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci/*
2628c2ecf20Sopenharmony_ci * sw_ccheck() checks synchronization bits and computes checksum of nibbles.
2638c2ecf20Sopenharmony_ci */
2648c2ecf20Sopenharmony_ci
2658c2ecf20Sopenharmony_cistatic int sw_check(__u64 t)
2668c2ecf20Sopenharmony_ci{
2678c2ecf20Sopenharmony_ci	unsigned char sum = 0;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	if ((t & 0x8080808080808080ULL) ^ 0x80)			/* Sync */
2708c2ecf20Sopenharmony_ci		return -1;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci	while (t) {						/* Sum */
2738c2ecf20Sopenharmony_ci		sum += t & 0xf;
2748c2ecf20Sopenharmony_ci		t >>= 4;
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci
2778c2ecf20Sopenharmony_ci	return sum & 0xf;
2788c2ecf20Sopenharmony_ci}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci/*
2818c2ecf20Sopenharmony_ci * sw_parse() analyzes SideWinder joystick data, and writes the results into
2828c2ecf20Sopenharmony_ci * the axes and buttons arrays.
2838c2ecf20Sopenharmony_ci */
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic int sw_parse(unsigned char *buf, struct sw *sw)
2868c2ecf20Sopenharmony_ci{
2878c2ecf20Sopenharmony_ci	int hat, i, j;
2888c2ecf20Sopenharmony_ci	struct input_dev *dev;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	switch (sw->type) {
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci		case SW_ID_3DP:
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci			if (sw_check(GB(0,64)) || (hat = (GB(6,1) << 3) | GB(60,3)) > 8)
2958c2ecf20Sopenharmony_ci				return -1;
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ci			dev = sw->dev[0];
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_X,        (GB( 3,3) << 7) | GB(16,7));
3008c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_Y,        (GB( 0,3) << 7) | GB(24,7));
3018c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_RZ,       (GB(35,2) << 7) | GB(40,7));
3028c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_THROTTLE, (GB(32,3) << 7) | GB(48,7));
3038c2ecf20Sopenharmony_ci
3048c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
3058c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
3068c2ecf20Sopenharmony_ci
3078c2ecf20Sopenharmony_ci			for (j = 0; j < 7; j++)
3088c2ecf20Sopenharmony_ci				input_report_key(dev, sw_btn[SW_ID_3DP][j], !GB(j+8,1));
3098c2ecf20Sopenharmony_ci
3108c2ecf20Sopenharmony_ci			input_report_key(dev, BTN_BASE4, !GB(38,1));
3118c2ecf20Sopenharmony_ci			input_report_key(dev, BTN_BASE5, !GB(37,1));
3128c2ecf20Sopenharmony_ci
3138c2ecf20Sopenharmony_ci			input_sync(dev);
3148c2ecf20Sopenharmony_ci
3158c2ecf20Sopenharmony_ci			return 0;
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci		case SW_ID_GP:
3188c2ecf20Sopenharmony_ci
3198c2ecf20Sopenharmony_ci			for (i = 0; i < sw->number; i ++) {
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_ci				if (sw_parity(GB(i*15,15)))
3228c2ecf20Sopenharmony_ci					return -1;
3238c2ecf20Sopenharmony_ci
3248c2ecf20Sopenharmony_ci				input_report_abs(sw->dev[i], ABS_X, GB(i*15+3,1) - GB(i*15+2,1));
3258c2ecf20Sopenharmony_ci				input_report_abs(sw->dev[i], ABS_Y, GB(i*15+0,1) - GB(i*15+1,1));
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci				for (j = 0; j < 10; j++)
3288c2ecf20Sopenharmony_ci					input_report_key(sw->dev[i], sw_btn[SW_ID_GP][j], !GB(i*15+j+4,1));
3298c2ecf20Sopenharmony_ci
3308c2ecf20Sopenharmony_ci				input_sync(sw->dev[i]);
3318c2ecf20Sopenharmony_ci			}
3328c2ecf20Sopenharmony_ci
3338c2ecf20Sopenharmony_ci			return 0;
3348c2ecf20Sopenharmony_ci
3358c2ecf20Sopenharmony_ci		case SW_ID_PP:
3368c2ecf20Sopenharmony_ci		case SW_ID_FFP:
3378c2ecf20Sopenharmony_ci
3388c2ecf20Sopenharmony_ci			if (!sw_parity(GB(0,48)) || (hat = GB(42,4)) > 8)
3398c2ecf20Sopenharmony_ci				return -1;
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci			dev = sw->dev[0];
3428c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_X,        GB( 9,10));
3438c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_Y,        GB(19,10));
3448c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_RZ,       GB(36, 6));
3458c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_THROTTLE, GB(29, 7));
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
3488c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci			for (j = 0; j < 9; j++)
3518c2ecf20Sopenharmony_ci				input_report_key(dev, sw_btn[SW_ID_PP][j], !GB(j,1));
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci			input_sync(dev);
3548c2ecf20Sopenharmony_ci
3558c2ecf20Sopenharmony_ci			return 0;
3568c2ecf20Sopenharmony_ci
3578c2ecf20Sopenharmony_ci		case SW_ID_FSP:
3588c2ecf20Sopenharmony_ci
3598c2ecf20Sopenharmony_ci			if (!sw_parity(GB(0,43)) || (hat = GB(28,4)) > 8)
3608c2ecf20Sopenharmony_ci				return -1;
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci			dev = sw->dev[0];
3638c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_X,        GB( 0,10));
3648c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_Y,        GB(16,10));
3658c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_THROTTLE, GB(32, 6));
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_HAT0X, sw_hat_to_axis[hat].x);
3688c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_HAT0Y, sw_hat_to_axis[hat].y);
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci			for (j = 0; j < 6; j++)
3718c2ecf20Sopenharmony_ci				input_report_key(dev, sw_btn[SW_ID_FSP][j], !GB(j+10,1));
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci			input_report_key(dev, BTN_TR,     !GB(26,1));
3748c2ecf20Sopenharmony_ci			input_report_key(dev, BTN_START,  !GB(27,1));
3758c2ecf20Sopenharmony_ci			input_report_key(dev, BTN_MODE,   !GB(38,1));
3768c2ecf20Sopenharmony_ci			input_report_key(dev, BTN_SELECT, !GB(39,1));
3778c2ecf20Sopenharmony_ci
3788c2ecf20Sopenharmony_ci			input_sync(dev);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci			return 0;
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci		case SW_ID_FFW:
3838c2ecf20Sopenharmony_ci
3848c2ecf20Sopenharmony_ci			if (!sw_parity(GB(0,33)))
3858c2ecf20Sopenharmony_ci				return -1;
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_ci			dev = sw->dev[0];
3888c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_RX,       GB( 0,10));
3898c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_RUDDER,   GB(10, 6));
3908c2ecf20Sopenharmony_ci			input_report_abs(dev, ABS_THROTTLE, GB(16, 6));
3918c2ecf20Sopenharmony_ci
3928c2ecf20Sopenharmony_ci			for (j = 0; j < 8; j++)
3938c2ecf20Sopenharmony_ci				input_report_key(dev, sw_btn[SW_ID_FFW][j], !GB(j+22,1));
3948c2ecf20Sopenharmony_ci
3958c2ecf20Sopenharmony_ci			input_sync(dev);
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_ci			return 0;
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return -1;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_ci/*
4048c2ecf20Sopenharmony_ci * sw_read() reads SideWinder joystick data, and reinitializes
4058c2ecf20Sopenharmony_ci * the joystick in case of persistent problems. This is the function that is
4068c2ecf20Sopenharmony_ci * called from the generic code to poll the joystick.
4078c2ecf20Sopenharmony_ci */
4088c2ecf20Sopenharmony_ci
4098c2ecf20Sopenharmony_cistatic int sw_read(struct sw *sw)
4108c2ecf20Sopenharmony_ci{
4118c2ecf20Sopenharmony_ci	unsigned char buf[SW_LENGTH];
4128c2ecf20Sopenharmony_ci	int i;
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	i = sw_read_packet(sw->gameport, buf, sw->length, 0);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (sw->type == SW_ID_3DP && sw->length == 66 && i != 66) {		/* Broken packet, try to fix */
4178c2ecf20Sopenharmony_ci
4188c2ecf20Sopenharmony_ci		if (i == 64 && !sw_check(sw_get_bits(buf,0,64,1))) {		/* Last init failed, 1 bit mode */
4198c2ecf20Sopenharmony_ci			printk(KERN_WARNING "sidewinder.c: Joystick in wrong mode on %s"
4208c2ecf20Sopenharmony_ci				" - going to reinitialize.\n", sw->gameport->phys);
4218c2ecf20Sopenharmony_ci			sw->fail = SW_FAIL;					/* Reinitialize */
4228c2ecf20Sopenharmony_ci			i = 128;						/* Bogus value */
4238c2ecf20Sopenharmony_ci		}
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci		if (i < 66 && GB(0,64) == GB(i*3-66,64))			/* 1 == 3 */
4268c2ecf20Sopenharmony_ci			i = 66;							/* Everything is fine */
4278c2ecf20Sopenharmony_ci
4288c2ecf20Sopenharmony_ci		if (i < 66 && GB(0,64) == GB(66,64))				/* 1 == 2 */
4298c2ecf20Sopenharmony_ci			i = 66;							/* Everything is fine */
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci		if (i < 66 && GB(i*3-132,64) == GB(i*3-66,64)) {		/* 2 == 3 */
4328c2ecf20Sopenharmony_ci			memmove(buf, buf + i - 22, 22);				/* Move data */
4338c2ecf20Sopenharmony_ci			i = 66;							/* Carry on */
4348c2ecf20Sopenharmony_ci		}
4358c2ecf20Sopenharmony_ci	}
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	if (i == sw->length && !sw_parse(buf, sw)) {				/* Parse data */
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		sw->fail = 0;
4408c2ecf20Sopenharmony_ci		sw->ok++;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci		if (sw->type == SW_ID_3DP && sw->length == 66			/* Many packets OK */
4438c2ecf20Sopenharmony_ci			&& sw->ok > SW_OK) {
4448c2ecf20Sopenharmony_ci
4458c2ecf20Sopenharmony_ci			printk(KERN_INFO "sidewinder.c: No more trouble on %s"
4468c2ecf20Sopenharmony_ci				" - enabling optimization again.\n", sw->gameport->phys);
4478c2ecf20Sopenharmony_ci			sw->length = 22;
4488c2ecf20Sopenharmony_ci		}
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci		return 0;
4518c2ecf20Sopenharmony_ci	}
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_ci	sw->ok = 0;
4548c2ecf20Sopenharmony_ci	sw->fail++;
4558c2ecf20Sopenharmony_ci
4568c2ecf20Sopenharmony_ci	if (sw->type == SW_ID_3DP && sw->length == 22 && sw->fail > SW_BAD) {	/* Consecutive bad packets */
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci		printk(KERN_INFO "sidewinder.c: Many bit errors on %s"
4598c2ecf20Sopenharmony_ci			" - disabling optimization.\n", sw->gameport->phys);
4608c2ecf20Sopenharmony_ci		sw->length = 66;
4618c2ecf20Sopenharmony_ci	}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_ci	if (sw->fail < SW_FAIL)
4648c2ecf20Sopenharmony_ci		return -1;							/* Not enough, don't reinitialize yet */
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_ci	printk(KERN_WARNING "sidewinder.c: Too many bit errors on %s"
4678c2ecf20Sopenharmony_ci		" - reinitializing joystick.\n", sw->gameport->phys);
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci	if (!i && sw->type == SW_ID_3DP) {					/* 3D Pro can be in analog mode */
4708c2ecf20Sopenharmony_ci		mdelay(3 * SW_TIMEOUT);
4718c2ecf20Sopenharmony_ci		sw_init_digital(sw->gameport);
4728c2ecf20Sopenharmony_ci	}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_ci	mdelay(SW_TIMEOUT);
4758c2ecf20Sopenharmony_ci	i = sw_read_packet(sw->gameport, buf, SW_LENGTH, 0);			/* Read normal data packet */
4768c2ecf20Sopenharmony_ci	mdelay(SW_TIMEOUT);
4778c2ecf20Sopenharmony_ci	sw_read_packet(sw->gameport, buf, SW_LENGTH, i);			/* Read ID packet, this initializes the stick */
4788c2ecf20Sopenharmony_ci
4798c2ecf20Sopenharmony_ci	sw->fail = SW_FAIL;
4808c2ecf20Sopenharmony_ci
4818c2ecf20Sopenharmony_ci	return -1;
4828c2ecf20Sopenharmony_ci}
4838c2ecf20Sopenharmony_ci
4848c2ecf20Sopenharmony_cistatic void sw_poll(struct gameport *gameport)
4858c2ecf20Sopenharmony_ci{
4868c2ecf20Sopenharmony_ci	struct sw *sw = gameport_get_drvdata(gameport);
4878c2ecf20Sopenharmony_ci
4888c2ecf20Sopenharmony_ci	sw->reads++;
4898c2ecf20Sopenharmony_ci	if (sw_read(sw))
4908c2ecf20Sopenharmony_ci		sw->bads++;
4918c2ecf20Sopenharmony_ci}
4928c2ecf20Sopenharmony_ci
4938c2ecf20Sopenharmony_cistatic int sw_open(struct input_dev *dev)
4948c2ecf20Sopenharmony_ci{
4958c2ecf20Sopenharmony_ci	struct sw *sw = input_get_drvdata(dev);
4968c2ecf20Sopenharmony_ci
4978c2ecf20Sopenharmony_ci	gameport_start_polling(sw->gameport);
4988c2ecf20Sopenharmony_ci	return 0;
4998c2ecf20Sopenharmony_ci}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_cistatic void sw_close(struct input_dev *dev)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	struct sw *sw = input_get_drvdata(dev);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci	gameport_stop_polling(sw->gameport);
5068c2ecf20Sopenharmony_ci}
5078c2ecf20Sopenharmony_ci
5088c2ecf20Sopenharmony_ci/*
5098c2ecf20Sopenharmony_ci * sw_print_packet() prints the contents of a SideWinder packet.
5108c2ecf20Sopenharmony_ci */
5118c2ecf20Sopenharmony_ci
5128c2ecf20Sopenharmony_cistatic void sw_print_packet(char *name, int length, unsigned char *buf, char bits)
5138c2ecf20Sopenharmony_ci{
5148c2ecf20Sopenharmony_ci	int i;
5158c2ecf20Sopenharmony_ci
5168c2ecf20Sopenharmony_ci	printk(KERN_INFO "sidewinder.c: %s packet, %d bits. [", name, length);
5178c2ecf20Sopenharmony_ci	for (i = (((length + 3) >> 2) - 1); i >= 0; i--)
5188c2ecf20Sopenharmony_ci		printk("%x", (int)sw_get_bits(buf, i << 2, 4, bits));
5198c2ecf20Sopenharmony_ci	printk("]\n");
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_ci/*
5238c2ecf20Sopenharmony_ci * sw_3dp_id() translates the 3DP id into a human legible string.
5248c2ecf20Sopenharmony_ci * Unfortunately I don't know how to do this for the other SW types.
5258c2ecf20Sopenharmony_ci */
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic void sw_3dp_id(unsigned char *buf, char *comment, size_t size)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	int i;
5308c2ecf20Sopenharmony_ci	char pnp[8], rev[9];
5318c2ecf20Sopenharmony_ci
5328c2ecf20Sopenharmony_ci	for (i = 0; i < 7; i++)						/* ASCII PnP ID */
5338c2ecf20Sopenharmony_ci		pnp[i] = sw_get_bits(buf, 24+8*i, 8, 1);
5348c2ecf20Sopenharmony_ci
5358c2ecf20Sopenharmony_ci	for (i = 0; i < 8; i++)						/* ASCII firmware revision */
5368c2ecf20Sopenharmony_ci		rev[i] = sw_get_bits(buf, 88+8*i, 8, 1);
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_ci	pnp[7] = rev[8] = 0;
5398c2ecf20Sopenharmony_ci
5408c2ecf20Sopenharmony_ci	snprintf(comment, size, " [PnP %d.%02d id %s rev %s]",
5418c2ecf20Sopenharmony_ci		(int) ((sw_get_bits(buf, 8, 6, 1) << 6) |		/* Two 6-bit values */
5428c2ecf20Sopenharmony_ci			sw_get_bits(buf, 16, 6, 1)) / 100,
5438c2ecf20Sopenharmony_ci		(int) ((sw_get_bits(buf, 8, 6, 1) << 6) |
5448c2ecf20Sopenharmony_ci			sw_get_bits(buf, 16, 6, 1)) % 100,
5458c2ecf20Sopenharmony_ci		 pnp, rev);
5468c2ecf20Sopenharmony_ci}
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci/*
5498c2ecf20Sopenharmony_ci * sw_guess_mode() checks the upper two button bits for toggling -
5508c2ecf20Sopenharmony_ci * indication of that the joystick is in 3-bit mode. This is documented
5518c2ecf20Sopenharmony_ci * behavior for 3DP ID packet, and for example the FSP does this in
5528c2ecf20Sopenharmony_ci * normal packets instead. Fun ...
5538c2ecf20Sopenharmony_ci */
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_cistatic int sw_guess_mode(unsigned char *buf, int len)
5568c2ecf20Sopenharmony_ci{
5578c2ecf20Sopenharmony_ci	int i;
5588c2ecf20Sopenharmony_ci	unsigned char xor = 0;
5598c2ecf20Sopenharmony_ci
5608c2ecf20Sopenharmony_ci	for (i = 1; i < len; i++)
5618c2ecf20Sopenharmony_ci		xor |= (buf[i - 1] ^ buf[i]) & 6;
5628c2ecf20Sopenharmony_ci
5638c2ecf20Sopenharmony_ci	return !!xor * 2 + 1;
5648c2ecf20Sopenharmony_ci}
5658c2ecf20Sopenharmony_ci
5668c2ecf20Sopenharmony_ci/*
5678c2ecf20Sopenharmony_ci * sw_connect() probes for SideWinder type joysticks.
5688c2ecf20Sopenharmony_ci */
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic int sw_connect(struct gameport *gameport, struct gameport_driver *drv)
5718c2ecf20Sopenharmony_ci{
5728c2ecf20Sopenharmony_ci	struct sw *sw;
5738c2ecf20Sopenharmony_ci	struct input_dev *input_dev;
5748c2ecf20Sopenharmony_ci	int i, j, k, l;
5758c2ecf20Sopenharmony_ci	int err = 0;
5768c2ecf20Sopenharmony_ci	unsigned char *buf = NULL;	/* [SW_LENGTH] */
5778c2ecf20Sopenharmony_ci	unsigned char *idbuf = NULL;	/* [SW_LENGTH] */
5788c2ecf20Sopenharmony_ci	unsigned char m = 1;
5798c2ecf20Sopenharmony_ci	char comment[40];
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	comment[0] = 0;
5828c2ecf20Sopenharmony_ci
5838c2ecf20Sopenharmony_ci	sw = kzalloc(sizeof(struct sw), GFP_KERNEL);
5848c2ecf20Sopenharmony_ci	buf = kmalloc(SW_LENGTH, GFP_KERNEL);
5858c2ecf20Sopenharmony_ci	idbuf = kmalloc(SW_LENGTH, GFP_KERNEL);
5868c2ecf20Sopenharmony_ci	if (!sw || !buf || !idbuf) {
5878c2ecf20Sopenharmony_ci		err = -ENOMEM;
5888c2ecf20Sopenharmony_ci		goto fail1;
5898c2ecf20Sopenharmony_ci	}
5908c2ecf20Sopenharmony_ci
5918c2ecf20Sopenharmony_ci	sw->gameport = gameport;
5928c2ecf20Sopenharmony_ci
5938c2ecf20Sopenharmony_ci	gameport_set_drvdata(gameport, sw);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
5968c2ecf20Sopenharmony_ci	if (err)
5978c2ecf20Sopenharmony_ci		goto fail1;
5988c2ecf20Sopenharmony_ci
5998c2ecf20Sopenharmony_ci	dbg("Init 0: Opened %s, io %#x, speed %d",
6008c2ecf20Sopenharmony_ci		gameport->phys, gameport->io, gameport->speed);
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	i = sw_read_packet(gameport, buf, SW_LENGTH, 0);		/* Read normal packet */
6038c2ecf20Sopenharmony_ci	msleep(SW_TIMEOUT);
6048c2ecf20Sopenharmony_ci	dbg("Init 1: Mode %d. Length %d.", m , i);
6058c2ecf20Sopenharmony_ci
6068c2ecf20Sopenharmony_ci	if (!i) {							/* No data. 3d Pro analog mode? */
6078c2ecf20Sopenharmony_ci		sw_init_digital(gameport);				/* Switch to digital */
6088c2ecf20Sopenharmony_ci		msleep(SW_TIMEOUT);
6098c2ecf20Sopenharmony_ci		i = sw_read_packet(gameport, buf, SW_LENGTH, 0);	/* Retry reading packet */
6108c2ecf20Sopenharmony_ci		msleep(SW_TIMEOUT);
6118c2ecf20Sopenharmony_ci		dbg("Init 1b: Length %d.", i);
6128c2ecf20Sopenharmony_ci		if (!i) {						/* No data -> FAIL */
6138c2ecf20Sopenharmony_ci			err = -ENODEV;
6148c2ecf20Sopenharmony_ci			goto fail2;
6158c2ecf20Sopenharmony_ci		}
6168c2ecf20Sopenharmony_ci	}
6178c2ecf20Sopenharmony_ci
6188c2ecf20Sopenharmony_ci	j = sw_read_packet(gameport, idbuf, SW_LENGTH, i);		/* Read ID. This initializes the stick */
6198c2ecf20Sopenharmony_ci	m |= sw_guess_mode(idbuf, j);					/* ID packet should carry mode info [3DP] */
6208c2ecf20Sopenharmony_ci	dbg("Init 2: Mode %d. ID Length %d.", m, j);
6218c2ecf20Sopenharmony_ci
6228c2ecf20Sopenharmony_ci	if (j <= 0) {							/* Read ID failed. Happens in 1-bit mode on PP */
6238c2ecf20Sopenharmony_ci		msleep(SW_TIMEOUT);
6248c2ecf20Sopenharmony_ci		i = sw_read_packet(gameport, buf, SW_LENGTH, 0);	/* Retry reading packet */
6258c2ecf20Sopenharmony_ci		m |= sw_guess_mode(buf, i);
6268c2ecf20Sopenharmony_ci		dbg("Init 2b: Mode %d. Length %d.", m, i);
6278c2ecf20Sopenharmony_ci		if (!i) {
6288c2ecf20Sopenharmony_ci			err = -ENODEV;
6298c2ecf20Sopenharmony_ci			goto fail2;
6308c2ecf20Sopenharmony_ci		}
6318c2ecf20Sopenharmony_ci		msleep(SW_TIMEOUT);
6328c2ecf20Sopenharmony_ci		j = sw_read_packet(gameport, idbuf, SW_LENGTH, i);	/* Retry reading ID */
6338c2ecf20Sopenharmony_ci		dbg("Init 2c: ID Length %d.", j);
6348c2ecf20Sopenharmony_ci	}
6358c2ecf20Sopenharmony_ci
6368c2ecf20Sopenharmony_ci	sw->type = -1;
6378c2ecf20Sopenharmony_ci	k = SW_FAIL;							/* Try SW_FAIL times */
6388c2ecf20Sopenharmony_ci	l = 0;
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	do {
6418c2ecf20Sopenharmony_ci		k--;
6428c2ecf20Sopenharmony_ci		msleep(SW_TIMEOUT);
6438c2ecf20Sopenharmony_ci		i = sw_read_packet(gameport, buf, SW_LENGTH, 0);	/* Read data packet */
6448c2ecf20Sopenharmony_ci		dbg("Init 3: Mode %d. Length %d. Last %d. Tries %d.", m, i, l, k);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci		if (i > l) {						/* Longer? As we can only lose bits, it makes */
6478c2ecf20Sopenharmony_ci									/* no sense to try detection for a packet shorter */
6488c2ecf20Sopenharmony_ci			l = i;						/* than the previous one */
6498c2ecf20Sopenharmony_ci
6508c2ecf20Sopenharmony_ci			sw->number = 1;
6518c2ecf20Sopenharmony_ci			sw->gameport = gameport;
6528c2ecf20Sopenharmony_ci			sw->length = i;
6538c2ecf20Sopenharmony_ci			sw->bits = m;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci			dbg("Init 3a: Case %d.\n", i * m);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci			switch (i * m) {
6588c2ecf20Sopenharmony_ci				case 60:
6598c2ecf20Sopenharmony_ci					sw->number++;
6608c2ecf20Sopenharmony_ci					fallthrough;
6618c2ecf20Sopenharmony_ci				case 45:				/* Ambiguous packet length */
6628c2ecf20Sopenharmony_ci					if (j <= 40) {			/* ID length less or eq 40 -> FSP */
6638c2ecf20Sopenharmony_ci				case 43:
6648c2ecf20Sopenharmony_ci						sw->type = SW_ID_FSP;
6658c2ecf20Sopenharmony_ci						break;
6668c2ecf20Sopenharmony_ci					}
6678c2ecf20Sopenharmony_ci					sw->number++;
6688c2ecf20Sopenharmony_ci					fallthrough;
6698c2ecf20Sopenharmony_ci				case 30:
6708c2ecf20Sopenharmony_ci					sw->number++;
6718c2ecf20Sopenharmony_ci					fallthrough;
6728c2ecf20Sopenharmony_ci				case 15:
6738c2ecf20Sopenharmony_ci					sw->type = SW_ID_GP;
6748c2ecf20Sopenharmony_ci					break;
6758c2ecf20Sopenharmony_ci				case 33:
6768c2ecf20Sopenharmony_ci				case 31:
6778c2ecf20Sopenharmony_ci					sw->type = SW_ID_FFW;
6788c2ecf20Sopenharmony_ci					break;
6798c2ecf20Sopenharmony_ci				case 48:				/* Ambiguous */
6808c2ecf20Sopenharmony_ci					if (j == 14) {			/* ID length 14*3 -> FFP */
6818c2ecf20Sopenharmony_ci						sw->type = SW_ID_FFP;
6828c2ecf20Sopenharmony_ci						sprintf(comment, " [AC %s]", sw_get_bits(idbuf,38,1,3) ? "off" : "on");
6838c2ecf20Sopenharmony_ci					} else
6848c2ecf20Sopenharmony_ci						sw->type = SW_ID_PP;
6858c2ecf20Sopenharmony_ci					break;
6868c2ecf20Sopenharmony_ci				case 66:
6878c2ecf20Sopenharmony_ci					sw->bits = 3;
6888c2ecf20Sopenharmony_ci					fallthrough;
6898c2ecf20Sopenharmony_ci				case 198:
6908c2ecf20Sopenharmony_ci					sw->length = 22;
6918c2ecf20Sopenharmony_ci					fallthrough;
6928c2ecf20Sopenharmony_ci				case 64:
6938c2ecf20Sopenharmony_ci					sw->type = SW_ID_3DP;
6948c2ecf20Sopenharmony_ci					if (j == 160)
6958c2ecf20Sopenharmony_ci						sw_3dp_id(idbuf, comment, sizeof(comment));
6968c2ecf20Sopenharmony_ci					break;
6978c2ecf20Sopenharmony_ci			}
6988c2ecf20Sopenharmony_ci		}
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	} while (k && sw->type == -1);
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_ci	if (sw->type == -1) {
7038c2ecf20Sopenharmony_ci		printk(KERN_WARNING "sidewinder.c: unknown joystick device detected "
7048c2ecf20Sopenharmony_ci			"on %s, contact <vojtech@ucw.cz>\n", gameport->phys);
7058c2ecf20Sopenharmony_ci		sw_print_packet("ID", j * 3, idbuf, 3);
7068c2ecf20Sopenharmony_ci		sw_print_packet("Data", i * m, buf, m);
7078c2ecf20Sopenharmony_ci		err = -ENODEV;
7088c2ecf20Sopenharmony_ci		goto fail2;
7098c2ecf20Sopenharmony_ci	}
7108c2ecf20Sopenharmony_ci
7118c2ecf20Sopenharmony_ci#ifdef SW_DEBUG
7128c2ecf20Sopenharmony_ci	sw_print_packet("ID", j * 3, idbuf, 3);
7138c2ecf20Sopenharmony_ci	sw_print_packet("Data", i * m, buf, m);
7148c2ecf20Sopenharmony_ci#endif
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci	gameport_set_poll_handler(gameport, sw_poll);
7178c2ecf20Sopenharmony_ci	gameport_set_poll_interval(gameport, 20);
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_ci	k = i;
7208c2ecf20Sopenharmony_ci	l = j;
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_ci	for (i = 0; i < sw->number; i++) {
7238c2ecf20Sopenharmony_ci		int bits, code;
7248c2ecf20Sopenharmony_ci
7258c2ecf20Sopenharmony_ci		snprintf(sw->name, sizeof(sw->name),
7268c2ecf20Sopenharmony_ci			 "Microsoft SideWinder %s", sw_name[sw->type]);
7278c2ecf20Sopenharmony_ci		snprintf(sw->phys[i], sizeof(sw->phys[i]),
7288c2ecf20Sopenharmony_ci			 "%s/input%d", gameport->phys, i);
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_ci		sw->dev[i] = input_dev = input_allocate_device();
7318c2ecf20Sopenharmony_ci		if (!input_dev) {
7328c2ecf20Sopenharmony_ci			err = -ENOMEM;
7338c2ecf20Sopenharmony_ci			goto fail3;
7348c2ecf20Sopenharmony_ci		}
7358c2ecf20Sopenharmony_ci
7368c2ecf20Sopenharmony_ci		input_dev->name = sw->name;
7378c2ecf20Sopenharmony_ci		input_dev->phys = sw->phys[i];
7388c2ecf20Sopenharmony_ci		input_dev->id.bustype = BUS_GAMEPORT;
7398c2ecf20Sopenharmony_ci		input_dev->id.vendor = GAMEPORT_ID_VENDOR_MICROSOFT;
7408c2ecf20Sopenharmony_ci		input_dev->id.product = sw->type;
7418c2ecf20Sopenharmony_ci		input_dev->id.version = 0x0100;
7428c2ecf20Sopenharmony_ci		input_dev->dev.parent = &gameport->dev;
7438c2ecf20Sopenharmony_ci
7448c2ecf20Sopenharmony_ci		input_set_drvdata(input_dev, sw);
7458c2ecf20Sopenharmony_ci
7468c2ecf20Sopenharmony_ci		input_dev->open = sw_open;
7478c2ecf20Sopenharmony_ci		input_dev->close = sw_close;
7488c2ecf20Sopenharmony_ci
7498c2ecf20Sopenharmony_ci		input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci		for (j = 0; (bits = sw_bit[sw->type][j]); j++) {
7528c2ecf20Sopenharmony_ci			int min, max, fuzz, flat;
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci			code = sw_abs[sw->type][j];
7558c2ecf20Sopenharmony_ci			min = bits == 1 ? -1 : 0;
7568c2ecf20Sopenharmony_ci			max = (1 << bits) - 1;
7578c2ecf20Sopenharmony_ci			fuzz = (bits >> 1) >= 2 ? 1 << ((bits >> 1) - 2) : 0;
7588c2ecf20Sopenharmony_ci			flat = code == ABS_THROTTLE || bits < 5 ?
7598c2ecf20Sopenharmony_ci				0 : 1 << (bits - 5);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci			input_set_abs_params(input_dev, code,
7628c2ecf20Sopenharmony_ci					     min, max, fuzz, flat);
7638c2ecf20Sopenharmony_ci		}
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci		for (j = 0; (code = sw_btn[sw->type][j]); j++)
7668c2ecf20Sopenharmony_ci			__set_bit(code, input_dev->keybit);
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci		dbg("%s%s [%d-bit id %d data %d]\n", sw->name, comment, m, l, k);
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci		err = input_register_device(sw->dev[i]);
7718c2ecf20Sopenharmony_ci		if (err)
7728c2ecf20Sopenharmony_ci			goto fail4;
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci out:	kfree(buf);
7768c2ecf20Sopenharmony_ci	kfree(idbuf);
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	return err;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci fail4:	input_free_device(sw->dev[i]);
7818c2ecf20Sopenharmony_ci fail3:	while (--i >= 0)
7828c2ecf20Sopenharmony_ci		input_unregister_device(sw->dev[i]);
7838c2ecf20Sopenharmony_ci fail2:	gameport_close(gameport);
7848c2ecf20Sopenharmony_ci fail1:	gameport_set_drvdata(gameport, NULL);
7858c2ecf20Sopenharmony_ci	kfree(sw);
7868c2ecf20Sopenharmony_ci	goto out;
7878c2ecf20Sopenharmony_ci}
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_cistatic void sw_disconnect(struct gameport *gameport)
7908c2ecf20Sopenharmony_ci{
7918c2ecf20Sopenharmony_ci	struct sw *sw = gameport_get_drvdata(gameport);
7928c2ecf20Sopenharmony_ci	int i;
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	for (i = 0; i < sw->number; i++)
7958c2ecf20Sopenharmony_ci		input_unregister_device(sw->dev[i]);
7968c2ecf20Sopenharmony_ci	gameport_close(gameport);
7978c2ecf20Sopenharmony_ci	gameport_set_drvdata(gameport, NULL);
7988c2ecf20Sopenharmony_ci	kfree(sw);
7998c2ecf20Sopenharmony_ci}
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_cistatic struct gameport_driver sw_drv = {
8028c2ecf20Sopenharmony_ci	.driver		= {
8038c2ecf20Sopenharmony_ci		.name	= "sidewinder",
8048c2ecf20Sopenharmony_ci		.owner	= THIS_MODULE,
8058c2ecf20Sopenharmony_ci	},
8068c2ecf20Sopenharmony_ci	.description	= DRIVER_DESC,
8078c2ecf20Sopenharmony_ci	.connect	= sw_connect,
8088c2ecf20Sopenharmony_ci	.disconnect	= sw_disconnect,
8098c2ecf20Sopenharmony_ci};
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_cimodule_gameport_driver(sw_drv);
812