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