-
Notifications
You must be signed in to change notification settings - Fork 0
/
lcdtopgun.c
346 lines (287 loc) · 8.85 KB
/
lcdtopgun.c
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* EMS LCD TopGun support
*
* (c) 2006 Christophe Thibault <chris@aegis-corp.org>
* 2008 Adolfo R. Brandes <arbrandes@gmail.com>
*
* Based on GunCon2 linux driver by Brian Goines
* and the Xpad linux driver by Marko Friedemann
*/
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
#include <linux/config.h>
#endif
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/input.h>
#include <linux/module.h>
#include <linux/init.h>
#include <linux/usb.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,17)
#include <linux/usb_input.h>
#else
#include <linux/usb/input.h>
#endif
static unsigned long debug = 0;
module_param(debug, ulong, 0444);
MODULE_PARM_DESC(debug, "Debugging");
/*
* Version Information
*/
#define DRIVER_VERSION "v0.3.2"
#define DRIVER_AUTHOR "Christophe Thibault <chris@aegis-corp.org>"
#define DRIVER_DESC "USB EMS LCD TopGun driver"
#define DRIVER_LICENSE "GPL"
/*
* Missing macro (at least on linux >= 3.5)
*/
#define info(format, arg...) \
printk(KERN_INFO format "\n" , ## arg)
#define err(format, arg...) \
printk(KERN_ERR KBUILD_MODNAME ": " format "\n", ##arg)
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE(DRIVER_LICENSE);
struct usb_topgun {
char name[128];
char phys[65];
struct usb_device *usbdev;
struct input_dev *dev;
struct urb *irq;
int open;
unsigned char *data;
dma_addr_t data_dma;
};
static const signed short topgun_btn[] = {
BTN_TRIGGER, BTN_A, BTN_B, BTN_C, /* Main buttons */
BTN_START, BTN_SELECT, /* Start, Select */
-1 /* Terminating entry */
};
static const signed short topgun_abs[] = {
ABS_X, ABS_Y, /* Aiming */
ABS_HAT0X, ABS_HAT0Y, /* POV Hat */
-1 /* Terminating entry */
};
static struct usb_device_id usb_topgun_id_table [] = {
{match_flags: USB_DEVICE_ID_MATCH_VENDOR | USB_DEVICE_ID_MATCH_PRODUCT, idVendor: 0xb9a, idProduct: 0x16a},
{ } /* Terminating entry */
};
MODULE_DEVICE_TABLE (usb, usb_topgun_id_table);
static void usb_topgun_irq(struct urb *urb
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
, struct pt_regs *regs
#endif
)
{
struct usb_topgun *topgun = urb->context;
unsigned char *data = topgun->data;
struct input_dev *dev = topgun->dev;
int status;
int i, x = 0, y = 0;
unsigned int gun;
switch (urb->status) {
case 0: /* success */
break;
case -ECONNRESET: /* unlink */
case -ENOENT:
case -ESHUTDOWN:
return;
/* -EPIPE: should clear the halt */
default: /* error */
goto resubmit;
}
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,19)
input_regs(dev, regs);
#endif
if (debug) {
printk(KERN_INFO "lcdtopgun_debug: data :");
for(i = 0; i < 20; i++) {
printk("0x%02x ", data[i]);
}
printk("\n");
}
input_report_key(dev, BTN_TRIGGER, !(data[1] & 0x20));
input_report_key(dev, BTN_A, !(data[0] & 0x04));
input_report_key(dev, BTN_B, !(data[0] & 0x08));
input_report_key(dev, BTN_C, !(data[0] & 0x02));
input_report_key(dev, BTN_START, !(data[1] & 0x80));
input_report_key(dev, BTN_SELECT, !(data[1] & 0x40));
if(!(data[0] & 0x10)) x += -1;
if(!(data[0] & 0x40)) x += 1;
if(!(data[0] & 0x80)) y += -1;
if(!(data[0] & 0x20)) y += 1;
gun = data[3];
gun <<= 8;
gun |= data[2];
/* stick */
input_report_abs(dev, ABS_X, gun);
input_report_abs(dev, ABS_Y, data[4]);
/* digital pad */
input_report_abs(dev, ABS_HAT0X, x);
input_report_abs(dev, ABS_HAT0Y, y);
input_sync(dev);
resubmit:
status = usb_submit_urb(urb, GFP_ATOMIC);
if (status)
err("can't resubmit intr, %s-%s/input0, status %d",
topgun->usbdev->bus->bus_name,
topgun->usbdev->devpath, status);
}
static int usb_topgun_open(struct input_dev *dev)
{
struct usb_topgun *topgun = input_get_drvdata(dev);
int status;
if (topgun->open++)
return 0;
topgun->irq->dev = topgun->usbdev;
if ((status = usb_submit_urb(topgun->irq, GFP_KERNEL))) {
err("open input urb failed: %d", status);
topgun->open--;
return -EIO;
}
return 0;
}
static void usb_topgun_close(struct input_dev *dev)
{
struct usb_topgun *topgun = input_get_drvdata(dev);
if (!--topgun->open)
usb_unlink_urb(topgun->irq);
}
static int usb_topgun_probe(struct usb_interface *intf, const struct usb_device_id *id)
{
struct usb_device *usbdev = interface_to_usbdev(intf);
struct usb_topgun *topgun;
struct input_dev *input_dev;
struct usb_endpoint_descriptor *endpoint;
struct usb_host_interface *interface;
int pipe, maxp, i, ret_reg;
char path[64];
char *buf;
interface = intf->cur_altsetting;
if (interface->desc.bNumEndpoints != 1)
return -ENODEV;
endpoint = &interface->endpoint[0].desc;
if (!(endpoint->bEndpointAddress & 0x80))
return -ENODEV;
if ((endpoint->bmAttributes & 3) != 3)
return -ENODEV;
pipe = usb_rcvintpipe(usbdev, endpoint->bEndpointAddress);
maxp = usb_maxpacket(usbdev, pipe, usb_pipeout(pipe));
topgun = kzalloc(sizeof(struct usb_topgun), GFP_KERNEL);
input_dev = input_allocate_device();
if (!topgun || !input_dev) {
input_free_device(input_dev);
kfree(topgun);
return -ENOMEM;
}
topgun->data = usb_alloc_coherent(usbdev, 8, GFP_ATOMIC, &topgun->data_dma);
if (!topgun->data) {
input_free_device(input_dev);
kfree(topgun);
return -ENOMEM;
}
topgun->irq = usb_alloc_urb(0, GFP_KERNEL);
if (!topgun->irq) {
usb_free_coherent(usbdev, 8, topgun->data, topgun->data_dma);
input_free_device(input_dev);
kfree(topgun);
return -ENODEV;
}
topgun->usbdev = usbdev;
topgun->dev = input_dev;
input_dev->evbit[0] = BIT(EV_KEY) | BIT(EV_ABS);
for (i = 0; topgun_btn[i] >= 0; ++i)
set_bit(topgun_btn[i], input_dev->keybit);
for (i = 0; topgun_abs[i] >= 0; ++i) {
signed short t = topgun_abs[i];
set_bit(t, input_dev->absbit);
switch (t) {
case ABS_X: input_set_abs_params(input_dev, t, 160, 672, 0, 0); break;
case ABS_Y: input_set_abs_params(input_dev, t, 32, 224, 0, 0); break;
case ABS_HAT0X:
case ABS_HAT0Y: input_set_abs_params(input_dev, t, -1, 1, 0, 0); break;
}
}
usb_make_path(usbdev, path, 64);
sprintf(topgun->phys, "%s/input0", path);
input_dev->name = topgun->name;
input_dev->phys = topgun->phys;
usb_to_input_id(usbdev, &input_dev->id);
input_set_drvdata(input_dev, topgun);
input_dev->open = usb_topgun_open;
input_dev->close = usb_topgun_close;
/* Start name manipulation. */
if (!(buf = kmalloc(63, GFP_KERNEL))) {
usb_free_coherent(usbdev, 8, topgun->data, topgun->data_dma);
kfree(topgun);
return -ENOMEM;
}
if (usbdev->descriptor.iManufacturer &&
usb_string(usbdev, usbdev->descriptor.iManufacturer, buf, 63) > 0)
strcat(topgun->name, buf);
if (usbdev->descriptor.iProduct &&
usb_string(usbdev, usbdev->descriptor.iProduct, buf, 63) > 0)
sprintf(topgun->name, "%s %s", topgun->name, buf);
if (!strlen(topgun->name))
sprintf(topgun->name, "EMS LCD TopGun %04x:%04x",
input_dev->id.vendor, input_dev->id.product);
kfree(buf);
/* End name manipulation. */
usb_fill_int_urb(topgun->irq, usbdev, pipe, topgun->data,
(maxp > 8 ? 8 : maxp),
usb_topgun_irq, topgun, endpoint->bInterval);
topgun->irq->transfer_dma = topgun->data_dma;
topgun->irq->transfer_flags |= URB_NO_TRANSFER_DMA_MAP;
ret_reg = input_register_device(topgun->dev);
if (debug)
printk(KERN_INFO "input: %s on %s\n", topgun->name, path);
usb_set_intfdata(intf, topgun);
return 0;
}
static void usb_topgun_disconnect(struct usb_interface *intf)
{
struct usb_topgun *topgun = usb_get_intfdata (intf);
usb_set_intfdata(intf, NULL);
if (topgun) {
usb_unlink_urb(topgun->irq);
input_unregister_device(topgun->dev);
usb_free_urb(topgun->irq);
usb_free_coherent(interface_to_usbdev(intf), 8, topgun->data, topgun->data_dma);
kfree(topgun);
}
}
static struct usb_driver usb_topgun_driver = {
.name = "lcdtopgun",
.probe = usb_topgun_probe,
.disconnect = usb_topgun_disconnect,
.id_table = usb_topgun_id_table,
};
static int __init usb_topgun_init(void)
{
int retval = usb_register(&usb_topgun_driver);
if (retval == 0)
// info(DRIVER_DESC " " DRIVER_VERSION " initialized" );
printk(KERN_INFO "%s: " DRIVER_DESC " " DRIVER_VERSION " initialized\n" , usb_topgun_driver.name);
return retval;
}
static void __exit usb_topgun_exit(void)
{
usb_deregister(&usb_topgun_driver);
}
module_init(usb_topgun_init);
module_exit(usb_topgun_exit);