vhci_rx.c 6.5 KB

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