usb.c revision 1.195 1 /* $NetBSD: usb.c,v 1.195 2021/06/12 12:13:10 riastradh Exp $ */
2
3 /*
4 * Copyright (c) 1998, 2002, 2008, 2012 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Lennart Augustsson (lennart (at) augustsson.net) at
9 * Carlstedt Research & Technology and Matthew R. Green (mrg (at) eterna.com.au).
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30 * POSSIBILITY OF SUCH DAMAGE.
31 */
32
33 /*
34 * USB specifications and other documentation can be found at
35 * http://www.usb.org/developers/docs/ and
36 * http://www.usb.org/developers/devclass_docs/
37 */
38
39 #include <sys/cdefs.h>
40 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.195 2021/06/12 12:13:10 riastradh 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 KASSERT(KERNEL_LOCKED_P());
457
458 /* Protected by KERNEL_LOCK */
459 nusbbusses++;
460
461 sc->sc_bus->ub_usbctl = self;
462 sc->sc_port.up_power = USB_MAX_POWER;
463
464 switch (sc->sc_bus->ub_revision) {
465 case USBREV_1_0:
466 case USBREV_1_1:
467 speed = USB_SPEED_FULL;
468 break;
469 case USBREV_2_0:
470 speed = USB_SPEED_HIGH;
471 break;
472 case USBREV_3_0:
473 speed = USB_SPEED_SUPER;
474 break;
475 case USBREV_3_1:
476 speed = USB_SPEED_SUPER_PLUS;
477 break;
478 default:
479 panic("usb_doattach");
480 }
481
482 ue = usb_alloc_event();
483 ue->u.ue_ctrlr.ue_bus = device_unit(self);
484 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue);
485
486 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0,
487 &sc->sc_port);
488 if (!err) {
489 dev = sc->sc_port.up_dev;
490 if (dev->ud_hub == NULL) {
491 sc->sc_dying = 1;
492 aprint_error("%s: root device is not a hub\n",
493 device_xname(self));
494 return;
495 }
496 sc->sc_bus->ub_roothub = dev;
497 usb_create_event_thread(self);
498 } else {
499 aprint_error("%s: root hub problem, error=%s\n",
500 device_xname(self), usbd_errstr(err));
501 sc->sc_dying = 1;
502 }
503
504 /*
505 * Drop this reference after the first set of attachments in the
506 * event thread.
507 */
508 config_pending_incr(self);
509
510 if (!pmf_device_register(self, NULL, NULL))
511 aprint_error_dev(self, "couldn't establish power handler\n");
512 else
513 sc->sc_pmf_registered = true;
514
515 return;
516 }
517
518 void
519 usb_create_event_thread(device_t self)
520 {
521 struct usb_softc *sc = device_private(self);
522
523 if (kthread_create(PRI_NONE, 0, NULL,
524 usb_event_thread, sc, &sc->sc_event_thread,
525 "%s", device_xname(self))) {
526 printf("%s: unable to create event thread for\n",
527 device_xname(self));
528 panic("usb_create_event_thread");
529 }
530 }
531
532 /*
533 * Add a task to be performed by the task thread. This function can be
534 * called from any context and the task will be executed in a process
535 * context ASAP.
536 */
537 void
538 usb_add_task(struct usbd_device *dev, struct usb_task *task, int queue)
539 {
540 struct usb_taskq *taskq;
541
542 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
543 SDT_PROBE3(usb, kernel, task, add, dev, task, queue);
544
545 KASSERT(0 <= queue);
546 KASSERT(queue < USB_NUM_TASKQS);
547 taskq = &usb_taskq[queue];
548 mutex_enter(&taskq->lock);
549 if (atomic_cas_uint(&task->queue, USB_NUM_TASKQS, queue) ==
550 USB_NUM_TASKQS) {
551 DPRINTFN(2, "task=%#jx", (uintptr_t)task, 0, 0, 0);
552 TAILQ_INSERT_TAIL(&taskq->tasks, task, next);
553 cv_signal(&taskq->cv);
554 } else {
555 DPRINTFN(2, "task=%#jx on q", (uintptr_t)task, 0, 0, 0);
556 }
557 mutex_exit(&taskq->lock);
558 }
559
560 /*
561 * usb_rem_task(dev, task)
562 *
563 * If task is queued to run, remove it from the queue. Return
564 * true if it successfully removed the task from the queue, false
565 * if not.
566 *
567 * Caller is _not_ guaranteed that the task is not running when
568 * this is done.
569 *
570 * Never sleeps.
571 */
572 bool
573 usb_rem_task(struct usbd_device *dev, struct usb_task *task)
574 {
575 unsigned queue;
576
577 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
578 SDT_PROBE2(usb, kernel, task, rem__start, dev, task);
579
580 while ((queue = task->queue) != USB_NUM_TASKQS) {
581 struct usb_taskq *taskq = &usb_taskq[queue];
582 mutex_enter(&taskq->lock);
583 if (__predict_true(task->queue == queue)) {
584 TAILQ_REMOVE(&taskq->tasks, task, next);
585 task->queue = USB_NUM_TASKQS;
586 mutex_exit(&taskq->lock);
587 SDT_PROBE3(usb, kernel, task, rem__done,
588 dev, task, true);
589 return true; /* removed from the queue */
590 }
591 mutex_exit(&taskq->lock);
592 }
593
594 SDT_PROBE3(usb, kernel, task, rem__done, dev, task, false);
595 return false; /* was not removed from the queue */
596 }
597
598 /*
599 * usb_rem_task_wait(dev, task, queue, interlock)
600 *
601 * If task is scheduled to run, remove it from the queue. If it
602 * may have already begun to run, drop interlock if not null, wait
603 * for it to complete, and reacquire interlock if not null.
604 * Return true if it successfully removed the task from the queue,
605 * false if not.
606 *
607 * Caller MUST guarantee that task will not be scheduled on a
608 * _different_ queue, at least until after this returns.
609 *
610 * If caller guarantees that task will not be scheduled on the
611 * same queue before this returns, then caller is guaranteed that
612 * the task is not running at all when this returns.
613 *
614 * May sleep.
615 */
616 bool
617 usb_rem_task_wait(struct usbd_device *dev, struct usb_task *task, int queue,
618 kmutex_t *interlock)
619 {
620 struct usb_taskq *taskq;
621 int queue1;
622 bool removed;
623
624 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
625 SDT_PROBE4(usb, kernel, task, rem__wait__start,
626 dev, task, queue, interlock);
627 ASSERT_SLEEPABLE();
628 KASSERT(0 <= queue);
629 KASSERT(queue < USB_NUM_TASKQS);
630
631 taskq = &usb_taskq[queue];
632 mutex_enter(&taskq->lock);
633 queue1 = task->queue;
634 if (queue1 == USB_NUM_TASKQS) {
635 /*
636 * It is not on the queue. It may be about to run, or
637 * it may have already finished running -- there is no
638 * stopping it now. Wait for it if it is running.
639 */
640 if (interlock)
641 mutex_exit(interlock);
642 while (taskq->current_task == task)
643 cv_wait(&taskq->cv, &taskq->lock);
644 removed = false;
645 } else {
646 /*
647 * It is still on the queue. We can stop it before the
648 * task thread will run it.
649 */
650 KASSERTMSG(queue1 == queue, "task %p on q%d expected on q%d",
651 task, queue1, queue);
652 TAILQ_REMOVE(&taskq->tasks, task, next);
653 task->queue = USB_NUM_TASKQS;
654 removed = true;
655 }
656 mutex_exit(&taskq->lock);
657
658 /*
659 * If there's an interlock, and we dropped it to wait,
660 * reacquire it.
661 */
662 if (interlock && !removed)
663 mutex_enter(interlock);
664
665 SDT_PROBE5(usb, kernel, task, rem__wait__done,
666 dev, task, queue, interlock, removed);
667 return removed;
668 }
669
670 /*
671 * usb_task_pending(dev, task)
672 *
673 * True if task is queued, false if not. Note that if task is
674 * already running, it is not considered queued.
675 *
676 * For _negative_ diagnostic assertions only:
677 *
678 * KASSERT(!usb_task_pending(dev, task));
679 */
680 bool
681 usb_task_pending(struct usbd_device *dev, struct usb_task *task)
682 {
683
684 return task->queue != USB_NUM_TASKQS;
685 }
686
687 void
688 usb_event_thread(void *arg)
689 {
690 struct usb_softc *sc = arg;
691 struct usbd_bus *bus = sc->sc_bus;
692
693 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
694
695 KASSERT(KERNEL_LOCKED_P());
696
697 /*
698 * In case this controller is a companion controller to an
699 * EHCI controller we need to wait until the EHCI controller
700 * has grabbed the port.
701 * XXX It would be nicer to do this with a tsleep(), but I don't
702 * know how to synchronize the creation of the threads so it
703 * will work.
704 */
705 usb_delay_ms(bus, 500);
706
707 /* Make sure first discover does something. */
708 mutex_enter(bus->ub_lock);
709 sc->sc_bus->ub_needsexplore = 1;
710 usb_discover(sc);
711 mutex_exit(bus->ub_lock);
712
713 /* Drop the config_pending reference from attach. */
714 config_pending_decr(bus->ub_usbctl);
715
716 mutex_enter(bus->ub_lock);
717 while (!sc->sc_dying) {
718 #if 0 /* not yet */
719 while (sc->sc_bus->ub_usepolling)
720 kpause("usbpoll", true, hz, bus->ub_lock);
721 #endif
722
723 if (usb_noexplore < 2)
724 usb_discover(sc);
725
726 cv_timedwait(&bus->ub_needsexplore_cv,
727 bus->ub_lock, usb_noexplore ? 0 : hz * 60);
728
729 DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0);
730 }
731 sc->sc_event_thread = NULL;
732
733 /* In case parent is waiting for us to exit. */
734 cv_signal(&bus->ub_needsexplore_cv);
735 mutex_exit(bus->ub_lock);
736
737 DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0);
738 kthread_exit(0);
739 }
740
741 void
742 usb_task_thread(void *arg)
743 {
744 struct usb_task *task;
745 struct usb_taskq *taskq;
746 bool mpsafe;
747
748 taskq = arg;
749
750 USBHIST_FUNC();
751 USBHIST_CALLARGS(usbdebug, "start taskq %#jx",
752 (uintptr_t)taskq, 0, 0, 0);
753
754 mutex_enter(&taskq->lock);
755 for (;;) {
756 task = TAILQ_FIRST(&taskq->tasks);
757 if (task == NULL) {
758 cv_wait(&taskq->cv, &taskq->lock);
759 task = TAILQ_FIRST(&taskq->tasks);
760 }
761 DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0);
762 if (task != NULL) {
763 mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE);
764 TAILQ_REMOVE(&taskq->tasks, task, next);
765 task->queue = USB_NUM_TASKQS;
766 taskq->current_task = task;
767 mutex_exit(&taskq->lock);
768
769 if (!mpsafe)
770 KERNEL_LOCK(1, curlwp);
771 SDT_PROBE1(usb, kernel, task, start, task);
772 task->fun(task->arg);
773 /* Can't dereference task after this point. */
774 SDT_PROBE1(usb, kernel, task, done, task);
775 if (!mpsafe)
776 KERNEL_UNLOCK_ONE(curlwp);
777
778 mutex_enter(&taskq->lock);
779 KASSERTMSG(taskq->current_task == task,
780 "somebody scribbled on usb taskq %p", taskq);
781 taskq->current_task = NULL;
782 cv_broadcast(&taskq->cv);
783 }
784 }
785 mutex_exit(&taskq->lock);
786 }
787
788 int
789 usbctlprint(void *aux, const char *pnp)
790 {
791 /* only "usb"es can attach to host controllers */
792 if (pnp)
793 aprint_normal("usb at %s", pnp);
794
795 return UNCONF;
796 }
797
798 int
799 usbopen(dev_t dev, int flag, int mode, struct lwp *l)
800 {
801 int unit = minor(dev);
802 struct usb_softc *sc;
803
804 if (nusbbusses == 0)
805 return ENXIO;
806
807 if (unit == USB_DEV_MINOR) {
808 if (usb_dev_open)
809 return EBUSY;
810 usb_dev_open = 1;
811 mutex_enter(&proc_lock);
812 usb_async_proc = NULL;
813 mutex_exit(&proc_lock);
814 return 0;
815 }
816
817 sc = device_lookup_private(&usb_cd, unit);
818 if (!sc)
819 return ENXIO;
820
821 if (sc->sc_dying)
822 return EIO;
823
824 return 0;
825 }
826
827 int
828 usbread(dev_t dev, struct uio *uio, int flag)
829 {
830 struct usb_event *ue;
831 struct usb_event_old *ueo = NULL; /* XXXGCC */
832 int useold = 0;
833 int error, n;
834
835 if (minor(dev) != USB_DEV_MINOR)
836 return ENXIO;
837
838 switch (uio->uio_resid) {
839 case sizeof(struct usb_event_old):
840 ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP);
841 useold = 1;
842 /* FALLTHROUGH */
843 case sizeof(struct usb_event):
844 ue = usb_alloc_event();
845 break;
846 default:
847 return EINVAL;
848 }
849
850 error = 0;
851 mutex_enter(&usb_event_lock);
852 for (;;) {
853 n = usb_get_next_event(ue);
854 if (n != 0)
855 break;
856 if (flag & IO_NDELAY) {
857 error = EWOULDBLOCK;
858 break;
859 }
860 error = cv_wait_sig(&usb_event_cv, &usb_event_lock);
861 if (error)
862 break;
863 }
864 mutex_exit(&usb_event_lock);
865 if (!error) {
866 if (useold) { /* copy fields to old struct */
867 MODULE_HOOK_CALL(usb_subr_copy_30_hook,
868 (ue, ueo, uio), enosys(), error);
869 if (error == ENOSYS)
870 error = EINVAL;
871
872 if (!error)
873 error = uiomove((void *)ueo, sizeof(*ueo), uio);
874 } else
875 error = uiomove((void *)ue, sizeof(*ue), uio);
876 }
877 usb_free_event(ue);
878 if (ueo)
879 kmem_free(ueo, sizeof(struct usb_event_old));
880
881 return error;
882 }
883
884 int
885 usbclose(dev_t dev, int flag, int mode,
886 struct lwp *l)
887 {
888 int unit = minor(dev);
889
890 if (unit == USB_DEV_MINOR) {
891 mutex_enter(&proc_lock);
892 usb_async_proc = NULL;
893 mutex_exit(&proc_lock);
894 usb_dev_open = 0;
895 }
896
897 return 0;
898 }
899
900 int
901 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l)
902 {
903 struct usb_softc *sc;
904 int unit = minor(devt);
905
906 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "cmd %#jx", cmd, 0, 0, 0);
907
908 if (unit == USB_DEV_MINOR) {
909 switch (cmd) {
910 case FIONBIO:
911 /* All handled in the upper FS layer. */
912 return 0;
913
914 case FIOASYNC:
915 mutex_enter(&proc_lock);
916 if (*(int *)data)
917 usb_async_proc = l->l_proc;
918 else
919 usb_async_proc = NULL;
920 mutex_exit(&proc_lock);
921 return 0;
922
923 default:
924 return EINVAL;
925 }
926 }
927
928 sc = device_lookup_private(&usb_cd, unit);
929
930 if (sc->sc_dying)
931 return EIO;
932
933 int error = 0;
934 switch (cmd) {
935 #ifdef USB_DEBUG
936 case USB_SETDEBUG:
937 if (!(flag & FWRITE))
938 return EBADF;
939 usbdebug = ((*(int *)data) & 0x000000ff);
940 break;
941 #endif /* USB_DEBUG */
942 case USB_REQUEST:
943 {
944 struct usb_ctl_request *ur = (void *)data;
945 int len = UGETW(ur->ucr_request.wLength);
946 struct iovec iov;
947 struct uio uio;
948 void *ptr = 0;
949 int addr = ur->ucr_addr;
950 usbd_status err;
951
952 if (!(flag & FWRITE)) {
953 error = EBADF;
954 goto fail;
955 }
956
957 DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0);
958 if (len < 0 || len > 32768) {
959 error = EINVAL;
960 goto fail;
961 }
962 if (addr < 0 || addr >= USB_MAX_DEVICES) {
963 error = EINVAL;
964 goto fail;
965 }
966 size_t dindex = usb_addr2dindex(addr);
967 if (sc->sc_bus->ub_devices[dindex] == NULL) {
968 error = EINVAL;
969 goto fail;
970 }
971 if (len != 0) {
972 iov.iov_base = (void *)ur->ucr_data;
973 iov.iov_len = len;
974 uio.uio_iov = &iov;
975 uio.uio_iovcnt = 1;
976 uio.uio_resid = len;
977 uio.uio_offset = 0;
978 uio.uio_rw =
979 ur->ucr_request.bmRequestType & UT_READ ?
980 UIO_READ : UIO_WRITE;
981 uio.uio_vmspace = l->l_proc->p_vmspace;
982 ptr = kmem_alloc(len, KM_SLEEP);
983 if (uio.uio_rw == UIO_WRITE) {
984 error = uiomove(ptr, len, &uio);
985 if (error)
986 goto ret;
987 }
988 }
989 err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex],
990 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen,
991 USBD_DEFAULT_TIMEOUT);
992 if (err) {
993 error = EIO;
994 goto ret;
995 }
996 if (len > ur->ucr_actlen)
997 len = ur->ucr_actlen;
998 if (len != 0) {
999 if (uio.uio_rw == UIO_READ) {
1000 error = uiomove(ptr, len, &uio);
1001 if (error)
1002 goto ret;
1003 }
1004 }
1005 ret:
1006 if (ptr) {
1007 len = UGETW(ur->ucr_request.wLength);
1008 kmem_free(ptr, len);
1009 }
1010 break;
1011 }
1012
1013 case USB_DEVICEINFO:
1014 {
1015 struct usbd_device *dev;
1016 struct usb_device_info *di = (void *)data;
1017 int addr = di->udi_addr;
1018
1019 if (addr < 0 || addr >= USB_MAX_DEVICES) {
1020 error = EINVAL;
1021 goto fail;
1022 }
1023 size_t dindex = usb_addr2dindex(addr);
1024 if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
1025 error = ENXIO;
1026 goto fail;
1027 }
1028 usbd_fill_deviceinfo(dev, di, 1);
1029 break;
1030 }
1031
1032 case USB_DEVICEINFO_OLD:
1033 {
1034 struct usbd_device *dev;
1035 struct usb_device_info_old *di = (void *)data;
1036 int addr = di->udi_addr;
1037
1038 if (addr < 1 || addr >= USB_MAX_DEVICES) {
1039 error = EINVAL;
1040 goto fail;
1041 }
1042 size_t dindex = usb_addr2dindex(addr);
1043 if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) {
1044 error = ENXIO;
1045 goto fail;
1046 }
1047 MODULE_HOOK_CALL(usb_subr_fill_30_hook,
1048 (dev, di, 1, usbd_devinfo_vp, usbd_printBCD),
1049 enosys(), error);
1050 if (error == ENOSYS)
1051 error = EINVAL;
1052 if (error)
1053 goto fail;
1054 break;
1055 }
1056
1057 case USB_DEVICESTATS:
1058 *(struct usb_device_stats *)data = sc->sc_bus->ub_stats;
1059 break;
1060
1061 default:
1062 error = EINVAL;
1063 }
1064
1065 fail:
1066
1067 DPRINTF("... done (error = %jd)", error, 0, 0, 0);
1068
1069 return error;
1070 }
1071
1072 int
1073 usbpoll(dev_t dev, int events, struct lwp *l)
1074 {
1075 int revents, mask;
1076
1077 if (minor(dev) == USB_DEV_MINOR) {
1078 revents = 0;
1079 mask = POLLIN | POLLRDNORM;
1080
1081 mutex_enter(&usb_event_lock);
1082 if (events & mask && usb_nevents > 0)
1083 revents |= events & mask;
1084 if (revents == 0 && events & mask)
1085 selrecord(l, &usb_selevent);
1086 mutex_exit(&usb_event_lock);
1087
1088 return revents;
1089 } else {
1090 return 0;
1091 }
1092 }
1093
1094 static void
1095 filt_usbrdetach(struct knote *kn)
1096 {
1097
1098 mutex_enter(&usb_event_lock);
1099 selremove_knote(&usb_selevent, kn);
1100 mutex_exit(&usb_event_lock);
1101 }
1102
1103 static int
1104 filt_usbread(struct knote *kn, long hint)
1105 {
1106
1107 if (usb_nevents == 0)
1108 return 0;
1109
1110 kn->kn_data = sizeof(struct usb_event);
1111 return 1;
1112 }
1113
1114 static const struct filterops usbread_filtops = {
1115 .f_isfd = 1,
1116 .f_attach = NULL,
1117 .f_detach = filt_usbrdetach,
1118 .f_event = filt_usbread,
1119 };
1120
1121 int
1122 usbkqfilter(dev_t dev, struct knote *kn)
1123 {
1124
1125 switch (kn->kn_filter) {
1126 case EVFILT_READ:
1127 if (minor(dev) != USB_DEV_MINOR)
1128 return 1;
1129 kn->kn_fop = &usbread_filtops;
1130 break;
1131
1132 default:
1133 return EINVAL;
1134 }
1135
1136 kn->kn_hook = NULL;
1137
1138 mutex_enter(&usb_event_lock);
1139 selrecord_knote(&usb_selevent, kn);
1140 mutex_exit(&usb_event_lock);
1141
1142 return 0;
1143 }
1144
1145 /* Explore device tree from the root. */
1146 Static void
1147 usb_discover(struct usb_softc *sc)
1148 {
1149 struct usbd_bus *bus = sc->sc_bus;
1150
1151 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1152
1153 KASSERT(KERNEL_LOCKED_P());
1154 KASSERT(mutex_owned(bus->ub_lock));
1155
1156 if (usb_noexplore > 1)
1157 return;
1158
1159 /*
1160 * We need mutual exclusion while traversing the device tree,
1161 * but this is guaranteed since this function is only called
1162 * from the event thread for the controller.
1163 *
1164 * Also, we now have bus->ub_lock held, and in combination
1165 * with ub_exploring, avoids interferring with polling.
1166 */
1167 SDT_PROBE1(usb, kernel, bus, discover__start, bus);
1168 while (bus->ub_needsexplore && !sc->sc_dying) {
1169 bus->ub_needsexplore = 0;
1170 mutex_exit(sc->sc_bus->ub_lock);
1171 SDT_PROBE1(usb, kernel, bus, explore__start, bus);
1172 bus->ub_roothub->ud_hub->uh_explore(bus->ub_roothub);
1173 SDT_PROBE1(usb, kernel, bus, explore__done, bus);
1174 mutex_enter(bus->ub_lock);
1175 }
1176 SDT_PROBE1(usb, kernel, bus, discover__done, bus);
1177 }
1178
1179 void
1180 usb_needs_explore(struct usbd_device *dev)
1181 {
1182
1183 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1184 SDT_PROBE1(usb, kernel, bus, needs__explore, dev->ud_bus);
1185
1186 mutex_enter(dev->ud_bus->ub_lock);
1187 dev->ud_bus->ub_needsexplore = 1;
1188 cv_signal(&dev->ud_bus->ub_needsexplore_cv);
1189 mutex_exit(dev->ud_bus->ub_lock);
1190 }
1191
1192 void
1193 usb_needs_reattach(struct usbd_device *dev)
1194 {
1195
1196 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1197 SDT_PROBE1(usb, kernel, bus, needs__reattach, dev->ud_bus);
1198
1199 mutex_enter(dev->ud_bus->ub_lock);
1200 dev->ud_powersrc->up_reattach = 1;
1201 dev->ud_bus->ub_needsexplore = 1;
1202 cv_signal(&dev->ud_bus->ub_needsexplore_cv);
1203 mutex_exit(dev->ud_bus->ub_lock);
1204 }
1205
1206 /* Called at with usb_event_lock held. */
1207 int
1208 usb_get_next_event(struct usb_event *ue)
1209 {
1210 struct usb_event_q *ueq;
1211
1212 KASSERT(mutex_owned(&usb_event_lock));
1213
1214 if (usb_nevents <= 0)
1215 return 0;
1216 ueq = SIMPLEQ_FIRST(&usb_events);
1217 #ifdef DIAGNOSTIC
1218 if (ueq == NULL) {
1219 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents);
1220 usb_nevents = 0;
1221 return 0;
1222 }
1223 #endif
1224 if (ue)
1225 *ue = ueq->ue;
1226 SIMPLEQ_REMOVE_HEAD(&usb_events, next);
1227 usb_free_event((struct usb_event *)(void *)ueq);
1228 usb_nevents--;
1229 return 1;
1230 }
1231
1232 void
1233 usbd_add_dev_event(int type, struct usbd_device *udev)
1234 {
1235 struct usb_event *ue = usb_alloc_event();
1236
1237 usbd_fill_deviceinfo(udev, &ue->u.ue_device, false);
1238 usb_add_event(type, ue);
1239 }
1240
1241 void
1242 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev)
1243 {
1244 struct usb_event *ue = usb_alloc_event();
1245
1246 ue->u.ue_driver.ue_cookie = udev->ud_cookie;
1247 strncpy(ue->u.ue_driver.ue_devname, device_xname(dev),
1248 sizeof(ue->u.ue_driver.ue_devname));
1249 usb_add_event(type, ue);
1250 }
1251
1252 Static struct usb_event *
1253 usb_alloc_event(void)
1254 {
1255 /* Yes, this is right; we allocate enough so that we can use it later */
1256 return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP);
1257 }
1258
1259 Static void
1260 usb_free_event(struct usb_event *uep)
1261 {
1262 kmem_free(uep, sizeof(struct usb_event_q));
1263 }
1264
1265 Static void
1266 usb_add_event(int type, struct usb_event *uep)
1267 {
1268 struct usb_event_q *ueq;
1269 struct timeval thetime;
1270
1271 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1272
1273 microtime(&thetime);
1274 /* Don't want to wait here with usb_event_lock held */
1275 ueq = (struct usb_event_q *)(void *)uep;
1276 ueq->ue = *uep;
1277 ueq->ue.ue_type = type;
1278 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time);
1279 SDT_PROBE1(usb, kernel, event, add, uep);
1280
1281 mutex_enter(&usb_event_lock);
1282 if (++usb_nevents >= USB_MAX_EVENTS) {
1283 /* Too many queued events, drop an old one. */
1284 DPRINTF("event dropped", 0, 0, 0, 0);
1285 #ifdef KDTRACE_HOOKS
1286 struct usb_event oue;
1287 if (usb_get_next_event(&oue))
1288 SDT_PROBE1(usb, kernel, event, drop, &oue);
1289 #else
1290 usb_get_next_event(NULL);
1291 #endif
1292 }
1293 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next);
1294 cv_signal(&usb_event_cv);
1295 selnotify(&usb_selevent, 0, 0);
1296 if (usb_async_proc != NULL) {
1297 kpreempt_disable();
1298 softint_schedule(usb_async_sih);
1299 kpreempt_enable();
1300 }
1301 mutex_exit(&usb_event_lock);
1302 }
1303
1304 Static void
1305 usb_async_intr(void *cookie)
1306 {
1307 proc_t *proc;
1308
1309 mutex_enter(&proc_lock);
1310 if ((proc = usb_async_proc) != NULL)
1311 psignal(proc, SIGIO);
1312 mutex_exit(&proc_lock);
1313 }
1314
1315 Static void
1316 usb_soft_intr(void *arg)
1317 {
1318 struct usbd_bus *bus = arg;
1319
1320 mutex_enter(bus->ub_lock);
1321 bus->ub_methods->ubm_softint(bus);
1322 mutex_exit(bus->ub_lock);
1323 }
1324
1325 void
1326 usb_schedsoftintr(struct usbd_bus *bus)
1327 {
1328
1329 USBHIST_FUNC();
1330 USBHIST_CALLARGS(usbdebug, "polling=%jd", bus->ub_usepolling, 0, 0, 0);
1331
1332 /* In case the bus never finished setting up. */
1333 if (__predict_false(bus->ub_soft == NULL))
1334 return;
1335
1336 if (bus->ub_usepolling) {
1337 bus->ub_methods->ubm_softint(bus);
1338 } else {
1339 kpreempt_disable();
1340 softint_schedule(bus->ub_soft);
1341 kpreempt_enable();
1342 }
1343 }
1344
1345 int
1346 usb_activate(device_t self, enum devact act)
1347 {
1348 struct usb_softc *sc = device_private(self);
1349
1350 switch (act) {
1351 case DVACT_DEACTIVATE:
1352 sc->sc_dying = 1;
1353 return 0;
1354 default:
1355 return EOPNOTSUPP;
1356 }
1357 }
1358
1359 void
1360 usb_childdet(device_t self, device_t child)
1361 {
1362 int i;
1363 struct usb_softc *sc = device_private(self);
1364 struct usbd_device *dev;
1365
1366 if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0)
1367 return;
1368
1369 for (i = 0; i < dev->ud_subdevlen; i++)
1370 if (dev->ud_subdevs[i] == child)
1371 dev->ud_subdevs[i] = NULL;
1372 }
1373
1374 int
1375 usb_detach(device_t self, int flags)
1376 {
1377 struct usb_softc *sc = device_private(self);
1378 struct usb_event *ue;
1379 int rc;
1380
1381 USBHIST_FUNC(); USBHIST_CALLED(usbdebug);
1382
1383 /* Make all devices disconnect. */
1384 if (sc->sc_port.up_dev != NULL &&
1385 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0)
1386 return rc;
1387
1388 if (sc->sc_pmf_registered)
1389 pmf_device_deregister(self);
1390 /* Kill off event thread. */
1391 sc->sc_dying = 1;
1392 while (sc->sc_event_thread != NULL) {
1393 mutex_enter(sc->sc_bus->ub_lock);
1394 cv_signal(&sc->sc_bus->ub_needsexplore_cv);
1395 cv_timedwait(&sc->sc_bus->ub_needsexplore_cv,
1396 sc->sc_bus->ub_lock, hz * 60);
1397 mutex_exit(sc->sc_bus->ub_lock);
1398 }
1399 DPRINTF("event thread dead", 0, 0, 0, 0);
1400
1401 if (sc->sc_bus->ub_soft != NULL) {
1402 softint_disestablish(sc->sc_bus->ub_soft);
1403 sc->sc_bus->ub_soft = NULL;
1404 }
1405
1406 ue = usb_alloc_event();
1407 ue->u.ue_ctrlr.ue_bus = device_unit(self);
1408 usb_add_event(USB_EVENT_CTRLR_DETACH, ue);
1409
1410 cv_destroy(&sc->sc_bus->ub_needsexplore_cv);
1411
1412 return 0;
1413 }
1414