.. | .. |
---|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
---|
1 | 2 | /* |
---|
2 | 3 | * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> |
---|
3 | 4 | * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> |
---|
.. | .. |
---|
5 | 6 | * USB/RS232 I-Force joysticks and wheels. |
---|
6 | 7 | */ |
---|
7 | 8 | |
---|
8 | | -/* |
---|
9 | | - * This program is free software; you can redistribute it and/or modify |
---|
10 | | - * it under the terms of the GNU General Public License as published by |
---|
11 | | - * the Free Software Foundation; either version 2 of the License, or |
---|
12 | | - * (at your option) any later version. |
---|
13 | | - * |
---|
14 | | - * This program is distributed in the hope that it will be useful, |
---|
15 | | - * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | | - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | | - * GNU General Public License for more details. |
---|
18 | | - * |
---|
19 | | - * You should have received a copy of the GNU General Public License |
---|
20 | | - * along with this program; if not, write to the Free Software |
---|
21 | | - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
---|
22 | | - */ |
---|
23 | | - |
---|
| 9 | +#include <linux/usb.h> |
---|
24 | 10 | #include "iforce.h" |
---|
25 | 11 | |
---|
26 | | -void iforce_usb_xmit(struct iforce *iforce) |
---|
| 12 | +struct iforce_usb { |
---|
| 13 | + struct iforce iforce; |
---|
| 14 | + |
---|
| 15 | + struct usb_device *usbdev; |
---|
| 16 | + struct usb_interface *intf; |
---|
| 17 | + struct urb *irq, *out; |
---|
| 18 | + |
---|
| 19 | + u8 data_in[IFORCE_MAX_LENGTH] ____cacheline_aligned; |
---|
| 20 | + u8 data_out[IFORCE_MAX_LENGTH] ____cacheline_aligned; |
---|
| 21 | +}; |
---|
| 22 | + |
---|
| 23 | +static void __iforce_usb_xmit(struct iforce *iforce) |
---|
27 | 24 | { |
---|
| 25 | + struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, |
---|
| 26 | + iforce); |
---|
28 | 27 | int n, c; |
---|
29 | 28 | unsigned long flags; |
---|
30 | 29 | |
---|
31 | 30 | spin_lock_irqsave(&iforce->xmit_lock, flags); |
---|
32 | 31 | |
---|
33 | 32 | if (iforce->xmit.head == iforce->xmit.tail) { |
---|
34 | | - clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); |
---|
| 33 | + iforce_clear_xmit_and_wake(iforce); |
---|
35 | 34 | spin_unlock_irqrestore(&iforce->xmit_lock, flags); |
---|
36 | 35 | return; |
---|
37 | 36 | } |
---|
38 | 37 | |
---|
39 | | - ((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail]; |
---|
| 38 | + ((char *)iforce_usb->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail]; |
---|
40 | 39 | XMIT_INC(iforce->xmit.tail, 1); |
---|
41 | 40 | n = iforce->xmit.buf[iforce->xmit.tail]; |
---|
42 | 41 | XMIT_INC(iforce->xmit.tail, 1); |
---|
43 | 42 | |
---|
44 | | - iforce->out->transfer_buffer_length = n + 1; |
---|
45 | | - iforce->out->dev = iforce->usbdev; |
---|
| 43 | + iforce_usb->out->transfer_buffer_length = n + 1; |
---|
| 44 | + iforce_usb->out->dev = iforce_usb->usbdev; |
---|
46 | 45 | |
---|
47 | 46 | /* Copy rest of data then */ |
---|
48 | 47 | c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE); |
---|
49 | 48 | if (n < c) c=n; |
---|
50 | 49 | |
---|
51 | | - memcpy(iforce->out->transfer_buffer + 1, |
---|
| 50 | + memcpy(iforce_usb->out->transfer_buffer + 1, |
---|
52 | 51 | &iforce->xmit.buf[iforce->xmit.tail], |
---|
53 | 52 | c); |
---|
54 | 53 | if (n != c) { |
---|
55 | | - memcpy(iforce->out->transfer_buffer + 1 + c, |
---|
| 54 | + memcpy(iforce_usb->out->transfer_buffer + 1 + c, |
---|
56 | 55 | &iforce->xmit.buf[0], |
---|
57 | 56 | n-c); |
---|
58 | 57 | } |
---|
59 | 58 | XMIT_INC(iforce->xmit.tail, n); |
---|
60 | 59 | |
---|
61 | | - if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) { |
---|
62 | | - clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); |
---|
63 | | - dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n); |
---|
| 60 | + if ( (n=usb_submit_urb(iforce_usb->out, GFP_ATOMIC)) ) { |
---|
| 61 | + dev_warn(&iforce_usb->intf->dev, |
---|
| 62 | + "usb_submit_urb failed %d\n", n); |
---|
| 63 | + iforce_clear_xmit_and_wake(iforce); |
---|
64 | 64 | } |
---|
65 | 65 | |
---|
66 | 66 | /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended. |
---|
.. | .. |
---|
69 | 69 | spin_unlock_irqrestore(&iforce->xmit_lock, flags); |
---|
70 | 70 | } |
---|
71 | 71 | |
---|
| 72 | +static void iforce_usb_xmit(struct iforce *iforce) |
---|
| 73 | +{ |
---|
| 74 | + if (!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) |
---|
| 75 | + __iforce_usb_xmit(iforce); |
---|
| 76 | +} |
---|
| 77 | + |
---|
| 78 | +static int iforce_usb_get_id(struct iforce *iforce, u8 id, |
---|
| 79 | + u8 *response_data, size_t *response_len) |
---|
| 80 | +{ |
---|
| 81 | + struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, |
---|
| 82 | + iforce); |
---|
| 83 | + u8 *buf; |
---|
| 84 | + int status; |
---|
| 85 | + |
---|
| 86 | + buf = kmalloc(IFORCE_MAX_LENGTH, GFP_KERNEL); |
---|
| 87 | + if (!buf) |
---|
| 88 | + return -ENOMEM; |
---|
| 89 | + |
---|
| 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, 1000); |
---|
| 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; |
---|
| 105 | + } |
---|
| 106 | + |
---|
| 107 | + kfree(buf); |
---|
| 108 | + return status; |
---|
| 109 | +} |
---|
| 110 | + |
---|
| 111 | +static int iforce_usb_start_io(struct iforce *iforce) |
---|
| 112 | +{ |
---|
| 113 | + struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, |
---|
| 114 | + iforce); |
---|
| 115 | + |
---|
| 116 | + if (usb_submit_urb(iforce_usb->irq, GFP_KERNEL)) |
---|
| 117 | + return -EIO; |
---|
| 118 | + |
---|
| 119 | + return 0; |
---|
| 120 | +} |
---|
| 121 | + |
---|
| 122 | +static void iforce_usb_stop_io(struct iforce *iforce) |
---|
| 123 | +{ |
---|
| 124 | + struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, |
---|
| 125 | + iforce); |
---|
| 126 | + |
---|
| 127 | + usb_kill_urb(iforce_usb->irq); |
---|
| 128 | + usb_kill_urb(iforce_usb->out); |
---|
| 129 | +} |
---|
| 130 | + |
---|
| 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, |
---|
| 136 | +}; |
---|
| 137 | + |
---|
72 | 138 | static void iforce_usb_irq(struct urb *urb) |
---|
73 | 139 | { |
---|
74 | | - struct iforce *iforce = urb->context; |
---|
75 | | - struct device *dev = &iforce->intf->dev; |
---|
| 140 | + struct iforce_usb *iforce_usb = urb->context; |
---|
| 141 | + struct iforce *iforce = &iforce_usb->iforce; |
---|
| 142 | + struct device *dev = &iforce_usb->intf->dev; |
---|
76 | 143 | int status; |
---|
77 | 144 | |
---|
78 | 145 | switch (urb->status) { |
---|
.. | .. |
---|
92 | 159 | goto exit; |
---|
93 | 160 | } |
---|
94 | 161 | |
---|
95 | | - iforce_process_packet(iforce, |
---|
96 | | - (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1); |
---|
| 162 | + iforce_process_packet(iforce, iforce_usb->data_in[0], |
---|
| 163 | + iforce_usb->data_in + 1, urb->actual_length - 1); |
---|
97 | 164 | |
---|
98 | 165 | exit: |
---|
99 | | - status = usb_submit_urb (urb, GFP_ATOMIC); |
---|
| 166 | + status = usb_submit_urb(urb, GFP_ATOMIC); |
---|
100 | 167 | if (status) |
---|
101 | 168 | dev_err(dev, "%s - usb_submit_urb failed with result %d\n", |
---|
102 | 169 | __func__, status); |
---|
.. | .. |
---|
104 | 171 | |
---|
105 | 172 | static void iforce_usb_out(struct urb *urb) |
---|
106 | 173 | { |
---|
107 | | - struct iforce *iforce = urb->context; |
---|
| 174 | + struct iforce_usb *iforce_usb = urb->context; |
---|
| 175 | + struct iforce *iforce = &iforce_usb->iforce; |
---|
108 | 176 | |
---|
109 | 177 | if (urb->status) { |
---|
110 | | - clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); |
---|
111 | | - dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n", |
---|
| 178 | + dev_dbg(&iforce_usb->intf->dev, "urb->status %d, exiting\n", |
---|
112 | 179 | urb->status); |
---|
| 180 | + iforce_clear_xmit_and_wake(iforce); |
---|
113 | 181 | return; |
---|
114 | 182 | } |
---|
115 | 183 | |
---|
116 | | - iforce_usb_xmit(iforce); |
---|
| 184 | + __iforce_usb_xmit(iforce); |
---|
117 | 185 | |
---|
118 | | - wake_up(&iforce->wait); |
---|
119 | | -} |
---|
120 | | - |
---|
121 | | -static void iforce_usb_ctrl(struct urb *urb) |
---|
122 | | -{ |
---|
123 | | - struct iforce *iforce = urb->context; |
---|
124 | | - if (urb->status) return; |
---|
125 | | - iforce->ecmd = 0xff00 | urb->actual_length; |
---|
126 | | - wake_up(&iforce->wait); |
---|
| 186 | + wake_up_all(&iforce->wait); |
---|
127 | 187 | } |
---|
128 | 188 | |
---|
129 | 189 | static int iforce_usb_probe(struct usb_interface *intf, |
---|
.. | .. |
---|
132 | 192 | struct usb_device *dev = interface_to_usbdev(intf); |
---|
133 | 193 | struct usb_host_interface *interface; |
---|
134 | 194 | struct usb_endpoint_descriptor *epirq, *epout; |
---|
135 | | - struct iforce *iforce; |
---|
| 195 | + struct iforce_usb *iforce_usb; |
---|
136 | 196 | int err = -ENOMEM; |
---|
137 | 197 | |
---|
138 | 198 | interface = intf->cur_altsetting; |
---|
.. | .. |
---|
148 | 208 | if (!usb_endpoint_is_int_out(epout)) |
---|
149 | 209 | return -ENODEV; |
---|
150 | 210 | |
---|
151 | | - if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL))) |
---|
| 211 | + iforce_usb = kzalloc(sizeof(*iforce_usb), GFP_KERNEL); |
---|
| 212 | + if (!iforce_usb) |
---|
152 | 213 | goto fail; |
---|
153 | 214 | |
---|
154 | | - if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL))) |
---|
| 215 | + iforce_usb->irq = usb_alloc_urb(0, GFP_KERNEL); |
---|
| 216 | + if (!iforce_usb->irq) |
---|
155 | 217 | goto fail; |
---|
156 | 218 | |
---|
157 | | - if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL))) |
---|
| 219 | + iforce_usb->out = usb_alloc_urb(0, GFP_KERNEL); |
---|
| 220 | + if (!iforce_usb->out) |
---|
158 | 221 | goto fail; |
---|
159 | 222 | |
---|
160 | | - if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL))) |
---|
161 | | - goto fail; |
---|
| 223 | + iforce_usb->iforce.xport_ops = &iforce_usb_xport_ops; |
---|
162 | 224 | |
---|
163 | | - iforce->bus = IFORCE_USB; |
---|
164 | | - iforce->usbdev = dev; |
---|
165 | | - iforce->intf = intf; |
---|
| 225 | + iforce_usb->usbdev = dev; |
---|
| 226 | + iforce_usb->intf = intf; |
---|
166 | 227 | |
---|
167 | | - iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE; |
---|
168 | | - iforce->cr.wIndex = 0; |
---|
169 | | - iforce->cr.wLength = cpu_to_le16(16); |
---|
| 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); |
---|
170 | 232 | |
---|
171 | | - usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress), |
---|
172 | | - iforce->data, 16, iforce_usb_irq, iforce, 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); |
---|
173 | 237 | |
---|
174 | | - usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress), |
---|
175 | | - iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval); |
---|
176 | | - |
---|
177 | | - usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0), |
---|
178 | | - (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce); |
---|
179 | | - |
---|
180 | | - err = iforce_init_device(iforce); |
---|
| 238 | + err = iforce_init_device(&intf->dev, BUS_USB, &iforce_usb->iforce); |
---|
181 | 239 | if (err) |
---|
182 | 240 | goto fail; |
---|
183 | 241 | |
---|
184 | | - usb_set_intfdata(intf, iforce); |
---|
| 242 | + usb_set_intfdata(intf, iforce_usb); |
---|
185 | 243 | return 0; |
---|
186 | 244 | |
---|
187 | 245 | fail: |
---|
188 | | - if (iforce) { |
---|
189 | | - usb_free_urb(iforce->irq); |
---|
190 | | - usb_free_urb(iforce->out); |
---|
191 | | - usb_free_urb(iforce->ctrl); |
---|
192 | | - kfree(iforce); |
---|
| 246 | + if (iforce_usb) { |
---|
| 247 | + usb_free_urb(iforce_usb->irq); |
---|
| 248 | + usb_free_urb(iforce_usb->out); |
---|
| 249 | + kfree(iforce_usb); |
---|
193 | 250 | } |
---|
194 | 251 | |
---|
195 | 252 | return err; |
---|
.. | .. |
---|
197 | 254 | |
---|
198 | 255 | static void iforce_usb_disconnect(struct usb_interface *intf) |
---|
199 | 256 | { |
---|
200 | | - struct iforce *iforce = usb_get_intfdata(intf); |
---|
| 257 | + struct iforce_usb *iforce_usb = usb_get_intfdata(intf); |
---|
201 | 258 | |
---|
202 | 259 | usb_set_intfdata(intf, NULL); |
---|
203 | 260 | |
---|
204 | | - input_unregister_device(iforce->dev); |
---|
| 261 | + input_unregister_device(iforce_usb->iforce.dev); |
---|
205 | 262 | |
---|
206 | | - usb_free_urb(iforce->irq); |
---|
207 | | - usb_free_urb(iforce->out); |
---|
208 | | - usb_free_urb(iforce->ctrl); |
---|
| 263 | + usb_free_urb(iforce_usb->irq); |
---|
| 264 | + usb_free_urb(iforce_usb->out); |
---|
209 | 265 | |
---|
210 | | - kfree(iforce); |
---|
| 266 | + kfree(iforce_usb); |
---|
211 | 267 | } |
---|
212 | 268 | |
---|
213 | 269 | static const struct usb_device_id iforce_usb_ids[] = { |
---|
.. | .. |
---|
219 | 275 | { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */ |
---|
220 | 276 | { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */ |
---|
221 | 277 | { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */ |
---|
| 278 | + { USB_DEVICE(0x06a3, 0xff04) }, /* Saitek R440 Force Wheel */ |
---|
222 | 279 | { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */ |
---|
223 | 280 | { USB_DEVICE(0x06f8, 0x0003) }, /* Guillemot Jet Leader Force Feedback */ |
---|
224 | 281 | { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */ |
---|
.. | .. |
---|
234 | 291 | .disconnect = iforce_usb_disconnect, |
---|
235 | 292 | .id_table = iforce_usb_ids, |
---|
236 | 293 | }; |
---|
| 294 | + |
---|
| 295 | +module_usb_driver(iforce_usb_driver); |
---|
| 296 | + |
---|
| 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"); |
---|