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/kthread.h>
21 #include <linux/socket.h>
22 #include <linux/scatterlist.h>
24 #include "usbip_common.h"
27 /* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
28 void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
31 struct stub_unlink *unlink;
33 unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
35 usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
39 unlink->seqnum = seqnum;
40 unlink->status = status;
42 list_add_tail(&unlink->list, &sdev->unlink_tx);
46 * stub_complete - completion handler of a usbip urb
47 * @urb: pointer to the urb completed
49 * When a urb has completed, the USB core driver calls this function mostly in
50 * the interrupt context. To return the result of a urb, the completed urb is
51 * linked to the pending list of returning.
54 void stub_complete(struct urb *urb)
56 struct stub_priv *priv = (struct stub_priv *) urb->context;
57 struct stub_device *sdev = priv->sdev;
60 usbip_dbg_stub_tx("complete! status %d\n", urb->status);
62 switch (urb->status) {
67 dev_info(&urb->dev->dev,
68 "stopped by a call to usb_kill_urb() because of cleaning up a virtual connection\n");
71 dev_info(&urb->dev->dev,
72 "unlinked by a call to usb_unlink_urb()\n");
75 dev_info(&urb->dev->dev, "endpoint %d is stalled\n",
76 usb_pipeendpoint(urb->pipe));
79 dev_info(&urb->dev->dev, "device removed?\n");
82 dev_info(&urb->dev->dev,
83 "urb completion with non-zero status %d\n",
89 * If the server breaks single SG request into the several URBs, the
90 * URBs must be reassembled before sending completed URB to the vhci.
91 * Don't wake up the tx thread until all the URBs are completed.
94 priv->completed_urbs++;
96 /* Only save the first error status */
97 if (urb->status && !priv->urb_status)
98 priv->urb_status = urb->status;
100 if (priv->completed_urbs < priv->num_urbs)
104 /* link a urb to the queue of tx. */
105 spin_lock_irqsave(&sdev->priv_lock, flags);
106 if (sdev->ud.tcp_socket == NULL) {
107 usbip_dbg_stub_tx("ignore urb for closed connection\n");
108 /* It will be freed in stub_device_cleanup_urbs(). */
109 } else if (priv->unlinking) {
110 stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
111 stub_free_priv_and_urb(priv);
113 list_move_tail(&priv->list, &sdev->priv_tx);
115 spin_unlock_irqrestore(&sdev->priv_lock, flags);
117 /* wake up tx_thread */
118 wake_up(&sdev->tx_waitq);
121 static inline void setup_base_pdu(struct usbip_header_basic *base,
122 __u32 command, __u32 seqnum)
124 base->command = command;
125 base->seqnum = seqnum;
131 static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
133 struct stub_priv *priv = (struct stub_priv *) urb->context;
135 setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
136 usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
139 static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
140 struct stub_unlink *unlink)
142 setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
143 rpdu->u.ret_unlink.status = unlink->status;
146 static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
149 struct stub_priv *priv, *tmp;
151 spin_lock_irqsave(&sdev->priv_lock, flags);
153 list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
154 list_move_tail(&priv->list, &sdev->priv_free);
155 spin_unlock_irqrestore(&sdev->priv_lock, flags);
159 spin_unlock_irqrestore(&sdev->priv_lock, flags);
164 static int stub_send_ret_submit(struct stub_device *sdev)
167 struct stub_priv *priv, *tmp;
172 size_t total_size = 0;
174 while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
175 struct urb *urb = priv->urbs[0];
176 struct usbip_header pdu_header;
177 struct usbip_iso_packet_descriptor *iso_buffer = NULL;
178 struct kvec *iov = NULL;
179 struct scatterlist *sg;
180 u32 actual_length = 0;
186 memset(&pdu_header, 0, sizeof(pdu_header));
187 memset(&msg, 0, sizeof(msg));
189 if (urb->actual_length > 0 && !urb->transfer_buffer &&
191 dev_err(&sdev->udev->dev,
192 "urb: actual_length %d transfer_buffer null\n",
197 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS)
198 iovnum = 2 + urb->number_of_packets;
199 else if (usb_pipein(urb->pipe) && urb->actual_length > 0 &&
201 iovnum = 1 + urb->num_sgs;
202 else if (usb_pipein(urb->pipe) && priv->sgl)
203 iovnum = 1 + priv->num_urbs;
207 iov = kcalloc(iovnum, sizeof(struct kvec), GFP_KERNEL);
210 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_MALLOC);
216 /* 1. setup usbip_header */
217 setup_ret_submit_pdu(&pdu_header, urb);
218 usbip_dbg_stub_tx("setup txdata seqnum: %d\n",
219 pdu_header.base.seqnum);
222 for (i = 0; i < priv->num_urbs; i++)
223 actual_length += priv->urbs[i]->actual_length;
225 pdu_header.u.ret_submit.status = priv->urb_status;
226 pdu_header.u.ret_submit.actual_length = actual_length;
229 usbip_header_correct_endian(&pdu_header, 1);
231 iov[iovnum].iov_base = &pdu_header;
232 iov[iovnum].iov_len = sizeof(pdu_header);
234 txsize += sizeof(pdu_header);
236 /* 2. setup transfer buffer */
237 if (usb_pipein(urb->pipe) && priv->sgl) {
238 /* If the server split a single SG request into several
239 * URBs because the server's HCD doesn't support SG,
240 * reassemble the split URB buffers into a single
243 for (i = 0; i < priv->num_urbs; i++) {
244 iov[iovnum].iov_base =
245 priv->urbs[i]->transfer_buffer;
246 iov[iovnum].iov_len =
247 priv->urbs[i]->actual_length;
250 txsize += actual_length;
251 } else if (usb_pipein(urb->pipe) &&
252 usb_pipetype(urb->pipe) != PIPE_ISOCHRONOUS &&
253 urb->actual_length > 0) {
255 unsigned int copy = urb->actual_length;
258 for_each_sg(urb->sg, sg, urb->num_sgs, i) {
262 if (copy < sg->length)
267 iov[iovnum].iov_base = sg_virt(sg);
268 iov[iovnum].iov_len = size;
274 iov[iovnum].iov_base = urb->transfer_buffer;
275 iov[iovnum].iov_len = urb->actual_length;
278 txsize += urb->actual_length;
279 } else if (usb_pipein(urb->pipe) &&
280 usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
282 * For isochronous packets: actual length is the sum of
283 * the actual length of the individual, packets, but as
284 * the packet offsets are not changed there will be
285 * padding between the packets. To optimally use the
286 * bandwidth the padding is not transmitted.
291 for (i = 0; i < urb->number_of_packets; i++) {
292 iov[iovnum].iov_base = urb->transfer_buffer +
293 urb->iso_frame_desc[i].offset;
294 iov[iovnum].iov_len =
295 urb->iso_frame_desc[i].actual_length;
297 txsize += urb->iso_frame_desc[i].actual_length;
300 if (txsize != sizeof(pdu_header) + urb->actual_length) {
301 dev_err(&sdev->udev->dev,
302 "actual length of urb %d does not match iso packet sizes %zu\n",
304 txsize-sizeof(pdu_header));
306 usbip_event_add(&sdev->ud,
307 SDEV_EVENT_ERROR_TCP);
312 /* 3. setup iso_packet_descriptor */
313 if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
316 iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
318 usbip_event_add(&sdev->ud,
319 SDEV_EVENT_ERROR_MALLOC);
324 iov[iovnum].iov_base = iso_buffer;
325 iov[iovnum].iov_len = len;
330 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg,
331 iov, iovnum, txsize);
333 dev_err(&sdev->udev->dev,
334 "sendmsg failed!, retval %d for %zd\n",
338 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
345 total_size += txsize;
348 spin_lock_irqsave(&sdev->priv_lock, flags);
349 list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
350 stub_free_priv_and_urb(priv);
352 spin_unlock_irqrestore(&sdev->priv_lock, flags);
357 static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
360 struct stub_unlink *unlink, *tmp;
362 spin_lock_irqsave(&sdev->priv_lock, flags);
364 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
365 list_move_tail(&unlink->list, &sdev->unlink_free);
366 spin_unlock_irqrestore(&sdev->priv_lock, flags);
370 spin_unlock_irqrestore(&sdev->priv_lock, flags);
375 static int stub_send_ret_unlink(struct stub_device *sdev)
378 struct stub_unlink *unlink, *tmp;
384 size_t total_size = 0;
386 while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
388 struct usbip_header pdu_header;
391 memset(&pdu_header, 0, sizeof(pdu_header));
392 memset(&msg, 0, sizeof(msg));
393 memset(&iov, 0, sizeof(iov));
395 usbip_dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
397 /* 1. setup usbip_header */
398 setup_ret_unlink_pdu(&pdu_header, unlink);
399 usbip_header_correct_endian(&pdu_header, 1);
401 iov[0].iov_base = &pdu_header;
402 iov[0].iov_len = sizeof(pdu_header);
403 txsize += sizeof(pdu_header);
405 ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
408 dev_err(&sdev->udev->dev,
409 "sendmsg failed!, retval %d for %zd\n",
411 usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
415 usbip_dbg_stub_tx("send txdata\n");
416 total_size += txsize;
419 spin_lock_irqsave(&sdev->priv_lock, flags);
421 list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
422 list_del(&unlink->list);
426 spin_unlock_irqrestore(&sdev->priv_lock, flags);
431 int stub_tx_loop(void *data)
433 struct usbip_device *ud = data;
434 struct stub_device *sdev = container_of(ud, struct stub_device, ud);
436 while (!kthread_should_stop()) {
437 if (usbip_event_happened(ud))
441 * send_ret_submit comes earlier than send_ret_unlink. stub_rx
442 * looks at only priv_init queue. If the completion of a URB is
443 * earlier than the receive of CMD_UNLINK, priv is moved to
444 * priv_tx queue and stub_rx does not find the target priv. In
445 * this case, vhci_rx receives the result of the submit request
446 * and then receives the result of the unlink request. The
447 * result of the submit is given back to the usbcore as the
448 * completion of the unlink request. The request of the
449 * unlink is ignored. This is ok because a driver who calls
450 * usb_unlink_urb() understands the unlink was too late by
451 * getting the status of the given-backed URB which has the
452 * status of usb_submit_urb().
454 if (stub_send_ret_submit(sdev) < 0)
457 if (stub_send_ret_unlink(sdev) < 0)
460 wait_event_interruptible(sdev->tx_waitq,
461 (!list_empty(&sdev->priv_tx) ||
462 !list_empty(&sdev->unlink_tx) ||
463 kthread_should_stop()));