Blame libusb/descriptor.c

Packit Service b0a153
/* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
Packit Service b0a153
/*
Packit Service b0a153
 * USB descriptor handling functions for libusb
Packit Service b0a153
 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
Packit Service b0a153
 * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
Packit Service b0a153
 *
Packit Service b0a153
 * This library is free software; you can redistribute it and/or
Packit Service b0a153
 * modify it under the terms of the GNU Lesser General Public
Packit Service b0a153
 * License as published by the Free Software Foundation; either
Packit Service b0a153
 * version 2.1 of the License, or (at your option) any later version.
Packit Service b0a153
 *
Packit Service b0a153
 * This library is distributed in the hope that it will be useful,
Packit Service b0a153
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit Service b0a153
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit Service b0a153
 * Lesser General Public License for more details.
Packit Service b0a153
 *
Packit Service b0a153
 * You should have received a copy of the GNU Lesser General Public
Packit Service b0a153
 * License along with this library; if not, write to the Free Software
Packit Service b0a153
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
Packit Service b0a153
 */
Packit Service b0a153
Packit Service b0a153
#include <config.h>
Packit Service b0a153
Packit Service b0a153
#include <errno.h>
Packit Service b0a153
#include <stdint.h>
Packit Service b0a153
#include <stdlib.h>
Packit Service b0a153
#include <string.h>
Packit Service b0a153
Packit Service b0a153
#include "libusbi.h"
Packit Service b0a153
Packit Service b0a153
#define DESC_HEADER_LENGTH		2
Packit Service b0a153
#define DEVICE_DESC_LENGTH		18
Packit Service b0a153
#define CONFIG_DESC_LENGTH		9
Packit Service b0a153
#define INTERFACE_DESC_LENGTH		9
Packit Service b0a153
#define ENDPOINT_DESC_LENGTH		7
Packit Service b0a153
#define ENDPOINT_AUDIO_DESC_LENGTH	9
Packit Service b0a153
Packit Service b0a153
/** @defgroup libusb_desc USB descriptors
Packit Service b0a153
 * This page details how to examine the various standard USB descriptors
Packit Service b0a153
 * for detected devices
Packit Service b0a153
 */
Packit Service b0a153
Packit Service b0a153
/* set host_endian if the w values are already in host endian format,
Packit Service b0a153
 * as opposed to bus endian. */
Packit Service b0a153
int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
Packit Service b0a153
	void *dest, int host_endian)
Packit Service b0a153
{
Packit Service b0a153
	const unsigned char *sp = source;
Packit Service b0a153
	unsigned char *dp = dest;
Packit Service b0a153
	uint16_t w;
Packit Service b0a153
	const char *cp;
Packit Service b0a153
	uint32_t d;
Packit Service b0a153
Packit Service b0a153
	for (cp = descriptor; *cp; cp++) {
Packit Service b0a153
		switch (*cp) {
Packit Service b0a153
			case 'b':	/* 8-bit byte */
Packit Service b0a153
				*dp++ = *sp++;
Packit Service b0a153
				break;
Packit Service b0a153
			case 'w':	/* 16-bit word, convert from little endian to CPU */
Packit Service b0a153
				dp += ((uintptr_t)dp & 1);	/* Align to word boundary */
Packit Service b0a153
Packit Service b0a153
				if (host_endian) {
Packit Service b0a153
					memcpy(dp, sp, 2);
Packit Service b0a153
				} else {
Packit Service b0a153
					w = (uint16_t)((sp[1] << 8) | sp[0]);
Packit Service b0a153
					*((uint16_t *)dp) = w;
Packit Service b0a153
				}
Packit Service b0a153
				sp += 2;
Packit Service b0a153
				dp += 2;
Packit Service b0a153
				break;
Packit Service b0a153
			case 'd':	/* 32-bit word, convert from little endian to CPU */
Packit Service b0a153
				dp += ((uintptr_t)dp & 1);	/* Align to word boundary */
Packit Service b0a153
Packit Service b0a153
				if (host_endian) {
Packit Service b0a153
					memcpy(dp, sp, 4);
Packit Service b0a153
				} else {
Packit Service b0a153
					d = (uint32_t)((sp[3] << 24) | (sp[2] << 16) |
Packit Service b0a153
								   (sp[1] << 8) | sp[0]);
Packit Service b0a153
					*((uint32_t *)dp) = d;
Packit Service b0a153
				}
Packit Service b0a153
				sp += 4;
Packit Service b0a153
				dp += 4;
Packit Service b0a153
				break;
Packit Service b0a153
			case 'u':	/* 16 byte UUID */
Packit Service b0a153
				memcpy(dp, sp, 16);
Packit Service b0a153
				sp += 16;
Packit Service b0a153
				dp += 16;
Packit Service b0a153
				break;
Packit Service b0a153
		}
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	return (int) (sp - source);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
Packit Service b0a153
{
Packit Service b0a153
	free((void *) endpoint->extra);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static int parse_endpoint(struct libusb_context *ctx,
Packit Service b0a153
	struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
Packit Service b0a153
	int size, int host_endian)
Packit Service b0a153
{
Packit Service b0a153
	struct usb_descriptor_header header;
Packit Service b0a153
	unsigned char *extra;
Packit Service b0a153
	unsigned char *begin;
Packit Service b0a153
	int parsed = 0;
Packit Service b0a153
	int len;
Packit Service b0a153
Packit Service b0a153
	if (size < DESC_HEADER_LENGTH) {
Packit Service b0a153
		usbi_err(ctx, "short endpoint descriptor read %d/%d",
Packit Service b0a153
			 size, DESC_HEADER_LENGTH);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor(buffer, "bb", &header, 0);
Packit Service b0a153
	if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
Packit Service b0a153
		usbi_err(ctx, "unexpected descriptor %x (expected %x)",
Packit Service b0a153
			header.bDescriptorType, LIBUSB_DT_ENDPOINT);
Packit Service b0a153
		return parsed;
Packit Service b0a153
	}
Packit Service b0a153
	if (header.bLength > size) {
Packit Service b0a153
		usbi_warn(ctx, "short endpoint descriptor read %d/%d",
Packit Service b0a153
			  size, header.bLength);
Packit Service b0a153
		return parsed;
Packit Service b0a153
	}
Packit Service b0a153
	if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
Packit Service b0a153
		usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
Packit Service b0a153
	else if (header.bLength >= ENDPOINT_DESC_LENGTH)
Packit Service b0a153
		usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
Packit Service b0a153
	else {
Packit Service b0a153
		usbi_err(ctx, "invalid endpoint bLength (%d)", header.bLength);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	buffer += header.bLength;
Packit Service b0a153
	size -= header.bLength;
Packit Service b0a153
	parsed += header.bLength;
Packit Service b0a153
Packit Service b0a153
	/* Skip over the rest of the Class Specific or Vendor Specific */
Packit Service b0a153
	/*  descriptors */
Packit Service b0a153
	begin = buffer;
Packit Service b0a153
	while (size >= DESC_HEADER_LENGTH) {
Packit Service b0a153
		usbi_parse_descriptor(buffer, "bb", &header, 0);
Packit Service b0a153
		if (header.bLength < DESC_HEADER_LENGTH) {
Packit Service b0a153
			usbi_err(ctx, "invalid extra ep desc len (%d)",
Packit Service b0a153
				 header.bLength);
Packit Service b0a153
			return LIBUSB_ERROR_IO;
Packit Service b0a153
		} else if (header.bLength > size) {
Packit Service b0a153
			usbi_warn(ctx, "short extra ep desc read %d/%d",
Packit Service b0a153
				  size, header.bLength);
Packit Service b0a153
			return parsed;
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		/* If we find another "proper" descriptor then we're done  */
Packit Service b0a153
		if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
Packit Service b0a153
				(header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
Packit Service b0a153
				(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
Packit Service b0a153
				(header.bDescriptorType == LIBUSB_DT_DEVICE))
Packit Service b0a153
			break;
Packit Service b0a153
Packit Service b0a153
		usbi_dbg("skipping descriptor %x", header.bDescriptorType);
Packit Service b0a153
		buffer += header.bLength;
Packit Service b0a153
		size -= header.bLength;
Packit Service b0a153
		parsed += header.bLength;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	/* Copy any unknown descriptors into a storage area for drivers */
Packit Service b0a153
	/*  to later parse */
Packit Service b0a153
	len = (int)(buffer - begin);
Packit Service b0a153
	if (len <= 0) {
Packit Service b0a153
		endpoint->extra = NULL;
Packit Service b0a153
		endpoint->extra_length = 0;
Packit Service b0a153
		return parsed;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	extra = malloc((size_t)len);
Packit Service b0a153
	endpoint->extra = extra;
Packit Service b0a153
	if (!extra) {
Packit Service b0a153
		endpoint->extra_length = 0;
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	memcpy(extra, begin, len);
Packit Service b0a153
	endpoint->extra_length = len;
Packit Service b0a153
Packit Service b0a153
	return parsed;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static void clear_interface(struct libusb_interface *usb_interface)
Packit Service b0a153
{
Packit Service b0a153
	int i;
Packit Service b0a153
	int j;
Packit Service b0a153
Packit Service b0a153
	if (usb_interface->altsetting) {
Packit Service b0a153
		for (i = 0; i < usb_interface->num_altsetting; i++) {
Packit Service b0a153
			struct libusb_interface_descriptor *ifp =
Packit Service b0a153
				(struct libusb_interface_descriptor *)
Packit Service b0a153
				usb_interface->altsetting + i;
Packit Service b0a153
			free((void *) ifp->extra);
Packit Service b0a153
			if (ifp->endpoint) {
Packit Service b0a153
				for (j = 0; j < ifp->bNumEndpoints; j++)
Packit Service b0a153
					clear_endpoint((struct libusb_endpoint_descriptor *)
Packit Service b0a153
						       ifp->endpoint + j);
Packit Service b0a153
			}
Packit Service b0a153
			free((void *) ifp->endpoint);
Packit Service b0a153
		}
Packit Service b0a153
	}
Packit Service b0a153
	free((void *) usb_interface->altsetting);
Packit Service b0a153
	usb_interface->altsetting = NULL;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static int parse_interface(libusb_context *ctx,
Packit Service b0a153
	struct libusb_interface *usb_interface, unsigned char *buffer, int size,
Packit Service b0a153
	int host_endian)
Packit Service b0a153
{
Packit Service b0a153
	int i;
Packit Service b0a153
	int len;
Packit Service b0a153
	int r;
Packit Service b0a153
	int parsed = 0;
Packit Service b0a153
	int interface_number = -1;
Packit Service b0a153
	struct usb_descriptor_header header;
Packit Service b0a153
	struct libusb_interface_descriptor *ifp;
Packit Service b0a153
	unsigned char *begin;
Packit Service b0a153
Packit Service b0a153
	usb_interface->num_altsetting = 0;
Packit Service b0a153
Packit Service b0a153
	while (size >= INTERFACE_DESC_LENGTH) {
Packit Service b0a153
		struct libusb_interface_descriptor *altsetting =
Packit Service b0a153
			(struct libusb_interface_descriptor *) usb_interface->altsetting;
Packit Service b0a153
		altsetting = usbi_reallocf(altsetting,
Packit Service b0a153
			sizeof(struct libusb_interface_descriptor) *
Packit Service b0a153
			((size_t)usb_interface->num_altsetting + 1));
Packit Service b0a153
		if (!altsetting) {
Packit Service b0a153
			r = LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
			goto err;
Packit Service b0a153
		}
Packit Service b0a153
		usb_interface->altsetting = altsetting;
Packit Service b0a153
Packit Service b0a153
		ifp = altsetting + usb_interface->num_altsetting;
Packit Service b0a153
		usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
Packit Service b0a153
		if (ifp->bDescriptorType != LIBUSB_DT_INTERFACE) {
Packit Service b0a153
			usbi_err(ctx, "unexpected descriptor %x (expected %x)",
Packit Service b0a153
				 ifp->bDescriptorType, LIBUSB_DT_INTERFACE);
Packit Service b0a153
			return parsed;
Packit Service b0a153
		}
Packit Service b0a153
		if (ifp->bLength < INTERFACE_DESC_LENGTH) {
Packit Service b0a153
			usbi_err(ctx, "invalid interface bLength (%d)",
Packit Service b0a153
				 ifp->bLength);
Packit Service b0a153
			r = LIBUSB_ERROR_IO;
Packit Service b0a153
			goto err;
Packit Service b0a153
		}
Packit Service b0a153
		if (ifp->bLength > size) {
Packit Service b0a153
			usbi_warn(ctx, "short intf descriptor read %d/%d",
Packit Service b0a153
				 size, ifp->bLength);
Packit Service b0a153
			return parsed;
Packit Service b0a153
		}
Packit Service b0a153
		if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
Packit Service b0a153
			usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);
Packit Service b0a153
			r = LIBUSB_ERROR_IO;
Packit Service b0a153
			goto err;
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		usb_interface->num_altsetting++;
Packit Service b0a153
		ifp->extra = NULL;
Packit Service b0a153
		ifp->extra_length = 0;
Packit Service b0a153
		ifp->endpoint = NULL;
Packit Service b0a153
Packit Service b0a153
		if (interface_number == -1)
Packit Service b0a153
			interface_number = ifp->bInterfaceNumber;
Packit Service b0a153
Packit Service b0a153
		/* Skip over the interface */
Packit Service b0a153
		buffer += ifp->bLength;
Packit Service b0a153
		parsed += ifp->bLength;
Packit Service b0a153
		size -= ifp->bLength;
Packit Service b0a153
Packit Service b0a153
		begin = buffer;
Packit Service b0a153
Packit Service b0a153
		/* Skip over any interface, class or vendor descriptors */
Packit Service b0a153
		while (size >= DESC_HEADER_LENGTH) {
Packit Service b0a153
			usbi_parse_descriptor(buffer, "bb", &header, 0);
Packit Service b0a153
			if (header.bLength < DESC_HEADER_LENGTH) {
Packit Service b0a153
				usbi_err(ctx,
Packit Service b0a153
					 "invalid extra intf desc len (%d)",
Packit Service b0a153
					 header.bLength);
Packit Service b0a153
				r = LIBUSB_ERROR_IO;
Packit Service b0a153
				goto err;
Packit Service b0a153
			} else if (header.bLength > size) {
Packit Service b0a153
				usbi_warn(ctx,
Packit Service b0a153
					  "short extra intf desc read %d/%d",
Packit Service b0a153
					  size, header.bLength);
Packit Service b0a153
				return parsed;
Packit Service b0a153
			}
Packit Service b0a153
Packit Service b0a153
			/* If we find another "proper" descriptor then we're done */
Packit Service b0a153
			if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
Packit Service b0a153
					(header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
Packit Service b0a153
					(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
Packit Service b0a153
					(header.bDescriptorType == LIBUSB_DT_DEVICE))
Packit Service b0a153
				break;
Packit Service b0a153
Packit Service b0a153
			buffer += header.bLength;
Packit Service b0a153
			parsed += header.bLength;
Packit Service b0a153
			size -= header.bLength;
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		/* Copy any unknown descriptors into a storage area for */
Packit Service b0a153
		/*  drivers to later parse */
Packit Service b0a153
		len = (int)(buffer - begin);
Packit Service b0a153
		if (len > 0) {
Packit Service b0a153
			ifp->extra = malloc((size_t)len);
Packit Service b0a153
			if (!ifp->extra) {
Packit Service b0a153
				r = LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
				goto err;
Packit Service b0a153
			}
Packit Service b0a153
			memcpy((unsigned char *) ifp->extra, begin, len);
Packit Service b0a153
			ifp->extra_length = len;
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		if (ifp->bNumEndpoints > 0) {
Packit Service b0a153
			struct libusb_endpoint_descriptor *endpoint;
Packit Service b0a153
			endpoint = calloc(ifp->bNumEndpoints, sizeof(struct libusb_endpoint_descriptor));
Packit Service b0a153
			ifp->endpoint = endpoint;
Packit Service b0a153
			if (!endpoint) {
Packit Service b0a153
				r = LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
				goto err;
Packit Service b0a153
			}
Packit Service b0a153
Packit Service b0a153
			for (i = 0; i < ifp->bNumEndpoints; i++) {
Packit Service b0a153
				r = parse_endpoint(ctx, endpoint + i, buffer, size,
Packit Service b0a153
					host_endian);
Packit Service b0a153
				if (r < 0)
Packit Service b0a153
					goto err;
Packit Service b0a153
				if (r == 0) {
Packit Service b0a153
					ifp->bNumEndpoints = (uint8_t)i;
Packit Service b0a153
					break;
Packit Service b0a153
				}
Packit Service b0a153
Packit Service b0a153
				buffer += r;
Packit Service b0a153
				parsed += r;
Packit Service b0a153
				size -= r;
Packit Service b0a153
			}
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		/* We check to see if it's an alternate to this one */
Packit Service b0a153
		ifp = (struct libusb_interface_descriptor *) buffer;
Packit Service b0a153
		if (size < LIBUSB_DT_INTERFACE_SIZE ||
Packit Service b0a153
				ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
Packit Service b0a153
				ifp->bInterfaceNumber != interface_number)
Packit Service b0a153
			return parsed;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	return parsed;
Packit Service b0a153
err:
Packit Service b0a153
	clear_interface(usb_interface);
Packit Service b0a153
	return r;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static void clear_configuration(struct libusb_config_descriptor *config)
Packit Service b0a153
{
Packit Service b0a153
	int i;
Packit Service b0a153
	if (config->interface) {
Packit Service b0a153
		for (i = 0; i < config->bNumInterfaces; i++)
Packit Service b0a153
			clear_interface((struct libusb_interface *)
Packit Service b0a153
					config->interface + i);
Packit Service b0a153
	}
Packit Service b0a153
	free((void *) config->interface);
Packit Service b0a153
	free((void *) config->extra);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static int parse_configuration(struct libusb_context *ctx,
Packit Service b0a153
	struct libusb_config_descriptor *config, unsigned char *buffer,
Packit Service b0a153
	int size, int host_endian)
Packit Service b0a153
{
Packit Service b0a153
	int i;
Packit Service b0a153
	int r;
Packit Service b0a153
	struct usb_descriptor_header header;
Packit Service b0a153
	struct libusb_interface *usb_interface;
Packit Service b0a153
Packit Service b0a153
	if (size < LIBUSB_DT_CONFIG_SIZE) {
Packit Service b0a153
		usbi_err(ctx, "short config descriptor read %d/%d",
Packit Service b0a153
			 size, LIBUSB_DT_CONFIG_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
Packit Service b0a153
	if (config->bDescriptorType != LIBUSB_DT_CONFIG) {
Packit Service b0a153
		usbi_err(ctx, "unexpected descriptor %x (expected %x)",
Packit Service b0a153
			 config->bDescriptorType, LIBUSB_DT_CONFIG);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
	if (config->bLength < LIBUSB_DT_CONFIG_SIZE) {
Packit Service b0a153
		usbi_err(ctx, "invalid config bLength (%d)", config->bLength);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
	if (config->bLength > size) {
Packit Service b0a153
		usbi_err(ctx, "short config descriptor read %d/%d",
Packit Service b0a153
			 size, config->bLength);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
	if (config->bNumInterfaces > USB_MAXINTERFACES) {
Packit Service b0a153
		usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	usb_interface = calloc(config->bNumInterfaces, sizeof(struct libusb_interface));
Packit Service b0a153
	config->interface = usb_interface;
Packit Service b0a153
	if (!usb_interface)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	buffer += config->bLength;
Packit Service b0a153
	size -= config->bLength;
Packit Service b0a153
Packit Service b0a153
	config->extra = NULL;
Packit Service b0a153
	config->extra_length = 0;
Packit Service b0a153
Packit Service b0a153
	for (i = 0; i < config->bNumInterfaces; i++) {
Packit Service b0a153
		int len;
Packit Service b0a153
		unsigned char *begin;
Packit Service b0a153
Packit Service b0a153
		/* Skip over the rest of the Class Specific or Vendor */
Packit Service b0a153
		/*  Specific descriptors */
Packit Service b0a153
		begin = buffer;
Packit Service b0a153
		while (size >= DESC_HEADER_LENGTH) {
Packit Service b0a153
			usbi_parse_descriptor(buffer, "bb", &header, 0);
Packit Service b0a153
Packit Service b0a153
			if (header.bLength < DESC_HEADER_LENGTH) {
Packit Service b0a153
				usbi_err(ctx,
Packit Service b0a153
					 "invalid extra config desc len (%d)",
Packit Service b0a153
					 header.bLength);
Packit Service b0a153
				r = LIBUSB_ERROR_IO;
Packit Service b0a153
				goto err;
Packit Service b0a153
			} else if (header.bLength > size) {
Packit Service b0a153
				usbi_warn(ctx,
Packit Service b0a153
					  "short extra config desc read %d/%d",
Packit Service b0a153
					  size, header.bLength);
Packit Service b0a153
				config->bNumInterfaces = (uint8_t)i;
Packit Service b0a153
				return size;
Packit Service b0a153
			}
Packit Service b0a153
Packit Service b0a153
			/* If we find another "proper" descriptor then we're done */
Packit Service b0a153
			if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
Packit Service b0a153
					(header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
Packit Service b0a153
					(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
Packit Service b0a153
					(header.bDescriptorType == LIBUSB_DT_DEVICE))
Packit Service b0a153
				break;
Packit Service b0a153
Packit Service b0a153
			usbi_dbg("skipping descriptor 0x%x", header.bDescriptorType);
Packit Service b0a153
			buffer += header.bLength;
Packit Service b0a153
			size -= header.bLength;
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		/* Copy any unknown descriptors into a storage area for */
Packit Service b0a153
		/*  drivers to later parse */
Packit Service b0a153
		len = (int)(buffer - begin);
Packit Service b0a153
		if (len > 0) {
Packit Service b0a153
			/* FIXME: We should realloc and append here */
Packit Service b0a153
			if (!config->extra_length) {
Packit Service b0a153
				config->extra = malloc((size_t)len);
Packit Service b0a153
				if (!config->extra) {
Packit Service b0a153
					r = LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
					goto err;
Packit Service b0a153
				}
Packit Service b0a153
Packit Service b0a153
				memcpy((unsigned char *) config->extra, begin, len);
Packit Service b0a153
				config->extra_length = len;
Packit Service b0a153
			}
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		r = parse_interface(ctx, usb_interface + i, buffer, size, host_endian);
Packit Service b0a153
		if (r < 0)
Packit Service b0a153
			goto err;
Packit Service b0a153
		if (r == 0) {
Packit Service b0a153
			config->bNumInterfaces = (uint8_t)i;
Packit Service b0a153
			break;
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		buffer += r;
Packit Service b0a153
		size -= r;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	return size;
Packit Service b0a153
Packit Service b0a153
err:
Packit Service b0a153
	clear_configuration(config);
Packit Service b0a153
	return r;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static int raw_desc_to_config(struct libusb_context *ctx,
Packit Service b0a153
	unsigned char *buf, int size, int host_endian,
Packit Service b0a153
	struct libusb_config_descriptor **config)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
Packit Service b0a153
	int r;
Packit Service b0a153
Packit Service b0a153
	if (!_config)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	r = parse_configuration(ctx, _config, buf, size, host_endian);
Packit Service b0a153
	if (r < 0) {
Packit Service b0a153
		usbi_err(ctx, "parse_configuration failed with error %d", r);
Packit Service b0a153
		free(_config);
Packit Service b0a153
		return r;
Packit Service b0a153
	} else if (r > 0) {
Packit Service b0a153
		usbi_warn(ctx, "still %d bytes of descriptor data left", r);
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	*config = _config;
Packit Service b0a153
	return LIBUSB_SUCCESS;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
int usbi_device_cache_descriptor(libusb_device *dev)
Packit Service b0a153
{
Packit Service b0a153
	int r, host_endian = 0;
Packit Service b0a153
Packit Service b0a153
	r = usbi_backend.get_device_descriptor(dev, (unsigned char *) &dev->device_descriptor,
Packit Service b0a153
						&host_endian);
Packit Service b0a153
	if (r < 0)
Packit Service b0a153
		return r;
Packit Service b0a153
Packit Service b0a153
	if (!host_endian) {
Packit Service b0a153
		dev->device_descriptor.bcdUSB = libusb_le16_to_cpu(dev->device_descriptor.bcdUSB);
Packit Service b0a153
		dev->device_descriptor.idVendor = libusb_le16_to_cpu(dev->device_descriptor.idVendor);
Packit Service b0a153
		dev->device_descriptor.idProduct = libusb_le16_to_cpu(dev->device_descriptor.idProduct);
Packit Service b0a153
		dev->device_descriptor.bcdDevice = libusb_le16_to_cpu(dev->device_descriptor.bcdDevice);
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	return LIBUSB_SUCCESS;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get the USB device descriptor for a given device.
Packit Service b0a153
 *
Packit Service b0a153
 * This is a non-blocking function; the device descriptor is cached in memory.
Packit Service b0a153
 *
Packit Service b0a153
 * Note since libusb-1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102, this
Packit Service b0a153
 * function always succeeds.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev the device
Packit Service b0a153
 * \param desc output location for the descriptor data
Packit Service b0a153
 * \returns 0 on success or a LIBUSB_ERROR code on failure
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
Packit Service b0a153
	struct libusb_device_descriptor *desc)
Packit Service b0a153
{
Greg Kroah-Hartman 3d962d
	usbi_dbg(" ");
Packit Service b0a153
	memcpy((unsigned char *) desc, (unsigned char *) &dev->device_descriptor,
Packit Service b0a153
	       sizeof (dev->device_descriptor));
Packit Service b0a153
	return 0;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get the USB configuration descriptor for the currently active configuration.
Packit Service b0a153
 * This is a non-blocking function which does not involve any requests being
Packit Service b0a153
 * sent to the device.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev a device
Packit Service b0a153
 * \param config output location for the USB configuration descriptor. Only
Packit Service b0a153
 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
Packit Service b0a153
 * after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on error
Packit Service b0a153
 * \see libusb_get_config_descriptor
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
Packit Service b0a153
	struct libusb_config_descriptor **config)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_config_descriptor _config;
Packit Service b0a153
	unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
Packit Service b0a153
	unsigned char *buf = NULL;
Packit Service b0a153
	int host_endian = 0;
Packit Service b0a153
	int r;
Packit Service b0a153
Packit Service b0a153
	r = usbi_backend.get_active_config_descriptor(dev, tmp,
Packit Service b0a153
		LIBUSB_DT_CONFIG_SIZE, &host_endian);
Packit Service b0a153
	if (r < 0)
Packit Service b0a153
		return r;
Packit Service b0a153
	if (r < LIBUSB_DT_CONFIG_SIZE) {
Packit Service b0a153
		usbi_err(dev->ctx, "short config descriptor read %d/%d",
Packit Service b0a153
			 r, LIBUSB_DT_CONFIG_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
Packit Service b0a153
	buf = malloc(_config.wTotalLength);
Packit Service b0a153
	if (!buf)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	r = usbi_backend.get_active_config_descriptor(dev, buf,
Packit Service b0a153
		_config.wTotalLength, &host_endian);
Packit Service b0a153
	if (r >= 0)
Packit Service b0a153
		r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
Packit Service b0a153
Packit Service b0a153
	free(buf);
Packit Service b0a153
	return r;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get a USB configuration descriptor based on its index.
Packit Service b0a153
 * This is a non-blocking function which does not involve any requests being
Packit Service b0a153
 * sent to the device.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev a device
Packit Service b0a153
 * \param config_index the index of the configuration you wish to retrieve
Packit Service b0a153
 * \param config output location for the USB configuration descriptor. Only
Packit Service b0a153
 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
Packit Service b0a153
 * after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on error
Packit Service b0a153
 * \see libusb_get_active_config_descriptor()
Packit Service b0a153
 * \see libusb_get_config_descriptor_by_value()
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
Packit Service b0a153
	uint8_t config_index, struct libusb_config_descriptor **config)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_config_descriptor _config;
Packit Service b0a153
	unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
Packit Service b0a153
	unsigned char *buf = NULL;
Packit Service b0a153
	int host_endian = 0;
Packit Service b0a153
	int r;
Packit Service b0a153
Packit Service b0a153
	usbi_dbg("index %d", config_index);
Packit Service b0a153
	if (config_index >= dev->num_configurations)
Packit Service b0a153
		return LIBUSB_ERROR_NOT_FOUND;
Packit Service b0a153
Packit Service b0a153
	r = usbi_backend.get_config_descriptor(dev, config_index, tmp,
Packit Service b0a153
		LIBUSB_DT_CONFIG_SIZE, &host_endian);
Packit Service b0a153
	if (r < 0)
Packit Service b0a153
		return r;
Packit Service b0a153
	if (r < LIBUSB_DT_CONFIG_SIZE) {
Packit Service b0a153
		usbi_err(dev->ctx, "short config descriptor read %d/%d",
Packit Service b0a153
			 r, LIBUSB_DT_CONFIG_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
Packit Service b0a153
	buf = malloc(_config.wTotalLength);
Packit Service b0a153
	if (!buf)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	r = usbi_backend.get_config_descriptor(dev, config_index, buf,
Packit Service b0a153
		_config.wTotalLength, &host_endian);
Packit Service b0a153
	if (r >= 0)
Packit Service b0a153
		r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
Packit Service b0a153
Packit Service b0a153
	free(buf);
Packit Service b0a153
	return r;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/* iterate through all configurations, returning the index of the configuration
Packit Service b0a153
 * matching a specific bConfigurationValue in the idx output parameter, or -1
Packit Service b0a153
 * if the config was not found.
Packit Service b0a153
 * returns 0 on success or a LIBUSB_ERROR code
Packit Service b0a153
 */
Packit Service b0a153
int usbi_get_config_index_by_value(struct libusb_device *dev,
Packit Service b0a153
	uint8_t bConfigurationValue, int *idx)
Packit Service b0a153
{
Packit Service b0a153
	uint8_t i;
Packit Service b0a153
Packit Service b0a153
	usbi_dbg("value %d", bConfigurationValue);
Packit Service b0a153
	for (i = 0; i < dev->num_configurations; i++) {
Packit Service b0a153
		unsigned char tmp[6];
Packit Service b0a153
		int host_endian;
Packit Service b0a153
		int r = usbi_backend.get_config_descriptor(dev, i, tmp, sizeof(tmp),
Packit Service b0a153
			&host_endian);
Packit Service b0a153
		if (r < 0) {
Packit Service b0a153
			*idx = -1;
Packit Service b0a153
			return r;
Packit Service b0a153
		}
Packit Service b0a153
		if (tmp[5] == bConfigurationValue) {
Packit Service b0a153
			*idx = i;
Packit Service b0a153
			return 0;
Packit Service b0a153
		}
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	*idx = -1;
Packit Service b0a153
	return 0;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get a USB configuration descriptor with a specific bConfigurationValue.
Packit Service b0a153
 * This is a non-blocking function which does not involve any requests being
Packit Service b0a153
 * sent to the device.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev a device
Packit Service b0a153
 * \param bConfigurationValue the bConfigurationValue of the configuration you
Packit Service b0a153
 * wish to retrieve
Packit Service b0a153
 * \param config output location for the USB configuration descriptor. Only
Packit Service b0a153
 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
Packit Service b0a153
 * after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on error
Packit Service b0a153
 * \see libusb_get_active_config_descriptor()
Packit Service b0a153
 * \see libusb_get_config_descriptor()
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
Packit Service b0a153
	uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
Packit Service b0a153
{
Packit Service b0a153
	int r, idx, host_endian;
Packit Service b0a153
	unsigned char *buf = NULL;
Packit Service b0a153
Packit Service b0a153
	if (usbi_backend.get_config_descriptor_by_value) {
Packit Service b0a153
		r = usbi_backend.get_config_descriptor_by_value(dev,
Packit Service b0a153
			bConfigurationValue, &buf, &host_endian);
Packit Service b0a153
		if (r < 0)
Packit Service b0a153
			return r;
Packit Service b0a153
		return raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
Packit Service b0a153
	if (r < 0)
Packit Service b0a153
		return r;
Packit Service b0a153
	else if (idx == -1)
Packit Service b0a153
		return LIBUSB_ERROR_NOT_FOUND;
Packit Service b0a153
	else
Packit Service b0a153
		return libusb_get_config_descriptor(dev, (uint8_t) idx, config);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Free a configuration descriptor obtained from
Packit Service b0a153
 * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
Packit Service b0a153
 * It is safe to call this function with a NULL config parameter, in which
Packit Service b0a153
 * case the function simply returns.
Packit Service b0a153
 *
Packit Service b0a153
 * \param config the configuration descriptor to free
Packit Service b0a153
 */
Packit Service b0a153
void API_EXPORTED libusb_free_config_descriptor(
Packit Service b0a153
	struct libusb_config_descriptor *config)
Packit Service b0a153
{
Packit Service b0a153
	if (!config)
Packit Service b0a153
		return;
Packit Service b0a153
Packit Service b0a153
	clear_configuration(config);
Packit Service b0a153
	free(config);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get an endpoints superspeed endpoint companion descriptor (if any)
Packit Service b0a153
 *
Packit Service b0a153
 * \param ctx the context to operate on, or NULL for the default context
Packit Service b0a153
 * \param endpoint endpoint descriptor from which to get the superspeed
Packit Service b0a153
 * endpoint companion descriptor
Packit Service b0a153
 * \param ep_comp output location for the superspeed endpoint companion
Packit Service b0a153
 * descriptor. Only valid if 0 was returned. Must be freed with
Packit Service b0a153
 * libusb_free_ss_endpoint_companion_descriptor() after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on error
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_ss_endpoint_companion_descriptor(
Packit Service b0a153
	struct libusb_context *ctx,
Packit Service b0a153
	const struct libusb_endpoint_descriptor *endpoint,
Packit Service b0a153
	struct libusb_ss_endpoint_companion_descriptor **ep_comp)
Packit Service b0a153
{
Packit Service b0a153
	struct usb_descriptor_header header;
Packit Service b0a153
	int size = endpoint->extra_length;
Packit Service b0a153
	const unsigned char *buffer = endpoint->extra;
Packit Service b0a153
Packit Service b0a153
	*ep_comp = NULL;
Packit Service b0a153
Packit Service b0a153
	while (size >= DESC_HEADER_LENGTH) {
Packit Service b0a153
		usbi_parse_descriptor(buffer, "bb", &header, 0);
Packit Service b0a153
		if (header.bLength < 2 || header.bLength > size) {
Packit Service b0a153
			usbi_err(ctx, "invalid descriptor length %d",
Packit Service b0a153
				 header.bLength);
Packit Service b0a153
			return LIBUSB_ERROR_IO;
Packit Service b0a153
		}
Packit Service b0a153
		if (header.bDescriptorType != LIBUSB_DT_SS_ENDPOINT_COMPANION) {
Packit Service b0a153
			buffer += header.bLength;
Packit Service b0a153
			size -= header.bLength;
Packit Service b0a153
			continue;
Packit Service b0a153
		}
Packit Service b0a153
		if (header.bLength < LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE) {
Packit Service b0a153
			usbi_err(ctx, "invalid ss-ep-comp-desc length %d",
Packit Service b0a153
				 header.bLength);
Packit Service b0a153
			return LIBUSB_ERROR_IO;
Packit Service b0a153
		}
Packit Service b0a153
		*ep_comp = malloc(sizeof(**ep_comp));
Packit Service b0a153
		if (*ep_comp == NULL)
Packit Service b0a153
			return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
		usbi_parse_descriptor(buffer, "bbbbw", *ep_comp, 0);
Packit Service b0a153
		return LIBUSB_SUCCESS;
Packit Service b0a153
	}
Packit Service b0a153
	return LIBUSB_ERROR_NOT_FOUND;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Free a superspeed endpoint companion descriptor obtained from
Packit Service b0a153
 * libusb_get_ss_endpoint_companion_descriptor().
Packit Service b0a153
 * It is safe to call this function with a NULL ep_comp parameter, in which
Packit Service b0a153
 * case the function simply returns.
Packit Service b0a153
 *
Packit Service b0a153
 * \param ep_comp the superspeed endpoint companion descriptor to free
Packit Service b0a153
 */
Packit Service b0a153
void API_EXPORTED libusb_free_ss_endpoint_companion_descriptor(
Packit Service b0a153
	struct libusb_ss_endpoint_companion_descriptor *ep_comp)
Packit Service b0a153
{
Packit Service b0a153
	free(ep_comp);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
static int parse_bos(struct libusb_context *ctx,
Packit Service b0a153
	struct libusb_bos_descriptor **bos,
Packit Service b0a153
	unsigned char *buffer, int size, int host_endian)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_bos_descriptor bos_header, *_bos;
Packit Service b0a153
	struct libusb_bos_dev_capability_descriptor dev_cap;
Packit Service b0a153
	int i;
Packit Service b0a153
Packit Service b0a153
	if (size < LIBUSB_DT_BOS_SIZE) {
Packit Service b0a153
		usbi_err(ctx, "short bos descriptor read %d/%d",
Packit Service b0a153
			 size, LIBUSB_DT_BOS_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor(buffer, "bbwb", &bos_header, host_endian);
Packit Service b0a153
	if (bos_header.bDescriptorType != LIBUSB_DT_BOS) {
Packit Service b0a153
		usbi_err(ctx, "unexpected descriptor %x (expected %x)",
Packit Service b0a153
			 bos_header.bDescriptorType, LIBUSB_DT_BOS);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
	if (bos_header.bLength < LIBUSB_DT_BOS_SIZE) {
Packit Service b0a153
		usbi_err(ctx, "invalid bos bLength (%d)", bos_header.bLength);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
	if (bos_header.bLength > size) {
Packit Service b0a153
		usbi_err(ctx, "short bos descriptor read %d/%d",
Packit Service b0a153
			 size, bos_header.bLength);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	_bos = calloc (1,
Packit Service b0a153
		sizeof(*_bos) + bos_header.bNumDeviceCaps * sizeof(void *));
Packit Service b0a153
	if (!_bos)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor(buffer, "bbwb", _bos, host_endian);
Packit Service b0a153
	buffer += bos_header.bLength;
Packit Service b0a153
	size -= bos_header.bLength;
Packit Service b0a153
Packit Service b0a153
	/* Get the device capability descriptors */
Packit Service b0a153
	for (i = 0; i < bos_header.bNumDeviceCaps; i++) {
Packit Service b0a153
		if (size < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
Packit Service b0a153
			usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
Packit Service b0a153
				  size, LIBUSB_DT_DEVICE_CAPABILITY_SIZE);
Packit Service b0a153
			break;
Packit Service b0a153
		}
Packit Service b0a153
		usbi_parse_descriptor(buffer, "bbb", &dev_cap, host_endian);
Packit Service b0a153
		if (dev_cap.bDescriptorType != LIBUSB_DT_DEVICE_CAPABILITY) {
Packit Service b0a153
			usbi_warn(ctx, "unexpected descriptor %x (expected %x)",
Packit Service b0a153
				  dev_cap.bDescriptorType, LIBUSB_DT_DEVICE_CAPABILITY);
Packit Service b0a153
			break;
Packit Service b0a153
		}
Packit Service b0a153
		if (dev_cap.bLength < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
Packit Service b0a153
			usbi_err(ctx, "invalid dev-cap bLength (%d)",
Packit Service b0a153
				 dev_cap.bLength);
Packit Service b0a153
			libusb_free_bos_descriptor(_bos);
Packit Service b0a153
			return LIBUSB_ERROR_IO;
Packit Service b0a153
		}
Packit Service b0a153
		if (dev_cap.bLength > size) {
Packit Service b0a153
			usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
Packit Service b0a153
				  size, dev_cap.bLength);
Packit Service b0a153
			break;
Packit Service b0a153
		}
Packit Service b0a153
Packit Service b0a153
		_bos->dev_capability[i] = malloc(dev_cap.bLength);
Packit Service b0a153
		if (!_bos->dev_capability[i]) {
Packit Service b0a153
			libusb_free_bos_descriptor(_bos);
Packit Service b0a153
			return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
		}
Packit Service b0a153
		memcpy(_bos->dev_capability[i], buffer, dev_cap.bLength);
Packit Service b0a153
		buffer += dev_cap.bLength;
Packit Service b0a153
		size -= dev_cap.bLength;
Packit Service b0a153
	}
Packit Service b0a153
	_bos->bNumDeviceCaps = (uint8_t)i;
Packit Service b0a153
	*bos = _bos;
Packit Service b0a153
Packit Service b0a153
	return LIBUSB_SUCCESS;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get a Binary Object Store (BOS) descriptor
Packit Service b0a153
 * This is a BLOCKING function, which will send requests to the device.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev_handle the handle of an open libusb device
Packit Service b0a153
 * \param bos output location for the BOS descriptor. Only valid if 0 was returned.
Packit Service b0a153
 * Must be freed with \ref libusb_free_bos_descriptor() after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns LIBUSB_ERROR_NOT_FOUND if the device doesn't have a BOS descriptor
Packit Service b0a153
 * \returns another LIBUSB_ERROR code on error
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_bos_descriptor(libusb_device_handle *dev_handle,
Packit Service b0a153
	struct libusb_bos_descriptor **bos)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_bos_descriptor _bos;
Packit Service b0a153
	uint8_t bos_header[LIBUSB_DT_BOS_SIZE] = {0};
Packit Service b0a153
	unsigned char *bos_data = NULL;
Packit Service b0a153
	const int host_endian = 0;
Packit Service b0a153
	int r;
Packit Service b0a153
Packit Service b0a153
	/* Read the BOS. This generates 2 requests on the bus,
Packit Service b0a153
	 * one for the header, and one for the full BOS */
Packit Service b0a153
	r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, bos_header,
Packit Service b0a153
				  LIBUSB_DT_BOS_SIZE);
Packit Service b0a153
	if (r < 0) {
Packit Service b0a153
		if (r != LIBUSB_ERROR_PIPE)
Packit Service b0a153
			usbi_err(HANDLE_CTX(dev_handle), "failed to read BOS (%d)", r);
Packit Service b0a153
		return r;
Packit Service b0a153
	}
Packit Service b0a153
	if (r < LIBUSB_DT_BOS_SIZE) {
Packit Service b0a153
		usbi_err(HANDLE_CTX(dev_handle), "short BOS read %d/%d",
Packit Service b0a153
			 r, LIBUSB_DT_BOS_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor(bos_header, "bbwb", &_bos, host_endian);
Packit Service b0a153
	usbi_dbg("found BOS descriptor: size %d bytes, %d capabilities",
Packit Service b0a153
		 _bos.wTotalLength, _bos.bNumDeviceCaps);
Packit Service b0a153
	bos_data = calloc(_bos.wTotalLength, 1);
Packit Service b0a153
	if (bos_data == NULL)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, bos_data,
Packit Service b0a153
				  _bos.wTotalLength);
Packit Service b0a153
	if (r >= 0)
Packit Service b0a153
		r = parse_bos(HANDLE_CTX(dev_handle), bos, bos_data, r, host_endian);
Packit Service b0a153
	else
Packit Service b0a153
		usbi_err(HANDLE_CTX(dev_handle), "failed to read BOS (%d)", r);
Packit Service b0a153
Packit Service b0a153
	free(bos_data);
Packit Service b0a153
	return r;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Free a BOS descriptor obtained from libusb_get_bos_descriptor().
Packit Service b0a153
 * It is safe to call this function with a NULL bos parameter, in which
Packit Service b0a153
 * case the function simply returns.
Packit Service b0a153
 *
Packit Service b0a153
 * \param bos the BOS descriptor to free
Packit Service b0a153
 */
Packit Service b0a153
void API_EXPORTED libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos)
Packit Service b0a153
{
Packit Service b0a153
	int i;
Packit Service b0a153
Packit Service b0a153
	if (!bos)
Packit Service b0a153
		return;
Packit Service b0a153
Packit Service b0a153
	for (i = 0; i < bos->bNumDeviceCaps; i++)
Packit Service b0a153
		free(bos->dev_capability[i]);
Packit Service b0a153
	free(bos);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get an USB 2.0 Extension descriptor
Packit Service b0a153
 *
Packit Service b0a153
 * \param ctx the context to operate on, or NULL for the default context
Packit Service b0a153
 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
Packit Service b0a153
 * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
Packit Service b0a153
 * LIBUSB_BT_USB_2_0_EXTENSION
Packit Service b0a153
 * \param usb_2_0_extension output location for the USB 2.0 Extension
Packit Service b0a153
 * descriptor. Only valid if 0 was returned. Must be freed with
Packit Service b0a153
 * libusb_free_usb_2_0_extension_descriptor() after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns a LIBUSB_ERROR code on error
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_usb_2_0_extension_descriptor(
Packit Service b0a153
	struct libusb_context *ctx,
Packit Service b0a153
	struct libusb_bos_dev_capability_descriptor *dev_cap,
Packit Service b0a153
	struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_usb_2_0_extension_descriptor *_usb_2_0_extension;
Packit Service b0a153
	const int host_endian = 0;
Packit Service b0a153
Packit Service b0a153
	if (dev_cap->bDevCapabilityType != LIBUSB_BT_USB_2_0_EXTENSION) {
Packit Service b0a153
		usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
Packit Service b0a153
			 dev_cap->bDevCapabilityType,
Packit Service b0a153
			 LIBUSB_BT_USB_2_0_EXTENSION);
Packit Service b0a153
		return LIBUSB_ERROR_INVALID_PARAM;
Packit Service b0a153
	}
Packit Service b0a153
	if (dev_cap->bLength < LIBUSB_BT_USB_2_0_EXTENSION_SIZE) {
Packit Service b0a153
		usbi_err(ctx, "short dev-cap descriptor read %d/%d",
Packit Service b0a153
			 dev_cap->bLength, LIBUSB_BT_USB_2_0_EXTENSION_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	_usb_2_0_extension = malloc(sizeof(*_usb_2_0_extension));
Packit Service b0a153
	if (!_usb_2_0_extension)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor((unsigned char *)dev_cap, "bbbd",
Packit Service b0a153
			      _usb_2_0_extension, host_endian);
Packit Service b0a153
Packit Service b0a153
	*usb_2_0_extension = _usb_2_0_extension;
Packit Service b0a153
	return LIBUSB_SUCCESS;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Free a USB 2.0 Extension descriptor obtained from
Packit Service b0a153
 * libusb_get_usb_2_0_extension_descriptor().
Packit Service b0a153
 * It is safe to call this function with a NULL usb_2_0_extension parameter,
Packit Service b0a153
 * in which case the function simply returns.
Packit Service b0a153
 *
Packit Service b0a153
 * \param usb_2_0_extension the USB 2.0 Extension descriptor to free
Packit Service b0a153
 */
Packit Service b0a153
void API_EXPORTED libusb_free_usb_2_0_extension_descriptor(
Packit Service b0a153
	struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension)
Packit Service b0a153
{
Packit Service b0a153
	free(usb_2_0_extension);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get a SuperSpeed USB Device Capability descriptor
Packit Service b0a153
 *
Packit Service b0a153
 * \param ctx the context to operate on, or NULL for the default context
Packit Service b0a153
 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
Packit Service b0a153
 * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
Packit Service b0a153
 * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
Packit Service b0a153
 * \param ss_usb_device_cap output location for the SuperSpeed USB Device
Packit Service b0a153
 * Capability descriptor. Only valid if 0 was returned. Must be freed with
Packit Service b0a153
 * libusb_free_ss_usb_device_capability_descriptor() after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns a LIBUSB_ERROR code on error
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_ss_usb_device_capability_descriptor(
Packit Service b0a153
	struct libusb_context *ctx,
Packit Service b0a153
	struct libusb_bos_dev_capability_descriptor *dev_cap,
Packit Service b0a153
	struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_ss_usb_device_capability_descriptor *_ss_usb_device_cap;
Packit Service b0a153
	const int host_endian = 0;
Packit Service b0a153
Packit Service b0a153
	if (dev_cap->bDevCapabilityType != LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
Packit Service b0a153
		usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
Packit Service b0a153
			 dev_cap->bDevCapabilityType,
Packit Service b0a153
			 LIBUSB_BT_SS_USB_DEVICE_CAPABILITY);
Packit Service b0a153
		return LIBUSB_ERROR_INVALID_PARAM;
Packit Service b0a153
	}
Packit Service b0a153
	if (dev_cap->bLength < LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) {
Packit Service b0a153
		usbi_err(ctx, "short dev-cap descriptor read %d/%d",
Packit Service b0a153
			 dev_cap->bLength, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	_ss_usb_device_cap = malloc(sizeof(*_ss_usb_device_cap));
Packit Service b0a153
	if (!_ss_usb_device_cap)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbwbbw",
Packit Service b0a153
			      _ss_usb_device_cap, host_endian);
Packit Service b0a153
Packit Service b0a153
	*ss_usb_device_cap = _ss_usb_device_cap;
Packit Service b0a153
	return LIBUSB_SUCCESS;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Free a SuperSpeed USB Device Capability descriptor obtained from
Packit Service b0a153
 * libusb_get_ss_usb_device_capability_descriptor().
Packit Service b0a153
 * It is safe to call this function with a NULL ss_usb_device_cap
Packit Service b0a153
 * parameter, in which case the function simply returns.
Packit Service b0a153
 *
Packit Service b0a153
 * \param ss_usb_device_cap the SuperSpeed USB Device Capability descriptor
Packit Service b0a153
 * to free
Packit Service b0a153
 */
Packit Service b0a153
void API_EXPORTED libusb_free_ss_usb_device_capability_descriptor(
Packit Service b0a153
	struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap)
Packit Service b0a153
{
Packit Service b0a153
	free(ss_usb_device_cap);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Get a Container ID descriptor
Packit Service b0a153
 *
Packit Service b0a153
 * \param ctx the context to operate on, or NULL for the default context
Packit Service b0a153
 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
Packit Service b0a153
 * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
Packit Service b0a153
 * LIBUSB_BT_CONTAINER_ID
Packit Service b0a153
 * \param container_id output location for the Container ID descriptor.
Packit Service b0a153
 * Only valid if 0 was returned. Must be freed with
Packit Service b0a153
 * libusb_free_container_id_descriptor() after use.
Packit Service b0a153
 * \returns 0 on success
Packit Service b0a153
 * \returns a LIBUSB_ERROR code on error
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_container_id_descriptor(struct libusb_context *ctx,
Packit Service b0a153
	struct libusb_bos_dev_capability_descriptor *dev_cap,
Packit Service b0a153
	struct libusb_container_id_descriptor **container_id)
Packit Service b0a153
{
Packit Service b0a153
	struct libusb_container_id_descriptor *_container_id;
Packit Service b0a153
	const int host_endian = 0;
Packit Service b0a153
Packit Service b0a153
	if (dev_cap->bDevCapabilityType != LIBUSB_BT_CONTAINER_ID) {
Packit Service b0a153
		usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
Packit Service b0a153
			 dev_cap->bDevCapabilityType,
Packit Service b0a153
			 LIBUSB_BT_CONTAINER_ID);
Packit Service b0a153
		return LIBUSB_ERROR_INVALID_PARAM;
Packit Service b0a153
	}
Packit Service b0a153
	if (dev_cap->bLength < LIBUSB_BT_CONTAINER_ID_SIZE) {
Packit Service b0a153
		usbi_err(ctx, "short dev-cap descriptor read %d/%d",
Packit Service b0a153
			 dev_cap->bLength, LIBUSB_BT_CONTAINER_ID_SIZE);
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	_container_id = malloc(sizeof(*_container_id));
Packit Service b0a153
	if (!_container_id)
Packit Service b0a153
		return LIBUSB_ERROR_NO_MEM;
Packit Service b0a153
Packit Service b0a153
	usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbu",
Packit Service b0a153
			      _container_id, host_endian);
Packit Service b0a153
Packit Service b0a153
	*container_id = _container_id;
Packit Service b0a153
	return LIBUSB_SUCCESS;
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Free a Container ID descriptor obtained from
Packit Service b0a153
 * libusb_get_container_id_descriptor().
Packit Service b0a153
 * It is safe to call this function with a NULL container_id parameter,
Packit Service b0a153
 * in which case the function simply returns.
Packit Service b0a153
 *
Packit Service b0a153
 * \param container_id the Container ID descriptor to free
Packit Service b0a153
 */
Packit Service b0a153
void API_EXPORTED libusb_free_container_id_descriptor(
Packit Service b0a153
	struct libusb_container_id_descriptor *container_id)
Packit Service b0a153
{
Packit Service b0a153
	free(container_id);
Packit Service b0a153
}
Packit Service b0a153
Packit Service b0a153
/** \ingroup libusb_desc
Packit Service b0a153
 * Retrieve a string descriptor in C style ASCII.
Packit Service b0a153
 *
Packit Service b0a153
 * Wrapper around libusb_get_string_descriptor(). Uses the first language
Packit Service b0a153
 * supported by the device.
Packit Service b0a153
 *
Packit Service b0a153
 * \param dev_handle a device handle
Packit Service b0a153
 * \param desc_index the index of the descriptor to retrieve
Packit Service b0a153
 * \param data output buffer for ASCII string descriptor
Packit Service b0a153
 * \param length size of data buffer
Packit Service b0a153
 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
Packit Service b0a153
 */
Packit Service b0a153
int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle,
Packit Service b0a153
	uint8_t desc_index, unsigned char *data, int length)
Packit Service b0a153
{
Packit Service b0a153
	unsigned char tbuf[255]; /* Some devices choke on size > 255 */
Packit Service b0a153
	int r, si, di;
Packit Service b0a153
	uint16_t langid;
Packit Service b0a153
Packit Service b0a153
	/* Asking for the zero'th index is special - it returns a string
Packit Service b0a153
	 * descriptor that contains all the language IDs supported by the
Packit Service b0a153
	 * device. Typically there aren't many - often only one. Language
Packit Service b0a153
	 * IDs are 16 bit numbers, and they start at the third byte in the
Packit Service b0a153
	 * descriptor. There's also no point in trying to read descriptor 0
Packit Service b0a153
	 * with this function. See USB 2.0 specification section 9.6.7 for
Packit Service b0a153
	 * more information.
Packit Service b0a153
	 */
Packit Service b0a153
Packit Service b0a153
	if (desc_index == 0)
Packit Service b0a153
		return LIBUSB_ERROR_INVALID_PARAM;
Packit Service b0a153
Packit Service b0a153
	r = libusb_get_string_descriptor(dev_handle, 0, 0, tbuf, sizeof(tbuf));
Packit Service b0a153
	if (r < 0)
Packit Service b0a153
		return r;
Packit Service b0a153
Packit Service b0a153
	if (r < 4)
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
Packit Service b0a153
	langid = (uint16_t)(tbuf[2] | (tbuf[3] << 8));
Packit Service b0a153
Packit Service b0a153
	r = libusb_get_string_descriptor(dev_handle, desc_index, langid, tbuf,
Packit Service b0a153
		sizeof(tbuf));
Packit Service b0a153
	if (r < 0)
Packit Service b0a153
		return r;
Packit Service b0a153
Packit Service b0a153
	if (tbuf[1] != LIBUSB_DT_STRING)
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
Packit Service b0a153
	if (tbuf[0] > r)
Packit Service b0a153
		return LIBUSB_ERROR_IO;
Packit Service b0a153
Packit Service b0a153
	di = 0;
Packit Service b0a153
	for (si = 2; si < tbuf[0]; si += 2) {
Packit Service b0a153
		if (di >= (length - 1))
Packit Service b0a153
			break;
Packit Service b0a153
Packit Service b0a153
		if ((tbuf[si] & 0x80) || (tbuf[si + 1])) /* non-ASCII */
Packit Service b0a153
			data[di++] = '?';
Packit Service b0a153
		else
Packit Service b0a153
			data[di++] = tbuf[si];
Packit Service b0a153
	}
Packit Service b0a153
Packit Service b0a153
	data[di] = 0;
Packit Service b0a153
	return di;
Packit Service b0a153
}