11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) 2001 Vojtech Pavlik
31da177e4SLinus Torvalds  *
41da177e4SLinus Torvalds  *  Based on the work of:
51da177e4SLinus Torvalds  *	Toby Deshane
61da177e4SLinus Torvalds  */
71da177e4SLinus Torvalds 
81da177e4SLinus Torvalds /*
91da177e4SLinus Torvalds  * InterAct digital gamepad/joystick driver for Linux
101da177e4SLinus Torvalds  */
111da177e4SLinus Torvalds 
121da177e4SLinus Torvalds /*
131da177e4SLinus Torvalds  * This program is free software; you can redistribute it and/or modify
141da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
151da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
161da177e4SLinus Torvalds  * (at your option) any later version.
171da177e4SLinus Torvalds  *
181da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
191da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
201da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
211da177e4SLinus Torvalds  * GNU General Public License for more details.
221da177e4SLinus Torvalds  *
231da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
241da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
251da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
261da177e4SLinus Torvalds  *
271da177e4SLinus Torvalds  * Should you need to contact me, the author, you can do so either by
281da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
291da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
301da177e4SLinus Torvalds  */
311da177e4SLinus Torvalds 
321da177e4SLinus Torvalds #include <linux/kernel.h>
331da177e4SLinus Torvalds #include <linux/slab.h>
341da177e4SLinus Torvalds #include <linux/module.h>
351da177e4SLinus Torvalds #include <linux/delay.h>
361da177e4SLinus Torvalds #include <linux/init.h>
371da177e4SLinus Torvalds #include <linux/gameport.h>
381da177e4SLinus Torvalds #include <linux/input.h>
394e57b681STim Schmielau #include <linux/jiffies.h>
401da177e4SLinus Torvalds 
411da177e4SLinus Torvalds #define DRIVER_DESC	"InterAct digital joystick driver"
421da177e4SLinus Torvalds 
431da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
441da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
451da177e4SLinus Torvalds MODULE_LICENSE("GPL");
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds #define INTERACT_MAX_START	600	/* 400 us */
481da177e4SLinus Torvalds #define INTERACT_MAX_STROBE	60	/* 40 us */
491da177e4SLinus Torvalds #define INTERACT_MAX_LENGTH	32	/* 32 bits */
501da177e4SLinus Torvalds 
511da177e4SLinus Torvalds #define INTERACT_TYPE_HHFX	0	/* HammerHead/FX */
521da177e4SLinus Torvalds #define INTERACT_TYPE_PP8D	1	/* ProPad 8 */
531da177e4SLinus Torvalds 
541da177e4SLinus Torvalds struct interact {
551da177e4SLinus Torvalds 	struct gameport *gameport;
5617dd3f0fSDmitry Torokhov 	struct input_dev *dev;
571da177e4SLinus Torvalds 	int bads;
581da177e4SLinus Torvalds 	int reads;
591da177e4SLinus Torvalds 	unsigned char type;
601da177e4SLinus Torvalds 	unsigned char length;
611da177e4SLinus Torvalds 	char phys[32];
621da177e4SLinus Torvalds };
631da177e4SLinus Torvalds 
641da177e4SLinus Torvalds static short interact_abs_hhfx[] =
651da177e4SLinus Torvalds 	{ ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
661da177e4SLinus Torvalds static short interact_abs_pp8d[] =
671da177e4SLinus Torvalds 	{ ABS_X, ABS_Y, -1 };
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds static short interact_btn_hhfx[] =
701da177e4SLinus Torvalds 	{ BTN_TR, BTN_X, BTN_Y, BTN_Z, BTN_A, BTN_B, BTN_C, BTN_TL, BTN_TL2, BTN_TR2, BTN_MODE, BTN_SELECT, -1 };
711da177e4SLinus Torvalds static short interact_btn_pp8d[] =
721da177e4SLinus Torvalds 	{ BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
731da177e4SLinus Torvalds 
741da177e4SLinus Torvalds struct interact_type {
751da177e4SLinus Torvalds 	int id;
761da177e4SLinus Torvalds 	short *abs;
771da177e4SLinus Torvalds 	short *btn;
781da177e4SLinus Torvalds 	char *name;
791da177e4SLinus Torvalds 	unsigned char length;
801da177e4SLinus Torvalds 	unsigned char b8;
811da177e4SLinus Torvalds };
821da177e4SLinus Torvalds 
831da177e4SLinus Torvalds static struct interact_type interact_type[] = {
841da177e4SLinus Torvalds 	{ 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX",    32, 4 },
851da177e4SLinus Torvalds 	{ 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
861da177e4SLinus Torvalds 	{ 0 }};
871da177e4SLinus Torvalds 
881da177e4SLinus Torvalds /*
891da177e4SLinus Torvalds  * interact_read_packet() reads and InterAct joystick data.
901da177e4SLinus Torvalds  */
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
931da177e4SLinus Torvalds {
941da177e4SLinus Torvalds 	unsigned long flags;
951da177e4SLinus Torvalds 	unsigned char u, v;
961da177e4SLinus Torvalds 	unsigned int t, s;
971da177e4SLinus Torvalds 	int i;
981da177e4SLinus Torvalds 
991da177e4SLinus Torvalds 	i = 0;
1001da177e4SLinus Torvalds 	data[0] = data[1] = data[2] = 0;
1011da177e4SLinus Torvalds 	t = gameport_time(gameport, INTERACT_MAX_START);
1021da177e4SLinus Torvalds 	s = gameport_time(gameport, INTERACT_MAX_STROBE);
1031da177e4SLinus Torvalds 
1041da177e4SLinus Torvalds 	local_irq_save(flags);
1051da177e4SLinus Torvalds 	gameport_trigger(gameport);
1061da177e4SLinus Torvalds 	v = gameport_read(gameport);
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds 	while (t > 0 && i < length) {
1091da177e4SLinus Torvalds 		t--;
1101da177e4SLinus Torvalds 		u = v; v = gameport_read(gameport);
1111da177e4SLinus Torvalds 		if (v & ~u & 0x40) {
1121da177e4SLinus Torvalds 			data[0] = (data[0] << 1) | ((v >> 4) & 1);
1131da177e4SLinus Torvalds 			data[1] = (data[1] << 1) | ((v >> 5) & 1);
1141da177e4SLinus Torvalds 			data[2] = (data[2] << 1) | ((v >> 7) & 1);
1151da177e4SLinus Torvalds 			i++;
1161da177e4SLinus Torvalds 			t = s;
1171da177e4SLinus Torvalds 		}
1181da177e4SLinus Torvalds 	}
1191da177e4SLinus Torvalds 
1201da177e4SLinus Torvalds 	local_irq_restore(flags);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	return i;
1231da177e4SLinus Torvalds }
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds /*
1261da177e4SLinus Torvalds  * interact_poll() reads and analyzes InterAct joystick data.
1271da177e4SLinus Torvalds  */
1281da177e4SLinus Torvalds 
1291da177e4SLinus Torvalds static void interact_poll(struct gameport *gameport)
1301da177e4SLinus Torvalds {
1311da177e4SLinus Torvalds 	struct interact *interact = gameport_get_drvdata(gameport);
13217dd3f0fSDmitry Torokhov 	struct input_dev *dev = interact->dev;
1331da177e4SLinus Torvalds 	u32 data[3];
1341da177e4SLinus Torvalds 	int i;
1351da177e4SLinus Torvalds 
1361da177e4SLinus Torvalds 	interact->reads++;
1371da177e4SLinus Torvalds 
1381da177e4SLinus Torvalds 	if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
1391da177e4SLinus Torvalds 		interact->bads++;
1401da177e4SLinus Torvalds 	} else {
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
1431da177e4SLinus Torvalds 			data[i] <<= INTERACT_MAX_LENGTH - interact->length;
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 		switch (interact->type) {
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 			case INTERACT_TYPE_HHFX:
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 				for (i = 0; i < 4; i++)
1501da177e4SLinus Torvalds 					input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
1511da177e4SLinus Torvalds 
1521da177e4SLinus Torvalds 				for (i = 0; i < 2; i++)
1531da177e4SLinus Torvalds 					input_report_abs(dev, ABS_HAT0Y - i,
1541da177e4SLinus Torvalds 						((data[1] >> ((i << 1) + 17)) & 1)  - ((data[1] >> ((i << 1) + 16)) & 1));
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 				for (i = 0; i < 8; i++)
1571da177e4SLinus Torvalds 					input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
1581da177e4SLinus Torvalds 
1591da177e4SLinus Torvalds 				for (i = 0; i < 4; i++)
1601da177e4SLinus Torvalds 					input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
1611da177e4SLinus Torvalds 
1621da177e4SLinus Torvalds 				break;
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 			case INTERACT_TYPE_PP8D:
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 				for (i = 0; i < 2; i++)
1671da177e4SLinus Torvalds 					input_report_abs(dev, interact_abs_pp8d[i],
1681da177e4SLinus Torvalds 						((data[0] >> ((i << 1) + 20)) & 1)  - ((data[0] >> ((i << 1) + 21)) & 1));
1691da177e4SLinus Torvalds 
1701da177e4SLinus Torvalds 				for (i = 0; i < 8; i++)
1711da177e4SLinus Torvalds 					input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
1721da177e4SLinus Torvalds 
1731da177e4SLinus Torvalds 				break;
1741da177e4SLinus Torvalds 		}
1751da177e4SLinus Torvalds 	}
1761da177e4SLinus Torvalds 
1771da177e4SLinus Torvalds 	input_sync(dev);
1781da177e4SLinus Torvalds }
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds /*
1811da177e4SLinus Torvalds  * interact_open() is a callback from the input open routine.
1821da177e4SLinus Torvalds  */
1831da177e4SLinus Torvalds 
1841da177e4SLinus Torvalds static int interact_open(struct input_dev *dev)
1851da177e4SLinus Torvalds {
1868715c1cfSDmitry Torokhov 	struct interact *interact = input_get_drvdata(dev);
1871da177e4SLinus Torvalds 
1881da177e4SLinus Torvalds 	gameport_start_polling(interact->gameport);
1891da177e4SLinus Torvalds 	return 0;
1901da177e4SLinus Torvalds }
1911da177e4SLinus Torvalds 
1921da177e4SLinus Torvalds /*
1931da177e4SLinus Torvalds  * interact_close() is a callback from the input close routine.
1941da177e4SLinus Torvalds  */
1951da177e4SLinus Torvalds 
1961da177e4SLinus Torvalds static void interact_close(struct input_dev *dev)
1971da177e4SLinus Torvalds {
1988715c1cfSDmitry Torokhov 	struct interact *interact = input_get_drvdata(dev);
1991da177e4SLinus Torvalds 
2001da177e4SLinus Torvalds 	gameport_stop_polling(interact->gameport);
2011da177e4SLinus Torvalds }
2021da177e4SLinus Torvalds 
2031da177e4SLinus Torvalds /*
2041da177e4SLinus Torvalds  * interact_connect() probes for InterAct joysticks.
2051da177e4SLinus Torvalds  */
2061da177e4SLinus Torvalds 
2071da177e4SLinus Torvalds static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
2081da177e4SLinus Torvalds {
2091da177e4SLinus Torvalds 	struct interact *interact;
21017dd3f0fSDmitry Torokhov 	struct input_dev *input_dev;
2111da177e4SLinus Torvalds 	__u32 data[3];
2121da177e4SLinus Torvalds 	int i, t;
2131da177e4SLinus Torvalds 	int err;
2141da177e4SLinus Torvalds 
21517dd3f0fSDmitry Torokhov 	interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
21617dd3f0fSDmitry Torokhov 	input_dev = input_allocate_device();
21717dd3f0fSDmitry Torokhov 	if (!interact || !input_dev) {
21817dd3f0fSDmitry Torokhov 		err = -ENOMEM;
21917dd3f0fSDmitry Torokhov 		goto fail1;
22017dd3f0fSDmitry Torokhov 	}
2211da177e4SLinus Torvalds 
2221da177e4SLinus Torvalds 	interact->gameport = gameport;
22317dd3f0fSDmitry Torokhov 	interact->dev = input_dev;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, interact);
2261da177e4SLinus Torvalds 
2271da177e4SLinus Torvalds 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
2281da177e4SLinus Torvalds 	if (err)
2291da177e4SLinus Torvalds 		goto fail1;
2301da177e4SLinus Torvalds 
2311da177e4SLinus Torvalds 	i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
2321da177e4SLinus Torvalds 
2331da177e4SLinus Torvalds 	if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
2341da177e4SLinus Torvalds 		err = -ENODEV;
2351da177e4SLinus Torvalds 		goto fail2;
2361da177e4SLinus Torvalds 	}
2371da177e4SLinus Torvalds 
2381da177e4SLinus Torvalds 	for (i = 0; interact_type[i].length; i++)
2391da177e4SLinus Torvalds 		if (interact_type[i].id == (data[2] >> 16))
2401da177e4SLinus Torvalds 			break;
2411da177e4SLinus Torvalds 
2421da177e4SLinus Torvalds 	if (!interact_type[i].length) {
2431da177e4SLinus Torvalds 		printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
2441da177e4SLinus Torvalds 			gameport->phys, i, data[0], data[1], data[2]);
2451da177e4SLinus Torvalds 		err = -ENODEV;
2461da177e4SLinus Torvalds 		goto fail2;
2471da177e4SLinus Torvalds 	}
2481da177e4SLinus Torvalds 
2491da177e4SLinus Torvalds 	gameport_set_poll_handler(gameport, interact_poll);
2501da177e4SLinus Torvalds 	gameport_set_poll_interval(gameport, 20);
2511da177e4SLinus Torvalds 
25210ca4c0aSDmitry Torokhov 	snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
2531da177e4SLinus Torvalds 
2541da177e4SLinus Torvalds 	interact->type = i;
2551da177e4SLinus Torvalds 	interact->length = interact_type[i].length;
2561da177e4SLinus Torvalds 
25717dd3f0fSDmitry Torokhov 	input_dev->name = interact_type[i].name;
25817dd3f0fSDmitry Torokhov 	input_dev->phys = interact->phys;
25917dd3f0fSDmitry Torokhov 	input_dev->id.bustype = BUS_GAMEPORT;
26017dd3f0fSDmitry Torokhov 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
26117dd3f0fSDmitry Torokhov 	input_dev->id.product = interact_type[i].id;
26217dd3f0fSDmitry Torokhov 	input_dev->id.version = 0x0100;
263935e658eSDmitry Torokhov 	input_dev->dev.parent = &gameport->dev;
2648715c1cfSDmitry Torokhov 
2658715c1cfSDmitry Torokhov 	input_set_drvdata(input_dev, interact);
2661da177e4SLinus Torvalds 
26717dd3f0fSDmitry Torokhov 	input_dev->open = interact_open;
26817dd3f0fSDmitry Torokhov 	input_dev->close = interact_close;
2691da177e4SLinus Torvalds 
2707b19ada2SJiri Slaby 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2711da177e4SLinus Torvalds 
2721da177e4SLinus Torvalds 	for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
273987a6c02SDaniel Mack 		if (i < interact_type[interact->type].b8)
274987a6c02SDaniel Mack 			input_set_abs_params(input_dev, t, 0, 255, 0, 0);
275987a6c02SDaniel Mack 		else
276987a6c02SDaniel Mack 			input_set_abs_params(input_dev, t, -1, 1, 0, 0);
2771da177e4SLinus Torvalds 	}
2781da177e4SLinus Torvalds 
2791da177e4SLinus Torvalds 	for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
280987a6c02SDaniel Mack 		__set_bit(t, input_dev->keybit);
2811da177e4SLinus Torvalds 
282127278ceSDmitry Torokhov 	err = input_register_device(interact->dev);
283127278ceSDmitry Torokhov 	if (err)
284127278ceSDmitry Torokhov 		goto fail2;
2851da177e4SLinus Torvalds 
2861da177e4SLinus Torvalds 	return 0;
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds fail2:	gameport_close(gameport);
2891da177e4SLinus Torvalds fail1:  gameport_set_drvdata(gameport, NULL);
29017dd3f0fSDmitry Torokhov 	input_free_device(input_dev);
2911da177e4SLinus Torvalds 	kfree(interact);
2921da177e4SLinus Torvalds 	return err;
2931da177e4SLinus Torvalds }
2941da177e4SLinus Torvalds 
2951da177e4SLinus Torvalds static void interact_disconnect(struct gameport *gameport)
2961da177e4SLinus Torvalds {
2971da177e4SLinus Torvalds 	struct interact *interact = gameport_get_drvdata(gameport);
2981da177e4SLinus Torvalds 
29917dd3f0fSDmitry Torokhov 	input_unregister_device(interact->dev);
3001da177e4SLinus Torvalds 	gameport_close(gameport);
3011da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, NULL);
3021da177e4SLinus Torvalds 	kfree(interact);
3031da177e4SLinus Torvalds }
3041da177e4SLinus Torvalds 
3051da177e4SLinus Torvalds static struct gameport_driver interact_drv = {
3061da177e4SLinus Torvalds 	.driver		= {
3071da177e4SLinus Torvalds 		.name	= "interact",
3081da177e4SLinus Torvalds 	},
3091da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
3101da177e4SLinus Torvalds 	.connect	= interact_connect,
3111da177e4SLinus Torvalds 	.disconnect	= interact_disconnect,
3121da177e4SLinus Torvalds };
3131da177e4SLinus Torvalds 
31498a84131SAxel Lin module_gameport_driver(interact_drv);
315