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