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