vhci_tx.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. static void setup_cmd_submit_pdu(struct usbip_header *pdup, struct urb *urb)
  24. {
  25. struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv);
  26. struct vhci_device *vdev = priv->vdev;
  27. usbip_dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
  28. usb_pipedevice(urb->pipe), vdev->devid);
  29. pdup->base.command = USBIP_CMD_SUBMIT;
  30. pdup->base.seqnum = priv->seqnum;
  31. pdup->base.devid = vdev->devid;
  32. pdup->base.direction = usb_pipein(urb->pipe) ?
  33. USBIP_DIR_IN : USBIP_DIR_OUT;
  34. pdup->base.ep = usb_pipeendpoint(urb->pipe);
  35. usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1);
  36. if (urb->setup_packet)
  37. memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8);
  38. }
  39. static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev)
  40. {
  41. struct vhci_priv *priv, *tmp;
  42. unsigned long flags;
  43. spin_lock_irqsave(&vdev->priv_lock, flags);
  44. list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) {
  45. list_move_tail(&priv->list, &vdev->priv_rx);
  46. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  47. return priv;
  48. }
  49. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  50. return NULL;
  51. }
  52. static int vhci_send_cmd_submit(struct vhci_device *vdev)
  53. {
  54. struct vhci_priv *priv = NULL;
  55. struct msghdr msg;
  56. struct kvec iov[3];
  57. size_t txsize;
  58. size_t total_size = 0;
  59. while ((priv = dequeue_from_priv_tx(vdev)) != NULL) {
  60. int ret;
  61. struct urb *urb = priv->urb;
  62. struct usbip_header pdu_header;
  63. struct usbip_iso_packet_descriptor *iso_buffer = NULL;
  64. txsize = 0;
  65. memset(&pdu_header, 0, sizeof(pdu_header));
  66. memset(&msg, 0, sizeof(msg));
  67. memset(&iov, 0, sizeof(iov));
  68. usbip_dbg_vhci_tx("setup txdata urb seqnum %lu\n",
  69. priv->seqnum);
  70. /* 1. setup usbip_header */
  71. setup_cmd_submit_pdu(&pdu_header, urb);
  72. usbip_header_correct_endian(&pdu_header, 1);
  73. iov[0].iov_base = &pdu_header;
  74. iov[0].iov_len = sizeof(pdu_header);
  75. txsize += sizeof(pdu_header);
  76. /* 2. setup transfer buffer */
  77. if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) {
  78. iov[1].iov_base = urb->transfer_buffer;
  79. iov[1].iov_len = urb->transfer_buffer_length;
  80. txsize += urb->transfer_buffer_length;
  81. }
  82. /* 3. setup iso_packet_descriptor */
  83. if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
  84. ssize_t len = 0;
  85. iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
  86. if (!iso_buffer) {
  87. usbip_event_add(&vdev->ud,
  88. SDEV_EVENT_ERROR_MALLOC);
  89. return -1;
  90. }
  91. iov[2].iov_base = iso_buffer;
  92. iov[2].iov_len = len;
  93. txsize += len;
  94. }
  95. ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize);
  96. if (ret != txsize) {
  97. pr_err("sendmsg failed!, ret=%d for %zd\n", ret,
  98. txsize);
  99. kfree(iso_buffer);
  100. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
  101. return -1;
  102. }
  103. kfree(iso_buffer);
  104. usbip_dbg_vhci_tx("send txdata\n");
  105. total_size += txsize;
  106. }
  107. return total_size;
  108. }
  109. static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev)
  110. {
  111. struct vhci_unlink *unlink, *tmp;
  112. unsigned long flags;
  113. spin_lock_irqsave(&vdev->priv_lock, flags);
  114. list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
  115. list_move_tail(&unlink->list, &vdev->unlink_rx);
  116. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  117. return unlink;
  118. }
  119. spin_unlock_irqrestore(&vdev->priv_lock, flags);
  120. return NULL;
  121. }
  122. static int vhci_send_cmd_unlink(struct vhci_device *vdev)
  123. {
  124. struct vhci_unlink *unlink = NULL;
  125. struct msghdr msg;
  126. struct kvec iov[3];
  127. size_t txsize;
  128. size_t total_size = 0;
  129. while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) {
  130. int ret;
  131. struct usbip_header pdu_header;
  132. txsize = 0;
  133. memset(&pdu_header, 0, sizeof(pdu_header));
  134. memset(&msg, 0, sizeof(msg));
  135. memset(&iov, 0, sizeof(iov));
  136. usbip_dbg_vhci_tx("setup cmd unlink, %lu\n", unlink->seqnum);
  137. /* 1. setup usbip_header */
  138. pdu_header.base.command = USBIP_CMD_UNLINK;
  139. pdu_header.base.seqnum = unlink->seqnum;
  140. pdu_header.base.devid = vdev->devid;
  141. pdu_header.base.ep = 0;
  142. pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum;
  143. usbip_header_correct_endian(&pdu_header, 1);
  144. iov[0].iov_base = &pdu_header;
  145. iov[0].iov_len = sizeof(pdu_header);
  146. txsize += sizeof(pdu_header);
  147. ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize);
  148. if (ret != txsize) {
  149. pr_err("sendmsg failed!, ret=%d for %zd\n", ret,
  150. txsize);
  151. usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
  152. return -1;
  153. }
  154. usbip_dbg_vhci_tx("send txdata\n");
  155. total_size += txsize;
  156. }
  157. return total_size;
  158. }
  159. int vhci_tx_loop(void *data)
  160. {
  161. struct usbip_device *ud = data;
  162. struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
  163. while (!kthread_should_stop()) {
  164. if (vhci_send_cmd_submit(vdev) < 0)
  165. break;
  166. if (vhci_send_cmd_unlink(vdev) < 0)
  167. break;
  168. wait_event_interruptible(vdev->waitq_tx,
  169. (!list_empty(&vdev->priv_tx) ||
  170. !list_empty(&vdev->unlink_tx) ||
  171. kthread_should_stop()));
  172. usbip_dbg_vhci_tx("pending urbs ?, now wake up\n");
  173. }
  174. return 0;
  175. }