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