1 From 4d7b5c7f8ad49b7f01fb8aed83c560ac43cfbda8 Mon Sep 17 00:00:00 2001
2 From: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
3 Date: Wed, 9 Jul 2008 14:56:51 -0600
4 Subject: [PATCH 13/23] Staging: USB/IP: add host driver
7 This adds the USB IP client driver
9 Brian Merrell cleaned up a lot of this code and submitted it for
10 inclusion. Greg also did a lot of cleanup.
12 Signed-off-by: Brian G. Merrell <bgmerrell@novell.com>
13 Cc: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
14 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
16 drivers/staging/usbip/Kconfig | 11 +
17 drivers/staging/usbip/Makefile | 3 +
18 drivers/staging/usbip/stub.h | 95 ++++++
19 drivers/staging/usbip/stub_dev.c | 483 +++++++++++++++++++++++++++++
20 drivers/staging/usbip/stub_main.c | 300 ++++++++++++++++++
21 drivers/staging/usbip/stub_rx.c | 615 +++++++++++++++++++++++++++++++++++++
22 drivers/staging/usbip/stub_tx.c | 371 ++++++++++++++++++++++
23 7 files changed, 1878 insertions(+), 0 deletions(-)
24 create mode 100644 drivers/staging/usbip/stub.h
25 create mode 100644 drivers/staging/usbip/stub_dev.c
26 create mode 100644 drivers/staging/usbip/stub_main.c
27 create mode 100644 drivers/staging/usbip/stub_rx.c
28 create mode 100644 drivers/staging/usbip/stub_tx.c
30 diff --git a/drivers/staging/usbip/Kconfig b/drivers/staging/usbip/Kconfig
31 index c4d68e1..7426235 100644
32 --- a/drivers/staging/usbip/Kconfig
33 +++ b/drivers/staging/usbip/Kconfig
34 @@ -23,3 +23,14 @@ config USB_IP_VHCI_HCD
36 To compile this driver as a module, choose M here: the
37 module will be called vhci_hcd.
40 + tristate "USB IP host driver"
41 + depends on USB_IP_COMMON
44 + This enables the USB IP device driver which will run on the
47 + To compile this driver as a module, choose M here: the
48 + module will be called usbip.
49 diff --git a/drivers/staging/usbip/Makefile b/drivers/staging/usbip/Makefile
50 index 6ef4c39..179f421 100644
51 --- a/drivers/staging/usbip/Makefile
52 +++ b/drivers/staging/usbip/Makefile
53 @@ -4,6 +4,9 @@ usbip_common_mod-objs := usbip_common.o usbip_event.o
54 obj-$(CONFIG_USB_IP_VHCI_HCD) += vhci-hcd.o
55 vhci-hcd-objs := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
57 +obj-$(CONFIG_USB_IP_HOST) += usbip.o
58 +usbip-objs := stub_dev.o stub_main.o stub_rx.o stub_tx.o
60 ifeq ($(CONFIG_USB_DEBUG),y)
61 EXTRA_CFLAGS += -DDEBUG
63 diff --git a/drivers/staging/usbip/stub.h b/drivers/staging/usbip/stub.h
65 index 0000000..f541a3a
67 +++ b/drivers/staging/usbip/stub.h
70 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
72 + * This is free software; you can redistribute it and/or modify
73 + * it under the terms of the GNU General Public License as published by
74 + * the Free Software Foundation; either version 2 of the License, or
75 + * (at your option) any later version.
77 + * This is distributed in the hope that it will be useful,
78 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
79 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 + * GNU General Public License for more details.
82 + * You should have received a copy of the GNU General Public License
83 + * along with this program; if not, write to the Free Software
84 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
88 +#include <linux/kernel.h>
89 +#include <linux/list.h>
90 +#include <linux/spinlock.h>
91 +#include <linux/slab.h>
92 +#include <linux/string.h>
93 +#include <linux/module.h>
94 +#include <linux/net.h>
97 + struct usb_interface *interface;
98 + struct list_head list;
100 + struct usbip_device ud;
104 + * stub_priv preserves private data of each urb.
105 + * It is allocated as stub_priv_cache and assigned to urb->context.
107 + * stub_priv is always linked to any one of 3 lists;
108 + * priv_init: linked to this until the comletion of a urb.
109 + * priv_tx : linked to this after the completion of a urb.
110 + * priv_free: linked to this after the sending of the result.
112 + * Any of these list operations should be locked by priv_lock.
114 + spinlock_t priv_lock;
115 + struct list_head priv_init;
116 + struct list_head priv_tx;
117 + struct list_head priv_free;
119 + /* see comments for unlinking in stub_rx.c */
120 + struct list_head unlink_tx;
121 + struct list_head unlink_free;
124 + wait_queue_head_t tx_waitq;
127 +/* private data into urb->priv */
129 + unsigned long seqnum;
130 + struct list_head list;
131 + struct stub_device *sdev;
137 +struct stub_unlink {
138 + unsigned long seqnum;
139 + struct list_head list;
144 +extern struct kmem_cache *stub_priv_cache;
147 +/*-------------------------------------------------------------------------*/
148 +/* prototype declarations */
151 +void stub_complete(struct urb *);
152 +void stub_tx_loop(struct usbip_task *);
155 +extern struct usb_driver stub_driver;
158 +void stub_rx_loop(struct usbip_task *);
159 +void stub_enqueue_ret_unlink(struct stub_device *, __u32, __u32);
162 +int match_busid(char *busid);
163 +void stub_device_cleanup_urbs(struct stub_device *sdev);
164 diff --git a/drivers/staging/usbip/stub_dev.c b/drivers/staging/usbip/stub_dev.c
166 index 0000000..ee455a0
168 +++ b/drivers/staging/usbip/stub_dev.c
171 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
173 + * This is free software; you can redistribute it and/or modify
174 + * it under the terms of the GNU General Public License as published by
175 + * the Free Software Foundation; either version 2 of the License, or
176 + * (at your option) any later version.
178 + * This is distributed in the hope that it will be useful,
179 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
180 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
181 + * GNU General Public License for more details.
183 + * You should have received a copy of the GNU General Public License
184 + * along with this program; if not, write to the Free Software
185 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
189 +#include "usbip_common.h"
194 +static int stub_probe(struct usb_interface *interface,
195 + const struct usb_device_id *id);
196 +static void stub_disconnect(struct usb_interface *interface);
200 + * Define device IDs here if you want to explicitly limit exportable devices.
201 + * In the most cases, wild card matching will be ok because driver binding can
202 + * be changed dynamically by a userland program.
204 +static struct usb_device_id stub_table[] = {
206 + /* just an example */
207 + { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
208 + { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
209 + { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
210 + { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
211 + { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
212 + { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
213 + { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
214 + { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
215 + { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
216 + { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
217 + { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
218 + { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
220 + /* magic for wild card */
221 + { .driver_info = 1 },
222 + { 0, } /* Terminating entry */
224 +MODULE_DEVICE_TABLE(usb, stub_table);
226 +struct usb_driver stub_driver = {
228 + .probe = stub_probe,
229 + .disconnect = stub_disconnect,
230 + .id_table = stub_table,
234 +/*-------------------------------------------------------------------------*/
236 +/* Define sysfs entries for a usbip-bound device */
240 + * usbip_status shows status of usbip as long as this driver is bound to the
243 +static ssize_t show_status(struct device *dev, struct device_attribute *attr,
246 + struct stub_device *sdev = dev_get_drvdata(dev);
250 + dev_err(dev, "sdev is null\n");
254 + spin_lock(&sdev->ud.lock);
255 + status = sdev->ud.status;
256 + spin_unlock(&sdev->ud.lock);
258 + return snprintf(buf, PAGE_SIZE, "%d\n", status);
260 +static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
263 + * usbip_sockfd gets a socket descriptor of an established TCP connection that
264 + * is used to transfer usbip requests by kernel threads. -1 is a magic number
265 + * by which usbip connection is finished.
267 +static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
268 + const char *buf, size_t count)
270 + struct stub_device *sdev = dev_get_drvdata(dev);
272 + struct socket *socket;
275 + dev_err(dev, "sdev is null\n");
279 + sscanf(buf, "%d", &sockfd);
281 + if (sockfd != -1) {
282 + dev_info(dev, "stub up\n");
284 + spin_lock(&sdev->ud.lock);
286 + if (sdev->ud.status != SDEV_ST_AVAILABLE) {
287 + dev_err(dev, "not ready\n");
288 + spin_unlock(&sdev->ud.lock);
292 + socket = sockfd_to_socket(sockfd);
294 + spin_unlock(&sdev->ud.lock);
299 + setnodelay(socket);
300 + setkeepalive(socket);
304 + sdev->ud.tcp_socket = socket;
306 + spin_unlock(&sdev->ud.lock);
308 + usbip_start_threads(&sdev->ud);
310 + spin_lock(&sdev->ud.lock);
311 + sdev->ud.status = SDEV_ST_USED;
312 + spin_unlock(&sdev->ud.lock);
315 + dev_info(dev, "stub down\n");
317 + spin_lock(&sdev->ud.lock);
318 + if (sdev->ud.status != SDEV_ST_USED) {
319 + spin_unlock(&sdev->ud.lock);
322 + spin_unlock(&sdev->ud.lock);
324 + usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
329 +static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
331 +static int stub_add_files(struct device *dev)
335 + err = device_create_file(dev, &dev_attr_usbip_status);
339 + err = device_create_file(dev, &dev_attr_usbip_sockfd);
343 + err = device_create_file(dev, &dev_attr_usbip_debug);
350 + device_remove_file(dev, &dev_attr_usbip_sockfd);
353 + device_remove_file(dev, &dev_attr_usbip_status);
359 +static void stub_remove_files(struct device *dev)
361 + device_remove_file(dev, &dev_attr_usbip_status);
362 + device_remove_file(dev, &dev_attr_usbip_sockfd);
363 + device_remove_file(dev, &dev_attr_usbip_debug);
368 +/*-------------------------------------------------------------------------*/
370 +/* Event handler functions called by an event handler thread */
372 +static void stub_shutdown_connection(struct usbip_device *ud)
374 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
377 + * When removing an exported device, kernel panic sometimes occurred
378 + * and then EIP was sk_wait_data of stub_rx thread. Is this because
379 + * sk_wait_data returned though stub_rx thread was already finished by
382 + if (ud->tcp_socket) {
383 + udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
384 + kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
387 + /* 1. stop threads */
388 + usbip_stop_threads(ud);
390 + /* 2. close the socket */
392 + * tcp_socket is freed after threads are killed.
393 + * So usbip_xmit do not touch NULL socket.
395 + if (ud->tcp_socket) {
396 + sock_release(ud->tcp_socket);
397 + ud->tcp_socket = NULL;
400 + /* 3. free used data */
401 + stub_device_cleanup_urbs(sdev);
403 + /* 4. free stub_unlink */
405 + unsigned long flags;
406 + struct stub_unlink *unlink, *tmp;
408 + spin_lock_irqsave(&sdev->priv_lock, flags);
410 + list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
411 + list_del(&unlink->list);
415 + list_for_each_entry_safe(unlink, tmp,
416 + &sdev->unlink_free, list) {
417 + list_del(&unlink->list);
421 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
425 +static void stub_device_reset(struct usbip_device *ud)
427 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
428 + struct usb_device *udev = interface_to_usbdev(sdev->interface);
431 + udbg("device reset");
432 + ret = usb_lock_device_for_reset(udev, sdev->interface);
434 + dev_err(&udev->dev, "lock for reset\n");
436 + spin_lock(&ud->lock);
437 + ud->status = SDEV_ST_ERROR;
438 + spin_unlock(&ud->lock);
443 + /* try to reset the device */
444 + ret = usb_reset_device(udev);
446 + usb_unlock_device(udev);
448 + spin_lock(&ud->lock);
450 + dev_err(&udev->dev, "device reset\n");
451 + ud->status = SDEV_ST_ERROR;
454 + dev_info(&udev->dev, "device reset\n");
455 + ud->status = SDEV_ST_AVAILABLE;
458 + spin_unlock(&ud->lock);
463 +static void stub_device_unusable(struct usbip_device *ud)
465 + spin_lock(&ud->lock);
466 + ud->status = SDEV_ST_ERROR;
467 + spin_unlock(&ud->lock);
471 +/*-------------------------------------------------------------------------*/
474 + * stub_device_alloc - allocate a new stub_device struct
475 + * @interface: usb_interface of a new device
477 + * Allocates and initializes a new stub_device struct.
479 +static struct stub_device *stub_device_alloc(struct usb_interface *interface)
481 + struct stub_device *sdev;
482 + int busnum = interface_to_busnum(interface);
483 + int devnum = interface_to_devnum(interface);
485 + dev_dbg(&interface->dev, "allocating stub device");
487 + /* yes, it's a new device */
488 + sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
490 + dev_err(&interface->dev, "no memory for stub_device\n");
494 + sdev->interface = interface;
497 + * devid is defined with devnum when this driver is first allocated.
498 + * devnum may change later if a device is reset. However, devid never
499 + * changes during a usbip connection.
501 + sdev->devid = (busnum << 16) | devnum;
503 + usbip_task_init(&sdev->ud.tcp_rx, "stub_rx", stub_rx_loop);
504 + usbip_task_init(&sdev->ud.tcp_tx, "stub_tx", stub_tx_loop);
506 + sdev->ud.side = USBIP_STUB;
507 + sdev->ud.status = SDEV_ST_AVAILABLE;
508 + /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
509 + spin_lock_init(&sdev->ud.lock);
510 + sdev->ud.tcp_socket = NULL;
512 + INIT_LIST_HEAD(&sdev->priv_init);
513 + INIT_LIST_HEAD(&sdev->priv_tx);
514 + INIT_LIST_HEAD(&sdev->priv_free);
515 + INIT_LIST_HEAD(&sdev->unlink_free);
516 + INIT_LIST_HEAD(&sdev->unlink_tx);
517 + /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
518 + spin_lock_init(&sdev->priv_lock);
520 + init_waitqueue_head(&sdev->tx_waitq);
522 + sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
523 + sdev->ud.eh_ops.reset = stub_device_reset;
524 + sdev->ud.eh_ops.unusable = stub_device_unusable;
526 + usbip_start_eh(&sdev->ud);
528 + udbg("register new interface\n");
532 +static int stub_device_free(struct stub_device *sdev)
538 + udbg("kfree udev ok\n");
544 +/*-------------------------------------------------------------------------*/
547 + * If a usb device has multiple active interfaces, this driver is bound to all
548 + * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
549 + * active interface). Currently, a userland program must ensure that it
550 + * looks at the usbip's sysfs entries of only the first active interface.
552 + * TODO: use "struct usb_device_driver" to bind a usb device.
553 + * However, it seems it is not fully supported in mainline kernel yet
556 +static int stub_probe(struct usb_interface *interface,
557 + const struct usb_device_id *id)
559 + struct usb_device *udev = interface_to_usbdev(interface);
560 + struct stub_device *sdev = NULL;
561 + char *udev_busid = interface->dev.parent->bus_id;
564 + dev_dbg(&interface->dev, "Enter\n");
566 + /* check we should claim or not by busid_table */
567 + if (match_busid(udev_busid)) {
568 + dev_info(&interface->dev,
569 + "this device %s is not in match_busid table. skip!\n",
573 + * Return value should be ENODEV or ENOXIO to continue trying
574 + * other matched drivers by the driver core.
575 + * See driver_probe_device() in driver/base/dd.c
580 + if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
581 + udbg("this device %s is a usb hub device. skip!\n",
586 + if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
587 + udbg("this device %s is attached on vhci_hcd. skip!\n",
592 + /* ok. this is my device. */
593 + sdev = stub_device_alloc(interface);
597 + dev_info(&interface->dev, "USB/IP Stub: register a new interface "
598 + "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
599 + interface->cur_altsetting->desc.bInterfaceNumber);
601 + /* set private data to usb_interface */
602 + usb_set_intfdata(interface, sdev);
604 + err = stub_add_files(&interface->dev);
606 + dev_err(&interface->dev, "create sysfs files for %s\n",
616 + * called in usb_disconnect() or usb_deregister()
617 + * but only if actconfig(active configuration) exists
619 +static void stub_disconnect(struct usb_interface *interface)
621 + struct stub_device *sdev = usb_get_intfdata(interface);
625 + /* get stub_device */
627 + err(" could not get device from inteface data");
632 + usb_set_intfdata(interface, NULL);
637 + * rx/tx threads are invoked for each usb_device.
639 + stub_remove_files(&interface->dev);
641 + /* 1. shutdown the current connection */
642 + usbip_event_add(&sdev->ud, SDEV_EVENT_REMOVED);
644 + /* 2. wait for the stop of the event handler */
645 + usbip_stop_eh(&sdev->ud);
648 + stub_device_free(sdev);
653 diff --git a/drivers/staging/usbip/stub_main.c b/drivers/staging/usbip/stub_main.c
655 index 0000000..c665d7f
657 +++ b/drivers/staging/usbip/stub_main.c
660 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
662 + * This is free software; you can redistribute it and/or modify
663 + * it under the terms of the GNU General Public License as published by
664 + * the Free Software Foundation; either version 2 of the License, or
665 + * (at your option) any later version.
667 + * This is distributed in the hope that it will be useful,
668 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
669 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
670 + * GNU General Public License for more details.
672 + * You should have received a copy of the GNU General Public License
673 + * along with this program; if not, write to the Free Software
674 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
679 +#include "usbip_common.h"
682 +/* Version Information */
683 +#define DRIVER_VERSION "1.0"
684 +#define DRIVER_AUTHOR "Takahiro Hirofuchi"
685 +#define DRIVER_DESC "Stub Driver for USB/IP"
687 +/* stub_priv is allocated from stub_priv_cache */
688 +struct kmem_cache *stub_priv_cache;
690 +/*-------------------------------------------------------------------------*/
692 +/* Define sysfs entries for the usbip driver */
696 + * busid_tables defines matching busids that usbip can grab. A user can change
697 + * dynamically what device is locally used and what device is exported to a
700 +#define MAX_BUSID 16
701 +static char busid_table[MAX_BUSID][BUS_ID_SIZE];
702 +static spinlock_t busid_table_lock;
705 +int match_busid(char *busid)
709 + spin_lock(&busid_table_lock);
711 + for (i = 0; i < MAX_BUSID; i++)
712 + if (busid_table[i][0])
713 + if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
714 + /* already registerd */
715 + spin_unlock(&busid_table_lock);
719 + spin_unlock(&busid_table_lock);
724 +static ssize_t show_match_busid(struct device_driver *drv, char *buf)
729 + spin_lock(&busid_table_lock);
731 + for (i = 0; i < MAX_BUSID; i++)
732 + if (busid_table[i][0])
733 + out += sprintf(out, "%s ", busid_table[i]);
735 + spin_unlock(&busid_table_lock);
737 + out += sprintf(out, "\n");
742 +static int add_match_busid(char *busid)
746 + if (!match_busid(busid))
749 + spin_lock(&busid_table_lock);
751 + for (i = 0; i < MAX_BUSID; i++)
752 + if (!busid_table[i][0]) {
753 + strncpy(busid_table[i], busid, BUS_ID_SIZE);
754 + spin_unlock(&busid_table_lock);
758 + spin_unlock(&busid_table_lock);
763 +static int del_match_busid(char *busid)
767 + spin_lock(&busid_table_lock);
769 + for (i = 0; i < MAX_BUSID; i++)
770 + if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
772 + memset(busid_table[i], 0, BUS_ID_SIZE);
773 + spin_unlock(&busid_table_lock);
777 + spin_unlock(&busid_table_lock);
782 +static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
786 + char busid[BUS_ID_SIZE];
791 + /* strnlen() does not include \0 */
792 + len = strnlen(buf + 4, BUS_ID_SIZE);
794 + /* busid needs to include \0 termination */
795 + if (!(len < BUS_ID_SIZE))
798 + strncpy(busid, buf + 4, BUS_ID_SIZE);
801 + if (!strncmp(buf, "add ", 4)) {
802 + if (add_match_busid(busid) < 0)
805 + udbg("add busid %s\n", busid);
808 + } else if (!strncmp(buf, "del ", 4)) {
809 + if (del_match_busid(busid) < 0)
812 + udbg("del busid %s\n", busid);
819 +static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid,
820 + store_match_busid);
824 +/*-------------------------------------------------------------------------*/
826 +/* Cleanup functions used to free private data */
828 +static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
830 + struct stub_priv *priv, *tmp;
832 + list_for_each_entry_safe(priv, tmp, listhead, list) {
833 + list_del(&priv->list);
840 +static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
842 + unsigned long flags;
843 + struct stub_priv *priv;
845 + spin_lock_irqsave(&sdev->priv_lock, flags);
847 + priv = stub_priv_pop_from_listhead(&sdev->priv_init);
849 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
853 + priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
855 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
859 + priv = stub_priv_pop_from_listhead(&sdev->priv_free);
861 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
865 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
869 +void stub_device_cleanup_urbs(struct stub_device *sdev)
871 + struct stub_priv *priv;
873 + udbg("free sdev %p\n", sdev);
875 + while ((priv = stub_priv_pop(sdev))) {
876 + struct urb *urb = priv->urb;
878 + udbg(" free urb %p\n", urb);
881 + kmem_cache_free(stub_priv_cache, priv);
883 + if (urb->transfer_buffer != NULL)
884 + kfree(urb->transfer_buffer);
886 + if (urb->setup_packet != NULL)
887 + kfree(urb->setup_packet);
894 +/*-------------------------------------------------------------------------*/
896 +static int __init usb_stub_init(void)
900 + stub_priv_cache = kmem_cache_create("stub_priv",
901 + sizeof(struct stub_priv), 0,
902 + SLAB_HWCACHE_ALIGN, NULL);
904 + if (!stub_priv_cache) {
905 + printk(KERN_ERR KBUILD_MODNAME
906 + ": create stub_priv_cache error\n");
910 + ret = usb_register(&stub_driver);
912 + printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n",
914 + goto error_usb_register;
917 + printk(KERN_INFO KBUILD_MODNAME ":"
918 + DRIVER_DESC ":" DRIVER_VERSION "\n");
920 + memset(busid_table, 0, sizeof(busid_table));
921 + spin_lock_init(&busid_table_lock);
923 + ret = driver_create_file(&stub_driver.drvwrap.driver,
924 + &driver_attr_match_busid);
927 + printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n");
928 + goto error_create_file;
933 + usb_deregister(&stub_driver);
935 + kmem_cache_destroy(stub_priv_cache);
939 +static void __exit usb_stub_exit(void)
941 + driver_remove_file(&stub_driver.drvwrap.driver,
942 + &driver_attr_match_busid);
945 + * deregister() calls stub_disconnect() for all devices. Device
946 + * specific data is cleared in stub_disconnect().
948 + usb_deregister(&stub_driver);
950 + kmem_cache_destroy(stub_priv_cache);
953 +module_init(usb_stub_init);
954 +module_exit(usb_stub_exit);
956 +MODULE_AUTHOR(DRIVER_AUTHOR);
957 +MODULE_DESCRIPTION(DRIVER_DESC);
958 +MODULE_LICENSE("GPL");
959 diff --git a/drivers/staging/usbip/stub_rx.c b/drivers/staging/usbip/stub_rx.c
961 index 0000000..36ce898
963 +++ b/drivers/staging/usbip/stub_rx.c
966 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
968 + * This is free software; you can redistribute it and/or modify
969 + * it under the terms of the GNU General Public License as published by
970 + * the Free Software Foundation; either version 2 of the License, or
971 + * (at your option) any later version.
973 + * This is distributed in the hope that it will be useful,
974 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
975 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
976 + * GNU General Public License for more details.
978 + * You should have received a copy of the GNU General Public License
979 + * along with this program; if not, write to the Free Software
980 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
984 +#include "usbip_common.h"
986 +#include "../../usb/core/hcd.h"
989 +static int is_clear_halt_cmd(struct urb *urb)
991 + struct usb_ctrlrequest *req;
993 + req = (struct usb_ctrlrequest *) urb->setup_packet;
995 + return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
996 + (req->bRequestType == USB_RECIP_ENDPOINT) &&
997 + (req->wValue == USB_ENDPOINT_HALT);
1000 +static int is_set_interface_cmd(struct urb *urb)
1002 + struct usb_ctrlrequest *req;
1004 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1006 + return (req->bRequest == USB_REQ_SET_INTERFACE) &&
1007 + (req->bRequestType == USB_RECIP_INTERFACE);
1010 +static int is_set_configuration_cmd(struct urb *urb)
1012 + struct usb_ctrlrequest *req;
1014 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1016 + return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
1017 + (req->bRequestType == USB_RECIP_DEVICE);
1020 +static int is_reset_device_cmd(struct urb *urb)
1022 + struct usb_ctrlrequest *req;
1026 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1027 + value = le16_to_cpu(req->wValue);
1028 + index = le16_to_cpu(req->wIndex);
1030 + if ((req->bRequest == USB_REQ_SET_FEATURE) &&
1031 + (req->bRequestType == USB_RT_PORT) &&
1032 + (value = USB_PORT_FEAT_RESET)) {
1033 + dbg_stub_rx("reset_device_cmd, port %u\n", index);
1039 +static int tweak_clear_halt_cmd(struct urb *urb)
1041 + struct usb_ctrlrequest *req;
1047 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1050 + * The stalled endpoint is specified in the wIndex value. The endpoint
1051 + * of the urb is the target of this clear_halt request (i.e., control
1054 + target_endp = le16_to_cpu(req->wIndex) & 0x000f;
1056 + /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80. */
1057 + target_dir = le16_to_cpu(req->wIndex) & 0x0080;
1060 + target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
1062 + target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
1064 + ret = usb_clear_halt(urb->dev, target_pipe);
1066 + uinfo("clear_halt error: devnum %d endp %d, %d\n",
1067 + urb->dev->devnum, target_endp, ret);
1069 + uinfo("clear_halt done: devnum %d endp %d\n",
1070 + urb->dev->devnum, target_endp);
1075 +static int tweak_set_interface_cmd(struct urb *urb)
1077 + struct usb_ctrlrequest *req;
1082 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1083 + alternate = le16_to_cpu(req->wValue);
1084 + interface = le16_to_cpu(req->wIndex);
1086 + dbg_stub_rx("set_interface: inf %u alt %u\n", interface, alternate);
1088 + ret = usb_set_interface(urb->dev, interface, alternate);
1090 + uinfo("set_interface error: inf %u alt %u, %d\n",
1091 + interface, alternate, ret);
1093 + uinfo("set_interface done: inf %u alt %u\n",
1100 +static int tweak_set_configuration_cmd(struct urb *urb)
1102 + struct usb_ctrlrequest *req;
1105 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1106 + config = le16_to_cpu(req->wValue);
1109 + * I have never seen a multi-config device. Very rare.
1110 + * For most devices, this will be called to choose a default
1111 + * configuration only once in an initialization phase.
1113 + * set_configuration may change a device configuration and its device
1114 + * drivers will be unbound and assigned for a new device configuration.
1115 + * This means this usbip driver will be also unbound when called, then
1116 + * eventually reassigned to the device as far as driver matching
1117 + * condition is kept.
1119 + * Unfortunatelly, an existing usbip connection will be dropped
1120 + * due to this driver unbinding. So, skip here.
1121 + * A user may need to set a special configuration value before
1122 + * exporting the device.
1124 + uinfo("set_configuration (%d) to %s\n", config, urb->dev->dev.bus_id);
1125 + uinfo("but, skip!\n");
1128 + /* return usb_driver_set_configuration(urb->dev, config); */
1131 +static int tweak_reset_device_cmd(struct urb *urb)
1133 + struct usb_ctrlrequest *req;
1138 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1139 + value = le16_to_cpu(req->wValue);
1140 + index = le16_to_cpu(req->wIndex);
1142 + uinfo("reset_device (port %d) to %s\n", index, urb->dev->dev.bus_id);
1144 + /* all interfaces should be owned by usbip driver, so just reset it. */
1145 + ret = usb_lock_device_for_reset(urb->dev, NULL);
1147 + dev_err(&urb->dev->dev, "lock for reset\n");
1151 + /* try to reset the device */
1152 + ret = usb_reset_device(urb->dev);
1154 + dev_err(&urb->dev->dev, "device reset\n");
1156 + usb_unlock_device(urb->dev);
1162 + * clear_halt, set_interface, and set_configuration require special tricks.
1164 +static void tweak_special_requests(struct urb *urb)
1166 + if (!urb || !urb->setup_packet)
1169 + if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
1172 + if (is_clear_halt_cmd(urb))
1173 + /* tweak clear_halt */
1174 + tweak_clear_halt_cmd(urb);
1176 + else if (is_set_interface_cmd(urb))
1177 + /* tweak set_interface */
1178 + tweak_set_interface_cmd(urb);
1180 + else if (is_set_configuration_cmd(urb))
1181 + /* tweak set_configuration */
1182 + tweak_set_configuration_cmd(urb);
1184 + else if (is_reset_device_cmd(urb))
1185 + tweak_reset_device_cmd(urb);
1187 + dbg_stub_rx("no need to tweak\n");
1191 + * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
1192 + * By unlinking the urb asynchronously, stub_rx can continuously
1193 + * process coming urbs. Even if the urb is unlinked, its completion
1194 + * handler will be called and stub_tx will send a return pdu.
1196 + * See also comments about unlinking strategy in vhci_hcd.c.
1198 +static int stub_recv_cmd_unlink(struct stub_device *sdev,
1199 + struct usbip_header *pdu)
1201 + struct list_head *listhead = &sdev->priv_init;
1202 + struct list_head *ptr;
1203 + unsigned long flags;
1205 + struct stub_priv *priv;
1208 + spin_lock_irqsave(&sdev->priv_lock, flags);
1210 + for (ptr = listhead->next; ptr != listhead; ptr = ptr->next) {
1211 + priv = list_entry(ptr, struct stub_priv, list);
1212 + if (priv->seqnum == pdu->u.cmd_unlink.seqnum) {
1215 + dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
1219 + * This matched urb is not completed yet (i.e., be in
1220 + * flight in usb hcd hardware/driver). Now we are
1221 + * cancelling it. The unlinking flag means that we are
1222 + * now not going to return the normal result pdu of a
1223 + * submission request, but going to return a result pdu
1224 + * of the unlink request.
1226 + priv->unlinking = 1;
1229 + * In the case that unlinking flag is on, prev->seqnum
1230 + * is changed from the seqnum of the cancelling urb to
1231 + * the seqnum of the unlink request. This will be used
1232 + * to make the result pdu of the unlink request.
1234 + priv->seqnum = pdu->base.seqnum;
1236 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1239 + * usb_unlink_urb() is now out of spinlocking to avoid
1240 + * spinlock recursion since stub_complete() is
1241 + * sometimes called in this context but not in the
1242 + * interrupt context. If stub_complete() is executed
1243 + * before we call usb_unlink_urb(), usb_unlink_urb()
1244 + * will return an error value. In this case, stub_tx
1245 + * will return the result pdu of this unlink request
1246 + * though submission is completed and actual unlinking
1247 + * is not executed. OK?
1249 + /* In the above case, urb->status is not -ECONNRESET,
1250 + * so a driver in a client host will know the failure
1251 + * of the unlink request ?
1253 + ret = usb_unlink_urb(priv->urb);
1254 + if (ret != -EINPROGRESS)
1255 + dev_err(&priv->urb->dev->dev,
1256 + "failed to unlink a urb %p, ret %d\n",
1262 + dbg_stub_rx("seqnum %d is not pending\n", pdu->u.cmd_unlink.seqnum);
1265 + * The urb of the unlink target is not found in priv_init queue. It was
1266 + * already completed and its results is/was going to be sent by a
1267 + * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
1268 + * return the completeness of this unlink request to vhci_hcd.
1270 + stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
1272 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1278 +static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
1280 + struct usbip_device *ud = &sdev->ud;
1282 + if (pdu->base.devid == sdev->devid) {
1283 + spin_lock(&ud->lock);
1284 + if (ud->status == SDEV_ST_USED) {
1285 + /* A request is valid. */
1286 + spin_unlock(&ud->lock);
1289 + spin_unlock(&ud->lock);
1295 +static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
1296 + struct usbip_header *pdu)
1298 + struct stub_priv *priv;
1299 + struct usbip_device *ud = &sdev->ud;
1300 + unsigned long flags;
1302 + spin_lock_irqsave(&sdev->priv_lock, flags);
1304 + priv = kmem_cache_alloc(stub_priv_cache, GFP_ATOMIC);
1306 + dev_err(&sdev->interface->dev, "alloc stub_priv\n");
1307 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1308 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1312 + memset(priv, 0, sizeof(struct stub_priv));
1314 + priv->seqnum = pdu->base.seqnum;
1315 + priv->sdev = sdev;
1318 + * After a stub_priv is linked to a list_head,
1319 + * our error handler can free allocated data.
1321 + list_add_tail(&priv->list, &sdev->priv_init);
1323 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1329 +static struct usb_host_endpoint *get_ep_from_epnum(struct usb_device *udev,
1332 + struct usb_host_config *config;
1334 + struct usb_host_endpoint *ep = NULL;
1339 + return &udev->ep0;
1341 + config = udev->actconfig;
1345 + for (i = 0; i < config->desc.bNumInterfaces; i++) {
1346 + struct usb_host_interface *setting;
1348 + setting = config->interface[i]->cur_altsetting;
1350 + for (j = 0; j < setting->desc.bNumEndpoints; j++) {
1351 + ep = &setting->endpoint[j];
1352 + epnum = (ep->desc.bEndpointAddress & 0x7f);
1354 + if (epnum == epnum0) {
1355 + /* uinfo("found epnum %d\n", epnum0); */
1369 +static int get_pipe(struct stub_device *sdev, int epnum, int dir)
1371 + struct usb_device *udev = interface_to_usbdev(sdev->interface);
1372 + struct usb_host_endpoint *ep;
1373 + struct usb_endpoint_descriptor *epd = NULL;
1375 + ep = get_ep_from_epnum(udev, epnum);
1377 + dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
1386 + /* epnum 0 is always control */
1388 + if (dir == USBIP_DIR_OUT)
1389 + return usb_sndctrlpipe(udev, 0);
1391 + return usb_rcvctrlpipe(udev, 0);
1395 + if (usb_endpoint_xfer_control(epd)) {
1396 + if (dir == USBIP_DIR_OUT)
1397 + return usb_sndctrlpipe(udev, epnum);
1399 + return usb_rcvctrlpipe(udev, epnum);
1402 + if (usb_endpoint_xfer_bulk(epd)) {
1403 + if (dir == USBIP_DIR_OUT)
1404 + return usb_sndbulkpipe(udev, epnum);
1406 + return usb_rcvbulkpipe(udev, epnum);
1409 + if (usb_endpoint_xfer_int(epd)) {
1410 + if (dir == USBIP_DIR_OUT)
1411 + return usb_sndintpipe(udev, epnum);
1413 + return usb_rcvintpipe(udev, epnum);
1416 + if (usb_endpoint_xfer_isoc(epd)) {
1417 + if (dir == USBIP_DIR_OUT)
1418 + return usb_sndisocpipe(udev, epnum);
1420 + return usb_rcvisocpipe(udev, epnum);
1424 + dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
1428 +static void stub_recv_cmd_submit(struct stub_device *sdev,
1429 + struct usbip_header *pdu)
1432 + struct stub_priv *priv;
1433 + struct usbip_device *ud = &sdev->ud;
1434 + struct usb_device *udev = interface_to_usbdev(sdev->interface);
1435 + int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction);
1438 + priv = stub_priv_alloc(sdev, pdu);
1443 + if (usb_pipeisoc(pipe))
1444 + priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
1447 + priv->urb = usb_alloc_urb(0, GFP_KERNEL);
1450 + dev_err(&sdev->interface->dev, "malloc urb\n");
1451 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1455 + /* set priv->urb->transfer_buffer */
1456 + if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
1457 + priv->urb->transfer_buffer =
1458 + kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
1460 + if (!priv->urb->transfer_buffer) {
1461 + dev_err(&sdev->interface->dev, "malloc x_buff\n");
1462 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1467 + /* set priv->urb->setup_packet */
1468 + priv->urb->setup_packet = kzalloc(8, GFP_KERNEL);
1469 + if (!priv->urb->setup_packet) {
1470 + dev_err(&sdev->interface->dev, "allocate setup_packet\n");
1471 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1474 + memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8);
1476 + /* set other members from the base header of pdu */
1477 + priv->urb->context = (void *) priv;
1478 + priv->urb->dev = udev;
1479 + priv->urb->pipe = pipe;
1480 + priv->urb->complete = stub_complete;
1482 + usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
1485 + if (usbip_recv_xbuff(ud, priv->urb) < 0)
1488 + if (usbip_recv_iso(ud, priv->urb) < 0)
1491 + /* no need to submit an intercepted request, but harmless? */
1492 + tweak_special_requests(priv->urb);
1494 + /* urb is now ready to submit */
1495 + ret = usb_submit_urb(priv->urb, GFP_KERNEL);
1498 + dbg_stub_rx("submit urb ok, seqnum %u\n", pdu->base.seqnum);
1500 + dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
1501 + usbip_dump_header(pdu);
1502 + usbip_dump_urb(priv->urb);
1506 + * This connection will be discarded.
1508 + usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
1511 + dbg_stub_rx("Leave\n");
1516 +static void stub_rx_pdu(struct usbip_device *ud)
1519 + struct usbip_header pdu;
1520 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1521 + struct device *dev = &sdev->interface->dev;
1523 + dbg_stub_rx("Enter\n");
1525 + memset(&pdu, 0, sizeof(pdu));
1527 + /* 1. receive a pdu header */
1528 + ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
1529 + if (ret != sizeof(pdu)) {
1530 + dev_err(dev, "recv a header, %d\n", ret);
1531 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1535 + usbip_header_correct_endian(&pdu, 0);
1537 + if (dbg_flag_stub_rx)
1538 + usbip_dump_header(&pdu);
1540 + if (!valid_request(sdev, &pdu)) {
1541 + dev_err(dev, "recv invalid request\n");
1542 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1546 + switch (pdu.base.command) {
1547 + case USBIP_CMD_UNLINK:
1548 + stub_recv_cmd_unlink(sdev, &pdu);
1551 + case USBIP_CMD_SUBMIT:
1552 + stub_recv_cmd_submit(sdev, &pdu);
1557 + dev_err(dev, "unknown pdu\n");
1558 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1564 +void stub_rx_loop(struct usbip_task *ut)
1566 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
1569 + if (signal_pending(current)) {
1570 + dbg_stub_rx("signal caught!\n");
1574 + if (usbip_event_happend(ud))
1580 diff --git a/drivers/staging/usbip/stub_tx.c b/drivers/staging/usbip/stub_tx.c
1581 new file mode 100644
1582 index 0000000..d5563cd
1584 +++ b/drivers/staging/usbip/stub_tx.c
1587 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1589 + * This is free software; you can redistribute it and/or modify
1590 + * it under the terms of the GNU General Public License as published by
1591 + * the Free Software Foundation; either version 2 of the License, or
1592 + * (at your option) any later version.
1594 + * This is distributed in the hope that it will be useful,
1595 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1596 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1597 + * GNU General Public License for more details.
1599 + * You should have received a copy of the GNU General Public License
1600 + * along with this program; if not, write to the Free Software
1601 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1605 +#include "usbip_common.h"
1609 +static void stub_free_priv_and_urb(struct stub_priv *priv)
1611 + struct urb *urb = priv->urb;
1613 + kfree(urb->setup_packet);
1614 + kfree(urb->transfer_buffer);
1615 + list_del(&priv->list);
1616 + kmem_cache_free(stub_priv_cache, priv);
1617 + usb_free_urb(urb);
1620 +/* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
1621 +void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
1624 + struct stub_unlink *unlink;
1626 + unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
1628 + dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
1629 + usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
1633 + unlink->seqnum = seqnum;
1634 + unlink->status = status;
1636 + list_add_tail(&unlink->list, &sdev->unlink_tx);
1640 + * stub_complete - completion handler of a usbip urb
1641 + * @urb: pointer to the urb completed
1644 + * When a urb has completed, the USB core driver calls this function mostly in
1645 + * the interrupt context. To return the result of a urb, the completed urb is
1646 + * linked to the pending list of returning.
1649 +void stub_complete(struct urb *urb)
1651 + struct stub_priv *priv = (struct stub_priv *) urb->context;
1652 + struct stub_device *sdev = priv->sdev;
1653 + unsigned long flags;
1655 + dbg_stub_tx("complete! status %d\n", urb->status);
1658 + switch (urb->status) {
1663 + uinfo("stopped by a call of usb_kill_urb() because of"
1664 + "cleaning up a virtual connection\n");
1667 + uinfo("unlinked by a call of usb_unlink_urb()\n");
1670 + uinfo("endpoint %d is stalled\n", usb_pipeendpoint(urb->pipe));
1673 + uinfo("device removed?\n");
1676 + uinfo("urb completion with non-zero status %d\n", urb->status);
1679 + /* link a urb to the queue of tx. */
1680 + spin_lock_irqsave(&sdev->priv_lock, flags);
1682 + if (priv->unlinking) {
1683 + stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
1684 + stub_free_priv_and_urb(priv);
1686 + list_move_tail(&priv->list, &sdev->priv_tx);
1689 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1691 + /* wake up tx_thread */
1692 + wake_up(&sdev->tx_waitq);
1696 +/*-------------------------------------------------------------------------*/
1699 +static inline void setup_base_pdu(struct usbip_header_basic *base,
1700 + __u32 command, __u32 seqnum)
1702 + base->command = command;
1703 + base->seqnum = seqnum;
1706 + base->direction = 0;
1709 +static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
1711 + struct stub_priv *priv = (struct stub_priv *) urb->context;
1713 + setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
1715 + usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
1718 +static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
1719 + struct stub_unlink *unlink)
1721 + setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
1723 + rpdu->u.ret_unlink.status = unlink->status;
1727 +/*-------------------------------------------------------------------------*/
1728 +/* send RET_SUBMIT */
1730 +static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
1732 + unsigned long flags;
1733 + struct stub_priv *priv, *tmp;
1735 + spin_lock_irqsave(&sdev->priv_lock, flags);
1737 + list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
1738 + list_move_tail(&priv->list, &sdev->priv_free);
1739 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1743 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1748 +static int stub_send_ret_submit(struct stub_device *sdev)
1750 + unsigned long flags;
1751 + struct stub_priv *priv, *tmp;
1753 + struct msghdr msg;
1754 + struct kvec iov[3];
1757 + size_t total_size = 0;
1759 + while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
1761 + struct urb *urb = priv->urb;
1762 + struct usbip_header pdu_header;
1763 + void *iso_buffer = NULL;
1766 + memset(&pdu_header, 0, sizeof(pdu_header));
1767 + memset(&msg, 0, sizeof(msg));
1768 + memset(&iov, 0, sizeof(iov));
1770 + dbg_stub_tx("setup txdata urb %p\n", urb);
1773 + /* 1. setup usbip_header */
1774 + setup_ret_submit_pdu(&pdu_header, urb);
1775 + usbip_header_correct_endian(&pdu_header, 1);
1777 + iov[0].iov_base = &pdu_header;
1778 + iov[0].iov_len = sizeof(pdu_header);
1779 + txsize += sizeof(pdu_header);
1781 + /* 2. setup transfer buffer */
1782 + if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
1783 + iov[1].iov_base = urb->transfer_buffer;
1784 + iov[1].iov_len = urb->actual_length;
1785 + txsize += urb->actual_length;
1788 + /* 3. setup iso_packet_descriptor */
1789 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1792 + iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
1793 + if (!iso_buffer) {
1794 + usbip_event_add(&sdev->ud,
1795 + SDEV_EVENT_ERROR_MALLOC);
1799 + iov[2].iov_base = iso_buffer;
1800 + iov[2].iov_len = len;
1804 + ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1806 + if (ret != txsize) {
1807 + dev_err(&sdev->interface->dev,
1808 + "sendmsg failed!, retval %d for %zd\n",
1810 + kfree(iso_buffer);
1811 + usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1815 + kfree(iso_buffer);
1816 + dbg_stub_tx("send txdata\n");
1818 + total_size += txsize;
1822 + spin_lock_irqsave(&sdev->priv_lock, flags);
1824 + list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
1825 + stub_free_priv_and_urb(priv);
1828 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1830 + return total_size;
1834 +/*-------------------------------------------------------------------------*/
1835 +/* send RET_UNLINK */
1837 +static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
1839 + unsigned long flags;
1840 + struct stub_unlink *unlink, *tmp;
1842 + spin_lock_irqsave(&sdev->priv_lock, flags);
1844 + list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
1845 + list_move_tail(&unlink->list, &sdev->unlink_free);
1846 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1850 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1856 +static int stub_send_ret_unlink(struct stub_device *sdev)
1858 + unsigned long flags;
1859 + struct stub_unlink *unlink, *tmp;
1861 + struct msghdr msg;
1862 + struct kvec iov[1];
1865 + size_t total_size = 0;
1867 + while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
1869 + struct usbip_header pdu_header;
1872 + memset(&pdu_header, 0, sizeof(pdu_header));
1873 + memset(&msg, 0, sizeof(msg));
1874 + memset(&iov, 0, sizeof(iov));
1876 + dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
1878 + /* 1. setup usbip_header */
1879 + setup_ret_unlink_pdu(&pdu_header, unlink);
1880 + usbip_header_correct_endian(&pdu_header, 1);
1882 + iov[0].iov_base = &pdu_header;
1883 + iov[0].iov_len = sizeof(pdu_header);
1884 + txsize += sizeof(pdu_header);
1886 + ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1888 + if (ret != txsize) {
1889 + dev_err(&sdev->interface->dev,
1890 + "sendmsg failed!, retval %d for %zd\n",
1892 + usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1897 + dbg_stub_tx("send txdata\n");
1899 + total_size += txsize;
1903 + spin_lock_irqsave(&sdev->priv_lock, flags);
1905 + list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
1906 + list_del(&unlink->list);
1910 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1912 + return total_size;
1916 +/*-------------------------------------------------------------------------*/
1918 +void stub_tx_loop(struct usbip_task *ut)
1920 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
1921 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1924 + if (signal_pending(current)) {
1925 + dbg_stub_tx("signal catched\n");
1929 + if (usbip_event_happend(ud))
1933 + * send_ret_submit comes earlier than send_ret_unlink. stub_rx
1934 + * looks at only priv_init queue. If the completion of a URB is
1935 + * earlier than the receive of CMD_UNLINK, priv is moved to
1936 + * priv_tx queue and stub_rx does not find the target priv. In
1937 + * this case, vhci_rx receives the result of the submit request
1938 + * and then receives the result of the unlink request. The
1939 + * result of the submit is given back to the usbcore as the
1940 + * completion of the unlink request. The request of the
1941 + * unlink is ignored. This is ok because a driver who calls
1942 + * usb_unlink_urb() understands the unlink was too late by
1943 + * getting the status of the given-backed URB which has the
1944 + * status of usb_submit_urb().
1946 + if (stub_send_ret_submit(sdev) < 0)
1949 + if (stub_send_ret_unlink(sdev) < 0)
1952 + wait_event_interruptible(sdev->tx_waitq,
1953 + (!list_empty(&sdev->priv_tx) ||
1954 + !list_empty(&sdev->unlink_tx)));