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