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