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