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 | |
Max Vozeler | b92a5e2 | 2011-01-12 15:02:01 +0200 | [diff] [blame] | 26 | /* get URB from transmitted urb queue. caller must hold vdev->priv_lock */ |
matt mooney | 5ef6ace | 2011-05-06 03:47:51 -0700 | [diff] [blame] | 27 | struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 28 | { |
| 29 | struct vhci_priv *priv, *tmp; |
| 30 | struct urb *urb = NULL; |
| 31 | int status; |
| 32 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 33 | list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) { |
Stefan Reif | 2663d79 | 2013-04-04 16:03:04 +0200 | [diff] [blame] | 34 | if (priv->seqnum != seqnum) |
| 35 | continue; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 36 | |
Stefan Reif | 2663d79 | 2013-04-04 16:03:04 +0200 | [diff] [blame] | 37 | urb = priv->urb; |
| 38 | status = urb->status; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 39 | |
Stefan Reif | 2663d79 | 2013-04-04 16:03:04 +0200 | [diff] [blame] | 40 | usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n", |
| 41 | urb, priv, seqnum); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 42 | |
Stefan Reif | 2663d79 | 2013-04-04 16:03:04 +0200 | [diff] [blame] | 43 | switch (status) { |
| 44 | case -ENOENT: |
| 45 | /* fall through */ |
| 46 | case -ECONNRESET: |
| 47 | dev_info(&urb->dev->dev, |
| 48 | "urb %p was unlinked %ssynchronuously.\n", urb, |
| 49 | status == -ENOENT ? "" : "a"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 50 | break; |
Stefan Reif | 2663d79 | 2013-04-04 16:03:04 +0200 | [diff] [blame] | 51 | case -EINPROGRESS: |
| 52 | /* no info output */ |
| 53 | break; |
| 54 | default: |
| 55 | dev_info(&urb->dev->dev, |
| 56 | "urb %p may be in a error, status %d\n", urb, |
| 57 | status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 58 | } |
Stefan Reif | 2663d79 | 2013-04-04 16:03:04 +0200 | [diff] [blame] | 59 | |
| 60 | list_del(&priv->list); |
| 61 | kfree(priv); |
| 62 | urb->hcpriv = NULL; |
| 63 | |
| 64 | break; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 65 | } |
| 66 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 67 | return urb; |
| 68 | } |
| 69 | |
| 70 | static void vhci_recv_ret_submit(struct vhci_device *vdev, |
matt mooney | 5ef6ace | 2011-05-06 03:47:51 -0700 | [diff] [blame] | 71 | struct usbip_header *pdu) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 72 | { |
| 73 | struct usbip_device *ud = &vdev->ud; |
| 74 | struct urb *urb; |
| 75 | |
Max Vozeler | b92a5e2 | 2011-01-12 15:02:01 +0200 | [diff] [blame] | 76 | spin_lock(&vdev->priv_lock); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 77 | urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum); |
Max Vozeler | b92a5e2 | 2011-01-12 15:02:01 +0200 | [diff] [blame] | 78 | spin_unlock(&vdev->priv_lock); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 79 | |
| 80 | if (!urb) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 81 | pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum); |
| 82 | pr_info("max seqnum %d\n", |
| 83 | atomic_read(&the_controller->seqnum)); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 84 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); |
| 85 | return; |
| 86 | } |
| 87 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 88 | /* unpack the pdu to a urb */ |
| 89 | usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0); |
| 90 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 91 | /* recv transfer buffer */ |
| 92 | if (usbip_recv_xbuff(ud, urb) < 0) |
| 93 | return; |
| 94 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 95 | /* recv iso_packet_descriptor */ |
| 96 | if (usbip_recv_iso(ud, urb) < 0) |
| 97 | return; |
| 98 | |
Arjan Mels | 28276a2 | 2011-04-05 20:26:59 +0200 | [diff] [blame] | 99 | /* restore the padding in iso packets */ |
Bart Westgeest | ac2b41a | 2012-01-23 10:55:46 -0500 | [diff] [blame] | 100 | usbip_pad_iso(ud, urb); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 101 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 102 | if (usbip_dbg_flag_vhci_rx) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 103 | usbip_dump_urb(urb); |
| 104 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 105 | usbip_dbg_vhci_rx("now giveback urb %p\n", urb); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 106 | |
Harvey Yang | 50b66b5 | 2013-01-22 13:31:31 +0800 | [diff] [blame] | 107 | spin_lock(&the_controller->lock); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 108 | usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); |
Harvey Yang | 50b66b5 | 2013-01-22 13:31:31 +0800 | [diff] [blame] | 109 | spin_unlock(&the_controller->lock); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 110 | |
| 111 | usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status); |
| 112 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 113 | usbip_dbg_vhci_rx("Leave\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 114 | } |
| 115 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 116 | static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev, |
matt mooney | 5ef6ace | 2011-05-06 03:47:51 -0700 | [diff] [blame] | 117 | struct usbip_header *pdu) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 118 | { |
| 119 | struct vhci_unlink *unlink, *tmp; |
| 120 | |
| 121 | spin_lock(&vdev->priv_lock); |
| 122 | |
| 123 | list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 124 | pr_info("unlink->seqnum %lu\n", unlink->seqnum); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 125 | if (unlink->seqnum == pdu->base.seqnum) { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 126 | usbip_dbg_vhci_rx("found pending unlink, %lu\n", |
matt mooney | 5ef6ace | 2011-05-06 03:47:51 -0700 | [diff] [blame] | 127 | unlink->seqnum); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 128 | list_del(&unlink->list); |
| 129 | |
| 130 | spin_unlock(&vdev->priv_lock); |
| 131 | return unlink; |
| 132 | } |
| 133 | } |
| 134 | |
| 135 | spin_unlock(&vdev->priv_lock); |
| 136 | |
| 137 | return NULL; |
| 138 | } |
| 139 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 140 | static void vhci_recv_ret_unlink(struct vhci_device *vdev, |
matt mooney | 5ef6ace | 2011-05-06 03:47:51 -0700 | [diff] [blame] | 141 | struct usbip_header *pdu) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 142 | { |
| 143 | struct vhci_unlink *unlink; |
| 144 | struct urb *urb; |
| 145 | |
| 146 | usbip_dump_header(pdu); |
| 147 | |
| 148 | unlink = dequeue_pending_unlink(vdev, pdu); |
| 149 | if (!unlink) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 150 | pr_info("cannot find the pending unlink %u\n", |
| 151 | pdu->base.seqnum); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 152 | return; |
| 153 | } |
| 154 | |
Max Vozeler | b92a5e2 | 2011-01-12 15:02:01 +0200 | [diff] [blame] | 155 | spin_lock(&vdev->priv_lock); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 156 | urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum); |
Max Vozeler | b92a5e2 | 2011-01-12 15:02:01 +0200 | [diff] [blame] | 157 | spin_unlock(&vdev->priv_lock); |
| 158 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 159 | if (!urb) { |
| 160 | /* |
| 161 | * I get the result of a unlink request. But, it seems that I |
| 162 | * already received the result of its submit result and gave |
| 163 | * back the URB. |
| 164 | */ |
Christopher Harvey | 3567f97 | 2012-03-22 16:57:50 -0400 | [diff] [blame] | 165 | pr_info("the urb (seqnum %d) was already given back\n", |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 166 | pdu->base.seqnum); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 167 | } else { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 168 | usbip_dbg_vhci_rx("now giveback urb %p\n", urb); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 169 | |
Bart Westgeest | c7f0089 | 2012-10-10 13:34:27 -0400 | [diff] [blame] | 170 | /* If unlink is successful, status is -ECONNRESET */ |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 171 | urb->status = pdu->u.ret_unlink.status; |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 172 | pr_info("urb->status %d\n", urb->status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 173 | |
Harvey Yang | 50b66b5 | 2013-01-22 13:31:31 +0800 | [diff] [blame] | 174 | spin_lock(&the_controller->lock); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 175 | usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb); |
Harvey Yang | 50b66b5 | 2013-01-22 13:31:31 +0800 | [diff] [blame] | 176 | spin_unlock(&the_controller->lock); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 177 | |
| 178 | usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, |
matt mooney | 5ef6ace | 2011-05-06 03:47:51 -0700 | [diff] [blame] | 179 | urb->status); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | kfree(unlink); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 183 | } |
| 184 | |
Max Vozeler | bd65f62 | 2011-01-12 15:02:04 +0200 | [diff] [blame] | 185 | static int vhci_priv_tx_empty(struct vhci_device *vdev) |
| 186 | { |
| 187 | int empty = 0; |
| 188 | |
| 189 | spin_lock(&vdev->priv_lock); |
Max Vozeler | bd65f62 | 2011-01-12 15:02:04 +0200 | [diff] [blame] | 190 | empty = list_empty(&vdev->priv_rx); |
Max Vozeler | bd65f62 | 2011-01-12 15:02:04 +0200 | [diff] [blame] | 191 | spin_unlock(&vdev->priv_lock); |
| 192 | |
| 193 | return empty; |
| 194 | } |
| 195 | |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 196 | /* recv a pdu */ |
| 197 | static void vhci_rx_pdu(struct usbip_device *ud) |
| 198 | { |
| 199 | int ret; |
| 200 | struct usbip_header pdu; |
| 201 | struct vhci_device *vdev = container_of(ud, struct vhci_device, ud); |
| 202 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 203 | usbip_dbg_vhci_rx("Enter\n"); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 204 | |
| 205 | memset(&pdu, 0, sizeof(pdu)); |
| 206 | |
Kurt Kanzenbach | 771788074 | 2013-04-04 16:03:05 +0200 | [diff] [blame] | 207 | /* receive a pdu header */ |
Bart Westgeest | 5a08c52 | 2011-12-19 17:44:11 -0500 | [diff] [blame] | 208 | ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu)); |
Max Vozeler | 7e249c8 | 2011-01-12 15:02:03 +0200 | [diff] [blame] | 209 | if (ret < 0) { |
| 210 | if (ret == -ECONNRESET) |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 211 | pr_info("connection reset by peer\n"); |
Max Vozeler | bd65f62 | 2011-01-12 15:02:04 +0200 | [diff] [blame] | 212 | else if (ret == -EAGAIN) { |
| 213 | /* ignore if connection was idle */ |
| 214 | if (vhci_priv_tx_empty(vdev)) |
| 215 | return; |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 216 | pr_info("connection timed out with pending urbs\n"); |
Max Vozeler | bd65f62 | 2011-01-12 15:02:04 +0200 | [diff] [blame] | 217 | } else if (ret != -ERESTARTSYS) |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 218 | pr_info("xmit failed %d\n", ret); |
Max Vozeler | bd65f62 | 2011-01-12 15:02:04 +0200 | [diff] [blame] | 219 | |
Max Vozeler | 7e249c8 | 2011-01-12 15:02:03 +0200 | [diff] [blame] | 220 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); |
| 221 | return; |
| 222 | } |
| 223 | if (ret == 0) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 224 | pr_info("connection closed"); |
Max Vozeler | 7e249c8 | 2011-01-12 15:02:03 +0200 | [diff] [blame] | 225 | usbip_event_add(ud, VDEV_EVENT_DOWN); |
| 226 | return; |
| 227 | } |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 228 | if (ret != sizeof(pdu)) { |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 229 | pr_err("received pdu size is %d, should be %d\n", ret, |
| 230 | (unsigned int)sizeof(pdu)); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 231 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); |
| 232 | return; |
| 233 | } |
| 234 | |
| 235 | usbip_header_correct_endian(&pdu, 0); |
| 236 | |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 237 | if (usbip_dbg_flag_vhci_rx) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 238 | usbip_dump_header(&pdu); |
| 239 | |
| 240 | switch (pdu.base.command) { |
| 241 | case USBIP_RET_SUBMIT: |
| 242 | vhci_recv_ret_submit(vdev, &pdu); |
| 243 | break; |
| 244 | case USBIP_RET_UNLINK: |
| 245 | vhci_recv_ret_unlink(vdev, &pdu); |
| 246 | break; |
| 247 | default: |
matt mooney | 49aecef | 2011-05-06 03:47:54 -0700 | [diff] [blame] | 248 | /* NOT REACHED */ |
matt mooney | 1a4b6f6 | 2011-05-19 16:47:32 -0700 | [diff] [blame] | 249 | pr_err("unknown pdu %u\n", pdu.base.command); |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 250 | usbip_dump_header(&pdu); |
| 251 | usbip_event_add(ud, VDEV_EVENT_ERROR_TCP); |
matt mooney | 49aecef | 2011-05-06 03:47:54 -0700 | [diff] [blame] | 252 | break; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 256 | int vhci_rx_loop(void *data) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 257 | { |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 258 | struct usbip_device *ud = data; |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 259 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 260 | while (!kthread_should_stop()) { |
Brian G. Merrell | b8868e4 | 2009-07-21 00:46:13 -0600 | [diff] [blame] | 261 | if (usbip_event_happened(ud)) |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 262 | break; |
| 263 | |
| 264 | vhci_rx_pdu(ud); |
| 265 | } |
Takahiro Hirofuchi | 04679b3 | 2008-07-09 14:56:51 -0600 | [diff] [blame] | 266 | |
Arnd Bergmann | 9720b4b | 2011-03-02 00:13:05 +0100 | [diff] [blame] | 267 | return 0; |
| 268 | } |