1 /* $NetBSD: usb.c,v 1.68 2002/02/20 20:30:12 christos Exp $ */
2 /* $FreeBSD: src/sys/dev/usb/usb.c,v 1.106 2005/03/27 15:31:23 iedowse Exp $ */
3 /* $DragonFly: src/sys/bus/usb/usb.c,v 1.25 2006/12/22 23:12:17 swildner Exp $ */
5 /* Also already merged from NetBSD:
6 * $NetBSD: usb.c,v 1.70 2002/05/09 21:54:32 augustss Exp $
7 * $NetBSD: usb.c,v 1.71 2002/06/01 23:51:04 lukem Exp $
8 * $NetBSD: usb.c,v 1.73 2002/09/23 05:51:19 simonb Exp $
9 * $NetBSD: usb.c,v 1.80 2003/11/07 17:03:25 wiz Exp $
13 * Copyright (c) 1998 The NetBSD Foundation, Inc.
14 * All rights reserved.
16 * This code is derived from software contributed to The NetBSD Foundation
17 * by Lennart Augustsson (lennart@augustsson.net) at
18 * Carlstedt Research & Technology.
20 * Redistribution and use in source and binary forms, with or without
21 * modification, are permitted provided that the following conditions
23 * 1. Redistributions of source code must retain the above copyright
24 * notice, this list of conditions and the following disclaimer.
25 * 2. Redistributions in binary form must reproduce the above copyright
26 * notice, this list of conditions and the following disclaimer in the
27 * documentation and/or other materials provided with the distribution.
28 * 3. All advertising materials mentioning features or use of this software
29 * must display the following acknowledgement:
30 * This product includes software developed by the NetBSD
31 * Foundation, Inc. and its contributors.
32 * 4. Neither the name of The NetBSD Foundation nor the names of its
33 * contributors may be used to endorse or promote products derived
34 * from this software without specific prior written permission.
36 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
37 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
38 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
40 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
41 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
42 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
43 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
44 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
45 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
46 * POSSIBILITY OF SUCH DAMAGE.
50 * USB specifications and other documentation can be found at
51 * http://www.usb.org/developers/docs/ and
52 * http://www.usb.org/developers/devclass_docs/
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/kernel.h>
59 #include <sys/malloc.h>
60 #if __FreeBSD_version >= 500000
61 #include <sys/mutex.h>
63 #if defined(__NetBSD__) || defined(__OpenBSD__)
64 #include <sys/device.h>
65 #elif defined(__FreeBSD__) || defined(__DragonFly__)
66 #include <sys/unistd.h>
67 #include <sys/module.h>
69 #include <sys/fcntl.h>
70 #include <sys/filio.h>
73 #include <sys/kthread.h>
76 #include <sys/device.h>
78 #if __FreeBSD_version >= 500014
79 #include <sys/selinfo.h>
81 #include <sys/select.h>
83 #include <sys/vnode.h>
84 #include <sys/signalvar.h>
85 #include <sys/sysctl.h>
86 #include <sys/thread2.h>
88 #include <bus/usb/usb.h>
89 #include <bus/usb/usbdi.h>
90 #include <bus/usb/usbdi_util.h>
92 #define USBUNIT(d) (minor(d)) /* usb_discover device nodes, kthread */
93 #define USB_DEV_MINOR 255 /* event queue device */
95 #if defined(__FreeBSD__) || defined(__DragonFly__)
96 MALLOC_DEFINE(M_USB, "USB", "USB");
97 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
98 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
101 #endif /* defined(__FreeBSD__) || defined(__DragonFly__) */
103 #include <bus/usb/usbdivar.h>
104 #include <bus/usb/usb_quirks.h>
106 /* Define this unconditionally in case a kernel module is loaded that
107 * has been compiled with debugging options.
109 SYSCTL_NODE(_hw, OID_AUTO, usb, CTLFLAG_RW, 0, "USB debugging");
112 #define DPRINTF(x) if (usbdebug) logprintf x
113 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x
115 SYSCTL_INT(_hw_usb, OID_AUTO, debug, CTLFLAG_RW,
116 &usbdebug, 0, "usb debug level");
118 * 0 - do usual exploration
119 * 1 - do not use timeout exploration
120 * >1 - do no exploration
122 int usb_noexplore = 0;
125 #define DPRINTFN(n,x)
129 USBBASEDEVICE sc_dev; /* base device */
130 #if defined(__FreeBSD__) || defined(__DragonFly__)
133 usbd_bus_handle sc_bus; /* USB controller */
134 struct usbd_port sc_port; /* dummy port for root hub */
136 struct thread *sc_event_thread;
142 TAILQ_HEAD(, usb_task) tasks;
143 usb_proc_ptr task_thread_proc;
145 int taskcreated; /* task thread exists. */
147 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
149 #if defined(__NetBSD__) || defined(__OpenBSD__)
151 #elif defined(__FreeBSD__) || defined(__DragonFly__)
158 struct dev_ops usb_ops = {
159 { "usb", USB_CDEV_MAJOR, 0 },
168 Static void usb_discover(void *);
169 #if defined(__FreeBSD__) || defined(__DragonFly__)
170 Static bus_child_detached_t usb_child_detached;
172 Static void usb_create_event_thread(void *);
173 Static void usb_event_thread(void *);
174 Static void usb_task_thread(void *);
176 #if defined(__FreeBSD__) || defined(__DragonFly__)
177 Static cdev_t usb_dev; /* The /dev/usb device. */
178 Static int usb_ndevs; /* Number of /dev/usbN devices. */
181 #define USB_MAX_EVENTS 100
184 TAILQ_ENTRY(usb_event_q) next;
186 Static TAILQ_HEAD(, usb_event_q) usb_events =
187 TAILQ_HEAD_INITIALIZER(usb_events);
188 Static int usb_nevents = 0;
189 Static struct selinfo usb_selevent;
190 Static struct proc *usb_async_proc; /* process that wants USB SIGIO */
191 Static int usb_dev_open = 0;
192 Static void usb_add_event(int, struct usb_event *);
194 Static int usb_get_next_event(struct usb_event *);
196 Static const char *usbrev_str[] = USBREV_STR;
198 USB_DECLARE_DRIVER_INIT(usb,
199 DEVMETHOD(bus_child_detached, usb_child_detached),
200 DEVMETHOD(device_suspend, bus_generic_suspend),
201 DEVMETHOD(device_resume, bus_generic_resume),
202 DEVMETHOD(device_shutdown, bus_generic_shutdown)
205 #if defined(__FreeBSD__) || defined(__DragonFly__)
206 MODULE_VERSION(usb, 1);
211 DPRINTF(("usbd_match\n"));
212 return (UMATCH_GENERIC);
217 #if defined(__NetBSD__) || defined(__OpenBSD__)
218 struct usb_softc *sc = (struct usb_softc *)self;
219 #elif defined(__FreeBSD__) || defined(__DragonFly__)
220 struct usb_softc *sc = device_get_softc(self);
221 void *aux = device_get_ivars(self);
224 usbd_device_handle dev;
232 DPRINTF(("usbd_attach\n"));
236 sc->sc_bus->usbctl = sc;
237 sc->sc_port.power = USB_MAX_POWER;
239 #if defined(__FreeBSD__) || defined(__DragonFly__)
240 kprintf("%s", USBDEVNAME(sc->sc_dev));
242 usbrev = sc->sc_bus->usbrev;
243 kprintf(": USB revision %s", usbrev_str[usbrev]);
247 speed = USB_SPEED_FULL;
250 speed = USB_SPEED_HIGH;
253 kprintf(", not supported\n");
255 USB_ATTACH_ERROR_RETURN;
259 /* Make sure not to use tsleep() if we are cold booting. */
261 sc->sc_bus->use_polling++;
263 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
264 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
266 #ifdef USB_USE_SOFTINTR
267 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
268 /* XXX we should have our own level */
269 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
270 sc->sc_bus->methods->soft_intr, sc->sc_bus);
271 if (sc->sc_bus->soft == NULL) {
272 kprintf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
274 USB_ATTACH_ERROR_RETURN;
277 usb_callout_init(sc->sc_bus->softi);
281 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
284 dev = sc->sc_port.device;
285 if (dev->hub == NULL) {
287 kprintf("%s: root device is not a hub\n",
288 USBDEVNAME(sc->sc_dev));
289 USB_ATTACH_ERROR_RETURN;
291 sc->sc_bus->root_hub = dev;
294 * Turning this code off will delay attachment of USB devices
295 * until the USB event thread is running, which means that
296 * the keyboard will not work until after cold boot.
298 #if defined(__FreeBSD__) || defined(__DragonFly__)
301 * XXX Exploring high speed device here will
304 if (speed != USB_SPEED_HIGH)
305 dev->hub->explore(sc->sc_bus->root_hub);
308 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
309 dev->hub->explore(sc->sc_bus->root_hub);
313 kprintf("%s: root hub problem, error=%d\n",
314 USBDEVNAME(sc->sc_dev), err);
318 sc->sc_bus->use_polling--;
320 config_pending_incr();
321 #if defined(__NetBSD__) || defined(__OpenBSD__)
322 usb_kthread_create(usb_create_event_thread, sc);
325 #if defined(__FreeBSD__) || defined(__DragonFly__)
326 usb_create_event_thread(sc);
327 /* The per controller devices (used for usb_discover) */
328 /* XXX This is redundant now, but old usbd's will want it */
329 dev_ops_add(&usb_ops, -1, device_get_unit(self));
330 tmp_dev = make_dev(&usb_ops, device_get_unit(self),
331 UID_ROOT, GID_OPERATOR, 0660,
332 "usb%d", device_get_unit(self));
333 sc->sc_usbdev = reference_dev(tmp_dev);
334 if (usb_ndevs++ == 0) {
335 /* The device spitting out events */
336 dev_ops_add(&usb_ops, -1, USB_DEV_MINOR);
337 tmp_dev = make_dev(&usb_ops, USB_DEV_MINOR,
338 UID_ROOT, GID_OPERATOR, 0660, "usb");
339 usb_dev = reference_dev(tmp_dev);
343 USB_ATTACH_SUCCESS_RETURN;
346 Static const char *taskq_names[] = USB_TASKQ_NAMES;
349 usb_create_event_thread(void *arg)
351 struct usb_softc *sc = arg;
354 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
355 "%s", USBDEVNAME(sc->sc_dev))) {
356 kprintf("%s: unable to create event thread for\n",
357 USBDEVNAME(sc->sc_dev));
358 panic("usb_create_event_thread");
361 for (i = 0; i < USB_NUM_TASKQS; i++) {
362 struct usb_taskq *taskq = &usb_taskq[i];
364 if (taskq->taskcreated == 0) {
365 taskq->taskcreated = 1;
366 taskq->name = taskq_names[i];
367 TAILQ_INIT(&taskq->tasks);
368 if (usb_kthread_create2(usb_task_thread, taskq,
369 &taskq->task_thread_proc, taskq->name)) {
370 kprintf("unable to create task thread\n");
371 panic("usb_create_event_thread task");
378 * Add a task to be performed by the task thread. This function can be
379 * called from any context and the task will be executed in a process
383 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue)
385 struct usb_taskq *taskq;
389 taskq = &usb_taskq[queue];
390 if (task->queue == -1) {
391 DPRINTFN(2,("usb_add_task: task=%p\n", task));
392 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
395 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
397 wakeup(&taskq->tasks);
403 usb_do_task(usbd_device_handle dev, struct usb_task *task, int queue,
406 struct usb_taskq *taskq;
410 taskq = &usb_taskq[queue];
411 if (task->queue == -1) {
412 DPRINTFN(2,("usb_add_task: task=%p\n", task));
413 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
416 DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
418 wakeup(&taskq->tasks);
420 /* Wait until task is finished */
421 tsleep((&taskq->tasks + 1), 0, "usbdotsk", time_out);
427 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
430 if (task->queue != -1) {
431 TAILQ_REMOVE(&usb_taskq[task->queue].tasks, task, next);
438 usb_event_thread(void *arg)
440 struct usb_softc *sc = arg;
442 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
446 DPRINTF(("usb_event_thread: start\n"));
449 * In case this controller is a companion controller to an
450 * EHCI controller we need to wait until the EHCI controller
451 * has grabbed the port.
452 * XXX It would be nicer to do this with a tsleep(), but I don't
453 * know how to synchronize the creation of the threads so it
456 usb_delay_ms(sc->sc_bus, 500);
460 /* Make sure first discover does something. */
461 sc->sc_bus->needs_explore = 1;
463 config_pending_decr();
465 while (!sc->sc_dying) {
467 if (usb_noexplore < 2)
471 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt",
472 usb_noexplore ? 0 : hz * 60);
474 tsleep(&sc->sc_bus->needs_explore, 0, "usbevt", hz * 60);
476 DPRINTFN(2,("usb_event_thread: woke up\n"));
478 sc->sc_event_thread = NULL;
482 /* In case parent is waiting for us to exit. */
485 DPRINTF(("usb_event_thread: exit\n"));
490 usb_task_thread(void *arg)
492 struct usb_task *task;
493 struct usb_taskq *taskq;
495 #if defined(__FreeBSD__) && __FreeBSD_version >= 500000
502 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name));
504 while (usb_ndevs > 0) {
505 task = TAILQ_FIRST(&taskq->tasks);
507 tsleep(&taskq->tasks, 0, "usbtsk", 0);
508 task = TAILQ_FIRST(&taskq->tasks);
510 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
512 TAILQ_REMOVE(&taskq->tasks, task, next);
515 task->fun(task->arg);
517 wakeup((&taskq->tasks + 1));
523 taskq->taskcreated = 0;
524 wakeup(&taskq->taskcreated);
526 DPRINTF(("usb_event_thread: exit\n"));
529 #if defined(__NetBSD__) || defined(__OpenBSD__)
531 usbctlprint(void *aux, const char *pnp)
533 /* only "usb"es can attach to host controllers */
535 kprintf("usb at %s", pnp);
539 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
542 usbopen(struct dev_open_args *ap)
544 cdev_t dev = ap->a_head.a_dev;
545 int unit = USBUNIT(dev);
546 struct usb_softc *sc;
548 if (unit == USB_DEV_MINOR) {
552 usb_async_proc = NULL;
556 USB_GET_SC_OPEN(usb, unit, sc);
565 usbread(struct dev_read_args *ap)
567 cdev_t dev = ap->a_head.a_dev;
568 struct uio *uio = ap->a_uio;
570 int unit = USBUNIT(dev);
573 if (unit != USB_DEV_MINOR)
576 if (uio->uio_resid != sizeof(struct usb_event))
582 n = usb_get_next_event(&ue);
585 if (ap->a_ioflag & IO_NDELAY) {
589 error = tsleep(&usb_events, PCATCH, "usbrea", 0);
595 error = uiomove((void *)&ue, uio->uio_resid, uio);
601 usbclose(struct dev_close_args *ap)
603 cdev_t dev = ap->a_head.a_dev;
604 int unit = USBUNIT(dev);
606 if (unit == USB_DEV_MINOR) {
607 usb_async_proc = NULL;
615 usbioctl(struct dev_ioctl_args *ap)
617 cdev_t devt = ap->a_head.a_dev;
618 struct usb_softc *sc;
619 int unit = USBUNIT(devt);
621 if (unit == USB_DEV_MINOR) {
624 if (*(int *)ap->a_data)
625 #if defined(__DragonFly__)
626 usb_async_proc = curproc;
627 #elif __FreeBSD_version >= 500000
628 usb_async_proc = p->td_proc;
633 usb_async_proc = NULL;
641 USB_GET_SC(usb, unit, sc);
647 #if defined(__FreeBSD__) || defined(__DragonFly__)
648 /* This part should be deleted */
654 struct usb_ctl_request *ur = (void *)ap->a_data;
655 int len = UGETW(ur->ucr_request.wLength);
659 int addr = ur->ucr_addr;
663 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
664 if (len < 0 || len > 32768)
666 if (addr < 0 || addr >= USB_MAX_DEVICES ||
667 sc->sc_bus->devices[addr] == 0)
670 iov.iov_base = (caddr_t)ur->ucr_data;
676 uio.uio_segflg = UIO_USERSPACE;
678 ur->ucr_request.bmRequestType & UT_READ ?
679 UIO_READ : UIO_WRITE;
680 uio.uio_td = curthread;
681 ptr = kmalloc(len, M_TEMP, M_WAITOK);
682 if (uio.uio_rw == UIO_WRITE) {
683 error = uiomove(ptr, len, &uio);
688 err = usbd_do_request_flags(sc->sc_bus->devices[addr],
689 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
690 USBD_DEFAULT_TIMEOUT);
696 if (uio.uio_rw == UIO_READ) {
697 error = uiomove(ptr, len, &uio);
710 struct usb_device_info *di = (void *)ap->a_data;
711 int addr = di->udi_addr;
712 usbd_device_handle dev;
714 if (addr < 1 || addr >= USB_MAX_DEVICES)
716 dev = sc->sc_bus->devices[addr];
719 usbd_fill_deviceinfo(dev, di, 1);
723 case USB_DEVICESTATS:
724 *(struct usb_device_stats *)ap->a_data = sc->sc_bus->stats;
734 usbpoll(struct dev_poll_args *ap)
736 cdev_t dev = ap->a_head.a_dev;
738 int unit = USBUNIT(dev);
740 if (unit == USB_DEV_MINOR) {
742 mask = POLLIN | POLLRDNORM;
745 if (ap->a_events & mask && usb_nevents > 0)
746 revents |= ap->a_events & mask;
747 if (revents == 0 && ap->a_events & mask)
748 selrecord(curthread, &usb_selevent);
750 ap->a_events = revents;
754 return (0); /* select/poll never wakes up - back compat */
758 /* Explore device tree from the root. */
760 usb_discover(void *v)
762 struct usb_softc *sc = v;
764 DPRINTFN(2,("usb_discover\n"));
766 if (usb_noexplore > 1)
771 * We need mutual exclusion while traversing the device tree,
772 * but this is guaranteed since this function is only called
773 * from the event thread for the controller.
776 while (sc->sc_bus->needs_explore && !sc->sc_dying) {
777 sc->sc_bus->needs_explore = 0;
780 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
788 usb_needs_explore(usbd_device_handle dev)
790 DPRINTFN(2,("usb_needs_explore\n"));
791 dev->bus->needs_explore = 1;
792 wakeup(&dev->bus->needs_explore);
795 /* Called from a critical section */
797 usb_get_next_event(struct usb_event *ue)
799 struct usb_event_q *ueq;
801 if (usb_nevents <= 0)
803 ueq = TAILQ_FIRST(&usb_events);
806 kprintf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
812 TAILQ_REMOVE(&usb_events, ueq, next);
813 kfree(ueq, M_USBDEV);
819 usbd_add_dev_event(int type, usbd_device_handle udev)
823 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
824 usb_add_event(type, &ue);
828 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
832 ue.u.ue_driver.ue_cookie = udev->cookie;
833 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
834 sizeof ue.u.ue_driver.ue_devname);
835 usb_add_event(type, &ue);
839 usb_add_event(int type, struct usb_event *uep)
841 struct usb_event_q *ueq;
843 struct timeval thetime;
845 ueq = kmalloc(sizeof *ueq, M_USBDEV, M_INTWAIT);
847 ueq->ue.ue_type = type;
849 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
852 if (USB_EVENT_IS_DETACH(type)) {
853 struct usb_event_q *ueqi, *ueqi_next;
855 for (ueqi = TAILQ_FIRST(&usb_events); ueqi; ueqi = ueqi_next) {
856 ueqi_next = TAILQ_NEXT(ueqi, next);
857 if (ueqi->ue.u.ue_driver.ue_cookie.cookie ==
858 uep->u.ue_device.udi_cookie.cookie) {
859 TAILQ_REMOVE(&usb_events, ueqi, next);
860 kfree(ueqi, M_USBDEV);
862 ueqi_next = TAILQ_FIRST(&usb_events);
866 if (usb_nevents >= USB_MAX_EVENTS) {
867 /* Too many queued events, drop an old one. */
868 DPRINTF(("usb: event dropped\n"));
869 usb_get_next_event(&ue);
871 TAILQ_INSERT_TAIL(&usb_events, ueq, next);
874 selwakeuppri(&usb_selevent, 0);
875 if (usb_async_proc != NULL) {
876 PROC_LOCK(usb_async_proc);
877 ksignal(usb_async_proc, SIGIO);
878 PROC_UNLOCK(usb_async_proc);
884 usb_schedsoftintr(usbd_bus_handle bus)
886 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
887 #ifdef USB_USE_SOFTINTR
888 if (bus->use_polling) {
889 bus->methods->soft_intr(bus);
891 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
892 softintr_schedule(bus->soft);
894 if (!callout_pending(&bus->softi))
895 callout_reset(&bus->softi, 0, bus->methods->soft_intr,
897 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
900 bus->methods->soft_intr(bus);
901 #endif /* USB_USE_SOFTINTR */
904 #if defined(__NetBSD__) || defined(__OpenBSD__)
906 usb_activate(device_ptr_t self, enum devact act)
908 struct usb_softc *sc = (struct usb_softc *)self;
909 usbd_device_handle dev = sc->sc_port.device;
916 case DVACT_DEACTIVATE:
918 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
919 for (i = 0; dev->subdevs[i]; i++)
920 rv |= config_deactivate(dev->subdevs[i]);
930 USB_DETACH_START(usb, sc);
933 DPRINTF(("usb_detach: start\n"));
937 /* Make all devices disconnect. */
938 if (sc->sc_port.device != NULL)
939 usb_disconnect_port(&sc->sc_port, self);
941 /* Kill off event thread. */
942 if (sc->sc_event_thread != NULL) {
943 wakeup(&sc->sc_bus->needs_explore);
944 if (tsleep(sc, 0, "usbdet", hz * 60))
945 kprintf("%s: event thread didn't die\n",
946 USBDEVNAME(sc->sc_dev));
947 DPRINTF(("usb_detach: event thread dead\n"));
950 #if defined(__FreeBSD__) || defined(__DragonFly__)
951 destroy_dev(sc->sc_usbdev);
952 if (--usb_ndevs == 0) {
955 destroy_dev(usb_dev);
958 for (i = 0; i < USB_NUM_TASKQS; i++) {
959 struct usb_taskq *taskq = &usb_taskq[i];
960 wakeup(&taskq->tasks);
961 if (tsleep(&taskq->taskcreated, 0, "usbtdt",
963 kprintf("usb task thread %s didn't die\n",
972 #ifdef USB_USE_SOFTINTR
973 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
974 if (sc->sc_bus->soft != NULL) {
975 softintr_disestablish(sc->sc_bus->soft);
976 sc->sc_bus->soft = NULL;
979 callout_stop(&sc->sc_bus->softi);
983 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
984 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
989 #if defined(__FreeBSD__) || defined(__DragonFly__)
991 usb_child_detached(device_t self, device_t child)
993 struct usb_softc *sc = device_get_softc(self);
995 /* XXX, should check it is the right device. */
996 sc->sc_port.device = NULL;
999 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
1000 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
1001 DRIVER_MODULE(usb, ehci, usb_driver, usb_devclass, 0, 0);