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