1 /* $NetBSD: usb.c,v 1.80 2003/11/07 17:03:25 wiz Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2002 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. 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 * 3. All advertising materials mentioning features or use of this software 20 * must display the following acknowledgement: 21 * This product includes software developed by the NetBSD 22 * Foundation, Inc. and its contributors. 23 * 4. Neither the name of The NetBSD Foundation nor the names of its 24 * contributors may be used to endorse or promote products derived 25 * from this software without specific prior written permission. 26 * 27 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 28 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 29 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 30 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 31 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 32 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 33 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 34 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 35 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 36 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 37 * POSSIBILITY OF SUCH DAMAGE. 38 */ 39 40 /* 41 * USB specifications and other documentation can be found at 42 * http://www.usb.org/developers/docs/ and 43 * http://www.usb.org/developers/devclass_docs/ 44 */ 45 46 #include <sys/cdefs.h> 47 __KERNEL_RCSID(0, "$NetBSD: usb.c,v 1.80 2003/11/07 17:03:25 wiz Exp $"); 48 49 #include "ohci.h" 50 #include "uhci.h" 51 52 #include <sys/param.h> 53 #include <sys/systm.h> 54 #include <sys/kernel.h> 55 #include <sys/malloc.h> 56 #include <sys/device.h> 57 #include <sys/kthread.h> 58 #include <sys/proc.h> 59 #include <sys/conf.h> 60 #include <sys/fcntl.h> 61 #include <sys/poll.h> 62 #include <sys/select.h> 63 #include <sys/vnode.h> 64 #include <sys/signalvar.h> 65 66 #include <dev/usb/usb.h> 67 #include <dev/usb/usbdi.h> 68 #include <dev/usb/usbdi_util.h> 69 70 #define USB_DEV_MINOR 255 71 72 #include <machine/bus.h> 73 74 #include <dev/usb/usbdivar.h> 75 #include <dev/usb/usb_quirks.h> 76 77 #ifdef USB_DEBUG 78 #define DPRINTF(x) if (usbdebug) logprintf x 79 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x 80 int usbdebug = 0; 81 #if defined(UHCI_DEBUG) && NUHCI > 0 82 extern int uhcidebug; 83 #endif 84 #if defined(OHCI_DEBUG) && NOHCI > 0 85 extern int ohcidebug; 86 #endif 87 /* 88 * 0 - do usual exploration 89 * 1 - do not use timeout exploration 90 * >1 - do no exploration 91 */ 92 int usb_noexplore = 0; 93 #else 94 #define DPRINTF(x) 95 #define DPRINTFN(n,x) 96 #endif 97 98 struct usb_softc { 99 USBBASEDEVICE sc_dev; /* base device */ 100 usbd_bus_handle sc_bus; /* USB controller */ 101 struct usbd_port sc_port; /* dummy port for root hub */ 102 103 struct proc *sc_event_thread; 104 105 char sc_dying; 106 }; 107 108 TAILQ_HEAD(, usb_task) usb_all_tasks; 109 110 dev_type_open(usbopen); 111 dev_type_close(usbclose); 112 dev_type_read(usbread); 113 dev_type_ioctl(usbioctl); 114 dev_type_poll(usbpoll); 115 dev_type_kqfilter(usbkqfilter); 116 117 const struct cdevsw usb_cdevsw = { 118 usbopen, usbclose, usbread, nowrite, usbioctl, 119 nostop, notty, usbpoll, nommap, usbkqfilter, 120 }; 121 122 Static void usb_discover(void *); 123 Static void usb_create_event_thread(void *); 124 Static void usb_event_thread(void *); 125 Static void usb_task_thread(void *); 126 Static struct proc *usb_task_thread_proc = NULL; 127 128 #define USB_MAX_EVENTS 100 129 struct usb_event_q { 130 struct usb_event ue; 131 SIMPLEQ_ENTRY(usb_event_q) next; 132 }; 133 Static SIMPLEQ_HEAD(, usb_event_q) usb_events = 134 SIMPLEQ_HEAD_INITIALIZER(usb_events); 135 Static int usb_nevents = 0; 136 Static struct selinfo usb_selevent; 137 Static usb_proc_ptr usb_async_proc; /* process that wants USB SIGIO */ 138 Static int usb_dev_open = 0; 139 Static void usb_add_event(int, struct usb_event *); 140 141 Static int usb_get_next_event(struct usb_event *); 142 143 Static const char *usbrev_str[] = USBREV_STR; 144 145 USB_DECLARE_DRIVER(usb); 146 147 USB_MATCH(usb) 148 { 149 DPRINTF(("usbd_match\n")); 150 return (UMATCH_GENERIC); 151 } 152 153 USB_ATTACH(usb) 154 { 155 struct usb_softc *sc = (struct usb_softc *)self; 156 usbd_device_handle dev; 157 usbd_status err; 158 int usbrev; 159 int speed; 160 struct usb_event ue; 161 162 DPRINTF(("usbd_attach\n")); 163 164 usbd_init(); 165 sc->sc_bus = aux; 166 sc->sc_bus->usbctl = sc; 167 sc->sc_port.power = USB_MAX_POWER; 168 169 usbrev = sc->sc_bus->usbrev; 170 printf(": USB revision %s", usbrev_str[usbrev]); 171 switch (usbrev) { 172 case USBREV_1_0: 173 case USBREV_1_1: 174 speed = USB_SPEED_FULL; 175 break; 176 case USBREV_2_0: 177 speed = USB_SPEED_HIGH; 178 break; 179 default: 180 printf(", not supported\n"); 181 sc->sc_dying = 1; 182 USB_ATTACH_ERROR_RETURN; 183 } 184 printf("\n"); 185 186 /* Make sure not to use tsleep() if we are cold booting. */ 187 if (cold) 188 sc->sc_bus->use_polling++; 189 190 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev); 191 usb_add_event(USB_EVENT_CTRLR_ATTACH, &ue); 192 193 #ifdef USB_USE_SOFTINTR 194 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 195 /* XXX we should have our own level */ 196 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET, 197 sc->sc_bus->methods->soft_intr, sc->sc_bus); 198 if (sc->sc_bus->soft == NULL) { 199 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev)); 200 sc->sc_dying = 1; 201 USB_ATTACH_ERROR_RETURN; 202 } 203 #else 204 usb_callout_init(sc->sc_bus->softi); 205 #endif 206 #endif 207 208 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0, 209 &sc->sc_port); 210 if (!err) { 211 dev = sc->sc_port.device; 212 if (dev->hub == NULL) { 213 sc->sc_dying = 1; 214 printf("%s: root device is not a hub\n", 215 USBDEVNAME(sc->sc_dev)); 216 USB_ATTACH_ERROR_RETURN; 217 } 218 sc->sc_bus->root_hub = dev; 219 #if 1 220 /* 221 * Turning this code off will delay attachment of USB devices 222 * until the USB event thread is running, which means that 223 * the keyboard will not work until after cold boot. 224 */ 225 if (cold && (sc->sc_dev.dv_cfdata->cf_flags & 1)) 226 dev->hub->explore(sc->sc_bus->root_hub); 227 #endif 228 } else { 229 printf("%s: root hub problem, error=%d\n", 230 USBDEVNAME(sc->sc_dev), err); 231 sc->sc_dying = 1; 232 } 233 if (cold) 234 sc->sc_bus->use_polling--; 235 236 config_pending_incr(); 237 usb_kthread_create(usb_create_event_thread, sc); 238 239 USB_ATTACH_SUCCESS_RETURN; 240 } 241 242 #if defined(__NetBSD__) || defined(__OpenBSD__) 243 void 244 usb_create_event_thread(void *arg) 245 { 246 struct usb_softc *sc = arg; 247 static int created = 0; 248 249 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread, 250 "%s", sc->sc_dev.dv_xname)) { 251 printf("%s: unable to create event thread for\n", 252 sc->sc_dev.dv_xname); 253 panic("usb_create_event_thread"); 254 } 255 if (!created) { 256 created = 1; 257 TAILQ_INIT(&usb_all_tasks); 258 if (usb_kthread_create1(usb_task_thread, NULL, 259 &usb_task_thread_proc, "usbtask")) { 260 printf("unable to create task thread\n"); 261 panic("usb_create_event_thread task"); 262 } 263 } 264 } 265 266 /* 267 * Add a task to be performed by the task thread. This function can be 268 * called from any context and the task will be executed in a process 269 * context ASAP. 270 */ 271 void 272 usb_add_task(usbd_device_handle dev, struct usb_task *task) 273 { 274 int s; 275 276 s = splusb(); 277 if (!task->onqueue) { 278 DPRINTFN(2,("usb_add_task: task=%p\n", task)); 279 TAILQ_INSERT_TAIL(&usb_all_tasks, task, next); 280 task->onqueue = 1; 281 } else { 282 DPRINTFN(3,("usb_add_task: task=%p on q\n", task)); 283 } 284 wakeup(&usb_all_tasks); 285 splx(s); 286 } 287 288 void 289 usb_rem_task(usbd_device_handle dev, struct usb_task *task) 290 { 291 int s; 292 293 s = splusb(); 294 if (task->onqueue) { 295 TAILQ_REMOVE(&usb_all_tasks, task, next); 296 task->onqueue = 0; 297 } 298 splx(s); 299 } 300 301 void 302 usb_event_thread(void *arg) 303 { 304 struct usb_softc *sc = arg; 305 306 DPRINTF(("usb_event_thread: start\n")); 307 308 /* 309 * In case this controller is a companion controller to an 310 * EHCI controller we need to wait until the EHCI controller 311 * has grabbed the port. 312 * XXX It would be nicer to do this with a tsleep(), but I don't 313 * know how to synchronize the creation of the threads so it 314 * will work. 315 */ 316 usb_delay_ms(sc->sc_bus, 500); 317 318 /* Make sure first discover does something. */ 319 sc->sc_bus->needs_explore = 1; 320 usb_discover(sc); 321 config_pending_decr(); 322 323 while (!sc->sc_dying) { 324 #ifdef USB_DEBUG 325 if (usb_noexplore < 2) 326 #endif 327 usb_discover(sc); 328 #ifdef USB_DEBUG 329 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt", 330 usb_noexplore ? 0 : hz * 60); 331 #else 332 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt", 333 hz * 60); 334 #endif 335 DPRINTFN(2,("usb_event_thread: woke up\n")); 336 } 337 sc->sc_event_thread = NULL; 338 339 /* In case parent is waiting for us to exit. */ 340 wakeup(sc); 341 342 DPRINTF(("usb_event_thread: exit\n")); 343 kthread_exit(0); 344 } 345 346 void 347 usb_task_thread(void *arg) 348 { 349 struct usb_task *task; 350 int s; 351 352 DPRINTF(("usb_task_thread: start\n")); 353 354 s = splusb(); 355 for (;;) { 356 task = TAILQ_FIRST(&usb_all_tasks); 357 if (task == NULL) { 358 tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0); 359 task = TAILQ_FIRST(&usb_all_tasks); 360 } 361 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task)); 362 if (task != NULL) { 363 TAILQ_REMOVE(&usb_all_tasks, task, next); 364 task->onqueue = 0; 365 splx(s); 366 task->fun(task->arg); 367 s = splusb(); 368 } 369 } 370 } 371 372 int 373 usbctlprint(void *aux, const char *pnp) 374 { 375 /* only "usb"es can attach to host controllers */ 376 if (pnp) 377 aprint_normal("usb at %s", pnp); 378 379 return (UNCONF); 380 } 381 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */ 382 383 int 384 usbopen(dev_t dev, int flag, int mode, usb_proc_ptr p) 385 { 386 int unit = minor(dev); 387 struct usb_softc *sc; 388 389 if (unit == USB_DEV_MINOR) { 390 if (usb_dev_open) 391 return (EBUSY); 392 usb_dev_open = 1; 393 usb_async_proc = 0; 394 return (0); 395 } 396 397 USB_GET_SC_OPEN(usb, unit, sc); 398 399 if (sc->sc_dying) 400 return (EIO); 401 402 return (0); 403 } 404 405 int 406 usbread(dev_t dev, struct uio *uio, int flag) 407 { 408 struct usb_event ue; 409 int s, error, n; 410 411 if (minor(dev) != USB_DEV_MINOR) 412 return (ENXIO); 413 414 if (uio->uio_resid != sizeof(struct usb_event)) 415 return (EINVAL); 416 417 error = 0; 418 s = splusb(); 419 for (;;) { 420 n = usb_get_next_event(&ue); 421 if (n != 0) 422 break; 423 if (flag & IO_NDELAY) { 424 error = EWOULDBLOCK; 425 break; 426 } 427 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0); 428 if (error) 429 break; 430 } 431 splx(s); 432 if (!error) 433 error = uiomove((void *)&ue, uio->uio_resid, uio); 434 435 return (error); 436 } 437 438 int 439 usbclose(dev_t dev, int flag, int mode, usb_proc_ptr p) 440 { 441 int unit = minor(dev); 442 443 if (unit == USB_DEV_MINOR) { 444 usb_async_proc = 0; 445 usb_dev_open = 0; 446 } 447 448 return (0); 449 } 450 451 int 452 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, usb_proc_ptr p) 453 { 454 struct usb_softc *sc; 455 int unit = minor(devt); 456 457 if (unit == USB_DEV_MINOR) { 458 switch (cmd) { 459 case FIONBIO: 460 /* All handled in the upper FS layer. */ 461 return (0); 462 463 case FIOASYNC: 464 if (*(int *)data) 465 usb_async_proc = p; 466 else 467 usb_async_proc = 0; 468 return (0); 469 470 default: 471 return (EINVAL); 472 } 473 } 474 475 USB_GET_SC(usb, unit, sc); 476 477 if (sc->sc_dying) 478 return (EIO); 479 480 switch (cmd) { 481 #ifdef USB_DEBUG 482 case USB_SETDEBUG: 483 if (!(flag & FWRITE)) 484 return (EBADF); 485 usbdebug = ((*(int *)data) & 0x000000ff); 486 #if defined(UHCI_DEBUG) && NUHCI > 0 487 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8; 488 #endif 489 #if defined(OHCI_DEBUG) && NOHCI > 0 490 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16; 491 #endif 492 break; 493 #endif /* USB_DEBUG */ 494 case USB_REQUEST: 495 { 496 struct usb_ctl_request *ur = (void *)data; 497 int len = UGETW(ur->ucr_request.wLength); 498 struct iovec iov; 499 struct uio uio; 500 void *ptr = 0; 501 int addr = ur->ucr_addr; 502 usbd_status err; 503 int error = 0; 504 505 if (!(flag & FWRITE)) 506 return (EBADF); 507 508 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len)); 509 if (len < 0 || len > 32768) 510 return (EINVAL); 511 if (addr < 0 || addr >= USB_MAX_DEVICES || 512 sc->sc_bus->devices[addr] == 0) 513 return (EINVAL); 514 if (len != 0) { 515 iov.iov_base = (caddr_t)ur->ucr_data; 516 iov.iov_len = len; 517 uio.uio_iov = &iov; 518 uio.uio_iovcnt = 1; 519 uio.uio_resid = len; 520 uio.uio_offset = 0; 521 uio.uio_segflg = UIO_USERSPACE; 522 uio.uio_rw = 523 ur->ucr_request.bmRequestType & UT_READ ? 524 UIO_READ : UIO_WRITE; 525 uio.uio_procp = p; 526 ptr = malloc(len, M_TEMP, M_WAITOK); 527 if (uio.uio_rw == UIO_WRITE) { 528 error = uiomove(ptr, len, &uio); 529 if (error) 530 goto ret; 531 } 532 } 533 err = usbd_do_request_flags(sc->sc_bus->devices[addr], 534 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen, 535 USBD_DEFAULT_TIMEOUT); 536 if (err) { 537 error = EIO; 538 goto ret; 539 } 540 if (len != 0) { 541 if (uio.uio_rw == UIO_READ) { 542 error = uiomove(ptr, len, &uio); 543 if (error) 544 goto ret; 545 } 546 } 547 ret: 548 if (ptr) 549 free(ptr, M_TEMP); 550 return (error); 551 } 552 553 case USB_DEVICEINFO: 554 { 555 struct usb_device_info *di = (void *)data; 556 int addr = di->udi_addr; 557 usbd_device_handle dev; 558 559 if (addr < 1 || addr >= USB_MAX_DEVICES) 560 return (EINVAL); 561 dev = sc->sc_bus->devices[addr]; 562 if (dev == NULL) 563 return (ENXIO); 564 usbd_fill_deviceinfo(dev, di, 1); 565 break; 566 } 567 568 case USB_DEVICESTATS: 569 *(struct usb_device_stats *)data = sc->sc_bus->stats; 570 break; 571 572 default: 573 return (EINVAL); 574 } 575 return (0); 576 } 577 578 int 579 usbpoll(dev_t dev, int events, usb_proc_ptr p) 580 { 581 int revents, mask, s; 582 583 if (minor(dev) == USB_DEV_MINOR) { 584 revents = 0; 585 mask = POLLIN | POLLRDNORM; 586 587 s = splusb(); 588 if (events & mask && usb_nevents > 0) 589 revents |= events & mask; 590 if (revents == 0 && events & mask) 591 selrecord(p, &usb_selevent); 592 splx(s); 593 594 return (revents); 595 } else { 596 return (ENXIO); 597 } 598 } 599 600 static void 601 filt_usbrdetach(struct knote *kn) 602 { 603 int s; 604 605 s = splusb(); 606 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext); 607 splx(s); 608 } 609 610 static int 611 filt_usbread(struct knote *kn, long hint) 612 { 613 614 if (usb_nevents == 0) 615 return (0); 616 617 kn->kn_data = sizeof(struct usb_event); 618 return (1); 619 } 620 621 static const struct filterops usbread_filtops = 622 { 1, NULL, filt_usbrdetach, filt_usbread }; 623 624 int 625 usbkqfilter(dev_t dev, struct knote *kn) 626 { 627 struct klist *klist; 628 int s; 629 630 switch (kn->kn_filter) { 631 case EVFILT_READ: 632 if (minor(dev) != USB_DEV_MINOR) 633 return (1); 634 klist = &usb_selevent.sel_klist; 635 kn->kn_fop = &usbread_filtops; 636 break; 637 638 default: 639 return (1); 640 } 641 642 kn->kn_hook = NULL; 643 644 s = splusb(); 645 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 646 splx(s); 647 648 return (0); 649 } 650 651 /* Explore device tree from the root. */ 652 Static void 653 usb_discover(void *v) 654 { 655 struct usb_softc *sc = v; 656 657 DPRINTFN(2,("usb_discover\n")); 658 #ifdef USB_DEBUG 659 if (usb_noexplore > 1) 660 return; 661 #endif 662 /* 663 * We need mutual exclusion while traversing the device tree, 664 * but this is guaranteed since this function is only called 665 * from the event thread for the controller. 666 */ 667 while (sc->sc_bus->needs_explore && !sc->sc_dying) { 668 sc->sc_bus->needs_explore = 0; 669 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub); 670 } 671 } 672 673 void 674 usb_needs_explore(usbd_device_handle dev) 675 { 676 DPRINTFN(2,("usb_needs_explore\n")); 677 dev->bus->needs_explore = 1; 678 wakeup(&dev->bus->needs_explore); 679 } 680 681 /* Called at splusb() */ 682 int 683 usb_get_next_event(struct usb_event *ue) 684 { 685 struct usb_event_q *ueq; 686 687 if (usb_nevents <= 0) 688 return (0); 689 ueq = SIMPLEQ_FIRST(&usb_events); 690 #ifdef DIAGNOSTIC 691 if (ueq == NULL) { 692 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents); 693 usb_nevents = 0; 694 return (0); 695 } 696 #endif 697 *ue = ueq->ue; 698 SIMPLEQ_REMOVE_HEAD(&usb_events, next); 699 free(ueq, M_USBDEV); 700 usb_nevents--; 701 return (1); 702 } 703 704 void 705 usbd_add_dev_event(int type, usbd_device_handle udev) 706 { 707 struct usb_event ue; 708 709 usbd_fill_deviceinfo(udev, &ue.u.ue_device, USB_EVENT_IS_ATTACH(type)); 710 usb_add_event(type, &ue); 711 } 712 713 void 714 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev) 715 { 716 struct usb_event ue; 717 718 ue.u.ue_driver.ue_cookie = udev->cookie; 719 strncpy(ue.u.ue_driver.ue_devname, USBDEVPTRNAME(dev), 720 sizeof ue.u.ue_driver.ue_devname); 721 usb_add_event(type, &ue); 722 } 723 724 Static void 725 usb_add_event(int type, struct usb_event *uep) 726 { 727 struct usb_event_q *ueq; 728 struct usb_event ue; 729 struct timeval thetime; 730 int s; 731 732 microtime(&thetime); 733 /* Don't want to wait here inside splusb() */ 734 ueq = malloc(sizeof *ueq, M_USBDEV, M_WAITOK); 735 ueq->ue = *uep; 736 ueq->ue.ue_type = type; 737 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time); 738 739 s = splusb(); 740 if (++usb_nevents >= USB_MAX_EVENTS) { 741 /* Too many queued events, drop an old one. */ 742 DPRINTFN(-1,("usb: event dropped\n")); 743 (void)usb_get_next_event(&ue); 744 } 745 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next); 746 wakeup(&usb_events); 747 selnotify(&usb_selevent, 0); 748 if (usb_async_proc != NULL) 749 psignal(usb_async_proc, SIGIO); 750 splx(s); 751 } 752 753 void 754 usb_schedsoftintr(usbd_bus_handle bus) 755 { 756 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling)); 757 #ifdef USB_USE_SOFTINTR 758 if (bus->use_polling) { 759 bus->methods->soft_intr(bus); 760 } else { 761 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 762 softintr_schedule(bus->soft); 763 #else 764 if (!callout_pending(&bus->softi)) 765 callout_reset(&bus->softi, 0, bus->methods->soft_intr, 766 bus); 767 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */ 768 } 769 #else 770 bus->methods->soft_intr(bus); 771 #endif /* USB_USE_SOFTINTR */ 772 } 773 774 int 775 usb_activate(device_ptr_t self, enum devact act) 776 { 777 struct usb_softc *sc = (struct usb_softc *)self; 778 usbd_device_handle dev = sc->sc_port.device; 779 int i, rv = 0; 780 781 switch (act) { 782 case DVACT_ACTIVATE: 783 return (EOPNOTSUPP); 784 785 case DVACT_DEACTIVATE: 786 sc->sc_dying = 1; 787 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) { 788 for (i = 0; dev->subdevs[i]; i++) 789 rv |= config_deactivate(dev->subdevs[i]); 790 } 791 break; 792 } 793 return (rv); 794 } 795 796 int 797 usb_detach(device_ptr_t self, int flags) 798 { 799 struct usb_softc *sc = (struct usb_softc *)self; 800 struct usb_event ue; 801 802 DPRINTF(("usb_detach: start\n")); 803 804 sc->sc_dying = 1; 805 806 /* Make all devices disconnect. */ 807 if (sc->sc_port.device != NULL) 808 usb_disconnect_port(&sc->sc_port, self); 809 810 /* Kill off event thread. */ 811 if (sc->sc_event_thread != NULL) { 812 wakeup(&sc->sc_bus->needs_explore); 813 if (tsleep(sc, PWAIT, "usbdet", hz * 60)) 814 printf("%s: event thread didn't die\n", 815 USBDEVNAME(sc->sc_dev)); 816 DPRINTF(("usb_detach: event thread dead\n")); 817 } 818 819 usbd_finish(); 820 821 #ifdef USB_USE_SOFTINTR 822 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 823 if (sc->sc_bus->soft != NULL) { 824 softintr_disestablish(sc->sc_bus->soft); 825 sc->sc_bus->soft = NULL; 826 } 827 #else 828 callout_stop(&sc->sc_bus->softi); 829 #endif 830 #endif 831 832 ue.u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev); 833 usb_add_event(USB_EVENT_CTRLR_DETACH, &ue); 834 835 return (0); 836 } 837