Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0+
0002 /*
0003  * Copyright (C) 2003-2008 Takahiro Hirofuchi
0004  * Copyright (C) 2015-2016 Nobuo Iwata
0005  */
0006 
0007 #include <linux/init.h>
0008 #include <linux/file.h>
0009 #include <linux/kernel.h>
0010 #include <linux/kthread.h>
0011 #include <linux/module.h>
0012 #include <linux/platform_device.h>
0013 #include <linux/slab.h>
0014 
0015 #include "usbip_common.h"
0016 #include "vhci.h"
0017 
0018 #define DRIVER_AUTHOR "Takahiro Hirofuchi"
0019 #define DRIVER_DESC "USB/IP 'Virtual' Host Controller (VHCI) Driver"
0020 
0021 /*
0022  * TODO
0023  *  - update root hub emulation
0024  *  - move the emulation code to userland ?
0025  *      porting to other operating systems
0026  *      minimize kernel code
0027  *  - add suspend/resume code
0028  *  - clean up everything
0029  */
0030 
0031 /* See usb gadget dummy hcd */
0032 
0033 static int vhci_hub_status(struct usb_hcd *hcd, char *buff);
0034 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
0035                 u16 wIndex, char *buff, u16 wLength);
0036 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
0037                 gfp_t mem_flags);
0038 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status);
0039 static int vhci_start(struct usb_hcd *vhci_hcd);
0040 static void vhci_stop(struct usb_hcd *hcd);
0041 static int vhci_get_frame_number(struct usb_hcd *hcd);
0042 
0043 static const char driver_name[] = "vhci_hcd";
0044 static const char driver_desc[] = "USB/IP Virtual Host Controller";
0045 
0046 int vhci_num_controllers = VHCI_NR_HCS;
0047 struct vhci *vhcis;
0048 
0049 static const char * const bit_desc[] = {
0050     "CONNECTION",       /*0*/
0051     "ENABLE",       /*1*/
0052     "SUSPEND",      /*2*/
0053     "OVER_CURRENT",     /*3*/
0054     "RESET",        /*4*/
0055     "L1",           /*5*/
0056     "R6",           /*6*/
0057     "R7",           /*7*/
0058     "POWER",        /*8*/
0059     "LOWSPEED",     /*9*/
0060     "HIGHSPEED",        /*10*/
0061     "PORT_TEST",        /*11*/
0062     "INDICATOR",        /*12*/
0063     "R13",          /*13*/
0064     "R14",          /*14*/
0065     "R15",          /*15*/
0066     "C_CONNECTION",     /*16*/
0067     "C_ENABLE",     /*17*/
0068     "C_SUSPEND",        /*18*/
0069     "C_OVER_CURRENT",   /*19*/
0070     "C_RESET",      /*20*/
0071     "C_L1",         /*21*/
0072     "R22",          /*22*/
0073     "R23",          /*23*/
0074     "R24",          /*24*/
0075     "R25",          /*25*/
0076     "R26",          /*26*/
0077     "R27",          /*27*/
0078     "R28",          /*28*/
0079     "R29",          /*29*/
0080     "R30",          /*30*/
0081     "R31",          /*31*/
0082 };
0083 
0084 static const char * const bit_desc_ss[] = {
0085     "CONNECTION",       /*0*/
0086     "ENABLE",       /*1*/
0087     "SUSPEND",      /*2*/
0088     "OVER_CURRENT",     /*3*/
0089     "RESET",        /*4*/
0090     "L1",           /*5*/
0091     "R6",           /*6*/
0092     "R7",           /*7*/
0093     "R8",           /*8*/
0094     "POWER",        /*9*/
0095     "HIGHSPEED",        /*10*/
0096     "PORT_TEST",        /*11*/
0097     "INDICATOR",        /*12*/
0098     "R13",          /*13*/
0099     "R14",          /*14*/
0100     "R15",          /*15*/
0101     "C_CONNECTION",     /*16*/
0102     "C_ENABLE",     /*17*/
0103     "C_SUSPEND",        /*18*/
0104     "C_OVER_CURRENT",   /*19*/
0105     "C_RESET",      /*20*/
0106     "C_BH_RESET",       /*21*/
0107     "C_LINK_STATE",     /*22*/
0108     "C_CONFIG_ERROR",   /*23*/
0109     "R24",          /*24*/
0110     "R25",          /*25*/
0111     "R26",          /*26*/
0112     "R27",          /*27*/
0113     "R28",          /*28*/
0114     "R29",          /*29*/
0115     "R30",          /*30*/
0116     "R31",          /*31*/
0117 };
0118 
0119 static void dump_port_status_diff(u32 prev_status, u32 new_status, bool usb3)
0120 {
0121     int i = 0;
0122     u32 bit = 1;
0123     const char * const *desc = bit_desc;
0124 
0125     if (usb3)
0126         desc = bit_desc_ss;
0127 
0128     pr_debug("status prev -> new: %08x -> %08x\n", prev_status, new_status);
0129     while (bit) {
0130         u32 prev = prev_status & bit;
0131         u32 new = new_status & bit;
0132         char change;
0133 
0134         if (!prev && new)
0135             change = '+';
0136         else if (prev && !new)
0137             change = '-';
0138         else
0139             change = ' ';
0140 
0141         if (prev || new) {
0142             pr_debug(" %c%s\n", change, desc[i]);
0143 
0144             if (bit == 1) /* USB_PORT_STAT_CONNECTION */
0145                 pr_debug(" %c%s\n", change, "USB_PORT_STAT_SPEED_5GBPS");
0146         }
0147         bit <<= 1;
0148         i++;
0149     }
0150     pr_debug("\n");
0151 }
0152 
0153 void rh_port_connect(struct vhci_device *vdev, enum usb_device_speed speed)
0154 {
0155     struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
0156     struct vhci *vhci = vhci_hcd->vhci;
0157     int     rhport = vdev->rhport;
0158     u32     status;
0159     unsigned long   flags;
0160 
0161     usbip_dbg_vhci_rh("rh_port_connect %d\n", rhport);
0162 
0163     spin_lock_irqsave(&vhci->lock, flags);
0164 
0165     status = vhci_hcd->port_status[rhport];
0166 
0167     status |= USB_PORT_STAT_CONNECTION | (1 << USB_PORT_FEAT_C_CONNECTION);
0168 
0169     switch (speed) {
0170     case USB_SPEED_HIGH:
0171         status |= USB_PORT_STAT_HIGH_SPEED;
0172         break;
0173     case USB_SPEED_LOW:
0174         status |= USB_PORT_STAT_LOW_SPEED;
0175         break;
0176     default:
0177         break;
0178     }
0179 
0180     vhci_hcd->port_status[rhport] = status;
0181 
0182     spin_unlock_irqrestore(&vhci->lock, flags);
0183 
0184     usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
0185 }
0186 
0187 static void rh_port_disconnect(struct vhci_device *vdev)
0188 {
0189     struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
0190     struct vhci *vhci = vhci_hcd->vhci;
0191     int     rhport = vdev->rhport;
0192     u32     status;
0193     unsigned long   flags;
0194 
0195     usbip_dbg_vhci_rh("rh_port_disconnect %d\n", rhport);
0196 
0197     spin_lock_irqsave(&vhci->lock, flags);
0198 
0199     status = vhci_hcd->port_status[rhport];
0200 
0201     status &= ~USB_PORT_STAT_CONNECTION;
0202     status |= (1 << USB_PORT_FEAT_C_CONNECTION);
0203 
0204     vhci_hcd->port_status[rhport] = status;
0205 
0206     spin_unlock_irqrestore(&vhci->lock, flags);
0207     usb_hcd_poll_rh_status(vhci_hcd_to_hcd(vhci_hcd));
0208 }
0209 
0210 #define PORT_C_MASK             \
0211     ((USB_PORT_STAT_C_CONNECTION        \
0212       | USB_PORT_STAT_C_ENABLE      \
0213       | USB_PORT_STAT_C_SUSPEND     \
0214       | USB_PORT_STAT_C_OVERCURRENT     \
0215       | USB_PORT_STAT_C_RESET) << 16)
0216 
0217 /*
0218  * Returns 0 if the status hasn't changed, or the number of bytes in buf.
0219  * Ports are 0-indexed from the HCD point of view,
0220  * and 1-indexed from the USB core pointer of view.
0221  *
0222  * @buf: a bitmap to show which port status has been changed.
0223  *  bit  0: reserved
0224  *  bit  1: the status of port 0 has been changed.
0225  *  bit  2: the status of port 1 has been changed.
0226  *  ...
0227  */
0228 static int vhci_hub_status(struct usb_hcd *hcd, char *buf)
0229 {
0230     struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
0231     struct vhci *vhci = vhci_hcd->vhci;
0232     int     retval = DIV_ROUND_UP(VHCI_HC_PORTS + 1, 8);
0233     int     rhport;
0234     int     changed = 0;
0235     unsigned long   flags;
0236 
0237     memset(buf, 0, retval);
0238 
0239     spin_lock_irqsave(&vhci->lock, flags);
0240     if (!HCD_HW_ACCESSIBLE(hcd)) {
0241         usbip_dbg_vhci_rh("hw accessible flag not on?\n");
0242         goto done;
0243     }
0244 
0245     /* check pseudo status register for each port */
0246     for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
0247         if ((vhci_hcd->port_status[rhport] & PORT_C_MASK)) {
0248             /* The status of a port has been changed, */
0249             usbip_dbg_vhci_rh("port %d status changed\n", rhport);
0250 
0251             buf[(rhport + 1) / 8] |= 1 << (rhport + 1) % 8;
0252             changed = 1;
0253         }
0254     }
0255 
0256     if ((hcd->state == HC_STATE_SUSPENDED) && (changed == 1))
0257         usb_hcd_resume_root_hub(hcd);
0258 
0259 done:
0260     spin_unlock_irqrestore(&vhci->lock, flags);
0261     return changed ? retval : 0;
0262 }
0263 
0264 /* usb 3.0 root hub device descriptor */
0265 static struct {
0266     struct usb_bos_descriptor bos;
0267     struct usb_ss_cap_descriptor ss_cap;
0268 } __packed usb3_bos_desc = {
0269 
0270     .bos = {
0271         .bLength        = USB_DT_BOS_SIZE,
0272         .bDescriptorType    = USB_DT_BOS,
0273         .wTotalLength       = cpu_to_le16(sizeof(usb3_bos_desc)),
0274         .bNumDeviceCaps     = 1,
0275     },
0276     .ss_cap = {
0277         .bLength        = USB_DT_USB_SS_CAP_SIZE,
0278         .bDescriptorType    = USB_DT_DEVICE_CAPABILITY,
0279         .bDevCapabilityType = USB_SS_CAP_TYPE,
0280         .wSpeedSupported    = cpu_to_le16(USB_5GBPS_OPERATION),
0281         .bFunctionalitySupport  = ilog2(USB_5GBPS_OPERATION),
0282     },
0283 };
0284 
0285 static inline void
0286 ss_hub_descriptor(struct usb_hub_descriptor *desc)
0287 {
0288     memset(desc, 0, sizeof *desc);
0289     desc->bDescriptorType = USB_DT_SS_HUB;
0290     desc->bDescLength = 12;
0291     desc->wHubCharacteristics = cpu_to_le16(
0292         HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
0293     desc->bNbrPorts = VHCI_HC_PORTS;
0294     desc->u.ss.bHubHdrDecLat = 0x04; /* Worst case: 0.4 micro sec*/
0295     desc->u.ss.DeviceRemovable = 0xffff;
0296 }
0297 
0298 static inline void hub_descriptor(struct usb_hub_descriptor *desc)
0299 {
0300     int width;
0301 
0302     memset(desc, 0, sizeof(*desc));
0303     desc->bDescriptorType = USB_DT_HUB;
0304     desc->wHubCharacteristics = cpu_to_le16(
0305         HUB_CHAR_INDV_PORT_LPSM | HUB_CHAR_COMMON_OCPM);
0306 
0307     desc->bNbrPorts = VHCI_HC_PORTS;
0308     BUILD_BUG_ON(VHCI_HC_PORTS > USB_MAXCHILDREN);
0309     width = desc->bNbrPorts / 8 + 1;
0310     desc->bDescLength = USB_DT_HUB_NONVAR_SIZE + 2 * width;
0311     memset(&desc->u.hs.DeviceRemovable[0], 0, width);
0312     memset(&desc->u.hs.DeviceRemovable[width], 0xff, width);
0313 }
0314 
0315 static int vhci_hub_control(struct usb_hcd *hcd, u16 typeReq, u16 wValue,
0316                 u16 wIndex, char *buf, u16 wLength)
0317 {
0318     struct vhci_hcd *vhci_hcd;
0319     struct vhci *vhci;
0320     int             retval = 0;
0321     int     rhport = -1;
0322     unsigned long   flags;
0323     bool invalid_rhport = false;
0324 
0325     u32 prev_port_status[VHCI_HC_PORTS];
0326 
0327     if (!HCD_HW_ACCESSIBLE(hcd))
0328         return -ETIMEDOUT;
0329 
0330     /*
0331      * NOTE:
0332      * wIndex (bits 0-7) shows the port number and begins from 1?
0333      */
0334     wIndex = ((__u8)(wIndex & 0x00ff));
0335     usbip_dbg_vhci_rh("typeReq %x wValue %x wIndex %x\n", typeReq, wValue,
0336               wIndex);
0337 
0338     /*
0339      * wIndex can be 0 for some request types (typeReq). rhport is
0340      * in valid range when wIndex >= 1 and < VHCI_HC_PORTS.
0341      *
0342      * Reference port_status[] only with valid rhport when
0343      * invalid_rhport is false.
0344      */
0345     if (wIndex < 1 || wIndex > VHCI_HC_PORTS) {
0346         invalid_rhport = true;
0347         if (wIndex > VHCI_HC_PORTS)
0348             pr_err("invalid port number %d\n", wIndex);
0349     } else
0350         rhport = wIndex - 1;
0351 
0352     vhci_hcd = hcd_to_vhci_hcd(hcd);
0353     vhci = vhci_hcd->vhci;
0354 
0355     spin_lock_irqsave(&vhci->lock, flags);
0356 
0357     /* store old status and compare now and old later */
0358     if (usbip_dbg_flag_vhci_rh) {
0359         if (!invalid_rhport)
0360             memcpy(prev_port_status, vhci_hcd->port_status,
0361                 sizeof(prev_port_status));
0362     }
0363 
0364     switch (typeReq) {
0365     case ClearHubFeature:
0366         usbip_dbg_vhci_rh(" ClearHubFeature\n");
0367         break;
0368     case ClearPortFeature:
0369         if (invalid_rhport) {
0370             pr_err("invalid port number %d\n", wIndex);
0371             goto error;
0372         }
0373         switch (wValue) {
0374         case USB_PORT_FEAT_SUSPEND:
0375             if (hcd->speed == HCD_USB3) {
0376                 pr_err(" ClearPortFeature: USB_PORT_FEAT_SUSPEND req not "
0377                        "supported for USB 3.0 roothub\n");
0378                 goto error;
0379             }
0380             usbip_dbg_vhci_rh(
0381                 " ClearPortFeature: USB_PORT_FEAT_SUSPEND\n");
0382             if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_SUSPEND) {
0383                 /* 20msec signaling */
0384                 vhci_hcd->resuming = 1;
0385                 vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(20);
0386             }
0387             break;
0388         case USB_PORT_FEAT_POWER:
0389             usbip_dbg_vhci_rh(
0390                 " ClearPortFeature: USB_PORT_FEAT_POWER\n");
0391             if (hcd->speed == HCD_USB3)
0392                 vhci_hcd->port_status[rhport] &= ~USB_SS_PORT_STAT_POWER;
0393             else
0394                 vhci_hcd->port_status[rhport] &= ~USB_PORT_STAT_POWER;
0395             break;
0396         default:
0397             usbip_dbg_vhci_rh(" ClearPortFeature: default %x\n",
0398                       wValue);
0399             if (wValue >= 32)
0400                 goto error;
0401             vhci_hcd->port_status[rhport] &= ~(1 << wValue);
0402             break;
0403         }
0404         break;
0405     case GetHubDescriptor:
0406         usbip_dbg_vhci_rh(" GetHubDescriptor\n");
0407         if (hcd->speed == HCD_USB3 &&
0408                 (wLength < USB_DT_SS_HUB_SIZE ||
0409                  wValue != (USB_DT_SS_HUB << 8))) {
0410             pr_err("Wrong hub descriptor type for USB 3.0 roothub.\n");
0411             goto error;
0412         }
0413         if (hcd->speed == HCD_USB3)
0414             ss_hub_descriptor((struct usb_hub_descriptor *) buf);
0415         else
0416             hub_descriptor((struct usb_hub_descriptor *) buf);
0417         break;
0418     case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
0419         if (hcd->speed != HCD_USB3)
0420             goto error;
0421 
0422         if ((wValue >> 8) != USB_DT_BOS)
0423             goto error;
0424 
0425         memcpy(buf, &usb3_bos_desc, sizeof(usb3_bos_desc));
0426         retval = sizeof(usb3_bos_desc);
0427         break;
0428     case GetHubStatus:
0429         usbip_dbg_vhci_rh(" GetHubStatus\n");
0430         *(__le32 *) buf = cpu_to_le32(0);
0431         break;
0432     case GetPortStatus:
0433         usbip_dbg_vhci_rh(" GetPortStatus port %x\n", wIndex);
0434         if (invalid_rhport) {
0435             pr_err("invalid port number %d\n", wIndex);
0436             retval = -EPIPE;
0437             goto error;
0438         }
0439 
0440         /* we do not care about resume. */
0441 
0442         /* whoever resets or resumes must GetPortStatus to
0443          * complete it!!
0444          */
0445         if (vhci_hcd->resuming && time_after(jiffies, vhci_hcd->re_timeout)) {
0446             vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_SUSPEND);
0447             vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_SUSPEND);
0448             vhci_hcd->resuming = 0;
0449             vhci_hcd->re_timeout = 0;
0450         }
0451 
0452         if ((vhci_hcd->port_status[rhport] & (1 << USB_PORT_FEAT_RESET)) !=
0453             0 && time_after(jiffies, vhci_hcd->re_timeout)) {
0454             vhci_hcd->port_status[rhport] |= (1 << USB_PORT_FEAT_C_RESET);
0455             vhci_hcd->port_status[rhport] &= ~(1 << USB_PORT_FEAT_RESET);
0456             vhci_hcd->re_timeout = 0;
0457 
0458             /*
0459              * A few drivers do usb reset during probe when
0460              * the device could be in VDEV_ST_USED state
0461              */
0462             if (vhci_hcd->vdev[rhport].ud.status ==
0463                 VDEV_ST_NOTASSIGNED ||
0464                 vhci_hcd->vdev[rhport].ud.status ==
0465                 VDEV_ST_USED) {
0466                 usbip_dbg_vhci_rh(
0467                     " enable rhport %d (status %u)\n",
0468                     rhport,
0469                     vhci_hcd->vdev[rhport].ud.status);
0470                 vhci_hcd->port_status[rhport] |=
0471                     USB_PORT_STAT_ENABLE;
0472             }
0473 
0474             if (hcd->speed < HCD_USB3) {
0475                 switch (vhci_hcd->vdev[rhport].speed) {
0476                 case USB_SPEED_HIGH:
0477                     vhci_hcd->port_status[rhport] |=
0478                           USB_PORT_STAT_HIGH_SPEED;
0479                     break;
0480                 case USB_SPEED_LOW:
0481                     vhci_hcd->port_status[rhport] |=
0482                         USB_PORT_STAT_LOW_SPEED;
0483                     break;
0484                 default:
0485                     pr_err("vhci_device speed not set\n");
0486                     break;
0487                 }
0488             }
0489         }
0490         ((__le16 *) buf)[0] = cpu_to_le16(vhci_hcd->port_status[rhport]);
0491         ((__le16 *) buf)[1] =
0492             cpu_to_le16(vhci_hcd->port_status[rhport] >> 16);
0493 
0494         usbip_dbg_vhci_rh(" GetPortStatus bye %x %x\n", ((u16 *)buf)[0],
0495                   ((u16 *)buf)[1]);
0496         break;
0497     case SetHubFeature:
0498         usbip_dbg_vhci_rh(" SetHubFeature\n");
0499         retval = -EPIPE;
0500         break;
0501     case SetPortFeature:
0502         switch (wValue) {
0503         case USB_PORT_FEAT_LINK_STATE:
0504             usbip_dbg_vhci_rh(
0505                 " SetPortFeature: USB_PORT_FEAT_LINK_STATE\n");
0506             if (hcd->speed != HCD_USB3) {
0507                 pr_err("USB_PORT_FEAT_LINK_STATE req not "
0508                        "supported for USB 2.0 roothub\n");
0509                 goto error;
0510             }
0511             /*
0512              * Since this is dummy we don't have an actual link so
0513              * there is nothing to do for the SET_LINK_STATE cmd
0514              */
0515             break;
0516         case USB_PORT_FEAT_U1_TIMEOUT:
0517             usbip_dbg_vhci_rh(
0518                 " SetPortFeature: USB_PORT_FEAT_U1_TIMEOUT\n");
0519             fallthrough;
0520         case USB_PORT_FEAT_U2_TIMEOUT:
0521             usbip_dbg_vhci_rh(
0522                 " SetPortFeature: USB_PORT_FEAT_U2_TIMEOUT\n");
0523             /* TODO: add suspend/resume support! */
0524             if (hcd->speed != HCD_USB3) {
0525                 pr_err("USB_PORT_FEAT_U1/2_TIMEOUT req not "
0526                        "supported for USB 2.0 roothub\n");
0527                 goto error;
0528             }
0529             break;
0530         case USB_PORT_FEAT_SUSPEND:
0531             usbip_dbg_vhci_rh(
0532                 " SetPortFeature: USB_PORT_FEAT_SUSPEND\n");
0533             /* Applicable only for USB2.0 hub */
0534             if (hcd->speed == HCD_USB3) {
0535                 pr_err("USB_PORT_FEAT_SUSPEND req not "
0536                        "supported for USB 3.0 roothub\n");
0537                 goto error;
0538             }
0539 
0540             if (invalid_rhport) {
0541                 pr_err("invalid port number %d\n", wIndex);
0542                 goto error;
0543             }
0544 
0545             vhci_hcd->port_status[rhport] |= USB_PORT_STAT_SUSPEND;
0546             break;
0547         case USB_PORT_FEAT_POWER:
0548             usbip_dbg_vhci_rh(
0549                 " SetPortFeature: USB_PORT_FEAT_POWER\n");
0550             if (invalid_rhport) {
0551                 pr_err("invalid port number %d\n", wIndex);
0552                 goto error;
0553             }
0554             if (hcd->speed == HCD_USB3)
0555                 vhci_hcd->port_status[rhport] |= USB_SS_PORT_STAT_POWER;
0556             else
0557                 vhci_hcd->port_status[rhport] |= USB_PORT_STAT_POWER;
0558             break;
0559         case USB_PORT_FEAT_BH_PORT_RESET:
0560             usbip_dbg_vhci_rh(
0561                 " SetPortFeature: USB_PORT_FEAT_BH_PORT_RESET\n");
0562             if (invalid_rhport) {
0563                 pr_err("invalid port number %d\n", wIndex);
0564                 goto error;
0565             }
0566             /* Applicable only for USB3.0 hub */
0567             if (hcd->speed != HCD_USB3) {
0568                 pr_err("USB_PORT_FEAT_BH_PORT_RESET req not "
0569                        "supported for USB 2.0 roothub\n");
0570                 goto error;
0571             }
0572             fallthrough;
0573         case USB_PORT_FEAT_RESET:
0574             usbip_dbg_vhci_rh(
0575                 " SetPortFeature: USB_PORT_FEAT_RESET\n");
0576             if (invalid_rhport) {
0577                 pr_err("invalid port number %d\n", wIndex);
0578                 goto error;
0579             }
0580             /* if it's already enabled, disable */
0581             if (hcd->speed == HCD_USB3) {
0582                 vhci_hcd->port_status[rhport] = 0;
0583                 vhci_hcd->port_status[rhport] =
0584                     (USB_SS_PORT_STAT_POWER |
0585                      USB_PORT_STAT_CONNECTION |
0586                      USB_PORT_STAT_RESET);
0587             } else if (vhci_hcd->port_status[rhport] & USB_PORT_STAT_ENABLE) {
0588                 vhci_hcd->port_status[rhport] &= ~(USB_PORT_STAT_ENABLE
0589                     | USB_PORT_STAT_LOW_SPEED
0590                     | USB_PORT_STAT_HIGH_SPEED);
0591             }
0592 
0593             /* 50msec reset signaling */
0594             vhci_hcd->re_timeout = jiffies + msecs_to_jiffies(50);
0595             fallthrough;
0596         default:
0597             usbip_dbg_vhci_rh(" SetPortFeature: default %d\n",
0598                       wValue);
0599             if (invalid_rhport) {
0600                 pr_err("invalid port number %d\n", wIndex);
0601                 goto error;
0602             }
0603             if (wValue >= 32)
0604                 goto error;
0605             if (hcd->speed == HCD_USB3) {
0606                 if ((vhci_hcd->port_status[rhport] &
0607                      USB_SS_PORT_STAT_POWER) != 0) {
0608                     vhci_hcd->port_status[rhport] |= (1 << wValue);
0609                 }
0610             } else
0611                 if ((vhci_hcd->port_status[rhport] &
0612                      USB_PORT_STAT_POWER) != 0) {
0613                     vhci_hcd->port_status[rhport] |= (1 << wValue);
0614                 }
0615         }
0616         break;
0617     case GetPortErrorCount:
0618         usbip_dbg_vhci_rh(" GetPortErrorCount\n");
0619         if (hcd->speed != HCD_USB3) {
0620             pr_err("GetPortErrorCount req not "
0621                    "supported for USB 2.0 roothub\n");
0622             goto error;
0623         }
0624         /* We'll always return 0 since this is a dummy hub */
0625         *(__le32 *) buf = cpu_to_le32(0);
0626         break;
0627     case SetHubDepth:
0628         usbip_dbg_vhci_rh(" SetHubDepth\n");
0629         if (hcd->speed != HCD_USB3) {
0630             pr_err("SetHubDepth req not supported for "
0631                    "USB 2.0 roothub\n");
0632             goto error;
0633         }
0634         break;
0635     default:
0636         pr_err("default hub control req: %04x v%04x i%04x l%d\n",
0637             typeReq, wValue, wIndex, wLength);
0638 error:
0639         /* "protocol stall" on error */
0640         retval = -EPIPE;
0641     }
0642 
0643     if (usbip_dbg_flag_vhci_rh) {
0644         pr_debug("port %d\n", rhport);
0645         /* Only dump valid port status */
0646         if (!invalid_rhport) {
0647             dump_port_status_diff(prev_port_status[rhport],
0648                           vhci_hcd->port_status[rhport],
0649                           hcd->speed == HCD_USB3);
0650         }
0651     }
0652     usbip_dbg_vhci_rh(" bye\n");
0653 
0654     spin_unlock_irqrestore(&vhci->lock, flags);
0655 
0656     if (!invalid_rhport &&
0657         (vhci_hcd->port_status[rhport] & PORT_C_MASK) != 0) {
0658         usb_hcd_poll_rh_status(hcd);
0659     }
0660 
0661     return retval;
0662 }
0663 
0664 static void vhci_tx_urb(struct urb *urb, struct vhci_device *vdev)
0665 {
0666     struct vhci_priv *priv;
0667     struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
0668     unsigned long flags;
0669 
0670     priv = kzalloc(sizeof(struct vhci_priv), GFP_ATOMIC);
0671     if (!priv) {
0672         usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
0673         return;
0674     }
0675 
0676     spin_lock_irqsave(&vdev->priv_lock, flags);
0677 
0678     priv->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
0679     if (priv->seqnum == 0xffff)
0680         dev_info(&urb->dev->dev, "seqnum max\n");
0681 
0682     priv->vdev = vdev;
0683     priv->urb = urb;
0684 
0685     urb->hcpriv = (void *) priv;
0686 
0687     list_add_tail(&priv->list, &vdev->priv_tx);
0688 
0689     wake_up(&vdev->waitq_tx);
0690     spin_unlock_irqrestore(&vdev->priv_lock, flags);
0691 }
0692 
0693 static int vhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
0694 {
0695     struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
0696     struct vhci *vhci = vhci_hcd->vhci;
0697     struct device *dev = &urb->dev->dev;
0698     u8 portnum = urb->dev->portnum;
0699     int ret = 0;
0700     struct vhci_device *vdev;
0701     unsigned long flags;
0702 
0703     if (portnum > VHCI_HC_PORTS) {
0704         pr_err("invalid port number %d\n", portnum);
0705         return -ENODEV;
0706     }
0707     vdev = &vhci_hcd->vdev[portnum-1];
0708 
0709     if (!urb->transfer_buffer && !urb->num_sgs &&
0710          urb->transfer_buffer_length) {
0711         dev_dbg(dev, "Null URB transfer buffer\n");
0712         return -EINVAL;
0713     }
0714 
0715     spin_lock_irqsave(&vhci->lock, flags);
0716 
0717     if (urb->status != -EINPROGRESS) {
0718         dev_err(dev, "URB already unlinked!, status %d\n", urb->status);
0719         spin_unlock_irqrestore(&vhci->lock, flags);
0720         return urb->status;
0721     }
0722 
0723     /* refuse enqueue for dead connection */
0724     spin_lock(&vdev->ud.lock);
0725     if (vdev->ud.status == VDEV_ST_NULL ||
0726         vdev->ud.status == VDEV_ST_ERROR) {
0727         dev_err(dev, "enqueue for inactive port %d\n", vdev->rhport);
0728         spin_unlock(&vdev->ud.lock);
0729         spin_unlock_irqrestore(&vhci->lock, flags);
0730         return -ENODEV;
0731     }
0732     spin_unlock(&vdev->ud.lock);
0733 
0734     ret = usb_hcd_link_urb_to_ep(hcd, urb);
0735     if (ret)
0736         goto no_need_unlink;
0737 
0738     /*
0739      * The enumeration process is as follows;
0740      *
0741      *  1. Get_Descriptor request to DevAddrs(0) EndPoint(0)
0742      *     to get max packet length of default pipe
0743      *
0744      *  2. Set_Address request to DevAddr(0) EndPoint(0)
0745      *
0746      */
0747     if (usb_pipedevice(urb->pipe) == 0) {
0748         __u8 type = usb_pipetype(urb->pipe);
0749         struct usb_ctrlrequest *ctrlreq =
0750             (struct usb_ctrlrequest *) urb->setup_packet;
0751 
0752         if (type != PIPE_CONTROL || !ctrlreq) {
0753             dev_err(dev, "invalid request to devnum 0\n");
0754             ret = -EINVAL;
0755             goto no_need_xmit;
0756         }
0757 
0758         switch (ctrlreq->bRequest) {
0759         case USB_REQ_SET_ADDRESS:
0760             /* set_address may come when a device is reset */
0761             dev_info(dev, "SetAddress Request (%d) to port %d\n",
0762                  ctrlreq->wValue, vdev->rhport);
0763 
0764             usb_put_dev(vdev->udev);
0765             vdev->udev = usb_get_dev(urb->dev);
0766 
0767             spin_lock(&vdev->ud.lock);
0768             vdev->ud.status = VDEV_ST_USED;
0769             spin_unlock(&vdev->ud.lock);
0770 
0771             if (urb->status == -EINPROGRESS) {
0772                 /* This request is successfully completed. */
0773                 /* If not -EINPROGRESS, possibly unlinked. */
0774                 urb->status = 0;
0775             }
0776 
0777             goto no_need_xmit;
0778 
0779         case USB_REQ_GET_DESCRIPTOR:
0780             if (ctrlreq->wValue == cpu_to_le16(USB_DT_DEVICE << 8))
0781                 usbip_dbg_vhci_hc(
0782                     "Not yet?:Get_Descriptor to device 0 (get max pipe size)\n");
0783 
0784             usb_put_dev(vdev->udev);
0785             vdev->udev = usb_get_dev(urb->dev);
0786             goto out;
0787 
0788         default:
0789             /* NOT REACHED */
0790             dev_err(dev,
0791                 "invalid request to devnum 0 bRequest %u, wValue %u\n",
0792                 ctrlreq->bRequest,
0793                 ctrlreq->wValue);
0794             ret =  -EINVAL;
0795             goto no_need_xmit;
0796         }
0797 
0798     }
0799 
0800 out:
0801     vhci_tx_urb(urb, vdev);
0802     spin_unlock_irqrestore(&vhci->lock, flags);
0803 
0804     return 0;
0805 
0806 no_need_xmit:
0807     usb_hcd_unlink_urb_from_ep(hcd, urb);
0808 no_need_unlink:
0809     spin_unlock_irqrestore(&vhci->lock, flags);
0810     if (!ret) {
0811         /* usb_hcd_giveback_urb() should be called with
0812          * irqs disabled
0813          */
0814         local_irq_disable();
0815         usb_hcd_giveback_urb(hcd, urb, urb->status);
0816         local_irq_enable();
0817     }
0818     return ret;
0819 }
0820 
0821 /*
0822  * vhci_rx gives back the urb after receiving the reply of the urb.  If an
0823  * unlink pdu is sent or not, vhci_rx receives a normal return pdu and gives
0824  * back its urb. For the driver unlinking the urb, the content of the urb is
0825  * not important, but the calling to its completion handler is important; the
0826  * completion of unlinking is notified by the completion handler.
0827  *
0828  *
0829  * CLIENT SIDE
0830  *
0831  * - When vhci_hcd receives RET_SUBMIT,
0832  *
0833  *  - case 1a). the urb of the pdu is not unlinking.
0834  *      - normal case
0835  *      => just give back the urb
0836  *
0837  *  - case 1b). the urb of the pdu is unlinking.
0838  *      - usbip.ko will return a reply of the unlinking request.
0839  *      => give back the urb now and go to case 2b).
0840  *
0841  * - When vhci_hcd receives RET_UNLINK,
0842  *
0843  *  - case 2a). a submit request is still pending in vhci_hcd.
0844  *      - urb was really pending in usbip.ko and urb_unlink_urb() was
0845  *        completed there.
0846  *      => free a pending submit request
0847  *      => notify unlink completeness by giving back the urb
0848  *
0849  *  - case 2b). a submit request is *not* pending in vhci_hcd.
0850  *      - urb was already given back to the core driver.
0851  *      => do not give back the urb
0852  *
0853  *
0854  * SERVER SIDE
0855  *
0856  * - When usbip receives CMD_UNLINK,
0857  *
0858  *  - case 3a). the urb of the unlink request is now in submission.
0859  *      => do usb_unlink_urb().
0860  *      => after the unlink is completed, send RET_UNLINK.
0861  *
0862  *  - case 3b). the urb of the unlink request is not in submission.
0863  *      - may be already completed or never be received
0864  *      => send RET_UNLINK
0865  *
0866  */
0867 static int vhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
0868 {
0869     struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
0870     struct vhci *vhci = vhci_hcd->vhci;
0871     struct vhci_priv *priv;
0872     struct vhci_device *vdev;
0873     unsigned long flags;
0874 
0875     spin_lock_irqsave(&vhci->lock, flags);
0876 
0877     priv = urb->hcpriv;
0878     if (!priv) {
0879         /* URB was never linked! or will be soon given back by
0880          * vhci_rx. */
0881         spin_unlock_irqrestore(&vhci->lock, flags);
0882         return -EIDRM;
0883     }
0884 
0885     {
0886         int ret = 0;
0887 
0888         ret = usb_hcd_check_unlink_urb(hcd, urb, status);
0889         if (ret) {
0890             spin_unlock_irqrestore(&vhci->lock, flags);
0891             return ret;
0892         }
0893     }
0894 
0895      /* send unlink request here? */
0896     vdev = priv->vdev;
0897 
0898     if (!vdev->ud.tcp_socket) {
0899         /* tcp connection is closed */
0900         spin_lock(&vdev->priv_lock);
0901 
0902         list_del(&priv->list);
0903         kfree(priv);
0904         urb->hcpriv = NULL;
0905 
0906         spin_unlock(&vdev->priv_lock);
0907 
0908         /*
0909          * If tcp connection is alive, we have sent CMD_UNLINK.
0910          * vhci_rx will receive RET_UNLINK and give back the URB.
0911          * Otherwise, we give back it here.
0912          */
0913         usb_hcd_unlink_urb_from_ep(hcd, urb);
0914 
0915         spin_unlock_irqrestore(&vhci->lock, flags);
0916         usb_hcd_giveback_urb(hcd, urb, urb->status);
0917         spin_lock_irqsave(&vhci->lock, flags);
0918 
0919     } else {
0920         /* tcp connection is alive */
0921         struct vhci_unlink *unlink;
0922 
0923         spin_lock(&vdev->priv_lock);
0924 
0925         /* setup CMD_UNLINK pdu */
0926         unlink = kzalloc(sizeof(struct vhci_unlink), GFP_ATOMIC);
0927         if (!unlink) {
0928             spin_unlock(&vdev->priv_lock);
0929             spin_unlock_irqrestore(&vhci->lock, flags);
0930             usbip_event_add(&vdev->ud, VDEV_EVENT_ERROR_MALLOC);
0931             return -ENOMEM;
0932         }
0933 
0934         unlink->seqnum = atomic_inc_return(&vhci_hcd->seqnum);
0935         if (unlink->seqnum == 0xffff)
0936             pr_info("seqnum max\n");
0937 
0938         unlink->unlink_seqnum = priv->seqnum;
0939 
0940         /* send cmd_unlink and try to cancel the pending URB in the
0941          * peer */
0942         list_add_tail(&unlink->list, &vdev->unlink_tx);
0943         wake_up(&vdev->waitq_tx);
0944 
0945         spin_unlock(&vdev->priv_lock);
0946     }
0947 
0948     spin_unlock_irqrestore(&vhci->lock, flags);
0949 
0950     usbip_dbg_vhci_hc("leave\n");
0951     return 0;
0952 }
0953 
0954 static void vhci_cleanup_unlink_list(struct vhci_device *vdev,
0955         struct list_head *unlink_list)
0956 {
0957     struct vhci_hcd *vhci_hcd = vdev_to_vhci_hcd(vdev);
0958     struct usb_hcd *hcd = vhci_hcd_to_hcd(vhci_hcd);
0959     struct vhci *vhci = vhci_hcd->vhci;
0960     struct vhci_unlink *unlink, *tmp;
0961     unsigned long flags;
0962 
0963     spin_lock_irqsave(&vhci->lock, flags);
0964     spin_lock(&vdev->priv_lock);
0965 
0966     list_for_each_entry_safe(unlink, tmp, unlink_list, list) {
0967         struct urb *urb;
0968 
0969         urb = pickup_urb_and_free_priv(vdev, unlink->unlink_seqnum);
0970         if (!urb) {
0971             list_del(&unlink->list);
0972             kfree(unlink);
0973             continue;
0974         }
0975 
0976         urb->status = -ENODEV;
0977 
0978         usb_hcd_unlink_urb_from_ep(hcd, urb);
0979 
0980         list_del(&unlink->list);
0981 
0982         spin_unlock(&vdev->priv_lock);
0983         spin_unlock_irqrestore(&vhci->lock, flags);
0984 
0985         usb_hcd_giveback_urb(hcd, urb, urb->status);
0986 
0987         spin_lock_irqsave(&vhci->lock, flags);
0988         spin_lock(&vdev->priv_lock);
0989 
0990         kfree(unlink);
0991     }
0992 
0993     spin_unlock(&vdev->priv_lock);
0994     spin_unlock_irqrestore(&vhci->lock, flags);
0995 }
0996 
0997 static void vhci_device_unlink_cleanup(struct vhci_device *vdev)
0998 {
0999     /* give back URB of unsent unlink request */
1000     vhci_cleanup_unlink_list(vdev, &vdev->unlink_tx);
1001 
1002     /* give back URB of unanswered unlink request */
1003     vhci_cleanup_unlink_list(vdev, &vdev->unlink_rx);
1004 }
1005 
1006 /*
1007  * The important thing is that only one context begins cleanup.
1008  * This is why error handling and cleanup become simple.
1009  * We do not want to consider race condition as possible.
1010  */
1011 static void vhci_shutdown_connection(struct usbip_device *ud)
1012 {
1013     struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1014 
1015     /* need this? see stub_dev.c */
1016     if (ud->tcp_socket) {
1017         pr_debug("shutdown tcp_socket %d\n", ud->sockfd);
1018         kernel_sock_shutdown(ud->tcp_socket, SHUT_RDWR);
1019     }
1020 
1021     /* kill threads related to this sdev */
1022     if (vdev->ud.tcp_rx) {
1023         kthread_stop_put(vdev->ud.tcp_rx);
1024         vdev->ud.tcp_rx = NULL;
1025     }
1026     if (vdev->ud.tcp_tx) {
1027         kthread_stop_put(vdev->ud.tcp_tx);
1028         vdev->ud.tcp_tx = NULL;
1029     }
1030     pr_info("stop threads\n");
1031 
1032     /* active connection is closed */
1033     if (vdev->ud.tcp_socket) {
1034         sockfd_put(vdev->ud.tcp_socket);
1035         vdev->ud.tcp_socket = NULL;
1036         vdev->ud.sockfd = -1;
1037     }
1038     pr_info("release socket\n");
1039 
1040     vhci_device_unlink_cleanup(vdev);
1041 
1042     /*
1043      * rh_port_disconnect() is a trigger of ...
1044      *   usb_disable_device():
1045      *  disable all the endpoints for a USB device.
1046      *   usb_disable_endpoint():
1047      *  disable endpoints. pending urbs are unlinked(dequeued).
1048      *
1049      * NOTE: After calling rh_port_disconnect(), the USB device drivers of a
1050      * detached device should release used urbs in a cleanup function (i.e.
1051      * xxx_disconnect()). Therefore, vhci_hcd does not need to release
1052      * pushed urbs and their private data in this function.
1053      *
1054      * NOTE: vhci_dequeue() must be considered carefully. When shutting down
1055      * a connection, vhci_shutdown_connection() expects vhci_dequeue()
1056      * gives back pushed urbs and frees their private data by request of
1057      * the cleanup function of a USB driver. When unlinking a urb with an
1058      * active connection, vhci_dequeue() does not give back the urb which
1059      * is actually given back by vhci_rx after receiving its return pdu.
1060      *
1061      */
1062     rh_port_disconnect(vdev);
1063 
1064     pr_info("disconnect device\n");
1065 }
1066 
1067 static void vhci_device_reset(struct usbip_device *ud)
1068 {
1069     struct vhci_device *vdev = container_of(ud, struct vhci_device, ud);
1070     unsigned long flags;
1071 
1072     spin_lock_irqsave(&ud->lock, flags);
1073 
1074     vdev->speed  = 0;
1075     vdev->devid  = 0;
1076 
1077     usb_put_dev(vdev->udev);
1078     vdev->udev = NULL;
1079 
1080     if (ud->tcp_socket) {
1081         sockfd_put(ud->tcp_socket);
1082         ud->tcp_socket = NULL;
1083         ud->sockfd = -1;
1084     }
1085     ud->status = VDEV_ST_NULL;
1086 
1087     spin_unlock_irqrestore(&ud->lock, flags);
1088 }
1089 
1090 static void vhci_device_unusable(struct usbip_device *ud)
1091 {
1092     unsigned long flags;
1093 
1094     spin_lock_irqsave(&ud->lock, flags);
1095     ud->status = VDEV_ST_ERROR;
1096     spin_unlock_irqrestore(&ud->lock, flags);
1097 }
1098 
1099 static void vhci_device_init(struct vhci_device *vdev)
1100 {
1101     memset(vdev, 0, sizeof(struct vhci_device));
1102 
1103     vdev->ud.side   = USBIP_VHCI;
1104     vdev->ud.status = VDEV_ST_NULL;
1105     spin_lock_init(&vdev->ud.lock);
1106     mutex_init(&vdev->ud.sysfs_lock);
1107 
1108     INIT_LIST_HEAD(&vdev->priv_rx);
1109     INIT_LIST_HEAD(&vdev->priv_tx);
1110     INIT_LIST_HEAD(&vdev->unlink_tx);
1111     INIT_LIST_HEAD(&vdev->unlink_rx);
1112     spin_lock_init(&vdev->priv_lock);
1113 
1114     init_waitqueue_head(&vdev->waitq_tx);
1115 
1116     vdev->ud.eh_ops.shutdown = vhci_shutdown_connection;
1117     vdev->ud.eh_ops.reset = vhci_device_reset;
1118     vdev->ud.eh_ops.unusable = vhci_device_unusable;
1119 
1120     usbip_start_eh(&vdev->ud);
1121 }
1122 
1123 static int hcd_name_to_id(const char *name)
1124 {
1125     char *c;
1126     long val;
1127     int ret;
1128 
1129     c = strchr(name, '.');
1130     if (c == NULL)
1131         return 0;
1132 
1133     ret = kstrtol(c+1, 10, &val);
1134     if (ret < 0)
1135         return ret;
1136 
1137     return val;
1138 }
1139 
1140 static int vhci_setup(struct usb_hcd *hcd)
1141 {
1142     struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1143     if (usb_hcd_is_primary_hcd(hcd)) {
1144         vhci->vhci_hcd_hs = hcd_to_vhci_hcd(hcd);
1145         vhci->vhci_hcd_hs->vhci = vhci;
1146         /*
1147          * Mark the first roothub as being USB 2.0.
1148          * The USB 3.0 roothub will be registered later by
1149          * vhci_hcd_probe()
1150          */
1151         hcd->speed = HCD_USB2;
1152         hcd->self.root_hub->speed = USB_SPEED_HIGH;
1153     } else {
1154         vhci->vhci_hcd_ss = hcd_to_vhci_hcd(hcd);
1155         vhci->vhci_hcd_ss->vhci = vhci;
1156         hcd->speed = HCD_USB3;
1157         hcd->self.root_hub->speed = USB_SPEED_SUPER;
1158     }
1159 
1160     /*
1161      * Support SG.
1162      * sg_tablesize is an arbitrary value to alleviate memory pressure
1163      * on the host.
1164      */
1165     hcd->self.sg_tablesize = 32;
1166     hcd->self.no_sg_constraint = 1;
1167 
1168     return 0;
1169 }
1170 
1171 static int vhci_start(struct usb_hcd *hcd)
1172 {
1173     struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1174     int id, rhport;
1175     int err;
1176 
1177     usbip_dbg_vhci_hc("enter vhci_start\n");
1178 
1179     if (usb_hcd_is_primary_hcd(hcd))
1180         spin_lock_init(&vhci_hcd->vhci->lock);
1181 
1182     /* initialize private data of usb_hcd */
1183 
1184     for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1185         struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1186 
1187         vhci_device_init(vdev);
1188         vdev->rhport = rhport;
1189     }
1190 
1191     atomic_set(&vhci_hcd->seqnum, 0);
1192 
1193     hcd->power_budget = 0; /* no limit */
1194     hcd->uses_new_polling = 1;
1195 
1196 #ifdef CONFIG_USB_OTG
1197     hcd->self.otg_port = 1;
1198 #endif
1199 
1200     id = hcd_name_to_id(hcd_name(hcd));
1201     if (id < 0) {
1202         pr_err("invalid vhci name %s\n", hcd_name(hcd));
1203         return -EINVAL;
1204     }
1205 
1206     /* vhci_hcd is now ready to be controlled through sysfs */
1207     if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1208         err = vhci_init_attr_group();
1209         if (err) {
1210             dev_err(hcd_dev(hcd), "init attr group failed, err = %d\n", err);
1211             return err;
1212         }
1213         err = sysfs_create_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1214         if (err) {
1215             dev_err(hcd_dev(hcd), "create sysfs files failed, err = %d\n", err);
1216             vhci_finish_attr_group();
1217             return err;
1218         }
1219         pr_info("created sysfs %s\n", hcd_name(hcd));
1220     }
1221 
1222     return 0;
1223 }
1224 
1225 static void vhci_stop(struct usb_hcd *hcd)
1226 {
1227     struct vhci_hcd *vhci_hcd = hcd_to_vhci_hcd(hcd);
1228     int id, rhport;
1229 
1230     usbip_dbg_vhci_hc("stop VHCI controller\n");
1231 
1232     /* 1. remove the userland interface of vhci_hcd */
1233     id = hcd_name_to_id(hcd_name(hcd));
1234     if (id == 0 && usb_hcd_is_primary_hcd(hcd)) {
1235         sysfs_remove_group(&hcd_dev(hcd)->kobj, &vhci_attr_group);
1236         vhci_finish_attr_group();
1237     }
1238 
1239     /* 2. shutdown all the ports of vhci_hcd */
1240     for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1241         struct vhci_device *vdev = &vhci_hcd->vdev[rhport];
1242 
1243         usbip_event_add(&vdev->ud, VDEV_EVENT_REMOVED);
1244         usbip_stop_eh(&vdev->ud);
1245     }
1246 }
1247 
1248 static int vhci_get_frame_number(struct usb_hcd *hcd)
1249 {
1250     dev_err_ratelimited(&hcd->self.root_hub->dev, "Not yet implemented\n");
1251     return 0;
1252 }
1253 
1254 #ifdef CONFIG_PM
1255 
1256 /* FIXME: suspend/resume */
1257 static int vhci_bus_suspend(struct usb_hcd *hcd)
1258 {
1259     struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1260     unsigned long flags;
1261 
1262     dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1263 
1264     spin_lock_irqsave(&vhci->lock, flags);
1265     hcd->state = HC_STATE_SUSPENDED;
1266     spin_unlock_irqrestore(&vhci->lock, flags);
1267 
1268     return 0;
1269 }
1270 
1271 static int vhci_bus_resume(struct usb_hcd *hcd)
1272 {
1273     struct vhci *vhci = *((void **)dev_get_platdata(hcd->self.controller));
1274     int rc = 0;
1275     unsigned long flags;
1276 
1277     dev_dbg(&hcd->self.root_hub->dev, "%s\n", __func__);
1278 
1279     spin_lock_irqsave(&vhci->lock, flags);
1280     if (!HCD_HW_ACCESSIBLE(hcd))
1281         rc = -ESHUTDOWN;
1282     else
1283         hcd->state = HC_STATE_RUNNING;
1284     spin_unlock_irqrestore(&vhci->lock, flags);
1285 
1286     return rc;
1287 }
1288 
1289 #else
1290 
1291 #define vhci_bus_suspend      NULL
1292 #define vhci_bus_resume       NULL
1293 #endif
1294 
1295 /* Change a group of bulk endpoints to support multiple stream IDs */
1296 static int vhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1297     struct usb_host_endpoint **eps, unsigned int num_eps,
1298     unsigned int num_streams, gfp_t mem_flags)
1299 {
1300     dev_dbg(&hcd->self.root_hub->dev, "vhci_alloc_streams not implemented\n");
1301     return 0;
1302 }
1303 
1304 /* Reverts a group of bulk endpoints back to not using stream IDs. */
1305 static int vhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
1306     struct usb_host_endpoint **eps, unsigned int num_eps,
1307     gfp_t mem_flags)
1308 {
1309     dev_dbg(&hcd->self.root_hub->dev, "vhci_free_streams not implemented\n");
1310     return 0;
1311 }
1312 
1313 static const struct hc_driver vhci_hc_driver = {
1314     .description    = driver_name,
1315     .product_desc   = driver_desc,
1316     .hcd_priv_size  = sizeof(struct vhci_hcd),
1317 
1318     .flags      = HCD_USB3 | HCD_SHARED,
1319 
1320     .reset      = vhci_setup,
1321     .start      = vhci_start,
1322     .stop       = vhci_stop,
1323 
1324     .urb_enqueue    = vhci_urb_enqueue,
1325     .urb_dequeue    = vhci_urb_dequeue,
1326 
1327     .get_frame_number = vhci_get_frame_number,
1328 
1329     .hub_status_data = vhci_hub_status,
1330     .hub_control    = vhci_hub_control,
1331     .bus_suspend    = vhci_bus_suspend,
1332     .bus_resume = vhci_bus_resume,
1333 
1334     .alloc_streams  = vhci_alloc_streams,
1335     .free_streams   = vhci_free_streams,
1336 };
1337 
1338 static int vhci_hcd_probe(struct platform_device *pdev)
1339 {
1340     struct vhci             *vhci = *((void **)dev_get_platdata(&pdev->dev));
1341     struct usb_hcd      *hcd_hs;
1342     struct usb_hcd      *hcd_ss;
1343     int         ret;
1344 
1345     usbip_dbg_vhci_hc("name %s id %d\n", pdev->name, pdev->id);
1346 
1347     /*
1348      * Allocate and initialize hcd.
1349      * Our private data is also allocated automatically.
1350      */
1351     hcd_hs = usb_create_hcd(&vhci_hc_driver, &pdev->dev, dev_name(&pdev->dev));
1352     if (!hcd_hs) {
1353         pr_err("create primary hcd failed\n");
1354         return -ENOMEM;
1355     }
1356     hcd_hs->has_tt = 1;
1357 
1358     /*
1359      * Finish generic HCD structure initialization and register.
1360      * Call the driver's reset() and start() routines.
1361      */
1362     ret = usb_add_hcd(hcd_hs, 0, 0);
1363     if (ret != 0) {
1364         pr_err("usb_add_hcd hs failed %d\n", ret);
1365         goto put_usb2_hcd;
1366     }
1367 
1368     hcd_ss = usb_create_shared_hcd(&vhci_hc_driver, &pdev->dev,
1369                        dev_name(&pdev->dev), hcd_hs);
1370     if (!hcd_ss) {
1371         ret = -ENOMEM;
1372         pr_err("create shared hcd failed\n");
1373         goto remove_usb2_hcd;
1374     }
1375 
1376     ret = usb_add_hcd(hcd_ss, 0, 0);
1377     if (ret) {
1378         pr_err("usb_add_hcd ss failed %d\n", ret);
1379         goto put_usb3_hcd;
1380     }
1381 
1382     usbip_dbg_vhci_hc("bye\n");
1383     return 0;
1384 
1385 put_usb3_hcd:
1386     usb_put_hcd(hcd_ss);
1387 remove_usb2_hcd:
1388     usb_remove_hcd(hcd_hs);
1389 put_usb2_hcd:
1390     usb_put_hcd(hcd_hs);
1391     vhci->vhci_hcd_hs = NULL;
1392     vhci->vhci_hcd_ss = NULL;
1393     return ret;
1394 }
1395 
1396 static int vhci_hcd_remove(struct platform_device *pdev)
1397 {
1398     struct vhci *vhci = *((void **)dev_get_platdata(&pdev->dev));
1399 
1400     /*
1401      * Disconnects the root hub,
1402      * then reverses the effects of usb_add_hcd(),
1403      * invoking the HCD's stop() methods.
1404      */
1405     usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1406     usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_ss));
1407 
1408     usb_remove_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1409     usb_put_hcd(vhci_hcd_to_hcd(vhci->vhci_hcd_hs));
1410 
1411     vhci->vhci_hcd_hs = NULL;
1412     vhci->vhci_hcd_ss = NULL;
1413 
1414     return 0;
1415 }
1416 
1417 #ifdef CONFIG_PM
1418 
1419 /* what should happen for USB/IP under suspend/resume? */
1420 static int vhci_hcd_suspend(struct platform_device *pdev, pm_message_t state)
1421 {
1422     struct usb_hcd *hcd;
1423     struct vhci *vhci;
1424     int rhport;
1425     int connected = 0;
1426     int ret = 0;
1427     unsigned long flags;
1428 
1429     dev_dbg(&pdev->dev, "%s\n", __func__);
1430 
1431     hcd = platform_get_drvdata(pdev);
1432     if (!hcd)
1433         return 0;
1434 
1435     vhci = *((void **)dev_get_platdata(hcd->self.controller));
1436 
1437     spin_lock_irqsave(&vhci->lock, flags);
1438 
1439     for (rhport = 0; rhport < VHCI_HC_PORTS; rhport++) {
1440         if (vhci->vhci_hcd_hs->port_status[rhport] &
1441             USB_PORT_STAT_CONNECTION)
1442             connected += 1;
1443 
1444         if (vhci->vhci_hcd_ss->port_status[rhport] &
1445             USB_PORT_STAT_CONNECTION)
1446             connected += 1;
1447     }
1448 
1449     spin_unlock_irqrestore(&vhci->lock, flags);
1450 
1451     if (connected > 0) {
1452         dev_info(&pdev->dev,
1453              "We have %d active connection%s. Do not suspend.\n",
1454              connected, (connected == 1 ? "" : "s"));
1455         ret =  -EBUSY;
1456     } else {
1457         dev_info(&pdev->dev, "suspend vhci_hcd");
1458         clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1459     }
1460 
1461     return ret;
1462 }
1463 
1464 static int vhci_hcd_resume(struct platform_device *pdev)
1465 {
1466     struct usb_hcd *hcd;
1467 
1468     dev_dbg(&pdev->dev, "%s\n", __func__);
1469 
1470     hcd = platform_get_drvdata(pdev);
1471     if (!hcd)
1472         return 0;
1473     set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1474     usb_hcd_poll_rh_status(hcd);
1475 
1476     return 0;
1477 }
1478 
1479 #else
1480 
1481 #define vhci_hcd_suspend    NULL
1482 #define vhci_hcd_resume     NULL
1483 
1484 #endif
1485 
1486 static struct platform_driver vhci_driver = {
1487     .probe  = vhci_hcd_probe,
1488     .remove = vhci_hcd_remove,
1489     .suspend = vhci_hcd_suspend,
1490     .resume = vhci_hcd_resume,
1491     .driver = {
1492         .name = driver_name,
1493     },
1494 };
1495 
1496 static void del_platform_devices(void)
1497 {
1498     struct platform_device *pdev;
1499     int i;
1500 
1501     for (i = 0; i < vhci_num_controllers; i++) {
1502         pdev = vhcis[i].pdev;
1503         if (pdev != NULL)
1504             platform_device_unregister(pdev);
1505         vhcis[i].pdev = NULL;
1506     }
1507     sysfs_remove_link(&platform_bus.kobj, driver_name);
1508 }
1509 
1510 static int __init vhci_hcd_init(void)
1511 {
1512     int i, ret;
1513 
1514     if (usb_disabled())
1515         return -ENODEV;
1516 
1517     if (vhci_num_controllers < 1)
1518         vhci_num_controllers = 1;
1519 
1520     vhcis = kcalloc(vhci_num_controllers, sizeof(struct vhci), GFP_KERNEL);
1521     if (vhcis == NULL)
1522         return -ENOMEM;
1523 
1524     for (i = 0; i < vhci_num_controllers; i++) {
1525         vhcis[i].pdev = platform_device_alloc(driver_name, i);
1526         if (!vhcis[i].pdev) {
1527             i--;
1528             while (i >= 0)
1529                 platform_device_put(vhcis[i--].pdev);
1530             ret = -ENOMEM;
1531             goto err_device_alloc;
1532         }
1533     }
1534     for (i = 0; i < vhci_num_controllers; i++) {
1535         void *vhci = &vhcis[i];
1536         ret = platform_device_add_data(vhcis[i].pdev, &vhci, sizeof(void *));
1537         if (ret)
1538             goto err_driver_register;
1539     }
1540 
1541     ret = platform_driver_register(&vhci_driver);
1542     if (ret)
1543         goto err_driver_register;
1544 
1545     for (i = 0; i < vhci_num_controllers; i++) {
1546         ret = platform_device_add(vhcis[i].pdev);
1547         if (ret < 0) {
1548             i--;
1549             while (i >= 0)
1550                 platform_device_del(vhcis[i--].pdev);
1551             goto err_add_hcd;
1552         }
1553     }
1554 
1555     return ret;
1556 
1557 err_add_hcd:
1558     platform_driver_unregister(&vhci_driver);
1559 err_driver_register:
1560     for (i = 0; i < vhci_num_controllers; i++)
1561         platform_device_put(vhcis[i].pdev);
1562 err_device_alloc:
1563     kfree(vhcis);
1564     return ret;
1565 }
1566 
1567 static void __exit vhci_hcd_exit(void)
1568 {
1569     del_platform_devices();
1570     platform_driver_unregister(&vhci_driver);
1571     kfree(vhcis);
1572 }
1573 
1574 module_init(vhci_hcd_init);
1575 module_exit(vhci_hcd_exit);
1576 
1577 MODULE_AUTHOR(DRIVER_AUTHOR);
1578 MODULE_DESCRIPTION(DRIVER_DESC);
1579 MODULE_LICENSE("GPL");