1 /* $NetBSD: usbdi.c,v 1.137 2012/03/11 01:06:07 mrg Exp $ */ 2 3 /* 4 * Copyright (c) 1998, 2012 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Lennart Augustsson (lennart@augustsson.net) at 9 * Carlstedt Research & Technology and Matthew R. Green (mrg@eterna.com.au). 10 * 11 * Redistribution and use in source and binary forms, with or without 12 * modification, are permitted provided that the following conditions 13 * are met: 14 * 1. Redistributions of source code must retain the above copyright 15 * notice, this list of conditions and the following disclaimer. 16 * 2. Redistributions in binary form must reproduce the above copyright 17 * notice, this list of conditions and the following disclaimer in the 18 * documentation and/or other materials provided with the distribution. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 21 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 22 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 23 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 24 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 30 * POSSIBILITY OF SUCH DAMAGE. 31 */ 32 33 #include <sys/cdefs.h> 34 __KERNEL_RCSID(0, "$NetBSD: usbdi.c,v 1.137 2012/03/11 01:06:07 mrg Exp $"); 35 36 #include "opt_compat_netbsd.h" 37 #include "opt_usb.h" 38 39 #include <sys/param.h> 40 #include <sys/systm.h> 41 #include <sys/kernel.h> 42 #include <sys/device.h> 43 #include <sys/malloc.h> 44 #include <sys/proc.h> 45 #include <sys/bus.h> 46 #include <sys/cpu.h> 47 48 #include <dev/usb/usb.h> 49 #include <dev/usb/usbdi.h> 50 #include <dev/usb/usbdi_util.h> 51 #include <dev/usb/usbdivar.h> 52 #include <dev/usb/usb_mem.h> 53 #include <dev/usb/usb_quirks.h> 54 55 /* UTF-8 encoding stuff */ 56 #include <fs/unicode.h> 57 58 #ifdef USB_DEBUG 59 #define DPRINTF(x) if (usbdebug) printf x 60 #define DPRINTFN(n,x) if (usbdebug>(n)) printf x 61 extern int usbdebug; 62 #else 63 #define DPRINTF(x) 64 #define DPRINTFN(n,x) 65 #endif 66 67 Static usbd_status usbd_ar_pipe(usbd_pipe_handle pipe); 68 Static void usbd_do_request_async_cb 69 (usbd_xfer_handle, usbd_private_handle, usbd_status); 70 Static void usbd_start_next(usbd_pipe_handle pipe); 71 Static usbd_status usbd_open_pipe_ival 72 (usbd_interface_handle, u_int8_t, u_int8_t, usbd_pipe_handle *, int); 73 74 static inline int 75 usbd_xfer_isread(usbd_xfer_handle xfer) 76 { 77 if (xfer->rqflags & URQ_REQUEST) 78 return (xfer->request.bmRequestType & UT_READ); 79 else 80 return (xfer->pipe->endpoint->edesc->bEndpointAddress & 81 UE_DIR_IN); 82 } 83 84 #if defined(USB_DEBUG) || defined(EHCI_DEBUG) 85 void 86 usbd_dump_iface(struct usbd_interface *iface) 87 { 88 printf("usbd_dump_iface: iface=%p\n", iface); 89 if (iface == NULL) 90 return; 91 printf(" device=%p idesc=%p index=%d altindex=%d priv=%p\n", 92 iface->device, iface->idesc, iface->index, iface->altindex, 93 iface->priv); 94 } 95 96 void 97 usbd_dump_device(struct usbd_device *dev) 98 { 99 printf("usbd_dump_device: dev=%p\n", dev); 100 if (dev == NULL) 101 return; 102 printf(" bus=%p default_pipe=%p\n", dev->bus, dev->default_pipe); 103 printf(" address=%d config=%d depth=%d speed=%d self_powered=%d " 104 "power=%d langid=%d\n", 105 dev->address, dev->config, dev->depth, dev->speed, 106 dev->self_powered, dev->power, dev->langid); 107 } 108 109 void 110 usbd_dump_endpoint(struct usbd_endpoint *endp) 111 { 112 printf("usbd_dump_endpoint: endp=%p\n", endp); 113 if (endp == NULL) 114 return; 115 printf(" edesc=%p refcnt=%d\n", endp->edesc, endp->refcnt); 116 if (endp->edesc) 117 printf(" bEndpointAddress=0x%02x\n", 118 endp->edesc->bEndpointAddress); 119 } 120 121 void 122 usbd_dump_queue(usbd_pipe_handle pipe) 123 { 124 usbd_xfer_handle xfer; 125 126 printf("usbd_dump_queue: pipe=%p\n", pipe); 127 SIMPLEQ_FOREACH(xfer, &pipe->queue, next) { 128 printf(" xfer=%p\n", xfer); 129 } 130 } 131 132 void 133 usbd_dump_pipe(usbd_pipe_handle pipe) 134 { 135 printf("usbd_dump_pipe: pipe=%p\n", pipe); 136 if (pipe == NULL) 137 return; 138 usbd_dump_iface(pipe->iface); 139 usbd_dump_device(pipe->device); 140 usbd_dump_endpoint(pipe->endpoint); 141 printf(" (usbd_dump_pipe:)\n refcnt=%d running=%d aborting=%d\n", 142 pipe->refcnt, pipe->running, pipe->aborting); 143 printf(" intrxfer=%p, repeat=%d, interval=%d\n", 144 pipe->intrxfer, pipe->repeat, pipe->interval); 145 } 146 #endif 147 148 usbd_status 149 usbd_open_pipe(usbd_interface_handle iface, u_int8_t address, 150 u_int8_t flags, usbd_pipe_handle *pipe) 151 { 152 return (usbd_open_pipe_ival(iface, address, flags, pipe, 153 USBD_DEFAULT_INTERVAL)); 154 } 155 156 usbd_status 157 usbd_open_pipe_ival(usbd_interface_handle iface, u_int8_t address, 158 u_int8_t flags, usbd_pipe_handle *pipe, int ival) 159 { 160 usbd_pipe_handle p; 161 struct usbd_endpoint *ep; 162 usbd_status err; 163 int i; 164 165 DPRINTFN(3,("usbd_open_pipe: iface=%p address=0x%x flags=0x%x\n", 166 iface, address, flags)); 167 168 for (i = 0; i < iface->idesc->bNumEndpoints; i++) { 169 ep = &iface->endpoints[i]; 170 if (ep->edesc == NULL) 171 return (USBD_IOERROR); 172 if (ep->edesc->bEndpointAddress == address) 173 goto found; 174 } 175 return (USBD_BAD_ADDRESS); 176 found: 177 if ((flags & USBD_EXCLUSIVE_USE) && ep->refcnt != 0) 178 return (USBD_IN_USE); 179 err = usbd_setup_pipe(iface->device, iface, ep, ival, &p); 180 if (err) 181 return (err); 182 LIST_INSERT_HEAD(&iface->pipes, p, next); 183 *pipe = p; 184 return (USBD_NORMAL_COMPLETION); 185 } 186 187 usbd_status 188 usbd_open_pipe_intr(usbd_interface_handle iface, u_int8_t address, 189 u_int8_t flags, usbd_pipe_handle *pipe, 190 usbd_private_handle priv, void *buffer, u_int32_t len, 191 usbd_callback cb, int ival) 192 { 193 usbd_status err; 194 usbd_xfer_handle xfer; 195 usbd_pipe_handle ipipe; 196 197 DPRINTFN(3,("usbd_open_pipe_intr: address=0x%x flags=0x%x len=%d\n", 198 address, flags, len)); 199 200 err = usbd_open_pipe_ival(iface, address, USBD_EXCLUSIVE_USE, 201 &ipipe, ival); 202 if (err) 203 return (err); 204 xfer = usbd_alloc_xfer(iface->device); 205 if (xfer == NULL) { 206 err = USBD_NOMEM; 207 goto bad1; 208 } 209 usbd_setup_xfer(xfer, ipipe, priv, buffer, len, flags, 210 USBD_NO_TIMEOUT, cb); 211 ipipe->intrxfer = xfer; 212 ipipe->repeat = 1; 213 err = usbd_transfer(xfer); 214 *pipe = ipipe; 215 if (err != USBD_IN_PROGRESS) 216 goto bad2; 217 return (USBD_NORMAL_COMPLETION); 218 219 bad2: 220 ipipe->intrxfer = NULL; 221 ipipe->repeat = 0; 222 usbd_free_xfer(xfer); 223 bad1: 224 usbd_close_pipe(ipipe); 225 return (err); 226 } 227 228 usbd_status 229 usbd_close_pipe(usbd_pipe_handle pipe) 230 { 231 #ifdef DIAGNOSTIC 232 if (pipe == NULL) { 233 printf("usbd_close_pipe: pipe==NULL\n"); 234 return (USBD_NORMAL_COMPLETION); 235 } 236 #endif 237 238 if (--pipe->refcnt != 0) 239 return (USBD_NORMAL_COMPLETION); 240 if (! SIMPLEQ_EMPTY(&pipe->queue)) 241 return (USBD_PENDING_REQUESTS); 242 LIST_REMOVE(pipe, next); 243 pipe->endpoint->refcnt--; 244 pipe->methods->close(pipe); 245 if (pipe->intrxfer != NULL) 246 usbd_free_xfer(pipe->intrxfer); 247 free(pipe, M_USB); 248 return (USBD_NORMAL_COMPLETION); 249 } 250 251 usbd_status 252 usbd_transfer(usbd_xfer_handle xfer) 253 { 254 usbd_pipe_handle pipe = xfer->pipe; 255 usb_dma_t *dmap = &xfer->dmabuf; 256 usbd_status err; 257 unsigned int size, flags; 258 int s; 259 260 DPRINTFN(5,("usbd_transfer: xfer=%p, flags=%#x, pipe=%p, running=%d\n", 261 xfer, xfer->flags, pipe, pipe->running)); 262 263 KASSERT(KERNEL_LOCKED_P()); 264 265 #ifdef USB_DEBUG 266 if (usbdebug > 5) 267 usbd_dump_queue(pipe); 268 #endif 269 xfer->done = 0; 270 271 if (pipe->aborting) 272 return (USBD_CANCELLED); 273 274 size = xfer->length; 275 /* If there is no buffer, allocate one. */ 276 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) { 277 struct usbd_bus *bus = pipe->device->bus; 278 279 #ifdef DIAGNOSTIC 280 if (xfer->rqflags & URQ_AUTO_DMABUF) 281 printf("usbd_transfer: has old buffer!\n"); 282 #endif 283 err = bus->methods->allocm(bus, dmap, size); 284 if (err) 285 return (err); 286 xfer->rqflags |= URQ_AUTO_DMABUF; 287 } 288 289 flags = xfer->flags; 290 291 /* Copy data if going out. */ 292 if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer)) 293 memcpy(KERNADDR(dmap, 0), xfer->buffer, size); 294 295 /* xfer is not valid after the transfer method unless synchronous */ 296 err = pipe->methods->transfer(xfer); 297 298 if (err != USBD_IN_PROGRESS && err) { 299 /* The transfer has not been queued, so free buffer. */ 300 if (xfer->rqflags & URQ_AUTO_DMABUF) { 301 struct usbd_bus *bus = pipe->device->bus; 302 303 bus->methods->freem(bus, &xfer->dmabuf); 304 xfer->rqflags &= ~URQ_AUTO_DMABUF; 305 } 306 } 307 308 if (!(flags & USBD_SYNCHRONOUS)) 309 return (err); 310 311 /* Sync transfer, wait for completion. */ 312 if (err != USBD_IN_PROGRESS) 313 return (err); 314 s = splusb(); 315 if (!xfer->done) { 316 if (pipe->device->bus->use_polling) 317 panic("usbd_transfer: not done"); 318 tsleep(xfer, PRIBIO, "usbsyn", 0); 319 } 320 splx(s); 321 return (xfer->status); 322 } 323 324 /* Like usbd_transfer(), but waits for completion. */ 325 usbd_status 326 usbd_sync_transfer(usbd_xfer_handle xfer) 327 { 328 xfer->flags |= USBD_SYNCHRONOUS; 329 return (usbd_transfer(xfer)); 330 } 331 332 void * 333 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size) 334 { 335 struct usbd_bus *bus = xfer->device->bus; 336 usbd_status err; 337 338 #ifdef DIAGNOSTIC 339 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 340 printf("usbd_alloc_buffer: xfer already has a buffer\n"); 341 #endif 342 err = bus->methods->allocm(bus, &xfer->dmabuf, size); 343 if (err) 344 return (NULL); 345 xfer->rqflags |= URQ_DEV_DMABUF; 346 return (KERNADDR(&xfer->dmabuf, 0)); 347 } 348 349 void 350 usbd_free_buffer(usbd_xfer_handle xfer) 351 { 352 #ifdef DIAGNOSTIC 353 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) { 354 printf("usbd_free_buffer: no buffer\n"); 355 return; 356 } 357 #endif 358 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF); 359 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf); 360 } 361 362 void * 363 usbd_get_buffer(usbd_xfer_handle xfer) 364 { 365 if (!(xfer->rqflags & URQ_DEV_DMABUF)) 366 return (0); 367 return (KERNADDR(&xfer->dmabuf, 0)); 368 } 369 370 usbd_xfer_handle 371 usbd_alloc_xfer(usbd_device_handle dev) 372 { 373 usbd_xfer_handle xfer; 374 375 xfer = dev->bus->methods->allocx(dev->bus); 376 if (xfer == NULL) 377 return (NULL); 378 xfer->device = dev; 379 callout_init(&xfer->timeout_handle, 0); 380 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer)); 381 return (xfer); 382 } 383 384 usbd_status 385 usbd_free_xfer(usbd_xfer_handle xfer) 386 { 387 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer)); 388 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 389 usbd_free_buffer(xfer); 390 #if defined(DIAGNOSTIC) 391 if (callout_pending(&xfer->timeout_handle)) { 392 callout_stop(&xfer->timeout_handle); 393 printf("usbd_free_xfer: timout_handle pending"); 394 } 395 #endif 396 xfer->device->bus->methods->freex(xfer->device->bus, xfer); 397 return (USBD_NORMAL_COMPLETION); 398 } 399 400 void 401 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 402 usbd_private_handle priv, void *buffer, u_int32_t length, 403 u_int16_t flags, u_int32_t timeout, 404 usbd_callback callback) 405 { 406 xfer->pipe = pipe; 407 xfer->priv = priv; 408 xfer->buffer = buffer; 409 xfer->length = length; 410 xfer->actlen = 0; 411 xfer->flags = flags; 412 xfer->timeout = timeout; 413 xfer->status = USBD_NOT_STARTED; 414 xfer->callback = callback; 415 xfer->rqflags &= ~URQ_REQUEST; 416 xfer->nframes = 0; 417 } 418 419 void 420 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev, 421 usbd_private_handle priv, u_int32_t timeout, 422 usb_device_request_t *req, void *buffer, 423 u_int32_t length, u_int16_t flags, 424 usbd_callback callback) 425 { 426 xfer->pipe = dev->default_pipe; 427 xfer->priv = priv; 428 xfer->buffer = buffer; 429 xfer->length = length; 430 xfer->actlen = 0; 431 xfer->flags = flags; 432 xfer->timeout = timeout; 433 xfer->status = USBD_NOT_STARTED; 434 xfer->callback = callback; 435 xfer->request = *req; 436 xfer->rqflags |= URQ_REQUEST; 437 xfer->nframes = 0; 438 } 439 440 void 441 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 442 usbd_private_handle priv, u_int16_t *frlengths, 443 u_int32_t nframes, u_int16_t flags, usbd_callback callback) 444 { 445 xfer->pipe = pipe; 446 xfer->priv = priv; 447 xfer->buffer = 0; 448 xfer->length = 0; 449 xfer->actlen = 0; 450 xfer->flags = flags; 451 xfer->timeout = USBD_NO_TIMEOUT; 452 xfer->status = USBD_NOT_STARTED; 453 xfer->callback = callback; 454 xfer->rqflags &= ~URQ_REQUEST; 455 xfer->frlengths = frlengths; 456 xfer->nframes = nframes; 457 } 458 459 void 460 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv, 461 void **buffer, u_int32_t *count, usbd_status *status) 462 { 463 if (priv != NULL) 464 *priv = xfer->priv; 465 if (buffer != NULL) 466 *buffer = xfer->buffer; 467 if (count != NULL) 468 *count = xfer->actlen; 469 if (status != NULL) 470 *status = xfer->status; 471 } 472 473 usb_config_descriptor_t * 474 usbd_get_config_descriptor(usbd_device_handle dev) 475 { 476 #ifdef DIAGNOSTIC 477 if (dev == NULL) { 478 printf("usbd_get_config_descriptor: dev == NULL\n"); 479 return (NULL); 480 } 481 #endif 482 return (dev->cdesc); 483 } 484 485 usb_interface_descriptor_t * 486 usbd_get_interface_descriptor(usbd_interface_handle iface) 487 { 488 #ifdef DIAGNOSTIC 489 if (iface == NULL) { 490 printf("usbd_get_interface_descriptor: dev == NULL\n"); 491 return (NULL); 492 } 493 #endif 494 return (iface->idesc); 495 } 496 497 usb_device_descriptor_t * 498 usbd_get_device_descriptor(usbd_device_handle dev) 499 { 500 return (&dev->ddesc); 501 } 502 503 usb_endpoint_descriptor_t * 504 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index) 505 { 506 if (index >= iface->idesc->bNumEndpoints) 507 return (0); 508 return (iface->endpoints[index].edesc); 509 } 510 511 /* Some drivers may wish to abort requests on the default pipe, * 512 * but there is no mechanism for getting a handle on it. */ 513 usbd_status 514 usbd_abort_default_pipe(struct usbd_device *device) 515 { 516 517 return usbd_abort_pipe(device->default_pipe); 518 } 519 520 usbd_status 521 usbd_abort_pipe(usbd_pipe_handle pipe) 522 { 523 usbd_status err; 524 int s; 525 usbd_xfer_handle intrxfer = pipe->intrxfer; 526 527 #ifdef DIAGNOSTIC 528 if (pipe == NULL) { 529 printf("usbd_abort_pipe: pipe==NULL\n"); 530 return (USBD_NORMAL_COMPLETION); 531 } 532 #endif 533 s = splusb(); 534 err = usbd_ar_pipe(pipe); 535 splx(s); 536 if (pipe->intrxfer != intrxfer) 537 usbd_free_xfer(intrxfer); 538 return (err); 539 } 540 541 usbd_status 542 usbd_clear_endpoint_stall(usbd_pipe_handle pipe) 543 { 544 usbd_device_handle dev = pipe->device; 545 usb_device_request_t req; 546 usbd_status err; 547 548 DPRINTFN(8, ("usbd_clear_endpoint_stall\n")); 549 550 /* 551 * Clearing en endpoint stall resets the endpoint toggle, so 552 * do the same to the HC toggle. 553 */ 554 pipe->methods->cleartoggle(pipe); 555 556 req.bmRequestType = UT_WRITE_ENDPOINT; 557 req.bRequest = UR_CLEAR_FEATURE; 558 USETW(req.wValue, UF_ENDPOINT_HALT); 559 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress); 560 USETW(req.wLength, 0); 561 err = usbd_do_request(dev, &req, 0); 562 #if 0 563 XXX should we do this? 564 if (!err) { 565 pipe->state = USBD_PIPE_ACTIVE; 566 /* XXX activate pipe */ 567 } 568 #endif 569 return (err); 570 } 571 572 usbd_status 573 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe) 574 { 575 usbd_device_handle dev = pipe->device; 576 usb_device_request_t req; 577 usbd_status err; 578 579 pipe->methods->cleartoggle(pipe); 580 581 req.bmRequestType = UT_WRITE_ENDPOINT; 582 req.bRequest = UR_CLEAR_FEATURE; 583 USETW(req.wValue, UF_ENDPOINT_HALT); 584 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress); 585 USETW(req.wLength, 0); 586 err = usbd_do_request_async(dev, &req, 0); 587 return (err); 588 } 589 590 void 591 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe) 592 { 593 pipe->methods->cleartoggle(pipe); 594 } 595 596 usbd_status 597 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count) 598 { 599 #ifdef DIAGNOSTIC 600 if (iface == NULL || iface->idesc == NULL) { 601 printf("usbd_endpoint_count: NULL pointer\n"); 602 return (USBD_INVAL); 603 } 604 #endif 605 *count = iface->idesc->bNumEndpoints; 606 return (USBD_NORMAL_COMPLETION); 607 } 608 609 usbd_status 610 usbd_interface_count(usbd_device_handle dev, u_int8_t *count) 611 { 612 if (dev->cdesc == NULL) 613 return (USBD_NOT_CONFIGURED); 614 *count = dev->cdesc->bNumInterface; 615 return (USBD_NORMAL_COMPLETION); 616 } 617 618 void 619 usbd_interface2device_handle(usbd_interface_handle iface, 620 usbd_device_handle *dev) 621 { 622 *dev = iface->device; 623 } 624 625 usbd_status 626 usbd_device2interface_handle(usbd_device_handle dev, 627 u_int8_t ifaceno, usbd_interface_handle *iface) 628 { 629 if (dev->cdesc == NULL) 630 return (USBD_NOT_CONFIGURED); 631 if (ifaceno >= dev->cdesc->bNumInterface) 632 return (USBD_INVAL); 633 *iface = &dev->ifaces[ifaceno]; 634 return (USBD_NORMAL_COMPLETION); 635 } 636 637 usbd_device_handle 638 usbd_pipe2device_handle(usbd_pipe_handle pipe) 639 { 640 return (pipe->device); 641 } 642 643 /* XXXX use altno */ 644 usbd_status 645 usbd_set_interface(usbd_interface_handle iface, int altidx) 646 { 647 usb_device_request_t req; 648 usbd_status err; 649 void *endpoints; 650 651 if (LIST_FIRST(&iface->pipes) != 0) 652 return (USBD_IN_USE); 653 654 endpoints = iface->endpoints; 655 err = usbd_fill_iface_data(iface->device, iface->index, altidx); 656 if (err) 657 return (err); 658 659 /* new setting works, we can free old endpoints */ 660 if (endpoints != NULL) 661 free(endpoints, M_USB); 662 663 #ifdef DIAGNOSTIC 664 if (iface->idesc == NULL) { 665 printf("usbd_set_interface: NULL pointer\n"); 666 return (USBD_INVAL); 667 } 668 #endif 669 670 req.bmRequestType = UT_WRITE_INTERFACE; 671 req.bRequest = UR_SET_INTERFACE; 672 USETW(req.wValue, iface->idesc->bAlternateSetting); 673 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 674 USETW(req.wLength, 0); 675 return (usbd_do_request(iface->device, &req, 0)); 676 } 677 678 int 679 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno) 680 { 681 char *p = (char *)cdesc; 682 char *end = p + UGETW(cdesc->wTotalLength); 683 usb_interface_descriptor_t *d; 684 int n; 685 686 for (n = 0; p < end; p += d->bLength) { 687 d = (usb_interface_descriptor_t *)p; 688 if (p + d->bLength <= end && 689 d->bDescriptorType == UDESC_INTERFACE && 690 d->bInterfaceNumber == ifaceno) 691 n++; 692 } 693 return (n); 694 } 695 696 int 697 usbd_get_interface_altindex(usbd_interface_handle iface) 698 { 699 return (iface->altindex); 700 } 701 702 usbd_status 703 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface) 704 { 705 usb_device_request_t req; 706 707 req.bmRequestType = UT_READ_INTERFACE; 708 req.bRequest = UR_GET_INTERFACE; 709 USETW(req.wValue, 0); 710 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 711 USETW(req.wLength, 1); 712 return (usbd_do_request(iface->device, &req, aiface)); 713 } 714 715 /*** Internal routines ***/ 716 717 /* Dequeue all pipe operations, called at splusb(). */ 718 Static usbd_status 719 usbd_ar_pipe(usbd_pipe_handle pipe) 720 { 721 usbd_xfer_handle xfer; 722 723 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe)); 724 #ifdef USB_DEBUG 725 if (usbdebug > 5) 726 usbd_dump_queue(pipe); 727 #endif 728 pipe->repeat = 0; 729 pipe->aborting = 1; 730 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) { 731 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n", 732 pipe, xfer, pipe->methods)); 733 /* Make the HC abort it (and invoke the callback). */ 734 pipe->methods->abort(xfer); 735 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */ 736 } 737 pipe->aborting = 0; 738 return (USBD_NORMAL_COMPLETION); 739 } 740 741 /* Called at splusb() */ 742 void 743 usb_transfer_complete(usbd_xfer_handle xfer) 744 { 745 usbd_pipe_handle pipe = xfer->pipe; 746 usb_dma_t *dmap = &xfer->dmabuf; 747 int sync = xfer->flags & USBD_SYNCHRONOUS; 748 int erred = xfer->status == USBD_CANCELLED || 749 xfer->status == USBD_TIMEOUT; 750 int repeat, polling; 751 752 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d " 753 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen)); 754 755 KASSERT(KERNEL_LOCKED_P()); 756 757 #ifdef DIAGNOSTIC 758 if (xfer->busy_free != XFER_ONQU) { 759 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n", 760 xfer, xfer->busy_free); 761 } 762 #endif 763 764 #ifdef DIAGNOSTIC 765 if (pipe == NULL) { 766 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer); 767 return; 768 } 769 #endif 770 repeat = pipe->repeat; 771 polling = pipe->device->bus->use_polling; 772 /* XXXX */ 773 if (polling) 774 pipe->running = 0; 775 776 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 && 777 usbd_xfer_isread(xfer)) { 778 #ifdef DIAGNOSTIC 779 if (xfer->actlen > xfer->length) { 780 printf("usb_transfer_complete: actlen > len %d > %d\n", 781 xfer->actlen, xfer->length); 782 xfer->actlen = xfer->length; 783 } 784 #endif 785 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen); 786 } 787 788 /* if we allocated the buffer in usbd_transfer() we free it here. */ 789 if (xfer->rqflags & URQ_AUTO_DMABUF) { 790 if (!repeat) { 791 struct usbd_bus *bus = pipe->device->bus; 792 bus->methods->freem(bus, dmap); 793 xfer->rqflags &= ~URQ_AUTO_DMABUF; 794 } 795 } 796 797 if (!repeat) { 798 /* Remove request from queue. */ 799 #ifdef DIAGNOSTIC 800 if (xfer != SIMPLEQ_FIRST(&pipe->queue)) 801 printf("usb_transfer_complete: bad dequeue %p != %p\n", 802 xfer, SIMPLEQ_FIRST(&pipe->queue)); 803 xfer->busy_free = XFER_BUSY; 804 #endif 805 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next); 806 } 807 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n", 808 repeat, SIMPLEQ_FIRST(&pipe->queue))); 809 810 /* Count completed transfers. */ 811 ++pipe->device->bus->stats.uds_requests 812 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE]; 813 814 xfer->done = 1; 815 if (!xfer->status && xfer->actlen < xfer->length && 816 !(xfer->flags & USBD_SHORT_XFER_OK)) { 817 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n", 818 xfer->actlen, xfer->length)); 819 xfer->status = USBD_SHORT_XFER; 820 } 821 822 if (repeat) { 823 if (xfer->callback) 824 xfer->callback(xfer, xfer->priv, xfer->status); 825 pipe->methods->done(xfer); 826 } else { 827 pipe->methods->done(xfer); 828 if (xfer->callback) 829 xfer->callback(xfer, xfer->priv, xfer->status); 830 } 831 832 if (sync && !polling) 833 wakeup(xfer); 834 835 if (!repeat) { 836 /* XXX should we stop the queue on all errors? */ 837 if (erred && pipe->iface != NULL) /* not control pipe */ 838 pipe->running = 0; 839 else 840 usbd_start_next(pipe); 841 } 842 } 843 844 usbd_status 845 usb_insert_transfer(usbd_xfer_handle xfer) 846 { 847 usbd_pipe_handle pipe = xfer->pipe; 848 usbd_status err; 849 int s; 850 851 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n", 852 pipe, pipe->running, xfer->timeout)); 853 854 KASSERT(KERNEL_LOCKED_P()); 855 856 #ifdef DIAGNOSTIC 857 if (xfer->busy_free != XFER_BUSY) { 858 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n", 859 xfer, xfer->busy_free); 860 return (USBD_INVAL); 861 } 862 xfer->busy_free = XFER_ONQU; 863 #endif 864 s = splusb(); 865 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next); 866 if (pipe->running) 867 err = USBD_IN_PROGRESS; 868 else { 869 pipe->running = 1; 870 err = USBD_NORMAL_COMPLETION; 871 } 872 splx(s); 873 return (err); 874 } 875 876 /* Called at splusb() */ 877 void 878 usbd_start_next(usbd_pipe_handle pipe) 879 { 880 usbd_xfer_handle xfer; 881 usbd_status err; 882 883 #ifdef DIAGNOSTIC 884 if (pipe == NULL) { 885 printf("usbd_start_next: pipe == NULL\n"); 886 return; 887 } 888 if (pipe->methods == NULL || pipe->methods->start == NULL) { 889 printf("usbd_start_next: pipe=%p no start method\n", pipe); 890 return; 891 } 892 #endif 893 894 /* Get next request in queue. */ 895 xfer = SIMPLEQ_FIRST(&pipe->queue); 896 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer)); 897 if (xfer == NULL) { 898 pipe->running = 0; 899 } else { 900 err = pipe->methods->start(xfer); 901 if (err != USBD_IN_PROGRESS) { 902 printf("usbd_start_next: error=%d\n", err); 903 pipe->running = 0; 904 /* XXX do what? */ 905 } 906 } 907 } 908 909 usbd_status 910 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data) 911 { 912 return (usbd_do_request_flags(dev, req, data, 0, 0, 913 USBD_DEFAULT_TIMEOUT)); 914 } 915 916 usbd_status 917 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req, 918 void *data, u_int16_t flags, int *actlen, u_int32_t timo) 919 { 920 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req, 921 data, flags, actlen, timo)); 922 } 923 924 usbd_status 925 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe, 926 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen, 927 u_int32_t timeout) 928 { 929 usbd_xfer_handle xfer; 930 usbd_status err; 931 932 #ifdef DIAGNOSTIC 933 if (cpu_intr_p() || cpu_softintr_p()) { 934 printf("usbd_do_request: not in process context\n"); 935 return (USBD_INVAL); 936 } 937 #endif 938 939 xfer = usbd_alloc_xfer(dev); 940 if (xfer == NULL) 941 return (USBD_NOMEM); 942 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, 943 data, UGETW(req->wLength), flags, 0); 944 xfer->pipe = pipe; 945 err = usbd_sync_transfer(xfer); 946 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 947 if (xfer->actlen > xfer->length) { 948 DPRINTF(("%s: overrun addr=%d type=0x%02x req=0x" 949 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n", 950 __func__, dev->address, xfer->request.bmRequestType, 951 xfer->request.bRequest, UGETW(xfer->request.wValue), 952 UGETW(xfer->request.wIndex), 953 UGETW(xfer->request.wLength), 954 xfer->length, xfer->actlen)); 955 } 956 #endif 957 if (actlen != NULL) 958 *actlen = xfer->actlen; 959 if (err == USBD_STALLED) { 960 /* 961 * The control endpoint has stalled. Control endpoints 962 * should not halt, but some may do so anyway so clear 963 * any halt condition. 964 */ 965 usb_device_request_t treq; 966 usb_status_t status; 967 u_int16_t s; 968 usbd_status nerr; 969 970 treq.bmRequestType = UT_READ_ENDPOINT; 971 treq.bRequest = UR_GET_STATUS; 972 USETW(treq.wValue, 0); 973 USETW(treq.wIndex, 0); 974 USETW(treq.wLength, sizeof(usb_status_t)); 975 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 976 &treq, &status,sizeof(usb_status_t), 977 0, 0); 978 nerr = usbd_sync_transfer(xfer); 979 if (nerr) 980 goto bad; 981 s = UGETW(status.wStatus); 982 DPRINTF(("usbd_do_request: status = 0x%04x\n", s)); 983 if (!(s & UES_HALT)) 984 goto bad; 985 treq.bmRequestType = UT_WRITE_ENDPOINT; 986 treq.bRequest = UR_CLEAR_FEATURE; 987 USETW(treq.wValue, UF_ENDPOINT_HALT); 988 USETW(treq.wIndex, 0); 989 USETW(treq.wLength, 0); 990 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 991 &treq, &status, 0, 0, 0); 992 nerr = usbd_sync_transfer(xfer); 993 if (nerr) 994 goto bad; 995 } 996 997 bad: 998 if (err) { 999 DPRINTF(("%s: returning err=%s\n", __func__, usbd_errstr(err))); 1000 } 1001 usbd_free_xfer(xfer); 1002 return (err); 1003 } 1004 1005 void 1006 usbd_do_request_async_cb(usbd_xfer_handle xfer, 1007 usbd_private_handle priv, usbd_status status) 1008 { 1009 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 1010 if (xfer->actlen > xfer->length) { 1011 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x" 1012 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n", 1013 xfer->pipe->device->address, 1014 xfer->request.bmRequestType, 1015 xfer->request.bRequest, UGETW(xfer->request.wValue), 1016 UGETW(xfer->request.wIndex), 1017 UGETW(xfer->request.wLength), 1018 xfer->length, xfer->actlen)); 1019 } 1020 #endif 1021 usbd_free_xfer(xfer); 1022 } 1023 1024 /* 1025 * Execute a request without waiting for completion. 1026 * Can be used from interrupt context. 1027 */ 1028 usbd_status 1029 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req, 1030 void *data) 1031 { 1032 usbd_xfer_handle xfer; 1033 usbd_status err; 1034 1035 xfer = usbd_alloc_xfer(dev); 1036 if (xfer == NULL) 1037 return (USBD_NOMEM); 1038 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req, 1039 data, UGETW(req->wLength), 0, usbd_do_request_async_cb); 1040 err = usbd_transfer(xfer); 1041 if (err != USBD_IN_PROGRESS) { 1042 usbd_free_xfer(xfer); 1043 return (err); 1044 } 1045 return (USBD_NORMAL_COMPLETION); 1046 } 1047 1048 const struct usbd_quirks * 1049 usbd_get_quirks(usbd_device_handle dev) 1050 { 1051 #ifdef DIAGNOSTIC 1052 if (dev == NULL) { 1053 printf("usbd_get_quirks: dev == NULL\n"); 1054 return 0; 1055 } 1056 #endif 1057 return (dev->quirks); 1058 } 1059 1060 /* XXX do periodic free() of free list */ 1061 1062 /* 1063 * Called from keyboard driver when in polling mode. 1064 */ 1065 void 1066 usbd_dopoll(usbd_interface_handle iface) 1067 { 1068 iface->device->bus->methods->do_poll(iface->device->bus); 1069 } 1070 1071 void 1072 usbd_set_polling(usbd_device_handle dev, int on) 1073 { 1074 if (on) 1075 dev->bus->use_polling++; 1076 else 1077 dev->bus->use_polling--; 1078 1079 /* Kick the host controller when switching modes */ 1080 dev->bus->methods->soft_intr(dev->bus); 1081 } 1082 1083 1084 usb_endpoint_descriptor_t * 1085 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address) 1086 { 1087 struct usbd_endpoint *ep; 1088 int i; 1089 1090 for (i = 0; i < iface->idesc->bNumEndpoints; i++) { 1091 ep = &iface->endpoints[i]; 1092 if (ep->edesc->bEndpointAddress == address) 1093 return (iface->endpoints[i].edesc); 1094 } 1095 return (0); 1096 } 1097 1098 /* 1099 * usbd_ratecheck() can limit the number of error messages that occurs. 1100 * When a device is unplugged it may take up to 0.25s for the hub driver 1101 * to notice it. If the driver continuosly tries to do I/O operations 1102 * this can generate a large number of messages. 1103 */ 1104 int 1105 usbd_ratecheck(struct timeval *last) 1106 { 1107 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/ 1108 1109 return (ratecheck(last, &errinterval)); 1110 } 1111 1112 /* 1113 * Search for a vendor/product pair in an array. The item size is 1114 * given as an argument. 1115 */ 1116 const struct usb_devno * 1117 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz, 1118 u_int16_t vendor, u_int16_t product) 1119 { 1120 while (nentries-- > 0) { 1121 u_int16_t tproduct = tbl->ud_product; 1122 if (tbl->ud_vendor == vendor && 1123 (tproduct == product || tproduct == USB_PRODUCT_ANY)) 1124 return (tbl); 1125 tbl = (const struct usb_devno *)((const char *)tbl + sz); 1126 } 1127 return (NULL); 1128 } 1129 1130 1131 void 1132 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter) 1133 { 1134 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev); 1135 1136 iter->cur = (const uByte *)cd; 1137 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength); 1138 } 1139 1140 const usb_descriptor_t * 1141 usb_desc_iter_next(usbd_desc_iter_t *iter) 1142 { 1143 const usb_descriptor_t *desc; 1144 1145 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) { 1146 if (iter->cur != iter->end) 1147 printf("usb_desc_iter_next: bad descriptor\n"); 1148 return NULL; 1149 } 1150 desc = (const usb_descriptor_t *)iter->cur; 1151 if (desc->bLength == 0) { 1152 printf("usb_desc_iter_next: descriptor length = 0\n"); 1153 return NULL; 1154 } 1155 iter->cur += desc->bLength; 1156 if (iter->cur > iter->end) { 1157 printf("usb_desc_iter_next: descriptor length too large\n"); 1158 return NULL; 1159 } 1160 return desc; 1161 } 1162 1163 usbd_status 1164 usbd_get_string(usbd_device_handle dev, int si, char *buf) 1165 { 1166 return usbd_get_string0(dev, si, buf, 1); 1167 } 1168 1169 usbd_status 1170 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode) 1171 { 1172 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE; 1173 usb_string_descriptor_t us; 1174 char *s; 1175 int i, n; 1176 u_int16_t c; 1177 usbd_status err; 1178 int size; 1179 1180 buf[0] = '\0'; 1181 if (si == 0) 1182 return (USBD_INVAL); 1183 if (dev->quirks->uq_flags & UQ_NO_STRINGS) 1184 return (USBD_STALLED); 1185 if (dev->langid == USBD_NOLANG) { 1186 /* Set up default language */ 1187 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us, 1188 &size); 1189 if (err || size < 4) { 1190 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n")); 1191 dev->langid = 0; /* Well, just pick something then */ 1192 } else { 1193 /* Pick the first language as the default. */ 1194 dev->langid = UGETW(us.bString[0]); 1195 } 1196 } 1197 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size); 1198 if (err) 1199 return (err); 1200 s = buf; 1201 n = size / 2 - 1; 1202 if (unicode) { 1203 for (i = 0; i < n; i++) { 1204 c = UGETW(us.bString[i]); 1205 if (swap) 1206 c = (c >> 8) | (c << 8); 1207 s += wput_utf8(s, 3, c); 1208 } 1209 *s++ = 0; 1210 } 1211 #ifdef COMPAT_30 1212 else { 1213 for (i = 0; i < n; i++) { 1214 c = UGETW(us.bString[i]); 1215 if (swap) 1216 c = (c >> 8) | (c << 8); 1217 *s++ = (c < 0x80) ? c : '?'; 1218 } 1219 *s++ = 0; 1220 } 1221 #endif 1222 return (USBD_NORMAL_COMPLETION); 1223 } 1224