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