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