1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
16 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
21 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #ifdef USB_GLOBAL_INCLUDE_FILE
29 #include USB_GLOBAL_INCLUDE_FILE
30 #else
31 #include "opt_ddb.h"
32
33 #include <sys/stdint.h>
34 #include <sys/stddef.h>
35 #include <sys/param.h>
36 #include <sys/queue.h>
37 #include <sys/types.h>
38 #include <sys/systm.h>
39 #include <sys/kernel.h>
40 #include <sys/bus.h>
41 #include <sys/module.h>
42 #include <sys/lock.h>
43 #include <sys/mutex.h>
44 #include <sys/condvar.h>
45 #include <sys/sysctl.h>
46 #include <sys/sx.h>
47 #include <sys/unistd.h>
48 #include <sys/callout.h>
49 #include <sys/malloc.h>
50 #include <sys/priv.h>
51
52 #include <dev/usb/usb.h>
53 #include <dev/usb/usbdi.h>
54
55 #define USB_DEBUG_VAR usb_ctrl_debug
56
57 #include <dev/usb/usb_core.h>
58 #include <dev/usb/usb_debug.h>
59 #include <dev/usb/usb_process.h>
60 #include <dev/usb/usb_busdma.h>
61 #include <dev/usb/usb_dynamic.h>
62 #include <dev/usb/usb_device.h>
63 #include <dev/usb/usb_dev.h>
64 #include <dev/usb/usb_hub.h>
65
66 #include <dev/usb/usb_controller.h>
67 #include <dev/usb/usb_bus.h>
68 #include <dev/usb/usb_pf.h>
69 #include "usb_if.h"
70 #endif /* USB_GLOBAL_INCLUDE_FILE */
71
72 /* function prototypes */
73
74 static device_probe_t usb_probe;
75 static device_attach_t usb_attach;
76 static device_detach_t usb_detach;
77 static device_suspend_t usb_suspend;
78 static device_resume_t usb_resume;
79 static device_shutdown_t usb_shutdown;
80
81 static void usb_attach_sub(device_t, struct usb_bus *);
82
83 /* static variables */
84
85 #ifdef USB_DEBUG
86 static int usb_ctrl_debug = 0;
87
88 static SYSCTL_NODE(_hw_usb, OID_AUTO, ctrl, CTLFLAG_RW | CTLFLAG_MPSAFE, 0,
89 "USB controller");
90 SYSCTL_INT(_hw_usb_ctrl, OID_AUTO, debug, CTLFLAG_RWTUN, &usb_ctrl_debug, 0,
91 "Debug level");
92 #endif
93
94 #if USB_HAVE_ROOT_MOUNT_HOLD
95 static int usb_no_boot_wait = 0;
96 SYSCTL_INT(_hw_usb, OID_AUTO, no_boot_wait, CTLFLAG_RDTUN, &usb_no_boot_wait, 0,
97 "No USB device enumerate waiting at boot.");
98 #endif
99
100 static int usb_no_suspend_wait = 0;
101 SYSCTL_INT(_hw_usb, OID_AUTO, no_suspend_wait, CTLFLAG_RWTUN,
102 &usb_no_suspend_wait, 0, "No USB device waiting at system suspend.");
103
104 static int usb_no_shutdown_wait = 0;
105 SYSCTL_INT(_hw_usb, OID_AUTO, no_shutdown_wait, CTLFLAG_RWTUN,
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, musbotg, usb_driver, usb_devclass, 0, 0);
135 DRIVER_MODULE(usbus, uss820dci, usb_driver, usb_devclass, 0, 0);
136 DRIVER_MODULE(usbus, octusb, usb_driver, usb_devclass, 0, 0);
137
138 /* Dual Mode Drivers */
139 DRIVER_MODULE(usbus, dwcotg, usb_driver, usb_devclass, 0, 0);
140 DRIVER_MODULE(usbus, saf1761otg, 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
usb_probe(device_t dev)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
usb_root_mount_rel(struct usb_bus * bus)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
usb_attach(device_t dev)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
usb_detach(device_t dev)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 #if USB_HAVE_UGEN
225 /* Wait for cleanup to complete */
226 usb_proc_mwait(USB_BUS_EXPLORE_PROC(bus),
227 &bus->cleanup_msg[0], &bus->cleanup_msg[1]);
228 #endif
229 USB_BUS_UNLOCK(bus);
230
231 #if USB_HAVE_PER_BUS_PROCESS
232 /* Get rid of USB callback processes */
233
234 usb_proc_free(USB_BUS_GIANT_PROC(bus));
235 usb_proc_free(USB_BUS_NON_GIANT_ISOC_PROC(bus));
236 usb_proc_free(USB_BUS_NON_GIANT_BULK_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
usb_suspend(device_t dev)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
usb_resume(device_t dev)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
usb_bus_reset_async_locked(struct usb_bus * bus)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
usb_shutdown(device_t dev)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
usb_bus_explore(struct usb_proc_msg * pm)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 if (bus->do_probe) {
384 bus->do_probe = 0;
385 bus->driver_added_refcount++;
386 }
387 if (bus->driver_added_refcount == 0) {
388 /* avoid zero, hence that is memory default */
389 bus->driver_added_refcount = 1;
390 }
391
392 #ifdef DDB
393 /*
394 * The following three lines of code are only here to
395 * recover from DDB:
396 */
397 usb_proc_rewakeup(USB_BUS_CONTROL_XFER_PROC(bus));
398 usb_proc_rewakeup(USB_BUS_GIANT_PROC(bus));
399 usb_proc_rewakeup(USB_BUS_NON_GIANT_ISOC_PROC(bus));
400 usb_proc_rewakeup(USB_BUS_NON_GIANT_BULK_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 /* Nice the enumeration a bit, to avoid looping too fast. */
420 usb_pause_mtx(&bus->bus_mtx, USB_MS_TO_TICKS(usb_enum_nice_time));
421 }
422
423 /*------------------------------------------------------------------------*
424 * usb_bus_detach
425 *
426 * This function is used to detach the device tree from the root.
427 *------------------------------------------------------------------------*/
428 static void
usb_bus_detach(struct usb_proc_msg * pm)429 usb_bus_detach(struct usb_proc_msg *pm)
430 {
431 struct usb_bus *bus;
432 struct usb_device *udev;
433 device_t dev;
434
435 bus = ((struct usb_bus_msg *)pm)->bus;
436 udev = bus->devices[USB_ROOT_HUB_ADDR];
437 dev = bus->bdev;
438 /* clear the softc */
439 device_set_softc(dev, NULL);
440 USB_BUS_UNLOCK(bus);
441
442 /* detach children first */
443 bus_topo_lock();
444 bus_generic_detach(dev);
445 bus_topo_unlock();
446
447 /*
448 * Free USB device and all subdevices, if any.
449 */
450 usb_free_device(udev, 0);
451
452 USB_BUS_LOCK(bus);
453 /* clear bdev variable last */
454 bus->bdev = NULL;
455 }
456
457 /*------------------------------------------------------------------------*
458 * usb_bus_suspend
459 *
460 * This function is used to suspend the USB controller.
461 *------------------------------------------------------------------------*/
462 static void
usb_bus_suspend(struct usb_proc_msg * pm)463 usb_bus_suspend(struct usb_proc_msg *pm)
464 {
465 struct usb_bus *bus;
466 struct usb_device *udev;
467 usb_error_t err;
468 uint8_t do_unlock;
469
470 DPRINTF("\n");
471
472 bus = ((struct usb_bus_msg *)pm)->bus;
473 udev = bus->devices[USB_ROOT_HUB_ADDR];
474
475 if (udev == NULL || bus->bdev == NULL)
476 return;
477
478 USB_BUS_UNLOCK(bus);
479
480 /*
481 * We use the shutdown event here because the suspend and
482 * resume events are reserved for the USB port suspend and
483 * resume. The USB system suspend is implemented like full
484 * shutdown and all connected USB devices will be disconnected
485 * subsequently. At resume all USB devices will be
486 * re-connected again.
487 */
488
489 bus_generic_shutdown(bus->bdev);
490
491 do_unlock = usbd_enum_lock(udev);
492
493 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
494 if (err)
495 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
496
497 USB_BUS_LOCK(bus);
498 bus->hw_power_state = 0;
499 bus->no_explore = 1;
500 USB_BUS_UNLOCK(bus);
501
502 if (bus->methods->set_hw_power != NULL)
503 (bus->methods->set_hw_power) (bus);
504
505 if (bus->methods->set_hw_power_sleep != NULL)
506 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SUSPEND);
507
508 if (do_unlock)
509 usbd_enum_unlock(udev);
510
511 USB_BUS_LOCK(bus);
512 }
513
514 /*------------------------------------------------------------------------*
515 * usb_bus_resume
516 *
517 * This function is used to resume the USB controller.
518 *------------------------------------------------------------------------*/
519 static void
usb_bus_resume(struct usb_proc_msg * pm)520 usb_bus_resume(struct usb_proc_msg *pm)
521 {
522 struct usb_bus *bus;
523 struct usb_device *udev;
524 usb_error_t err;
525 uint8_t do_unlock;
526
527 DPRINTF("\n");
528
529 bus = ((struct usb_bus_msg *)pm)->bus;
530 udev = bus->devices[USB_ROOT_HUB_ADDR];
531
532 if (udev == NULL || bus->bdev == NULL)
533 return;
534
535 USB_BUS_UNLOCK(bus);
536
537 do_unlock = usbd_enum_lock(udev);
538 #if 0
539 DEVMETHOD(usb_take_controller, NULL); /* dummy */
540 #endif
541 USB_TAKE_CONTROLLER(device_get_parent(bus->bdev));
542
543 USB_BUS_LOCK(bus);
544 bus->hw_power_state =
545 USB_HW_POWER_CONTROL |
546 USB_HW_POWER_BULK |
547 USB_HW_POWER_INTERRUPT |
548 USB_HW_POWER_ISOC |
549 USB_HW_POWER_NON_ROOT_HUB;
550 bus->no_explore = 0;
551 USB_BUS_UNLOCK(bus);
552
553 if (bus->methods->set_hw_power_sleep != NULL)
554 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_RESUME);
555
556 if (bus->methods->set_hw_power != NULL)
557 (bus->methods->set_hw_power) (bus);
558
559 /* restore USB configuration to index 0 */
560 err = usbd_set_config_index(udev, 0);
561 if (err)
562 device_printf(bus->bdev, "Could not configure root HUB\n");
563
564 /* probe and attach */
565 err = usb_probe_and_attach(udev, USB_IFACE_INDEX_ANY);
566 if (err) {
567 device_printf(bus->bdev, "Could not probe and "
568 "attach root HUB\n");
569 }
570
571 if (do_unlock)
572 usbd_enum_unlock(udev);
573
574 USB_BUS_LOCK(bus);
575 }
576
577 /*------------------------------------------------------------------------*
578 * usb_bus_reset
579 *
580 * This function is used to reset the USB controller.
581 *------------------------------------------------------------------------*/
582 static void
usb_bus_reset(struct usb_proc_msg * pm)583 usb_bus_reset(struct usb_proc_msg *pm)
584 {
585 struct usb_bus *bus;
586
587 DPRINTF("\n");
588
589 bus = ((struct usb_bus_msg *)pm)->bus;
590
591 if (bus->bdev == NULL || bus->no_explore != 0)
592 return;
593
594 /* a suspend and resume will reset the USB controller */
595 usb_bus_suspend(pm);
596 usb_bus_resume(pm);
597 }
598
599 /*------------------------------------------------------------------------*
600 * usb_bus_shutdown
601 *
602 * This function is used to shutdown the USB controller.
603 *------------------------------------------------------------------------*/
604 static void
usb_bus_shutdown(struct usb_proc_msg * pm)605 usb_bus_shutdown(struct usb_proc_msg *pm)
606 {
607 struct usb_bus *bus;
608 struct usb_device *udev;
609 usb_error_t err;
610 uint8_t do_unlock;
611
612 bus = ((struct usb_bus_msg *)pm)->bus;
613 udev = bus->devices[USB_ROOT_HUB_ADDR];
614
615 if (udev == NULL || bus->bdev == NULL)
616 return;
617
618 USB_BUS_UNLOCK(bus);
619
620 bus_generic_shutdown(bus->bdev);
621
622 do_unlock = usbd_enum_lock(udev);
623
624 err = usbd_set_config_index(udev, USB_UNCONFIG_INDEX);
625 if (err)
626 device_printf(bus->bdev, "Could not unconfigure root HUB\n");
627
628 USB_BUS_LOCK(bus);
629 bus->hw_power_state = 0;
630 bus->no_explore = 1;
631 USB_BUS_UNLOCK(bus);
632
633 if (bus->methods->set_hw_power != NULL)
634 (bus->methods->set_hw_power) (bus);
635
636 if (bus->methods->set_hw_power_sleep != NULL)
637 (bus->methods->set_hw_power_sleep) (bus, USB_HW_POWER_SHUTDOWN);
638
639 if (do_unlock)
640 usbd_enum_unlock(udev);
641
642 USB_BUS_LOCK(bus);
643 }
644
645 /*------------------------------------------------------------------------*
646 * usb_bus_cleanup
647 *
648 * This function is used to cleanup leftover USB character devices.
649 *------------------------------------------------------------------------*/
650 #if USB_HAVE_UGEN
651 static void
usb_bus_cleanup(struct usb_proc_msg * pm)652 usb_bus_cleanup(struct usb_proc_msg *pm)
653 {
654 struct usb_bus *bus;
655 struct usb_fs_privdata *pd;
656
657 bus = ((struct usb_bus_msg *)pm)->bus;
658
659 while ((pd = LIST_FIRST(&bus->pd_cleanup_list)) != NULL) {
660 LIST_REMOVE(pd, pd_next);
661 USB_BUS_UNLOCK(bus);
662
663 usb_destroy_dev_sync(pd);
664
665 USB_BUS_LOCK(bus);
666 }
667 }
668 #endif
669
670 static void
usb_power_wdog(void * arg)671 usb_power_wdog(void *arg)
672 {
673 struct usb_bus *bus = arg;
674
675 USB_BUS_LOCK_ASSERT(bus, MA_OWNED);
676
677 usb_callout_reset(&bus->power_wdog,
678 4 * hz, usb_power_wdog, arg);
679
680 #ifdef DDB
681 /*
682 * The following line of code is only here to recover from
683 * DDB:
684 */
685 usb_proc_rewakeup(USB_BUS_EXPLORE_PROC(bus)); /* recover from DDB */
686 #endif
687
688 #if USB_HAVE_POWERD
689 USB_BUS_UNLOCK(bus);
690
691 usb_bus_power_update(bus);
692
693 USB_BUS_LOCK(bus);
694 #endif
695 }
696
697 /*------------------------------------------------------------------------*
698 * usb_bus_attach
699 *
700 * This function attaches USB in context of the explore thread.
701 *------------------------------------------------------------------------*/
702 static void
usb_bus_attach(struct usb_proc_msg * pm)703 usb_bus_attach(struct usb_proc_msg *pm)
704 {
705 struct usb_bus *bus;
706 struct usb_device *child;
707 device_t dev;
708 usb_error_t err;
709 enum usb_dev_speed speed;
710
711 bus = ((struct usb_bus_msg *)pm)->bus;
712 dev = bus->bdev;
713
714 DPRINTF("\n");
715
716 switch (bus->usbrev) {
717 case USB_REV_1_0:
718 speed = USB_SPEED_FULL;
719 device_printf(bus->bdev, "12Mbps Full Speed USB v1.0\n");
720 break;
721
722 case USB_REV_1_1:
723 speed = USB_SPEED_FULL;
724 device_printf(bus->bdev, "12Mbps Full Speed USB v1.1\n");
725 break;
726
727 case USB_REV_2_0:
728 speed = USB_SPEED_HIGH;
729 device_printf(bus->bdev, "480Mbps High Speed USB v2.0\n");
730 break;
731
732 case USB_REV_2_5:
733 speed = USB_SPEED_VARIABLE;
734 device_printf(bus->bdev, "480Mbps Wireless USB v2.5\n");
735 break;
736
737 case USB_REV_3_0:
738 speed = USB_SPEED_SUPER;
739 device_printf(bus->bdev, "5.0Gbps Super Speed USB v3.0\n");
740 break;
741
742 default:
743 device_printf(bus->bdev, "Unsupported USB revision\n");
744 #if USB_HAVE_ROOT_MOUNT_HOLD
745 usb_root_mount_rel(bus);
746 #endif
747 return;
748 }
749
750 /* default power_mask value */
751 bus->hw_power_state =
752 USB_HW_POWER_CONTROL |
753 USB_HW_POWER_BULK |
754 USB_HW_POWER_INTERRUPT |
755 USB_HW_POWER_ISOC |
756 USB_HW_POWER_NON_ROOT_HUB;
757
758 USB_BUS_UNLOCK(bus);
759
760 /* make sure power is set at least once */
761
762 if (bus->methods->set_hw_power != NULL) {
763 (bus->methods->set_hw_power) (bus);
764 }
765
766 /* allocate the Root USB device */
767
768 child = usb_alloc_device(bus->bdev, bus, NULL, 0, 0, 1,
769 speed, USB_MODE_HOST);
770 if (child) {
771 err = usb_probe_and_attach(child,
772 USB_IFACE_INDEX_ANY);
773 if (!err) {
774 if ((bus->devices[USB_ROOT_HUB_ADDR] == NULL) ||
775 (bus->devices[USB_ROOT_HUB_ADDR]->hub == NULL)) {
776 err = USB_ERR_NO_ROOT_HUB;
777 }
778 }
779 } else {
780 err = USB_ERR_NOMEM;
781 }
782
783 USB_BUS_LOCK(bus);
784
785 if (err) {
786 device_printf(bus->bdev, "Root HUB problem, error=%s\n",
787 usbd_errstr(err));
788 #if USB_HAVE_ROOT_MOUNT_HOLD
789 usb_root_mount_rel(bus);
790 #endif
791 }
792
793 /* set softc - we are ready */
794 device_set_softc(dev, bus);
795
796 /* start watchdog */
797 usb_power_wdog(bus);
798 }
799
800 /*------------------------------------------------------------------------*
801 * usb_attach_sub
802 *
803 * This function creates a thread which runs the USB attach code.
804 *------------------------------------------------------------------------*/
805 static void
usb_attach_sub(device_t dev,struct usb_bus * bus)806 usb_attach_sub(device_t dev, struct usb_bus *bus)
807 {
808 bus_topo_lock();
809 if (usb_devclass_ptr == NULL)
810 usb_devclass_ptr = devclass_find("usbus");
811 bus_topo_unlock();
812
813 #if USB_HAVE_PF
814 usbpf_attach(bus);
815 #endif
816 /* Initialise USB process messages */
817 bus->explore_msg[0].hdr.pm_callback = &usb_bus_explore;
818 bus->explore_msg[0].bus = bus;
819 bus->explore_msg[1].hdr.pm_callback = &usb_bus_explore;
820 bus->explore_msg[1].bus = bus;
821
822 bus->detach_msg[0].hdr.pm_callback = &usb_bus_detach;
823 bus->detach_msg[0].bus = bus;
824 bus->detach_msg[1].hdr.pm_callback = &usb_bus_detach;
825 bus->detach_msg[1].bus = bus;
826
827 bus->attach_msg[0].hdr.pm_callback = &usb_bus_attach;
828 bus->attach_msg[0].bus = bus;
829 bus->attach_msg[1].hdr.pm_callback = &usb_bus_attach;
830 bus->attach_msg[1].bus = bus;
831
832 bus->suspend_msg[0].hdr.pm_callback = &usb_bus_suspend;
833 bus->suspend_msg[0].bus = bus;
834 bus->suspend_msg[1].hdr.pm_callback = &usb_bus_suspend;
835 bus->suspend_msg[1].bus = bus;
836
837 bus->resume_msg[0].hdr.pm_callback = &usb_bus_resume;
838 bus->resume_msg[0].bus = bus;
839 bus->resume_msg[1].hdr.pm_callback = &usb_bus_resume;
840 bus->resume_msg[1].bus = bus;
841
842 bus->reset_msg[0].hdr.pm_callback = &usb_bus_reset;
843 bus->reset_msg[0].bus = bus;
844 bus->reset_msg[1].hdr.pm_callback = &usb_bus_reset;
845 bus->reset_msg[1].bus = bus;
846
847 bus->shutdown_msg[0].hdr.pm_callback = &usb_bus_shutdown;
848 bus->shutdown_msg[0].bus = bus;
849 bus->shutdown_msg[1].hdr.pm_callback = &usb_bus_shutdown;
850 bus->shutdown_msg[1].bus = bus;
851
852 #if USB_HAVE_UGEN
853 LIST_INIT(&bus->pd_cleanup_list);
854 bus->cleanup_msg[0].hdr.pm_callback = &usb_bus_cleanup;
855 bus->cleanup_msg[0].bus = bus;
856 bus->cleanup_msg[1].hdr.pm_callback = &usb_bus_cleanup;
857 bus->cleanup_msg[1].bus = bus;
858 #endif
859
860 #if USB_HAVE_PER_BUS_PROCESS
861 /* Create USB explore and callback processes */
862
863 if (usb_proc_create(USB_BUS_GIANT_PROC(bus),
864 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
865 device_printf(dev, "WARNING: Creation of USB Giant "
866 "callback process failed.\n");
867 } else if (usb_proc_create(USB_BUS_NON_GIANT_ISOC_PROC(bus),
868 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGHEST)) {
869 device_printf(dev, "WARNING: Creation of USB non-Giant ISOC "
870 "callback process failed.\n");
871 } else if (usb_proc_create(USB_BUS_NON_GIANT_BULK_PROC(bus),
872 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_HIGH)) {
873 device_printf(dev, "WARNING: Creation of USB non-Giant BULK "
874 "callback process failed.\n");
875 } else if (usb_proc_create(USB_BUS_EXPLORE_PROC(bus),
876 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
877 device_printf(dev, "WARNING: Creation of USB explore "
878 "process failed.\n");
879 } else if (usb_proc_create(USB_BUS_CONTROL_XFER_PROC(bus),
880 &bus->bus_mtx, device_get_nameunit(dev), USB_PRI_MED)) {
881 device_printf(dev, "WARNING: Creation of USB control transfer "
882 "process failed.\n");
883 } else
884 #endif
885 {
886 /* Get final attach going */
887 USB_BUS_LOCK(bus);
888 usb_proc_msignal(USB_BUS_EXPLORE_PROC(bus),
889 &bus->attach_msg[0], &bus->attach_msg[1]);
890 USB_BUS_UNLOCK(bus);
891
892 /* Do initial explore */
893 usb_needs_explore(bus, 1);
894 }
895 }
896 SYSUNINIT(usb_bus_unload, SI_SUB_KLD, SI_ORDER_ANY, usb_bus_unload, NULL);
897
898 /*------------------------------------------------------------------------*
899 * usb_bus_mem_flush_all_cb
900 *------------------------------------------------------------------------*/
901 #if USB_HAVE_BUSDMA
902 static void
usb_bus_mem_flush_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)903 usb_bus_mem_flush_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
904 struct usb_page *pg, usb_size_t size, usb_size_t align)
905 {
906 usb_pc_cpu_flush(pc);
907 }
908 #endif
909
910 /*------------------------------------------------------------------------*
911 * usb_bus_mem_flush_all - factored out code
912 *------------------------------------------------------------------------*/
913 #if USB_HAVE_BUSDMA
914 void
usb_bus_mem_flush_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)915 usb_bus_mem_flush_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
916 {
917 if (cb) {
918 cb(bus, &usb_bus_mem_flush_all_cb);
919 }
920 }
921 #endif
922
923 /*------------------------------------------------------------------------*
924 * usb_bus_mem_alloc_all_cb
925 *------------------------------------------------------------------------*/
926 #if USB_HAVE_BUSDMA
927 static void
usb_bus_mem_alloc_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)928 usb_bus_mem_alloc_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
929 struct usb_page *pg, usb_size_t size, usb_size_t align)
930 {
931 /* need to initialize the page cache */
932 pc->tag_parent = bus->dma_parent_tag;
933
934 if (usb_pc_alloc_mem(pc, pg, size, align)) {
935 bus->alloc_failed = 1;
936 }
937 }
938 #endif
939
940 /*------------------------------------------------------------------------*
941 * usb_bus_mem_alloc_all - factored out code
942 *
943 * Returns:
944 * 0: Success
945 * Else: Failure
946 *------------------------------------------------------------------------*/
947 uint8_t
usb_bus_mem_alloc_all(struct usb_bus * bus,bus_dma_tag_t dmat,usb_bus_mem_cb_t * cb)948 usb_bus_mem_alloc_all(struct usb_bus *bus, bus_dma_tag_t dmat,
949 usb_bus_mem_cb_t *cb)
950 {
951 bus->alloc_failed = 0;
952
953 mtx_init(&bus->bus_mtx, device_get_nameunit(bus->parent),
954 "usb_def_mtx", MTX_DEF | MTX_RECURSE);
955
956 mtx_init(&bus->bus_spin_lock, device_get_nameunit(bus->parent),
957 "usb_spin_mtx", MTX_SPIN | MTX_RECURSE);
958
959 usb_callout_init_mtx(&bus->power_wdog,
960 &bus->bus_mtx, 0);
961
962 TAILQ_INIT(&bus->intr_q.head);
963
964 #if USB_HAVE_BUSDMA
965 usb_dma_tag_setup(bus->dma_parent_tag, bus->dma_tags,
966 dmat, &bus->bus_mtx, NULL, bus->dma_bits, USB_BUS_DMA_TAG_MAX);
967 #endif
968 if ((bus->devices_max > USB_MAX_DEVICES) ||
969 (bus->devices_max < USB_MIN_DEVICES) ||
970 (bus->devices == NULL)) {
971 DPRINTFN(0, "Devices field has not been "
972 "initialised properly\n");
973 bus->alloc_failed = 1; /* failure */
974 }
975 #if USB_HAVE_BUSDMA
976 if (cb) {
977 cb(bus, &usb_bus_mem_alloc_all_cb);
978 }
979 #endif
980 if (bus->alloc_failed) {
981 usb_bus_mem_free_all(bus, cb);
982 }
983 return (bus->alloc_failed);
984 }
985
986 /*------------------------------------------------------------------------*
987 * usb_bus_mem_free_all_cb
988 *------------------------------------------------------------------------*/
989 #if USB_HAVE_BUSDMA
990 static void
usb_bus_mem_free_all_cb(struct usb_bus * bus,struct usb_page_cache * pc,struct usb_page * pg,usb_size_t size,usb_size_t align)991 usb_bus_mem_free_all_cb(struct usb_bus *bus, struct usb_page_cache *pc,
992 struct usb_page *pg, usb_size_t size, usb_size_t align)
993 {
994 usb_pc_free_mem(pc);
995 }
996 #endif
997
998 /*------------------------------------------------------------------------*
999 * usb_bus_mem_free_all - factored out code
1000 *------------------------------------------------------------------------*/
1001 void
usb_bus_mem_free_all(struct usb_bus * bus,usb_bus_mem_cb_t * cb)1002 usb_bus_mem_free_all(struct usb_bus *bus, usb_bus_mem_cb_t *cb)
1003 {
1004 #if USB_HAVE_BUSDMA
1005 if (cb) {
1006 cb(bus, &usb_bus_mem_free_all_cb);
1007 }
1008 usb_dma_tag_unsetup(bus->dma_parent_tag);
1009 #endif
1010
1011 mtx_destroy(&bus->bus_mtx);
1012 mtx_destroy(&bus->bus_spin_lock);
1013 }
1014
1015 /* convenience wrappers */
1016 void
usb_proc_explore_mwait(struct usb_device * udev,void * pm1,void * pm2)1017 usb_proc_explore_mwait(struct usb_device *udev, void *pm1, void *pm2)
1018 {
1019 usb_proc_mwait(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2);
1020 }
1021
1022 void *
usb_proc_explore_msignal(struct usb_device * udev,void * pm1,void * pm2)1023 usb_proc_explore_msignal(struct usb_device *udev, void *pm1, void *pm2)
1024 {
1025 return (usb_proc_msignal(USB_BUS_EXPLORE_PROC(udev->bus), pm1, pm2));
1026 }
1027
1028 void
usb_proc_explore_lock(struct usb_device * udev)1029 usb_proc_explore_lock(struct usb_device *udev)
1030 {
1031 USB_BUS_LOCK(udev->bus);
1032 }
1033
1034 void
usb_proc_explore_unlock(struct usb_device * udev)1035 usb_proc_explore_unlock(struct usb_device *udev)
1036 {
1037 USB_BUS_UNLOCK(udev->bus);
1038 }
1039