Home | History | Annotate | Line # | Download | only in usb
usb.c revision 1.184
      1 /*	$NetBSD: usb.c,v 1.184 2020/05/23 23:42:42 ad 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.184 2020/05/23 23:42:42 ad 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 
    268 
    269 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc),
    270     usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet,
    271     DVF_DETACH_SHUTDOWN);
    272 
    273 static const char *taskq_names[] = USB_TASKQ_NAMES;
    274 
    275 int
    276 usb_match(device_t parent, cfdata_t match, void *aux)
    277 {
    278 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    279 
    280 	return UMATCH_GENERIC;
    281 }
    282 
    283 void
    284 usb_attach(device_t parent, device_t self, void *aux)
    285 {
    286 	static ONCE_DECL(init_control);
    287 	struct usb_softc *sc = device_private(self);
    288 	int usbrev;
    289 
    290 	sc->sc_bus = aux;
    291 	usbrev = sc->sc_bus->ub_revision;
    292 
    293 	cv_init(&sc->sc_bus->ub_needsexplore_cv, "usbevt");
    294 	sc->sc_pmf_registered = false;
    295 
    296 	aprint_naive("\n");
    297 	aprint_normal(": USB revision %s", usbrev_str[usbrev]);
    298 	switch (usbrev) {
    299 	case USBREV_1_0:
    300 	case USBREV_1_1:
    301 	case USBREV_2_0:
    302 	case USBREV_3_0:
    303 	case USBREV_3_1:
    304 		break;
    305 	default:
    306 		aprint_error(", not supported\n");
    307 		sc->sc_dying = 1;
    308 		return;
    309 	}
    310 	aprint_normal("\n");
    311 
    312 	/* XXX we should have our own level */
    313 	sc->sc_bus->ub_soft = softint_establish(SOFTINT_USB | SOFTINT_MPSAFE,
    314 	    usb_soft_intr, sc->sc_bus);
    315 	if (sc->sc_bus->ub_soft == NULL) {
    316 		aprint_error("%s: can't register softintr\n",
    317 			     device_xname(self));
    318 		sc->sc_dying = 1;
    319 		return;
    320 	}
    321 
    322 	sc->sc_bus->ub_methods->ubm_getlock(sc->sc_bus, &sc->sc_bus->ub_lock);
    323 	KASSERT(sc->sc_bus->ub_lock != NULL);
    324 
    325 	RUN_ONCE(&init_control, usb_once_init);
    326 	config_interrupts(self, usb_doattach);
    327 }
    328 
    329 static int
    330 usb_once_init(void)
    331 {
    332 	struct usb_taskq *taskq;
    333 	int i;
    334 
    335 	USBHIST_LINK_STATIC(usbhist);
    336 
    337 	selinit(&usb_selevent);
    338 	mutex_init(&usb_event_lock, MUTEX_DEFAULT, IPL_NONE);
    339 	cv_init(&usb_event_cv, "usbrea");
    340 
    341 	for (i = 0; i < USB_NUM_TASKQS; i++) {
    342 		taskq = &usb_taskq[i];
    343 
    344 		TAILQ_INIT(&taskq->tasks);
    345 		/*
    346 		 * Since USB task methods usb_{add,rem}_task are callable
    347 		 * from any context, we have to make this lock a spinlock.
    348 		 */
    349 		mutex_init(&taskq->lock, MUTEX_DEFAULT, IPL_USB);
    350 		cv_init(&taskq->cv, "usbtsk");
    351 		taskq->name = taskq_names[i];
    352 		taskq->current_task = NULL;
    353 		if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    354 		    usb_task_thread, taskq, &taskq->task_thread_lwp,
    355 		    "%s", taskq->name)) {
    356 			printf("unable to create task thread: %s\n", taskq->name);
    357 			panic("usb_create_event_thread task");
    358 		}
    359 		/*
    360 		 * XXX we should make sure these threads are alive before
    361 		 * end up using them in usb_doattach().
    362 		 */
    363 	}
    364 
    365 	KASSERT(usb_async_sih == NULL);
    366 	usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE,
    367 	   usb_async_intr, NULL);
    368 
    369 	return 0;
    370 }
    371 
    372 static void
    373 usb_doattach(device_t self)
    374 {
    375 	struct usb_softc *sc = device_private(self);
    376 	struct usbd_device *dev;
    377 	usbd_status err;
    378 	int speed;
    379 	struct usb_event *ue;
    380 
    381 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    382 
    383 	sc->sc_bus->ub_usbctl = self;
    384 	sc->sc_port.up_power = USB_MAX_POWER;
    385 
    386 	switch (sc->sc_bus->ub_revision) {
    387 	case USBREV_1_0:
    388 	case USBREV_1_1:
    389 		speed = USB_SPEED_FULL;
    390 		break;
    391 	case USBREV_2_0:
    392 		speed = USB_SPEED_HIGH;
    393 		break;
    394 	case USBREV_3_0:
    395 		speed = USB_SPEED_SUPER;
    396 		break;
    397 	case USBREV_3_1:
    398 		speed = USB_SPEED_SUPER_PLUS;
    399 		break;
    400 	default:
    401 		panic("usb_doattach");
    402 	}
    403 
    404 	ue = usb_alloc_event();
    405 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
    406 	usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
    407 
    408 	err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
    409 		  &sc->sc_port);
    410 	if (!err) {
    411 		dev = sc->sc_port.up_dev;
    412 		if (dev->ud_hub == NULL) {
    413 			sc->sc_dying = 1;
    414 			aprint_error("%s: root device is not a hub\n",
    415 				     device_xname(self));
    416 			return;
    417 		}
    418 		sc->sc_bus->ub_roothub = dev;
    419 		usb_create_event_thread(self);
    420 	} else {
    421 		aprint_error("%s: root hub problem, error=%s\n",
    422 			     device_xname(self), usbd_errstr(err));
    423 		sc->sc_dying = 1;
    424 	}
    425 
    426 	/*
    427 	 * Drop this reference after the first set of attachments in the
    428 	 * event thread.
    429 	 */
    430 	config_pending_incr(self);
    431 
    432 	if (!pmf_device_register(self, NULL, NULL))
    433 		aprint_error_dev(self, "couldn't establish power handler\n");
    434 	else
    435 		sc->sc_pmf_registered = true;
    436 
    437 	return;
    438 }
    439 
    440 void
    441 usb_create_event_thread(device_t self)
    442 {
    443 	struct usb_softc *sc = device_private(self);
    444 
    445 	if (kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL,
    446 	    usb_event_thread, sc, &sc->sc_event_thread,
    447 	    "%s", device_xname(self))) {
    448 		printf("%s: unable to create event thread for\n",
    449 		       device_xname(self));
    450 		panic("usb_create_event_thread");
    451 	}
    452 }
    453 
    454 /*
    455  * Add a task to be performed by the task thread.  This function can be
    456  * called from any context and the task will be executed in a process
    457  * context ASAP.
    458  */
    459 void
    460 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
    461 {
    462 	struct usb_taskq *taskq;
    463 
    464 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    465 	SDT_PROBE3(usb, kernel, task, add,  dev, task, queue);
    466 
    467 	KASSERT(0 <= queue);
    468 	KASSERT(queue < USB_NUM_TASKQS);
    469 	taskq = &usb_taskq[queue];
    470 	mutex_enter(&taskq->lock);
    471 	if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
    472 	    USB_NUM_TASKQS) {
    473 		DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
    474 		TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
    475 		cv_signal(&taskq->cv);
    476 	} else {
    477 		DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
    478 	}
    479 	mutex_exit(&taskq->lock);
    480 }
    481 
    482 /*
    483  * usb_rem_task(dev, task)
    484  *
    485  *	If task is queued to run, remove it from the queue.  Return
    486  *	true if it successfully removed the task from the queue, false
    487  *	if not.
    488  *
    489  *	Caller is _not_ guaranteed that the task is not running when
    490  *	this is done.
    491  *
    492  *	Never sleeps.
    493  */
    494 bool
    495 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
    496 {
    497 	unsigned queue;
    498 
    499 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    500 	SDT_PROBE2(usb, kernel, task, rem__start,  dev, task);
    501 
    502 	while ((queue = task->queue) != USB_NUM_TASKQS) {
    503 		struct usb_taskq *taskq = &usb_taskq[queue];
    504 		mutex_enter(&taskq->lock);
    505 		if (__predict_true(task->queue == queue)) {
    506 			TAILQ_REMOVE(&taskq->tasks, task, next);
    507 			task->queue = USB_NUM_TASKQS;
    508 			mutex_exit(&taskq->lock);
    509 			SDT_PROBE3(usb, kernel, task, rem__done,
    510 			    dev, task, true);
    511 			return true; /* removed from the queue */
    512 		}
    513 		mutex_exit(&taskq->lock);
    514 	}
    515 
    516 	SDT_PROBE3(usb, kernel, task, rem__done,  dev, task, false);
    517 	return false;		/* was not removed from the queue */
    518 }
    519 
    520 /*
    521  * usb_rem_task_wait(dev, task, queue, interlock)
    522  *
    523  *	If task is scheduled to run, remove it from the queue.  If it
    524  *	may have already begun to run, drop interlock if not null, wait
    525  *	for it to complete, and reacquire interlock if not null.
    526  *	Return true if it successfully removed the task from the queue,
    527  *	false if not.
    528  *
    529  *	Caller MUST guarantee that task will not be scheduled on a
    530  *	_different_ queue, at least until after this returns.
    531  *
    532  *	If caller guarantees that task will not be scheduled on the
    533  *	same queue before this returns, then caller is guaranteed that
    534  *	the task is not running at all when this returns.
    535  *
    536  *	May sleep.
    537  */
    538 bool
    539 usb_rem_task_wait(struct usbd_device *dev, struct usb_task *task, int queue,
    540     kmutex_t *interlock)
    541 {
    542 	struct usb_taskq *taskq;
    543 	int queue1;
    544 	bool removed;
    545 
    546 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    547 	SDT_PROBE4(usb, kernel, task, rem__wait__start,
    548 	    dev, task, queue, interlock);
    549 	ASSERT_SLEEPABLE();
    550 	KASSERT(0 <= queue);
    551 	KASSERT(queue < USB_NUM_TASKQS);
    552 
    553 	taskq = &usb_taskq[queue];
    554 	mutex_enter(&taskq->lock);
    555 	queue1 = task->queue;
    556 	if (queue1 == USB_NUM_TASKQS) {
    557 		/*
    558 		 * It is not on the queue.  It may be about to run, or
    559 		 * it may have already finished running -- there is no
    560 		 * stopping it now.  Wait for it if it is running.
    561 		 */
    562 		if (interlock)
    563 			mutex_exit(interlock);
    564 		while (taskq->current_task == task)
    565 			cv_wait(&taskq->cv, &taskq->lock);
    566 		removed = false;
    567 	} else {
    568 		/*
    569 		 * It is still on the queue.  We can stop it before the
    570 		 * task thread will run it.
    571 		 */
    572 		KASSERTMSG(queue1 == queue, "task %p on q%d expected on q%d",
    573 		    task, queue1, queue);
    574 		TAILQ_REMOVE(&taskq->tasks, task, next);
    575 		task->queue = USB_NUM_TASKQS;
    576 		removed = true;
    577 	}
    578 	mutex_exit(&taskq->lock);
    579 
    580 	/*
    581 	 * If there's an interlock, and we dropped it to wait,
    582 	 * reacquire it.
    583 	 */
    584 	if (interlock && !removed)
    585 		mutex_enter(interlock);
    586 
    587 	SDT_PROBE5(usb, kernel, task, rem__wait__done,
    588 	    dev, task, queue, interlock, removed);
    589 	return removed;
    590 }
    591 
    592 /*
    593  * usb_task_pending(dev, task)
    594  *
    595  *	True if task is queued, false if not.  Note that if task is
    596  *	already running, it is not considered queued.
    597  *
    598  *	For _negative_ diagnostic assertions only:
    599  *
    600  *		KASSERT(!usb_task_pending(dev, task));
    601  */
    602 bool
    603 usb_task_pending(struct usbd_device *dev, struct usb_task *task)
    604 {
    605 
    606 	return task->queue != USB_NUM_TASKQS;
    607 }
    608 
    609 void
    610 usb_event_thread(void *arg)
    611 {
    612 	struct usb_softc *sc = arg;
    613 	struct usbd_bus *bus = sc->sc_bus;
    614 
    615 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
    616 
    617 	/*
    618 	 * In case this controller is a companion controller to an
    619 	 * EHCI controller we need to wait until the EHCI controller
    620 	 * has grabbed the port.
    621 	 * XXX It would be nicer to do this with a tsleep(), but I don't
    622 	 * know how to synchronize the creation of the threads so it
    623 	 * will work.
    624 	 */
    625 	usb_delay_ms(bus, 500);
    626 
    627 	/* Make sure first discover does something. */
    628 	mutex_enter(bus->ub_lock);
    629 	sc->sc_bus->ub_needsexplore = 1;
    630 	usb_discover(sc);
    631 	mutex_exit(bus->ub_lock);
    632 
    633 	/* Drop the config_pending reference from attach. */
    634 	config_pending_decr(bus->ub_usbctl);
    635 
    636 	mutex_enter(bus->ub_lock);
    637 	while (!sc->sc_dying) {
    638 #if 0 /* not yet */
    639 		while (sc->sc_bus->ub_usepolling)
    640 			kpause("usbpoll", true, hz, bus->ub_lock);
    641 #endif
    642 
    643 		if (usb_noexplore < 2)
    644 			usb_discover(sc);
    645 
    646 		cv_timedwait(&bus->ub_needsexplore_cv,
    647 		    bus->ub_lock, usb_noexplore ? 0 : hz * 60);
    648 
    649 		DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
    650 	}
    651 	sc->sc_event_thread = NULL;
    652 
    653 	/* In case parent is waiting for us to exit. */
    654 	cv_signal(&bus->ub_needsexplore_cv);
    655 	mutex_exit(bus->ub_lock);
    656 
    657 	DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
    658 	kthread_exit(0);
    659 }
    660 
    661 void
    662 usb_task_thread(void *arg)
    663 {
    664 	struct usb_task *task;
    665 	struct usb_taskq *taskq;
    666 	bool mpsafe;
    667 
    668 	taskq = arg;
    669 
    670 	USBHIST_FUNC();
    671 	USBHIST_CALLARGS(usbdebug, "start taskq %#jx",
    672 	    (uintptr_t)taskq, 0, 0, 0);
    673 
    674 	mutex_enter(&taskq->lock);
    675 	for (;;) {
    676 		task = TAILQ_FIRST(&taskq->tasks);
    677 		if (task == NULL) {
    678 			cv_wait(&taskq->cv, &taskq->lock);
    679 			task = TAILQ_FIRST(&taskq->tasks);
    680 		}
    681 		DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
    682 		if (task != NULL) {
    683 			mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
    684 			TAILQ_REMOVE(&taskq->tasks, task, next);
    685 			task->queue = USB_NUM_TASKQS;
    686 			taskq->current_task = task;
    687 			mutex_exit(&taskq->lock);
    688 
    689 			if (!mpsafe)
    690 				KERNEL_LOCK(1, curlwp);
    691 			SDT_PROBE1(usb, kernel, task, start,  task);
    692 			task->fun(task->arg);
    693 			/* Can't dereference task after this point.  */
    694 			SDT_PROBE1(usb, kernel, task, done,  task);
    695 			if (!mpsafe)
    696 				KERNEL_UNLOCK_ONE(curlwp);
    697 
    698 			mutex_enter(&taskq->lock);
    699 			KASSERTMSG(taskq->current_task == task,
    700 			    "somebody scribbled on usb taskq %p", taskq);
    701 			taskq->current_task = NULL;
    702 			cv_broadcast(&taskq->cv);
    703 		}
    704 	}
    705 	mutex_exit(&taskq->lock);
    706 }
    707 
    708 int
    709 usbctlprint(void *aux, const char *pnp)
    710 {
    711 	/* only "usb"es can attach to host controllers */
    712 	if (pnp)
    713 		aprint_normal("usb at %s", pnp);
    714 
    715 	return UNCONF;
    716 }
    717 
    718 int
    719 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
    720 {
    721 	int unit = minor(dev);
    722 	struct usb_softc *sc;
    723 
    724 	if (unit == USB_DEV_MINOR) {
    725 		if (usb_dev_open)
    726 			return EBUSY;
    727 		usb_dev_open = 1;
    728 		mutex_enter(&proc_lock);
    729 		usb_async_proc = 0;
    730 		mutex_exit(&proc_lock);
    731 		return 0;
    732 	}
    733 
    734 	sc = device_lookup_private(&usb_cd, unit);
    735 	if (!sc)
    736 		return ENXIO;
    737 
    738 	if (sc->sc_dying)
    739 		return EIO;
    740 
    741 	return 0;
    742 }
    743 
    744 int
    745 usbread(dev_t dev, struct uio *uio, int flag)
    746 {
    747 	struct usb_event *ue;
    748 	struct usb_event_old *ueo = NULL;	/* XXXGCC */
    749 	int useold = 0;
    750 	int error, n;
    751 
    752 	if (minor(dev) != USB_DEV_MINOR)
    753 		return ENXIO;
    754 
    755 	switch (uio->uio_resid) {
    756 	case sizeof(struct usb_event_old):
    757 		ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
    758 		useold = 1;
    759 		/* FALLTHROUGH */
    760 	case sizeof(struct usb_event):
    761 		ue = usb_alloc_event();
    762 		break;
    763 	default:
    764 		return EINVAL;
    765 	}
    766 
    767 	error = 0;
    768 	mutex_enter(&usb_event_lock);
    769 	for (;;) {
    770 		n = usb_get_next_event(ue);
    771 		if (n != 0)
    772 			break;
    773 		if (flag & IO_NDELAY) {
    774 			error = EWOULDBLOCK;
    775 			break;
    776 		}
    777 		error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
    778 		if (error)
    779 			break;
    780 	}
    781 	mutex_exit(&usb_event_lock);
    782 	if (!error) {
    783 		if (useold) { /* copy fields to old struct */
    784 			MODULE_HOOK_CALL(usb_subr_copy_30_hook,
    785 			    (ue, ueo, uio), enosys(), error);
    786 			if (error == ENOSYS)
    787 				error = EINVAL;
    788 
    789 			if (!error)
    790 				error = uiomove((void *)ueo, sizeof(*ueo), uio);
    791 		} else
    792 			error = uiomove((void *)ue, sizeof(*ue), uio);
    793 	}
    794 	usb_free_event(ue);
    795 	if (ueo)
    796 		kmem_free(ueo, sizeof(struct usb_event_old));
    797 
    798 	return error;
    799 }
    800 
    801 int
    802 usbclose(dev_t dev, int flag, int mode,
    803     struct lwp *l)
    804 {
    805 	int unit = minor(dev);
    806 
    807 	if (unit == USB_DEV_MINOR) {
    808 		mutex_enter(&proc_lock);
    809 		usb_async_proc = 0;
    810 		mutex_exit(&proc_lock);
    811 		usb_dev_open = 0;
    812 	}
    813 
    814 	return 0;
    815 }
    816 
    817 int
    818 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
    819 {
    820 	struct usb_softc *sc;
    821 	int unit = minor(devt);
    822 
    823 	USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "cmd %#jx", cmd, 0, 0, 0);
    824 
    825 	if (unit == USB_DEV_MINOR) {
    826 		switch (cmd) {
    827 		case FIONBIO:
    828 			/* All handled in the upper FS layer. */
    829 			return 0;
    830 
    831 		case FIOASYNC:
    832 			mutex_enter(&proc_lock);
    833 			if (*(int *)data)
    834 				usb_async_proc = l->l_proc;
    835 			else
    836 				usb_async_proc = 0;
    837 			mutex_exit(&proc_lock);
    838 			return 0;
    839 
    840 		default:
    841 			return EINVAL;
    842 		}
    843 	}
    844 
    845 	sc = device_lookup_private(&usb_cd, unit);
    846 
    847 	if (sc->sc_dying)
    848 		return EIO;
    849 
    850 	int error = 0;
    851 	switch (cmd) {
    852 #ifdef USB_DEBUG
    853 	case USB_SETDEBUG:
    854 		if (!(flag & FWRITE))
    855 			return EBADF;
    856 		usbdebug  = ((*(int *)data) & 0x000000ff);
    857 		break;
    858 #endif /* USB_DEBUG */
    859 	case USB_REQUEST:
    860 	{
    861 		struct usb_ctl_request *ur = (void *)data;
    862 		int len = UGETW(ur->ucr_request.wLength);
    863 		struct iovec iov;
    864 		struct uio uio;
    865 		void *ptr = 0;
    866 		int addr = ur->ucr_addr;
    867 		usbd_status err;
    868 
    869 		if (!(flag & FWRITE)) {
    870 			error = EBADF;
    871 			goto fail;
    872 		}
    873 
    874 		DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
    875 		if (len < 0 || len > 32768) {
    876 			error = EINVAL;
    877 			goto fail;
    878 		}
    879 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    880 			error = EINVAL;
    881 			goto fail;
    882 		}
    883 		size_t dindex = usb_addr2dindex(addr);
    884 		if (sc->sc_bus->ub_devices[dindex] == NULL) {
    885 			error = EINVAL;
    886 			goto fail;
    887 		}
    888 		if (len != 0) {
    889 			iov.iov_base = (void *)ur->ucr_data;
    890 			iov.iov_len = len;
    891 			uio.uio_iov = &iov;
    892 			uio.uio_iovcnt = 1;
    893 			uio.uio_resid = len;
    894 			uio.uio_offset = 0;
    895 			uio.uio_rw =
    896 				ur->ucr_request.bmRequestType & UT_READ ?
    897 				UIO_READ : UIO_WRITE;
    898 			uio.uio_vmspace = l->l_proc->p_vmspace;
    899 			ptr = kmem_alloc(len, KM_SLEEP);
    900 			if (uio.uio_rw == UIO_WRITE) {
    901 				error = uiomove(ptr, len, &uio);
    902 				if (error)
    903 					goto ret;
    904 			}
    905 		}
    906 		err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
    907 			  &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
    908 			  USBD_DEFAULT_TIMEOUT);
    909 		if (err) {
    910 			error = EIO;
    911 			goto ret;
    912 		}
    913 		if (len > ur->ucr_actlen)
    914 			len = ur->ucr_actlen;
    915 		if (len != 0) {
    916 			if (uio.uio_rw == UIO_READ) {
    917 				error = uiomove(ptr, len, &uio);
    918 				if (error)
    919 					goto ret;
    920 			}
    921 		}
    922 	ret:
    923 		if (ptr) {
    924 			len = UGETW(ur->ucr_request.wLength);
    925 			kmem_free(ptr, len);
    926 		}
    927 		break;
    928 	}
    929 
    930 	case USB_DEVICEINFO:
    931 	{
    932 		struct usbd_device *dev;
    933 		struct usb_device_info *di = (void *)data;
    934 		int addr = di->udi_addr;
    935 
    936 		if (addr < 0 || addr >= USB_MAX_DEVICES) {
    937 			error = EINVAL;
    938 			goto fail;
    939 		}
    940 		size_t dindex = usb_addr2dindex(addr);
    941 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    942 			error = ENXIO;
    943 			goto fail;
    944 		}
    945 		usbd_fill_deviceinfo(dev, di, 1);
    946 		break;
    947 	}
    948 
    949 	case USB_DEVICEINFO_OLD:
    950 	{
    951 		struct usbd_device *dev;
    952 		struct usb_device_info_old *di = (void *)data;
    953 		int addr = di->udi_addr;
    954 
    955 		if (addr < 1 || addr >= USB_MAX_DEVICES) {
    956 			error = EINVAL;
    957 			goto fail;
    958 		}
    959 		size_t dindex = usb_addr2dindex(addr);
    960 		if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
    961 			error = ENXIO;
    962 			goto fail;
    963 		}
    964 		MODULE_HOOK_CALL(usb_subr_fill_30_hook,
    965 		    (dev, di, 1, usbd_devinfo_vp, usbd_printBCD),
    966 		    enosys(), error);
    967 		if (error == ENOSYS)
    968 			error = EINVAL;
    969 		if (error)
    970 			goto fail;
    971 		break;
    972 	}
    973 
    974 	case USB_DEVICESTATS:
    975 		*(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
    976 		break;
    977 
    978 	default:
    979 		error = EINVAL;
    980 	}
    981 
    982 fail:
    983 
    984 	DPRINTF("... done (error = %jd)", error, 0, 0, 0);
    985 
    986 	return error;
    987 }
    988 
    989 int
    990 usbpoll(dev_t dev, int events, struct lwp *l)
    991 {
    992 	int revents, mask;
    993 
    994 	if (minor(dev) == USB_DEV_MINOR) {
    995 		revents = 0;
    996 		mask = POLLIN | POLLRDNORM;
    997 
    998 		mutex_enter(&usb_event_lock);
    999 		if (events & mask && usb_nevents > 0)
   1000 			revents |= events & mask;
   1001 		if (revents == 0 && events & mask)
   1002 			selrecord(l, &usb_selevent);
   1003 		mutex_exit(&usb_event_lock);
   1004 
   1005 		return revents;
   1006 	} else {
   1007 		return 0;
   1008 	}
   1009 }
   1010 
   1011 static void
   1012 filt_usbrdetach(struct knote *kn)
   1013 {
   1014 
   1015 	mutex_enter(&usb_event_lock);
   1016 	SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext);
   1017 	mutex_exit(&usb_event_lock);
   1018 }
   1019 
   1020 static int
   1021 filt_usbread(struct knote *kn, long hint)
   1022 {
   1023 
   1024 	if (usb_nevents == 0)
   1025 		return 0;
   1026 
   1027 	kn->kn_data = sizeof(struct usb_event);
   1028 	return 1;
   1029 }
   1030 
   1031 static const struct filterops usbread_filtops = {
   1032 	.f_isfd = 1,
   1033 	.f_attach = NULL,
   1034 	.f_detach = filt_usbrdetach,
   1035 	.f_event = filt_usbread,
   1036 };
   1037 
   1038 int
   1039 usbkqfilter(dev_t dev, struct knote *kn)
   1040 {
   1041 	struct klist *klist;
   1042 
   1043 	switch (kn->kn_filter) {
   1044 	case EVFILT_READ:
   1045 		if (minor(dev) != USB_DEV_MINOR)
   1046 			return 1;
   1047 		klist = &usb_selevent.sel_klist;
   1048 		kn->kn_fop = &usbread_filtops;
   1049 		break;
   1050 
   1051 	default:
   1052 		return EINVAL;
   1053 	}
   1054 
   1055 	kn->kn_hook = NULL;
   1056 
   1057 	mutex_enter(&usb_event_lock);
   1058 	SLIST_INSERT_HEAD(klist, kn, kn_selnext);
   1059 	mutex_exit(&usb_event_lock);
   1060 
   1061 	return 0;
   1062 }
   1063 
   1064 /* Explore device tree from the root. */
   1065 Static void
   1066 usb_discover(struct usb_softc *sc)
   1067 {
   1068 	struct usbd_bus *bus = sc->sc_bus;
   1069 
   1070 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1071 
   1072 	KASSERT(mutex_owned(bus->ub_lock));
   1073 
   1074 	if (usb_noexplore > 1)
   1075 		return;
   1076 
   1077 	/*
   1078 	 * We need mutual exclusion while traversing the device tree,
   1079 	 * but this is guaranteed since this function is only called
   1080 	 * from the event thread for the controller.
   1081 	 *
   1082 	 * Also, we now have bus->ub_lock held, and in combination
   1083 	 * with ub_exploring, avoids interferring with polling.
   1084 	 */
   1085 	SDT_PROBE1(usb, kernel, bus, discover__start,  bus);
   1086 	while (bus->ub_needsexplore && !sc->sc_dying) {
   1087 		bus->ub_needsexplore = 0;
   1088 		mutex_exit(sc->sc_bus->ub_lock);
   1089 		SDT_PROBE1(usb, kernel, bus, explore__start,  bus);
   1090 		bus->ub_roothub->ud_hub->uh_explore(bus->ub_roothub);
   1091 		SDT_PROBE1(usb, kernel, bus, explore__done,  bus);
   1092 		mutex_enter(bus->ub_lock);
   1093 	}
   1094 	SDT_PROBE1(usb, kernel, bus, discover__done,  bus);
   1095 }
   1096 
   1097 void
   1098 usb_needs_explore(struct usbd_device *dev)
   1099 {
   1100 
   1101 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1102 	SDT_PROBE1(usb, kernel, bus, needs__explore,  dev->ud_bus);
   1103 
   1104 	mutex_enter(dev->ud_bus->ub_lock);
   1105 	dev->ud_bus->ub_needsexplore = 1;
   1106 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1107 	mutex_exit(dev->ud_bus->ub_lock);
   1108 }
   1109 
   1110 void
   1111 usb_needs_reattach(struct usbd_device *dev)
   1112 {
   1113 
   1114 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1115 	SDT_PROBE1(usb, kernel, bus, needs__reattach,  dev->ud_bus);
   1116 
   1117 	mutex_enter(dev->ud_bus->ub_lock);
   1118 	dev->ud_powersrc->up_reattach = 1;
   1119 	dev->ud_bus->ub_needsexplore = 1;
   1120 	cv_signal(&dev->ud_bus->ub_needsexplore_cv);
   1121 	mutex_exit(dev->ud_bus->ub_lock);
   1122 }
   1123 
   1124 /* Called at with usb_event_lock held. */
   1125 int
   1126 usb_get_next_event(struct usb_event *ue)
   1127 {
   1128 	struct usb_event_q *ueq;
   1129 
   1130 	KASSERT(mutex_owned(&usb_event_lock));
   1131 
   1132 	if (usb_nevents <= 0)
   1133 		return 0;
   1134 	ueq = SIMPLEQ_FIRST(&usb_events);
   1135 #ifdef DIAGNOSTIC
   1136 	if (ueq == NULL) {
   1137 		printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
   1138 		usb_nevents = 0;
   1139 		return 0;
   1140 	}
   1141 #endif
   1142 	if (ue)
   1143 		*ue = ueq->ue;
   1144 	SIMPLEQ_REMOVE_HEAD(&usb_events, next);
   1145 	usb_free_event((struct usb_event *)(void *)ueq);
   1146 	usb_nevents--;
   1147 	return 1;
   1148 }
   1149 
   1150 void
   1151 usbd_add_dev_event(int type, struct usbd_device *udev)
   1152 {
   1153 	struct usb_event *ue = usb_alloc_event();
   1154 
   1155 	usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
   1156 	usb_add_event(type, ue);
   1157 }
   1158 
   1159 void
   1160 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
   1161 {
   1162 	struct usb_event *ue = usb_alloc_event();
   1163 
   1164 	ue->u.ue_driver.ue_cookie = udev->ud_cookie;
   1165 	strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
   1166 	    sizeof(ue->u.ue_driver.ue_devname));
   1167 	usb_add_event(type, ue);
   1168 }
   1169 
   1170 Static struct usb_event *
   1171 usb_alloc_event(void)
   1172 {
   1173 	/* Yes, this is right; we allocate enough so that we can use it later */
   1174 	return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
   1175 }
   1176 
   1177 Static void
   1178 usb_free_event(struct usb_event *uep)
   1179 {
   1180 	kmem_free(uep, sizeof(struct usb_event_q));
   1181 }
   1182 
   1183 Static void
   1184 usb_add_event(int type, struct usb_event *uep)
   1185 {
   1186 	struct usb_event_q *ueq;
   1187 	struct timeval thetime;
   1188 
   1189 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1190 
   1191 	microtime(&thetime);
   1192 	/* Don't want to wait here with usb_event_lock held */
   1193 	ueq = (struct usb_event_q *)(void *)uep;
   1194 	ueq->ue = *uep;
   1195 	ueq->ue.ue_type = type;
   1196 	TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
   1197 	SDT_PROBE1(usb, kernel, event, add,  uep);
   1198 
   1199 	mutex_enter(&usb_event_lock);
   1200 	if (++usb_nevents >= USB_MAX_EVENTS) {
   1201 		/* Too many queued events, drop an old one. */
   1202 		DPRINTF("event dropped", 0, 0, 0, 0);
   1203 #ifdef KDTRACE_HOOKS
   1204 		struct usb_event oue;
   1205 		if (usb_get_next_event(&oue))
   1206 			SDT_PROBE1(usb, kernel, event, drop,  &oue);
   1207 #else
   1208 		usb_get_next_event(NULL);
   1209 #endif
   1210 	}
   1211 	SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
   1212 	cv_signal(&usb_event_cv);
   1213 	selnotify(&usb_selevent, 0, 0);
   1214 	if (usb_async_proc != NULL) {
   1215 		kpreempt_disable();
   1216 		softint_schedule(usb_async_sih);
   1217 		kpreempt_enable();
   1218 	}
   1219 	mutex_exit(&usb_event_lock);
   1220 }
   1221 
   1222 Static void
   1223 usb_async_intr(void *cookie)
   1224 {
   1225 	proc_t *proc;
   1226 
   1227 	mutex_enter(&proc_lock);
   1228 	if ((proc = usb_async_proc) != NULL)
   1229 		psignal(proc, SIGIO);
   1230 	mutex_exit(&proc_lock);
   1231 }
   1232 
   1233 Static void
   1234 usb_soft_intr(void *arg)
   1235 {
   1236 	struct usbd_bus *bus = arg;
   1237 
   1238 	mutex_enter(bus->ub_lock);
   1239 	bus->ub_methods->ubm_softint(bus);
   1240 	mutex_exit(bus->ub_lock);
   1241 }
   1242 
   1243 void
   1244 usb_schedsoftintr(struct usbd_bus *bus)
   1245 {
   1246 
   1247 	USBHIST_FUNC();
   1248 	USBHIST_CALLARGS(usbdebug, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
   1249 
   1250 	/* In case the bus never finished setting up. */
   1251 	if (__predict_false(bus->ub_soft == NULL))
   1252 		return;
   1253 
   1254 	if (bus->ub_usepolling) {
   1255 		bus->ub_methods->ubm_softint(bus);
   1256 	} else {
   1257 		kpreempt_disable();
   1258 		softint_schedule(bus->ub_soft);
   1259 		kpreempt_enable();
   1260 	}
   1261 }
   1262 
   1263 int
   1264 usb_activate(device_t self, enum devact act)
   1265 {
   1266 	struct usb_softc *sc = device_private(self);
   1267 
   1268 	switch (act) {
   1269 	case DVACT_DEACTIVATE:
   1270 		sc->sc_dying = 1;
   1271 		return 0;
   1272 	default:
   1273 		return EOPNOTSUPP;
   1274 	}
   1275 }
   1276 
   1277 void
   1278 usb_childdet(device_t self, device_t child)
   1279 {
   1280 	int i;
   1281 	struct usb_softc *sc = device_private(self);
   1282 	struct usbd_device *dev;
   1283 
   1284 	if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
   1285 		return;
   1286 
   1287 	for (i = 0; i < dev->ud_subdevlen; i++)
   1288 		if (dev->ud_subdevs[i] == child)
   1289 			dev->ud_subdevs[i] = NULL;
   1290 }
   1291 
   1292 int
   1293 usb_detach(device_t self, int flags)
   1294 {
   1295 	struct usb_softc *sc = device_private(self);
   1296 	struct usb_event *ue;
   1297 	int rc;
   1298 
   1299 	USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
   1300 
   1301 	/* Make all devices disconnect. */
   1302 	if (sc->sc_port.up_dev != NULL &&
   1303 	    (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
   1304 		return rc;
   1305 
   1306 	if (sc->sc_pmf_registered)
   1307 		pmf_device_deregister(self);
   1308 	/* Kill off event thread. */
   1309 	sc->sc_dying = 1;
   1310 	while (sc->sc_event_thread != NULL) {
   1311 		mutex_enter(sc->sc_bus->ub_lock);
   1312 		cv_signal(&sc->sc_bus->ub_needsexplore_cv);
   1313 		cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
   1314 		    sc->sc_bus->ub_lock, hz * 60);
   1315 		mutex_exit(sc->sc_bus->ub_lock);
   1316 	}
   1317 	DPRINTF("event thread dead", 0, 0, 0, 0);
   1318 
   1319 	if (sc->sc_bus->ub_soft != NULL) {
   1320 		softint_disestablish(sc->sc_bus->ub_soft);
   1321 		sc->sc_bus->ub_soft = NULL;
   1322 	}
   1323 
   1324 	ue = usb_alloc_event();
   1325 	ue->u.ue_ctrlr.ue_bus = device_unit(self);
   1326 	usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
   1327 
   1328 	cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
   1329 
   1330 	return 0;
   1331 }
   1332