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