Loading...
1/*
2 * Copyright (c) 2000-2001 Vojtech Pavlik
3 * Copyright (c) 2000 Mark Fletcher
4 */
5
6/*
7 * Gravis Stinger gamepad driver for Linux
8 */
9
10/*
11 * This program is free warftware; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
24 *
25 * Should you need to contact me, the author, you can do so either by
26 * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
27 * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
28 */
29
30#include <linux/kernel.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/input.h>
34#include <linux/serio.h>
35#include <linux/init.h>
36
37#define DRIVER_DESC "Gravis Stinger gamepad driver"
38
39MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
40MODULE_DESCRIPTION(DRIVER_DESC);
41MODULE_LICENSE("GPL");
42
43/*
44 * Constants.
45 */
46
47#define STINGER_MAX_LENGTH 8
48
49/*
50 * Per-Stinger data.
51 */
52
53struct stinger {
54 struct input_dev *dev;
55 int idx;
56 unsigned char data[STINGER_MAX_LENGTH];
57 char phys[32];
58};
59
60/*
61 * stinger_process_packet() decodes packets the driver receives from the
62 * Stinger. It updates the data accordingly.
63 */
64
65static void stinger_process_packet(struct stinger *stinger)
66{
67 struct input_dev *dev = stinger->dev;
68 unsigned char *data = stinger->data;
69
70 if (!stinger->idx) return;
71
72 input_report_key(dev, BTN_A, ((data[0] & 0x20) >> 5));
73 input_report_key(dev, BTN_B, ((data[0] & 0x10) >> 4));
74 input_report_key(dev, BTN_C, ((data[0] & 0x08) >> 3));
75 input_report_key(dev, BTN_X, ((data[0] & 0x04) >> 2));
76 input_report_key(dev, BTN_Y, ((data[3] & 0x20) >> 5));
77 input_report_key(dev, BTN_Z, ((data[3] & 0x10) >> 4));
78 input_report_key(dev, BTN_TL, ((data[3] & 0x08) >> 3));
79 input_report_key(dev, BTN_TR, ((data[3] & 0x04) >> 2));
80 input_report_key(dev, BTN_SELECT, ((data[3] & 0x02) >> 1));
81 input_report_key(dev, BTN_START, (data[3] & 0x01));
82
83 input_report_abs(dev, ABS_X, (data[1] & 0x3F) - ((data[0] & 0x01) << 6));
84 input_report_abs(dev, ABS_Y, ((data[0] & 0x02) << 5) - (data[2] & 0x3F));
85
86 input_sync(dev);
87
88 return;
89}
90
91/*
92 * stinger_interrupt() is called by the low level driver when characters
93 * are ready for us. We then buffer them for further processing, or call the
94 * packet processing routine.
95 */
96
97static irqreturn_t stinger_interrupt(struct serio *serio,
98 unsigned char data, unsigned int flags)
99{
100 struct stinger *stinger = serio_get_drvdata(serio);
101
102 /* All Stinger packets are 4 bytes */
103
104 if (stinger->idx < STINGER_MAX_LENGTH)
105 stinger->data[stinger->idx++] = data;
106
107 if (stinger->idx == 4) {
108 stinger_process_packet(stinger);
109 stinger->idx = 0;
110 }
111
112 return IRQ_HANDLED;
113}
114
115/*
116 * stinger_disconnect() is the opposite of stinger_connect()
117 */
118
119static void stinger_disconnect(struct serio *serio)
120{
121 struct stinger *stinger = serio_get_drvdata(serio);
122
123 serio_close(serio);
124 serio_set_drvdata(serio, NULL);
125 input_unregister_device(stinger->dev);
126 kfree(stinger);
127}
128
129/*
130 * stinger_connect() is the routine that is called when someone adds a
131 * new serio device that supports Stinger protocol and registers it as
132 * an input device.
133 */
134
135static int stinger_connect(struct serio *serio, struct serio_driver *drv)
136{
137 struct stinger *stinger;
138 struct input_dev *input_dev;
139 int err = -ENOMEM;
140
141 stinger = kmalloc(sizeof(struct stinger), GFP_KERNEL);
142 input_dev = input_allocate_device();
143 if (!stinger || !input_dev)
144 goto fail1;
145
146 stinger->dev = input_dev;
147 snprintf(stinger->phys, sizeof(stinger->phys), "%s/serio0", serio->phys);
148
149 input_dev->name = "Gravis Stinger";
150 input_dev->phys = stinger->phys;
151 input_dev->id.bustype = BUS_RS232;
152 input_dev->id.vendor = SERIO_STINGER;
153 input_dev->id.product = 0x0001;
154 input_dev->id.version = 0x0100;
155 input_dev->dev.parent = &serio->dev;
156
157 input_dev->evbit[0] = BIT_MASK(EV_KEY) | BIT_MASK(EV_ABS);
158 input_dev->keybit[BIT_WORD(BTN_A)] = BIT_MASK(BTN_A) | BIT_MASK(BTN_B) |
159 BIT_MASK(BTN_C) | BIT_MASK(BTN_X) | BIT_MASK(BTN_Y) |
160 BIT_MASK(BTN_Z) | BIT_MASK(BTN_TL) | BIT_MASK(BTN_TR) |
161 BIT_MASK(BTN_START) | BIT_MASK(BTN_SELECT);
162 input_set_abs_params(input_dev, ABS_X, -64, 64, 0, 4);
163 input_set_abs_params(input_dev, ABS_Y, -64, 64, 0, 4);
164
165 serio_set_drvdata(serio, stinger);
166
167 err = serio_open(serio, drv);
168 if (err)
169 goto fail2;
170
171 err = input_register_device(stinger->dev);
172 if (err)
173 goto fail3;
174
175 return 0;
176
177 fail3: serio_close(serio);
178 fail2: serio_set_drvdata(serio, NULL);
179 fail1: input_free_device(input_dev);
180 kfree(stinger);
181 return err;
182}
183
184/*
185 * The serio driver structure.
186 */
187
188static struct serio_device_id stinger_serio_ids[] = {
189 {
190 .type = SERIO_RS232,
191 .proto = SERIO_STINGER,
192 .id = SERIO_ANY,
193 .extra = SERIO_ANY,
194 },
195 { 0 }
196};
197
198MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
199
200static struct serio_driver stinger_drv = {
201 .driver = {
202 .name = "stinger",
203 },
204 .description = DRIVER_DESC,
205 .id_table = stinger_serio_ids,
206 .interrupt = stinger_interrupt,
207 .connect = stinger_connect,
208 .disconnect = stinger_disconnect,
209};
210
211/*
212 * The functions for inserting/removing us as a module.
213 */
214
215static int __init stinger_init(void)
216{
217 return serio_register_driver(&stinger_drv);
218}
219
220static void __exit stinger_exit(void)
221{
222 serio_unregister_driver(&stinger_drv);
223}
224
225module_init(stinger_init);
226module_exit(stinger_exit);
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
19MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>");
20MODULE_DESCRIPTION(DRIVER_DESC);
21MODULE_LICENSE("GPL");
22
23/*
24 * Constants.
25 */
26
27#define STINGER_MAX_LENGTH 8
28
29/*
30 * Per-Stinger data.
31 */
32
33struct 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
45static 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
77static 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
99static 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
115static 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
168static 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
178MODULE_DEVICE_TABLE(serio, stinger_serio_ids);
179
180static 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
191module_serio_driver(stinger_drv);