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