xref: /openbmc/linux/drivers/input/joystick/stinger.c (revision fa0426c6cfdf89726f4e9495d6870f01374459f9)
11da177e4SLinus Torvalds /*
21da177e4SLinus Torvalds  *  Copyright (c) 2000-2001 Vojtech Pavlik
31da177e4SLinus Torvalds  *  Copyright (c) 2000 Mark Fletcher
41da177e4SLinus Torvalds  */
51da177e4SLinus Torvalds 
61da177e4SLinus Torvalds /*
71da177e4SLinus Torvalds  * Gravis Stinger gamepad driver for Linux
81da177e4SLinus Torvalds  */
91da177e4SLinus Torvalds 
101da177e4SLinus Torvalds /*
111da177e4SLinus Torvalds  * This program is free warftware; you can redistribute it and/or modify
121da177e4SLinus Torvalds  * it under the terms of the GNU General Public License as published by
131da177e4SLinus Torvalds  * the Free Software Foundation; either version 2 of the License, or
141da177e4SLinus Torvalds  * (at your option) any later version.
151da177e4SLinus Torvalds  *
161da177e4SLinus Torvalds  * This program is distributed in the hope that it will be useful,
171da177e4SLinus Torvalds  * but WITHOUT ANY WARRANTY; without even the implied warranty of
181da177e4SLinus Torvalds  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
191da177e4SLinus Torvalds  * GNU General Public License for more details.
201da177e4SLinus Torvalds  *
211da177e4SLinus Torvalds  * You should have received a copy of the GNU General Public License
221da177e4SLinus Torvalds  * along with this program; if not, write to the Free Software
231da177e4SLinus Torvalds  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
241da177e4SLinus Torvalds  *
251da177e4SLinus Torvalds  *  Should you need to contact me, the author, you can do so either by
261da177e4SLinus Torvalds  * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
271da177e4SLinus Torvalds  * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
281da177e4SLinus Torvalds  */
291da177e4SLinus Torvalds 
301da177e4SLinus Torvalds #include <linux/kernel.h>
311da177e4SLinus Torvalds #include <linux/module.h>
321da177e4SLinus Torvalds #include <linux/slab.h>
331da177e4SLinus Torvalds #include <linux/input.h>
341da177e4SLinus Torvalds #include <linux/serio.h>
351da177e4SLinus Torvalds 
361da177e4SLinus Torvalds #define DRIVER_DESC	"Gravis Stinger gamepad driver"
371da177e4SLinus Torvalds 
381da177e4SLinus Torvalds MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
391da177e4SLinus Torvalds MODULE_DESCRIPTION(DRIVER_DESC);
401da177e4SLinus Torvalds MODULE_LICENSE("GPL");
411da177e4SLinus Torvalds 
421da177e4SLinus Torvalds /*
431da177e4SLinus Torvalds  * Constants.
441da177e4SLinus Torvalds  */
451da177e4SLinus Torvalds 
461da177e4SLinus Torvalds #define STINGER_MAX_LENGTH 8
471da177e4SLinus Torvalds 
481da177e4SLinus Torvalds /*
491da177e4SLinus Torvalds  * Per-Stinger data.
501da177e4SLinus Torvalds  */
511da177e4SLinus Torvalds 
521da177e4SLinus Torvalds struct stinger {
5317dd3f0fSDmitry Torokhov 	struct input_dev *dev;
541da177e4SLinus Torvalds 	int idx;
551da177e4SLinus Torvalds 	unsigned char data[STINGER_MAX_LENGTH];
561da177e4SLinus Torvalds 	char phys[32];
571da177e4SLinus Torvalds };
581da177e4SLinus Torvalds 
591da177e4SLinus Torvalds /*
601da177e4SLinus Torvalds  * stinger_process_packet() decodes packets the driver receives from the
611da177e4SLinus Torvalds  * Stinger. It updates the data accordingly.
621da177e4SLinus Torvalds  */
631da177e4SLinus Torvalds 
647d12e780SDavid Howells static void stinger_process_packet(struct stinger *stinger)
651da177e4SLinus Torvalds {
6617dd3f0fSDmitry Torokhov 	struct input_dev *dev = stinger->dev;
671da177e4SLinus Torvalds 	unsigned char *data = stinger->data;
681da177e4SLinus Torvalds 
691da177e4SLinus Torvalds 	if (!stinger->idx) return;
701da177e4SLinus Torvalds 
711da177e4SLinus Torvalds 	input_report_key(dev, BTN_A,	  ((data[0] & 0x20) >> 5));
721da177e4SLinus Torvalds 	input_report_key(dev, BTN_B,	  ((data[0] & 0x10) >> 4));
731da177e4SLinus Torvalds 	input_report_key(dev, BTN_C,	  ((data[0] & 0x08) >> 3));
741da177e4SLinus Torvalds 	input_report_key(dev, BTN_X,	  ((data[0] & 0x04) >> 2));
751da177e4SLinus Torvalds 	input_report_key(dev, BTN_Y,	  ((data[3] & 0x20) >> 5));
761da177e4SLinus Torvalds 	input_report_key(dev, BTN_Z,	  ((data[3] & 0x10) >> 4));
771da177e4SLinus Torvalds 	input_report_key(dev, BTN_TL,     ((data[3] & 0x08) >> 3));
781da177e4SLinus Torvalds 	input_report_key(dev, BTN_TR,     ((data[3] & 0x04) >> 2));
791da177e4SLinus Torvalds 	input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
801da177e4SLinus Torvalds 	input_report_key(dev, BTN_START,   (data[3] & 0x01));
811da177e4SLinus Torvalds 
821da177e4SLinus Torvalds 	input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
831da177e4SLinus Torvalds 	input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
841da177e4SLinus Torvalds 
851da177e4SLinus Torvalds 	input_sync(dev);
861da177e4SLinus Torvalds 
871da177e4SLinus Torvalds 	return;
881da177e4SLinus Torvalds }
891da177e4SLinus Torvalds 
901da177e4SLinus Torvalds /*
911da177e4SLinus Torvalds  * stinger_interrupt() is called by the low level driver when characters
921da177e4SLinus Torvalds  * are ready for us. We then buffer them for further processing, or call the
931da177e4SLinus Torvalds  * packet processing routine.
941da177e4SLinus Torvalds  */
951da177e4SLinus Torvalds 
961da177e4SLinus Torvalds static irqreturn_t stinger_interrupt(struct serio *serio,
977d12e780SDavid Howells 	unsigned char data, unsigned int flags)
981da177e4SLinus Torvalds {
991da177e4SLinus Torvalds 	struct stinger *stinger = serio_get_drvdata(serio);
1001da177e4SLinus Torvalds 
1011da177e4SLinus Torvalds 	/* All Stinger packets are 4 bytes */
1021da177e4SLinus Torvalds 
1031da177e4SLinus Torvalds 	if (stinger->idx < STINGER_MAX_LENGTH)
1041da177e4SLinus Torvalds 		stinger->data[stinger->idx++] = data;
1051da177e4SLinus Torvalds 
1061da177e4SLinus Torvalds 	if (stinger->idx == 4) {
1077d12e780SDavid Howells 		stinger_process_packet(stinger);
1081da177e4SLinus Torvalds 		stinger->idx = 0;
1091da177e4SLinus Torvalds 	}
1101da177e4SLinus Torvalds 
1111da177e4SLinus Torvalds 	return IRQ_HANDLED;
1121da177e4SLinus Torvalds }
1131da177e4SLinus Torvalds 
1141da177e4SLinus Torvalds /*
1151da177e4SLinus Torvalds  * stinger_disconnect() is the opposite of stinger_connect()
1161da177e4SLinus Torvalds  */
1171da177e4SLinus Torvalds 
1181da177e4SLinus Torvalds static void stinger_disconnect(struct serio *serio)
1191da177e4SLinus Torvalds {
1201da177e4SLinus Torvalds 	struct stinger *stinger = serio_get_drvdata(serio);
1211da177e4SLinus Torvalds 
1221da177e4SLinus Torvalds 	serio_close(serio);
1231da177e4SLinus Torvalds 	serio_set_drvdata(serio, NULL);
12417dd3f0fSDmitry Torokhov 	input_unregister_device(stinger->dev);
1251da177e4SLinus Torvalds 	kfree(stinger);
1261da177e4SLinus Torvalds }
1271da177e4SLinus Torvalds 
1281da177e4SLinus Torvalds /*
1291da177e4SLinus Torvalds  * stinger_connect() is the routine that is called when someone adds a
1301da177e4SLinus Torvalds  * new serio device that supports Stinger protocol and registers it as
1311da177e4SLinus Torvalds  * an input device.
1321da177e4SLinus Torvalds  */
1331da177e4SLinus Torvalds 
1341da177e4SLinus Torvalds static int stinger_connect(struct serio *serio, struct serio_driver *drv)
1351da177e4SLinus Torvalds {
1361da177e4SLinus Torvalds 	struct stinger *stinger;
13717dd3f0fSDmitry Torokhov 	struct input_dev *input_dev;
13817dd3f0fSDmitry Torokhov 	int err = -ENOMEM;
1391da177e4SLinus Torvalds 
14017dd3f0fSDmitry Torokhov 	stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
14117dd3f0fSDmitry Torokhov 	input_dev = input_allocate_device();
14217dd3f0fSDmitry Torokhov 	if (!stinger || !input_dev)
143127278ceSDmitry Torokhov 		goto fail1;
1441da177e4SLinus Torvalds 
14517dd3f0fSDmitry Torokhov 	stinger->dev = input_dev;
14610ca4c0aSDmitry Torokhov 	snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
1471da177e4SLinus Torvalds 
14817dd3f0fSDmitry Torokhov 	input_dev->name = "Gravis Stinger";
14917dd3f0fSDmitry Torokhov 	input_dev->phys = stinger->phys;
15017dd3f0fSDmitry Torokhov 	input_dev->id.bustype = BUS_RS232;
15117dd3f0fSDmitry Torokhov 	input_dev->id.vendor = SERIO_STINGER;
15217dd3f0fSDmitry Torokhov 	input_dev->id.product = 0x0001;
15317dd3f0fSDmitry Torokhov 	input_dev->id.version = 0x0100;
154935e658eSDmitry Torokhov 	input_dev->dev.parent = &serio->dev;
1551da177e4SLinus Torvalds 
1567b19ada2SJiri Slaby 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
1577b19ada2SJiri Slaby 	input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
1587b19ada2SJiri Slaby 		BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
1597b19ada2SJiri Slaby 		BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
1607b19ada2SJiri Slaby 		BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
16117dd3f0fSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
16217dd3f0fSDmitry Torokhov 	input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
1631da177e4SLinus Torvalds 
1641da177e4SLinus Torvalds 	serio_set_drvdata(serio, stinger);
1651da177e4SLinus Torvalds 
1661da177e4SLinus Torvalds 	err = serio_open(serio, drv);
16717dd3f0fSDmitry Torokhov 	if (err)
168127278ceSDmitry Torokhov 		goto fail2;
16917dd3f0fSDmitry Torokhov 
170127278ceSDmitry Torokhov 	err = input_register_device(stinger->dev);
171127278ceSDmitry Torokhov 	if (err)
172127278ceSDmitry Torokhov 		goto fail3;
173127278ceSDmitry Torokhov 
17417dd3f0fSDmitry Torokhov 	return 0;
17517dd3f0fSDmitry Torokhov 
176127278ceSDmitry Torokhov  fail3:	serio_close(serio);
177127278ceSDmitry Torokhov  fail2:	serio_set_drvdata(serio, NULL);
178127278ceSDmitry Torokhov  fail1:	input_free_device(input_dev);
1791da177e4SLinus Torvalds 	kfree(stinger);
1801da177e4SLinus Torvalds 	return err;
1811da177e4SLinus Torvalds }
1821da177e4SLinus Torvalds 
1831da177e4SLinus Torvalds /*
1841da177e4SLinus Torvalds  * The serio driver structure.
1851da177e4SLinus Torvalds  */
1861da177e4SLinus Torvalds 
187*fa0426c6SArvind Yadav static const struct serio_device_id stinger_serio_ids[] = {
1881da177e4SLinus Torvalds 	{
1891da177e4SLinus Torvalds 		.type	= SERIO_RS232,
1901da177e4SLinus Torvalds 		.proto	= SERIO_STINGER,
1911da177e4SLinus Torvalds 		.id	= SERIO_ANY,
1921da177e4SLinus Torvalds 		.extra	= SERIO_ANY,
1931da177e4SLinus Torvalds 	},
1941da177e4SLinus Torvalds 	{ 0 }
1951da177e4SLinus Torvalds };
1961da177e4SLinus Torvalds 
1971da177e4SLinus Torvalds MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
1981da177e4SLinus Torvalds 
1991da177e4SLinus Torvalds static struct serio_driver stinger_drv = {
2001da177e4SLinus Torvalds 	.driver		= {
2011da177e4SLinus Torvalds 		.name	= "stinger",
2021da177e4SLinus Torvalds 	},
2031da177e4SLinus Torvalds 	.description	= DRIVER_DESC,
2041da177e4SLinus Torvalds 	.id_table	= stinger_serio_ids,
2051da177e4SLinus Torvalds 	.interrupt	= stinger_interrupt,
2061da177e4SLinus Torvalds 	.connect	= stinger_connect,
2071da177e4SLinus Torvalds 	.disconnect	= stinger_disconnect,
2081da177e4SLinus Torvalds };
2091da177e4SLinus Torvalds 
21065ac9f7aSAxel Lin module_serio_driver(stinger_drv);
211