source: rtems-libbsd/freebsd/sys/dev/usb/usb_hub.c @ 8e65e1b

55-freebsd-126-freebsd-12freebsd-9.3
Last change on this file since 8e65e1b was 8e65e1b, checked in by Sebastian Huber <sebastian.huber@…>, on 08/23/16 at 13:51:45

usb: Update to FreeBSD trunk 2016-08-23

FreeBSD trunk, 2016-08-23, 9fe7c416e6abb28b1398fd3e5687099846800cfd.

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