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