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