sys/bus/u4b/usb_request.c
usb_error_t
usbd_req_get_desc(struct usb_device *udev,
    struct lock *lock, uint16_t *actlen, void *desc,
    uint16_t min_len, uint16_t max_len,
    uint16_t id, uint8_t type, uint8_t index,
    uint8_t retries)
{
	struct usb_device_request req;
	uint8_t *buf;
	usb_error_t err;

	DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
	    id, type, index, max_len);

	req.bmRequestType = UT_READ_DEVICE;
	req.bRequest = UR_GET_DESCRIPTOR;
	USETW2(req.wValue, type, index);
	USETW(req.wIndex, id);

	while (1) {

		if ((min_len < 2) || (max_len < 2)) {
			err = USB_ERR_INVAL;
			goto done;
		}
		USETW(req.wLength, min_len);

		err = usbd_do_request_flags(udev, lock, &req,
		    desc, 0, NULL, 1000);

		if (err) {
			if (!retries) {
				goto done;
			}
			retries--;

			usb_pause_mtx(lock, hz / 5);

			continue;
		}
		buf = desc;

		if (min_len == max_len) {

			/* enforce correct length */
			if ((buf[0] > min_len) && (actlen == NULL))
				buf[0] = min_len;

			/* enforce correct type */
			buf[1] = type;

			goto done;
		}
		/* range check */

		if (max_len > buf[0]) {
			max_len = buf[0];
		}
		/* zero minimum data */

		while (min_len > max_len) {
			min_len--;
			buf[min_len] = 0;
		}

		/* set new minimum length */

		min_len = max_len;
	}
done:
	if (actlen != NULL) {
		if (err)
			*actlen = 0;
		else
			*actlen = min_len;
	}
	return (err);
}
/*------------------------------------------------------------------------*
 *	usbd_req_get_desc
 *
 * This function can be used to retrieve USB descriptors. It contains
 * some additional logic like zeroing of missing descriptor bytes and
 * retrying an USB descriptor in case of failure. The "min_len"
 * argument specifies the minimum descriptor length. The "max_len"
 * argument specifies the maximum descriptor length. If the real
 * descriptor length is less than the minimum length the missing
 * byte(s) will be zeroed. The type field, the second byte of the USB
 * descriptor, will get forced to the correct type. If the "actlen"
 * pointer is non-NULL, the actual length of the transfer will get
 * stored in the 16-bit unsigned integer which it is pointing to. The
 * first byte of the descriptor will not get updated. If the "actlen"
 * pointer is NULL the first byte of the descriptor will get updated
 * to reflect the actual length instead. If "min_len" is not equal to
 * "max_len" then this function will try to retrive the beginning of
 * the descriptor and base the maximum length on the first byte of the
 * descriptor.
 *
 * Returns:
 *    0: Success
 * Else: Failure
 *------------------------------------------------------------------------*/
usbd_req_get_desc
This function can be used to retrieve USB descriptors. It contains
some additional logic like zeroing of missing descriptor bytes and
retrying an USB descriptor in case of failure. The "min_len"
argument specifies the minimum descriptor length. The "max_len"
argument specifies the maximum descriptor length. If the real
descriptor length is less than the minimum length the missing
byte(s) will be zeroed. The type field, the second byte of the USB
descriptor, will get forced to the correct type. If the "actlen"
pointer is non-NULL, the actual length of the transfer will get
stored in the 16-bit unsigned integer which it is pointing to. The
first byte of the descriptor will not get updated. If the "actlen"
pointer is NULL the first byte of the descriptor will get updated
to reflect the actual length instead. If "min_len" is not equal to
"max_len" then this function will try to retrive the beginning of
the descriptor and base the maximum length on the first byte of the
descriptor.

Success
Else: Failure
usbd_req_get_desc This function can be used to retrieve USB descriptors . It contains some additional logic like zeroing of missing descriptor bytes and retrying an USB descriptor in case of failure . The " min_len " argument specifies the minimum descriptor length . The " max_len " argument specifies the maximum descriptor length . If the real descriptor length is less than the minimum length the missing byte ( s ) will be zeroed . The type field the second byte of the USB descriptor will get forced to the correct type . If the " actlen " pointer is non - NULL the actual length of the transfer will get stored in the 16 - bit unsigned integer which it is pointing to . The first byte of the descriptor will not get updated . If the " actlen " pointer is NULL the first byte of the descriptor will get updated to reflect the actual length instead . If " min_len " is not equal to " max_len " then this function will try to retrive the beginning of the descriptor and base the maximum length on the first byte of the descriptor . Success Else : Failure
usb_error_t
usbd_req_get_desc(struct usb_device *udev,
    struct lock *lock, uint16_t *actlen, void *desc,
    uint16_t min_len, uint16_t max_len,
    uint16_t id, uint8_t type, uint8_t index,
    uint8_t retries)
{
	struct usb_device_request req;
	uint8_t *buf;
	usb_error_t err;
	DPRINTFN(4, "id=%d, type=%d, index=%d, max_len=%d\n",
	    id, type, index, max_len);
	req.bmRequestType = UT_READ_DEVICE;
	req.bRequest = UR_GET_DESCRIPTOR;
	USETW2(req.wValue, type, index);
	USETW(req.wIndex, id);
	while (1) {
		if ((min_len < 2) || (max_len < 2)) {
			err = USB_ERR_INVAL;
			goto done;
		}
		USETW(req.wLength, min_len);
		err = usbd_do_request_flags(udev, lock, &req,
		    desc, 0, NULL, 1000);
		if (err) {
			if (!retries) {
				goto done;
			}
			retries--;
			usb_pause_mtx(lock, hz / 5);
			continue;
		}
		buf = desc;
		if (min_len == max_len) {
			if ((buf[0] > min_len) && (actlen == NULL))
				buf[0] = min_len;
			buf[1] = type;
			goto done;
		}
		if (max_len > buf[0]) {
			max_len = buf[0];
		}
		while (min_len > max_len) {
			min_len--;
			buf[min_len] = 0;
		}
		min_len = max_len;
	}
done:
	if (actlen != NULL) {
		if (err)
			*actlen = 0;
		else
			*actlen = min_len;
	}
	return (err);
}
usb_error_t usbd_req_get_desc ( struct usb_device * udev , struct lock * lock , uint16_t * actlen , void * desc , uint16_t min_len , uint16_t max_len , uint16_t id , uint8_t type , uint8_t index , uint8_t retries ) { struct usb_device_request req ; uint8_t * buf ; usb_error_t err ; DPRINTFN ( 4 , " \n " , id , type , index , max_len ) ; req . bmRequestType = UT_READ_DEVICE ; req . bRequest = UR_GET_DESCRIPTOR ; USETW2 ( req . wValue , type , index ) ; USETW ( req . wIndex , id ) ; while ( 1 ) { if ( ( min_len < 2 ) || ( max_len < 2 ) ) { err = USB_ERR_INVAL ; goto done ; } USETW ( req . wLength , min_len ) ; err = usbd_do_request_flags ( udev , lock , & req , desc , 0 , NULL , 1000 ) ; if ( err ) { if ( ! retries ) { goto done ; } retries -- ; usb_pause_mtx ( lock , hz / 5 ) ; continue ; } buf = desc ; if ( min_len == max_len ) { if ( ( buf [ 0 ] > min_len ) && ( actlen == NULL ) ) buf [ 0 ] = min_len ; buf [ 1 ] = type ; goto done ; } if ( max_len > buf [ 0 ] ) { max_len = buf [ 0 ] ; } while ( min_len > max_len ) { min_len -- ; buf [ min_len ] = 0 ; } min_len = max_len ; } done : if ( actlen != NULL ) { if ( err ) * actlen = 0 ; else * actlen = min_len ; } return ( err ) ; }
usbd_req_get_desc
 This function can be used to retrieve USB descriptors.
usbd_req_get_desc This function can be used to retrieve USB descriptors .
/* enforce correct length */ /* enforce correct type */ /* range check */ /* zero minimum data */ /* set new minimum length */