WIP FPC-III support
[linux/fpc-iii.git] / drivers / input / joystick / iforce / iforce-usb.c
blob6c554c11a7ac3f35a33b318f7f6bef589a8c6b81
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
4 * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
6 * USB/RS232 I-Force joysticks and wheels.
7 */
9 #include <linux/usb.h>
10 #include "iforce.h"
12 struct iforce_usb {
13 struct iforce iforce;
15 struct usb_device *usbdev;
16 struct usb_interface *intf;
17 struct urb *irq, *out;
19 u8 data_in[IFORCE_MAX_LENGTH] ____cacheline_aligned;
20 u8 data_out[IFORCE_MAX_LENGTH] ____cacheline_aligned;
23 static void __iforce_usb_xmit(struct iforce *iforce)
25 struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
26 iforce);
27 int n, c;
28 unsigned long flags;
30 spin_lock_irqsave(&iforce->xmit_lock, flags);
32 if (iforce->xmit.head == iforce->xmit.tail) {
33 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
34 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
35 return;
38 ((char *)iforce_usb->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
39 XMIT_INC(iforce->xmit.tail, 1);
40 n = iforce->xmit.buf[iforce->xmit.tail];
41 XMIT_INC(iforce->xmit.tail, 1);
43 iforce_usb->out->transfer_buffer_length = n + 1;
44 iforce_usb->out->dev = iforce_usb->usbdev;
46 /* Copy rest of data then */
47 c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
48 if (n < c) c=n;
50 memcpy(iforce_usb->out->transfer_buffer + 1,
51 &iforce->xmit.buf[iforce->xmit.tail],
52 c);
53 if (n != c) {
54 memcpy(iforce_usb->out->transfer_buffer + 1 + c,
55 &iforce->xmit.buf[0],
56 n-c);
58 XMIT_INC(iforce->xmit.tail, n);
60 if ( (n=usb_submit_urb(iforce_usb->out, GFP_ATOMIC)) ) {
61 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
62 dev_warn(&iforce_usb->intf->dev,
63 "usb_submit_urb failed %d\n", n);
66 /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
67 * As long as the urb completion handler is not called, the transmiting
68 * is considered to be running */
69 spin_unlock_irqrestore(&iforce->xmit_lock, flags);
72 static void iforce_usb_xmit(struct iforce *iforce)
74 if (!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags))
75 __iforce_usb_xmit(iforce);
78 static int iforce_usb_get_id(struct iforce *iforce, u8 id,
79 u8 *response_data, size_t *response_len)
81 struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
82 iforce);
83 u8 *buf;
84 int status;
86 buf = kmalloc(IFORCE_MAX_LENGTH, GFP_KERNEL);
87 if (!buf)
88 return -ENOMEM;
90 status = usb_control_msg(iforce_usb->usbdev,
91 usb_rcvctrlpipe(iforce_usb->usbdev, 0),
92 id,
93 USB_TYPE_VENDOR | USB_DIR_IN |
94 USB_RECIP_INTERFACE,
95 0, 0, buf, IFORCE_MAX_LENGTH, HZ);
96 if (status < 0) {
97 dev_err(&iforce_usb->intf->dev,
98 "usb_submit_urb failed: %d\n", status);
99 } else if (buf[0] != id) {
100 status = -EIO;
101 } else {
102 memcpy(response_data, buf, status);
103 *response_len = status;
104 status = 0;
107 kfree(buf);
108 return status;
111 static int iforce_usb_start_io(struct iforce *iforce)
113 struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
114 iforce);
116 if (usb_submit_urb(iforce_usb->irq, GFP_KERNEL))
117 return -EIO;
119 return 0;
122 static void iforce_usb_stop_io(struct iforce *iforce)
124 struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb,
125 iforce);
127 usb_kill_urb(iforce_usb->irq);
128 usb_kill_urb(iforce_usb->out);
131 static const struct iforce_xport_ops iforce_usb_xport_ops = {
132 .xmit = iforce_usb_xmit,
133 .get_id = iforce_usb_get_id,
134 .start_io = iforce_usb_start_io,
135 .stop_io = iforce_usb_stop_io,
138 static void iforce_usb_irq(struct urb *urb)
140 struct iforce_usb *iforce_usb = urb->context;
141 struct iforce *iforce = &iforce_usb->iforce;
142 struct device *dev = &iforce_usb->intf->dev;
143 int status;
145 switch (urb->status) {
146 case 0:
147 /* success */
148 break;
149 case -ECONNRESET:
150 case -ENOENT:
151 case -ESHUTDOWN:
152 /* this urb is terminated, clean up */
153 dev_dbg(dev, "%s - urb shutting down with status: %d\n",
154 __func__, urb->status);
155 return;
156 default:
157 dev_dbg(dev, "%s - urb has status of: %d\n",
158 __func__, urb->status);
159 goto exit;
162 iforce_process_packet(iforce, iforce_usb->data_in[0],
163 iforce_usb->data_in + 1, urb->actual_length - 1);
165 exit:
166 status = usb_submit_urb(urb, GFP_ATOMIC);
167 if (status)
168 dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
169 __func__, status);
172 static void iforce_usb_out(struct urb *urb)
174 struct iforce_usb *iforce_usb = urb->context;
175 struct iforce *iforce = &iforce_usb->iforce;
177 if (urb->status) {
178 clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
179 dev_dbg(&iforce_usb->intf->dev, "urb->status %d, exiting\n",
180 urb->status);
181 return;
184 __iforce_usb_xmit(iforce);
186 wake_up(&iforce->wait);
189 static int iforce_usb_probe(struct usb_interface *intf,
190 const struct usb_device_id *id)
192 struct usb_device *dev = interface_to_usbdev(intf);
193 struct usb_host_interface *interface;
194 struct usb_endpoint_descriptor *epirq, *epout;
195 struct iforce_usb *iforce_usb;
196 int err = -ENOMEM;
198 interface = intf->cur_altsetting;
200 if (interface->desc.bNumEndpoints < 2)
201 return -ENODEV;
203 epirq = &interface->endpoint[0].desc;
204 if (!usb_endpoint_is_int_in(epirq))
205 return -ENODEV;
207 epout = &interface->endpoint[1].desc;
208 if (!usb_endpoint_is_int_out(epout))
209 return -ENODEV;
211 iforce_usb = kzalloc(sizeof(*iforce_usb), GFP_KERNEL);
212 if (!iforce_usb)
213 goto fail;
215 iforce_usb->irq = usb_alloc_urb(0, GFP_KERNEL);
216 if (!iforce_usb->irq)
217 goto fail;
219 iforce_usb->out = usb_alloc_urb(0, GFP_KERNEL);
220 if (!iforce_usb->out)
221 goto fail;
223 iforce_usb->iforce.xport_ops = &iforce_usb_xport_ops;
225 iforce_usb->usbdev = dev;
226 iforce_usb->intf = intf;
228 usb_fill_int_urb(iforce_usb->irq, dev,
229 usb_rcvintpipe(dev, epirq->bEndpointAddress),
230 iforce_usb->data_in, sizeof(iforce_usb->data_in),
231 iforce_usb_irq, iforce_usb, epirq->bInterval);
233 usb_fill_int_urb(iforce_usb->out, dev,
234 usb_sndintpipe(dev, epout->bEndpointAddress),
235 iforce_usb->data_out, sizeof(iforce_usb->data_out),
236 iforce_usb_out, iforce_usb, epout->bInterval);
238 err = iforce_init_device(&intf->dev, BUS_USB, &iforce_usb->iforce);
239 if (err)
240 goto fail;
242 usb_set_intfdata(intf, iforce_usb);
243 return 0;
245 fail:
246 if (iforce_usb) {
247 usb_free_urb(iforce_usb->irq);
248 usb_free_urb(iforce_usb->out);
249 kfree(iforce_usb);
252 return err;
255 static void iforce_usb_disconnect(struct usb_interface *intf)
257 struct iforce_usb *iforce_usb = usb_get_intfdata(intf);
259 usb_set_intfdata(intf, NULL);
261 input_unregister_device(iforce_usb->iforce.dev);
263 usb_free_urb(iforce_usb->irq);
264 usb_free_urb(iforce_usb->out);
266 kfree(iforce_usb);
269 static const struct usb_device_id iforce_usb_ids[] = {
270 { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */
271 { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */
272 { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */
273 { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */
274 { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */
275 { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */
276 { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */
277 { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */
278 { USB_DEVICE(0x06a3, 0xff04) }, /* Saitek R440 Force Wheel */
279 { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */
280 { USB_DEVICE(0x06f8, 0x0003) }, /* Guillemot Jet Leader Force Feedback */
281 { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */
282 { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */
283 { } /* Terminating entry */
286 MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
288 struct usb_driver iforce_usb_driver = {
289 .name = "iforce",
290 .probe = iforce_usb_probe,
291 .disconnect = iforce_usb_disconnect,
292 .id_table = iforce_usb_ids,
295 module_usb_driver(iforce_usb_driver);
297 MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>, Johann Deneux <johann.deneux@gmail.com>");
298 MODULE_DESCRIPTION("USB I-Force joysticks and wheels driver");
299 MODULE_LICENSE("GPL");