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