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