xref: /linux/drivers/input/joystick/stinger.c (revision fd7d598270724cc787982ea48bbe17ad383a8b7f)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  *  Copyright (c) 2000-2001 Vojtech Pavlik
4  *  Copyright (c) 2000 Mark Fletcher
5  */
6 
7 /*
8  * Gravis Stinger gamepad driver for Linux
9  */
10 
11 #include <linux/kernel.h>
12 #include <linux/module.h>
13 #include <linux/slab.h>
14 #include <linux/input.h>
15 #include <linux/serio.h>
16 
17 #define DRIVER_DESC	"Gravis Stinger gamepad driver"
18 
19 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
20 MODULE_DESCRIPTION(DRIVER_DESC);
21 MODULE_LICENSE("GPL");
22 
23 /*
24  * Constants.
25  */
26 
27 #define STINGER_MAX_LENGTH 8
28 
29 /*
30  * Per-Stinger data.
31  */
32 
33 struct stinger {
34 	struct input_dev *dev;
35 	int idx;
36 	unsigned char data[STINGER_MAX_LENGTH];
37 	char phys[32];
38 };
39 
40 /*
41  * stinger_process_packet() decodes packets the driver receives from the
42  * Stinger. It updates the data accordingly.
43  */
44 
45 static void stinger_process_packet(struct stinger *stinger)
46 {
47 	struct input_dev *dev = stinger->dev;
48 	unsigned char *data = stinger->data;
49 
50 	if (!stinger->idx) return;
51 
52 	input_report_key(dev, BTN_A,	  ((data[0] & 0x20) >> 5));
53 	input_report_key(dev, BTN_B,	  ((data[0] & 0x10) >> 4));
54 	input_report_key(dev, BTN_C,	  ((data[0] & 0x08) >> 3));
55 	input_report_key(dev, BTN_X,	  ((data[0] & 0x04) >> 2));
56 	input_report_key(dev, BTN_Y,	  ((data[3] & 0x20) >> 5));
57 	input_report_key(dev, BTN_Z,	  ((data[3] & 0x10) >> 4));
58 	input_report_key(dev, BTN_TL,     ((data[3] & 0x08) >> 3));
59 	input_report_key(dev, BTN_TR,     ((data[3] & 0x04) >> 2));
60 	input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
61 	input_report_key(dev, BTN_START,   (data[3] & 0x01));
62 
63 	input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
64 	input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
65 
66 	input_sync(dev);
67 
68 	return;
69 }
70 
71 /*
72  * stinger_interrupt() is called by the low level driver when characters
73  * are ready for us. We then buffer them for further processing, or call the
74  * packet processing routine.
75  */
76 
77 static irqreturn_t stinger_interrupt(struct serio *serio,
78 	unsigned char data, unsigned int flags)
79 {
80 	struct stinger *stinger = serio_get_drvdata(serio);
81 
82 	/* All Stinger packets are 4 bytes */
83 
84 	if (stinger->idx < STINGER_MAX_LENGTH)
85 		stinger->data[stinger->idx++] = data;
86 
87 	if (stinger->idx == 4) {
88 		stinger_process_packet(stinger);
89 		stinger->idx = 0;
90 	}
91 
92 	return IRQ_HANDLED;
93 }
94 
95 /*
96  * stinger_disconnect() is the opposite of stinger_connect()
97  */
98 
99 static void stinger_disconnect(struct serio *serio)
100 {
101 	struct stinger *stinger = serio_get_drvdata(serio);
102 
103 	serio_close(serio);
104 	serio_set_drvdata(serio, NULL);
105 	input_unregister_device(stinger->dev);
106 	kfree(stinger);
107 }
108 
109 /*
110  * stinger_connect() is the routine that is called when someone adds a
111  * new serio device that supports Stinger protocol and registers it as
112  * an input device.
113  */
114 
115 static int stinger_connect(struct serio *serio, struct serio_driver *drv)
116 {
117 	struct stinger *stinger;
118 	struct input_dev *input_dev;
119 	int err = -ENOMEM;
120 
121 	stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
122 	input_dev = input_allocate_device();
123 	if (!stinger || !input_dev)
124 		goto fail1;
125 
126 	stinger->dev = input_dev;
127 	snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
128 
129 	input_dev->name = "Gravis Stinger";
130 	input_dev->phys = stinger->phys;
131 	input_dev->id.bustype = BUS_RS232;
132 	input_dev->id.vendor = SERIO_STINGER;
133 	input_dev->id.product = 0x0001;
134 	input_dev->id.version = 0x0100;
135 	input_dev->dev.parent = &serio->dev;
136 
137 	input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
138 	input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
139 		BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
140 		BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
141 		BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
142 	input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
143 	input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
144 
145 	serio_set_drvdata(serio, stinger);
146 
147 	err = serio_open(serio, drv);
148 	if (err)
149 		goto fail2;
150 
151 	err = input_register_device(stinger->dev);
152 	if (err)
153 		goto fail3;
154 
155 	return 0;
156 
157  fail3:	serio_close(serio);
158  fail2:	serio_set_drvdata(serio, NULL);
159  fail1:	input_free_device(input_dev);
160 	kfree(stinger);
161 	return err;
162 }
163 
164 /*
165  * The serio driver structure.
166  */
167 
168 static const struct serio_device_id stinger_serio_ids[] = {
169 	{
170 		.type	= SERIO_RS232,
171 		.proto	= SERIO_STINGER,
172 		.id	= SERIO_ANY,
173 		.extra	= SERIO_ANY,
174 	},
175 	{ 0 }
176 };
177 
178 MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
179 
180 static struct serio_driver stinger_drv = {
181 	.driver		= {
182 		.name	= "stinger",
183 	},
184 	.description	= DRIVER_DESC,
185 	.id_table	= stinger_serio_ids,
186 	.interrupt	= stinger_interrupt,
187 	.connect	= stinger_connect,
188 	.disconnect	= stinger_disconnect,
189 };
190 
191 module_serio_driver(stinger_drv);
192