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