11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (c) 2001 Vojtech Pavlik
41da177e4SLinus Torvalds  *
51da177e4SLinus Torvalds  *  Based on the work of:
61da177e4SLinus Torvalds  *	Toby Deshane
71da177e4SLinus Torvalds  */
81da177e4SLinus Torvalds 
91da177e4SLinus Torvalds /*
101da177e4SLinus Torvalds  * InterAct digital gamepad/joystick driver for Linux
111da177e4SLinus Torvalds  */
121da177e4SLinus Torvalds 
131da177e4SLinus Torvalds /*
141da177e4SLinus Torvalds  */
151da177e4SLinus Torvalds 
161da177e4SLinus Torvalds #include <linux/kernel.h>
171da177e4SLinus Torvalds #include <linux/slab.h>
181da177e4SLinus Torvalds #include <linux/module.h>
191da177e4SLinus Torvalds #include <linux/delay.h>
201da177e4SLinus Torvalds #include <linux/gameport.h>
211da177e4SLinus Torvalds #include <linux/input.h>
224e57b681STim Schmielau #include <linux/jiffies.h>
231da177e4SLinus Torvalds 
241da177e4SLinus Torvalds #define DRIVER_DESC	"InterAct digital joystick driver"
251da177e4SLinus Torvalds 
261da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
271da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
281da177e4SLinus Torvalds MODULE_LICENSE("GPL");
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #define INTERACT_MAX_START	600	/* 400 us */
311da177e4SLinus Torvalds #define INTERACT_MAX_STROBE	60	/* 40 us */
321da177e4SLinus Torvalds #define INTERACT_MAX_LENGTH	32	/* 32 bits */
331da177e4SLinus Torvalds 
341da177e4SLinus Torvalds #define INTERACT_TYPE_HHFX	0	/* HammerHead/FX */
351da177e4SLinus Torvalds #define INTERACT_TYPE_PP8D	1	/* ProPad 8 */
361da177e4SLinus Torvalds 
371da177e4SLinus Torvalds struct interact {
381da177e4SLinus Torvalds 	struct gameport *gameport;
3917dd3f0fSDmitry Torokhov 	struct input_dev *dev;
401da177e4SLinus Torvalds 	int bads;
411da177e4SLinus Torvalds 	int reads;
421da177e4SLinus Torvalds 	unsigned char type;
431da177e4SLinus Torvalds 	unsigned char length;
441da177e4SLinus Torvalds 	char phys[32];
451da177e4SLinus Torvalds };
461da177e4SLinus Torvalds 
471da177e4SLinus Torvalds static short interact_abs_hhfx[] =
481da177e4SLinus Torvalds 	{ ABS_RX, ABS_RY, ABS_X, ABS_Y, ABS_HAT0X, ABS_HAT0Y, -1 };
491da177e4SLinus Torvalds static short interact_abs_pp8d[] =
501da177e4SLinus Torvalds 	{ ABS_X, ABS_Y, -1 };
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds static short interact_btn_hhfx[] =
531da177e4SLinus 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 };
541da177e4SLinus Torvalds static short interact_btn_pp8d[] =
551da177e4SLinus Torvalds 	{ BTN_C, BTN_TL, BTN_TR, BTN_A, BTN_B, BTN_Y, BTN_Z, BTN_X, -1 };
561da177e4SLinus Torvalds 
571da177e4SLinus Torvalds struct interact_type {
581da177e4SLinus Torvalds 	int id;
591da177e4SLinus Torvalds 	short *abs;
601da177e4SLinus Torvalds 	short *btn;
611da177e4SLinus Torvalds 	char *name;
621da177e4SLinus Torvalds 	unsigned char length;
631da177e4SLinus Torvalds 	unsigned char b8;
641da177e4SLinus Torvalds };
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds static struct interact_type interact_type[] = {
671da177e4SLinus Torvalds 	{ 0x6202, interact_abs_hhfx, interact_btn_hhfx, "InterAct HammerHead/FX",    32, 4 },
681da177e4SLinus Torvalds 	{ 0x53f8, interact_abs_pp8d, interact_btn_pp8d, "InterAct ProPad 8 Digital", 16, 0 },
691da177e4SLinus Torvalds 	{ 0 }};
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds  * interact_read_packet() reads and InterAct joystick data.
731da177e4SLinus Torvalds  */
741da177e4SLinus Torvalds 
751da177e4SLinus Torvalds static int interact_read_packet(struct gameport *gameport, int length, u32 *data)
761da177e4SLinus Torvalds {
771da177e4SLinus Torvalds 	unsigned long flags;
781da177e4SLinus Torvalds 	unsigned char u, v;
791da177e4SLinus Torvalds 	unsigned int t, s;
801da177e4SLinus Torvalds 	int i;
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	i = 0;
831da177e4SLinus Torvalds 	data[0] = data[1] = data[2] = 0;
841da177e4SLinus Torvalds 	t = gameport_time(gameport, INTERACT_MAX_START);
851da177e4SLinus Torvalds 	s = gameport_time(gameport, INTERACT_MAX_STROBE);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	local_irq_save(flags);
881da177e4SLinus Torvalds 	gameport_trigger(gameport);
891da177e4SLinus Torvalds 	v = gameport_read(gameport);
901da177e4SLinus Torvalds 
911da177e4SLinus Torvalds 	while (t > 0 && i < length) {
921da177e4SLinus Torvalds 		t--;
931da177e4SLinus Torvalds 		u = v; v = gameport_read(gameport);
941da177e4SLinus Torvalds 		if (v & ~u & 0x40) {
951da177e4SLinus Torvalds 			data[0] = (data[0] << 1) | ((v >> 4) & 1);
961da177e4SLinus Torvalds 			data[1] = (data[1] << 1) | ((v >> 5) & 1);
971da177e4SLinus Torvalds 			data[2] = (data[2] << 1) | ((v >> 7) & 1);
981da177e4SLinus Torvalds 			i++;
991da177e4SLinus Torvalds 			t = s;
1001da177e4SLinus Torvalds 		}
1011da177e4SLinus Torvalds 	}
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	local_irq_restore(flags);
1041da177e4SLinus Torvalds 
1051da177e4SLinus Torvalds 	return i;
1061da177e4SLinus Torvalds }
1071da177e4SLinus Torvalds 
1081da177e4SLinus Torvalds /*
1091da177e4SLinus Torvalds  * interact_poll() reads and analyzes InterAct joystick data.
1101da177e4SLinus Torvalds  */
1111da177e4SLinus Torvalds 
1121da177e4SLinus Torvalds static void interact_poll(struct gameport *gameport)
1131da177e4SLinus Torvalds {
1141da177e4SLinus Torvalds 	struct interact *interact = gameport_get_drvdata(gameport);
11517dd3f0fSDmitry Torokhov 	struct input_dev *dev = interact->dev;
1161da177e4SLinus Torvalds 	u32 data[3];
1171da177e4SLinus Torvalds 	int i;
1181da177e4SLinus Torvalds 
1191da177e4SLinus Torvalds 	interact->reads++;
1201da177e4SLinus Torvalds 
1211da177e4SLinus Torvalds 	if (interact_read_packet(interact->gameport, interact->length, data) < interact->length) {
1221da177e4SLinus Torvalds 		interact->bads++;
1231da177e4SLinus Torvalds 	} else {
1241da177e4SLinus Torvalds 
1251da177e4SLinus Torvalds 		for (i = 0; i < 3; i++)
1261da177e4SLinus Torvalds 			data[i] <<= INTERACT_MAX_LENGTH - interact->length;
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds 		switch (interact->type) {
1291da177e4SLinus Torvalds 
1301da177e4SLinus Torvalds 			case INTERACT_TYPE_HHFX:
1311da177e4SLinus Torvalds 
1321da177e4SLinus Torvalds 				for (i = 0; i < 4; i++)
1331da177e4SLinus Torvalds 					input_report_abs(dev, interact_abs_hhfx[i], (data[i & 1] >> ((i >> 1) << 3)) & 0xff);
1341da177e4SLinus Torvalds 
1351da177e4SLinus Torvalds 				for (i = 0; i < 2; i++)
1361da177e4SLinus Torvalds 					input_report_abs(dev, ABS_HAT0Y - i,
1371da177e4SLinus Torvalds 						((data[1] >> ((i << 1) + 17)) & 1)  - ((data[1] >> ((i << 1) + 16)) & 1));
1381da177e4SLinus Torvalds 
1391da177e4SLinus Torvalds 				for (i = 0; i < 8; i++)
1401da177e4SLinus Torvalds 					input_report_key(dev, interact_btn_hhfx[i], (data[0] >> (i + 16)) & 1);
1411da177e4SLinus Torvalds 
1421da177e4SLinus Torvalds 				for (i = 0; i < 4; i++)
1431da177e4SLinus Torvalds 					input_report_key(dev, interact_btn_hhfx[i + 8], (data[1] >> (i + 20)) & 1);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 				break;
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 			case INTERACT_TYPE_PP8D:
1481da177e4SLinus Torvalds 
1491da177e4SLinus Torvalds 				for (i = 0; i < 2; i++)
1501da177e4SLinus Torvalds 					input_report_abs(dev, interact_abs_pp8d[i],
1511da177e4SLinus Torvalds 						((data[0] >> ((i << 1) + 20)) & 1)  - ((data[0] >> ((i << 1) + 21)) & 1));
1521da177e4SLinus Torvalds 
1531da177e4SLinus Torvalds 				for (i = 0; i < 8; i++)
1541da177e4SLinus Torvalds 					input_report_key(dev, interact_btn_pp8d[i], (data[1] >> (i + 16)) & 1);
1551da177e4SLinus Torvalds 
1561da177e4SLinus Torvalds 				break;
1571da177e4SLinus Torvalds 		}
1581da177e4SLinus Torvalds 	}
1591da177e4SLinus Torvalds 
1601da177e4SLinus Torvalds 	input_sync(dev);
1611da177e4SLinus Torvalds }
1621da177e4SLinus Torvalds 
1631da177e4SLinus Torvalds /*
1641da177e4SLinus Torvalds  * interact_open() is a callback from the input open routine.
1651da177e4SLinus Torvalds  */
1661da177e4SLinus Torvalds 
1671da177e4SLinus Torvalds static int interact_open(struct input_dev *dev)
1681da177e4SLinus Torvalds {
1698715c1cfSDmitry Torokhov 	struct interact *interact = input_get_drvdata(dev);
1701da177e4SLinus Torvalds 
1711da177e4SLinus Torvalds 	gameport_start_polling(interact->gameport);
1721da177e4SLinus Torvalds 	return 0;
1731da177e4SLinus Torvalds }
1741da177e4SLinus Torvalds 
1751da177e4SLinus Torvalds /*
1761da177e4SLinus Torvalds  * interact_close() is a callback from the input close routine.
1771da177e4SLinus Torvalds  */
1781da177e4SLinus Torvalds 
1791da177e4SLinus Torvalds static void interact_close(struct input_dev *dev)
1801da177e4SLinus Torvalds {
1818715c1cfSDmitry Torokhov 	struct interact *interact = input_get_drvdata(dev);
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds 	gameport_stop_polling(interact->gameport);
1841da177e4SLinus Torvalds }
1851da177e4SLinus Torvalds 
1861da177e4SLinus Torvalds /*
1871da177e4SLinus Torvalds  * interact_connect() probes for InterAct joysticks.
1881da177e4SLinus Torvalds  */
1891da177e4SLinus Torvalds 
1901da177e4SLinus Torvalds static int interact_connect(struct gameport *gameport, struct gameport_driver *drv)
1911da177e4SLinus Torvalds {
1921da177e4SLinus Torvalds 	struct interact *interact;
19317dd3f0fSDmitry Torokhov 	struct input_dev *input_dev;
1941da177e4SLinus Torvalds 	__u32 data[3];
1951da177e4SLinus Torvalds 	int i, t;
1961da177e4SLinus Torvalds 	int err;
1971da177e4SLinus Torvalds 
19817dd3f0fSDmitry Torokhov 	interact = kzalloc(sizeof(struct interact), GFP_KERNEL);
19917dd3f0fSDmitry Torokhov 	input_dev = input_allocate_device();
20017dd3f0fSDmitry Torokhov 	if (!interact || !input_dev) {
20117dd3f0fSDmitry Torokhov 		err = -ENOMEM;
20217dd3f0fSDmitry Torokhov 		goto fail1;
20317dd3f0fSDmitry Torokhov 	}
2041da177e4SLinus Torvalds 
2051da177e4SLinus Torvalds 	interact->gameport = gameport;
20617dd3f0fSDmitry Torokhov 	interact->dev = input_dev;
2071da177e4SLinus Torvalds 
2081da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, interact);
2091da177e4SLinus Torvalds 
2101da177e4SLinus Torvalds 	err = gameport_open(gameport, drv, GAMEPORT_MODE_RAW);
2111da177e4SLinus Torvalds 	if (err)
2121da177e4SLinus Torvalds 		goto fail1;
2131da177e4SLinus Torvalds 
2141da177e4SLinus Torvalds 	i = interact_read_packet(gameport, INTERACT_MAX_LENGTH * 2, data);
2151da177e4SLinus Torvalds 
2161da177e4SLinus Torvalds 	if (i != 32 || (data[0] >> 24) != 0x0c || (data[1] >> 24) != 0x02) {
2171da177e4SLinus Torvalds 		err = -ENODEV;
2181da177e4SLinus Torvalds 		goto fail2;
2191da177e4SLinus Torvalds 	}
2201da177e4SLinus Torvalds 
2211da177e4SLinus Torvalds 	for (i = 0; interact_type[i].length; i++)
2221da177e4SLinus Torvalds 		if (interact_type[i].id == (data[2] >> 16))
2231da177e4SLinus Torvalds 			break;
2241da177e4SLinus Torvalds 
2251da177e4SLinus Torvalds 	if (!interact_type[i].length) {
2261da177e4SLinus Torvalds 		printk(KERN_WARNING "interact.c: Unknown joystick on %s. [len %d d0 %08x d1 %08x i2 %08x]\n",
2271da177e4SLinus Torvalds 			gameport->phys, i, data[0], data[1], data[2]);
2281da177e4SLinus Torvalds 		err = -ENODEV;
2291da177e4SLinus Torvalds 		goto fail2;
2301da177e4SLinus Torvalds 	}
2311da177e4SLinus Torvalds 
2321da177e4SLinus Torvalds 	gameport_set_poll_handler(gameport, interact_poll);
2331da177e4SLinus Torvalds 	gameport_set_poll_interval(gameport, 20);
2341da177e4SLinus Torvalds 
23510ca4c0aSDmitry Torokhov 	snprintf(interact->phys, sizeof(interact->phys), "%s/input0", gameport->phys);
2361da177e4SLinus Torvalds 
2371da177e4SLinus Torvalds 	interact->type = i;
2381da177e4SLinus Torvalds 	interact->length = interact_type[i].length;
2391da177e4SLinus Torvalds 
24017dd3f0fSDmitry Torokhov 	input_dev->name = interact_type[i].name;
24117dd3f0fSDmitry Torokhov 	input_dev->phys = interact->phys;
24217dd3f0fSDmitry Torokhov 	input_dev->id.bustype = BUS_GAMEPORT;
24317dd3f0fSDmitry Torokhov 	input_dev->id.vendor = GAMEPORT_ID_VENDOR_INTERACT;
24417dd3f0fSDmitry Torokhov 	input_dev->id.product = interact_type[i].id;
24517dd3f0fSDmitry Torokhov 	input_dev->id.version = 0x0100;
246935e658eSDmitry Torokhov 	input_dev->dev.parent = &gameport->dev;
2478715c1cfSDmitry Torokhov 
2488715c1cfSDmitry Torokhov 	input_set_drvdata(input_dev, interact);
2491da177e4SLinus Torvalds 
25017dd3f0fSDmitry Torokhov 	input_dev->open = interact_open;
25117dd3f0fSDmitry Torokhov 	input_dev->close = interact_close;
2521da177e4SLinus Torvalds 
2537b19ada2SJiri Slaby 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
2541da177e4SLinus Torvalds 
2551da177e4SLinus Torvalds 	for (i = 0; (t = interact_type[interact->type].abs[i]) >= 0; i++) {
256987a6c02SDaniel Mack 		if (i < interact_type[interact->type].b8)
257987a6c02SDaniel Mack 			input_set_abs_params(input_dev, t, 0, 255, 0, 0);
258987a6c02SDaniel Mack 		else
259987a6c02SDaniel Mack 			input_set_abs_params(input_dev, t, -1, 1, 0, 0);
2601da177e4SLinus Torvalds 	}
2611da177e4SLinus Torvalds 
2621da177e4SLinus Torvalds 	for (i = 0; (t = interact_type[interact->type].btn[i]) >= 0; i++)
263987a6c02SDaniel Mack 		__set_bit(t, input_dev->keybit);
2641da177e4SLinus Torvalds 
265127278ceSDmitry Torokhov 	err = input_register_device(interact->dev);
266127278ceSDmitry Torokhov 	if (err)
267127278ceSDmitry Torokhov 		goto fail2;
2681da177e4SLinus Torvalds 
2691da177e4SLinus Torvalds 	return 0;
2701da177e4SLinus Torvalds 
2711da177e4SLinus Torvalds fail2:	gameport_close(gameport);
2721da177e4SLinus Torvalds fail1:  gameport_set_drvdata(gameport, NULL);
27317dd3f0fSDmitry Torokhov 	input_free_device(input_dev);
2741da177e4SLinus Torvalds 	kfree(interact);
2751da177e4SLinus Torvalds 	return err;
2761da177e4SLinus Torvalds }
2771da177e4SLinus Torvalds 
2781da177e4SLinus Torvalds static void interact_disconnect(struct gameport *gameport)
2791da177e4SLinus Torvalds {
2801da177e4SLinus Torvalds 	struct interact *interact = gameport_get_drvdata(gameport);
2811da177e4SLinus Torvalds 
28217dd3f0fSDmitry Torokhov 	input_unregister_device(interact->dev);
2831da177e4SLinus Torvalds 	gameport_close(gameport);
2841da177e4SLinus Torvalds 	gameport_set_drvdata(gameport, NULL);
2851da177e4SLinus Torvalds 	kfree(interact);
2861da177e4SLinus Torvalds }
2871da177e4SLinus Torvalds 
2881da177e4SLinus Torvalds static struct gameport_driver interact_drv = {
2891da177e4SLinus Torvalds 	.driver		= {
2901da177e4SLinus Torvalds 		.name	= "interact",
2911da177e4SLinus Torvalds 	},
2921da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
2931da177e4SLinus Torvalds 	.connect	= interact_connect,
2941da177e4SLinus Torvalds 	.disconnect	= interact_disconnect,
2951da177e4SLinus Torvalds };
2961da177e4SLinus Torvalds 
29798a84131SAxel Lin module_gameport_driver(interact_drv);
298