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