1 /* $NetBSD: usb.c,v 1.119 2009/11/12 20:11:35 dyoung Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2002, 2008 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 * 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.119 2009/11/12 20:11:35 dyoung Exp $"); 41 42 #include "opt_compat_netbsd.h" 43 44 #include "ohci.h" 45 #include "uhci.h" 46 47 #include <sys/param.h> 48 #include <sys/systm.h> 49 #include <sys/kernel.h> 50 #include <sys/malloc.h> 51 #include <sys/device.h> 52 #include <sys/kthread.h> 53 #include <sys/proc.h> 54 #include <sys/conf.h> 55 #include <sys/fcntl.h> 56 #include <sys/poll.h> 57 #include <sys/select.h> 58 #include <sys/vnode.h> 59 #include <sys/signalvar.h> 60 #include <sys/intr.h> 61 62 #include <dev/usb/usb.h> 63 #include <dev/usb/usbdi.h> 64 #include <dev/usb/usbdi_util.h> 65 66 #define USB_DEV_MINOR 255 67 68 #include <sys/bus.h> 69 70 #include <dev/usb/usbdivar.h> 71 #include <dev/usb/usb_quirks.h> 72 73 #ifdef USB_DEBUG 74 #define DPRINTF(x) if (usbdebug) logprintf x 75 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf x 76 int usbdebug = 0; 77 #if defined(UHCI_DEBUG) && NUHCI > 0 78 extern int uhcidebug; 79 #endif 80 #if defined(OHCI_DEBUG) && NOHCI > 0 81 extern int ohcidebug; 82 #endif 83 /* 84 * 0 - do usual exploration 85 * 1 - do not use timeout exploration 86 * >1 - do no exploration 87 */ 88 int usb_noexplore = 0; 89 #else 90 #define DPRINTF(x) 91 #define DPRINTFN(n,x) 92 #endif 93 94 struct usb_softc { 95 #if 0 96 USBBASEDEVICE sc_dev; /* base device */ 97 #endif 98 usbd_bus_handle sc_bus; /* USB controller */ 99 struct usbd_port sc_port; /* dummy port for root hub */ 100 101 struct lwp *sc_event_thread; 102 103 char sc_dying; 104 }; 105 106 struct usb_taskq { 107 TAILQ_HEAD(, usb_task) tasks; 108 struct lwp *task_thread_lwp; 109 const char *name; 110 int taskcreated; /* task thread exists. */ 111 }; 112 113 static struct usb_taskq usb_taskq[USB_NUM_TASKQS]; 114 115 dev_type_open(usbopen); 116 dev_type_close(usbclose); 117 dev_type_read(usbread); 118 dev_type_ioctl(usbioctl); 119 dev_type_poll(usbpoll); 120 dev_type_kqfilter(usbkqfilter); 121 122 const struct cdevsw usb_cdevsw = { 123 usbopen, usbclose, usbread, nowrite, usbioctl, 124 nostop, notty, usbpoll, nommap, usbkqfilter, D_OTHER, 125 }; 126 127 Static void usb_discover(struct usb_softc *); 128 Static void usb_create_event_thread(device_t); 129 Static void usb_event_thread(void *); 130 Static void usb_task_thread(void *); 131 132 #define USB_MAX_EVENTS 100 133 struct usb_event_q { 134 struct usb_event ue; 135 SIMPLEQ_ENTRY(usb_event_q) next; 136 }; 137 Static SIMPLEQ_HEAD(, usb_event_q) usb_events = 138 SIMPLEQ_HEAD_INITIALIZER(usb_events); 139 Static int usb_nevents = 0; 140 Static struct selinfo usb_selevent; 141 Static usb_proc_ptr usb_async_proc; /* process that wants USB SIGIO */ 142 Static void *usb_async_sih; 143 Static int usb_dev_open = 0; 144 Static struct usb_event *usb_alloc_event(void); 145 Static void usb_free_event(struct usb_event *); 146 Static void usb_add_event(int, struct usb_event *); 147 Static int usb_get_next_event(struct usb_event *); 148 Static void usb_async_intr(void *); 149 150 #ifdef COMPAT_30 151 Static void usb_copy_old_devinfo(struct usb_device_info_old *, const struct usb_device_info *); 152 #endif 153 154 Static const char *usbrev_str[] = USBREV_STR; 155 156 static int usb_match(device_t, cfdata_t, void *); 157 static void usb_attach(device_t, device_t, void *); 158 static int usb_detach(device_t, int); 159 static int usb_activate(device_t, enum devact); 160 static void usb_childdet(device_t, device_t); 161 static void usb_doattach(device_t); 162 163 extern struct cfdriver usb_cd; 164 165 CFATTACH_DECL3_NEW(usb, sizeof(struct usb_softc), 166 usb_match, usb_attach, usb_detach, usb_activate, NULL, usb_childdet, 167 DVF_DETACH_SHUTDOWN); 168 169 int 170 usb_match(device_t parent, cfdata_t match, void *aux) 171 { 172 DPRINTF(("usbd_match\n")); 173 return (UMATCH_GENERIC); 174 } 175 176 void 177 usb_attach(device_t parent, device_t self, void *aux) 178 { 179 struct usb_softc *sc = device_private(self); 180 int usbrev; 181 182 sc->sc_bus = aux; 183 usbrev = sc->sc_bus->usbrev; 184 185 aprint_naive("\n"); 186 aprint_normal(": USB revision %s", usbrev_str[usbrev]); 187 switch (usbrev) { 188 case USBREV_1_0: 189 case USBREV_1_1: 190 case USBREV_2_0: 191 break; 192 default: 193 aprint_error(", not supported\n"); 194 sc->sc_dying = 1; 195 USB_ATTACH_ERROR_RETURN; 196 } 197 aprint_normal("\n"); 198 199 config_interrupts(self, usb_doattach); 200 } 201 202 static void 203 usb_doattach(device_t self) 204 { 205 static bool usb_selevent_init; /* XXX */ 206 struct usb_softc *sc = device_private(self); 207 usbd_device_handle dev; 208 usbd_status err; 209 int speed; 210 struct usb_event *ue; 211 212 if (!usb_selevent_init) { 213 selinit(&usb_selevent); 214 usb_selevent_init = true; 215 } 216 DPRINTF(("usbd_doattach\n")); 217 218 sc->sc_bus->usbctl = self; 219 sc->sc_port.power = USB_MAX_POWER; 220 221 switch (sc->sc_bus->usbrev) { 222 case USBREV_1_0: 223 case USBREV_1_1: 224 speed = USB_SPEED_FULL; 225 break; 226 case USBREV_2_0: 227 speed = USB_SPEED_HIGH; 228 break; 229 default: 230 panic("usb_doattach"); 231 } 232 233 ue = usb_alloc_event(); 234 ue->u.ue_ctrlr.ue_bus = device_unit(self); 235 usb_add_event(USB_EVENT_CTRLR_ATTACH, ue); 236 237 #ifdef USB_USE_SOFTINTR 238 /* XXX we should have our own level */ 239 sc->sc_bus->soft = softint_establish(SOFTINT_NET, 240 sc->sc_bus->methods->soft_intr, sc->sc_bus); 241 if (sc->sc_bus->soft == NULL) { 242 aprint_error("%s: can't register softintr\n", 243 device_xname(self)); 244 sc->sc_dying = 1; 245 USB_ATTACH_ERROR_RETURN; 246 } 247 #endif 248 249 err = usbd_new_device(self, sc->sc_bus, 0, speed, 0, 250 &sc->sc_port); 251 if (!err) { 252 dev = sc->sc_port.device; 253 if (dev->hub == NULL) { 254 sc->sc_dying = 1; 255 aprint_error("%s: root device is not a hub\n", 256 device_xname(self)); 257 USB_ATTACH_ERROR_RETURN; 258 } 259 sc->sc_bus->root_hub = dev; 260 #if 1 261 /* 262 * Turning this code off will delay attachment of USB devices 263 * until the USB event thread is running, which means that 264 * the keyboard will not work until after cold boot. 265 */ 266 if (cold && (device_cfdata(self)->cf_flags & 1)) 267 dev->hub->explore(sc->sc_bus->root_hub); 268 #endif 269 } else { 270 aprint_error("%s: root hub problem, error=%d\n", 271 device_xname(self), err); 272 sc->sc_dying = 1; 273 } 274 275 config_pending_incr(); 276 usb_create_event_thread(self); 277 278 if (!pmf_device_register(self, NULL, NULL)) 279 aprint_error_dev(self, "couldn't establish power handler\n"); 280 281 usb_async_sih = softint_establish(SOFTINT_CLOCK | SOFTINT_MPSAFE, 282 usb_async_intr, NULL); 283 284 USB_ATTACH_SUCCESS_RETURN; 285 } 286 287 static const char *taskq_names[] = USB_TASKQ_NAMES; 288 289 void 290 usb_create_event_thread(device_t self) 291 { 292 struct usb_softc *sc = device_private(self); 293 struct usb_taskq *taskq; 294 int i; 295 296 if (usb_kthread_create1(PRI_NONE, 0, NULL, usb_event_thread, sc, 297 &sc->sc_event_thread, "%s", device_xname(self))) { 298 printf("%s: unable to create event thread for\n", 299 device_xname(self)); 300 panic("usb_create_event_thread"); 301 } 302 for (i = 0; i < USB_NUM_TASKQS; i++) { 303 taskq = &usb_taskq[i]; 304 305 if (taskq->taskcreated) 306 continue; 307 308 TAILQ_INIT(&taskq->tasks); 309 taskq->taskcreated = 1; 310 taskq->name = taskq_names[i]; 311 if (usb_kthread_create1(PRI_NONE, 0, NULL, usb_task_thread, 312 taskq, &taskq->task_thread_lwp, taskq->name)) { 313 printf("unable to create task thread: %s\n", taskq->name); 314 panic("usb_create_event_thread task"); 315 } 316 } 317 } 318 319 /* 320 * Add a task to be performed by the task thread. This function can be 321 * called from any context and the task will be executed in a process 322 * context ASAP. 323 */ 324 void 325 usb_add_task(usbd_device_handle dev, struct usb_task *task, int queue) 326 { 327 struct usb_taskq *taskq; 328 int s; 329 330 taskq = &usb_taskq[queue]; 331 s = splusb(); 332 if (task->queue == -1) { 333 DPRINTFN(2,("usb_add_task: task=%p\n", task)); 334 TAILQ_INSERT_TAIL(&taskq->tasks, task, next); 335 task->queue = queue; 336 } else { 337 DPRINTFN(3,("usb_add_task: task=%p on q\n", task)); 338 } 339 wakeup(&taskq->tasks); 340 splx(s); 341 } 342 343 void 344 usb_rem_task(usbd_device_handle dev, struct usb_task *task) 345 { 346 struct usb_taskq *taskq; 347 int s; 348 349 taskq = &usb_taskq[task->queue]; 350 s = splusb(); 351 if (task->queue != -1) { 352 TAILQ_REMOVE(&taskq->tasks, task, next); 353 task->queue = -1; 354 } 355 splx(s); 356 } 357 358 void 359 usb_event_thread(void *arg) 360 { 361 struct usb_softc *sc = arg; 362 363 DPRINTF(("usb_event_thread: start\n")); 364 365 /* 366 * In case this controller is a companion controller to an 367 * EHCI controller we need to wait until the EHCI controller 368 * has grabbed the port. 369 * XXX It would be nicer to do this with a tsleep(), but I don't 370 * know how to synchronize the creation of the threads so it 371 * will work. 372 */ 373 usb_delay_ms(sc->sc_bus, 500); 374 375 /* Make sure first discover does something. */ 376 sc->sc_bus->needs_explore = 1; 377 usb_discover(sc); 378 config_pending_decr(); 379 380 while (!sc->sc_dying) { 381 #ifdef USB_DEBUG 382 if (usb_noexplore < 2) 383 #endif 384 usb_discover(sc); 385 #ifdef USB_DEBUG 386 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt", 387 usb_noexplore ? 0 : hz * 60); 388 #else 389 (void)tsleep(&sc->sc_bus->needs_explore, PWAIT, "usbevt", 390 hz * 60); 391 #endif 392 DPRINTFN(2,("usb_event_thread: woke up\n")); 393 } 394 sc->sc_event_thread = NULL; 395 396 /* In case parent is waiting for us to exit. */ 397 wakeup(sc); 398 399 DPRINTF(("usb_event_thread: exit\n")); 400 kthread_exit(0); 401 } 402 403 void 404 usb_task_thread(void *arg) 405 { 406 struct usb_task *task; 407 struct usb_taskq *taskq; 408 int s; 409 410 taskq = arg; 411 DPRINTF(("usb_task_thread: start taskq %s\n", taskq->name)); 412 413 s = splusb(); 414 for (;;) { 415 task = TAILQ_FIRST(&taskq->tasks); 416 if (task == NULL) { 417 tsleep(&taskq->tasks, PWAIT, "usbtsk", 0); 418 task = TAILQ_FIRST(&taskq->tasks); 419 } 420 DPRINTFN(2,("usb_task_thread: woke up task=%p\n", task)); 421 if (task != NULL) { 422 TAILQ_REMOVE(&taskq->tasks, task, next); 423 task->queue = -1; 424 splx(s); 425 task->fun(task->arg); 426 s = splusb(); 427 } 428 } 429 } 430 431 int 432 usbctlprint(void *aux, const char *pnp) 433 { 434 /* only "usb"es can attach to host controllers */ 435 if (pnp) 436 aprint_normal("usb at %s", pnp); 437 438 return (UNCONF); 439 } 440 441 int 442 usbopen(dev_t dev, int flag, int mode, struct lwp *l) 443 { 444 int unit = minor(dev); 445 struct usb_softc *sc; 446 447 if (unit == USB_DEV_MINOR) { 448 if (usb_dev_open) 449 return (EBUSY); 450 usb_dev_open = 1; 451 mutex_enter(proc_lock); 452 usb_async_proc = 0; 453 mutex_exit(proc_lock); 454 return (0); 455 } 456 457 sc = device_lookup_private(&usb_cd, unit); 458 if (!sc) 459 return (ENXIO); 460 461 if (sc->sc_dying) 462 return (EIO); 463 464 return (0); 465 } 466 467 int 468 usbread(dev_t dev, struct uio *uio, int flag) 469 { 470 struct usb_event *ue; 471 #ifdef COMPAT_30 472 struct usb_event_old *ueo = NULL; /* XXXGCC */ 473 #endif 474 int s, error, n, useold; 475 476 if (minor(dev) != USB_DEV_MINOR) 477 return (ENXIO); 478 479 useold = 0; 480 switch (uio->uio_resid) { 481 #ifdef COMPAT_30 482 case sizeof(struct usb_event_old): 483 ueo = malloc(sizeof(struct usb_event_old), M_USBDEV, 484 M_WAITOK|M_ZERO); 485 useold = 1; 486 /* FALLTHRU */ 487 #endif 488 case sizeof(struct usb_event): 489 ue = usb_alloc_event(); 490 break; 491 default: 492 return (EINVAL); 493 } 494 495 error = 0; 496 s = splusb(); 497 for (;;) { 498 n = usb_get_next_event(ue); 499 if (n != 0) 500 break; 501 if (flag & IO_NDELAY) { 502 error = EWOULDBLOCK; 503 break; 504 } 505 error = tsleep(&usb_events, PZERO | PCATCH, "usbrea", 0); 506 if (error) 507 break; 508 } 509 splx(s); 510 if (!error) { 511 #ifdef COMPAT_30 512 if (useold) { /* copy fields to old struct */ 513 ueo->ue_type = ue->ue_type; 514 memcpy(&ueo->ue_time, &ue->ue_time, 515 sizeof(struct timespec)); 516 switch (ue->ue_type) { 517 case USB_EVENT_DEVICE_ATTACH: 518 case USB_EVENT_DEVICE_DETACH: 519 usb_copy_old_devinfo(&ueo->u.ue_device, &ue->u.ue_device); 520 break; 521 522 case USB_EVENT_CTRLR_ATTACH: 523 case USB_EVENT_CTRLR_DETACH: 524 ueo->u.ue_ctrlr.ue_bus=ue->u.ue_ctrlr.ue_bus; 525 break; 526 527 case USB_EVENT_DRIVER_ATTACH: 528 case USB_EVENT_DRIVER_DETACH: 529 ueo->u.ue_driver.ue_cookie=ue->u.ue_driver.ue_cookie; 530 memcpy(ueo->u.ue_driver.ue_devname, 531 ue->u.ue_driver.ue_devname, 532 sizeof(ue->u.ue_driver.ue_devname)); 533 break; 534 default: 535 ; 536 } 537 538 error = uiomove((void *)ueo, sizeof *ueo, uio); 539 } else 540 #endif 541 error = uiomove((void *)ue, sizeof *ue, uio); 542 } 543 usb_free_event(ue); 544 #ifdef COMPAT_30 545 if (useold) 546 free(ueo, M_USBDEV); 547 #endif 548 549 return (error); 550 } 551 552 int 553 usbclose(dev_t dev, int flag, int mode, 554 struct lwp *l) 555 { 556 int unit = minor(dev); 557 558 if (unit == USB_DEV_MINOR) { 559 mutex_enter(proc_lock); 560 usb_async_proc = 0; 561 mutex_exit(proc_lock); 562 usb_dev_open = 0; 563 } 564 565 return (0); 566 } 567 568 int 569 usbioctl(dev_t devt, u_long cmd, void *data, int flag, struct lwp *l) 570 { 571 struct usb_softc *sc; 572 int unit = minor(devt); 573 574 if (unit == USB_DEV_MINOR) { 575 switch (cmd) { 576 case FIONBIO: 577 /* All handled in the upper FS layer. */ 578 return (0); 579 580 case FIOASYNC: 581 mutex_enter(proc_lock); 582 if (*(int *)data) 583 usb_async_proc = l->l_proc; 584 else 585 usb_async_proc = 0; 586 mutex_exit(proc_lock); 587 return (0); 588 589 default: 590 return (EINVAL); 591 } 592 } 593 594 sc = device_lookup_private(&usb_cd, unit); 595 596 if (sc->sc_dying) 597 return (EIO); 598 599 switch (cmd) { 600 #ifdef USB_DEBUG 601 case USB_SETDEBUG: 602 if (!(flag & FWRITE)) 603 return (EBADF); 604 usbdebug = ((*(int *)data) & 0x000000ff); 605 #if defined(UHCI_DEBUG) && NUHCI > 0 606 uhcidebug = ((*(int *)data) & 0x0000ff00) >> 8; 607 #endif 608 #if defined(OHCI_DEBUG) && NOHCI > 0 609 ohcidebug = ((*(int *)data) & 0x00ff0000) >> 16; 610 #endif 611 break; 612 #endif /* USB_DEBUG */ 613 case USB_REQUEST: 614 { 615 struct usb_ctl_request *ur = (void *)data; 616 int len = UGETW(ur->ucr_request.wLength); 617 struct iovec iov; 618 struct uio uio; 619 void *ptr = 0; 620 int addr = ur->ucr_addr; 621 usbd_status err; 622 int error = 0; 623 624 if (!(flag & FWRITE)) 625 return (EBADF); 626 627 DPRINTF(("usbioctl: USB_REQUEST addr=%d len=%d\n", addr, len)); 628 if (len < 0 || len > 32768) 629 return (EINVAL); 630 if (addr < 0 || addr >= USB_MAX_DEVICES || 631 sc->sc_bus->devices[addr] == 0) 632 return (EINVAL); 633 if (len != 0) { 634 iov.iov_base = (void *)ur->ucr_data; 635 iov.iov_len = len; 636 uio.uio_iov = &iov; 637 uio.uio_iovcnt = 1; 638 uio.uio_resid = len; 639 uio.uio_offset = 0; 640 uio.uio_rw = 641 ur->ucr_request.bmRequestType & UT_READ ? 642 UIO_READ : UIO_WRITE; 643 uio.uio_vmspace = l->l_proc->p_vmspace; 644 ptr = malloc(len, M_TEMP, M_WAITOK); 645 if (uio.uio_rw == UIO_WRITE) { 646 error = uiomove(ptr, len, &uio); 647 if (error) 648 goto ret; 649 } 650 } 651 err = usbd_do_request_flags(sc->sc_bus->devices[addr], 652 &ur->ucr_request, ptr, ur->ucr_flags, &ur->ucr_actlen, 653 USBD_DEFAULT_TIMEOUT); 654 if (err) { 655 error = EIO; 656 goto ret; 657 } 658 if (len > ur->ucr_actlen) 659 len = ur->ucr_actlen; 660 if (len != 0) { 661 if (uio.uio_rw == UIO_READ) { 662 error = uiomove(ptr, len, &uio); 663 if (error) 664 goto ret; 665 } 666 } 667 ret: 668 if (ptr) 669 free(ptr, M_TEMP); 670 return (error); 671 } 672 673 case USB_DEVICEINFO: 674 { 675 usbd_device_handle dev; 676 struct usb_device_info *di = (void *)data; 677 int addr = di->udi_addr; 678 679 if (addr < 1 || addr >= USB_MAX_DEVICES) 680 return EINVAL; 681 if ((dev = sc->sc_bus->devices[addr]) == NULL) 682 return ENXIO; 683 usbd_fill_deviceinfo(dev, di, 1); 684 break; 685 } 686 687 #ifdef COMPAT_30 688 case USB_DEVICEINFO_OLD: 689 { 690 usbd_device_handle dev; 691 struct usb_device_info_old *di = (void *)data; 692 int addr = di->udi_addr; 693 694 if (addr < 1 || addr >= USB_MAX_DEVICES) 695 return EINVAL; 696 if ((dev = sc->sc_bus->devices[addr]) == NULL) 697 return ENXIO; 698 usbd_fill_deviceinfo_old(dev, di, 1); 699 break; 700 } 701 #endif 702 703 case USB_DEVICESTATS: 704 *(struct usb_device_stats *)data = sc->sc_bus->stats; 705 break; 706 707 default: 708 return (EINVAL); 709 } 710 return (0); 711 } 712 713 int 714 usbpoll(dev_t dev, int events, struct lwp *l) 715 { 716 int revents, mask, s; 717 718 if (minor(dev) == USB_DEV_MINOR) { 719 revents = 0; 720 mask = POLLIN | POLLRDNORM; 721 722 s = splusb(); 723 if (events & mask && usb_nevents > 0) 724 revents |= events & mask; 725 if (revents == 0 && events & mask) 726 selrecord(l, &usb_selevent); 727 splx(s); 728 729 return (revents); 730 } else { 731 return (0); 732 } 733 } 734 735 static void 736 filt_usbrdetach(struct knote *kn) 737 { 738 int s; 739 740 s = splusb(); 741 SLIST_REMOVE(&usb_selevent.sel_klist, kn, knote, kn_selnext); 742 splx(s); 743 } 744 745 static int 746 filt_usbread(struct knote *kn, long hint) 747 { 748 749 if (usb_nevents == 0) 750 return (0); 751 752 kn->kn_data = sizeof(struct usb_event); 753 return (1); 754 } 755 756 static const struct filterops usbread_filtops = 757 { 1, NULL, filt_usbrdetach, filt_usbread }; 758 759 int 760 usbkqfilter(dev_t dev, struct knote *kn) 761 { 762 struct klist *klist; 763 int s; 764 765 switch (kn->kn_filter) { 766 case EVFILT_READ: 767 if (minor(dev) != USB_DEV_MINOR) 768 return (1); 769 klist = &usb_selevent.sel_klist; 770 kn->kn_fop = &usbread_filtops; 771 break; 772 773 default: 774 return (EINVAL); 775 } 776 777 kn->kn_hook = NULL; 778 779 s = splusb(); 780 SLIST_INSERT_HEAD(klist, kn, kn_selnext); 781 splx(s); 782 783 return (0); 784 } 785 786 /* Explore device tree from the root. */ 787 Static void 788 usb_discover(struct usb_softc *sc) 789 { 790 791 DPRINTFN(2,("usb_discover\n")); 792 #ifdef USB_DEBUG 793 if (usb_noexplore > 1) 794 return; 795 #endif 796 /* 797 * We need mutual exclusion while traversing the device tree, 798 * but this is guaranteed since this function is only called 799 * from the event thread for the controller. 800 */ 801 while (sc->sc_bus->needs_explore && !sc->sc_dying) { 802 sc->sc_bus->needs_explore = 0; 803 sc->sc_bus->root_hub->hub->explore(sc->sc_bus->root_hub); 804 } 805 } 806 807 void 808 usb_needs_explore(usbd_device_handle dev) 809 { 810 DPRINTFN(2,("usb_needs_explore\n")); 811 dev->bus->needs_explore = 1; 812 wakeup(&dev->bus->needs_explore); 813 } 814 815 void 816 usb_needs_reattach(usbd_device_handle dev) 817 { 818 DPRINTFN(2,("usb_needs_reattach\n")); 819 dev->powersrc->reattach = 1; 820 dev->bus->needs_explore = 1; 821 wakeup(&dev->bus->needs_explore); 822 } 823 824 /* Called at splusb() */ 825 int 826 usb_get_next_event(struct usb_event *ue) 827 { 828 struct usb_event_q *ueq; 829 830 if (usb_nevents <= 0) 831 return (0); 832 ueq = SIMPLEQ_FIRST(&usb_events); 833 #ifdef DIAGNOSTIC 834 if (ueq == NULL) { 835 printf("usb: usb_nevents got out of sync! %d\n", usb_nevents); 836 usb_nevents = 0; 837 return (0); 838 } 839 #endif 840 if (ue) 841 *ue = ueq->ue; 842 SIMPLEQ_REMOVE_HEAD(&usb_events, next); 843 usb_free_event((struct usb_event *)(void *)ueq); 844 usb_nevents--; 845 return (1); 846 } 847 848 void 849 usbd_add_dev_event(int type, usbd_device_handle udev) 850 { 851 struct usb_event *ue = usb_alloc_event(); 852 853 usbd_fill_deviceinfo(udev, &ue->u.ue_device, USB_EVENT_IS_ATTACH(type)); 854 usb_add_event(type, ue); 855 } 856 857 void 858 usbd_add_drv_event(int type, usbd_device_handle udev, device_t dev) 859 { 860 struct usb_event *ue = usb_alloc_event(); 861 862 ue->u.ue_driver.ue_cookie = udev->cookie; 863 strncpy(ue->u.ue_driver.ue_devname, USBDEVPTRNAME(dev), 864 sizeof ue->u.ue_driver.ue_devname); 865 usb_add_event(type, ue); 866 } 867 868 Static struct usb_event * 869 usb_alloc_event(void) 870 { 871 /* Yes, this is right; we allocate enough so that we can use it later */ 872 return malloc(sizeof(struct usb_event_q), M_USBDEV, M_WAITOK|M_ZERO); 873 } 874 875 Static void 876 usb_free_event(struct usb_event *uep) 877 { 878 free(uep, M_USBDEV); 879 } 880 881 Static void 882 usb_add_event(int type, struct usb_event *uep) 883 { 884 struct usb_event_q *ueq; 885 struct timeval thetime; 886 int s; 887 888 microtime(&thetime); 889 /* Don't want to wait here inside splusb() */ 890 ueq = (struct usb_event_q *)(void *)uep; 891 ueq->ue = *uep; 892 ueq->ue.ue_type = type; 893 TIMEVAL_TO_TIMESPEC(&thetime, &ueq->ue.ue_time); 894 895 s = splusb(); 896 if (++usb_nevents >= USB_MAX_EVENTS) { 897 /* Too many queued events, drop an old one. */ 898 DPRINTFN(-1,("usb: event dropped\n")); 899 (void)usb_get_next_event(0); 900 } 901 SIMPLEQ_INSERT_TAIL(&usb_events, ueq, next); 902 wakeup(&usb_events); 903 selnotify(&usb_selevent, 0, 0); 904 if (usb_async_proc != NULL) { 905 softint_schedule(usb_async_sih); 906 } 907 splx(s); 908 } 909 910 Static void 911 usb_async_intr(void *cookie) 912 { 913 proc_t *proc; 914 915 mutex_enter(proc_lock); 916 if ((proc = usb_async_proc) != NULL) 917 psignal(proc, SIGIO); 918 mutex_exit(proc_lock); 919 } 920 921 void 922 usb_schedsoftintr(usbd_bus_handle bus) 923 { 924 DPRINTFN(10,("usb_schedsoftintr: polling=%d\n", bus->use_polling)); 925 #ifdef USB_USE_SOFTINTR 926 if (bus->use_polling) { 927 bus->methods->soft_intr(bus); 928 } else { 929 softint_schedule(bus->soft); 930 } 931 #else 932 bus->methods->soft_intr(bus); 933 #endif /* USB_USE_SOFTINTR */ 934 } 935 936 int 937 usb_activate(device_t self, enum devact act) 938 { 939 struct usb_softc *sc = device_private(self); 940 941 switch (act) { 942 case DVACT_DEACTIVATE: 943 sc->sc_dying = 1; 944 return 0; 945 default: 946 return EOPNOTSUPP; 947 } 948 } 949 950 void 951 usb_childdet(device_t self, device_t child) 952 { 953 int i; 954 struct usb_softc *sc = device_private(self); 955 struct usbd_device *dev; 956 957 if ((dev = sc->sc_port.device) == NULL || dev->subdevlen == 0) 958 return; 959 960 for (i = 0; i < dev->subdevlen; i++) 961 if (dev->subdevs[i] == child) 962 dev->subdevs[i] = NULL; 963 } 964 965 int 966 usb_detach(device_t self, int flags) 967 { 968 struct usb_softc *sc = device_private(self); 969 struct usb_event *ue; 970 int rc; 971 972 DPRINTF(("usb_detach: start\n")); 973 974 /* Make all devices disconnect. */ 975 if (sc->sc_port.device != NULL && 976 (rc = usb_disconnect_port(&sc->sc_port, self, flags)) != 0) 977 return rc; 978 979 pmf_device_deregister(self); 980 /* Kill off event thread. */ 981 sc->sc_dying = 1; 982 while (sc->sc_event_thread != NULL) { 983 wakeup(&sc->sc_bus->needs_explore); 984 tsleep(sc, PWAIT, "usbdet", hz * 60); 985 } 986 DPRINTF(("usb_detach: event thread dead\n")); 987 988 #ifdef USB_USE_SOFTINTR 989 if (sc->sc_bus->soft != NULL) { 990 softint_disestablish(sc->sc_bus->soft); 991 sc->sc_bus->soft = NULL; 992 } 993 #endif 994 995 ue = usb_alloc_event(); 996 ue->u.ue_ctrlr.ue_bus = device_unit(self); 997 usb_add_event(USB_EVENT_CTRLR_DETACH, ue); 998 999 return (0); 1000 } 1001 1002 #ifdef COMPAT_30 1003 Static void 1004 usb_copy_old_devinfo(struct usb_device_info_old *uo, 1005 const struct usb_device_info *ue) 1006 { 1007 const unsigned char *p; 1008 unsigned char *q; 1009 int i, n; 1010 1011 uo->udi_bus = ue->udi_bus; 1012 uo->udi_addr = ue->udi_addr; 1013 uo->udi_cookie = ue->udi_cookie; 1014 for (i = 0, p = (const unsigned char *)ue->udi_product, 1015 q = (unsigned char *)uo->udi_product; 1016 *p && i < USB_MAX_STRING_LEN - 1; p++) { 1017 if (*p < 0x80) 1018 q[i++] = *p; 1019 else { 1020 q[i++] = '?'; 1021 if ((*p & 0xe0) == 0xe0) 1022 p++; 1023 p++; 1024 } 1025 } 1026 q[i] = 0; 1027 1028 for (i = 0, p = ue->udi_vendor, q = uo->udi_vendor; 1029 *p && i < USB_MAX_STRING_LEN - 1; p++) { 1030 if (* p < 0x80) 1031 q[i++] = *p; 1032 else { 1033 q[i++] = '?'; 1034 p++; 1035 if ((*p & 0xe0) == 0xe0) 1036 p++; 1037 } 1038 } 1039 q[i] = 0; 1040 1041 memcpy(uo->udi_release, ue->udi_release, sizeof(uo->udi_release)); 1042 1043 uo->udi_productNo = ue->udi_productNo; 1044 uo->udi_vendorNo = ue->udi_vendorNo; 1045 uo->udi_releaseNo = ue->udi_releaseNo; 1046 uo->udi_class = ue->udi_class; 1047 uo->udi_subclass = ue->udi_subclass; 1048 uo->udi_protocol = ue->udi_protocol; 1049 uo->udi_config = ue->udi_config; 1050 uo->udi_speed = ue->udi_speed; 1051 uo->udi_power = ue->udi_power; 1052 uo->udi_nports = ue->udi_nports; 1053 1054 for (n=0; n<USB_MAX_DEVNAMES; n++) 1055 memcpy(uo->udi_devnames[n], 1056 ue->udi_devnames[n], USB_MAX_DEVNAMELEN); 1057 memcpy(uo->udi_ports, ue->udi_ports, sizeof(uo->udi_ports)); 1058 } 1059 #endif 1060