11da177e4SLinus Torvalds /* 21da177e4SLinus Torvalds * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> 3598972d4SJohann Deneux * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> 41da177e4SLinus Torvalds * 51da177e4SLinus Torvalds * USB/RS232 I-Force joysticks and wheels. 61da177e4SLinus Torvalds */ 71da177e4SLinus Torvalds 81da177e4SLinus Torvalds /* 91da177e4SLinus Torvalds * This program is free software; you can redistribute it and/or modify 101da177e4SLinus Torvalds * it under the terms of the GNU General Public License as published by 111da177e4SLinus Torvalds * the Free Software Foundation; either version 2 of the License, or 121da177e4SLinus Torvalds * (at your option) any later version. 131da177e4SLinus Torvalds * 141da177e4SLinus Torvalds * This program is distributed in the hope that it will be useful, 151da177e4SLinus Torvalds * but WITHOUT ANY WARRANTY; without even the implied warranty of 161da177e4SLinus Torvalds * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 171da177e4SLinus Torvalds * GNU General Public License for more details. 181da177e4SLinus Torvalds * 191da177e4SLinus Torvalds * You should have received a copy of the GNU General Public License 201da177e4SLinus Torvalds * along with this program; if not, write to the Free Software 211da177e4SLinus Torvalds * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 221da177e4SLinus Torvalds */ 231da177e4SLinus Torvalds 241da177e4SLinus Torvalds #include "iforce.h" 251da177e4SLinus Torvalds 2638d10769SDmitry Torokhov static void __iforce_usb_xmit(struct iforce *iforce) 271da177e4SLinus Torvalds { 281da177e4SLinus Torvalds int n, c; 291da177e4SLinus Torvalds unsigned long flags; 301da177e4SLinus Torvalds 311da177e4SLinus Torvalds spin_lock_irqsave(&iforce->xmit_lock, flags); 321da177e4SLinus Torvalds 331da177e4SLinus Torvalds if (iforce->xmit.head == iforce->xmit.tail) { 341da177e4SLinus Torvalds clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); 351da177e4SLinus Torvalds spin_unlock_irqrestore(&iforce->xmit_lock, flags); 361da177e4SLinus Torvalds return; 371da177e4SLinus Torvalds } 381da177e4SLinus Torvalds 391da177e4SLinus Torvalds ((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail]; 401da177e4SLinus Torvalds XMIT_INC(iforce->xmit.tail, 1); 411da177e4SLinus Torvalds n = iforce->xmit.buf[iforce->xmit.tail]; 421da177e4SLinus Torvalds XMIT_INC(iforce->xmit.tail, 1); 431da177e4SLinus Torvalds 441da177e4SLinus Torvalds iforce->out->transfer_buffer_length = n + 1; 451da177e4SLinus Torvalds iforce->out->dev = iforce->usbdev; 461da177e4SLinus Torvalds 471da177e4SLinus Torvalds /* Copy rest of data then */ 481da177e4SLinus Torvalds c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE); 491da177e4SLinus Torvalds if (n < c) c=n; 501da177e4SLinus Torvalds 511da177e4SLinus Torvalds memcpy(iforce->out->transfer_buffer + 1, 521da177e4SLinus Torvalds &iforce->xmit.buf[iforce->xmit.tail], 531da177e4SLinus Torvalds c); 541da177e4SLinus Torvalds if (n != c) { 551da177e4SLinus Torvalds memcpy(iforce->out->transfer_buffer + 1 + c, 561da177e4SLinus Torvalds &iforce->xmit.buf[0], 571da177e4SLinus Torvalds n-c); 581da177e4SLinus Torvalds } 591da177e4SLinus Torvalds XMIT_INC(iforce->xmit.tail, n); 601da177e4SLinus Torvalds 611da177e4SLinus Torvalds if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) { 62b8691fd2SJohann Deneux clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); 63a852d78eSGreg Kroah-Hartman dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n); 641da177e4SLinus Torvalds } 651da177e4SLinus Torvalds 661da177e4SLinus Torvalds /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended. 671da177e4SLinus Torvalds * As long as the urb completion handler is not called, the transmiting 681da177e4SLinus Torvalds * is considered to be running */ 691da177e4SLinus Torvalds spin_unlock_irqrestore(&iforce->xmit_lock, flags); 701da177e4SLinus Torvalds } 711da177e4SLinus Torvalds 7238d10769SDmitry Torokhov static void iforce_usb_xmit(struct iforce *iforce) 7338d10769SDmitry Torokhov { 7438d10769SDmitry Torokhov if (!test_and_set_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags)) 7538d10769SDmitry Torokhov __iforce_usb_xmit(iforce); 7638d10769SDmitry Torokhov } 7738d10769SDmitry Torokhov 782a1433ffSDmitry Torokhov static int iforce_usb_get_id(struct iforce *iforce, u8 *packet) 792a1433ffSDmitry Torokhov { 802a1433ffSDmitry Torokhov int status; 812a1433ffSDmitry Torokhov 822a1433ffSDmitry Torokhov iforce->cr.bRequest = packet[0]; 832a1433ffSDmitry Torokhov iforce->ctrl->dev = iforce->usbdev; 842a1433ffSDmitry Torokhov 852a1433ffSDmitry Torokhov status = usb_submit_urb(iforce->ctrl, GFP_KERNEL); 862a1433ffSDmitry Torokhov if (status) { 872a1433ffSDmitry Torokhov dev_err(&iforce->intf->dev, 882a1433ffSDmitry Torokhov "usb_submit_urb failed %d\n", status); 892a1433ffSDmitry Torokhov return -EIO; 902a1433ffSDmitry Torokhov } 912a1433ffSDmitry Torokhov 922a1433ffSDmitry Torokhov wait_event_interruptible_timeout(iforce->wait, 932a1433ffSDmitry Torokhov iforce->ctrl->status != -EINPROGRESS, HZ); 942a1433ffSDmitry Torokhov 952a1433ffSDmitry Torokhov if (iforce->ctrl->status) { 962a1433ffSDmitry Torokhov dev_dbg(&iforce->intf->dev, 972a1433ffSDmitry Torokhov "iforce->ctrl->status = %d\n", 982a1433ffSDmitry Torokhov iforce->ctrl->status); 992a1433ffSDmitry Torokhov usb_unlink_urb(iforce->ctrl); 1002a1433ffSDmitry Torokhov return -EIO; 1012a1433ffSDmitry Torokhov } 1022a1433ffSDmitry Torokhov 1032a1433ffSDmitry Torokhov return -(iforce->edata[0] != packet[0]); 1042a1433ffSDmitry Torokhov } 1052a1433ffSDmitry Torokhov 10605ca3828SDmitry Torokhov static int iforce_usb_start_io(struct iforce *iforce) 10705ca3828SDmitry Torokhov { 10805ca3828SDmitry Torokhov if (usb_submit_urb(iforce->irq, GFP_KERNEL)) 10905ca3828SDmitry Torokhov return -EIO; 11005ca3828SDmitry Torokhov 11105ca3828SDmitry Torokhov return 0; 11205ca3828SDmitry Torokhov } 11305ca3828SDmitry Torokhov 11405ca3828SDmitry Torokhov static void iforce_usb_stop_io(struct iforce *iforce) 11505ca3828SDmitry Torokhov { 11605ca3828SDmitry Torokhov usb_kill_urb(iforce->irq); 11705ca3828SDmitry Torokhov usb_kill_urb(iforce->out); 11805ca3828SDmitry Torokhov usb_kill_urb(iforce->ctrl); 11905ca3828SDmitry Torokhov } 12005ca3828SDmitry Torokhov 12138d10769SDmitry Torokhov static const struct iforce_xport_ops iforce_usb_xport_ops = { 12238d10769SDmitry Torokhov .xmit = iforce_usb_xmit, 1232a1433ffSDmitry Torokhov .get_id = iforce_usb_get_id, 12405ca3828SDmitry Torokhov .start_io = iforce_usb_start_io, 12505ca3828SDmitry Torokhov .stop_io = iforce_usb_stop_io, 12638d10769SDmitry Torokhov }; 12738d10769SDmitry Torokhov 1287d12e780SDavid Howells static void iforce_usb_irq(struct urb *urb) 1291da177e4SLinus Torvalds { 1301da177e4SLinus Torvalds struct iforce *iforce = urb->context; 131a852d78eSGreg Kroah-Hartman struct device *dev = &iforce->intf->dev; 1321da177e4SLinus Torvalds int status; 1331da177e4SLinus Torvalds 1341da177e4SLinus Torvalds switch (urb->status) { 1351da177e4SLinus Torvalds case 0: 1361da177e4SLinus Torvalds /* success */ 1371da177e4SLinus Torvalds break; 1381da177e4SLinus Torvalds case -ECONNRESET: 1391da177e4SLinus Torvalds case -ENOENT: 1401da177e4SLinus Torvalds case -ESHUTDOWN: 1411da177e4SLinus Torvalds /* this urb is terminated, clean up */ 142f576125dSGreg Kroah-Hartman dev_dbg(dev, "%s - urb shutting down with status: %d\n", 143ea3e6c59SHarvey Harrison __func__, urb->status); 1441da177e4SLinus Torvalds return; 1451da177e4SLinus Torvalds default: 146f576125dSGreg Kroah-Hartman dev_dbg(dev, "%s - urb has status of: %d\n", 147f576125dSGreg Kroah-Hartman __func__, urb->status); 1481da177e4SLinus Torvalds goto exit; 1491da177e4SLinus Torvalds } 1501da177e4SLinus Torvalds 1511da177e4SLinus Torvalds iforce_process_packet(iforce, 1527d12e780SDavid Howells (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1); 1531da177e4SLinus Torvalds 1541da177e4SLinus Torvalds exit: 1551da177e4SLinus Torvalds status = usb_submit_urb (urb, GFP_ATOMIC); 1561da177e4SLinus Torvalds if (status) 157f576125dSGreg Kroah-Hartman dev_err(dev, "%s - usb_submit_urb failed with result %d\n", 158ea3e6c59SHarvey Harrison __func__, status); 1591da177e4SLinus Torvalds } 1601da177e4SLinus Torvalds 1617d12e780SDavid Howells static void iforce_usb_out(struct urb *urb) 1621da177e4SLinus Torvalds { 1631da177e4SLinus Torvalds struct iforce *iforce = urb->context; 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds if (urb->status) { 166c2b27ef6SDmitry Torokhov clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); 167a852d78eSGreg Kroah-Hartman dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n", 168f576125dSGreg Kroah-Hartman urb->status); 1691da177e4SLinus Torvalds return; 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 17238d10769SDmitry Torokhov __iforce_usb_xmit(iforce); 1731da177e4SLinus Torvalds 1741da177e4SLinus Torvalds wake_up(&iforce->wait); 1751da177e4SLinus Torvalds } 1761da177e4SLinus Torvalds 1777d12e780SDavid Howells static void iforce_usb_ctrl(struct urb *urb) 1781da177e4SLinus Torvalds { 1791da177e4SLinus Torvalds struct iforce *iforce = urb->context; 1801da177e4SLinus Torvalds if (urb->status) return; 1811da177e4SLinus Torvalds iforce->ecmd = 0xff00 | urb->actual_length; 1821da177e4SLinus Torvalds wake_up(&iforce->wait); 1831da177e4SLinus Torvalds } 1841da177e4SLinus Torvalds 1851da177e4SLinus Torvalds static int iforce_usb_probe(struct usb_interface *intf, 1861da177e4SLinus Torvalds const struct usb_device_id *id) 1871da177e4SLinus Torvalds { 1881da177e4SLinus Torvalds struct usb_device *dev = interface_to_usbdev(intf); 1891da177e4SLinus Torvalds struct usb_host_interface *interface; 1901da177e4SLinus Torvalds struct usb_endpoint_descriptor *epirq, *epout; 1911da177e4SLinus Torvalds struct iforce *iforce; 19217dd3f0fSDmitry Torokhov int err = -ENOMEM; 1931da177e4SLinus Torvalds 1941da177e4SLinus Torvalds interface = intf->cur_altsetting; 1951da177e4SLinus Torvalds 19659cf8bedSJohan Hovold if (interface->desc.bNumEndpoints < 2) 19759cf8bedSJohan Hovold return -ENODEV; 19859cf8bedSJohan Hovold 1991da177e4SLinus Torvalds epirq = &interface->endpoint[0].desc; 2001da177e4SLinus Torvalds epout = &interface->endpoint[1].desc; 2011da177e4SLinus Torvalds 20217dd3f0fSDmitry Torokhov if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL))) 2031da177e4SLinus Torvalds goto fail; 2041da177e4SLinus Torvalds 20517dd3f0fSDmitry Torokhov if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL))) 2061da177e4SLinus Torvalds goto fail; 2071da177e4SLinus Torvalds 20817dd3f0fSDmitry Torokhov if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL))) 2091da177e4SLinus Torvalds goto fail; 2101da177e4SLinus Torvalds 21117dd3f0fSDmitry Torokhov if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL))) 2121da177e4SLinus Torvalds goto fail; 2131da177e4SLinus Torvalds 21438d10769SDmitry Torokhov iforce->xport_ops = &iforce_usb_xport_ops; 2151da177e4SLinus Torvalds iforce->bus = IFORCE_USB; 2161da177e4SLinus Torvalds iforce->usbdev = dev; 217a852d78eSGreg Kroah-Hartman iforce->intf = intf; 2181da177e4SLinus Torvalds 2191da177e4SLinus Torvalds iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE; 2201da177e4SLinus Torvalds iforce->cr.wIndex = 0; 2216b8588f7SAl Viro iforce->cr.wLength = cpu_to_le16(16); 2221da177e4SLinus Torvalds 2231da177e4SLinus Torvalds usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress), 2241da177e4SLinus Torvalds iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval); 2251da177e4SLinus Torvalds 226b8691fd2SJohann Deneux usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress), 227b8691fd2SJohann Deneux iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval); 2281da177e4SLinus Torvalds 2291da177e4SLinus Torvalds usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0), 2301da177e4SLinus Torvalds (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce); 2311da177e4SLinus Torvalds 232*501025dfSDmitry Torokhov err = iforce_init_device(&intf->dev, BUS_USB, iforce); 23317dd3f0fSDmitry Torokhov if (err) 23417dd3f0fSDmitry Torokhov goto fail; 2351da177e4SLinus Torvalds 2361da177e4SLinus Torvalds usb_set_intfdata(intf, iforce); 2371da177e4SLinus Torvalds return 0; 2381da177e4SLinus Torvalds 2391da177e4SLinus Torvalds fail: 2401da177e4SLinus Torvalds if (iforce) { 2418f21d119SMariusz Kozlowski usb_free_urb(iforce->irq); 2428f21d119SMariusz Kozlowski usb_free_urb(iforce->out); 2438f21d119SMariusz Kozlowski usb_free_urb(iforce->ctrl); 2441da177e4SLinus Torvalds kfree(iforce); 2451da177e4SLinus Torvalds } 2461da177e4SLinus Torvalds 24717dd3f0fSDmitry Torokhov return err; 2481da177e4SLinus Torvalds } 2491da177e4SLinus Torvalds 25098b7fb04SDmitry Torokhov static void iforce_usb_disconnect(struct usb_interface *intf) 2511da177e4SLinus Torvalds { 25298b7fb04SDmitry Torokhov struct iforce *iforce = usb_get_intfdata(intf); 25398b7fb04SDmitry Torokhov 25498b7fb04SDmitry Torokhov usb_set_intfdata(intf, NULL); 25598b7fb04SDmitry Torokhov 25698b7fb04SDmitry Torokhov input_unregister_device(iforce->dev); 2571da177e4SLinus Torvalds 2581da177e4SLinus Torvalds usb_free_urb(iforce->irq); 2591da177e4SLinus Torvalds usb_free_urb(iforce->out); 2601da177e4SLinus Torvalds usb_free_urb(iforce->ctrl); 2611da177e4SLinus Torvalds 2621da177e4SLinus Torvalds kfree(iforce); 2631da177e4SLinus Torvalds } 2641da177e4SLinus Torvalds 265c1295851SArvind Yadav static const struct usb_device_id iforce_usb_ids[] = { 2661da177e4SLinus Torvalds { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */ 2671da177e4SLinus Torvalds { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */ 2681da177e4SLinus Torvalds { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */ 2691da177e4SLinus Torvalds { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */ 2701da177e4SLinus Torvalds { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */ 2711da177e4SLinus Torvalds { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */ 2721da177e4SLinus Torvalds { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */ 27368947b8fSJiri Kosina { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */ 2741da177e4SLinus Torvalds { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */ 275d861f7bfSMarek Vasut { USB_DEVICE(0x06f8, 0x0003) }, /* Guillemot Jet Leader Force Feedback */ 2761da177e4SLinus Torvalds { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */ 27718098a6cSMarian-Nicolae V. Ion { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */ 2781da177e4SLinus Torvalds { } /* Terminating entry */ 2791da177e4SLinus Torvalds }; 2801da177e4SLinus Torvalds 2811da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, iforce_usb_ids); 2821da177e4SLinus Torvalds 2831da177e4SLinus Torvalds struct usb_driver iforce_usb_driver = { 2841da177e4SLinus Torvalds .name = "iforce", 2851da177e4SLinus Torvalds .probe = iforce_usb_probe, 2861da177e4SLinus Torvalds .disconnect = iforce_usb_disconnect, 2871da177e4SLinus Torvalds .id_table = iforce_usb_ids, 2881da177e4SLinus Torvalds }; 289