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