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