vhci_rx.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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_hcd = vdev_to_vhci_hcd(vdev);
  63. struct vhci *vhci = vhci_hcd->vhci;
  64. struct usbip_device *ud = &vdev->ud;
  65. struct urb *urb;
  66. unsigned long flags;
  67. spin_lock_irqsave(&vdev->priv_lock, flags);
  68. urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
  69. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  70. if (!urb) {
  71. pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
  72. pr_info("max seqnum %d\n",
  73. atomic_read(&vhci_hcd->seqnum));
  74. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  75. return;
  76. }
  77. /* unpack the pdu to a urb */
  78. usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
  79. /* recv transfer buffer */
  80. if (usbip_recv_xbuff(ud, urb) < 0)
  81. return;
  82. /* recv iso_packet_descriptor */
  83. if (usbip_recv_iso(ud, urb) < 0)
  84. return;
  85. /* restore the padding in iso packets */
  86. usbip_pad_iso(ud, urb);
  87. if (usbip_dbg_flag_vhci_rx)
  88. usbip_dump_urb(urb);
  89. usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
  90. spin_lock_irqsave(&vhci->lock, flags);
  91. usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
  92. spin_unlock_irqrestore(&vhci->lock, flags);
  93. usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
  94. usbip_dbg_vhci_rx("Leave\n");
  95. }
  96. static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
  97. struct usbip_header *pdu)
  98. {
  99. struct vhci_unlink *unlink, *tmp;
  100. unsigned long flags;
  101. spin_lock_irqsave(&vdev->priv_lock, flags);
  102. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
  103. pr_info("unlink->seqnum %lu\n", unlink->seqnum);
  104. if (unlink->seqnum == pdu->base.seqnum) {
  105. usbip_dbg_vhci_rx("found pending unlink, %lu\n",
  106. unlink->seqnum);
  107. list_del(&unlink->list);
  108. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  109. return unlink;
  110. }
  111. }
  112. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  113. return NULL;
  114. }
  115. static void vhci_recv_ret_unlink(struct vhci_device *vdev,
  116. struct usbip_header *pdu)
  117. {
  118. struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
  119. struct vhci *vhci = vhci_hcd->vhci;
  120. struct vhci_unlink *unlink;
  121. struct urb *urb;
  122. unsigned long flags;
  123. usbip_dump_header(pdu);
  124. unlink = dequeue_pending_unlink(vdev, pdu);
  125. if (!unlink) {
  126. pr_info("cannot find the pending unlink %u\n",
  127. pdu->base.seqnum);
  128. return;
  129. }
  130. spin_lock_irqsave(&vdev->priv_lock, flags);
  131. urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
  132. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  133. if (!urb) {
  134. /*
  135. * I get the result of a unlink request. But, it seems that I
  136. * already received the result of its submit result and gave
  137. * back the URB.
  138. */
  139. pr_info("the urb (seqnum %d) was already given back\n",
  140. pdu->base.seqnum);
  141. } else {
  142. usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
  143. /* If unlink is successful, status is -ECONNRESET */
  144. urb->status = pdu->u.ret_unlink.status;
  145. pr_info("urb->status %d\n", urb->status);
  146. spin_lock_irqsave(&vhci->lock, flags);
  147. usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
  148. spin_unlock_irqrestore(&vhci->lock, flags);
  149. usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
  150. }
  151. kfree(unlink);
  152. }
  153. static int vhci_priv_tx_empty(struct vhci_device *vdev)
  154. {
  155. int empty = 0;
  156. unsigned long flags;
  157. spin_lock_irqsave(&vdev->priv_lock, flags);
  158. empty = list_empty(&vdev->priv_rx);
  159. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  160. return empty;
  161. }
  162. /* recv a pdu */
  163. static void vhci_rx_pdu(struct usbip_device *ud)
  164. {
  165. int ret;
  166. struct usbip_header pdu;
  167. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  168. usbip_dbg_vhci_rx("Enter\n");
  169. memset(&pdu, 0, sizeof(pdu));
  170. /* receive a pdu header */
  171. ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
  172. if (ret < 0) {
  173. if (ret == -ECONNRESET)
  174. pr_info("connection reset by peer\n");
  175. else if (ret == -EAGAIN) {
  176. /* ignore if connection was idle */
  177. if (vhci_priv_tx_empty(vdev))
  178. return;
  179. pr_info("connection timed out with pending urbs\n");
  180. } else if (ret != -ERESTARTSYS)
  181. pr_info("xmit failed %d\n", ret);
  182. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  183. return;
  184. }
  185. if (ret == 0) {
  186. pr_info("connection closed");
  187. usbip_event_add(ud, VDEV_EVENT_DOWN);
  188. return;
  189. }
  190. if (ret != sizeof(pdu)) {
  191. pr_err("received pdu size is %d, should be %d\n", ret,
  192. (unsigned int)sizeof(pdu));
  193. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  194. return;
  195. }
  196. usbip_header_correct_endian(&pdu, 0);
  197. if (usbip_dbg_flag_vhci_rx)
  198. usbip_dump_header(&pdu);
  199. switch (pdu.base.command) {
  200. case USBIP_RET_SUBMIT:
  201. vhci_recv_ret_submit(vdev, &pdu);
  202. break;
  203. case USBIP_RET_UNLINK:
  204. vhci_recv_ret_unlink(vdev, &pdu);
  205. break;
  206. default:
  207. /* NOT REACHED */
  208. pr_err("unknown pdu %u\n", pdu.base.command);
  209. usbip_dump_header(&pdu);
  210. usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
  211. break;
  212. }
  213. }
  214. int vhci_rx_loop(void *data)
  215. {
  216. struct usbip_device *ud = data;
  217. while (!kthread_should_stop()) {
  218. if (usbip_event_happened(ud))
  219. break;
  220. vhci_rx_pdu(ud);
  221. }
  222. return 0;
  223. }