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