iforce-usb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  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->dev->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. int status;
  68. switch (urb->status) {
  69. case 0:
  70. /* success */
  71. break;
  72. case -ECONNRESET:
  73. case -ENOENT:
  74. case -ESHUTDOWN:
  75. /* this urb is terminated, clean up */
  76. dbg("%s - urb shutting down with status: %d",
  77. __func__, urb->status);
  78. return;
  79. default:
  80. dbg("%s - urb has status of: %d", __func__, urb->status);
  81. goto exit;
  82. }
  83. iforce_process_packet(iforce,
  84. (iforce->data[0] << 8) | (urb->actual_length - 1), iforce->data + 1);
  85. exit:
  86. status = usb_submit_urb (urb, GFP_ATOMIC);
  87. if (status)
  88. err ("%s - usb_submit_urb failed with result %d",
  89. __func__, status);
  90. }
  91. static void iforce_usb_out(struct urb *urb)
  92. {
  93. struct iforce *iforce = urb->context;
  94. if (urb->status) {
  95. clear_bit(IFORCE_XMIT_RUNNING, iforce->xmit_flags);
  96. dbg("urb->status %d, exiting", urb->status);
  97. return;
  98. }
  99. iforce_usb_xmit(iforce);
  100. wake_up(&iforce->wait);
  101. }
  102. static void iforce_usb_ctrl(struct urb *urb)
  103. {
  104. struct iforce *iforce = urb->context;
  105. if (urb->status) return;
  106. iforce->ecmd = 0xff00 | urb->actual_length;
  107. wake_up(&iforce->wait);
  108. }
  109. static int iforce_usb_probe(struct usb_interface *intf,
  110. const struct usb_device_id *id)
  111. {
  112. struct usb_device *dev = interface_to_usbdev(intf);
  113. struct usb_host_interface *interface;
  114. struct usb_endpoint_descriptor *epirq, *epout;
  115. struct iforce *iforce;
  116. int err = -ENOMEM;
  117. interface = intf->cur_altsetting;
  118. epirq = &interface->endpoint[0].desc;
  119. epout = &interface->endpoint[1].desc;
  120. if (!(iforce = kzalloc(sizeof(struct iforce) + 32, GFP_KERNEL)))
  121. goto fail;
  122. if (!(iforce->irq = usb_alloc_urb(0, GFP_KERNEL)))
  123. goto fail;
  124. if (!(iforce->out = usb_alloc_urb(0, GFP_KERNEL)))
  125. goto fail;
  126. if (!(iforce->ctrl = usb_alloc_urb(0, GFP_KERNEL)))
  127. goto fail;
  128. iforce->bus = IFORCE_USB;
  129. iforce->usbdev = dev;
  130. iforce->cr.bRequestType = USB_TYPE_VENDOR | USB_DIR_IN | USB_RECIP_INTERFACE;
  131. iforce->cr.wIndex = 0;
  132. iforce->cr.wLength = cpu_to_le16(16);
  133. usb_fill_int_urb(iforce->irq, dev, usb_rcvintpipe(dev, epirq->bEndpointAddress),
  134. iforce->data, 16, iforce_usb_irq, iforce, epirq->bInterval);
  135. usb_fill_int_urb(iforce->out, dev, usb_sndintpipe(dev, epout->bEndpointAddress),
  136. iforce + 1, 32, iforce_usb_out, iforce, epout->bInterval);
  137. usb_fill_control_urb(iforce->ctrl, dev, usb_rcvctrlpipe(dev, 0),
  138. (void*) &iforce->cr, iforce->edata, 16, iforce_usb_ctrl, iforce);
  139. err = iforce_init_device(iforce);
  140. if (err)
  141. goto fail;
  142. usb_set_intfdata(intf, iforce);
  143. return 0;
  144. fail:
  145. if (iforce) {
  146. usb_free_urb(iforce->irq);
  147. usb_free_urb(iforce->out);
  148. usb_free_urb(iforce->ctrl);
  149. kfree(iforce);
  150. }
  151. return err;
  152. }
  153. static void iforce_usb_disconnect(struct usb_interface *intf)
  154. {
  155. struct iforce *iforce = usb_get_intfdata(intf);
  156. usb_set_intfdata(intf, NULL);
  157. input_unregister_device(iforce->dev);
  158. usb_free_urb(iforce->irq);
  159. usb_free_urb(iforce->out);
  160. usb_free_urb(iforce->ctrl);
  161. kfree(iforce);
  162. }
  163. static struct usb_device_id iforce_usb_ids [] = {
  164. { USB_DEVICE(0x044f, 0xa01c) }, /* Thrustmaster Motor Sport GT */
  165. { USB_DEVICE(0x046d, 0xc281) }, /* Logitech WingMan Force */
  166. { USB_DEVICE(0x046d, 0xc291) }, /* Logitech WingMan Formula Force */
  167. { USB_DEVICE(0x05ef, 0x020a) }, /* AVB Top Shot Pegasus */
  168. { USB_DEVICE(0x05ef, 0x8884) }, /* AVB Mag Turbo Force */
  169. { USB_DEVICE(0x05ef, 0x8888) }, /* AVB Top Shot FFB Racing Wheel */
  170. { USB_DEVICE(0x061c, 0xc0a4) }, /* ACT LABS Force RS */
  171. { USB_DEVICE(0x061c, 0xc084) }, /* ACT LABS Force RS */
  172. { USB_DEVICE(0x06f8, 0x0001) }, /* Guillemot Race Leader Force Feedback */
  173. { USB_DEVICE(0x06f8, 0x0003) }, /* Guillemot Jet Leader Force Feedback */
  174. { USB_DEVICE(0x06f8, 0x0004) }, /* Guillemot Force Feedback Racing Wheel */
  175. { USB_DEVICE(0x06f8, 0xa302) }, /* Guillemot Jet Leader 3D */
  176. { } /* Terminating entry */
  177. };
  178. MODULE_DEVICE_TABLE (usb, iforce_usb_ids);
  179. struct usb_driver iforce_usb_driver = {
  180. .name = "iforce",
  181. .probe = iforce_usb_probe,
  182. .disconnect = iforce_usb_disconnect,
  183. .id_table = iforce_usb_ids,
  184. };