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