1 /* $FreeBSD: stable/9/sys/dev/usb/controller/usb_controller.c 278291 2015-02-05 21:37:59Z hselasky $ */
2 /*-
3  * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24  * SUCH DAMAGE.
25  */
26 
27 #include "opt_ddb.h"
28 
29 #include <sys/stdint.h>
30 #include <sys/stddef.h>
31 #include <sys/param.h>
32 #include <sys/queue.h>
33 #include <sys/types.h>
34 #include <sys/systm.h>
35 #include <sys/kernel.h>
36 #include <sys/bus.h>
37 #include <sys/module.h>
38 #include <sys/lock.h>
39 #include <sys/mutex.h>
40 #include <sys/condvar.h>
41 #include <sys/sysctl.h>
42 #include <sys/sx.h>
43 #include <sys/unistd.h>
44 #include <sys/callout.h>
45 #include <sys/malloc.h>
46 #include <sys/priv.h>
47 
48 #include <dev/usb/usb.h>
49 #include <dev/usb/usbdi.h>
50 
51 #define	USB_DEBUG_VAR usb_ctrl_debug
52 
53 #include <dev/usb/usb_core.h>
54 #include <dev/usb/usb_debug.h>
55 #include <dev/usb/usb_process.h>
56 #include <dev/usb/usb_busdma.h>
57 #include <dev/usb/usb_dynamic.h>
58 #include <dev/usb/usb_device.h>
59 #include <dev/usb/usb_dev.h>
60 #include <dev/usb/usb_hub.h>
61 
62 #include <dev/usb/usb_controller.h>
63 #include <dev/usb/usb_bus.h>
64 #include <dev/usb/usb_pf.h>
65 #include "usb_if.h"
66 
67 /* function prototypes  */
68 
69 static device_probe_t usb_probe;
70 static device_attach_t usb_attach;
71 static device_detach_t usb_detach;
72 static device_suspend_t usb_suspend;
73 static device_resume_t usb_resume;
74 static device_shutdown_t usb_shutdown;
75 
76 static void	usb_attach_sub(device_t, struct usb_bus *);
77 
78 /* static variables */
79 
80 #ifdef USB_DEBUG
81 static int usb_ctrl_debug = 0;
82 
83 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW, 0, "USB controller");
84 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RW, &usb_ctrl_debug, 0,
85     "Debug level");
86 #endif
87 
88 static int usb_no_boot_wait = 0;
89 TUNABLE_INT("hw.usb.no_boot_wait", &usb_no_boot_wait);
90 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RD|CTLFLAG_TUN, &usb_no_boot_wait, 0,
91     "No USB device enumerate waiting at boot.");
92 
93 static int usb_no_suspend_wait = 0;
94 TUNABLE_INT("hw.usb.no_suspend_wait", &usb_no_suspend_wait);
95 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RW|CTLFLAG_TUN,
96     &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
97 
98 static int usb_no_shutdown_wait = 0;
99 TUNABLE_INT("hw.usb.no_shutdown_wait", &usb_no_shutdown_wait);
100 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RW|CTLFLAG_TUN,
101     &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
102 
103 static devclass_t usb_devclass;
104 
105 static device_method_t usb_methods[] = {
106 	DEVMETHOD(device_probe, usb_probe),
107 	DEVMETHOD(device_attach, usb_attach),
108 	DEVMETHOD(device_detach, usb_detach),
109 	DEVMETHOD(device_suspend, usb_suspend),
110 	DEVMETHOD(device_resume, usb_resume),
111 	DEVMETHOD(device_shutdown, usb_shutdown),
112 	{0, 0}
113 };
114 
115 static driver_t usb_driver = {
116 	.name = "usbus",
117 	.methods = usb_methods,
118 	.size = 0,
119 };
120 
121 /* Host Only Drivers */
122 DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0);
123 DRIVER_MODULE(usbus, uhci, usb_driver, usb_devclass, 0, 0);
124 DRIVER_MODULE(usbus, ehci, usb_driver, usb_devclass, 0, 0);
125 DRIVER_MODULE(usbus, xhci, usb_driver, usb_devclass, 0, 0);
126 
127 /* Device Only Drivers */
128 DRIVER_MODULE(usbus, at91_udp, usb_driver, usb_devclass, 0, 0);
129 DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
130 DRIVER_MODULE(usbus, uss820dci, usb_driver, usb_devclass, 0, 0);
131 
132 /*------------------------------------------------------------------------*
133  *	usb_probe
134  *
135  * This function is called from "{ehci,ohci,uhci}_pci_attach()".
136  *------------------------------------------------------------------------*/
137 static int
usb_probe(device_t dev)138 usb_probe(device_t dev)
139 {
140 	DPRINTF("\n");
141 	return (0);
142 }
143 
144 static void
usb_root_mount_rel(struct usb_bus * bus)145 usb_root_mount_rel(struct usb_bus *bus)
146 {
147 	if (bus->bus_roothold != NULL) {
148 		DPRINTF("Releasing root mount hold %p\n", bus->bus_roothold);
149 		root_mount_rel(bus->bus_roothold);
150 		bus->bus_roothold = NULL;
151 	}
152 }
153 
154 /*------------------------------------------------------------------------*
155  *	usb_attach
156  *------------------------------------------------------------------------*/
157 static int
usb_attach(device_t dev)158 usb_attach(device_t dev)
159 {
160 	struct usb_bus *bus = device_get_ivars(dev);
161 
162 	DPRINTF("\n");
163 
164 	if (bus == NULL) {
165 		device_printf(dev, "USB device has no ivars\n");
166 		return (ENXIO);
167 	}
168 
169 	if (usb_no_boot_wait == 0) {
170 		/* delay vfs_mountroot until the bus is explored */
171 		bus->bus_roothold = root_mount_hold(device_get_nameunit(dev));
172 	}
173 
174 	usb_attach_sub(dev, bus);
175 
176 	return (0);			/* return success */
177 }
178 
179 /*------------------------------------------------------------------------*
180  *	usb_detach
181  *------------------------------------------------------------------------*/
182 static int
usb_detach(device_t dev)183 usb_detach(device_t dev)
184 {
185 	struct usb_bus *bus = device_get_softc(dev);
186 
187 	DPRINTF("\n");
188 
189 	if (bus == NULL) {
190 		/* was never setup properly */
191 		return (0);
192 	}
193 	/* Stop power watchdog */
194 	usb_callout_drain(&bus->power_wdog);
195 
196 	/* Let the USB explore process detach all devices. */
197 	usb_root_mount_rel(bus);
198 
199 	USB_BUS_LOCK(bus);
200 
201 	/* Queue detach job */
202 	usb_proc_msignal(&bus->explore_proc,
203 	    &bus->detach_msg[0], &bus->detach_msg[1]);
204 
205 	/* Wait for detach to complete */
206 	usb_proc_mwait(&bus->explore_proc,
207 	    &bus->detach_msg[0], &bus->detach_msg[1]);
208 
209 #if USB_HAVE_UGEN
210 	/* Wait for cleanup to complete */
211 	usb_proc_mwait(&bus->explore_proc,
212 	    &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
213 #endif
214 	USB_BUS_UNLOCK(bus);
215 
216 	/* Get rid of USB callback processes */
217 
218 	usb_proc_free(&bus->giant_callback_proc);
219 	usb_proc_free(&bus->non_giant_callback_proc);
220 
221 	/* Get rid of USB explore process */
222 
223 	usb_proc_free(&bus->explore_proc);
224 
225 	/* Get rid of control transfer process */
226 
227 	usb_proc_free(&bus->control_xfer_proc);
228 
229 #if USB_HAVE_PF
230 	usbpf_detach(bus);
231 #endif
232 	return (0);
233 }
234 
235 /*------------------------------------------------------------------------*
236  *	usb_suspend
237  *------------------------------------------------------------------------*/
238 static int
usb_suspend(device_t dev)239 usb_suspend(device_t dev)
240 {
241 	struct usb_bus *bus = device_get_softc(dev);
242 
243 	DPRINTF("\n");
244 
245 	if (bus == NULL) {
246 		/* was never setup properly */
247 		return (0);
248 	}
249 
250 	USB_BUS_LOCK(bus);
251 	usb_proc_msignal(&bus->explore_proc,
252 	    &bus->suspend_msg[0], &bus->suspend_msg[1]);
253 	if (usb_no_suspend_wait == 0) {
254 		/* wait for suspend callback to be executed */
255 		usb_proc_mwait(&bus->explore_proc,
256 		    &bus->suspend_msg[0], &bus->suspend_msg[1]);
257 	}
258 	USB_BUS_UNLOCK(bus);
259 
260 	return (0);
261 }
262 
263 /*------------------------------------------------------------------------*
264  *	usb_resume
265  *------------------------------------------------------------------------*/
266 static int
usb_resume(device_t dev)267 usb_resume(device_t dev)
268 {
269 	struct usb_bus *bus = device_get_softc(dev);
270 
271 	DPRINTF("\n");
272 
273 	if (bus == NULL) {
274 		/* was never setup properly */
275 		return (0);
276 	}
277 
278 	USB_BUS_LOCK(bus);
279 	usb_proc_msignal(&bus->explore_proc,
280 	    &bus->resume_msg[0], &bus->resume_msg[1]);
281 	USB_BUS_UNLOCK(bus);
282 
283 	return (0);
284 }
285 
286 /*------------------------------------------------------------------------*
287  *	usb_bus_reset_async_locked
288  *------------------------------------------------------------------------*/
289 void
usb_bus_reset_async_locked(struct usb_bus * bus)290 usb_bus_reset_async_locked(struct usb_bus *bus)
291 {
292 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
293 
294 	DPRINTF("\n");
295 
296 	if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
297 	    bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
298 		DPRINTF("Reset already pending\n");
299 		return;
300 	}
301 
302 	device_printf(bus->parent, "Resetting controller\n");
303 
304 	usb_proc_msignal(&bus->explore_proc,
305 	    &bus->reset_msg[0], &bus->reset_msg[1]);
306 }
307 
308 /*------------------------------------------------------------------------*
309  *	usb_shutdown
310  *------------------------------------------------------------------------*/
311 static int
usb_shutdown(device_t dev)312 usb_shutdown(device_t dev)
313 {
314 	struct usb_bus *bus = device_get_softc(dev);
315 
316 	DPRINTF("\n");
317 
318 	if (bus == NULL) {
319 		/* was never setup properly */
320 		return (0);
321 	}
322 
323 	DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
324 
325 	USB_BUS_LOCK(bus);
326 	usb_proc_msignal(&bus->explore_proc,
327 	    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
328 	if (usb_no_shutdown_wait == 0) {
329 		/* wait for shutdown callback to be executed */
330 		usb_proc_mwait(&bus->explore_proc,
331 		    &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
332 	}
333 	USB_BUS_UNLOCK(bus);
334 
335 	DPRINTF("%s: Controller shutdown complete\n",
336 	    device_get_nameunit(bus->bdev));
337 
338 	return (0);
339 }
340 
341 /*------------------------------------------------------------------------*
342  *	usb_bus_explore
343  *
344  * This function is used to explore the device tree from the root.
345  *------------------------------------------------------------------------*/
346 static void
usb_bus_explore(struct usb_proc_msg * pm)347 usb_bus_explore(struct usb_proc_msg *pm)
348 {
349 	struct usb_bus *bus;
350 	struct usb_device *udev;
351 
352 	bus = ((struct usb_bus_msg *)pm)->bus;
353 	udev = bus->devices[USB_ROOT_HUB_ADDR];
354 
355 	if (bus->no_explore != 0)
356 		return;
357 
358 	if (udev != NULL) {
359 		USB_BUS_UNLOCK(bus);
360 		uhub_explore_handle_re_enumerate(udev);
361 		USB_BUS_LOCK(bus);
362 	}
363 
364 	if (udev != NULL && udev->hub != NULL) {
365 
366 		if (bus->do_probe) {
367 			bus->do_probe = 0;
368 			bus->driver_added_refcount++;
369 		}
370 		if (bus->driver_added_refcount == 0) {
371 			/* avoid zero, hence that is memory default */
372 			bus->driver_added_refcount = 1;
373 		}
374 
375 #ifdef DDB
376 		/*
377 		 * The following three lines of code are only here to
378 		 * recover from DDB:
379 		 */
380 		usb_proc_rewakeup(&bus->control_xfer_proc);
381 		usb_proc_rewakeup(&bus->giant_callback_proc);
382 		usb_proc_rewakeup(&bus->non_giant_callback_proc);
383 #endif
384 
385 		USB_BUS_UNLOCK(bus);
386 
387 #if USB_HAVE_POWERD
388 		/*
389 		 * First update the USB power state!
390 		 */
391 		usb_bus_powerd(bus);
392 #endif
393 		 /* Explore the Root USB HUB. */
394 		(udev->hub->explore) (udev);
395 		USB_BUS_LOCK(bus);
396 	}
397 	usb_root_mount_rel(bus);
398 }
399 
400 /*------------------------------------------------------------------------*
401  *	usb_bus_detach
402  *
403  * This function is used to detach the device tree from the root.
404  *------------------------------------------------------------------------*/
405 static void
usb_bus_detach(struct usb_proc_msg * pm)406 usb_bus_detach(struct usb_proc_msg *pm)
407 {
408 	struct usb_bus *bus;
409 	struct usb_device *udev;
410 	device_t dev;
411 
412 	bus = ((struct usb_bus_msg *)pm)->bus;
413 	udev = bus->devices[USB_ROOT_HUB_ADDR];
414 	dev = bus->bdev;
415 	/* clear the softc */
416 	device_set_softc(dev, NULL);
417 	USB_BUS_UNLOCK(bus);
418 
419 	/* detach children first */
420 	mtx_lock(&Giant);
421 	bus_generic_detach(dev);
422 	mtx_unlock(&Giant);
423 
424 	/*
425 	 * Free USB device and all subdevices, if any.
426 	 */
427 	usb_free_device(udev, 0);
428 
429 	USB_BUS_LOCK(bus);
430 	/* clear bdev variable last */
431 	bus->bdev = NULL;
432 }
433 
434 /*------------------------------------------------------------------------*
435  *	usb_bus_suspend
436  *
437  * This function is used to suspend the USB controller.
438  *------------------------------------------------------------------------*/
439 static void
usb_bus_suspend(struct usb_proc_msg * pm)440 usb_bus_suspend(struct usb_proc_msg *pm)
441 {
442 	struct usb_bus *bus;
443 	struct usb_device *udev;
444 	usb_error_t err;
445 	uint8_t do_unlock;
446 
447 	DPRINTF("\n");
448 
449 	bus = ((struct usb_bus_msg *)pm)->bus;
450 	udev = bus->devices[USB_ROOT_HUB_ADDR];
451 
452 	if (udev == NULL || bus->bdev == NULL)
453 		return;
454 
455 	USB_BUS_UNLOCK(bus);
456 
457 	/*
458 	 * We use the shutdown event here because the suspend and
459 	 * resume events are reserved for the USB port suspend and
460 	 * resume. The USB system suspend is implemented like full
461 	 * shutdown and all connected USB devices will be disconnected
462 	 * subsequently. At resume all USB devices will be
463 	 * re-connected again.
464 	 */
465 
466 	bus_generic_shutdown(bus->bdev);
467 
468 	do_unlock = usbd_enum_lock(udev);
469 
470 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
471 	if (err)
472 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
473 
474 	USB_BUS_LOCK(bus);
475 	bus->hw_power_state = 0;
476 	bus->no_explore = 1;
477 	USB_BUS_UNLOCK(bus);
478 
479 	if (bus->methods->set_hw_power != NULL)
480 		(bus->methods->set_hw_power) (bus);
481 
482 	if (bus->methods->set_hw_power_sleep != NULL)
483 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
484 
485 	if (do_unlock)
486 		usbd_enum_unlock(udev);
487 
488 	USB_BUS_LOCK(bus);
489 }
490 
491 /*------------------------------------------------------------------------*
492  *	usb_bus_resume
493  *
494  * This function is used to resume the USB controller.
495  *------------------------------------------------------------------------*/
496 static void
usb_bus_resume(struct usb_proc_msg * pm)497 usb_bus_resume(struct usb_proc_msg *pm)
498 {
499 	struct usb_bus *bus;
500 	struct usb_device *udev;
501 	usb_error_t err;
502 	uint8_t do_unlock;
503 
504 	DPRINTF("\n");
505 
506 	bus = ((struct usb_bus_msg *)pm)->bus;
507 	udev = bus->devices[USB_ROOT_HUB_ADDR];
508 
509 	if (udev == NULL || bus->bdev == NULL)
510 		return;
511 
512 	USB_BUS_UNLOCK(bus);
513 
514 	do_unlock = usbd_enum_lock(udev);
515 #if 0
516 	DEVMETHOD(usb_take_controller, NULL);	/* dummy */
517 #endif
518 	USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
519 
520 	USB_BUS_LOCK(bus);
521  	bus->hw_power_state =
522 	  USB_HW_POWER_CONTROL |
523 	  USB_HW_POWER_BULK |
524 	  USB_HW_POWER_INTERRUPT |
525 	  USB_HW_POWER_ISOC |
526 	  USB_HW_POWER_NON_ROOT_HUB;
527 	bus->no_explore = 0;
528 	USB_BUS_UNLOCK(bus);
529 
530 	if (bus->methods->set_hw_power_sleep != NULL)
531 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
532 
533 	if (bus->methods->set_hw_power != NULL)
534 		(bus->methods->set_hw_power) (bus);
535 
536 	/* restore USB configuration to index 0 */
537 	err = usbd_set_config_index(udev, 0);
538 	if (err)
539 		device_printf(bus->bdev, "Could not configure root HUB\n");
540 
541 	/* probe and attach */
542 	err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
543 	if (err) {
544 		device_printf(bus->bdev, "Could not probe and "
545 		    "attach root HUB\n");
546 	}
547 
548 	if (do_unlock)
549 		usbd_enum_unlock(udev);
550 
551 	USB_BUS_LOCK(bus);
552 }
553 
554 /*------------------------------------------------------------------------*
555  *	usb_bus_reset
556  *
557  * This function is used to reset the USB controller.
558  *------------------------------------------------------------------------*/
559 static void
usb_bus_reset(struct usb_proc_msg * pm)560 usb_bus_reset(struct usb_proc_msg *pm)
561 {
562 	struct usb_bus *bus;
563 
564 	DPRINTF("\n");
565 
566 	bus = ((struct usb_bus_msg *)pm)->bus;
567 
568 	if (bus->bdev == NULL || bus->no_explore != 0)
569 		return;
570 
571 	/* a suspend and resume will reset the USB controller */
572 	usb_bus_suspend(pm);
573 	usb_bus_resume(pm);
574 }
575 
576 /*------------------------------------------------------------------------*
577  *	usb_bus_shutdown
578  *
579  * This function is used to shutdown the USB controller.
580  *------------------------------------------------------------------------*/
581 static void
usb_bus_shutdown(struct usb_proc_msg * pm)582 usb_bus_shutdown(struct usb_proc_msg *pm)
583 {
584 	struct usb_bus *bus;
585 	struct usb_device *udev;
586 	usb_error_t err;
587 	uint8_t do_unlock;
588 
589 	bus = ((struct usb_bus_msg *)pm)->bus;
590 	udev = bus->devices[USB_ROOT_HUB_ADDR];
591 
592 	if (udev == NULL || bus->bdev == NULL)
593 		return;
594 
595 	USB_BUS_UNLOCK(bus);
596 
597 	bus_generic_shutdown(bus->bdev);
598 
599 	do_unlock = usbd_enum_lock(udev);
600 
601 	err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
602 	if (err)
603 		device_printf(bus->bdev, "Could not unconfigure root HUB\n");
604 
605 	USB_BUS_LOCK(bus);
606 	bus->hw_power_state = 0;
607 	bus->no_explore = 1;
608 	USB_BUS_UNLOCK(bus);
609 
610 	if (bus->methods->set_hw_power != NULL)
611 		(bus->methods->set_hw_power) (bus);
612 
613 	if (bus->methods->set_hw_power_sleep != NULL)
614 		(bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
615 
616 	if (do_unlock)
617 		usbd_enum_unlock(udev);
618 
619 	USB_BUS_LOCK(bus);
620 }
621 
622 /*------------------------------------------------------------------------*
623  *	usb_bus_cleanup
624  *
625  * This function is used to cleanup leftover USB character devices.
626  *------------------------------------------------------------------------*/
627 #if USB_HAVE_UGEN
628 static void
usb_bus_cleanup(struct usb_proc_msg * pm)629 usb_bus_cleanup(struct usb_proc_msg *pm)
630 {
631 	struct usb_bus *bus;
632 	struct usb_fs_privdata *pd;
633 
634 	bus = ((struct usb_bus_msg *)pm)->bus;
635 
636 	while ((pd = LIST_FIRST(&bus->pd_cleanup_list)) != NULL) {
637 
638 		LIST_REMOVE(pd, pd_next);
639 		USB_BUS_UNLOCK(bus);
640 
641 		usb_destroy_dev_sync(pd);
642 
643 		USB_BUS_LOCK(bus);
644 	}
645 }
646 #endif
647 
648 static void
usb_power_wdog(void * arg)649 usb_power_wdog(void *arg)
650 {
651 	struct usb_bus *bus = arg;
652 
653 	USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
654 
655 	usb_callout_reset(&bus->power_wdog,
656 	    4 * hz, usb_power_wdog, arg);
657 
658 #ifdef DDB
659 	/*
660 	 * The following line of code is only here to recover from
661 	 * DDB:
662 	 */
663 	usb_proc_rewakeup(&bus->explore_proc);	/* recover from DDB */
664 #endif
665 
666 #if USB_HAVE_POWERD
667 	USB_BUS_UNLOCK(bus);
668 
669 	usb_bus_power_update(bus);
670 
671 	USB_BUS_LOCK(bus);
672 #endif
673 }
674 
675 /*------------------------------------------------------------------------*
676  *	usb_bus_attach
677  *
678  * This function attaches USB in context of the explore thread.
679  *------------------------------------------------------------------------*/
680 static void
usb_bus_attach(struct usb_proc_msg * pm)681 usb_bus_attach(struct usb_proc_msg *pm)
682 {
683 	struct usb_bus *bus;
684 	struct usb_device *child;
685 	device_t dev;
686 	usb_error_t err;
687 	enum usb_dev_speed speed;
688 
689 	bus = ((struct usb_bus_msg *)pm)->bus;
690 	dev = bus->bdev;
691 
692 	DPRINTF("\n");
693 
694 	switch (bus->usbrev) {
695 	case USB_REV_1_0:
696 		speed = USB_SPEED_FULL;
697 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
698 		break;
699 
700 	case USB_REV_1_1:
701 		speed = USB_SPEED_FULL;
702 		device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
703 		break;
704 
705 	case USB_REV_2_0:
706 		speed = USB_SPEED_HIGH;
707 		device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
708 		break;
709 
710 	case USB_REV_2_5:
711 		speed = USB_SPEED_VARIABLE;
712 		device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
713 		break;
714 
715 	case USB_REV_3_0:
716 		speed = USB_SPEED_SUPER;
717 		device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
718 		break;
719 
720 	default:
721 		device_printf(bus->bdev, "Unsupported USB revision\n");
722 		usb_root_mount_rel(bus);
723 		return;
724 	}
725 
726 	/* default power_mask value */
727 	bus->hw_power_state =
728 	  USB_HW_POWER_CONTROL |
729 	  USB_HW_POWER_BULK |
730 	  USB_HW_POWER_INTERRUPT |
731 	  USB_HW_POWER_ISOC |
732 	  USB_HW_POWER_NON_ROOT_HUB;
733 
734 	USB_BUS_UNLOCK(bus);
735 
736 	/* make sure power is set at least once */
737 
738 	if (bus->methods->set_hw_power != NULL) {
739 		(bus->methods->set_hw_power) (bus);
740 	}
741 
742 	/* allocate the Root USB device */
743 
744 	child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
745 	    speed, USB_MODE_HOST);
746 	if (child) {
747 		err = usb_probe_and_attach(child,
748 		    USB_IFACE_INDEX_ANY);
749 		if (!err) {
750 			if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
751 			    (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
752 				err = USB_ERR_NO_ROOT_HUB;
753 			}
754 		}
755 	} else {
756 		err = USB_ERR_NOMEM;
757 	}
758 
759 	USB_BUS_LOCK(bus);
760 
761 	if (err) {
762 		device_printf(bus->bdev, "Root HUB problem, error=%s\n",
763 		    usbd_errstr(err));
764 		usb_root_mount_rel(bus);
765 	}
766 
767 	/* set softc - we are ready */
768 	device_set_softc(dev, bus);
769 
770 	/* start watchdog */
771 	usb_power_wdog(bus);
772 }
773 
774 /*------------------------------------------------------------------------*
775  *	usb_attach_sub
776  *
777  * This function creates a thread which runs the USB attach code.
778  *------------------------------------------------------------------------*/
779 static void
usb_attach_sub(device_t dev,struct usb_bus * bus)780 usb_attach_sub(device_t dev, struct usb_bus *bus)
781 {
782 	const char *pname = device_get_nameunit(dev);
783 
784 	mtx_lock(&Giant);
785 	if (usb_devclass_ptr == NULL)
786 		usb_devclass_ptr = devclass_find("usbus");
787 	mtx_unlock(&Giant);
788 
789 #if USB_HAVE_PF
790 	usbpf_attach(bus);
791 #endif
792 	/* Initialise USB process messages */
793 	bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
794 	bus->explore_msg[0].bus = bus;
795 	bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
796 	bus->explore_msg[1].bus = bus;
797 
798 	bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
799 	bus->detach_msg[0].bus = bus;
800 	bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
801 	bus->detach_msg[1].bus = bus;
802 
803 	bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
804 	bus->attach_msg[0].bus = bus;
805 	bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
806 	bus->attach_msg[1].bus = bus;
807 
808 	bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
809 	bus->suspend_msg[0].bus = bus;
810 	bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
811 	bus->suspend_msg[1].bus = bus;
812 
813 	bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
814 	bus->resume_msg[0].bus = bus;
815 	bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
816 	bus->resume_msg[1].bus = bus;
817 
818 	bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
819 	bus->reset_msg[0].bus = bus;
820 	bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
821 	bus->reset_msg[1].bus = bus;
822 
823 	bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
824 	bus->shutdown_msg[0].bus = bus;
825 	bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
826 	bus->shutdown_msg[1].bus = bus;
827 
828 #if USB_HAVE_UGEN
829 	LIST_INIT(&bus->pd_cleanup_list);
830 	bus->cleanup_msg[0].hdr.pm_callback = &usb_bus_cleanup;
831 	bus->cleanup_msg[0].bus = bus;
832 	bus->cleanup_msg[1].hdr.pm_callback = &usb_bus_cleanup;
833 	bus->cleanup_msg[1].bus = bus;
834 #endif
835 
836 	/* Create USB explore and callback processes */
837 
838 	if (usb_proc_create(&bus->giant_callback_proc,
839 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
840 		device_printf(dev, "WARNING: Creation of USB Giant "
841 		    "callback process failed.\n");
842 	} else if (usb_proc_create(&bus->non_giant_callback_proc,
843 	    &bus->bus_mtx, pname, USB_PRI_HIGH)) {
844 		device_printf(dev, "WARNING: Creation of USB non-Giant "
845 		    "callback process failed.\n");
846 	} else if (usb_proc_create(&bus->explore_proc,
847 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
848 		device_printf(dev, "WARNING: Creation of USB explore "
849 		    "process failed.\n");
850 	} else if (usb_proc_create(&bus->control_xfer_proc,
851 	    &bus->bus_mtx, pname, USB_PRI_MED)) {
852 		device_printf(dev, "WARNING: Creation of USB control transfer "
853 		    "process failed.\n");
854 	} else {
855 		/* Get final attach going */
856 		USB_BUS_LOCK(bus);
857 		usb_proc_msignal(&bus->explore_proc,
858 		    &bus->attach_msg[0], &bus->attach_msg[1]);
859 		USB_BUS_UNLOCK(bus);
860 
861 		/* Do initial explore */
862 		usb_needs_explore(bus, 1);
863 	}
864 }
865 
866 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
867 
868 /*------------------------------------------------------------------------*
869  *	usb_bus_mem_flush_all_cb
870  *------------------------------------------------------------------------*/
871 #if USB_HAVE_BUSDMA
872 static void
usb_bus_mem_flush_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)873 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
874     struct usb_page *pg, usb_size_t size, usb_size_t align)
875 {
876 	usb_pc_cpu_flush(pc);
877 }
878 #endif
879 
880 /*------------------------------------------------------------------------*
881  *	usb_bus_mem_flush_all - factored out code
882  *------------------------------------------------------------------------*/
883 #if USB_HAVE_BUSDMA
884 void
usb_bus_mem_flush_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)885 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
886 {
887 	if (cb) {
888 		cb(bus, &usb_bus_mem_flush_all_cb);
889 	}
890 }
891 #endif
892 
893 /*------------------------------------------------------------------------*
894  *	usb_bus_mem_alloc_all_cb
895  *------------------------------------------------------------------------*/
896 #if USB_HAVE_BUSDMA
897 static void
usb_bus_mem_alloc_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)898 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
899     struct usb_page *pg, usb_size_t size, usb_size_t align)
900 {
901 	/* need to initialize the page cache */
902 	pc->tag_parent = bus->dma_parent_tag;
903 
904 	if (usb_pc_alloc_mem(pc, pg, size, align)) {
905 		bus->alloc_failed = 1;
906 	}
907 }
908 #endif
909 
910 /*------------------------------------------------------------------------*
911  *	usb_bus_mem_alloc_all - factored out code
912  *
913  * Returns:
914  *    0: Success
915  * Else: Failure
916  *------------------------------------------------------------------------*/
917 uint8_t
usb_bus_mem_alloc_all(struct usb_bus * bus,bus_dma_tag_t dmat,usb_bus_mem_cb_t * cb)918 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
919     usb_bus_mem_cb_t *cb)
920 {
921 	bus->alloc_failed = 0;
922 
923 	mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
924 	    NULL, MTX_DEF | MTX_RECURSE);
925 
926 	usb_callout_init_mtx(&bus->power_wdog,
927 	    &bus->bus_mtx, 0);
928 
929 	TAILQ_INIT(&bus->intr_q.head);
930 
931 #if USB_HAVE_BUSDMA
932 	usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
933 	    dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
934 #endif
935 	if ((bus->devices_max > USB_MAX_DEVICES) ||
936 	    (bus->devices_max < USB_MIN_DEVICES) ||
937 	    (bus->devices == NULL)) {
938 		DPRINTFN(0, "Devices field has not been "
939 		    "initialised properly\n");
940 		bus->alloc_failed = 1;		/* failure */
941 	}
942 #if USB_HAVE_BUSDMA
943 	if (cb) {
944 		cb(bus, &usb_bus_mem_alloc_all_cb);
945 	}
946 #endif
947 	if (bus->alloc_failed) {
948 		usb_bus_mem_free_all(bus, cb);
949 	}
950 	return (bus->alloc_failed);
951 }
952 
953 /*------------------------------------------------------------------------*
954  *	usb_bus_mem_free_all_cb
955  *------------------------------------------------------------------------*/
956 #if USB_HAVE_BUSDMA
957 static void
usb_bus_mem_free_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)958 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
959     struct usb_page *pg, usb_size_t size, usb_size_t align)
960 {
961 	usb_pc_free_mem(pc);
962 }
963 #endif
964 
965 /*------------------------------------------------------------------------*
966  *	usb_bus_mem_free_all - factored out code
967  *------------------------------------------------------------------------*/
968 void
usb_bus_mem_free_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)969 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
970 {
971 #if USB_HAVE_BUSDMA
972 	if (cb) {
973 		cb(bus, &usb_bus_mem_free_all_cb);
974 	}
975 	usb_dma_tag_unsetup(bus->dma_parent_tag);
976 #endif
977 
978 	mtx_destroy(&bus->bus_mtx);
979 }
980 
981 /* convenience wrappers */
982 void
usb_proc_explore_mwait(struct usb_device * udev,void * pm1,void * pm2)983 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
984 {
985 	usb_proc_mwait(&udev->bus->explore_proc, pm1, pm2);
986 }
987 
988 void	*
usb_proc_explore_msignal(struct usb_device * udev,void * pm1,void * pm2)989 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
990 {
991 	return (usb_proc_msignal(&udev->bus->explore_proc, pm1, pm2));
992 }
993 
994 void
usb_proc_explore_lock(struct usb_device * udev)995 usb_proc_explore_lock(struct usb_device *udev)
996 {
997 	USB_BUS_LOCK(udev->bus);
998 }
999 
1000 void
usb_proc_explore_unlock(struct usb_device * udev)1001 usb_proc_explore_unlock(struct usb_device *udev)
1002 {
1003 	USB_BUS_UNLOCK(udev->bus);
1004 }
1005