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