vhci_rx.c 6.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 * Copyright (C) 2003-2008 Takahiro Hirofuchi
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
 * USA.
 */

20
#include <linux/kthread.h>
21
#include <linux/slab.h>
22

23 24 25
#include "usbip_common.h"
#include "vhci.h"

26
/* get URB from transmitted urb queue. caller must hold vdev->priv_lock */
27
struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev, __u32 seqnum)
28 29 30 31 32 33
{
	struct vhci_priv *priv, *tmp;
	struct urb *urb = NULL;
	int status;

	list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
34 35
		if (priv->seqnum != seqnum)
			continue;
36

37 38
		urb = priv->urb;
		status = urb->status;
39

40 41
		usbip_dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
				urb, priv, seqnum);
42

43 44 45 46 47 48 49 50 51 52
		switch (status) {
		case -ENOENT:
			/* fall through */
		case -ECONNRESET:
			dev_info(&urb->dev->dev,
				 "urb %p was unlinked %ssynchronuously.\n", urb,
				 status == -ENOENT ? "" : "a");
			break;
		case -EINPROGRESS:
			/* no info output */
53
			break;
54 55 56 57
		default:
			dev_info(&urb->dev->dev,
				 "urb %p may be in a error, status %d\n", urb,
				 status);
58
		}
59 60 61 62 63 64

		list_del(&priv->list);
		kfree(priv);
		urb->hcpriv = NULL;

		break;
65 66 67 68 69 70
	}

	return urb;
}

static void vhci_recv_ret_submit(struct vhci_device *vdev,
71
				 struct usbip_header *pdu)
72
{
73 74
	struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
	struct vhci *vhci = vhci_hcd->vhci;
75 76
	struct usbip_device *ud = &vdev->ud;
	struct urb *urb;
77
	unsigned long flags;
78

79
	spin_lock_irqsave(&vdev->priv_lock, flags);
80
	urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
81
	spin_unlock_irqrestore(&vdev->priv_lock, flags);
82 83

	if (!urb) {
84 85
		pr_err("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
		pr_info("max seqnum %d\n",
86
			atomic_read(&vhci_hcd->seqnum));
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
		return;
	}

	/* unpack the pdu to a urb */
	usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);

	/* recv transfer buffer */
	if (usbip_recv_xbuff(ud, urb) < 0)
		return;

	/* recv iso_packet_descriptor */
	if (usbip_recv_iso(ud, urb) < 0)
		return;

102
	/* restore the padding in iso packets */
103
	usbip_pad_iso(ud, urb);
104

B
Brian G. Merrell 已提交
105
	if (usbip_dbg_flag_vhci_rx)
106 107
		usbip_dump_urb(urb);

B
Brian G. Merrell 已提交
108
	usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
109

110
	spin_lock_irqsave(&vhci->lock, flags);
111
	usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
112
	spin_unlock_irqrestore(&vhci->lock, flags);
113

114
	usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
115

B
Brian G. Merrell 已提交
116
	usbip_dbg_vhci_rx("Leave\n");
117 118 119
}

static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
120
						  struct usbip_header *pdu)
121 122
{
	struct vhci_unlink *unlink, *tmp;
123
	unsigned long flags;
124

125
	spin_lock_irqsave(&vdev->priv_lock, flags);
126 127

	list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
128
		pr_info("unlink->seqnum %lu\n", unlink->seqnum);
129
		if (unlink->seqnum == pdu->base.seqnum) {
B
Brian G. Merrell 已提交
130
			usbip_dbg_vhci_rx("found pending unlink, %lu\n",
131
					  unlink->seqnum);
132 133
			list_del(&unlink->list);

134
			spin_unlock_irqrestore(&vdev->priv_lock, flags);
135 136 137 138
			return unlink;
		}
	}

139
	spin_unlock_irqrestore(&vdev->priv_lock, flags);
140 141 142 143 144

	return NULL;
}

static void vhci_recv_ret_unlink(struct vhci_device *vdev,
145
				 struct usbip_header *pdu)
146
{
147 148
	struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
	struct vhci *vhci = vhci_hcd->vhci;
149 150
	struct vhci_unlink *unlink;
	struct urb *urb;
151
	unsigned long flags;
152 153 154 155 156

	usbip_dump_header(pdu);

	unlink = dequeue_pending_unlink(vdev, pdu);
	if (!unlink) {
157 158
		pr_info("cannot find the pending unlink %u\n",
			pdu->base.seqnum);
159 160 161
		return;
	}

162
	spin_lock_irqsave(&vdev->priv_lock, flags);
163
	urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
164
	spin_unlock_irqrestore(&vdev->priv_lock, flags);
165

166 167 168 169 170 171
	if (!urb) {
		/*
		 * I get the result of a unlink request. But, it seems that I
		 * already received the result of its submit result and gave
		 * back the URB.
		 */
172
		pr_info("the urb (seqnum %d) was already given back\n",
173
			pdu->base.seqnum);
174
	} else {
B
Brian G. Merrell 已提交
175
		usbip_dbg_vhci_rx("now giveback urb %p\n", urb);
176

177
		/* If unlink is successful, status is -ECONNRESET */
178
		urb->status = pdu->u.ret_unlink.status;
179
		pr_info("urb->status %d\n", urb->status);
180

181
		spin_lock_irqsave(&vhci->lock, flags);
182
		usb_hcd_unlink_urb_from_ep(vhci_hcd_to_hcd(vhci_hcd), urb);
183
		spin_unlock_irqrestore(&vhci->lock, flags);
184

185
		usb_hcd_giveback_urb(vhci_hcd_to_hcd(vhci_hcd), urb, urb->status);
186 187 188 189 190
	}

	kfree(unlink);
}

191 192 193
static int vhci_priv_tx_empty(struct vhci_device *vdev)
{
	int empty = 0;
194
	unsigned long flags;
195

196
	spin_lock_irqsave(&vdev->priv_lock, flags);
197
	empty = list_empty(&vdev->priv_rx);
198
	spin_unlock_irqrestore(&vdev->priv_lock, flags);
199 200 201 202

	return empty;
}

203 204 205 206 207 208 209
/* recv a pdu */
static void vhci_rx_pdu(struct usbip_device *ud)
{
	int ret;
	struct usbip_header pdu;
	struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);

B
Brian G. Merrell 已提交
210
	usbip_dbg_vhci_rx("Enter\n");
211 212 213

	memset(&pdu, 0, sizeof(pdu));

214
	/* receive a pdu header */
215
	ret = usbip_recv(ud->tcp_socket, &pdu, sizeof(pdu));
216 217
	if (ret < 0) {
		if (ret == -ECONNRESET)
218
			pr_info("connection reset by peer\n");
219 220 221 222
		else if (ret == -EAGAIN) {
			/* ignore if connection was idle */
			if (vhci_priv_tx_empty(vdev))
				return;
223
			pr_info("connection timed out with pending urbs\n");
224
		} else if (ret != -ERESTARTSYS)
225
			pr_info("xmit failed %d\n", ret);
226

227 228 229 230
		usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
		return;
	}
	if (ret == 0) {
231
		pr_info("connection closed");
232 233 234
		usbip_event_add(ud, VDEV_EVENT_DOWN);
		return;
	}
235
	if (ret != sizeof(pdu)) {
236 237
		pr_err("received pdu size is %d, should be %d\n", ret,
		       (unsigned int)sizeof(pdu));
238 239 240 241 242 243
		usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
		return;
	}

	usbip_header_correct_endian(&pdu, 0);

B
Brian G. Merrell 已提交
244
	if (usbip_dbg_flag_vhci_rx)
245 246 247 248 249 250 251 252 253 254
		usbip_dump_header(&pdu);

	switch (pdu.base.command) {
	case USBIP_RET_SUBMIT:
		vhci_recv_ret_submit(vdev, &pdu);
		break;
	case USBIP_RET_UNLINK:
		vhci_recv_ret_unlink(vdev, &pdu);
		break;
	default:
255
		/* NOT REACHED */
256
		pr_err("unknown pdu %u\n", pdu.base.command);
257 258
		usbip_dump_header(&pdu);
		usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
259
		break;
260 261 262
	}
}

263
int vhci_rx_loop(void *data)
264
{
265
	struct usbip_device *ud = data;
266

267
	while (!kthread_should_stop()) {
B
Brian G. Merrell 已提交
268
		if (usbip_event_happened(ud))
269 270 271 272 273
			break;

		vhci_rx_pdu(ud);
	}

274 275
	return 0;
}