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