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