1 /* $NetBSD: usbdi.c,v 1.123 2008/05/26 18:00:33 drochner 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.123 2008/05/26 18:00:33 drochner Exp $"); 36 37 #include "opt_compat_netbsd.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 46 #include <sys/bus.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) logprintf x 60 #define DPRINTFN(n,x) if (usbdebug>(n)) logprintf 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 #ifdef USB_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 #ifdef USB_DEBUG 263 if (usbdebug > 5) 264 usbd_dump_queue(pipe); 265 #endif 266 xfer->done = 0; 267 268 if (pipe->aborting) 269 return (USBD_CANCELLED); 270 271 size = xfer->length; 272 /* If there is no buffer, allocate one. */ 273 if (!(xfer->rqflags & URQ_DEV_DMABUF) && size != 0) { 274 struct usbd_bus *bus = pipe->device->bus; 275 276 #ifdef DIAGNOSTIC 277 if (xfer->rqflags & URQ_AUTO_DMABUF) 278 printf("usbd_transfer: has old buffer!\n"); 279 #endif 280 err = bus->methods->allocm(bus, dmap, size); 281 if (err) 282 return (err); 283 xfer->rqflags |= URQ_AUTO_DMABUF; 284 } 285 286 flags = xfer->flags; 287 288 /* Copy data if going out. */ 289 if (!(flags & USBD_NO_COPY) && size != 0 && !usbd_xfer_isread(xfer)) 290 memcpy(KERNADDR(dmap, 0), xfer->buffer, size); 291 292 /* xfer is not valid after the transfer method unless synchronous */ 293 err = pipe->methods->transfer(xfer); 294 295 if (err != USBD_IN_PROGRESS && err) { 296 /* The transfer has not been queued, so free buffer. */ 297 if (xfer->rqflags & URQ_AUTO_DMABUF) { 298 struct usbd_bus *bus = pipe->device->bus; 299 300 bus->methods->freem(bus, &xfer->dmabuf); 301 xfer->rqflags &= ~URQ_AUTO_DMABUF; 302 } 303 } 304 305 if (!(flags & USBD_SYNCHRONOUS)) 306 return (err); 307 308 /* Sync transfer, wait for completion. */ 309 if (err != USBD_IN_PROGRESS) 310 return (err); 311 s = splusb(); 312 if (!xfer->done) { 313 if (pipe->device->bus->use_polling) 314 panic("usbd_transfer: not done"); 315 tsleep(xfer, PRIBIO, "usbsyn", 0); 316 } 317 splx(s); 318 return (xfer->status); 319 } 320 321 /* Like usbd_transfer(), but waits for completion. */ 322 usbd_status 323 usbd_sync_transfer(usbd_xfer_handle xfer) 324 { 325 xfer->flags |= USBD_SYNCHRONOUS; 326 return (usbd_transfer(xfer)); 327 } 328 329 void * 330 usbd_alloc_buffer(usbd_xfer_handle xfer, u_int32_t size) 331 { 332 struct usbd_bus *bus = xfer->device->bus; 333 usbd_status err; 334 335 #ifdef DIAGNOSTIC 336 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 337 printf("usbd_alloc_buffer: xfer already has a buffer\n"); 338 #endif 339 err = bus->methods->allocm(bus, &xfer->dmabuf, size); 340 if (err) 341 return (NULL); 342 xfer->rqflags |= URQ_DEV_DMABUF; 343 return (KERNADDR(&xfer->dmabuf, 0)); 344 } 345 346 void 347 usbd_free_buffer(usbd_xfer_handle xfer) 348 { 349 #ifdef DIAGNOSTIC 350 if (!(xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF))) { 351 printf("usbd_free_buffer: no buffer\n"); 352 return; 353 } 354 #endif 355 xfer->rqflags &= ~(URQ_DEV_DMABUF | URQ_AUTO_DMABUF); 356 xfer->device->bus->methods->freem(xfer->device->bus, &xfer->dmabuf); 357 } 358 359 void * 360 usbd_get_buffer(usbd_xfer_handle xfer) 361 { 362 if (!(xfer->rqflags & URQ_DEV_DMABUF)) 363 return (0); 364 return (KERNADDR(&xfer->dmabuf, 0)); 365 } 366 367 usbd_xfer_handle 368 usbd_alloc_xfer(usbd_device_handle dev) 369 { 370 usbd_xfer_handle xfer; 371 372 xfer = dev->bus->methods->allocx(dev->bus); 373 if (xfer == NULL) 374 return (NULL); 375 xfer->device = dev; 376 usb_callout_init(xfer->timeout_handle); 377 DPRINTFN(5,("usbd_alloc_xfer() = %p\n", xfer)); 378 return (xfer); 379 } 380 381 usbd_status 382 usbd_free_xfer(usbd_xfer_handle xfer) 383 { 384 DPRINTFN(5,("usbd_free_xfer: %p\n", xfer)); 385 if (xfer->rqflags & (URQ_DEV_DMABUF | URQ_AUTO_DMABUF)) 386 usbd_free_buffer(xfer); 387 #if defined(DIAGNOSTIC) 388 if (callout_pending(&xfer->timeout_handle)) { 389 callout_stop(&xfer->timeout_handle); 390 printf("usbd_free_xfer: timout_handle pending"); 391 } 392 #endif 393 xfer->device->bus->methods->freex(xfer->device->bus, xfer); 394 return (USBD_NORMAL_COMPLETION); 395 } 396 397 void 398 usbd_setup_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 399 usbd_private_handle priv, void *buffer, u_int32_t length, 400 u_int16_t flags, u_int32_t timeout, 401 usbd_callback callback) 402 { 403 xfer->pipe = pipe; 404 xfer->priv = priv; 405 xfer->buffer = buffer; 406 xfer->length = length; 407 xfer->actlen = 0; 408 xfer->flags = flags; 409 xfer->timeout = timeout; 410 xfer->status = USBD_NOT_STARTED; 411 xfer->callback = callback; 412 xfer->rqflags &= ~URQ_REQUEST; 413 xfer->nframes = 0; 414 } 415 416 void 417 usbd_setup_default_xfer(usbd_xfer_handle xfer, usbd_device_handle dev, 418 usbd_private_handle priv, u_int32_t timeout, 419 usb_device_request_t *req, void *buffer, 420 u_int32_t length, u_int16_t flags, 421 usbd_callback callback) 422 { 423 xfer->pipe = dev->default_pipe; 424 xfer->priv = priv; 425 xfer->buffer = buffer; 426 xfer->length = length; 427 xfer->actlen = 0; 428 xfer->flags = flags; 429 xfer->timeout = timeout; 430 xfer->status = USBD_NOT_STARTED; 431 xfer->callback = callback; 432 xfer->request = *req; 433 xfer->rqflags |= URQ_REQUEST; 434 xfer->nframes = 0; 435 } 436 437 void 438 usbd_setup_isoc_xfer(usbd_xfer_handle xfer, usbd_pipe_handle pipe, 439 usbd_private_handle priv, u_int16_t *frlengths, 440 u_int32_t nframes, u_int16_t flags, usbd_callback callback) 441 { 442 xfer->pipe = pipe; 443 xfer->priv = priv; 444 xfer->buffer = 0; 445 xfer->length = 0; 446 xfer->actlen = 0; 447 xfer->flags = flags; 448 xfer->timeout = USBD_NO_TIMEOUT; 449 xfer->status = USBD_NOT_STARTED; 450 xfer->callback = callback; 451 xfer->rqflags &= ~URQ_REQUEST; 452 xfer->frlengths = frlengths; 453 xfer->nframes = nframes; 454 } 455 456 void 457 usbd_get_xfer_status(usbd_xfer_handle xfer, usbd_private_handle *priv, 458 void **buffer, u_int32_t *count, usbd_status *status) 459 { 460 if (priv != NULL) 461 *priv = xfer->priv; 462 if (buffer != NULL) 463 *buffer = xfer->buffer; 464 if (count != NULL) 465 *count = xfer->actlen; 466 if (status != NULL) 467 *status = xfer->status; 468 } 469 470 usb_config_descriptor_t * 471 usbd_get_config_descriptor(usbd_device_handle dev) 472 { 473 #ifdef DIAGNOSTIC 474 if (dev == NULL) { 475 printf("usbd_get_config_descriptor: dev == NULL\n"); 476 return (NULL); 477 } 478 #endif 479 return (dev->cdesc); 480 } 481 482 usb_interface_descriptor_t * 483 usbd_get_interface_descriptor(usbd_interface_handle iface) 484 { 485 #ifdef DIAGNOSTIC 486 if (iface == NULL) { 487 printf("usbd_get_interface_descriptor: dev == NULL\n"); 488 return (NULL); 489 } 490 #endif 491 return (iface->idesc); 492 } 493 494 usb_device_descriptor_t * 495 usbd_get_device_descriptor(usbd_device_handle dev) 496 { 497 return (&dev->ddesc); 498 } 499 500 usb_endpoint_descriptor_t * 501 usbd_interface2endpoint_descriptor(usbd_interface_handle iface, u_int8_t index) 502 { 503 if (index >= iface->idesc->bNumEndpoints) 504 return (0); 505 return (iface->endpoints[index].edesc); 506 } 507 508 usbd_status 509 usbd_abort_pipe(usbd_pipe_handle pipe) 510 { 511 usbd_status err; 512 int s; 513 514 #ifdef DIAGNOSTIC 515 if (pipe == NULL) { 516 printf("usbd_close_pipe: pipe==NULL\n"); 517 return (USBD_NORMAL_COMPLETION); 518 } 519 #endif 520 s = splusb(); 521 err = usbd_ar_pipe(pipe); 522 splx(s); 523 return (err); 524 } 525 526 usbd_status 527 usbd_clear_endpoint_stall(usbd_pipe_handle pipe) 528 { 529 usbd_device_handle dev = pipe->device; 530 usb_device_request_t req; 531 usbd_status err; 532 533 DPRINTFN(8, ("usbd_clear_endpoint_stall\n")); 534 535 /* 536 * Clearing en endpoint stall resets the endpoint toggle, so 537 * do the same to the HC toggle. 538 */ 539 pipe->methods->cleartoggle(pipe); 540 541 req.bmRequestType = UT_WRITE_ENDPOINT; 542 req.bRequest = UR_CLEAR_FEATURE; 543 USETW(req.wValue, UF_ENDPOINT_HALT); 544 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress); 545 USETW(req.wLength, 0); 546 err = usbd_do_request(dev, &req, 0); 547 #if 0 548 XXX should we do this? 549 if (!err) { 550 pipe->state = USBD_PIPE_ACTIVE; 551 /* XXX activate pipe */ 552 } 553 #endif 554 return (err); 555 } 556 557 usbd_status 558 usbd_clear_endpoint_stall_async(usbd_pipe_handle pipe) 559 { 560 usbd_device_handle dev = pipe->device; 561 usb_device_request_t req; 562 usbd_status err; 563 564 pipe->methods->cleartoggle(pipe); 565 566 req.bmRequestType = UT_WRITE_ENDPOINT; 567 req.bRequest = UR_CLEAR_FEATURE; 568 USETW(req.wValue, UF_ENDPOINT_HALT); 569 USETW(req.wIndex, pipe->endpoint->edesc->bEndpointAddress); 570 USETW(req.wLength, 0); 571 err = usbd_do_request_async(dev, &req, 0); 572 return (err); 573 } 574 575 void 576 usbd_clear_endpoint_toggle(usbd_pipe_handle pipe) 577 { 578 pipe->methods->cleartoggle(pipe); 579 } 580 581 usbd_status 582 usbd_endpoint_count(usbd_interface_handle iface, u_int8_t *count) 583 { 584 #ifdef DIAGNOSTIC 585 if (iface == NULL || iface->idesc == NULL) { 586 printf("usbd_endpoint_count: NULL pointer\n"); 587 return (USBD_INVAL); 588 } 589 #endif 590 *count = iface->idesc->bNumEndpoints; 591 return (USBD_NORMAL_COMPLETION); 592 } 593 594 usbd_status 595 usbd_interface_count(usbd_device_handle dev, u_int8_t *count) 596 { 597 if (dev->cdesc == NULL) 598 return (USBD_NOT_CONFIGURED); 599 *count = dev->cdesc->bNumInterface; 600 return (USBD_NORMAL_COMPLETION); 601 } 602 603 void 604 usbd_interface2device_handle(usbd_interface_handle iface, 605 usbd_device_handle *dev) 606 { 607 *dev = iface->device; 608 } 609 610 usbd_status 611 usbd_device2interface_handle(usbd_device_handle dev, 612 u_int8_t ifaceno, usbd_interface_handle *iface) 613 { 614 if (dev->cdesc == NULL) 615 return (USBD_NOT_CONFIGURED); 616 if (ifaceno >= dev->cdesc->bNumInterface) 617 return (USBD_INVAL); 618 *iface = &dev->ifaces[ifaceno]; 619 return (USBD_NORMAL_COMPLETION); 620 } 621 622 usbd_device_handle 623 usbd_pipe2device_handle(usbd_pipe_handle pipe) 624 { 625 return (pipe->device); 626 } 627 628 /* XXXX use altno */ 629 usbd_status 630 usbd_set_interface(usbd_interface_handle iface, int altidx) 631 { 632 usb_device_request_t req; 633 usbd_status err; 634 void *endpoints; 635 636 if (LIST_FIRST(&iface->pipes) != 0) 637 return (USBD_IN_USE); 638 639 endpoints = iface->endpoints; 640 err = usbd_fill_iface_data(iface->device, iface->index, altidx); 641 if (err) 642 return (err); 643 644 /* new setting works, we can free old endpoints */ 645 if (endpoints != NULL) 646 free(endpoints, M_USB); 647 648 #ifdef DIAGNOSTIC 649 if (iface->idesc == NULL) { 650 printf("usbd_set_interface: NULL pointer\n"); 651 return (USBD_INVAL); 652 } 653 #endif 654 655 req.bmRequestType = UT_WRITE_INTERFACE; 656 req.bRequest = UR_SET_INTERFACE; 657 USETW(req.wValue, iface->idesc->bAlternateSetting); 658 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 659 USETW(req.wLength, 0); 660 return (usbd_do_request(iface->device, &req, 0)); 661 } 662 663 int 664 usbd_get_no_alts(usb_config_descriptor_t *cdesc, int ifaceno) 665 { 666 char *p = (char *)cdesc; 667 char *end = p + UGETW(cdesc->wTotalLength); 668 usb_interface_descriptor_t *d; 669 int n; 670 671 for (n = 0; p < end; p += d->bLength) { 672 d = (usb_interface_descriptor_t *)p; 673 if (p + d->bLength <= end && 674 d->bDescriptorType == UDESC_INTERFACE && 675 d->bInterfaceNumber == ifaceno) 676 n++; 677 } 678 return (n); 679 } 680 681 int 682 usbd_get_interface_altindex(usbd_interface_handle iface) 683 { 684 return (iface->altindex); 685 } 686 687 usbd_status 688 usbd_get_interface(usbd_interface_handle iface, u_int8_t *aiface) 689 { 690 usb_device_request_t req; 691 692 req.bmRequestType = UT_READ_INTERFACE; 693 req.bRequest = UR_GET_INTERFACE; 694 USETW(req.wValue, 0); 695 USETW(req.wIndex, iface->idesc->bInterfaceNumber); 696 USETW(req.wLength, 1); 697 return (usbd_do_request(iface->device, &req, aiface)); 698 } 699 700 /*** Internal routines ***/ 701 702 /* Dequeue all pipe operations, called at splusb(). */ 703 Static usbd_status 704 usbd_ar_pipe(usbd_pipe_handle pipe) 705 { 706 usbd_xfer_handle xfer; 707 708 SPLUSBCHECK; 709 710 DPRINTFN(2,("usbd_ar_pipe: pipe=%p\n", pipe)); 711 #ifdef USB_DEBUG 712 if (usbdebug > 5) 713 usbd_dump_queue(pipe); 714 #endif 715 pipe->repeat = 0; 716 pipe->aborting = 1; 717 while ((xfer = SIMPLEQ_FIRST(&pipe->queue)) != NULL) { 718 DPRINTFN(2,("usbd_ar_pipe: pipe=%p xfer=%p (methods=%p)\n", 719 pipe, xfer, pipe->methods)); 720 /* Make the HC abort it (and invoke the callback). */ 721 pipe->methods->abort(xfer); 722 /* XXX only for non-0 usbd_clear_endpoint_stall(pipe); */ 723 } 724 pipe->aborting = 0; 725 return (USBD_NORMAL_COMPLETION); 726 } 727 728 /* Called at splusb() */ 729 void 730 usb_transfer_complete(usbd_xfer_handle xfer) 731 { 732 usbd_pipe_handle pipe = xfer->pipe; 733 usb_dma_t *dmap = &xfer->dmabuf; 734 int sync = xfer->flags & USBD_SYNCHRONOUS; 735 int erred = xfer->status == USBD_CANCELLED || 736 xfer->status == USBD_TIMEOUT; 737 int repeat, polling; 738 739 SPLUSBCHECK; 740 741 DPRINTFN(5, ("usb_transfer_complete: pipe=%p xfer=%p status=%d " 742 "actlen=%d\n", pipe, xfer, xfer->status, xfer->actlen)); 743 #ifdef DIAGNOSTIC 744 if (xfer->busy_free != XFER_ONQU) { 745 printf("usb_transfer_complete: xfer=%p not busy 0x%08x\n", 746 xfer, xfer->busy_free); 747 } 748 #endif 749 750 #ifdef DIAGNOSTIC 751 if (pipe == NULL) { 752 printf("usbd_transfer_cb: pipe==0, xfer=%p\n", xfer); 753 return; 754 } 755 #endif 756 repeat = pipe->repeat; 757 polling = pipe->device->bus->use_polling; 758 /* XXXX */ 759 if (polling) 760 pipe->running = 0; 761 762 if (!(xfer->flags & USBD_NO_COPY) && xfer->actlen != 0 && 763 usbd_xfer_isread(xfer)) { 764 #ifdef DIAGNOSTIC 765 if (xfer->actlen > xfer->length) { 766 printf("usb_transfer_complete: actlen > len %d > %d\n", 767 xfer->actlen, xfer->length); 768 xfer->actlen = xfer->length; 769 } 770 #endif 771 memcpy(xfer->buffer, KERNADDR(dmap, 0), xfer->actlen); 772 } 773 774 /* if we allocated the buffer in usbd_transfer() we free it here. */ 775 if (xfer->rqflags & URQ_AUTO_DMABUF) { 776 if (!repeat) { 777 struct usbd_bus *bus = pipe->device->bus; 778 bus->methods->freem(bus, dmap); 779 xfer->rqflags &= ~URQ_AUTO_DMABUF; 780 } 781 } 782 783 if (!repeat) { 784 /* Remove request from queue. */ 785 #ifdef DIAGNOSTIC 786 if (xfer != SIMPLEQ_FIRST(&pipe->queue)) 787 printf("usb_transfer_complete: bad dequeue %p != %p\n", 788 xfer, SIMPLEQ_FIRST(&pipe->queue)); 789 xfer->busy_free = XFER_BUSY; 790 #endif 791 SIMPLEQ_REMOVE_HEAD(&pipe->queue, next); 792 } 793 DPRINTFN(5,("usb_transfer_complete: repeat=%d new head=%p\n", 794 repeat, SIMPLEQ_FIRST(&pipe->queue))); 795 796 /* Count completed transfers. */ 797 ++pipe->device->bus->stats.uds_requests 798 [pipe->endpoint->edesc->bmAttributes & UE_XFERTYPE]; 799 800 xfer->done = 1; 801 if (!xfer->status && xfer->actlen < xfer->length && 802 !(xfer->flags & USBD_SHORT_XFER_OK)) { 803 DPRINTFN(-1,("usbd_transfer_cb: short transfer %d<%d\n", 804 xfer->actlen, xfer->length)); 805 xfer->status = USBD_SHORT_XFER; 806 } 807 808 if (xfer->callback) 809 xfer->callback(xfer, xfer->priv, xfer->status); 810 811 #ifdef DIAGNOSTIC 812 if (pipe->methods->done != NULL) 813 pipe->methods->done(xfer); 814 else 815 printf("usb_transfer_complete: pipe->methods->done == NULL\n"); 816 #else 817 pipe->methods->done(xfer); 818 #endif 819 820 if (sync && !polling) 821 wakeup(xfer); 822 823 if (!repeat) { 824 /* XXX should we stop the queue on all errors? */ 825 if (erred && pipe->iface != NULL) /* not control pipe */ 826 pipe->running = 0; 827 else 828 usbd_start_next(pipe); 829 } 830 } 831 832 usbd_status 833 usb_insert_transfer(usbd_xfer_handle xfer) 834 { 835 usbd_pipe_handle pipe = xfer->pipe; 836 usbd_status err; 837 int s; 838 839 DPRINTFN(5,("usb_insert_transfer: pipe=%p running=%d timeout=%d\n", 840 pipe, pipe->running, xfer->timeout)); 841 #ifdef DIAGNOSTIC 842 if (xfer->busy_free != XFER_BUSY) { 843 printf("usb_insert_transfer: xfer=%p not busy 0x%08x\n", 844 xfer, xfer->busy_free); 845 return (USBD_INVAL); 846 } 847 xfer->busy_free = XFER_ONQU; 848 #endif 849 s = splusb(); 850 SIMPLEQ_INSERT_TAIL(&pipe->queue, xfer, next); 851 if (pipe->running) 852 err = USBD_IN_PROGRESS; 853 else { 854 pipe->running = 1; 855 err = USBD_NORMAL_COMPLETION; 856 } 857 splx(s); 858 return (err); 859 } 860 861 /* Called at splusb() */ 862 void 863 usbd_start_next(usbd_pipe_handle pipe) 864 { 865 usbd_xfer_handle xfer; 866 usbd_status err; 867 868 SPLUSBCHECK; 869 870 #ifdef DIAGNOSTIC 871 if (pipe == NULL) { 872 printf("usbd_start_next: pipe == NULL\n"); 873 return; 874 } 875 if (pipe->methods == NULL || pipe->methods->start == NULL) { 876 printf("usbd_start_next: pipe=%p no start method\n", pipe); 877 return; 878 } 879 #endif 880 881 /* Get next request in queue. */ 882 xfer = SIMPLEQ_FIRST(&pipe->queue); 883 DPRINTFN(5, ("usbd_start_next: pipe=%p, xfer=%p\n", pipe, xfer)); 884 if (xfer == NULL) { 885 pipe->running = 0; 886 } else { 887 err = pipe->methods->start(xfer); 888 if (err != USBD_IN_PROGRESS) { 889 printf("usbd_start_next: error=%d\n", err); 890 pipe->running = 0; 891 /* XXX do what? */ 892 } 893 } 894 } 895 896 usbd_status 897 usbd_do_request(usbd_device_handle dev, usb_device_request_t *req, void *data) 898 { 899 return (usbd_do_request_flags(dev, req, data, 0, 0, 900 USBD_DEFAULT_TIMEOUT)); 901 } 902 903 usbd_status 904 usbd_do_request_flags(usbd_device_handle dev, usb_device_request_t *req, 905 void *data, u_int16_t flags, int *actlen, u_int32_t timo) 906 { 907 return (usbd_do_request_flags_pipe(dev, dev->default_pipe, req, 908 data, flags, actlen, timo)); 909 } 910 911 usbd_status 912 usbd_do_request_flags_pipe(usbd_device_handle dev, usbd_pipe_handle pipe, 913 usb_device_request_t *req, void *data, u_int16_t flags, int *actlen, 914 u_int32_t timeout) 915 { 916 usbd_xfer_handle xfer; 917 usbd_status err; 918 919 #ifdef DIAGNOSTIC 920 if (dev->bus->intr_context) { 921 printf("usbd_do_request: not in process context\n"); 922 return (USBD_INVAL); 923 } 924 #endif 925 926 xfer = usbd_alloc_xfer(dev); 927 if (xfer == NULL) 928 return (USBD_NOMEM); 929 usbd_setup_default_xfer(xfer, dev, 0, timeout, req, 930 data, UGETW(req->wLength), flags, 0); 931 xfer->pipe = pipe; 932 err = usbd_sync_transfer(xfer); 933 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 934 if (xfer->actlen > xfer->length) { 935 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x" 936 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n", 937 dev->address, xfer->request.bmRequestType, 938 xfer->request.bRequest, UGETW(xfer->request.wValue), 939 UGETW(xfer->request.wIndex), 940 UGETW(xfer->request.wLength), 941 xfer->length, xfer->actlen)); 942 } 943 #endif 944 if (actlen != NULL) 945 *actlen = xfer->actlen; 946 if (err == USBD_STALLED) { 947 /* 948 * The control endpoint has stalled. Control endpoints 949 * should not halt, but some may do so anyway so clear 950 * any halt condition. 951 */ 952 usb_device_request_t treq; 953 usb_status_t status; 954 u_int16_t s; 955 usbd_status nerr; 956 957 treq.bmRequestType = UT_READ_ENDPOINT; 958 treq.bRequest = UR_GET_STATUS; 959 USETW(treq.wValue, 0); 960 USETW(treq.wIndex, 0); 961 USETW(treq.wLength, sizeof(usb_status_t)); 962 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 963 &treq, &status,sizeof(usb_status_t), 964 0, 0); 965 nerr = usbd_sync_transfer(xfer); 966 if (nerr) 967 goto bad; 968 s = UGETW(status.wStatus); 969 DPRINTF(("usbd_do_request: status = 0x%04x\n", s)); 970 if (!(s & UES_HALT)) 971 goto bad; 972 treq.bmRequestType = UT_WRITE_ENDPOINT; 973 treq.bRequest = UR_CLEAR_FEATURE; 974 USETW(treq.wValue, UF_ENDPOINT_HALT); 975 USETW(treq.wIndex, 0); 976 USETW(treq.wLength, 0); 977 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, 978 &treq, &status, 0, 0, 0); 979 nerr = usbd_sync_transfer(xfer); 980 if (nerr) 981 goto bad; 982 } 983 984 bad: 985 usbd_free_xfer(xfer); 986 return (err); 987 } 988 989 void 990 usbd_do_request_async_cb(usbd_xfer_handle xfer, 991 usbd_private_handle priv, usbd_status status) 992 { 993 #if defined(USB_DEBUG) || defined(DIAGNOSTIC) 994 if (xfer->actlen > xfer->length) { 995 DPRINTF(("usbd_do_request: overrun addr=%d type=0x%02x req=0x" 996 "%02x val=%d index=%d rlen=%d length=%d actlen=%d\n", 997 xfer->pipe->device->address, 998 xfer->request.bmRequestType, 999 xfer->request.bRequest, UGETW(xfer->request.wValue), 1000 UGETW(xfer->request.wIndex), 1001 UGETW(xfer->request.wLength), 1002 xfer->length, xfer->actlen)); 1003 } 1004 #endif 1005 usbd_free_xfer(xfer); 1006 } 1007 1008 /* 1009 * Execute a request without waiting for completion. 1010 * Can be used from interrupt context. 1011 */ 1012 usbd_status 1013 usbd_do_request_async(usbd_device_handle dev, usb_device_request_t *req, 1014 void *data) 1015 { 1016 usbd_xfer_handle xfer; 1017 usbd_status err; 1018 1019 xfer = usbd_alloc_xfer(dev); 1020 if (xfer == NULL) 1021 return (USBD_NOMEM); 1022 usbd_setup_default_xfer(xfer, dev, 0, USBD_DEFAULT_TIMEOUT, req, 1023 data, UGETW(req->wLength), 0, usbd_do_request_async_cb); 1024 err = usbd_transfer(xfer); 1025 if (err != USBD_IN_PROGRESS) { 1026 usbd_free_xfer(xfer); 1027 return (err); 1028 } 1029 return (USBD_NORMAL_COMPLETION); 1030 } 1031 1032 const struct usbd_quirks * 1033 usbd_get_quirks(usbd_device_handle dev) 1034 { 1035 #ifdef DIAGNOSTIC 1036 if (dev == NULL) { 1037 printf("usbd_get_quirks: dev == NULL\n"); 1038 return 0; 1039 } 1040 #endif 1041 return (dev->quirks); 1042 } 1043 1044 /* XXX do periodic free() of free list */ 1045 1046 /* 1047 * Called from keyboard driver when in polling mode. 1048 */ 1049 void 1050 usbd_dopoll(usbd_interface_handle iface) 1051 { 1052 iface->device->bus->methods->do_poll(iface->device->bus); 1053 } 1054 1055 void 1056 usbd_set_polling(usbd_device_handle dev, int on) 1057 { 1058 if (on) 1059 dev->bus->use_polling++; 1060 else 1061 dev->bus->use_polling--; 1062 /* When polling we need to make sure there is nothing pending to do. */ 1063 if (dev->bus->use_polling) 1064 dev->bus->methods->soft_intr(dev->bus); 1065 } 1066 1067 1068 usb_endpoint_descriptor_t * 1069 usbd_get_endpoint_descriptor(usbd_interface_handle iface, u_int8_t address) 1070 { 1071 struct usbd_endpoint *ep; 1072 int i; 1073 1074 for (i = 0; i < iface->idesc->bNumEndpoints; i++) { 1075 ep = &iface->endpoints[i]; 1076 if (ep->edesc->bEndpointAddress == address) 1077 return (iface->endpoints[i].edesc); 1078 } 1079 return (0); 1080 } 1081 1082 /* 1083 * usbd_ratecheck() can limit the number of error messages that occurs. 1084 * When a device is unplugged it may take up to 0.25s for the hub driver 1085 * to notice it. If the driver continuosly tries to do I/O operations 1086 * this can generate a large number of messages. 1087 */ 1088 int 1089 usbd_ratecheck(struct timeval *last) 1090 { 1091 static struct timeval errinterval = { 0, 250000 }; /* 0.25 s*/ 1092 1093 return (ratecheck(last, &errinterval)); 1094 } 1095 1096 /* 1097 * Search for a vendor/product pair in an array. The item size is 1098 * given as an argument. 1099 */ 1100 const struct usb_devno * 1101 usb_match_device(const struct usb_devno *tbl, u_int nentries, u_int sz, 1102 u_int16_t vendor, u_int16_t product) 1103 { 1104 while (nentries-- > 0) { 1105 u_int16_t tproduct = tbl->ud_product; 1106 if (tbl->ud_vendor == vendor && 1107 (tproduct == product || tproduct == USB_PRODUCT_ANY)) 1108 return (tbl); 1109 tbl = (const struct usb_devno *)((const char *)tbl + sz); 1110 } 1111 return (NULL); 1112 } 1113 1114 1115 void 1116 usb_desc_iter_init(usbd_device_handle dev, usbd_desc_iter_t *iter) 1117 { 1118 const usb_config_descriptor_t *cd = usbd_get_config_descriptor(dev); 1119 1120 iter->cur = (const uByte *)cd; 1121 iter->end = (const uByte *)cd + UGETW(cd->wTotalLength); 1122 } 1123 1124 const usb_descriptor_t * 1125 usb_desc_iter_next(usbd_desc_iter_t *iter) 1126 { 1127 const usb_descriptor_t *desc; 1128 1129 if (iter->cur + sizeof(usb_descriptor_t) >= iter->end) { 1130 if (iter->cur != iter->end) 1131 printf("usb_desc_iter_next: bad descriptor\n"); 1132 return NULL; 1133 } 1134 desc = (const usb_descriptor_t *)iter->cur; 1135 if (desc->bLength == 0) { 1136 printf("usb_desc_iter_next: descriptor length = 0\n"); 1137 return NULL; 1138 } 1139 iter->cur += desc->bLength; 1140 if (iter->cur > iter->end) { 1141 printf("usb_desc_iter_next: descriptor length too large\n"); 1142 return NULL; 1143 } 1144 return desc; 1145 } 1146 1147 usbd_status 1148 usbd_get_string(usbd_device_handle dev, int si, char *buf) 1149 { 1150 return usbd_get_string0(dev, si, buf, 1); 1151 } 1152 1153 usbd_status 1154 usbd_get_string0(usbd_device_handle dev, int si, char *buf, int unicode) 1155 { 1156 int swap = dev->quirks->uq_flags & UQ_SWAP_UNICODE; 1157 usb_string_descriptor_t us; 1158 char *s; 1159 int i, n; 1160 u_int16_t c; 1161 usbd_status err; 1162 int size; 1163 1164 buf[0] = '\0'; 1165 if (si == 0) 1166 return (USBD_INVAL); 1167 if (dev->quirks->uq_flags & UQ_NO_STRINGS) 1168 return (USBD_STALLED); 1169 if (dev->langid == USBD_NOLANG) { 1170 /* Set up default language */ 1171 err = usbd_get_string_desc(dev, USB_LANGUAGE_TABLE, 0, &us, 1172 &size); 1173 if (err || size < 4) { 1174 DPRINTFN(-1,("usbd_get_string: getting lang failed, using 0\n")); 1175 dev->langid = 0; /* Well, just pick something then */ 1176 } else { 1177 /* Pick the first language as the default. */ 1178 dev->langid = UGETW(us.bString[0]); 1179 } 1180 } 1181 err = usbd_get_string_desc(dev, si, dev->langid, &us, &size); 1182 if (err) 1183 return (err); 1184 s = buf; 1185 n = size / 2 - 1; 1186 if (unicode) { 1187 for (i = 0; i < n; i++) { 1188 c = UGETW(us.bString[i]); 1189 if (swap) 1190 c = (c >> 8) | (c << 8); 1191 s += wput_utf8(s, 3, c); 1192 } 1193 *s++ = 0; 1194 } 1195 #ifdef COMPAT_30 1196 else { 1197 for (i = 0; i < n; i++) { 1198 c = UGETW(us.bString[i]); 1199 if (swap) 1200 c = (c >> 8) | (c << 8); 1201 *s++ = (c < 0x80) ? c : '?'; 1202 } 1203 *s++ = 0; 1204 } 1205 #endif 1206 return (USBD_NORMAL_COMPLETION); 1207 } 1208