vhci_rx.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. /*
  2. * Copyright (C) 2003-2008 Takahiro Hirofuchi
  3. *
  4. * This is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
  17. * USA.
  18. */
  19. #include <linux/kthread.h>
  20. #include <linux/slab.h>
  21. #include "usbip_common.h"
  22. #include "vhci.h"
  23. /* get URB from transmitted urb queue. caller must hold vdev->priv_lock */
  24. struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum)
  25. {
  26. struct vhci_priv *priv, *tmp;
  27. struct urb *urb = NULL;
  28. int status;
  29. list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
  30. if (priv->seqnum != seqnum)
  31. continue;
  32. urb = priv->urb;
  33. status = urb->status;
  34. usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
  35. urb, priv, seqnum);
  36. switch (status) {
  37. case -ENOENT:
  38. /* fall through */
  39. case -ECONNRESET:
  40. dev_info(&urb->dev->dev,
  41. "urb %p was unlinked %ssynchronuously.\n", urb,
  42. status == -ENOENT ? "" : "a");
  43. break;
  44. case -EINPROGRESS:
  45. /* no info output */
  46. break;
  47. default:
  48. dev_info(&urb->dev->dev,
  49. "urb %p may be in a error, status %d\n", urb,
  50. status);
  51. }
  52. list_del(&priv->list);
  53. kfree(priv);
  54. urb->hcpriv = NULL;
  55. break;
  56. }
  57. return urb;
  58. }
  59. static void vhci_recv_ret_submit(struct vhci_device *vdev,
  60. struct usbip_header *pdu)
  61. {
  62. struct vhci_hcd *vhci = vdev_to_vhci(vdev);
  63. struct usbip_device *ud = &vdev->ud;
  64. struct urb *urb;
  65. unsigned long flags;
  66. spin_lock_irqsave(&vdev->priv_lock, flags);
  67. urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
  68. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  69. if (!urb) {
  70. pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
  71. pr_info("max seqnum %d\n",
  72. atomic_read(&vhci->seqnum));
  73. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  74. return;
  75. }
  76. /* unpack the pdu to a urb */
  77. usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
  78. /* recv transfer buffer */
  79. if (usbip_recv_xbuff(ud, urb) < 0)
  80. return;
  81. /* recv iso_packet_descriptor */
  82. if (usbip_recv_iso(ud, urb) < 0)
  83. return;
  84. /* restore the padding in iso packets */
  85. usbip_pad_iso(ud, urb);
  86. if (usbip_dbg_flag_vhci_rx)
  87. usbip_dump_urb(urb);
  88. usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
  89. spin_lock_irqsave(&vhci->lock, flags);
  90. usb_hcd_unlink_urb_from_ep(vhci_to_hcd(vhci), urb);
  91. spin_unlock_irqrestore(&vhci->lock, flags);
  92. usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
  93. usbip_dbg_vhci_rx("Leave\n");
  94. }
  95. static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
  96. struct usbip_header *pdu)
  97. {
  98. struct vhci_unlink *unlink, *tmp;
  99. unsigned long flags;
  100. spin_lock_irqsave(&vdev->priv_lock, flags);
  101. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
  102. pr_info("unlink->seqnum %lu\n", unlink->seqnum);
  103. if (unlink->seqnum == pdu->base.seqnum) {
  104. usbip_dbg_vhci_rx("found pending unlink, %lu\n",
  105. unlink->seqnum);
  106. list_del(&unlink->list);
  107. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  108. return unlink;
  109. }
  110. }
  111. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  112. return NULL;
  113. }
  114. static void vhci_recv_ret_unlink(struct vhci_device *vdev,
  115. struct usbip_header *pdu)
  116. {
  117. struct vhci_hcd *vhci = vdev_to_vhci(vdev);
  118. struct vhci_unlink *unlink;
  119. struct urb *urb;
  120. unsigned long flags;
  121. usbip_dump_header(pdu);
  122. unlink = dequeue_pending_unlink(vdev, pdu);
  123. if (!unlink) {
  124. pr_info("cannot find the pending unlink %u\n",
  125. pdu->base.seqnum);
  126. return;
  127. }
  128. spin_lock_irqsave(&vdev->priv_lock, flags);
  129. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  130. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  131. if (!urb) {
  132. /*
  133. * I get the result of a unlink request. But, it seems that I
  134. * already received the result of its submit result and gave
  135. * back the URB.
  136. */
  137. pr_info("the urb (seqnum %d) was already given back\n",
  138. pdu->base.seqnum);
  139. } else {
  140. usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
  141. /* If unlink is successful, status is -ECONNRESET */
  142. urb->status = pdu->u.ret_unlink.status;
  143. pr_info("urb->status %d\n", urb->status);
  144. spin_lock_irqsave(&vhci->lock, flags);
  145. usb_hcd_unlink_urb_from_ep(vhci_to_hcd(vhci), urb);
  146. spin_unlock_irqrestore(&vhci->lock, flags);
  147. usb_hcd_giveback_urb(vhci_to_hcd(vhci), urb, urb->status);
  148. }
  149. kfree(unlink);
  150. }
  151. static int vhci_priv_tx_empty(struct vhci_device *vdev)
  152. {
  153. int empty = 0;
  154. unsigned long flags;
  155. spin_lock_irqsave(&vdev->priv_lock, flags);
  156. empty = list_empty(&vdev->priv_rx);
  157. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  158. return empty;
  159. }
  160. /* recv a pdu */
  161. static void vhci_rx_pdu(struct usbip_device *ud)
  162. {
  163. int ret;
  164. struct usbip_header pdu;
  165. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  166. usbip_dbg_vhci_rx("Enter\n");
  167. memset(&pdu, 0, sizeof(pdu));
  168. /* receive a pdu header */
  169. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  170. if (ret < 0) {
  171. if (ret == -ECONNRESET)
  172. pr_info("connection reset by peer\n");
  173. else if (ret == -EAGAIN) {
  174. /* ignore if connection was idle */
  175. if (vhci_priv_tx_empty(vdev))
  176. return;
  177. pr_info("connection timed out with pending urbs\n");
  178. } else if (ret != -ERESTARTSYS)
  179. pr_info("xmit failed %d\n", ret);
  180. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  181. return;
  182. }
  183. if (ret == 0) {
  184. pr_info("connection closed");
  185. usbip_event_add(ud, VDEV_EVENT_DOWN);
  186. return;
  187. }
  188. if (ret != sizeof(pdu)) {
  189. pr_err("received pdu size is %d, should be %d\n", ret,
  190. (unsigned int)sizeof(pdu));
  191. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  192. return;
  193. }
  194. usbip_header_correct_endian(&pdu, 0);
  195. if (usbip_dbg_flag_vhci_rx)
  196. usbip_dump_header(&pdu);
  197. switch (pdu.base.command) {
  198. case USBIP_RET_SUBMIT:
  199. vhci_recv_ret_submit(vdev, &pdu);
  200. break;
  201. case USBIP_RET_UNLINK:
  202. vhci_recv_ret_unlink(vdev, &pdu);
  203. break;
  204. default:
  205. /* NOT REACHED */
  206. pr_err("unknown pdu %u\n", pdu.base.command);
  207. usbip_dump_header(&pdu);
  208. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  209. break;
  210. }
  211. }
  212. int vhci_rx_loop(void *data)
  213. {
  214. struct usbip_device *ud = data;
  215. while (!kthread_should_stop()) {
  216. if (usbip_event_happened(ud))
  217. break;
  218. vhci_rx_pdu(ud);
  219. }
  220. return 0;
  221. }