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