• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* $FreeBSD$ */
2 /*-
3  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4  *
5  * Copyright (c) 2008 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 #include "implementation/global_implementation.h"
30 #if USB_HAVE_DEVICE_TOPOLOGY
31 #include "implementation/usb_btree.h"
32 #endif
33 
34 /* function prototypes  */
35 
36 static device_probe_t usb_probe;
37 static device_attach_t usb_attach;
38 static device_detach_t usb_detach;
39 static device_suspend_t usb_suspend;
40 static device_resume_t usb_resume;
41 static device_shutdown_t usb_shutdown;
42 
43 static void	usb_attach_sub(device_t, struct usb_bus *);
44 
45 #undef	USB_DEBUG_VAR
46 #define	USB_DEBUG_VAR   usb_ctrl_debug
47 #ifdef LOSCFG_USB_DEBUG
48 static int usb_ctrl_debug = 0;
49 void
usb_controller_debug_func(int level)50 usb_controller_debug_func(int level)
51 {
52 	usb_ctrl_debug = level;
53 	PRINTK("The level of usb controller debug is %d\n", level);
54 }
55 DEBUG_MODULE(controller, usb_controller_debug_func);
56 #endif
57 
58 static int usb_no_suspend_wait = 0;
59 static int usb_no_resume_wait = 0;
60 static int usb_no_shutdown_wait = 0;
61 static devclass_t usb_devclass;
62 
63 static device_method_t usb_methods[] = {
64 	DEVMETHOD(device_probe, usb_probe),
65 	DEVMETHOD(device_attach, usb_attach),
66 	DEVMETHOD(device_detach, usb_detach),
67 	DEVMETHOD(device_suspend, usb_suspend),
68 	DEVMETHOD(device_resume, usb_resume),
69 	DEVMETHOD(device_shutdown, usb_shutdown),
70 
71 	DEVMETHOD_END
72 };
73 
74 static driver_t usb_driver = {
75 	.name = "usbus",
76 	.methods = usb_methods,
77 	.size = 0,
78 };
79 
80 /* Host Only Drivers */
81 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
82 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
83 
84 /*------------------------------------------------------------------------*
85  *	usb_probe
86  *
87  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
88  *------------------------------------------------------------------------*/
89 static int
usb_probe(device_t dev)90 usb_probe(device_t dev)
91 {
92 	DPRINTF("\n");
93 	return (0);
94 }
95 
96 #if USB_HAVE_ROOT_MOUNT_HOLD
97 static void
usb_root_mount_rel(struct usb_bus * bus)98 usb_root_mount_rel(struct usb_bus *bus)
99 {
100 	if (bus->bus_roothold != NULL) {
101 		DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
102 		root_mount_rel(bus->bus_roothold);
103 		bus->bus_roothold = NULL;
104 	}
105 }
106 #endif
107 
108 #if USB_HAVE_DEVICE_TOPOLOGY
109 usbd_bt_tree hub_tree;
110 #endif
111 
112 /*------------------------------------------------------------------------*
113  *	usb_attach
114  *------------------------------------------------------------------------*/
115 static int
usb_attach(device_t dev)116 usb_attach(device_t dev)
117 {
118 	struct usb_bus *bus = (struct usb_bus *)device_get_ivars(dev);
119 #if USB_HAVE_DEVICE_TOPOLOGY
120 	struct node_info info;
121 #endif
122 	DPRINTF("\n");
123 
124 	if (bus == NULL) {
125 		device_printf(dev, "USB device has no ivars\n");
126 		return (ENXIO);
127 	}
128 
129 #if USB_HAVE_ROOT_MOUNT_HOLD
130 	if (usb_no_boot_wait == 0) {
131 		/* delay vfs_mountroot until the bus is explored */
132 		bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
133 	}
134 #endif
135 
136 #if USB_HAVE_DEVICE_TOPOLOGY
137 	info.port_no = 0;
138 	info.nameunit = device_get_nameunit(dev);
139 	hub_tree = usbd_create_bt_node(&info);
140 	if (hub_tree == NULL) {
141 		PRINT_ERR("Root node create failed!\n");
142 	}
143 #endif
144 	usb_attach_sub(dev, bus);
145 	return (0);			/* return success */
146 }
147 
148 /*------------------------------------------------------------------------*
149  *	usb_detach
150  *------------------------------------------------------------------------*/
151 static int
usb_detach(device_t dev)152 usb_detach(device_t dev)
153 {
154 	struct usb_bus *bus = (struct usb_bus *)device_get_softc(dev);
155 
156 	DPRINTF("\n");
157 
158 	if (bus == NULL) {
159 		/* was never setup properly */
160 		return (0);
161 	}
162 	/* Stop power watchdog */
163 	callout_drain(&bus->power_wdog);
164 
165 #if USB_HAVE_ROOT_MOUNT_HOLD
166 	/* Let the USB explore process detach all devices. */
167 	usb_root_mount_rel(bus);
168 #endif
169 
170 	USB_BUS_LOCK(bus);
171 
172 	/* Queue detach job */
173 	(void)usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
174 	    &bus->detach_msg[0], &bus->detach_msg[1]);
175 
176 	/* Wait for detach to complete */
177 	usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
178 	    &bus->detach_msg[0], &bus->detach_msg[1]);
179 
180 	USB_BUS_UNLOCK(bus);
181 
182 #if USB_HAVE_PER_BUS_PROCESS
183 	/* Get rid of USB callback processes */
184 
185 	usb_proc_free(USB_BUS_GIANT_PROC(bus));
186 	usb_proc_free(USB_BUS_NON_GIANT_ISOC_PROC(bus));
187 	usb_proc_free(USB_BUS_NON_GIANT_BULK_PROC(bus));
188 
189 	/* Get rid of USB explore process */
190 
191 	usb_proc_free(USB_BUS_EXPLORE_PROC(bus));
192 
193 	/* Get rid of control transfer process */
194 
195 	usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus));
196 #endif
197 
198 #if USB_HAVE_PF
199 	usbpf_detach(bus);
200 #endif
201 
202 #if USB_HAVE_DEVICE_TOPOLOGY
203 	usbd_free_bt_node(hub_tree);
204 	hub_tree = NULL;
205 #endif
206 
207 	return (0);
208 }
209 
210 /*------------------------------------------------------------------------*
211  *	usb_suspend
212  *------------------------------------------------------------------------*/
213 static int
usb_suspend(device_t dev)214 usb_suspend(device_t dev)
215 {
216 	struct usb_bus *bus = (struct usb_bus *)device_get_softc(dev);
217 
218 	DPRINTF("\n");
219 
220 	if (bus == NULL) {
221 		/* was never setup properly */
222 		return (0);
223 	}
224 
225 	USB_BUS_LOCK(bus);
226 	(void)usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
227 	    &bus->suspend_msg[0], &bus->suspend_msg[1]);
228 	if (usb_no_suspend_wait == 0) {
229 		/* wait for suspend callback to be executed */
230 		usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
231 		    &bus->suspend_msg[0], &bus->suspend_msg[1]);
232 	}
233 	USB_BUS_UNLOCK(bus);
234 
235 	return (0);
236 }
237 
238 /*------------------------------------------------------------------------*
239  *	usb_resume
240  *------------------------------------------------------------------------*/
241 static int
usb_resume(device_t dev)242 usb_resume(device_t dev)
243 {
244 	struct usb_bus *bus = (struct usb_bus *)device_get_softc(dev);
245 
246 	DPRINTF("\n");
247 
248 	if (bus == NULL) {
249 		/* was never setup properly */
250 		return (0);
251 	}
252 
253 	USB_BUS_LOCK(bus);
254 	(void)usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
255 	    &bus->resume_msg[0], &bus->resume_msg[1]);
256 	if (usb_no_resume_wait == 0) {
257 		/* wait for resume callback to be executed */
258 		usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
259 		    &bus->resume_msg[0], &bus->resume_msg[1]);
260 	}
261 	USB_BUS_UNLOCK(bus);
262 
263 	return (0);
264 }
265 
266 /*------------------------------------------------------------------------*
267  *	usb_bus_reset_async_locked
268  *------------------------------------------------------------------------*/
269 void
usb_bus_reset_async_locked(struct usb_bus * bus)270 usb_bus_reset_async_locked(struct usb_bus *bus)
271 {
272 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
273 
274 	DPRINTF("\n");
275 
276 	if ((bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL) ||
277 	    (bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL)) {
278 		DPRINTF("Reset already pending\n");
279 		return;
280 	}
281 
282 	device_printf(bus->parent, "Resetting controller\n");
283 
284 	(void)usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
285 	    &bus->reset_msg[0], &bus->reset_msg[1]);
286 }
287 
288 /*------------------------------------------------------------------------*
289  *	usb_shutdown
290  *------------------------------------------------------------------------*/
291 static int
usb_shutdown(device_t dev)292 usb_shutdown(device_t dev)
293 {
294 	struct usb_bus *bus = device_get_softc(dev);
295 
296 	DPRINTF("\n");
297 
298 	if (bus == NULL) {
299 		/* was never setup properly */
300 		return (0);
301 	}
302 
303 	DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
304 
305 	USB_BUS_LOCK(bus);
306 	(void)usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
307 	    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
308 	if (usb_no_shutdown_wait == 0) {
309 		/* wait for shutdown callback to be executed */
310 		usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
311 		    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
312 	}
313 	USB_BUS_UNLOCK(bus);
314 
315 	DPRINTF("%s: Controller shutdown complete\n",
316 	    device_get_nameunit(bus->bdev));
317 
318 	return (0);
319 }
320 
321 /*------------------------------------------------------------------------*
322  *	usb_bus_explore
323  *
324  * This function is used to explore the device tree from the root.
325  *------------------------------------------------------------------------*/
326 static void
usb_bus_explore(struct usb_proc_msg * pm)327 usb_bus_explore(struct usb_proc_msg *pm)
328 {
329 	struct usb_bus *bus;
330 	struct usb_device *udev;
331 
332 	bus = ((struct usb_bus_msg *)pm)->bus;
333 	udev = bus->devices[USB_ROOT_HUB_ADDR];
334 
335 	if (bus->no_explore != 0)
336 		return;
337 
338 	if (udev != NULL) {
339 		USB_BUS_UNLOCK(bus);
340 		uhub_explore_handle_re_enumerate(udev);
341 		USB_BUS_LOCK(bus);
342 	}
343 
344 	if ((udev != NULL) && (udev->hub != NULL)) {
345 		if (bus->do_probe) {
346 			bus->do_probe = 0;
347 			bus->driver_added_refcount++;
348 		}
349 		if (bus->driver_added_refcount == 0) {
350 			/* avoid zero, hence that is memory default */
351 			bus->driver_added_refcount = 1;
352 		}
353 
354 #ifdef DDB
355 		/*
356 		 * The following three lines of code are only here to
357 		 * recover from DDB:
358 		 */
359 		usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus));
360 		usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus));
361 		usb_proc_rewakeup(USB_BUS_NON_GIANT_ISOC_PROC(bus));
362 		usb_proc_rewakeup(USB_BUS_NON_GIANT_BULK_PROC(bus));
363 #endif
364 
365 		USB_BUS_UNLOCK(bus);
366 
367 #if USB_HAVE_POWERD
368 		/*
369 		 * First update the USB power state!
370 		 */
371 		usb_bus_powerd(bus);
372 #endif
373 		 /* Explore the Root USB HUB. */
374 		(void)(udev->hub->explore) (udev);
375 		USB_BUS_LOCK(bus);
376 	}
377 #if USB_HAVE_ROOT_MOUNT_HOLD
378 	usb_root_mount_rel(bus);
379 #endif
380 }
381 
382 /*------------------------------------------------------------------------*
383  *	usb_bus_detach
384  *
385  * This function is used to detach the device tree from the root.
386  *------------------------------------------------------------------------*/
387 static void
usb_bus_detach(struct usb_proc_msg * pm)388 usb_bus_detach(struct usb_proc_msg *pm)
389 {
390 	struct usb_bus *bus;
391 	struct usb_device *udev;
392 	device_t dev;
393 
394 	bus = ((struct usb_bus_msg *)pm)->bus;
395 	udev = bus->devices[USB_ROOT_HUB_ADDR];
396 	dev = bus->bdev;
397 	/* clear the softc */
398 	device_set_softc(dev, NULL);
399 	USB_BUS_UNLOCK(bus);
400 
401 	/* detach children first */
402 	mtx_lock(&Giant);
403 	(void)bus_generic_detach(dev);
404 	mtx_unlock(&Giant);
405 
406 	/*
407 	 * Free USB device and all subdevices, if any.
408 	 */
409 	usb_free_device(udev, 0);
410 
411 	USB_BUS_LOCK(bus);
412 	/* clear bdev variable last */
413 	bus->bdev = NULL;
414 }
415 
416 /*------------------------------------------------------------------------*
417  *	usb_bus_suspend
418  *
419  * This function is used to suspend the USB controller.
420  *------------------------------------------------------------------------*/
421 static void
usb_bus_suspend(struct usb_proc_msg * pm)422 usb_bus_suspend(struct usb_proc_msg *pm)
423 {
424 	struct usb_bus *bus;
425 	struct usb_device *udev;
426 	usb_error_t err;
427 	uint8_t do_unlock;
428 
429 	DPRINTF("\n");
430 
431 	bus = ((struct usb_bus_msg *)pm)->bus;
432 	udev = bus->devices[USB_ROOT_HUB_ADDR];
433 
434 	if ((udev == NULL) || (bus->bdev == NULL))
435 		return;
436 
437 	USB_BUS_UNLOCK(bus);
438 
439 	/*
440 	 * We use the shutdown event here because the suspend and
441 	 * resume events are reserved for the USB port suspend and
442 	 * resume. The USB system suspend is implemented like full
443 	 * shutdown and all connected USB devices will be disconnected
444 	 * subsequently. At resume all USB devices will be
445 	 * re-connected again.
446 	 */
447 
448 	(void)bus_generic_shutdown(bus->bdev);
449 
450 	do_unlock = usbd_enum_lock(udev);
451 
452 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
453 	if (err)
454 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
455 
456 	USB_BUS_LOCK(bus);
457 	bus->hw_power_state = 0;
458 	bus->no_explore = 1;
459 	USB_BUS_UNLOCK(bus);
460 
461 	if (bus->methods->set_hw_power != NULL)
462 		(bus->methods->set_hw_power) (bus);
463 
464 	if (bus->methods->set_hw_power_sleep != NULL)
465 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
466 
467 	if (do_unlock)
468 		usbd_enum_unlock(udev);
469 
470 	USB_BUS_LOCK(bus);
471 }
472 
473 /*------------------------------------------------------------------------*
474  *	usb_bus_resume
475  *
476  * This function is used to resume the USB controller.
477  *------------------------------------------------------------------------*/
478 static void
usb_bus_resume(struct usb_proc_msg * pm)479 usb_bus_resume(struct usb_proc_msg *pm)
480 {
481 	struct usb_bus *bus;
482 	struct usb_device *udev;
483 	usb_error_t err;
484 	uint8_t do_unlock;
485 
486 	DPRINTF("\n");
487 
488 	bus = ((struct usb_bus_msg *)pm)->bus;
489 	udev = bus->devices[USB_ROOT_HUB_ADDR];
490 
491 	if ((udev == NULL) || (bus->bdev == NULL))
492 		return;
493 
494 	USB_BUS_UNLOCK(bus);
495 
496 	do_unlock = usbd_enum_lock(udev);
497 
498 	USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
499 
500 	USB_BUS_LOCK(bus);
501  	bus->hw_power_state =
502 	  USB_HW_POWER_CONTROL |
503 	  USB_HW_POWER_BULK |
504 	  USB_HW_POWER_INTERRUPT |
505 	  USB_HW_POWER_ISOC |
506 	  USB_HW_POWER_NON_ROOT_HUB;
507 	bus->no_explore = 0;
508 	USB_BUS_UNLOCK(bus);
509 
510 	if (bus->methods->set_hw_power_sleep != NULL)
511 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
512 
513 	if (bus->methods->set_hw_power != NULL)
514 		(bus->methods->set_hw_power) (bus);
515 
516 	/* restore USB configuration to index 0 */
517 	err = usbd_set_config_index(udev, 0);
518 	if (err)
519 		device_printf(bus->bdev, "Could not configure root HUB\n");
520 
521 	/* probe and attach */
522 	err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
523 	if (err) {
524 		device_printf(bus->bdev, "Could not probe and "
525 		    "attach root HUB\n");
526 	}
527 
528 	if (do_unlock)
529 		usbd_enum_unlock(udev);
530 
531 	USB_BUS_LOCK(bus);
532 }
533 
534 /*------------------------------------------------------------------------*
535  *	usb_bus_reset
536  *
537  * This function is used to reset the USB controller.
538  *------------------------------------------------------------------------*/
539 static void
usb_bus_reset(struct usb_proc_msg * pm)540 usb_bus_reset(struct usb_proc_msg *pm)
541 {
542 	struct usb_bus *bus;
543 
544 	DPRINTF("\n");
545 
546 	bus = ((struct usb_bus_msg *)pm)->bus;
547 
548 	if ((bus->bdev == NULL) || (bus->no_explore != 0))
549 		return;
550 
551 	/* a suspend and resume will reset the USB controller */
552 	usb_bus_suspend(pm);
553 	usb_bus_resume(pm);
554 }
555 
556 /*------------------------------------------------------------------------*
557  *	usb_bus_shutdown
558  *
559  * This function is used to shutdown the USB controller.
560  *------------------------------------------------------------------------*/
561 static void
usb_bus_shutdown(struct usb_proc_msg * pm)562 usb_bus_shutdown(struct usb_proc_msg *pm)
563 {
564 	struct usb_bus *bus;
565 	struct usb_device *udev;
566 	usb_error_t err;
567 	uint8_t do_unlock;
568 
569 	DPRINTF("\n");
570 	bus = ((struct usb_bus_msg *)pm)->bus;
571 	udev = bus->devices[USB_ROOT_HUB_ADDR];
572 
573 	if ((udev == NULL) || (bus->bdev == NULL))
574 		return;
575 
576 	USB_BUS_UNLOCK(bus);
577 
578 	(void)bus_generic_shutdown(bus->bdev);
579 
580 	do_unlock = usbd_enum_lock(udev);
581 
582 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
583 	if (err)
584 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
585 
586 	USB_BUS_LOCK(bus);
587 	bus->hw_power_state = 0;
588 	bus->no_explore = 1;
589 	USB_BUS_UNLOCK(bus);
590 
591 	if (bus->methods->set_hw_power != NULL)
592 		(bus->methods->set_hw_power) (bus);
593 
594 	if (bus->methods->set_hw_power_sleep != NULL)
595 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
596 
597 	if (do_unlock)
598 		usbd_enum_unlock(udev);
599 
600 	USB_BUS_LOCK(bus);
601 }
602 
603 /*------------------------------------------------------------------------*
604  *	usb_bus_attach
605  *
606  * This function attaches USB in context of the explore thread.
607  *------------------------------------------------------------------------*/
608 static void
usb_bus_attach(struct usb_proc_msg * pm)609 usb_bus_attach(struct usb_proc_msg *pm)
610 {
611 	struct usb_bus *bus;
612 	struct usb_device *child;
613 	device_t dev;
614 	usb_error_t err;
615 	enum usb_dev_speed speed;
616 
617 	bus = ((struct usb_bus_msg *)pm)->bus;
618 	dev = bus->bdev;
619 
620 	DPRINTF("\n");
621 
622 	switch (bus->usbrev) {
623 	case USB_REV_1_0:
624 		speed = USB_SPEED_FULL;
625 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
626 		break;
627 
628 	case USB_REV_1_1:
629 		speed = USB_SPEED_FULL;
630 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
631 		break;
632 
633 	case USB_REV_2_0:
634 		speed = USB_SPEED_HIGH;
635 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
636 		break;
637 
638 	case USB_REV_2_5:
639 		speed = USB_SPEED_VARIABLE;
640 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
641 		break;
642 
643 	case USB_REV_3_0:
644 		speed = USB_SPEED_SUPER;
645 		device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
646 		break;
647 
648 	default:
649 		device_printf(bus->bdev, "Unsupported USB revision\n");
650 #if USB_HAVE_ROOT_MOUNT_HOLD
651 		usb_root_mount_rel(bus);
652 #endif
653 		return;
654 	}
655 
656 	/* default power_mask value */
657 	bus->hw_power_state =
658 	  USB_HW_POWER_CONTROL |
659 	  USB_HW_POWER_BULK |
660 	  USB_HW_POWER_INTERRUPT |
661 	  USB_HW_POWER_ISOC |
662 	  USB_HW_POWER_NON_ROOT_HUB;
663 
664 	USB_BUS_UNLOCK(bus);
665 
666 	/* make sure power is set at least once */
667 
668 	if (bus->methods->set_hw_power != NULL) {
669 		(bus->methods->set_hw_power) (bus);
670 	}
671 
672 	/* allocate the Root USB device */
673 
674 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
675 	    speed, USB_MODE_HOST);
676 	if (child) {
677 		err = usb_probe_and_attach(child,
678 		    USB_IFACE_INDEX_ANY);
679 		if (!err) {
680 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
681 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
682 				err = USB_ERR_NO_ROOT_HUB;
683 			}
684 		}
685 	} else {
686 		err = USB_ERR_NOMEM;
687 	}
688 
689 	USB_BUS_LOCK(bus);
690 
691 	if (err) {
692 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
693 		    usbd_errstr(err));
694 #if USB_HAVE_ROOT_MOUNT_HOLD
695 		usb_root_mount_rel(bus);
696 #endif
697 	}
698 
699 	/* set softc - we are ready */
700 	device_set_softc(dev, bus);
701 }
702 
703 /*------------------------------------------------------------------------*
704  *	usb_attach_sub
705  *
706  * This function creates a thread which runs the USB attach code.
707  *------------------------------------------------------------------------*/
708 
709 static void
usb_attach_sub(device_t dev,struct usb_bus * bus)710 usb_attach_sub(device_t dev, struct usb_bus *bus)
711 {
712 	mtx_lock(&Giant);
713 	if (usb_devclass_ptr == NULL)
714 		usb_devclass_ptr = devclass_find("usbus");
715 	mtx_unlock(&Giant);
716 
717 #if USB_HAVE_PF
718 	usbpf_attach(bus);
719 #endif
720 	/* Initialise USB process messages */
721 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
722 	bus->explore_msg[0].bus = bus;
723 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
724 	bus->explore_msg[1].bus = bus;
725 
726 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
727 	bus->detach_msg[0].bus = bus;
728 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
729 	bus->detach_msg[1].bus = bus;
730 
731 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
732 	bus->attach_msg[0].bus = bus;
733 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
734 	bus->attach_msg[1].bus = bus;
735 
736 	bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
737 	bus->suspend_msg[0].bus = bus;
738 	bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
739 	bus->suspend_msg[1].bus = bus;
740 
741 	bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
742 	bus->resume_msg[0].bus = bus;
743 	bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
744 	bus->resume_msg[1].bus = bus;
745 
746 	bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
747 	bus->reset_msg[0].bus = bus;
748 	bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
749 	bus->reset_msg[1].bus = bus;
750 
751 	bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
752 	bus->shutdown_msg[0].bus = bus;
753 	bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
754 	bus->shutdown_msg[1].bus = bus;
755 
756 #if USB_HAVE_PER_BUS_PROCESS
757 	/* Create USB explore and callback processes */
758 	if (usb_proc_create(USB_BUS_GIANT_PROC(bus),
759 	    &bus->bus_mtx, "USB_GIANT_Task", USB_PRI_MED)) {
760 		device_printf(dev, "WARNING: Creation of USB Giant "
761 		    "callback process failed.\n");
762 	} else if (usb_proc_create(USB_BUS_NON_GIANT_ISOC_PROC(bus),
763 	    &bus->bus_mtx, "USB_NGIAN_ISOC_Task", USB_PRI_HIGHEST)) {
764 		device_printf(dev, "WARNING: Creation of USB non-Giant ISOC"
765 		    "callback process failed.\n");
766 	} else if (usb_proc_create(USB_BUS_NON_GIANT_BULK_PROC(bus),
767 	    &bus->bus_mtx, "USB_NGIAN_BULK_Task", USB_PRI_HIGH)) {
768 		device_printf(dev, "WARNING: Creation of USB non-Giant BULK"
769 		    "callback process failed.\n");
770 	} else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
771 	    &bus->bus_mtx, "USB_EXPLR_Task", USB_PRI_MED)) {
772 		device_printf(dev, "WARNING: Creation of USB explore "
773 		    "process failed.\n");
774 	} else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
775 	    &bus->bus_mtx, "USB_CXFER_Task", USB_PRI_MED)) {
776 		device_printf(dev, "WARNING: Creation of USB control transfer "
777 		    "process failed.\n");
778 	} else
779 #endif
780 	{
781 		/* Get final attach going */
782 		USB_BUS_LOCK(bus);
783 		(void)usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
784 		    &bus->attach_msg[0], &bus->attach_msg[1]);
785 		USB_BUS_UNLOCK(bus);
786 
787 		/* Do initial explore */
788 		if (usb_port_status_get()) {
789 			usb_needs_explore(bus, 1);
790 		}
791 	}
792 }
793 
794 /*------------------------------------------------------------------------*
795  *	usb_bus_mem_flush_all_cb
796  *------------------------------------------------------------------------*/
797 #if USB_HAVE_BUSDMA
798 static void
usb_bus_mem_flush_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t nsize,usb_size_t align)799 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
800     struct usb_page *pg, usb_size_t nsize, usb_size_t align)
801 {
802 	usb_pc_cpu_flush(pc);
803 }
804 #endif
805 
806 /*------------------------------------------------------------------------*
807  *	usb_bus_mem_flush_all - factored out code
808  *------------------------------------------------------------------------*/
809 #if USB_HAVE_BUSDMA
810 void
usb_bus_mem_flush_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)811 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
812 {
813 	if (cb) {
814 		cb(bus, &usb_bus_mem_flush_all_cb);
815 	}
816 }
817 #endif
818 
819 /*------------------------------------------------------------------------*
820  *	usb_bus_mem_alloc_all_cb
821  *------------------------------------------------------------------------*/
822 #if USB_HAVE_BUSDMA
823 static void
usb_bus_mem_alloc_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t nsize,usb_size_t align)824 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
825     struct usb_page *pg, usb_size_t nsize, usb_size_t align)
826 {
827 	/* need to initialize the page cache */
828 	pc->tag_parent = bus->dma_parent_tag;
829 
830 	if (usb_pc_alloc_mem(pc, pg, nsize, align)) {
831 		bus->alloc_failed = 1;
832 	}
833 }
834 #endif
835 
836 /*------------------------------------------------------------------------*
837  *	usb_bus_mem_alloc_all - factored out code
838  *
839  * Returns:
840  *    0: Success
841  * Else: Failure
842  *------------------------------------------------------------------------*/
843 uint8_t
usb_bus_mem_alloc_all(struct usb_bus * bus,bus_dma_tag_t dmat,usb_bus_mem_cb_t * cb)844 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
845     usb_bus_mem_cb_t *cb)
846 {
847 	bus->alloc_failed = 0;
848 
849 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
850 	    "usb_def_mtx", MTX_DEF | MTX_RECURSE);
851 
852 	mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent),
853 	    "usb_spin_mtx", MTX_SPIN | MTX_RECURSE);
854 
855 	callout_init_mtx(&bus->power_wdog,
856 	    &bus->bus_mtx, 0);
857 
858 	TAILQ_INIT(&bus->intr_q.head);
859 
860 #if USB_HAVE_BUSDMA
861 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
862 	    dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
863 #endif
864 	if ((bus->devices_max > USB_MAX_DEVICES) ||
865 	    (bus->devices_max < USB_MIN_DEVICES) ||
866 	    (bus->devices == NULL)) {
867 		DPRINTFN(0, "Devices field has not been "
868 		    "initialised properly\n");
869 		bus->alloc_failed = 1;		/* failure */
870 	}
871 #if USB_HAVE_BUSDMA
872 	if (cb) {
873 		cb(bus, &usb_bus_mem_alloc_all_cb);
874 	}
875 #endif
876 	if (bus->alloc_failed) {
877 		usb_bus_mem_free_all(bus, cb);
878 	}
879 	return (bus->alloc_failed);
880 }
881 
882 /*------------------------------------------------------------------------*
883  *	usb_bus_mem_free_all_cb
884  *------------------------------------------------------------------------*/
885 #if USB_HAVE_BUSDMA
886 static void
usb_bus_mem_free_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t nsize,usb_size_t align)887 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
888     struct usb_page *pg, usb_size_t nsize, usb_size_t align)
889 {
890 	usb_pc_free_mem(pc);
891 }
892 #endif
893 
894 /*------------------------------------------------------------------------*
895  *	usb_bus_mem_free_all - factored out code
896  *------------------------------------------------------------------------*/
897 void
usb_bus_mem_free_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)898 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
899 {
900 #if USB_HAVE_BUSDMA
901 	if (cb) {
902 		cb(bus, &usb_bus_mem_free_all_cb);
903 	}
904 	usb_dma_tag_unsetup(bus->dma_parent_tag);
905 #endif
906 
907 	mtx_destroy(&bus->bus_mtx);
908 	mtx_destroy(&bus->bus_spin_lock);
909 }
910 
911 /* convenience wrappers */
912 void
usb_proc_explore_mwait(struct usb_device * udev,void * pm1,void * pm2)913 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
914 {
915 	usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
916 }
917 
918 void *
usb_proc_explore_msignal(struct usb_device * udev,void * pm1,void * pm2)919 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
920 {
921 	return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
922 }
923 
924 void
usb_proc_explore_lock(struct usb_device * udev)925 usb_proc_explore_lock(struct usb_device *udev)
926 {
927 	USB_BUS_LOCK(udev->bus);
928 }
929 
930 void
usb_proc_explore_unlock(struct usb_device * udev)931 usb_proc_explore_unlock(struct usb_device *udev)
932 {
933 	USB_BUS_UNLOCK(udev->bus);
934 }
935 
936 #undef	USB_DEBUG_VAR
937