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