iforce-usb.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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. * Should you need to contact me, the author, you can do so either by
  23. * e-mail - mail your message to <vojtech@ucw.cz>, or by paper mail:
  24. * Vojtech Pavlik, Simunkova 1594, Prague 8, 182 00 Czech Republic
  25. */
  26. #include "iforce.h"
  27. void iforce_usb_xmit(struct iforce *iforce)
  28. {
  29. int n, c;
  30. unsigned long flags;
  31. spin_lock_irqsave(&iforce->xmit_lock, flags);
  32. if (iforce->xmit.head == iforce->xmit.tail) {
  33. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  34. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  35. return;
  36. }
  37. ((char *)iforce->out->transfer_buffer)[0] = iforce->xmit.buf[iforce->xmit.tail];
  38. XMIT_INC(iforce->xmit.tail, 1);
  39. n = iforce->xmit.buf[iforce->xmit.tail];
  40. XMIT_INC(iforce->xmit.tail, 1);
  41. iforce->out->transfer_buffer_length = n + 1;
  42. iforce->out->dev = iforce->usbdev;
  43. /* Copy rest of data then */
  44. c = CIRC_CNT_TO_END(iforce->xmit.head, iforce->xmit.tail, XMIT_SIZE);
  45. if (n < c) c=n;
  46. memcpy(iforce->out->transfer_buffer + 1,
  47. &iforce->xmit.buf[iforce->xmit.tail],
  48. c);
  49. if (n != c) {
  50. memcpy(iforce->out->transfer_buffer + 1 + c,
  51. &iforce->xmit.buf[0],
  52. n-c);
  53. }
  54. XMIT_INC(iforce->xmit.tail, n);
  55. if ( (n=usb_submit_urb(iforce->out, GFP_ATOMIC)) ) {
  56. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  57. dev_warn(&iforce->intf->dev, "usb_submit_urb failed %d\n", n);
  58. }
  59. /* The IFORCE_XMIT_RUNNING bit is not cleared here. That's intended.
  60. * As long as the urb completion handler is not called, the transmiting
  61. * is considered to be running */
  62. spin_unlock_irqrestore(&iforce->xmit_lock, flags);
  63. }
  64. static void iforce_usb_irq(struct urb *urb)
  65. {
  66. struct iforce *iforce = urb->context;
  67. struct device *dev = &iforce->intf->dev;
  68. int status;
  69. switch (urb->status) {
  70. case 0:
  71. /* success */
  72. break;
  73. case -ECONNRESET:
  74. case -ENOENT:
  75. case -ESHUTDOWN:
  76. /* this urb is terminated, clean up */
  77. dev_dbg(dev, "%s - urb shutting down with status: %d\n",
  78. __func__, urb->status);
  79. return;
  80. default:
  81. dev_dbg(dev, "%s - urb has status of: %d\n",
  82. __func__, urb->status);
  83. goto exit;
  84. }
  85. iforce_process_packet(iforce,
  86. (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
  87. exit:
  88. status = usb_submit_urb (urb, GFP_ATOMIC);
  89. if (status)
  90. dev_err(dev, "%s - usb_submit_urb failed with result %d\n",
  91. __func__, status);
  92. }
  93. static void iforce_usb_out(struct urb *urb)
  94. {
  95. struct iforce *iforce = urb->context;
  96. if (urb->status) {
  97. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  98. dev_dbg(&iforce->intf->dev, "urb->status %d, exiting\n",
  99. urb->status);
  100. return;
  101. }
  102. iforce_usb_xmit(iforce);
  103. wake_up(&iforce->wait);
  104. }
  105. static void iforce_usb_ctrl(struct urb *urb)
  106. {
  107. struct iforce *iforce = urb->context;
  108. if (urb->status) return;
  109. iforce->ecmd = 0xff00 | urb->actual_length;
  110. wake_up(&iforce->wait);
  111. }
  112. static int iforce_usb_probe(struct usb_interface *intf,
  113. const struct usb_device_id *id)
  114. {
  115. struct usb_device *dev = interface_to_usbdev(intf);
  116. struct usb_host_interface *interface;
  117. struct usb_endpoint_descriptor *epirq, *epout;
  118. struct iforce *iforce;
  119. int err = -ENOMEM;
  120. interface = intf->cur_altsetting;
  121. if (interface->desc.bNumEndpoints < 2)
  122. return -ENODEV;
  123. epirq = &interface->endpoint[0].desc;
  124. epout = &interface->endpoint[1].desc;
  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 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. };