Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.37
      1 /*	$NetBSD: usb.c,v 1.37 2000/01/24 18:35:51 thorpej 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 int	uhcidebug;
     92 #endif
     93 #ifdef OHCI_DEBUG
     94 int	ohcidebug;
     95 #endif
     96 /*
     97  * 0  - do usual exploration
     98  * 1  - do not use timeout exploration
     99  * >1 - do no exploration
    100  */
    101 int	usb_noexplore = 0;
    102 #else
    103 #define DPRINTF(x)
    104 #define DPRINTFN(n,x)
    105 #endif
    106 
    107 struct usb_softc {
    108 	USBBASEDEVICE	sc_dev;		/* base device */
    109 	usbd_bus_handle sc_bus;		/* USB controller */
    110 	struct usbd_port sc_port;	/* dummy port for root hub */
    111 
    112 #if defined(__FreeBSD__)
    113 	/* This part should be deleted when kthreads is available */
    114 	struct selinfo	sc_consel;	/* waiting for connect change */
    115 #else
    116 	struct proc    *sc_event_thread;
    117 #endif
    118 
    119 	char		sc_dying;
    120 };
    121 
    122 #if defined(__NetBSD__) || defined(__OpenBSD__)
    123 cdev_decl(usb);
    124 #elif defined(__FreeBSD__)
    125 d_open_t  usbopen;
    126 d_close_t usbclose;
    127 d_read_t usbread;
    128 d_ioctl_t usbioctl;
    129 int usbpoll __P((dev_t, int, struct proc *));
    130 
    131 struct cdevsw usb_cdevsw = {
    132 	/* open */      usbopen,
    133 	/* close */     usbclose,
    134 	/* read */      noread,
    135 	/* write */     nowrite,
    136 	/* ioctl */     usbioctl,
    137 	/* poll */      usbpoll,
    138 	/* mmap */      nommap,
    139 	/* strategy */  nostrategy,
    140 	/* name */      "usb",
    141 	/* maj */       USB_CDEV_MAJOR,
    142 	/* dump */      nodump,
    143 	/* psize */     nopsize,
    144 	/* flags */     0,
    145 	/* bmaj */      -1
    146 };
    147 #endif
    148 
    149 static usbd_status usb_discover __P((struct usb_softc *));
    150 static void	usb_create_event_thread __P((void *));
    151 static void	usb_event_thread __P((void *));
    152 
    153 #define USB_MAX_EVENTS 50
    154 struct usb_event_q {
    155 	struct usb_event ue;
    156 	SIMPLEQ_ENTRY(usb_event_q) next;
    157 };
    158 static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    159 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    160 static int usb_nevents = 0;
    161 static struct selinfo usb_selevent;
    162 static struct proc *usb_async_proc;  /* process who wants USB SIGIO */
    163 static int usb_dev_open = 0;
    164 
    165 static int usb_get_next_event __P((struct usb_event *));
    166 
    167 #if defined(__NetBSD__) || defined(__OpenBSD__)
    168 /* Flag to see if we are in the cold boot process. */
    169 extern int cold;
    170 #endif
    171 
    172 static const char *usbrev_str[] = USBREV_STR;
    173 
    174 USB_DECLARE_DRIVER(usb);
    175 
    176 USB_MATCH(usb)
    177 {
    178 	DPRINTF(("usbd_match\n"));
    179 	return (UMATCH_GENERIC);
    180 }
    181 
    182 USB_ATTACH(usb)
    183 {
    184 #if defined(__NetBSD__) || defined(__OpenBSD__)
    185 	struct usb_softc *sc = (struct usb_softc *)self;
    186 #elif defined(__FreeBSD__)
    187 	struct usb_softc *sc = device_get_softc(self);
    188 	void *aux = device_get_ivars(self);
    189 #endif
    190 	usbd_device_handle dev;
    191 	usbd_status err;
    192 	int usbrev;
    193 
    194 #if defined(__FreeBSD__)
    195 	printf("%s", USBDEVNAME(sc->sc_dev));
    196 	sc->sc_dev = self;
    197 #endif
    198 
    199 	DPRINTF(("usbd_attach\n"));
    200 
    201 	usbd_init();
    202 	sc->sc_bus = aux;
    203 	sc->sc_bus->usbctl = sc;
    204 	sc->sc_port.power = USB_MAX_POWER;
    205 
    206 	usbrev = sc->sc_bus->usbrev;
    207 	printf(": USB revision %s", usbrev_str[usbrev]);
    208 	if (usbrev != USBREV_1_0 && usbrev != USBREV_1_1) {
    209 		printf(", not supported\n");
    210 		USB_ATTACH_ERROR_RETURN;
    211 	}
    212 	printf("\n");
    213 
    214 	/* Make sure not to use tsleep() if we are cold booting. */
    215 	if (cold)
    216 		sc->sc_bus->use_polling++;
    217 
    218 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, 0, 0,
    219 		  &sc->sc_port);
    220 	if (!err) {
    221 		dev = sc->sc_port.device;
    222 		if (dev->hub == NULL) {
    223 			sc->sc_dying = 1;
    224 			printf("%s: root device is not a hub\n",
    225 			       USBDEVNAME(sc->sc_dev));
    226 			USB_ATTACH_ERROR_RETURN;
    227 		}
    228 		sc->sc_bus->root_hub = dev;
    229 #if 1
    230 		/*
    231 		 * Turning this code off will delay attachment of USB devices
    232 		 * until the USB event thread is running, which means that
    233 		 * the keyboard will not work until after cold boot.
    234 		 */
    235 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
    236 			dev->hub->explore(sc->sc_bus->root_hub);
    237 #endif
    238 	} else {
    239 		printf("%s: root hub problem, error=%d\n",
    240 		       USBDEVNAME(sc->sc_dev), err);
    241 		sc->sc_dying = 1;
    242 	}
    243 	if (cold)
    244 		sc->sc_bus->use_polling--;
    245 
    246 	config_pending_incr();
    247 	kthread_create(usb_create_event_thread, sc);
    248 
    249 #if defined(__FreeBSD__)
    250 	make_dev(&usb_cdevsw, device_get_unit(self), UID_ROOT, GID_OPERATOR,
    251 		 0644, "usb%d", device_get_unit(self));
    252 #endif
    253 
    254 	USB_ATTACH_SUCCESS_RETURN;
    255 }
    256 
    257 #if defined(__NetBSD__) || defined(__OpenBSD__)
    258 void
    259 usb_create_event_thread(arg)
    260 	void *arg;
    261 {
    262 	struct usb_softc *sc = arg;
    263 
    264 	if (kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    265 			   "%s", sc->sc_dev.dv_xname)) {
    266 		printf("%s: unable to create event thread for\n",
    267 		       sc->sc_dev.dv_xname);
    268 		panic("usb_create_event_thread");
    269 	}
    270 }
    271 
    272 void
    273 usb_event_thread(arg)
    274 	void *arg;
    275 {
    276 	struct usb_softc *sc = arg;
    277 	int first = 1;
    278 
    279 	DPRINTF(("usb_event_thread: start\n"));
    280 
    281 	while (!sc->sc_dying) {
    282 #ifdef USB_DEBUG
    283 		if (usb_noexplore < 2)
    284 #endif
    285 		usb_discover(sc);
    286 		if (first) {
    287 			config_pending_decr();
    288 			first = 0;
    289 		}
    290 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    291 #ifdef USB_DEBUG
    292 			     usb_noexplore ? 0 :
    293 #endif
    294 			     hz*60
    295 			);
    296 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    297 	}
    298 	sc->sc_event_thread = 0;
    299 
    300 	/* In case parent is waiting for us to exit. */
    301 	wakeup(sc);
    302 
    303 	DPRINTF(("usb_event_thread: exit\n"));
    304 	kthread_exit(0);
    305 }
    306 
    307 int
    308 usbctlprint(aux, pnp)
    309 	void *aux;
    310 	const char *pnp;
    311 {
    312 	/* only "usb"es can attach to host controllers */
    313 	if (pnp)
    314 		printf("usb at %s", pnp);
    315 
    316 	return (UNCONF);
    317 }
    318 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    319 
    320 int
    321 usbopen(dev, flag, mode, p)
    322 	dev_t dev;
    323 	int flag, mode;
    324 	struct proc *p;
    325 {
    326 	int unit = minor(dev);
    327 	struct usb_softc *sc;
    328 
    329 	if (unit == USB_DEV_MINOR) {
    330 		if (usb_dev_open)
    331 			return (EBUSY);
    332 		usb_dev_open = 1;
    333 		usb_async_proc = 0;
    334 		return (0);
    335 	}
    336 
    337 	USB_GET_SC_OPEN(usb, unit, sc);
    338 
    339 	if (sc->sc_dying)
    340 		return (EIO);
    341 
    342 	return (0);
    343 }
    344 
    345 int
    346 usbread(dev, uio, flag)
    347 	dev_t dev;
    348 	struct uio *uio;
    349 	int flag;
    350 {
    351 	struct usb_event ue;
    352 	int s, error, n;
    353 
    354 	if (minor(dev) != USB_DEV_MINOR)
    355 		return (ENXIO);
    356 
    357 	if (uio->uio_resid != sizeof(struct usb_event))
    358 		return (EINVAL);
    359 
    360 	error = 0;
    361 	s = splusb();
    362 	for (;;) {
    363 		n = usb_get_next_event(&ue);
    364 		if (n != 0)
    365 			break;
    366 		if (flag & IO_NDELAY) {
    367 			error = EWOULDBLOCK;
    368 			break;
    369 		}
    370 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    371 		if (error)
    372 			break;
    373 	}
    374 	splx(s);
    375 	if (!error)
    376 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    377 
    378 	return (error);
    379 }
    380 
    381 int
    382 usbclose(dev, flag, mode, p)
    383 	dev_t dev;
    384 	int flag, mode;
    385 	struct proc *p;
    386 {
    387 	int unit = minor(dev);
    388 
    389 	if (unit == USB_DEV_MINOR) {
    390 		usb_async_proc = 0;
    391 		usb_dev_open = 0;
    392 	}
    393 
    394 	return (0);
    395 }
    396 
    397 int
    398 usbioctl(devt, cmd, data, flag, p)
    399 	dev_t devt;
    400 	u_long cmd;
    401 	caddr_t data;
    402 	int flag;
    403 	struct proc *p;
    404 {
    405 	struct usb_softc *sc;
    406 	int unit = minor(devt);
    407 
    408 	if (unit == USB_DEV_MINOR) {
    409 		switch (cmd) {
    410 		case FIONBIO:
    411 			/* All handled in the upper FS layer. */
    412 			return (0);
    413 
    414 		case FIOASYNC:
    415 			if (*(int *)data)
    416 				usb_async_proc = p;
    417 			else
    418 				usb_async_proc = 0;
    419 			return (0);
    420 
    421 		default:
    422 			return (EINVAL);
    423 		}
    424 	}
    425 
    426 	USB_GET_SC(usb, unit, sc);
    427 
    428 	if (sc->sc_dying)
    429 		return (EIO);
    430 
    431 	switch (cmd) {
    432 #if defined(__FreeBSD__)
    433 	/* This part should be deleted when kthreads is available */
    434   	case USB_DISCOVER:
    435   		usb_discover(sc);
    436   		break;
    437 #endif
    438 #ifdef USB_DEBUG
    439 	case USB_SETDEBUG:
    440 		usbdebug  = ((*(int *)data) & 0x000000ff);
    441 #ifdef UHCI_DEBUG
    442 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    443 #endif
    444 #ifdef OHCI_DEBUG
    445 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    446 #endif
    447 		break;
    448 #endif
    449 	case USB_REQUEST:
    450 	{
    451 		struct usb_ctl_request *ur = (void *)data;
    452 		int len = UGETW(ur->request.wLength);
    453 		struct iovec iov;
    454 		struct uio uio;
    455 		void *ptr = 0;
    456 		int addr = ur->addr;
    457 		usbd_status err;
    458 		int error = 0;
    459 
    460 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    461 		if (len < 0 || len > 32768)
    462 			return (EINVAL);
    463 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    464 		    sc->sc_bus->devices[addr] == 0)
    465 			return (EINVAL);
    466 		if (len != 0) {
    467 			iov.iov_base = (caddr_t)ur->data;
    468 			iov.iov_len = len;
    469 			uio.uio_iov = &iov;
    470 			uio.uio_iovcnt = 1;
    471 			uio.uio_resid = len;
    472 			uio.uio_offset = 0;
    473 			uio.uio_segflg = UIO_USERSPACE;
    474 			uio.uio_rw =
    475 				ur->request.bmRequestType & UT_READ ?
    476 				UIO_READ : UIO_WRITE;
    477 			uio.uio_procp = p;
    478 			ptr = malloc(len, M_TEMP, M_WAITOK);
    479 			if (uio.uio_rw == UIO_WRITE) {
    480 				error = uiomove(ptr, len, &uio);
    481 				if (error)
    482 					goto ret;
    483 			}
    484 		}
    485 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    486 			  &ur->request, ptr, ur->flags, &ur->actlen);
    487 		if (err) {
    488 			error = EIO;
    489 			goto ret;
    490 		}
    491 		if (len != 0) {
    492 			if (uio.uio_rw == UIO_READ) {
    493 				error = uiomove(ptr, len, &uio);
    494 				if (error)
    495 					goto ret;
    496 			}
    497 		}
    498 	ret:
    499 		if (ptr)
    500 			free(ptr, M_TEMP);
    501 		return (error);
    502 	}
    503 
    504 	case USB_DEVICEINFO:
    505 	{
    506 		struct usb_device_info *di = (void *)data;
    507 		int addr = di->addr;
    508 		usbd_device_handle dev;
    509 
    510 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    511 			return (EINVAL);
    512 		dev = sc->sc_bus->devices[addr];
    513 		if (dev == NULL)
    514 			return (ENXIO);
    515 		usbd_fill_deviceinfo(dev, di);
    516 		break;
    517 	}
    518 
    519 	case USB_DEVICESTATS:
    520 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    521 		break;
    522 
    523 	default:
    524 		return (EINVAL);
    525 	}
    526 	return (0);
    527 }
    528 
    529 int
    530 usbpoll(dev, events, p)
    531 	dev_t dev;
    532 	int events;
    533 	struct proc *p;
    534 {
    535 	int revents, mask, s;
    536 
    537 	if (minor(dev) == USB_DEV_MINOR) {
    538 		revents = 0;
    539 		mask = POLLIN | POLLRDNORM;
    540 
    541 		s = splusb();
    542 		if (events & mask && usb_nevents > 0)
    543 			revents |= events & mask;
    544 		if (revents == 0 && events & mask)
    545 			selrecord(p, &usb_selevent);
    546 		splx(s);
    547 
    548 		return (revents);
    549 	} else {
    550 #if defined(__FreeBSD__)
    551 		/* This part should be deleted when kthreads is available */
    552 		struct usb_softc *sc;
    553 		int unit = minor(dev);
    554 
    555 		USB_GET_SC(usb, unit, sc);
    556 
    557 		revents = 0;
    558 		mask = POLLOUT | POLLRDNORM;
    559 
    560 		s = splusb();
    561 		if (events & mask && sc->sc_bus->needs_explore)
    562 			revents |= events & mask;
    563 		if (revents == 0 && events & mask)
    564 			selrecord(p, &sc->sc_consel);
    565 		splx(s);
    566 
    567 		return (revents);
    568 #else
    569 		return (ENXIO);
    570 #endif
    571 	}
    572 }
    573 
    574 /* Explore device tree from the root. */
    575 usbd_status
    576 usb_discover(sc)
    577 	struct usb_softc *sc;
    578 {
    579 #if defined(__FreeBSD__)
    580 	/* The splxxx parts should be deleted when kthreads is available */
    581 	int s;
    582 #endif
    583 
    584 	/*
    585 	 * We need mutual exclusion while traversing the device tree,
    586 	 * but this is guaranteed since this function is only called
    587 	 * from the event thread for the controller.
    588 	 */
    589 #if defined(__FreeBSD__)
    590 	s = splusb();
    591 #endif
    592 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    593 		sc->sc_bus->needs_explore = 0;
    594 #if defined(__FreeBSD__)
    595 		splx(s);
    596 #endif
    597 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    598 #if defined(__FreeBSD__)
    599 		s = splusb();
    600 #endif
    601 	}
    602 #if defined(__FreeBSD__)
    603 	splx(s);
    604 #endif
    605 
    606 	return (USBD_NORMAL_COMPLETION);
    607 }
    608 
    609 void
    610 usb_needs_explore(bus)
    611 	usbd_bus_handle bus;
    612 {
    613 	bus->needs_explore = 1;
    614 #if defined(__FreeBSD__)
    615 	/* This part should be deleted when kthreads is available */
    616 	selwakeup(&bus->usbctl->sc_consel);
    617 #endif
    618 	wakeup(&bus->needs_explore);
    619 }
    620 
    621 /* Called at splusb() */
    622 int
    623 usb_get_next_event(ue)
    624 	struct usb_event *ue;
    625 {
    626 	struct usb_event_q *ueq;
    627 
    628 	if (usb_nevents <= 0)
    629 		return (0);
    630 	ueq = SIMPLEQ_FIRST(&usb_events);
    631 	*ue = ueq->ue;
    632 	SIMPLEQ_REMOVE_HEAD(&usb_events, ueq, next);
    633 	free(ueq, M_USBDEV);
    634 	usb_nevents--;
    635 	return (1);
    636 }
    637 
    638 void
    639 usbd_add_event(type, dev)
    640 	int type;
    641 	usbd_device_handle dev;
    642 {
    643 	struct usb_event_q *ueq;
    644 	struct usb_event ue;
    645 	struct timeval thetime;
    646 	int s;
    647 
    648 	s = splusb();
    649 	if (++usb_nevents >= USB_MAX_EVENTS) {
    650 		/* Too many queued events, drop an old one. */
    651 		DPRINTFN(-1,("usb: event dropped\n"));
    652 		(void)usb_get_next_event(&ue);
    653 	}
    654 	/* Don't want to wait here inside splusb() */
    655 	ueq = malloc(sizeof *ueq, M_USBDEV, M_NOWAIT);
    656 	if (ueq == NULL) {
    657 		printf("usb: no memory, event dropped\n");
    658 		splx(s);
    659 		return;
    660 	}
    661 	ueq->ue.ue_type = type;
    662 	ueq->ue.ue_cookie = dev->cookie;
    663 	usbd_fill_deviceinfo(dev, &ueq->ue.ue_device);
    664 	microtime(&thetime);
    665 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    666 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    667 	wakeup(&usb_events);
    668 	selwakeup(&usb_selevent);
    669 	if (usb_async_proc != NULL)
    670 		psignal(usb_async_proc, SIGIO);
    671 	splx(s);
    672 }
    673 
    674 #if defined(__NetBSD__) || defined(__OpenBSD__)
    675 int
    676 usb_activate(self, act)
    677 	device_ptr_t self;
    678 	enum devact act;
    679 {
    680 	struct usb_softc *sc = (struct usb_softc *)self;
    681 	usbd_device_handle dev = sc->sc_port.device;
    682 	int i, rv = 0;
    683 
    684 	switch (act) {
    685 	case DVACT_ACTIVATE:
    686 		return (EOPNOTSUPP);
    687 		break;
    688 
    689 	case DVACT_DEACTIVATE:
    690 		sc->sc_dying = 1;
    691 		if (dev && dev->cdesc && dev->subdevs) {
    692 			for (i = 0; dev->subdevs[i]; i++)
    693 				rv |= config_deactivate(dev->subdevs[i]);
    694 		}
    695 		break;
    696 	}
    697 	return (rv);
    698 }
    699 
    700 int
    701 usb_detach(self, flags)
    702 	device_ptr_t self;
    703 	int flags;
    704 {
    705 	struct usb_softc *sc = (struct usb_softc *)self;
    706 
    707 	DPRINTF(("usb_detach: start\n"));
    708 
    709 	sc->sc_dying = 1;
    710 
    711 	/* Make all devices disconnect. */
    712 	if (sc->sc_port.device)
    713 		usb_disconnect_port(&sc->sc_port, self);
    714 
    715 	/* Kill off event thread. */
    716 	if (sc->sc_event_thread) {
    717 		wakeup(&sc->sc_bus->needs_explore);
    718 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    719 			printf("%s: event thread didn't die\n",
    720 			       USBDEVNAME(sc->sc_dev));
    721 		DPRINTF(("usb_detach: event thread dead\n"));
    722 	}
    723 
    724 	usbd_finish();
    725 	return (0);
    726 }
    727 #elif defined(__FreeBSD__)
    728 int
    729 usb_detach(device_t self)
    730 {
    731 	DPRINTF(("%s: unload, prevented\n", USBDEVNAME(self)));
    732 
    733 	return (EINVAL);
    734 }
    735 #endif
    736 
    737 
    738 #if defined(__FreeBSD__)
    739 DRIVER_MODULE(usb, ohci, usb_driver, usb_devclass, 0, 0);
    740 DRIVER_MODULE(usb, uhci, usb_driver, usb_devclass, 0, 0);
    741 #endif
    742