1 /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
3 * USB descriptor handling functions for libusb
4 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
5 * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #define DESC_HEADER_LENGTH 2
32 #define DEVICE_DESC_LENGTH 18
33 #define CONFIG_DESC_LENGTH 9
34 #define INTERFACE_DESC_LENGTH 9
35 #define ENDPOINT_DESC_LENGTH 7
36 #define ENDPOINT_AUDIO_DESC_LENGTH 9
38 /** @defgroup libusb_desc USB descriptors
39 * This page details how to examine the various standard USB descriptors
40 * for detected devices
43 /* set host_endian if the w values are already in host endian format,
44 * as opposed to bus endian. */
45 int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
46 void *dest, int host_endian)
48 const unsigned char *sp = source;
49 unsigned char *dp = dest;
54 for (cp = descriptor; *cp; cp++) {
56 case 'b': /* 8-bit byte */
59 case 'w': /* 16-bit word, convert from little endian to CPU */
60 dp += ((uintptr_t)dp & 1); /* Align to word boundary */
65 w = (sp[1] << 8) | sp[0];
66 *((uint16_t *)dp) = w;
71 case 'd': /* 32-bit word, convert from little endian to CPU */
72 dp += ((uintptr_t)dp & 1); /* Align to word boundary */
77 d = (sp[3] << 24) | (sp[2] << 16) |
79 *((uint32_t *)dp) = d;
84 case 'u': /* 16 byte UUID */
92 return (int) (sp - source);
95 static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
97 free((void *) endpoint->extra);
100 static int parse_endpoint(struct libusb_context *ctx,
101 struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
102 int size, int host_endian)
104 struct usb_descriptor_header header;
105 unsigned char *extra;
106 unsigned char *begin;
110 if (size < DESC_HEADER_LENGTH) {
111 usbi_err(ctx, "short endpoint descriptor read %d/%d",
112 size, DESC_HEADER_LENGTH);
113 return LIBUSB_ERROR_IO;
116 usbi_parse_descriptor(buffer, "bb", &header, 0);
117 if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
118 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
119 header.bDescriptorType, LIBUSB_DT_ENDPOINT);
122 if (header.bLength > size) {
123 usbi_warn(ctx, "short endpoint descriptor read %d/%d",
124 size, header.bLength);
127 if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
128 usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
129 else if (header.bLength >= ENDPOINT_DESC_LENGTH)
130 usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
132 usbi_err(ctx, "invalid endpoint bLength (%d)", header.bLength);
133 return LIBUSB_ERROR_IO;
136 buffer += header.bLength;
137 size -= header.bLength;
138 parsed += header.bLength;
140 /* Skip over the rest of the Class Specific or Vendor Specific */
143 while (size >= DESC_HEADER_LENGTH) {
144 usbi_parse_descriptor(buffer, "bb", &header, 0);
145 if (header.bLength < DESC_HEADER_LENGTH) {
146 usbi_err(ctx, "invalid extra ep desc len (%d)",
148 return LIBUSB_ERROR_IO;
149 } else if (header.bLength > size) {
150 usbi_warn(ctx, "short extra ep desc read %d/%d",
151 size, header.bLength);
155 /* If we find another "proper" descriptor then we're done */
156 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
157 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
158 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
159 (header.bDescriptorType == LIBUSB_DT_DEVICE))
162 usbi_dbg("skipping descriptor %x", header.bDescriptorType);
163 buffer += header.bLength;
164 size -= header.bLength;
165 parsed += header.bLength;
168 /* Copy any unknown descriptors into a storage area for drivers */
170 len = (int)(buffer - begin);
172 endpoint->extra = NULL;
173 endpoint->extra_length = 0;
178 endpoint->extra = extra;
180 endpoint->extra_length = 0;
181 return LIBUSB_ERROR_NO_MEM;
184 memcpy(extra, begin, len);
185 endpoint->extra_length = len;
190 static void clear_interface(struct libusb_interface *usb_interface)
195 if (usb_interface->altsetting) {
196 for (i = 0; i < usb_interface->num_altsetting; i++) {
197 struct libusb_interface_descriptor *ifp =
198 (struct libusb_interface_descriptor *)
199 usb_interface->altsetting + i;
200 free((void *) ifp->extra);
202 for (j = 0; j < ifp->bNumEndpoints; j++)
203 clear_endpoint((struct libusb_endpoint_descriptor *)
206 free((void *) ifp->endpoint);
209 free((void *) usb_interface->altsetting);
210 usb_interface->altsetting = NULL;
213 static int parse_interface(libusb_context *ctx,
214 struct libusb_interface *usb_interface, unsigned char *buffer, int size,
221 int interface_number = -1;
222 struct usb_descriptor_header header;
223 struct libusb_interface_descriptor *ifp;
224 unsigned char *begin;
226 usb_interface->num_altsetting = 0;
228 while (size >= INTERFACE_DESC_LENGTH) {
229 struct libusb_interface_descriptor *altsetting =
230 (struct libusb_interface_descriptor *) usb_interface->altsetting;
231 altsetting = usbi_reallocf(altsetting,
232 sizeof(struct libusb_interface_descriptor) *
233 (usb_interface->num_altsetting + 1));
235 r = LIBUSB_ERROR_NO_MEM;
238 usb_interface->altsetting = altsetting;
240 ifp = altsetting + usb_interface->num_altsetting;
241 usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
242 if (ifp->bDescriptorType != LIBUSB_DT_INTERFACE) {
243 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
244 ifp->bDescriptorType, LIBUSB_DT_INTERFACE);
247 if (ifp->bLength < INTERFACE_DESC_LENGTH) {
248 usbi_err(ctx, "invalid interface bLength (%d)",
253 if (ifp->bLength > size) {
254 usbi_warn(ctx, "short intf descriptor read %d/%d",
258 if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
259 usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);
264 usb_interface->num_altsetting++;
266 ifp->extra_length = 0;
267 ifp->endpoint = NULL;
269 if (interface_number == -1)
270 interface_number = ifp->bInterfaceNumber;
272 /* Skip over the interface */
273 buffer += ifp->bLength;
274 parsed += ifp->bLength;
275 size -= ifp->bLength;
279 /* Skip over any interface, class or vendor descriptors */
280 while (size >= DESC_HEADER_LENGTH) {
281 usbi_parse_descriptor(buffer, "bb", &header, 0);
282 if (header.bLength < DESC_HEADER_LENGTH) {
284 "invalid extra intf desc len (%d)",
288 } else if (header.bLength > size) {
290 "short extra intf desc read %d/%d",
291 size, header.bLength);
295 /* If we find another "proper" descriptor then we're done */
296 if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
297 (header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
298 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
299 (header.bDescriptorType == LIBUSB_DT_DEVICE))
302 buffer += header.bLength;
303 parsed += header.bLength;
304 size -= header.bLength;
307 /* Copy any unknown descriptors into a storage area for */
308 /* drivers to later parse */
309 len = (int)(buffer - begin);
311 ifp->extra = malloc(len);
313 r = LIBUSB_ERROR_NO_MEM;
316 memcpy((unsigned char *) ifp->extra, begin, len);
317 ifp->extra_length = len;
320 if (ifp->bNumEndpoints > 0) {
321 struct libusb_endpoint_descriptor *endpoint;
322 endpoint = calloc(ifp->bNumEndpoints, sizeof(struct libusb_endpoint_descriptor));
323 ifp->endpoint = endpoint;
325 r = LIBUSB_ERROR_NO_MEM;
329 for (i = 0; i < ifp->bNumEndpoints; i++) {
330 r = parse_endpoint(ctx, endpoint + i, buffer, size,
335 ifp->bNumEndpoints = (uint8_t)i;
345 /* We check to see if it's an alternate to this one */
346 ifp = (struct libusb_interface_descriptor *) buffer;
347 if (size < LIBUSB_DT_INTERFACE_SIZE ||
348 ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
349 ifp->bInterfaceNumber != interface_number)
355 clear_interface(usb_interface);
359 static void clear_configuration(struct libusb_config_descriptor *config)
362 if (config->interface) {
363 for (i = 0; i < config->bNumInterfaces; i++)
364 clear_interface((struct libusb_interface *)
365 config->interface + i);
367 free((void *) config->interface);
368 free((void *) config->extra);
371 static int parse_configuration(struct libusb_context *ctx,
372 struct libusb_config_descriptor *config, unsigned char *buffer,
373 int size, int host_endian)
377 struct usb_descriptor_header header;
378 struct libusb_interface *usb_interface;
380 if (size < LIBUSB_DT_CONFIG_SIZE) {
381 usbi_err(ctx, "short config descriptor read %d/%d",
382 size, LIBUSB_DT_CONFIG_SIZE);
383 return LIBUSB_ERROR_IO;
386 usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
387 if (config->bDescriptorType != LIBUSB_DT_CONFIG) {
388 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
389 config->bDescriptorType, LIBUSB_DT_CONFIG);
390 return LIBUSB_ERROR_IO;
392 if (config->bLength < LIBUSB_DT_CONFIG_SIZE) {
393 usbi_err(ctx, "invalid config bLength (%d)", config->bLength);
394 return LIBUSB_ERROR_IO;
396 if (config->bLength > size) {
397 usbi_err(ctx, "short config descriptor read %d/%d",
398 size, config->bLength);
399 return LIBUSB_ERROR_IO;
401 if (config->bNumInterfaces > USB_MAXINTERFACES) {
402 usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
403 return LIBUSB_ERROR_IO;
406 usb_interface = calloc(config->bNumInterfaces, sizeof(struct libusb_interface));
407 config->interface = usb_interface;
409 return LIBUSB_ERROR_NO_MEM;
411 buffer += config->bLength;
412 size -= config->bLength;
414 config->extra = NULL;
415 config->extra_length = 0;
417 for (i = 0; i < config->bNumInterfaces; i++) {
419 unsigned char *begin;
421 /* Skip over the rest of the Class Specific or Vendor */
422 /* Specific descriptors */
424 while (size >= DESC_HEADER_LENGTH) {
425 usbi_parse_descriptor(buffer, "bb", &header, 0);
427 if (header.bLength < DESC_HEADER_LENGTH) {
429 "invalid extra config desc len (%d)",
433 } else if (header.bLength > size) {
435 "short extra config desc read %d/%d",
436 size, header.bLength);
437 config->bNumInterfaces = (uint8_t)i;
441 /* If we find another "proper" descriptor then we're done */
442 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
443 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
444 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
445 (header.bDescriptorType == LIBUSB_DT_DEVICE))
448 usbi_dbg("skipping descriptor 0x%x", header.bDescriptorType);
449 buffer += header.bLength;
450 size -= header.bLength;
453 /* Copy any unknown descriptors into a storage area for */
454 /* drivers to later parse */
455 len = (int)(buffer - begin);
457 /* FIXME: We should realloc and append here */
458 if (!config->extra_length) {
459 config->extra = malloc(len);
460 if (!config->extra) {
461 r = LIBUSB_ERROR_NO_MEM;
465 memcpy((unsigned char *) config->extra, begin, len);
466 config->extra_length = len;
470 r = parse_interface(ctx, usb_interface + i, buffer, size, host_endian);
474 config->bNumInterfaces = (uint8_t)i;
485 clear_configuration(config);
489 static int raw_desc_to_config(struct libusb_context *ctx,
490 unsigned char *buf, int size, int host_endian,
491 struct libusb_config_descriptor **config)
493 struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
497 return LIBUSB_ERROR_NO_MEM;
499 r = parse_configuration(ctx, _config, buf, size, host_endian);
501 usbi_err(ctx, "parse_configuration failed with error %d", r);
505 usbi_warn(ctx, "still %d bytes of descriptor data left", r);
509 return LIBUSB_SUCCESS;
512 int usbi_device_cache_descriptor(libusb_device *dev)
514 int r, host_endian = 0;
516 r = usbi_backend.get_device_descriptor(dev, (unsigned char *) &dev->device_descriptor,
522 dev->device_descriptor.bcdUSB = libusb_le16_to_cpu(dev->device_descriptor.bcdUSB);
523 dev->device_descriptor.idVendor = libusb_le16_to_cpu(dev->device_descriptor.idVendor);
524 dev->device_descriptor.idProduct = libusb_le16_to_cpu(dev->device_descriptor.idProduct);
525 dev->device_descriptor.bcdDevice = libusb_le16_to_cpu(dev->device_descriptor.bcdDevice);
528 return LIBUSB_SUCCESS;
531 /** \ingroup libusb_desc
532 * Get the USB device descriptor for a given device.
534 * This is a non-blocking function; the device descriptor is cached in memory.
536 * Note since libusb-1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102, this
537 * function always succeeds.
539 * \param dev the device
540 * \param desc output location for the descriptor data
541 * \returns 0 on success or a LIBUSB_ERROR code on failure
543 int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
544 struct libusb_device_descriptor *desc)
547 memcpy((unsigned char *) desc, (unsigned char *) &dev->device_descriptor,
548 sizeof (dev->device_descriptor));
552 /** \ingroup libusb_desc
553 * Get the USB configuration descriptor for the currently active configuration.
554 * This is a non-blocking function which does not involve any requests being
555 * sent to the device.
557 * \param dev a device
558 * \param config output location for the USB configuration descriptor. Only
559 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
561 * \returns 0 on success
562 * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
563 * \returns another LIBUSB_ERROR code on error
564 * \see libusb_get_config_descriptor
566 int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
567 struct libusb_config_descriptor **config)
569 struct libusb_config_descriptor _config;
570 unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
571 unsigned char *buf = NULL;
575 r = usbi_backend.get_active_config_descriptor(dev, tmp,
576 LIBUSB_DT_CONFIG_SIZE, &host_endian);
579 if (r < LIBUSB_DT_CONFIG_SIZE) {
580 usbi_err(dev->ctx, "short config descriptor read %d/%d",
581 r, LIBUSB_DT_CONFIG_SIZE);
582 return LIBUSB_ERROR_IO;
585 usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
586 buf = malloc(_config.wTotalLength);
588 return LIBUSB_ERROR_NO_MEM;
590 r = usbi_backend.get_active_config_descriptor(dev, buf,
591 _config.wTotalLength, &host_endian);
593 r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
599 /** \ingroup libusb_desc
600 * Get a USB configuration descriptor based on its index.
601 * This is a non-blocking function which does not involve any requests being
602 * sent to the device.
604 * \param dev a device
605 * \param config_index the index of the configuration you wish to retrieve
606 * \param config output location for the USB configuration descriptor. Only
607 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
609 * \returns 0 on success
610 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
611 * \returns another LIBUSB_ERROR code on error
612 * \see libusb_get_active_config_descriptor()
613 * \see libusb_get_config_descriptor_by_value()
615 int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
616 uint8_t config_index, struct libusb_config_descriptor **config)
618 struct libusb_config_descriptor _config;
619 unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
620 unsigned char *buf = NULL;
624 usbi_dbg("index %d", config_index);
625 if (config_index >= dev->num_configurations)
626 return LIBUSB_ERROR_NOT_FOUND;
628 r = usbi_backend.get_config_descriptor(dev, config_index, tmp,
629 LIBUSB_DT_CONFIG_SIZE, &host_endian);
632 if (r < LIBUSB_DT_CONFIG_SIZE) {
633 usbi_err(dev->ctx, "short config descriptor read %d/%d",
634 r, LIBUSB_DT_CONFIG_SIZE);
635 return LIBUSB_ERROR_IO;
638 usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
639 buf = malloc(_config.wTotalLength);
641 return LIBUSB_ERROR_NO_MEM;
643 r = usbi_backend.get_config_descriptor(dev, config_index, buf,
644 _config.wTotalLength, &host_endian);
646 r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
652 /* iterate through all configurations, returning the index of the configuration
653 * matching a specific bConfigurationValue in the idx output parameter, or -1
654 * if the config was not found.
655 * returns 0 on success or a LIBUSB_ERROR code
657 int usbi_get_config_index_by_value(struct libusb_device *dev,
658 uint8_t bConfigurationValue, int *idx)
662 usbi_dbg("value %d", bConfigurationValue);
663 for (i = 0; i < dev->num_configurations; i++) {
664 unsigned char tmp[6];
666 int r = usbi_backend.get_config_descriptor(dev, i, tmp, sizeof(tmp),
672 if (tmp[5] == bConfigurationValue) {
682 /** \ingroup libusb_desc
683 * Get a USB configuration descriptor with a specific bConfigurationValue.
684 * This is a non-blocking function which does not involve any requests being
685 * sent to the device.
687 * \param dev a device
688 * \param bConfigurationValue the bConfigurationValue of the configuration you
690 * \param config output location for the USB configuration descriptor. Only
691 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
693 * \returns 0 on success
694 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
695 * \returns another LIBUSB_ERROR code on error
696 * \see libusb_get_active_config_descriptor()
697 * \see libusb_get_config_descriptor()
699 int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
700 uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
702 int r, idx, host_endian;
703 unsigned char *buf = NULL;
705 if (usbi_backend.get_config_descriptor_by_value) {
706 r = usbi_backend.get_config_descriptor_by_value(dev,
707 bConfigurationValue, &buf, &host_endian);
710 return raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
713 r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
717 return LIBUSB_ERROR_NOT_FOUND;
719 return libusb_get_config_descriptor(dev, (uint8_t) idx, config);
722 /** \ingroup libusb_desc
723 * Free a configuration descriptor obtained from
724 * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
725 * It is safe to call this function with a NULL config parameter, in which
726 * case the function simply returns.
728 * \param config the configuration descriptor to free
730 void API_EXPORTED libusb_free_config_descriptor(
731 struct libusb_config_descriptor *config)
736 clear_configuration(config);
740 /** \ingroup libusb_desc
741 * Get an endpoints superspeed endpoint companion descriptor (if any)
743 * \param ctx the context to operate on, or NULL for the default context
744 * \param endpoint endpoint descriptor from which to get the superspeed
745 * endpoint companion descriptor
746 * \param ep_comp output location for the superspeed endpoint companion
747 * descriptor. Only valid if 0 was returned. Must be freed with
748 * libusb_free_ss_endpoint_companion_descriptor() after use.
749 * \returns 0 on success
750 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
751 * \returns another LIBUSB_ERROR code on error
753 int API_EXPORTED libusb_get_ss_endpoint_companion_descriptor(
754 struct libusb_context *ctx,
755 const struct libusb_endpoint_descriptor *endpoint,
756 struct libusb_ss_endpoint_companion_descriptor **ep_comp)
758 struct usb_descriptor_header header;
759 int size = endpoint->extra_length;
760 const unsigned char *buffer = endpoint->extra;
764 while (size >= DESC_HEADER_LENGTH) {
765 usbi_parse_descriptor(buffer, "bb", &header, 0);
766 if (header.bLength < 2 || header.bLength > size) {
767 usbi_err(ctx, "invalid descriptor length %d",
769 return LIBUSB_ERROR_IO;
771 if (header.bDescriptorType != LIBUSB_DT_SS_ENDPOINT_COMPANION) {
772 buffer += header.bLength;
773 size -= header.bLength;
776 if (header.bLength < LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE) {
777 usbi_err(ctx, "invalid ss-ep-comp-desc length %d",
779 return LIBUSB_ERROR_IO;
781 *ep_comp = malloc(sizeof(**ep_comp));
782 if (*ep_comp == NULL)
783 return LIBUSB_ERROR_NO_MEM;
784 usbi_parse_descriptor(buffer, "bbbbw", *ep_comp, 0);
785 return LIBUSB_SUCCESS;
787 return LIBUSB_ERROR_NOT_FOUND;
790 /** \ingroup libusb_desc
791 * Free a superspeed endpoint companion descriptor obtained from
792 * libusb_get_ss_endpoint_companion_descriptor().
793 * It is safe to call this function with a NULL ep_comp parameter, in which
794 * case the function simply returns.
796 * \param ep_comp the superspeed endpoint companion descriptor to free
798 void API_EXPORTED libusb_free_ss_endpoint_companion_descriptor(
799 struct libusb_ss_endpoint_companion_descriptor *ep_comp)
804 static int parse_bos(struct libusb_context *ctx,
805 struct libusb_bos_descriptor **bos,
806 unsigned char *buffer, int size, int host_endian)
808 struct libusb_bos_descriptor bos_header, *_bos;
809 struct libusb_bos_dev_capability_descriptor dev_cap;
812 if (size < LIBUSB_DT_BOS_SIZE) {
813 usbi_err(ctx, "short bos descriptor read %d/%d",
814 size, LIBUSB_DT_BOS_SIZE);
815 return LIBUSB_ERROR_IO;
818 usbi_parse_descriptor(buffer, "bbwb", &bos_header, host_endian);
819 if (bos_header.bDescriptorType != LIBUSB_DT_BOS) {
820 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
821 bos_header.bDescriptorType, LIBUSB_DT_BOS);
822 return LIBUSB_ERROR_IO;
824 if (bos_header.bLength < LIBUSB_DT_BOS_SIZE) {
825 usbi_err(ctx, "invalid bos bLength (%d)", bos_header.bLength);
826 return LIBUSB_ERROR_IO;
828 if (bos_header.bLength > size) {
829 usbi_err(ctx, "short bos descriptor read %d/%d",
830 size, bos_header.bLength);
831 return LIBUSB_ERROR_IO;
835 sizeof(*_bos) + bos_header.bNumDeviceCaps * sizeof(void *));
837 return LIBUSB_ERROR_NO_MEM;
839 usbi_parse_descriptor(buffer, "bbwb", _bos, host_endian);
840 buffer += bos_header.bLength;
841 size -= bos_header.bLength;
843 /* Get the device capability descriptors */
844 for (i = 0; i < bos_header.bNumDeviceCaps; i++) {
845 if (size < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
846 usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
847 size, LIBUSB_DT_DEVICE_CAPABILITY_SIZE);
850 usbi_parse_descriptor(buffer, "bbb", &dev_cap, host_endian);
851 if (dev_cap.bDescriptorType != LIBUSB_DT_DEVICE_CAPABILITY) {
852 usbi_warn(ctx, "unexpected descriptor %x (expected %x)",
853 dev_cap.bDescriptorType, LIBUSB_DT_DEVICE_CAPABILITY);
856 if (dev_cap.bLength < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
857 usbi_err(ctx, "invalid dev-cap bLength (%d)",
859 libusb_free_bos_descriptor(_bos);
860 return LIBUSB_ERROR_IO;
862 if (dev_cap.bLength > size) {
863 usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
864 size, dev_cap.bLength);
868 _bos->dev_capability[i] = malloc(dev_cap.bLength);
869 if (!_bos->dev_capability[i]) {
870 libusb_free_bos_descriptor(_bos);
871 return LIBUSB_ERROR_NO_MEM;
873 memcpy(_bos->dev_capability[i], buffer, dev_cap.bLength);
874 buffer += dev_cap.bLength;
875 size -= dev_cap.bLength;
877 _bos->bNumDeviceCaps = (uint8_t)i;
880 return LIBUSB_SUCCESS;
883 /** \ingroup libusb_desc
884 * Get a Binary Object Store (BOS) descriptor
885 * This is a BLOCKING function, which will send requests to the device.
887 * \param dev_handle the handle of an open libusb device
888 * \param bos output location for the BOS descriptor. Only valid if 0 was returned.
889 * Must be freed with \ref libusb_free_bos_descriptor() after use.
890 * \returns 0 on success
891 * \returns LIBUSB_ERROR_NOT_FOUND if the device doesn't have a BOS descriptor
892 * \returns another LIBUSB_ERROR code on error
894 int API_EXPORTED libusb_get_bos_descriptor(libusb_device_handle *dev_handle,
895 struct libusb_bos_descriptor **bos)
897 struct libusb_bos_descriptor _bos;
898 uint8_t bos_header[LIBUSB_DT_BOS_SIZE] = {0};
899 unsigned char *bos_data = NULL;
900 const int host_endian = 0;
903 /* Read the BOS. This generates 2 requests on the bus,
904 * one for the header, and one for the full BOS */
905 r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, bos_header,
908 if (r != LIBUSB_ERROR_PIPE)
909 usbi_err(HANDLE_CTX(dev_handle), "failed to read BOS (%d)", r);
912 if (r < LIBUSB_DT_BOS_SIZE) {
913 usbi_err(HANDLE_CTX(dev_handle), "short BOS read %d/%d",
914 r, LIBUSB_DT_BOS_SIZE);
915 return LIBUSB_ERROR_IO;
918 usbi_parse_descriptor(bos_header, "bbwb", &_bos, host_endian);
919 usbi_dbg("found BOS descriptor: size %d bytes, %d capabilities",
920 _bos.wTotalLength, _bos.bNumDeviceCaps);
921 bos_data = calloc(_bos.wTotalLength, 1);
922 if (bos_data == NULL)
923 return LIBUSB_ERROR_NO_MEM;
925 r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, bos_data,
928 r = parse_bos(HANDLE_CTX(dev_handle), bos, bos_data, r, host_endian);
930 usbi_err(HANDLE_CTX(dev_handle), "failed to read BOS (%d)", r);
936 /** \ingroup libusb_desc
937 * Free a BOS descriptor obtained from libusb_get_bos_descriptor().
938 * It is safe to call this function with a NULL bos parameter, in which
939 * case the function simply returns.
941 * \param bos the BOS descriptor to free
943 void API_EXPORTED libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos)
950 for (i = 0; i < bos->bNumDeviceCaps; i++)
951 free(bos->dev_capability[i]);
955 /** \ingroup libusb_desc
956 * Get an USB 2.0 Extension descriptor
958 * \param ctx the context to operate on, or NULL for the default context
959 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
960 * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
961 * LIBUSB_BT_USB_2_0_EXTENSION
962 * \param usb_2_0_extension output location for the USB 2.0 Extension
963 * descriptor. Only valid if 0 was returned. Must be freed with
964 * libusb_free_usb_2_0_extension_descriptor() after use.
965 * \returns 0 on success
966 * \returns a LIBUSB_ERROR code on error
968 int API_EXPORTED libusb_get_usb_2_0_extension_descriptor(
969 struct libusb_context *ctx,
970 struct libusb_bos_dev_capability_descriptor *dev_cap,
971 struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension)
973 struct libusb_usb_2_0_extension_descriptor *_usb_2_0_extension;
974 const int host_endian = 0;
976 if (dev_cap->bDevCapabilityType != LIBUSB_BT_USB_2_0_EXTENSION) {
977 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
978 dev_cap->bDevCapabilityType,
979 LIBUSB_BT_USB_2_0_EXTENSION);
980 return LIBUSB_ERROR_INVALID_PARAM;
982 if (dev_cap->bLength < LIBUSB_BT_USB_2_0_EXTENSION_SIZE) {
983 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
984 dev_cap->bLength, LIBUSB_BT_USB_2_0_EXTENSION_SIZE);
985 return LIBUSB_ERROR_IO;
988 _usb_2_0_extension = malloc(sizeof(*_usb_2_0_extension));
989 if (!_usb_2_0_extension)
990 return LIBUSB_ERROR_NO_MEM;
992 usbi_parse_descriptor((unsigned char *)dev_cap, "bbbd",
993 _usb_2_0_extension, host_endian);
995 *usb_2_0_extension = _usb_2_0_extension;
996 return LIBUSB_SUCCESS;
999 /** \ingroup libusb_desc
1000 * Free a USB 2.0 Extension descriptor obtained from
1001 * libusb_get_usb_2_0_extension_descriptor().
1002 * It is safe to call this function with a NULL usb_2_0_extension parameter,
1003 * in which case the function simply returns.
1005 * \param usb_2_0_extension the USB 2.0 Extension descriptor to free
1007 void API_EXPORTED libusb_free_usb_2_0_extension_descriptor(
1008 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension)
1010 free(usb_2_0_extension);
1013 /** \ingroup libusb_desc
1014 * Get a SuperSpeed USB Device Capability descriptor
1016 * \param ctx the context to operate on, or NULL for the default context
1017 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1018 * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
1019 * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
1020 * \param ss_usb_device_cap output location for the SuperSpeed USB Device
1021 * Capability descriptor. Only valid if 0 was returned. Must be freed with
1022 * libusb_free_ss_usb_device_capability_descriptor() after use.
1023 * \returns 0 on success
1024 * \returns a LIBUSB_ERROR code on error
1026 int API_EXPORTED libusb_get_ss_usb_device_capability_descriptor(
1027 struct libusb_context *ctx,
1028 struct libusb_bos_dev_capability_descriptor *dev_cap,
1029 struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap)
1031 struct libusb_ss_usb_device_capability_descriptor *_ss_usb_device_cap;
1032 const int host_endian = 0;
1034 if (dev_cap->bDevCapabilityType != LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
1035 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
1036 dev_cap->bDevCapabilityType,
1037 LIBUSB_BT_SS_USB_DEVICE_CAPABILITY);
1038 return LIBUSB_ERROR_INVALID_PARAM;
1040 if (dev_cap->bLength < LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) {
1041 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
1042 dev_cap->bLength, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE);
1043 return LIBUSB_ERROR_IO;
1046 _ss_usb_device_cap = malloc(sizeof(*_ss_usb_device_cap));
1047 if (!_ss_usb_device_cap)
1048 return LIBUSB_ERROR_NO_MEM;
1050 usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbwbbw",
1051 _ss_usb_device_cap, host_endian);
1053 *ss_usb_device_cap = _ss_usb_device_cap;
1054 return LIBUSB_SUCCESS;
1057 /** \ingroup libusb_desc
1058 * Free a SuperSpeed USB Device Capability descriptor obtained from
1059 * libusb_get_ss_usb_device_capability_descriptor().
1060 * It is safe to call this function with a NULL ss_usb_device_cap
1061 * parameter, in which case the function simply returns.
1063 * \param ss_usb_device_cap the USB 2.0 Extension descriptor to free
1065 void API_EXPORTED libusb_free_ss_usb_device_capability_descriptor(
1066 struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap)
1068 free(ss_usb_device_cap);
1071 /** \ingroup libusb_desc
1072 * Get a Container ID descriptor
1074 * \param ctx the context to operate on, or NULL for the default context
1075 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1076 * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
1077 * LIBUSB_BT_CONTAINER_ID
1078 * \param container_id output location for the Container ID descriptor.
1079 * Only valid if 0 was returned. Must be freed with
1080 * libusb_free_container_id_descriptor() after use.
1081 * \returns 0 on success
1082 * \returns a LIBUSB_ERROR code on error
1084 int API_EXPORTED libusb_get_container_id_descriptor(struct libusb_context *ctx,
1085 struct libusb_bos_dev_capability_descriptor *dev_cap,
1086 struct libusb_container_id_descriptor **container_id)
1088 struct libusb_container_id_descriptor *_container_id;
1089 const int host_endian = 0;
1091 if (dev_cap->bDevCapabilityType != LIBUSB_BT_CONTAINER_ID) {
1092 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
1093 dev_cap->bDevCapabilityType,
1094 LIBUSB_BT_CONTAINER_ID);
1095 return LIBUSB_ERROR_INVALID_PARAM;
1097 if (dev_cap->bLength < LIBUSB_BT_CONTAINER_ID_SIZE) {
1098 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
1099 dev_cap->bLength, LIBUSB_BT_CONTAINER_ID_SIZE);
1100 return LIBUSB_ERROR_IO;
1103 _container_id = malloc(sizeof(*_container_id));
1105 return LIBUSB_ERROR_NO_MEM;
1107 usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbu",
1108 _container_id, host_endian);
1110 *container_id = _container_id;
1111 return LIBUSB_SUCCESS;
1114 /** \ingroup libusb_desc
1115 * Free a Container ID descriptor obtained from
1116 * libusb_get_container_id_descriptor().
1117 * It is safe to call this function with a NULL container_id parameter,
1118 * in which case the function simply returns.
1120 * \param container_id the USB 2.0 Extension descriptor to free
1122 void API_EXPORTED libusb_free_container_id_descriptor(
1123 struct libusb_container_id_descriptor *container_id)
1128 /** \ingroup libusb_desc
1129 * Retrieve a string descriptor in C style ASCII.
1131 * Wrapper around libusb_get_string_descriptor(). Uses the first language
1132 * supported by the device.
1134 * \param dev_handle a device handle
1135 * \param desc_index the index of the descriptor to retrieve
1136 * \param data output buffer for ASCII string descriptor
1137 * \param length size of data buffer
1138 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1140 int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle,
1141 uint8_t desc_index, unsigned char *data, int length)
1143 unsigned char tbuf[255]; /* Some devices choke on size > 255 */
1147 /* Asking for the zero'th index is special - it returns a string
1148 * descriptor that contains all the language IDs supported by the
1149 * device. Typically there aren't many - often only one. Language
1150 * IDs are 16 bit numbers, and they start at the third byte in the
1151 * descriptor. There's also no point in trying to read descriptor 0
1152 * with this function. See USB 2.0 specification section 9.6.7 for
1156 if (desc_index == 0)
1157 return LIBUSB_ERROR_INVALID_PARAM;
1159 r = libusb_get_string_descriptor(dev_handle, 0, 0, tbuf, sizeof(tbuf));
1164 return LIBUSB_ERROR_IO;
1166 langid = tbuf[2] | (tbuf[3] << 8);
1168 r = libusb_get_string_descriptor(dev_handle, desc_index, langid, tbuf,
1173 if (tbuf[1] != LIBUSB_DT_STRING)
1174 return LIBUSB_ERROR_IO;
1177 return LIBUSB_ERROR_IO;
1180 for (si = 2; si < tbuf[0]; si += 2) {
1181 if (di >= (length - 1))
1184 if ((tbuf[si] & 0x80) || (tbuf[si + 1])) /* non-ASCII */
1187 data[di++] = tbuf[si];