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