Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.75
      1 /*	$NetBSD: usb.c,v 1.75 2002/11/26 18:49:49 christos Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2002 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Lennart Augustsson (lennart (at) augustsson.net) at
      9  * Carlstedt Research & Technology.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  * 3. All advertising materials mentioning features or use of this software
     20  *    must display the following acknowledgement:
     21  *        This product includes software developed by the NetBSD
     22  *        Foundation, Inc. and its contributors.
     23  * 4. Neither the name of The NetBSD Foundation nor the names of its
     24  *    contributors may be used to endorse or promote products derived
     25  *    from this software without specific prior written permission.
     26  *
     27  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     28  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     29  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     30  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     31  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     32  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     33  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     34  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     35  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     36  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     37  * POSSIBILITY OF SUCH DAMAGE.
     38  */
     39 
     40 /*
     41  * USB specifications and other documentation can be found at
     42  * http://www.usb.org/developers/data/ and
     43  * http://www.usb.org/developers/index.html .
     44  */
     45 
     46 #include <sys/cdefs.h>
     47 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.75 2002/11/26 18:49:49 christos Exp $");
     48 
     49 #include <sys/param.h>
     50 #include <sys/systm.h>
     51 #include <sys/kernel.h>
     52 #include <sys/malloc.h>
     53 #include <sys/device.h>
     54 #include <sys/kthread.h>
     55 #include <sys/proc.h>
     56 #include <sys/conf.h>
     57 #include <sys/fcntl.h>
     58 #include <sys/poll.h>
     59 #include <sys/select.h>
     60 #include <sys/vnode.h>
     61 #include <sys/signalvar.h>
     62 
     63 #include <dev/usb/usb.h>
     64 #include <dev/usb/usbdi.h>
     65 #include <dev/usb/usbdi_util.h>
     66 
     67 #define USB_DEV_MINOR 255
     68 
     69 #include <machine/bus.h>
     70 
     71 #include <dev/usb/usbdivar.h>
     72 #include <dev/usb/usb_quirks.h>
     73 
     74 #ifdef USB_DEBUG
     75 #define DPRINTF(x)	if (usbdebug) logprintf x
     76 #define DPRINTFN(n,x)	if (usbdebug>(n)) logprintf x
     77 int	usbdebug = 0;
     78 #ifdef UHCI_DEBUG
     79 int	uhcidebug;
     80 #endif
     81 #ifdef OHCI_DEBUG
     82 int	ohcidebug;
     83 #endif
     84 /*
     85  * 0  - do usual exploration
     86  * 1  - do not use timeout exploration
     87  * >1 - do no exploration
     88  */
     89 int	usb_noexplore = 0;
     90 #else
     91 #define DPRINTF(x)
     92 #define DPRINTFN(n,x)
     93 #endif
     94 
     95 struct usb_softc {
     96 	USBBASEDEVICE	sc_dev;		/* base device */
     97 	usbd_bus_handle sc_bus;		/* USB controller */
     98 	struct usbd_port sc_port;	/* dummy port for root hub */
     99 
    100 	struct proc	*sc_event_thread;
    101 
    102 	char		sc_dying;
    103 };
    104 
    105 TAILQ_HEAD(, usb_task) usb_all_tasks;
    106 
    107 dev_type_open(usbopen);
    108 dev_type_close(usbclose);
    109 dev_type_read(usbread);
    110 dev_type_ioctl(usbioctl);
    111 dev_type_poll(usbpoll);
    112 dev_type_kqfilter(usbkqfilter);
    113 
    114 const struct cdevsw usb_cdevsw = {
    115 	usbopen, usbclose, usbread, nowrite, usbioctl,
    116 	nostop, notty, usbpoll, nommap, usbkqfilter,
    117 };
    118 
    119 Static void	usb_discover(void *);
    120 Static void	usb_create_event_thread(void *);
    121 Static void	usb_event_thread(void *);
    122 Static void	usb_task_thread(void *);
    123 Static struct proc *usb_task_thread_proc = NULL;
    124 
    125 #define USB_MAX_EVENTS 100
    126 struct usb_event_q {
    127 	struct usb_event ue;
    128 	SIMPLEQ_ENTRY(usb_event_q) next;
    129 };
    130 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    131 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    132 Static int usb_nevents = 0;
    133 Static struct selinfo usb_selevent;
    134 Static usb_proc_ptr usb_async_proc;  /* process that wants USB SIGIO */
    135 Static int usb_dev_open = 0;
    136 Static void usb_add_event(int, struct usb_event *);
    137 
    138 Static int usb_get_next_event(struct usb_event *);
    139 
    140 Static const char *usbrev_str[] = USBREV_STR;
    141 
    142 USB_DECLARE_DRIVER(usb);
    143 
    144 USB_MATCH(usb)
    145 {
    146 	DPRINTF(("usbd_match\n"));
    147 	return (UMATCH_GENERIC);
    148 }
    149 
    150 USB_ATTACH(usb)
    151 {
    152 	struct usb_softc *sc = (struct usb_softc *)self;
    153 	usbd_device_handle dev;
    154 	usbd_status err;
    155 	int usbrev;
    156 	int speed;
    157 	struct usb_event ue;
    158 
    159 	DPRINTF(("usbd_attach\n"));
    160 
    161 	usbd_init();
    162 	sc->sc_bus = aux;
    163 	sc->sc_bus->usbctl = sc;
    164 	sc->sc_port.power = USB_MAX_POWER;
    165 
    166 	usbrev = sc->sc_bus->usbrev;
    167 	printf(": USB revision %s", usbrev_str[usbrev]);
    168 	switch (usbrev) {
    169 	case USBREV_1_0:
    170 	case USBREV_1_1:
    171 		speed = USB_SPEED_FULL;
    172 		break;
    173 	case USBREV_2_0:
    174 		speed = USB_SPEED_HIGH;
    175 		break;
    176 	default:
    177 		printf(", not supported\n");
    178 		sc->sc_dying = 1;
    179 		USB_ATTACH_ERROR_RETURN;
    180 	}
    181 	printf("\n");
    182 
    183 	/* Make sure not to use tsleep() if we are cold booting. */
    184 	if (cold)
    185 		sc->sc_bus->use_polling++;
    186 
    187 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    188 	usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue);
    189 
    190 #ifdef USB_USE_SOFTINTR
    191 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    192 	/* XXX we should have our own level */
    193 	sc->sc_bus->soft = softintr_establish(IPL_SOFTNET,
    194 	    sc->sc_bus->methods->soft_intr, sc->sc_bus);
    195 	if (sc->sc_bus->soft == NULL) {
    196 		printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev));
    197 		sc->sc_dying = 1;
    198 		USB_ATTACH_ERROR_RETURN;
    199 	}
    200 #else
    201 	usb_callout_init(sc->sc_bus->softi);
    202 #endif
    203 #endif
    204 
    205 	err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0,
    206 		  &sc->sc_port);
    207 	if (!err) {
    208 		dev = sc->sc_port.device;
    209 		if (dev->hub == NULL) {
    210 			sc->sc_dying = 1;
    211 			printf("%s: root device is not a hub\n",
    212 			       USBDEVNAME(sc->sc_dev));
    213 			USB_ATTACH_ERROR_RETURN;
    214 		}
    215 		sc->sc_bus->root_hub = dev;
    216 #if 1
    217 		/*
    218 		 * Turning this code off will delay attachment of USB devices
    219 		 * until the USB event thread is running, which means that
    220 		 * the keyboard will not work until after cold boot.
    221 		 */
    222 		if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1))
    223 			dev->hub->explore(sc->sc_bus->root_hub);
    224 #endif
    225 	} else {
    226 		printf("%s: root hub problem, error=%d\n",
    227 		       USBDEVNAME(sc->sc_dev), err);
    228 		sc->sc_dying = 1;
    229 	}
    230 	if (cold)
    231 		sc->sc_bus->use_polling--;
    232 
    233 	config_pending_incr();
    234 	usb_kthread_create(usb_create_event_thread, sc);
    235 
    236 	USB_ATTACH_SUCCESS_RETURN;
    237 }
    238 
    239 #if defined(__NetBSD__) || defined(__OpenBSD__)
    240 void
    241 usb_create_event_thread(void *arg)
    242 {
    243 	struct usb_softc *sc = arg;
    244 	static int created = 0;
    245 
    246 	if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread,
    247 			   "%s", sc->sc_dev.dv_xname)) {
    248 		printf("%s: unable to create event thread for\n",
    249 		       sc->sc_dev.dv_xname);
    250 		panic("usb_create_event_thread");
    251 	}
    252 	if (!created) {
    253 		created = 1;
    254 		TAILQ_INIT(&usb_all_tasks);
    255 		if (usb_kthread_create1(usb_task_thread, NULL,
    256 					&usb_task_thread_proc, "usbtask")) {
    257 			printf("unable to create task thread\n");
    258 			panic("usb_create_event_thread task");
    259 		}
    260 	}
    261 }
    262 
    263 /*
    264  * Add a task to be performed by the task thread.  This function can be
    265  * called from any context and the task will be executed in a process
    266  * context ASAP.
    267  */
    268 void
    269 usb_add_task(usbd_device_handle dev, struct usb_task *task)
    270 {
    271 	int s;
    272 
    273 	s = splusb();
    274 	if (!task->onqueue) {
    275 		DPRINTFN(2,("usb_add_task: task=%p\n", task));
    276 		TAILQ_INSERT_TAIL(&usb_all_tasks, task, next);
    277 		task->onqueue = 1;
    278 	} else {
    279 		DPRINTFN(3,("usb_add_task: task=%p on q\n", task));
    280 	}
    281 	wakeup(&usb_all_tasks);
    282 	splx(s);
    283 }
    284 
    285 void
    286 usb_rem_task(usbd_device_handle dev, struct usb_task *task)
    287 {
    288 	int s;
    289 
    290 	s = splusb();
    291 	if (task->onqueue) {
    292 		TAILQ_REMOVE(&usb_all_tasks, task, next);
    293 		task->onqueue = 0;
    294 	}
    295 	splx(s);
    296 }
    297 
    298 void
    299 usb_event_thread(void *arg)
    300 {
    301 	struct usb_softc *sc = arg;
    302 
    303 	DPRINTF(("usb_event_thread: start\n"));
    304 
    305 	/*
    306 	 * In case this controller is a companion controller to an
    307 	 * EHCI controller we need to wait until the EHCI controller
    308 	 * has grabbed the port.
    309 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    310 	 * know how to synchronize the creation of the threads so it
    311 	 * will work.
    312 	 */
    313 	usb_delay_ms(sc->sc_bus, 500);
    314 
    315 	/* Make sure first discover does something. */
    316 	sc->sc_bus->needs_explore = 1;
    317 	usb_discover(sc);
    318 	config_pending_decr();
    319 
    320 	while (!sc->sc_dying) {
    321 #ifdef USB_DEBUG
    322 		if (usb_noexplore < 2)
    323 #endif
    324 		usb_discover(sc);
    325 #ifdef USB_DEBUG
    326 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    327 		    usb_noexplore ? 0 : hz * 60);
    328 #else
    329 		(void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt",
    330 		    hz * 60);
    331 #endif
    332 		DPRINTFN(2,("usb_event_thread: woke up\n"));
    333 	}
    334 	sc->sc_event_thread = NULL;
    335 
    336 	/* In case parent is waiting for us to exit. */
    337 	wakeup(sc);
    338 
    339 	DPRINTF(("usb_event_thread: exit\n"));
    340 	kthread_exit(0);
    341 }
    342 
    343 void
    344 usb_task_thread(void *arg)
    345 {
    346 	struct usb_task *task;
    347 	int s;
    348 
    349 	DPRINTF(("usb_task_thread: start\n"));
    350 
    351 	s = splusb();
    352 	for (;;) {
    353 		task = TAILQ_FIRST(&usb_all_tasks);
    354 		if (task == NULL) {
    355 			tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0);
    356 			task = TAILQ_FIRST(&usb_all_tasks);
    357 		}
    358 		DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task));
    359 		if (task != NULL) {
    360 			TAILQ_REMOVE(&usb_all_tasks, task, next);
    361 			task->onqueue = 0;
    362 			splx(s);
    363 			task->fun(task->arg);
    364 			s = splusb();
    365 		}
    366 	}
    367 }
    368 
    369 int
    370 usbctlprint(void *aux, const char *pnp)
    371 {
    372 	/* only "usb"es can attach to host controllers */
    373 	if (pnp)
    374 		printf("usb at %s", pnp);
    375 
    376 	return (UNCONF);
    377 }
    378 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */
    379 
    380 int
    381 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p)
    382 {
    383 	int unit = minor(dev);
    384 	struct usb_softc *sc;
    385 
    386 	if (unit == USB_DEV_MINOR) {
    387 		if (usb_dev_open)
    388 			return (EBUSY);
    389 		usb_dev_open = 1;
    390 		usb_async_proc = 0;
    391 		return (0);
    392 	}
    393 
    394 	USB_GET_SC_OPEN(usb, unit, sc);
    395 
    396 	if (sc->sc_dying)
    397 		return (EIO);
    398 
    399 	return (0);
    400 }
    401 
    402 int
    403 usbread(dev_t dev, struct uio *uio, int flag)
    404 {
    405 	struct usb_event ue;
    406 	int s, error, n;
    407 
    408 	if (minor(dev) != USB_DEV_MINOR)
    409 		return (ENXIO);
    410 
    411 	if (uio->uio_resid != sizeof(struct usb_event))
    412 		return (EINVAL);
    413 
    414 	error = 0;
    415 	s = splusb();
    416 	for (;;) {
    417 		n = usb_get_next_event(&ue);
    418 		if (n != 0)
    419 			break;
    420 		if (flag & IO_NDELAY) {
    421 			error = EWOULDBLOCK;
    422 			break;
    423 		}
    424 		error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0);
    425 		if (error)
    426 			break;
    427 	}
    428 	splx(s);
    429 	if (!error)
    430 		error = uiomove((void *)&ue, uio->uio_resid, uio);
    431 
    432 	return (error);
    433 }
    434 
    435 int
    436 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p)
    437 {
    438 	int unit = minor(dev);
    439 
    440 	if (unit == USB_DEV_MINOR) {
    441 		usb_async_proc = 0;
    442 		usb_dev_open = 0;
    443 	}
    444 
    445 	return (0);
    446 }
    447 
    448 int
    449 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p)
    450 {
    451 	struct usb_softc *sc;
    452 	int unit = minor(devt);
    453 
    454 	if (unit == USB_DEV_MINOR) {
    455 		switch (cmd) {
    456 		case FIONBIO:
    457 			/* All handled in the upper FS layer. */
    458 			return (0);
    459 
    460 		case FIOASYNC:
    461 			if (*(int *)data)
    462 				usb_async_proc = p;
    463 			else
    464 				usb_async_proc = 0;
    465 			return (0);
    466 
    467 		default:
    468 			return (EINVAL);
    469 		}
    470 	}
    471 
    472 	USB_GET_SC(usb, unit, sc);
    473 
    474 	if (sc->sc_dying)
    475 		return (EIO);
    476 
    477 	switch (cmd) {
    478 #ifdef USB_DEBUG
    479 	case USB_SETDEBUG:
    480 		if (!(flag & FWRITE))
    481 			return (EBADF);
    482 		usbdebug  = ((*(int *)data) & 0x000000ff);
    483 #ifdef UHCI_DEBUG
    484 		uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8;
    485 #endif
    486 #ifdef OHCI_DEBUG
    487 		ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16;
    488 #endif
    489 		break;
    490 #endif /* USB_DEBUG */
    491 	case USB_REQUEST:
    492 	{
    493 		struct usb_ctl_request *ur = (void *)data;
    494 		int len = UGETW(ur->ucr_request.wLength);
    495 		struct iovec iov;
    496 		struct uio uio;
    497 		void *ptr = 0;
    498 		int addr = ur->ucr_addr;
    499 		usbd_status err;
    500 		int error = 0;
    501 
    502 		if (!(flag & FWRITE))
    503 			return (EBADF);
    504 
    505 		DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len));
    506 		if (len < 0 || len > 32768)
    507 			return (EINVAL);
    508 		if (addr < 0 || addr >= USB_MAX_DEVICES ||
    509 		    sc->sc_bus->devices[addr] == 0)
    510 			return (EINVAL);
    511 		if (len != 0) {
    512 			iov.iov_base = (caddr_t)ur->ucr_data;
    513 			iov.iov_len = len;
    514 			uio.uio_iov = &iov;
    515 			uio.uio_iovcnt = 1;
    516 			uio.uio_resid = len;
    517 			uio.uio_offset = 0;
    518 			uio.uio_segflg = UIO_USERSPACE;
    519 			uio.uio_rw =
    520 				ur->ucr_request.bmRequestType & UT_READ ?
    521 				UIO_READ : UIO_WRITE;
    522 			uio.uio_procp = p;
    523 			ptr = malloc(len, M_TEMP, M_WAITOK);
    524 			if (uio.uio_rw == UIO_WRITE) {
    525 				error = uiomove(ptr, len, &uio);
    526 				if (error)
    527 					goto ret;
    528 			}
    529 		}
    530 		err = usbd_do_request_flags(sc->sc_bus->devices[addr],
    531 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    532 			  USBD_DEFAULT_TIMEOUT);
    533 		if (err) {
    534 			error = EIO;
    535 			goto ret;
    536 		}
    537 		if (len != 0) {
    538 			if (uio.uio_rw == UIO_READ) {
    539 				error = uiomove(ptr, len, &uio);
    540 				if (error)
    541 					goto ret;
    542 			}
    543 		}
    544 	ret:
    545 		if (ptr)
    546 			free(ptr, M_TEMP);
    547 		return (error);
    548 	}
    549 
    550 	case USB_DEVICEINFO:
    551 	{
    552 		struct usb_device_info *di = (void *)data;
    553 		int addr = di->udi_addr;
    554 		usbd_device_handle dev;
    555 
    556 		if (addr < 1 || addr >= USB_MAX_DEVICES)
    557 			return (EINVAL);
    558 		dev = sc->sc_bus->devices[addr];
    559 		if (dev == NULL)
    560 			return (ENXIO);
    561 		usbd_fill_deviceinfo(dev, di, 1);
    562 		break;
    563 	}
    564 
    565 	case USB_DEVICESTATS:
    566 		*(struct usb_device_stats *)data = sc->sc_bus->stats;
    567 		break;
    568 
    569 	default:
    570 		return (EINVAL);
    571 	}
    572 	return (0);
    573 }
    574 
    575 int
    576 usbpoll(dev_t dev, int events, usb_proc_ptr p)
    577 {
    578 	int revents, mask, s;
    579 
    580 	if (minor(dev) == USB_DEV_MINOR) {
    581 		revents = 0;
    582 		mask = POLLIN | POLLRDNORM;
    583 
    584 		s = splusb();
    585 		if (events & mask && usb_nevents > 0)
    586 			revents |= events & mask;
    587 		if (revents == 0 && events & mask)
    588 			selrecord(p, &usb_selevent);
    589 		splx(s);
    590 
    591 		return (revents);
    592 	} else {
    593 		return (ENXIO);
    594 	}
    595 }
    596 
    597 static void
    598 filt_usbrdetach(struct knote *kn)
    599 {
    600 	int s;
    601 
    602 	s = splusb();
    603 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
    604 	splx(s);
    605 }
    606 
    607 static int
    608 filt_usbread(struct knote *kn, long hint)
    609 {
    610 
    611 	if (usb_nevents == 0)
    612 		return (0);
    613 
    614 	kn->kn_data = sizeof(struct usb_event);
    615 	return (1);
    616 }
    617 
    618 static const struct filterops usbread_filtops =
    619 	{ 1, NULL, filt_usbrdetach, filt_usbread };
    620 
    621 int
    622 usbkqfilter(dev_t dev, struct knote *kn)
    623 {
    624 	struct klist *klist;
    625 	int s;
    626 
    627 	switch (kn->kn_filter) {
    628 	case EVFILT_READ:
    629 		if (minor(dev) != USB_DEV_MINOR)
    630 			return (1);
    631 		klist = &usb_selevent.sel_klist;
    632 		kn->kn_fop = &usbread_filtops;
    633 		break;
    634 
    635 	default:
    636 		return (1);
    637 	}
    638 
    639 	kn->kn_hook = NULL;
    640 
    641 	s = splusb();
    642 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
    643 	splx(s);
    644 
    645 	return (0);
    646 }
    647 
    648 /* Explore device tree from the root. */
    649 Static void
    650 usb_discover(void *v)
    651 {
    652 	struct usb_softc *sc = v;
    653 
    654 	DPRINTFN(2,("usb_discover\n"));
    655 #ifdef USB_DEBUG
    656 	if (usb_noexplore > 1)
    657 		return;
    658 #endif
    659 	/*
    660 	 * We need mutual exclusion while traversing the device tree,
    661 	 * but this is guaranteed since this function is only called
    662 	 * from the event thread for the controller.
    663 	 */
    664 	while (sc->sc_bus->needs_explore && !sc->sc_dying) {
    665 		sc->sc_bus->needs_explore = 0;
    666 		sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub);
    667 	}
    668 }
    669 
    670 void
    671 usb_needs_explore(usbd_device_handle dev)
    672 {
    673 	DPRINTFN(2,("usb_needs_explore\n"));
    674 	dev->bus->needs_explore = 1;
    675 	wakeup(&dev->bus->needs_explore);
    676 }
    677 
    678 /* Called at splusb() */
    679 int
    680 usb_get_next_event(struct usb_event *ue)
    681 {
    682 	struct usb_event_q *ueq;
    683 
    684 	if (usb_nevents <= 0)
    685 		return (0);
    686 	ueq = SIMPLEQ_FIRST(&usb_events);
    687 #ifdef DIAGNOSTIC
    688 	if (ueq == NULL) {
    689 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
    690 		usb_nevents = 0;
    691 		return (0);
    692 	}
    693 #endif
    694 	*ue = ueq->ue;
    695 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
    696 	free(ueq, M_USBDEV);
    697 	usb_nevents--;
    698 	return (1);
    699 }
    700 
    701 void
    702 usbd_add_dev_event(int type, usbd_device_handle udev)
    703 {
    704 	struct usb_event ue;
    705 
    706 	usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type));
    707 	usb_add_event(type, &ue);
    708 }
    709 
    710 void
    711 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev)
    712 {
    713 	struct usb_event ue;
    714 
    715 	ue.u.ue_driver.ue_cookie = udev->cookie;
    716 	strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev),
    717 	    sizeof ue.u.ue_driver.ue_devname);
    718 	usb_add_event(type, &ue);
    719 }
    720 
    721 Static void
    722 usb_add_event(int type, struct usb_event *uep)
    723 {
    724 	struct usb_event_q *ueq;
    725 	struct usb_event ue;
    726 	struct timeval thetime;
    727 	int s;
    728 
    729 	microtime(&thetime);
    730 	/* Don't want to wait here inside splusb() */
    731 	ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK);
    732 	ueq->ue = *uep;
    733 	ueq->ue.ue_type = type;
    734 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
    735 
    736 	s = splusb();
    737 	if (++usb_nevents >= USB_MAX_EVENTS) {
    738 		/* Too many queued events, drop an old one. */
    739 		DPRINTFN(-1,("usb: event dropped\n"));
    740 		(void)usb_get_next_event(&ue);
    741 	}
    742 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
    743 	wakeup(&usb_events);
    744 	selnotify(&usb_selevent, 0);
    745 	if (usb_async_proc != NULL)
    746 		psignal(usb_async_proc, SIGIO);
    747 	splx(s);
    748 }
    749 
    750 void
    751 usb_schedsoftintr(usbd_bus_handle bus)
    752 {
    753 	DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling));
    754 #ifdef USB_USE_SOFTINTR
    755 	if (bus->use_polling) {
    756 		bus->methods->soft_intr(bus);
    757 	} else {
    758 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    759 		softintr_schedule(bus->soft);
    760 #else
    761 		if (!callout_pending(&bus->softi))
    762 			callout_reset(&bus->softi, 0, bus->methods->soft_intr,
    763 			    bus);
    764 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */
    765 	}
    766 #else
    767 	bus->methods->soft_intr(bus);
    768 #endif /* USB_USE_SOFTINTR */
    769 }
    770 
    771 int
    772 usb_activate(device_ptr_t self, enum devact act)
    773 {
    774 	struct usb_softc *sc = (struct usb_softc *)self;
    775 	usbd_device_handle dev = sc->sc_port.device;
    776 	int i, rv = 0;
    777 
    778 	switch (act) {
    779 	case DVACT_ACTIVATE:
    780 		return (EOPNOTSUPP);
    781 
    782 	case DVACT_DEACTIVATE:
    783 		sc->sc_dying = 1;
    784 		if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) {
    785 			for (i = 0; dev->subdevs[i]; i++)
    786 				rv |= config_deactivate(dev->subdevs[i]);
    787 		}
    788 		break;
    789 	}
    790 	return (rv);
    791 }
    792 
    793 int
    794 usb_detach(device_ptr_t self, int flags)
    795 {
    796 	struct usb_softc *sc = (struct usb_softc *)self;
    797 	struct usb_event ue;
    798 
    799 	DPRINTF(("usb_detach: start\n"));
    800 
    801 	sc->sc_dying = 1;
    802 
    803 	/* Make all devices disconnect. */
    804 	if (sc->sc_port.device != NULL)
    805 		usb_disconnect_port(&sc->sc_port, self);
    806 
    807 	/* Kill off event thread. */
    808 	if (sc->sc_event_thread != NULL) {
    809 		wakeup(&sc->sc_bus->needs_explore);
    810 		if (tsleep(sc, PWAIT, "usbdet", hz * 60))
    811 			printf("%s: event thread didn't die\n",
    812 			       USBDEVNAME(sc->sc_dev));
    813 		DPRINTF(("usb_detach: event thread dead\n"));
    814 	}
    815 
    816 	usbd_finish();
    817 
    818 #ifdef USB_USE_SOFTINTR
    819 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS
    820 	if (sc->sc_bus->soft != NULL) {
    821 		softintr_disestablish(sc->sc_bus->soft);
    822 		sc->sc_bus->soft = NULL;
    823 	}
    824 #else
    825 	callout_stop(&sc->sc_bus->softi);
    826 #endif
    827 #endif
    828 
    829 	ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev);
    830 	usb_add_event(USB_EVENT_CTRLR_DETACH, &ue);
    831 
    832 	return (0);
    833 }
    834