2 * $NetBSD: usb.c,v 1.68 2002/02/20 20:30:12 christos Exp $
3 * $FreeBSD: src/sys/dev/usb/usb.c,v 1.106 2005/03/27 15:31:23 iedowse Exp $
4 * $DragonFly: src/sys/bus/usb/usb.c,v 1.29 2007/06/17 08:42:31 dillon Exp $
7 /* Also already merged from NetBSD:
8 * $NetBSD: usb.c,v 1.70 2002/05/09 21:54:32 augustss Exp $
9 * $NetBSD: usb.c,v 1.71 2002/06/01 23:51:04 lukem Exp $
10 * $NetBSD: usb.c,v 1.73 2002/09/23 05:51:19 simonb Exp $
11 * $NetBSD: usb.c,v 1.80 2003/11/07 17:03:25 wiz Exp $
15 * Copyright (c) 1998 The NetBSD Foundation, Inc.
16 * All rights reserved.
18 * This code is derived from software contributed to The NetBSD Foundation
19 * by Lennart Augustsson (lennart@augustsson.net) at
20 * Carlstedt Research & Technology.
22 * Redistribution and use in source and binary forms, with or without
23 * modification, are permitted provided that the following conditions
25 * 1. Redistributions of source code must retain the above copyright
26 * notice, this list of conditions and the following disclaimer.
27 * 2. Redistributions in binary form must reproduce the above copyright
28 * notice, this list of conditions and the following disclaimer in the
29 * documentation and/or other materials provided with the distribution.
30 * 3. All advertising materials mentioning features or use of this software
31 * must display the following acknowledgement:
32 * This product includes software developed by the NetBSD
33 * Foundation, Inc. and its contributors.
34 * 4. Neither the name of The NetBSD Foundation nor the names of its
35 * contributors may be used to endorse or promote products derived
36 * from this software without specific prior written permission.
38 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
39 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
40 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
42 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
43 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
44 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
45 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
46 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
47 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
48 * POSSIBILITY OF SUCH DAMAGE.
52 * USB specifications and other documentation can be found at
53 * http://www.usb.org/developers/docs/ and
54 * http://www.usb.org/developers/devclass_docs/
57 #include <sys/param.h>
58 #include <sys/systm.h>
59 #include <sys/kernel.h>
61 #include <sys/malloc.h>
62 #if __FreeBSD_version >= 500000
63 #include <sys/mutex.h>
65 #if defined(__NetBSD__) || defined(__OpenBSD__)
66 #include <sys/device.h>
67 #elif defined(__FreeBSD__) || defined(__DragonFly__)
68 #include <sys/unistd.h>
69 #include <sys/module.h>
71 #include <sys/fcntl.h>
72 #include <sys/filio.h>
75 #include <sys/kthread.h>
78 #include <sys/device.h>
80 #if __FreeBSD_version >= 500014
81 #include <sys/selinfo.h>
83 #include <sys/select.h>
85 #include <sys/vnode.h>
86 #include <sys/signalvar.h>
87 #include <sys/sysctl.h>
88 #include <sys/thread2.h>
90 #include <bus/usb/usb.h>
91 #include <bus/usb/usbdi.h>
92 #include <bus/usb/usbdi_util.h>
94 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
95 #define USB_DEV_MINOR 255 /* event queue device */
97 #if defined(__FreeBSD__) || defined(__DragonFly__)
98 MALLOC_DEFINE(M_USB, "USB", "USB");
99 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
100 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
103 #endif /* defined(__FreeBSD__) || defined(__DragonFly__) */
105 #include <bus/usb/usbdivar.h>
106 #include <bus/usb/usb_quirks.h>
108 /* Define this unconditionally in case a kernel module is loaded that
109 * has been compiled with debugging options.
111 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
114 #define DPRINTF(x) if (usbdebug) logprintf x
115 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
117 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
118 &usbdebug, 0, "usb debug level");
120 * 0 - do usual exploration
121 * 1 - do not use timeout exploration
122 * >1 - do no exploration
124 int usb_noexplore = 0;
127 #define DPRINTFN(n,x)
131 USBBASEDEVICE sc_dev; /* base device */
132 #if defined(__FreeBSD__) || defined(__DragonFly__)
135 usbd_bus_handle sc_bus; /* USB controller */
136 struct usbd_port sc_port; /* dummy port for root hub */
138 struct thread *sc_event_thread;
144 TAILQ_HEAD(, usb_task) tasks;
145 usb_proc_ptr task_thread_proc;
147 int taskcreated; /* task thread exists. */
149 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
151 #if defined(__NetBSD__) || defined(__OpenBSD__)
153 #elif defined(__FreeBSD__) || defined(__DragonFly__)
160 struct dev_ops usb_ops = {
161 { "usb", USB_CDEV_MAJOR, 0 },
170 Static void usb_discover(void *);
171 #if defined(__FreeBSD__) || defined(__DragonFly__)
172 Static bus_child_detached_t usb_child_detached;
174 Static void usb_create_event_thread(void *);
175 Static void usb_event_thread(void *);
176 Static void usb_task_thread(void *);
178 #if defined(__FreeBSD__) || defined(__DragonFly__)
179 Static cdev_t usb_dev; /* The /dev/usb device. */
180 Static int usb_ndevs; /* Number of /dev/usbN devices. */
183 #define USB_MAX_EVENTS 100
186 TAILQ_ENTRY(usb_event_q) next;
188 Static TAILQ_HEAD(, usb_event_q) usb_events =
189 TAILQ_HEAD_INITIALIZER(usb_events);
190 Static int usb_nevents = 0;
191 Static struct selinfo usb_selevent;
192 Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
193 Static int usb_dev_open = 0;
194 Static void usb_add_event(int, struct usb_event *);
196 Static int usb_get_next_event(struct usb_event *);
198 Static const char *usbrev_str[] = USBREV_STR;
200 USB_DECLARE_DRIVER_INIT(usb,
201 DEVMETHOD(bus_child_detached, usb_child_detached),
202 DEVMETHOD(device_suspend, bus_generic_suspend),
203 DEVMETHOD(device_resume, bus_generic_resume),
204 DEVMETHOD(device_shutdown, bus_generic_shutdown)
207 #if defined(__FreeBSD__) || defined(__DragonFly__)
208 MODULE_VERSION(usb, 1);
213 DPRINTF(("usbd_match\n"));
214 return (UMATCH_GENERIC);
219 #if defined(__NetBSD__) || defined(__OpenBSD__)
220 struct usb_softc *sc = (struct usb_softc *)self;
221 #elif defined(__FreeBSD__) || defined(__DragonFly__)
222 struct usb_softc *sc = device_get_softc(self);
223 void *aux = device_get_ivars(self);
226 usbd_device_handle dev;
234 DPRINTF(("usbd_attach\n"));
238 sc->sc_bus->usbctl = sc;
239 sc->sc_port.power = USB_MAX_POWER;
241 #if defined(__FreeBSD__) || defined(__DragonFly__)
242 kprintf("%s", USBDEVNAME(sc->sc_dev));
244 usbrev = sc->sc_bus->usbrev;
245 kprintf(": USB revision %s", usbrev_str[usbrev]);
249 speed = USB_SPEED_FULL;
252 speed = USB_SPEED_HIGH;
255 kprintf(", not supported\n");
257 USB_ATTACH_ERROR_RETURN;
262 /* Make sure not to use tsleep() if we are cold booting. */
264 sc->sc_bus->use_polling++;
267 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
268 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
270 #ifdef USB_USE_SOFTINTR
271 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
272 /* XXX we should have our own level */
273 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
274 sc->sc_bus->methods->soft_intr, sc->sc_bus);
275 if (sc->sc_bus->soft == NULL) {
276 kprintf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
278 USB_ATTACH_ERROR_RETURN;
281 usb_callout_init(sc->sc_bus->softi);
285 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
288 dev = sc->sc_port.device;
289 if (dev->hub == NULL) {
291 kprintf("%s: root device is not a hub\n",
292 USBDEVNAME(sc->sc_dev));
293 USB_ATTACH_ERROR_RETURN;
295 sc->sc_bus->root_hub = dev;
298 * Turning this code off will delay attachment of USB devices
299 * until the USB event thread is running, which means that
300 * the keyboard will not work until after cold boot.
302 #if defined(__FreeBSD__) || defined(__DragonFly__)
305 * XXX Exploring high speed device here will
308 if (speed != USB_SPEED_HIGH)
309 dev->hub->explore(sc->sc_bus->root_hub);
312 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
313 dev->hub->explore(sc->sc_bus->root_hub);
317 kprintf("%s: root hub problem, error=%d\n",
318 USBDEVNAME(sc->sc_dev), err);
323 sc->sc_bus->use_polling--;
326 config_pending_incr();
327 #if defined(__NetBSD__) || defined(__OpenBSD__)
328 usb_kthread_create(usb_create_event_thread, sc);
331 #if defined(__FreeBSD__) || defined(__DragonFly__)
332 usb_create_event_thread(sc);
333 /* The per controller devices (used for usb_discover) */
334 /* XXX This is redundant now, but old usbd's will want it */
335 dev_ops_add(&usb_ops, -1, device_get_unit(self));
336 tmp_dev = make_dev(&usb_ops, device_get_unit(self),
337 UID_ROOT, GID_OPERATOR, 0660,
338 "usb%d", device_get_unit(self));
339 sc->sc_usbdev = reference_dev(tmp_dev);
340 if (usb_ndevs++ == 0) {
341 /* The device spitting out events */
342 dev_ops_add(&usb_ops, -1, USB_DEV_MINOR);
343 tmp_dev = make_dev(&usb_ops, USB_DEV_MINOR,
344 UID_ROOT, GID_OPERATOR, 0660, "usb");
345 usb_dev = reference_dev(tmp_dev);
349 USB_ATTACH_SUCCESS_RETURN;
352 Static const char *taskq_names[] = USB_TASKQ_NAMES;
355 usb_create_event_thread(void *arg)
357 struct usb_softc *sc = arg;
360 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
361 "%s", USBDEVNAME(sc->sc_dev))) {
362 kprintf("%s: unable to create event thread for\n",
363 USBDEVNAME(sc->sc_dev));
364 panic("usb_create_event_thread");
367 for (i = 0; i < USB_NUM_TASKQS; i++) {
368 struct usb_taskq *taskq = &usb_taskq[i];
370 if (taskq->taskcreated == 0) {
371 taskq->taskcreated = 1;
372 taskq->name = taskq_names[i];
373 TAILQ_INIT(&taskq->tasks);
374 if (usb_kthread_create2(usb_task_thread, taskq,
375 &taskq->task_thread_proc, taskq->name)) {
376 kprintf("unable to create task thread\n");
377 panic("usb_create_event_thread task");
384 * Add a task to be performed by the task thread. This function can be
385 * called from any context and the task will be executed in a process
389 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
391 struct usb_taskq *taskq;
395 taskq = &usb_taskq[queue];
396 if (task->queue == -1) {
397 DPRINTFN(2,("usb_add_task: task=%p\n", task));
398 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
401 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
403 wakeup(&taskq->tasks);
409 usb_do_task(usbd_device_handle dev, struct usb_task *task, int queue,
412 struct usb_taskq *taskq;
416 taskq = &usb_taskq[queue];
417 if (task->queue == -1) {
418 DPRINTFN(2,("usb_add_task: task=%p\n", task));
419 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
422 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
424 wakeup(&taskq->tasks);
426 /* Wait until task is finished */
427 tsleep((&taskq->tasks + 1), 0, "usbdotsk", time_out);
433 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
436 if (task->queue != -1) {
437 TAILQ_REMOVE(&usb_taskq[task->queue].tasks, task, next);
444 usb_event_thread(void *arg)
446 struct usb_softc *sc = arg;
448 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
452 DPRINTF(("usb_event_thread: start\n"));
455 * In case this controller is a companion controller to an
456 * EHCI controller we need to wait until the EHCI controller
457 * has grabbed the port.
458 * XXX It would be nicer to do this with a tsleep(), but I don't
459 * know how to synchronize the creation of the threads so it
462 usb_delay_ms(sc->sc_bus, 500);
466 /* Make sure first discover does something. */
467 sc->sc_bus->needs_explore = 1;
469 config_pending_decr();
471 while (!sc->sc_dying) {
473 if (usb_noexplore < 2)
477 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
478 usb_noexplore ? 0 : hz * 60);
480 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt", hz * 60);
482 DPRINTFN(2,("usb_event_thread: woke up\n"));
484 sc->sc_event_thread = NULL;
488 /* In case parent is waiting for us to exit. */
491 DPRINTF(("usb_event_thread: exit\n"));
496 usb_task_thread(void *arg)
498 struct usb_task *task;
499 struct usb_taskq *taskq;
501 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
508 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
510 while (usb_ndevs > 0) {
511 task = TAILQ_FIRST(&taskq->tasks);
513 tsleep(&taskq->tasks, 0, "usbtsk", 0);
514 task = TAILQ_FIRST(&taskq->tasks);
516 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
518 TAILQ_REMOVE(&taskq->tasks, task, next);
521 task->fun(task->arg);
523 wakeup((&taskq->tasks + 1));
529 taskq->taskcreated = 0;
530 wakeup(&taskq->taskcreated);
532 DPRINTF(("usb_event_thread: exit\n"));
535 #if defined(__NetBSD__) || defined(__OpenBSD__)
537 usbctlprint(void *aux, const char *pnp)
539 /* only "usb"es can attach to host controllers */
541 kprintf("usb at %s", pnp);
545 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
548 usbopen(struct dev_open_args *ap)
550 cdev_t dev = ap->a_head.a_dev;
551 int unit = USBUNIT(dev);
552 struct usb_softc *sc;
554 if (unit == USB_DEV_MINOR) {
558 usb_async_proc = NULL;
562 USB_GET_SC_OPEN(usb, unit, sc);
571 usbread(struct dev_read_args *ap)
573 cdev_t dev = ap->a_head.a_dev;
574 struct uio *uio = ap->a_uio;
576 int unit = USBUNIT(dev);
579 if (unit != USB_DEV_MINOR)
582 if (uio->uio_resid != sizeof(struct usb_event))
588 n = usb_get_next_event(&ue);
591 if (ap->a_ioflag & IO_NDELAY) {
595 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
601 error = uiomove((void *)&ue, uio->uio_resid, uio);
607 usbclose(struct dev_close_args *ap)
609 cdev_t dev = ap->a_head.a_dev;
610 int unit = USBUNIT(dev);
612 if (unit == USB_DEV_MINOR) {
613 usb_async_proc = NULL;
621 usbioctl(struct dev_ioctl_args *ap)
623 cdev_t devt = ap->a_head.a_dev;
624 struct usb_softc *sc;
625 int unit = USBUNIT(devt);
627 if (unit == USB_DEV_MINOR) {
630 if (*(int *)ap->a_data)
631 #if defined(__DragonFly__)
632 usb_async_proc = curproc;
633 #elif __FreeBSD_version >= 500000
634 usb_async_proc = p->td_proc;
639 usb_async_proc = NULL;
647 USB_GET_SC(usb, unit, sc);
653 #if defined(__FreeBSD__) || defined(__DragonFly__)
654 /* This part should be deleted */
660 struct usb_ctl_request *ur = (void *)ap->a_data;
661 int len = UGETW(ur->ucr_request.wLength);
665 int addr = ur->ucr_addr;
669 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
670 if (len < 0 || len > 32768)
672 if (addr < 0 || addr >= USB_MAX_DEVICES ||
673 sc->sc_bus->devices[addr] == 0)
676 iov.iov_base = (caddr_t)ur->ucr_data;
682 uio.uio_segflg = UIO_USERSPACE;
684 ur->ucr_request.bmRequestType & UT_READ ?
685 UIO_READ : UIO_WRITE;
686 uio.uio_td = curthread;
687 ptr = kmalloc(len, M_TEMP, M_WAITOK);
688 if (uio.uio_rw == UIO_WRITE) {
689 error = uiomove(ptr, len, &uio);
694 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
695 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
696 USBD_DEFAULT_TIMEOUT);
702 if (uio.uio_rw == UIO_READ) {
703 error = uiomove(ptr, len, &uio);
716 struct usb_device_info *di = (void *)ap->a_data;
717 int addr = di->udi_addr;
718 usbd_device_handle dev;
720 if (addr < 1 || addr >= USB_MAX_DEVICES)
722 dev = sc->sc_bus->devices[addr];
725 usbd_fill_deviceinfo(dev, di, 1);
729 case USB_DEVICESTATS:
730 *(struct usb_device_stats *)ap->a_data = sc->sc_bus->stats;
740 usbpoll(struct dev_poll_args *ap)
742 cdev_t dev = ap->a_head.a_dev;
744 int unit = USBUNIT(dev);
746 if (unit == USB_DEV_MINOR) {
748 mask = POLLIN | POLLRDNORM;
751 if (ap->a_events & mask && usb_nevents > 0)
752 revents |= ap->a_events & mask;
753 if (revents == 0 && ap->a_events & mask)
754 selrecord(curthread, &usb_selevent);
756 ap->a_events = revents;
760 return (0); /* select/poll never wakes up - back compat */
764 /* Explore device tree from the root. */
766 usb_discover(void *v)
768 struct usb_softc *sc = v;
770 DPRINTFN(2,("usb_discover\n"));
772 if (usb_noexplore > 1)
777 * We need mutual exclusion while traversing the device tree,
778 * but this is guaranteed since this function is only called
779 * from the event thread for the controller.
782 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
783 sc->sc_bus->needs_explore = 0;
786 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
794 usb_needs_explore(usbd_device_handle dev)
796 DPRINTFN(2,("usb_needs_explore\n"));
797 dev->bus->needs_explore = 1;
798 wakeup(&dev->bus->needs_explore);
801 /* Called from a critical section */
803 usb_get_next_event(struct usb_event *ue)
805 struct usb_event_q *ueq;
807 if (usb_nevents <= 0)
809 ueq = TAILQ_FIRST(&usb_events);
812 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
818 TAILQ_REMOVE(&usb_events, ueq, next);
819 kfree(ueq, M_USBDEV);
825 usbd_add_dev_event(int type, usbd_device_handle udev)
829 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
830 usb_add_event(type, &ue);
834 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
838 ue.u.ue_driver.ue_cookie = udev->cookie;
839 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
840 sizeof ue.u.ue_driver.ue_devname);
841 usb_add_event(type, &ue);
845 usb_add_event(int type, struct usb_event *uep)
847 struct usb_event_q *ueq;
849 struct timeval thetime;
851 ueq = kmalloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
853 ueq->ue.ue_type = type;
855 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
858 if (USB_EVENT_IS_DETACH(type)) {
859 struct usb_event_q *ueqi, *ueqi_next;
861 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
862 ueqi_next = TAILQ_NEXT(ueqi, next);
863 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
864 uep->u.ue_device.udi_cookie.cookie) {
865 TAILQ_REMOVE(&usb_events, ueqi, next);
866 kfree(ueqi, M_USBDEV);
868 ueqi_next = TAILQ_FIRST(&usb_events);
872 if (usb_nevents >= USB_MAX_EVENTS) {
873 /* Too many queued events, drop an old one. */
874 DPRINTF(("usb: event dropped\n"));
875 usb_get_next_event(&ue);
877 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
880 selwakeuppri(&usb_selevent, 0);
881 if (usb_async_proc != NULL) {
882 PROC_LOCK(usb_async_proc);
883 ksignal(usb_async_proc, SIGIO);
884 PROC_UNLOCK(usb_async_proc);
890 usb_schedsoftintr(usbd_bus_handle bus)
892 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
893 #ifdef USB_USE_SOFTINTR
894 if (bus->use_polling) {
895 bus->methods->soft_intr(bus);
897 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
898 softintr_schedule(bus->soft);
900 if (!callout_pending(&bus->softi))
901 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
903 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
906 bus->methods->soft_intr(bus);
907 #endif /* USB_USE_SOFTINTR */
910 #if defined(__NetBSD__) || defined(__OpenBSD__)
912 usb_activate(device_ptr_t self, enum devact act)
914 struct usb_softc *sc = (struct usb_softc *)self;
915 usbd_device_handle dev = sc->sc_port.device;
922 case DVACT_DEACTIVATE:
924 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
925 for (i = 0; dev->subdevs[i]; i++)
926 rv |= config_deactivate(dev->subdevs[i]);
936 USB_DETACH_START(usb, sc);
939 DPRINTF(("usb_detach: start\n"));
943 /* Make all devices disconnect. */
944 if (sc->sc_port.device != NULL)
945 usb_disconnect_port(&sc->sc_port, self);
947 /* Kill off event thread. */
948 if (sc->sc_event_thread != NULL) {
949 wakeup(&sc->sc_bus->needs_explore);
950 if (tsleep(sc, 0, "usbdet", hz * 60))
951 kprintf("%s: event thread didn't die\n",
952 USBDEVNAME(sc->sc_dev));
953 DPRINTF(("usb_detach: event thread dead\n"));
956 #if defined(__FreeBSD__) || defined(__DragonFly__)
957 destroy_dev(sc->sc_usbdev);
958 if (--usb_ndevs == 0) {
961 destroy_dev(usb_dev);
964 for (i = 0; i < USB_NUM_TASKQS; i++) {
965 struct usb_taskq *taskq = &usb_taskq[i];
966 wakeup(&taskq->tasks);
967 if (tsleep(&taskq->taskcreated, 0, "usbtdt",
969 kprintf("usb task thread %s didn't die\n",
978 #ifdef USB_USE_SOFTINTR
979 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
980 if (sc->sc_bus->soft != NULL) {
981 softintr_disestablish(sc->sc_bus->soft);
982 sc->sc_bus->soft = NULL;
985 callout_stop(&sc->sc_bus->softi);
989 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
990 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
995 #if defined(__FreeBSD__) || defined(__DragonFly__)
997 usb_child_detached(device_t self, device_t child)
999 struct usb_softc *sc = device_get_softc(self);
1001 /* XXX, should check it is the right device. */
1002 sc->sc_port.device = NULL;
1005 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
1006 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
1007 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);