1 /* $FreeBSD$ */ 2 /*- 3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in the 12 * documentation and/or other materials provided with the distribution. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24 * SUCH DAMAGE. 25 */ 26 27 /* 28 * This file contains the emulation layer for LibUSB v0.1 from sourceforge. 29 */ 30 31 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE 32 #include LIBUSB_GLOBAL_INCLUDE_FILE 33 #else 34 #include <errno.h> 35 #include <stdio.h> 36 #include <stdlib.h> 37 #include <string.h> 38 #include <time.h> 39 #include <sys/queue.h> 40 #endif 41 42 #include "libusb20.h" 43 #include "libusb20_desc.h" 44 #include "libusb20_int.h" 45 #include "usb.h" 46 47 /* 48 * The two following macros were taken from the original LibUSB v0.1 49 * for sake of compatibility: 50 */ 51 #define LIST_ADD(begin, ent) \ 52 do { \ 53 if (begin) { \ 54 ent->next = begin; \ 55 ent->next->prev = ent; \ 56 } else { \ 57 ent->next = NULL; \ 58 } \ 59 ent->prev = NULL; \ 60 begin = ent; \ 61 } while(0) 62 63 #define LIST_DEL(begin, ent) \ 64 do { \ 65 if (ent->prev) { \ 66 ent->prev->next = ent->next; \ 67 } else { \ 68 begin = ent->next; \ 69 } \ 70 if (ent->next) { \ 71 ent->next->prev = ent->prev; \ 72 } \ 73 ent->prev = NULL; \ 74 ent->next = NULL; \ 75 } while (0) 76 77 struct usb_bus *usb_busses = NULL; 78 79 static struct usb_bus usb_global_bus = { 80 .dirname = {"/dev/usb"}, 81 .root_dev = NULL, 82 .devices = NULL, 83 }; 84 85 static struct libusb20_backend *usb_backend = NULL; 86 87 struct usb_parse_state { 88 89 struct { 90 struct libusb20_endpoint *currep; 91 struct libusb20_interface *currifc; 92 struct libusb20_config *currcfg; 93 struct libusb20_me_struct *currextra; 94 } a; 95 96 struct { 97 struct usb_config_descriptor *currcfg; 98 struct usb_interface_descriptor *currifc; 99 struct usb_endpoint_descriptor *currep; 100 struct usb_interface *currifcw; 101 uint8_t *currextra; 102 } b; 103 104 uint8_t preparse; 105 }; 106 107 static struct libusb20_transfer * 108 usb_get_transfer_by_ep_no(usb_dev_handle * dev, uint8_t ep_no) 109 { 110 struct libusb20_device *pdev = (void *)dev; 111 struct libusb20_transfer *xfer; 112 int err; 113 uint32_t bufsize; 114 uint8_t x; 115 uint8_t speed; 116 117 x = (ep_no & LIBUSB20_ENDPOINT_ADDRESS_MASK) * 2; 118 119 if (ep_no & LIBUSB20_ENDPOINT_DIR_MASK) { 120 /* this is an IN endpoint */ 121 x |= 1; 122 } 123 speed = libusb20_dev_get_speed(pdev); 124 125 /* select a sensible buffer size */ 126 if (speed == LIBUSB20_SPEED_LOW) { 127 bufsize = 256; 128 } else if (speed == LIBUSB20_SPEED_FULL) { 129 bufsize = 4096; 130 } else { 131 bufsize = 16384; 132 } 133 134 xfer = libusb20_tr_get_pointer(pdev, x); 135 136 if (xfer == NULL) 137 return (xfer); 138 139 err = libusb20_tr_open(xfer, bufsize, 1, ep_no); 140 if (err == LIBUSB20_ERROR_BUSY) { 141 /* already opened */ 142 return (xfer); 143 } else if (err) { 144 return (NULL); 145 } 146 /* success */ 147 return (xfer); 148 } 149 150 usb_dev_handle * 151 usb_open(struct usb_device *dev) 152 { 153 int err; 154 155 err = libusb20_dev_open(dev->dev, 16 * 2); 156 if (err == LIBUSB20_ERROR_BUSY) { 157 /* 158 * Workaround buggy USB applications which open the USB 159 * device multiple times: 160 */ 161 return (dev->dev); 162 } 163 if (err) 164 return (NULL); 165 166 /* 167 * Dequeue USB device from backend queue so that it does not get 168 * freed when the backend is re-scanned: 169 */ 170 libusb20_be_dequeue_device(usb_backend, dev->dev); 171 172 return (dev->dev); 173 } 174 175 int 176 usb_close(usb_dev_handle * udev) 177 { 178 struct usb_device *dev; 179 int err; 180 181 err = libusb20_dev_close((void *)udev); 182 183 if (err) 184 return (-1); 185 186 if (usb_backend != NULL) { 187 /* 188 * Enqueue USB device to backend queue so that it gets freed 189 * when the backend is re-scanned: 190 */ 191 libusb20_be_enqueue_device(usb_backend, (void *)udev); 192 } else { 193 /* 194 * The backend is gone. Free device data so that we 195 * don't start leaking memory! 196 */ 197 dev = usb_device(udev); 198 libusb20_dev_free((void *)udev); 199 LIST_DEL(usb_global_bus.devices, dev); 200 free(dev); 201 } 202 return (0); 203 } 204 205 int 206 usb_get_string(usb_dev_handle * dev, int strindex, 207 int langid, char *buf, size_t buflen) 208 { 209 int err; 210 211 if (dev == NULL) 212 return (-1); 213 214 if (buflen > 65535) 215 buflen = 65535; 216 217 err = libusb20_dev_req_string_sync((void *)dev, 218 strindex, langid, buf, buflen); 219 220 if (err) 221 return (-1); 222 223 return (0); 224 } 225 226 int 227 usb_get_string_simple(usb_dev_handle * dev, int strindex, 228 char *buf, size_t buflen) 229 { 230 int err; 231 232 if (dev == NULL) 233 return (-1); 234 235 if (buflen > 65535) 236 buflen = 65535; 237 238 err = libusb20_dev_req_string_simple_sync((void *)dev, 239 strindex, buf, buflen); 240 241 if (err) 242 return (-1); 243 244 return (strlen(buf)); 245 } 246 247 int 248 usb_get_descriptor_by_endpoint(usb_dev_handle * udev, int ep, uint8_t type, 249 uint8_t ep_index, void *buf, int size) 250 { 251 memset(buf, 0, size); 252 253 if (udev == NULL) 254 return (-1); 255 256 if (size > 65535) 257 size = 65535; 258 259 return (usb_control_msg(udev, ep | USB_ENDPOINT_IN, 260 USB_REQ_GET_DESCRIPTOR, (type << 8) + ep_index, 0, 261 buf, size, 1000)); 262 } 263 264 int 265 usb_get_descriptor(usb_dev_handle * udev, uint8_t type, uint8_t desc_index, 266 void *buf, int size) 267 { 268 memset(buf, 0, size); 269 270 if (udev == NULL) 271 return (-1); 272 273 if (size > 65535) 274 size = 65535; 275 276 return (usb_control_msg(udev, USB_ENDPOINT_IN, USB_REQ_GET_DESCRIPTOR, 277 (type << 8) + desc_index, 0, buf, size, 1000)); 278 } 279 280 int 281 usb_parse_descriptor(uint8_t *source, char *description, void *dest) 282 { 283 uint8_t *sp = source; 284 uint8_t *dp = dest; 285 uint16_t w; 286 uint32_t d; 287 char *cp; 288 289 for (cp = description; *cp; cp++) { 290 switch (*cp) { 291 case 'b': /* 8-bit byte */ 292 *dp++ = *sp++; 293 break; 294 /* 295 * 16-bit word, convert from little endian to CPU 296 */ 297 case 'w': 298 w = (sp[1] << 8) | sp[0]; 299 sp += 2; 300 /* Align to word boundary */ 301 dp += ((dp - (uint8_t *)0) & 1); 302 *((uint16_t *)dp) = w; 303 dp += 2; 304 break; 305 /* 306 * 32-bit dword, convert from little endian to CPU 307 */ 308 case 'd': 309 d = (sp[3] << 24) | (sp[2] << 16) | 310 (sp[1] << 8) | sp[0]; 311 sp += 4; 312 /* Align to word boundary */ 313 dp += ((dp - (uint8_t *)0) & 1); 314 /* Align to double word boundary */ 315 dp += ((dp - (uint8_t *)0) & 2); 316 *((uint32_t *)dp) = d; 317 dp += 4; 318 break; 319 } 320 } 321 return (sp - source); 322 } 323 324 static void 325 usb_parse_extra(struct usb_parse_state *ps, uint8_t **pptr, int *plen) 326 { 327 void *ptr; 328 uint16_t len; 329 330 ptr = ps->a.currextra->ptr; 331 len = ps->a.currextra->len; 332 333 if (ps->preparse == 0) { 334 memcpy(ps->b.currextra, ptr, len); 335 *pptr = ps->b.currextra; 336 *plen = len; 337 } 338 ps->b.currextra += len; 339 return; 340 } 341 342 static void 343 usb_parse_endpoint(struct usb_parse_state *ps) 344 { 345 struct usb_endpoint_descriptor *bep; 346 struct libusb20_endpoint *aep; 347 348 aep = ps->a.currep; 349 bep = ps->b.currep++; 350 351 if (ps->preparse == 0) { 352 /* copy descriptor fields */ 353 bep->bLength = aep->desc.bLength; 354 bep->bDescriptorType = aep->desc.bDescriptorType; 355 bep->bEndpointAddress = aep->desc.bEndpointAddress; 356 bep->bmAttributes = aep->desc.bmAttributes; 357 bep->wMaxPacketSize = aep->desc.wMaxPacketSize; 358 bep->bInterval = aep->desc.bInterval; 359 bep->bRefresh = aep->desc.bRefresh; 360 bep->bSynchAddress = aep->desc.bSynchAddress; 361 } 362 ps->a.currextra = &aep->extra; 363 usb_parse_extra(ps, &bep->extra, &bep->extralen); 364 return; 365 } 366 367 static void 368 usb_parse_iface_sub(struct usb_parse_state *ps) 369 { 370 struct libusb20_interface *aifc; 371 struct usb_interface_descriptor *bifc; 372 uint8_t x; 373 374 aifc = ps->a.currifc; 375 bifc = ps->b.currifc++; 376 377 if (ps->preparse == 0) { 378 /* copy descriptor fields */ 379 bifc->bLength = aifc->desc.bLength; 380 bifc->bDescriptorType = aifc->desc.bDescriptorType; 381 bifc->bInterfaceNumber = aifc->desc.bInterfaceNumber; 382 bifc->bAlternateSetting = aifc->desc.bAlternateSetting; 383 bifc->bNumEndpoints = aifc->num_endpoints; 384 bifc->bInterfaceClass = aifc->desc.bInterfaceClass; 385 bifc->bInterfaceSubClass = aifc->desc.bInterfaceSubClass; 386 bifc->bInterfaceProtocol = aifc->desc.bInterfaceProtocol; 387 bifc->iInterface = aifc->desc.iInterface; 388 bifc->endpoint = ps->b.currep; 389 } 390 for (x = 0; x != aifc->num_endpoints; x++) { 391 ps->a.currep = aifc->endpoints + x; 392 usb_parse_endpoint(ps); 393 } 394 395 ps->a.currextra = &aifc->extra; 396 usb_parse_extra(ps, &bifc->extra, &bifc->extralen); 397 return; 398 } 399 400 static void 401 usb_parse_iface(struct usb_parse_state *ps) 402 { 403 struct libusb20_interface *aifc; 404 struct usb_interface *bifc; 405 uint8_t x; 406 407 aifc = ps->a.currifc; 408 bifc = ps->b.currifcw++; 409 410 if (ps->preparse == 0) { 411 /* initialise interface wrapper */ 412 bifc->altsetting = ps->b.currifc; 413 bifc->num_altsetting = aifc->num_altsetting + 1; 414 } 415 usb_parse_iface_sub(ps); 416 417 for (x = 0; x != aifc->num_altsetting; x++) { 418 ps->a.currifc = aifc->altsetting + x; 419 usb_parse_iface_sub(ps); 420 } 421 return; 422 } 423 424 static void 425 usb_parse_config(struct usb_parse_state *ps) 426 { 427 struct libusb20_config *acfg; 428 struct usb_config_descriptor *bcfg; 429 uint8_t x; 430 431 acfg = ps->a.currcfg; 432 bcfg = ps->b.currcfg; 433 434 if (ps->preparse == 0) { 435 /* initialise config wrapper */ 436 bcfg->bLength = acfg->desc.bLength; 437 bcfg->bDescriptorType = acfg->desc.bDescriptorType; 438 bcfg->wTotalLength = acfg->desc.wTotalLength; 439 bcfg->bNumInterfaces = acfg->num_interface; 440 bcfg->bConfigurationValue = acfg->desc.bConfigurationValue; 441 bcfg->iConfiguration = acfg->desc.iConfiguration; 442 bcfg->bmAttributes = acfg->desc.bmAttributes; 443 bcfg->MaxPower = acfg->desc.bMaxPower; 444 bcfg->interface = ps->b.currifcw; 445 } 446 for (x = 0; x != acfg->num_interface; x++) { 447 ps->a.currifc = acfg->interface + x; 448 usb_parse_iface(ps); 449 } 450 451 ps->a.currextra = &acfg->extra; 452 usb_parse_extra(ps, &bcfg->extra, &bcfg->extralen); 453 return; 454 } 455 456 int 457 usb_parse_configuration(struct usb_config_descriptor *config, 458 uint8_t *buffer) 459 { 460 struct usb_parse_state ps; 461 uint8_t *ptr; 462 uint32_t a; 463 uint32_t b; 464 uint32_t c; 465 uint32_t d; 466 467 if ((buffer == NULL) || (config == NULL)) { 468 return (-1); 469 } 470 memset(&ps, 0, sizeof(ps)); 471 472 ps.a.currcfg = libusb20_parse_config_desc(buffer); 473 ps.b.currcfg = config; 474 if (ps.a.currcfg == NULL) { 475 /* could not parse config or out of memory */ 476 return (-1); 477 } 478 /* do the pre-parse */ 479 ps.preparse = 1; 480 usb_parse_config(&ps); 481 482 a = ((uint8_t *)(ps.b.currifcw) - ((uint8_t *)0)); 483 b = ((uint8_t *)(ps.b.currifc) - ((uint8_t *)0)); 484 c = ((uint8_t *)(ps.b.currep) - ((uint8_t *)0)); 485 d = ((uint8_t *)(ps.b.currextra) - ((uint8_t *)0)); 486 487 /* allocate memory for our configuration */ 488 ptr = malloc(a + b + c + d); 489 if (ptr == NULL) { 490 /* free config structure */ 491 free(ps.a.currcfg); 492 return (-1); 493 } 494 495 /* "currifcw" must be first, hence this pointer is freed */ 496 ps.b.currifcw = (void *)(ptr); 497 ps.b.currifc = (void *)(ptr + a); 498 ps.b.currep = (void *)(ptr + a + b); 499 ps.b.currextra = (void *)(ptr + a + b + c); 500 501 /* generate a libusb v0.1 compatible structure */ 502 ps.preparse = 0; 503 usb_parse_config(&ps); 504 505 /* free config structure */ 506 free(ps.a.currcfg); 507 508 return (0); /* success */ 509 } 510 511 void 512 usb_destroy_configuration(struct usb_device *dev) 513 { 514 uint8_t c; 515 516 if (dev->config == NULL) { 517 return; 518 } 519 for (c = 0; c != dev->descriptor.bNumConfigurations; c++) { 520 struct usb_config_descriptor *cf = &dev->config[c]; 521 522 if (cf->interface != NULL) { 523 free(cf->interface); 524 cf->interface = NULL; 525 } 526 } 527 528 free(dev->config); 529 dev->config = NULL; 530 return; 531 } 532 533 void 534 usb_fetch_and_parse_descriptors(usb_dev_handle * udev) 535 { 536 struct usb_device *dev; 537 struct libusb20_device *pdev; 538 uint8_t *ptr; 539 int error; 540 uint32_t size; 541 uint16_t len; 542 uint8_t x; 543 544 if (udev == NULL) { 545 /* be NULL safe */ 546 return; 547 } 548 dev = usb_device(udev); 549 pdev = (void *)udev; 550 551 if (dev->descriptor.bNumConfigurations == 0) { 552 /* invalid device */ 553 return; 554 } 555 size = dev->descriptor.bNumConfigurations * 556 sizeof(struct usb_config_descriptor); 557 558 dev->config = malloc(size); 559 if (dev->config == NULL) { 560 /* out of memory */ 561 return; 562 } 563 memset(dev->config, 0, size); 564 565 for (x = 0; x != dev->descriptor.bNumConfigurations; x++) { 566 567 error = (pdev->methods->get_config_desc_full) ( 568 pdev, &ptr, &len, x); 569 570 if (error) { 571 usb_destroy_configuration(dev); 572 return; 573 } 574 usb_parse_configuration(dev->config + x, ptr); 575 576 /* free config buffer */ 577 free(ptr); 578 } 579 return; 580 } 581 582 static int 583 usb_std_io(usb_dev_handle * dev, int ep, char *bytes, int size, 584 int timeout, int is_intr) 585 { 586 struct libusb20_transfer *xfer; 587 uint32_t temp; 588 uint32_t maxsize; 589 uint32_t actlen; 590 char *oldbytes; 591 592 xfer = usb_get_transfer_by_ep_no(dev, ep); 593 if (xfer == NULL) 594 return (-1); 595 596 if (libusb20_tr_pending(xfer)) { 597 /* there is already a transfer ongoing */ 598 return (-1); 599 } 600 maxsize = libusb20_tr_get_max_total_length(xfer); 601 oldbytes = bytes; 602 603 /* 604 * We allow transferring zero bytes which is the same 605 * equivalent to a zero length USB packet. 606 */ 607 do { 608 609 temp = size; 610 if (temp > maxsize) { 611 /* find maximum possible length */ 612 temp = maxsize; 613 } 614 if (is_intr) 615 libusb20_tr_setup_intr(xfer, bytes, temp, timeout); 616 else 617 libusb20_tr_setup_bulk(xfer, bytes, temp, timeout); 618 619 libusb20_tr_start(xfer); 620 621 while (1) { 622 623 if (libusb20_dev_process((void *)dev) != 0) { 624 /* device detached */ 625 return (-1); 626 } 627 if (libusb20_tr_pending(xfer) == 0) { 628 /* transfer complete */ 629 break; 630 } 631 /* wait for USB event from kernel */ 632 libusb20_dev_wait_process((void *)dev, -1); 633 } 634 635 switch (libusb20_tr_get_status(xfer)) { 636 case 0: 637 /* success */ 638 break; 639 case LIBUSB20_TRANSFER_TIMED_OUT: 640 /* transfer timeout */ 641 return (-ETIMEDOUT); 642 default: 643 /* other transfer error */ 644 return (-ENXIO); 645 } 646 actlen = libusb20_tr_get_actual_length(xfer); 647 648 bytes += actlen; 649 size -= actlen; 650 651 if (actlen != temp) { 652 /* short transfer */ 653 break; 654 } 655 } while (size > 0); 656 657 return (bytes - oldbytes); 658 } 659 660 int 661 usb_bulk_write(usb_dev_handle * dev, int ep, char *bytes, 662 int size, int timeout) 663 { 664 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK, 665 bytes, size, timeout, 0)); 666 } 667 668 int 669 usb_bulk_read(usb_dev_handle * dev, int ep, char *bytes, 670 int size, int timeout) 671 { 672 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK, 673 bytes, size, timeout, 0)); 674 } 675 676 int 677 usb_interrupt_write(usb_dev_handle * dev, int ep, char *bytes, 678 int size, int timeout) 679 { 680 return (usb_std_io(dev, ep & ~USB_ENDPOINT_DIR_MASK, 681 bytes, size, timeout, 1)); 682 } 683 684 int 685 usb_interrupt_read(usb_dev_handle * dev, int ep, char *bytes, 686 int size, int timeout) 687 { 688 return (usb_std_io(dev, ep | USB_ENDPOINT_DIR_MASK, 689 bytes, size, timeout, 1)); 690 } 691 692 int 693 usb_control_msg(usb_dev_handle * dev, int requesttype, int request, 694 int value, int wIndex, char *bytes, int size, int timeout) 695 { 696 struct LIBUSB20_CONTROL_SETUP_DECODED req; 697 int err; 698 uint16_t actlen; 699 700 LIBUSB20_INIT(LIBUSB20_CONTROL_SETUP, &req); 701 702 req.bmRequestType = requesttype; 703 req.bRequest = request; 704 req.wValue = value; 705 req.wIndex = wIndex; 706 req.wLength = size; 707 708 err = libusb20_dev_request_sync((void *)dev, &req, bytes, 709 &actlen, timeout, 0); 710 711 if (err) 712 return (-1); 713 714 return (actlen); 715 } 716 717 int 718 usb_set_configuration(usb_dev_handle * udev, int bConfigurationValue) 719 { 720 struct usb_device *dev; 721 int err; 722 uint8_t i; 723 724 /* 725 * Need to translate from "bConfigurationValue" to 726 * configuration index: 727 */ 728 729 if (bConfigurationValue == 0) { 730 /* unconfigure */ 731 i = 255; 732 } else { 733 /* lookup configuration index */ 734 dev = usb_device(udev); 735 736 /* check if the configuration array is not there */ 737 if (dev->config == NULL) { 738 return (-1); 739 } 740 for (i = 0;; i++) { 741 if (i == dev->descriptor.bNumConfigurations) { 742 /* "bConfigurationValue" not found */ 743 return (-1); 744 } 745 if ((dev->config + i)->bConfigurationValue == 746 bConfigurationValue) { 747 break; 748 } 749 } 750 } 751 752 err = libusb20_dev_set_config_index((void *)udev, i); 753 754 if (err) 755 return (-1); 756 757 return (0); 758 } 759 760 int 761 usb_claim_interface(usb_dev_handle * dev, int interface) 762 { 763 struct libusb20_device *pdev = (void *)dev; 764 765 pdev->claimed_interface = interface; 766 767 return (0); 768 } 769 770 int 771 usb_release_interface(usb_dev_handle * dev, int interface) 772 { 773 /* do nothing */ 774 return (0); 775 } 776 777 int 778 usb_set_altinterface(usb_dev_handle * dev, int alternate) 779 { 780 struct libusb20_device *pdev = (void *)dev; 781 int err; 782 uint8_t iface; 783 784 iface = pdev->claimed_interface; 785 786 err = libusb20_dev_set_alt_index((void *)dev, iface, alternate); 787 788 if (err) 789 return (-1); 790 791 return (0); 792 } 793 794 int 795 usb_resetep(usb_dev_handle * dev, unsigned int ep) 796 { 797 /* emulate an endpoint reset through clear-STALL */ 798 return (usb_clear_halt(dev, ep)); 799 } 800 801 int 802 usb_clear_halt(usb_dev_handle * dev, unsigned int ep) 803 { 804 struct libusb20_transfer *xfer; 805 806 xfer = usb_get_transfer_by_ep_no(dev, ep); 807 if (xfer == NULL) 808 return (-1); 809 810 libusb20_tr_clear_stall_sync(xfer); 811 812 return (0); 813 } 814 815 int 816 usb_reset(usb_dev_handle * dev) 817 { 818 int err; 819 820 err = libusb20_dev_reset((void *)dev); 821 822 if (err) 823 return (-1); 824 825 /* 826 * Be compatible with LibUSB from sourceforge and close the 827 * handle after reset! 828 */ 829 return (usb_close(dev)); 830 } 831 832 int 833 usb_check_connected(usb_dev_handle * dev) 834 { 835 int err; 836 837 err = libusb20_dev_check_connected((void *)dev); 838 839 if (err) 840 return (-1); 841 842 return (0); 843 } 844 845 const char * 846 usb_strerror(void) 847 { 848 /* TODO */ 849 return ("Unknown error"); 850 } 851 852 void 853 usb_init(void) 854 { 855 /* nothing to do */ 856 return; 857 } 858 859 void 860 usb_set_debug(int level) 861 { 862 /* use kernel UGEN debugging if you need to see what is going on */ 863 return; 864 } 865 866 int 867 usb_find_busses(void) 868 { 869 usb_busses = &usb_global_bus; 870 return (1); 871 } 872 873 int 874 usb_find_devices(void) 875 { 876 struct libusb20_device *pdev; 877 struct usb_device *udev; 878 struct LIBUSB20_DEVICE_DESC_DECODED *ddesc; 879 int devnum; 880 int err; 881 882 /* cleanup after last device search */ 883 /* close all opened devices, if any */ 884 885 while ((pdev = libusb20_be_device_foreach(usb_backend, NULL))) { 886 udev = pdev->privLuData; 887 libusb20_be_dequeue_device(usb_backend, pdev); 888 libusb20_dev_free(pdev); 889 if (udev != NULL) { 890 LIST_DEL(usb_global_bus.devices, udev); 891 free(udev); 892 } 893 } 894 895 /* free old USB backend, if any */ 896 897 libusb20_be_free(usb_backend); 898 899 /* do a new backend device search */ 900 usb_backend = libusb20_be_alloc_default(); 901 if (usb_backend == NULL) { 902 return (-1); 903 } 904 /* iterate all devices */ 905 906 devnum = 1; 907 pdev = NULL; 908 while ((pdev = libusb20_be_device_foreach(usb_backend, pdev))) { 909 udev = malloc(sizeof(*udev)); 910 if (udev == NULL) 911 break; 912 913 memset(udev, 0, sizeof(*udev)); 914 915 udev->bus = &usb_global_bus; 916 917 snprintf(udev->filename, sizeof(udev->filename), 918 "/dev/ugen%u.%u", 919 libusb20_dev_get_bus_number(pdev), 920 libusb20_dev_get_address(pdev)); 921 922 ddesc = libusb20_dev_get_device_desc(pdev); 923 924 udev->descriptor.bLength = sizeof(udev->descriptor); 925 udev->descriptor.bDescriptorType = ddesc->bDescriptorType; 926 udev->descriptor.bcdUSB = ddesc->bcdUSB; 927 udev->descriptor.bDeviceClass = ddesc->bDeviceClass; 928 udev->descriptor.bDeviceSubClass = ddesc->bDeviceSubClass; 929 udev->descriptor.bDeviceProtocol = ddesc->bDeviceProtocol; 930 udev->descriptor.bMaxPacketSize0 = ddesc->bMaxPacketSize0; 931 udev->descriptor.idVendor = ddesc->idVendor; 932 udev->descriptor.idProduct = ddesc->idProduct; 933 udev->descriptor.bcdDevice = ddesc->bcdDevice; 934 udev->descriptor.iManufacturer = ddesc->iManufacturer; 935 udev->descriptor.iProduct = ddesc->iProduct; 936 udev->descriptor.iSerialNumber = ddesc->iSerialNumber; 937 udev->descriptor.bNumConfigurations = 938 ddesc->bNumConfigurations; 939 if (udev->descriptor.bNumConfigurations > USB_MAXCONFIG) { 940 /* truncate number of configurations */ 941 udev->descriptor.bNumConfigurations = USB_MAXCONFIG; 942 } 943 udev->devnum = devnum++; 944 /* link together the two structures */ 945 udev->dev = pdev; 946 pdev->privLuData = udev; 947 948 err = libusb20_dev_open(pdev, 0); 949 if (err == 0) { 950 /* XXX get all config descriptors by default */ 951 usb_fetch_and_parse_descriptors((void *)pdev); 952 libusb20_dev_close(pdev); 953 } 954 LIST_ADD(usb_global_bus.devices, udev); 955 } 956 957 return (devnum - 1); /* success */ 958 } 959 960 struct usb_device * 961 usb_device(usb_dev_handle * dev) 962 { 963 struct libusb20_device *pdev; 964 965 pdev = (void *)dev; 966 967 return (pdev->privLuData); 968 } 969 970 struct usb_bus * 971 usb_get_busses(void) 972 { 973 return (usb_busses); 974 } 975 976 int 977 usb_get_driver_np(usb_dev_handle * dev, int interface, char *name, int namelen) 978 { 979 struct libusb20_device *pdev; 980 char *ptr; 981 int err; 982 983 pdev = (void *)dev; 984 985 if (pdev == NULL) 986 return (-1); 987 if (namelen < 1) 988 return (-1); 989 if (namelen > 255) 990 namelen = 255; 991 992 err = libusb20_dev_get_iface_desc(pdev, interface, name, namelen); 993 if (err != 0) 994 return (-1); 995 996 /* we only want the driver name */ 997 ptr = strstr(name, ":"); 998 if (ptr != NULL) 999 *ptr = 0; 1000 1001 return (0); 1002 } 1003 1004 int 1005 usb_detach_kernel_driver_np(usb_dev_handle * dev, int interface) 1006 { 1007 struct libusb20_device *pdev; 1008 int err; 1009 1010 pdev = (void *)dev; 1011 1012 if (pdev == NULL) 1013 return (-1); 1014 1015 err = libusb20_dev_detach_kernel_driver(pdev, interface); 1016 if (err != 0) 1017 return (-1); 1018 1019 return (0); 1020 } 1021