xref: /linux/drivers/input/joystick/stinger.c (revision a1ff5a7d78a036d6c2178ee5acd6ba4946243800)
1e8fc9828SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
21da177e4SLinus Torvalds /*
31da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 Vojtech Pavlik
41da177e4SLinus Torvalds  *  Copyright (c) 2000 Mark Fletcher
51da177e4SLinus Torvalds  */
61da177e4SLinus Torvalds 
71da177e4SLinus Torvalds /*
81da177e4SLinus Torvalds  * Gravis Stinger gamepad driver for Linux
91da177e4SLinus Torvalds  */
101da177e4SLinus Torvalds 
111da177e4SLinus Torvalds #include <linux/kernel.h>
121da177e4SLinus Torvalds #include <linux/module.h>
131da177e4SLinus Torvalds #include <linux/slab.h>
141da177e4SLinus Torvalds #include <linux/input.h>
151da177e4SLinus Torvalds #include <linux/serio.h>
161da177e4SLinus Torvalds 
171da177e4SLinus Torvalds #define DRIVER_DESC	"Gravis Stinger gamepad driver"
181da177e4SLinus Torvalds 
191da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
201da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
211da177e4SLinus Torvalds MODULE_LICENSE("GPL");
221da177e4SLinus Torvalds 
231da177e4SLinus Torvalds /*
241da177e4SLinus Torvalds  * Constants.
251da177e4SLinus Torvalds  */
261da177e4SLinus Torvalds 
271da177e4SLinus Torvalds #define STINGER_MAX_LENGTH 8
281da177e4SLinus Torvalds 
291da177e4SLinus Torvalds /*
301da177e4SLinus Torvalds  * Per-Stinger data.
311da177e4SLinus Torvalds  */
321da177e4SLinus Torvalds 
331da177e4SLinus Torvalds struct stinger {
3417dd3f0fSDmitry Torokhov 	struct input_dev *dev;
351da177e4SLinus Torvalds 	int idx;
361da177e4SLinus Torvalds 	unsigned char data[STINGER_MAX_LENGTH];
371da177e4SLinus Torvalds 	char phys[32];
381da177e4SLinus Torvalds };
391da177e4SLinus Torvalds 
401da177e4SLinus Torvalds /*
411da177e4SLinus Torvalds  * stinger_process_packet() decodes packets the driver receives from the
421da177e4SLinus Torvalds  * Stinger. It updates the data accordingly.
431da177e4SLinus Torvalds  */
441da177e4SLinus Torvalds 
stinger_process_packet(struct stinger * stinger)457d12e780SDavid Howells static void stinger_process_packet(struct stinger *stinger)
461da177e4SLinus Torvalds {
4717dd3f0fSDmitry Torokhov 	struct input_dev *dev = stinger->dev;
481da177e4SLinus Torvalds 	unsigned char *data = stinger->data;
491da177e4SLinus Torvalds 
501da177e4SLinus Torvalds 	if (!stinger->idx) return;
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds 	input_report_key(dev, BTN_A,	  ((data[0] & 0x20) >> 5));
531da177e4SLinus Torvalds 	input_report_key(dev, BTN_B,	  ((data[0] & 0x10) >> 4));
541da177e4SLinus Torvalds 	input_report_key(dev, BTN_C,	  ((data[0] & 0x08) >> 3));
551da177e4SLinus Torvalds 	input_report_key(dev, BTN_X,	  ((data[0] & 0x04) >> 2));
561da177e4SLinus Torvalds 	input_report_key(dev, BTN_Y,	  ((data[3] & 0x20) >> 5));
571da177e4SLinus Torvalds 	input_report_key(dev, BTN_Z,	  ((data[3] & 0x10) >> 4));
581da177e4SLinus Torvalds 	input_report_key(dev, BTN_TL,     ((data[3] & 0x08) >> 3));
591da177e4SLinus Torvalds 	input_report_key(dev, BTN_TR,     ((data[3] & 0x04) >> 2));
601da177e4SLinus Torvalds 	input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
611da177e4SLinus Torvalds 	input_report_key(dev, BTN_START,   (data[3] & 0x01));
621da177e4SLinus Torvalds 
631da177e4SLinus Torvalds 	input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
641da177e4SLinus Torvalds 	input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
651da177e4SLinus Torvalds 
661da177e4SLinus Torvalds 	input_sync(dev);
671da177e4SLinus Torvalds 
681da177e4SLinus Torvalds 	return;
691da177e4SLinus Torvalds }
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds /*
721da177e4SLinus Torvalds  * stinger_interrupt() is called by the low level driver when characters
731da177e4SLinus Torvalds  * are ready for us. We then buffer them for further processing, or call the
741da177e4SLinus Torvalds  * packet processing routine.
751da177e4SLinus Torvalds  */
761da177e4SLinus Torvalds 
stinger_interrupt(struct serio * serio,unsigned char data,unsigned int flags)771da177e4SLinus Torvalds static irqreturn_t stinger_interrupt(struct serio *serio,
787d12e780SDavid Howells 	unsigned char data, unsigned int flags)
791da177e4SLinus Torvalds {
801da177e4SLinus Torvalds 	struct stinger *stinger = serio_get_drvdata(serio);
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	/* All Stinger packets are 4 bytes */
831da177e4SLinus Torvalds 
841da177e4SLinus Torvalds 	if (stinger->idx < STINGER_MAX_LENGTH)
851da177e4SLinus Torvalds 		stinger->data[stinger->idx++] = data;
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	if (stinger->idx == 4) {
887d12e780SDavid Howells 		stinger_process_packet(stinger);
891da177e4SLinus Torvalds 		stinger->idx = 0;
901da177e4SLinus Torvalds 	}
911da177e4SLinus Torvalds 
921da177e4SLinus Torvalds 	return IRQ_HANDLED;
931da177e4SLinus Torvalds }
941da177e4SLinus Torvalds 
951da177e4SLinus Torvalds /*
961da177e4SLinus Torvalds  * stinger_disconnect() is the opposite of stinger_connect()
971da177e4SLinus Torvalds  */
981da177e4SLinus Torvalds 
stinger_disconnect(struct serio * serio)991da177e4SLinus Torvalds static void stinger_disconnect(struct serio *serio)
1001da177e4SLinus Torvalds {
1011da177e4SLinus Torvalds 	struct stinger *stinger = serio_get_drvdata(serio);
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	serio_close(serio);
1041da177e4SLinus Torvalds 	serio_set_drvdata(serio, NULL);
10517dd3f0fSDmitry Torokhov 	input_unregister_device(stinger->dev);
1061da177e4SLinus Torvalds 	kfree(stinger);
1071da177e4SLinus Torvalds }
1081da177e4SLinus Torvalds 
1091da177e4SLinus Torvalds /*
1101da177e4SLinus Torvalds  * stinger_connect() is the routine that is called when someone adds a
1111da177e4SLinus Torvalds  * new serio device that supports Stinger protocol and registers it as
1121da177e4SLinus Torvalds  * an input device.
1131da177e4SLinus Torvalds  */
1141da177e4SLinus Torvalds 
stinger_connect(struct serio * serio,struct serio_driver * drv)1151da177e4SLinus Torvalds static int stinger_connect(struct serio *serio, struct serio_driver *drv)
1161da177e4SLinus Torvalds {
1171da177e4SLinus Torvalds 	struct stinger *stinger;
11817dd3f0fSDmitry Torokhov 	struct input_dev *input_dev;
11917dd3f0fSDmitry Torokhov 	int err = -ENOMEM;
1201da177e4SLinus Torvalds 
121*4654c4ccSErick Archer 	stinger = kmalloc(sizeof(*stinger), GFP_KERNEL);
12217dd3f0fSDmitry Torokhov 	input_dev = input_allocate_device();
12317dd3f0fSDmitry Torokhov 	if (!stinger || !input_dev)
124127278ceSDmitry Torokhov 		goto fail1;
1251da177e4SLinus Torvalds 
12617dd3f0fSDmitry Torokhov 	stinger->dev = input_dev;
12710ca4c0aSDmitry Torokhov 	snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
1281da177e4SLinus Torvalds 
12917dd3f0fSDmitry Torokhov 	input_dev->name = "Gravis Stinger";
13017dd3f0fSDmitry Torokhov 	input_dev->phys = stinger->phys;
13117dd3f0fSDmitry Torokhov 	input_dev->id.bustype = BUS_RS232;
13217dd3f0fSDmitry Torokhov 	input_dev->id.vendor = SERIO_STINGER;
13317dd3f0fSDmitry Torokhov 	input_dev->id.product = 0x0001;
13417dd3f0fSDmitry Torokhov 	input_dev->id.version = 0x0100;
135935e658eSDmitry Torokhov 	input_dev->dev.parent = &serio->dev;
1361da177e4SLinus Torvalds 
1377b19ada2SJiri Slaby 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1387b19ada2SJiri Slaby 	input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
1397b19ada2SJiri Slaby 		BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
1407b19ada2SJiri Slaby 		BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
1417b19ada2SJiri Slaby 		BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
14217dd3f0fSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
14317dd3f0fSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
1441da177e4SLinus Torvalds 
1451da177e4SLinus Torvalds 	serio_set_drvdata(serio, stinger);
1461da177e4SLinus Torvalds 
1471da177e4SLinus Torvalds 	err = serio_open(serio, drv);
14817dd3f0fSDmitry Torokhov 	if (err)
149127278ceSDmitry Torokhov 		goto fail2;
15017dd3f0fSDmitry Torokhov 
151127278ceSDmitry Torokhov 	err = input_register_device(stinger->dev);
152127278ceSDmitry Torokhov 	if (err)
153127278ceSDmitry Torokhov 		goto fail3;
154127278ceSDmitry Torokhov 
15517dd3f0fSDmitry Torokhov 	return 0;
15617dd3f0fSDmitry Torokhov 
157127278ceSDmitry Torokhov  fail3:	serio_close(serio);
158127278ceSDmitry Torokhov  fail2:	serio_set_drvdata(serio, NULL);
159127278ceSDmitry Torokhov  fail1:	input_free_device(input_dev);
1601da177e4SLinus Torvalds 	kfree(stinger);
1611da177e4SLinus Torvalds 	return err;
1621da177e4SLinus Torvalds }
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds /*
1651da177e4SLinus Torvalds  * The serio driver structure.
1661da177e4SLinus Torvalds  */
1671da177e4SLinus Torvalds 
168fa0426c6SArvind Yadav static const struct serio_device_id stinger_serio_ids[] = {
1691da177e4SLinus Torvalds 	{
1701da177e4SLinus Torvalds 		.type	= SERIO_RS232,
1711da177e4SLinus Torvalds 		.proto	= SERIO_STINGER,
1721da177e4SLinus Torvalds 		.id	= SERIO_ANY,
1731da177e4SLinus Torvalds 		.extra	= SERIO_ANY,
1741da177e4SLinus Torvalds 	},
1751da177e4SLinus Torvalds 	{ 0 }
1761da177e4SLinus Torvalds };
1771da177e4SLinus Torvalds 
1781da177e4SLinus Torvalds MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
1791da177e4SLinus Torvalds 
1801da177e4SLinus Torvalds static struct serio_driver stinger_drv = {
1811da177e4SLinus Torvalds 	.driver		= {
1821da177e4SLinus Torvalds 		.name	= "stinger",
1831da177e4SLinus Torvalds 	},
1841da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
1851da177e4SLinus Torvalds 	.id_table	= stinger_serio_ids,
1861da177e4SLinus Torvalds 	.interrupt	= stinger_interrupt,
1871da177e4SLinus Torvalds 	.connect	= stinger_connect,
1881da177e4SLinus Torvalds 	.disconnect	= stinger_disconnect,
1891da177e4SLinus Torvalds };
1901da177e4SLinus Torvalds 
19165ac9f7aSAxel Lin module_serio_driver(stinger_drv);
192