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)
103 /* restore the padding in iso packets */
104 if (usbip_pad_iso(ud, urb) < 0)
107 if (usbip_dbg_flag_vhci_rx)
111 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
113 spin_lock(&the_controller->lock);
114 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
115 spin_unlock(&the_controller->lock);
117 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
120 usbip_dbg_vhci_rx("Leave\n");
126 static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
127 struct usbip_header *pdu)
129 struct vhci_unlink *unlink, *tmp;
131 spin_lock(&vdev->priv_lock);
133 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
134 usbip_uinfo("unlink->seqnum %lu\n", unlink->seqnum);
135 if (unlink->seqnum == pdu->base.seqnum) {
136 usbip_dbg_vhci_rx("found pending unlink, %lu\n",
138 list_del(&unlink->list);
140 spin_unlock(&vdev->priv_lock);
145 spin_unlock(&vdev->priv_lock);
151 static void vhci_recv_ret_unlink(struct vhci_device *vdev,
152 struct usbip_header *pdu)
154 struct vhci_unlink *unlink;
157 usbip_dump_header(pdu);
159 unlink = dequeue_pending_unlink(vdev, pdu);
161 usbip_uinfo("cannot find the pending unlink %u\n",
166 spin_lock(&vdev->priv_lock);
168 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
170 spin_unlock(&vdev->priv_lock);
174 * I get the result of a unlink request. But, it seems that I
175 * already received the result of its submit result and gave
178 usbip_uinfo("the urb (seqnum %d) was already given backed\n",
181 usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
183 /* If unlink is succeed, status is -ECONNRESET */
184 urb->status = pdu->u.ret_unlink.status;
185 usbip_uinfo("%d\n", urb->status);
187 spin_lock(&the_controller->lock);
188 usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
189 spin_unlock(&the_controller->lock);
191 usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
200 static int vhci_priv_tx_empty(struct vhci_device *vdev)
204 spin_lock(&vdev->priv_lock);
206 empty = list_empty(&vdev->priv_rx);
208 spin_unlock(&vdev->priv_lock);
214 static void vhci_rx_pdu(struct usbip_device *ud)
217 struct usbip_header pdu;
218 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
221 usbip_dbg_vhci_rx("Enter\n");
223 memset(&pdu, 0, sizeof(pdu));
225 /* 1. receive a pdu header */
226 ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
228 if (ret == -ECONNRESET)
229 usbip_uinfo("connection reset by peer\n");
230 else if (ret == -EAGAIN) {
231 /* ignore if connection was idle */
232 if (vhci_priv_tx_empty(vdev))
234 usbip_uinfo("connection timed out with pending urbs\n");
235 } else if (ret != -ERESTARTSYS)
236 usbip_uinfo("xmit failed %d\n", ret);
238 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
242 usbip_uinfo("connection closed");
243 usbip_event_add(ud, VDEV_EVENT_DOWN);
246 if (ret != sizeof(pdu)) {
247 usbip_uerr("received pdu size is %d, should be %d\n",
248 ret, (unsigned int)sizeof(pdu));
249 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
253 usbip_header_correct_endian(&pdu, 0);
255 if (usbip_dbg_flag_vhci_rx)
256 usbip_dump_header(&pdu);
258 switch (pdu.base.command) {
259 case USBIP_RET_SUBMIT:
260 vhci_recv_ret_submit(vdev, &pdu);
262 case USBIP_RET_UNLINK:
263 vhci_recv_ret_unlink(vdev, &pdu);
267 usbip_uerr("unknown pdu %u\n", pdu.base.command);
268 usbip_dump_header(&pdu);
269 usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
274 /*-------------------------------------------------------------------------*/
276 int vhci_rx_loop(void *data)
278 struct usbip_device *ud = data;
281 while (!kthread_should_stop()) {
282 if (usbip_event_happened(ud))