11a59d1b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later 21da177e4SLinus Torvalds /* 31da177e4SLinus Torvalds * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz> 4598972d4SJohann Deneux * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com> 51da177e4SLinus Torvalds * 61da177e4SLinus Torvalds * USB/RS232 I-Force joysticks and wheels. 71da177e4SLinus Torvalds */ 81da177e4SLinus Torvalds 94f99de6dSDmitry Torokhov #include <linux/usb.h> 101da177e4SLinus Torvalds #include "iforce.h" 111da177e4SLinus Torvalds 1281fd4313SDmitry Torokhov struct iforce_usb { 1381fd4313SDmitry Torokhov struct iforce iforce; 1481fd4313SDmitry Torokhov 1581fd4313SDmitry Torokhov struct usb_device *usbdev; 1681fd4313SDmitry Torokhov struct usb_interface *intf; 1748735862SDmitry Torokhov struct urb *irq, *out; 18dfad2b17SDmitry Torokhov 19dfad2b17SDmitry Torokhov u8 data_in[IFORCE_MAX_LENGTH] ____cacheline_aligned; 20dfad2b17SDmitry Torokhov u8 data_out[IFORCE_MAX_LENGTH] ____cacheline_aligned; 2181fd4313SDmitry Torokhov }; 2281fd4313SDmitry Torokhov 2338d10769SDmitry Torokhov static void __iforce_usb_xmit(struct iforce *iforce) 241da177e4SLinus Torvalds { 2581fd4313SDmitry Torokhov struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, 2681fd4313SDmitry Torokhov iforce); 271da177e4SLinus Torvalds int n, c; 281da177e4SLinus Torvalds unsigned long flags; 291da177e4SLinus Torvalds 301da177e4SLinus Torvalds spin_lock_irqsave(&iforce->xmit_lock, flags); 311da177e4SLinus Torvalds 321da177e4SLinus Torvalds if (iforce->xmit.head == iforce->xmit.tail) { 331da177e4SLinus Torvalds clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); 341da177e4SLinus Torvalds spin_unlock_irqrestore(&iforce->xmit_lock, flags); 351da177e4SLinus Torvalds return; 361da177e4SLinus Torvalds } 371da177e4SLinus Torvalds 3881fd4313SDmitry Torokhov ((char *)iforce_usb->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail]; 391da177e4SLinus Torvalds XMIT_INC(iforce->xmit.tail, 1); 401da177e4SLinus Torvalds n = iforce->xmit.buf[iforce->xmit.tail]; 411da177e4SLinus Torvalds XMIT_INC(iforce->xmit.tail, 1); 421da177e4SLinus Torvalds 4381fd4313SDmitry Torokhov iforce_usb->out->transfer_buffer_length = n + 1; 4481fd4313SDmitry Torokhov iforce_usb->out->dev = iforce_usb->usbdev; 451da177e4SLinus Torvalds 461da177e4SLinus Torvalds /* Copy rest of data then */ 471da177e4SLinus Torvalds c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE); 481da177e4SLinus Torvalds if (n < c) c=n; 491da177e4SLinus Torvalds 5081fd4313SDmitry Torokhov memcpy(iforce_usb->out->transfer_buffer + 1, 511da177e4SLinus Torvalds &iforce->xmit.buf[iforce->xmit.tail], 521da177e4SLinus Torvalds c); 531da177e4SLinus Torvalds if (n != c) { 5481fd4313SDmitry Torokhov memcpy(iforce_usb->out->transfer_buffer + 1 + c, 551da177e4SLinus Torvalds &iforce->xmit.buf[0], 561da177e4SLinus Torvalds n-c); 571da177e4SLinus Torvalds } 581da177e4SLinus Torvalds XMIT_INC(iforce->xmit.tail, n); 591da177e4SLinus Torvalds 6081fd4313SDmitry Torokhov if ( (n=usb_submit_urb(iforce_usb->out, GFP_ATOMIC)) ) { 61b8691fd2SJohann Deneux clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); 6281fd4313SDmitry Torokhov dev_warn(&iforce_usb->intf->dev, 6381fd4313SDmitry Torokhov "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 786ac0aec6SDmitry Torokhov static int iforce_usb_get_id(struct iforce *iforce, u8 id, 796ac0aec6SDmitry Torokhov u8 *response_data, size_t *response_len) 802a1433ffSDmitry Torokhov { 8181fd4313SDmitry Torokhov struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, 8281fd4313SDmitry Torokhov iforce); 8348735862SDmitry Torokhov u8 *buf; 842a1433ffSDmitry Torokhov int status; 852a1433ffSDmitry Torokhov 8648735862SDmitry Torokhov buf = kmalloc(IFORCE_MAX_LENGTH, GFP_KERNEL); 8748735862SDmitry Torokhov if (!buf) 8848735862SDmitry Torokhov return -ENOMEM; 892a1433ffSDmitry Torokhov 9048735862SDmitry Torokhov status = usb_control_msg(iforce_usb->usbdev, 9148735862SDmitry Torokhov usb_rcvctrlpipe(iforce_usb->usbdev, 0), 926ac0aec6SDmitry Torokhov id, 9348735862SDmitry Torokhov USB_TYPE_VENDOR | USB_DIR_IN | 9448735862SDmitry Torokhov USB_RECIP_INTERFACE, 9548735862SDmitry Torokhov 0, 0, buf, IFORCE_MAX_LENGTH, HZ); 9648735862SDmitry Torokhov if (status < 0) { 9781fd4313SDmitry Torokhov dev_err(&iforce_usb->intf->dev, 9848735862SDmitry Torokhov "usb_submit_urb failed: %d\n", status); 996ac0aec6SDmitry Torokhov } else if (buf[0] != id) { 10048735862SDmitry Torokhov status = -EIO; 10148735862SDmitry Torokhov } else { 1026ac0aec6SDmitry Torokhov memcpy(response_data, buf, status); 1036ac0aec6SDmitry Torokhov *response_len = status; 10448735862SDmitry Torokhov status = 0; 1052a1433ffSDmitry Torokhov } 1062a1433ffSDmitry Torokhov 10748735862SDmitry Torokhov kfree(buf); 10848735862SDmitry Torokhov return status; 1092a1433ffSDmitry Torokhov } 1102a1433ffSDmitry Torokhov 11105ca3828SDmitry Torokhov static int iforce_usb_start_io(struct iforce *iforce) 11205ca3828SDmitry Torokhov { 11381fd4313SDmitry Torokhov struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, 11481fd4313SDmitry Torokhov iforce); 11581fd4313SDmitry Torokhov 11681fd4313SDmitry Torokhov if (usb_submit_urb(iforce_usb->irq, GFP_KERNEL)) 11705ca3828SDmitry Torokhov return -EIO; 11805ca3828SDmitry Torokhov 11905ca3828SDmitry Torokhov return 0; 12005ca3828SDmitry Torokhov } 12105ca3828SDmitry Torokhov 12205ca3828SDmitry Torokhov static void iforce_usb_stop_io(struct iforce *iforce) 12305ca3828SDmitry Torokhov { 12481fd4313SDmitry Torokhov struct iforce_usb *iforce_usb = container_of(iforce, struct iforce_usb, 12581fd4313SDmitry Torokhov iforce); 12681fd4313SDmitry Torokhov 12781fd4313SDmitry Torokhov usb_kill_urb(iforce_usb->irq); 12881fd4313SDmitry Torokhov usb_kill_urb(iforce_usb->out); 12905ca3828SDmitry Torokhov } 13005ca3828SDmitry Torokhov 13138d10769SDmitry Torokhov static const struct iforce_xport_ops iforce_usb_xport_ops = { 13238d10769SDmitry Torokhov .xmit = iforce_usb_xmit, 1332a1433ffSDmitry Torokhov .get_id = iforce_usb_get_id, 13405ca3828SDmitry Torokhov .start_io = iforce_usb_start_io, 13505ca3828SDmitry Torokhov .stop_io = iforce_usb_stop_io, 13638d10769SDmitry Torokhov }; 13738d10769SDmitry Torokhov 1387d12e780SDavid Howells static void iforce_usb_irq(struct urb *urb) 1391da177e4SLinus Torvalds { 14081fd4313SDmitry Torokhov struct iforce_usb *iforce_usb = urb->context; 14181fd4313SDmitry Torokhov struct iforce *iforce = &iforce_usb->iforce; 14281fd4313SDmitry Torokhov struct device *dev = &iforce_usb->intf->dev; 1431da177e4SLinus Torvalds int status; 1441da177e4SLinus Torvalds 1451da177e4SLinus Torvalds switch (urb->status) { 1461da177e4SLinus Torvalds case 0: 1471da177e4SLinus Torvalds /* success */ 1481da177e4SLinus Torvalds break; 1491da177e4SLinus Torvalds case -ECONNRESET: 1501da177e4SLinus Torvalds case -ENOENT: 1511da177e4SLinus Torvalds case -ESHUTDOWN: 1521da177e4SLinus Torvalds /* this urb is terminated, clean up */ 153f576125dSGreg Kroah-Hartman dev_dbg(dev, "%s - urb shutting down with status: %d\n", 154ea3e6c59SHarvey Harrison __func__, urb->status); 1551da177e4SLinus Torvalds return; 1561da177e4SLinus Torvalds default: 157f576125dSGreg Kroah-Hartman dev_dbg(dev, "%s - urb has status of: %d\n", 158f576125dSGreg Kroah-Hartman __func__, urb->status); 1591da177e4SLinus Torvalds goto exit; 1601da177e4SLinus Torvalds } 1611da177e4SLinus Torvalds 162dfad2b17SDmitry Torokhov iforce_process_packet(iforce, iforce_usb->data_in[0], 163dfad2b17SDmitry Torokhov iforce_usb->data_in + 1, urb->actual_length - 1); 1641da177e4SLinus Torvalds 1651da177e4SLinus Torvalds exit: 1661da177e4SLinus Torvalds status = usb_submit_urb(urb, GFP_ATOMIC); 1671da177e4SLinus Torvalds if (status) 168f576125dSGreg Kroah-Hartman dev_err(dev, "%s - usb_submit_urb failed with result %d\n", 169ea3e6c59SHarvey Harrison __func__, status); 1701da177e4SLinus Torvalds } 1711da177e4SLinus Torvalds 1727d12e780SDavid Howells static void iforce_usb_out(struct urb *urb) 1731da177e4SLinus Torvalds { 17481fd4313SDmitry Torokhov struct iforce_usb *iforce_usb = urb->context; 17581fd4313SDmitry Torokhov struct iforce *iforce = &iforce_usb->iforce; 1761da177e4SLinus Torvalds 1771da177e4SLinus Torvalds if (urb->status) { 178c2b27ef6SDmitry Torokhov clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags); 17981fd4313SDmitry Torokhov dev_dbg(&iforce_usb->intf->dev, "urb->status %d, exiting\n", 180f576125dSGreg Kroah-Hartman urb->status); 1811da177e4SLinus Torvalds return; 1821da177e4SLinus Torvalds } 1831da177e4SLinus Torvalds 18438d10769SDmitry Torokhov __iforce_usb_xmit(iforce); 1851da177e4SLinus Torvalds 1861da177e4SLinus Torvalds wake_up(&iforce->wait); 1871da177e4SLinus Torvalds } 1881da177e4SLinus Torvalds 1891da177e4SLinus Torvalds static int iforce_usb_probe(struct usb_interface *intf, 1901da177e4SLinus Torvalds const struct usb_device_id *id) 1911da177e4SLinus Torvalds { 1921da177e4SLinus Torvalds struct usb_device *dev = interface_to_usbdev(intf); 1931da177e4SLinus Torvalds struct usb_host_interface *interface; 1941da177e4SLinus Torvalds struct usb_endpoint_descriptor *epirq, *epout; 19581fd4313SDmitry Torokhov struct iforce_usb *iforce_usb; 19617dd3f0fSDmitry Torokhov int err = -ENOMEM; 1971da177e4SLinus Torvalds 1981da177e4SLinus Torvalds interface = intf->cur_altsetting; 1991da177e4SLinus Torvalds 20059cf8bedSJohan Hovold if (interface->desc.bNumEndpoints < 2) 20159cf8bedSJohan Hovold return -ENODEV; 20259cf8bedSJohan Hovold 2031da177e4SLinus Torvalds epirq = &interface->endpoint[0].desc; 204*849f5ae3SOliver Neukum if (!usb_endpoint_is_int_in(epirq)) 205*849f5ae3SOliver Neukum return -ENODEV; 206*849f5ae3SOliver Neukum 2071da177e4SLinus Torvalds epout = &interface->endpoint[1].desc; 208*849f5ae3SOliver Neukum if (!usb_endpoint_is_int_out(epout)) 209*849f5ae3SOliver Neukum return -ENODEV; 2101da177e4SLinus Torvalds 211dfad2b17SDmitry Torokhov iforce_usb = kzalloc(sizeof(*iforce_usb), GFP_KERNEL); 212dfad2b17SDmitry Torokhov if (!iforce_usb) 2131da177e4SLinus Torvalds goto fail; 2141da177e4SLinus Torvalds 215dfad2b17SDmitry Torokhov iforce_usb->irq = usb_alloc_urb(0, GFP_KERNEL); 216dfad2b17SDmitry Torokhov if (!iforce_usb->irq) 2171da177e4SLinus Torvalds goto fail; 2181da177e4SLinus Torvalds 219dfad2b17SDmitry Torokhov iforce_usb->out = usb_alloc_urb(0, GFP_KERNEL); 220dfad2b17SDmitry Torokhov if (!iforce_usb->out) 2211da177e4SLinus Torvalds goto fail; 2221da177e4SLinus Torvalds 2238624dfd1SDmitry Torokhov iforce_usb->iforce.xport_ops = &iforce_usb_xport_ops; 2241da177e4SLinus Torvalds 22581fd4313SDmitry Torokhov iforce_usb->usbdev = dev; 22681fd4313SDmitry Torokhov iforce_usb->intf = intf; 2271da177e4SLinus Torvalds 228dfad2b17SDmitry Torokhov usb_fill_int_urb(iforce_usb->irq, dev, 229dfad2b17SDmitry Torokhov usb_rcvintpipe(dev, epirq->bEndpointAddress), 230dfad2b17SDmitry Torokhov iforce_usb->data_in, sizeof(iforce_usb->data_in), 231dfad2b17SDmitry Torokhov iforce_usb_irq, iforce_usb, epirq->bInterval); 2321da177e4SLinus Torvalds 233dfad2b17SDmitry Torokhov usb_fill_int_urb(iforce_usb->out, dev, 234dfad2b17SDmitry Torokhov usb_sndintpipe(dev, epout->bEndpointAddress), 235dfad2b17SDmitry Torokhov iforce_usb->data_out, sizeof(iforce_usb->data_out), 236dfad2b17SDmitry Torokhov iforce_usb_out, iforce_usb, epout->bInterval); 23781fd4313SDmitry Torokhov 2388624dfd1SDmitry Torokhov err = iforce_init_device(&intf->dev, BUS_USB, &iforce_usb->iforce); 23917dd3f0fSDmitry Torokhov if (err) 24017dd3f0fSDmitry Torokhov goto fail; 2411da177e4SLinus Torvalds 24281fd4313SDmitry Torokhov usb_set_intfdata(intf, iforce_usb); 2431da177e4SLinus Torvalds return 0; 2441da177e4SLinus Torvalds 2451da177e4SLinus Torvalds fail: 24681fd4313SDmitry Torokhov if (iforce_usb) { 24781fd4313SDmitry Torokhov usb_free_urb(iforce_usb->irq); 24881fd4313SDmitry Torokhov usb_free_urb(iforce_usb->out); 24981fd4313SDmitry Torokhov kfree(iforce_usb); 2501da177e4SLinus Torvalds } 2511da177e4SLinus Torvalds 25217dd3f0fSDmitry Torokhov return err; 2531da177e4SLinus Torvalds } 2541da177e4SLinus Torvalds 25598b7fb04SDmitry Torokhov static void iforce_usb_disconnect(struct usb_interface *intf) 2561da177e4SLinus Torvalds { 25781fd4313SDmitry Torokhov struct iforce_usb *iforce_usb = usb_get_intfdata(intf); 25898b7fb04SDmitry Torokhov 25998b7fb04SDmitry Torokhov usb_set_intfdata(intf, NULL); 26098b7fb04SDmitry Torokhov 26181fd4313SDmitry Torokhov input_unregister_device(iforce_usb->iforce.dev); 2621da177e4SLinus Torvalds 26381fd4313SDmitry Torokhov usb_free_urb(iforce_usb->irq); 26481fd4313SDmitry Torokhov usb_free_urb(iforce_usb->out); 2651da177e4SLinus Torvalds 26681fd4313SDmitry Torokhov kfree(iforce_usb); 2671da177e4SLinus Torvalds } 2681da177e4SLinus Torvalds 269c1295851SArvind Yadav static const struct usb_device_id iforce_usb_ids[] = { 2701da177e4SLinus Torvalds { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */ 2711da177e4SLinus Torvalds { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */ 2721da177e4SLinus Torvalds { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */ 2731da177e4SLinus Torvalds { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */ 2741da177e4SLinus Torvalds { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */ 2751da177e4SLinus Torvalds { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */ 2761da177e4SLinus Torvalds { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */ 27768947b8fSJiri Kosina { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */ 27811518370STim Schumacher { USB_DEVICE(0x06a3, 0xff04) }, /* Saitek R440 Force Wheel */ 2791da177e4SLinus Torvalds { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */ 280d861f7bfSMarek Vasut { USB_DEVICE(0x06f8, 0x0003) }, /* Guillemot Jet Leader Force Feedback */ 2811da177e4SLinus Torvalds { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */ 28218098a6cSMarian-Nicolae V. Ion { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */ 2831da177e4SLinus Torvalds { } /* Terminating entry */ 2841da177e4SLinus Torvalds }; 2851da177e4SLinus Torvalds 2861da177e4SLinus Torvalds MODULE_DEVICE_TABLE (usb, iforce_usb_ids); 2871da177e4SLinus Torvalds 2881da177e4SLinus Torvalds struct usb_driver iforce_usb_driver = { 2891da177e4SLinus Torvalds .name = "iforce", 2901da177e4SLinus Torvalds .probe = iforce_usb_probe, 2911da177e4SLinus Torvalds .disconnect = iforce_usb_disconnect, 2921da177e4SLinus Torvalds .id_table = iforce_usb_ids, 2931da177e4SLinus Torvalds }; 2944f99de6dSDmitry Torokhov 2954f99de6dSDmitry Torokhov module_usb_driver(iforce_usb_driver); 2964f99de6dSDmitry Torokhov 2974f99de6dSDmitry Torokhov MODULE_AUTHOR("Vojtech Pavlik <vojtech@ucw.cz>, Johann Deneux <johann.deneux@gmail.com>"); 2984f99de6dSDmitry Torokhov MODULE_DESCRIPTION("USB I-Force joysticks and wheels driver"); 2994f99de6dSDmitry Torokhov MODULE_LICENSE("GPL"); 300