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