FreeBSD kernel usb device Code
usb_controller.c
Go to the documentation of this file.
1/* $FreeBSD$ */
2/*-
3 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 *
5 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29#ifdef USB_GLOBAL_INCLUDE_FILE
30#include USB_GLOBAL_INCLUDE_FILE
31#else
32#include "opt_ddb.h"
33
34#include <sys/stdint.h>
35#include <sys/stddef.h>
36#include <sys/param.h>
37#include <sys/queue.h>
38#include <sys/types.h>
39#include <sys/systm.h>
40#include <sys/kernel.h>
41#include <sys/bus.h>
42#include <sys/module.h>
43#include <sys/lock.h>
44#include <sys/mutex.h>
45#include <sys/condvar.h>
46#include <sys/sysctl.h>
47#include <sys/sx.h>
48#include <sys/unistd.h>
49#include <sys/callout.h>
50#include <sys/malloc.h>
51#include <sys/priv.h>
52
53#include <dev/usb/usb.h>
54#include <dev/usb/usbdi.h>
55
56#define USB_DEBUG_VAR usb_ctrl_debug
57
58#include <dev/usb/usb_core.h>
59#include <dev/usb/usb_debug.h>
60#include <dev/usb/usb_process.h>
61#include <dev/usb/usb_busdma.h>
62#include <dev/usb/usb_dynamic.h>
63#include <dev/usb/usb_device.h>
64#include <dev/usb/usb_dev.h>
65#include <dev/usb/usb_hub.h>
66
68#include <dev/usb/usb_bus.h>
69#include <dev/usb/usb_pf.h>
70#include "usb_if.h"
71#endif /* USB_GLOBAL_INCLUDE_FILE */
72
73/* function prototypes */
74
75static device_probe_t usb_probe;
76static device_attach_t usb_attach;
77static device_detach_t usb_detach;
78static device_suspend_t usb_suspend;
79static device_resume_t usb_resume;
80static device_shutdown_t usb_shutdown;
81
82static void usb_attach_sub(device_t, struct usb_bus *);
83
84/* static variables */
85
86#ifdef USB_DEBUG
87static int usb_ctrl_debug = 0;
88
89static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
90 "USB controller");
91SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RWTUN, &usb_ctrl_debug, 0,
92 "Debug level");
93#endif
94
95#if USB_HAVE_ROOT_MOUNT_HOLD
96static int usb_no_boot_wait = 0;
97SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
98 "No USB device enumerate waiting at boot.");
99#endif
100
101static int usb_no_suspend_wait = 0;
102SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RWTUN,
103 &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
104
105static int usb_no_shutdown_wait = 0;
106SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RWTUN,
107 &usb_no_shutdown_wait, 0, "No USB device waiting at system shutdown.");
108
109static devclass_t usb_devclass;
110
111static 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
122static driver_t usb_driver = {
123 .name = "usbus",
124 .methods = usb_methods,
125 .size = 0,
126};
127
128/* Host Only Drivers */
133
134/* Device Only Drivers */
135DRIVER_MODULE(usbus, musbotg, usb_driver, usb_devclass, 0, 0);
136DRIVER_MODULE(usbus, uss820dci, usb_driver, usb_devclass, 0, 0);
137DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
138
139/* Dual Mode Drivers */
140DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0);
141DRIVER_MODULE(usbus, saf1761otg, usb_driver, usb_devclass, 0, 0);
142
143/*------------------------------------------------------------------------*
144 * usb_probe
145 *
146 * This function is called from "{ehci,ohci,uhci}_pci_attach()".
147 *------------------------------------------------------------------------*/
148static int
149usb_probe(device_t dev)
150{
151 DPRINTF("\n");
152 return (0);
153}
154
155#if USB_HAVE_ROOT_MOUNT_HOLD
156static void
157usb_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 *------------------------------------------------------------------------*/
170static int
171usb_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
189
190 return (0); /* return success */
191}
192
193/*------------------------------------------------------------------------*
194 * usb_detach
195 *------------------------------------------------------------------------*/
196static int
197usb_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
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
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_ISOC_PROC(bus));
237 usb_proc_free(USB_BUS_NON_GIANT_BULK_PROC(bus));
238
239 /* Get rid of USB explore process */
240
241 usb_proc_free(USB_BUS_EXPLORE_PROC(bus));
242
243 /* Get rid of control transfer process */
244
245 usb_proc_free(USB_BUS_CONTROL_XFER_PROC(bus));
246#endif
247
248#if USB_HAVE_PF
250#endif
251 return (0);
252}
253
254/*------------------------------------------------------------------------*
255 * usb_suspend
256 *------------------------------------------------------------------------*/
257static int
258usb_suspend(device_t dev)
259{
260 struct usb_bus *bus = device_get_softc(dev);
261
262 DPRINTF("\n");
263
264 if (bus == NULL) {
265 /* was never setup properly */
266 return (0);
267 }
268
270 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
271 &bus->suspend_msg[0], &bus->suspend_msg[1]);
272 if (usb_no_suspend_wait == 0) {
273 /* wait for suspend callback to be executed */
274 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
275 &bus->suspend_msg[0], &bus->suspend_msg[1]);
276 }
278
279 return (0);
280}
281
282/*------------------------------------------------------------------------*
283 * usb_resume
284 *------------------------------------------------------------------------*/
285static int
286usb_resume(device_t dev)
287{
288 struct usb_bus *bus = device_get_softc(dev);
289
290 DPRINTF("\n");
291
292 if (bus == NULL) {
293 /* was never setup properly */
294 return (0);
295 }
296
298 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
299 &bus->resume_msg[0], &bus->resume_msg[1]);
301
302 return (0);
303}
304
305/*------------------------------------------------------------------------*
306 * usb_bus_reset_async_locked
307 *------------------------------------------------------------------------*/
308void
310{
311 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
312
313 DPRINTF("\n");
314
315 if (bus->reset_msg[0].hdr.pm_qentry.tqe_prev != NULL ||
316 bus->reset_msg[1].hdr.pm_qentry.tqe_prev != NULL) {
317 DPRINTF("Reset already pending\n");
318 return;
319 }
320
321 device_printf(bus->parent, "Resetting controller\n");
322
323 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
324 &bus->reset_msg[0], &bus->reset_msg[1]);
325}
326
327/*------------------------------------------------------------------------*
328 * usb_shutdown
329 *------------------------------------------------------------------------*/
330static int
331usb_shutdown(device_t dev)
332{
333 struct usb_bus *bus = device_get_softc(dev);
334
335 DPRINTF("\n");
336
337 if (bus == NULL) {
338 /* was never setup properly */
339 return (0);
340 }
341
342 DPRINTF("%s: Controller shutdown\n", device_get_nameunit(bus->bdev));
343
345 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
346 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
347 if (usb_no_shutdown_wait == 0) {
348 /* wait for shutdown callback to be executed */
349 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
350 &bus->shutdown_msg[0], &bus->shutdown_msg[1]);
351 }
353
354 DPRINTF("%s: Controller shutdown complete\n",
355 device_get_nameunit(bus->bdev));
356
357 return (0);
358}
359
360/*------------------------------------------------------------------------*
361 * usb_bus_explore
362 *
363 * This function is used to explore the device tree from the root.
364 *------------------------------------------------------------------------*/
365static void
367{
368 struct usb_bus *bus;
369 struct usb_device *udev;
370
371 bus = ((struct usb_bus_msg *)pm)->bus;
373
374 if (bus->no_explore != 0)
375 return;
376
377 if (udev != NULL) {
381 }
382
383 if (udev != NULL && udev->hub != NULL) {
384 if (bus->do_probe) {
385 bus->do_probe = 0;
387 }
388 if (bus->driver_added_refcount == 0) {
389 /* avoid zero, hence that is memory default */
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_ISOC_PROC(bus));
401 usb_proc_rewakeup(USB_BUS_NON_GIANT_BULK_PROC(bus));
402#endif
403
405
406#if USB_HAVE_POWERD
407 /*
408 * First update the USB power state!
409 */
411#endif
412 /* Explore the Root USB HUB. */
413 (udev->hub->explore) (udev);
415 }
416#if USB_HAVE_ROOT_MOUNT_HOLD
417 usb_root_mount_rel(bus);
418#endif
419}
420
421/*------------------------------------------------------------------------*
422 * usb_bus_detach
423 *
424 * This function is used to detach the device tree from the root.
425 *------------------------------------------------------------------------*/
426static void
428{
429 struct usb_bus *bus;
430 struct usb_device *udev;
431 device_t dev;
432
433 bus = ((struct usb_bus_msg *)pm)->bus;
435 dev = bus->bdev;
436 /* clear the softc */
437 device_set_softc(dev, NULL);
439
440 /* detach children first */
441 bus_topo_lock();
442 bus_generic_detach(dev);
443 bus_topo_unlock();
444
445 /*
446 * Free USB device and all subdevices, if any.
447 */
448 usb_free_device(udev, 0);
449
451 /* clear bdev variable last */
452 bus->bdev = NULL;
453}
454
455/*------------------------------------------------------------------------*
456 * usb_bus_suspend
457 *
458 * This function is used to suspend the USB controller.
459 *------------------------------------------------------------------------*/
460static void
462{
463 struct usb_bus *bus;
464 struct usb_device *udev;
465 usb_error_t err;
466 uint8_t do_unlock;
467
468 DPRINTF("\n");
469
470 bus = ((struct usb_bus_msg *)pm)->bus;
472
473 if (udev == NULL || bus->bdev == NULL)
474 return;
475
477
478 /*
479 * We use the shutdown event here because the suspend and
480 * resume events are reserved for the USB port suspend and
481 * resume. The USB system suspend is implemented like full
482 * shutdown and all connected USB devices will be disconnected
483 * subsequently. At resume all USB devices will be
484 * re-connected again.
485 */
486
487 bus_generic_shutdown(bus->bdev);
488
489 do_unlock = usbd_enum_lock(udev);
490
492 if (err)
493 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
494
496 bus->hw_power_state = 0;
497 bus->no_explore = 1;
499
500 if (bus->methods->set_hw_power != NULL)
502
503 if (bus->methods->set_hw_power_sleep != NULL)
505
506 if (do_unlock)
507 usbd_enum_unlock(udev);
508
510}
511
512/*------------------------------------------------------------------------*
513 * usb_bus_resume
514 *
515 * This function is used to resume the USB controller.
516 *------------------------------------------------------------------------*/
517static void
519{
520 struct usb_bus *bus;
521 struct usb_device *udev;
522 usb_error_t err;
523 uint8_t do_unlock;
524
525 DPRINTF("\n");
526
527 bus = ((struct usb_bus_msg *)pm)->bus;
529
530 if (udev == NULL || bus->bdev == NULL)
531 return;
532
534
535 do_unlock = usbd_enum_lock(udev);
536#if 0
537 DEVMETHOD(usb_take_controller, NULL); /* dummy */
538#endif
539 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
540
548 bus->no_explore = 0;
550
551 if (bus->methods->set_hw_power_sleep != NULL)
553
554 if (bus->methods->set_hw_power != NULL)
556
557 /* restore USB configuration to index 0 */
558 err = usbd_set_config_index(udev, 0);
559 if (err)
560 device_printf(bus->bdev, "Could not configure root HUB\n");
561
562 /* probe and attach */
564 if (err) {
565 device_printf(bus->bdev, "Could not probe and "
566 "attach root HUB\n");
567 }
568
569 if (do_unlock)
570 usbd_enum_unlock(udev);
571
573}
574
575/*------------------------------------------------------------------------*
576 * usb_bus_reset
577 *
578 * This function is used to reset the USB controller.
579 *------------------------------------------------------------------------*/
580static void
582{
583 struct usb_bus *bus;
584
585 DPRINTF("\n");
586
587 bus = ((struct usb_bus_msg *)pm)->bus;
588
589 if (bus->bdev == NULL || bus->no_explore != 0)
590 return;
591
592 /* a suspend and resume will reset the USB controller */
593 usb_bus_suspend(pm);
594 usb_bus_resume(pm);
595}
596
597/*------------------------------------------------------------------------*
598 * usb_bus_shutdown
599 *
600 * This function is used to shutdown the USB controller.
601 *------------------------------------------------------------------------*/
602static void
604{
605 struct usb_bus *bus;
606 struct usb_device *udev;
607 usb_error_t err;
608 uint8_t do_unlock;
609
610 bus = ((struct usb_bus_msg *)pm)->bus;
612
613 if (udev == NULL || bus->bdev == NULL)
614 return;
615
617
618 bus_generic_shutdown(bus->bdev);
619
620 do_unlock = usbd_enum_lock(udev);
621
623 if (err)
624 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
625
627 bus->hw_power_state = 0;
628 bus->no_explore = 1;
630
631 if (bus->methods->set_hw_power != NULL)
633
634 if (bus->methods->set_hw_power_sleep != NULL)
636
637 if (do_unlock)
638 usbd_enum_unlock(udev);
639
641}
642
643/*------------------------------------------------------------------------*
644 * usb_bus_cleanup
645 *
646 * This function is used to cleanup leftover USB character devices.
647 *------------------------------------------------------------------------*/
648#if USB_HAVE_UGEN
649static void
650usb_bus_cleanup(struct usb_proc_msg *pm)
651{
652 struct usb_bus *bus;
653 struct usb_fs_privdata *pd;
654
655 bus = ((struct usb_bus_msg *)pm)->bus;
656
657 while ((pd = LIST_FIRST(&bus->pd_cleanup_list)) != NULL) {
658 LIST_REMOVE(pd, pd_next);
660
661 usb_destroy_dev_sync(pd);
662
664 }
665}
666#endif
667
668static void
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
688
690
692#endif
693}
694
695/*------------------------------------------------------------------------*
696 * usb_bus_attach
697 *
698 * This function attaches USB in context of the explore thread.
699 *------------------------------------------------------------------------*/
700static void
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 */
755
757
758 /* make sure power is set at least once */
759
760 if (bus->methods->set_hw_power != NULL) {
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) {
771 if (!err) {
772 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
773 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
775 }
776 }
777 } else {
778 err = USB_ERR_NOMEM;
779 }
780
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 */
796}
797
798/*------------------------------------------------------------------------*
799 * usb_attach_sub
800 *
801 * This function creates a thread which runs the USB attach code.
802 *------------------------------------------------------------------------*/
803static void
804usb_attach_sub(device_t dev, struct usb_bus *bus)
805{
806 bus_topo_lock();
807 if (usb_devclass_ptr == NULL)
808 usb_devclass_ptr = devclass_find("usbus");
809 bus_topo_unlock();
810
811#if USB_HAVE_PF
813#endif
814 /* Initialise USB process messages */
816 bus->explore_msg[0].bus = bus;
818 bus->explore_msg[1].bus = bus;
819
821 bus->detach_msg[0].bus = bus;
823 bus->detach_msg[1].bus = bus;
824
826 bus->attach_msg[0].bus = bus;
828 bus->attach_msg[1].bus = bus;
829
831 bus->suspend_msg[0].bus = bus;
833 bus->suspend_msg[1].bus = bus;
834
836 bus->resume_msg[0].bus = bus;
838 bus->resume_msg[1].bus = bus;
839
841 bus->reset_msg[0].bus = bus;
843 bus->reset_msg[1].bus = bus;
844
846 bus->shutdown_msg[0].bus = bus;
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_ISOC_PROC(bus),
866 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGHEST)) {
867 device_printf(dev, "WARNING: Creation of USB non-Giant ISOC "
868 "callback process failed.\n");
869 } else if (usb_proc_create(USB_BUS_NON_GIANT_BULK_PROC(bus),
870 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) {
871 device_printf(dev, "WARNING: Creation of USB non-Giant BULK "
872 "callback process failed.\n");
873 } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
874 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
875 device_printf(dev, "WARNING: Creation of USB explore "
876 "process failed.\n");
877 } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
878 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
879 device_printf(dev, "WARNING: Creation of USB control transfer "
880 "process failed.\n");
881 } else
882#endif
883 {
884 /* Get final attach going */
886 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
887 &bus->attach_msg[0], &bus->attach_msg[1]);
889
890 /* Do initial explore */
892 }
893}
894SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
895
896/*------------------------------------------------------------------------*
897 * usb_bus_mem_flush_all_cb
898 *------------------------------------------------------------------------*/
899#if USB_HAVE_BUSDMA
900static void
901usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
902 struct usb_page *pg, usb_size_t size, usb_size_t align)
903{
905}
906#endif
907
908/*------------------------------------------------------------------------*
909 * usb_bus_mem_flush_all - factored out code
910 *------------------------------------------------------------------------*/
911#if USB_HAVE_BUSDMA
912void
914{
915 if (cb) {
916 cb(bus, &usb_bus_mem_flush_all_cb);
917 }
918}
919#endif
920
921/*------------------------------------------------------------------------*
922 * usb_bus_mem_alloc_all_cb
923 *------------------------------------------------------------------------*/
924#if USB_HAVE_BUSDMA
925static void
926usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
927 struct usb_page *pg, usb_size_t size, usb_size_t align)
928{
929 /* need to initialize the page cache */
930 pc->tag_parent = bus->dma_parent_tag;
931
932 if (usb_pc_alloc_mem(pc, pg, size, align)) {
933 bus->alloc_failed = 1;
934 }
935}
936#endif
937
938/*------------------------------------------------------------------------*
939 * usb_bus_mem_alloc_all - factored out code
940 *
941 * Returns:
942 * 0: Success
943 * Else: Failure
944 *------------------------------------------------------------------------*/
945uint8_t
946usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
948{
949 bus->alloc_failed = 0;
950
951 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
952 "usb_def_mtx", MTX_DEF | MTX_RECURSE);
953
954 mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent),
955 "usb_spin_mtx", MTX_SPIN | MTX_RECURSE);
956
958 &bus->bus_mtx, 0);
959
960 TAILQ_INIT(&bus->intr_q.head);
961
962#if USB_HAVE_BUSDMA
963 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
964 dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
965#endif
968 (bus->devices == NULL)) {
969 DPRINTFN(0, "Devices field has not been "
970 "initialised properly\n");
971 bus->alloc_failed = 1; /* failure */
972 }
973#if USB_HAVE_BUSDMA
974 if (cb) {
975 cb(bus, &usb_bus_mem_alloc_all_cb);
976 }
977#endif
978 if (bus->alloc_failed) {
980 }
981 return (bus->alloc_failed);
982}
983
984/*------------------------------------------------------------------------*
985 * usb_bus_mem_free_all_cb
986 *------------------------------------------------------------------------*/
987#if USB_HAVE_BUSDMA
988static void
989usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
990 struct usb_page *pg, usb_size_t size, usb_size_t align)
991{
992 usb_pc_free_mem(pc);
993}
994#endif
995
996/*------------------------------------------------------------------------*
997 * usb_bus_mem_free_all - factored out code
998 *------------------------------------------------------------------------*/
999void
1001{
1002#if USB_HAVE_BUSDMA
1003 if (cb) {
1004 cb(bus, &usb_bus_mem_free_all_cb);
1005 }
1006 usb_dma_tag_unsetup(bus->dma_parent_tag);
1007#endif
1008
1009 mtx_destroy(&bus->bus_mtx);
1010 mtx_destroy(&bus->bus_spin_lock);
1011}
1012
1013/* convenience wrappers */
1014void
1015usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
1016{
1017 usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
1018}
1019
1020void *
1021usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
1022{
1023 return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
1024}
1025
1026void
1028{
1029 USB_BUS_LOCK(udev->bus);
1030}
1031
1032void
1034{
1035 USB_BUS_UNLOCK(udev->bus);
1036}
static int debug
Definition: cfumass.c:73
static SYSCTL_NODE(_hw_usb, OID_AUTO, dwc_otg, CTLFLAG_RW|CTLFLAG_MPSAFE, 0, "USB DWC OTG")
uint8_t ctrl
Definition: if_axge.c:86
uint8_t size
Definition: if_axge.c:89
device_t child
u_int bus
device_t dev
void(* set_hw_power)(struct usb_bus *)
void(* set_hw_power_sleep)(struct usb_bus *, uint32_t)
struct usb_bus * bus
Definition: usb_bus.h:41
struct usb_proc_msg hdr
Definition: usb_bus.h:40
struct usb_bus_msg shutdown_msg[2]
Definition: usb_bus.h:87
struct usb_bus_msg suspend_msg[2]
Definition: usb_bus.h:84
struct usb_bus_msg resume_msg[2]
Definition: usb_bus.h:85
enum usb_revision usbrev
Definition: usb_bus.h:119
struct mtx bus_spin_lock
Definition: usb_bus.h:96
struct usb_callout power_wdog
Definition: usb_bus.h:98
uint8_t driver_added_refcount
Definition: usb_bus.h:118
uint8_t do_probe
Definition: usb_bus.h:122
struct usb_bus_msg reset_msg[2]
Definition: usb_bus.h:86
device_t bdev
Definition: usb_bus.h:101
usb_power_mask_t hw_power_state
Definition: usb_bus.h:112
struct mtx bus_mtx
Definition: usb_bus.h:95
device_t parent
Definition: usb_bus.h:100
const struct usb_bus_methods * methods
Definition: usb_bus.h:107
struct usb_device ** devices
Definition: usb_bus.h:108
struct usb_bus_msg explore_msg[2]
Definition: usb_bus.h:81
uint8_t alloc_failed
Definition: usb_bus.h:117
uint8_t devices_max
Definition: usb_bus.h:121
uint8_t no_explore
Definition: usb_bus.h:123
struct usb_bus_msg attach_msg[2]
Definition: usb_bus.h:83
uint8_t dma_bits
Definition: usb_bus.h:124
struct usb_bus_msg detach_msg[2]
Definition: usb_bus.h:82
struct usb_xfer_queue intr_q
Definition: usb_bus.h:97
enum usb_dev_speed speed
Definition: usb_device.h:235
struct usb_hub * hub
Definition: usb_device.h:221
struct usb_bus * bus
Definition: usb_device.h:216
usb_error_t(* explore)(struct usb_device *hub)
Definition: usb_hub.h:50
struct usb_dma_parent_tag * tag_parent
Definition: usb_busdma.h:95
usb_proc_callback_t * pm_callback
Definition: usbdi.h:522
#define DPRINTF(...)
Definition: umass.c:179
#define USB_UNCONFIG_INDEX
Definition: usb.h:75
#define USB_ROOT_HUB_ADDR
Definition: usb.h:73
#define USB_IFACE_INDEX_ANY
Definition: usb.h:76
usb_dev_speed
Definition: usb.h:751
@ USB_SPEED_FULL
Definition: usb.h:754
@ USB_SPEED_VARIABLE
Definition: usb.h:752
@ USB_SPEED_HIGH
Definition: usb.h:755
@ USB_SPEED_SUPER
Definition: usb.h:756
@ USB_MODE_HOST
Definition: usb.h:778
#define USB_MIN_DEVICES
Definition: usb.h:74
@ USB_REV_2_0
Definition: usb.h:768
@ USB_REV_1_0
Definition: usb.h:766
@ USB_REV_3_0
Definition: usb.h:770
@ USB_REV_2_5
Definition: usb.h:769
@ USB_REV_1_1
Definition: usb.h:767
void usb_pc_free_mem(struct usb_page_cache *pc)
void usb_dma_tag_setup(struct usb_dma_parent_tag *udpt, struct usb_dma_tag *udt, bus_dma_tag_t dmat, struct mtx *mtx, usb_dma_callback_t *func, uint8_t ndmabits, uint8_t nudt)
void usb_dma_tag_unsetup(struct usb_dma_parent_tag *udpt)
void usb_pc_cpu_flush(struct usb_page_cache *pc)
uint8_t usb_pc_alloc_mem(struct usb_page_cache *pc, struct usb_page *pg, usb_size_t size, usb_size_t align)
static int usb_no_shutdown_wait
static device_shutdown_t usb_shutdown
void usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
static void usb_bus_suspend(struct usb_proc_msg *pm)
static device_probe_t usb_probe
void * usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
static devclass_t usb_devclass
static device_method_t usb_methods[]
SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RWTUN, &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.")
static int usb_no_suspend_wait
static void usb_attach_sub(device_t, struct usb_bus *)
uint8_t usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat, usb_bus_mem_cb_t *cb)
static device_suspend_t usb_suspend
static device_resume_t usb_resume
void usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
static device_attach_t usb_attach
static void usb_bus_reset(struct usb_proc_msg *pm)
static void usb_bus_detach(struct usb_proc_msg *pm)
static void usb_bus_shutdown(struct usb_proc_msg *pm)
void usb_proc_explore_unlock(struct usb_device *udev)
static void usb_bus_resume(struct usb_proc_msg *pm)
static void usb_bus_explore(struct usb_proc_msg *pm)
void usb_proc_explore_lock(struct usb_device *udev)
static device_detach_t usb_detach
SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL)
static driver_t usb_driver
DRIVER_MODULE(usbus, ohci, usb_driver, usb_devclass, 0, 0)
static void usb_power_wdog(void *arg)
static void usb_bus_attach(struct usb_proc_msg *pm)
void usb_bus_reset_async_locked(struct usb_bus *bus)
#define USB_HW_POWER_RESUME
void usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
#define USB_BUS_DMA_TAG_MAX
#define USB_HW_POWER_BULK
#define USB_HW_POWER_NON_ROOT_HUB
#define USB_HW_POWER_CONTROL
#define USB_HW_POWER_SUSPEND
void() usb_bus_mem_cb_t(struct usb_bus *bus, usb_bus_mem_sub_cb_t *scb)
#define USB_HW_POWER_SHUTDOWN
#define USB_HW_POWER_ISOC
#define USB_HW_POWER_INTERRUPT
#define USB_BUS_LOCK(_b)
Definition: usb_core.h:45
#define USB_BUS_UNLOCK(_b)
Definition: usb_core.h:46
#define USB_BUS_LOCK_ASSERT(_b, _t)
Definition: usb_core.h:47
uint8_t usbd_enum_lock(struct usb_device *udev)
Definition: usb_device.c:2896
usb_error_t usb_probe_and_attach(struct usb_device *udev, uint8_t iface_index)
Definition: usb_device.c:1441
usb_error_t usbd_set_config_index(struct usb_device *udev, uint8_t index)
Definition: usb_device.c:672
void usb_free_device(struct usb_device *udev, uint8_t flag)
Definition: usb_device.c:2271
void usbd_enum_unlock(struct usb_device *udev)
Definition: usb_device.c:2936
struct usb_device * usb_alloc_device(device_t parent_dev, struct usb_bus *bus, struct usb_device *parent_hub, uint8_t depth, uint8_t port_index, uint8_t port_no, enum usb_dev_speed speed, enum usb_hc_mode mode)
Definition: usb_device.c:1737
devclass_t usb_devclass_ptr
Definition: usb_dynamic.c:80
void usb_bus_unload(void *arg)
Definition: usb_dynamic.c:154
const char * usbd_errstr(usb_error_t err)
Definition: usb_error.c:93
#define USB_MAX_DEVICES
Definition: usb_freebsd.h:79
uint32_t usb_size_t
Definition: usb_freebsd.h:102
void usb_needs_explore(struct usb_bus *bus, uint8_t do_probe)
Definition: usb_hub.c:2255
void uhub_explore_handle_re_enumerate(struct usb_device *child)
Definition: usb_hub.c:417
void usb_bus_powerd(struct usb_bus *bus)
void usb_bus_power_update(struct usb_bus *bus)
void usbpf_attach(struct usb_bus *ubus)
Definition: usb_pf.c:242
void usbpf_detach(struct usb_bus *ubus)
Definition: usb_pf.c:250
int usb_proc_create(struct usb_process *up, struct mtx *p_mtx, const char *pmesg, uint8_t prio)
Definition: usb_process.c:224
void usb_proc_mwait(struct usb_process *up, void *_pm0, void *_pm1)
Definition: usb_process.c:394
void * usb_proc_msignal(struct usb_process *up, void *_pm0, void *_pm1)
Definition: usb_process.c:288
void usb_proc_rewakeup(struct usb_process *up)
Definition: usb_process.c:490
void usb_proc_free(struct usb_process *up)
Definition: usb_process.c:261
#define USB_PRI_MED
Definition: usb_process.h:41
#define USB_PRI_HIGHEST
Definition: usb_process.h:39
#define USB_PRI_HIGH
Definition: usb_process.h:40
#define usb_callout_init_mtx(c, m, f)
Definition: usbdi.h:480
#define usb_callout_reset(c,...)
Definition: usbdi.h:481
usb_error_t
Definition: usbdi.h:45
@ USB_ERR_NO_ROOT_HUB
Definition: usbdi.h:72
@ USB_ERR_NOMEM
Definition: usbdi.h:50
#define usb_callout_drain(c)
Definition: usbdi.h:497