1 // SPDX-License-Identifier: GPL-2.0+ 2 /* 3 * Copyright (C) 2003-2008 Takahiro Hirofuchi 4 */ 5 6 #include <linux/kthread.h> 7 #include <linux/slab.h> 8 9 #include "usbip_common.h" 10 #include "vhci.h" 11 12 static void setup_cmd_submit_pdu(struct usbip_header *pdup, struct urb *urb) 13 { 14 struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv); 15 struct vhci_device *vdev = priv->vdev; 16 17 usbip_dbg_vhci_tx("URB, local devnum %u, remote devid %u\n", 18 usb_pipedevice(urb->pipe), vdev->devid); 19 20 pdup->base.command = USBIP_CMD_SUBMIT; 21 pdup->base.seqnum = priv->seqnum; 22 pdup->base.devid = vdev->devid; 23 pdup->base.direction = usb_pipein(urb->pipe) ? 24 USBIP_DIR_IN : USBIP_DIR_OUT; 25 pdup->base.ep = usb_pipeendpoint(urb->pipe); 26 27 usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1); 28 29 if (urb->setup_packet) 30 memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8); 31 } 32 33 static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev) 34 { 35 struct vhci_priv *priv, *tmp; 36 unsigned long flags; 37 38 spin_lock_irqsave(&vdev->priv_lock, flags); 39 40 list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) { 41 list_move_tail(&priv->list, &vdev->priv_rx); 42 spin_unlock_irqrestore(&vdev->priv_lock, flags); 43 return priv; 44 } 45 46 spin_unlock_irqrestore(&vdev->priv_lock, flags); 47 48 return NULL; 49 } 50 51 static int vhci_send_cmd_submit(struct vhci_device *vdev) 52 { 53 struct vhci_priv *priv = NULL; 54 55 struct msghdr msg; 56 struct kvec iov[3]; 57 size_t txsize; 58 59 size_t total_size = 0; 60 61 while ((priv = dequeue_from_priv_tx(vdev)) != NULL) { 62 int ret; 63 struct urb *urb = priv->urb; 64 struct usbip_header pdu_header; 65 struct usbip_iso_packet_descriptor *iso_buffer = NULL; 66 67 txsize = 0; 68 memset(&pdu_header, 0, sizeof(pdu_header)); 69 memset(&msg, 0, sizeof(msg)); 70 memset(&iov, 0, sizeof(iov)); 71 72 usbip_dbg_vhci_tx("setup txdata urb %p\n", urb); 73 74 /* 1. setup usbip_header */ 75 setup_cmd_submit_pdu(&pdu_header, urb); 76 usbip_header_correct_endian(&pdu_header, 1); 77 78 iov[0].iov_base = &pdu_header; 79 iov[0].iov_len = sizeof(pdu_header); 80 txsize += sizeof(pdu_header); 81 82 /* 2. setup transfer buffer */ 83 if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) { 84 iov[1].iov_base = urb->transfer_buffer; 85 iov[1].iov_len = urb->transfer_buffer_length; 86 txsize += urb->transfer_buffer_length; 87 } 88 89 /* 3. setup iso_packet_descriptor */ 90 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) { 91 ssize_t len = 0; 92 93 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len); 94 if (!iso_buffer) { 95 usbip_event_add(&vdev->ud, 96 SDEV_EVENT_ERROR_MALLOC); 97 return -1; 98 } 99 100 iov[2].iov_base = iso_buffer; 101 iov[2].iov_len = len; 102 txsize += len; 103 } 104 105 ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize); 106 if (ret != txsize) { 107 pr_err("sendmsg failed!, ret=%d for %zd\n", ret, 108 txsize); 109 kfree(iso_buffer); 110 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP); 111 return -1; 112 } 113 114 kfree(iso_buffer); 115 usbip_dbg_vhci_tx("send txdata\n"); 116 117 total_size += txsize; 118 } 119 120 return total_size; 121 } 122 123 static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev) 124 { 125 struct vhci_unlink *unlink, *tmp; 126 unsigned long flags; 127 128 spin_lock_irqsave(&vdev->priv_lock, flags); 129 130 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) { 131 list_move_tail(&unlink->list, &vdev->unlink_rx); 132 spin_unlock_irqrestore(&vdev->priv_lock, flags); 133 return unlink; 134 } 135 136 spin_unlock_irqrestore(&vdev->priv_lock, flags); 137 138 return NULL; 139 } 140 141 static int vhci_send_cmd_unlink(struct vhci_device *vdev) 142 { 143 struct vhci_unlink *unlink = NULL; 144 145 struct msghdr msg; 146 struct kvec iov[3]; 147 size_t txsize; 148 149 size_t total_size = 0; 150 151 while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) { 152 int ret; 153 struct usbip_header pdu_header; 154 155 txsize = 0; 156 memset(&pdu_header, 0, sizeof(pdu_header)); 157 memset(&msg, 0, sizeof(msg)); 158 memset(&iov, 0, sizeof(iov)); 159 160 usbip_dbg_vhci_tx("setup cmd unlink, %lu\n", unlink->seqnum); 161 162 /* 1. setup usbip_header */ 163 pdu_header.base.command = USBIP_CMD_UNLINK; 164 pdu_header.base.seqnum = unlink->seqnum; 165 pdu_header.base.devid = vdev->devid; 166 pdu_header.base.ep = 0; 167 pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum; 168 169 usbip_header_correct_endian(&pdu_header, 1); 170 171 iov[0].iov_base = &pdu_header; 172 iov[0].iov_len = sizeof(pdu_header); 173 txsize += sizeof(pdu_header); 174 175 ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize); 176 if (ret != txsize) { 177 pr_err("sendmsg failed!, ret=%d for %zd\n", ret, 178 txsize); 179 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP); 180 return -1; 181 } 182 183 usbip_dbg_vhci_tx("send txdata\n"); 184 185 total_size += txsize; 186 } 187 188 return total_size; 189 } 190 191 int vhci_tx_loop(void *data) 192 { 193 struct usbip_device *ud = data; 194 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); 195 196 while (!kthread_should_stop()) { 197 if (vhci_send_cmd_submit(vdev) < 0) 198 break; 199 200 if (vhci_send_cmd_unlink(vdev) < 0) 201 break; 202 203 wait_event_interruptible(vdev->waitq_tx, 204 (!list_empty(&vdev->priv_tx) || 205 !list_empty(&vdev->unlink_tx) || 206 kthread_should_stop())); 207 208 usbip_dbg_vhci_tx("pending urbs ?, now wake up\n"); 209 } 210 211 return 0; 212 } 213