1 /* $NetBSD: usb.c,v 1.89 2006/10/12 01:32:00 christos 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.89 2006/10/12 01:32:00 christos 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, D_OTHER, 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 struct usb_event *usb_alloc_event(void); 140 Static void usb_free_event(struct usb_event *); 141 Static void usb_add_event(int, struct usb_event *); 142 143 Static int usb_get_next_event(struct usb_event *); 144 145 Static const char *usbrev_str[] = USBREV_STR; 146 147 USB_DECLARE_DRIVER(usb); 148 149 USB_MATCH(usb) 150 { 151 DPRINTF(("usbd_match\n")); 152 return (UMATCH_GENERIC); 153 } 154 155 USB_ATTACH(usb) 156 { 157 struct usb_softc *sc = (struct usb_softc *)self; 158 usbd_device_handle dev; 159 usbd_status err; 160 int usbrev; 161 int speed; 162 struct usb_event *ue; 163 164 DPRINTF(("usbd_attach\n")); 165 166 usbd_init(); 167 sc->sc_bus = aux; 168 sc->sc_bus->usbctl = sc; 169 sc->sc_port.power = USB_MAX_POWER; 170 171 usbrev = sc->sc_bus->usbrev; 172 printf(": USB revision %s", usbrev_str[usbrev]); 173 switch (usbrev) { 174 case USBREV_1_0: 175 case USBREV_1_1: 176 speed = USB_SPEED_FULL; 177 break; 178 case USBREV_2_0: 179 speed = USB_SPEED_HIGH; 180 break; 181 default: 182 printf(", not supported\n"); 183 sc->sc_dying = 1; 184 USB_ATTACH_ERROR_RETURN; 185 } 186 printf("\n"); 187 188 /* Make sure not to use tsleep() if we are cold booting. */ 189 if (cold) 190 sc->sc_bus->use_polling++; 191 192 ue = usb_alloc_event(); 193 ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev); 194 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue); 195 196 #ifdef USB_USE_SOFTINTR 197 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 198 /* XXX we should have our own level */ 199 sc->sc_bus->soft = softintr_establish(IPL_SOFTNET, 200 sc->sc_bus->methods->soft_intr, sc->sc_bus); 201 if (sc->sc_bus->soft == NULL) { 202 printf("%s: can't register softintr\n", USBDEVNAME(sc->sc_dev)); 203 sc->sc_dying = 1; 204 USB_ATTACH_ERROR_RETURN; 205 } 206 #else 207 usb_callout_init(sc->sc_bus->softi); 208 #endif 209 #endif 210 211 err = usbd_new_device(USBDEV(sc->sc_dev), sc->sc_bus, 0, speed, 0, 212 &sc->sc_port); 213 if (!err) { 214 dev = sc->sc_port.device; 215 if (dev->hub == NULL) { 216 sc->sc_dying = 1; 217 printf("%s: root device is not a hub\n", 218 USBDEVNAME(sc->sc_dev)); 219 USB_ATTACH_ERROR_RETURN; 220 } 221 sc->sc_bus->root_hub = dev; 222 #if 1 223 /* 224 * Turning this code off will delay attachment of USB devices 225 * until the USB event thread is running, which means that 226 * the keyboard will not work until after cold boot. 227 */ 228 if (cold && (device_cfdata(&sc->sc_dev)->cf_flags & 1)) 229 dev->hub->explore(sc->sc_bus->root_hub); 230 #endif 231 } else { 232 printf("%s: root hub problem, error=%d\n", 233 USBDEVNAME(sc->sc_dev), err); 234 sc->sc_dying = 1; 235 } 236 if (cold) 237 sc->sc_bus->use_polling--; 238 239 config_pending_incr(); 240 usb_kthread_create(usb_create_event_thread, sc); 241 242 USB_ATTACH_SUCCESS_RETURN; 243 } 244 245 #if defined(__NetBSD__) || defined(__OpenBSD__) 246 void 247 usb_create_event_thread(void *arg) 248 { 249 struct usb_softc *sc = arg; 250 static int created = 0; 251 252 if (usb_kthread_create1(usb_event_thread, sc, &sc->sc_event_thread, 253 "%s", sc->sc_dev.dv_xname)) { 254 printf("%s: unable to create event thread for\n", 255 sc->sc_dev.dv_xname); 256 panic("usb_create_event_thread"); 257 } 258 if (!created) { 259 created = 1; 260 TAILQ_INIT(&usb_all_tasks); 261 if (usb_kthread_create1(usb_task_thread, NULL, 262 &usb_task_thread_proc, "usbtask")) { 263 printf("unable to create task thread\n"); 264 panic("usb_create_event_thread task"); 265 } 266 } 267 } 268 269 /* 270 * Add a task to be performed by the task thread. This function can be 271 * called from any context and the task will be executed in a process 272 * context ASAP. 273 */ 274 void 275 usb_add_task(usbd_device_handle dev __unused, struct usb_task *task) 276 { 277 int s; 278 279 s = splusb(); 280 if (!task->onqueue) { 281 DPRINTFN(2,("usb_add_task: task=%p\n", task)); 282 TAILQ_INSERT_TAIL(&usb_all_tasks, task, next); 283 task->onqueue = 1; 284 } else { 285 DPRINTFN(3,("usb_add_task: task=%p on q\n", task)); 286 } 287 wakeup(&usb_all_tasks); 288 splx(s); 289 } 290 291 void 292 usb_rem_task(usbd_device_handle dev __unused, struct usb_task *task) 293 { 294 int s; 295 296 s = splusb(); 297 if (task->onqueue) { 298 TAILQ_REMOVE(&usb_all_tasks, task, next); 299 task->onqueue = 0; 300 } 301 splx(s); 302 } 303 304 void 305 usb_event_thread(void *arg) 306 { 307 struct usb_softc *sc = arg; 308 309 DPRINTF(("usb_event_thread: start\n")); 310 311 /* 312 * In case this controller is a companion controller to an 313 * EHCI controller we need to wait until the EHCI controller 314 * has grabbed the port. 315 * XXX It would be nicer to do this with a tsleep(), but I don't 316 * know how to synchronize the creation of the threads so it 317 * will work. 318 */ 319 usb_delay_ms(sc->sc_bus, 500); 320 321 /* Make sure first discover does something. */ 322 sc->sc_bus->needs_explore = 1; 323 usb_discover(sc); 324 config_pending_decr(); 325 326 while (!sc->sc_dying) { 327 #ifdef USB_DEBUG 328 if (usb_noexplore < 2) 329 #endif 330 usb_discover(sc); 331 #ifdef USB_DEBUG 332 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt", 333 usb_noexplore ? 0 : hz * 60); 334 #else 335 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt", 336 hz * 60); 337 #endif 338 DPRINTFN(2,("usb_event_thread: woke up\n")); 339 } 340 sc->sc_event_thread = NULL; 341 342 /* In case parent is waiting for us to exit. */ 343 wakeup(sc); 344 345 DPRINTF(("usb_event_thread: exit\n")); 346 kthread_exit(0); 347 } 348 349 void 350 usb_task_thread(void *arg __unused) 351 { 352 struct usb_task *task; 353 int s; 354 355 DPRINTF(("usb_task_thread: start\n")); 356 357 s = splusb(); 358 for (;;) { 359 task = TAILQ_FIRST(&usb_all_tasks); 360 if (task == NULL) { 361 tsleep(&usb_all_tasks, PWAIT, "usbtsk", 0); 362 task = TAILQ_FIRST(&usb_all_tasks); 363 } 364 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task)); 365 if (task != NULL) { 366 TAILQ_REMOVE(&usb_all_tasks, task, next); 367 task->onqueue = 0; 368 splx(s); 369 task->fun(task->arg); 370 s = splusb(); 371 } 372 } 373 } 374 375 int 376 usbctlprint(void *aux __unused, const char *pnp) 377 { 378 /* only "usb"es can attach to host controllers */ 379 if (pnp) 380 aprint_normal("usb at %s", pnp); 381 382 return (UNCONF); 383 } 384 #endif /* defined(__NetBSD__) || defined(__OpenBSD__) */ 385 386 int 387 usbopen(dev_t dev, int flag __unused, int mode __unused, struct lwp *l __unused) 388 { 389 int unit = minor(dev); 390 struct usb_softc *sc; 391 392 if (unit == USB_DEV_MINOR) { 393 if (usb_dev_open) 394 return (EBUSY); 395 usb_dev_open = 1; 396 usb_async_proc = 0; 397 return (0); 398 } 399 400 USB_GET_SC_OPEN(usb, unit, sc); 401 402 if (sc->sc_dying) 403 return (EIO); 404 405 return (0); 406 } 407 408 int 409 usbread(dev_t dev, struct uio *uio, int flag) 410 { 411 struct usb_event *ue = usb_alloc_event(); 412 int s, error, n; 413 414 if (minor(dev) != USB_DEV_MINOR) 415 return (ENXIO); 416 417 if (uio->uio_resid != sizeof(struct usb_event)) 418 return (EINVAL); 419 420 error = 0; 421 s = splusb(); 422 for (;;) { 423 n = usb_get_next_event(ue); 424 if (n != 0) 425 break; 426 if (flag & IO_NDELAY) { 427 error = EWOULDBLOCK; 428 break; 429 } 430 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0); 431 if (error) 432 break; 433 } 434 splx(s); 435 if (!error) 436 error = uiomove((void *)ue, uio->uio_resid, uio); 437 usb_free_event(ue); 438 439 return (error); 440 } 441 442 int 443 usbclose(dev_t dev, int flag __unused, int mode __unused, 444 struct lwp *l __unused) 445 { 446 int unit = minor(dev); 447 448 if (unit == USB_DEV_MINOR) { 449 usb_async_proc = 0; 450 usb_dev_open = 0; 451 } 452 453 return (0); 454 } 455 456 int 457 usbioctl(dev_t devt, u_long cmd, caddr_t data, int flag, struct lwp *l) 458 { 459 struct usb_softc *sc; 460 int unit = minor(devt); 461 462 if (unit == USB_DEV_MINOR) { 463 switch (cmd) { 464 case FIONBIO: 465 /* All handled in the upper FS layer. */ 466 return (0); 467 468 case FIOASYNC: 469 if (*(int *)data) 470 usb_async_proc = l->l_proc; 471 else 472 usb_async_proc = 0; 473 return (0); 474 475 default: 476 return (EINVAL); 477 } 478 } 479 480 USB_GET_SC(usb, unit, sc); 481 482 if (sc->sc_dying) 483 return (EIO); 484 485 switch (cmd) { 486 #ifdef USB_DEBUG 487 case USB_SETDEBUG: 488 if (!(flag & FWRITE)) 489 return (EBADF); 490 usbdebug = ((*(int *)data) & 0x000000ff); 491 #if defined(UHCI_DEBUG) && NUHCI > 0 492 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8; 493 #endif 494 #if defined(OHCI_DEBUG) && NOHCI > 0 495 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16; 496 #endif 497 break; 498 #endif /* USB_DEBUG */ 499 case USB_REQUEST: 500 { 501 struct usb_ctl_request *ur = (void *)data; 502 int len = UGETW(ur->ucr_request.wLength); 503 struct iovec iov; 504 struct uio uio; 505 void *ptr = 0; 506 int addr = ur->ucr_addr; 507 usbd_status err; 508 int error = 0; 509 510 if (!(flag & FWRITE)) 511 return (EBADF); 512 513 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len)); 514 if (len < 0 || len > 32768) 515 return (EINVAL); 516 if (addr < 0 || addr >= USB_MAX_DEVICES || 517 sc->sc_bus->devices[addr] == 0) 518 return (EINVAL); 519 if (len != 0) { 520 iov.iov_base = (caddr_t)ur->ucr_data; 521 iov.iov_len = len; 522 uio.uio_iov = &iov; 523 uio.uio_iovcnt = 1; 524 uio.uio_resid = len; 525 uio.uio_offset = 0; 526 uio.uio_rw = 527 ur->ucr_request.bmRequestType & UT_READ ? 528 UIO_READ : UIO_WRITE; 529 uio.uio_vmspace = l->l_proc->p_vmspace; 530 ptr = malloc(len, M_TEMP, M_WAITOK); 531 if (uio.uio_rw == UIO_WRITE) { 532 error = uiomove(ptr, len, &uio); 533 if (error) 534 goto ret; 535 } 536 } 537 err = usbd_do_request_flags(sc->sc_bus->devices[addr], 538 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen, 539 USBD_DEFAULT_TIMEOUT); 540 if (err) { 541 error = EIO; 542 goto ret; 543 } 544 if (len != 0) { 545 if (uio.uio_rw == UIO_READ) { 546 error = uiomove(ptr, len, &uio); 547 if (error) 548 goto ret; 549 } 550 } 551 ret: 552 if (ptr) 553 free(ptr, M_TEMP); 554 return (error); 555 } 556 557 case USB_DEVICEINFO: 558 { 559 struct usb_device_info *di = (void *)data; 560 int addr = di->udi_addr; 561 usbd_device_handle dev; 562 563 if (addr < 1 || addr >= USB_MAX_DEVICES) 564 return (EINVAL); 565 dev = sc->sc_bus->devices[addr]; 566 if (dev == NULL) 567 return (ENXIO); 568 usbd_fill_deviceinfo(dev, di, 1); 569 break; 570 } 571 572 case USB_DEVICESTATS: 573 *(struct usb_device_stats *)data = sc->sc_bus->stats; 574 break; 575 576 default: 577 return (EINVAL); 578 } 579 return (0); 580 } 581 582 int 583 usbpoll(dev_t dev, int events, struct lwp *l) 584 { 585 int revents, mask, s; 586 587 if (minor(dev) == USB_DEV_MINOR) { 588 revents = 0; 589 mask = POLLIN | POLLRDNORM; 590 591 s = splusb(); 592 if (events & mask && usb_nevents > 0) 593 revents |= events & mask; 594 if (revents == 0 && events & mask) 595 selrecord(l, &usb_selevent); 596 splx(s); 597 598 return (revents); 599 } else { 600 return (0); 601 } 602 } 603 604 static void 605 filt_usbrdetach(struct knote *kn) 606 { 607 int s; 608 609 s = splusb(); 610 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext); 611 splx(s); 612 } 613 614 static int 615 filt_usbread(struct knote *kn, long hint __unused) 616 { 617 618 if (usb_nevents == 0) 619 return (0); 620 621 kn->kn_data = sizeof(struct usb_event); 622 return (1); 623 } 624 625 static const struct filterops usbread_filtops = 626 { 1, NULL, filt_usbrdetach, filt_usbread }; 627 628 int 629 usbkqfilter(dev_t dev, struct knote *kn) 630 { 631 struct klist *klist; 632 int s; 633 634 switch (kn->kn_filter) { 635 case EVFILT_READ: 636 if (minor(dev) != USB_DEV_MINOR) 637 return (1); 638 klist = &usb_selevent.sel_klist; 639 kn->kn_fop = &usbread_filtops; 640 break; 641 642 default: 643 return (1); 644 } 645 646 kn->kn_hook = NULL; 647 648 s = splusb(); 649 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 650 splx(s); 651 652 return (0); 653 } 654 655 /* Explore device tree from the root. */ 656 Static void 657 usb_discover(void *v) 658 { 659 struct usb_softc *sc = v; 660 661 DPRINTFN(2,("usb_discover\n")); 662 #ifdef USB_DEBUG 663 if (usb_noexplore > 1) 664 return; 665 #endif 666 /* 667 * We need mutual exclusion while traversing the device tree, 668 * but this is guaranteed since this function is only called 669 * from the event thread for the controller. 670 */ 671 while (sc->sc_bus->needs_explore && !sc->sc_dying) { 672 sc->sc_bus->needs_explore = 0; 673 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub); 674 } 675 } 676 677 void 678 usb_needs_explore(usbd_device_handle dev) 679 { 680 DPRINTFN(2,("usb_needs_explore\n")); 681 dev->bus->needs_explore = 1; 682 wakeup(&dev->bus->needs_explore); 683 } 684 685 void 686 usb_needs_reattach(usbd_device_handle dev) 687 { 688 DPRINTFN(2,("usb_needs_reattach\n")); 689 dev->powersrc->reattach = 1; 690 dev->bus->needs_explore = 1; 691 wakeup(&dev->bus->needs_explore); 692 } 693 694 /* Called at splusb() */ 695 int 696 usb_get_next_event(struct usb_event *ue) 697 { 698 struct usb_event_q *ueq; 699 700 if (usb_nevents <= 0) 701 return (0); 702 ueq = SIMPLEQ_FIRST(&usb_events); 703 #ifdef DIAGNOSTIC 704 if (ueq == NULL) { 705 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents); 706 usb_nevents = 0; 707 return (0); 708 } 709 #endif 710 if (ue) 711 *ue = ueq->ue; 712 SIMPLEQ_REMOVE_HEAD(&usb_events, next); 713 usb_free_event((struct usb_event *)(void *)ueq); 714 usb_nevents--; 715 return (1); 716 } 717 718 void 719 usbd_add_dev_event(int type, usbd_device_handle udev) 720 { 721 struct usb_event *ue = usb_alloc_event(); 722 723 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type)); 724 usb_add_event(type, ue); 725 } 726 727 void 728 usbd_add_drv_event(int type, usbd_device_handle udev, device_ptr_t dev) 729 { 730 struct usb_event *ue = usb_alloc_event(); 731 732 ue->u.ue_driver.ue_cookie = udev->cookie; 733 strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev), 734 sizeof ue->u.ue_driver.ue_devname); 735 usb_add_event(type, ue); 736 } 737 738 Static struct usb_event * 739 usb_alloc_event(void) 740 { 741 /* Yes, this is right; we allocate enough so that we can use it later */ 742 return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO); 743 } 744 745 Static void 746 usb_free_event(struct usb_event *uep) 747 { 748 free(uep, M_USBDEV); 749 } 750 751 Static void 752 usb_add_event(int type, struct usb_event *uep) 753 { 754 struct usb_event_q *ueq; 755 struct timeval thetime; 756 int s; 757 758 microtime(&thetime); 759 /* Don't want to wait here inside splusb() */ 760 ueq = (struct usb_event_q *)(void *)uep; 761 ueq->ue = *uep; 762 ueq->ue.ue_type = type; 763 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time); 764 765 s = splusb(); 766 if (++usb_nevents >= USB_MAX_EVENTS) { 767 /* Too many queued events, drop an old one. */ 768 DPRINTFN(-1,("usb: event dropped\n")); 769 (void)usb_get_next_event(0); 770 } 771 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next); 772 wakeup(&usb_events); 773 selnotify(&usb_selevent, 0); 774 if (usb_async_proc != NULL) 775 psignal(usb_async_proc, SIGIO); 776 splx(s); 777 } 778 779 void 780 usb_schedsoftintr(usbd_bus_handle bus) 781 { 782 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling)); 783 #ifdef USB_USE_SOFTINTR 784 if (bus->use_polling) { 785 bus->methods->soft_intr(bus); 786 } else { 787 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 788 softintr_schedule(bus->soft); 789 #else 790 if (!callout_pending(&bus->softi)) 791 callout_reset(&bus->softi, 0, bus->methods->soft_intr, 792 bus); 793 #endif /* __HAVE_GENERIC_SOFT_INTERRUPTS */ 794 } 795 #else 796 bus->methods->soft_intr(bus); 797 #endif /* USB_USE_SOFTINTR */ 798 } 799 800 int 801 usb_activate(device_ptr_t self, enum devact act) 802 { 803 struct usb_softc *sc = (struct usb_softc *)self; 804 usbd_device_handle dev = sc->sc_port.device; 805 int i, rv = 0; 806 807 switch (act) { 808 case DVACT_ACTIVATE: 809 return (EOPNOTSUPP); 810 811 case DVACT_DEACTIVATE: 812 sc->sc_dying = 1; 813 if (dev != NULL && dev->cdesc != NULL && dev->subdevs != NULL) { 814 for (i = 0; dev->subdevs[i]; i++) 815 rv |= config_deactivate(dev->subdevs[i]); 816 } 817 break; 818 } 819 return (rv); 820 } 821 822 int 823 usb_detach(device_ptr_t self, int flags __unused) 824 { 825 struct usb_softc *sc = (struct usb_softc *)self; 826 struct usb_event *ue; 827 828 DPRINTF(("usb_detach: start\n")); 829 830 sc->sc_dying = 1; 831 832 /* Make all devices disconnect. */ 833 if (sc->sc_port.device != NULL) 834 usb_disconnect_port(&sc->sc_port, self); 835 836 /* Kill off event thread. */ 837 if (sc->sc_event_thread != NULL) { 838 wakeup(&sc->sc_bus->needs_explore); 839 if (tsleep(sc, PWAIT, "usbdet", hz * 60)) 840 printf("%s: event thread didn't die\n", 841 USBDEVNAME(sc->sc_dev)); 842 DPRINTF(("usb_detach: event thread dead\n")); 843 } 844 845 usbd_finish(); 846 847 #ifdef USB_USE_SOFTINTR 848 #ifdef __HAVE_GENERIC_SOFT_INTERRUPTS 849 if (sc->sc_bus->soft != NULL) { 850 softintr_disestablish(sc->sc_bus->soft); 851 sc->sc_bus->soft = NULL; 852 } 853 #else 854 callout_stop(&sc->sc_bus->softi); 855 #endif 856 #endif 857 858 ue = usb_alloc_event(); 859 ue->u.ue_ctrlr.ue_bus = USBDEVUNIT(sc->sc_dev); 860 usb_add_event(USB_EVENT_CTRLR_DETACH, ue); 861 862 return (0); 863 } 864