Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> |
Johann Deneux | 598972d | 2007-04-12 01:30:24 -0400 | [diff] [blame] | 3 | * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 4 | * |
| 5 | * USB/RS232 I-Force joysticks and wheels. |
| 6 | */ |
| 7 | |
| 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 | * Should you need to contact me, the author, you can do so either by |
| 24 | * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail: |
| 25 | * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic |
| 26 | */ |
| 27 | |
| 28 | #include "iforce.h" |
| 29 | |
| 30 | void iforce_usb_xmit(struct iforce *iforce) |
| 31 | { |
| 32 | int n, c; |
| 33 | unsigned long flags; |
| 34 | |
| 35 | spin_lock_irqsave(&iforce->xmit_lock, flags); |
| 36 | |
| 37 | if (iforce->xmit.head == iforce->xmit.tail) { |
| 38 | clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); |
| 39 | spin_unlock_irqrestore(&iforce->xmit_lock, flags); |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | ((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail]; |
| 44 | XMIT_INC(iforce->xmit.tail, 1); |
| 45 | n = iforce->xmit.buf[iforce->xmit.tail]; |
| 46 | XMIT_INC(iforce->xmit.tail, 1); |
| 47 | |
| 48 | iforce->out->transfer_buffer_length = n + 1; |
| 49 | iforce->out->dev = iforce->usbdev; |
| 50 | |
| 51 | /* Copy rest of data then */ |
| 52 | c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE); |
| 53 | if (n < c) c=n; |
| 54 | |
| 55 | memcpy(iforce->out->transfer_buffer + 1, |
| 56 | &iforce->xmit.buf[iforce->xmit.tail], |
| 57 | c); |
| 58 | if (n != c) { |
| 59 | memcpy(iforce->out->transfer_buffer + 1 + c, |
| 60 | &iforce->xmit.buf[0], |
| 61 | n-c); |
| 62 | } |
| 63 | XMIT_INC(iforce->xmit.tail, n); |
| 64 | |
| 65 | if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) { |
Johann Deneux | b8691fd | 2007-05-14 00:09:28 -0400 | [diff] [blame] | 66 | clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); |
Greg Kroah-Hartman | a852d78 | 2012-05-04 15:23:04 -0700 | [diff] [blame] | 67 | dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended. |
| 71 | * As long as the urb completion handler is not called, the transmiting |
| 72 | * is considered to be running */ |
| 73 | spin_unlock_irqrestore(&iforce->xmit_lock, flags); |
| 74 | } |
| 75 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 76 | static void iforce_usb_irq(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 77 | { |
| 78 | struct iforce *iforce = urb->context; |
Greg Kroah-Hartman | a852d78 | 2012-05-04 15:23:04 -0700 | [diff] [blame] | 79 | struct device *dev = &iforce->intf->dev; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 80 | int status; |
| 81 | |
| 82 | switch (urb->status) { |
| 83 | case 0: |
| 84 | /* success */ |
| 85 | break; |
| 86 | case -ECONNRESET: |
| 87 | case -ENOENT: |
| 88 | case -ESHUTDOWN: |
| 89 | /* this urb is terminated, clean up */ |
Greg Kroah-Hartman | f576125 | 2012-05-01 21:26:05 -0700 | [diff] [blame] | 90 | dev_dbg(dev, "%s - urb shutting down with status: %d\n", |
| 91 | __func__, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 92 | return; |
| 93 | default: |
Greg Kroah-Hartman | f576125 | 2012-05-01 21:26:05 -0700 | [diff] [blame] | 94 | dev_dbg(dev, "%s - urb has status of: %d\n", |
| 95 | __func__, urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | goto exit; |
| 97 | } |
| 98 | |
| 99 | iforce_process_packet(iforce, |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 100 | (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | |
| 102 | exit: |
| 103 | status = usb_submit_urb (urb, GFP_ATOMIC); |
| 104 | if (status) |
Greg Kroah-Hartman | f576125 | 2012-05-01 21:26:05 -0700 | [diff] [blame] | 105 | dev_err(dev, "%s - usb_submit_urb failed with result %d\n", |
Greg Kroah-Hartman | 7b22a88 | 2012-04-25 14:34:49 -0700 | [diff] [blame] | 106 | __func__, status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | } |
| 108 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 109 | static void iforce_usb_out(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | { |
| 111 | struct iforce *iforce = urb->context; |
| 112 | |
| 113 | if (urb->status) { |
Dmitry Torokhov | c2b27ef | 2009-12-30 12:18:24 -0800 | [diff] [blame] | 114 | clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); |
Greg Kroah-Hartman | a852d78 | 2012-05-04 15:23:04 -0700 | [diff] [blame] | 115 | dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n", |
Greg Kroah-Hartman | f576125 | 2012-05-01 21:26:05 -0700 | [diff] [blame] | 116 | urb->status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 117 | return; |
| 118 | } |
| 119 | |
| 120 | iforce_usb_xmit(iforce); |
| 121 | |
| 122 | wake_up(&iforce->wait); |
| 123 | } |
| 124 | |
David Howells | 7d12e78 | 2006-10-05 14:55:46 +0100 | [diff] [blame] | 125 | static void iforce_usb_ctrl(struct urb *urb) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 126 | { |
| 127 | struct iforce *iforce = urb->context; |
| 128 | if (urb->status) return; |
| 129 | iforce->ecmd = 0xff00 | urb->actual_length; |
| 130 | wake_up(&iforce->wait); |
| 131 | } |
| 132 | |
| 133 | static int iforce_usb_probe(struct usb_interface *intf, |
| 134 | const struct usb_device_id *id) |
| 135 | { |
| 136 | struct usb_device *dev = interface_to_usbdev(intf); |
| 137 | struct usb_host_interface *interface; |
| 138 | struct usb_endpoint_descriptor *epirq, *epout; |
| 139 | struct iforce *iforce; |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 140 | int err = -ENOMEM; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 141 | |
| 142 | interface = intf->cur_altsetting; |
| 143 | |
Johan Hovold | 59cf8be | 2017-03-16 11:34:02 -0700 | [diff] [blame^] | 144 | if (interface->desc.bNumEndpoints < 2) |
| 145 | return -ENODEV; |
| 146 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 147 | epirq = &interface->endpoint[0].desc; |
| 148 | epout = &interface->endpoint[1].desc; |
| 149 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 150 | if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | goto fail; |
| 152 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 153 | if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 155 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 156 | if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 158 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 159 | if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL))) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | |
| 162 | iforce->bus = IFORCE_USB; |
| 163 | iforce->usbdev = dev; |
Greg Kroah-Hartman | a852d78 | 2012-05-04 15:23:04 -0700 | [diff] [blame] | 164 | iforce->intf = intf; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | |
| 166 | iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE; |
| 167 | iforce->cr.wIndex = 0; |
Al Viro | 6b8588f | 2008-04-28 07:00:26 +0100 | [diff] [blame] | 168 | iforce->cr.wLength = cpu_to_le16(16); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 169 | |
| 170 | usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress), |
| 171 | iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval); |
| 172 | |
Johann Deneux | b8691fd | 2007-05-14 00:09:28 -0400 | [diff] [blame] | 173 | usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress), |
| 174 | iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | |
| 176 | usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0), |
| 177 | (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce); |
| 178 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 179 | err = iforce_init_device(iforce); |
| 180 | if (err) |
| 181 | goto fail; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | |
| 183 | usb_set_intfdata(intf, iforce); |
| 184 | return 0; |
| 185 | |
| 186 | fail: |
| 187 | if (iforce) { |
Mariusz Kozlowski | 8f21d11 | 2006-11-08 15:34:09 +0100 | [diff] [blame] | 188 | usb_free_urb(iforce->irq); |
| 189 | usb_free_urb(iforce->out); |
| 190 | usb_free_urb(iforce->ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 191 | kfree(iforce); |
| 192 | } |
| 193 | |
Dmitry Torokhov | 17dd3f0 | 2005-09-15 02:01:52 -0500 | [diff] [blame] | 194 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 195 | } |
| 196 | |
Dmitry Torokhov | 98b7fb0 | 2009-12-24 22:37:49 -0800 | [diff] [blame] | 197 | static void iforce_usb_disconnect(struct usb_interface *intf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | { |
Dmitry Torokhov | 98b7fb0 | 2009-12-24 22:37:49 -0800 | [diff] [blame] | 199 | struct iforce *iforce = usb_get_intfdata(intf); |
| 200 | |
| 201 | usb_set_intfdata(intf, NULL); |
| 202 | |
| 203 | input_unregister_device(iforce->dev); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | usb_free_urb(iforce->irq); |
| 206 | usb_free_urb(iforce->out); |
| 207 | usb_free_urb(iforce->ctrl); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | |
Dmitry Torokhov | 98b7fb0 | 2009-12-24 22:37:49 -0800 | [diff] [blame] | 209 | kfree(iforce); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | static struct usb_device_id iforce_usb_ids [] = { |
| 213 | { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */ |
| 214 | { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */ |
| 215 | { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */ |
| 216 | { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */ |
| 217 | { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */ |
| 218 | { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */ |
| 219 | { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */ |
Jiri Kosina | 68947b8 | 2009-08-19 22:07:44 -0700 | [diff] [blame] | 220 | { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */ |
Marek Vasut | d861f7b | 2010-05-10 15:35:11 -0700 | [diff] [blame] | 222 | { USB_DEVICE(0x06f8, 0x0003) }, /* Guillemot Jet Leader Force Feedback */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */ |
Marian-Nicolae V. Ion | 18098a6 | 2005-05-29 02:30:01 -0500 | [diff] [blame] | 224 | { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { } /* Terminating entry */ |
| 226 | }; |
| 227 | |
| 228 | MODULE_DEVICE_TABLE (usb, iforce_usb_ids); |
| 229 | |
| 230 | struct usb_driver iforce_usb_driver = { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 231 | .name = "iforce", |
| 232 | .probe = iforce_usb_probe, |
| 233 | .disconnect = iforce_usb_disconnect, |
| 234 | .id_table = iforce_usb_ids, |
| 235 | }; |