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