The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/usb/usb_hub.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /* $FreeBSD$ */
    2 /*-
    3  * Copyright (c) 1998 The NetBSD Foundation, Inc. All rights reserved.
    4  * Copyright (c) 1998 Lennart Augustsson. All rights reserved.
    5  * Copyright (c) 2008-2010 Hans Petter Selasky. All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 /*
   30  * USB spec: http://www.usb.org/developers/docs/usbspec.zip 
   31  */
   32 
   33 #ifdef USB_GLOBAL_INCLUDE_FILE
   34 #include USB_GLOBAL_INCLUDE_FILE
   35 #else
   36 #include <sys/stdint.h>
   37 #include <sys/stddef.h>
   38 #include <sys/param.h>
   39 #include <sys/queue.h>
   40 #include <sys/types.h>
   41 #include <sys/systm.h>
   42 #include <sys/kernel.h>
   43 #include <sys/bus.h>
   44 #include <sys/module.h>
   45 #include <sys/lock.h>
   46 #include <sys/mutex.h>
   47 #include <sys/condvar.h>
   48 #include <sys/sysctl.h>
   49 #include <sys/sx.h>
   50 #include <sys/unistd.h>
   51 #include <sys/callout.h>
   52 #include <sys/malloc.h>
   53 #include <sys/priv.h>
   54 
   55 #include <dev/usb/usb.h>
   56 #include <dev/usb/usbdi.h>
   57 #include <dev/usb/usbdi_util.h>
   58 
   59 #define USB_DEBUG_VAR uhub_debug
   60 
   61 #include <dev/usb/usb_core.h>
   62 #include <dev/usb/usb_process.h>
   63 #include <dev/usb/usb_device.h>
   64 #include <dev/usb/usb_request.h>
   65 #include <dev/usb/usb_debug.h>
   66 #include <dev/usb/usb_hub.h>
   67 #include <dev/usb/usb_util.h>
   68 #include <dev/usb/usb_busdma.h>
   69 #include <dev/usb/usb_transfer.h>
   70 #include <dev/usb/usb_dynamic.h>
   71 
   72 #include <dev/usb/usb_controller.h>
   73 #include <dev/usb/usb_bus.h>
   74 #endif                  /* USB_GLOBAL_INCLUDE_FILE */
   75 
   76 #define UHUB_INTR_INTERVAL 250          /* ms */
   77 enum {
   78         UHUB_INTR_TRANSFER,
   79 #if USB_HAVE_TT_SUPPORT
   80         UHUB_RESET_TT_TRANSFER,
   81 #endif
   82         UHUB_N_TRANSFER,
   83 };
   84 
   85 #ifdef USB_DEBUG
   86 static int uhub_debug = 0;
   87 
   88 static SYSCTL_NODE(_hw_usb, OID_AUTO, uhub, CTLFLAG_RW, 0, "USB HUB");
   89 SYSCTL_INT(_hw_usb_uhub, OID_AUTO, debug, CTLFLAG_RW | CTLFLAG_TUN, &uhub_debug, 0,
   90     "Debug level");
   91 TUNABLE_INT("hw.usb.uhub.debug", &uhub_debug);
   92 #endif
   93 
   94 #if USB_HAVE_POWERD
   95 static int usb_power_timeout = 30;      /* seconds */
   96 
   97 SYSCTL_INT(_hw_usb, OID_AUTO, power_timeout, CTLFLAG_RW,
   98     &usb_power_timeout, 0, "USB power timeout");
   99 #endif
  100 
  101 #if USB_HAVE_DISABLE_ENUM
  102 static int usb_disable_enumeration = 0;
  103 SYSCTL_INT(_hw_usb, OID_AUTO, disable_enumeration, CTLFLAG_RWTUN,
  104     &usb_disable_enumeration, 0, "Set to disable all USB device enumeration. "
  105         "This can secure against USB devices turning evil, "
  106         "for example a USB memory stick becoming a USB keyboard.");
  107 TUNABLE_INT("hw.usb.disable_enumeration", &usb_disable_enumeration);
  108 
  109 static int usb_disable_port_power = 0;
  110 SYSCTL_INT(_hw_usb, OID_AUTO, disable_port_power, CTLFLAG_RWTUN,
  111     &usb_disable_port_power, 0, "Set to disable all USB port power.");
  112 TUNABLE_INT("hw.usb.disable_port_power", &usb_disable_port_power);
  113 #endif
  114 
  115 struct uhub_current_state {
  116         uint16_t port_change;
  117         uint16_t port_status;
  118 };
  119 
  120 struct uhub_softc {
  121         struct uhub_current_state sc_st;/* current state */
  122 #if (USB_HAVE_FIXED_PORT != 0)
  123         struct usb_hub sc_hub;
  124 #endif
  125         device_t sc_dev;                /* base device */
  126         struct mtx sc_mtx;              /* our mutex */
  127         struct usb_device *sc_udev;     /* USB device */
  128         struct usb_xfer *sc_xfer[UHUB_N_TRANSFER];      /* interrupt xfer */
  129 #if USB_HAVE_DISABLE_ENUM
  130         int sc_disable_enumeration;
  131         int sc_disable_port_power;
  132 #endif
  133         uint8_t sc_usb_port_errors;     /* error counter */
  134 #define UHUB_USB_PORT_ERRORS_MAX 4
  135         uint8_t sc_flags;
  136 #define UHUB_FLAG_DID_EXPLORE 0x01
  137 };
  138 
  139 #define UHUB_PROTO(sc) ((sc)->sc_udev->ddesc.bDeviceProtocol)
  140 #define UHUB_IS_HIGH_SPEED(sc) (UHUB_PROTO(sc) != UDPROTO_FSHUB)
  141 #define UHUB_IS_SINGLE_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBSTT)
  142 #define UHUB_IS_MULTI_TT(sc) (UHUB_PROTO(sc) == UDPROTO_HSHUBMTT)
  143 #define UHUB_IS_SUPER_SPEED(sc) (UHUB_PROTO(sc) == UDPROTO_SSHUB)
  144 
  145 /* prototypes for type checking: */
  146 
  147 static device_probe_t uhub_probe;
  148 static device_attach_t uhub_attach;
  149 static device_detach_t uhub_detach;
  150 static device_suspend_t uhub_suspend;
  151 static device_resume_t uhub_resume;
  152 
  153 static bus_driver_added_t uhub_driver_added;
  154 static bus_child_location_str_t uhub_child_location_string;
  155 static bus_child_pnpinfo_str_t uhub_child_pnpinfo_string;
  156 
  157 static usb_callback_t uhub_intr_callback;
  158 #if USB_HAVE_TT_SUPPORT
  159 static usb_callback_t uhub_reset_tt_callback;
  160 #endif
  161 
  162 static void usb_dev_resume_peer(struct usb_device *udev);
  163 static void usb_dev_suspend_peer(struct usb_device *udev);
  164 static uint8_t usb_peer_should_wakeup(struct usb_device *udev);
  165 
  166 static const struct usb_config uhub_config[UHUB_N_TRANSFER] = {
  167 
  168         [UHUB_INTR_TRANSFER] = {
  169                 .type = UE_INTERRUPT,
  170                 .endpoint = UE_ADDR_ANY,
  171                 .direction = UE_DIR_ANY,
  172                 .timeout = 0,
  173                 .flags = {.pipe_bof = 1,.short_xfer_ok = 1,},
  174                 .bufsize = 0,   /* use wMaxPacketSize */
  175                 .callback = &uhub_intr_callback,
  176                 .interval = UHUB_INTR_INTERVAL,
  177         },
  178 #if USB_HAVE_TT_SUPPORT
  179         [UHUB_RESET_TT_TRANSFER] = {
  180                 .type = UE_CONTROL,
  181                 .endpoint = 0x00,       /* Control pipe */
  182                 .direction = UE_DIR_ANY,
  183                 .bufsize = sizeof(struct usb_device_request),
  184                 .callback = &uhub_reset_tt_callback,
  185                 .timeout = 1000,        /* 1 second */
  186                 .usb_mode = USB_MODE_HOST,
  187         },
  188 #endif
  189 };
  190 
  191 /*
  192  * driver instance for "hub" connected to "usb"
  193  * and "hub" connected to "hub"
  194  */
  195 static devclass_t uhub_devclass;
  196 
  197 static device_method_t uhub_methods[] = {
  198         DEVMETHOD(device_probe, uhub_probe),
  199         DEVMETHOD(device_attach, uhub_attach),
  200         DEVMETHOD(device_detach, uhub_detach),
  201 
  202         DEVMETHOD(device_suspend, uhub_suspend),
  203         DEVMETHOD(device_resume, uhub_resume),
  204 
  205         DEVMETHOD(bus_child_location_str, uhub_child_location_string),
  206         DEVMETHOD(bus_child_pnpinfo_str, uhub_child_pnpinfo_string),
  207         DEVMETHOD(bus_driver_added, uhub_driver_added),
  208         DEVMETHOD_END
  209 };
  210 
  211 static driver_t uhub_driver = {
  212         .name = "uhub",
  213         .methods = uhub_methods,
  214         .size = sizeof(struct uhub_softc)
  215 };
  216 
  217 DRIVER_MODULE(uhub, usbus, uhub_driver, uhub_devclass, 0, 0);
  218 DRIVER_MODULE(uhub, uhub, uhub_driver, uhub_devclass, NULL, 0);
  219 MODULE_VERSION(uhub, 1);
  220 
  221 static void
  222 uhub_intr_callback(struct usb_xfer *xfer, usb_error_t error)
  223 {
  224         struct uhub_softc *sc = usbd_xfer_softc(xfer);
  225 
  226         switch (USB_GET_STATE(xfer)) {
  227         case USB_ST_TRANSFERRED:
  228                 DPRINTFN(2, "\n");
  229                 /*
  230                  * This is an indication that some port
  231                  * has changed status. Notify the bus
  232                  * event handler thread that we need
  233                  * to be explored again:
  234                  */
  235                 usb_needs_explore(sc->sc_udev->bus, 0);
  236 
  237         case USB_ST_SETUP:
  238                 usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
  239                 usbd_transfer_submit(xfer);
  240                 break;
  241 
  242         default:                        /* Error */
  243                 if (xfer->error != USB_ERR_CANCELLED) {
  244                         /*
  245                          * Do a clear-stall. The "stall_pipe" flag
  246                          * will get cleared before next callback by
  247                          * the USB stack.
  248                          */
  249                         usbd_xfer_set_stall(xfer);
  250                         usbd_xfer_set_frame_len(xfer, 0, usbd_xfer_max_len(xfer));
  251                         usbd_transfer_submit(xfer);
  252                 }
  253                 break;
  254         }
  255 }
  256 
  257 /*------------------------------------------------------------------------*
  258  *      uhub_reset_tt_proc
  259  *
  260  * This function starts the TT reset USB request
  261  *------------------------------------------------------------------------*/
  262 #if USB_HAVE_TT_SUPPORT
  263 static void
  264 uhub_reset_tt_proc(struct usb_proc_msg *_pm)
  265 {
  266         struct usb_udev_msg *pm = (void *)_pm;
  267         struct usb_device *udev = pm->udev;
  268         struct usb_hub *hub;
  269         struct uhub_softc *sc;
  270 
  271         hub = udev->hub;
  272         if (hub == NULL)
  273                 return;
  274         sc = hub->hubsoftc;
  275         if (sc == NULL)
  276                 return;
  277 
  278         /* Change lock */
  279         USB_BUS_UNLOCK(udev->bus);
  280         mtx_lock(&sc->sc_mtx);
  281         /* Start transfer */
  282         usbd_transfer_start(sc->sc_xfer[UHUB_RESET_TT_TRANSFER]);
  283         /* Change lock */
  284         mtx_unlock(&sc->sc_mtx);
  285         USB_BUS_LOCK(udev->bus);
  286 }
  287 #endif
  288 
  289 /*------------------------------------------------------------------------*
  290  *      uhub_tt_buffer_reset_async_locked
  291  *
  292  * This function queues a TT reset for the given USB device and endpoint.
  293  *------------------------------------------------------------------------*/
  294 #if USB_HAVE_TT_SUPPORT
  295 void
  296 uhub_tt_buffer_reset_async_locked(struct usb_device *child, struct usb_endpoint *ep)
  297 {
  298         struct usb_device_request req;
  299         struct usb_device *udev;
  300         struct usb_hub *hub;
  301         struct usb_port *up;
  302         uint16_t wValue;
  303         uint8_t port;
  304 
  305         if (child == NULL || ep == NULL)
  306                 return;
  307 
  308         udev = child->parent_hs_hub;
  309         port = child->hs_port_no;
  310 
  311         if (udev == NULL)
  312                 return;
  313 
  314         hub = udev->hub;
  315         if ((hub == NULL) ||
  316             (udev->speed != USB_SPEED_HIGH) ||
  317             (child->speed != USB_SPEED_LOW &&
  318              child->speed != USB_SPEED_FULL) ||
  319             (child->flags.usb_mode != USB_MODE_HOST) ||
  320             (port == 0) || (ep->edesc == NULL)) {
  321                 /* not applicable */
  322                 return;
  323         }
  324 
  325         USB_BUS_LOCK_ASSERT(udev->bus, MA_OWNED);
  326 
  327         up = hub->ports + port - 1;
  328 
  329         if (udev->ddesc.bDeviceClass == UDCLASS_HUB &&
  330             udev->ddesc.bDeviceProtocol == UDPROTO_HSHUBSTT)
  331                 port = 1;
  332 
  333         /* if we already received a clear buffer request, reset the whole TT */
  334         if (up->req_reset_tt.bRequest != 0) {
  335                 req.bmRequestType = UT_WRITE_CLASS_OTHER;
  336                 req.bRequest = UR_RESET_TT;
  337                 USETW(req.wValue, 0);
  338                 req.wIndex[0] = port;
  339                 req.wIndex[1] = 0;
  340                 USETW(req.wLength, 0);
  341         } else {
  342                 wValue = (ep->edesc->bEndpointAddress & 0xF) |
  343                       ((child->address & 0x7F) << 4) |
  344                       ((ep->edesc->bEndpointAddress & 0x80) << 8) |
  345                       ((ep->edesc->bmAttributes & 3) << 12);
  346 
  347                 req.bmRequestType = UT_WRITE_CLASS_OTHER;
  348                 req.bRequest = UR_CLEAR_TT_BUFFER;
  349                 USETW(req.wValue, wValue);
  350                 req.wIndex[0] = port;
  351                 req.wIndex[1] = 0;
  352                 USETW(req.wLength, 0);
  353         }
  354         up->req_reset_tt = req;
  355         /* get reset transfer started */
  356         usb_proc_msignal(USB_BUS_TT_PROC(udev->bus),
  357             &hub->tt_msg[0], &hub->tt_msg[1]);
  358 }
  359 #endif
  360 
  361 #if USB_HAVE_TT_SUPPORT
  362 static void
  363 uhub_reset_tt_callback(struct usb_xfer *xfer, usb_error_t error)
  364 {
  365         struct uhub_softc *sc;
  366         struct usb_device *udev;
  367         struct usb_port *up;
  368         uint8_t x;
  369 
  370         DPRINTF("TT buffer reset\n");
  371 
  372         sc = usbd_xfer_softc(xfer);
  373         udev = sc->sc_udev;
  374 
  375         switch (USB_GET_STATE(xfer)) {
  376         case USB_ST_TRANSFERRED:
  377         case USB_ST_SETUP:
  378 tr_setup:
  379                 USB_BUS_LOCK(udev->bus);
  380                 /* find first port which needs a TT reset */
  381                 for (x = 0; x != udev->hub->nports; x++) {
  382                         up = udev->hub->ports + x;
  383 
  384                         if (up->req_reset_tt.bRequest == 0)
  385                                 continue;
  386 
  387                         /* copy in the transfer */
  388                         usbd_copy_in(xfer->frbuffers, 0, &up->req_reset_tt,
  389                             sizeof(up->req_reset_tt));
  390                         /* reset buffer */
  391                         memset(&up->req_reset_tt, 0, sizeof(up->req_reset_tt));
  392 
  393                         /* set length */
  394                         usbd_xfer_set_frame_len(xfer, 0, sizeof(up->req_reset_tt));
  395                         xfer->nframes = 1;
  396                         USB_BUS_UNLOCK(udev->bus);
  397 
  398                         usbd_transfer_submit(xfer);
  399                         return;
  400                 }
  401                 USB_BUS_UNLOCK(udev->bus);
  402                 break;
  403 
  404         default:
  405                 if (error == USB_ERR_CANCELLED)
  406                         break;
  407 
  408                 DPRINTF("TT buffer reset failed (%s)\n", usbd_errstr(error));
  409                 goto tr_setup;
  410         }
  411 }
  412 #endif
  413 
  414 /*------------------------------------------------------------------------*
  415  *      uhub_count_active_host_ports
  416  *
  417  * This function counts the number of active ports at the given speed.
  418  *------------------------------------------------------------------------*/
  419 uint8_t
  420 uhub_count_active_host_ports(struct usb_device *udev, enum usb_dev_speed speed)
  421 {
  422         struct uhub_softc *sc;
  423         struct usb_device *child;
  424         struct usb_hub *hub;
  425         struct usb_port *up;
  426         uint8_t retval = 0;
  427         uint8_t x;
  428 
  429         if (udev == NULL)
  430                 goto done;
  431         hub = udev->hub;
  432         if (hub == NULL)
  433                 goto done;
  434         sc = hub->hubsoftc;
  435         if (sc == NULL)
  436                 goto done;
  437 
  438         for (x = 0; x != hub->nports; x++) {
  439                 up = hub->ports + x;
  440                 child = usb_bus_port_get_device(udev->bus, up);
  441                 if (child != NULL &&
  442                     child->flags.usb_mode == USB_MODE_HOST &&
  443                     child->speed == speed)
  444                         retval++;
  445         }
  446 done:
  447         return (retval);
  448 }
  449 
  450 void
  451 uhub_explore_handle_re_enumerate(struct usb_device *child)
  452 {
  453         uint8_t do_unlock;
  454         usb_error_t err;
  455 
  456         /* check if device should be re-enumerated */
  457         if (child->flags.usb_mode != USB_MODE_HOST)
  458                 return;
  459 
  460         do_unlock = usbd_enum_lock(child);
  461         switch (child->re_enumerate_wait) {
  462         case USB_RE_ENUM_START:
  463                 err = usbd_set_config_index(child,
  464                     USB_UNCONFIG_INDEX);
  465                 if (err != 0) {
  466                         DPRINTF("Unconfigure failed: %s: Ignored.\n",
  467                             usbd_errstr(err));
  468                 }
  469                 if (child->parent_hub == NULL) {
  470                         /* the root HUB cannot be re-enumerated */
  471                         DPRINTFN(6, "cannot reset root HUB\n");
  472                         err = 0;
  473                 } else {
  474                         err = usbd_req_re_enumerate(child, NULL);
  475                 }
  476                 if (err == 0)
  477                         err = usbd_set_config_index(child, 0);
  478                 if (err == 0) {
  479                         err = usb_probe_and_attach(child,
  480                             USB_IFACE_INDEX_ANY);
  481                 }
  482                 child->re_enumerate_wait = USB_RE_ENUM_DONE;
  483                 break;
  484 
  485         case USB_RE_ENUM_PWR_OFF:
  486                 /* get the device unconfigured */
  487                 err = usbd_set_config_index(child,
  488                     USB_UNCONFIG_INDEX);
  489                 if (err) {
  490                         DPRINTFN(0, "Could not unconfigure "
  491                             "device (ignored)\n");
  492                 }
  493                 if (child->parent_hub == NULL) {
  494                         /* the root HUB cannot be re-enumerated */
  495                         DPRINTFN(6, "cannot set port feature\n");
  496                         err = 0;
  497                 } else {
  498                         /* clear port enable */
  499                         err = usbd_req_clear_port_feature(child->parent_hub,
  500                             NULL, child->port_no, UHF_PORT_ENABLE);
  501                         if (err) {
  502                                 DPRINTFN(0, "Could not disable port "
  503                                     "(ignored)\n");
  504                         }
  505                 }
  506                 child->re_enumerate_wait = USB_RE_ENUM_DONE;
  507                 break;
  508 
  509         case USB_RE_ENUM_SET_CONFIG:
  510                 err = usbd_set_config_index(child,
  511                     child->next_config_index);
  512                 if (err != 0) {
  513                         DPRINTF("Configure failed: %s: Ignored.\n",
  514                             usbd_errstr(err));
  515                 } else {
  516                         err = usb_probe_and_attach(child,
  517                             USB_IFACE_INDEX_ANY);
  518                 }
  519                 child->re_enumerate_wait = USB_RE_ENUM_DONE;
  520                 break;
  521 
  522         default:
  523                 child->re_enumerate_wait = USB_RE_ENUM_DONE;
  524                 break;
  525         }
  526         if (do_unlock)
  527                 usbd_enum_unlock(child);
  528 }
  529 
  530 /*------------------------------------------------------------------------*
  531  *      uhub_explore_sub - subroutine
  532  *
  533  * Return values:
  534  *    0: Success
  535  * Else: A control transaction failed
  536  *------------------------------------------------------------------------*/
  537 static usb_error_t
  538 uhub_explore_sub(struct uhub_softc *sc, struct usb_port *up)
  539 {
  540         struct usb_bus *bus;
  541         struct usb_device *child;
  542         uint8_t refcount;
  543         usb_error_t err;
  544 
  545         bus = sc->sc_udev->bus;
  546         err = 0;
  547 
  548         /* get driver added refcount from USB bus */
  549         refcount = bus->driver_added_refcount;
  550 
  551         /* get device assosiated with the given port */
  552         child = usb_bus_port_get_device(bus, up);
  553         if (child == NULL) {
  554                 /* nothing to do */
  555                 goto done;
  556         }
  557 
  558         uhub_explore_handle_re_enumerate(child);
  559 
  560         /* check if probe and attach should be done */
  561 
  562         if (child->driver_added_refcount != refcount) {
  563                 child->driver_added_refcount = refcount;
  564                 err = usb_probe_and_attach(child,
  565                     USB_IFACE_INDEX_ANY);
  566                 if (err) {
  567                         goto done;
  568                 }
  569         }
  570         /* start control transfer, if device mode */
  571 
  572         if (child->flags.usb_mode == USB_MODE_DEVICE)
  573                 usbd_ctrl_transfer_setup(child);
  574 
  575         /* if a HUB becomes present, do a recursive HUB explore */
  576 
  577         if (child->hub)
  578                 err = (child->hub->explore) (child);
  579 
  580 done:
  581         return (err);
  582 }
  583 
  584 /*------------------------------------------------------------------------*
  585  *      uhub_read_port_status - factored out code
  586  *------------------------------------------------------------------------*/
  587 static usb_error_t
  588 uhub_read_port_status(struct uhub_softc *sc, uint8_t portno)
  589 {
  590         struct usb_port_status ps;
  591         usb_error_t err;
  592 
  593         if (sc->sc_usb_port_errors >= UHUB_USB_PORT_ERRORS_MAX) {
  594                 DPRINTFN(4, "port %d, HUB looks dead, too many errors\n", portno);
  595                 sc->sc_st.port_status = 0;
  596                 sc->sc_st.port_change = 0;
  597                 return (USB_ERR_TIMEOUT);
  598         }
  599 
  600         err = usbd_req_get_port_status(
  601             sc->sc_udev, NULL, &ps, portno);
  602 
  603         if (err == 0) {
  604                 sc->sc_st.port_status = UGETW(ps.wPortStatus);
  605                 sc->sc_st.port_change = UGETW(ps.wPortChange);
  606                 sc->sc_usb_port_errors = 0;
  607         } else {
  608                 sc->sc_st.port_status = 0;
  609                 sc->sc_st.port_change = 0;
  610                 sc->sc_usb_port_errors++;
  611         }
  612 
  613         /* debugging print */
  614 
  615         DPRINTFN(4, "port %d, wPortStatus=0x%04x, "
  616             "wPortChange=0x%04x, err=%s\n",
  617             portno, sc->sc_st.port_status,
  618             sc->sc_st.port_change, usbd_errstr(err));
  619         return (err);
  620 }
  621 
  622 /*------------------------------------------------------------------------*
  623  *      uhub_reattach_port
  624  *
  625  * Returns:
  626  *    0: Success
  627  * Else: A control transaction failed
  628  *------------------------------------------------------------------------*/
  629 static usb_error_t
  630 uhub_reattach_port(struct uhub_softc *sc, uint8_t portno)
  631 {
  632         struct usb_device *child;
  633         struct usb_device *udev;
  634         enum usb_dev_speed speed;
  635         enum usb_hc_mode mode;
  636         usb_error_t err;
  637         uint16_t power_mask;
  638         uint8_t timeout;
  639 
  640         DPRINTF("reattaching port %d\n", portno);
  641 
  642         timeout = 0;
  643         udev = sc->sc_udev;
  644         child = usb_bus_port_get_device(udev->bus,
  645             udev->hub->ports + portno - 1);
  646 
  647 repeat:
  648 
  649         /* first clear the port connection change bit */
  650 
  651         err = usbd_req_clear_port_feature(udev, NULL,
  652             portno, UHF_C_PORT_CONNECTION);
  653 
  654         if (err)
  655                 goto error;
  656 
  657         /* check if there is a child */
  658 
  659         if (child != NULL) {
  660                 /*
  661                  * Free USB device and all subdevices, if any.
  662                  */
  663                 usb_free_device(child, 0);
  664                 child = NULL;
  665         }
  666         /* get fresh status */
  667 
  668         err = uhub_read_port_status(sc, portno);
  669         if (err)
  670                 goto error;
  671 
  672 #if USB_HAVE_DISABLE_ENUM
  673         /* check if we should skip enumeration from this USB HUB */
  674         if (usb_disable_enumeration != 0 ||
  675             sc->sc_disable_enumeration != 0) {
  676                 DPRINTF("Enumeration is disabled!\n");
  677                 goto error;
  678         }
  679 #endif
  680         /* check if nothing is connected to the port */
  681 
  682         if (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))
  683                 goto error;
  684 
  685         /* check if there is no power on the port and print a warning */
  686 
  687         switch (udev->speed) {
  688         case USB_SPEED_HIGH:
  689         case USB_SPEED_FULL:
  690         case USB_SPEED_LOW:
  691                 power_mask = UPS_PORT_POWER;
  692                 break;
  693         case USB_SPEED_SUPER:
  694                 if (udev->parent_hub == NULL)
  695                         power_mask = 0; /* XXX undefined */
  696                 else
  697                         power_mask = UPS_PORT_POWER_SS;
  698                 break;
  699         default:
  700                 power_mask = 0;
  701                 break;
  702         }
  703         if ((sc->sc_st.port_status & power_mask) != power_mask) {
  704                 DPRINTF("WARNING: strange, connected port %d "
  705                     "has no power\n", portno);
  706         }
  707 
  708         /* check if the device is in Host Mode */
  709 
  710         if (!(sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)) {
  711 
  712                 DPRINTF("Port %d is in Host Mode\n", portno);
  713 
  714                 if (sc->sc_st.port_status & UPS_SUSPEND) {
  715                         /*
  716                          * NOTE: Should not get here in SuperSpeed
  717                          * mode, because the HUB should report this
  718                          * bit as zero.
  719                          */
  720                         DPRINTF("Port %d was still "
  721                             "suspended, clearing.\n", portno);
  722                         err = usbd_req_clear_port_feature(udev,
  723                             NULL, portno, UHF_PORT_SUSPEND);
  724                 }
  725 
  726                 /* USB Host Mode */
  727 
  728                 /* wait for maximum device power up time */
  729 
  730                 usb_pause_mtx(NULL, 
  731                     USB_MS_TO_TICKS(usb_port_powerup_delay));
  732 
  733                 /* reset port, which implies enabling it */
  734 
  735                 err = usbd_req_reset_port(udev, NULL, portno);
  736 
  737                 if (err) {
  738                         DPRINTFN(0, "port %d reset "
  739                             "failed, error=%s\n",
  740                             portno, usbd_errstr(err));
  741                         goto error;
  742                 }
  743                 /* get port status again, it might have changed during reset */
  744 
  745                 err = uhub_read_port_status(sc, portno);
  746                 if (err) {
  747                         goto error;
  748                 }
  749                 /* check if something changed during port reset */
  750 
  751                 if ((sc->sc_st.port_change & UPS_C_CONNECT_STATUS) ||
  752                     (!(sc->sc_st.port_status & UPS_CURRENT_CONNECT_STATUS))) {
  753                         if (timeout) {
  754                                 DPRINTFN(0, "giving up port reset "
  755                                     "- device vanished\n");
  756                                 goto error;
  757                         }
  758                         timeout = 1;
  759                         goto repeat;
  760                 }
  761         } else {
  762                 DPRINTF("Port %d is in Device Mode\n", portno);
  763         }
  764 
  765         /*
  766          * Figure out the device speed
  767          */
  768         switch (udev->speed) {
  769         case USB_SPEED_HIGH:
  770                 if (sc->sc_st.port_status & UPS_HIGH_SPEED)
  771                         speed = USB_SPEED_HIGH;
  772                 else if (sc->sc_st.port_status & UPS_LOW_SPEED)
  773                         speed = USB_SPEED_LOW;
  774                 else
  775                         speed = USB_SPEED_FULL;
  776                 break;
  777         case USB_SPEED_FULL:
  778                 if (sc->sc_st.port_status & UPS_LOW_SPEED)
  779                         speed = USB_SPEED_LOW;
  780                 else
  781                         speed = USB_SPEED_FULL;
  782                 break;
  783         case USB_SPEED_LOW:
  784                 speed = USB_SPEED_LOW;
  785                 break;
  786         case USB_SPEED_SUPER:
  787                 if (udev->parent_hub == NULL) {
  788                         /* Root HUB - special case */
  789                         switch (sc->sc_st.port_status & UPS_OTHER_SPEED) {
  790                         case 0:
  791                                 speed = USB_SPEED_FULL;
  792                                 break;
  793                         case UPS_LOW_SPEED:
  794                                 speed = USB_SPEED_LOW;
  795                                 break;
  796                         case UPS_HIGH_SPEED:
  797                                 speed = USB_SPEED_HIGH;
  798                                 break;
  799                         default:
  800                                 speed = USB_SPEED_SUPER;
  801                                 break;
  802                         }
  803                 } else {
  804                         speed = USB_SPEED_SUPER;
  805                 }
  806                 break;
  807         default:
  808                 /* same speed like parent */
  809                 speed = udev->speed;
  810                 break;
  811         }
  812         if (speed == USB_SPEED_SUPER) {
  813                 err = usbd_req_set_hub_u1_timeout(udev, NULL,
  814                     portno, 128 - (2 * udev->depth));
  815                 if (err) {
  816                         DPRINTFN(0, "port %d U1 timeout "
  817                             "failed, error=%s\n",
  818                             portno, usbd_errstr(err));
  819                 }
  820                 err = usbd_req_set_hub_u2_timeout(udev, NULL,
  821                     portno, 128 - (2 * udev->depth));
  822                 if (err) {
  823                         DPRINTFN(0, "port %d U2 timeout "
  824                             "failed, error=%s\n",
  825                             portno, usbd_errstr(err));
  826                 }
  827         }
  828 
  829         /*
  830          * Figure out the device mode
  831          *
  832          * NOTE: This part is currently FreeBSD specific.
  833          */
  834         if (udev->parent_hub != NULL) {
  835                 /* inherit mode from the parent HUB */
  836                 mode = udev->parent_hub->flags.usb_mode;
  837         } else if (sc->sc_st.port_status & UPS_PORT_MODE_DEVICE)
  838                 mode = USB_MODE_DEVICE;
  839         else
  840                 mode = USB_MODE_HOST;
  841 
  842         /* need to create a new child */
  843         child = usb_alloc_device(sc->sc_dev, udev->bus, udev,
  844             udev->depth + 1, portno - 1, portno, speed, mode);
  845         if (child == NULL) {
  846                 DPRINTFN(0, "could not allocate new device\n");
  847                 goto error;
  848         }
  849         return (0);                     /* success */
  850 
  851 error:
  852         if (child != NULL) {
  853                 /*
  854                  * Free USB device and all subdevices, if any.
  855                  */
  856                 usb_free_device(child, 0);
  857                 child = NULL;
  858         }
  859         if (err == 0) {
  860                 if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
  861                         err = usbd_req_clear_port_feature(
  862                             sc->sc_udev, NULL,
  863                             portno, UHF_PORT_ENABLE);
  864                 }
  865         }
  866         if (err) {
  867                 DPRINTFN(0, "device problem (%s), "
  868                     "disabling port %d\n", usbd_errstr(err), portno);
  869         }
  870         return (err);
  871 }
  872 
  873 /*------------------------------------------------------------------------*
  874  *      usb_device_20_compatible
  875  *
  876  * Returns:
  877  *    0: HUB does not support suspend and resume
  878  * Else: HUB supports suspend and resume
  879  *------------------------------------------------------------------------*/
  880 static uint8_t
  881 usb_device_20_compatible(struct usb_device *udev)
  882 {
  883         if (udev == NULL)
  884                 return (0);
  885         switch (udev->speed) {
  886         case USB_SPEED_LOW:
  887         case USB_SPEED_FULL:
  888         case USB_SPEED_HIGH:
  889                 return (1);
  890         default:
  891                 return (0);
  892         }
  893 }
  894 
  895 /*------------------------------------------------------------------------*
  896  *      uhub_suspend_resume_port
  897  *
  898  * Returns:
  899  *    0: Success
  900  * Else: A control transaction failed
  901  *------------------------------------------------------------------------*/
  902 static usb_error_t
  903 uhub_suspend_resume_port(struct uhub_softc *sc, uint8_t portno)
  904 {
  905         struct usb_device *child;
  906         struct usb_device *udev;
  907         uint8_t is_suspend;
  908         usb_error_t err;
  909 
  910         DPRINTF("port %d\n", portno);
  911 
  912         udev = sc->sc_udev;
  913         child = usb_bus_port_get_device(udev->bus,
  914             udev->hub->ports + portno - 1);
  915 
  916         /* first clear the port suspend change bit */
  917 
  918         if (usb_device_20_compatible(udev)) {
  919                 err = usbd_req_clear_port_feature(udev, NULL,
  920                     portno, UHF_C_PORT_SUSPEND);
  921         } else {
  922                 err = usbd_req_clear_port_feature(udev, NULL,
  923                     portno, UHF_C_PORT_LINK_STATE);
  924         }
  925 
  926         if (err) {
  927                 DPRINTF("clearing suspend failed.\n");
  928                 goto done;
  929         }
  930         /* get fresh status */
  931 
  932         err = uhub_read_port_status(sc, portno);
  933         if (err) {
  934                 DPRINTF("reading port status failed.\n");
  935                 goto done;
  936         }
  937         /* convert current state */
  938 
  939         if (usb_device_20_compatible(udev)) {
  940                 if (sc->sc_st.port_status & UPS_SUSPEND) {
  941                         is_suspend = 1;
  942                 } else {
  943                         is_suspend = 0;
  944                 }
  945         } else {
  946                 switch (UPS_PORT_LINK_STATE_GET(sc->sc_st.port_status)) {
  947                 case UPS_PORT_LS_U3:
  948                         is_suspend = 1;
  949                         break;
  950                 case UPS_PORT_LS_SS_INA:
  951                         usbd_req_warm_reset_port(udev, NULL, portno);
  952                         is_suspend = 0;
  953                         break;
  954                 default:
  955                         is_suspend = 0;
  956                         break;
  957                 }
  958         }
  959 
  960         DPRINTF("suspended=%u\n", is_suspend);
  961 
  962         /* do the suspend or resume */
  963 
  964         if (child) {
  965                 /*
  966                  * This code handle two cases: 1) Host Mode - we can only
  967                  * receive resume here 2) Device Mode - we can receive
  968                  * suspend and resume here
  969                  */
  970                 if (is_suspend == 0)
  971                         usb_dev_resume_peer(child);
  972                 else if (child->flags.usb_mode == USB_MODE_DEVICE)
  973                         usb_dev_suspend_peer(child);
  974         }
  975 done:
  976         return (err);
  977 }
  978 
  979 /*------------------------------------------------------------------------*
  980  *      uhub_root_interrupt
  981  *
  982  * This function is called when a Root HUB interrupt has
  983  * happened. "ptr" and "len" makes up the Root HUB interrupt
  984  * packet. This function is called having the "bus_mtx" locked.
  985  *------------------------------------------------------------------------*/
  986 void
  987 uhub_root_intr(struct usb_bus *bus, const uint8_t *ptr, uint8_t len)
  988 {
  989         USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
  990 
  991         usb_needs_explore(bus, 0);
  992 }
  993 
  994 static uint8_t
  995 uhub_is_too_deep(struct usb_device *udev)
  996 {
  997         switch (udev->speed) {
  998         case USB_SPEED_FULL:
  999         case USB_SPEED_LOW:
 1000         case USB_SPEED_HIGH:
 1001                 if (udev->depth > USB_HUB_MAX_DEPTH)
 1002                         return (1);
 1003                 break;
 1004         case USB_SPEED_SUPER:
 1005                 if (udev->depth > USB_SS_HUB_DEPTH_MAX)
 1006                         return (1);
 1007                 break;
 1008         default:
 1009                 break;
 1010         }
 1011         return (0);
 1012 }
 1013 
 1014 /*------------------------------------------------------------------------*
 1015  *      uhub_explore
 1016  *
 1017  * Returns:
 1018  *     0: Success
 1019  *  Else: Failure
 1020  *------------------------------------------------------------------------*/
 1021 static usb_error_t
 1022 uhub_explore(struct usb_device *udev)
 1023 {
 1024         struct usb_hub *hub;
 1025         struct uhub_softc *sc;
 1026         struct usb_port *up;
 1027         usb_error_t err;
 1028         uint8_t portno;
 1029         uint8_t x;
 1030         uint8_t do_unlock;
 1031 
 1032         hub = udev->hub;
 1033         sc = hub->hubsoftc;
 1034 
 1035         DPRINTFN(11, "udev=%p addr=%d\n", udev, udev->address);
 1036 
 1037         /* ignore devices that are too deep */
 1038         if (uhub_is_too_deep(udev))
 1039                 return (USB_ERR_TOO_DEEP);
 1040 
 1041         /* check if device is suspended */
 1042         if (udev->flags.self_suspended) {
 1043                 /* need to wait until the child signals resume */
 1044                 DPRINTF("Device is suspended!\n");
 1045                 return (0);
 1046         }
 1047 
 1048         /*
 1049          * Make sure we don't race against user-space applications
 1050          * like LibUSB:
 1051          */
 1052         do_unlock = usbd_enum_lock(udev);
 1053 
 1054         for (x = 0; x != hub->nports; x++) {
 1055                 up = hub->ports + x;
 1056                 portno = x + 1;
 1057 
 1058                 err = uhub_read_port_status(sc, portno);
 1059                 if (err) {
 1060                         /* most likely the HUB is gone */
 1061                         break;
 1062                 }
 1063                 if (sc->sc_st.port_change & UPS_C_OVERCURRENT_INDICATOR) {
 1064                         DPRINTF("Overcurrent on port %u.\n", portno);
 1065                         err = usbd_req_clear_port_feature(
 1066                             udev, NULL, portno, UHF_C_PORT_OVER_CURRENT);
 1067                         if (err) {
 1068                                 /* most likely the HUB is gone */
 1069                                 break;
 1070                         }
 1071                 }
 1072                 if (!(sc->sc_flags & UHUB_FLAG_DID_EXPLORE)) {
 1073                         /*
 1074                          * Fake a connect status change so that the
 1075                          * status gets checked initially!
 1076                          */
 1077                         sc->sc_st.port_change |=
 1078                             UPS_C_CONNECT_STATUS;
 1079                 }
 1080                 if (sc->sc_st.port_change & UPS_C_PORT_ENABLED) {
 1081                         err = usbd_req_clear_port_feature(
 1082                             udev, NULL, portno, UHF_C_PORT_ENABLE);
 1083                         if (err) {
 1084                                 /* most likely the HUB is gone */
 1085                                 break;
 1086                         }
 1087                         if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
 1088                                 /*
 1089                                  * Ignore the port error if the device
 1090                                  * has vanished !
 1091                                  */
 1092                         } else if (sc->sc_st.port_status & UPS_PORT_ENABLED) {
 1093                                 DPRINTFN(0, "illegal enable change, "
 1094                                     "port %d\n", portno);
 1095                         } else {
 1096 
 1097                                 if (up->restartcnt == USB_RESTART_MAX) {
 1098                                         /* XXX could try another speed ? */
 1099                                         DPRINTFN(0, "port error, giving up "
 1100                                             "port %d\n", portno);
 1101                                 } else {
 1102                                         sc->sc_st.port_change |=
 1103                                             UPS_C_CONNECT_STATUS;
 1104                                         up->restartcnt++;
 1105                                 }
 1106                         }
 1107                 }
 1108                 if (sc->sc_st.port_change & UPS_C_CONNECT_STATUS) {
 1109                         err = uhub_reattach_port(sc, portno);
 1110                         if (err) {
 1111                                 /* most likely the HUB is gone */
 1112                                 break;
 1113                         }
 1114                 }
 1115                 if (sc->sc_st.port_change & (UPS_C_SUSPEND |
 1116                     UPS_C_PORT_LINK_STATE)) {
 1117                         err = uhub_suspend_resume_port(sc, portno);
 1118                         if (err) {
 1119                                 /* most likely the HUB is gone */
 1120                                 break;
 1121                         }
 1122                 }
 1123                 err = uhub_explore_sub(sc, up);
 1124                 if (err) {
 1125                         /* no device(s) present */
 1126                         continue;
 1127                 }
 1128                 /* explore succeeded - reset restart counter */
 1129                 up->restartcnt = 0;
 1130         }
 1131 
 1132         if (do_unlock)
 1133                 usbd_enum_unlock(udev);
 1134 
 1135         /* initial status checked */
 1136         sc->sc_flags |= UHUB_FLAG_DID_EXPLORE;
 1137 
 1138         /* return success */
 1139         return (USB_ERR_NORMAL_COMPLETION);
 1140 }
 1141 
 1142 static int
 1143 uhub_probe(device_t dev)
 1144 {
 1145         struct usb_attach_arg *uaa = device_get_ivars(dev);
 1146 
 1147         if (uaa->usb_mode != USB_MODE_HOST)
 1148                 return (ENXIO);
 1149 
 1150         /*
 1151          * The subclass for USB HUBs is currently ignored because it
 1152          * is 0 for some and 1 for others.
 1153          */
 1154         if (uaa->info.bConfigIndex == 0 &&
 1155             uaa->info.bDeviceClass == UDCLASS_HUB)
 1156                 return (0);
 1157 
 1158         return (ENXIO);
 1159 }
 1160 
 1161 /* NOTE: The information returned by this function can be wrong. */
 1162 usb_error_t
 1163 uhub_query_info(struct usb_device *udev, uint8_t *pnports, uint8_t *ptt)
 1164 {
 1165         struct usb_hub_descriptor hubdesc20;
 1166         struct usb_hub_ss_descriptor hubdesc30;
 1167         usb_error_t err;
 1168         uint8_t nports;
 1169         uint8_t tt;
 1170 
 1171         if (udev->ddesc.bDeviceClass != UDCLASS_HUB)
 1172                 return (USB_ERR_INVAL);
 1173 
 1174         nports = 0;
 1175         tt = 0;
 1176 
 1177         switch (udev->speed) {
 1178         case USB_SPEED_LOW:
 1179         case USB_SPEED_FULL:
 1180         case USB_SPEED_HIGH:
 1181                 /* assuming that there is one port */
 1182                 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
 1183                 if (err) {
 1184                         DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
 1185                             "error=%s\n", usbd_errstr(err));
 1186                         break;
 1187                 }
 1188                 nports = hubdesc20.bNbrPorts;
 1189                 if (nports > 127)
 1190                         nports = 127;
 1191 
 1192                 if (udev->speed == USB_SPEED_HIGH)
 1193                         tt = (UGETW(hubdesc20.wHubCharacteristics) >> 5) & 3;
 1194                 break;
 1195 
 1196         case USB_SPEED_SUPER:
 1197                 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
 1198                 if (err) {
 1199                         DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
 1200                             "error=%s\n", usbd_errstr(err));
 1201                         break;
 1202                 }
 1203                 nports = hubdesc30.bNbrPorts;
 1204                 if (nports > 16)
 1205                         nports = 16;
 1206                 break;
 1207 
 1208         default:
 1209                 err = USB_ERR_INVAL;
 1210                 break;
 1211         }
 1212 
 1213         if (pnports != NULL)
 1214                 *pnports = nports;
 1215 
 1216         if (ptt != NULL)
 1217                 *ptt = tt;
 1218 
 1219         return (err);
 1220 }
 1221 
 1222 static int
 1223 uhub_attach(device_t dev)
 1224 {
 1225         struct uhub_softc *sc = device_get_softc(dev);
 1226         struct usb_attach_arg *uaa = device_get_ivars(dev);
 1227         struct usb_device *udev = uaa->device;
 1228         struct usb_device *parent_hub = udev->parent_hub;
 1229         struct usb_hub *hub;
 1230         struct usb_hub_descriptor hubdesc20;
 1231         struct usb_hub_ss_descriptor hubdesc30;
 1232 #if USB_HAVE_DISABLE_ENUM
 1233         struct sysctl_ctx_list *sysctl_ctx;
 1234         struct sysctl_oid *sysctl_tree;
 1235 #endif
 1236         uint16_t pwrdly;
 1237         uint16_t nports;
 1238         uint8_t x;
 1239         uint8_t portno;
 1240         uint8_t removable;
 1241         uint8_t iface_index;
 1242         usb_error_t err;
 1243 
 1244         sc->sc_udev = udev;
 1245         sc->sc_dev = dev;
 1246 
 1247         mtx_init(&sc->sc_mtx, "USB HUB mutex", NULL, MTX_DEF);
 1248 
 1249         device_set_usb_desc(dev);
 1250 
 1251         DPRINTFN(2, "depth=%d selfpowered=%d, parent=%p, "
 1252             "parent->selfpowered=%d\n",
 1253             udev->depth,
 1254             udev->flags.self_powered,
 1255             parent_hub,
 1256             parent_hub ?
 1257             parent_hub->flags.self_powered : 0);
 1258 
 1259         if (uhub_is_too_deep(udev)) {
 1260                 DPRINTFN(0, "HUB at depth %d, "
 1261                     "exceeds maximum. HUB ignored\n", (int)udev->depth);
 1262                 goto error;
 1263         }
 1264 
 1265         if (!udev->flags.self_powered && parent_hub &&
 1266             !parent_hub->flags.self_powered) {
 1267                 DPRINTFN(0, "Bus powered HUB connected to "
 1268                     "bus powered HUB. HUB ignored\n");
 1269                 goto error;
 1270         }
 1271 
 1272         if (UHUB_IS_MULTI_TT(sc)) {
 1273                 err = usbd_set_alt_interface_index(udev, 0, 1);
 1274                 if (err) {
 1275                         device_printf(dev, "MTT could not be enabled\n");
 1276                         goto error;
 1277                 }
 1278                 device_printf(dev, "MTT enabled\n");
 1279         }
 1280 
 1281         /* get HUB descriptor */
 1282 
 1283         DPRINTFN(2, "Getting HUB descriptor\n");
 1284 
 1285         switch (udev->speed) {
 1286         case USB_SPEED_LOW:
 1287         case USB_SPEED_FULL:
 1288         case USB_SPEED_HIGH:
 1289                 /* assuming that there is one port */
 1290                 err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, 1);
 1291                 if (err) {
 1292                         DPRINTFN(0, "getting USB 2.0 HUB descriptor failed,"
 1293                             "error=%s\n", usbd_errstr(err));
 1294                         goto error;
 1295                 }
 1296                 /* get number of ports */
 1297                 nports = hubdesc20.bNbrPorts;
 1298 
 1299                 /* get power delay */
 1300                 pwrdly = ((hubdesc20.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
 1301                     usb_extra_power_up_time);
 1302 
 1303                 /* get complete HUB descriptor */
 1304                 if (nports >= 8) {
 1305                         /* check number of ports */
 1306                         if (nports > 127) {
 1307                                 DPRINTFN(0, "Invalid number of USB 2.0 ports,"
 1308                                     "error=%s\n", usbd_errstr(err));
 1309                                 goto error;
 1310                         }
 1311                         /* get complete HUB descriptor */
 1312                         err = usbd_req_get_hub_descriptor(udev, NULL, &hubdesc20, nports);
 1313 
 1314                         if (err) {
 1315                                 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
 1316                                     "error=%s\n", usbd_errstr(err));
 1317                                 goto error;
 1318                         }
 1319                         if (hubdesc20.bNbrPorts != nports) {
 1320                                 DPRINTFN(0, "Number of ports changed\n");
 1321                                 goto error;
 1322                         }
 1323                 }
 1324                 break;
 1325         case USB_SPEED_SUPER:
 1326                 if (udev->parent_hub != NULL) {
 1327                         err = usbd_req_set_hub_depth(udev, NULL,
 1328                             udev->depth - 1);
 1329                         if (err) {
 1330                                 DPRINTFN(0, "Setting USB 3.0 HUB depth failed,"
 1331                                     "error=%s\n", usbd_errstr(err));
 1332                                 goto error;
 1333                         }
 1334                 }
 1335                 err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, 1);
 1336                 if (err) {
 1337                         DPRINTFN(0, "Getting USB 3.0 HUB descriptor failed,"
 1338                             "error=%s\n", usbd_errstr(err));
 1339                         goto error;
 1340                 }
 1341                 /* get number of ports */
 1342                 nports = hubdesc30.bNbrPorts;
 1343 
 1344                 /* get power delay */
 1345                 pwrdly = ((hubdesc30.bPwrOn2PwrGood * UHD_PWRON_FACTOR) +
 1346                     usb_extra_power_up_time);
 1347 
 1348                 /* get complete HUB descriptor */
 1349                 if (nports >= 8) {
 1350                         /* check number of ports */
 1351                         if (nports > ((udev->parent_hub != NULL) ? 15 : 127)) {
 1352                                 DPRINTFN(0, "Invalid number of USB 3.0 ports,"
 1353                                     "error=%s\n", usbd_errstr(err));
 1354                                 goto error;
 1355                         }
 1356                         /* get complete HUB descriptor */
 1357                         err = usbd_req_get_ss_hub_descriptor(udev, NULL, &hubdesc30, nports);
 1358 
 1359                         if (err) {
 1360                                 DPRINTFN(0, "Getting USB 2.0 HUB descriptor failed,"
 1361                                     "error=%s\n", usbd_errstr(err));
 1362                                 goto error;
 1363                         }
 1364                         if (hubdesc30.bNbrPorts != nports) {
 1365                                 DPRINTFN(0, "Number of ports changed\n");
 1366                                 goto error;
 1367                         }
 1368                 }
 1369                 break;
 1370         default:
 1371                 DPRINTF("Assuming HUB has only one port\n");
 1372                 /* default number of ports */
 1373                 nports = 1;
 1374                 /* default power delay */
 1375                 pwrdly = ((10 * UHD_PWRON_FACTOR) + usb_extra_power_up_time);
 1376                 break;
 1377         }
 1378         if (nports == 0) {
 1379                 DPRINTFN(0, "portless HUB\n");
 1380                 goto error;
 1381         }
 1382         if (nports > USB_MAX_PORTS) {
 1383                 DPRINTF("Port limit exceeded\n");
 1384                 goto error;
 1385         }
 1386 #if (USB_HAVE_FIXED_PORT == 0)
 1387         hub = malloc(sizeof(hub[0]) + (sizeof(hub->ports[0]) * nports),
 1388             M_USBDEV, M_WAITOK | M_ZERO);
 1389 
 1390         if (hub == NULL)
 1391                 goto error;
 1392 #else
 1393         hub = &sc->sc_hub;
 1394 #endif
 1395         udev->hub = hub;
 1396 
 1397         /* initialize HUB structure */
 1398         hub->hubsoftc = sc;
 1399         hub->explore = &uhub_explore;
 1400         hub->nports = nports;
 1401         hub->hubudev = udev;
 1402 #if USB_HAVE_TT_SUPPORT
 1403         hub->tt_msg[0].hdr.pm_callback = &uhub_reset_tt_proc;
 1404         hub->tt_msg[0].udev = udev;
 1405         hub->tt_msg[1].hdr.pm_callback = &uhub_reset_tt_proc;
 1406         hub->tt_msg[1].udev = udev;
 1407 #endif
 1408         /* if self powered hub, give ports maximum current */
 1409         if (udev->flags.self_powered) {
 1410                 hub->portpower = USB_MAX_POWER;
 1411         } else {
 1412                 hub->portpower = USB_MIN_POWER;
 1413         }
 1414 
 1415         /* set up interrupt pipe */
 1416         iface_index = 0;
 1417         if (udev->parent_hub == NULL) {
 1418                 /* root HUB is special */
 1419                 err = 0;
 1420         } else {
 1421                 /* normal HUB */
 1422                 err = usbd_transfer_setup(udev, &iface_index, sc->sc_xfer,
 1423                     uhub_config, UHUB_N_TRANSFER, sc, &sc->sc_mtx);
 1424         }
 1425         if (err) {
 1426                 DPRINTFN(0, "cannot setup interrupt transfer, "
 1427                     "errstr=%s\n", usbd_errstr(err));
 1428                 goto error;
 1429         }
 1430         /* wait with power off for a while */
 1431         usb_pause_mtx(NULL, USB_MS_TO_TICKS(USB_POWER_DOWN_TIME));
 1432 
 1433 #if USB_HAVE_DISABLE_ENUM
 1434         /* Add device sysctls */
 1435 
 1436         sysctl_ctx = device_get_sysctl_ctx(dev);
 1437         sysctl_tree = device_get_sysctl_tree(dev);
 1438 
 1439         if (sysctl_ctx != NULL && sysctl_tree != NULL) {
 1440                 char path[128];
 1441 
 1442                 snprintf(path, sizeof(path), "dev.uhub.%d.disable_enumeration",
 1443                     device_get_unit(dev));
 1444                 TUNABLE_INT_FETCH(path, &sc->sc_disable_enumeration);
 1445 
 1446                 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
 1447                     OID_AUTO, "disable_enumeration", CTLFLAG_RWTUN,
 1448                     &sc->sc_disable_enumeration, 0,
 1449                     "Set to disable enumeration on this USB HUB.");
 1450 
 1451                 snprintf(path, sizeof(path), "dev.uhub.%d.disable_port_power",
 1452                     device_get_unit(dev));
 1453                 TUNABLE_INT_FETCH(path, &sc->sc_disable_port_power);
 1454 
 1455                 (void) SYSCTL_ADD_INT(sysctl_ctx, SYSCTL_CHILDREN(sysctl_tree),
 1456                     OID_AUTO, "disable_port_power", CTLFLAG_RWTUN,
 1457                     &sc->sc_disable_port_power, 0,
 1458                     "Set to disable USB port power on this USB HUB.");
 1459         }
 1460 #endif
 1461         /*
 1462          * To have the best chance of success we do things in the exact same
 1463          * order as Windoze98.  This should not be necessary, but some
 1464          * devices do not follow the USB specs to the letter.
 1465          *
 1466          * These are the events on the bus when a hub is attached:
 1467          *  Get device and config descriptors (see attach code)
 1468          *  Get hub descriptor (see above)
 1469          *  For all ports
 1470          *     turn on power
 1471          *     wait for power to become stable
 1472          * (all below happens in explore code)
 1473          *  For all ports
 1474          *     clear C_PORT_CONNECTION
 1475          *  For all ports
 1476          *     get port status
 1477          *     if device connected
 1478          *        wait 100 ms
 1479          *        turn on reset
 1480          *        wait
 1481          *        clear C_PORT_RESET
 1482          *        get port status
 1483          *        proceed with device attachment
 1484          */
 1485 
 1486         /* XXX should check for none, individual, or ganged power? */
 1487 
 1488         removable = 0;
 1489 
 1490         for (x = 0; x != nports; x++) {
 1491                 /* set up data structures */
 1492                 struct usb_port *up = hub->ports + x;
 1493 
 1494                 up->device_index = 0;
 1495                 up->restartcnt = 0;
 1496                 portno = x + 1;
 1497 
 1498                 /* check if port is removable */
 1499                 switch (udev->speed) {
 1500                 case USB_SPEED_LOW:
 1501                 case USB_SPEED_FULL:
 1502                 case USB_SPEED_HIGH:
 1503                         if (!UHD_NOT_REMOV(&hubdesc20, portno))
 1504                                 removable++;
 1505                         break;
 1506                 case USB_SPEED_SUPER:
 1507                         if (!UHD_NOT_REMOV(&hubdesc30, portno))
 1508                                 removable++;
 1509                         break;
 1510                 default:
 1511                         DPRINTF("Assuming removable port\n");
 1512                         removable++;
 1513                         break;
 1514                 }
 1515                 if (err == 0) {
 1516 #if USB_HAVE_DISABLE_ENUM
 1517                         /* check if we should disable USB port power or not */
 1518                         if (usb_disable_port_power != 0 ||
 1519                             sc->sc_disable_port_power != 0) {
 1520                                 /* turn the power off */
 1521                                 DPRINTFN(2, "Turning port %d power off\n", portno);
 1522                                 err = usbd_req_clear_port_feature(udev, NULL,
 1523                                     portno, UHF_PORT_POWER);
 1524                         } else {
 1525 #endif
 1526                                 /* turn the power on */
 1527                                 DPRINTFN(2, "Turning port %d power on\n", portno);
 1528                                 err = usbd_req_set_port_feature(udev, NULL,
 1529                                     portno, UHF_PORT_POWER);
 1530 #if USB_HAVE_DISABLE_ENUM
 1531                         }
 1532 #endif
 1533                 }
 1534                 if (err != 0) {
 1535                         DPRINTFN(0, "port %d power on or off failed, %s\n",
 1536                             portno, usbd_errstr(err));
 1537                 }
 1538                 DPRINTF("turn on port %d power\n",
 1539                     portno);
 1540 
 1541                 /* wait for stable power */
 1542                 usb_pause_mtx(NULL, USB_MS_TO_TICKS(pwrdly));
 1543         }
 1544 
 1545         device_printf(dev, "%d port%s with %d "
 1546             "removable, %s powered\n", nports, (nports != 1) ? "s" : "",
 1547             removable, udev->flags.self_powered ? "self" : "bus");
 1548 
 1549         /* Start the interrupt endpoint, if any */
 1550 
 1551         mtx_lock(&sc->sc_mtx);
 1552         usbd_transfer_start(sc->sc_xfer[UHUB_INTR_TRANSFER]);
 1553         mtx_unlock(&sc->sc_mtx);
 1554 
 1555         /* Enable automatic power save on all USB HUBs */
 1556 
 1557         usbd_set_power_mode(udev, USB_POWER_MODE_SAVE);
 1558 
 1559         return (0);
 1560 
 1561 error:
 1562         usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
 1563 
 1564 #if (USB_HAVE_FIXED_PORT == 0)
 1565         free(udev->hub, M_USBDEV);
 1566 #endif
 1567         udev->hub = NULL;
 1568 
 1569         mtx_destroy(&sc->sc_mtx);
 1570 
 1571         return (ENXIO);
 1572 }
 1573 
 1574 /*
 1575  * Called from process context when the hub is gone.
 1576  * Detach all devices on active ports.
 1577  */
 1578 static int
 1579 uhub_detach(device_t dev)
 1580 {
 1581         struct uhub_softc *sc = device_get_softc(dev);
 1582         struct usb_hub *hub = sc->sc_udev->hub;
 1583         struct usb_bus *bus = sc->sc_udev->bus;
 1584         struct usb_device *child;
 1585         uint8_t x;
 1586 
 1587         if (hub == NULL)                /* must be partially working */
 1588                 return (0);
 1589 
 1590         /* Make sure interrupt transfer is gone. */
 1591         usbd_transfer_unsetup(sc->sc_xfer, UHUB_N_TRANSFER);
 1592 
 1593         /* Detach all ports */
 1594         for (x = 0; x != hub->nports; x++) {
 1595 
 1596                 child = usb_bus_port_get_device(bus, hub->ports + x);
 1597 
 1598                 if (child == NULL) {
 1599                         continue;
 1600                 }
 1601 
 1602                 /*
 1603                  * Free USB device and all subdevices, if any.
 1604                  */
 1605                 usb_free_device(child, 0);
 1606         }
 1607 
 1608 #if USB_HAVE_TT_SUPPORT
 1609         /* Make sure our TT messages are not queued anywhere */
 1610         USB_BUS_LOCK(bus);
 1611         usb_proc_mwait(USB_BUS_TT_PROC(bus),
 1612             &hub->tt_msg[0], &hub->tt_msg[1]);
 1613         USB_BUS_UNLOCK(bus);
 1614 #endif
 1615 
 1616 #if (USB_HAVE_FIXED_PORT == 0)
 1617         free(hub, M_USBDEV);
 1618 #endif
 1619         sc->sc_udev->hub = NULL;
 1620 
 1621         mtx_destroy(&sc->sc_mtx);
 1622 
 1623         return (0);
 1624 }
 1625 
 1626 static int
 1627 uhub_suspend(device_t dev)
 1628 {
 1629         DPRINTF("\n");
 1630         /* Sub-devices are not suspended here! */
 1631         return (0);
 1632 }
 1633 
 1634 static int
 1635 uhub_resume(device_t dev)
 1636 {
 1637         DPRINTF("\n");
 1638         /* Sub-devices are not resumed here! */
 1639         return (0);
 1640 }
 1641 
 1642 static void
 1643 uhub_driver_added(device_t dev, driver_t *driver)
 1644 {
 1645         usb_needs_explore_all();
 1646 }
 1647 
 1648 struct hub_result {
 1649         struct usb_device *udev;
 1650         uint8_t portno;
 1651         uint8_t iface_index;
 1652 };
 1653 
 1654 static void
 1655 uhub_find_iface_index(struct usb_hub *hub, device_t child,
 1656     struct hub_result *res)
 1657 {
 1658         struct usb_interface *iface;
 1659         struct usb_device *udev;
 1660         uint8_t nports;
 1661         uint8_t x;
 1662         uint8_t i;
 1663 
 1664         nports = hub->nports;
 1665         for (x = 0; x != nports; x++) {
 1666                 udev = usb_bus_port_get_device(hub->hubudev->bus,
 1667                     hub->ports + x);
 1668                 if (!udev) {
 1669                         continue;
 1670                 }
 1671                 for (i = 0; i != USB_IFACE_MAX; i++) {
 1672                         iface = usbd_get_iface(udev, i);
 1673                         if (iface &&
 1674                             (iface->subdev == child)) {
 1675                                 res->iface_index = i;
 1676                                 res->udev = udev;
 1677                                 res->portno = x + 1;
 1678                                 return;
 1679                         }
 1680                 }
 1681         }
 1682         res->iface_index = 0;
 1683         res->udev = NULL;
 1684         res->portno = 0;
 1685 }
 1686 
 1687 static int
 1688 uhub_child_location_string(device_t parent, device_t child,
 1689     char *buf, size_t buflen)
 1690 {
 1691         struct uhub_softc *sc;
 1692         struct usb_hub *hub;
 1693         struct hub_result res;
 1694 
 1695         if (!device_is_attached(parent)) {
 1696                 if (buflen)
 1697                         buf[0] = 0;
 1698                 return (0);
 1699         }
 1700 
 1701         sc = device_get_softc(parent);
 1702         hub = sc->sc_udev->hub;
 1703 
 1704         mtx_lock(&Giant);
 1705         uhub_find_iface_index(hub, child, &res);
 1706         if (!res.udev) {
 1707                 DPRINTF("device not on hub\n");
 1708                 if (buflen) {
 1709                         buf[0] = '\0';
 1710                 }
 1711                 goto done;
 1712         }
 1713         snprintf(buf, buflen, "bus=%u hubaddr=%u port=%u devaddr=%u"
 1714             " interface=%u"
 1715 #if USB_HAVE_UGEN
 1716             " ugen=%s"
 1717 #endif
 1718             , device_get_unit(res.udev->bus->bdev)
 1719             , (res.udev->parent_hub != NULL) ?
 1720             res.udev->parent_hub->device_index : 0
 1721             , res.portno, res.udev->device_index, res.iface_index
 1722 #if USB_HAVE_UGEN
 1723             , res.udev->ugen_name
 1724 #endif
 1725             );
 1726 done:
 1727         mtx_unlock(&Giant);
 1728 
 1729         return (0);
 1730 }
 1731 
 1732 static int
 1733 uhub_child_pnpinfo_string(device_t parent, device_t child,
 1734     char *buf, size_t buflen)
 1735 {
 1736         struct uhub_softc *sc;
 1737         struct usb_hub *hub;
 1738         struct usb_interface *iface;
 1739         struct hub_result res;
 1740 
 1741         if (!device_is_attached(parent)) {
 1742                 if (buflen)
 1743                         buf[0] = 0;
 1744                 return (0);
 1745         }
 1746 
 1747         sc = device_get_softc(parent);
 1748         hub = sc->sc_udev->hub;
 1749 
 1750         mtx_lock(&Giant);
 1751         uhub_find_iface_index(hub, child, &res);
 1752         if (!res.udev) {
 1753                 DPRINTF("device not on hub\n");
 1754                 if (buflen) {
 1755                         buf[0] = '\0';
 1756                 }
 1757                 goto done;
 1758         }
 1759         iface = usbd_get_iface(res.udev, res.iface_index);
 1760         if (iface && iface->idesc) {
 1761                 snprintf(buf, buflen, "vendor=0x%04x product=0x%04x "
 1762                     "devclass=0x%02x devsubclass=0x%02x "
 1763                     "sernum=\"%s\" "
 1764                     "release=0x%04x "
 1765                     "mode=%s "
 1766                     "intclass=0x%02x intsubclass=0x%02x "
 1767                     "intprotocol=0x%02x" "%s%s",
 1768                     UGETW(res.udev->ddesc.idVendor),
 1769                     UGETW(res.udev->ddesc.idProduct),
 1770                     res.udev->ddesc.bDeviceClass,
 1771                     res.udev->ddesc.bDeviceSubClass,
 1772                     usb_get_serial(res.udev),
 1773                     UGETW(res.udev->ddesc.bcdDevice),
 1774                     (res.udev->flags.usb_mode == USB_MODE_HOST) ? "host" : "device",
 1775                     iface->idesc->bInterfaceClass,
 1776                     iface->idesc->bInterfaceSubClass,
 1777                     iface->idesc->bInterfaceProtocol,
 1778                     iface->pnpinfo ? " " : "",
 1779                     iface->pnpinfo ? iface->pnpinfo : "");
 1780         } else {
 1781                 if (buflen) {
 1782                         buf[0] = '\0';
 1783                 }
 1784                 goto done;
 1785         }
 1786 done:
 1787         mtx_unlock(&Giant);
 1788 
 1789         return (0);
 1790 }
 1791 
 1792 /*
 1793  * The USB Transaction Translator:
 1794  * ===============================
 1795  *
 1796  * When doing LOW- and FULL-speed USB transfers accross a HIGH-speed
 1797  * USB HUB, bandwidth must be allocated for ISOCHRONOUS and INTERRUPT
 1798  * USB transfers. To utilize bandwidth dynamically the "scatter and
 1799  * gather" principle must be applied. This means that bandwidth must
 1800  * be divided into equal parts of bandwidth. With regard to USB all
 1801  * data is transferred in smaller packets with length
 1802  * "wMaxPacketSize". The problem however is that "wMaxPacketSize" is
 1803  * not a constant!
 1804  *
 1805  * The bandwidth scheduler which I have implemented will simply pack
 1806  * the USB transfers back to back until there is no more space in the
 1807  * schedule. Out of the 8 microframes which the USB 2.0 standard
 1808  * provides, only 6 are available for non-HIGH-speed devices. I have
 1809  * reserved the first 4 microframes for ISOCHRONOUS transfers. The
 1810  * last 2 microframes I have reserved for INTERRUPT transfers. Without
 1811  * this division, it is very difficult to allocate and free bandwidth
 1812  * dynamically.
 1813  *
 1814  * NOTE about the Transaction Translator in USB HUBs:
 1815  *
 1816  * USB HUBs have a very simple Transaction Translator, that will
 1817  * simply pipeline all the SPLIT transactions. That means that the
 1818  * transactions will be executed in the order they are queued!
 1819  *
 1820  */
 1821 
 1822 /*------------------------------------------------------------------------*
 1823  *      usb_intr_find_best_slot
 1824  *
 1825  * Return value:
 1826  *   The best Transaction Translation slot for an interrupt endpoint.
 1827  *------------------------------------------------------------------------*/
 1828 static uint8_t
 1829 usb_intr_find_best_slot(usb_size_t *ptr, uint8_t start,
 1830     uint8_t end, uint8_t mask)
 1831 {
 1832         usb_size_t min = (usb_size_t)-1;
 1833         usb_size_t sum;
 1834         uint8_t x;
 1835         uint8_t y;
 1836         uint8_t z;
 1837 
 1838         y = 0;
 1839 
 1840         /* find the last slot with lesser used bandwidth */
 1841 
 1842         for (x = start; x < end; x++) {
 1843 
 1844                 sum = 0;
 1845 
 1846                 /* compute sum of bandwidth */
 1847                 for (z = x; z < end; z++) {
 1848                         if (mask & (1U << (z - x)))
 1849                                 sum += ptr[z];
 1850                 }
 1851 
 1852                 /* check if the current multi-slot is more optimal */
 1853                 if (min >= sum) {
 1854                         min = sum;
 1855                         y = x;
 1856                 }
 1857 
 1858                 /* check if the mask is about to be shifted out */
 1859                 if (mask & (1U << (end - 1 - x)))
 1860                         break;
 1861         }
 1862         return (y);
 1863 }
 1864 
 1865 /*------------------------------------------------------------------------*
 1866  *      usb_hs_bandwidth_adjust
 1867  *
 1868  * This function will update the bandwith usage for the microframe
 1869  * having index "slot" by "len" bytes. "len" can be negative.  If the
 1870  * "slot" argument is greater or equal to "USB_HS_MICRO_FRAMES_MAX"
 1871  * the "slot" argument will be replaced by the slot having least used
 1872  * bandwidth. The "mask" argument is used for multi-slot allocations.
 1873  *
 1874  * Returns:
 1875  *    The slot in which the bandwidth update was done: 0..7
 1876  *------------------------------------------------------------------------*/
 1877 static uint8_t
 1878 usb_hs_bandwidth_adjust(struct usb_device *udev, int16_t len,
 1879     uint8_t slot, uint8_t mask)
 1880 {
 1881         struct usb_bus *bus = udev->bus;
 1882         struct usb_hub *hub;
 1883         enum usb_dev_speed speed;
 1884         uint8_t x;
 1885 
 1886         USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
 1887 
 1888         speed = usbd_get_speed(udev);
 1889 
 1890         switch (speed) {
 1891         case USB_SPEED_LOW:
 1892         case USB_SPEED_FULL:
 1893                 if (speed == USB_SPEED_LOW) {
 1894                         len *= 8;
 1895                 }
 1896                 /*
 1897                  * The Host Controller Driver should have
 1898                  * performed checks so that the lookup
 1899                  * below does not result in a NULL pointer
 1900                  * access.
 1901                  */
 1902 
 1903                 hub = udev->parent_hs_hub->hub;
 1904                 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
 1905                         slot = usb_intr_find_best_slot(hub->uframe_usage,
 1906                             USB_FS_ISOC_UFRAME_MAX, 6, mask);
 1907                 }
 1908                 for (x = slot; x < 8; x++) {
 1909                         if (mask & (1U << (x - slot))) {
 1910                                 hub->uframe_usage[x] += len;
 1911                                 bus->uframe_usage[x] += len;
 1912                         }
 1913                 }
 1914                 break;
 1915         default:
 1916                 if (slot >= USB_HS_MICRO_FRAMES_MAX) {
 1917                         slot = usb_intr_find_best_slot(bus->uframe_usage, 0,
 1918                             USB_HS_MICRO_FRAMES_MAX, mask);
 1919                 }
 1920                 for (x = slot; x < 8; x++) {
 1921                         if (mask & (1U << (x - slot))) {
 1922                                 bus->uframe_usage[x] += len;
 1923                         }
 1924                 }
 1925                 break;
 1926         }
 1927         return (slot);
 1928 }
 1929 
 1930 /*------------------------------------------------------------------------*
 1931  *      usb_hs_bandwidth_alloc
 1932  *
 1933  * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
 1934  *------------------------------------------------------------------------*/
 1935 void
 1936 usb_hs_bandwidth_alloc(struct usb_xfer *xfer)
 1937 {
 1938         struct usb_device *udev;
 1939         uint8_t slot;
 1940         uint8_t mask;
 1941         uint8_t speed;
 1942 
 1943         udev = xfer->xroot->udev;
 1944 
 1945         if (udev->flags.usb_mode != USB_MODE_HOST)
 1946                 return;         /* not supported */
 1947 
 1948         xfer->endpoint->refcount_bw++;
 1949         if (xfer->endpoint->refcount_bw != 1)
 1950                 return;         /* already allocated */
 1951 
 1952         speed = usbd_get_speed(udev);
 1953 
 1954         switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
 1955         case UE_INTERRUPT:
 1956                 /* allocate a microframe slot */
 1957 
 1958                 mask = 0x01;
 1959                 slot = usb_hs_bandwidth_adjust(udev,
 1960                     xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
 1961 
 1962                 xfer->endpoint->usb_uframe = slot;
 1963                 xfer->endpoint->usb_smask = mask << slot;
 1964 
 1965                 if ((speed != USB_SPEED_FULL) &&
 1966                     (speed != USB_SPEED_LOW)) {
 1967                         xfer->endpoint->usb_cmask = 0x00 ;
 1968                 } else {
 1969                         xfer->endpoint->usb_cmask = (-(0x04 << slot)) & 0xFE;
 1970                 }
 1971                 break;
 1972 
 1973         case UE_ISOCHRONOUS:
 1974                 switch (usbd_xfer_get_fps_shift(xfer)) {
 1975                 case 0:
 1976                         mask = 0xFF;
 1977                         break;
 1978                 case 1:
 1979                         mask = 0x55;
 1980                         break;
 1981                 case 2:
 1982                         mask = 0x11;
 1983                         break;
 1984                 default:
 1985                         mask = 0x01;
 1986                         break;
 1987                 }
 1988 
 1989                 /* allocate a microframe multi-slot */
 1990 
 1991                 slot = usb_hs_bandwidth_adjust(udev,
 1992                     xfer->max_frame_size, USB_HS_MICRO_FRAMES_MAX, mask);
 1993 
 1994                 xfer->endpoint->usb_uframe = slot;
 1995                 xfer->endpoint->usb_cmask = 0;
 1996                 xfer->endpoint->usb_smask = mask << slot;
 1997                 break;
 1998 
 1999         default:
 2000                 xfer->endpoint->usb_uframe = 0;
 2001                 xfer->endpoint->usb_cmask = 0;
 2002                 xfer->endpoint->usb_smask = 0;
 2003                 break;
 2004         }
 2005 
 2006         DPRINTFN(11, "slot=%d, mask=0x%02x\n", 
 2007             xfer->endpoint->usb_uframe, 
 2008             xfer->endpoint->usb_smask >> xfer->endpoint->usb_uframe);
 2009 }
 2010 
 2011 /*------------------------------------------------------------------------*
 2012  *      usb_hs_bandwidth_free
 2013  *
 2014  * This function is a wrapper function for "usb_hs_bandwidth_adjust()".
 2015  *------------------------------------------------------------------------*/
 2016 void
 2017 usb_hs_bandwidth_free(struct usb_xfer *xfer)
 2018 {
 2019         struct usb_device *udev;
 2020         uint8_t slot;
 2021         uint8_t mask;
 2022 
 2023         udev = xfer->xroot->udev;
 2024 
 2025         if (udev->flags.usb_mode != USB_MODE_HOST)
 2026                 return;         /* not supported */
 2027 
 2028         xfer->endpoint->refcount_bw--;
 2029         if (xfer->endpoint->refcount_bw != 0)
 2030                 return;         /* still allocated */
 2031 
 2032         switch (xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE) {
 2033         case UE_INTERRUPT:
 2034         case UE_ISOCHRONOUS:
 2035 
 2036                 slot = xfer->endpoint->usb_uframe;
 2037                 mask = xfer->endpoint->usb_smask;
 2038 
 2039                 /* free microframe slot(s): */    
 2040                 usb_hs_bandwidth_adjust(udev,
 2041                     -xfer->max_frame_size, slot, mask >> slot);
 2042 
 2043                 DPRINTFN(11, "slot=%d, mask=0x%02x\n", 
 2044                     slot, mask >> slot);
 2045 
 2046                 xfer->endpoint->usb_uframe = 0;
 2047                 xfer->endpoint->usb_cmask = 0;
 2048                 xfer->endpoint->usb_smask = 0;
 2049                 break;
 2050 
 2051         default:
 2052                 break;
 2053         }
 2054 }
 2055 
 2056 /*------------------------------------------------------------------------*
 2057  *      usb_isoc_time_expand
 2058  *
 2059  * This function will expand the time counter from 7-bit to 16-bit.
 2060  *
 2061  * Returns:
 2062  *   16-bit isochronous time counter.
 2063  *------------------------------------------------------------------------*/
 2064 uint16_t
 2065 usb_isoc_time_expand(struct usb_bus *bus, uint16_t isoc_time_curr)
 2066 {
 2067         uint16_t rem;
 2068 
 2069         USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
 2070 
 2071         rem = bus->isoc_time_last & (USB_ISOC_TIME_MAX - 1);
 2072 
 2073         isoc_time_curr &= (USB_ISOC_TIME_MAX - 1);
 2074 
 2075         if (isoc_time_curr < rem) {
 2076                 /* the time counter wrapped around */
 2077                 bus->isoc_time_last += USB_ISOC_TIME_MAX;
 2078         }
 2079         /* update the remainder */
 2080 
 2081         bus->isoc_time_last &= ~(USB_ISOC_TIME_MAX - 1);
 2082         bus->isoc_time_last |= isoc_time_curr;
 2083 
 2084         return (bus->isoc_time_last);
 2085 }
 2086 
 2087 /*------------------------------------------------------------------------*
 2088  *      usbd_fs_isoc_schedule_alloc_slot
 2089  *
 2090  * This function will allocate bandwidth for an isochronous FULL speed
 2091  * transaction in the FULL speed schedule.
 2092  *
 2093  * Returns:
 2094  *    <8: Success
 2095  * Else: Error
 2096  *------------------------------------------------------------------------*/
 2097 #if USB_HAVE_TT_SUPPORT
 2098 uint8_t
 2099 usbd_fs_isoc_schedule_alloc_slot(struct usb_xfer *isoc_xfer, uint16_t isoc_time)
 2100 {
 2101         struct usb_xfer *xfer;
 2102         struct usb_xfer *pipe_xfer;
 2103         struct usb_bus *bus;
 2104         usb_frlength_t len;
 2105         usb_frlength_t data_len;
 2106         uint16_t delta;
 2107         uint16_t slot;
 2108         uint8_t retval;
 2109 
 2110         data_len = 0;
 2111         slot = 0;
 2112 
 2113         bus = isoc_xfer->xroot->bus;
 2114 
 2115         TAILQ_FOREACH(xfer, &bus->intr_q.head, wait_entry) {
 2116 
 2117                 /* skip self, if any */
 2118 
 2119                 if (xfer == isoc_xfer)
 2120                         continue;
 2121 
 2122                 /* check if this USB transfer is going through the same TT */
 2123 
 2124                 if (xfer->xroot->udev->parent_hs_hub !=
 2125                     isoc_xfer->xroot->udev->parent_hs_hub) {
 2126                         continue;
 2127                 }
 2128                 if ((isoc_xfer->xroot->udev->parent_hs_hub->
 2129                     ddesc.bDeviceProtocol == UDPROTO_HSHUBMTT) &&
 2130                     (xfer->xroot->udev->hs_port_no !=
 2131                     isoc_xfer->xroot->udev->hs_port_no)) {
 2132                         continue;
 2133                 }
 2134                 if (xfer->endpoint->methods != isoc_xfer->endpoint->methods)
 2135                         continue;
 2136 
 2137                 /* check if isoc_time is part of this transfer */
 2138 
 2139                 delta = xfer->isoc_time_complete - isoc_time;
 2140                 if (delta > 0 && delta <= xfer->nframes) {
 2141                         delta = xfer->nframes - delta;
 2142 
 2143                         len = xfer->frlengths[delta];
 2144                         len += 8;
 2145                         len *= 7;
 2146                         len /= 6;
 2147 
 2148                         data_len += len;
 2149                 }
 2150 
 2151                 /*
 2152                  * Check double buffered transfers. Only stream ID
 2153                  * equal to zero is valid here!
 2154                  */
 2155                 TAILQ_FOREACH(pipe_xfer, &xfer->endpoint->endpoint_q[0].head,
 2156                     wait_entry) {
 2157 
 2158                         /* skip self, if any */
 2159 
 2160                         if (pipe_xfer == isoc_xfer)
 2161                                 continue;
 2162 
 2163                         /* check if isoc_time is part of this transfer */
 2164 
 2165                         delta = pipe_xfer->isoc_time_complete - isoc_time;
 2166                         if (delta > 0 && delta <= pipe_xfer->nframes) {
 2167                                 delta = pipe_xfer->nframes - delta;
 2168 
 2169                                 len = pipe_xfer->frlengths[delta];
 2170                                 len += 8;
 2171                                 len *= 7;
 2172                                 len /= 6;
 2173 
 2174                                 data_len += len;
 2175                         }
 2176                 }
 2177         }
 2178 
 2179         while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
 2180                 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
 2181                 slot++;
 2182         }
 2183 
 2184         /* check for overflow */
 2185 
 2186         if (slot >= USB_FS_ISOC_UFRAME_MAX)
 2187                 return (255);
 2188 
 2189         retval = slot;
 2190 
 2191         delta = isoc_xfer->isoc_time_complete - isoc_time;
 2192         if (delta > 0 && delta <= isoc_xfer->nframes) {
 2193                 delta = isoc_xfer->nframes - delta;
 2194 
 2195                 len = isoc_xfer->frlengths[delta];
 2196                 len += 8;
 2197                 len *= 7;
 2198                 len /= 6;
 2199 
 2200                 data_len += len;
 2201         }
 2202 
 2203         while (data_len >= USB_FS_BYTES_PER_HS_UFRAME) {
 2204                 data_len -= USB_FS_BYTES_PER_HS_UFRAME;
 2205                 slot++;
 2206         }
 2207 
 2208         /* check for overflow */
 2209 
 2210         if (slot >= USB_FS_ISOC_UFRAME_MAX)
 2211                 return (255);
 2212 
 2213         return (retval);
 2214 }
 2215 #endif
 2216 
 2217 /*------------------------------------------------------------------------*
 2218  *      usb_bus_port_get_device
 2219  *
 2220  * This function is NULL safe.
 2221  *------------------------------------------------------------------------*/
 2222 struct usb_device *
 2223 usb_bus_port_get_device(struct usb_bus *bus, struct usb_port *up)
 2224 {
 2225         if ((bus == NULL) || (up == NULL)) {
 2226                 /* be NULL safe */
 2227                 return (NULL);
 2228         }
 2229         if (up->device_index == 0) {
 2230                 /* nothing to do */
 2231                 return (NULL);
 2232         }
 2233         return (bus->devices[up->device_index]);
 2234 }
 2235 
 2236 /*------------------------------------------------------------------------*
 2237  *      usb_bus_port_set_device
 2238  *
 2239  * This function is NULL safe.
 2240  *------------------------------------------------------------------------*/
 2241 void
 2242 usb_bus_port_set_device(struct usb_bus *bus, struct usb_port *up,
 2243     struct usb_device *udev, uint8_t device_index)
 2244 {
 2245         if (bus == NULL) {
 2246                 /* be NULL safe */
 2247                 return;
 2248         }
 2249         /*
 2250          * There is only one case where we don't
 2251          * have an USB port, and that is the Root Hub!
 2252          */
 2253         if (up) {
 2254                 if (udev) {
 2255                         up->device_index = device_index;
 2256                 } else {
 2257                         device_index = up->device_index;
 2258                         up->device_index = 0;
 2259                 }
 2260         }
 2261         /*
 2262          * Make relationships to our new device
 2263          */
 2264         if (device_index != 0) {
 2265 #if USB_HAVE_UGEN
 2266                 mtx_lock(&usb_ref_lock);
 2267 #endif
 2268                 bus->devices[device_index] = udev;
 2269 #if USB_HAVE_UGEN
 2270                 mtx_unlock(&usb_ref_lock);
 2271 #endif
 2272         }
 2273         /*
 2274          * Debug print
 2275          */
 2276         DPRINTFN(2, "bus %p devices[%u] = %p\n", bus, device_index, udev);
 2277 }
 2278 
 2279 /*------------------------------------------------------------------------*
 2280  *      usb_needs_explore
 2281  *
 2282  * This functions is called when the USB event thread needs to run.
 2283  *------------------------------------------------------------------------*/
 2284 void
 2285 usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
 2286 {
 2287         uint8_t do_unlock;
 2288 
 2289         DPRINTF("\n");
 2290 
 2291         if (cold != 0) {
 2292                 DPRINTF("Cold\n");
 2293                 return;
 2294         }
 2295 
 2296         if (bus == NULL) {
 2297                 DPRINTF("No bus pointer!\n");
 2298                 return;
 2299         }
 2300         if ((bus->devices == NULL) ||
 2301             (bus->devices[USB_ROOT_HUB_ADDR] == NULL)) {
 2302                 DPRINTF("No root HUB\n");
 2303                 return;
 2304         }
 2305         if (mtx_owned(&bus->bus_mtx)) {
 2306                 do_unlock = 0;
 2307         } else {
 2308                 USB_BUS_LOCK(bus);
 2309                 do_unlock = 1;
 2310         }
 2311         if (do_probe) {
 2312                 bus->do_probe = 1;
 2313         }
 2314         if (usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
 2315             &bus->explore_msg[0], &bus->explore_msg[1])) {
 2316                 /* ignore */
 2317         }
 2318         if (do_unlock) {
 2319                 USB_BUS_UNLOCK(bus);
 2320         }
 2321 }
 2322 
 2323 /*------------------------------------------------------------------------*
 2324  *      usb_needs_explore_all
 2325  *
 2326  * This function is called whenever a new driver is loaded and will
 2327  * cause that all USB busses are re-explored.
 2328  *------------------------------------------------------------------------*/
 2329 void
 2330 usb_needs_explore_all(void)
 2331 {
 2332         struct usb_bus *bus;
 2333         devclass_t dc;
 2334         device_t dev;
 2335         int max;
 2336 
 2337         DPRINTFN(3, "\n");
 2338 
 2339         dc = usb_devclass_ptr;
 2340         if (dc == NULL) {
 2341                 DPRINTFN(0, "no devclass\n");
 2342                 return;
 2343         }
 2344         /*
 2345          * Explore all USB busses in parallell.
 2346          */
 2347         max = devclass_get_maxunit(dc);
 2348         while (max >= 0) {
 2349                 dev = devclass_get_device(dc, max);
 2350                 if (dev) {
 2351                         bus = device_get_softc(dev);
 2352                         if (bus) {
 2353                                 usb_needs_explore(bus, 1);
 2354                         }
 2355                 }
 2356                 max--;
 2357         }
 2358 }
 2359 
 2360 /*------------------------------------------------------------------------*
 2361  *      usb_needs_explore_init
 2362  *
 2363  * This function will ensure that the USB controllers are not enumerated
 2364  * until the "cold" variable is cleared.
 2365  *------------------------------------------------------------------------*/
 2366 static void
 2367 usb_needs_explore_init(void *arg)
 2368 {
 2369         /*
 2370          * The cold variable should be cleared prior to this function
 2371          * being called:
 2372          */
 2373         if (cold == 0)
 2374                 usb_needs_explore_all();
 2375         else
 2376                 DPRINTFN(-1, "Cold variable is still set!\n");
 2377 }
 2378 SYSINIT(usb_needs_explore_init, SI_SUB_KICK_SCHEDULER, SI_ORDER_SECOND, usb_needs_explore_init, NULL);
 2379 
 2380 /*------------------------------------------------------------------------*
 2381  *      usb_bus_power_update
 2382  *
 2383  * This function will ensure that all USB devices on the given bus are
 2384  * properly suspended or resumed according to the device transfer
 2385  * state.
 2386  *------------------------------------------------------------------------*/
 2387 #if USB_HAVE_POWERD
 2388 void
 2389 usb_bus_power_update(struct usb_bus *bus)
 2390 {
 2391         usb_needs_explore(bus, 0 /* no probe */ );
 2392 }
 2393 #endif
 2394 
 2395 /*------------------------------------------------------------------------*
 2396  *      usbd_transfer_power_ref
 2397  *
 2398  * This function will modify the power save reference counts and
 2399  * wakeup the USB device associated with the given USB transfer, if
 2400  * needed.
 2401  *------------------------------------------------------------------------*/
 2402 #if USB_HAVE_POWERD
 2403 void
 2404 usbd_transfer_power_ref(struct usb_xfer *xfer, int val)
 2405 {
 2406         static const usb_power_mask_t power_mask[4] = {
 2407                 [UE_CONTROL] = USB_HW_POWER_CONTROL,
 2408                 [UE_BULK] = USB_HW_POWER_BULK,
 2409                 [UE_INTERRUPT] = USB_HW_POWER_INTERRUPT,
 2410                 [UE_ISOCHRONOUS] = USB_HW_POWER_ISOC,
 2411         };
 2412         struct usb_device *udev;
 2413         uint8_t needs_explore;
 2414         uint8_t needs_hw_power;
 2415         uint8_t xfer_type;
 2416 
 2417         udev = xfer->xroot->udev;
 2418 
 2419         if (udev->device_index == USB_ROOT_HUB_ADDR) {
 2420                 /* no power save for root HUB */
 2421                 return;
 2422         }
 2423         USB_BUS_LOCK(udev->bus);
 2424 
 2425         xfer_type = xfer->endpoint->edesc->bmAttributes & UE_XFERTYPE;
 2426 
 2427         udev->pwr_save.last_xfer_time = ticks;
 2428         udev->pwr_save.type_refs[xfer_type] += val;
 2429 
 2430         if (xfer->flags_int.control_xfr) {
 2431                 udev->pwr_save.read_refs += val;
 2432                 if (xfer->flags_int.usb_mode == USB_MODE_HOST) {
 2433                         /*
 2434                          * It is not allowed to suspend during a
 2435                          * control transfer:
 2436                          */
 2437                         udev->pwr_save.write_refs += val;
 2438                 }
 2439         } else if (USB_GET_DATA_ISREAD(xfer)) {
 2440                 udev->pwr_save.read_refs += val;
 2441         } else {
 2442                 udev->pwr_save.write_refs += val;
 2443         }
 2444 
 2445         if (val > 0) {
 2446                 if (udev->flags.self_suspended)
 2447                         needs_explore = usb_peer_should_wakeup(udev);
 2448                 else
 2449                         needs_explore = 0;
 2450 
 2451                 if (!(udev->bus->hw_power_state & power_mask[xfer_type])) {
 2452                         DPRINTF("Adding type %u to power state\n", xfer_type);
 2453                         udev->bus->hw_power_state |= power_mask[xfer_type];
 2454                         needs_hw_power = 1;
 2455                 } else {
 2456                         needs_hw_power = 0;
 2457                 }
 2458         } else {
 2459                 needs_explore = 0;
 2460                 needs_hw_power = 0;
 2461         }
 2462 
 2463         USB_BUS_UNLOCK(udev->bus);
 2464 
 2465         if (needs_explore) {
 2466                 DPRINTF("update\n");
 2467                 usb_bus_power_update(udev->bus);
 2468         } else if (needs_hw_power) {
 2469                 DPRINTF("needs power\n");
 2470                 if (udev->bus->methods->set_hw_power != NULL) {
 2471                         (udev->bus->methods->set_hw_power) (udev->bus);
 2472                 }
 2473         }
 2474 }
 2475 #endif
 2476 
 2477 /*------------------------------------------------------------------------*
 2478  *      usb_peer_should_wakeup
 2479  *
 2480  * This function returns non-zero if the current device should wake up.
 2481  *------------------------------------------------------------------------*/
 2482 static uint8_t
 2483 usb_peer_should_wakeup(struct usb_device *udev)
 2484 {
 2485         return (((udev->power_mode == USB_POWER_MODE_ON) &&
 2486             (udev->flags.usb_mode == USB_MODE_HOST)) ||
 2487             (udev->driver_added_refcount != udev->bus->driver_added_refcount) ||
 2488             (udev->re_enumerate_wait != USB_RE_ENUM_DONE) ||
 2489             (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0) ||
 2490             (udev->pwr_save.write_refs != 0) ||
 2491             ((udev->pwr_save.read_refs != 0) &&
 2492             (udev->flags.usb_mode == USB_MODE_HOST) &&
 2493             (usb_peer_can_wakeup(udev) == 0)));
 2494 }
 2495 
 2496 /*------------------------------------------------------------------------*
 2497  *      usb_bus_powerd
 2498  *
 2499  * This function implements the USB power daemon and is called
 2500  * regularly from the USB explore thread.
 2501  *------------------------------------------------------------------------*/
 2502 #if USB_HAVE_POWERD
 2503 void
 2504 usb_bus_powerd(struct usb_bus *bus)
 2505 {
 2506         struct usb_device *udev;
 2507         usb_ticks_t temp;
 2508         usb_ticks_t limit;
 2509         usb_ticks_t mintime;
 2510         usb_size_t type_refs[5];
 2511         uint8_t x;
 2512 
 2513         limit = usb_power_timeout;
 2514         if (limit == 0)
 2515                 limit = hz;
 2516         else if (limit > 255)
 2517                 limit = 255 * hz;
 2518         else
 2519                 limit = limit * hz;
 2520 
 2521         DPRINTF("bus=%p\n", bus);
 2522 
 2523         USB_BUS_LOCK(bus);
 2524 
 2525         /*
 2526          * The root HUB device is never suspended
 2527          * and we simply skip it.
 2528          */
 2529         for (x = USB_ROOT_HUB_ADDR + 1;
 2530             x != bus->devices_max; x++) {
 2531 
 2532                 udev = bus->devices[x];
 2533                 if (udev == NULL)
 2534                         continue;
 2535 
 2536                 temp = ticks - udev->pwr_save.last_xfer_time;
 2537 
 2538                 if (usb_peer_should_wakeup(udev)) {
 2539                         /* check if we are suspended */
 2540                         if (udev->flags.self_suspended != 0) {
 2541                                 USB_BUS_UNLOCK(bus);
 2542                                 usb_dev_resume_peer(udev);
 2543                                 USB_BUS_LOCK(bus);
 2544                         }
 2545                 } else if ((temp >= limit) &&
 2546                     (udev->flags.usb_mode == USB_MODE_HOST) &&
 2547                     (udev->flags.self_suspended == 0)) {
 2548                         /* try to do suspend */
 2549 
 2550                         USB_BUS_UNLOCK(bus);
 2551                         usb_dev_suspend_peer(udev);
 2552                         USB_BUS_LOCK(bus);
 2553                 }
 2554         }
 2555 
 2556         /* reset counters */
 2557 
 2558         mintime = (usb_ticks_t)-1;
 2559         type_refs[0] = 0;
 2560         type_refs[1] = 0;
 2561         type_refs[2] = 0;
 2562         type_refs[3] = 0;
 2563         type_refs[4] = 0;
 2564 
 2565         /* Re-loop all the devices to get the actual state */
 2566 
 2567         for (x = USB_ROOT_HUB_ADDR + 1;
 2568             x != bus->devices_max; x++) {
 2569 
 2570                 udev = bus->devices[x];
 2571                 if (udev == NULL)
 2572                         continue;
 2573 
 2574                 /* we found a non-Root-Hub USB device */
 2575                 type_refs[4] += 1;
 2576 
 2577                 /* "last_xfer_time" can be updated by a resume */
 2578                 temp = ticks - udev->pwr_save.last_xfer_time;
 2579 
 2580                 /*
 2581                  * Compute minimum time since last transfer for the complete
 2582                  * bus:
 2583                  */
 2584                 if (temp < mintime)
 2585                         mintime = temp;
 2586 
 2587                 if (udev->flags.self_suspended == 0) {
 2588                         type_refs[0] += udev->pwr_save.type_refs[0];
 2589                         type_refs[1] += udev->pwr_save.type_refs[1];
 2590                         type_refs[2] += udev->pwr_save.type_refs[2];
 2591                         type_refs[3] += udev->pwr_save.type_refs[3];
 2592                 }
 2593         }
 2594 
 2595         if (mintime >= (usb_ticks_t)(1 * hz)) {
 2596                 /* recompute power masks */
 2597                 DPRINTF("Recomputing power masks\n");
 2598                 bus->hw_power_state = 0;
 2599                 if (type_refs[UE_CONTROL] != 0)
 2600                         bus->hw_power_state |= USB_HW_POWER_CONTROL;
 2601                 if (type_refs[UE_BULK] != 0)
 2602                         bus->hw_power_state |= USB_HW_POWER_BULK;
 2603                 if (type_refs[UE_INTERRUPT] != 0)
 2604                         bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
 2605                 if (type_refs[UE_ISOCHRONOUS] != 0)
 2606                         bus->hw_power_state |= USB_HW_POWER_ISOC;
 2607                 if (type_refs[4] != 0)
 2608                         bus->hw_power_state |= USB_HW_POWER_NON_ROOT_HUB;
 2609         }
 2610         USB_BUS_UNLOCK(bus);
 2611 
 2612         if (bus->methods->set_hw_power != NULL) {
 2613                 /* always update hardware power! */
 2614                 (bus->methods->set_hw_power) (bus);
 2615         }
 2616         return;
 2617 }
 2618 #endif
 2619 
 2620 static usb_error_t
 2621 usbd_device_30_remote_wakeup(struct usb_device *udev, uint8_t bRequest)
 2622 {
 2623         struct usb_device_request req = {};
 2624 
 2625         req.bmRequestType = UT_WRITE_INTERFACE;
 2626         req.bRequest = bRequest;
 2627         USETW(req.wValue, USB_INTERFACE_FUNC_SUSPEND);
 2628         USETW(req.wIndex, USB_INTERFACE_FUNC_SUSPEND_LP |
 2629             USB_INTERFACE_FUNC_SUSPEND_RW);
 2630 
 2631         return (usbd_do_request(udev, NULL, &req, 0));
 2632 }
 2633 
 2634 static usb_error_t
 2635 usbd_clear_dev_wakeup(struct usb_device *udev)
 2636 {
 2637         usb_error_t err;
 2638 
 2639         if (usb_device_20_compatible(udev)) {
 2640                 err = usbd_req_clear_device_feature(udev,
 2641                     NULL, UF_DEVICE_REMOTE_WAKEUP);
 2642         } else {
 2643                 err = usbd_device_30_remote_wakeup(udev,
 2644                     UR_CLEAR_FEATURE);
 2645         }
 2646         return (err);
 2647 }
 2648 
 2649 static usb_error_t
 2650 usbd_set_dev_wakeup(struct usb_device *udev)
 2651 {
 2652         usb_error_t err;
 2653 
 2654         if (usb_device_20_compatible(udev)) {
 2655                 err = usbd_req_set_device_feature(udev,
 2656                     NULL, UF_DEVICE_REMOTE_WAKEUP);
 2657         } else {
 2658                 err = usbd_device_30_remote_wakeup(udev,
 2659                     UR_SET_FEATURE);
 2660         }
 2661         return (err);
 2662 }
 2663 
 2664 /*------------------------------------------------------------------------*
 2665  *      usb_dev_resume_peer
 2666  *
 2667  * This function will resume an USB peer and do the required USB
 2668  * signalling to get an USB device out of the suspended state.
 2669  *------------------------------------------------------------------------*/
 2670 static void
 2671 usb_dev_resume_peer(struct usb_device *udev)
 2672 {
 2673         struct usb_bus *bus;
 2674         int err;
 2675 
 2676         /* be NULL safe */
 2677         if (udev == NULL)
 2678                 return;
 2679 
 2680         /* check if already resumed */
 2681         if (udev->flags.self_suspended == 0)
 2682                 return;
 2683 
 2684         /* we need a parent HUB to do resume */
 2685         if (udev->parent_hub == NULL)
 2686                 return;
 2687 
 2688         DPRINTF("udev=%p\n", udev);
 2689 
 2690         if ((udev->flags.usb_mode == USB_MODE_DEVICE) &&
 2691             (udev->flags.remote_wakeup == 0)) {
 2692                 /*
 2693                  * If the host did not set the remote wakeup feature, we can
 2694                  * not wake it up either!
 2695                  */
 2696                 DPRINTF("remote wakeup is not set!\n");
 2697                 return;
 2698         }
 2699         /* get bus pointer */
 2700         bus = udev->bus;
 2701 
 2702         /* resume parent hub first */
 2703         usb_dev_resume_peer(udev->parent_hub);
 2704 
 2705         /* reduce chance of instant resume failure by waiting a little bit */
 2706         usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
 2707 
 2708         if (usb_device_20_compatible(udev)) {
 2709                 /* resume current port (Valid in Host and Device Mode) */
 2710                 err = usbd_req_clear_port_feature(udev->parent_hub,
 2711                     NULL, udev->port_no, UHF_PORT_SUSPEND);
 2712                 if (err) {
 2713                         DPRINTFN(0, "Resuming port failed\n");
 2714                         return;
 2715                 }
 2716         } else {
 2717                 /* resume current port (Valid in Host and Device Mode) */
 2718                 err = usbd_req_set_port_link_state(udev->parent_hub,
 2719                     NULL, udev->port_no, UPS_PORT_LS_U0);
 2720                 if (err) {
 2721                         DPRINTFN(0, "Resuming port failed\n");
 2722                         return;
 2723                 }
 2724         }
 2725 
 2726         /* resume settle time */
 2727         usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
 2728 
 2729         if (bus->methods->device_resume != NULL) {
 2730                 /* resume USB device on the USB controller */
 2731                 (bus->methods->device_resume) (udev);
 2732         }
 2733         USB_BUS_LOCK(bus);
 2734         /* set that this device is now resumed */
 2735         udev->flags.self_suspended = 0;
 2736 #if USB_HAVE_POWERD
 2737         /* make sure that we don't go into suspend right away */
 2738         udev->pwr_save.last_xfer_time = ticks;
 2739 
 2740         /* make sure the needed power masks are on */
 2741         if (udev->pwr_save.type_refs[UE_CONTROL] != 0)
 2742                 bus->hw_power_state |= USB_HW_POWER_CONTROL;
 2743         if (udev->pwr_save.type_refs[UE_BULK] != 0)
 2744                 bus->hw_power_state |= USB_HW_POWER_BULK;
 2745         if (udev->pwr_save.type_refs[UE_INTERRUPT] != 0)
 2746                 bus->hw_power_state |= USB_HW_POWER_INTERRUPT;
 2747         if (udev->pwr_save.type_refs[UE_ISOCHRONOUS] != 0)
 2748                 bus->hw_power_state |= USB_HW_POWER_ISOC;
 2749 #endif
 2750         USB_BUS_UNLOCK(bus);
 2751 
 2752         if (bus->methods->set_hw_power != NULL) {
 2753                 /* always update hardware power! */
 2754                 (bus->methods->set_hw_power) (bus);
 2755         }
 2756 
 2757         usbd_sr_lock(udev);
 2758 
 2759         /* notify all sub-devices about resume */
 2760         err = usb_suspend_resume(udev, 0);
 2761 
 2762         usbd_sr_unlock(udev);
 2763 
 2764         /* check if peer has wakeup capability */
 2765         if (usb_peer_can_wakeup(udev)) {
 2766                 /* clear remote wakeup */
 2767                 err = usbd_clear_dev_wakeup(udev);
 2768                 if (err) {
 2769                         DPRINTFN(0, "Clearing device "
 2770                             "remote wakeup failed: %s\n",
 2771                             usbd_errstr(err));
 2772                 }
 2773         }
 2774 }
 2775 
 2776 /*------------------------------------------------------------------------*
 2777  *      usb_dev_suspend_peer
 2778  *
 2779  * This function will suspend an USB peer and do the required USB
 2780  * signalling to get an USB device into the suspended state.
 2781  *------------------------------------------------------------------------*/
 2782 static void
 2783 usb_dev_suspend_peer(struct usb_device *udev)
 2784 {
 2785         struct usb_device *child;
 2786         int err;
 2787         uint8_t x;
 2788         uint8_t nports;
 2789 
 2790 repeat:
 2791         /* be NULL safe */
 2792         if (udev == NULL)
 2793                 return;
 2794 
 2795         /* check if already suspended */
 2796         if (udev->flags.self_suspended)
 2797                 return;
 2798 
 2799         /* we need a parent HUB to do suspend */
 2800         if (udev->parent_hub == NULL)
 2801                 return;
 2802 
 2803         DPRINTF("udev=%p\n", udev);
 2804 
 2805         /* check if the current device is a HUB */
 2806         if (udev->hub != NULL) {
 2807                 nports = udev->hub->nports;
 2808 
 2809                 /* check if all devices on the HUB are suspended */
 2810                 for (x = 0; x != nports; x++) {
 2811                         child = usb_bus_port_get_device(udev->bus,
 2812                             udev->hub->ports + x);
 2813 
 2814                         if (child == NULL)
 2815                                 continue;
 2816 
 2817                         if (child->flags.self_suspended)
 2818                                 continue;
 2819 
 2820                         DPRINTFN(1, "Port %u is busy on the HUB!\n", x + 1);
 2821                         return;
 2822                 }
 2823         }
 2824 
 2825         if (usb_peer_can_wakeup(udev)) {
 2826                 /*
 2827                  * This request needs to be done before we set
 2828                  * "udev->flags.self_suspended":
 2829                  */
 2830 
 2831                 /* allow device to do remote wakeup */
 2832                 err = usbd_set_dev_wakeup(udev);
 2833                 if (err) {
 2834                         DPRINTFN(0, "Setting device "
 2835                             "remote wakeup failed\n");
 2836                 }
 2837         }
 2838 
 2839         USB_BUS_LOCK(udev->bus);
 2840         /*
 2841          * Checking for suspend condition and setting suspended bit
 2842          * must be atomic!
 2843          */
 2844         err = usb_peer_should_wakeup(udev);
 2845         if (err == 0) {
 2846                 /*
 2847                  * Set that this device is suspended. This variable
 2848                  * must be set before calling USB controller suspend
 2849                  * callbacks.
 2850                  */
 2851                 udev->flags.self_suspended = 1;
 2852         }
 2853         USB_BUS_UNLOCK(udev->bus);
 2854 
 2855         if (err != 0) {
 2856                 if (usb_peer_can_wakeup(udev)) {
 2857                         /* allow device to do remote wakeup */
 2858                         err = usbd_clear_dev_wakeup(udev);
 2859                         if (err) {
 2860                                 DPRINTFN(0, "Setting device "
 2861                                     "remote wakeup failed\n");
 2862                         }
 2863                 }
 2864 
 2865                 if (udev->flags.usb_mode == USB_MODE_DEVICE) {
 2866                         /* resume parent HUB first */
 2867                         usb_dev_resume_peer(udev->parent_hub);
 2868 
 2869                         /* reduce chance of instant resume failure by waiting a little bit */
 2870                         usb_pause_mtx(NULL, USB_MS_TO_TICKS(20));
 2871 
 2872                         /* resume current port (Valid in Host and Device Mode) */
 2873                         err = usbd_req_clear_port_feature(udev->parent_hub,
 2874                             NULL, udev->port_no, UHF_PORT_SUSPEND);
 2875 
 2876                         /* resume settle time */
 2877                         usb_pause_mtx(NULL, USB_MS_TO_TICKS(usb_port_resume_delay));
 2878                 }
 2879                 DPRINTF("Suspend was cancelled!\n");
 2880                 return;
 2881         }
 2882 
 2883         usbd_sr_lock(udev);
 2884 
 2885         /* notify all sub-devices about suspend */
 2886         err = usb_suspend_resume(udev, 1);
 2887 
 2888         usbd_sr_unlock(udev);
 2889 
 2890         if (udev->bus->methods->device_suspend != NULL) {
 2891                 usb_timeout_t temp;
 2892 
 2893                 /* suspend device on the USB controller */
 2894                 (udev->bus->methods->device_suspend) (udev);
 2895 
 2896                 /* do DMA delay */
 2897                 temp = usbd_get_dma_delay(udev);
 2898                 if (temp != 0)
 2899                         usb_pause_mtx(NULL, USB_MS_TO_TICKS(temp));
 2900 
 2901         }
 2902 
 2903         if (usb_device_20_compatible(udev)) {
 2904                 /* suspend current port */
 2905                 err = usbd_req_set_port_feature(udev->parent_hub,
 2906                     NULL, udev->port_no, UHF_PORT_SUSPEND);
 2907                 if (err) {
 2908                         DPRINTFN(0, "Suspending port failed\n");
 2909                         return;
 2910                 }
 2911         } else {
 2912                 /* suspend current port */
 2913                 err = usbd_req_set_port_link_state(udev->parent_hub,
 2914                     NULL, udev->port_no, UPS_PORT_LS_U3);
 2915                 if (err) {
 2916                         DPRINTFN(0, "Suspending port failed\n");
 2917                         return;
 2918                 }
 2919         }
 2920 
 2921         udev = udev->parent_hub;
 2922         goto repeat;
 2923 }
 2924 
 2925 /*------------------------------------------------------------------------*
 2926  *      usbd_set_power_mode
 2927  *
 2928  * This function will set the power mode, see USB_POWER_MODE_XXX for a
 2929  * USB device.
 2930  *------------------------------------------------------------------------*/
 2931 void
 2932 usbd_set_power_mode(struct usb_device *udev, uint8_t power_mode)
 2933 {
 2934         /* filter input argument */
 2935         if ((power_mode != USB_POWER_MODE_ON) &&
 2936             (power_mode != USB_POWER_MODE_OFF))
 2937                 power_mode = USB_POWER_MODE_SAVE;
 2938 
 2939         power_mode = usbd_filter_power_mode(udev, power_mode);  
 2940 
 2941         udev->power_mode = power_mode;  /* update copy of power mode */
 2942 
 2943 #if USB_HAVE_POWERD
 2944         usb_bus_power_update(udev->bus);
 2945 #else
 2946         usb_needs_explore(udev->bus, 0 /* no probe */ );
 2947 #endif
 2948 }
 2949 
 2950 /*------------------------------------------------------------------------*
 2951  *      usbd_filter_power_mode
 2952  *
 2953  * This function filters the power mode based on hardware requirements.
 2954  *------------------------------------------------------------------------*/
 2955 uint8_t
 2956 usbd_filter_power_mode(struct usb_device *udev, uint8_t power_mode)
 2957 {
 2958         struct usb_bus_methods *mtod;
 2959         int8_t temp;
 2960 
 2961         mtod = udev->bus->methods;
 2962         temp = -1;
 2963 
 2964         if (mtod->get_power_mode != NULL)
 2965                 (mtod->get_power_mode) (udev, &temp);
 2966 
 2967         /* check if we should not filter */
 2968         if (temp < 0)
 2969                 return (power_mode);
 2970 
 2971         /* use fixed power mode given by hardware driver */
 2972         return (temp);
 2973 }
 2974 
 2975 /*------------------------------------------------------------------------*
 2976  *      usbd_start_re_enumerate
 2977  *
 2978  * This function starts re-enumeration of the given USB device. This
 2979  * function does not need to be called BUS-locked. This function does
 2980  * not wait until the re-enumeration is completed.
 2981  *------------------------------------------------------------------------*/
 2982 void
 2983 usbd_start_re_enumerate(struct usb_device *udev)
 2984 {
 2985         if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
 2986                 udev->re_enumerate_wait = USB_RE_ENUM_START;
 2987                 usb_needs_explore(udev->bus, 0);
 2988         }
 2989 }
 2990 
 2991 /*-----------------------------------------------------------------------*
 2992  *      usbd_start_set_config
 2993  *
 2994  * This function starts setting a USB configuration. This function
 2995  * does not need to be called BUS-locked. This function does not wait
 2996  * until the set USB configuratino is completed.
 2997  *------------------------------------------------------------------------*/
 2998 usb_error_t
 2999 usbd_start_set_config(struct usb_device *udev, uint8_t index)
 3000 {
 3001         if (udev->re_enumerate_wait == USB_RE_ENUM_DONE) {
 3002                 if (udev->curr_config_index == index) {
 3003                         /* no change needed */
 3004                         return (0);
 3005                 }
 3006                 udev->next_config_index = index;
 3007                 udev->re_enumerate_wait = USB_RE_ENUM_SET_CONFIG;
 3008                 usb_needs_explore(udev->bus, 0);
 3009                 return (0);
 3010         } else if (udev->re_enumerate_wait == USB_RE_ENUM_SET_CONFIG) {
 3011                 if (udev->next_config_index == index) {
 3012                         /* no change needed */
 3013                         return (0);
 3014                 }
 3015         }
 3016         return (USB_ERR_PENDING_REQUESTS);
 3017 }

Cache object: 169a2dc77ce929e3ee57069f4d51a0bb


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.