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