Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.186
      1 /*	$NetBSD: usb.c,v 1.186 2020/05/26 19:55:43 skrll Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 1998, 2002, 2008, 2012 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 and Matthew R. Green (mrg (at) eterna.com.au).
     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  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * USB specifications and other documentation can be found at
     35  * http://www.usb.org/developers/docs/ and
     36  * http://www.usb.org/developers/devclass_docs/
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.186 2020/05/26 19:55:43 skrll Exp $");
     41 
     42 #ifdef _KERNEL_OPT
     43 #include "opt_usb.h"
     44 #include "opt_compat_netbsd.h"
     45 #endif
     46 
     47 #include <sys/param.h>
     48 #include <sys/systm.h>
     49 #include <sys/kernel.h>
     50 #include <sys/kmem.h>
     51 #include <sys/device.h>
     52 #include <sys/kthread.h>
     53 #include <sys/proc.h>
     54 #include <sys/conf.h>
     55 #include <sys/fcntl.h>
     56 #include <sys/poll.h>
     57 #include <sys/select.h>
     58 #include <sys/vnode.h>
     59 #include <sys/signalvar.h>
     60 #include <sys/intr.h>
     61 #include <sys/module.h>
     62 #include <sys/mutex.h>
     63 #include <sys/bus.h>
     64 #include <sys/once.h>
     65 #include <sys/atomic.h>
     66 #include <sys/sysctl.h>
     67 #include <sys/compat_stub.h>
     68 #include <sys/sdt.h>
     69 
     70 #include <dev/usb/usb.h>
     71 #include <dev/usb/usbdi.h>
     72 #include <dev/usb/usbdi_util.h>
     73 #include <dev/usb/usbdivar.h>
     74 #include <dev/usb/usb_verbose.h>
     75 #include <dev/usb/usb_quirks.h>
     76 #include <dev/usb/usbhist.h>
     77 #include <dev/usb/usb_sdt.h>
     78 
     79 #include "ioconf.h"
     80 
     81 #if defined(USB_DEBUG)
     82 
     83 #ifndef USBHIST_SIZE
     84 #define USBHIST_SIZE 50000
     85 #endif
     86 
     87 static struct kern_history_ent usbhistbuf[USBHIST_SIZE];
     88 USBHIST_DEFINE(usbhist) = KERNHIST_INITIALIZER(usbhist, usbhistbuf);
     89 
     90 #endif
     91 
     92 #define USB_DEV_MINOR 255
     93 
     94 #ifdef USB_DEBUG
     95 /*
     96  * 0  - do usual exploration
     97  * 1  - do not use timeout exploration
     98  * >1 - do no exploration
     99  */
    100 int	usb_noexplore = 0;
    101 
    102 int	usbdebug = 0;
    103 SYSCTL_SETUP(sysctl_hw_usb_setup, "sysctl hw.usb setup")
    104 {
    105 	int err;
    106 	const struct sysctlnode *rnode;
    107 	const struct sysctlnode *cnode;
    108 
    109 	err = sysctl_createv(clog, 0, NULL, &rnode,
    110 	    CTLFLAG_PERMANENT, CTLTYPE_NODE, "usb",
    111 	    SYSCTL_DESCR("usb global controls"),
    112 	    NULL, 0, NULL, 0, CTL_HW, CTL_CREATE, CTL_EOL);
    113 
    114 	if (err)
    115 		goto fail;
    116 
    117 	/* control debugging printfs */
    118 	err = sysctl_createv(clog, 0, &rnode, &cnode,
    119 	    CTLFLAG_PERMANENT|CTLFLAG_READWRITE, CTLTYPE_INT,
    120 	    "debug", SYSCTL_DESCR("Enable debugging output"),
    121 	    NULL, 0, &usbdebug, sizeof(usbdebug), CTL_CREATE, CTL_EOL);
    122 	if (err)
    123 		goto fail;
    124 
    125 	return;
    126 fail:
    127 	aprint_error("%s: sysctl_createv failed (err = %d)\n", __func__, err);
    128 }
    129 #else
    130 #define	usb_noexplore 0
    131 #endif
    132 
    133 #define	DPRINTF(FMT,A,B,C,D)	USBHIST_LOG(usbdebug,FMT,A,B,C,D)
    134 #define	DPRINTFN(N,FMT,A,B,C,D)	USBHIST_LOGN(usbdebug,N,FMT,A,B,C,D)
    135 
    136 struct usb_softc {
    137 #if 0
    138 	device_t	sc_dev;		/* base device */
    139 #endif
    140 	struct usbd_bus *sc_bus;	/* USB controller */
    141 	struct usbd_port sc_port;	/* dummy port for root hub */
    142 
    143 	struct lwp	*sc_event_thread;
    144 
    145 	char		sc_dying;
    146 	bool		sc_pmf_registered;
    147 };
    148 
    149 struct usb_taskq {
    150 	TAILQ_HEAD(, usb_task) tasks;
    151 	kmutex_t lock;
    152 	kcondvar_t cv;
    153 	struct lwp *task_thread_lwp;
    154 	const char *name;
    155 	struct usb_task *current_task;
    156 };
    157 
    158 static struct usb_taskq usb_taskq[USB_NUM_TASKQS];
    159 
    160 /* XXX wrong place */
    161 #ifdef KDTRACE_HOOKS
    162 #define	__dtrace_used
    163 #else
    164 #define	__dtrace_used	__unused
    165 #endif
    166 
    167 SDT_PROVIDER_DEFINE(usb);
    168 
    169 SDT_PROBE_DEFINE3(usb, kernel, task, add,
    170     "struct usbd_device *"/*dev*/, "struct usb_task *"/*task*/, "int"/*q*/);
    171 SDT_PROBE_DEFINE2(usb, kernel, task, rem__start,
    172     "struct usbd_device *"/*dev*/, "struct usb_task *"/*task*/);
    173 SDT_PROBE_DEFINE3(usb, kernel, task, rem__done,
    174     "struct usbd_device *"/*dev*/,
    175     "struct usb_task *"/*task*/,
    176     "bool"/*removed*/);
    177 SDT_PROBE_DEFINE4(usb, kernel, task, rem__wait__start,
    178     "struct usbd_device *"/*dev*/,
    179     "struct usb_task *"/*task*/,
    180     "int"/*queue*/,
    181     "kmutex_t *"/*interlock*/);
    182 SDT_PROBE_DEFINE5(usb, kernel, task, rem__wait__done,
    183     "struct usbd_device *"/*dev*/,
    184     "struct usb_task *"/*task*/,
    185     "int"/*queue*/,
    186     "kmutex_t *"/*interlock*/,
    187     "bool"/*done*/);
    188 
    189 SDT_PROBE_DEFINE1(usb, kernel, task, start,  "struct usb_task *"/*task*/);
    190 SDT_PROBE_DEFINE1(usb, kernel, task, done,  "struct usb_task *"/*task*/);
    191 
    192 SDT_PROBE_DEFINE1(usb, kernel, bus, needs__explore,
    193     "struct usbd_bus *"/*bus*/);
    194 SDT_PROBE_DEFINE1(usb, kernel, bus, needs__reattach,
    195     "struct usbd_bus *"/*bus*/);
    196 SDT_PROBE_DEFINE1(usb, kernel, bus, discover__start,
    197     "struct usbd_bus *"/*bus*/);
    198 SDT_PROBE_DEFINE1(usb, kernel, bus, discover__done,
    199     "struct usbd_bus *"/*bus*/);
    200 SDT_PROBE_DEFINE1(usb, kernel, bus, explore__start,
    201     "struct usbd_bus *"/*bus*/);
    202 SDT_PROBE_DEFINE1(usb, kernel, bus, explore__done,
    203     "struct usbd_bus *"/*bus*/);
    204 
    205 SDT_PROBE_DEFINE1(usb, kernel, event, add,  "struct usb_event *"/*uep*/);
    206 SDT_PROBE_DEFINE1(usb, kernel, event, drop,  "struct usb_event *"/*uep*/);
    207 
    208 dev_type_open(usbopen);
    209 dev_type_close(usbclose);
    210 dev_type_read(usbread);
    211 dev_type_ioctl(usbioctl);
    212 dev_type_poll(usbpoll);
    213 dev_type_kqfilter(usbkqfilter);
    214 
    215 const struct cdevsw usb_cdevsw = {
    216 	.d_open = usbopen,
    217 	.d_close = usbclose,
    218 	.d_read = usbread,
    219 	.d_write = nowrite,
    220 	.d_ioctl = usbioctl,
    221 	.d_stop = nostop,
    222 	.d_tty = notty,
    223 	.d_poll = usbpoll,
    224 	.d_mmap = nommap,
    225 	.d_kqfilter = usbkqfilter,
    226 	.d_discard = nodiscard,
    227 	.d_flag = D_OTHER
    228 };
    229 
    230 Static void	usb_discover(struct usb_softc *);
    231 Static void	usb_create_event_thread(device_t);
    232 Static void	usb_event_thread(void *);
    233 Static void	usb_task_thread(void *);
    234 
    235 #define USB_MAX_EVENTS 100
    236 struct usb_event_q {
    237 	struct usb_event ue;
    238 	SIMPLEQ_ENTRY(usb_event_q) next;
    239 };
    240 Static SIMPLEQ_HEAD(, usb_event_q) usb_events =
    241 	SIMPLEQ_HEAD_INITIALIZER(usb_events);
    242 Static int usb_nevents = 0;
    243 Static struct selinfo usb_selevent;
    244 Static kmutex_t usb_event_lock;
    245 Static kcondvar_t usb_event_cv;
    246 /* XXX this is gross and broken */
    247 Static proc_t *usb_async_proc;  /* process that wants USB SIGIO */
    248 Static void *usb_async_sih;
    249 Static int usb_dev_open = 0;
    250 Static struct usb_event *usb_alloc_event(void);
    251 Static void usb_free_event(struct usb_event *);
    252 Static void usb_add_event(int, struct usb_event *);
    253 Static int usb_get_next_event(struct usb_event *);
    254 Static void usb_async_intr(void *);
    255 Static void usb_soft_intr(void *);
    256 
    257 Static const char *usbrev_str[] = USBREV_STR;
    258 
    259 static int usb_match(device_t, cfdata_t, void *);
    260 static void usb_attach(device_t, device_t, void *);
    261 static int usb_detach(device_t, int);
    262 static int usb_activate(device_t, enum devact);
    263 static void usb_childdet(device_t, device_t);
    264 static int usb_once_init(void);
    265 static void usb_doattach(device_t);
    266 
    267 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
    268     usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
    269     DVF_DETACH_SHUTDOWN);
    270 
    271 static const char *taskq_names[] = USB_TASKQ_NAMES;
    272 
    273 int
    274 usb_match(device_t parent, cfdata_t match, void *aux)
    275 {
    276 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    277 
    278 	return UMATCH_GENERIC;
    279 }
    280 
    281 void
    282 usb_attach(device_t parent, device_t self, void *aux)
    283 {
    284 	static ONCE_DECL(init_control);
    285 	struct usb_softc *sc = device_private(self);
    286 	int usbrev;
    287 
    288 	sc->sc_bus = aux;
    289 	usbrev = sc->sc_bus->ub_revision;
    290 
    291 	cv_init(&sc->sc_bus->ub_needsexplore_cv, "usbevt");
    292 	sc->sc_pmf_registered = false;
    293 
    294 	aprint_naive("\n");
    295 	aprint_normal(": USB revision %s", usbrev_str[usbrev]);
    296 	switch (usbrev) {
    297 	case USBREV_1_0:
    298 	case USBREV_1_1:
    299 	case USBREV_2_0:
    300 	case USBREV_3_0:
    301 	case USBREV_3_1:
    302 		break;
    303 	default:
    304 		aprint_error(", not supported\n");
    305 		sc->sc_dying = 1;
    306 		return;
    307 	}
    308 	aprint_normal("\n");
    309 
    310 	/* XXX we should have our own level */
    311 	sc->sc_bus->ub_soft = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
    312 	    usb_soft_intr, sc->sc_bus);
    313 	if (sc->sc_bus->ub_soft == NULL) {
    314 		aprint_error("%s: can't register softintr\n",
    315 			     device_xname(self));
    316 		sc->sc_dying = 1;
    317 		return;
    318 	}
    319 
    320 	sc->sc_bus->ub_methods->ubm_getlock(sc->sc_bus, &sc->sc_bus->ub_lock);
    321 	KASSERT(sc->sc_bus->ub_lock != NULL);
    322 
    323 	RUN_ONCE(&init_control, usb_once_init);
    324 	config_interrupts(self, usb_doattach);
    325 }
    326 
    327 static int
    328 usb_once_init(void)
    329 {
    330 	struct usb_taskq *taskq;
    331 	int i;
    332 
    333 	USBHIST_LINK_STATIC(usbhist);
    334 
    335 	selinit(&usb_selevent);
    336 	mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
    337 	cv_init(&usb_event_cv, "usbrea");
    338 
    339 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    340 		taskq = &usb_taskq[i];
    341 
    342 		TAILQ_INIT(&taskq->tasks);
    343 		/*
    344 		 * Since USB task methods usb_{add,rem}_task are callable
    345 		 * from any context, we have to make this lock a spinlock.
    346 		 */
    347 		mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
    348 		cv_init(&taskq->cv, "usbtsk");
    349 		taskq->name = taskq_names[i];
    350 		taskq->current_task = NULL;
    351 		if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    352 		    usb_task_thread, taskq, &taskq->task_thread_lwp,
    353 		    "%s", taskq->name)) {
    354 			printf("unable to create task thread: %s\n", taskq->name);
    355 			panic("usb_create_event_thread task");
    356 		}
    357 		/*
    358 		 * XXX we should make sure these threads are alive before
    359 		 * end up using them in usb_doattach().
    360 		 */
    361 	}
    362 
    363 	KASSERT(usb_async_sih == NULL);
    364 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    365 	   usb_async_intr, NULL);
    366 
    367 	return 0;
    368 }
    369 
    370 static void
    371 usb_doattach(device_t self)
    372 {
    373 	struct usb_softc *sc = device_private(self);
    374 	struct usbd_device *dev;
    375 	usbd_status err;
    376 	int speed;
    377 	struct usb_event *ue;
    378 
    379 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    380 
    381 	sc->sc_bus->ub_usbctl = self;
    382 	sc->sc_port.up_power = USB_MAX_POWER;
    383 
    384 	switch (sc->sc_bus->ub_revision) {
    385 	case USBREV_1_0:
    386 	case USBREV_1_1:
    387 		speed = USB_SPEED_FULL;
    388 		break;
    389 	case USBREV_2_0:
    390 		speed = USB_SPEED_HIGH;
    391 		break;
    392 	case USBREV_3_0:
    393 		speed = USB_SPEED_SUPER;
    394 		break;
    395 	case USBREV_3_1:
    396 		speed = USB_SPEED_SUPER_PLUS;
    397 		break;
    398 	default:
    399 		panic("usb_doattach");
    400 	}
    401 
    402 	ue = usb_alloc_event();
    403 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
    404 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    405 
    406 	err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
    407 		  &sc->sc_port);
    408 	if (!err) {
    409 		dev = sc->sc_port.up_dev;
    410 		if (dev->ud_hub == NULL) {
    411 			sc->sc_dying = 1;
    412 			aprint_error("%s: root device is not a hub\n",
    413 				     device_xname(self));
    414 			return;
    415 		}
    416 		sc->sc_bus->ub_roothub = dev;
    417 		usb_create_event_thread(self);
    418 	} else {
    419 		aprint_error("%s: root hub problem, error=%s\n",
    420 			     device_xname(self), usbd_errstr(err));
    421 		sc->sc_dying = 1;
    422 	}
    423 
    424 	/*
    425 	 * Drop this reference after the first set of attachments in the
    426 	 * event thread.
    427 	 */
    428 	config_pending_incr(self);
    429 
    430 	if (!pmf_device_register(self, NULL, NULL))
    431 		aprint_error_dev(self, "couldn't establish power handler\n");
    432 	else
    433 		sc->sc_pmf_registered = true;
    434 
    435 	return;
    436 }
    437 
    438 void
    439 usb_create_event_thread(device_t self)
    440 {
    441 	struct usb_softc *sc = device_private(self);
    442 
    443 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    444 	    usb_event_thread, sc, &sc->sc_event_thread,
    445 	    "%s", device_xname(self))) {
    446 		printf("%s: unable to create event thread for\n",
    447 		       device_xname(self));
    448 		panic("usb_create_event_thread");
    449 	}
    450 }
    451 
    452 /*
    453  * Add a task to be performed by the task thread.  This function can be
    454  * called from any context and the task will be executed in a process
    455  * context ASAP.
    456  */
    457 void
    458 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
    459 {
    460 	struct usb_taskq *taskq;
    461 
    462 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    463 	SDT_PROBE3(usb, kernel, task, add,  dev, task, queue);
    464 
    465 	KASSERT(0 <= queue);
    466 	KASSERT(queue < USB_NUM_TASKQS);
    467 	taskq = &usb_taskq[queue];
    468 	mutex_enter(&taskq->lock);
    469 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
    470 	    USB_NUM_TASKQS) {
    471 		DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
    472 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    473 		cv_signal(&taskq->cv);
    474 	} else {
    475 		DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
    476 	}
    477 	mutex_exit(&taskq->lock);
    478 }
    479 
    480 /*
    481  * usb_rem_task(dev, task)
    482  *
    483  *	If task is queued to run, remove it from the queue.  Return
    484  *	true if it successfully removed the task from the queue, false
    485  *	if not.
    486  *
    487  *	Caller is _not_ guaranteed that the task is not running when
    488  *	this is done.
    489  *
    490  *	Never sleeps.
    491  */
    492 bool
    493 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
    494 {
    495 	unsigned queue;
    496 
    497 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    498 	SDT_PROBE2(usb, kernel, task, rem__start,  dev, task);
    499 
    500 	while ((queue = task->queue) != USB_NUM_TASKQS) {
    501 		struct usb_taskq *taskq = &usb_taskq[queue];
    502 		mutex_enter(&taskq->lock);
    503 		if (__predict_true(task->queue == queue)) {
    504 			TAILQ_REMOVE(&taskq->tasks, task, next);
    505 			task->queue = USB_NUM_TASKQS;
    506 			mutex_exit(&taskq->lock);
    507 			SDT_PROBE3(usb, kernel, task, rem__done,
    508 			    dev, task, true);
    509 			return true; /* removed from the queue */
    510 		}
    511 		mutex_exit(&taskq->lock);
    512 	}
    513 
    514 	SDT_PROBE3(usb, kernel, task, rem__done,  dev, task, false);
    515 	return false;		/* was not removed from the queue */
    516 }
    517 
    518 /*
    519  * usb_rem_task_wait(dev, task, queue, interlock)
    520  *
    521  *	If task is scheduled to run, remove it from the queue.  If it
    522  *	may have already begun to run, drop interlock if not null, wait
    523  *	for it to complete, and reacquire interlock if not null.
    524  *	Return true if it successfully removed the task from the queue,
    525  *	false if not.
    526  *
    527  *	Caller MUST guarantee that task will not be scheduled on a
    528  *	_different_ queue, at least until after this returns.
    529  *
    530  *	If caller guarantees that task will not be scheduled on the
    531  *	same queue before this returns, then caller is guaranteed that
    532  *	the task is not running at all when this returns.
    533  *
    534  *	May sleep.
    535  */
    536 bool
    537 usb_rem_task_wait(struct usbd_device *dev, struct usb_task *task, int queue,
    538     kmutex_t *interlock)
    539 {
    540 	struct usb_taskq *taskq;
    541 	int queue1;
    542 	bool removed;
    543 
    544 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    545 	SDT_PROBE4(usb, kernel, task, rem__wait__start,
    546 	    dev, task, queue, interlock);
    547 	ASSERT_SLEEPABLE();
    548 	KASSERT(0 <= queue);
    549 	KASSERT(queue < USB_NUM_TASKQS);
    550 
    551 	taskq = &usb_taskq[queue];
    552 	mutex_enter(&taskq->lock);
    553 	queue1 = task->queue;
    554 	if (queue1 == USB_NUM_TASKQS) {
    555 		/*
    556 		 * It is not on the queue.  It may be about to run, or
    557 		 * it may have already finished running -- there is no
    558 		 * stopping it now.  Wait for it if it is running.
    559 		 */
    560 		if (interlock)
    561 			mutex_exit(interlock);
    562 		while (taskq->current_task == task)
    563 			cv_wait(&taskq->cv, &taskq->lock);
    564 		removed = false;
    565 	} else {
    566 		/*
    567 		 * It is still on the queue.  We can stop it before the
    568 		 * task thread will run it.
    569 		 */
    570 		KASSERTMSG(queue1 == queue, "task %p on q%d expected on q%d",
    571 		    task, queue1, queue);
    572 		TAILQ_REMOVE(&taskq->tasks, task, next);
    573 		task->queue = USB_NUM_TASKQS;
    574 		removed = true;
    575 	}
    576 	mutex_exit(&taskq->lock);
    577 
    578 	/*
    579 	 * If there's an interlock, and we dropped it to wait,
    580 	 * reacquire it.
    581 	 */
    582 	if (interlock && !removed)
    583 		mutex_enter(interlock);
    584 
    585 	SDT_PROBE5(usb, kernel, task, rem__wait__done,
    586 	    dev, task, queue, interlock, removed);
    587 	return removed;
    588 }
    589 
    590 /*
    591  * usb_task_pending(dev, task)
    592  *
    593  *	True if task is queued, false if not.  Note that if task is
    594  *	already running, it is not considered queued.
    595  *
    596  *	For _negative_ diagnostic assertions only:
    597  *
    598  *		KASSERT(!usb_task_pending(dev, task));
    599  */
    600 bool
    601 usb_task_pending(struct usbd_device *dev, struct usb_task *task)
    602 {
    603 
    604 	return task->queue != USB_NUM_TASKQS;
    605 }
    606 
    607 void
    608 usb_event_thread(void *arg)
    609 {
    610 	struct usb_softc *sc = arg;
    611 	struct usbd_bus *bus = sc->sc_bus;
    612 
    613 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    614 
    615 	/*
    616 	 * In case this controller is a companion controller to an
    617 	 * EHCI controller we need to wait until the EHCI controller
    618 	 * has grabbed the port.
    619 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    620 	 * know how to synchronize the creation of the threads so it
    621 	 * will work.
    622 	 */
    623 	usb_delay_ms(bus, 500);
    624 
    625 	/* Make sure first discover does something. */
    626 	mutex_enter(bus->ub_lock);
    627 	sc->sc_bus->ub_needsexplore = 1;
    628 	usb_discover(sc);
    629 	mutex_exit(bus->ub_lock);
    630 
    631 	/* Drop the config_pending reference from attach. */
    632 	config_pending_decr(bus->ub_usbctl);
    633 
    634 	mutex_enter(bus->ub_lock);
    635 	while (!sc->sc_dying) {
    636 #if 0 /* not yet */
    637 		while (sc->sc_bus->ub_usepolling)
    638 			kpause("usbpoll", true, hz, bus->ub_lock);
    639 #endif
    640 
    641 		if (usb_noexplore < 2)
    642 			usb_discover(sc);
    643 
    644 		cv_timedwait(&bus->ub_needsexplore_cv,
    645 		    bus->ub_lock, usb_noexplore ? 0 : hz * 60);
    646 
    647 		DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
    648 	}
    649 	sc->sc_event_thread = NULL;
    650 
    651 	/* In case parent is waiting for us to exit. */
    652 	cv_signal(&bus->ub_needsexplore_cv);
    653 	mutex_exit(bus->ub_lock);
    654 
    655 	DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
    656 	kthread_exit(0);
    657 }
    658 
    659 void
    660 usb_task_thread(void *arg)
    661 {
    662 	struct usb_task *task;
    663 	struct usb_taskq *taskq;
    664 	bool mpsafe;
    665 
    666 	taskq = arg;
    667 
    668 	USBHIST_FUNC();
    669 	USBHIST_CALLARGS(usbdebug, "start taskq %#jx",
    670 	    (uintptr_t)taskq, 0, 0, 0);
    671 
    672 	mutex_enter(&taskq->lock);
    673 	for (;;) {
    674 		task = TAILQ_FIRST(&taskq->tasks);
    675 		if (task == NULL) {
    676 			cv_wait(&taskq->cv, &taskq->lock);
    677 			task = TAILQ_FIRST(&taskq->tasks);
    678 		}
    679 		DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
    680 		if (task != NULL) {
    681 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
    682 			TAILQ_REMOVE(&taskq->tasks, task, next);
    683 			task->queue = USB_NUM_TASKQS;
    684 			taskq->current_task = task;
    685 			mutex_exit(&taskq->lock);
    686 
    687 			if (!mpsafe)
    688 				KERNEL_LOCK(1, curlwp);
    689 			SDT_PROBE1(usb, kernel, task, start,  task);
    690 			task->fun(task->arg);
    691 			/* Can't dereference task after this point.  */
    692 			SDT_PROBE1(usb, kernel, task, done,  task);
    693 			if (!mpsafe)
    694 				KERNEL_UNLOCK_ONE(curlwp);
    695 
    696 			mutex_enter(&taskq->lock);
    697 			KASSERTMSG(taskq->current_task == task,
    698 			    "somebody scribbled on usb taskq %p", taskq);
    699 			taskq->current_task = NULL;
    700 			cv_broadcast(&taskq->cv);
    701 		}
    702 	}
    703 	mutex_exit(&taskq->lock);
    704 }
    705 
    706 int
    707 usbctlprint(void *aux, const char *pnp)
    708 {
    709 	/* only "usb"es can attach to host controllers */
    710 	if (pnp)
    711 		aprint_normal("usb at %s", pnp);
    712 
    713 	return UNCONF;
    714 }
    715 
    716 int
    717 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    718 {
    719 	int unit = minor(dev);
    720 	struct usb_softc *sc;
    721 
    722 	if (unit == USB_DEV_MINOR) {
    723 		if (usb_dev_open)
    724 			return EBUSY;
    725 		usb_dev_open = 1;
    726 		mutex_enter(&proc_lock);
    727 		usb_async_proc = NULL;
    728 		mutex_exit(&proc_lock);
    729 		return 0;
    730 	}
    731 
    732 	sc = device_lookup_private(&usb_cd, unit);
    733 	if (!sc)
    734 		return ENXIO;
    735 
    736 	if (sc->sc_dying)
    737 		return EIO;
    738 
    739 	return 0;
    740 }
    741 
    742 int
    743 usbread(dev_t dev, struct uio *uio, int flag)
    744 {
    745 	struct usb_event *ue;
    746 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    747 	int useold = 0;
    748 	int error, n;
    749 
    750 	if (minor(dev) != USB_DEV_MINOR)
    751 		return ENXIO;
    752 
    753 	switch (uio->uio_resid) {
    754 	case sizeof(struct usb_event_old):
    755 		ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
    756 		useold = 1;
    757 		/* FALLTHROUGH */
    758 	case sizeof(struct usb_event):
    759 		ue = usb_alloc_event();
    760 		break;
    761 	default:
    762 		return EINVAL;
    763 	}
    764 
    765 	error = 0;
    766 	mutex_enter(&usb_event_lock);
    767 	for (;;) {
    768 		n = usb_get_next_event(ue);
    769 		if (n != 0)
    770 			break;
    771 		if (flag & IO_NDELAY) {
    772 			error = EWOULDBLOCK;
    773 			break;
    774 		}
    775 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
    776 		if (error)
    777 			break;
    778 	}
    779 	mutex_exit(&usb_event_lock);
    780 	if (!error) {
    781 		if (useold) { /* copy fields to old struct */
    782 			MODULE_HOOK_CALL(usb_subr_copy_30_hook,
    783 			    (ue, ueo, uio), enosys(), error);
    784 			if (error == ENOSYS)
    785 				error = EINVAL;
    786 
    787 			if (!error)
    788 				error = uiomove((void *)ueo, sizeof(*ueo), uio);
    789 		} else
    790 			error = uiomove((void *)ue, sizeof(*ue), uio);
    791 	}
    792 	usb_free_event(ue);
    793 	if (ueo)
    794 		kmem_free(ueo, sizeof(struct usb_event_old));
    795 
    796 	return error;
    797 }
    798 
    799 int
    800 usbclose(dev_t dev, int flag, int mode,
    801     struct lwp *l)
    802 {
    803 	int unit = minor(dev);
    804 
    805 	if (unit == USB_DEV_MINOR) {
    806 		mutex_enter(&proc_lock);
    807 		usb_async_proc = NULL;
    808 		mutex_exit(&proc_lock);
    809 		usb_dev_open = 0;
    810 	}
    811 
    812 	return 0;
    813 }
    814 
    815 int
    816 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    817 {
    818 	struct usb_softc *sc;
    819 	int unit = minor(devt);
    820 
    821 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "cmd %#jx", cmd, 0, 0, 0);
    822 
    823 	if (unit == USB_DEV_MINOR) {
    824 		switch (cmd) {
    825 		case FIONBIO:
    826 			/* All handled in the upper FS layer. */
    827 			return 0;
    828 
    829 		case FIOASYNC:
    830 			mutex_enter(&proc_lock);
    831 			if (*(int *)data)
    832 				usb_async_proc = l->l_proc;
    833 			else
    834 				usb_async_proc = NULL;
    835 			mutex_exit(&proc_lock);
    836 			return 0;
    837 
    838 		default:
    839 			return EINVAL;
    840 		}
    841 	}
    842 
    843 	sc = device_lookup_private(&usb_cd, unit);
    844 
    845 	if (sc->sc_dying)
    846 		return EIO;
    847 
    848 	int error = 0;
    849 	switch (cmd) {
    850 #ifdef USB_DEBUG
    851 	case USB_SETDEBUG:
    852 		if (!(flag & FWRITE))
    853 			return EBADF;
    854 		usbdebug  = ((*(int *)data) & 0x000000ff);
    855 		break;
    856 #endif /* USB_DEBUG */
    857 	case USB_REQUEST:
    858 	{
    859 		struct usb_ctl_request *ur = (void *)data;
    860 		int len = UGETW(ur->ucr_request.wLength);
    861 		struct iovec iov;
    862 		struct uio uio;
    863 		void *ptr = 0;
    864 		int addr = ur->ucr_addr;
    865 		usbd_status err;
    866 
    867 		if (!(flag & FWRITE)) {
    868 			error = EBADF;
    869 			goto fail;
    870 		}
    871 
    872 		DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
    873 		if (len < 0 || len > 32768) {
    874 			error = EINVAL;
    875 			goto fail;
    876 		}
    877 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    878 			error = EINVAL;
    879 			goto fail;
    880 		}
    881 		size_t dindex = usb_addr2dindex(addr);
    882 		if (sc->sc_bus->ub_devices[dindex] == NULL) {
    883 			error = EINVAL;
    884 			goto fail;
    885 		}
    886 		if (len != 0) {
    887 			iov.iov_base = (void *)ur->ucr_data;
    888 			iov.iov_len = len;
    889 			uio.uio_iov = &iov;
    890 			uio.uio_iovcnt = 1;
    891 			uio.uio_resid = len;
    892 			uio.uio_offset = 0;
    893 			uio.uio_rw =
    894 				ur->ucr_request.bmRequestType & UT_READ ?
    895 				UIO_READ : UIO_WRITE;
    896 			uio.uio_vmspace = l->l_proc->p_vmspace;
    897 			ptr = kmem_alloc(len, KM_SLEEP);
    898 			if (uio.uio_rw == UIO_WRITE) {
    899 				error = uiomove(ptr, len, &uio);
    900 				if (error)
    901 					goto ret;
    902 			}
    903 		}
    904 		err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
    905 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    906 			  USBD_DEFAULT_TIMEOUT);
    907 		if (err) {
    908 			error = EIO;
    909 			goto ret;
    910 		}
    911 		if (len > ur->ucr_actlen)
    912 			len = ur->ucr_actlen;
    913 		if (len != 0) {
    914 			if (uio.uio_rw == UIO_READ) {
    915 				error = uiomove(ptr, len, &uio);
    916 				if (error)
    917 					goto ret;
    918 			}
    919 		}
    920 	ret:
    921 		if (ptr) {
    922 			len = UGETW(ur->ucr_request.wLength);
    923 			kmem_free(ptr, len);
    924 		}
    925 		break;
    926 	}
    927 
    928 	case USB_DEVICEINFO:
    929 	{
    930 		struct usbd_device *dev;
    931 		struct usb_device_info *di = (void *)data;
    932 		int addr = di->udi_addr;
    933 
    934 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    935 			error = EINVAL;
    936 			goto fail;
    937 		}
    938 		size_t dindex = usb_addr2dindex(addr);
    939 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    940 			error = ENXIO;
    941 			goto fail;
    942 		}
    943 		usbd_fill_deviceinfo(dev, di, 1);
    944 		break;
    945 	}
    946 
    947 	case USB_DEVICEINFO_OLD:
    948 	{
    949 		struct usbd_device *dev;
    950 		struct usb_device_info_old *di = (void *)data;
    951 		int addr = di->udi_addr;
    952 
    953 		if (addr < 1 || addr >= USB_MAX_DEVICES) {
    954 			error = EINVAL;
    955 			goto fail;
    956 		}
    957 		size_t dindex = usb_addr2dindex(addr);
    958 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    959 			error = ENXIO;
    960 			goto fail;
    961 		}
    962 		MODULE_HOOK_CALL(usb_subr_fill_30_hook,
    963 		    (dev, di, 1, usbd_devinfo_vp, usbd_printBCD),
    964 		    enosys(), error);
    965 		if (error == ENOSYS)
    966 			error = EINVAL;
    967 		if (error)
    968 			goto fail;
    969 		break;
    970 	}
    971 
    972 	case USB_DEVICESTATS:
    973 		*(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
    974 		break;
    975 
    976 	default:
    977 		error = EINVAL;
    978 	}
    979 
    980 fail:
    981 
    982 	DPRINTF("... done (error = %jd)", error, 0, 0, 0);
    983 
    984 	return error;
    985 }
    986 
    987 int
    988 usbpoll(dev_t dev, int events, struct lwp *l)
    989 {
    990 	int revents, mask;
    991 
    992 	if (minor(dev) == USB_DEV_MINOR) {
    993 		revents = 0;
    994 		mask = POLLIN | POLLRDNORM;
    995 
    996 		mutex_enter(&usb_event_lock);
    997 		if (events & mask && usb_nevents > 0)
    998 			revents |= events & mask;
    999 		if (revents == 0 && events & mask)
   1000 			selrecord(l, &usb_selevent);
   1001 		mutex_exit(&usb_event_lock);
   1002 
   1003 		return revents;
   1004 	} else {
   1005 		return 0;
   1006 	}
   1007 }
   1008 
   1009 static void
   1010 filt_usbrdetach(struct knote *kn)
   1011 {
   1012 
   1013 	mutex_enter(&usb_event_lock);
   1014 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
   1015 	mutex_exit(&usb_event_lock);
   1016 }
   1017 
   1018 static int
   1019 filt_usbread(struct knote *kn, long hint)
   1020 {
   1021 
   1022 	if (usb_nevents == 0)
   1023 		return 0;
   1024 
   1025 	kn->kn_data = sizeof(struct usb_event);
   1026 	return 1;
   1027 }
   1028 
   1029 static const struct filterops usbread_filtops = {
   1030 	.f_isfd = 1,
   1031 	.f_attach = NULL,
   1032 	.f_detach = filt_usbrdetach,
   1033 	.f_event = filt_usbread,
   1034 };
   1035 
   1036 int
   1037 usbkqfilter(dev_t dev, struct knote *kn)
   1038 {
   1039 	struct klist *klist;
   1040 
   1041 	switch (kn->kn_filter) {
   1042 	case EVFILT_READ:
   1043 		if (minor(dev) != USB_DEV_MINOR)
   1044 			return 1;
   1045 		klist = &usb_selevent.sel_klist;
   1046 		kn->kn_fop = &usbread_filtops;
   1047 		break;
   1048 
   1049 	default:
   1050 		return EINVAL;
   1051 	}
   1052 
   1053 	kn->kn_hook = NULL;
   1054 
   1055 	mutex_enter(&usb_event_lock);
   1056 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1057 	mutex_exit(&usb_event_lock);
   1058 
   1059 	return 0;
   1060 }
   1061 
   1062 /* Explore device tree from the root. */
   1063 Static void
   1064 usb_discover(struct usb_softc *sc)
   1065 {
   1066 	struct usbd_bus *bus = sc->sc_bus;
   1067 
   1068 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1069 
   1070 	KASSERT(mutex_owned(bus->ub_lock));
   1071 
   1072 	if (usb_noexplore > 1)
   1073 		return;
   1074 
   1075 	/*
   1076 	 * We need mutual exclusion while traversing the device tree,
   1077 	 * but this is guaranteed since this function is only called
   1078 	 * from the event thread for the controller.
   1079 	 *
   1080 	 * Also, we now have bus->ub_lock held, and in combination
   1081 	 * with ub_exploring, avoids interferring with polling.
   1082 	 */
   1083 	SDT_PROBE1(usb, kernel, bus, discover__start,  bus);
   1084 	while (bus->ub_needsexplore && !sc->sc_dying) {
   1085 		bus->ub_needsexplore = 0;
   1086 		mutex_exit(sc->sc_bus->ub_lock);
   1087 		SDT_PROBE1(usb, kernel, bus, explore__start,  bus);
   1088 		bus->ub_roothub->ud_hub->uh_explore(bus->ub_roothub);
   1089 		SDT_PROBE1(usb, kernel, bus, explore__done,  bus);
   1090 		mutex_enter(bus->ub_lock);
   1091 	}
   1092 	SDT_PROBE1(usb, kernel, bus, discover__done,  bus);
   1093 }
   1094 
   1095 void
   1096 usb_needs_explore(struct usbd_device *dev)
   1097 {
   1098 
   1099 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1100 	SDT_PROBE1(usb, kernel, bus, needs__explore,  dev->ud_bus);
   1101 
   1102 	mutex_enter(dev->ud_bus->ub_lock);
   1103 	dev->ud_bus->ub_needsexplore = 1;
   1104 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1105 	mutex_exit(dev->ud_bus->ub_lock);
   1106 }
   1107 
   1108 void
   1109 usb_needs_reattach(struct usbd_device *dev)
   1110 {
   1111 
   1112 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1113 	SDT_PROBE1(usb, kernel, bus, needs__reattach,  dev->ud_bus);
   1114 
   1115 	mutex_enter(dev->ud_bus->ub_lock);
   1116 	dev->ud_powersrc->up_reattach = 1;
   1117 	dev->ud_bus->ub_needsexplore = 1;
   1118 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1119 	mutex_exit(dev->ud_bus->ub_lock);
   1120 }
   1121 
   1122 /* Called at with usb_event_lock held. */
   1123 int
   1124 usb_get_next_event(struct usb_event *ue)
   1125 {
   1126 	struct usb_event_q *ueq;
   1127 
   1128 	KASSERT(mutex_owned(&usb_event_lock));
   1129 
   1130 	if (usb_nevents <= 0)
   1131 		return 0;
   1132 	ueq = SIMPLEQ_FIRST(&usb_events);
   1133 #ifdef DIAGNOSTIC
   1134 	if (ueq == NULL) {
   1135 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
   1136 		usb_nevents = 0;
   1137 		return 0;
   1138 	}
   1139 #endif
   1140 	if (ue)
   1141 		*ue = ueq->ue;
   1142 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
   1143 	usb_free_event((struct usb_event *)(void *)ueq);
   1144 	usb_nevents--;
   1145 	return 1;
   1146 }
   1147 
   1148 void
   1149 usbd_add_dev_event(int type, struct usbd_device *udev)
   1150 {
   1151 	struct usb_event *ue = usb_alloc_event();
   1152 
   1153 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
   1154 	usb_add_event(type, ue);
   1155 }
   1156 
   1157 void
   1158 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
   1159 {
   1160 	struct usb_event *ue = usb_alloc_event();
   1161 
   1162 	ue->u.ue_driver.ue_cookie = udev->ud_cookie;
   1163 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
   1164 	    sizeof(ue->u.ue_driver.ue_devname));
   1165 	usb_add_event(type, ue);
   1166 }
   1167 
   1168 Static struct usb_event *
   1169 usb_alloc_event(void)
   1170 {
   1171 	/* Yes, this is right; we allocate enough so that we can use it later */
   1172 	return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
   1173 }
   1174 
   1175 Static void
   1176 usb_free_event(struct usb_event *uep)
   1177 {
   1178 	kmem_free(uep, sizeof(struct usb_event_q));
   1179 }
   1180 
   1181 Static void
   1182 usb_add_event(int type, struct usb_event *uep)
   1183 {
   1184 	struct usb_event_q *ueq;
   1185 	struct timeval thetime;
   1186 
   1187 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1188 
   1189 	microtime(&thetime);
   1190 	/* Don't want to wait here with usb_event_lock held */
   1191 	ueq = (struct usb_event_q *)(void *)uep;
   1192 	ueq->ue = *uep;
   1193 	ueq->ue.ue_type = type;
   1194 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1195 	SDT_PROBE1(usb, kernel, event, add,  uep);
   1196 
   1197 	mutex_enter(&usb_event_lock);
   1198 	if (++usb_nevents >= USB_MAX_EVENTS) {
   1199 		/* Too many queued events, drop an old one. */
   1200 		DPRINTF("event dropped", 0, 0, 0, 0);
   1201 #ifdef KDTRACE_HOOKS
   1202 		struct usb_event oue;
   1203 		if (usb_get_next_event(&oue))
   1204 			SDT_PROBE1(usb, kernel, event, drop,  &oue);
   1205 #else
   1206 		usb_get_next_event(NULL);
   1207 #endif
   1208 	}
   1209 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
   1210 	cv_signal(&usb_event_cv);
   1211 	selnotify(&usb_selevent, 0, 0);
   1212 	if (usb_async_proc != NULL) {
   1213 		kpreempt_disable();
   1214 		softint_schedule(usb_async_sih);
   1215 		kpreempt_enable();
   1216 	}
   1217 	mutex_exit(&usb_event_lock);
   1218 }
   1219 
   1220 Static void
   1221 usb_async_intr(void *cookie)
   1222 {
   1223 	proc_t *proc;
   1224 
   1225 	mutex_enter(&proc_lock);
   1226 	if ((proc = usb_async_proc) != NULL)
   1227 		psignal(proc, SIGIO);
   1228 	mutex_exit(&proc_lock);
   1229 }
   1230 
   1231 Static void
   1232 usb_soft_intr(void *arg)
   1233 {
   1234 	struct usbd_bus *bus = arg;
   1235 
   1236 	mutex_enter(bus->ub_lock);
   1237 	bus->ub_methods->ubm_softint(bus);
   1238 	mutex_exit(bus->ub_lock);
   1239 }
   1240 
   1241 void
   1242 usb_schedsoftintr(struct usbd_bus *bus)
   1243 {
   1244 
   1245 	USBHIST_FUNC();
   1246 	USBHIST_CALLARGS(usbdebug, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
   1247 
   1248 	/* In case the bus never finished setting up. */
   1249 	if (__predict_false(bus->ub_soft == NULL))
   1250 		return;
   1251 
   1252 	if (bus->ub_usepolling) {
   1253 		bus->ub_methods->ubm_softint(bus);
   1254 	} else {
   1255 		kpreempt_disable();
   1256 		softint_schedule(bus->ub_soft);
   1257 		kpreempt_enable();
   1258 	}
   1259 }
   1260 
   1261 int
   1262 usb_activate(device_t self, enum devact act)
   1263 {
   1264 	struct usb_softc *sc = device_private(self);
   1265 
   1266 	switch (act) {
   1267 	case DVACT_DEACTIVATE:
   1268 		sc->sc_dying = 1;
   1269 		return 0;
   1270 	default:
   1271 		return EOPNOTSUPP;
   1272 	}
   1273 }
   1274 
   1275 void
   1276 usb_childdet(device_t self, device_t child)
   1277 {
   1278 	int i;
   1279 	struct usb_softc *sc = device_private(self);
   1280 	struct usbd_device *dev;
   1281 
   1282 	if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
   1283 		return;
   1284 
   1285 	for (i = 0; i < dev->ud_subdevlen; i++)
   1286 		if (dev->ud_subdevs[i] == child)
   1287 			dev->ud_subdevs[i] = NULL;
   1288 }
   1289 
   1290 int
   1291 usb_detach(device_t self, int flags)
   1292 {
   1293 	struct usb_softc *sc = device_private(self);
   1294 	struct usb_event *ue;
   1295 	int rc;
   1296 
   1297 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1298 
   1299 	/* Make all devices disconnect. */
   1300 	if (sc->sc_port.up_dev != NULL &&
   1301 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
   1302 		return rc;
   1303 
   1304 	if (sc->sc_pmf_registered)
   1305 		pmf_device_deregister(self);
   1306 	/* Kill off event thread. */
   1307 	sc->sc_dying = 1;
   1308 	while (sc->sc_event_thread != NULL) {
   1309 		mutex_enter(sc->sc_bus->ub_lock);
   1310 		cv_signal(&sc->sc_bus->ub_needsexplore_cv);
   1311 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
   1312 		    sc->sc_bus->ub_lock, hz * 60);
   1313 		mutex_exit(sc->sc_bus->ub_lock);
   1314 	}
   1315 	DPRINTF("event thread dead", 0, 0, 0, 0);
   1316 
   1317 	if (sc->sc_bus->ub_soft != NULL) {
   1318 		softint_disestablish(sc->sc_bus->ub_soft);
   1319 		sc->sc_bus->ub_soft = NULL;
   1320 	}
   1321 
   1322 	ue = usb_alloc_event();
   1323 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
   1324 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1325 
   1326 	cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
   1327 
   1328 	return 0;
   1329 }
   1330