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