1 From 04679b3489e048cd5dae79e050a3afed8e4e42b6 Mon Sep 17 00:00:00 2001
2 From: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
3 Date: Wed, 9 Jul 2008 14:56:51 -0600
4 Subject: [PATCH 12/23] Staging: USB/IP: add client driver
7 This adds the USB IP client driver
9 Brian Merrell cleaned up a lot of this code and submitted it for
10 inclusion. Greg also did a lot of cleanup.
12 Signed-off-by: Brian G. Merrell <bgmerrell@novell.com>
13 Cc: Takahiro Hirofuchi <hirofuchi@users.sourceforge.net>
14 Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>
16 drivers/staging/usbip/Kconfig | 11 +
17 drivers/staging/usbip/Makefile | 3 +
18 drivers/staging/usbip/vhci.h | 142 ++++
19 drivers/staging/usbip/vhci_hcd.c | 1275 ++++++++++++++++++++++++++++++++++++
20 drivers/staging/usbip/vhci_rx.c | 251 +++++++
21 drivers/staging/usbip/vhci_sysfs.c | 250 +++++++
22 drivers/staging/usbip/vhci_tx.c | 239 +++++++
23 7 files changed, 2171 insertions(+), 0 deletions(-)
24 create mode 100644 drivers/staging/usbip/vhci.h
25 create mode 100644 drivers/staging/usbip/vhci_hcd.c
26 create mode 100644 drivers/staging/usbip/vhci_rx.c
27 create mode 100644 drivers/staging/usbip/vhci_sysfs.c
28 create mode 100644 drivers/staging/usbip/vhci_tx.c
30 diff --git a/drivers/staging/usbip/Kconfig b/drivers/staging/usbip/Kconfig
31 index 37efb5e..c4d68e1 100644
32 --- a/drivers/staging/usbip/Kconfig
33 +++ b/drivers/staging/usbip/Kconfig
34 @@ -12,3 +12,14 @@ config USB_IP_COMMON
35 module will be called usbip_common_mod.
39 +config USB_IP_VHCI_HCD
40 + tristate "USB IP client driver"
41 + depends on USB_IP_COMMON
44 + This enables the USB IP host controller driver which will
45 + run on the client machine.
47 + To compile this driver as a module, choose M here: the
48 + module will be called vhci_hcd.
49 diff --git a/drivers/staging/usbip/Makefile b/drivers/staging/usbip/Makefile
50 index ce925ca..6ef4c39 100644
51 --- a/drivers/staging/usbip/Makefile
52 +++ b/drivers/staging/usbip/Makefile
54 obj-$(CONFIG_USB_IP_COMMON) += usbip_common_mod.o
55 usbip_common_mod-objs := usbip_common.o usbip_event.o
57 +obj-$(CONFIG_USB_IP_VHCI_HCD) += vhci-hcd.o
58 +vhci-hcd-objs := vhci_sysfs.o vhci_tx.o vhci_rx.o vhci_hcd.o
60 ifeq ($(CONFIG_USB_DEBUG),y)
61 EXTRA_CFLAGS += -DDEBUG
63 diff --git a/drivers/staging/usbip/vhci.h b/drivers/staging/usbip/vhci.h
65 index 0000000..5e37517
67 +++ b/drivers/staging/usbip/vhci.h
70 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
72 + * This is free software; you can redistribute it and/or modify
73 + * it under the terms of the GNU General Public License as published by
74 + * the Free Software Foundation; either version 2 of the License, or
75 + * (at your option) any later version.
77 + * This is distributed in the hope that it will be useful,
78 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
79 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
80 + * GNU General Public License for more details.
82 + * You should have received a copy of the GNU General Public License
83 + * along with this program; if not, write to the Free Software
84 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
88 +#include <linux/platform_device.h>
89 +#include "../../usb/core/hcd.h"
93 + struct usb_device *udev;
96 + * devid specifies a remote usb device uniquely instead
97 + * of combination of busnum and devnum.
101 + /* speed of a remote device */
102 + enum usb_device_speed speed;
104 + /* vhci root-hub port to which this device is attached */
107 + struct usbip_device ud;
110 + /* lock for the below link lists */
111 + spinlock_t priv_lock;
113 + /* vhci_priv is linked to one of them. */
114 + struct list_head priv_tx;
115 + struct list_head priv_rx;
117 + /* vhci_unlink is linked to one of them */
118 + struct list_head unlink_tx;
119 + struct list_head unlink_rx;
121 + /* vhci_tx thread sleeps for this queue */
122 + wait_queue_head_t waitq_tx;
126 +/* urb->hcpriv, use container_of() */
128 + unsigned long seqnum;
129 + struct list_head list;
131 + struct vhci_device *vdev;
136 +struct vhci_unlink {
137 + /* seqnum of this request */
138 + unsigned long seqnum;
140 + struct list_head list;
142 + /* seqnum of the unlink target */
143 + unsigned long unlink_seqnum;
147 + * The number of ports is less than 16 ?
148 + * USB_MAXCHILDREN is statically defined to 16 in usb.h. Its maximum value
149 + * would be 31 because the event_bits[1] of struct usb_hub is defined as
150 + * unsigned long in hub.h
152 +#define VHCI_NPORTS 8
154 +/* for usb_bus.hcpriv */
158 + u32 port_status[VHCI_NPORTS];
160 + unsigned resuming:1;
161 + unsigned long re_timeout;
167 + * wIndex shows the port number and begins from 1.
168 + * But, the index of this array begins from 0.
170 + struct vhci_device vdev[VHCI_NPORTS];
172 + /* vhci_device which has not been assiged its address yet */
177 +extern struct vhci_hcd *the_controller;
178 +extern struct attribute_group dev_attr_group;
181 +/*-------------------------------------------------------------------------*/
182 +/* prototype declaration */
185 +void rh_port_connect(int rhport, enum usb_device_speed speed);
186 +void rh_port_disconnect(int rhport);
187 +void vhci_rx_loop(struct usbip_task *ut);
188 +void vhci_tx_loop(struct usbip_task *ut);
190 +#define hardware (&the_controller->pdev.dev)
192 +static inline struct vhci_device *port_to_vdev(__u32 port)
194 + return &the_controller->vdev[port];
197 +static inline struct vhci_hcd *hcd_to_vhci(struct usb_hcd *hcd)
199 + return (struct vhci_hcd *) (hcd->hcd_priv);
202 +static inline struct usb_hcd *vhci_to_hcd(struct vhci_hcd *vhci)
204 + return container_of((void *) vhci, struct usb_hcd, hcd_priv);
207 +static inline struct device *vhci_dev(struct vhci_hcd *vhci)
209 + return vhci_to_hcd(vhci)->self.controller;
211 diff --git a/drivers/staging/usbip/vhci_hcd.c b/drivers/staging/usbip/vhci_hcd.c
213 index 0000000..5b5a2e3
215 +++ b/drivers/staging/usbip/vhci_hcd.c
218 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
220 + * This is free software; you can redistribute it and/or modify
221 + * it under the terms of the GNU General Public License as published by
222 + * the Free Software Foundation; either version 2 of the License, or
223 + * (at your option) any later version.
225 + * This is distributed in the hope that it will be useful,
226 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
227 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
228 + * GNU General Public License for more details.
230 + * You should have received a copy of the GNU General Public License
231 + * along with this program; if not, write to the Free Software
232 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
237 +#include "usbip_common.h"
240 +#define DRIVER_VERSION "1.0"
241 +#define DRIVER_AUTHOR "Takahiro Hirofuchi"
242 +#define DRIVER_DESC "Virtual Host Controller Interface Driver for USB/IP"
243 +#define DRIVER_LICENCE "GPL"
244 +MODULE_AUTHOR(DRIVER_AUTHOR);
245 +MODULE_DESCRIPTION(DRIVER_DESC);
246 +MODULE_LICENSE(DRIVER_LICENCE);
252 + * - update root hub emulation
253 + * - move the emulation code to userland ?
254 + * porting to other operating systems
255 + * minimize kernel code
256 + * - add suspend/resume code
257 + * - clean up everything
261 +/* See usb gadget dummy hcd */
264 +static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
265 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
266 + u16 wIndex, char *buff, u16 wLength);
267 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
269 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
270 +static int vhci_start(struct usb_hcd *vhci_hcd);
271 +static void vhci_stop(struct usb_hcd *hcd);
272 +static int vhci_get_frame_number(struct usb_hcd *hcd);
274 +static const char driver_name[] = "vhci_hcd";
275 +static const char driver_desc[] = "USB/IP Virtual Host Contoroller";
277 +struct vhci_hcd *the_controller;
279 +static const char *bit_desc[] = {
280 + "CONNECTION", /*0*/
283 + "OVER_CURRENT", /*3*/
290 + "HIGHSPEED", /*10*/
291 + "PORT_TEST", /*11*/
292 + "INDICATOR", /*12*/
296 + "C_CONNECTION", /*16*/
298 + "C_SUSPEND", /*18*/
299 + "C_OVER_CURRENT", /*19*/
315 +static void dump_port_status(u32 status)
319 + printk(KERN_DEBUG "status %08x:", status);
320 + for (i = 0; i < 32; i++) {
321 + if (status & (1 << i))
322 + printk(" %s", bit_desc[i]);
330 +void rh_port_connect(int rhport, enum usb_device_speed speed)
332 + unsigned long flags;
334 + dbg_vhci_rh("rh_port_connect %d\n", rhport);
336 + spin_lock_irqsave(&the_controller->lock, flags);
338 + the_controller->port_status[rhport] |= USB_PORT_STAT_CONNECTION
339 + | (1 << USB_PORT_FEAT_C_CONNECTION);
342 + case USB_SPEED_HIGH:
343 + the_controller->port_status[rhport] |= USB_PORT_STAT_HIGH_SPEED;
345 + case USB_SPEED_LOW:
346 + the_controller->port_status[rhport] |= USB_PORT_STAT_LOW_SPEED;
352 + /* spin_lock(&the_controller->vdev[rhport].ud.lock);
353 + * the_controller->vdev[rhport].ud.status = VDEV_CONNECT;
354 + * spin_unlock(&the_controller->vdev[rhport].ud.lock); */
356 + the_controller->pending_port = rhport;
358 + spin_unlock_irqrestore(&the_controller->lock, flags);
360 + usb_hcd_poll_rh_status(vhci_to_hcd(the_controller));
363 +void rh_port_disconnect(int rhport)
365 + unsigned long flags;
367 + dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
369 + spin_lock_irqsave(&the_controller->lock, flags);
370 + /* stop_activity(dum, driver); */
371 + the_controller->port_status[rhport] &= ~USB_PORT_STAT_CONNECTION;
372 + the_controller->port_status[rhport] |=
373 + (1 << USB_PORT_FEAT_C_CONNECTION);
376 + /* not yet complete the disconnection
377 + * spin_lock(&vdev->ud.lock);
378 + * vdev->ud.status = VHC_ST_DISCONNECT;
379 + * spin_unlock(&vdev->ud.lock); */
381 + spin_unlock_irqrestore(&the_controller->lock, flags);
386 +/*----------------------------------------------------------------------*/
388 +#define PORT_C_MASK \
389 + ((USB_PORT_STAT_C_CONNECTION \
390 + | USB_PORT_STAT_C_ENABLE \
391 + | USB_PORT_STAT_C_SUSPEND \
392 + | USB_PORT_STAT_C_OVERCURRENT \
393 + | USB_PORT_STAT_C_RESET) << 16)
396 + * This function is almostly the same as dummy_hcd.c:dummy_hub_status() without
397 + * suspend/resume support. But, it is modified to provide multiple ports.
399 + * @buf: a bitmap to show which port status has been changed.
400 + * bit 0: reserved or used for another purpose?
401 + * bit 1: the status of port 0 has been changed.
402 + * bit 2: the status of port 1 has been changed.
404 + * bit 7: the status of port 6 has been changed.
405 + * bit 8: the status of port 7 has been changed.
407 + * bit 15: the status of port 14 has been changed.
409 + * So, the maximum number of ports is 31 ( port 0 to port 30) ?
411 + * The return value is the actual transfered length in byte. If nothing has
412 + * been changed, return 0. In the case that the number of ports is less than or
413 + * equal to 6 (VHCI_NPORTS==7), return 1.
416 +static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
418 + struct vhci_hcd *vhci;
419 + unsigned long flags;
422 + /* the enough buffer is allocated according to USB_MAXCHILDREN */
423 + unsigned long *event_bits = (unsigned long *) buf;
430 + vhci = hcd_to_vhci(hcd);
432 + spin_lock_irqsave(&vhci->lock, flags);
433 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
434 + dbg_vhci_rh("hw accessible flag in on?\n");
438 + /* check pseudo status register for each port */
439 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
440 + if ((vhci->port_status[rhport] & PORT_C_MASK)) {
441 + /* The status of a port has been changed, */
442 + dbg_vhci_rh("port %d is changed\n", rhport);
444 + *event_bits |= 1 << (rhport + 1);
449 + uinfo("changed %d\n", changed);
451 + if (hcd->state == HC_STATE_SUSPENDED)
452 + usb_hcd_resume_root_hub(hcd);
455 + retval = 1 + (VHCI_NPORTS / 8);
460 + spin_unlock_irqrestore(&vhci->lock, flags);
464 +/* See hub_configure in hub.c */
465 +static inline void hub_descriptor(struct usb_hub_descriptor *desc)
467 + memset(desc, 0, sizeof(*desc));
468 + desc->bDescriptorType = 0x29;
469 + desc->bDescLength = 9;
470 + desc->wHubCharacteristics = (__force __u16)
471 + (__constant_cpu_to_le16(0x0001));
472 + desc->bNbrPorts = VHCI_NPORTS;
473 + desc->bitmap[0] = 0xff;
474 + desc->bitmap[1] = 0xff;
477 +static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
478 + u16 wIndex, char *buf, u16 wLength)
480 + struct vhci_hcd *dum;
482 + unsigned long flags;
485 + u32 prev_port_status[VHCI_NPORTS];
487 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags))
492 + * wIndex shows the port number and begins from 1.
494 + dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
496 + if (wIndex > VHCI_NPORTS)
497 + printk(KERN_ERR "%s: invalid port number %d\n", __func__, wIndex);
498 + rhport = ((__u8)(wIndex & 0x00ff)) - 1;
500 + dum = hcd_to_vhci(hcd);
502 + spin_lock_irqsave(&dum->lock, flags);
504 + /* store old status and compare now and old later */
505 + if (dbg_flag_vhci_rh) {
507 + for (i = 0; i < VHCI_NPORTS; i++)
508 + prev_port_status[i] = dum->port_status[i];
512 + case ClearHubFeature:
513 + dbg_vhci_rh(" ClearHubFeature\n");
515 + case ClearPortFeature:
517 + case USB_PORT_FEAT_SUSPEND:
518 + if (dum->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
519 + /* 20msec signaling */
522 + jiffies + msecs_to_jiffies(20);
525 + case USB_PORT_FEAT_POWER:
526 + dbg_vhci_rh(" ClearPortFeature: USB_PORT_FEAT_POWER\n");
527 + dum->port_status[rhport] = 0;
528 + /* dum->address = 0; */
529 + /* dum->hdev = 0; */
532 + case USB_PORT_FEAT_C_RESET:
533 + dbg_vhci_rh(" ClearPortFeature: "
534 + "USB_PORT_FEAT_C_RESET\n");
535 + switch (dum->vdev[rhport].speed) {
536 + case USB_SPEED_HIGH:
537 + dum->port_status[rhport] |=
538 + USB_PORT_STAT_HIGH_SPEED;
540 + case USB_SPEED_LOW:
541 + dum->port_status[rhport] |=
542 + USB_PORT_STAT_LOW_SPEED;
548 + dbg_vhci_rh(" ClearPortFeature: default %x\n", wValue);
549 + dum->port_status[rhport] &= ~(1 << wValue);
552 + case GetHubDescriptor:
553 + dbg_vhci_rh(" GetHubDescriptor\n");
554 + hub_descriptor((struct usb_hub_descriptor *) buf);
557 + dbg_vhci_rh(" GetHubStatus\n");
558 + *(__le32 *) buf = __constant_cpu_to_le32(0);
560 + case GetPortStatus:
561 + dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
562 + if (wIndex > VHCI_NPORTS || wIndex < 1) {
563 + printk(KERN_ERR "%s: invalid port number %d\n",
568 + /* we do no care of resume. */
570 + /* whoever resets or resumes must GetPortStatus to
573 + if (dum->resuming && time_after(jiffies, dum->re_timeout)) {
574 + printk(KERN_ERR "%s: not yet\n", __func__);
575 + dum->port_status[rhport] |=
576 + (1 << USB_PORT_FEAT_C_SUSPEND);
577 + dum->port_status[rhport] &=
578 + ~(1 << USB_PORT_FEAT_SUSPEND);
580 + dum->re_timeout = 0;
581 + /* if (dum->driver && dum->driver->resume) {
582 + * spin_unlock (&dum->lock);
583 + * dum->driver->resume (&dum->gadget);
584 + * spin_lock (&dum->lock);
588 + if ((dum->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
589 + 0 && time_after(jiffies, dum->re_timeout)) {
590 + dum->port_status[rhport] |=
591 + (1 << USB_PORT_FEAT_C_RESET);
592 + dum->port_status[rhport] &=
593 + ~(1 << USB_PORT_FEAT_RESET);
594 + dum->re_timeout = 0;
596 + if (dum->vdev[rhport].ud.status ==
597 + VDEV_ST_NOTASSIGNED) {
598 + dbg_vhci_rh(" enable rhport %d (status %u)\n",
600 + dum->vdev[rhport].ud.status);
601 + dum->port_status[rhport] |=
602 + USB_PORT_STAT_ENABLE;
607 + dum->port_status[rhport] |=
608 + USB_PORT_STAT_ENABLE;
609 + /* give it the best speed we agree on */
610 + dum->gadget.speed = dum->driver->speed;
611 + dum->gadget.ep0->maxpacket = 64;
612 + switch (dum->gadget.speed) {
613 + case USB_SPEED_HIGH:
614 + dum->port_status[rhport] |=
615 + USB_PORT_STAT_HIGH_SPEED;
617 + case USB_SPEED_LOW:
618 + dum->gadget.ep0->maxpacket = 8;
619 + dum->port_status[rhport] |=
620 + USB_PORT_STAT_LOW_SPEED;
623 + dum->gadget.speed = USB_SPEED_FULL;
630 + ((u16 *) buf)[0] = cpu_to_le16(dum->port_status[rhport]);
632 + cpu_to_le16(dum->port_status[rhport] >> 16);
634 + dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
637 + case SetHubFeature:
638 + dbg_vhci_rh(" SetHubFeature\n");
641 + case SetPortFeature:
643 + case USB_PORT_FEAT_SUSPEND:
644 + dbg_vhci_rh(" SetPortFeature: "
645 + "USB_PORT_FEAT_SUSPEND\n");
646 + printk(KERN_ERR "%s: not yet\n", __func__);
648 + dum->port_status[rhport] |=
649 + (1 << USB_PORT_FEAT_SUSPEND);
650 + if (dum->driver->suspend) {
651 + spin_unlock(&dum->lock);
652 + dum->driver->suspend(&dum->gadget);
653 + spin_lock(&dum->lock);
657 + case USB_PORT_FEAT_RESET:
658 + dbg_vhci_rh(" SetPortFeature: USB_PORT_FEAT_RESET\n");
659 + /* if it's already running, disconnect first */
660 + if (dum->port_status[rhport] & USB_PORT_STAT_ENABLE) {
661 + dum->port_status[rhport] &=
662 + ~(USB_PORT_STAT_ENABLE |
663 + USB_PORT_STAT_LOW_SPEED |
664 + USB_PORT_STAT_HIGH_SPEED);
667 + dev_dbg(hardware, "disconnect\n");
668 + stop_activity(dum, dum->driver);
672 + /* FIXME test that code path! */
674 + /* 50msec reset signaling */
675 + dum->re_timeout = jiffies + msecs_to_jiffies(50);
679 + dbg_vhci_rh(" SetPortFeature: default %d\n", wValue);
680 + dum->port_status[rhport] |= (1 << wValue);
685 + printk(KERN_ERR "%s: default: no such request\n", __func__);
686 + /* dev_dbg (hardware,
687 + * "hub control req%04x v%04x i%04x l%d\n",
688 + * typeReq, wValue, wIndex, wLength); */
690 + /* "protocol stall" on error */
694 + if (dbg_flag_vhci_rh) {
695 + printk(KERN_DEBUG "port %d\n", rhport);
696 + dump_port_status(prev_port_status[rhport]);
697 + dump_port_status(dum->port_status[rhport]);
699 + dbg_vhci_rh(" bye\n");
701 + spin_unlock_irqrestore(&dum->lock, flags);
708 +/*----------------------------------------------------------------------*/
710 +static struct vhci_device *get_vdev(struct usb_device *udev)
717 + for (i = 0; i < VHCI_NPORTS; i++)
718 + if (the_controller->vdev[i].udev == udev)
719 + return port_to_vdev(i);
724 +static void vhci_tx_urb(struct urb *urb)
726 + struct vhci_device *vdev = get_vdev(urb->dev);
727 + struct vhci_priv *priv;
728 + unsigned long flag;
731 + err("could not get virtual device");
736 + spin_lock_irqsave(&vdev->priv_lock, flag);
738 + priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
740 + dev_err(&urb->dev->dev, "malloc vhci_priv\n");
741 + spin_unlock_irqrestore(&vdev->priv_lock, flag);
742 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
746 + priv->seqnum = atomic_inc_return(&the_controller->seqnum);
747 + if (priv->seqnum == 0xffff)
748 + uinfo("seqnum max\n");
753 + urb->hcpriv = (void *) priv;
756 + list_add_tail(&priv->list, &vdev->priv_tx);
758 + wake_up(&vdev->waitq_tx);
759 + spin_unlock_irqrestore(&vdev->priv_lock, flag);
762 +static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
765 + struct device *dev = &urb->dev->dev;
767 + unsigned long flags;
769 + dbg_vhci_hc("enter, usb_hcd %p urb %p mem_flags %d\n",
770 + hcd, urb, mem_flags);
772 + /* patch to usb_sg_init() is in 2.5.60 */
773 + BUG_ON(!urb->transfer_buffer && urb->transfer_buffer_length);
775 + spin_lock_irqsave(&the_controller->lock, flags);
777 + /* check HC is active or not */
778 + if (!HC_IS_RUNNING(hcd->state)) {
779 + dev_err(dev, "HC is not running\n");
780 + spin_unlock_irqrestore(&the_controller->lock, flags);
784 + if (urb->status != -EINPROGRESS) {
785 + dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
786 + spin_unlock_irqrestore(&the_controller->lock, flags);
787 + return urb->status;
790 + ret = usb_hcd_link_urb_to_ep(hcd, urb);
792 + goto no_need_unlink;
795 + * The enumelation process is as follows;
797 + * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
798 + * to get max packet length of default pipe
800 + * 2. Set_Address request to DevAddr(0) EndPoint(0)
804 + if (usb_pipedevice(urb->pipe) == 0) {
805 + __u8 type = usb_pipetype(urb->pipe);
806 + struct usb_ctrlrequest *ctrlreq =
807 + (struct usb_ctrlrequest *) urb->setup_packet;
808 + struct vhci_device *vdev =
809 + port_to_vdev(the_controller->pending_port);
811 + if (type != PIPE_CONTROL || !ctrlreq) {
812 + dev_err(dev, "invalid request to devnum 0\n");
817 + switch (ctrlreq->bRequest) {
818 + case USB_REQ_SET_ADDRESS:
819 + /* set_address may come when a device is reset */
820 + dev_info(dev, "SetAddress Request (%d) to port %d\n",
821 + ctrlreq->wValue, vdev->rhport);
823 + vdev->udev = urb->dev;
825 + spin_lock(&vdev->ud.lock);
826 + vdev->ud.status = VDEV_ST_USED;
827 + spin_unlock(&vdev->ud.lock);
829 + if (urb->status == -EINPROGRESS) {
830 + /* This request is successfully completed. */
831 + /* If not -EINPROGRESS, possibly unlinked. */
837 + case USB_REQ_GET_DESCRIPTOR:
838 + if (ctrlreq->wValue == (USB_DT_DEVICE << 8))
839 + dbg_vhci_hc("Not yet?: "
840 + "Get_Descriptor to device 0 "
841 + "(get max pipe size)\n");
843 + /* FIXME: reference count? (usb_get_dev()) */
844 + vdev->udev = urb->dev;
849 + dev_err(dev, "invalid request to devnum 0 bRequest %u, "
850 + "wValue %u\n", ctrlreq->bRequest,
861 + spin_unlock_irqrestore(&the_controller->lock, flags);
866 + usb_hcd_unlink_urb_from_ep(hcd, urb);
868 + spin_unlock_irqrestore(&the_controller->lock, flags);
870 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
876 + * vhci_rx gives back the urb after receiving the reply of the urb. If an
877 + * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
878 + * back its urb. For the driver unlinking the urb, the content of the urb is
879 + * not important, but the calling to its completion handler is important; the
880 + * completion of unlinking is notified by the completion handler.
885 + * - When vhci_hcd receives RET_SUBMIT,
887 + * - case 1a). the urb of the pdu is not unlinking.
889 + * => just give back the urb
891 + * - case 1b). the urb of the pdu is unlinking.
892 + * - usbip.ko will return a reply of the unlinking request.
893 + * => give back the urb now and go to case 2b).
895 + * - When vhci_hcd receives RET_UNLINK,
897 + * - case 2a). a submit request is still pending in vhci_hcd.
898 + * - urb was really pending in usbip.ko and urb_unlink_urb() was
900 + * => free a pending submit request
901 + * => notify unlink completeness by giving back the urb
903 + * - case 2b). a submit request is *not* pending in vhci_hcd.
904 + * - urb was already given back to the core driver.
905 + * => do not give back the urb
910 + * - When usbip receives CMD_UNLINK,
912 + * - case 3a). the urb of the unlink request is now in submission.
913 + * => do usb_unlink_urb().
914 + * => after the unlink is completed, send RET_UNLINK.
916 + * - case 3b). the urb of the unlink request is not in submission.
917 + * - may be already completed or never be received
918 + * => send RET_UNLINK
921 +static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
923 + unsigned long flags;
924 + struct vhci_priv *priv;
925 + struct vhci_device *vdev;
927 + uinfo("vhci_hcd: dequeue a urb %p\n", urb);
930 + spin_lock_irqsave(&the_controller->lock, flags);
932 + priv = urb->hcpriv;
934 + /* URB was never linked! or will be soon given back by
936 + spin_unlock_irqrestore(&the_controller->lock, flags);
942 + ret = usb_hcd_check_unlink_urb(hcd, urb, status);
944 + spin_unlock_irqrestore(&the_controller->lock, flags);
949 + /* send unlink request here? */
952 + if (!vdev->ud.tcp_socket) {
953 + /* tcp connection is closed */
954 + unsigned long flags2;
956 + spin_lock_irqsave(&vdev->priv_lock, flags2);
958 + uinfo("vhci_hcd: device %p seems to be disconnected\n", vdev);
959 + list_del(&priv->list);
961 + urb->hcpriv = NULL;
963 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
966 + /* tcp connection is alive */
967 + unsigned long flags2;
968 + struct vhci_unlink *unlink;
970 + spin_lock_irqsave(&vdev->priv_lock, flags2);
972 + /* setup CMD_UNLINK pdu */
973 + unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
975 + uerr("malloc vhci_unlink\n");
976 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
977 + spin_unlock_irqrestore(&the_controller->lock, flags);
978 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
982 + unlink->seqnum = atomic_inc_return(&the_controller->seqnum);
983 + if (unlink->seqnum == 0xffff)
984 + uinfo("seqnum max\n");
986 + unlink->unlink_seqnum = priv->seqnum;
988 + uinfo("vhci_hcd: device %p seems to be still connected\n",
991 + /* send cmd_unlink and try to cancel the pending URB in the
993 + list_add_tail(&unlink->list, &vdev->unlink_tx);
994 + wake_up(&vdev->waitq_tx);
996 + spin_unlock_irqrestore(&vdev->priv_lock, flags2);
1001 + * If tcp connection is alive, we have sent CMD_UNLINK.
1002 + * vhci_rx will receive RET_UNLINK and give back the URB.
1003 + * Otherwise, we give back it here.
1005 + if (!vdev->ud.tcp_socket) {
1006 + /* tcp connection is closed */
1007 + uinfo("vhci_hcd: vhci_urb_dequeue() gives back urb %p\n", urb);
1009 + usb_hcd_unlink_urb_from_ep(hcd, urb);
1011 + spin_unlock_irqrestore(&the_controller->lock, flags);
1012 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
1014 + spin_lock_irqsave(&the_controller->lock, flags);
1017 + spin_unlock_irqrestore(&the_controller->lock, flags);
1019 + dbg_vhci_hc("leave\n");
1024 +static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
1026 + struct vhci_unlink *unlink, *tmp;
1028 + spin_lock(&vdev->priv_lock);
1030 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
1031 + list_del(&unlink->list);
1035 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
1036 + list_del(&unlink->list);
1040 + spin_unlock(&vdev->priv_lock);
1044 + * The important thing is that only one context begins cleanup.
1045 + * This is why error handling and cleanup become simple.
1046 + * We do not want to consider race condition as possible.
1048 +static void vhci_shutdown_connection(struct usbip_device *ud)
1050 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1052 + /* need this? see stub_dev.c */
1053 + if (ud->tcp_socket) {
1054 + udbg("shutdown tcp_socket %p\n", ud->tcp_socket);
1055 + kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
1058 + usbip_stop_threads(&vdev->ud);
1059 + uinfo("stop threads\n");
1061 + /* active connection is closed */
1062 + if (vdev->ud.tcp_socket != NULL) {
1063 + sock_release(vdev->ud.tcp_socket);
1064 + vdev->ud.tcp_socket = NULL;
1066 + uinfo("release socket\n");
1068 + vhci_device_unlink_cleanup(vdev);
1071 + * rh_port_disconnect() is a trigger of ...
1072 + * usb_disable_device():
1073 + * disable all the endpoints for a USB device.
1074 + * usb_disable_endpoint():
1075 + * disable endpoints. pending urbs are unlinked(dequeued).
1077 + * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
1078 + * deteched device should release used urbs in a cleanup function(i.e.
1079 + * xxx_disconnect()). Therefore, vhci_hcd does not need to release
1080 + * pushed urbs and their private data in this function.
1082 + * NOTE: vhci_dequeue() must be considered carefully. When shutdowning
1083 + * a connection, vhci_shutdown_connection() expects vhci_dequeue()
1084 + * gives back pushed urbs and frees their private data by request of
1085 + * the cleanup function of a USB driver. When unlinking a urb with an
1086 + * active connection, vhci_dequeue() does not give back the urb which
1087 + * is actually given back by vhci_rx after receiving its return pdu.
1090 + rh_port_disconnect(vdev->rhport);
1092 + uinfo("disconnect device\n");
1096 +static void vhci_device_reset(struct usbip_device *ud)
1098 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1100 + spin_lock(&ud->lock);
1105 + ud->tcp_socket = NULL;
1107 + ud->status = VDEV_ST_NULL;
1109 + spin_unlock(&ud->lock);
1112 +static void vhci_device_unusable(struct usbip_device *ud)
1114 + spin_lock(&ud->lock);
1116 + ud->status = VDEV_ST_ERROR;
1118 + spin_unlock(&ud->lock);
1121 +static void vhci_device_init(struct vhci_device *vdev)
1123 + memset(vdev, 0, sizeof(*vdev));
1125 + usbip_task_init(&vdev->ud.tcp_rx, "vhci_rx", vhci_rx_loop);
1126 + usbip_task_init(&vdev->ud.tcp_tx, "vhci_tx", vhci_tx_loop);
1128 + vdev->ud.side = USBIP_VHCI;
1129 + vdev->ud.status = VDEV_ST_NULL;
1130 + /* vdev->ud.lock = SPIN_LOCK_UNLOCKED; */
1131 + spin_lock_init(&vdev->ud.lock);
1133 + INIT_LIST_HEAD(&vdev->priv_rx);
1134 + INIT_LIST_HEAD(&vdev->priv_tx);
1135 + INIT_LIST_HEAD(&vdev->unlink_tx);
1136 + INIT_LIST_HEAD(&vdev->unlink_rx);
1137 + /* vdev->priv_lock = SPIN_LOCK_UNLOCKED; */
1138 + spin_lock_init(&vdev->priv_lock);
1140 + init_waitqueue_head(&vdev->waitq_tx);
1142 + vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
1143 + vdev->ud.eh_ops.reset = vhci_device_reset;
1144 + vdev->ud.eh_ops.unusable = vhci_device_unusable;
1146 + usbip_start_eh(&vdev->ud);
1150 +/*----------------------------------------------------------------------*/
1152 +static int vhci_start(struct usb_hcd *hcd)
1154 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1158 + dbg_vhci_hc("enter vhci_start\n");
1161 + /* initialize private data of usb_hcd */
1163 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++) {
1164 + struct vhci_device *vdev = &vhci->vdev[rhport];
1165 + vhci_device_init(vdev);
1166 + vdev->rhport = rhport;
1169 + atomic_set(&vhci->seqnum, 0);
1170 + spin_lock_init(&vhci->lock);
1174 + hcd->power_budget = 0; /* no limit */
1175 + hcd->state = HC_STATE_RUNNING;
1176 + hcd->uses_new_polling = 1;
1179 + /* vhci_hcd is now ready to be controlled through sysfs */
1180 + err = sysfs_create_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
1182 + uerr("create sysfs files\n");
1189 +static void vhci_stop(struct usb_hcd *hcd)
1191 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1194 + dbg_vhci_hc("stop VHCI controller\n");
1197 + /* 1. remove the userland interface of vhci_hcd */
1198 + sysfs_remove_group(&vhci_dev(vhci)->kobj, &dev_attr_group);
1200 + /* 2. shutdown all the ports of vhci_hcd */
1201 + for (rhport = 0 ; rhport < VHCI_NPORTS; rhport++) {
1202 + struct vhci_device *vdev = &vhci->vdev[rhport];
1204 + usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1205 + usbip_stop_eh(&vdev->ud);
1209 + uinfo("vhci_stop done\n");
1212 +/*----------------------------------------------------------------------*/
1214 +static int vhci_get_frame_number(struct usb_hcd *hcd)
1216 + uerr("Not yet implemented\n");
1223 +/* FIXME: suspend/resume */
1224 +static int vhci_bus_suspend(struct usb_hcd *hcd)
1226 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1228 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1230 + spin_lock_irq(&vhci->lock);
1231 + /* vhci->rh_state = DUMMY_RH_SUSPENDED;
1232 + * set_link_state(vhci); */
1233 + hcd->state = HC_STATE_SUSPENDED;
1234 + spin_unlock_irq(&vhci->lock);
1239 +static int vhci_bus_resume(struct usb_hcd *hcd)
1241 + struct vhci_hcd *vhci = hcd_to_vhci(hcd);
1244 + dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1246 + spin_lock_irq(&vhci->lock);
1247 + if (!test_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags)) {
1250 + /* vhci->rh_state = DUMMY_RH_RUNNING;
1251 + * set_link_state(vhci);
1252 + * if (!list_empty(&vhci->urbp_list))
1253 + * mod_timer(&vhci->timer, jiffies); */
1254 + hcd->state = HC_STATE_RUNNING;
1256 + spin_unlock_irq(&vhci->lock);
1264 +#define vhci_bus_suspend NULL
1265 +#define vhci_bus_resume NULL
1270 +static struct hc_driver vhci_hc_driver = {
1271 + .description = driver_name,
1272 + .product_desc = driver_desc,
1273 + .hcd_priv_size = sizeof(struct vhci_hcd),
1275 + .flags = HCD_USB2,
1277 + .start = vhci_start,
1278 + .stop = vhci_stop,
1280 + .urb_enqueue = vhci_urb_enqueue,
1281 + .urb_dequeue = vhci_urb_dequeue,
1283 + .get_frame_number = vhci_get_frame_number,
1285 + .hub_status_data = vhci_hub_status,
1286 + .hub_control = vhci_hub_control,
1287 + .bus_suspend = vhci_bus_suspend,
1288 + .bus_resume = vhci_bus_resume,
1291 +static int vhci_hcd_probe(struct platform_device *pdev)
1293 + struct usb_hcd *hcd;
1296 + uinfo("proving...\n");
1298 + dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1300 + /* will be removed */
1301 + if (pdev->dev.dma_mask) {
1302 + dev_info(&pdev->dev, "vhci_hcd DMA not supported\n");
1307 + * Allocate and initialize hcd.
1308 + * Our private data is also allocated automatically.
1310 + hcd = usb_create_hcd(&vhci_hc_driver, &pdev->dev, pdev->dev.bus_id);
1312 + uerr("create hcd failed\n");
1317 + /* this is private data for vhci_hcd */
1318 + the_controller = hcd_to_vhci(hcd);
1321 + * Finish generic HCD structure initialization and register.
1322 + * Call the driver's reset() and start() routines.
1324 + ret = usb_add_hcd(hcd, 0, 0);
1326 + uerr("usb_add_hcd failed %d\n", ret);
1328 + the_controller = NULL;
1333 + dbg_vhci_hc("bye\n");
1338 +static int vhci_hcd_remove(struct platform_device *pdev)
1340 + struct usb_hcd *hcd;
1342 + hcd = platform_get_drvdata(pdev);
1347 + * Disconnects the root hub,
1348 + * then reverses the effects of usb_add_hcd(),
1349 + * invoking the HCD's stop() methods.
1351 + usb_remove_hcd(hcd);
1353 + the_controller = NULL;
1363 +/* what should happen for USB/IP under suspend/resume? */
1364 +static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1366 + struct usb_hcd *hcd;
1368 + int connected = 0;
1371 + dev_dbg(&pdev->dev, "%s\n", __func__);
1373 + hcd = platform_get_drvdata(pdev);
1375 + spin_lock(&the_controller->lock);
1377 + for (rhport = 0; rhport < VHCI_NPORTS; rhport++)
1378 + if (the_controller->port_status[rhport] &
1379 + USB_PORT_STAT_CONNECTION)
1382 + spin_unlock(&the_controller->lock);
1384 + if (connected > 0) {
1385 + uinfo("We have %d active connection%s. Do not suspend.\n",
1386 + connected, (connected == 1 ? "" : "s"));
1389 + uinfo("suspend vhci_hcd");
1390 + clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1396 +static int vhci_hcd_resume(struct platform_device *pdev)
1398 + struct usb_hcd *hcd;
1400 + dev_dbg(&pdev->dev, "%s\n", __func__);
1402 + hcd = platform_get_drvdata(pdev);
1403 + set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1404 + usb_hcd_poll_rh_status(hcd);
1411 +#define vhci_hcd_suspend NULL
1412 +#define vhci_hcd_resume NULL
1417 +static struct platform_driver vhci_driver = {
1418 + .probe = vhci_hcd_probe,
1419 + .remove = __devexit_p(vhci_hcd_remove),
1420 + .suspend = vhci_hcd_suspend,
1421 + .resume = vhci_hcd_resume,
1423 + .name = (char *) driver_name,
1424 + .owner = THIS_MODULE,
1428 +/*----------------------------------------------------------------------*/
1431 + * The VHCI 'device' is 'virtual'; not a real plug&play hardware.
1432 + * We need to add this virtual device as a platform device arbitrarily:
1433 + * 1. platform_device_register()
1435 +static void the_pdev_release(struct device *dev)
1440 +static struct platform_device the_pdev = {
1441 + /* should be the same name as driver_name */
1442 + .name = (char *) driver_name,
1445 + /* .driver = &vhci_driver, */
1446 + .release = the_pdev_release,
1450 +static int __init vhci_init(void)
1454 + dbg_vhci_hc("enter\n");
1455 + if (usb_disabled())
1458 + printk(KERN_INFO KBUILD_MODNAME ": %s, %s\n", driver_name,
1461 + ret = platform_driver_register(&vhci_driver);
1463 + goto err_driver_register;
1465 + ret = platform_device_register(&the_pdev);
1467 + goto err_platform_device_register;
1469 + dbg_vhci_hc("bye\n");
1472 + /* error occurred */
1473 +err_platform_device_register:
1474 + platform_driver_unregister(&vhci_driver);
1476 +err_driver_register:
1477 + dbg_vhci_hc("bye\n");
1480 +module_init(vhci_init);
1482 +static void __exit vhci_cleanup(void)
1484 + dbg_vhci_hc("enter\n");
1486 + platform_device_unregister(&the_pdev);
1487 + platform_driver_unregister(&vhci_driver);
1489 + dbg_vhci_hc("bye\n");
1491 +module_exit(vhci_cleanup);
1492 diff --git a/drivers/staging/usbip/vhci_rx.c b/drivers/staging/usbip/vhci_rx.c
1493 new file mode 100644
1494 index 0000000..933ccaf
1496 +++ b/drivers/staging/usbip/vhci_rx.c
1499 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1501 + * This is free software; you can redistribute it and/or modify
1502 + * it under the terms of the GNU General Public License as published by
1503 + * the Free Software Foundation; either version 2 of the License, or
1504 + * (at your option) any later version.
1506 + * This is distributed in the hope that it will be useful,
1507 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1508 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1509 + * GNU General Public License for more details.
1511 + * You should have received a copy of the GNU General Public License
1512 + * along with this program; if not, write to the Free Software
1513 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1517 +#include "usbip_common.h"
1521 +/* get URB from transmitted urb queue */
1522 +static struct urb *pickup_urb_and_free_priv(struct vhci_device *vdev,
1525 + struct vhci_priv *priv, *tmp;
1526 + struct urb *urb = NULL;
1529 + spin_lock(&vdev->priv_lock);
1531 + list_for_each_entry_safe(priv, tmp, &vdev->priv_rx, list) {
1532 + if (priv->seqnum == seqnum) {
1534 + status = urb->status;
1536 + dbg_vhci_rx("find urb %p vurb %p seqnum %u\n",
1537 + urb, priv, seqnum);
1539 + /* TODO: fix logic here to improve indent situtation */
1540 + if (status != -EINPROGRESS) {
1541 + if (status == -ENOENT ||
1542 + status == -ECONNRESET)
1543 + dev_info(&urb->dev->dev,
1544 + "urb %p was unlinked "
1545 + "%ssynchronuously.\n", urb,
1546 + status == -ENOENT ? "" : "a");
1548 + dev_info(&urb->dev->dev,
1549 + "urb %p may be in a error, "
1550 + "status %d\n", urb, status);
1553 + list_del(&priv->list);
1555 + urb->hcpriv = NULL;
1561 + spin_unlock(&vdev->priv_lock);
1566 +static void vhci_recv_ret_submit(struct vhci_device *vdev,
1567 + struct usbip_header *pdu)
1569 + struct usbip_device *ud = &vdev->ud;
1573 + urb = pickup_urb_and_free_priv(vdev, pdu->base.seqnum);
1577 + uerr("cannot find a urb of seqnum %u\n", pdu->base.seqnum);
1578 + uinfo("max seqnum %d\n", atomic_read(&the_controller->seqnum));
1579 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1584 + /* unpack the pdu to a urb */
1585 + usbip_pack_pdu(pdu, urb, USBIP_RET_SUBMIT, 0);
1588 + /* recv transfer buffer */
1589 + if (usbip_recv_xbuff(ud, urb) < 0)
1593 + /* recv iso_packet_descriptor */
1594 + if (usbip_recv_iso(ud, urb) < 0)
1598 + if (dbg_flag_vhci_rx)
1599 + usbip_dump_urb(urb);
1602 + dbg_vhci_rx("now giveback urb %p\n", urb);
1604 + spin_lock(&the_controller->lock);
1605 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
1606 + spin_unlock(&the_controller->lock);
1608 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb, urb->status);
1611 + dbg_vhci_rx("Leave\n");
1617 +static struct vhci_unlink *dequeue_pending_unlink(struct vhci_device *vdev,
1618 + struct usbip_header *pdu)
1620 + struct vhci_unlink *unlink, *tmp;
1622 + spin_lock(&vdev->priv_lock);
1624 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_rx, list) {
1625 + uinfo("unlink->seqnum %lu\n", unlink->seqnum);
1626 + if (unlink->seqnum == pdu->base.seqnum) {
1627 + dbg_vhci_rx("found pending unlink, %lu\n",
1629 + list_del(&unlink->list);
1631 + spin_unlock(&vdev->priv_lock);
1636 + spin_unlock(&vdev->priv_lock);
1642 +static void vhci_recv_ret_unlink(struct vhci_device *vdev,
1643 + struct usbip_header *pdu)
1645 + struct vhci_unlink *unlink;
1648 + usbip_dump_header(pdu);
1650 + unlink = dequeue_pending_unlink(vdev, pdu);
1652 + uinfo("cannot find the pending unlink %u\n", pdu->base.seqnum);
1656 + urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
1659 + * I get the result of a unlink request. But, it seems that I
1660 + * already received the result of its submit result and gave
1663 + uinfo("the urb (seqnum %d) was already given backed\n",
1664 + pdu->base.seqnum);
1666 + dbg_vhci_rx("now giveback urb %p\n", urb);
1668 + /* If unlink is succeed, status is -ECONNRESET */
1669 + urb->status = pdu->u.ret_unlink.status;
1670 + uinfo("%d\n", urb->status);
1672 + spin_lock(&the_controller->lock);
1673 + usb_hcd_unlink_urb_from_ep(vhci_to_hcd(the_controller), urb);
1674 + spin_unlock(&the_controller->lock);
1676 + usb_hcd_giveback_urb(vhci_to_hcd(the_controller), urb,
1686 +static void vhci_rx_pdu(struct usbip_device *ud)
1689 + struct usbip_header pdu;
1690 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1693 + dbg_vhci_rx("Enter\n");
1695 + memset(&pdu, 0, sizeof(pdu));
1698 + /* 1. receive a pdu header */
1699 + ret = usbip_xmit(0, ud->tcp_socket, (char *) &pdu, sizeof(pdu), 0);
1700 + if (ret != sizeof(pdu)) {
1701 + uerr("receiving pdu failed! size is %d, should be %d\n",
1702 + ret, sizeof(pdu));
1703 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1707 + usbip_header_correct_endian(&pdu, 0);
1709 + if (dbg_flag_vhci_rx)
1710 + usbip_dump_header(&pdu);
1712 + switch (pdu.base.command) {
1713 + case USBIP_RET_SUBMIT:
1714 + vhci_recv_ret_submit(vdev, &pdu);
1716 + case USBIP_RET_UNLINK:
1717 + vhci_recv_ret_unlink(vdev, &pdu);
1721 + uerr("unknown pdu %u\n", pdu.base.command);
1722 + usbip_dump_header(&pdu);
1723 + usbip_event_add(ud, VDEV_EVENT_ERROR_TCP);
1728 +/*-------------------------------------------------------------------------*/
1730 +void vhci_rx_loop(struct usbip_task *ut)
1732 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_rx);
1736 + if (signal_pending(current)) {
1737 + dbg_vhci_rx("signal catched!\n");
1742 + if (usbip_event_happend(ud))
1749 diff --git a/drivers/staging/usbip/vhci_sysfs.c b/drivers/staging/usbip/vhci_sysfs.c
1750 new file mode 100644
1751 index 0000000..24c2851
1753 +++ b/drivers/staging/usbip/vhci_sysfs.c
1756 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
1758 + * This is free software; you can redistribute it and/or modify
1759 + * it under the terms of the GNU General Public License as published by
1760 + * the Free Software Foundation; either version 2 of the License, or
1761 + * (at your option) any later version.
1763 + * This is distributed in the hope that it will be useful,
1764 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
1765 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1766 + * GNU General Public License for more details.
1768 + * You should have received a copy of the GNU General Public License
1769 + * along with this program; if not, write to the Free Software
1770 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
1774 +#include "usbip_common.h"
1777 +#include <linux/in.h>
1779 +/* TODO: refine locking ?*/
1781 +/* Sysfs entry to show port status */
1782 +static ssize_t show_status(struct device *dev, struct device_attribute *attr,
1788 + if (!the_controller || !out)
1791 + spin_lock(&the_controller->lock);
1795 + * prt sta spd dev socket local_busid
1796 + * 000 004 000 000 c5a7bb80 1-2.3
1797 + * 001 004 000 000 d8cee980 2-3.4
1799 + * IP address can be retrieved from a socket pointer address by looking
1800 + * up /proc/net/{tcp,tcp6}. Also, a userland program may remember a
1801 + * port number and its peer IP address.
1803 + out += sprintf(out, "prt sta spd bus dev socket "
1806 + for (i = 0; i < VHCI_NPORTS; i++) {
1807 + struct vhci_device *vdev = port_to_vdev(i);
1809 + spin_lock(&vdev->ud.lock);
1811 + out += sprintf(out, "%03u %03u ", i, vdev->ud.status);
1813 + if (vdev->ud.status == VDEV_ST_USED) {
1814 + out += sprintf(out, "%03u %08x ",
1815 + vdev->speed, vdev->devid);
1816 + out += sprintf(out, "%16p ", vdev->ud.tcp_socket);
1817 + out += sprintf(out, "%s", vdev->udev->dev.bus_id);
1820 + out += sprintf(out, "000 000 000 0000000000000000 0-0");
1822 + out += sprintf(out, "\n");
1824 + spin_unlock(&vdev->ud.lock);
1827 + spin_unlock(&the_controller->lock);
1831 +static DEVICE_ATTR(status, S_IRUGO, show_status, NULL);
1833 +/* Sysfs entry to shutdown a virtual connection */
1834 +static int vhci_port_disconnect(__u32 rhport)
1836 + struct vhci_device *vdev;
1838 + dbg_vhci_sysfs("enter\n");
1841 + spin_lock(&the_controller->lock);
1843 + vdev = port_to_vdev(rhport);
1845 + spin_lock(&vdev->ud.lock);
1846 + if (vdev->ud.status == VDEV_ST_NULL) {
1847 + uerr("not connected %d\n", vdev->ud.status);
1850 + spin_unlock(&vdev->ud.lock);
1851 + spin_unlock(&the_controller->lock);
1857 + spin_unlock(&vdev->ud.lock);
1858 + spin_unlock(&the_controller->lock);
1860 + usbip_event_add(&vdev->ud, VDEV_EVENT_DOWN);
1865 +static ssize_t store_detach(struct device *dev, struct device_attribute *attr,
1866 + const char *buf, size_t count)
1871 + sscanf(buf, "%u", &rhport);
1873 + /* check rhport */
1874 + if (rhport >= VHCI_NPORTS) {
1875 + uerr("invalid port %u\n", rhport);
1879 + err = vhci_port_disconnect(rhport);
1883 + dbg_vhci_sysfs("Leave\n");
1886 +static DEVICE_ATTR(detach, S_IWUSR, NULL, store_detach);
1888 +/* Sysfs entry to establish a virtual connection */
1889 +static int valid_args(__u32 rhport, enum usb_device_speed speed)
1891 + /* check rhport */
1892 + if ((rhport < 0) || (rhport >= VHCI_NPORTS)) {
1893 + uerr("port %u\n", rhport);
1899 + case USB_SPEED_LOW:
1900 + case USB_SPEED_FULL:
1901 + case USB_SPEED_HIGH:
1902 + case USB_SPEED_VARIABLE:
1905 + uerr("speed %d\n", speed);
1913 + * To start a new USB/IP attachment, a userland program needs to setup a TCP
1914 + * connection and then write its socket descriptor with remote device
1915 + * information into this sysfs file.
1917 + * A remote device is virtually attached to the root-hub port of @rhport with
1918 + * @speed. @devid is embedded into a request to specify the remote device in a
1921 + * write() returns 0 on success, else negative errno.
1923 +static ssize_t store_attach(struct device *dev, struct device_attribute *attr,
1924 + const char *buf, size_t count)
1926 + struct vhci_device *vdev;
1927 + struct socket *socket;
1929 + __u32 rhport = 0, devid = 0, speed = 0;
1932 + * @rhport: port number of vhci_hcd
1933 + * @sockfd: socket descriptor of an established TCP connection
1934 + * @devid: unique device identifier in a remote host
1935 + * @speed: usb device speed in a remote host
1937 + sscanf(buf, "%u %u %u %u", &rhport, &sockfd, &devid, &speed);
1939 + dbg_vhci_sysfs("rhport(%u) sockfd(%u) devid(%u) speed(%u)\n",
1940 + rhport, sockfd, devid, speed);
1943 + /* check received parameters */
1944 + if (valid_args(rhport, speed) < 0)
1947 + /* check sockfd */
1948 + socket = sockfd_to_socket(sockfd);
1952 + /* now need lock until setting vdev status as used */
1954 + /* begin a lock */
1955 + spin_lock(&the_controller->lock);
1957 + vdev = port_to_vdev(rhport);
1959 + spin_lock(&vdev->ud.lock);
1961 + if (vdev->ud.status != VDEV_ST_NULL) {
1962 + /* end of the lock */
1963 + spin_unlock(&vdev->ud.lock);
1964 + spin_unlock(&the_controller->lock);
1966 + uerr("port %d already used\n", rhport);
1970 + uinfo("rhport(%u) sockfd(%d) devid(%u) speed(%u)\n",
1971 + rhport, sockfd, devid, speed);
1973 + vdev->devid = devid;
1974 + vdev->speed = speed;
1975 + vdev->ud.tcp_socket = socket;
1976 + vdev->ud.status = VDEV_ST_NOTASSIGNED;
1978 + spin_unlock(&vdev->ud.lock);
1979 + spin_unlock(&the_controller->lock);
1980 + /* end the lock */
1983 + * this function will sleep, so should be out of the lock. but, it's ok
1984 + * because we already marked vdev as being used. really?
1986 + usbip_start_threads(&vdev->ud);
1988 + rh_port_connect(rhport, speed);
1992 +static DEVICE_ATTR(attach, S_IWUSR, NULL, store_attach);
1994 +static struct attribute *dev_attrs[] = {
1995 + &dev_attr_status.attr,
1996 + &dev_attr_detach.attr,
1997 + &dev_attr_attach.attr,
1998 + &dev_attr_usbip_debug.attr,
2002 +struct attribute_group dev_attr_group = {
2003 + .attrs = dev_attrs,
2005 diff --git a/drivers/staging/usbip/vhci_tx.c b/drivers/staging/usbip/vhci_tx.c
2006 new file mode 100644
2007 index 0000000..1f552a9
2009 +++ b/drivers/staging/usbip/vhci_tx.c
2012 + * Copyright (C) 2003-2008 Takahiro Hirofuchi
2014 + * This is free software; you can redistribute it and/or modify
2015 + * it under the terms of the GNU General Public License as published by
2016 + * the Free Software Foundation; either version 2 of the License, or
2017 + * (at your option) any later version.
2019 + * This is distributed in the hope that it will be useful,
2020 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
2021 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2022 + * GNU General Public License for more details.
2024 + * You should have received a copy of the GNU General Public License
2025 + * along with this program; if not, write to the Free Software
2026 + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
2030 +#include "usbip_common.h"
2034 +static void setup_cmd_submit_pdu(struct usbip_header *pdup, struct urb *urb)
2036 + struct vhci_priv *priv = ((struct vhci_priv *)urb->hcpriv);
2037 + struct vhci_device *vdev = priv->vdev;
2039 + dbg_vhci_tx("URB, local devnum %u, remote devid %u\n",
2040 + usb_pipedevice(urb->pipe), vdev->devid);
2042 + pdup->base.command = USBIP_CMD_SUBMIT;
2043 + pdup->base.seqnum = priv->seqnum;
2044 + pdup->base.devid = vdev->devid;
2045 + if (usb_pipein(urb->pipe))
2046 + pdup->base.direction = USBIP_DIR_IN;
2048 + pdup->base.direction = USBIP_DIR_OUT;
2049 + pdup->base.ep = usb_pipeendpoint(urb->pipe);
2051 + usbip_pack_pdu(pdup, urb, USBIP_CMD_SUBMIT, 1);
2053 + if (urb->setup_packet)
2054 + memcpy(pdup->u.cmd_submit.setup, urb->setup_packet, 8);
2057 +static struct vhci_priv *dequeue_from_priv_tx(struct vhci_device *vdev)
2059 + unsigned long flags;
2060 + struct vhci_priv *priv, *tmp;
2062 + spin_lock_irqsave(&vdev->priv_lock, flags);
2064 + list_for_each_entry_safe(priv, tmp, &vdev->priv_tx, list) {
2065 + list_move_tail(&priv->list, &vdev->priv_rx);
2066 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2070 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2077 +static int vhci_send_cmd_submit(struct vhci_device *vdev)
2079 + struct vhci_priv *priv = NULL;
2081 + struct msghdr msg;
2082 + struct kvec iov[3];
2085 + size_t total_size = 0;
2087 + while ((priv = dequeue_from_priv_tx(vdev)) != NULL) {
2089 + struct urb *urb = priv->urb;
2090 + struct usbip_header pdu_header;
2091 + void *iso_buffer = NULL;
2094 + memset(&pdu_header, 0, sizeof(pdu_header));
2095 + memset(&msg, 0, sizeof(msg));
2096 + memset(&iov, 0, sizeof(iov));
2098 + dbg_vhci_tx("setup txdata urb %p\n", urb);
2101 + /* 1. setup usbip_header */
2102 + setup_cmd_submit_pdu(&pdu_header, urb);
2103 + usbip_header_correct_endian(&pdu_header, 1);
2105 + iov[0].iov_base = &pdu_header;
2106 + iov[0].iov_len = sizeof(pdu_header);
2107 + txsize += sizeof(pdu_header);
2109 + /* 2. setup transfer buffer */
2110 + if (!usb_pipein(urb->pipe) && urb->transfer_buffer_length > 0) {
2111 + iov[1].iov_base = urb->transfer_buffer;
2112 + iov[1].iov_len = urb->transfer_buffer_length;
2113 + txsize += urb->transfer_buffer_length;
2116 + /* 3. setup iso_packet_descriptor */
2117 + if (usb_pipetype(urb->pipe) == PIPE_ISOCHRONOUS) {
2120 + iso_buffer = usbip_alloc_iso_desc_pdu(urb, &len);
2121 + if (!iso_buffer) {
2122 + usbip_event_add(&vdev->ud,
2123 + SDEV_EVENT_ERROR_MALLOC);
2127 + iov[2].iov_base = iso_buffer;
2128 + iov[2].iov_len = len;
2132 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 3, txsize);
2133 + if (ret != txsize) {
2134 + uerr("sendmsg failed!, retval %d for %zd\n", ret,
2136 + kfree(iso_buffer);
2137 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
2141 + kfree(iso_buffer);
2142 + dbg_vhci_tx("send txdata\n");
2144 + total_size += txsize;
2147 + return total_size;
2151 +/*-------------------------------------------------------------------------*/
2153 +static struct vhci_unlink *dequeue_from_unlink_tx(struct vhci_device *vdev)
2155 + unsigned long flags;
2156 + struct vhci_unlink *unlink, *tmp;
2158 + spin_lock_irqsave(&vdev->priv_lock, flags);
2160 + list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
2161 + list_move_tail(&unlink->list, &vdev->unlink_rx);
2162 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2166 + spin_unlock_irqrestore(&vdev->priv_lock, flags);
2171 +static int vhci_send_cmd_unlink(struct vhci_device *vdev)
2173 + struct vhci_unlink *unlink = NULL;
2175 + struct msghdr msg;
2176 + struct kvec iov[3];
2179 + size_t total_size = 0;
2181 + while ((unlink = dequeue_from_unlink_tx(vdev)) != NULL) {
2183 + struct usbip_header pdu_header;
2186 + memset(&pdu_header, 0, sizeof(pdu_header));
2187 + memset(&msg, 0, sizeof(msg));
2188 + memset(&iov, 0, sizeof(iov));
2190 + dbg_vhci_tx("setup cmd unlink, %lu \n", unlink->seqnum);
2193 + /* 1. setup usbip_header */
2194 + pdu_header.base.command = USBIP_CMD_UNLINK;
2195 + pdu_header.base.seqnum = unlink->seqnum;
2196 + pdu_header.base.devid = vdev->devid;
2197 + pdu_header.base.ep = 0;
2198 + pdu_header.u.cmd_unlink.seqnum = unlink->unlink_seqnum;
2200 + usbip_header_correct_endian(&pdu_header, 1);
2202 + iov[0].iov_base = &pdu_header;
2203 + iov[0].iov_len = sizeof(pdu_header);
2204 + txsize += sizeof(pdu_header);
2206 + ret = kernel_sendmsg(vdev->ud.tcp_socket, &msg, iov, 1, txsize);
2207 + if (ret != txsize) {
2208 + uerr("sendmsg failed!, retval %d for %zd\n", ret,
2210 + usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_TCP);
2215 + dbg_vhci_tx("send txdata\n");
2217 + total_size += txsize;
2220 + return total_size;
2224 +/*-------------------------------------------------------------------------*/
2226 +void vhci_tx_loop(struct usbip_task *ut)
2228 + struct usbip_device *ud = container_of(ut, struct usbip_device, tcp_tx);
2229 + struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
2232 + if (signal_pending(current)) {
2233 + uinfo("vhci_tx signal catched\n");
2237 + if (vhci_send_cmd_submit(vdev) < 0)
2240 + if (vhci_send_cmd_unlink(vdev) < 0)
2243 + wait_event_interruptible(vdev->waitq_tx,
2244 + (!list_empty(&vdev->priv_tx) ||
2245 + !list_empty(&vdev->unlink_tx)));
2247 + dbg_vhci_tx("pending urbs ?, now wake up\n");