1 /* $NetBSD: usb.c,v 1.196 2021/06/13 14:48: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@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.196 2021/06/13 14:48: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 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 usb_delay_ms(bus, 500); 728 729 /* Make sure first discover does something. */ 730 mutex_enter(bus->ub_lock); 731 sc->sc_bus->ub_needsexplore = 1; 732 usb_discover(sc); 733 mutex_exit(bus->ub_lock); 734 735 /* Drop the config_pending reference from attach. */ 736 config_pending_decr(bus->ub_usbctl); 737 738 mutex_enter(bus->ub_lock); 739 while (!sc->sc_dying) { 740 #if 0 /* not yet */ 741 while (sc->sc_bus->ub_usepolling) 742 kpause("usbpoll", true, hz, bus->ub_lock); 743 #endif 744 745 if (usb_noexplore < 2) 746 usb_discover(sc); 747 748 cv_timedwait(&bus->ub_needsexplore_cv, 749 bus->ub_lock, usb_noexplore ? 0 : hz * 60); 750 751 DPRINTFN(2, "sc %#jx woke up", (uintptr_t)sc, 0, 0, 0); 752 } 753 sc->sc_event_thread = NULL; 754 755 /* In case parent is waiting for us to exit. */ 756 cv_signal(&bus->ub_needsexplore_cv); 757 mutex_exit(bus->ub_lock); 758 759 DPRINTF("sc %#jx exit", (uintptr_t)sc, 0, 0, 0); 760 kthread_exit(0); 761 } 762 763 void 764 usb_task_thread(void *arg) 765 { 766 struct usb_task *task; 767 struct usb_taskq *taskq; 768 bool mpsafe; 769 770 taskq = arg; 771 772 USBHIST_FUNC(); 773 USBHIST_CALLARGS(usbdebug, "start taskq %#jx", 774 (uintptr_t)taskq, 0, 0, 0); 775 776 mutex_enter(&taskq->lock); 777 for (;;) { 778 task = TAILQ_FIRST(&taskq->tasks); 779 if (task == NULL) { 780 cv_wait(&taskq->cv, &taskq->lock); 781 task = TAILQ_FIRST(&taskq->tasks); 782 } 783 DPRINTFN(2, "woke up task=%#jx", (uintptr_t)task, 0, 0, 0); 784 if (task != NULL) { 785 mpsafe = ISSET(task->flags, USB_TASKQ_MPSAFE); 786 TAILQ_REMOVE(&taskq->tasks, task, next); 787 task->queue = USB_NUM_TASKQS; 788 taskq->current_task = task; 789 mutex_exit(&taskq->lock); 790 791 if (!mpsafe) 792 KERNEL_LOCK(1, curlwp); 793 SDT_PROBE1(usb, kernel, task, start, task); 794 task->fun(task->arg); 795 /* Can't dereference task after this point. */ 796 SDT_PROBE1(usb, kernel, task, done, task); 797 if (!mpsafe) 798 KERNEL_UNLOCK_ONE(curlwp); 799 800 mutex_enter(&taskq->lock); 801 KASSERTMSG(taskq->current_task == task, 802 "somebody scribbled on usb taskq %p", taskq); 803 taskq->current_task = NULL; 804 cv_broadcast(&taskq->cv); 805 } 806 } 807 mutex_exit(&taskq->lock); 808 } 809 810 int 811 usbctlprint(void *aux, const char *pnp) 812 { 813 /* only "usb"es can attach to host controllers */ 814 if (pnp) 815 aprint_normal("usb at %s", pnp); 816 817 return UNCONF; 818 } 819 820 int 821 usbopen(dev_t dev, int flag, int mode, struct lwp *l) 822 { 823 int unit = minor(dev); 824 struct usb_softc *sc; 825 826 if (nusbbusses == 0) 827 return ENXIO; 828 829 if (unit == USB_DEV_MINOR) { 830 if (usb_dev_open) 831 return EBUSY; 832 usb_dev_open = 1; 833 mutex_enter(&proc_lock); 834 usb_async_proc = NULL; 835 mutex_exit(&proc_lock); 836 return 0; 837 } 838 839 sc = device_lookup_private(&usb_cd, unit); 840 if (!sc) 841 return ENXIO; 842 843 if (sc->sc_dying) 844 return EIO; 845 846 return 0; 847 } 848 849 int 850 usbread(dev_t dev, struct uio *uio, int flag) 851 { 852 struct usb_event *ue; 853 struct usb_event_old *ueo = NULL; /* XXXGCC */ 854 int useold = 0; 855 int error, n; 856 857 if (minor(dev) != USB_DEV_MINOR) 858 return ENXIO; 859 860 switch (uio->uio_resid) { 861 case sizeof(struct usb_event_old): 862 ueo = kmem_zalloc(sizeof(struct usb_event_old), KM_SLEEP); 863 useold = 1; 864 /* FALLTHROUGH */ 865 case sizeof(struct usb_event): 866 ue = usb_alloc_event(); 867 break; 868 default: 869 return EINVAL; 870 } 871 872 error = 0; 873 mutex_enter(&usb_event_lock); 874 for (;;) { 875 n = usb_get_next_event(ue); 876 if (n != 0) 877 break; 878 if (flag & IO_NDELAY) { 879 error = EWOULDBLOCK; 880 break; 881 } 882 error = cv_wait_sig(&usb_event_cv, &usb_event_lock); 883 if (error) 884 break; 885 } 886 mutex_exit(&usb_event_lock); 887 if (!error) { 888 if (useold) { /* copy fields to old struct */ 889 MODULE_HOOK_CALL(usb_subr_copy_30_hook, 890 (ue, ueo, uio), enosys(), error); 891 if (error == ENOSYS) 892 error = EINVAL; 893 894 if (!error) 895 error = uiomove((void *)ueo, sizeof(*ueo), uio); 896 } else 897 error = uiomove((void *)ue, sizeof(*ue), uio); 898 } 899 usb_free_event(ue); 900 if (ueo) 901 kmem_free(ueo, sizeof(struct usb_event_old)); 902 903 return error; 904 } 905 906 int 907 usbclose(dev_t dev, int flag, int mode, 908 struct lwp *l) 909 { 910 int unit = minor(dev); 911 912 if (unit == USB_DEV_MINOR) { 913 mutex_enter(&proc_lock); 914 usb_async_proc = NULL; 915 mutex_exit(&proc_lock); 916 usb_dev_open = 0; 917 } 918 919 return 0; 920 } 921 922 int 923 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l) 924 { 925 struct usb_softc *sc; 926 int unit = minor(devt); 927 928 USBHIST_FUNC(); USBHIST_CALLARGS(usbdebug, "cmd %#jx", cmd, 0, 0, 0); 929 930 if (unit == USB_DEV_MINOR) { 931 switch (cmd) { 932 case FIONBIO: 933 /* All handled in the upper FS layer. */ 934 return 0; 935 936 case FIOASYNC: 937 mutex_enter(&proc_lock); 938 if (*(int *)data) 939 usb_async_proc = l->l_proc; 940 else 941 usb_async_proc = NULL; 942 mutex_exit(&proc_lock); 943 return 0; 944 945 default: 946 return EINVAL; 947 } 948 } 949 950 sc = device_lookup_private(&usb_cd, unit); 951 952 if (sc->sc_dying) 953 return EIO; 954 955 int error = 0; 956 switch (cmd) { 957 #ifdef USB_DEBUG 958 case USB_SETDEBUG: 959 if (!(flag & FWRITE)) 960 return EBADF; 961 usbdebug = ((*(int *)data) & 0x000000ff); 962 break; 963 #endif /* USB_DEBUG */ 964 case USB_REQUEST: 965 { 966 struct usb_ctl_request *ur = (void *)data; 967 int len = UGETW(ur->ucr_request.wLength); 968 struct iovec iov; 969 struct uio uio; 970 void *ptr = 0; 971 int addr = ur->ucr_addr; 972 usbd_status err; 973 974 if (!(flag & FWRITE)) { 975 error = EBADF; 976 goto fail; 977 } 978 979 DPRINTF("USB_REQUEST addr=%jd len=%jd", addr, len, 0, 0); 980 if (len < 0 || len > 32768) { 981 error = EINVAL; 982 goto fail; 983 } 984 if (addr < 0 || addr >= USB_MAX_DEVICES) { 985 error = EINVAL; 986 goto fail; 987 } 988 size_t dindex = usb_addr2dindex(addr); 989 if (sc->sc_bus->ub_devices[dindex] == NULL) { 990 error = EINVAL; 991 goto fail; 992 } 993 if (len != 0) { 994 iov.iov_base = (void *)ur->ucr_data; 995 iov.iov_len = len; 996 uio.uio_iov = &iov; 997 uio.uio_iovcnt = 1; 998 uio.uio_resid = len; 999 uio.uio_offset = 0; 1000 uio.uio_rw = 1001 ur->ucr_request.bmRequestType & UT_READ ? 1002 UIO_READ : UIO_WRITE; 1003 uio.uio_vmspace = l->l_proc->p_vmspace; 1004 ptr = kmem_alloc(len, KM_SLEEP); 1005 if (uio.uio_rw == UIO_WRITE) { 1006 error = uiomove(ptr, len, &uio); 1007 if (error) 1008 goto ret; 1009 } 1010 } 1011 err = usbd_do_request_flags(sc->sc_bus->ub_devices[dindex], 1012 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen, 1013 USBD_DEFAULT_TIMEOUT); 1014 if (err) { 1015 error = EIO; 1016 goto ret; 1017 } 1018 if (len > ur->ucr_actlen) 1019 len = ur->ucr_actlen; 1020 if (len != 0) { 1021 if (uio.uio_rw == UIO_READ) { 1022 error = uiomove(ptr, len, &uio); 1023 if (error) 1024 goto ret; 1025 } 1026 } 1027 ret: 1028 if (ptr) { 1029 len = UGETW(ur->ucr_request.wLength); 1030 kmem_free(ptr, len); 1031 } 1032 break; 1033 } 1034 1035 case USB_DEVICEINFO: 1036 { 1037 struct usbd_device *dev; 1038 struct usb_device_info *di = (void *)data; 1039 int addr = di->udi_addr; 1040 1041 if (addr < 0 || addr >= USB_MAX_DEVICES) { 1042 error = EINVAL; 1043 goto fail; 1044 } 1045 size_t dindex = usb_addr2dindex(addr); 1046 if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) { 1047 error = ENXIO; 1048 goto fail; 1049 } 1050 usbd_fill_deviceinfo(dev, di, 1); 1051 break; 1052 } 1053 1054 case USB_DEVICEINFO_OLD: 1055 { 1056 struct usbd_device *dev; 1057 struct usb_device_info_old *di = (void *)data; 1058 int addr = di->udi_addr; 1059 1060 if (addr < 1 || addr >= USB_MAX_DEVICES) { 1061 error = EINVAL; 1062 goto fail; 1063 } 1064 size_t dindex = usb_addr2dindex(addr); 1065 if ((dev = sc->sc_bus->ub_devices[dindex]) == NULL) { 1066 error = ENXIO; 1067 goto fail; 1068 } 1069 MODULE_HOOK_CALL(usb_subr_fill_30_hook, 1070 (dev, di, 1, usbd_devinfo_vp, usbd_printBCD), 1071 enosys(), error); 1072 if (error == ENOSYS) 1073 error = EINVAL; 1074 if (error) 1075 goto fail; 1076 break; 1077 } 1078 1079 case USB_DEVICESTATS: 1080 *(struct usb_device_stats *)data = sc->sc_bus->ub_stats; 1081 break; 1082 1083 default: 1084 error = EINVAL; 1085 } 1086 1087 fail: 1088 1089 DPRINTF("... done (error = %jd)", error, 0, 0, 0); 1090 1091 return error; 1092 } 1093 1094 int 1095 usbpoll(dev_t dev, int events, struct lwp *l) 1096 { 1097 int revents, mask; 1098 1099 if (minor(dev) == USB_DEV_MINOR) { 1100 revents = 0; 1101 mask = POLLIN | POLLRDNORM; 1102 1103 mutex_enter(&usb_event_lock); 1104 if (events & mask && usb_nevents > 0) 1105 revents |= events & mask; 1106 if (revents == 0 && events & mask) 1107 selrecord(l, &usb_selevent); 1108 mutex_exit(&usb_event_lock); 1109 1110 return revents; 1111 } else { 1112 return 0; 1113 } 1114 } 1115 1116 static void 1117 filt_usbrdetach(struct knote *kn) 1118 { 1119 1120 mutex_enter(&usb_event_lock); 1121 selremove_knote(&usb_selevent, kn); 1122 mutex_exit(&usb_event_lock); 1123 } 1124 1125 static int 1126 filt_usbread(struct knote *kn, long hint) 1127 { 1128 1129 if (usb_nevents == 0) 1130 return 0; 1131 1132 kn->kn_data = sizeof(struct usb_event); 1133 return 1; 1134 } 1135 1136 static const struct filterops usbread_filtops = { 1137 .f_isfd = 1, 1138 .f_attach = NULL, 1139 .f_detach = filt_usbrdetach, 1140 .f_event = filt_usbread, 1141 }; 1142 1143 int 1144 usbkqfilter(dev_t dev, struct knote *kn) 1145 { 1146 1147 switch (kn->kn_filter) { 1148 case EVFILT_READ: 1149 if (minor(dev) != USB_DEV_MINOR) 1150 return 1; 1151 kn->kn_fop = &usbread_filtops; 1152 break; 1153 1154 default: 1155 return EINVAL; 1156 } 1157 1158 kn->kn_hook = NULL; 1159 1160 mutex_enter(&usb_event_lock); 1161 selrecord_knote(&usb_selevent, kn); 1162 mutex_exit(&usb_event_lock); 1163 1164 return 0; 1165 } 1166 1167 /* Explore device tree from the root. */ 1168 Static void 1169 usb_discover(struct usb_softc *sc) 1170 { 1171 struct usbd_bus *bus = sc->sc_bus; 1172 1173 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1174 1175 KASSERT(KERNEL_LOCKED_P()); 1176 KASSERT(mutex_owned(bus->ub_lock)); 1177 1178 if (usb_noexplore > 1) 1179 return; 1180 1181 /* 1182 * We need mutual exclusion while traversing the device tree, 1183 * but this is guaranteed since this function is only called 1184 * from the event thread for the controller. 1185 * 1186 * Also, we now have bus->ub_lock held, and in combination 1187 * with ub_exploring, avoids interferring with polling. 1188 */ 1189 SDT_PROBE1(usb, kernel, bus, discover__start, bus); 1190 while (bus->ub_needsexplore && !sc->sc_dying) { 1191 bus->ub_needsexplore = 0; 1192 mutex_exit(sc->sc_bus->ub_lock); 1193 SDT_PROBE1(usb, kernel, bus, explore__start, bus); 1194 bus->ub_roothub->ud_hub->uh_explore(bus->ub_roothub); 1195 SDT_PROBE1(usb, kernel, bus, explore__done, bus); 1196 mutex_enter(bus->ub_lock); 1197 } 1198 SDT_PROBE1(usb, kernel, bus, discover__done, bus); 1199 } 1200 1201 void 1202 usb_needs_explore(struct usbd_device *dev) 1203 { 1204 1205 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1206 SDT_PROBE1(usb, kernel, bus, needs__explore, dev->ud_bus); 1207 1208 mutex_enter(dev->ud_bus->ub_lock); 1209 dev->ud_bus->ub_needsexplore = 1; 1210 cv_signal(&dev->ud_bus->ub_needsexplore_cv); 1211 mutex_exit(dev->ud_bus->ub_lock); 1212 } 1213 1214 void 1215 usb_needs_reattach(struct usbd_device *dev) 1216 { 1217 1218 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1219 SDT_PROBE1(usb, kernel, bus, needs__reattach, dev->ud_bus); 1220 1221 mutex_enter(dev->ud_bus->ub_lock); 1222 dev->ud_powersrc->up_reattach = 1; 1223 dev->ud_bus->ub_needsexplore = 1; 1224 cv_signal(&dev->ud_bus->ub_needsexplore_cv); 1225 mutex_exit(dev->ud_bus->ub_lock); 1226 } 1227 1228 /* Called at with usb_event_lock held. */ 1229 int 1230 usb_get_next_event(struct usb_event *ue) 1231 { 1232 struct usb_event_q *ueq; 1233 1234 KASSERT(mutex_owned(&usb_event_lock)); 1235 1236 if (usb_nevents <= 0) 1237 return 0; 1238 ueq = SIMPLEQ_FIRST(&usb_events); 1239 #ifdef DIAGNOSTIC 1240 if (ueq == NULL) { 1241 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents); 1242 usb_nevents = 0; 1243 return 0; 1244 } 1245 #endif 1246 if (ue) 1247 *ue = ueq->ue; 1248 SIMPLEQ_REMOVE_HEAD(&usb_events, next); 1249 usb_free_event((struct usb_event *)(void *)ueq); 1250 usb_nevents--; 1251 return 1; 1252 } 1253 1254 void 1255 usbd_add_dev_event(int type, struct usbd_device *udev) 1256 { 1257 struct usb_event *ue = usb_alloc_event(); 1258 1259 usbd_fill_deviceinfo(udev, &ue->u.ue_device, false); 1260 usb_add_event(type, ue); 1261 } 1262 1263 void 1264 usbd_add_drv_event(int type, struct usbd_device *udev, device_t dev) 1265 { 1266 struct usb_event *ue = usb_alloc_event(); 1267 1268 ue->u.ue_driver.ue_cookie = udev->ud_cookie; 1269 strncpy(ue->u.ue_driver.ue_devname, device_xname(dev), 1270 sizeof(ue->u.ue_driver.ue_devname)); 1271 usb_add_event(type, ue); 1272 } 1273 1274 Static struct usb_event * 1275 usb_alloc_event(void) 1276 { 1277 /* Yes, this is right; we allocate enough so that we can use it later */ 1278 return kmem_zalloc(sizeof(struct usb_event_q), KM_SLEEP); 1279 } 1280 1281 Static void 1282 usb_free_event(struct usb_event *uep) 1283 { 1284 kmem_free(uep, sizeof(struct usb_event_q)); 1285 } 1286 1287 Static void 1288 usb_add_event(int type, struct usb_event *uep) 1289 { 1290 struct usb_event_q *ueq; 1291 struct timeval thetime; 1292 1293 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1294 1295 microtime(&thetime); 1296 /* Don't want to wait here with usb_event_lock held */ 1297 ueq = (struct usb_event_q *)(void *)uep; 1298 ueq->ue = *uep; 1299 ueq->ue.ue_type = type; 1300 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time); 1301 SDT_PROBE1(usb, kernel, event, add, uep); 1302 1303 mutex_enter(&usb_event_lock); 1304 if (++usb_nevents >= USB_MAX_EVENTS) { 1305 /* Too many queued events, drop an old one. */ 1306 DPRINTF("event dropped", 0, 0, 0, 0); 1307 #ifdef KDTRACE_HOOKS 1308 struct usb_event oue; 1309 if (usb_get_next_event(&oue)) 1310 SDT_PROBE1(usb, kernel, event, drop, &oue); 1311 #else 1312 usb_get_next_event(NULL); 1313 #endif 1314 } 1315 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next); 1316 cv_signal(&usb_event_cv); 1317 selnotify(&usb_selevent, 0, 0); 1318 if (usb_async_proc != NULL) { 1319 kpreempt_disable(); 1320 softint_schedule(usb_async_sih); 1321 kpreempt_enable(); 1322 } 1323 mutex_exit(&usb_event_lock); 1324 } 1325 1326 Static void 1327 usb_async_intr(void *cookie) 1328 { 1329 proc_t *proc; 1330 1331 mutex_enter(&proc_lock); 1332 if ((proc = usb_async_proc) != NULL) 1333 psignal(proc, SIGIO); 1334 mutex_exit(&proc_lock); 1335 } 1336 1337 Static void 1338 usb_soft_intr(void *arg) 1339 { 1340 struct usbd_bus *bus = arg; 1341 1342 mutex_enter(bus->ub_lock); 1343 bus->ub_methods->ubm_softint(bus); 1344 mutex_exit(bus->ub_lock); 1345 } 1346 1347 void 1348 usb_schedsoftintr(struct usbd_bus *bus) 1349 { 1350 1351 USBHIST_FUNC(); 1352 USBHIST_CALLARGS(usbdebug, "polling=%jd", bus->ub_usepolling, 0, 0, 0); 1353 1354 /* In case the bus never finished setting up. */ 1355 if (__predict_false(bus->ub_soft == NULL)) 1356 return; 1357 1358 if (bus->ub_usepolling) { 1359 bus->ub_methods->ubm_softint(bus); 1360 } else { 1361 kpreempt_disable(); 1362 softint_schedule(bus->ub_soft); 1363 kpreempt_enable(); 1364 } 1365 } 1366 1367 int 1368 usb_activate(device_t self, enum devact act) 1369 { 1370 struct usb_softc *sc = device_private(self); 1371 1372 switch (act) { 1373 case DVACT_DEACTIVATE: 1374 sc->sc_dying = 1; 1375 return 0; 1376 default: 1377 return EOPNOTSUPP; 1378 } 1379 } 1380 1381 void 1382 usb_childdet(device_t self, device_t child) 1383 { 1384 int i; 1385 struct usb_softc *sc = device_private(self); 1386 struct usbd_device *dev; 1387 1388 if ((dev = sc->sc_port.up_dev) == NULL || dev->ud_subdevlen == 0) 1389 return; 1390 1391 for (i = 0; i < dev->ud_subdevlen; i++) 1392 if (dev->ud_subdevs[i] == child) 1393 dev->ud_subdevs[i] = NULL; 1394 } 1395 1396 int 1397 usb_detach(device_t self, int flags) 1398 { 1399 struct usb_softc *sc = device_private(self); 1400 struct usb_event *ue; 1401 int rc; 1402 1403 USBHIST_FUNC(); USBHIST_CALLED(usbdebug); 1404 1405 /* Make all devices disconnect. */ 1406 if (sc->sc_port.up_dev != NULL && 1407 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0) 1408 return rc; 1409 1410 if (sc->sc_pmf_registered) 1411 pmf_device_deregister(self); 1412 /* Kill off event thread. */ 1413 sc->sc_dying = 1; 1414 while (sc->sc_event_thread != NULL) { 1415 mutex_enter(sc->sc_bus->ub_lock); 1416 cv_signal(&sc->sc_bus->ub_needsexplore_cv); 1417 cv_timedwait(&sc->sc_bus->ub_needsexplore_cv, 1418 sc->sc_bus->ub_lock, hz * 60); 1419 mutex_exit(sc->sc_bus->ub_lock); 1420 } 1421 DPRINTF("event thread dead", 0, 0, 0, 0); 1422 1423 if (sc->sc_bus->ub_soft != NULL) { 1424 softint_disestablish(sc->sc_bus->ub_soft); 1425 sc->sc_bus->ub_soft = NULL; 1426 } 1427 1428 ue = usb_alloc_event(); 1429 ue->u.ue_ctrlr.ue_bus = device_unit(self); 1430 usb_add_event(USB_EVENT_CTRLR_DETACH, ue); 1431 1432 cv_destroy(&sc->sc_bus->ub_needsexplore_cv); 1433 1434 return 0; 1435 } 1436