xref: /openbmc/linux/drivers/input/joystick/adi.c (revision 98a84131926ebafe868c65631b69d4912922211e)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) 1998-2005 Vojtech Pavlik
31da177e4SLinus Torvalds  */
41da177e4SLinus Torvalds 
51da177e4SLinus Torvalds /*
61da177e4SLinus Torvalds  * Logitech ADI joystick family driver for Linux
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
111da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
121da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
131da177e4SLinus Torvalds  * (at your option) any later version.
141da177e4SLinus Torvalds  *
151da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
161da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
171da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
181da177e4SLinus Torvalds  * GNU General Public License for more details.
191da177e4SLinus Torvalds  *
201da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
211da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
221da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
231da177e4SLinus Torvalds  *
241da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
251da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
261da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
271da177e4SLinus Torvalds  */
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds #include <linux/delay.h>
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/module.h>
321da177e4SLinus Torvalds #include <linux/string.h>
331da177e4SLinus Torvalds #include <linux/slab.h>
341da177e4SLinus Torvalds #include <linux/input.h>
351da177e4SLinus Torvalds #include <linux/gameport.h>
361da177e4SLinus Torvalds #include <linux/init.h>
374e57b681STim Schmielau #include <linux/jiffies.h>
381da177e4SLinus Torvalds 
391da177e4SLinus Torvalds #define DRIVER_DESC	"Logitech ADI joystick family driver"
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
421da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
431da177e4SLinus Torvalds MODULE_LICENSE("GPL");
441da177e4SLinus Torvalds 
451da177e4SLinus Torvalds /*
461da177e4SLinus Torvalds  * Times, array sizes, flags, ids.
471da177e4SLinus Torvalds  */
481da177e4SLinus Torvalds 
491da177e4SLinus Torvalds #define ADI_MAX_START		200	/* Trigger to packet timeout [200us] */
501da177e4SLinus Torvalds #define ADI_MAX_STROBE		40	/* Single bit timeout [40us] */
511da177e4SLinus Torvalds #define ADI_INIT_DELAY		10	/* Delay after init packet [10ms] */
521da177e4SLinus Torvalds #define ADI_DATA_DELAY		4	/* Delay after data packet [4ms] */
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds #define ADI_MAX_LENGTH		256
551da177e4SLinus Torvalds #define ADI_MIN_LENGTH		8
561da177e4SLinus Torvalds #define ADI_MIN_LEN_LENGTH	10
571da177e4SLinus Torvalds #define ADI_MIN_ID_LENGTH	66
5817dd3f0fSDmitry Torokhov #define ADI_MAX_NAME_LENGTH	64
591da177e4SLinus Torvalds #define ADI_MAX_CNAME_LENGTH	16
601da177e4SLinus Torvalds #define ADI_MAX_PHYS_LENGTH	64
611da177e4SLinus Torvalds 
621da177e4SLinus Torvalds #define ADI_FLAG_HAT		0x04
631da177e4SLinus Torvalds #define ADI_FLAG_10BIT		0x08
641da177e4SLinus Torvalds 
651da177e4SLinus Torvalds #define ADI_ID_TPD		0x01
661da177e4SLinus Torvalds #define ADI_ID_WGP		0x06
671da177e4SLinus Torvalds #define ADI_ID_WGPE		0x08
681da177e4SLinus Torvalds #define ADI_ID_MAX		0x0a
691da177e4SLinus Torvalds 
701da177e4SLinus Torvalds /*
711da177e4SLinus Torvalds  * Names, buttons, axes ...
721da177e4SLinus Torvalds  */
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds static char *adi_names[] = {	"WingMan Extreme Digital", "ThunderPad Digital", "SideCar", "CyberMan 2",
751da177e4SLinus Torvalds 				"WingMan Interceptor", "WingMan Formula", "WingMan GamePad",
761da177e4SLinus Torvalds 				"WingMan Extreme Digital 3D", "WingMan GamePad Extreme",
771da177e4SLinus Torvalds 				"WingMan GamePad USB", "Unknown Device %#x" };
781da177e4SLinus Torvalds 
791da177e4SLinus Torvalds static char adi_wmgpe_abs[] =	{ ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y };
801da177e4SLinus Torvalds static char adi_wmi_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
811da177e4SLinus Torvalds static char adi_wmed3d_abs[] =	{ ABS_X, ABS_Y, ABS_THROTTLE, ABS_RZ, ABS_HAT0X, ABS_HAT0Y };
821da177e4SLinus Torvalds static char adi_cm2_abs[] =	{ ABS_X, ABS_Y, ABS_Z, ABS_RX, ABS_RY, ABS_RZ };
831da177e4SLinus Torvalds static char adi_wmf_abs[] =	{ ABS_WHEEL, ABS_GAS, ABS_BRAKE, ABS_HAT0X, ABS_HAT0Y, ABS_HAT1X, ABS_HAT1Y, ABS_HAT2X, ABS_HAT2Y };
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds static short adi_wmgpe_key[] =	{ BTN_A, BTN_B, BTN_C, BTN_X, BTN_Y, BTN_Z,  BTN_TL, BTN_TR, BTN_START, BTN_MODE, BTN_SELECT };
861da177e4SLinus Torvalds static short adi_wmi_key[] =	{ BTN_TRIGGER,  BTN_TOP, BTN_THUMB, BTN_TOP2, BTN_BASE, BTN_BASE2, BTN_BASE3, BTN_BASE4, BTN_EXTRA };
871da177e4SLinus Torvalds static short adi_wmed3d_key[] =	{ BTN_TRIGGER, BTN_THUMB, BTN_THUMB2, BTN_TOP, BTN_TOP2, BTN_BASE, BTN_BASE2 };
881da177e4SLinus Torvalds static short adi_cm2_key[] =	{ BTN_1, BTN_2, BTN_3, BTN_4, BTN_5, BTN_6, BTN_7, BTN_8 };
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds static char* adi_abs[] = { adi_wmi_abs, adi_wmgpe_abs, adi_wmf_abs, adi_cm2_abs, adi_wmi_abs, adi_wmf_abs,
911da177e4SLinus Torvalds 			   adi_wmgpe_abs, adi_wmed3d_abs, adi_wmgpe_abs, adi_wmgpe_abs, adi_wmi_abs };
921da177e4SLinus Torvalds 
931da177e4SLinus Torvalds static short* adi_key[] = { adi_wmi_key, adi_wmgpe_key, adi_cm2_key, adi_cm2_key, adi_wmi_key, adi_cm2_key,
941da177e4SLinus Torvalds 			    adi_wmgpe_key, adi_wmed3d_key, adi_wmgpe_key, adi_wmgpe_key, adi_wmi_key };
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds /*
971da177e4SLinus Torvalds  * Hat to axis conversion arrays.
981da177e4SLinus Torvalds  */
991da177e4SLinus Torvalds 
1001da177e4SLinus Torvalds static struct {
1011da177e4SLinus Torvalds 	int x;
1021da177e4SLinus Torvalds 	int y;
1031da177e4SLinus Torvalds } adi_hat_to_axis[] = {{ 0, 0}, { 0,-1}, { 1,-1}, { 1, 0}, { 1, 1}, { 0, 1}, {-1, 1}, {-1, 0}, {-1,-1}};
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds /*
1061da177e4SLinus Torvalds  * Per-port information.
1071da177e4SLinus Torvalds  */
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds struct adi {
11017dd3f0fSDmitry Torokhov 	struct input_dev *dev;
1111da177e4SLinus Torvalds 	int length;
1121da177e4SLinus Torvalds 	int ret;
1131da177e4SLinus Torvalds 	int idx;
1141da177e4SLinus Torvalds 	unsigned char id;
1151da177e4SLinus Torvalds 	char buttons;
1161da177e4SLinus Torvalds 	char axes10;
1171da177e4SLinus Torvalds 	char axes8;
1181da177e4SLinus Torvalds 	signed char pad;
1191da177e4SLinus Torvalds 	char hats;
1201da177e4SLinus Torvalds 	char *abs;
1211da177e4SLinus Torvalds 	short *key;
1221da177e4SLinus Torvalds 	char name[ADI_MAX_NAME_LENGTH];
1231da177e4SLinus Torvalds 	char cname[ADI_MAX_CNAME_LENGTH];
1241da177e4SLinus Torvalds 	char phys[ADI_MAX_PHYS_LENGTH];
1251da177e4SLinus Torvalds 	unsigned char data[ADI_MAX_LENGTH];
1261da177e4SLinus Torvalds };
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds struct adi_port {
1291da177e4SLinus Torvalds 	struct gameport *gameport;
1301da177e4SLinus Torvalds 	struct adi adi[2];
1311da177e4SLinus Torvalds 	int bad;
1321da177e4SLinus Torvalds 	int reads;
1331da177e4SLinus Torvalds };
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds /*
1361da177e4SLinus Torvalds  * adi_read_packet() reads a Logitech ADI packet.
1371da177e4SLinus Torvalds  */
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds static void adi_read_packet(struct adi_port *port)
1401da177e4SLinus Torvalds {
1411da177e4SLinus Torvalds 	struct adi *adi = port->adi;
1421da177e4SLinus Torvalds 	struct gameport *gameport = port->gameport;
1431da177e4SLinus Torvalds 	unsigned char u, v, w, x, z;
1441da177e4SLinus Torvalds 	int t[2], s[2], i;
1451da177e4SLinus Torvalds 	unsigned long flags;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
1481da177e4SLinus Torvalds 		adi[i].ret = -1;
1491da177e4SLinus Torvalds 		t[i] = gameport_time(gameport, ADI_MAX_START);
1501da177e4SLinus Torvalds 		s[i] = 0;
1511da177e4SLinus Torvalds 	}
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 	local_irq_save(flags);
1541da177e4SLinus Torvalds 
1551da177e4SLinus Torvalds 	gameport_trigger(gameport);
1561da177e4SLinus Torvalds 	v = z = gameport_read(gameport);
1571da177e4SLinus Torvalds 
1581da177e4SLinus Torvalds 	do {
1591da177e4SLinus Torvalds 		u = v;
1601da177e4SLinus Torvalds 		w = u ^ (v = x = gameport_read(gameport));
1611da177e4SLinus Torvalds 		for (i = 0; i < 2; i++, w >>= 2, x >>= 2) {
1621da177e4SLinus Torvalds 			t[i]--;
1631da177e4SLinus Torvalds 			if ((w & 0x30) && s[i]) {
1641da177e4SLinus Torvalds 				if ((w & 0x30) < 0x30 && adi[i].ret < ADI_MAX_LENGTH && t[i] > 0) {
1651da177e4SLinus Torvalds 					adi[i].data[++adi[i].ret] = w;
1661da177e4SLinus Torvalds 					t[i] = gameport_time(gameport, ADI_MAX_STROBE);
1671da177e4SLinus Torvalds 				} else t[i] = 0;
1681da177e4SLinus Torvalds 			} else if (!(x & 0x30)) s[i] = 1;
1691da177e4SLinus Torvalds 		}
1701da177e4SLinus Torvalds 	} while (t[0] > 0 || t[1] > 0);
1711da177e4SLinus Torvalds 
1721da177e4SLinus Torvalds 	local_irq_restore(flags);
1731da177e4SLinus Torvalds 
1741da177e4SLinus Torvalds 	return;
1751da177e4SLinus Torvalds }
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds /*
1781da177e4SLinus Torvalds  * adi_move_bits() detects a possible 2-stream mode, and moves
1791da177e4SLinus Torvalds  * the bits accordingly.
1801da177e4SLinus Torvalds  */
1811da177e4SLinus Torvalds 
1821da177e4SLinus Torvalds static void adi_move_bits(struct adi_port *port, int length)
1831da177e4SLinus Torvalds {
1841da177e4SLinus Torvalds 	int i;
1851da177e4SLinus Torvalds 	struct adi *adi = port->adi;
1861da177e4SLinus Torvalds 
1871da177e4SLinus Torvalds 	adi[0].idx = adi[1].idx = 0;
1881da177e4SLinus Torvalds 
1891da177e4SLinus Torvalds 	if (adi[0].ret <= 0 || adi[1].ret <= 0) return;
1901da177e4SLinus Torvalds 	if (adi[0].data[0] & 0x20 || ~adi[1].data[0] & 0x20) return;
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds 	for (i = 1; i <= adi[1].ret; i++)
1931da177e4SLinus Torvalds 		adi[0].data[((length - 1) >> 1) + i + 1] = adi[1].data[i];
1941da177e4SLinus Torvalds 
1951da177e4SLinus Torvalds 	adi[0].ret += adi[1].ret;
1961da177e4SLinus Torvalds 	adi[1].ret = -1;
1971da177e4SLinus Torvalds }
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds /*
2001da177e4SLinus Torvalds  * adi_get_bits() gathers bits from the data packet.
2011da177e4SLinus Torvalds  */
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds static inline int adi_get_bits(struct adi *adi, int count)
2041da177e4SLinus Torvalds {
2051da177e4SLinus Torvalds 	int bits = 0;
2061da177e4SLinus Torvalds 	int i;
2071da177e4SLinus Torvalds 	if ((adi->idx += count) > adi->ret) return 0;
2081da177e4SLinus Torvalds 	for (i = 0; i < count; i++)
2091da177e4SLinus Torvalds 		bits |= ((adi->data[adi->idx - i] >> 5) & 1) << i;
2101da177e4SLinus Torvalds 	return bits;
2111da177e4SLinus Torvalds }
2121da177e4SLinus Torvalds 
2131da177e4SLinus Torvalds /*
2141da177e4SLinus Torvalds  * adi_decode() decodes Logitech joystick data into input events.
2151da177e4SLinus Torvalds  */
2161da177e4SLinus Torvalds 
2171da177e4SLinus Torvalds static int adi_decode(struct adi *adi)
2181da177e4SLinus Torvalds {
21917dd3f0fSDmitry Torokhov 	struct input_dev *dev = adi->dev;
2201da177e4SLinus Torvalds 	char *abs = adi->abs;
2211da177e4SLinus Torvalds 	short *key = adi->key;
2221da177e4SLinus Torvalds 	int i, t;
2231da177e4SLinus Torvalds 
2241da177e4SLinus Torvalds 	if (adi->ret < adi->length || adi->id != (adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4)))
2251da177e4SLinus Torvalds 		return -1;
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	for (i = 0; i < adi->axes10; i++)
2281da177e4SLinus Torvalds 		input_report_abs(dev, *abs++, adi_get_bits(adi, 10));
2291da177e4SLinus Torvalds 
2301da177e4SLinus Torvalds 	for (i = 0; i < adi->axes8; i++)
2311da177e4SLinus Torvalds 		input_report_abs(dev, *abs++, adi_get_bits(adi, 8));
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	for (i = 0; i < adi->buttons && i < 63; i++) {
2341da177e4SLinus Torvalds 		if (i == adi->pad) {
2351da177e4SLinus Torvalds 			t = adi_get_bits(adi, 4);
2361da177e4SLinus Torvalds 			input_report_abs(dev, *abs++, ((t >> 2) & 1) - ( t       & 1));
2371da177e4SLinus Torvalds 			input_report_abs(dev, *abs++, ((t >> 1) & 1) - ((t >> 3) & 1));
2381da177e4SLinus Torvalds 		}
2391da177e4SLinus Torvalds 		input_report_key(dev, *key++, adi_get_bits(adi, 1));
2401da177e4SLinus Torvalds 	}
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	for (i = 0; i < adi->hats; i++) {
2431da177e4SLinus Torvalds 		if ((t = adi_get_bits(adi, 4)) > 8) t = 0;
2441da177e4SLinus Torvalds 		input_report_abs(dev, *abs++, adi_hat_to_axis[t].x);
2451da177e4SLinus Torvalds 		input_report_abs(dev, *abs++, adi_hat_to_axis[t].y);
2461da177e4SLinus Torvalds 	}
2471da177e4SLinus Torvalds 
2481da177e4SLinus Torvalds 	for (i = 63; i < adi->buttons; i++)
2491da177e4SLinus Torvalds 		input_report_key(dev, *key++, adi_get_bits(adi, 1));
2501da177e4SLinus Torvalds 
2511da177e4SLinus Torvalds 	input_sync(dev);
2521da177e4SLinus Torvalds 
2531da177e4SLinus Torvalds 	return 0;
2541da177e4SLinus Torvalds }
2551da177e4SLinus Torvalds 
2561da177e4SLinus Torvalds /*
2571da177e4SLinus Torvalds  * adi_read() reads the data packet and decodes it.
2581da177e4SLinus Torvalds  */
2591da177e4SLinus Torvalds 
2601da177e4SLinus Torvalds static int adi_read(struct adi_port *port)
2611da177e4SLinus Torvalds {
2621da177e4SLinus Torvalds 	int i;
2631da177e4SLinus Torvalds 	int result = 0;
2641da177e4SLinus Torvalds 
2651da177e4SLinus Torvalds 	adi_read_packet(port);
2661da177e4SLinus Torvalds 	adi_move_bits(port, port->adi[0].length);
2671da177e4SLinus Torvalds 
2681da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
2691da177e4SLinus Torvalds 		if (port->adi[i].length)
2701da177e4SLinus Torvalds 			 result |= adi_decode(port->adi + i);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	return result;
2731da177e4SLinus Torvalds }
2741da177e4SLinus Torvalds 
2751da177e4SLinus Torvalds /*
2761da177e4SLinus Torvalds  * adi_poll() repeatedly polls the Logitech joysticks.
2771da177e4SLinus Torvalds  */
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds static void adi_poll(struct gameport *gameport)
2801da177e4SLinus Torvalds {
2811da177e4SLinus Torvalds 	struct adi_port *port = gameport_get_drvdata(gameport);
2821da177e4SLinus Torvalds 
2831da177e4SLinus Torvalds 	port->bad -= adi_read(port);
2841da177e4SLinus Torvalds 	port->reads++;
2851da177e4SLinus Torvalds }
2861da177e4SLinus Torvalds 
2871da177e4SLinus Torvalds /*
2881da177e4SLinus Torvalds  * adi_open() is a callback from the input open routine.
2891da177e4SLinus Torvalds  */
2901da177e4SLinus Torvalds 
2911da177e4SLinus Torvalds static int adi_open(struct input_dev *dev)
2921da177e4SLinus Torvalds {
2938715c1cfSDmitry Torokhov 	struct adi_port *port = input_get_drvdata(dev);
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds 	gameport_start_polling(port->gameport);
2961da177e4SLinus Torvalds 	return 0;
2971da177e4SLinus Torvalds }
2981da177e4SLinus Torvalds 
2991da177e4SLinus Torvalds /*
3001da177e4SLinus Torvalds  * adi_close() is a callback from the input close routine.
3011da177e4SLinus Torvalds  */
3021da177e4SLinus Torvalds 
3031da177e4SLinus Torvalds static void adi_close(struct input_dev *dev)
3041da177e4SLinus Torvalds {
3058715c1cfSDmitry Torokhov 	struct adi_port *port = input_get_drvdata(dev);
3061da177e4SLinus Torvalds 
3071da177e4SLinus Torvalds 	gameport_stop_polling(port->gameport);
3081da177e4SLinus Torvalds }
3091da177e4SLinus Torvalds 
3101da177e4SLinus Torvalds /*
3111da177e4SLinus Torvalds  * adi_init_digital() sends a trigger & delay sequence
3121da177e4SLinus Torvalds  * to reset and initialize a Logitech joystick into digital mode.
3131da177e4SLinus Torvalds  */
3141da177e4SLinus Torvalds 
3151da177e4SLinus Torvalds static void adi_init_digital(struct gameport *gameport)
3161da177e4SLinus Torvalds {
3171da177e4SLinus Torvalds 	int seq[] = { 4, -2, -3, 10, -6, -11, -7, -9, 11, 0 };
3181da177e4SLinus Torvalds 	int i;
3191da177e4SLinus Torvalds 
3201da177e4SLinus Torvalds 	for (i = 0; seq[i]; i++) {
3211da177e4SLinus Torvalds 		gameport_trigger(gameport);
32217dd3f0fSDmitry Torokhov 		if (seq[i] > 0)
32317dd3f0fSDmitry Torokhov 			msleep(seq[i]);
3241da177e4SLinus Torvalds 		if (seq[i] < 0) {
3251da177e4SLinus Torvalds 			mdelay(-seq[i]);
3261da177e4SLinus Torvalds 			udelay(-seq[i]*14);	/* It looks like mdelay() is off by approx 1.4% */
3271da177e4SLinus Torvalds 		}
3281da177e4SLinus Torvalds 	}
3291da177e4SLinus Torvalds }
3301da177e4SLinus Torvalds 
3311da177e4SLinus Torvalds static void adi_id_decode(struct adi *adi, struct adi_port *port)
3321da177e4SLinus Torvalds {
3331da177e4SLinus Torvalds 	int i, t;
3341da177e4SLinus Torvalds 
3351da177e4SLinus Torvalds 	if (adi->ret < ADI_MIN_ID_LENGTH) /* Minimum ID packet length */
3361da177e4SLinus Torvalds 		return;
3371da177e4SLinus Torvalds 
3381da177e4SLinus Torvalds 	if (adi->ret < (t = adi_get_bits(adi, 10))) {
3391da177e4SLinus Torvalds 		printk(KERN_WARNING "adi: Short ID packet: reported: %d != read: %d\n", t, adi->ret);
3401da177e4SLinus Torvalds 		return;
3411da177e4SLinus Torvalds 	}
3421da177e4SLinus Torvalds 
3431da177e4SLinus Torvalds 	adi->id = adi_get_bits(adi, 4) | (adi_get_bits(adi, 4) << 4);
3441da177e4SLinus Torvalds 
3451da177e4SLinus Torvalds 	if ((t = adi_get_bits(adi, 4)) & ADI_FLAG_HAT) adi->hats++;
3461da177e4SLinus Torvalds 
3471da177e4SLinus Torvalds 	adi->length = adi_get_bits(adi, 10);
3481da177e4SLinus Torvalds 
3491da177e4SLinus Torvalds 	if (adi->length >= ADI_MAX_LENGTH || adi->length < ADI_MIN_LENGTH) {
3501da177e4SLinus Torvalds 		printk(KERN_WARNING "adi: Bad data packet length (%d).\n", adi->length);
3511da177e4SLinus Torvalds 		adi->length = 0;
3521da177e4SLinus Torvalds 		return;
3531da177e4SLinus Torvalds 	}
3541da177e4SLinus Torvalds 
3551da177e4SLinus Torvalds 	adi->axes8 = adi_get_bits(adi, 4);
3561da177e4SLinus Torvalds 	adi->buttons = adi_get_bits(adi, 6);
3571da177e4SLinus Torvalds 
3581da177e4SLinus Torvalds 	if (adi_get_bits(adi, 6) != 8 && adi->hats) {
3591da177e4SLinus Torvalds 		printk(KERN_WARNING "adi: Other than 8-dir POVs not supported yet.\n");
3601da177e4SLinus Torvalds 		adi->length = 0;
3611da177e4SLinus Torvalds 		return;
3621da177e4SLinus Torvalds 	}
3631da177e4SLinus Torvalds 
3641da177e4SLinus Torvalds 	adi->buttons += adi_get_bits(adi, 6);
3651da177e4SLinus Torvalds 	adi->hats += adi_get_bits(adi, 4);
3661da177e4SLinus Torvalds 
3671da177e4SLinus Torvalds 	i = adi_get_bits(adi, 4);
3681da177e4SLinus Torvalds 
3691da177e4SLinus Torvalds 	if (t & ADI_FLAG_10BIT) {
3701da177e4SLinus Torvalds 		adi->axes10 = adi->axes8 - i;
3711da177e4SLinus Torvalds 		adi->axes8 = i;
3721da177e4SLinus Torvalds 	}
3731da177e4SLinus Torvalds 
3741da177e4SLinus Torvalds 	t = adi_get_bits(adi, 4);
3751da177e4SLinus Torvalds 
3761da177e4SLinus Torvalds 	for (i = 0; i < t; i++)
3771da177e4SLinus Torvalds 		adi->cname[i] = adi_get_bits(adi, 8);
3781da177e4SLinus Torvalds 	adi->cname[i] = 0;
3791da177e4SLinus Torvalds 
3801da177e4SLinus Torvalds 	t = 8 + adi->buttons + adi->axes10 * 10 + adi->axes8 * 8 + adi->hats * 4;
3811da177e4SLinus Torvalds 	if (adi->length != t && adi->length != t + (t & 1)) {
3821da177e4SLinus Torvalds 		printk(KERN_WARNING "adi: Expected length %d != data length %d\n", t, adi->length);
3831da177e4SLinus Torvalds 		adi->length = 0;
3841da177e4SLinus Torvalds 		return;
3851da177e4SLinus Torvalds 	}
3861da177e4SLinus Torvalds 
3871da177e4SLinus Torvalds 	switch (adi->id) {
3881da177e4SLinus Torvalds 		case ADI_ID_TPD:
3891da177e4SLinus Torvalds 			adi->pad = 4;
3901da177e4SLinus Torvalds 			adi->buttons -= 4;
3911da177e4SLinus Torvalds 			break;
3921da177e4SLinus Torvalds 		case ADI_ID_WGP:
3931da177e4SLinus Torvalds 			adi->pad = 0;
3941da177e4SLinus Torvalds 			adi->buttons -= 4;
3951da177e4SLinus Torvalds 			break;
3961da177e4SLinus Torvalds 		default:
3971da177e4SLinus Torvalds 			adi->pad = -1;
3981da177e4SLinus Torvalds 			break;
3991da177e4SLinus Torvalds 	}
4001da177e4SLinus Torvalds }
4011da177e4SLinus Torvalds 
40217dd3f0fSDmitry Torokhov static int adi_init_input(struct adi *adi, struct adi_port *port, int half)
4031da177e4SLinus Torvalds {
40417dd3f0fSDmitry Torokhov 	struct input_dev *input_dev;
4051da177e4SLinus Torvalds 	char buf[ADI_MAX_NAME_LENGTH];
40617dd3f0fSDmitry Torokhov 	int i, t;
4071da177e4SLinus Torvalds 
40817dd3f0fSDmitry Torokhov 	adi->dev = input_dev = input_allocate_device();
40917dd3f0fSDmitry Torokhov 	if (!input_dev)
41017dd3f0fSDmitry Torokhov 		return -ENOMEM;
4111da177e4SLinus Torvalds 
4121da177e4SLinus Torvalds 	t = adi->id < ADI_ID_MAX ? adi->id : ADI_ID_MAX;
4131da177e4SLinus Torvalds 
4141da177e4SLinus Torvalds 	snprintf(buf, ADI_MAX_PHYS_LENGTH, adi_names[t], adi->id);
41517dd3f0fSDmitry Torokhov 	snprintf(adi->name, ADI_MAX_NAME_LENGTH, "Logitech %s [%s]", buf, adi->cname);
4161da177e4SLinus Torvalds 	snprintf(adi->phys, ADI_MAX_PHYS_LENGTH, "%s/input%d", port->gameport->phys, half);
4171da177e4SLinus Torvalds 
4181da177e4SLinus Torvalds 	adi->abs = adi_abs[t];
4191da177e4SLinus Torvalds 	adi->key = adi_key[t];
4201da177e4SLinus Torvalds 
42117dd3f0fSDmitry Torokhov 	input_dev->name = adi->name;
42217dd3f0fSDmitry Torokhov 	input_dev->phys = adi->phys;
42317dd3f0fSDmitry Torokhov 	input_dev->id.bustype = BUS_GAMEPORT;
42417dd3f0fSDmitry Torokhov 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_LOGITECH;
42517dd3f0fSDmitry Torokhov 	input_dev->id.product = adi->id;
42617dd3f0fSDmitry Torokhov 	input_dev->id.version = 0x0100;
427935e658eSDmitry Torokhov 	input_dev->dev.parent = &port->gameport->dev;
4288715c1cfSDmitry Torokhov 
4298715c1cfSDmitry Torokhov 	input_set_drvdata(input_dev, port);
4301da177e4SLinus Torvalds 
43117dd3f0fSDmitry Torokhov 	input_dev->open = adi_open;
43217dd3f0fSDmitry Torokhov 	input_dev->close = adi_close;
4331da177e4SLinus Torvalds 
4347b19ada2SJiri Slaby 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
4351da177e4SLinus Torvalds 
4361da177e4SLinus Torvalds 	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++)
43717dd3f0fSDmitry Torokhov 		set_bit(adi->abs[i], input_dev->absbit);
4381da177e4SLinus Torvalds 
4391da177e4SLinus Torvalds 	for (i = 0; i < adi->buttons; i++)
44017dd3f0fSDmitry Torokhov 		set_bit(adi->key[i], input_dev->keybit);
44117dd3f0fSDmitry Torokhov 
44217dd3f0fSDmitry Torokhov 	return 0;
4431da177e4SLinus Torvalds }
4441da177e4SLinus Torvalds 
4451da177e4SLinus Torvalds static void adi_init_center(struct adi *adi)
4461da177e4SLinus Torvalds {
4471da177e4SLinus Torvalds 	int i, t, x;
4481da177e4SLinus Torvalds 
4491da177e4SLinus Torvalds 	if (!adi->length)
4501da177e4SLinus Torvalds 		return;
4511da177e4SLinus Torvalds 
4521da177e4SLinus Torvalds 	for (i = 0; i < adi->axes10 + adi->axes8 + (adi->hats + (adi->pad != -1)) * 2; i++) {
4531da177e4SLinus Torvalds 
4541da177e4SLinus Torvalds 		t = adi->abs[i];
455987a6c02SDaniel Mack 		x = input_abs_get_val(adi->dev, t);
4561da177e4SLinus Torvalds 
4571da177e4SLinus Torvalds 		if (t == ABS_THROTTLE || t == ABS_RUDDER || adi->id == ADI_ID_WGPE)
4581da177e4SLinus Torvalds 			x = i < adi->axes10 ? 512 : 128;
4591da177e4SLinus Torvalds 
4601da177e4SLinus Torvalds 		if (i < adi->axes10)
46117dd3f0fSDmitry Torokhov 			input_set_abs_params(adi->dev, t, 64, x * 2 - 64, 2, 16);
4621da177e4SLinus Torvalds 		else if (i < adi->axes10 + adi->axes8)
46317dd3f0fSDmitry Torokhov 			input_set_abs_params(adi->dev, t, 48, x * 2 - 48, 1, 16);
4641da177e4SLinus Torvalds 		else
46517dd3f0fSDmitry Torokhov 			input_set_abs_params(adi->dev, t, -1, 1, 0, 0);
4661da177e4SLinus Torvalds 	}
4671da177e4SLinus Torvalds }
4681da177e4SLinus Torvalds 
4691da177e4SLinus Torvalds /*
4701da177e4SLinus Torvalds  * adi_connect() probes for Logitech ADI joysticks.
4711da177e4SLinus Torvalds  */
4721da177e4SLinus Torvalds 
4731da177e4SLinus Torvalds static int adi_connect(struct gameport *gameport, struct gameport_driver *drv)
4741da177e4SLinus Torvalds {
4751da177e4SLinus Torvalds 	struct adi_port *port;
4761da177e4SLinus Torvalds 	int i;
4771da177e4SLinus Torvalds 	int err;
4781da177e4SLinus Torvalds 
47917dd3f0fSDmitry Torokhov 	port = kzalloc(sizeof(struct adi_port), GFP_KERNEL);
48017dd3f0fSDmitry Torokhov 	if (!port)
4811da177e4SLinus Torvalds 		return -ENOMEM;
4821da177e4SLinus Torvalds 
4831da177e4SLinus Torvalds 	port->gameport = gameport;
4841da177e4SLinus Torvalds 
4851da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, port);
4861da177e4SLinus Torvalds 
4871da177e4SLinus Torvalds 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
48817dd3f0fSDmitry Torokhov 	if (err)
48917dd3f0fSDmitry Torokhov 		goto fail1;
4901da177e4SLinus Torvalds 
4911da177e4SLinus Torvalds 	adi_init_digital(gameport);
4921da177e4SLinus Torvalds 	adi_read_packet(port);
4931da177e4SLinus Torvalds 
4941da177e4SLinus Torvalds 	if (port->adi[0].ret >= ADI_MIN_LEN_LENGTH)
4951da177e4SLinus Torvalds 		adi_move_bits(port, adi_get_bits(port->adi, 10));
4961da177e4SLinus Torvalds 
4971da177e4SLinus Torvalds 	for (i = 0; i < 2; i++) {
4981da177e4SLinus Torvalds 		adi_id_decode(port->adi + i, port);
49917dd3f0fSDmitry Torokhov 
50017dd3f0fSDmitry Torokhov 		if (!port->adi[i].length)
50117dd3f0fSDmitry Torokhov 			continue;
50217dd3f0fSDmitry Torokhov 
50317dd3f0fSDmitry Torokhov 		err = adi_init_input(port->adi + i, port, i);
50417dd3f0fSDmitry Torokhov 		if (err)
50517dd3f0fSDmitry Torokhov 			goto fail2;
5061da177e4SLinus Torvalds 	}
5071da177e4SLinus Torvalds 
5081da177e4SLinus Torvalds 	if (!port->adi[0].length && !port->adi[1].length) {
50917dd3f0fSDmitry Torokhov 		err = -ENODEV;
51017dd3f0fSDmitry Torokhov 		goto fail2;
5111da177e4SLinus Torvalds 	}
5121da177e4SLinus Torvalds 
5131da177e4SLinus Torvalds 	gameport_set_poll_handler(gameport, adi_poll);
5141da177e4SLinus Torvalds 	gameport_set_poll_interval(gameport, 20);
5151da177e4SLinus Torvalds 
5161da177e4SLinus Torvalds 	msleep(ADI_INIT_DELAY);
5171da177e4SLinus Torvalds 	if (adi_read(port)) {
5181da177e4SLinus Torvalds 		msleep(ADI_DATA_DELAY);
5191da177e4SLinus Torvalds 		adi_read(port);
5201da177e4SLinus Torvalds 	}
5211da177e4SLinus Torvalds 
5221da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
5231da177e4SLinus Torvalds 		if (port->adi[i].length > 0) {
5241da177e4SLinus Torvalds 			adi_init_center(port->adi + i);
525127278ceSDmitry Torokhov 			err = input_register_device(port->adi[i].dev);
526127278ceSDmitry Torokhov 			if (err)
527127278ceSDmitry Torokhov 				goto fail3;
5281da177e4SLinus Torvalds 		}
5291da177e4SLinus Torvalds 
5301da177e4SLinus Torvalds 	return 0;
53117dd3f0fSDmitry Torokhov 
532127278ceSDmitry Torokhov  fail3: while (--i >= 0) {
533127278ceSDmitry Torokhov 		if (port->adi[i].length > 0) {
534127278ceSDmitry Torokhov 			input_unregister_device(port->adi[i].dev);
535127278ceSDmitry Torokhov 			port->adi[i].dev = NULL;
536127278ceSDmitry Torokhov 		}
537127278ceSDmitry Torokhov 	}
53817dd3f0fSDmitry Torokhov  fail2:	for (i = 0; i < 2; i++)
53917dd3f0fSDmitry Torokhov 		if (port->adi[i].dev)
54017dd3f0fSDmitry Torokhov 			input_free_device(port->adi[i].dev);
54117dd3f0fSDmitry Torokhov 	gameport_close(gameport);
54217dd3f0fSDmitry Torokhov  fail1:	gameport_set_drvdata(gameport, NULL);
54317dd3f0fSDmitry Torokhov 	kfree(port);
54417dd3f0fSDmitry Torokhov 	return err;
5451da177e4SLinus Torvalds }
5461da177e4SLinus Torvalds 
5471da177e4SLinus Torvalds static void adi_disconnect(struct gameport *gameport)
5481da177e4SLinus Torvalds {
5491da177e4SLinus Torvalds 	int i;
5501da177e4SLinus Torvalds 	struct adi_port *port = gameport_get_drvdata(gameport);
5511da177e4SLinus Torvalds 
5521da177e4SLinus Torvalds 	for (i = 0; i < 2; i++)
5531da177e4SLinus Torvalds 		if (port->adi[i].length > 0)
55417dd3f0fSDmitry Torokhov 			input_unregister_device(port->adi[i].dev);
5551da177e4SLinus Torvalds 	gameport_close(gameport);
5561da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, NULL);
5571da177e4SLinus Torvalds 	kfree(port);
5581da177e4SLinus Torvalds }
5591da177e4SLinus Torvalds 
5601da177e4SLinus Torvalds static struct gameport_driver adi_drv = {
5611da177e4SLinus Torvalds 	.driver		= {
5621da177e4SLinus Torvalds 		.name	= "adi",
5631da177e4SLinus Torvalds 	},
5641da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
5651da177e4SLinus Torvalds 	.connect	= adi_connect,
5661da177e4SLinus Torvalds 	.disconnect	= adi_disconnect,
5671da177e4SLinus Torvalds };
5681da177e4SLinus Torvalds 
569*98a84131SAxel Lin module_gameport_driver(adi_drv);
570