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