1 --- kernel-2.6.28.orig/drivers/usb/Kconfig
2 +++ kernel-2.6.28/drivers/usb/Kconfig
5 source "drivers/usb/otg/Kconfig"
7 +source "drivers/usb/usbip/Kconfig"
10 --- kernel-2.6.28.orig/drivers/usb/Makefile
11 +++ kernel-2.6.28/drivers/usb/Makefile
14 obj-$(CONFIG_USB_ATM) += atm/
15 obj-$(CONFIG_USB_SPEEDTOUCH) += atm/
17 +obj-$(CONFIG_USB_IP_COMMON) += usbip/
19 +++ kernel-2.6.28/drivers/usb/usbip/Kconfig
22 + tristate "USB IP support (EXPERIMENTAL)"
23 + depends on USB && NET && EXPERIMENTAL
26 + This enables pushing USB packets over IP to allow remote
27 + machines access to USB devices directly. For more details,
28 + and links to the userspace utility programs to let this work
29 + properly, see http://usbip.naist.jp/
31 + To compile this driver as a module, choose M here: the
32 + module will be called usbip_common_mod.
36 +config USB_IP_VHCI_HCD
37 + tristate "USB IP client driver"
38 + depends on USB_IP_COMMON
41 + This enables the USB IP host controller driver which will
42 + run on the client machine.
44 + To compile this driver as a module, choose M here: the
45 + module will be called vhci_hcd.
48 + tristate "USB IP host driver"
49 + depends on USB_IP_COMMON
52 + This enables the USB IP device driver which will run on the
55 + To compile this driver as a module, choose M here: the
56 + module will be called usbip.
58 +++ kernel-2.6.28/drivers/usb/usbip/Makefile
60 +obj-$(CONFIG_USB_IP_COMMON) += usbip_common_mod.o
61 +usbip_common_mod-objs := usbip_common.o usbip_event.o
63 +obj-$(CONFIG_USB_IP_VHCI_HCD) += vhci-hcd.o
64 +vhci-hcd-objs := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
66 +obj-$(CONFIG_USB_IP_HOST) += usbip.o
67 +usbip-objs := stub_dev.o stub_main.o stub_rx.o stub_tx.o
69 +ifeq ($(CONFIG_USB_DEBUG),y)
70 + EXTRA_CFLAGS += -DDEBUG
73 +++ kernel-2.6.28/drivers/usb/usbip/README
76 + - more discussion about the protocol
78 + - review of the userspace interface
80 +Please send patches for this code to Greg Kroah-Hartman <greg@kroah.com>
82 +++ kernel-2.6.28/drivers/usb/usbip/stub.h
85 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
87 + * This is free software; you can redistribute it and/or modify
88 + * it under the terms of the GNU General Public License as published by
89 + * the Free Software Foundation; either version 2 of the License, or
90 + * (at your option) any later version.
92 + * This is distributed in the hope that it will be useful,
93 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
94 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
95 + * GNU General Public License for more details.
97 + * You should have received a copy of the GNU General Public License
98 + * along with this program; if not, write to the Free Software
99 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
103 +#include <linux/kernel.h>
104 +#include <linux/list.h>
105 +#include <linux/spinlock.h>
106 +#include <linux/slab.h>
107 +#include <linux/string.h>
108 +#include <linux/module.h>
109 +#include <linux/net.h>
111 +struct stub_device {
112 + struct usb_interface *interface;
113 + struct list_head list;
115 + struct usbip_device ud;
119 + * stub_priv preserves private data of each urb.
120 + * It is allocated as stub_priv_cache and assigned to urb->context.
122 + * stub_priv is always linked to any one of 3 lists;
123 + * priv_init: linked to this until the comletion of a urb.
124 + * priv_tx : linked to this after the completion of a urb.
125 + * priv_free: linked to this after the sending of the result.
127 + * Any of these list operations should be locked by priv_lock.
129 + spinlock_t priv_lock;
130 + struct list_head priv_init;
131 + struct list_head priv_tx;
132 + struct list_head priv_free;
134 + /* see comments for unlinking in stub_rx.c */
135 + struct list_head unlink_tx;
136 + struct list_head unlink_free;
139 + wait_queue_head_t tx_waitq;
142 +/* private data into urb->priv */
144 + unsigned long seqnum;
145 + struct list_head list;
146 + struct stub_device *sdev;
152 +struct stub_unlink {
153 + unsigned long seqnum;
154 + struct list_head list;
159 +extern struct kmem_cache *stub_priv_cache;
162 +/*-------------------------------------------------------------------------*/
163 +/* prototype declarations */
166 +void stub_complete(struct urb *);
167 +void stub_tx_loop(struct usbip_task *);
170 +extern struct usb_driver stub_driver;
173 +void stub_rx_loop(struct usbip_task *);
174 +void stub_enqueue_ret_unlink(struct stub_device *, __u32, __u32);
177 +int match_busid(char *busid);
178 +void stub_device_cleanup_urbs(struct stub_device *sdev);
180 +++ kernel-2.6.28/drivers/usb/usbip/stub_dev.c
183 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
185 + * This is free software; you can redistribute it and/or modify
186 + * it under the terms of the GNU General Public License as published by
187 + * the Free Software Foundation; either version 2 of the License, or
188 + * (at your option) any later version.
190 + * This is distributed in the hope that it will be useful,
191 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
192 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
193 + * GNU General Public License for more details.
195 + * You should have received a copy of the GNU General Public License
196 + * along with this program; if not, write to the Free Software
197 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
201 +#include "usbip_common.h"
206 +static int stub_probe(struct usb_interface *interface,
207 + const struct usb_device_id *id);
208 +static void stub_disconnect(struct usb_interface *interface);
212 + * Define device IDs here if you want to explicitly limit exportable devices.
213 + * In the most cases, wild card matching will be ok because driver binding can
214 + * be changed dynamically by a userland program.
216 +static struct usb_device_id stub_table[] = {
218 + /* just an example */
219 + { USB_DEVICE(0x05ac, 0x0301) }, /* Mac 1 button mouse */
220 + { USB_DEVICE(0x0430, 0x0009) }, /* Plat Home Keyboard */
221 + { USB_DEVICE(0x059b, 0x0001) }, /* Iomega USB Zip 100 */
222 + { USB_DEVICE(0x04b3, 0x4427) }, /* IBM USB CD-ROM */
223 + { USB_DEVICE(0x05a9, 0xa511) }, /* LifeView USB cam */
224 + { USB_DEVICE(0x55aa, 0x0201) }, /* Imation card reader */
225 + { USB_DEVICE(0x046d, 0x0870) }, /* Qcam Express(QV-30) */
226 + { USB_DEVICE(0x04bb, 0x0101) }, /* IO-DATA HD 120GB */
227 + { USB_DEVICE(0x04bb, 0x0904) }, /* IO-DATA USB-ET/TX */
228 + { USB_DEVICE(0x04bb, 0x0201) }, /* IO-DATA USB-ET/TX */
229 + { USB_DEVICE(0x08bb, 0x2702) }, /* ONKYO USB Speaker */
230 + { USB_DEVICE(0x046d, 0x08b2) }, /* Logicool Qcam 4000 Pro */
232 + /* magic for wild card */
233 + { .driver_info = 1 },
234 + { 0, } /* Terminating entry */
236 +MODULE_DEVICE_TABLE(usb, stub_table);
238 +struct usb_driver stub_driver = {
240 + .probe = stub_probe,
241 + .disconnect = stub_disconnect,
242 + .id_table = stub_table,
246 +/*-------------------------------------------------------------------------*/
248 +/* Define sysfs entries for a usbip-bound device */
252 + * usbip_status shows status of usbip as long as this driver is bound to the
255 +static ssize_t show_status(struct device *dev, struct device_attribute *attr,
258 + struct stub_device *sdev = dev_get_drvdata(dev);
262 + dev_err(dev, "sdev is null\n");
266 + spin_lock(&sdev->ud.lock);
267 + status = sdev->ud.status;
268 + spin_unlock(&sdev->ud.lock);
270 + return snprintf(buf, PAGE_SIZE, "%d\n", status);
272 +static DEVICE_ATTR(usbip_status, S_IRUGO, show_status, NULL);
275 + * usbip_sockfd gets a socket descriptor of an established TCP connection that
276 + * is used to transfer usbip requests by kernel threads. -1 is a magic number
277 + * by which usbip connection is finished.
279 +static ssize_t store_sockfd(struct device *dev, struct device_attribute *attr,
280 + const char *buf, size_t count)
282 + struct stub_device *sdev = dev_get_drvdata(dev);
284 + struct socket *socket;
287 + dev_err(dev, "sdev is null\n");
291 + sscanf(buf, "%d", &sockfd);
293 + if (sockfd != -1) {
294 + dev_info(dev, "stub up\n");
296 + spin_lock(&sdev->ud.lock);
298 + if (sdev->ud.status != SDEV_ST_AVAILABLE) {
299 + dev_err(dev, "not ready\n");
300 + spin_unlock(&sdev->ud.lock);
304 + socket = sockfd_to_socket(sockfd);
306 + spin_unlock(&sdev->ud.lock);
311 + setnodelay(socket);
312 + setkeepalive(socket);
316 + sdev->ud.tcp_socket = socket;
318 + spin_unlock(&sdev->ud.lock);
320 + usbip_start_threads(&sdev->ud);
322 + spin_lock(&sdev->ud.lock);
323 + sdev->ud.status = SDEV_ST_USED;
324 + spin_unlock(&sdev->ud.lock);
327 + dev_info(dev, "stub down\n");
329 + spin_lock(&sdev->ud.lock);
330 + if (sdev->ud.status != SDEV_ST_USED) {
331 + spin_unlock(&sdev->ud.lock);
334 + spin_unlock(&sdev->ud.lock);
336 + usbip_event_add(&sdev->ud, SDEV_EVENT_DOWN);
341 +static DEVICE_ATTR(usbip_sockfd, S_IWUSR, NULL, store_sockfd);
343 +static int stub_add_files(struct device *dev)
347 + err = device_create_file(dev, &dev_attr_usbip_status);
351 + err = device_create_file(dev, &dev_attr_usbip_sockfd);
355 + err = device_create_file(dev, &dev_attr_usbip_debug);
362 + device_remove_file(dev, &dev_attr_usbip_sockfd);
365 + device_remove_file(dev, &dev_attr_usbip_status);
371 +static void stub_remove_files(struct device *dev)
373 + device_remove_file(dev, &dev_attr_usbip_status);
374 + device_remove_file(dev, &dev_attr_usbip_sockfd);
375 + device_remove_file(dev, &dev_attr_usbip_debug);
380 +/*-------------------------------------------------------------------------*/
382 +/* Event handler functions called by an event handler thread */
384 +static void stub_shutdown_connection(struct usbip_device *ud)
386 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
389 + * When removing an exported device, kernel panic sometimes occurred
390 + * and then EIP was sk_wait_data of stub_rx thread. Is this because
391 + * sk_wait_data returned though stub_rx thread was already finished by
394 + if (ud->tcp_socket) {
395 + udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
396 + kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
399 + /* 1. stop threads */
400 + usbip_stop_threads(ud);
402 + /* 2. close the socket */
404 + * tcp_socket is freed after threads are killed.
405 + * So usbip_xmit do not touch NULL socket.
407 + if (ud->tcp_socket) {
408 + sock_release(ud->tcp_socket);
409 + ud->tcp_socket = NULL;
412 + /* 3. free used data */
413 + stub_device_cleanup_urbs(sdev);
415 + /* 4. free stub_unlink */
417 + unsigned long flags;
418 + struct stub_unlink *unlink, *tmp;
420 + spin_lock_irqsave(&sdev->priv_lock, flags);
422 + list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
423 + list_del(&unlink->list);
427 + list_for_each_entry_safe(unlink, tmp,
428 + &sdev->unlink_free, list) {
429 + list_del(&unlink->list);
433 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
437 +static void stub_device_reset(struct usbip_device *ud)
439 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
440 + struct usb_device *udev = interface_to_usbdev(sdev->interface);
443 + udbg("device reset");
444 + ret = usb_lock_device_for_reset(udev, sdev->interface);
446 + dev_err(&udev->dev, "lock for reset\n");
448 + spin_lock(&ud->lock);
449 + ud->status = SDEV_ST_ERROR;
450 + spin_unlock(&ud->lock);
455 + /* try to reset the device */
456 + ret = usb_reset_device(udev);
458 + usb_unlock_device(udev);
460 + spin_lock(&ud->lock);
462 + dev_err(&udev->dev, "device reset\n");
463 + ud->status = SDEV_ST_ERROR;
466 + dev_info(&udev->dev, "device reset\n");
467 + ud->status = SDEV_ST_AVAILABLE;
470 + spin_unlock(&ud->lock);
475 +static void stub_device_unusable(struct usbip_device *ud)
477 + spin_lock(&ud->lock);
478 + ud->status = SDEV_ST_ERROR;
479 + spin_unlock(&ud->lock);
483 +/*-------------------------------------------------------------------------*/
486 + * stub_device_alloc - allocate a new stub_device struct
487 + * @interface: usb_interface of a new device
489 + * Allocates and initializes a new stub_device struct.
491 +static struct stub_device *stub_device_alloc(struct usb_interface *interface)
493 + struct stub_device *sdev;
494 + int busnum = interface_to_busnum(interface);
495 + int devnum = interface_to_devnum(interface);
497 + dev_dbg(&interface->dev, "allocating stub device");
499 + /* yes, it's a new device */
500 + sdev = kzalloc(sizeof(struct stub_device), GFP_KERNEL);
502 + dev_err(&interface->dev, "no memory for stub_device\n");
506 + sdev->interface = interface;
509 + * devid is defined with devnum when this driver is first allocated.
510 + * devnum may change later if a device is reset. However, devid never
511 + * changes during a usbip connection.
513 + sdev->devid = (busnum << 16) | devnum;
515 + usbip_task_init(&sdev->ud.tcp_rx, "stub_rx", stub_rx_loop);
516 + usbip_task_init(&sdev->ud.tcp_tx, "stub_tx", stub_tx_loop);
518 + sdev->ud.side = USBIP_STUB;
519 + sdev->ud.status = SDEV_ST_AVAILABLE;
520 + /* sdev->ud.lock = SPIN_LOCK_UNLOCKED; */
521 + spin_lock_init(&sdev->ud.lock);
522 + sdev->ud.tcp_socket = NULL;
524 + INIT_LIST_HEAD(&sdev->priv_init);
525 + INIT_LIST_HEAD(&sdev->priv_tx);
526 + INIT_LIST_HEAD(&sdev->priv_free);
527 + INIT_LIST_HEAD(&sdev->unlink_free);
528 + INIT_LIST_HEAD(&sdev->unlink_tx);
529 + /* sdev->priv_lock = SPIN_LOCK_UNLOCKED; */
530 + spin_lock_init(&sdev->priv_lock);
532 + init_waitqueue_head(&sdev->tx_waitq);
534 + sdev->ud.eh_ops.shutdown = stub_shutdown_connection;
535 + sdev->ud.eh_ops.reset = stub_device_reset;
536 + sdev->ud.eh_ops.unusable = stub_device_unusable;
538 + usbip_start_eh(&sdev->ud);
540 + udbg("register new interface\n");
544 +static int stub_device_free(struct stub_device *sdev)
550 + udbg("kfree udev ok\n");
556 +/*-------------------------------------------------------------------------*/
559 + * If a usb device has multiple active interfaces, this driver is bound to all
560 + * the active interfaces. However, usbip exports *a* usb device (i.e., not *an*
561 + * active interface). Currently, a userland program must ensure that it
562 + * looks at the usbip's sysfs entries of only the first active interface.
564 + * TODO: use "struct usb_device_driver" to bind a usb device.
565 + * However, it seems it is not fully supported in mainline kernel yet
568 +static int stub_probe(struct usb_interface *interface,
569 + const struct usb_device_id *id)
571 + struct usb_device *udev = interface_to_usbdev(interface);
572 + struct stub_device *sdev = NULL;
573 + char *udev_busid = interface->dev.parent->bus_id;
576 + dev_dbg(&interface->dev, "Enter\n");
578 + /* check we should claim or not by busid_table */
579 + if (match_busid(udev_busid)) {
580 + dev_info(&interface->dev,
581 + "this device %s is not in match_busid table. skip!\n",
585 + * Return value should be ENODEV or ENOXIO to continue trying
586 + * other matched drivers by the driver core.
587 + * See driver_probe_device() in driver/base/dd.c
592 + if (udev->descriptor.bDeviceClass == USB_CLASS_HUB) {
593 + udbg("this device %s is a usb hub device. skip!\n",
598 + if (!strcmp(udev->bus->bus_name, "vhci_hcd")) {
599 + udbg("this device %s is attached on vhci_hcd. skip!\n",
604 + /* ok. this is my device. */
605 + sdev = stub_device_alloc(interface);
609 + dev_info(&interface->dev, "USB/IP Stub: register a new interface "
610 + "(bus %u dev %u ifn %u)\n", udev->bus->busnum, udev->devnum,
611 + interface->cur_altsetting->desc.bInterfaceNumber);
613 + /* set private data to usb_interface */
614 + usb_set_intfdata(interface, sdev);
616 + err = stub_add_files(&interface->dev);
618 + dev_err(&interface->dev, "create sysfs files for %s\n",
628 + * called in usb_disconnect() or usb_deregister()
629 + * but only if actconfig(active configuration) exists
631 +static void stub_disconnect(struct usb_interface *interface)
633 + struct stub_device *sdev = usb_get_intfdata(interface);
637 + /* get stub_device */
639 + err(" could not get device from inteface data");
644 + usb_set_intfdata(interface, NULL);
649 + * rx/tx threads are invoked for each usb_device.
651 + stub_remove_files(&interface->dev);
653 + /* 1. shutdown the current connection */
654 + usbip_event_add(&sdev->ud, SDEV_EVENT_REMOVED);
656 + /* 2. wait for the stop of the event handler */
657 + usbip_stop_eh(&sdev->ud);
660 + stub_device_free(sdev);
666 +++ kernel-2.6.28/drivers/usb/usbip/stub_main.c
669 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
671 + * This is free software; you can redistribute it and/or modify
672 + * it under the terms of the GNU General Public License as published by
673 + * the Free Software Foundation; either version 2 of the License, or
674 + * (at your option) any later version.
676 + * This is distributed in the hope that it will be useful,
677 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
678 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
679 + * GNU General Public License for more details.
681 + * You should have received a copy of the GNU General Public License
682 + * along with this program; if not, write to the Free Software
683 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
688 +#include "usbip_common.h"
691 +/* Version Information */
692 +#define DRIVER_VERSION "1.0"
693 +#define DRIVER_AUTHOR "Takahiro Hirofuchi"
694 +#define DRIVER_DESC "Stub Driver for USB/IP"
696 +/* stub_priv is allocated from stub_priv_cache */
697 +struct kmem_cache *stub_priv_cache;
699 +/*-------------------------------------------------------------------------*/
701 +/* Define sysfs entries for the usbip driver */
705 + * busid_tables defines matching busids that usbip can grab. A user can change
706 + * dynamically what device is locally used and what device is exported to a
709 +#define MAX_BUSID 16
710 +static char busid_table[MAX_BUSID][BUS_ID_SIZE];
711 +static spinlock_t busid_table_lock;
714 +int match_busid(char *busid)
718 + spin_lock(&busid_table_lock);
720 + for (i = 0; i < MAX_BUSID; i++)
721 + if (busid_table[i][0])
722 + if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
723 + /* already registerd */
724 + spin_unlock(&busid_table_lock);
728 + spin_unlock(&busid_table_lock);
733 +static ssize_t show_match_busid(struct device_driver *drv, char *buf)
738 + spin_lock(&busid_table_lock);
740 + for (i = 0; i < MAX_BUSID; i++)
741 + if (busid_table[i][0])
742 + out += sprintf(out, "%s ", busid_table[i]);
744 + spin_unlock(&busid_table_lock);
746 + out += sprintf(out, "\n");
751 +static int add_match_busid(char *busid)
755 + if (!match_busid(busid))
758 + spin_lock(&busid_table_lock);
760 + for (i = 0; i < MAX_BUSID; i++)
761 + if (!busid_table[i][0]) {
762 + strncpy(busid_table[i], busid, BUS_ID_SIZE);
763 + spin_unlock(&busid_table_lock);
767 + spin_unlock(&busid_table_lock);
772 +static int del_match_busid(char *busid)
776 + spin_lock(&busid_table_lock);
778 + for (i = 0; i < MAX_BUSID; i++)
779 + if (!strncmp(busid_table[i], busid, BUS_ID_SIZE)) {
781 + memset(busid_table[i], 0, BUS_ID_SIZE);
782 + spin_unlock(&busid_table_lock);
786 + spin_unlock(&busid_table_lock);
791 +static ssize_t store_match_busid(struct device_driver *dev, const char *buf,
795 + char busid[BUS_ID_SIZE];
800 + /* strnlen() does not include \0 */
801 + len = strnlen(buf + 4, BUS_ID_SIZE);
803 + /* busid needs to include \0 termination */
804 + if (!(len < BUS_ID_SIZE))
807 + strncpy(busid, buf + 4, BUS_ID_SIZE);
810 + if (!strncmp(buf, "add ", 4)) {
811 + if (add_match_busid(busid) < 0)
814 + udbg("add busid %s\n", busid);
817 + } else if (!strncmp(buf, "del ", 4)) {
818 + if (del_match_busid(busid) < 0)
821 + udbg("del busid %s\n", busid);
828 +static DRIVER_ATTR(match_busid, S_IRUSR|S_IWUSR, show_match_busid,
829 + store_match_busid);
833 +/*-------------------------------------------------------------------------*/
835 +/* Cleanup functions used to free private data */
837 +static struct stub_priv *stub_priv_pop_from_listhead(struct list_head *listhead)
839 + struct stub_priv *priv, *tmp;
841 + list_for_each_entry_safe(priv, tmp, listhead, list) {
842 + list_del(&priv->list);
849 +static struct stub_priv *stub_priv_pop(struct stub_device *sdev)
851 + unsigned long flags;
852 + struct stub_priv *priv;
854 + spin_lock_irqsave(&sdev->priv_lock, flags);
856 + priv = stub_priv_pop_from_listhead(&sdev->priv_init);
858 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
862 + priv = stub_priv_pop_from_listhead(&sdev->priv_tx);
864 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
868 + priv = stub_priv_pop_from_listhead(&sdev->priv_free);
870 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
874 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
878 +void stub_device_cleanup_urbs(struct stub_device *sdev)
880 + struct stub_priv *priv;
882 + udbg("free sdev %p\n", sdev);
884 + while ((priv = stub_priv_pop(sdev))) {
885 + struct urb *urb = priv->urb;
887 + udbg(" free urb %p\n", urb);
890 + kmem_cache_free(stub_priv_cache, priv);
892 + if (urb->transfer_buffer != NULL)
893 + kfree(urb->transfer_buffer);
895 + if (urb->setup_packet != NULL)
896 + kfree(urb->setup_packet);
903 +/*-------------------------------------------------------------------------*/
905 +static int __init usb_stub_init(void)
909 + stub_priv_cache = kmem_cache_create("stub_priv",
910 + sizeof(struct stub_priv), 0,
911 + SLAB_HWCACHE_ALIGN, NULL);
913 + if (!stub_priv_cache) {
914 + printk(KERN_ERR KBUILD_MODNAME
915 + ": create stub_priv_cache error\n");
919 + ret = usb_register(&stub_driver);
921 + printk(KERN_ERR KBUILD_MODNAME ": usb_register failed %d\n",
923 + goto error_usb_register;
926 + printk(KERN_INFO KBUILD_MODNAME ":"
927 + DRIVER_DESC ":" DRIVER_VERSION "\n");
929 + memset(busid_table, 0, sizeof(busid_table));
930 + spin_lock_init(&busid_table_lock);
932 + ret = driver_create_file(&stub_driver.drvwrap.driver,
933 + &driver_attr_match_busid);
936 + printk(KERN_ERR KBUILD_MODNAME ": create driver sysfs\n");
937 + goto error_create_file;
942 + usb_deregister(&stub_driver);
944 + kmem_cache_destroy(stub_priv_cache);
948 +static void __exit usb_stub_exit(void)
950 + driver_remove_file(&stub_driver.drvwrap.driver,
951 + &driver_attr_match_busid);
954 + * deregister() calls stub_disconnect() for all devices. Device
955 + * specific data is cleared in stub_disconnect().
957 + usb_deregister(&stub_driver);
959 + kmem_cache_destroy(stub_priv_cache);
962 +module_init(usb_stub_init);
963 +module_exit(usb_stub_exit);
965 +MODULE_AUTHOR(DRIVER_AUTHOR);
966 +MODULE_DESCRIPTION(DRIVER_DESC);
967 +MODULE_LICENSE("GPL");
969 +++ kernel-2.6.28/drivers/usb/usbip/stub_rx.c
972 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
974 + * This is free software; you can redistribute it and/or modify
975 + * it under the terms of the GNU General Public License as published by
976 + * the Free Software Foundation; either version 2 of the License, or
977 + * (at your option) any later version.
979 + * This is distributed in the hope that it will be useful,
980 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
981 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
982 + * GNU General Public License for more details.
984 + * You should have received a copy of the GNU General Public License
985 + * along with this program; if not, write to the Free Software
986 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
990 +#include "usbip_common.h"
992 +#include "../../usb/core/hcd.h"
995 +static int is_clear_halt_cmd(struct urb *urb)
997 + struct usb_ctrlrequest *req;
999 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1001 + return (req->bRequest == USB_REQ_CLEAR_FEATURE) &&
1002 + (req->bRequestType == USB_RECIP_ENDPOINT) &&
1003 + (req->wValue == USB_ENDPOINT_HALT);
1006 +static int is_set_interface_cmd(struct urb *urb)
1008 + struct usb_ctrlrequest *req;
1010 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1012 + return (req->bRequest == USB_REQ_SET_INTERFACE) &&
1013 + (req->bRequestType == USB_RECIP_INTERFACE);
1016 +static int is_set_configuration_cmd(struct urb *urb)
1018 + struct usb_ctrlrequest *req;
1020 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1022 + return (req->bRequest == USB_REQ_SET_CONFIGURATION) &&
1023 + (req->bRequestType == USB_RECIP_DEVICE);
1026 +static int is_reset_device_cmd(struct urb *urb)
1028 + struct usb_ctrlrequest *req;
1032 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1033 + value = le16_to_cpu(req->wValue);
1034 + index = le16_to_cpu(req->wIndex);
1036 + if ((req->bRequest == USB_REQ_SET_FEATURE) &&
1037 + (req->bRequestType == USB_RT_PORT) &&
1038 + (value = USB_PORT_FEAT_RESET)) {
1039 + dbg_stub_rx("reset_device_cmd, port %u\n", index);
1045 +static int tweak_clear_halt_cmd(struct urb *urb)
1047 + struct usb_ctrlrequest *req;
1053 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1056 + * The stalled endpoint is specified in the wIndex value. The endpoint
1057 + * of the urb is the target of this clear_halt request (i.e., control
1060 + target_endp = le16_to_cpu(req->wIndex) & 0x000f;
1062 + /* the stalled endpoint direction is IN or OUT?. USB_DIR_IN is 0x80. */
1063 + target_dir = le16_to_cpu(req->wIndex) & 0x0080;
1066 + target_pipe = usb_rcvctrlpipe(urb->dev, target_endp);
1068 + target_pipe = usb_sndctrlpipe(urb->dev, target_endp);
1070 + ret = usb_clear_halt(urb->dev, target_pipe);
1072 + uinfo("clear_halt error: devnum %d endp %d, %d\n",
1073 + urb->dev->devnum, target_endp, ret);
1075 + uinfo("clear_halt done: devnum %d endp %d\n",
1076 + urb->dev->devnum, target_endp);
1081 +static int tweak_set_interface_cmd(struct urb *urb)
1083 + struct usb_ctrlrequest *req;
1088 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1089 + alternate = le16_to_cpu(req->wValue);
1090 + interface = le16_to_cpu(req->wIndex);
1092 + dbg_stub_rx("set_interface: inf %u alt %u\n", interface, alternate);
1094 + ret = usb_set_interface(urb->dev, interface, alternate);
1096 + uinfo("set_interface error: inf %u alt %u, %d\n",
1097 + interface, alternate, ret);
1099 + uinfo("set_interface done: inf %u alt %u\n",
1106 +static int tweak_set_configuration_cmd(struct urb *urb)
1108 + struct usb_ctrlrequest *req;
1111 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1112 + config = le16_to_cpu(req->wValue);
1115 + * I have never seen a multi-config device. Very rare.
1116 + * For most devices, this will be called to choose a default
1117 + * configuration only once in an initialization phase.
1119 + * set_configuration may change a device configuration and its device
1120 + * drivers will be unbound and assigned for a new device configuration.
1121 + * This means this usbip driver will be also unbound when called, then
1122 + * eventually reassigned to the device as far as driver matching
1123 + * condition is kept.
1125 + * Unfortunatelly, an existing usbip connection will be dropped
1126 + * due to this driver unbinding. So, skip here.
1127 + * A user may need to set a special configuration value before
1128 + * exporting the device.
1130 + uinfo("set_configuration (%d) to %s\n", config, urb->dev->dev.bus_id);
1131 + uinfo("but, skip!\n");
1134 + /* return usb_driver_set_configuration(urb->dev, config); */
1137 +static int tweak_reset_device_cmd(struct urb *urb)
1139 + struct usb_ctrlrequest *req;
1144 + req = (struct usb_ctrlrequest *) urb->setup_packet;
1145 + value = le16_to_cpu(req->wValue);
1146 + index = le16_to_cpu(req->wIndex);
1148 + uinfo("reset_device (port %d) to %s\n", index, urb->dev->dev.bus_id);
1150 + /* all interfaces should be owned by usbip driver, so just reset it. */
1151 + ret = usb_lock_device_for_reset(urb->dev, NULL);
1153 + dev_err(&urb->dev->dev, "lock for reset\n");
1157 + /* try to reset the device */
1158 + ret = usb_reset_device(urb->dev);
1160 + dev_err(&urb->dev->dev, "device reset\n");
1162 + usb_unlock_device(urb->dev);
1168 + * clear_halt, set_interface, and set_configuration require special tricks.
1170 +static void tweak_special_requests(struct urb *urb)
1172 + if (!urb || !urb->setup_packet)
1175 + if (usb_pipetype(urb->pipe) != PIPE_CONTROL)
1178 + if (is_clear_halt_cmd(urb))
1179 + /* tweak clear_halt */
1180 + tweak_clear_halt_cmd(urb);
1182 + else if (is_set_interface_cmd(urb))
1183 + /* tweak set_interface */
1184 + tweak_set_interface_cmd(urb);
1186 + else if (is_set_configuration_cmd(urb))
1187 + /* tweak set_configuration */
1188 + tweak_set_configuration_cmd(urb);
1190 + else if (is_reset_device_cmd(urb))
1191 + tweak_reset_device_cmd(urb);
1193 + dbg_stub_rx("no need to tweak\n");
1197 + * stub_recv_unlink() unlinks the URB by a call to usb_unlink_urb().
1198 + * By unlinking the urb asynchronously, stub_rx can continuously
1199 + * process coming urbs. Even if the urb is unlinked, its completion
1200 + * handler will be called and stub_tx will send a return pdu.
1202 + * See also comments about unlinking strategy in vhci_hcd.c.
1204 +static int stub_recv_cmd_unlink(struct stub_device *sdev,
1205 + struct usbip_header *pdu)
1207 + struct list_head *listhead = &sdev->priv_init;
1208 + struct list_head *ptr;
1209 + unsigned long flags;
1211 + struct stub_priv *priv;
1214 + spin_lock_irqsave(&sdev->priv_lock, flags);
1216 + for (ptr = listhead->next; ptr != listhead; ptr = ptr->next) {
1217 + priv = list_entry(ptr, struct stub_priv, list);
1218 + if (priv->seqnum == pdu->u.cmd_unlink.seqnum) {
1221 + dev_info(&priv->urb->dev->dev, "unlink urb %p\n",
1225 + * This matched urb is not completed yet (i.e., be in
1226 + * flight in usb hcd hardware/driver). Now we are
1227 + * cancelling it. The unlinking flag means that we are
1228 + * now not going to return the normal result pdu of a
1229 + * submission request, but going to return a result pdu
1230 + * of the unlink request.
1232 + priv->unlinking = 1;
1235 + * In the case that unlinking flag is on, prev->seqnum
1236 + * is changed from the seqnum of the cancelling urb to
1237 + * the seqnum of the unlink request. This will be used
1238 + * to make the result pdu of the unlink request.
1240 + priv->seqnum = pdu->base.seqnum;
1242 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1245 + * usb_unlink_urb() is now out of spinlocking to avoid
1246 + * spinlock recursion since stub_complete() is
1247 + * sometimes called in this context but not in the
1248 + * interrupt context. If stub_complete() is executed
1249 + * before we call usb_unlink_urb(), usb_unlink_urb()
1250 + * will return an error value. In this case, stub_tx
1251 + * will return the result pdu of this unlink request
1252 + * though submission is completed and actual unlinking
1253 + * is not executed. OK?
1255 + /* In the above case, urb->status is not -ECONNRESET,
1256 + * so a driver in a client host will know the failure
1257 + * of the unlink request ?
1259 + ret = usb_unlink_urb(priv->urb);
1260 + if (ret != -EINPROGRESS)
1261 + dev_err(&priv->urb->dev->dev,
1262 + "failed to unlink a urb %p, ret %d\n",
1268 + dbg_stub_rx("seqnum %d is not pending\n", pdu->u.cmd_unlink.seqnum);
1271 + * The urb of the unlink target is not found in priv_init queue. It was
1272 + * already completed and its results is/was going to be sent by a
1273 + * CMD_RET pdu. In this case, usb_unlink_urb() is not needed. We only
1274 + * return the completeness of this unlink request to vhci_hcd.
1276 + stub_enqueue_ret_unlink(sdev, pdu->base.seqnum, 0);
1278 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1284 +static int valid_request(struct stub_device *sdev, struct usbip_header *pdu)
1286 + struct usbip_device *ud = &sdev->ud;
1288 + if (pdu->base.devid == sdev->devid) {
1289 + spin_lock(&ud->lock);
1290 + if (ud->status == SDEV_ST_USED) {
1291 + /* A request is valid. */
1292 + spin_unlock(&ud->lock);
1295 + spin_unlock(&ud->lock);
1301 +static struct stub_priv *stub_priv_alloc(struct stub_device *sdev,
1302 + struct usbip_header *pdu)
1304 + struct stub_priv *priv;
1305 + struct usbip_device *ud = &sdev->ud;
1306 + unsigned long flags;
1308 + spin_lock_irqsave(&sdev->priv_lock, flags);
1310 + priv = kmem_cache_alloc(stub_priv_cache, GFP_ATOMIC);
1312 + dev_err(&sdev->interface->dev, "alloc stub_priv\n");
1313 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1314 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1318 + memset(priv, 0, sizeof(struct stub_priv));
1320 + priv->seqnum = pdu->base.seqnum;
1321 + priv->sdev = sdev;
1324 + * After a stub_priv is linked to a list_head,
1325 + * our error handler can free allocated data.
1327 + list_add_tail(&priv->list, &sdev->priv_init);
1329 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1335 +static struct usb_host_endpoint *get_ep_from_epnum(struct usb_device *udev,
1338 + struct usb_host_config *config;
1340 + struct usb_host_endpoint *ep = NULL;
1345 + return &udev->ep0;
1347 + config = udev->actconfig;
1351 + for (i = 0; i < config->desc.bNumInterfaces; i++) {
1352 + struct usb_host_interface *setting;
1354 + setting = config->interface[i]->cur_altsetting;
1356 + for (j = 0; j < setting->desc.bNumEndpoints; j++) {
1357 + ep = &setting->endpoint[j];
1358 + epnum = (ep->desc.bEndpointAddress & 0x7f);
1360 + if (epnum == epnum0) {
1361 + /* uinfo("found epnum %d\n", epnum0); */
1375 +static int get_pipe(struct stub_device *sdev, int epnum, int dir)
1377 + struct usb_device *udev = interface_to_usbdev(sdev->interface);
1378 + struct usb_host_endpoint *ep;
1379 + struct usb_endpoint_descriptor *epd = NULL;
1381 + ep = get_ep_from_epnum(udev, epnum);
1383 + dev_err(&sdev->interface->dev, "no such endpoint?, %d\n",
1392 + /* epnum 0 is always control */
1394 + if (dir == USBIP_DIR_OUT)
1395 + return usb_sndctrlpipe(udev, 0);
1397 + return usb_rcvctrlpipe(udev, 0);
1401 + if (usb_endpoint_xfer_control(epd)) {
1402 + if (dir == USBIP_DIR_OUT)
1403 + return usb_sndctrlpipe(udev, epnum);
1405 + return usb_rcvctrlpipe(udev, epnum);
1408 + if (usb_endpoint_xfer_bulk(epd)) {
1409 + if (dir == USBIP_DIR_OUT)
1410 + return usb_sndbulkpipe(udev, epnum);
1412 + return usb_rcvbulkpipe(udev, epnum);
1415 + if (usb_endpoint_xfer_int(epd)) {
1416 + if (dir == USBIP_DIR_OUT)
1417 + return usb_sndintpipe(udev, epnum);
1419 + return usb_rcvintpipe(udev, epnum);
1422 + if (usb_endpoint_xfer_isoc(epd)) {
1423 + if (dir == USBIP_DIR_OUT)
1424 + return usb_sndisocpipe(udev, epnum);
1426 + return usb_rcvisocpipe(udev, epnum);
1430 + dev_err(&sdev->interface->dev, "get pipe, epnum %d\n", epnum);
1434 +static void stub_recv_cmd_submit(struct stub_device *sdev,
1435 + struct usbip_header *pdu)
1438 + struct stub_priv *priv;
1439 + struct usbip_device *ud = &sdev->ud;
1440 + struct usb_device *udev = interface_to_usbdev(sdev->interface);
1441 + int pipe = get_pipe(sdev, pdu->base.ep, pdu->base.direction);
1444 + priv = stub_priv_alloc(sdev, pdu);
1449 + if (usb_pipeisoc(pipe))
1450 + priv->urb = usb_alloc_urb(pdu->u.cmd_submit.number_of_packets,
1453 + priv->urb = usb_alloc_urb(0, GFP_KERNEL);
1456 + dev_err(&sdev->interface->dev, "malloc urb\n");
1457 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1461 + /* set priv->urb->transfer_buffer */
1462 + if (pdu->u.cmd_submit.transfer_buffer_length > 0) {
1463 + priv->urb->transfer_buffer =
1464 + kzalloc(pdu->u.cmd_submit.transfer_buffer_length,
1466 + if (!priv->urb->transfer_buffer) {
1467 + dev_err(&sdev->interface->dev, "malloc x_buff\n");
1468 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1473 + /* set priv->urb->setup_packet */
1474 + priv->urb->setup_packet = kzalloc(8, GFP_KERNEL);
1475 + if (!priv->urb->setup_packet) {
1476 + dev_err(&sdev->interface->dev, "allocate setup_packet\n");
1477 + usbip_event_add(ud, SDEV_EVENT_ERROR_MALLOC);
1480 + memcpy(priv->urb->setup_packet, &pdu->u.cmd_submit.setup, 8);
1482 + /* set other members from the base header of pdu */
1483 + priv->urb->context = (void *) priv;
1484 + priv->urb->dev = udev;
1485 + priv->urb->pipe = pipe;
1486 + priv->urb->complete = stub_complete;
1488 + usbip_pack_pdu(pdu, priv->urb, USBIP_CMD_SUBMIT, 0);
1491 + if (usbip_recv_xbuff(ud, priv->urb) < 0)
1494 + if (usbip_recv_iso(ud, priv->urb) < 0)
1497 + /* no need to submit an intercepted request, but harmless? */
1498 + tweak_special_requests(priv->urb);
1500 + /* urb is now ready to submit */
1501 + ret = usb_submit_urb(priv->urb, GFP_KERNEL);
1504 + dbg_stub_rx("submit urb ok, seqnum %u\n", pdu->base.seqnum);
1506 + dev_err(&sdev->interface->dev, "submit_urb error, %d\n", ret);
1507 + usbip_dump_header(pdu);
1508 + usbip_dump_urb(priv->urb);
1512 + * This connection will be discarded.
1514 + usbip_event_add(ud, SDEV_EVENT_ERROR_SUBMIT);
1517 + dbg_stub_rx("Leave\n");
1522 +static void stub_rx_pdu(struct usbip_device *ud)
1525 + struct usbip_header pdu;
1526 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1527 + struct device *dev = &sdev->interface->dev;
1529 + dbg_stub_rx("Enter\n");
1531 + memset(&pdu, 0, sizeof(pdu));
1533 + /* 1. receive a pdu header */
1534 + ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
1535 + if (ret != sizeof(pdu)) {
1536 + dev_err(dev, "recv a header, %d\n", ret);
1537 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1541 + usbip_header_correct_endian(&pdu, 0);
1543 + if (dbg_flag_stub_rx)
1544 + usbip_dump_header(&pdu);
1546 + if (!valid_request(sdev, &pdu)) {
1547 + dev_err(dev, "recv invalid request\n");
1548 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1552 + switch (pdu.base.command) {
1553 + case USBIP_CMD_UNLINK:
1554 + stub_recv_cmd_unlink(sdev, &pdu);
1557 + case USBIP_CMD_SUBMIT:
1558 + stub_recv_cmd_submit(sdev, &pdu);
1563 + dev_err(dev, "unknown pdu\n");
1564 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
1570 +void stub_rx_loop(struct usbip_task *ut)
1572 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
1575 + if (signal_pending(current)) {
1576 + dbg_stub_rx("signal caught!\n");
1580 + if (usbip_event_happend(ud))
1587 +++ kernel-2.6.28/drivers/usb/usbip/stub_tx.c
1590 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1592 + * This is free software; you can redistribute it and/or modify
1593 + * it under the terms of the GNU General Public License as published by
1594 + * the Free Software Foundation; either version 2 of the License, or
1595 + * (at your option) any later version.
1597 + * This is distributed in the hope that it will be useful,
1598 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1599 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1600 + * GNU General Public License for more details.
1602 + * You should have received a copy of the GNU General Public License
1603 + * along with this program; if not, write to the Free Software
1604 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1608 +#include "usbip_common.h"
1612 +static void stub_free_priv_and_urb(struct stub_priv *priv)
1614 + struct urb *urb = priv->urb;
1616 + kfree(urb->setup_packet);
1617 + kfree(urb->transfer_buffer);
1618 + list_del(&priv->list);
1619 + kmem_cache_free(stub_priv_cache, priv);
1620 + usb_free_urb(urb);
1623 +/* be in spin_lock_irqsave(&sdev->priv_lock, flags) */
1624 +void stub_enqueue_ret_unlink(struct stub_device *sdev, __u32 seqnum,
1627 + struct stub_unlink *unlink;
1629 + unlink = kzalloc(sizeof(struct stub_unlink), GFP_ATOMIC);
1631 + dev_err(&sdev->interface->dev, "alloc stub_unlink\n");
1632 + usbip_event_add(&sdev->ud, VDEV_EVENT_ERROR_MALLOC);
1636 + unlink->seqnum = seqnum;
1637 + unlink->status = status;
1639 + list_add_tail(&unlink->list, &sdev->unlink_tx);
1643 + * stub_complete - completion handler of a usbip urb
1644 + * @urb: pointer to the urb completed
1647 + * When a urb has completed, the USB core driver calls this function mostly in
1648 + * the interrupt context. To return the result of a urb, the completed urb is
1649 + * linked to the pending list of returning.
1652 +void stub_complete(struct urb *urb)
1654 + struct stub_priv *priv = (struct stub_priv *) urb->context;
1655 + struct stub_device *sdev = priv->sdev;
1656 + unsigned long flags;
1658 + dbg_stub_tx("complete! status %d\n", urb->status);
1661 + switch (urb->status) {
1666 + uinfo("stopped by a call of usb_kill_urb() because of"
1667 + "cleaning up a virtual connection\n");
1670 + uinfo("unlinked by a call of usb_unlink_urb()\n");
1673 + uinfo("endpoint %d is stalled\n", usb_pipeendpoint(urb->pipe));
1676 + uinfo("device removed?\n");
1679 + uinfo("urb completion with non-zero status %d\n", urb->status);
1682 + /* link a urb to the queue of tx. */
1683 + spin_lock_irqsave(&sdev->priv_lock, flags);
1685 + if (priv->unlinking) {
1686 + stub_enqueue_ret_unlink(sdev, priv->seqnum, urb->status);
1687 + stub_free_priv_and_urb(priv);
1689 + list_move_tail(&priv->list, &sdev->priv_tx);
1692 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1694 + /* wake up tx_thread */
1695 + wake_up(&sdev->tx_waitq);
1699 +/*-------------------------------------------------------------------------*/
1702 +static inline void setup_base_pdu(struct usbip_header_basic *base,
1703 + __u32 command, __u32 seqnum)
1705 + base->command = command;
1706 + base->seqnum = seqnum;
1709 + base->direction = 0;
1712 +static void setup_ret_submit_pdu(struct usbip_header *rpdu, struct urb *urb)
1714 + struct stub_priv *priv = (struct stub_priv *) urb->context;
1716 + setup_base_pdu(&rpdu->base, USBIP_RET_SUBMIT, priv->seqnum);
1718 + usbip_pack_pdu(rpdu, urb, USBIP_RET_SUBMIT, 1);
1721 +static void setup_ret_unlink_pdu(struct usbip_header *rpdu,
1722 + struct stub_unlink *unlink)
1724 + setup_base_pdu(&rpdu->base, USBIP_RET_UNLINK, unlink->seqnum);
1726 + rpdu->u.ret_unlink.status = unlink->status;
1730 +/*-------------------------------------------------------------------------*/
1731 +/* send RET_SUBMIT */
1733 +static struct stub_priv *dequeue_from_priv_tx(struct stub_device *sdev)
1735 + unsigned long flags;
1736 + struct stub_priv *priv, *tmp;
1738 + spin_lock_irqsave(&sdev->priv_lock, flags);
1740 + list_for_each_entry_safe(priv, tmp, &sdev->priv_tx, list) {
1741 + list_move_tail(&priv->list, &sdev->priv_free);
1742 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1746 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1751 +static int stub_send_ret_submit(struct stub_device *sdev)
1753 + unsigned long flags;
1754 + struct stub_priv *priv, *tmp;
1756 + struct msghdr msg;
1757 + struct kvec iov[3];
1760 + size_t total_size = 0;
1762 + while ((priv = dequeue_from_priv_tx(sdev)) != NULL) {
1764 + struct urb *urb = priv->urb;
1765 + struct usbip_header pdu_header;
1766 + void *iso_buffer = NULL;
1769 + memset(&pdu_header, 0, sizeof(pdu_header));
1770 + memset(&msg, 0, sizeof(msg));
1771 + memset(&iov, 0, sizeof(iov));
1773 + dbg_stub_tx("setup txdata urb %p\n", urb);
1776 + /* 1. setup usbip_header */
1777 + setup_ret_submit_pdu(&pdu_header, urb);
1778 + usbip_header_correct_endian(&pdu_header, 1);
1780 + iov[0].iov_base = &pdu_header;
1781 + iov[0].iov_len = sizeof(pdu_header);
1782 + txsize += sizeof(pdu_header);
1784 + /* 2. setup transfer buffer */
1785 + if (usb_pipein(urb->pipe) && urb->actual_length > 0) {
1786 + iov[1].iov_base = urb->transfer_buffer;
1787 + iov[1].iov_len = urb->actual_length;
1788 + txsize += urb->actual_length;
1791 + /* 3. setup iso_packet_descriptor */
1792 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
1795 + iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
1796 + if (!iso_buffer) {
1797 + usbip_event_add(&sdev->ud,
1798 + SDEV_EVENT_ERROR_MALLOC);
1802 + iov[2].iov_base = iso_buffer;
1803 + iov[2].iov_len = len;
1807 + ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1809 + if (ret != txsize) {
1810 + dev_err(&sdev->interface->dev,
1811 + "sendmsg failed!, retval %d for %zd\n",
1813 + kfree(iso_buffer);
1814 + usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1818 + kfree(iso_buffer);
1819 + dbg_stub_tx("send txdata\n");
1821 + total_size += txsize;
1825 + spin_lock_irqsave(&sdev->priv_lock, flags);
1827 + list_for_each_entry_safe(priv, tmp, &sdev->priv_free, list) {
1828 + stub_free_priv_and_urb(priv);
1831 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1833 + return total_size;
1837 +/*-------------------------------------------------------------------------*/
1838 +/* send RET_UNLINK */
1840 +static struct stub_unlink *dequeue_from_unlink_tx(struct stub_device *sdev)
1842 + unsigned long flags;
1843 + struct stub_unlink *unlink, *tmp;
1845 + spin_lock_irqsave(&sdev->priv_lock, flags);
1847 + list_for_each_entry_safe(unlink, tmp, &sdev->unlink_tx, list) {
1848 + list_move_tail(&unlink->list, &sdev->unlink_free);
1849 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1853 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1859 +static int stub_send_ret_unlink(struct stub_device *sdev)
1861 + unsigned long flags;
1862 + struct stub_unlink *unlink, *tmp;
1864 + struct msghdr msg;
1865 + struct kvec iov[1];
1868 + size_t total_size = 0;
1870 + while ((unlink = dequeue_from_unlink_tx(sdev)) != NULL) {
1872 + struct usbip_header pdu_header;
1875 + memset(&pdu_header, 0, sizeof(pdu_header));
1876 + memset(&msg, 0, sizeof(msg));
1877 + memset(&iov, 0, sizeof(iov));
1879 + dbg_stub_tx("setup ret unlink %lu\n", unlink->seqnum);
1881 + /* 1. setup usbip_header */
1882 + setup_ret_unlink_pdu(&pdu_header, unlink);
1883 + usbip_header_correct_endian(&pdu_header, 1);
1885 + iov[0].iov_base = &pdu_header;
1886 + iov[0].iov_len = sizeof(pdu_header);
1887 + txsize += sizeof(pdu_header);
1889 + ret = kernel_sendmsg(sdev->ud.tcp_socket, &msg, iov,
1891 + if (ret != txsize) {
1892 + dev_err(&sdev->interface->dev,
1893 + "sendmsg failed!, retval %d for %zd\n",
1895 + usbip_event_add(&sdev->ud, SDEV_EVENT_ERROR_TCP);
1900 + dbg_stub_tx("send txdata\n");
1902 + total_size += txsize;
1906 + spin_lock_irqsave(&sdev->priv_lock, flags);
1908 + list_for_each_entry_safe(unlink, tmp, &sdev->unlink_free, list) {
1909 + list_del(&unlink->list);
1913 + spin_unlock_irqrestore(&sdev->priv_lock, flags);
1915 + return total_size;
1919 +/*-------------------------------------------------------------------------*/
1921 +void stub_tx_loop(struct usbip_task *ut)
1923 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
1924 + struct stub_device *sdev = container_of(ud, struct stub_device, ud);
1927 + if (signal_pending(current)) {
1928 + dbg_stub_tx("signal catched\n");
1932 + if (usbip_event_happend(ud))
1936 + * send_ret_submit comes earlier than send_ret_unlink. stub_rx
1937 + * looks at only priv_init queue. If the completion of a URB is
1938 + * earlier than the receive of CMD_UNLINK, priv is moved to
1939 + * priv_tx queue and stub_rx does not find the target priv. In
1940 + * this case, vhci_rx receives the result of the submit request
1941 + * and then receives the result of the unlink request. The
1942 + * result of the submit is given back to the usbcore as the
1943 + * completion of the unlink request. The request of the
1944 + * unlink is ignored. This is ok because a driver who calls
1945 + * usb_unlink_urb() understands the unlink was too late by
1946 + * getting the status of the given-backed URB which has the
1947 + * status of usb_submit_urb().
1949 + if (stub_send_ret_submit(sdev) < 0)
1952 + if (stub_send_ret_unlink(sdev) < 0)
1955 + wait_event_interruptible(sdev->tx_waitq,
1956 + (!list_empty(&sdev->priv_tx) ||
1957 + !list_empty(&sdev->unlink_tx)));
1961 +++ kernel-2.6.28/drivers/usb/usbip/usbip_common.c
1964 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1966 + * This is free software; you can redistribute it and/or modify
1967 + * it under the terms of the GNU General Public License as published by
1968 + * the Free Software Foundation; either version 2 of the License, or
1969 + * (at your option) any later version.
1971 + * This is distributed in the hope that it will be useful,
1972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1974 + * GNU General Public License for more details.
1976 + * You should have received a copy of the GNU General Public License
1977 + * along with this program; if not, write to the Free Software
1978 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1982 +#include <linux/kernel.h>
1983 +#include <linux/file.h>
1984 +#include <linux/tcp.h>
1985 +#include <linux/in.h>
1986 +#include "usbip_common.h"
1988 +/* version information */
1989 +#define DRIVER_VERSION "1.0"
1990 +#define DRIVER_AUTHOR "Takahiro Hirofuchi <hirofuchi _at_ users.sourceforge.net>"
1991 +#define DRIVER_DESC "usbip common driver"
1993 +/*-------------------------------------------------------------------------*/
1994 +/* debug routines */
1996 +#ifdef CONFIG_USB_DEBUG
1997 +unsigned long usbip_debug_flag = 0xffffffff;
1999 +unsigned long usbip_debug_flag;
2001 +EXPORT_SYMBOL_GPL(usbip_debug_flag);
2005 +struct device_attribute dev_attr_usbip_debug;
2006 +EXPORT_SYMBOL_GPL(dev_attr_usbip_debug);
2009 +static ssize_t show_flag(struct device *dev, struct device_attribute *attr,
2012 + return sprintf(buf, "%lx\n", usbip_debug_flag);
2015 +static ssize_t store_flag(struct device *dev, struct device_attribute *attr,
2016 + const char *buf, size_t count)
2018 + unsigned long flag;
2020 + sscanf(buf, "%lx", &flag);
2021 + usbip_debug_flag = flag;
2025 +DEVICE_ATTR(usbip_debug, (S_IRUGO | S_IWUSR), show_flag, store_flag);
2027 +static void usbip_dump_buffer(char *buff, int bufflen)
2031 + if (bufflen > 128) {
2032 + for (i = 0; i < 128; i++) {
2035 + printk("%02x ", (unsigned char) buff[i]);
2041 + printk("... (%d byte)\n", bufflen);
2045 + for (i = 0; i < bufflen; i++) {
2048 + printk("%02x ", (unsigned char) buff[i]);
2058 +static void usbip_dump_pipe(unsigned int p)
2060 + unsigned char type = usb_pipetype(p);
2061 + unsigned char ep = usb_pipeendpoint(p);
2062 + unsigned char dev = usb_pipedevice(p);
2063 + unsigned char dir = usb_pipein(p);
2065 + printk("dev(%d) ", dev);
2066 + printk("ep(%d) ", ep);
2067 + printk("%s ", dir ? "IN" : "OUT");
2070 + case PIPE_ISOCHRONOUS:
2071 + printk("%s ", "ISO");
2073 + case PIPE_INTERRUPT:
2074 + printk("%s ", "INT");
2076 + case PIPE_CONTROL:
2077 + printk("%s ", "CTL");
2080 + printk("%s ", "BLK");
2090 +static void usbip_dump_usb_device(struct usb_device *udev)
2092 + struct device *dev = &udev->dev;
2095 + dev_dbg(dev, " devnum(%d) devpath(%s)",
2096 + udev->devnum, udev->devpath);
2098 + switch (udev->speed) {
2099 + case USB_SPEED_HIGH:
2100 + printk(" SPD_HIGH");
2102 + case USB_SPEED_FULL:
2103 + printk(" SPD_FULL");
2105 + case USB_SPEED_LOW:
2106 + printk(" SPD_LOW");
2108 + case USB_SPEED_UNKNOWN:
2109 + printk(" SPD_UNKNOWN");
2112 + printk(" SPD_ERROR");
2115 + printk(" tt %p, ttport %d", udev->tt, udev->ttport);
2118 + dev_dbg(dev, " ");
2119 + for (i = 0; i < 16; i++)
2120 + printk(" %2u", i);
2123 + dev_dbg(dev, " toggle0(IN) :");
2124 + for (i = 0; i < 16; i++)
2125 + printk(" %2u", (udev->toggle[0] & (1 << i)) ? 1 : 0);
2128 + dev_dbg(dev, " toggle1(OUT):");
2129 + for (i = 0; i < 16; i++)
2130 + printk(" %2u", (udev->toggle[1] & (1 << i)) ? 1 : 0);
2134 + dev_dbg(dev, " epmaxp_in :");
2135 + for (i = 0; i < 16; i++) {
2136 + if (udev->ep_in[i])
2138 + le16_to_cpu(udev->ep_in[i]->desc.wMaxPacketSize));
2142 + dev_dbg(dev, " epmaxp_out :");
2143 + for (i = 0; i < 16; i++) {
2144 + if (udev->ep_out[i])
2146 + le16_to_cpu(udev->ep_out[i]->desc.wMaxPacketSize));
2150 + dev_dbg(dev, "parent %p, bus %p\n", udev->parent, udev->bus);
2152 + dev_dbg(dev, "descriptor %p, config %p, actconfig %p, "
2153 + "rawdescriptors %p\n", &udev->descriptor, udev->config,
2154 + udev->actconfig, udev->rawdescriptors);
2156 + dev_dbg(dev, "have_langid %d, string_langid %d\n",
2157 + udev->have_langid, udev->string_langid);
2159 + dev_dbg(dev, "maxchild %d, children %p\n",
2160 + udev->maxchild, udev->children);
2163 +static void usbip_dump_request_type(__u8 rt)
2165 + switch (rt & USB_RECIP_MASK) {
2166 + case USB_RECIP_DEVICE:
2169 + case USB_RECIP_INTERFACE:
2172 + case USB_RECIP_ENDPOINT:
2175 + case USB_RECIP_OTHER:
2183 +static void usbip_dump_usb_ctrlrequest(struct usb_ctrlrequest *cmd)
2186 + printk(" %s : null pointer\n", __func__);
2191 + printk("bRequestType(%02X) ", cmd->bRequestType);
2192 + printk("bRequest(%02X) " , cmd->bRequest);
2193 + printk("wValue(%04X) ", cmd->wValue);
2194 + printk("wIndex(%04X) ", cmd->wIndex);
2195 + printk("wLength(%04X) ", cmd->wLength);
2199 + if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
2200 + printk("STANDARD ");
2201 + switch (cmd->bRequest) {
2202 + case USB_REQ_GET_STATUS:
2203 + printk("GET_STATUS");
2205 + case USB_REQ_CLEAR_FEATURE:
2206 + printk("CLEAR_FEAT");
2208 + case USB_REQ_SET_FEATURE:
2209 + printk("SET_FEAT ");
2211 + case USB_REQ_SET_ADDRESS:
2212 + printk("SET_ADDRRS");
2214 + case USB_REQ_GET_DESCRIPTOR:
2215 + printk("GET_DESCRI");
2217 + case USB_REQ_SET_DESCRIPTOR:
2218 + printk("SET_DESCRI");
2220 + case USB_REQ_GET_CONFIGURATION:
2221 + printk("GET_CONFIG");
2223 + case USB_REQ_SET_CONFIGURATION:
2224 + printk("SET_CONFIG");
2226 + case USB_REQ_GET_INTERFACE:
2227 + printk("GET_INTERF");
2229 + case USB_REQ_SET_INTERFACE:
2230 + printk("SET_INTERF");
2232 + case USB_REQ_SYNCH_FRAME:
2233 + printk("SYNC_FRAME");
2236 + printk("REQ(%02X) ", cmd->bRequest);
2240 + usbip_dump_request_type(cmd->bRequestType);
2242 + } else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_CLASS)
2245 + else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_VENDOR)
2246 + printk("VENDOR ");
2248 + else if ((cmd->bRequestType & USB_TYPE_MASK) == USB_TYPE_RESERVED)
2249 + printk("RESERVED");
2254 +void usbip_dump_urb(struct urb *urb)
2256 + struct device *dev;
2259 + printk(KERN_DEBUG KBUILD_MODNAME
2260 + ":%s: urb: null pointer!!\n", __func__);
2265 + printk(KERN_DEBUG KBUILD_MODNAME
2266 + ":%s: urb->dev: null pointer!!\n", __func__);
2269 + dev = &urb->dev->dev;
2271 + dev_dbg(dev, " urb :%p\n", urb);
2272 + dev_dbg(dev, " dev :%p\n", urb->dev);
2274 + usbip_dump_usb_device(urb->dev);
2276 + dev_dbg(dev, " pipe :%08x ", urb->pipe);
2278 + usbip_dump_pipe(urb->pipe);
2280 + dev_dbg(dev, " status :%d\n", urb->status);
2281 + dev_dbg(dev, " transfer_flags :%08X\n", urb->transfer_flags);
2282 + dev_dbg(dev, " transfer_buffer :%p\n", urb->transfer_buffer);
2283 + dev_dbg(dev, " transfer_buffer_length:%d\n", urb->transfer_buffer_length);
2284 + dev_dbg(dev, " actual_length :%d\n", urb->actual_length);
2285 + dev_dbg(dev, " setup_packet :%p\n", urb->setup_packet);
2287 + if (urb->setup_packet && usb_pipetype(urb->pipe) == PIPE_CONTROL)
2288 + usbip_dump_usb_ctrlrequest(
2289 + (struct usb_ctrlrequest *)urb->setup_packet);
2291 + dev_dbg(dev, " start_frame :%d\n", urb->start_frame);
2292 + dev_dbg(dev, " number_of_packets :%d\n", urb->number_of_packets);
2293 + dev_dbg(dev, " interval :%d\n", urb->interval);
2294 + dev_dbg(dev, " error_count :%d\n", urb->error_count);
2295 + dev_dbg(dev, " context :%p\n", urb->context);
2296 + dev_dbg(dev, " complete :%p\n", urb->complete);
2298 +EXPORT_SYMBOL_GPL(usbip_dump_urb);
2300 +void usbip_dump_header(struct usbip_header *pdu)
2302 + udbg("BASE: cmd %u seq %u devid %u dir %u ep %u\n",
2303 + pdu->base.command,
2306 + pdu->base.direction,
2309 + switch (pdu->base.command) {
2310 + case USBIP_CMD_SUBMIT:
2311 + udbg("CMD_SUBMIT: x_flags %u x_len %u sf %u #p %u iv %u\n",
2312 + pdu->u.cmd_submit.transfer_flags,
2313 + pdu->u.cmd_submit.transfer_buffer_length,
2314 + pdu->u.cmd_submit.start_frame,
2315 + pdu->u.cmd_submit.number_of_packets,
2316 + pdu->u.cmd_submit.interval);
2318 + case USBIP_CMD_UNLINK:
2319 + udbg("CMD_UNLINK: seq %u\n", pdu->u.cmd_unlink.seqnum);
2321 + case USBIP_RET_SUBMIT:
2322 + udbg("RET_SUBMIT: st %d al %u sf %d ec %d\n",
2323 + pdu->u.ret_submit.status,
2324 + pdu->u.ret_submit.actual_length,
2325 + pdu->u.ret_submit.start_frame,
2326 + pdu->u.ret_submit.error_count);
2327 + case USBIP_RET_UNLINK:
2328 + udbg("RET_UNLINK: status %d\n", pdu->u.ret_unlink.status);
2332 + udbg("UNKNOWN\n");
2335 +EXPORT_SYMBOL_GPL(usbip_dump_header);
2338 +/*-------------------------------------------------------------------------*/
2339 +/* thread routines */
2341 +int usbip_thread(void *param)
2343 + struct usbip_task *ut = param;
2349 + daemonize(ut->name);
2350 + allow_signal(SIGKILL);
2351 + ut->thread = current;
2354 + /* srv.rb must wait for rx_thread starting */
2355 + complete(&ut->thread_done);
2357 + /* start of while loop */
2361 + ut->thread = NULL;
2363 + complete_and_exit(&ut->thread_done, 0);
2366 +void usbip_start_threads(struct usbip_device *ud)
2369 + * threads are invoked per one device (per one connection).
2371 + kernel_thread(usbip_thread, (void *)&ud->tcp_rx, 0);
2372 + kernel_thread(usbip_thread, (void *)&ud->tcp_tx, 0);
2374 + /* confirm threads are starting */
2375 + wait_for_completion(&ud->tcp_rx.thread_done);
2376 + wait_for_completion(&ud->tcp_tx.thread_done);
2378 +EXPORT_SYMBOL_GPL(usbip_start_threads);
2380 +void usbip_stop_threads(struct usbip_device *ud)
2382 + /* kill threads related to this sdev, if v.c. exists */
2383 + if (ud->tcp_rx.thread != NULL) {
2384 + send_sig(SIGKILL, ud->tcp_rx.thread, 1);
2385 + wait_for_completion(&ud->tcp_rx.thread_done);
2386 + udbg("rx_thread for ud %p has finished\n", ud);
2389 + if (ud->tcp_tx.thread != NULL) {
2390 + send_sig(SIGKILL, ud->tcp_tx.thread, 1);
2391 + wait_for_completion(&ud->tcp_tx.thread_done);
2392 + udbg("tx_thread for ud %p has finished\n", ud);
2395 +EXPORT_SYMBOL_GPL(usbip_stop_threads);
2397 +void usbip_task_init(struct usbip_task *ut, char *name,
2398 + void (*loop_ops)(struct usbip_task *))
2400 + ut->thread = NULL;
2401 + init_completion(&ut->thread_done);
2403 + ut->loop_ops = loop_ops;
2405 +EXPORT_SYMBOL_GPL(usbip_task_init);
2408 +/*-------------------------------------------------------------------------*/
2409 +/* socket routines */
2411 + /* Send/receive messages over TCP/IP. I refer drivers/block/nbd.c */
2412 +int usbip_xmit(int send, struct socket *sock, char *buf,
2413 + int size, int msg_flags)
2416 + struct msghdr msg;
2420 + /* for blocks of if (dbg_flag_xmit) */
2424 + dbg_xmit("enter\n");
2426 + if (!sock || !buf || !size) {
2427 + printk(KERN_ERR "%s: invalid arg, sock %p buff %p size %d\n",
2428 + __func__, sock, buf, size);
2433 + if (dbg_flag_xmit) {
2435 + if (!in_interrupt())
2436 + printk(KERN_DEBUG "%-10s:", current->comm);
2438 + printk(KERN_DEBUG "interupt :");
2440 + printk("%s: sending... , sock %p, buf %p, "
2441 + "size %d, msg_flags %d\n", __func__,
2442 + sock, buf, size, msg_flags);
2443 + usbip_dump_buffer(buf, size);
2449 + sock->sk->sk_allocation = GFP_NOIO;
2450 + iov.iov_base = buf;
2451 + iov.iov_len = size;
2452 + msg.msg_name = NULL;
2453 + msg.msg_namelen = 0;
2454 + msg.msg_control = NULL;
2455 + msg.msg_controllen = 0;
2456 + msg.msg_namelen = 0;
2457 + msg.msg_flags = msg_flags | MSG_NOSIGNAL;
2460 + result = kernel_sendmsg(sock, &msg, &iov, 1, size);
2462 + result = kernel_recvmsg(sock, &msg, &iov, 1, size,
2465 + if (result <= 0) {
2466 + udbg("usbip_xmit: %s sock %p buf %p size %u ret %d"
2468 + send ? "send" : "receive", sock, buf,
2469 + size, result, total);
2477 + } while (size > 0);
2480 + if (dbg_flag_xmit) {
2482 + if (!in_interrupt())
2483 + printk(KERN_DEBUG "%-10s:", current->comm);
2485 + printk(KERN_DEBUG "interupt :");
2487 + printk("usbip_xmit: receiving....\n");
2488 + usbip_dump_buffer(bp, osize);
2489 + printk("usbip_xmit: received, osize %d ret %d size %d "
2490 + "total %d\n", osize, result, size,
2495 + printk("usbip_xmit: send, total %d\n", total);
2503 +EXPORT_SYMBOL_GPL(usbip_xmit);
2506 +/* now a usrland utility should set options. */
2508 +int setquickack(struct socket *socket)
2510 + mm_segment_t oldfs;
2516 + ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_QUICKACK,
2517 + (char __user *) &val, sizeof(ret));
2523 +int setnodelay(struct socket *socket)
2525 + mm_segment_t oldfs;
2531 + ret = socket->ops->setsockopt(socket, SOL_TCP, TCP_NODELAY,
2532 + (char __user *) &val, sizeof(ret));
2538 +int setkeepalive(struct socket *socket)
2540 + mm_segment_t oldfs;
2546 + ret = socket->ops->setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE,
2547 + (char __user *) &val, sizeof(ret));
2553 +void setreuse(struct socket *socket)
2555 + socket->sk->sk_reuse = 1;
2559 +struct socket *sockfd_to_socket(unsigned int sockfd)
2561 + struct socket *socket;
2562 + struct file *file;
2563 + struct inode *inode;
2565 + file = fget(sockfd);
2567 + printk(KERN_ERR "%s: invalid sockfd\n", __func__);
2571 + inode = file->f_dentry->d_inode;
2573 + if (!inode || !S_ISSOCK(inode->i_mode))
2576 + socket = SOCKET_I(inode);
2580 +EXPORT_SYMBOL_GPL(sockfd_to_socket);
2584 +/*-------------------------------------------------------------------------*/
2587 +/* there may be more cases to tweak the flags. */
2588 +static unsigned int tweak_transfer_flags(unsigned int flags)
2591 + if (flags & URB_NO_TRANSFER_DMA_MAP)
2593 + * vhci_hcd does not provide DMA-mapped I/O. The upper
2594 + * driver does not need to set this flag. The remote
2595 + * usbip.ko does not still perform DMA-mapped I/O for
2596 + * DMA-caplable host controllers. So, clear this flag.
2598 + flags &= ~URB_NO_TRANSFER_DMA_MAP;
2600 + if (flags & URB_NO_SETUP_DMA_MAP)
2601 + flags &= ~URB_NO_SETUP_DMA_MAP;
2606 +static void usbip_pack_cmd_submit(struct usbip_header *pdu, struct urb *urb,
2609 + struct usbip_header_cmd_submit *spdu = &pdu->u.cmd_submit;
2612 + * Some members are not still implemented in usbip. I hope this issue
2613 + * will be discussed when usbip is ported to other operating systems.
2617 + spdu->transfer_flags =
2618 + tweak_transfer_flags(urb->transfer_flags);
2619 + spdu->transfer_buffer_length = urb->transfer_buffer_length;
2620 + spdu->start_frame = urb->start_frame;
2621 + spdu->number_of_packets = urb->number_of_packets;
2622 + spdu->interval = urb->interval;
2625 + urb->transfer_flags = spdu->transfer_flags;
2627 + urb->transfer_buffer_length = spdu->transfer_buffer_length;
2628 + urb->start_frame = spdu->start_frame;
2629 + urb->number_of_packets = spdu->number_of_packets;
2630 + urb->interval = spdu->interval;
2634 +static void usbip_pack_ret_submit(struct usbip_header *pdu, struct urb *urb,
2637 + struct usbip_header_ret_submit *rpdu = &pdu->u.ret_submit;
2642 + rpdu->status = urb->status;
2643 + rpdu->actual_length = urb->actual_length;
2644 + rpdu->start_frame = urb->start_frame;
2645 + rpdu->error_count = urb->error_count;
2649 + urb->status = rpdu->status;
2650 + urb->actual_length = rpdu->actual_length;
2651 + urb->start_frame = rpdu->start_frame;
2652 + urb->error_count = rpdu->error_count;
2657 +void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
2661 + case USBIP_CMD_SUBMIT:
2662 + usbip_pack_cmd_submit(pdu, urb, pack);
2664 + case USBIP_RET_SUBMIT:
2665 + usbip_pack_ret_submit(pdu, urb, pack);
2668 + err("unknown command");
2673 +EXPORT_SYMBOL_GPL(usbip_pack_pdu);
2676 +static void correct_endian_basic(struct usbip_header_basic *base, int send)
2679 + base->command = cpu_to_be32(base->command);
2680 + base->seqnum = cpu_to_be32(base->seqnum);
2681 + base->devid = cpu_to_be32(base->devid);
2682 + base->direction = cpu_to_be32(base->direction);
2683 + base->ep = cpu_to_be32(base->ep);
2685 + base->command = be32_to_cpu(base->command);
2686 + base->seqnum = be32_to_cpu(base->seqnum);
2687 + base->devid = be32_to_cpu(base->devid);
2688 + base->direction = be32_to_cpu(base->direction);
2689 + base->ep = be32_to_cpu(base->ep);
2693 +static void correct_endian_cmd_submit(struct usbip_header_cmd_submit *pdu,
2697 + pdu->transfer_flags = cpu_to_be32(pdu->transfer_flags);
2699 + cpu_to_be32s(&pdu->transfer_buffer_length);
2700 + cpu_to_be32s(&pdu->start_frame);
2701 + cpu_to_be32s(&pdu->number_of_packets);
2702 + cpu_to_be32s(&pdu->interval);
2704 + pdu->transfer_flags = be32_to_cpu(pdu->transfer_flags);
2706 + be32_to_cpus(&pdu->transfer_buffer_length);
2707 + be32_to_cpus(&pdu->start_frame);
2708 + be32_to_cpus(&pdu->number_of_packets);
2709 + be32_to_cpus(&pdu->interval);
2713 +static void correct_endian_ret_submit(struct usbip_header_ret_submit *pdu,
2717 + cpu_to_be32s(&pdu->status);
2718 + cpu_to_be32s(&pdu->actual_length);
2719 + cpu_to_be32s(&pdu->start_frame);
2720 + cpu_to_be32s(&pdu->error_count);
2722 + be32_to_cpus(&pdu->status);
2723 + be32_to_cpus(&pdu->actual_length);
2724 + be32_to_cpus(&pdu->start_frame);
2725 + be32_to_cpus(&pdu->error_count);
2729 +static void correct_endian_cmd_unlink(struct usbip_header_cmd_unlink *pdu,
2733 + pdu->seqnum = cpu_to_be32(pdu->seqnum);
2735 + pdu->seqnum = be32_to_cpu(pdu->seqnum);
2738 +static void correct_endian_ret_unlink(struct usbip_header_ret_unlink *pdu,
2742 + cpu_to_be32s(&pdu->status);
2744 + be32_to_cpus(&pdu->status);
2747 +void usbip_header_correct_endian(struct usbip_header *pdu, int send)
2752 + cmd = pdu->base.command;
2754 + correct_endian_basic(&pdu->base, send);
2757 + cmd = pdu->base.command;
2760 + case USBIP_CMD_SUBMIT:
2761 + correct_endian_cmd_submit(&pdu->u.cmd_submit, send);
2763 + case USBIP_RET_SUBMIT:
2764 + correct_endian_ret_submit(&pdu->u.ret_submit, send);
2766 + case USBIP_CMD_UNLINK:
2767 + correct_endian_cmd_unlink(&pdu->u.cmd_unlink, send);
2769 + case USBIP_RET_UNLINK:
2770 + correct_endian_ret_unlink(&pdu->u.ret_unlink, send);
2774 + err("unknown command in pdu header: %d", cmd);
2778 +EXPORT_SYMBOL_GPL(usbip_header_correct_endian);
2780 +static void usbip_iso_pakcet_correct_endian(
2781 + struct usbip_iso_packet_descriptor *iso,
2784 + /* does not need all members. but copy all simply. */
2786 + iso->offset = cpu_to_be32(iso->offset);
2787 + iso->length = cpu_to_be32(iso->length);
2788 + iso->status = cpu_to_be32(iso->status);
2789 + iso->actual_length = cpu_to_be32(iso->actual_length);
2791 + iso->offset = be32_to_cpu(iso->offset);
2792 + iso->length = be32_to_cpu(iso->length);
2793 + iso->status = be32_to_cpu(iso->status);
2794 + iso->actual_length = be32_to_cpu(iso->actual_length);
2798 +static void usbip_pack_iso(struct usbip_iso_packet_descriptor *iso,
2799 + struct usb_iso_packet_descriptor *uiso, int pack)
2802 + iso->offset = uiso->offset;
2803 + iso->length = uiso->length;
2804 + iso->status = uiso->status;
2805 + iso->actual_length = uiso->actual_length;
2807 + uiso->offset = iso->offset;
2808 + uiso->length = iso->length;
2809 + uiso->status = iso->status;
2810 + uiso->actual_length = iso->actual_length;
2815 +/* must free buffer */
2816 +void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen)
2819 + struct usbip_iso_packet_descriptor *iso;
2820 + int np = urb->number_of_packets;
2821 + ssize_t size = np * sizeof(*iso);
2824 + buff = kzalloc(size, GFP_KERNEL);
2828 + for (i = 0; i < np; i++) {
2829 + iso = buff + (i * sizeof(*iso));
2831 + usbip_pack_iso(iso, &urb->iso_frame_desc[i], 1);
2832 + usbip_iso_pakcet_correct_endian(iso, 1);
2839 +EXPORT_SYMBOL_GPL(usbip_alloc_iso_desc_pdu);
2841 +/* some members of urb must be substituted before. */
2842 +int usbip_recv_iso(struct usbip_device *ud, struct urb *urb)
2845 + struct usbip_iso_packet_descriptor *iso;
2846 + int np = urb->number_of_packets;
2847 + int size = np * sizeof(*iso);
2851 + if (!usb_pipeisoc(urb->pipe))
2854 + /* my Bluetooth dongle gets ISO URBs which are np = 0 */
2856 + /* uinfo("iso np == 0\n"); */
2857 + /* usbip_dump_urb(urb); */
2861 + buff = kzalloc(size, GFP_KERNEL);
2865 + ret = usbip_xmit(0, ud->tcp_socket, buff, size, 0);
2866 + if (ret != size) {
2867 + dev_err(&urb->dev->dev, "recv iso_frame_descriptor, %d\n",
2871 + if (ud->side == USBIP_STUB)
2872 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
2874 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
2879 + for (i = 0; i < np; i++) {
2880 + iso = buff + (i * sizeof(*iso));
2882 + usbip_iso_pakcet_correct_endian(iso, 0);
2883 + usbip_pack_iso(iso, &urb->iso_frame_desc[i], 0);
2891 +EXPORT_SYMBOL_GPL(usbip_recv_iso);
2894 +/* some members of urb must be substituted before. */
2895 +int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb)
2900 + if (ud->side == USBIP_STUB) {
2902 + /* the direction of urb must be OUT. */
2903 + if (usb_pipein(urb->pipe))
2906 + size = urb->transfer_buffer_length;
2909 + /* the direction of urb must be IN. */
2910 + if (usb_pipeout(urb->pipe))
2913 + size = urb->actual_length;
2916 + /* no need to recv xbuff */
2920 + ret = usbip_xmit(0, ud->tcp_socket, (char *)urb->transfer_buffer,
2922 + if (ret != size) {
2923 + dev_err(&urb->dev->dev, "recv xbuf, %d\n", ret);
2924 + if (ud->side == USBIP_STUB) {
2925 + usbip_event_add(ud, SDEV_EVENT_ERROR_TCP);
2927 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
2934 +EXPORT_SYMBOL_GPL(usbip_recv_xbuff);
2937 +/*-------------------------------------------------------------------------*/
2939 +static int __init usbip_common_init(void)
2941 + printk(KERN_INFO KBUILD_MODNAME ": " DRIVER_DESC "" DRIVER_VERSION);
2946 +static void __exit usbip_common_exit(void)
2954 +module_init(usbip_common_init);
2955 +module_exit(usbip_common_exit);
2957 +MODULE_AUTHOR(DRIVER_AUTHOR);
2958 +MODULE_DESCRIPTION(DRIVER_DESC);
2959 +MODULE_LICENSE("GPL");
2961 +++ kernel-2.6.28/drivers/usb/usbip/usbip_common.h
2964 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
2966 + * This is free software; you can redistribute it and/or modify
2967 + * it under the terms of the GNU General Public License as published by
2968 + * the Free Software Foundation; either version 2 of the License, or
2969 + * (at your option) any later version.
2971 + * This is distributed in the hope that it will be useful,
2972 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2973 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2974 + * GNU General Public License for more details.
2976 + * You should have received a copy of the GNU General Public License
2977 + * along with this program; if not, write to the Free Software
2978 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
2982 +#ifndef __VHCI_COMMON_H
2983 +#define __VHCI_COMMON_H
2986 +#include <linux/version.h>
2987 +#include <linux/usb.h>
2988 +#include <asm/byteorder.h>
2989 +#include <net/sock.h>
2991 +/*-------------------------------------------------------------------------*/
2994 + * define macros to print messages
2998 + * udbg - print debug messages if CONFIG_USB_DEBUG is defined
3003 +#ifdef CONFIG_USB_DEBUG
3005 +#define udbg(fmt, args...) \
3007 + printk(KERN_DEBUG "%-10s:(%s,%d) %s: " fmt, \
3008 + (in_interrupt() ? "interrupt" : (current)->comm),\
3009 + __FILE__, __LINE__, __func__, ##args); \
3012 +#else /* CONFIG_USB_DEBUG */
3014 +#define udbg(fmt, args...) do { } while (0)
3016 +#endif /* CONFIG_USB_DEBUG */
3020 + usbip_debug_xmit = (1 << 0),
3021 + usbip_debug_sysfs = (1 << 1),
3022 + usbip_debug_urb = (1 << 2),
3023 + usbip_debug_eh = (1 << 3),
3025 + usbip_debug_stub_cmp = (1 << 8),
3026 + usbip_debug_stub_dev = (1 << 9),
3027 + usbip_debug_stub_rx = (1 << 10),
3028 + usbip_debug_stub_tx = (1 << 11),
3030 + usbip_debug_vhci_rh = (1 << 8),
3031 + usbip_debug_vhci_hc = (1 << 9),
3032 + usbip_debug_vhci_rx = (1 << 10),
3033 + usbip_debug_vhci_tx = (1 << 11),
3034 + usbip_debug_vhci_sysfs = (1 << 12)
3037 +#define dbg_flag_xmit (usbip_debug_flag & usbip_debug_xmit)
3038 +#define dbg_flag_vhci_rh (usbip_debug_flag & usbip_debug_vhci_rh)
3039 +#define dbg_flag_vhci_hc (usbip_debug_flag & usbip_debug_vhci_hc)
3040 +#define dbg_flag_vhci_rx (usbip_debug_flag & usbip_debug_vhci_rx)
3041 +#define dbg_flag_vhci_tx (usbip_debug_flag & usbip_debug_vhci_tx)
3042 +#define dbg_flag_vhci_sysfs (usbip_debug_flag & usbip_debug_vhci_sysfs)
3043 +#define dbg_flag_stub_rx (usbip_debug_flag & usbip_debug_stub_rx)
3044 +#define dbg_flag_stub_tx (usbip_debug_flag & usbip_debug_stub_tx)
3046 +extern unsigned long usbip_debug_flag;
3047 +extern struct device_attribute dev_attr_usbip_debug;
3049 +#define dbg_with_flag(flag, fmt, args...) \
3051 + if (flag & usbip_debug_flag) \
3052 + udbg(fmt , ##args); \
3055 +#define dbg_sysfs(fmt, args...) \
3056 + dbg_with_flag(usbip_debug_sysfs, fmt , ##args)
3057 +#define dbg_xmit(fmt, args...) \
3058 + dbg_with_flag(usbip_debug_xmit, fmt , ##args)
3059 +#define dbg_urb(fmt, args...) \
3060 + dbg_with_flag(usbip_debug_urb, fmt , ##args)
3061 +#define dbg_eh(fmt, args...) \
3062 + dbg_with_flag(usbip_debug_eh, fmt , ##args)
3064 +#define dbg_vhci_rh(fmt, args...) \
3065 + dbg_with_flag(usbip_debug_vhci_rh, fmt , ##args)
3066 +#define dbg_vhci_hc(fmt, args...) \
3067 + dbg_with_flag(usbip_debug_vhci_hc, fmt , ##args)
3068 +#define dbg_vhci_rx(fmt, args...) \
3069 + dbg_with_flag(usbip_debug_vhci_rx, fmt , ##args)
3070 +#define dbg_vhci_tx(fmt, args...) \
3071 + dbg_with_flag(usbip_debug_vhci_tx, fmt , ##args)
3072 +#define dbg_vhci_sysfs(fmt, args...) \
3073 + dbg_with_flag(usbip_debug_vhci_sysfs, fmt , ##args)
3075 +#define dbg_stub_cmp(fmt, args...) \
3076 + dbg_with_flag(usbip_debug_stub_cmp, fmt , ##args)
3077 +#define dbg_stub_rx(fmt, args...) \
3078 + dbg_with_flag(usbip_debug_stub_rx, fmt , ##args)
3079 +#define dbg_stub_tx(fmt, args...) \
3080 + dbg_with_flag(usbip_debug_stub_tx, fmt , ##args)
3084 + * uerr - print error messages
3088 +#define uerr(fmt, args...) \
3090 + printk(KERN_ERR "%-10s: ***ERROR*** (%s,%d) %s: " fmt, \
3091 + (in_interrupt() ? "interrupt" : (current)->comm),\
3092 + __FILE__, __LINE__, __func__, ##args); \
3096 + * uinfo - print information messages
3100 +#define uinfo(fmt, args...) \
3102 + printk(KERN_INFO "usbip: " fmt , ## args); \
3106 +/*-------------------------------------------------------------------------*/
3109 + * USB/IP request headers.
3110 + * Currently, we define 4 request types:
3112 + * - CMD_SUBMIT transfers a USB request, corresponding to usb_submit_urb().
3113 + * (client to server)
3114 + * - RET_RETURN transfers the result of CMD_SUBMIT.
3115 + * (server to client)
3116 + * - CMD_UNLINK transfers an unlink request of a pending USB request.
3117 + * (client to server)
3118 + * - RET_UNLINK transfers the result of CMD_UNLINK.
3119 + * (server to client)
3121 + * Note: The below request formats are based on the USB subsystem of Linux. Its
3122 + * details will be defined when other implementations come.
3128 + * A basic header followed by other additional headers.
3130 +struct usbip_header_basic {
3131 +#define USBIP_CMD_SUBMIT 0x0001
3132 +#define USBIP_CMD_UNLINK 0x0002
3133 +#define USBIP_RET_SUBMIT 0x0003
3134 +#define USBIP_RET_UNLINK 0x0004
3137 + /* sequencial number which identifies requests.
3138 + * incremented per connections */
3141 + /* devid is used to specify a remote USB device uniquely instead
3142 + * of busnum and devnum in Linux. In the case of Linux stub_driver,
3143 + * this value is ((busnum << 16) | devnum) */
3146 +#define USBIP_DIR_OUT 0
3147 +#define USBIP_DIR_IN 1
3149 + __u32 ep; /* endpoint number */
3150 +} __attribute__ ((packed));
3153 + * An additional header for a CMD_SUBMIT packet.
3155 +struct usbip_header_cmd_submit {
3156 + /* these values are basically the same as in a URB. */
3158 + /* the same in a URB. */
3159 + __u32 transfer_flags;
3161 + /* set the following data size (out),
3162 + * or expected reading data size (in) */
3163 + __s32 transfer_buffer_length;
3165 + /* it is difficult for usbip to sync frames (reserved only?) */
3166 + __s32 start_frame;
3168 + /* the number of iso descriptors that follows this header */
3169 + __s32 number_of_packets;
3171 + /* the maximum time within which this request works in a host
3172 + * controller of a server side */
3175 + /* set setup packet data for a CTRL request */
3176 + unsigned char setup[8];
3177 +} __attribute__ ((packed));
3180 + * An additional header for a RET_SUBMIT packet.
3182 +struct usbip_header_ret_submit {
3184 + __s32 actual_length; /* returned data length */
3185 + __s32 start_frame; /* ISO and INT */
3186 + __s32 number_of_packets; /* ISO only */
3187 + __s32 error_count; /* ISO only */
3188 +} __attribute__ ((packed));
3191 + * An additional header for a CMD_UNLINK packet.
3193 +struct usbip_header_cmd_unlink {
3194 + __u32 seqnum; /* URB's seqnum which will be unlinked */
3195 +} __attribute__ ((packed));
3199 + * An additional header for a RET_UNLINK packet.
3201 +struct usbip_header_ret_unlink {
3203 +} __attribute__ ((packed));
3206 +/* the same as usb_iso_packet_descriptor but packed for pdu */
3207 +struct usbip_iso_packet_descriptor {
3209 + __u32 length; /* expected length */
3210 + __u32 actual_length;
3212 +} __attribute__ ((packed));
3216 + * All usbip packets use a common header to keep code simple.
3218 +struct usbip_header {
3219 + struct usbip_header_basic base;
3222 + struct usbip_header_cmd_submit cmd_submit;
3223 + struct usbip_header_ret_submit ret_submit;
3224 + struct usbip_header_cmd_unlink cmd_unlink;
3225 + struct usbip_header_ret_unlink ret_unlink;
3227 +} __attribute__ ((packed));
3232 +/*-------------------------------------------------------------------------*/
3235 +int usbip_xmit(int, struct socket *, char *, int, int);
3236 +int usbip_sendmsg(struct socket *, struct msghdr *, int);
3239 +static inline int interface_to_busnum(struct usb_interface *interface)
3241 + struct usb_device *udev = interface_to_usbdev(interface);
3242 + return udev->bus->busnum;
3245 +static inline int interface_to_devnum(struct usb_interface *interface)
3247 + struct usb_device *udev = interface_to_usbdev(interface);
3248 + return udev->devnum;
3251 +static inline int interface_to_infnum(struct usb_interface *interface)
3253 + return interface->cur_altsetting->desc.bInterfaceNumber;
3257 +int setnodelay(struct socket *);
3258 +int setquickack(struct socket *);
3259 +int setkeepalive(struct socket *socket);
3260 +void setreuse(struct socket *);
3263 +struct socket *sockfd_to_socket(unsigned int);
3264 +int set_sockaddr(struct socket *socket, struct sockaddr_storage *ss);
3266 +void usbip_dump_urb(struct urb *purb);
3267 +void usbip_dump_header(struct usbip_header *pdu);
3270 +struct usbip_device;
3272 +struct usbip_task {
3273 + struct task_struct *thread;
3274 + struct completion thread_done;
3276 + void (*loop_ops)(struct usbip_task *);
3284 +enum usbip_status {
3285 + /* sdev is available. */
3286 + SDEV_ST_AVAILABLE = 0x01,
3287 + /* sdev is now used. */
3289 + /* sdev is unusable because of a fatal error. */
3292 + /* vdev does not connect a remote device. */
3294 + /* vdev is used, but the USB address is not assigned yet */
3295 + VDEV_ST_NOTASSIGNED,
3300 +/* a common structure for stub_device and vhci_device */
3301 +struct usbip_device {
3302 + enum usbip_side side;
3304 + enum usbip_status status;
3306 + /* lock for status */
3309 + struct socket *tcp_socket;
3311 + struct usbip_task tcp_rx;
3312 + struct usbip_task tcp_tx;
3314 + /* event handler */
3315 +#define USBIP_EH_SHUTDOWN (1 << 0)
3316 +#define USBIP_EH_BYE (1 << 1)
3317 +#define USBIP_EH_RESET (1 << 2)
3318 +#define USBIP_EH_UNUSABLE (1 << 3)
3320 +#define SDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_RESET | USBIP_EH_BYE)
3321 +#define SDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3322 +#define SDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3323 +#define SDEV_EVENT_ERROR_SUBMIT (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3324 +#define SDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
3326 +#define VDEV_EVENT_REMOVED (USBIP_EH_SHUTDOWN | USBIP_EH_BYE)
3327 +#define VDEV_EVENT_DOWN (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3328 +#define VDEV_EVENT_ERROR_TCP (USBIP_EH_SHUTDOWN | USBIP_EH_RESET)
3329 +#define VDEV_EVENT_ERROR_MALLOC (USBIP_EH_SHUTDOWN | USBIP_EH_UNUSABLE)
3331 + unsigned long event;
3332 + struct usbip_task eh;
3333 + wait_queue_head_t eh_waitq;
3336 + void (*shutdown)(struct usbip_device *);
3337 + void (*reset)(struct usbip_device *);
3338 + void (*unusable)(struct usbip_device *);
3343 +void usbip_task_init(struct usbip_task *ut, char *,
3344 + void (*loop_ops)(struct usbip_task *));
3346 +void usbip_start_threads(struct usbip_device *ud);
3347 +void usbip_stop_threads(struct usbip_device *ud);
3348 +int usbip_thread(void *param);
3350 +void usbip_pack_pdu(struct usbip_header *pdu, struct urb *urb, int cmd,
3353 +void usbip_header_correct_endian(struct usbip_header *pdu, int send);
3354 +/* some members of urb must be substituted before. */
3355 +int usbip_recv_xbuff(struct usbip_device *ud, struct urb *urb);
3356 +/* some members of urb must be substituted before. */
3357 +int usbip_recv_iso(struct usbip_device *ud, struct urb *urb);
3358 +void *usbip_alloc_iso_desc_pdu(struct urb *urb, ssize_t *bufflen);
3361 +/* usbip_event.c */
3362 +void usbip_start_eh(struct usbip_device *ud);
3363 +void usbip_stop_eh(struct usbip_device *ud);
3364 +void usbip_event_add(struct usbip_device *ud, unsigned long event);
3365 +int usbip_event_happend(struct usbip_device *ud);
3370 +++ kernel-2.6.28/drivers/usb/usbip/usbip_event.c
3373 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
3375 + * This is free software; you can redistribute it and/or modify
3376 + * it under the terms of the GNU General Public License as published by
3377 + * the Free Software Foundation; either version 2 of the License, or
3378 + * (at your option) any later version.
3380 + * This is distributed in the hope that it will be useful,
3381 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3382 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3383 + * GNU General Public License for more details.
3385 + * You should have received a copy of the GNU General Public License
3386 + * along with this program; if not, write to the Free Software
3387 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3391 +#include "usbip_common.h"
3393 +static int event_handler(struct usbip_device *ud)
3395 + dbg_eh("enter\n");
3398 + * Events are handled by only this thread.
3400 + while (usbip_event_happend(ud)) {
3401 + dbg_eh("pending event %lx\n", ud->event);
3404 + * NOTE: shutdown must come first.
3405 + * Shutdown the device.
3407 + if (ud->event & USBIP_EH_SHUTDOWN) {
3408 + ud->eh_ops.shutdown(ud);
3410 + ud->event &= ~USBIP_EH_SHUTDOWN;
3415 + /* Stop the error handler. */
3416 + if (ud->event & USBIP_EH_BYE)
3419 + /* Reset the device. */
3420 + if (ud->event & USBIP_EH_RESET) {
3421 + ud->eh_ops.reset(ud);
3423 + ud->event &= ~USBIP_EH_RESET;
3428 + /* Mark the device as unusable. */
3429 + if (ud->event & USBIP_EH_UNUSABLE) {
3430 + ud->eh_ops.unusable(ud);
3432 + ud->event &= ~USBIP_EH_UNUSABLE;
3438 + printk(KERN_ERR "%s: unknown event\n", __func__);
3445 +static void event_handler_loop(struct usbip_task *ut)
3447 + struct usbip_device *ud = container_of(ut, struct usbip_device, eh);
3450 + if (signal_pending(current)) {
3451 + dbg_eh("signal catched!\n");
3455 + if (event_handler(ud) < 0)
3458 + wait_event_interruptible(ud->eh_waitq, usbip_event_happend(ud));
3459 + dbg_eh("wakeup\n");
3463 +void usbip_start_eh(struct usbip_device *ud)
3465 + struct usbip_task *eh = &ud->eh;
3467 + init_waitqueue_head(&ud->eh_waitq);
3470 + usbip_task_init(eh, "usbip_eh", event_handler_loop);
3472 + kernel_thread(usbip_thread, (void *)eh, 0);
3474 + wait_for_completion(&eh->thread_done);
3476 +EXPORT_SYMBOL_GPL(usbip_start_eh);
3478 +void usbip_stop_eh(struct usbip_device *ud)
3480 + struct usbip_task *eh = &ud->eh;
3482 + wait_for_completion(&eh->thread_done);
3483 + dbg_eh("usbip_eh has finished\n");
3485 +EXPORT_SYMBOL_GPL(usbip_stop_eh);
3487 +void usbip_event_add(struct usbip_device *ud, unsigned long event)
3489 + spin_lock(&ud->lock);
3491 + ud->event |= event;
3493 + wake_up(&ud->eh_waitq);
3495 + spin_unlock(&ud->lock);
3497 +EXPORT_SYMBOL_GPL(usbip_event_add);
3499 +int usbip_event_happend(struct usbip_device *ud)
3503 + spin_lock(&ud->lock);
3505 + if (ud->event != 0)
3508 + spin_unlock(&ud->lock);
3512 +EXPORT_SYMBOL_GPL(usbip_event_happend);
3514 +++ kernel-2.6.28/drivers/usb/usbip/vhci.h
3517 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
3519 + * This is free software; you can redistribute it and/or modify
3520 + * it under the terms of the GNU General Public License as published by
3521 + * the Free Software Foundation; either version 2 of the License, or
3522 + * (at your option) any later version.
3524 + * This is distributed in the hope that it will be useful,
3525 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3526 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3527 + * GNU General Public License for more details.
3529 + * You should have received a copy of the GNU General Public License
3530 + * along with this program; if not, write to the Free Software
3531 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3535 +#include <linux/platform_device.h>
3536 +#include "../../usb/core/hcd.h"
3539 +struct vhci_device {
3540 + struct usb_device *udev;
3543 + * devid specifies a remote usb device uniquely instead
3544 + * of combination of busnum and devnum.
3548 + /* speed of a remote device */
3549 + enum usb_device_speed speed;
3551 + /* vhci root-hub port to which this device is attached */
3554 + struct usbip_device ud;
3557 + /* lock for the below link lists */
3558 + spinlock_t priv_lock;
3560 + /* vhci_priv is linked to one of them. */
3561 + struct list_head priv_tx;
3562 + struct list_head priv_rx;
3564 + /* vhci_unlink is linked to one of them */
3565 + struct list_head unlink_tx;
3566 + struct list_head unlink_rx;
3568 + /* vhci_tx thread sleeps for this queue */
3569 + wait_queue_head_t waitq_tx;
3573 +/* urb->hcpriv, use container_of() */
3575 + unsigned long seqnum;
3576 + struct list_head list;
3578 + struct vhci_device *vdev;
3583 +struct vhci_unlink {
3584 + /* seqnum of this request */
3585 + unsigned long seqnum;
3587 + struct list_head list;
3589 + /* seqnum of the unlink target */
3590 + unsigned long unlink_seqnum;
3594 + * The number of ports is less than 16 ?
3595 + * USB_MAXCHILDREN is statically defined to 16 in usb.h. Its maximum value
3596 + * would be 31 because the event_bits[1] of struct usb_hub is defined as
3597 + * unsigned long in hub.h
3599 +#define VHCI_NPORTS 8
3601 +/* for usb_bus.hcpriv */
3605 + u32 port_status[VHCI_NPORTS];
3607 + unsigned resuming:1;
3608 + unsigned long re_timeout;
3614 + * wIndex shows the port number and begins from 1.
3615 + * But, the index of this array begins from 0.
3617 + struct vhci_device vdev[VHCI_NPORTS];
3619 + /* vhci_device which has not been assiged its address yet */
3624 +extern struct vhci_hcd *the_controller;
3625 +extern struct attribute_group dev_attr_group;
3628 +/*-------------------------------------------------------------------------*/
3629 +/* prototype declaration */
3632 +void rh_port_connect(int rhport, enum usb_device_speed speed);
3633 +void rh_port_disconnect(int rhport);
3634 +void vhci_rx_loop(struct usbip_task *ut);
3635 +void vhci_tx_loop(struct usbip_task *ut);
3637 +#define hardware (&the_controller->pdev.dev)
3639 +static inline struct vhci_device *port_to_vdev(__u32 port)
3641 + return &the_controller->vdev[port];
3644 +static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd)
3646 + return (struct vhci_hcd *) (hcd->hcd_priv);
3649 +static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci)
3651 + return container_of((void *) vhci, struct usb_hcd, hcd_priv);
3654 +static inline struct device *vhci_dev(struct vhci_hcd *vhci)
3656 + return vhci_to_hcd(vhci)->self.controller;
3659 +++ kernel-2.6.28/drivers/usb/usbip/vhci_hcd.c
3662 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
3664 + * This is free software; you can redistribute it and/or modify
3665 + * it under the terms of the GNU General Public License as published by
3666 + * the Free Software Foundation; either version 2 of the License, or
3667 + * (at your option) any later version.
3669 + * This is distributed in the hope that it will be useful,
3670 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
3671 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3672 + * GNU General Public License for more details.
3674 + * You should have received a copy of the GNU General Public License
3675 + * along with this program; if not, write to the Free Software
3676 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
3681 +#include "usbip_common.h"
3684 +#define DRIVER_VERSION "1.0"
3685 +#define DRIVER_AUTHOR "Takahiro Hirofuchi"
3686 +#define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
3687 +#define DRIVER_LICENCE "GPL"
3688 +MODULE_AUTHOR(DRIVER_AUTHOR);
3689 +MODULE_DESCRIPTION(DRIVER_DESC);
3690 +MODULE_LICENSE(DRIVER_LICENCE);
3696 + * - update root hub emulation
3697 + * - move the emulation code to userland ?
3698 + * porting to other operating systems
3699 + * minimize kernel code
3700 + * - add suspend/resume code
3701 + * - clean up everything
3705 +/* See usb gadget dummy hcd */
3708 +static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
3709 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3710 + u16 wIndex, char *buff, u16 wLength);
3711 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
3713 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
3714 +static int vhci_start(struct usb_hcd *vhci_hcd);
3715 +static void vhci_stop(struct usb_hcd *hcd);
3716 +static int vhci_get_frame_number(struct usb_hcd *hcd);
3718 +static const char driver_name[] = "vhci_hcd";
3719 +static const char driver_desc[] = "USB/IP Virtual Host Contoroller";
3721 +struct vhci_hcd *the_controller;
3723 +static const char *bit_desc[] = {
3724 + "CONNECTION", /*0*/
3727 + "OVER_CURRENT", /*3*/
3734 + "HIGHSPEED", /*10*/
3735 + "PORT_TEST", /*11*/
3736 + "INDICATOR", /*12*/
3740 + "C_CONNECTION", /*16*/
3741 + "C_ENABLE", /*17*/
3742 + "C_SUSPEND", /*18*/
3743 + "C_OVER_CURRENT", /*19*/
3759 +static void dump_port_status(u32 status)
3763 + printk(KERN_DEBUG "status %08x:", status);
3764 + for (i = 0; i < 32; i++) {
3765 + if (status & (1 << i))
3766 + printk(" %s", bit_desc[i]);
3774 +void rh_port_connect(int rhport, enum usb_device_speed speed)
3776 + unsigned long flags;
3778 + dbg_vhci_rh("rh_port_connect %d\n", rhport);
3780 + spin_lock_irqsave(&the_controller->lock, flags);
3782 + the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
3783 + | (1 << USB_PORT_FEAT_C_CONNECTION);
3786 + case USB_SPEED_HIGH:
3787 + the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
3789 + case USB_SPEED_LOW:
3790 + the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
3796 + /* spin_lock(&the_controller->vdev[rhport].ud.lock);
3797 + * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
3798 + * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
3800 + the_controller->pending_port = rhport;
3802 + spin_unlock_irqrestore(&the_controller->lock, flags);
3804 + usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
3807 +void rh_port_disconnect(int rhport)
3809 + unsigned long flags;
3811 + dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
3813 + spin_lock_irqsave(&the_controller->lock, flags);
3814 + /* stop_activity(dum, driver); */
3815 + the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
3816 + the_controller->port_status[rhport] |=
3817 + (1 << USB_PORT_FEAT_C_CONNECTION);
3820 + /* not yet complete the disconnection
3821 + * spin_lock(&vdev->ud.lock);
3822 + * vdev->ud.status = VHC_ST_DISCONNECT;
3823 + * spin_unlock(&vdev->ud.lock); */
3825 + spin_unlock_irqrestore(&the_controller->lock, flags);
3830 +/*----------------------------------------------------------------------*/
3832 +#define PORT_C_MASK \
3833 + ((USB_PORT_STAT_C_CONNECTION \
3834 + | USB_PORT_STAT_C_ENABLE \
3835 + | USB_PORT_STAT_C_SUSPEND \
3836 + | USB_PORT_STAT_C_OVERCURRENT \
3837 + | USB_PORT_STAT_C_RESET) << 16)
3840 + * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
3841 + * suspend/resume support. But, it is modified to provide multiple ports.
3843 + * @buf: a bitmap to show which port status has been changed.
3844 + * bit 0: reserved or used for another purpose?
3845 + * bit 1: the status of port 0 has been changed.
3846 + * bit 2: the status of port 1 has been changed.
3848 + * bit 7: the status of port 6 has been changed.
3849 + * bit 8: the status of port 7 has been changed.
3851 + * bit 15: the status of port 14 has been changed.
3853 + * So, the maximum number of ports is 31 ( port 0 to port 30) ?
3855 + * The return value is the actual transfered length in byte. If nothing has
3856 + * been changed, return 0. In the case that the number of ports is less than or
3857 + * equal to 6 (VHCI_NPORTS==7), return 1.
3860 +static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
3862 + struct vhci_hcd *vhci;
3863 + unsigned long flags;
3866 + /* the enough buffer is allocated according to USB_MAXCHILDREN */
3867 + unsigned long *event_bits = (unsigned long *) buf;
3874 + vhci = hcd_to_vhci(hcd);
3876 + spin_lock_irqsave(&vhci->lock, flags);
3877 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
3878 + dbg_vhci_rh("hw accessible flag in on?\n");
3882 + /* check pseudo status register for each port */
3883 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
3884 + if ((vhci->port_status[rhport] & PORT_C_MASK)) {
3885 + /* The status of a port has been changed, */
3886 + dbg_vhci_rh("port %d is changed\n", rhport);
3888 + *event_bits |= 1 << (rhport + 1);
3893 + uinfo("changed %d\n", changed);
3895 + if (hcd->state == HC_STATE_SUSPENDED)
3896 + usb_hcd_resume_root_hub(hcd);
3899 + retval = 1 + (VHCI_NPORTS / 8);
3904 + spin_unlock_irqrestore(&vhci->lock, flags);
3908 +/* See hub_configure in hub.c */
3909 +static inline void hub_descriptor(struct usb_hub_descriptor *desc)
3911 + memset(desc, 0, sizeof(*desc));
3912 + desc->bDescriptorType = 0x29;
3913 + desc->bDescLength = 9;
3914 + desc->wHubCharacteristics = (__force __u16)
3915 + (__constant_cpu_to_le16(0x0001));
3916 + desc->bNbrPorts = VHCI_NPORTS;
3917 + desc->bitmap[0] = 0xff;
3918 + desc->bitmap[1] = 0xff;
3921 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
3922 + u16 wIndex, char *buf, u16 wLength)
3924 + struct vhci_hcd *dum;
3926 + unsigned long flags;
3929 + u32 prev_port_status[VHCI_NPORTS];
3931 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
3932 + return -ETIMEDOUT;
3936 + * wIndex shows the port number and begins from 1.
3938 + dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
3940 + if (wIndex > VHCI_NPORTS)
3941 + printk(KERN_ERR "%s: invalid port number %d\n", __func__, wIndex);
3942 + rhport = ((__u8)(wIndex & 0x00ff)) - 1;
3944 + dum = hcd_to_vhci(hcd);
3946 + spin_lock_irqsave(&dum->lock, flags);
3948 + /* store old status and compare now and old later */
3949 + if (dbg_flag_vhci_rh) {
3951 + for (i = 0; i < VHCI_NPORTS; i++)
3952 + prev_port_status[i] = dum->port_status[i];
3955 + switch (typeReq) {
3956 + case ClearHubFeature:
3957 + dbg_vhci_rh(" ClearHubFeature\n");
3959 + case ClearPortFeature:
3961 + case USB_PORT_FEAT_SUSPEND:
3962 + if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
3963 + /* 20msec signaling */
3964 + dum->resuming = 1;
3966 + jiffies + msecs_to_jiffies(20);
3969 + case USB_PORT_FEAT_POWER:
3970 + dbg_vhci_rh(" ClearPortFeature: USB_PORT_FEAT_POWER\n");
3971 + dum->port_status[rhport] = 0;
3972 + /* dum->address = 0; */
3973 + /* dum->hdev = 0; */
3974 + dum->resuming = 0;
3976 + case USB_PORT_FEAT_C_RESET:
3977 + dbg_vhci_rh(" ClearPortFeature: "
3978 + "USB_PORT_FEAT_C_RESET\n");
3979 + switch (dum->vdev[rhport].speed) {
3980 + case USB_SPEED_HIGH:
3981 + dum->port_status[rhport] |=
3982 + USB_PORT_STAT_HIGH_SPEED;
3984 + case USB_SPEED_LOW:
3985 + dum->port_status[rhport] |=
3986 + USB_PORT_STAT_LOW_SPEED;
3992 + dbg_vhci_rh(" ClearPortFeature: default %x\n", wValue);
3993 + dum->port_status[rhport] &= ~(1 << wValue);
3996 + case GetHubDescriptor:
3997 + dbg_vhci_rh(" GetHubDescriptor\n");
3998 + hub_descriptor((struct usb_hub_descriptor *) buf);
4000 + case GetHubStatus:
4001 + dbg_vhci_rh(" GetHubStatus\n");
4002 + *(__le32 *) buf = __constant_cpu_to_le32(0);
4004 + case GetPortStatus:
4005 + dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
4006 + if (wIndex > VHCI_NPORTS || wIndex < 1) {
4007 + printk(KERN_ERR "%s: invalid port number %d\n",
4008 + __func__, wIndex);
4012 + /* we do no care of resume. */
4014 + /* whoever resets or resumes must GetPortStatus to
4017 + if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
4018 + printk(KERN_ERR "%s: not yet\n", __func__);
4019 + dum->port_status[rhport] |=
4020 + (1 << USB_PORT_FEAT_C_SUSPEND);
4021 + dum->port_status[rhport] &=
4022 + ~(1 << USB_PORT_FEAT_SUSPEND);
4023 + dum->resuming = 0;
4024 + dum->re_timeout = 0;
4025 + /* if (dum->driver && dum->driver->resume) {
4026 + * spin_unlock (&dum->lock);
4027 + * dum->driver->resume (&dum->gadget);
4028 + * spin_lock (&dum->lock);
4032 + if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
4033 + 0 && time_after(jiffies, dum->re_timeout)) {
4034 + dum->port_status[rhport] |=
4035 + (1 << USB_PORT_FEAT_C_RESET);
4036 + dum->port_status[rhport] &=
4037 + ~(1 << USB_PORT_FEAT_RESET);
4038 + dum->re_timeout = 0;
4040 + if (dum->vdev[rhport].ud.status ==
4041 + VDEV_ST_NOTASSIGNED) {
4042 + dbg_vhci_rh(" enable rhport %d (status %u)\n",
4044 + dum->vdev[rhport].ud.status);
4045 + dum->port_status[rhport] |=
4046 + USB_PORT_STAT_ENABLE;
4049 + if (dum->driver) {
4051 + dum->port_status[rhport] |=
4052 + USB_PORT_STAT_ENABLE;
4053 + /* give it the best speed we agree on */
4054 + dum->gadget.speed = dum->driver->speed;
4055 + dum->gadget.ep0->maxpacket = 64;
4056 + switch (dum->gadget.speed) {
4057 + case USB_SPEED_HIGH:
4058 + dum->port_status[rhport] |=
4059 + USB_PORT_STAT_HIGH_SPEED;
4061 + case USB_SPEED_LOW:
4062 + dum->gadget.ep0->maxpacket = 8;
4063 + dum->port_status[rhport] |=
4064 + USB_PORT_STAT_LOW_SPEED;
4067 + dum->gadget.speed = USB_SPEED_FULL;
4074 + ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
4075 + ((u16 *) buf)[1] =
4076 + cpu_to_le16(dum->port_status[rhport] >> 16);
4078 + dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
4081 + case SetHubFeature:
4082 + dbg_vhci_rh(" SetHubFeature\n");
4085 + case SetPortFeature:
4087 + case USB_PORT_FEAT_SUSPEND:
4088 + dbg_vhci_rh(" SetPortFeature: "
4089 + "USB_PORT_FEAT_SUSPEND\n");
4090 + printk(KERN_ERR "%s: not yet\n", __func__);
4092 + dum->port_status[rhport] |=
4093 + (1 << USB_PORT_FEAT_SUSPEND);
4094 + if (dum->driver->suspend) {
4095 + spin_unlock(&dum->lock);
4096 + dum->driver->suspend(&dum->gadget);
4097 + spin_lock(&dum->lock);
4101 + case USB_PORT_FEAT_RESET:
4102 + dbg_vhci_rh(" SetPortFeature: USB_PORT_FEAT_RESET\n");
4103 + /* if it's already running, disconnect first */
4104 + if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
4105 + dum->port_status[rhport] &=
4106 + ~(USB_PORT_STAT_ENABLE |
4107 + USB_PORT_STAT_LOW_SPEED |
4108 + USB_PORT_STAT_HIGH_SPEED);
4110 + if (dum->driver) {
4111 + dev_dbg(hardware, "disconnect\n");
4112 + stop_activity(dum, dum->driver);
4116 + /* FIXME test that code path! */
4118 + /* 50msec reset signaling */
4119 + dum->re_timeout = jiffies + msecs_to_jiffies(50);
4123 + dbg_vhci_rh(" SetPortFeature: default %d\n", wValue);
4124 + dum->port_status[rhport] |= (1 << wValue);
4129 + printk(KERN_ERR "%s: default: no such request\n", __func__);
4130 + /* dev_dbg (hardware,
4131 + * "hub control req%04x v%04x i%04x l%d\n",
4132 + * typeReq, wValue, wIndex, wLength); */
4134 + /* "protocol stall" on error */
4138 + if (dbg_flag_vhci_rh) {
4139 + printk(KERN_DEBUG "port %d\n", rhport);
4140 + dump_port_status(prev_port_status[rhport]);
4141 + dump_port_status(dum->port_status[rhport]);
4143 + dbg_vhci_rh(" bye\n");
4145 + spin_unlock_irqrestore(&dum->lock, flags);
4152 +/*----------------------------------------------------------------------*/
4154 +static struct vhci_device *get_vdev(struct usb_device *udev)
4161 + for (i = 0; i < VHCI_NPORTS; i++)
4162 + if (the_controller->vdev[i].udev == udev)
4163 + return port_to_vdev(i);
4168 +static void vhci_tx_urb(struct urb *urb)
4170 + struct vhci_device *vdev = get_vdev(urb->dev);
4171 + struct vhci_priv *priv;
4172 + unsigned long flag;
4175 + err("could not get virtual device");
4180 + spin_lock_irqsave(&vdev->priv_lock, flag);
4182 + priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
4184 + dev_err(&urb->dev->dev, "malloc vhci_priv\n");
4185 + spin_unlock_irqrestore(&vdev->priv_lock, flag);
4186 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
4190 + priv->seqnum = atomic_inc_return(&the_controller->seqnum);
4191 + if (priv->seqnum == 0xffff)
4192 + uinfo("seqnum max\n");
4194 + priv->vdev = vdev;
4197 + urb->hcpriv = (void *) priv;
4200 + list_add_tail(&priv->list, &vdev->priv_tx);
4202 + wake_up(&vdev->waitq_tx);
4203 + spin_unlock_irqrestore(&vdev->priv_lock, flag);
4206 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
4209 + struct device *dev = &urb->dev->dev;
4211 + unsigned long flags;
4213 + dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
4214 + hcd, urb, mem_flags);
4216 + /* patch to usb_sg_init() is in 2.5.60 */
4217 + BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
4219 + spin_lock_irqsave(&the_controller->lock, flags);
4221 + /* check HC is active or not */
4222 + if (!HC_IS_RUNNING(hcd->state)) {
4223 + dev_err(dev, "HC is not running\n");
4224 + spin_unlock_irqrestore(&the_controller->lock, flags);
4228 + if (urb->status != -EINPROGRESS) {
4229 + dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
4230 + spin_unlock_irqrestore(&the_controller->lock, flags);
4231 + return urb->status;
4234 + ret = usb_hcd_link_urb_to_ep(hcd, urb);
4236 + goto no_need_unlink;
4239 + * The enumelation process is as follows;
4241 + * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
4242 + * to get max packet length of default pipe
4244 + * 2. Set_Address request to DevAddr(0) EndPoint(0)
4248 + if (usb_pipedevice(urb->pipe) == 0) {
4249 + __u8 type = usb_pipetype(urb->pipe);
4250 + struct usb_ctrlrequest *ctrlreq =
4251 + (struct usb_ctrlrequest *) urb->setup_packet;
4252 + struct vhci_device *vdev =
4253 + port_to_vdev(the_controller->pending_port);
4255 + if (type != PIPE_CONTROL || !ctrlreq) {
4256 + dev_err(dev, "invalid request to devnum 0\n");
4258 + goto no_need_xmit;
4261 + switch (ctrlreq->bRequest) {
4262 + case USB_REQ_SET_ADDRESS:
4263 + /* set_address may come when a device is reset */
4264 + dev_info(dev, "SetAddress Request (%d) to port %d\n",
4265 + ctrlreq->wValue, vdev->rhport);
4267 + vdev->udev = urb->dev;
4269 + spin_lock(&vdev->ud.lock);
4270 + vdev->ud.status = VDEV_ST_USED;
4271 + spin_unlock(&vdev->ud.lock);
4273 + if (urb->status == -EINPROGRESS) {
4274 + /* This request is successfully completed. */
4275 + /* If not -EINPROGRESS, possibly unlinked. */
4279 + goto no_need_xmit;
4281 + case USB_REQ_GET_DESCRIPTOR:
4282 + if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
4283 + dbg_vhci_hc("Not yet?: "
4284 + "Get_Descriptor to device 0 "
4285 + "(get max pipe size)\n");
4287 + /* FIXME: reference count? (usb_get_dev()) */
4288 + vdev->udev = urb->dev;
4293 + dev_err(dev, "invalid request to devnum 0 bRequest %u, "
4294 + "wValue %u\n", ctrlreq->bRequest,
4297 + goto no_need_xmit;
4305 + spin_unlock_irqrestore(&the_controller->lock, flags);
4310 + usb_hcd_unlink_urb_from_ep(hcd, urb);
4312 + spin_unlock_irqrestore(&the_controller->lock, flags);
4314 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
4320 + * vhci_rx gives back the urb after receiving the reply of the urb. If an
4321 + * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
4322 + * back its urb. For the driver unlinking the urb, the content of the urb is
4323 + * not important, but the calling to its completion handler is important; the
4324 + * completion of unlinking is notified by the completion handler.
4329 + * - When vhci_hcd receives RET_SUBMIT,
4331 + * - case 1a). the urb of the pdu is not unlinking.
4333 + * => just give back the urb
4335 + * - case 1b). the urb of the pdu is unlinking.
4336 + * - usbip.ko will return a reply of the unlinking request.
4337 + * => give back the urb now and go to case 2b).
4339 + * - When vhci_hcd receives RET_UNLINK,
4341 + * - case 2a). a submit request is still pending in vhci_hcd.
4342 + * - urb was really pending in usbip.ko and urb_unlink_urb() was
4343 + * completed there.
4344 + * => free a pending submit request
4345 + * => notify unlink completeness by giving back the urb
4347 + * - case 2b). a submit request is *not* pending in vhci_hcd.
4348 + * - urb was already given back to the core driver.
4349 + * => do not give back the urb
4354 + * - When usbip receives CMD_UNLINK,
4356 + * - case 3a). the urb of the unlink request is now in submission.
4357 + * => do usb_unlink_urb().
4358 + * => after the unlink is completed, send RET_UNLINK.
4360 + * - case 3b). the urb of the unlink request is not in submission.
4361 + * - may be already completed or never be received
4362 + * => send RET_UNLINK
4365 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
4367 + unsigned long flags;
4368 + struct vhci_priv *priv;
4369 + struct vhci_device *vdev;
4371 + uinfo("vhci_hcd: dequeue a urb %p\n", urb);
4374 + spin_lock_irqsave(&the_controller->lock, flags);
4376 + priv = urb->hcpriv;
4378 + /* URB was never linked! or will be soon given back by
4380 + spin_unlock_irqrestore(&the_controller->lock, flags);
4386 + ret = usb_hcd_check_unlink_urb(hcd, urb, status);
4388 + spin_unlock_irqrestore(&the_controller->lock, flags);
4393 + /* send unlink request here? */
4394 + vdev = priv->vdev;
4396 + if (!vdev->ud.tcp_socket) {
4397 + /* tcp connection is closed */
4398 + unsigned long flags2;
4400 + spin_lock_irqsave(&vdev->priv_lock, flags2);
4402 + uinfo("vhci_hcd: device %p seems to be disconnected\n", vdev);
4403 + list_del(&priv->list);
4405 + urb->hcpriv = NULL;
4407 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
4410 + /* tcp connection is alive */
4411 + unsigned long flags2;
4412 + struct vhci_unlink *unlink;
4414 + spin_lock_irqsave(&vdev->priv_lock, flags2);
4416 + /* setup CMD_UNLINK pdu */
4417 + unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
4419 + uerr("malloc vhci_unlink\n");
4420 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
4421 + spin_unlock_irqrestore(&the_controller->lock, flags);
4422 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
4426 + unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
4427 + if (unlink->seqnum == 0xffff)
4428 + uinfo("seqnum max\n");
4430 + unlink->unlink_seqnum = priv->seqnum;
4432 + uinfo("vhci_hcd: device %p seems to be still connected\n",
4435 + /* send cmd_unlink and try to cancel the pending URB in the
4437 + list_add_tail(&unlink->list, &vdev->unlink_tx);
4438 + wake_up(&vdev->waitq_tx);
4440 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
4445 + * If tcp connection is alive, we have sent CMD_UNLINK.
4446 + * vhci_rx will receive RET_UNLINK and give back the URB.
4447 + * Otherwise, we give back it here.
4449 + if (!vdev->ud.tcp_socket) {
4450 + /* tcp connection is closed */
4451 + uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n", urb);
4453 + usb_hcd_unlink_urb_from_ep(hcd, urb);
4455 + spin_unlock_irqrestore(&the_controller->lock, flags);
4456 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
4458 + spin_lock_irqsave(&the_controller->lock, flags);
4461 + spin_unlock_irqrestore(&the_controller->lock, flags);
4463 + dbg_vhci_hc("leave\n");
4468 +static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
4470 + struct vhci_unlink *unlink, *tmp;
4472 + spin_lock(&vdev->priv_lock);
4474 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
4475 + list_del(&unlink->list);
4479 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
4480 + list_del(&unlink->list);
4484 + spin_unlock(&vdev->priv_lock);
4488 + * The important thing is that only one context begins cleanup.
4489 + * This is why error handling and cleanup become simple.
4490 + * We do not want to consider race condition as possible.
4492 +static void vhci_shutdown_connection(struct usbip_device *ud)
4494 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
4496 + /* need this? see stub_dev.c */
4497 + if (ud->tcp_socket) {
4498 + udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
4499 + kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
4502 + usbip_stop_threads(&vdev->ud);
4503 + uinfo("stop threads\n");
4505 + /* active connection is closed */
4506 + if (vdev->ud.tcp_socket != NULL) {
4507 + sock_release(vdev->ud.tcp_socket);
4508 + vdev->ud.tcp_socket = NULL;
4510 + uinfo("release socket\n");
4512 + vhci_device_unlink_cleanup(vdev);
4515 + * rh_port_disconnect() is a trigger of ...
4516 + * usb_disable_device():
4517 + * disable all the endpoints for a USB device.
4518 + * usb_disable_endpoint():
4519 + * disable endpoints. pending urbs are unlinked(dequeued).
4521 + * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
4522 + * deteched device should release used urbs in a cleanup function(i.e.
4523 + * xxx_disconnect()). Therefore, vhci_hcd does not need to release
4524 + * pushed urbs and their private data in this function.
4526 + * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
4527 + * a connection, vhci_shutdown_connection() expects vhci_dequeue()
4528 + * gives back pushed urbs and frees their private data by request of
4529 + * the cleanup function of a USB driver. When unlinking a urb with an
4530 + * active connection, vhci_dequeue() does not give back the urb which
4531 + * is actually given back by vhci_rx after receiving its return pdu.
4534 + rh_port_disconnect(vdev->rhport);
4536 + uinfo("disconnect device\n");
4540 +static void vhci_device_reset(struct usbip_device *ud)
4542 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
4544 + spin_lock(&ud->lock);
4549 + ud->tcp_socket = NULL;
4551 + ud->status = VDEV_ST_NULL;
4553 + spin_unlock(&ud->lock);
4556 +static void vhci_device_unusable(struct usbip_device *ud)
4558 + spin_lock(&ud->lock);
4560 + ud->status = VDEV_ST_ERROR;
4562 + spin_unlock(&ud->lock);
4565 +static void vhci_device_init(struct vhci_device *vdev)
4567 + memset(vdev, 0, sizeof(*vdev));
4569 + usbip_task_init(&vdev->ud.tcp_rx, "vhci_rx", vhci_rx_loop);
4570 + usbip_task_init(&vdev->ud.tcp_tx, "vhci_tx", vhci_tx_loop);
4572 + vdev->ud.side = USBIP_VHCI;
4573 + vdev->ud.status = VDEV_ST_NULL;
4574 + /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
4575 + spin_lock_init(&vdev->ud.lock);
4577 + INIT_LIST_HEAD(&vdev->priv_rx);
4578 + INIT_LIST_HEAD(&vdev->priv_tx);
4579 + INIT_LIST_HEAD(&vdev->unlink_tx);
4580 + INIT_LIST_HEAD(&vdev->unlink_rx);
4581 + /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
4582 + spin_lock_init(&vdev->priv_lock);
4584 + init_waitqueue_head(&vdev->waitq_tx);
4586 + vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
4587 + vdev->ud.eh_ops.reset = vhci_device_reset;
4588 + vdev->ud.eh_ops.unusable = vhci_device_unusable;
4590 + usbip_start_eh(&vdev->ud);
4594 +/*----------------------------------------------------------------------*/
4596 +static int vhci_start(struct usb_hcd *hcd)
4598 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4602 + dbg_vhci_hc("enter vhci_start\n");
4605 + /* initialize private data of usb_hcd */
4607 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
4608 + struct vhci_device *vdev = &vhci->vdev[rhport];
4609 + vhci_device_init(vdev);
4610 + vdev->rhport = rhport;
4613 + atomic_set(&vhci->seqnum, 0);
4614 + spin_lock_init(&vhci->lock);
4618 + hcd->power_budget = 0; /* no limit */
4619 + hcd->state = HC_STATE_RUNNING;
4620 + hcd->uses_new_polling = 1;
4623 + /* vhci_hcd is now ready to be controlled through sysfs */
4624 + err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
4626 + uerr("create sysfs files\n");
4633 +static void vhci_stop(struct usb_hcd *hcd)
4635 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4638 + dbg_vhci_hc("stop VHCI controller\n");
4641 + /* 1. remove the userland interface of vhci_hcd */
4642 + sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
4644 + /* 2. shutdown all the ports of vhci_hcd */
4645 + for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
4646 + struct vhci_device *vdev = &vhci->vdev[rhport];
4648 + usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
4649 + usbip_stop_eh(&vdev->ud);
4653 + uinfo("vhci_stop done\n");
4656 +/*----------------------------------------------------------------------*/
4658 +static int vhci_get_frame_number(struct usb_hcd *hcd)
4660 + uerr("Not yet implemented\n");
4667 +/* FIXME: suspend/resume */
4668 +static int vhci_bus_suspend(struct usb_hcd *hcd)
4670 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4672 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
4674 + spin_lock_irq(&vhci->lock);
4675 + /* vhci->rh_state = DUMMY_RH_SUSPENDED;
4676 + * set_link_state(vhci); */
4677 + hcd->state = HC_STATE_SUSPENDED;
4678 + spin_unlock_irq(&vhci->lock);
4683 +static int vhci_bus_resume(struct usb_hcd *hcd)
4685 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
4688 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
4690 + spin_lock_irq(&vhci->lock);
4691 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
4694 + /* vhci->rh_state = DUMMY_RH_RUNNING;
4695 + * set_link_state(vhci);
4696 + * if (!list_empty(&vhci->urbp_list))
4697 + * mod_timer(&vhci->timer, jiffies); */
4698 + hcd->state = HC_STATE_RUNNING;
4700 + spin_unlock_irq(&vhci->lock);
4708 +#define vhci_bus_suspend NULL
4709 +#define vhci_bus_resume NULL
4714 +static struct hc_driver vhci_hc_driver = {
4715 + .description = driver_name,
4716 + .product_desc = driver_desc,
4717 + .hcd_priv_size = sizeof(struct vhci_hcd),
4719 + .flags = HCD_USB2,
4721 + .start = vhci_start,
4722 + .stop = vhci_stop,
4724 + .urb_enqueue = vhci_urb_enqueue,
4725 + .urb_dequeue = vhci_urb_dequeue,
4727 + .get_frame_number = vhci_get_frame_number,
4729 + .hub_status_data = vhci_hub_status,
4730 + .hub_control = vhci_hub_control,
4731 + .bus_suspend = vhci_bus_suspend,
4732 + .bus_resume = vhci_bus_resume,
4735 +static int vhci_hcd_probe(struct platform_device *pdev)
4737 + struct usb_hcd *hcd;
4740 + uinfo("proving...\n");
4742 + dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
4744 + /* will be removed */
4745 + if (pdev->dev.dma_mask) {
4746 + dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
4751 + * Allocate and initialize hcd.
4752 + * Our private data is also allocated automatically.
4754 + hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, pdev->dev.bus_id);
4756 + uerr("create hcd failed\n");
4761 + /* this is private data for vhci_hcd */
4762 + the_controller = hcd_to_vhci(hcd);
4765 + * Finish generic HCD structure initialization and register.
4766 + * Call the driver's reset() and start() routines.
4768 + ret = usb_add_hcd(hcd, 0, 0);
4770 + uerr("usb_add_hcd failed %d\n", ret);
4772 + the_controller = NULL;
4777 + dbg_vhci_hc("bye\n");
4782 +static int vhci_hcd_remove(struct platform_device *pdev)
4784 + struct usb_hcd *hcd;
4786 + hcd = platform_get_drvdata(pdev);
4791 + * Disconnects the root hub,
4792 + * then reverses the effects of usb_add_hcd(),
4793 + * invoking the HCD's stop() methods.
4795 + usb_remove_hcd(hcd);
4797 + the_controller = NULL;
4807 +/* what should happen for USB/IP under suspend/resume? */
4808 +static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
4810 + struct usb_hcd *hcd;
4812 + int connected = 0;
4815 + dev_dbg(&pdev->dev, "%s\n", __func__);
4817 + hcd = platform_get_drvdata(pdev);
4819 + spin_lock(&the_controller->lock);
4821 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
4822 + if (the_controller->port_status[rhport] &
4823 + USB_PORT_STAT_CONNECTION)
4826 + spin_unlock(&the_controller->lock);
4828 + if (connected > 0) {
4829 + uinfo("We have %d active connection%s. Do not suspend.\n",
4830 + connected, (connected == 1 ? "" : "s"));
4833 + uinfo("suspend vhci_hcd");
4834 + clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4840 +static int vhci_hcd_resume(struct platform_device *pdev)
4842 + struct usb_hcd *hcd;
4844 + dev_dbg(&pdev->dev, "%s\n", __func__);
4846 + hcd = platform_get_drvdata(pdev);
4847 + set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
4848 + usb_hcd_poll_rh_status(hcd);
4855 +#define vhci_hcd_suspend NULL
4856 +#define vhci_hcd_resume NULL
4861 +static struct platform_driver vhci_driver = {
4862 + .probe = vhci_hcd_probe,
4863 + .remove = __devexit_p(vhci_hcd_remove),
4864 + .suspend = vhci_hcd_suspend,
4865 + .resume = vhci_hcd_resume,
4867 + .name = (char *) driver_name,
4868 + .owner = THIS_MODULE,
4872 +/*----------------------------------------------------------------------*/
4875 + * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
4876 + * We need to add this virtual device as a platform device arbitrarily:
4877 + * 1. platform_device_register()
4879 +static void the_pdev_release(struct device *dev)
4884 +static struct platform_device the_pdev = {
4885 + /* should be the same name as driver_name */
4886 + .name = (char *) driver_name,
4889 + /* .driver = &vhci_driver, */
4890 + .release = the_pdev_release,
4894 +static int __init vhci_init(void)
4898 + dbg_vhci_hc("enter\n");
4899 + if (usb_disabled())
4902 + printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
4905 + ret = platform_driver_register(&vhci_driver);
4907 + goto err_driver_register;
4909 + ret = platform_device_register(&the_pdev);
4911 + goto err_platform_device_register;
4913 + dbg_vhci_hc("bye\n");
4916 + /* error occurred */
4917 +err_platform_device_register:
4918 + platform_driver_unregister(&vhci_driver);
4920 +err_driver_register:
4921 + dbg_vhci_hc("bye\n");
4924 +module_init(vhci_init);
4926 +static void __exit vhci_cleanup(void)
4928 + dbg_vhci_hc("enter\n");
4930 + platform_device_unregister(&the_pdev);
4931 + platform_driver_unregister(&vhci_driver);
4933 + dbg_vhci_hc("bye\n");
4935 +module_exit(vhci_cleanup);
4937 +++ kernel-2.6.28/drivers/usb/usbip/vhci_rx.c
4940 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
4942 + * This is free software; you can redistribute it and/or modify
4943 + * it under the terms of the GNU General Public License as published by
4944 + * the Free Software Foundation; either version 2 of the License, or
4945 + * (at your option) any later version.
4947 + * This is distributed in the hope that it will be useful,
4948 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
4949 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4950 + * GNU General Public License for more details.
4952 + * You should have received a copy of the GNU General Public License
4953 + * along with this program; if not, write to the Free Software
4954 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
4958 +#include "usbip_common.h"
4962 +/* get URB from transmitted urb queue */
4963 +static struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev,
4966 + struct vhci_priv *priv, *tmp;
4967 + struct urb *urb = NULL;
4970 + spin_lock(&vdev->priv_lock);
4972 + list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
4973 + if (priv->seqnum == seqnum) {
4975 + status = urb->status;
4977 + dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
4978 + urb, priv, seqnum);
4980 + /* TODO: fix logic here to improve indent situtation */
4981 + if (status != -EINPROGRESS) {
4982 + if (status == -ENOENT ||
4983 + status == -ECONNRESET)
4984 + dev_info(&urb->dev->dev,
4985 + "urb %p was unlinked "
4986 + "%ssynchronuously.\n", urb,
4987 + status == -ENOENT ? "" : "a");
4989 + dev_info(&urb->dev->dev,
4990 + "urb %p may be in a error, "
4991 + "status %d\n", urb, status);
4994 + list_del(&priv->list);
4996 + urb->hcpriv = NULL;
5002 + spin_unlock(&vdev->priv_lock);
5007 +static void vhci_recv_ret_submit(struct vhci_device *vdev,
5008 + struct usbip_header *pdu)
5010 + struct usbip_device *ud = &vdev->ud;
5014 + urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
5018 + uerr("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
5019 + uinfo("max seqnum %d\n", atomic_read(&the_controller->seqnum));
5020 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
5025 + /* unpack the pdu to a urb */
5026 + usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
5029 + /* recv transfer buffer */
5030 + if (usbip_recv_xbuff(ud, urb) < 0)
5034 + /* recv iso_packet_descriptor */
5035 + if (usbip_recv_iso(ud, urb) < 0)
5039 + if (dbg_flag_vhci_rx)
5040 + usbip_dump_urb(urb);
5043 + dbg_vhci_rx("now giveback urb %p\n", urb);
5045 + spin_lock(&the_controller->lock);
5046 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
5047 + spin_unlock(&the_controller->lock);
5049 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
5052 + dbg_vhci_rx("Leave\n");
5058 +static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
5059 + struct usbip_header *pdu)
5061 + struct vhci_unlink *unlink, *tmp;
5063 + spin_lock(&vdev->priv_lock);
5065 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
5066 + uinfo("unlink->seqnum %lu\n", unlink->seqnum);
5067 + if (unlink->seqnum == pdu->base.seqnum) {
5068 + dbg_vhci_rx("found pending unlink, %lu\n",
5070 + list_del(&unlink->list);
5072 + spin_unlock(&vdev->priv_lock);
5077 + spin_unlock(&vdev->priv_lock);
5083 +static void vhci_recv_ret_unlink(struct vhci_device *vdev,
5084 + struct usbip_header *pdu)
5086 + struct vhci_unlink *unlink;
5089 + usbip_dump_header(pdu);
5091 + unlink = dequeue_pending_unlink(vdev, pdu);
5093 + uinfo("cannot find the pending unlink %u\n", pdu->base.seqnum);
5097 + urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
5100 + * I get the result of a unlink request. But, it seems that I
5101 + * already received the result of its submit result and gave
5104 + uinfo("the urb (seqnum %d) was already given backed\n",
5105 + pdu->base.seqnum);
5107 + dbg_vhci_rx("now giveback urb %p\n", urb);
5109 + /* If unlink is succeed, status is -ECONNRESET */
5110 + urb->status = pdu->u.ret_unlink.status;
5111 + uinfo("%d\n", urb->status);
5113 + spin_lock(&the_controller->lock);
5114 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
5115 + spin_unlock(&the_controller->lock);
5117 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
5127 +static void vhci_rx_pdu(struct usbip_device *ud)
5130 + struct usbip_header pdu;
5131 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
5134 + dbg_vhci_rx("Enter\n");
5136 + memset(&pdu, 0, sizeof(pdu));
5139 + /* 1. receive a pdu header */
5140 + ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
5141 + if (ret != sizeof(pdu)) {
5142 + uerr("receiving pdu failed! size is %d, should be %d\n",
5143 + ret, (unsigned int)sizeof(pdu));
5144 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
5148 + usbip_header_correct_endian(&pdu, 0);
5150 + if (dbg_flag_vhci_rx)
5151 + usbip_dump_header(&pdu);
5153 + switch (pdu.base.command) {
5154 + case USBIP_RET_SUBMIT:
5155 + vhci_recv_ret_submit(vdev, &pdu);
5157 + case USBIP_RET_UNLINK:
5158 + vhci_recv_ret_unlink(vdev, &pdu);
5162 + uerr("unknown pdu %u\n", pdu.base.command);
5163 + usbip_dump_header(&pdu);
5164 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
5169 +/*-------------------------------------------------------------------------*/
5171 +void vhci_rx_loop(struct usbip_task *ut)
5173 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
5177 + if (signal_pending(current)) {
5178 + dbg_vhci_rx("signal catched!\n");
5183 + if (usbip_event_happend(ud))
5191 +++ kernel-2.6.28/drivers/usb/usbip/vhci_sysfs.c
5194 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
5196 + * This is free software; you can redistribute it and/or modify
5197 + * it under the terms of the GNU General Public License as published by
5198 + * the Free Software Foundation; either version 2 of the License, or
5199 + * (at your option) any later version.
5201 + * This is distributed in the hope that it will be useful,
5202 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5203 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5204 + * GNU General Public License for more details.
5206 + * You should have received a copy of the GNU General Public License
5207 + * along with this program; if not, write to the Free Software
5208 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
5212 +#include "usbip_common.h"
5215 +#include <linux/in.h>
5217 +/* TODO: refine locking ?*/
5219 +/* Sysfs entry to show port status */
5220 +static ssize_t show_status(struct device *dev, struct device_attribute *attr,
5226 + if (!the_controller || !out)
5229 + spin_lock(&the_controller->lock);
5233 + * prt sta spd dev socket local_busid
5234 + * 000 004 000 000 c5a7bb80 1-2.3
5235 + * 001 004 000 000 d8cee980 2-3.4
5237 + * IP address can be retrieved from a socket pointer address by looking
5238 + * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
5239 + * port number and its peer IP address.
5241 + out += sprintf(out, "prt sta spd bus dev socket "
5244 + for (i = 0; i < VHCI_NPORTS; i++) {
5245 + struct vhci_device *vdev = port_to_vdev(i);
5247 + spin_lock(&vdev->ud.lock);
5249 + out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
5251 + if (vdev->ud.status == VDEV_ST_USED) {
5252 + out += sprintf(out, "%03u %08x ",
5253 + vdev->speed, vdev->devid);
5254 + out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
5255 + out += sprintf(out, "%s", vdev->udev->dev.bus_id);
5258 + out += sprintf(out, "000 000 000 0000000000000000 0-0");
5260 + out += sprintf(out, "\n");
5262 + spin_unlock(&vdev->ud.lock);
5265 + spin_unlock(&the_controller->lock);
5269 +static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
5271 +/* Sysfs entry to shutdown a virtual connection */
5272 +static int vhci_port_disconnect(__u32 rhport)
5274 + struct vhci_device *vdev;
5276 + dbg_vhci_sysfs("enter\n");
5279 + spin_lock(&the_controller->lock);
5281 + vdev = port_to_vdev(rhport);
5283 + spin_lock(&vdev->ud.lock);
5284 + if (vdev->ud.status == VDEV_ST_NULL) {
5285 + uerr("not connected %d\n", vdev->ud.status);
5288 + spin_unlock(&vdev->ud.lock);
5289 + spin_unlock(&the_controller->lock);
5295 + spin_unlock(&vdev->ud.lock);
5296 + spin_unlock(&the_controller->lock);
5298 + usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
5303 +static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
5304 + const char *buf, size_t count)
5309 + sscanf(buf, "%u", &rhport);
5311 + /* check rhport */
5312 + if (rhport >= VHCI_NPORTS) {
5313 + uerr("invalid port %u\n", rhport);
5317 + err = vhci_port_disconnect(rhport);
5321 + dbg_vhci_sysfs("Leave\n");
5324 +static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
5326 +/* Sysfs entry to establish a virtual connection */
5327 +static int valid_args(__u32 rhport, enum usb_device_speed speed)
5329 + /* check rhport */
5330 + if ((rhport < 0) || (rhport >= VHCI_NPORTS)) {
5331 + uerr("port %u\n", rhport);
5337 + case USB_SPEED_LOW:
5338 + case USB_SPEED_FULL:
5339 + case USB_SPEED_HIGH:
5340 + case USB_SPEED_VARIABLE:
5343 + uerr("speed %d\n", speed);
5351 + * To start a new USB/IP attachment, a userland program needs to setup a TCP
5352 + * connection and then write its socket descriptor with remote device
5353 + * information into this sysfs file.
5355 + * A remote device is virtually attached to the root-hub port of @rhport with
5356 + * @speed. @devid is embedded into a request to specify the remote device in a
5359 + * write() returns 0 on success, else negative errno.
5361 +static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
5362 + const char *buf, size_t count)
5364 + struct vhci_device *vdev;
5365 + struct socket *socket;
5367 + __u32 rhport = 0, devid = 0, speed = 0;
5370 + * @rhport: port number of vhci_hcd
5371 + * @sockfd: socket descriptor of an established TCP connection
5372 + * @devid: unique device identifier in a remote host
5373 + * @speed: usb device speed in a remote host
5375 + sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
5377 + dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
5378 + rhport, sockfd, devid, speed);
5381 + /* check received parameters */
5382 + if (valid_args(rhport, speed) < 0)
5385 + /* check sockfd */
5386 + socket = sockfd_to_socket(sockfd);
5390 + /* now need lock until setting vdev status as used */
5392 + /* begin a lock */
5393 + spin_lock(&the_controller->lock);
5395 + vdev = port_to_vdev(rhport);
5397 + spin_lock(&vdev->ud.lock);
5399 + if (vdev->ud.status != VDEV_ST_NULL) {
5400 + /* end of the lock */
5401 + spin_unlock(&vdev->ud.lock);
5402 + spin_unlock(&the_controller->lock);
5404 + uerr("port %d already used\n", rhport);
5408 + uinfo("rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
5409 + rhport, sockfd, devid, speed);
5411 + vdev->devid = devid;
5412 + vdev->speed = speed;
5413 + vdev->ud.tcp_socket = socket;
5414 + vdev->ud.status = VDEV_ST_NOTASSIGNED;
5416 + spin_unlock(&vdev->ud.lock);
5417 + spin_unlock(&the_controller->lock);
5418 + /* end the lock */
5421 + * this function will sleep, so should be out of the lock. but, it's ok
5422 + * because we already marked vdev as being used. really?
5424 + usbip_start_threads(&vdev->ud);
5426 + rh_port_connect(rhport, speed);
5430 +static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
5432 +static struct attribute *dev_attrs[] = {
5433 + &dev_attr_status.attr,
5434 + &dev_attr_detach.attr,
5435 + &dev_attr_attach.attr,
5436 + &dev_attr_usbip_debug.attr,
5440 +struct attribute_group dev_attr_group = {
5441 + .attrs = dev_attrs,
5444 +++ kernel-2.6.28/drivers/usb/usbip/vhci_tx.c
5447 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
5449 + * This is free software; you can redistribute it and/or modify
5450 + * it under the terms of the GNU General Public License as published by
5451 + * the Free Software Foundation; either version 2 of the License, or
5452 + * (at your option) any later version.
5454 + * This is distributed in the hope that it will be useful,
5455 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
5456 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
5457 + * GNU General Public License for more details.
5459 + * You should have received a copy of the GNU General Public License
5460 + * along with this program; if not, write to the Free Software
5461 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
5465 +#include "usbip_common.h"
5469 +static void setup_cmd_submit_pdu(struct usbip_header *pdup, struct urb *urb)
5471 + struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv);
5472 + struct vhci_device *vdev = priv->vdev;
5474 + dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
5475 + usb_pipedevice(urb->pipe), vdev->devid);
5477 + pdup->base.command = USBIP_CMD_SUBMIT;
5478 + pdup->base.seqnum = priv->seqnum;
5479 + pdup->base.devid = vdev->devid;
5480 + if (usb_pipein(urb->pipe))
5481 + pdup->base.direction = USBIP_DIR_IN;
5483 + pdup->base.direction = USBIP_DIR_OUT;
5484 + pdup->base.ep = usb_pipeendpoint(urb->pipe);
5486 + usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1);
5488 + if (urb->setup_packet)
5489 + memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8);
5492 +static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev)
5494 + unsigned long flags;
5495 + struct vhci_priv *priv, *tmp;
5497 + spin_lock_irqsave(&vdev->priv_lock, flags);
5499 + list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) {
5500 + list_move_tail(&priv->list, &vdev->priv_rx);
5501 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
5505 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
5512 +static int vhci_send_cmd_submit(struct vhci_device *vdev)
5514 + struct vhci_priv *priv = NULL;
5516 + struct msghdr msg;
5517 + struct kvec iov[3];
5520 + size_t total_size = 0;
5522 + while ((priv = dequeue_from_priv_tx(vdev)) != NULL) {
5524 + struct urb *urb = priv->urb;
5525 + struct usbip_header pdu_header;
5526 + void *iso_buffer = NULL;
5529 + memset(&pdu_header, 0, sizeof(pdu_header));
5530 + memset(&msg, 0, sizeof(msg));
5531 + memset(&iov, 0, sizeof(iov));
5533 + dbg_vhci_tx("setup txdata urb %p\n", urb);
5536 + /* 1. setup usbip_header */
5537 + setup_cmd_submit_pdu(&pdu_header, urb);
5538 + usbip_header_correct_endian(&pdu_header, 1);
5540 + iov[0].iov_base = &pdu_header;
5541 + iov[0].iov_len = sizeof(pdu_header);
5542 + txsize += sizeof(pdu_header);
5544 + /* 2. setup transfer buffer */
5545 + if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) {
5546 + iov[1].iov_base = urb->transfer_buffer;
5547 + iov[1].iov_len = urb->transfer_buffer_length;
5548 + txsize += urb->transfer_buffer_length;
5551 + /* 3. setup iso_packet_descriptor */
5552 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
5555 + iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
5556 + if (!iso_buffer) {
5557 + usbip_event_add(&vdev->ud,
5558 + SDEV_EVENT_ERROR_MALLOC);
5562 + iov[2].iov_base = iso_buffer;
5563 + iov[2].iov_len = len;
5567 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize);
5568 + if (ret != txsize) {
5569 + uerr("sendmsg failed!, retval %d for %zd\n", ret,
5571 + kfree(iso_buffer);
5572 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
5576 + kfree(iso_buffer);
5577 + dbg_vhci_tx("send txdata\n");
5579 + total_size += txsize;
5582 + return total_size;
5586 +/*-------------------------------------------------------------------------*/
5588 +static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev)
5590 + unsigned long flags;
5591 + struct vhci_unlink *unlink, *tmp;
5593 + spin_lock_irqsave(&vdev->priv_lock, flags);
5595 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
5596 + list_move_tail(&unlink->list, &vdev->unlink_rx);
5597 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
5601 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
5606 +static int vhci_send_cmd_unlink(struct vhci_device *vdev)
5608 + struct vhci_unlink *unlink = NULL;
5610 + struct msghdr msg;
5611 + struct kvec iov[3];
5614 + size_t total_size = 0;
5616 + while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) {
5618 + struct usbip_header pdu_header;
5621 + memset(&pdu_header, 0, sizeof(pdu_header));
5622 + memset(&msg, 0, sizeof(msg));
5623 + memset(&iov, 0, sizeof(iov));
5625 + dbg_vhci_tx("setup cmd unlink, %lu \n", unlink->seqnum);
5628 + /* 1. setup usbip_header */
5629 + pdu_header.base.command = USBIP_CMD_UNLINK;
5630 + pdu_header.base.seqnum = unlink->seqnum;
5631 + pdu_header.base.devid = vdev->devid;
5632 + pdu_header.base.ep = 0;
5633 + pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum;
5635 + usbip_header_correct_endian(&pdu_header, 1);
5637 + iov[0].iov_base = &pdu_header;
5638 + iov[0].iov_len = sizeof(pdu_header);
5639 + txsize += sizeof(pdu_header);
5641 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize);
5642 + if (ret != txsize) {
5643 + uerr("sendmsg failed!, retval %d for %zd\n", ret,
5645 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
5650 + dbg_vhci_tx("send txdata\n");
5652 + total_size += txsize;
5655 + return total_size;
5659 +/*-------------------------------------------------------------------------*/
5661 +void vhci_tx_loop(struct usbip_task *ut)
5663 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
5664 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
5667 + if (signal_pending(current)) {
5668 + uinfo("vhci_tx signal catched\n");
5672 + if (vhci_send_cmd_submit(vdev) < 0)
5675 + if (vhci_send_cmd_unlink(vdev) < 0)
5678 + wait_event_interruptible(vdev->waitq_tx,
5679 + (!list_empty(&vdev->priv_tx) ||
5680 + !list_empty(&vdev->unlink_tx)));
5682 + dbg_vhci_tx("pending urbs ?, now wake up\n");