Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.32
      1 /*	$NetBSD: usb.c,v 1.32 1999/11/20 01:15:25 augustss Exp $	*/
      2 /*	$FreeBSD: src/sys/dev/usb/usb.c,v 1.20 1999/11/17 22:33:46 n_hibma Exp $	*/
      3 
      4 /*
      5  * Copyright (c) 1998 The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Lennart Augustsson (augustss (at) carlstedt.se) at
     10  * Carlstedt Research & Technology.
     11  *
     12  * Redistribution and use in source and binary forms, with or without
     13  * modification, are permitted provided that the following conditions
     14  * are met:
     15  * 1. Redistributions of source code must retain the above copyright
     16  *    notice, this list of conditions and the following disclaimer.
     17  * 2. Redistributions in binary form must reproduce the above copyright
     18  *    notice, this list of conditions and the following disclaimer in the
     19  *    documentation and/or other materials provided with the distribution.
     20  * 3. All advertising materials mentioning features or use of this software
     21  *    must display the following acknowledgement:
     22  *        This product includes software developed by the NetBSD
     23  *        Foundation, Inc. and its contributors.
     24  * 4. Neither the name of The NetBSD Foundation nor the names of its
     25  *    contributors may be used to endorse or promote products derived
     26  *    from this software without specific prior written permission.
     27  *
     28  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     29  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     30  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     31  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     32  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     33  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     34  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     35  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     36  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     37  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     38  * POSSIBILITY OF SUCH DAMAGE.
     39  */
     40 
     41 /*
     42  * USB specifications and other documentation can be found at
     43  * http://www.usb.org/developers/data/ and
     44  * http://www.usb.org/developers/index.html .
     45  */
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/malloc.h>
     51 #if defined(__NetBSD__) || defined(__OpenBSD__)
     52 #include <sys/device.h>
     53 #include <sys/kthread.h>
     54 #include <sys/proc.h>
     55 #elif defined(__FreeBSD__)
     56 #include <sys/module.h>
     57 #include <sys/bus.h>
     58 #include <sys/filio.h>
     59 #include <sys/uio.h>
     60 #endif
     61 #include <sys/conf.h>
     62 #include <sys/poll.h>
     63 #include <sys/select.h>
     64 #include <sys/vnode.h>
     65 #include <sys/signalvar.h>
     66 
     67 #include <dev/usb/usb.h>
     68 #include <dev/usb/usbdi.h>
     69 #include <dev/usb/usbdi_util.h>
     70 
     71 #define USB_DEV_MINOR 255
     72 
     73 #if defined(__FreeBSD__)
     74 MALLOC_DEFINE(M_USB, "USB", "USB");
     75 MALLOC_DEFINE(M_USBDEV, "USBdev", "USB device");
     76 MALLOC_DEFINE(M_USBHC, "USBHC", "USB host controller");
     77 
     78 #include "usb_if.h"
     79 #endif /* defined(__FreeBSD__) */
     80 
     81 #include <machine/bus.h>
     82 
     83 #include <dev/usb/usbdivar.h>
     84 #include <dev/usb/usb_quirks.h>
     85 
     86 #ifdef USB_DEBUG
     87 #define DPRINTF(x)	if (usbdebug) logprintf x
     88 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     89 int	usbdebug = 0;
     90 #ifdef UHCI_DEBUG
     91 extern int uhcidebug;
     92 #endif
     93 #ifdef OHCI_DEBUG
     94 extern int ohcidebug;
     95 #endif
     96 int	usb_noexplore = 0;
     97 #else
     98 #define DPRINTF(x)
     99 #define DPRINTFN(n,x)
    100 #endif
    101 
    102 struct usb_softc {
    103 	USBBASEDEVICE	sc_dev;		/* base device */
    104 	usbd_bus_handle sc_bus;		/* USB controller */
    105 	struct usbd_port sc_port;	/* dummy port for root hub */
    106 
    107 #if defined(__FreeBSD__)
    108 	/* This part should be deleted when kthreads is available */
    109 	struct selinfo	sc_consel;	/* waiting for connect change */
    110 #else
    111 	struct proc    *sc_event_thread;
    112 #endif
    113 
    114 	char		sc_dying;
    115 };
    116 
    117 #if defined(__NetBSD__) || defined(__OpenBSD__)
    118 cdev_decl(usb);
    119 #elif defined(__FreeBSD__)
    120 d_open_t  usbopen;
    121 d_close_t usbclose;
    122 d_read_t usbread;
    123 d_ioctl_t usbioctl;
    124 int usbpoll __P((dev_t, int, struct proc *));
    125 
    126 struct cdevsw usb_cdevsw = {
    127 	/* open */      usbopen,
    128 	/* close */     usbclose,
    129 	/* read */      noread,
    130 	/* write */     nowrite,
    131 	/* ioctl */     usbioctl,
    132 	/* poll */      usbpoll,
    133 	/* mmap */      nommap,
    134 	/* strategy */  nostrategy,
    135 	/* name */      "usb",
    136 	/* maj */       USB_CDEV_MAJOR,
    137 	/* dump */      nodump,
    138 	/* psize */     nopsize,
    139 	/* flags */     0,
    140 	/* bmaj */      -1
    141 };
    142 #endif
    143 
    144 static usbd_status usb_discover __P((struct usb_softc *));
    145 static void	usb_create_event_thread __P((void *));
    146 static void	usb_event_thread __P((void *));
    147 
    148 #define USB_MAX_EVENTS 50
    149 struct usb_event_q {
    150 	struct usb_event ue;
    151 	SIMPLEQ_ENTRY(usb_event_q) next;
    152 };
    153 static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    154 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    155 static int usb_nevents = 0;
    156 static struct selinfo usb_selevent;
    157 static struct proc *usb_async_proc;  /* process who wants USB SIGIO */
    158 static int usb_dev_open = 0;
    159 
    160 static int usb_get_next_event __P((struct usb_event *));
    161 
    162 #if defined(__NetBSD__) || defined(__OpenBSD__)
    163 /* Flag to see if we are in the cold boot process. */
    164 extern int cold;
    165 #endif
    166 
    167 static const char *usbrev_str[] = USBREV_STR;
    168 
    169 USB_DECLARE_DRIVER(usb);
    170 
    171 USB_MATCH(usb)
    172 {
    173 	DPRINTF(("usbd_match\n"));
    174 	return (UMATCH_GENERIC);
    175 }
    176 
    177 USB_ATTACH(usb)
    178 {
    179 #if defined(__NetBSD__) || defined(__OpenBSD__)
    180 	struct usb_softc *sc = (struct usb_softc *)self;
    181 #elif defined(__FreeBSD__)
    182 	struct usb_softc *sc = device_get_softc(self);
    183 	void *aux = device_get_ivars(self);
    184 #endif
    185 	usbd_device_handle dev;
    186 	usbd_status err;
    187 	int usbrev;
    188 
    189 #if defined(__FreeBSD__)
    190 	printf("%s", USBDEVNAME(sc->sc_dev));
    191 	sc->sc_dev = self;
    192 #endif
    193 
    194 	DPRINTF(("usbd_attach\n"));
    195 
    196 	usbd_init();
    197 	sc->sc_bus = aux;
    198 	sc->sc_bus->usbctl = sc;
    199 	sc->sc_port.power = USB_MAX_POWER;
    200 
    201 	usbrev = sc->sc_bus->usbrev;
    202 	printf(": USB revision %s", usbrev_str[usbrev]);
    203 	if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
    204 		printf(", not supported\n");
    205 		USB_ATTACH_ERROR_RETURN;
    206 	}
    207 	printf("\n");
    208 
    209 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
    210 		  &sc->sc_port);
    211 	if (!err) {
    212 		dev = sc->sc_port.device;
    213 		if (dev->hub == NULL) {
    214 			sc->sc_dying = 1;
    215 			printf("%s: root device is not a hub\n",
    216 			       USBDEVNAME(sc->sc_dev));
    217 			USB_ATTACH_ERROR_RETURN;
    218 		}
    219 		sc->sc_bus->root_hub = dev;
    220 #if 1
    221 		/*
    222 		 * Turning this code off will delay attachment of USB devices
    223 		 * until the USB event thread is running, which means that
    224 		 * the keyboard will not work until after cold boot.
    225 		 */
    226 		if (cold) {
    227 			sc->sc_bus->use_polling++;
    228 			dev->hub->explore(sc->sc_bus->root_hub);
    229 			sc->sc_bus->use_polling--;
    230 		}
    231 #endif
    232 	} else {
    233 		printf("%s: root hub problem, error=%d\n",
    234 		       USBDEVNAME(sc->sc_dev), err);
    235 		sc->sc_dying = 1;
    236 	}
    237 
    238 	kthread_create(usb_create_event_thread, sc);
    239 
    240 #if defined(__FreeBSD__)
    241 	make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
    242 		 0644, "usb%d", device_get_unit(self));
    243 #endif
    244 
    245 	USB_ATTACH_SUCCESS_RETURN;
    246 }
    247 
    248 #if defined(__NetBSD__) || defined(__OpenBSD__)
    249 void
    250 usb_create_event_thread(arg)
    251 	void *arg;
    252 {
    253 	struct usb_softc *sc = arg;
    254 
    255 	if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    256 			   "%s", sc->sc_dev.dv_xname)) {
    257 		printf("%s: unable to create event thread for\n",
    258 		       sc->sc_dev.dv_xname);
    259 		panic("usb_create_event_thread");
    260 	}
    261 }
    262 
    263 void
    264 usb_event_thread(arg)
    265 	void *arg;
    266 {
    267 	struct usb_softc *sc = arg;
    268 
    269 	DPRINTF(("usb_event_thread: start\n"));
    270 
    271 	while (!sc->sc_dying) {
    272 #ifdef USB_DEBUG
    273 		if (!usb_noexplore)
    274 #endif
    275 		usb_discover(sc);
    276 		(void)tsleep(&sc->sc_bus->needs_explore,
    277 			     PWAIT, "usbevt", hz*60);
    278 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    279 	}
    280 	sc->sc_event_thread = 0;
    281 
    282 	/* In case parent is waiting for us to exit. */
    283 	wakeup(sc);
    284 
    285 	DPRINTF(("usb_event_thread: exit\n"));
    286 	kthread_exit(0);
    287 }
    288 
    289 int
    290 usbctlprint(aux, pnp)
    291 	void *aux;
    292 	const char *pnp;
    293 {
    294 	/* only "usb"es can attach to host controllers */
    295 	if (pnp)
    296 		printf("usb at %s", pnp);
    297 
    298 	return (UNCONF);
    299 }
    300 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    301 
    302 int
    303 usbopen(dev, flag, mode, p)
    304 	dev_t dev;
    305 	int flag, mode;
    306 	struct proc *p;
    307 {
    308 	int unit = minor(dev);
    309 	struct usb_softc *sc;
    310 
    311 	if (unit == USB_DEV_MINOR) {
    312 		if (usb_dev_open)
    313 			return (EBUSY);
    314 		usb_dev_open = 1;
    315 		usb_async_proc = 0;
    316 		return (0);
    317 	}
    318 
    319 	USB_GET_SC_OPEN(usb, unit, sc);
    320 
    321 	if (sc->sc_dying)
    322 		return (EIO);
    323 
    324 	return (0);
    325 }
    326 
    327 int
    328 usbread(dev, uio, flag)
    329 	dev_t dev;
    330 	struct uio *uio;
    331 	int flag;
    332 {
    333 	struct usb_event ue;
    334 	int s, error, n;
    335 
    336 	if (minor(dev) != USB_DEV_MINOR)
    337 		return (ENXIO);
    338 
    339 	if (uio->uio_resid != sizeof(struct usb_event))
    340 		return (EINVAL);
    341 
    342 	error = 0;
    343 	s = splusb();
    344 	for (;;) {
    345 		n = usb_get_next_event(&ue);
    346 		if (n != 0)
    347 			break;
    348 		if (flag & IO_NDELAY) {
    349 			error = EWOULDBLOCK;
    350 			break;
    351 		}
    352 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    353 		if (error)
    354 			break;
    355 	}
    356 	splx(s);
    357 	if (!error)
    358 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    359 
    360 	return (error);
    361 }
    362 
    363 int
    364 usbclose(dev, flag, mode, p)
    365 	dev_t dev;
    366 	int flag, mode;
    367 	struct proc *p;
    368 {
    369 	int unit = minor(dev);
    370 
    371 	if (unit == USB_DEV_MINOR) {
    372 		usb_async_proc = 0;
    373 		usb_dev_open = 0;
    374 	}
    375 
    376 	return (0);
    377 }
    378 
    379 int
    380 usbioctl(devt, cmd, data, flag, p)
    381 	dev_t devt;
    382 	u_long cmd;
    383 	caddr_t data;
    384 	int flag;
    385 	struct proc *p;
    386 {
    387 	struct usb_softc *sc;
    388 	int unit = minor(devt);
    389 
    390 	if (unit == USB_DEV_MINOR) {
    391 		switch (cmd) {
    392 		case FIONBIO:
    393 			/* All handled in the upper FS layer. */
    394 			return (0);
    395 
    396 		case FIOASYNC:
    397 			if (*(int *)data)
    398 				usb_async_proc = p;
    399 			else
    400 				usb_async_proc = 0;
    401 			return (0);
    402 
    403 		default:
    404 			return (EINVAL);
    405 		}
    406 	}
    407 
    408 	USB_GET_SC(usb, unit, sc);
    409 
    410 	if (sc->sc_dying)
    411 		return (EIO);
    412 
    413 	switch (cmd) {
    414 #if defined(__FreeBSD__)
    415 	/* This part should be deleted when kthreads is available */
    416   	case USB_DISCOVER:
    417   		usb_discover(sc);
    418   		break;
    419 #endif
    420 #ifdef USB_DEBUG
    421 	case USB_SETDEBUG:
    422 		usbdebug  = ((*(int *)data) & 0x000000ff);
    423 #ifdef UHCI_DEBUG
    424 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    425 #endif
    426 #ifdef OHCI_DEBUG
    427 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    428 #endif
    429 		break;
    430 #endif
    431 	case USB_REQUEST:
    432 	{
    433 		struct usb_ctl_request *ur = (void *)data;
    434 		int len = UGETW(ur->request.wLength);
    435 		struct iovec iov;
    436 		struct uio uio;
    437 		void *ptr = 0;
    438 		int addr = ur->addr;
    439 		usbd_status err;
    440 		int error = 0;
    441 
    442 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    443 		if (len < 0 || len > 32768)
    444 			return (EINVAL);
    445 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    446 		    sc->sc_bus->devices[addr] == 0)
    447 			return (EINVAL);
    448 		if (len != 0) {
    449 			iov.iov_base = (caddr_t)ur->data;
    450 			iov.iov_len = len;
    451 			uio.uio_iov = &iov;
    452 			uio.uio_iovcnt = 1;
    453 			uio.uio_resid = len;
    454 			uio.uio_offset = 0;
    455 			uio.uio_segflg = UIO_USERSPACE;
    456 			uio.uio_rw =
    457 				ur->request.bmRequestType & UT_READ ?
    458 				UIO_READ : UIO_WRITE;
    459 			uio.uio_procp = p;
    460 			ptr = malloc(len, M_TEMP, M_WAITOK);
    461 			if (uio.uio_rw == UIO_WRITE) {
    462 				error = uiomove(ptr, len, &uio);
    463 				if (error)
    464 					goto ret;
    465 			}
    466 		}
    467 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    468 			  &ur->request, ptr, ur->flags, &ur->actlen);
    469 		if (err) {
    470 			error = EIO;
    471 			goto ret;
    472 		}
    473 		if (len != 0) {
    474 			if (uio.uio_rw == UIO_READ) {
    475 				error = uiomove(ptr, len, &uio);
    476 				if (error)
    477 					goto ret;
    478 			}
    479 		}
    480 	ret:
    481 		if (ptr)
    482 			free(ptr, M_TEMP);
    483 		return (error);
    484 	}
    485 
    486 	case USB_DEVICEINFO:
    487 	{
    488 		struct usb_device_info *di = (void *)data;
    489 		int addr = di->addr;
    490 		usbd_device_handle dev;
    491 
    492 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    493 			return (EINVAL);
    494 		dev = sc->sc_bus->devices[addr];
    495 		if (dev == NULL)
    496 			return (ENXIO);
    497 		usbd_fill_deviceinfo(dev, di);
    498 		break;
    499 	}
    500 
    501 	case USB_DEVICESTATS:
    502 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    503 		break;
    504 
    505 	default:
    506 		return (EINVAL);
    507 	}
    508 	return (0);
    509 }
    510 
    511 int
    512 usbpoll(dev, events, p)
    513 	dev_t dev;
    514 	int events;
    515 	struct proc *p;
    516 {
    517 	int revents, mask, s;
    518 
    519 	if (minor(dev) == USB_DEV_MINOR) {
    520 		revents = 0;
    521 		mask = POLLIN | POLLRDNORM;
    522 
    523 		s = splusb();
    524 		if (events & mask && usb_nevents > 0)
    525 			revents |= events & mask;
    526 		if (revents == 0 && events & mask)
    527 			selrecord(p, &usb_selevent);
    528 		splx(s);
    529 
    530 		return (revents);
    531 	} else {
    532 #if defined(__FreeBSD__)
    533 		/* This part should be deleted when kthreads is available */
    534 		struct usb_softc *sc;
    535 		int unit = minor(dev);
    536 
    537 		USB_GET_SC(usb, unit, sc);
    538 
    539 		revents = 0;
    540 		mask = POLLOUT | POLLRDNORM;
    541 
    542 		s = splusb();
    543 		if (events & mask && sc->sc_bus->needs_explore)
    544 			revents |= events & mask;
    545 		if (revents == 0 && events & mask)
    546 			selrecord(p, &sc->sc_consel);
    547 		splx(s);
    548 
    549 		return (revents);
    550 #else
    551 		return (ENXIO);
    552 #endif
    553 	}
    554 }
    555 
    556 /* Explore device tree from the root. */
    557 usbd_status
    558 usb_discover(sc)
    559 	struct usb_softc *sc;
    560 {
    561 #if defined(__FreeBSD__)
    562 	/* The splxxx parts should be deleted when kthreads is available */
    563 	int s;
    564 #endif
    565 
    566 	/*
    567 	 * We need mutual exclusion while traversing the device tree,
    568 	 * but this is guaranteed since this function is only called
    569 	 * from the event thread for the controller.
    570 	 */
    571 #if defined(__FreeBSD__)
    572 	s = splusb();
    573 #endif
    574 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    575 		sc->sc_bus->needs_explore = 0;
    576 #if defined(__FreeBSD__)
    577 		splx(s);
    578 #endif
    579 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    580 #if defined(__FreeBSD__)
    581 		s = splusb();
    582 #endif
    583 	}
    584 #if defined(__FreeBSD__)
    585 	splx(s);
    586 #endif
    587 
    588 	return (USBD_NORMAL_COMPLETION);
    589 }
    590 
    591 void
    592 usb_needs_explore(bus)
    593 	usbd_bus_handle bus;
    594 {
    595 	bus->needs_explore = 1;
    596 #if defined(__FreeBSD__)
    597 	/* This part should be deleted when kthreads is available */
    598 	selwakeup(&bus->usbctl->sc_consel);
    599 #endif
    600 	wakeup(&bus->needs_explore);
    601 }
    602 
    603 /* Called at splusb() */
    604 int
    605 usb_get_next_event(ue)
    606 	struct usb_event *ue;
    607 {
    608 	struct usb_event_q *ueq;
    609 
    610 	if (usb_nevents <= 0)
    611 		return (0);
    612 	ueq = SIMPLEQ_FIRST(&usb_events);
    613 	*ue = ueq->ue;
    614 	SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
    615 	free(ueq, M_USBDEV);
    616 	usb_nevents--;
    617 	return (1);
    618 }
    619 
    620 void
    621 usbd_add_event(type, dev)
    622 	int type;
    623 	usbd_device_handle dev;
    624 {
    625 	struct usb_event_q *ueq;
    626 	struct usb_event ue;
    627 	struct timeval thetime;
    628 	int s;
    629 
    630 	s = splusb();
    631 	if (++usb_nevents >= USB_MAX_EVENTS) {
    632 		/* Too many queued events, drop an old one. */
    633 		DPRINTFN(-1,("usb: event dropped\n"));
    634 		(void)usb_get_next_event(&ue);
    635 	}
    636 	/* Don't want to wait here inside splusb() */
    637 	ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
    638 	if (ueq == NULL) {
    639 		printf("usb: no memory, event dropped\n");
    640 		splx(s);
    641 		return;
    642 	}
    643 	ueq->ue.ue_type = type;
    644 	ueq->ue.ue_cookie = dev->cookie;
    645 	usbd_fill_deviceinfo(dev, &ueq->ue.ue_device);
    646 	microtime(&thetime);
    647 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    648 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    649 	wakeup(&usb_events);
    650 	selwakeup(&usb_selevent);
    651 	if (usb_async_proc != NULL)
    652 		psignal(usb_async_proc, SIGIO);
    653 	splx(s);
    654 }
    655 
    656 #if defined(__NetBSD__) || defined(__OpenBSD__)
    657 int
    658 usb_activate(self, act)
    659 	device_ptr_t self;
    660 	enum devact act;
    661 {
    662 	struct usb_softc *sc = (struct usb_softc *)self;
    663 	usbd_device_handle dev = sc->sc_port.device;
    664 	int i, rv = 0;
    665 
    666 	switch (act) {
    667 	case DVACT_ACTIVATE:
    668 		return (EOPNOTSUPP);
    669 		break;
    670 
    671 	case DVACT_DEACTIVATE:
    672 		sc->sc_dying = 1;
    673 		if (dev && dev->cdesc && dev->subdevs) {
    674 			for (i = 0; dev->subdevs[i]; i++)
    675 				rv |= config_deactivate(dev->subdevs[i]);
    676 		}
    677 		break;
    678 	}
    679 	return (rv);
    680 }
    681 
    682 int
    683 usb_detach(self, flags)
    684 	device_ptr_t self;
    685 	int flags;
    686 {
    687 	struct usb_softc *sc = (struct usb_softc *)self;
    688 
    689 	DPRINTF(("usb_detach: start\n"));
    690 
    691 	sc->sc_dying = 1;
    692 
    693 	/* Make all devices disconnect. */
    694 	if (sc->sc_port.device)
    695 		usb_disconnect_port(&sc->sc_port, self);
    696 
    697 	/* Kill off event thread. */
    698 	if (sc->sc_event_thread) {
    699 		wakeup(&sc->sc_bus->needs_explore);
    700 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    701 			printf("%s: event thread didn't die\n",
    702 			       USBDEVNAME(sc->sc_dev));
    703 		DPRINTF(("usb_detach: event thread dead\n"));
    704 	}
    705 
    706 	usbd_finish();
    707 	return (0);
    708 }
    709 #elif defined(__FreeBSD__)
    710 int
    711 usb_detach(device_t self)
    712 {
    713 	DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
    714 
    715 	return (EINVAL);
    716 }
    717 #endif
    718 
    719 
    720 #if defined(__FreeBSD__)
    721 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
    722 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
    723 #endif
    724