vhci_rx.c 6.2 KB

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