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