iforce-usb.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright (c) 2000-2002 Vojtech Pavlik <vojtech@ucw.cz>
  3. * Copyright (c) 2001-2002, 2007 Johann Deneux <johann.deneux@gmail.com>
  4. *
  5. * USB/RS232 I-Force joysticks and wheels.
  6. */
  7. /*
  8. * This program is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  21. */
  22. #include "iforce.h"
  23. void iforce_usb_xmit(struct iforce *iforce)
  24. {
  25. int n, c;
  26. unsigned long flags;
  27. spin_lock_irqsave(&iforce->xmit_lock, flags);
  28. if (iforce->xmit.head == iforce->xmit.tail) {
  29. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  30. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  31. return;
  32. }
  33. ((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
  34. XMIT_INC(iforce->xmit.tail, 1);
  35. n = iforce->xmit.buf[iforce->xmit.tail];
  36. XMIT_INC(iforce->xmit.tail, 1);
  37. iforce->out->transfer_buffer_length = n + 1;
  38. iforce->out->dev = iforce->usbdev;
  39. /* Copy rest of data then */
  40. c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
  41. if (n < c) c=n;
  42. memcpy(iforce->out->transfer_buffer + 1,
  43. &iforce->xmit.buf[iforce->xmit.tail],
  44. c);
  45. if (n != c) {
  46. memcpy(iforce->out->transfer_buffer + 1 + c,
  47. &iforce->xmit.buf[0],
  48. n-c);
  49. }
  50. XMIT_INC(iforce->xmit.tail, n);
  51. if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) {
  52. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  53. dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n);
  54. }
  55. /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
  56. * As long as the urb completion handler is not called, the transmiting
  57. * is considered to be running */
  58. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  59. }
  60. static void iforce_usb_irq(struct urb *urb)
  61. {
  62. struct iforce *iforce = urb->context;
  63. struct device *dev = &iforce->intf->dev;
  64. int status;
  65. switch (urb->status) {
  66. case 0:
  67. /* success */
  68. break;
  69. case -ECONNRESET:
  70. case -ENOENT:
  71. case -ESHUTDOWN:
  72. /* this urb is terminated, clean up */
  73. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  74. __func__, urb->status);
  75. return;
  76. default:
  77. dev_dbg(dev, "%s - urb has status of: %d\n",
  78. __func__, urb->status);
  79. goto exit;
  80. }
  81. iforce_process_packet(iforce,
  82. (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
  83. exit:
  84. status = usb_submit_urb (urb, GFP_ATOMIC);
  85. if (status)
  86. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  87. __func__, status);
  88. }
  89. static void iforce_usb_out(struct urb *urb)
  90. {
  91. struct iforce *iforce = urb->context;
  92. if (urb->status) {
  93. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  94. dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n",
  95. urb->status);
  96. return;
  97. }
  98. iforce_usb_xmit(iforce);
  99. wake_up(&iforce->wait);
  100. }
  101. static void iforce_usb_ctrl(struct urb *urb)
  102. {
  103. struct iforce *iforce = urb->context;
  104. if (urb->status) return;
  105. iforce->ecmd = 0xff00 | urb->actual_length;
  106. wake_up(&iforce->wait);
  107. }
  108. static int iforce_usb_probe(struct usb_interface *intf,
  109. const struct usb_device_id *id)
  110. {
  111. struct usb_device *dev = interface_to_usbdev(intf);
  112. struct usb_host_interface *interface;
  113. struct usb_endpoint_descriptor *epirq, *epout;
  114. struct iforce *iforce;
  115. int err = -ENOMEM;
  116. interface = intf->cur_altsetting;
  117. if (interface->desc.bNumEndpoints < 2)
  118. return -ENODEV;
  119. epirq = &interface->endpoint[0].desc;
  120. if (!usb_endpoint_is_int_in(epirq))
  121. return -ENODEV;
  122. epout = &interface->endpoint[1].desc;
  123. if (!usb_endpoint_is_int_out(epout))
  124. return -ENODEV;
  125. if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
  126. goto fail;
  127. if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
  128. goto fail;
  129. if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
  130. goto fail;
  131. if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
  132. goto fail;
  133. iforce->bus = IFORCE_USB;
  134. iforce->usbdev = dev;
  135. iforce->intf = intf;
  136. iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
  137. iforce->cr.wIndex = 0;
  138. iforce->cr.wLength = cpu_to_le16(16);
  139. usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
  140. iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
  141. usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
  142. iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
  143. usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
  144. (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
  145. err = iforce_init_device(iforce);
  146. if (err)
  147. goto fail;
  148. usb_set_intfdata(intf, iforce);
  149. return 0;
  150. fail:
  151. if (iforce) {
  152. usb_free_urb(iforce->irq);
  153. usb_free_urb(iforce->out);
  154. usb_free_urb(iforce->ctrl);
  155. kfree(iforce);
  156. }
  157. return err;
  158. }
  159. static void iforce_usb_disconnect(struct usb_interface *intf)
  160. {
  161. struct iforce *iforce = usb_get_intfdata(intf);
  162. usb_set_intfdata(intf, NULL);
  163. input_unregister_device(iforce->dev);
  164. usb_free_urb(iforce->irq);
  165. usb_free_urb(iforce->out);
  166. usb_free_urb(iforce->ctrl);
  167. kfree(iforce);
  168. }
  169. static const struct usb_device_id iforce_usb_ids[] = {
  170. { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */
  171. { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */
  172. { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */
  173. { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */
  174. { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */
  175. { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */
  176. { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */
  177. { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */
  178. { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */
  179. { USB_DEVICE(0x06f8, 0x0003) }, /* Guillemot Jet Leader Force Feedback */
  180. { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */
  181. { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */
  182. { } /* Terminating entry */
  183. };
  184. MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
  185. struct usb_driver iforce_usb_driver = {
  186. .name = "iforce",
  187. .probe = iforce_usb_probe,
  188. .disconnect = iforce_usb_disconnect,
  189. .id_table = iforce_usb_ids,
  190. };