2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
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.
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.
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,
20 #include <linux/slab.h>
21 #include <linux/kthread.h>
23 #include "usbip_common.h"
27 /* get URB from transmitted urb queue. caller must hold vdev->priv_lock */
28 struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev,
31 struct vhci_priv *priv, *tmp;
32 struct urb *urb = NULL;
35 list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
36 if (priv->seqnum == seqnum) {
40 usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
43 /* TODO: fix logic here to improve indent situtation */
44 if (status != -EINPROGRESS) {
45 if (status == -ENOENT ||
46 status == -ECONNRESET)
47 dev_info(&urb->dev->dev,
48 "urb %p was unlinked "
49 "%ssynchronuously.\n", urb,
50 status == -ENOENT ? "" : "a");
52 dev_info(&urb->dev->dev,
53 "urb %p may be in a error, "
54 "status %d\n", urb, status);
57 list_del(&priv->list);
68 static void vhci_recv_ret_submit(struct vhci_device *vdev,
69 struct usbip_header *pdu)
71 struct usbip_device *ud = &vdev->ud;
74 spin_lock(&vdev->priv_lock);
76 urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
78 spin_unlock(&vdev->priv_lock);
81 usbip_uerr("cannot find a urb of seqnum %u\n",
83 usbip_uinfo("max seqnum %d\n",
84 atomic_read(&the_controller->seqnum));
85 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
90 /* unpack the pdu to a urb */
91 usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
94 /* recv transfer buffer */
95 if (usbip_recv_xbuff(ud, urb) < 0)
99 /* recv iso_packet_descriptor */
100 if (usbip_recv_iso(ud, urb) < 0)
104 if (usbip_dbg_flag_vhci_rx)
108 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
110 spin_lock(&the_controller->lock);
111 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
112 spin_unlock(&the_controller->lock);
114 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
117 usbip_dbg_vhci_rx("Leave\n");
123 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
124 struct usbip_header *pdu)
126 struct vhci_unlink *unlink, *tmp;
128 spin_lock(&vdev->priv_lock);
130 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
131 usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum);
132 if (unlink->seqnum == pdu->base.seqnum) {
133 usbip_dbg_vhci_rx("found pending unlink, %lu\n",
135 list_del(&unlink->list);
137 spin_unlock(&vdev->priv_lock);
142 spin_unlock(&vdev->priv_lock);
148 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
149 struct usbip_header *pdu)
151 struct vhci_unlink *unlink;
154 usbip_dump_header(pdu);
156 unlink = dequeue_pending_unlink(vdev, pdu);
158 usbip_uinfo("cannot find the pending unlink %u\n",
163 spin_lock(&vdev->priv_lock);
165 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
167 spin_unlock(&vdev->priv_lock);
171 * I get the result of a unlink request. But, it seems that I
172 * already received the result of its submit result and gave
175 usbip_uinfo("the urb (seqnum %d) was already given backed\n",
178 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
180 /* If unlink is succeed, status is -ECONNRESET */
181 urb->status = pdu->u.ret_unlink.status;
182 usbip_uinfo("%d\n", urb->status);
184 spin_lock(&the_controller->lock);
185 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
186 spin_unlock(&the_controller->lock);
188 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
197 static int vhci_priv_tx_empty(struct vhci_device *vdev)
201 spin_lock(&vdev->priv_lock);
203 empty = list_empty(&vdev->priv_rx);
205 spin_unlock(&vdev->priv_lock);
211 static void vhci_rx_pdu(struct usbip_device *ud)
214 struct usbip_header pdu;
215 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
218 usbip_dbg_vhci_rx("Enter\n");
220 memset(&pdu, 0, sizeof(pdu));
222 /* 1. receive a pdu header */
223 ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
225 if (ret == -ECONNRESET)
226 usbip_uinfo("connection reset by peer\n");
227 else if (ret == -EAGAIN) {
228 /* ignore if connection was idle */
229 if (vhci_priv_tx_empty(vdev))
231 usbip_uinfo("connection timed out with pending urbs\n");
232 } else if (ret != -ERESTARTSYS)
233 usbip_uinfo("xmit failed %d\n", ret);
235 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
239 usbip_uinfo("connection closed");
240 usbip_event_add(ud, VDEV_EVENT_DOWN);
243 if (ret != sizeof(pdu)) {
244 usbip_uerr("received pdu size is %d, should be %d\n",
245 ret, (unsigned int)sizeof(pdu));
246 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
250 usbip_header_correct_endian(&pdu, 0);
252 if (usbip_dbg_flag_vhci_rx)
253 usbip_dump_header(&pdu);
255 switch (pdu.base.command) {
256 case USBIP_RET_SUBMIT:
257 vhci_recv_ret_submit(vdev, &pdu);
259 case USBIP_RET_UNLINK:
260 vhci_recv_ret_unlink(vdev, &pdu);
264 usbip_uerr("unknown pdu %u\n", pdu.base.command);
265 usbip_dump_header(&pdu);
266 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
271 /*-------------------------------------------------------------------------*/
273 int vhci_rx_loop(void *data)
275 struct usbip_device *ud = data;
278 while (!kthread_should_stop()) {
279 if (usbip_event_happened(ud))