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