2 * Copyright (C) 2003-2008 Takahiro Hirofuchi
3 * Copyright (C) 2015-2016 Nobuo Iwata
5 * This is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
21 #include <linux/init.h>
22 #include <linux/file.h>
23 #include <linux/kernel.h>
24 #include <linux/kthread.h>
25 #include <linux/module.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
29 #include "usbip_common.h"
32 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
33 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
37 * - update root hub emulation
38 * - move the emulation code to userland ?
39 * porting to other operating systems
40 * minimize kernel code
41 * - add suspend/resume code
42 * - clean up everything
45 /* See usb gadget dummy hcd */
47 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
48 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
49 u16 wIndex, char *buff, u16 wLength);
50 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
52 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
53 static int vhci_start(struct usb_hcd *vhci_hcd);
54 static void vhci_stop(struct usb_hcd *hcd);
55 static int vhci_get_frame_number(struct usb_hcd *hcd);
57 static const char driver_name[] = "vhci_hcd";
58 static const char driver_desc[] = "USB/IP Virtual Host Controller";
60 int vhci_num_controllers = VHCI_NR_HCS;
63 static const char * const bit_desc[] = {
80 "C_CONNECTION", /*16*/
83 "C_OVER_CURRENT", /*19*/
98 static const char * const bit_desc_ss[] = {
102 "OVER_CURRENT", /*3*/
115 "C_CONNECTION", /*16*/
118 "C_OVER_CURRENT", /*19*/
121 "C_LINK_STATE", /*22*/
122 "C_CONFIG_ERROR", /*23*/
133 static void dump_port_status_diff(u32 prev_status, u32 new_status, bool usb3)
137 const char * const *desc = bit_desc;
142 pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
144 u32 prev = prev_status & bit;
145 u32 new = new_status & bit;
150 else if (prev && !new)
156 pr_debug(" %c%s\n", change, desc[i]);
158 if (bit == 1) /* USB_PORT_STAT_CONNECTION */
159 pr_debug(" %c%s\n", change, "USB_PORT_STAT_SPEED_5GBPS");
167 void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed)
169 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
170 struct vhci *vhci = vhci_hcd->vhci;
171 int rhport = vdev->rhport;
175 usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
177 spin_lock_irqsave(&vhci->lock, flags);
179 status = vhci_hcd->port_status[rhport];
181 status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION);
185 status |= USB_PORT_STAT_HIGH_SPEED;
188 status |= USB_PORT_STAT_LOW_SPEED;
194 vhci_hcd->port_status[rhport] = status;
196 spin_unlock_irqrestore(&vhci->lock, flags);
198 usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
201 static void rh_port_disconnect(struct vhci_device *vdev)
203 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
204 struct vhci *vhci = vhci_hcd->vhci;
205 int rhport = vdev->rhport;
209 usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
211 spin_lock_irqsave(&vhci->lock, flags);
213 status = vhci_hcd->port_status[rhport];
215 status &= ~USB_PORT_STAT_CONNECTION;
216 status |= (1 << USB_PORT_FEAT_C_CONNECTION);
218 vhci_hcd->port_status[rhport] = status;
220 spin_unlock_irqrestore(&vhci->lock, flags);
221 usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
224 #define PORT_C_MASK \
225 ((USB_PORT_STAT_C_CONNECTION \
226 | USB_PORT_STAT_C_ENABLE \
227 | USB_PORT_STAT_C_SUSPEND \
228 | USB_PORT_STAT_C_OVERCURRENT \
229 | USB_PORT_STAT_C_RESET) << 16)
232 * Returns 0 if the status hasn't changed, or the number of bytes in buf.
233 * Ports are 0-indexed from the HCD point of view,
234 * and 1-indexed from the USB core pointer of view.
236 * @buf: a bitmap to show which port status has been changed.
238 * bit 1: the status of port 0 has been changed.
239 * bit 2: the status of port 1 has been changed.
242 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
244 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
245 struct vhci *vhci = vhci_hcd->vhci;
246 int retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8);
251 memset(buf, 0, retval);
253 spin_lock_irqsave(&vhci->lock, flags);
254 if (!HCD_HW_ACCESSIBLE(hcd)) {
255 usbip_dbg_vhci_rh("hw accessible flag not on?\n");
259 /* check pseudo status register for each port */
260 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
261 if ((vhci_hcd->port_status[rhport] & PORT_C_MASK)) {
262 /* The status of a port has been changed, */
263 usbip_dbg_vhci_rh("port %d status changed\n", rhport);
265 buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
270 if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
271 usb_hcd_resume_root_hub(hcd);
274 spin_unlock_irqrestore(&vhci->lock, flags);
275 return changed ? retval : 0;
278 /* usb 3.0 root hub device descriptor */
280 struct usb_bos_descriptor bos;
281 struct usb_ss_cap_descriptor ss_cap;
282 } __packed usb3_bos_desc = {
285 .bLength = USB_DT_BOS_SIZE,
286 .bDescriptorType = USB_DT_BOS,
287 .wTotalLength = cpu_to_le16(sizeof(usb3_bos_desc)),
291 .bLength = USB_DT_USB_SS_CAP_SIZE,
292 .bDescriptorType = USB_DT_DEVICE_CAPABILITY,
293 .bDevCapabilityType = USB_SS_CAP_TYPE,
294 .wSpeedSupported = cpu_to_le16(USB_5GBPS_OPERATION),
295 .bFunctionalitySupport = ilog2(USB_5GBPS_OPERATION),
300 ss_hub_descriptor(struct usb_hub_descriptor *desc)
302 memset(desc, 0, sizeof *desc);
303 desc->bDescriptorType = USB_DT_SS_HUB;
304 desc->bDescLength = 12;
305 desc->wHubCharacteristics = cpu_to_le16(
306 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
307 desc->bNbrPorts = VHCI_HC_PORTS;
308 desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
309 desc->u.ss.DeviceRemovable = 0xffff;
312 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
316 memset(desc, 0, sizeof(*desc));
317 desc->bDescriptorType = USB_DT_HUB;
318 desc->wHubCharacteristics = cpu_to_le16(
319 HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
321 desc->bNbrPorts = VHCI_HC_PORTS;
322 BUILD_BUG_ON(VHCI_HC_PORTS > USB_MAXCHILDREN);
323 width = desc->bNbrPorts / 8 + 1;
324 desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width;
325 memset(&desc->u.hs.DeviceRemovable[0], 0, width);
326 memset(&desc->u.hs.DeviceRemovable[width], 0xff, width);
329 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
330 u16 wIndex, char *buf, u16 wLength)
332 struct vhci_hcd *vhci_hcd;
337 bool invalid_rhport = false;
339 u32 prev_port_status[VHCI_HC_PORTS];
341 if (!HCD_HW_ACCESSIBLE(hcd))
346 * wIndex (bits 0-7) shows the port number and begins from 1?
348 wIndex = ((__u8)(wIndex & 0x00ff));
349 usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
353 * wIndex can be 0 for some request types (typeReq). rhport is
354 * in valid range when wIndex >= 1 and < VHCI_HC_PORTS.
356 * Reference port_status[] only with valid rhport when
357 * invalid_rhport is false.
359 if (wIndex < 1 || wIndex > VHCI_HC_PORTS) {
360 invalid_rhport = true;
361 if (wIndex > VHCI_HC_PORTS)
362 pr_err("invalid port number %d\n", wIndex);
366 vhci_hcd = hcd_to_vhci_hcd(hcd);
367 vhci = vhci_hcd->vhci;
369 spin_lock_irqsave(&vhci->lock, flags);
371 /* store old status and compare now and old later */
372 if (usbip_dbg_flag_vhci_rh) {
374 memcpy(prev_port_status, vhci_hcd->port_status,
375 sizeof(prev_port_status));
379 case ClearHubFeature:
380 usbip_dbg_vhci_rh(" ClearHubFeature\n");
382 case ClearPortFeature:
383 if (invalid_rhport) {
384 pr_err("invalid port number %d\n", wIndex);
388 case USB_PORT_FEAT_SUSPEND:
389 if (hcd->speed == HCD_USB3) {
390 pr_err(" ClearPortFeature: USB_PORT_FEAT_SUSPEND req not "
391 "supported for USB 3.0 roothub\n");
395 " ClearPortFeature: USB_PORT_FEAT_SUSPEND\n");
396 if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
397 /* 20msec signaling */
398 vhci_hcd->resuming = 1;
399 vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
402 case USB_PORT_FEAT_POWER:
404 " ClearPortFeature: USB_PORT_FEAT_POWER\n");
405 if (hcd->speed == HCD_USB3)
406 vhci_hcd->port_status[rhport] &= ~USB_SS_PORT_STAT_POWER;
408 vhci_hcd->port_status[rhport] &= ~USB_PORT_STAT_POWER;
411 usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
415 vhci_hcd->port_status[rhport] &= ~(1 << wValue);
419 case GetHubDescriptor:
420 usbip_dbg_vhci_rh(" GetHubDescriptor\n");
421 if (hcd->speed == HCD_USB3 &&
422 (wLength < USB_DT_SS_HUB_SIZE ||
423 wValue != (USB_DT_SS_HUB << 8))) {
424 pr_err("Wrong hub descriptor type for USB 3.0 roothub.\n");
427 if (hcd->speed == HCD_USB3)
428 ss_hub_descriptor((struct usb_hub_descriptor *) buf);
430 hub_descriptor((struct usb_hub_descriptor *) buf);
432 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
433 if (hcd->speed != HCD_USB3)
436 if ((wValue >> 8) != USB_DT_BOS)
439 memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
440 retval = sizeof(usb3_bos_desc);
443 usbip_dbg_vhci_rh(" GetHubStatus\n");
444 *(__le32 *) buf = cpu_to_le32(0);
447 usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
448 if (invalid_rhport) {
449 pr_err("invalid port number %d\n", wIndex);
454 /* we do not care about resume. */
456 /* whoever resets or resumes must GetPortStatus to
459 if (vhci_hcd->resuming && time_after(jiffies, vhci_hcd->re_timeout)) {
460 vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_SUSPEND);
461 vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_SUSPEND);
462 vhci_hcd->resuming = 0;
463 vhci_hcd->re_timeout = 0;
466 if ((vhci_hcd->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
467 0 && time_after(jiffies, vhci_hcd->re_timeout)) {
468 vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_RESET);
469 vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_RESET);
470 vhci_hcd->re_timeout = 0;
473 * A few drivers do usb reset during probe when
474 * the device could be in VDEV_ST_USED state
476 if (vhci_hcd->vdev[rhport].ud.status ==
477 VDEV_ST_NOTASSIGNED ||
478 vhci_hcd->vdev[rhport].ud.status ==
481 " enable rhport %d (status %u)\n",
483 vhci_hcd->vdev[rhport].ud.status);
484 vhci_hcd->port_status[rhport] |=
485 USB_PORT_STAT_ENABLE;
488 if (hcd->speed < HCD_USB3) {
489 switch (vhci_hcd->vdev[rhport].speed) {
491 vhci_hcd->port_status[rhport] |=
492 USB_PORT_STAT_HIGH_SPEED;
495 vhci_hcd->port_status[rhport] |=
496 USB_PORT_STAT_LOW_SPEED;
499 pr_err("vhci_device speed not set\n");
504 ((__le16 *) buf)[0] = cpu_to_le16(vhci_hcd->port_status[rhport]);
505 ((__le16 *) buf)[1] =
506 cpu_to_le16(vhci_hcd->port_status[rhport] >> 16);
508 usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
512 usbip_dbg_vhci_rh(" SetHubFeature\n");
517 case USB_PORT_FEAT_LINK_STATE:
519 " SetPortFeature: USB_PORT_FEAT_LINK_STATE\n");
520 if (hcd->speed != HCD_USB3) {
521 pr_err("USB_PORT_FEAT_LINK_STATE req not "
522 "supported for USB 2.0 roothub\n");
526 * Since this is dummy we don't have an actual link so
527 * there is nothing to do for the SET_LINK_STATE cmd
530 case USB_PORT_FEAT_U1_TIMEOUT:
532 " SetPortFeature: USB_PORT_FEAT_U1_TIMEOUT\n");
533 case USB_PORT_FEAT_U2_TIMEOUT:
535 " SetPortFeature: USB_PORT_FEAT_U2_TIMEOUT\n");
536 /* TODO: add suspend/resume support! */
537 if (hcd->speed != HCD_USB3) {
538 pr_err("USB_PORT_FEAT_U1/2_TIMEOUT req not "
539 "supported for USB 2.0 roothub\n");
543 case USB_PORT_FEAT_SUSPEND:
545 " SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
546 /* Applicable only for USB2.0 hub */
547 if (hcd->speed == HCD_USB3) {
548 pr_err("USB_PORT_FEAT_SUSPEND req not "
549 "supported for USB 3.0 roothub\n");
553 if (invalid_rhport) {
554 pr_err("invalid port number %d\n", wIndex);
558 vhci_hcd->port_status[rhport] |= USB_PORT_STAT_SUSPEND;
560 case USB_PORT_FEAT_POWER:
562 " SetPortFeature: USB_PORT_FEAT_POWER\n");
563 if (invalid_rhport) {
564 pr_err("invalid port number %d\n", wIndex);
567 if (hcd->speed == HCD_USB3)
568 vhci_hcd->port_status[rhport] |= USB_SS_PORT_STAT_POWER;
570 vhci_hcd->port_status[rhport] |= USB_PORT_STAT_POWER;
572 case USB_PORT_FEAT_BH_PORT_RESET:
574 " SetPortFeature: USB_PORT_FEAT_BH_PORT_RESET\n");
575 if (invalid_rhport) {
576 pr_err("invalid port number %d\n", wIndex);
579 /* Applicable only for USB3.0 hub */
580 if (hcd->speed != HCD_USB3) {
581 pr_err("USB_PORT_FEAT_BH_PORT_RESET req not "
582 "supported for USB 2.0 roothub\n");
586 case USB_PORT_FEAT_RESET:
588 " SetPortFeature: USB_PORT_FEAT_RESET\n");
589 if (invalid_rhport) {
590 pr_err("invalid port number %d\n", wIndex);
593 /* if it's already enabled, disable */
594 if (hcd->speed == HCD_USB3) {
595 vhci_hcd->port_status[rhport] = 0;
596 vhci_hcd->port_status[rhport] =
597 (USB_SS_PORT_STAT_POWER |
598 USB_PORT_STAT_CONNECTION |
599 USB_PORT_STAT_RESET);
600 } else if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_ENABLE) {
601 vhci_hcd->port_status[rhport] &= ~(USB_PORT_STAT_ENABLE
602 | USB_PORT_STAT_LOW_SPEED
603 | USB_PORT_STAT_HIGH_SPEED);
606 /* 50msec reset signaling */
607 vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
611 usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
613 if (invalid_rhport) {
614 pr_err("invalid port number %d\n", wIndex);
619 if (hcd->speed == HCD_USB3) {
620 if ((vhci_hcd->port_status[rhport] &
621 USB_SS_PORT_STAT_POWER) != 0) {
622 vhci_hcd->port_status[rhport] |= (1 << wValue);
625 if ((vhci_hcd->port_status[rhport] &
626 USB_PORT_STAT_POWER) != 0) {
627 vhci_hcd->port_status[rhport] |= (1 << wValue);
631 case GetPortErrorCount:
632 usbip_dbg_vhci_rh(" GetPortErrorCount\n");
633 if (hcd->speed != HCD_USB3) {
634 pr_err("GetPortErrorCount req not "
635 "supported for USB 2.0 roothub\n");
638 /* We'll always return 0 since this is a dummy hub */
639 *(__le32 *) buf = cpu_to_le32(0);
642 usbip_dbg_vhci_rh(" SetHubDepth\n");
643 if (hcd->speed != HCD_USB3) {
644 pr_err("SetHubDepth req not supported for "
645 "USB 2.0 roothub\n");
650 pr_err("default hub control req: %04x v%04x i%04x l%d\n",
651 typeReq, wValue, wIndex, wLength);
653 /* "protocol stall" on error */
657 if (usbip_dbg_flag_vhci_rh) {
658 pr_debug("port %d\n", rhport);
659 /* Only dump valid port status */
660 if (!invalid_rhport) {
661 dump_port_status_diff(prev_port_status[rhport],
662 vhci_hcd->port_status[rhport],
663 hcd->speed == HCD_USB3);
666 usbip_dbg_vhci_rh(" bye\n");
668 spin_unlock_irqrestore(&vhci->lock, flags);
670 if (!invalid_rhport &&
671 (vhci_hcd->port_status[rhport] & PORT_C_MASK) != 0) {
672 usb_hcd_poll_rh_status(hcd);
678 static void vhci_tx_urb(struct urb *urb, struct vhci_device *vdev)
680 struct vhci_priv *priv;
681 struct vhci_hcd *vhci_hcd;
685 pr_err("could not get virtual device");
688 vhci_hcd = vdev_to_vhci_hcd(vdev);
690 priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
692 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
696 spin_lock_irqsave(&vdev->priv_lock, flags);
698 priv->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
699 if (priv->seqnum == 0xffff)
700 dev_info(&urb->dev->dev, "seqnum max\n");
705 urb->hcpriv = (void *) priv;
707 list_add_tail(&priv->list, &vdev->priv_tx);
709 wake_up(&vdev->waitq_tx);
710 spin_unlock_irqrestore(&vdev->priv_lock, flags);
713 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
715 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
716 struct vhci *vhci = vhci_hcd->vhci;
717 struct device *dev = &urb->dev->dev;
718 u8 portnum = urb->dev->portnum;
720 struct vhci_device *vdev;
723 if (portnum > VHCI_HC_PORTS) {
724 pr_err("invalid port number %d\n", portnum);
727 vdev = &vhci_hcd->vdev[portnum-1];
729 if (!urb->transfer_buffer && !urb->num_sgs &&
730 urb->transfer_buffer_length) {
731 dev_dbg(dev, "Null URB transfer buffer\n");
735 spin_lock_irqsave(&vhci->lock, flags);
737 if (urb->status != -EINPROGRESS) {
738 dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
739 spin_unlock_irqrestore(&vhci->lock, flags);
743 /* refuse enqueue for dead connection */
744 spin_lock(&vdev->ud.lock);
745 if (vdev->ud.status == VDEV_ST_NULL ||
746 vdev->ud.status == VDEV_ST_ERROR) {
747 dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
748 spin_unlock(&vdev->ud.lock);
749 spin_unlock_irqrestore(&vhci->lock, flags);
752 spin_unlock(&vdev->ud.lock);
754 ret = usb_hcd_link_urb_to_ep(hcd, urb);
759 * The enumeration process is as follows;
761 * 1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
762 * to get max packet length of default pipe
764 * 2. Set_Address request to DevAddr(0) EndPoint(0)
767 if (usb_pipedevice(urb->pipe) == 0) {
768 __u8 type = usb_pipetype(urb->pipe);
769 struct usb_ctrlrequest *ctrlreq =
770 (struct usb_ctrlrequest *) urb->setup_packet;
772 if (type != PIPE_CONTROL || !ctrlreq) {
773 dev_err(dev, "invalid request to devnum 0\n");
778 switch (ctrlreq->bRequest) {
779 case USB_REQ_SET_ADDRESS:
780 /* set_address may come when a device is reset */
781 dev_info(dev, "SetAddress Request (%d) to port %d\n",
782 ctrlreq->wValue, vdev->rhport);
784 usb_put_dev(vdev->udev);
785 vdev->udev = usb_get_dev(urb->dev);
787 spin_lock(&vdev->ud.lock);
788 vdev->ud.status = VDEV_ST_USED;
789 spin_unlock(&vdev->ud.lock);
791 if (urb->status == -EINPROGRESS) {
792 /* This request is successfully completed. */
793 /* If not -EINPROGRESS, possibly unlinked. */
799 case USB_REQ_GET_DESCRIPTOR:
800 if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
802 "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
804 usb_put_dev(vdev->udev);
805 vdev->udev = usb_get_dev(urb->dev);
811 "invalid request to devnum 0 bRequest %u, wValue %u\n",
821 vhci_tx_urb(urb, vdev);
822 spin_unlock_irqrestore(&vhci->lock, flags);
827 usb_hcd_unlink_urb_from_ep(hcd, urb);
829 spin_unlock_irqrestore(&vhci->lock, flags);
831 usb_hcd_giveback_urb(hcd, urb, urb->status);
836 * vhci_rx gives back the urb after receiving the reply of the urb. If an
837 * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
838 * back its urb. For the driver unlinking the urb, the content of the urb is
839 * not important, but the calling to its completion handler is important; the
840 * completion of unlinking is notified by the completion handler.
845 * - When vhci_hcd receives RET_SUBMIT,
847 * - case 1a). the urb of the pdu is not unlinking.
849 * => just give back the urb
851 * - case 1b). the urb of the pdu is unlinking.
852 * - usbip.ko will return a reply of the unlinking request.
853 * => give back the urb now and go to case 2b).
855 * - When vhci_hcd receives RET_UNLINK,
857 * - case 2a). a submit request is still pending in vhci_hcd.
858 * - urb was really pending in usbip.ko and urb_unlink_urb() was
860 * => free a pending submit request
861 * => notify unlink completeness by giving back the urb
863 * - case 2b). a submit request is *not* pending in vhci_hcd.
864 * - urb was already given back to the core driver.
865 * => do not give back the urb
870 * - When usbip receives CMD_UNLINK,
872 * - case 3a). the urb of the unlink request is now in submission.
873 * => do usb_unlink_urb().
874 * => after the unlink is completed, send RET_UNLINK.
876 * - case 3b). the urb of the unlink request is not in submission.
877 * - may be already completed or never be received
881 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
883 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
884 struct vhci *vhci = vhci_hcd->vhci;
885 struct vhci_priv *priv;
886 struct vhci_device *vdev;
889 spin_lock_irqsave(&vhci->lock, flags);
893 /* URB was never linked! or will be soon given back by
895 spin_unlock_irqrestore(&vhci->lock, flags);
902 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
904 spin_unlock_irqrestore(&vhci->lock, flags);
909 /* send unlink request here? */
912 if (!vdev->ud.tcp_socket) {
913 /* tcp connection is closed */
914 spin_lock(&vdev->priv_lock);
916 list_del(&priv->list);
920 spin_unlock(&vdev->priv_lock);
923 * If tcp connection is alive, we have sent CMD_UNLINK.
924 * vhci_rx will receive RET_UNLINK and give back the URB.
925 * Otherwise, we give back it here.
927 usb_hcd_unlink_urb_from_ep(hcd, urb);
929 spin_unlock_irqrestore(&vhci->lock, flags);
930 usb_hcd_giveback_urb(hcd, urb, urb->status);
931 spin_lock_irqsave(&vhci->lock, flags);
934 /* tcp connection is alive */
935 struct vhci_unlink *unlink;
937 spin_lock(&vdev->priv_lock);
939 /* setup CMD_UNLINK pdu */
940 unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
942 spin_unlock(&vdev->priv_lock);
943 spin_unlock_irqrestore(&vhci->lock, flags);
944 usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
948 unlink->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
949 if (unlink->seqnum == 0xffff)
950 pr_info("seqnum max\n");
952 unlink->unlink_seqnum = priv->seqnum;
954 /* send cmd_unlink and try to cancel the pending URB in the
956 list_add_tail(&unlink->list, &vdev->unlink_tx);
957 wake_up(&vdev->waitq_tx);
959 spin_unlock(&vdev->priv_lock);
962 spin_unlock_irqrestore(&vhci->lock, flags);
964 usbip_dbg_vhci_hc("leave\n");
968 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
970 struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
971 struct usb_hcd *hcd = vhci_hcd_to_hcd(vhci_hcd);
972 struct vhci *vhci = vhci_hcd->vhci;
973 struct vhci_unlink *unlink, *tmp;
976 spin_lock_irqsave(&vhci->lock, flags);
977 spin_lock(&vdev->priv_lock);
979 list_for_each_entry_safe(unlink, tmp, &vdev->unlink_tx, list) {
982 /* give back urb of unsent unlink request */
983 pr_info("unlink cleanup tx %lu\n", unlink->unlink_seqnum);
985 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
987 list_del(&unlink->list);
992 urb->status = -ENODEV;
994 usb_hcd_unlink_urb_from_ep(hcd, urb);
996 list_del(&unlink->list);
998 spin_unlock(&vdev->priv_lock);
999 spin_unlock_irqrestore(&vhci->lock, flags);
1001 usb_hcd_giveback_urb(hcd, urb, urb->status);
1003 spin_lock_irqsave(&vhci->lock, flags);
1004 spin_lock(&vdev->priv_lock);
1009 while (!list_empty(&vdev->unlink_rx)) {
1012 unlink = list_first_entry(&vdev->unlink_rx, struct vhci_unlink,
1015 /* give back URB of unanswered unlink request */
1016 pr_info("unlink cleanup rx %lu\n", unlink->unlink_seqnum);
1018 urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
1020 pr_info("the urb (seqnum %lu) was already given back\n",
1021 unlink->unlink_seqnum);
1022 list_del(&unlink->list);
1027 urb->status = -ENODEV;
1029 usb_hcd_unlink_urb_from_ep(hcd, urb);
1031 list_del(&unlink->list);
1033 spin_unlock(&vdev->priv_lock);
1034 spin_unlock_irqrestore(&vhci->lock, flags);
1036 usb_hcd_giveback_urb(hcd, urb, urb->status);
1038 spin_lock_irqsave(&vhci->lock, flags);
1039 spin_lock(&vdev->priv_lock);
1044 spin_unlock(&vdev->priv_lock);
1045 spin_unlock_irqrestore(&vhci->lock, flags);
1049 * The important thing is that only one context begins cleanup.
1050 * This is why error handling and cleanup become simple.
1051 * We do not want to consider race condition as possible.
1053 static void vhci_shutdown_connection(struct usbip_device *ud)
1055 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1057 /* need this? see stub_dev.c */
1058 if (ud->tcp_socket) {
1059 pr_debug("shutdown tcp_socket %d\n", ud->sockfd);
1060 kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
1063 /* kill threads related to this sdev */
1064 if (vdev->ud.tcp_rx) {
1065 kthread_stop_put(vdev->ud.tcp_rx);
1066 vdev->ud.tcp_rx = NULL;
1068 if (vdev->ud.tcp_tx) {
1069 kthread_stop_put(vdev->ud.tcp_tx);
1070 vdev->ud.tcp_tx = NULL;
1072 pr_info("stop threads\n");
1074 /* active connection is closed */
1075 if (vdev->ud.tcp_socket) {
1076 sockfd_put(vdev->ud.tcp_socket);
1077 vdev->ud.tcp_socket = NULL;
1078 vdev->ud.sockfd = -1;
1080 pr_info("release socket\n");
1082 vhci_device_unlink_cleanup(vdev);
1085 * rh_port_disconnect() is a trigger of ...
1086 * usb_disable_device():
1087 * disable all the endpoints for a USB device.
1088 * usb_disable_endpoint():
1089 * disable endpoints. pending urbs are unlinked(dequeued).
1091 * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
1092 * detached device should release used urbs in a cleanup function (i.e.
1093 * xxx_disconnect()). Therefore, vhci_hcd does not need to release
1094 * pushed urbs and their private data in this function.
1096 * NOTE: vhci_dequeue() must be considered carefully. When shutting down
1097 * a connection, vhci_shutdown_connection() expects vhci_dequeue()
1098 * gives back pushed urbs and frees their private data by request of
1099 * the cleanup function of a USB driver. When unlinking a urb with an
1100 * active connection, vhci_dequeue() does not give back the urb which
1101 * is actually given back by vhci_rx after receiving its return pdu.
1104 rh_port_disconnect(vdev);
1106 pr_info("disconnect device\n");
1109 static void vhci_device_reset(struct usbip_device *ud)
1111 struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1112 unsigned long flags;
1114 spin_lock_irqsave(&ud->lock, flags);
1119 usb_put_dev(vdev->udev);
1122 if (ud->tcp_socket) {
1123 sockfd_put(ud->tcp_socket);
1124 ud->tcp_socket = NULL;
1127 ud->status = VDEV_ST_NULL;
1129 spin_unlock_irqrestore(&ud->lock, flags);
1132 static void vhci_device_unusable(struct usbip_device *ud)
1134 unsigned long flags;
1136 spin_lock_irqsave(&ud->lock, flags);
1137 ud->status = VDEV_ST_ERROR;
1138 spin_unlock_irqrestore(&ud->lock, flags);
1141 static void vhci_device_init(struct vhci_device *vdev)
1143 memset(vdev, 0, sizeof(struct vhci_device));
1145 vdev->ud.side = USBIP_VHCI;
1146 vdev->ud.status = VDEV_ST_NULL;
1147 spin_lock_init(&vdev->ud.lock);
1148 mutex_init(&vdev->ud.sysfs_lock);
1150 INIT_LIST_HEAD(&vdev->priv_rx);
1151 INIT_LIST_HEAD(&vdev->priv_tx);
1152 INIT_LIST_HEAD(&vdev->unlink_tx);
1153 INIT_LIST_HEAD(&vdev->unlink_rx);
1154 spin_lock_init(&vdev->priv_lock);
1156 init_waitqueue_head(&vdev->waitq_tx);
1158 vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
1159 vdev->ud.eh_ops.reset = vhci_device_reset;
1160 vdev->ud.eh_ops.unusable = vhci_device_unusable;
1162 usbip_start_eh(&vdev->ud);
1165 static int hcd_name_to_id(const char *name)
1171 c = strchr(name, '.');
1175 ret = kstrtol(c+1, 10, &val);
1182 static int vhci_setup(struct usb_hcd *hcd)
1184 struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1185 if (usb_hcd_is_primary_hcd(hcd)) {
1186 vhci->vhci_hcd_hs = hcd_to_vhci_hcd(hcd);
1187 vhci->vhci_hcd_hs->vhci = vhci;
1189 * Mark the first roothub as being USB 2.0.
1190 * The USB 3.0 roothub will be registered later by
1193 hcd->speed = HCD_USB2;
1194 hcd->self.root_hub->speed = USB_SPEED_HIGH;
1196 vhci->vhci_hcd_ss = hcd_to_vhci_hcd(hcd);
1197 vhci->vhci_hcd_ss->vhci = vhci;
1198 hcd->speed = HCD_USB3;
1199 hcd->self.root_hub->speed = USB_SPEED_SUPER;
1204 * sg_tablesize is an arbitrary value to alleviate memory pressure
1207 hcd->self.sg_tablesize = 32;
1208 hcd->self.no_sg_constraint = 1;
1213 static int vhci_start(struct usb_hcd *hcd)
1215 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1219 usbip_dbg_vhci_hc("enter vhci_start\n");
1221 if (usb_hcd_is_primary_hcd(hcd))
1222 spin_lock_init(&vhci_hcd->vhci->lock);
1224 /* initialize private data of usb_hcd */
1226 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1227 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1229 vhci_device_init(vdev);
1230 vdev->rhport = rhport;
1233 atomic_set(&vhci_hcd->seqnum, 0);
1235 hcd->power_budget = 0; /* no limit */
1236 hcd->uses_new_polling = 1;
1238 #ifdef CONFIG_USB_OTG
1239 hcd->self.otg_port = 1;
1242 id = hcd_name_to_id(hcd_name(hcd));
1244 pr_err("invalid vhci name %s\n", hcd_name(hcd));
1248 /* vhci_hcd is now ready to be controlled through sysfs */
1249 if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1250 err = vhci_init_attr_group();
1252 pr_err("init attr group\n");
1255 err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1257 pr_err("create sysfs files\n");
1258 vhci_finish_attr_group();
1261 pr_info("created sysfs %s\n", hcd_name(hcd));
1267 static void vhci_stop(struct usb_hcd *hcd)
1269 struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1272 usbip_dbg_vhci_hc("stop VHCI controller\n");
1274 /* 1. remove the userland interface of vhci_hcd */
1275 id = hcd_name_to_id(hcd_name(hcd));
1276 if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1277 sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1278 vhci_finish_attr_group();
1281 /* 2. shutdown all the ports of vhci_hcd */
1282 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1283 struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1285 usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1286 usbip_stop_eh(&vdev->ud);
1290 static int vhci_get_frame_number(struct usb_hcd *hcd)
1292 dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
1298 /* FIXME: suspend/resume */
1299 static int vhci_bus_suspend(struct usb_hcd *hcd)
1301 struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1302 unsigned long flags;
1304 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1306 spin_lock_irqsave(&vhci->lock, flags);
1307 hcd->state = HC_STATE_SUSPENDED;
1308 spin_unlock_irqrestore(&vhci->lock, flags);
1313 static int vhci_bus_resume(struct usb_hcd *hcd)
1315 struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1317 unsigned long flags;
1319 dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1321 spin_lock_irqsave(&vhci->lock, flags);
1322 if (!HCD_HW_ACCESSIBLE(hcd))
1325 hcd->state = HC_STATE_RUNNING;
1326 spin_unlock_irqrestore(&vhci->lock, flags);
1333 #define vhci_bus_suspend NULL
1334 #define vhci_bus_resume NULL
1337 /* Change a group of bulk endpoints to support multiple stream IDs */
1338 static int vhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1339 struct usb_host_endpoint **eps, unsigned int num_eps,
1340 unsigned int num_streams, gfp_t mem_flags)
1342 dev_dbg(&hcd->self.root_hub->dev, "vhci_alloc_streams not implemented\n");
1346 /* Reverts a group of bulk endpoints back to not using stream IDs. */
1347 static int vhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1348 struct usb_host_endpoint **eps, unsigned int num_eps,
1351 dev_dbg(&hcd->self.root_hub->dev, "vhci_free_streams not implemented\n");
1355 static const struct hc_driver vhci_hc_driver = {
1356 .description = driver_name,
1357 .product_desc = driver_desc,
1358 .hcd_priv_size = sizeof(struct vhci_hcd),
1360 .flags = HCD_USB3 | HCD_SHARED,
1362 .reset = vhci_setup,
1363 .start = vhci_start,
1366 .urb_enqueue = vhci_urb_enqueue,
1367 .urb_dequeue = vhci_urb_dequeue,
1369 .get_frame_number = vhci_get_frame_number,
1371 .hub_status_data = vhci_hub_status,
1372 .hub_control = vhci_hub_control,
1373 .bus_suspend = vhci_bus_suspend,
1374 .bus_resume = vhci_bus_resume,
1376 .alloc_streams = vhci_alloc_streams,
1377 .free_streams = vhci_free_streams,
1380 static int vhci_hcd_probe(struct platform_device *pdev)
1382 struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
1383 struct usb_hcd *hcd_hs;
1384 struct usb_hcd *hcd_ss;
1387 usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1390 * Allocate and initialize hcd.
1391 * Our private data is also allocated automatically.
1393 hcd_hs = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1395 pr_err("create primary hcd failed\n");
1401 * Finish generic HCD structure initialization and register.
1402 * Call the driver's reset() and start() routines.
1404 ret = usb_add_hcd(hcd_hs, 0, 0);
1406 pr_err("usb_add_hcd hs failed %d\n", ret);
1410 hcd_ss = usb_create_shared_hcd(&vhci_hc_driver, &pdev->dev,
1411 dev_name(&pdev->dev), hcd_hs);
1414 pr_err("create shared hcd failed\n");
1415 goto remove_usb2_hcd;
1418 ret = usb_add_hcd(hcd_ss, 0, 0);
1420 pr_err("usb_add_hcd ss failed %d\n", ret);
1424 usbip_dbg_vhci_hc("bye\n");
1428 usb_put_hcd(hcd_ss);
1430 usb_remove_hcd(hcd_hs);
1432 usb_put_hcd(hcd_hs);
1433 vhci->vhci_hcd_hs = NULL;
1434 vhci->vhci_hcd_ss = NULL;
1438 static int vhci_hcd_remove(struct platform_device *pdev)
1440 struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
1443 * Disconnects the root hub,
1444 * then reverses the effects of usb_add_hcd(),
1445 * invoking the HCD's stop() methods.
1447 usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1448 usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1450 usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1451 usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1453 vhci->vhci_hcd_hs = NULL;
1454 vhci->vhci_hcd_ss = NULL;
1461 /* what should happen for USB/IP under suspend/resume? */
1462 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1464 struct usb_hcd *hcd;
1469 unsigned long flags;
1471 dev_dbg(&pdev->dev, "%s\n", __func__);
1473 hcd = platform_get_drvdata(pdev);
1477 vhci = *((void **)dev_get_platdata(hcd->self.controller));
1479 spin_lock_irqsave(&vhci->lock, flags);
1481 for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1482 if (vhci->vhci_hcd_hs->port_status[rhport] &
1483 USB_PORT_STAT_CONNECTION)
1486 if (vhci->vhci_hcd_ss->port_status[rhport] &
1487 USB_PORT_STAT_CONNECTION)
1491 spin_unlock_irqrestore(&vhci->lock, flags);
1493 if (connected > 0) {
1494 dev_info(&pdev->dev,
1495 "We have %d active connection%s. Do not suspend.\n",
1496 connected, (connected == 1 ? "" : "s"));
1499 dev_info(&pdev->dev, "suspend vhci_hcd");
1500 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1506 static int vhci_hcd_resume(struct platform_device *pdev)
1508 struct usb_hcd *hcd;
1510 dev_dbg(&pdev->dev, "%s\n", __func__);
1512 hcd = platform_get_drvdata(pdev);
1515 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1516 usb_hcd_poll_rh_status(hcd);
1523 #define vhci_hcd_suspend NULL
1524 #define vhci_hcd_resume NULL
1528 static struct platform_driver vhci_driver = {
1529 .probe = vhci_hcd_probe,
1530 .remove = vhci_hcd_remove,
1531 .suspend = vhci_hcd_suspend,
1532 .resume = vhci_hcd_resume,
1534 .name = driver_name,
1538 static void del_platform_devices(void)
1540 struct platform_device *pdev;
1543 for (i = 0; i < vhci_num_controllers; i++) {
1544 pdev = vhcis[i].pdev;
1546 platform_device_unregister(pdev);
1547 vhcis[i].pdev = NULL;
1549 sysfs_remove_link(&platform_bus.kobj, driver_name);
1552 static int __init vhci_hcd_init(void)
1559 if (vhci_num_controllers < 1)
1560 vhci_num_controllers = 1;
1562 vhcis = kcalloc(vhci_num_controllers, sizeof(struct vhci), GFP_KERNEL);
1566 for (i = 0; i < vhci_num_controllers; i++) {
1567 vhcis[i].pdev = platform_device_alloc(driver_name, i);
1568 if (!vhcis[i].pdev) {
1571 platform_device_put(vhcis[i--].pdev);
1573 goto err_device_alloc;
1576 for (i = 0; i < vhci_num_controllers; i++) {
1577 void *vhci = &vhcis[i];
1578 ret = platform_device_add_data(vhcis[i].pdev, &vhci, sizeof(void *));
1580 goto err_driver_register;
1583 ret = platform_driver_register(&vhci_driver);
1585 goto err_driver_register;
1587 for (i = 0; i < vhci_num_controllers; i++) {
1588 ret = platform_device_add(vhcis[i].pdev);
1592 platform_device_del(vhcis[i--].pdev);
1600 platform_driver_unregister(&vhci_driver);
1601 err_driver_register:
1602 for (i = 0; i < vhci_num_controllers; i++)
1603 platform_device_put(vhcis[i].pdev);
1609 static void __exit vhci_hcd_exit(void)
1611 del_platform_devices();
1612 platform_driver_unregister(&vhci_driver);
1616 module_init(vhci_hcd_init);
1617 module_exit(vhci_hcd_exit);
1619 MODULE_AUTHOR(DRIVER_AUTHOR);
1620 MODULE_DESCRIPTION(DRIVER_DESC);
1621 MODULE_LICENSE("GPL");