393 } 394 } 395 396 err = xfer->error; 397 398 if (err) { 399 break; 400 } 401 /* subtract length of SETUP packet, if any */ 402 403 if (xfer->aframes > 0) { 404 xfer->actlen -= xfer->frlengths[0]; 405 } else { 406 xfer->actlen = 0; 407 } 408 409 /* check for short packet */ 410 411 if (temp > xfer->actlen) { 412 temp = xfer->actlen; 413 if (!(flags & USB_SHORT_XFER_OK)) { 414 err = USB_ERR_SHORT_XFER; 415 } 416 length = temp; 417 } 418 if (temp > 0) { 419 if (req->bmRequestType & UT_READ) { 420 if (flags & USB_USER_DATA_PTR) { 421 USB_XFER_UNLOCK(xfer); 422 err = usb2_copy_out_user(xfer->frbuffers + 1, 423 0, data, temp); 424 USB_XFER_LOCK(xfer); 425 if (err) { 426 err = USB_ERR_INVAL; 427 break; 428 } 429 } else { 430 usb2_copy_out(xfer->frbuffers + 1, 431 0, data, temp); 432 } 433 } 434 } 435 /* 436 * Clear "frlengths[0]" so that we don't send the setup 437 * packet again: 438 */ 439 xfer->frlengths[0] = 0; 440 441 /* update length and data pointer */ 442 length -= temp; 443 data = USB_ADD_BYTES(data, temp); 444 445 if (actlen) { 446 (*actlen) += temp; 447 } 448 /* check for timeout */ 449 450 delta_ticks = ticks - start_ticks; 451 if (delta_ticks > max_ticks) { 452 if (!err) { 453 err = USB_ERR_TIMEOUT; 454 } 455 } 456 if (err) { 457 break; 458 } 459 } 460 461 if (err) { 462 /* 463 * Make sure that the control endpoint is no longer 464 * blocked in case of a non-transfer related error: 465 */ 466 usb2_transfer_stop(xfer); 467 } 468 USB_XFER_UNLOCK(xfer); 469 470done: 471 sx_xunlock(udev->default_sx); 472 473 if (mtx) { 474 mtx_lock(mtx); 475 } 476 return ((usb2_error_t)err); 477} 478 479/*------------------------------------------------------------------------* 480 * usb2_req_reset_port 481 * 482 * This function will instruct an USB HUB to perform a reset sequence 483 * on the specified port number. 484 * 485 * Returns: 486 * 0: Success. The USB device should now be at address zero. 487 * Else: Failure. No USB device is present and the USB port should be 488 * disabled. 489 *------------------------------------------------------------------------*/ 490usb2_error_t 491usb2_req_reset_port(struct usb2_device *udev, struct mtx *mtx, uint8_t port) 492{ 493 struct usb2_port_status ps; 494 usb2_error_t err; 495 uint16_t n; 496 497#if USB_DEBUG 498 uint16_t pr_poll_delay; 499 uint16_t pr_recovery_delay; 500 501#endif 502 err = usb2_req_set_port_feature(udev, mtx, port, UHF_PORT_RESET); 503 if (err) { 504 goto done; 505 } 506#if USB_DEBUG 507 /* range check input parameters */ 508 pr_poll_delay = usb2_pr_poll_delay; 509 if (pr_poll_delay < 1) { 510 pr_poll_delay = 1; 511 } else if (pr_poll_delay > 1000) { 512 pr_poll_delay = 1000; 513 } 514 pr_recovery_delay = usb2_pr_recovery_delay; 515 if (pr_recovery_delay > 1000) { 516 pr_recovery_delay = 1000; 517 } 518#endif 519 n = 0; 520 while (1) { 521#if USB_DEBUG 522 /* wait for the device to recover from reset */ 523 usb2_pause_mtx(mtx, pr_poll_delay); 524 n += pr_poll_delay; 525#else 526 /* wait for the device to recover from reset */ 527 usb2_pause_mtx(mtx, USB_PORT_RESET_DELAY); 528 n += USB_PORT_RESET_DELAY; 529#endif 530 err = usb2_req_get_port_status(udev, mtx, &ps, port); 531 if (err) { 532 goto done; 533 } 534 /* if the device disappeared, just give up */ 535 if (!(UGETW(ps.wPortStatus) & UPS_CURRENT_CONNECT_STATUS)) { 536 goto done; 537 } 538 /* check if reset is complete */ 539 if (UGETW(ps.wPortChange) & UPS_C_PORT_RESET) { 540 break; 541 } 542 /* check for timeout */ 543 if (n > 1000) { 544 n = 0; 545 break; 546 } 547 } 548 549 /* clear port reset first */ 550 err = usb2_req_clear_port_feature( 551 udev, mtx, port, UHF_C_PORT_RESET); 552 if (err) { 553 goto done; 554 } 555 /* check for timeout */ 556 if (n == 0) { 557 err = USB_ERR_TIMEOUT; 558 goto done; 559 } 560#if USB_DEBUG 561 /* wait for the device to recover from reset */ 562 usb2_pause_mtx(mtx, pr_recovery_delay); 563#else 564 /* wait for the device to recover from reset */ 565 usb2_pause_mtx(mtx, USB_PORT_RESET_RECOVERY); 566#endif 567 568done: 569 DPRINTFN(2, "port %d reset returning error=%s\n", 570 port, usb2_errstr(err)); 571 return (err); 572} 573 574/*------------------------------------------------------------------------* 575 * usb2_req_get_desc 576 * 577 * This function can be used to retrieve USB descriptors. It contains 578 * some additional logic like zeroing of missing descriptor bytes and 579 * retrying an USB descriptor in case of failure. The "min_len" 580 * argument specifies the minimum descriptor length. The "max_len" 581 * argument specifies the maximum descriptor length. If the real 582 * descriptor length is less than the minimum length the missing 583 * byte(s) will be zeroed. The length field, first byte, of the USB 584 * descriptor will get overwritten in case it indicates a length that 585 * is too big. Also the type field, second byte, of the USB descriptor 586 * will get forced to the correct type. 587 * 588 * Returns: 589 * 0: Success 590 * Else: Failure 591 *------------------------------------------------------------------------*/ 592usb2_error_t 593usb2_req_get_desc(struct usb2_device *udev, struct mtx *mtx, void *desc, 594 uint16_t min_len, uint16_t max_len, 595 uint16_t id, uint8_t type, uint8_t index, 596 uint8_t retries) 597{ 598 struct usb2_device_request req; 599 uint8_t *buf; 600 usb2_error_t err; 601 602 DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n", 603 id, type, index, max_len); 604 605 req.bmRequestType = UT_READ_DEVICE; 606 req.bRequest = UR_GET_DESCRIPTOR; 607 USETW2(req.wValue, type, index); 608 USETW(req.wIndex, id); 609 610 while (1) { 611 612 if ((min_len < 2) || (max_len < 2)) { 613 err = USB_ERR_INVAL; 614 goto done; 615 } 616 USETW(req.wLength, min_len); 617 618 err = usb2_do_request_flags(udev, mtx, &req, 619 desc, 0, NULL, 1000); 620 621 if (err) { 622 if (!retries) { 623 goto done; 624 } 625 retries--; 626 627 usb2_pause_mtx(mtx, 200); 628 629 continue; 630 } 631 buf = desc; 632 633 if (min_len == max_len) { 634 635 /* enforce correct type and length */ 636 637 if (buf[0] > min_len) { 638 buf[0] = min_len; 639 } 640 buf[1] = type; 641 642 goto done; 643 } 644 /* range check */ 645 646 if (max_len > buf[0]) { 647 max_len = buf[0]; 648 } 649 /* zero minimum data */ 650 651 while (min_len > max_len) { 652 min_len--; 653 buf[min_len] = 0; 654 } 655 656 /* set new minimum length */ 657 658 min_len = max_len; 659 } 660done: 661 return (err); 662} 663 664/*------------------------------------------------------------------------* 665 * usb2_req_get_string_any 666 * 667 * This function will return the string given by "string_index" 668 * using the first language ID. The maximum length "len" includes 669 * the terminating zero. The "len" argument should be twice as 670 * big pluss 2 bytes, compared with the actual maximum string length ! 671 * 672 * Returns: 673 * 0: Success 674 * Else: Failure 675 *------------------------------------------------------------------------*/ 676usb2_error_t 677usb2_req_get_string_any(struct usb2_device *udev, struct mtx *mtx, char *buf, 678 uint16_t len, uint8_t string_index) 679{ 680 char *s; 681 uint8_t *temp; 682 uint16_t i; 683 uint16_t n; 684 uint16_t c; 685 uint8_t swap; 686 usb2_error_t err; 687 688 if (len == 0) { 689 /* should not happen */ 690 return (USB_ERR_NORMAL_COMPLETION); 691 } 692 if (string_index == 0) { 693 /* this is the language table */ 694 buf[0] = 0; 695 return (USB_ERR_INVAL); 696 } 697 if (udev->flags.no_strings) { 698 buf[0] = 0; 699 return (USB_ERR_STALLED); 700 } 701 err = usb2_req_get_string_desc 702 (udev, mtx, buf, len, udev->langid, string_index); 703 if (err) { 704 buf[0] = 0; 705 return (err); 706 } 707 temp = (uint8_t *)buf; 708 709 if (temp[0] < 2) { 710 /* string length is too short */ 711 buf[0] = 0; 712 return (USB_ERR_INVAL); 713 } 714 /* reserve one byte for terminating zero */ 715 len--; 716 717 /* find maximum length */ 718 s = buf; 719 n = (temp[0] / 2) - 1; 720 if (n > len) { 721 n = len; 722 } 723 /* skip descriptor header */ 724 temp += 2; 725 726 /* reset swap state */ 727 swap = 3; 728 729 /* convert and filter */ 730 for (i = 0; (i != n); i++) { 731 c = UGETW(temp + (2 * i)); 732 733 /* convert from Unicode, handle buggy strings */ 734 if (((c & 0xff00) == 0) && (swap & 1)) { 735 /* Little Endian, default */ 736 *s = c; 737 swap = 1; 738 } else if (((c & 0x00ff) == 0) && (swap & 2)) { 739 /* Big Endian */ 740 *s = c >> 8; 741 swap = 2; 742 } else { 743 /* silently skip bad character */ 744 continue; 745 } 746 747 /* 748 * Filter by default - we don't allow greater and less than 749 * signs because they might confuse the dmesg printouts! 750 */ 751 if ((*s == '<') || (*s == '>') || (!isprint(*s))) { 752 /* silently skip bad character */ 753 continue; 754 } 755 s++; 756 } 757 *s = 0; /* zero terminate resulting string */ 758 return (USB_ERR_NORMAL_COMPLETION); 759} 760 761/*------------------------------------------------------------------------* 762 * usb2_req_get_string_desc 763 * 764 * If you don't know the language ID, consider using 765 * "usb2_req_get_string_any()". 766 * 767 * Returns: 768 * 0: Success 769 * Else: Failure 770 *------------------------------------------------------------------------*/ 771usb2_error_t 772usb2_req_get_string_desc(struct usb2_device *udev, struct mtx *mtx, void *sdesc, 773 uint16_t max_len, uint16_t lang_id, 774 uint8_t string_index) 775{ 776 return (usb2_req_get_desc(udev, mtx, sdesc, 2, max_len, lang_id, 777 UDESC_STRING, string_index, 0)); 778} 779 780/*------------------------------------------------------------------------* 781 * usb2_req_get_config_desc 782 * 783 * Returns: 784 * 0: Success 785 * Else: Failure 786 *------------------------------------------------------------------------*/ 787usb2_error_t 788usb2_req_get_config_desc(struct usb2_device *udev, struct mtx *mtx, 789 struct usb2_config_descriptor *d, uint8_t conf_index) 790{ 791 usb2_error_t err; 792 793 DPRINTFN(4, "confidx=%d\n", conf_index); 794 795 err = usb2_req_get_desc(udev, mtx, d, sizeof(*d), 796 sizeof(*d), 0, UDESC_CONFIG, conf_index, 0); 797 if (err) { 798 goto done; 799 } 800 /* Extra sanity checking */ 801 if (UGETW(d->wTotalLength) < sizeof(*d)) { 802 err = USB_ERR_INVAL; 803 } 804done: 805 return (err); 806} 807 808/*------------------------------------------------------------------------* 809 * usb2_req_get_config_desc_full 810 * 811 * This function gets the complete USB configuration descriptor and 812 * ensures that "wTotalLength" is correct. 813 * 814 * Returns: 815 * 0: Success 816 * Else: Failure 817 *------------------------------------------------------------------------*/ 818usb2_error_t 819usb2_req_get_config_desc_full(struct usb2_device *udev, struct mtx *mtx, 820 struct usb2_config_descriptor **ppcd, struct malloc_type *mtype, 821 uint8_t index) 822{ 823 struct usb2_config_descriptor cd; 824 struct usb2_config_descriptor *cdesc; 825 uint16_t len; 826 usb2_error_t err; 827 828 DPRINTFN(4, "index=%d\n", index); 829 830 *ppcd = NULL; 831 832 err = usb2_req_get_config_desc(udev, mtx, &cd, index); 833 if (err) { 834 return (err); 835 } 836 /* get full descriptor */ 837 len = UGETW(cd.wTotalLength); 838 if (len < sizeof(*cdesc)) { 839 /* corrupt descriptor */ 840 return (USB_ERR_INVAL); 841 } 842 cdesc = malloc(len, mtype, M_WAITOK); 843 if (cdesc == NULL) { 844 return (USB_ERR_NOMEM); 845 } 846 err = usb2_req_get_desc(udev, mtx, cdesc, len, len, 0, 847 UDESC_CONFIG, index, 3); 848 if (err) { 849 free(cdesc, mtype); 850 return (err); 851 } 852 /* make sure that the device is not fooling us: */ 853 USETW(cdesc->wTotalLength, len); 854 855 *ppcd = cdesc; 856 857 return (0); /* success */ 858} 859 860/*------------------------------------------------------------------------* 861 * usb2_req_get_device_desc 862 * 863 * Returns: 864 * 0: Success 865 * Else: Failure 866 *------------------------------------------------------------------------*/ 867usb2_error_t 868usb2_req_get_device_desc(struct usb2_device *udev, struct mtx *mtx, 869 struct usb2_device_descriptor *d) 870{ 871 DPRINTFN(4, "\n"); 872 return (usb2_req_get_desc(udev, mtx, d, sizeof(*d), 873 sizeof(*d), 0, UDESC_DEVICE, 0, 3)); 874} 875 876/*------------------------------------------------------------------------* 877 * usb2_req_get_alt_interface_no 878 * 879 * Returns: 880 * 0: Success 881 * Else: Failure 882 *------------------------------------------------------------------------*/ 883usb2_error_t 884usb2_req_get_alt_interface_no(struct usb2_device *udev, struct mtx *mtx, 885 uint8_t *alt_iface_no, uint8_t iface_index) 886{ 887 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 888 struct usb2_device_request req; 889 890 if ((iface == NULL) || (iface->idesc == NULL)) { 891 return (USB_ERR_INVAL); 892 } 893 req.bmRequestType = UT_READ_INTERFACE; 894 req.bRequest = UR_GET_INTERFACE; 895 USETW(req.wValue, 0); 896 req.wIndex[0] = iface->idesc->bInterfaceNumber; 897 req.wIndex[1] = 0; 898 USETW(req.wLength, 1); 899 return (usb2_do_request(udev, mtx, &req, alt_iface_no)); 900} 901 902/*------------------------------------------------------------------------* 903 * usb2_req_set_alt_interface_no 904 * 905 * Returns: 906 * 0: Success 907 * Else: Failure 908 *------------------------------------------------------------------------*/ 909usb2_error_t 910usb2_req_set_alt_interface_no(struct usb2_device *udev, struct mtx *mtx, 911 uint8_t iface_index, uint8_t alt_no) 912{ 913 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 914 struct usb2_device_request req; 915 916 if ((iface == NULL) || (iface->idesc == NULL)) { 917 return (USB_ERR_INVAL); 918 } 919 req.bmRequestType = UT_WRITE_INTERFACE; 920 req.bRequest = UR_SET_INTERFACE; 921 req.wValue[0] = alt_no; 922 req.wValue[1] = 0; 923 req.wIndex[0] = iface->idesc->bInterfaceNumber; 924 req.wIndex[1] = 0; 925 USETW(req.wLength, 0); 926 return (usb2_do_request(udev, mtx, &req, 0)); 927} 928 929/*------------------------------------------------------------------------* 930 * usb2_req_get_device_status 931 * 932 * Returns: 933 * 0: Success 934 * Else: Failure 935 *------------------------------------------------------------------------*/ 936usb2_error_t 937usb2_req_get_device_status(struct usb2_device *udev, struct mtx *mtx, 938 struct usb2_status *st) 939{ 940 struct usb2_device_request req; 941 942 req.bmRequestType = UT_READ_DEVICE; 943 req.bRequest = UR_GET_STATUS; 944 USETW(req.wValue, 0); 945 USETW(req.wIndex, 0); 946 USETW(req.wLength, sizeof(*st)); 947 return (usb2_do_request(udev, mtx, &req, st)); 948} 949 950/*------------------------------------------------------------------------* 951 * usb2_req_get_hub_descriptor 952 * 953 * Returns: 954 * 0: Success 955 * Else: Failure 956 *------------------------------------------------------------------------*/ 957usb2_error_t 958usb2_req_get_hub_descriptor(struct usb2_device *udev, struct mtx *mtx, 959 struct usb2_hub_descriptor *hd, uint8_t nports) 960{ 961 struct usb2_device_request req; 962 uint16_t len = (nports + 7 + (8 * 8)) / 8; 963 964 req.bmRequestType = UT_READ_CLASS_DEVICE; 965 req.bRequest = UR_GET_DESCRIPTOR; 966 USETW2(req.wValue, UDESC_HUB, 0); 967 USETW(req.wIndex, 0); 968 USETW(req.wLength, len); 969 return (usb2_do_request(udev, mtx, &req, hd)); 970} 971 972/*------------------------------------------------------------------------* 973 * usb2_req_get_hub_status 974 * 975 * Returns: 976 * 0: Success 977 * Else: Failure 978 *------------------------------------------------------------------------*/ 979usb2_error_t 980usb2_req_get_hub_status(struct usb2_device *udev, struct mtx *mtx, 981 struct usb2_hub_status *st) 982{ 983 struct usb2_device_request req; 984 985 req.bmRequestType = UT_READ_CLASS_DEVICE; 986 req.bRequest = UR_GET_STATUS; 987 USETW(req.wValue, 0); 988 USETW(req.wIndex, 0); 989 USETW(req.wLength, sizeof(struct usb2_hub_status)); 990 return (usb2_do_request(udev, mtx, &req, st)); 991} 992 993/*------------------------------------------------------------------------* 994 * usb2_req_set_address 995 * 996 * This function is used to set the address for an USB device. After 997 * port reset the USB device will respond at address zero. 998 * 999 * Returns: 1000 * 0: Success 1001 * Else: Failure 1002 *------------------------------------------------------------------------*/ 1003usb2_error_t 1004usb2_req_set_address(struct usb2_device *udev, struct mtx *mtx, uint16_t addr) 1005{ 1006 struct usb2_device_request req; 1007 1008 DPRINTFN(6, "setting device address=%d\n", addr); 1009 1010 req.bmRequestType = UT_WRITE_DEVICE; 1011 req.bRequest = UR_SET_ADDRESS; 1012 USETW(req.wValue, addr); 1013 USETW(req.wIndex, 0); 1014 USETW(req.wLength, 0); 1015 1016 /* Setting the address should not take more than 1 second ! */ 1017 return (usb2_do_request_flags(udev, mtx, &req, NULL, 1018 USB_DELAY_STATUS_STAGE, NULL, 1000)); 1019} 1020 1021/*------------------------------------------------------------------------* 1022 * usb2_req_get_port_status 1023 * 1024 * Returns: 1025 * 0: Success 1026 * Else: Failure 1027 *------------------------------------------------------------------------*/ 1028usb2_error_t 1029usb2_req_get_port_status(struct usb2_device *udev, struct mtx *mtx, 1030 struct usb2_port_status *ps, uint8_t port) 1031{ 1032 struct usb2_device_request req; 1033 1034 req.bmRequestType = UT_READ_CLASS_OTHER; 1035 req.bRequest = UR_GET_STATUS; 1036 USETW(req.wValue, 0); 1037 req.wIndex[0] = port; 1038 req.wIndex[1] = 0; 1039 USETW(req.wLength, sizeof *ps); 1040 return (usb2_do_request(udev, mtx, &req, ps)); 1041} 1042 1043/*------------------------------------------------------------------------* 1044 * usb2_req_clear_hub_feature 1045 * 1046 * Returns: 1047 * 0: Success 1048 * Else: Failure 1049 *------------------------------------------------------------------------*/ 1050usb2_error_t 1051usb2_req_clear_hub_feature(struct usb2_device *udev, struct mtx *mtx, 1052 uint16_t sel) 1053{ 1054 struct usb2_device_request req; 1055 1056 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1057 req.bRequest = UR_CLEAR_FEATURE; 1058 USETW(req.wValue, sel); 1059 USETW(req.wIndex, 0); 1060 USETW(req.wLength, 0); 1061 return (usb2_do_request(udev, mtx, &req, 0)); 1062} 1063 1064/*------------------------------------------------------------------------* 1065 * usb2_req_set_hub_feature 1066 * 1067 * Returns: 1068 * 0: Success 1069 * Else: Failure 1070 *------------------------------------------------------------------------*/ 1071usb2_error_t 1072usb2_req_set_hub_feature(struct usb2_device *udev, struct mtx *mtx, 1073 uint16_t sel) 1074{ 1075 struct usb2_device_request req; 1076 1077 req.bmRequestType = UT_WRITE_CLASS_DEVICE; 1078 req.bRequest = UR_SET_FEATURE; 1079 USETW(req.wValue, sel); 1080 USETW(req.wIndex, 0); 1081 USETW(req.wLength, 0); 1082 return (usb2_do_request(udev, mtx, &req, 0)); 1083} 1084 1085/*------------------------------------------------------------------------* 1086 * usb2_req_clear_port_feature 1087 * 1088 * Returns: 1089 * 0: Success 1090 * Else: Failure 1091 *------------------------------------------------------------------------*/ 1092usb2_error_t 1093usb2_req_clear_port_feature(struct usb2_device *udev, struct mtx *mtx, 1094 uint8_t port, uint16_t sel) 1095{ 1096 struct usb2_device_request req; 1097 1098 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1099 req.bRequest = UR_CLEAR_FEATURE; 1100 USETW(req.wValue, sel); 1101 req.wIndex[0] = port; 1102 req.wIndex[1] = 0; 1103 USETW(req.wLength, 0); 1104 return (usb2_do_request(udev, mtx, &req, 0)); 1105} 1106 1107/*------------------------------------------------------------------------* 1108 * usb2_req_set_port_feature 1109 * 1110 * Returns: 1111 * 0: Success 1112 * Else: Failure 1113 *------------------------------------------------------------------------*/ 1114usb2_error_t 1115usb2_req_set_port_feature(struct usb2_device *udev, struct mtx *mtx, 1116 uint8_t port, uint16_t sel) 1117{ 1118 struct usb2_device_request req; 1119 1120 req.bmRequestType = UT_WRITE_CLASS_OTHER; 1121 req.bRequest = UR_SET_FEATURE; 1122 USETW(req.wValue, sel); 1123 req.wIndex[0] = port; 1124 req.wIndex[1] = 0; 1125 USETW(req.wLength, 0); 1126 return (usb2_do_request(udev, mtx, &req, 0)); 1127} 1128 1129/*------------------------------------------------------------------------* 1130 * usb2_req_set_protocol 1131 * 1132 * Returns: 1133 * 0: Success 1134 * Else: Failure 1135 *------------------------------------------------------------------------*/ 1136usb2_error_t 1137usb2_req_set_protocol(struct usb2_device *udev, struct mtx *mtx, 1138 uint8_t iface_index, uint16_t report) 1139{ 1140 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1141 struct usb2_device_request req; 1142 1143 if ((iface == NULL) || (iface->idesc == NULL)) { 1144 return (USB_ERR_INVAL); 1145 } 1146 DPRINTFN(5, "iface=%p, report=%d, endpt=%d\n", 1147 iface, report, iface->idesc->bInterfaceNumber); 1148 1149 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1150 req.bRequest = UR_SET_PROTOCOL; 1151 USETW(req.wValue, report); 1152 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1153 req.wIndex[1] = 0; 1154 USETW(req.wLength, 0); 1155 return (usb2_do_request(udev, mtx, &req, 0)); 1156} 1157 1158/*------------------------------------------------------------------------* 1159 * usb2_req_set_report 1160 * 1161 * Returns: 1162 * 0: Success 1163 * Else: Failure 1164 *------------------------------------------------------------------------*/ 1165usb2_error_t 1166usb2_req_set_report(struct usb2_device *udev, struct mtx *mtx, void *data, uint16_t len, 1167 uint8_t iface_index, uint8_t type, uint8_t id) 1168{ 1169 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1170 struct usb2_device_request req; 1171 1172 if ((iface == NULL) || (iface->idesc == NULL)) { 1173 return (USB_ERR_INVAL); 1174 } 1175 DPRINTFN(5, "len=%d\n", len); 1176 1177 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1178 req.bRequest = UR_SET_REPORT; 1179 USETW2(req.wValue, type, id); 1180 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1181 req.wIndex[1] = 0; 1182 USETW(req.wLength, len); 1183 return (usb2_do_request(udev, mtx, &req, data)); 1184} 1185 1186/*------------------------------------------------------------------------* 1187 * usb2_req_get_report 1188 * 1189 * Returns: 1190 * 0: Success 1191 * Else: Failure 1192 *------------------------------------------------------------------------*/ 1193usb2_error_t 1194usb2_req_get_report(struct usb2_device *udev, struct mtx *mtx, void *data, 1195 uint16_t len, uint8_t iface_index, uint8_t type, uint8_t id) 1196{ 1197 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1198 struct usb2_device_request req; 1199 1200 if ((iface == NULL) || (iface->idesc == NULL) || (id == 0)) { 1201 return (USB_ERR_INVAL); 1202 } 1203 DPRINTFN(5, "len=%d\n", len); 1204 1205 req.bmRequestType = UT_READ_CLASS_INTERFACE; 1206 req.bRequest = UR_GET_REPORT; 1207 USETW2(req.wValue, type, id); 1208 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1209 req.wIndex[1] = 0; 1210 USETW(req.wLength, len); 1211 return (usb2_do_request(udev, mtx, &req, data)); 1212} 1213 1214/*------------------------------------------------------------------------* 1215 * usb2_req_set_idle 1216 * 1217 * Returns: 1218 * 0: Success 1219 * Else: Failure 1220 *------------------------------------------------------------------------*/ 1221usb2_error_t 1222usb2_req_set_idle(struct usb2_device *udev, struct mtx *mtx, 1223 uint8_t iface_index, uint8_t duration, uint8_t id) 1224{ 1225 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1226 struct usb2_device_request req; 1227 1228 if ((iface == NULL) || (iface->idesc == NULL)) { 1229 return (USB_ERR_INVAL); 1230 } 1231 DPRINTFN(5, "%d %d\n", duration, id); 1232 1233 req.bmRequestType = UT_WRITE_CLASS_INTERFACE; 1234 req.bRequest = UR_SET_IDLE; 1235 USETW2(req.wValue, duration, id); 1236 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1237 req.wIndex[1] = 0; 1238 USETW(req.wLength, 0); 1239 return (usb2_do_request(udev, mtx, &req, 0)); 1240} 1241 1242/*------------------------------------------------------------------------* 1243 * usb2_req_get_report_descriptor 1244 * 1245 * Returns: 1246 * 0: Success 1247 * Else: Failure 1248 *------------------------------------------------------------------------*/ 1249usb2_error_t 1250usb2_req_get_report_descriptor(struct usb2_device *udev, struct mtx *mtx, 1251 void *d, uint16_t size, uint8_t iface_index) 1252{ 1253 struct usb2_interface *iface = usb2_get_iface(udev, iface_index); 1254 struct usb2_device_request req; 1255 1256 if ((iface == NULL) || (iface->idesc == NULL)) { 1257 return (USB_ERR_INVAL); 1258 } 1259 req.bmRequestType = UT_READ_INTERFACE; 1260 req.bRequest = UR_GET_DESCRIPTOR; 1261 USETW2(req.wValue, UDESC_REPORT, 0); /* report id should be 0 */ 1262 req.wIndex[0] = iface->idesc->bInterfaceNumber; 1263 req.wIndex[1] = 0; 1264 USETW(req.wLength, size); 1265 return (usb2_do_request(udev, mtx, &req, d)); 1266} 1267 1268/*------------------------------------------------------------------------* 1269 * usb2_req_set_config 1270 * 1271 * This function is used to select the current configuration number in 1272 * both USB device side mode and USB host side mode. When setting the 1273 * configuration the function of the interfaces can change. 1274 * 1275 * Returns: 1276 * 0: Success 1277 * Else: Failure 1278 *------------------------------------------------------------------------*/ 1279usb2_error_t 1280usb2_req_set_config(struct usb2_device *udev, struct mtx *mtx, uint8_t conf) 1281{ 1282 struct usb2_device_request req; 1283 1284 DPRINTF("setting config %d\n", conf); 1285 1286 /* do "set configuration" request */ 1287 1288 req.bmRequestType = UT_WRITE_DEVICE; 1289 req.bRequest = UR_SET_CONFIG; 1290 req.wValue[0] = conf; 1291 req.wValue[1] = 0; 1292 USETW(req.wIndex, 0); 1293 USETW(req.wLength, 0); 1294 return (usb2_do_request(udev, mtx, &req, 0)); 1295} 1296 1297/*------------------------------------------------------------------------* 1298 * usb2_req_get_config 1299 * 1300 * Returns: 1301 * 0: Success 1302 * Else: Failure 1303 *------------------------------------------------------------------------*/ 1304usb2_error_t 1305usb2_req_get_config(struct usb2_device *udev, struct mtx *mtx, uint8_t *pconf) 1306{ 1307 struct usb2_device_request req; 1308 1309 req.bmRequestType = UT_READ_DEVICE; 1310 req.bRequest = UR_GET_CONFIG; 1311 USETW(req.wValue, 0); 1312 USETW(req.wIndex, 0); 1313 USETW(req.wLength, 1); 1314 return (usb2_do_request(udev, mtx, &req, pconf)); 1315} 1316 1317/*------------------------------------------------------------------------* 1318 * usb2_req_re_enumerate 1319 * 1320 * NOTE: After this function returns the hardware is in the 1321 * unconfigured state! The application is responsible for setting a 1322 * new configuration. 1323 * 1324 * Returns: 1325 * 0: Success 1326 * Else: Failure 1327 *------------------------------------------------------------------------*/ 1328usb2_error_t 1329usb2_req_re_enumerate(struct usb2_device *udev, struct mtx *mtx) 1330{ 1331 struct usb2_device *parent_hub; 1332 usb2_error_t err; 1333 uint8_t old_addr; 1334 uint8_t do_retry = 1; 1335 1336 if (udev->flags.usb2_mode != USB_MODE_HOST) { 1337 return (USB_ERR_INVAL); 1338 } 1339 old_addr = udev->address; 1340 parent_hub = udev->parent_hub; 1341 if (parent_hub == NULL) { 1342 return (USB_ERR_INVAL); 1343 } 1344retry: 1345 err = usb2_req_reset_port(parent_hub, mtx, udev->port_no); 1346 if (err) { 1347 DPRINTFN(0, "addr=%d, port reset failed\n", old_addr); 1348 goto done; 1349 } 1350 /* 1351 * After that the port has been reset our device should be at 1352 * address zero: 1353 */ 1354 udev->address = USB_START_ADDR; 1355 1356 /* reset "bMaxPacketSize" */ 1357 udev->ddesc.bMaxPacketSize = USB_MAX_IPACKET; 1358 1359 /* 1360 * Restore device address: 1361 */ 1362 err = usb2_req_set_address(udev, mtx, old_addr); 1363 if (err) { 1364 /* XXX ignore any errors! */ 1365 DPRINTFN(0, "addr=%d, set address failed! (ignored)\n", 1366 old_addr); 1367 } 1368 /* restore device address */ 1369 udev->address = old_addr; 1370 1371 /* allow device time to set new address */ 1372 usb2_pause_mtx(mtx, USB_SET_ADDRESS_SETTLE); 1373 1374 /* get the device descriptor */ 1375 err = usb2_req_get_desc(udev, mtx, &udev->ddesc, 1376 USB_MAX_IPACKET, USB_MAX_IPACKET, 0, UDESC_DEVICE, 0, 0); 1377 if (err) { 1378 DPRINTFN(0, "getting device descriptor " 1379 "at addr %d failed!\n", udev->address); 1380 goto done; 1381 } 1382 /* get the full device descriptor */ 1383 err = usb2_req_get_device_desc(udev, mtx, &udev->ddesc); 1384 if (err) { 1385 DPRINTFN(0, "addr=%d, getting device " 1386 "descriptor failed!\n", old_addr); 1387 goto done; 1388 } 1389done: 1390 if (err && do_retry) { 1391 /* give the USB firmware some time to load */ 1392 usb2_pause_mtx(mtx, 500); 1393 /* no more retries after this retry */ 1394 do_retry = 0; 1395 /* try again */ 1396 goto retry; 1397 } 1398 /* restore address */ 1399 udev->address = old_addr; 1400 return (err); 1401} 1402 1403/*------------------------------------------------------------------------* 1404 * usb2_req_clear_device_feature 1405 * 1406 * Returns: 1407 * 0: Success 1408 * Else: Failure 1409 *------------------------------------------------------------------------*/ 1410usb2_error_t 1411usb2_req_clear_device_feature(struct usb2_device *udev, struct mtx *mtx, 1412 uint16_t sel) 1413{ 1414 struct usb2_device_request req; 1415 1416 req.bmRequestType = UT_WRITE_DEVICE; 1417 req.bRequest = UR_CLEAR_FEATURE; 1418 USETW(req.wValue, sel); 1419 USETW(req.wIndex, 0); 1420 USETW(req.wLength, 0); 1421 return (usb2_do_request(udev, mtx, &req, 0)); 1422} 1423 1424/*------------------------------------------------------------------------* 1425 * usb2_req_set_device_feature 1426 * 1427 * Returns: 1428 * 0: Success 1429 * Else: Failure 1430 *------------------------------------------------------------------------*/ 1431usb2_error_t 1432usb2_req_set_device_feature(struct usb2_device *udev, struct mtx *mtx, 1433 uint16_t sel) 1434{ 1435 struct usb2_device_request req; 1436 1437 req.bmRequestType = UT_WRITE_DEVICE; 1438 req.bRequest = UR_SET_FEATURE; 1439 USETW(req.wValue, sel); 1440 USETW(req.wIndex, 0); 1441 USETW(req.wLength, 0); 1442 return (usb2_do_request(udev, mtx, &req, 0)); 1443}
|