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 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)
98 free((unsigned char *) endpoint->extra);
101 static int parse_endpoint(struct libusb_context *ctx,
102 struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
103 int size, int host_endian)
105 struct usb_descriptor_header header;
106 unsigned char *extra;
107 unsigned char *begin;
111 if (size < DESC_HEADER_LENGTH) {
112 usbi_err(ctx, "short endpoint descriptor read %d/%d",
113 size, DESC_HEADER_LENGTH);
114 return LIBUSB_ERROR_IO;
117 usbi_parse_descriptor(buffer, "bb", &header, 0);
118 if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
119 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
120 header.bDescriptorType, LIBUSB_DT_ENDPOINT);
123 if (header.bLength > size) {
124 usbi_warn(ctx, "short endpoint descriptor read %d/%d",
125 size, header.bLength);
128 if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
129 usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
130 else if (header.bLength >= ENDPOINT_DESC_LENGTH)
131 usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
133 usbi_err(ctx, "invalid endpoint bLength (%d)", header.bLength);
134 return LIBUSB_ERROR_IO;
137 buffer += header.bLength;
138 size -= header.bLength;
139 parsed += header.bLength;
141 /* Skip over the rest of the Class Specific or Vendor Specific */
144 while (size >= DESC_HEADER_LENGTH) {
145 usbi_parse_descriptor(buffer, "bb", &header, 0);
146 if (header.bLength < DESC_HEADER_LENGTH) {
147 usbi_err(ctx, "invalid extra ep desc len (%d)",
149 return LIBUSB_ERROR_IO;
150 } else if (header.bLength > size) {
151 usbi_warn(ctx, "short extra ep desc read %d/%d",
152 size, header.bLength);
156 /* If we find another "proper" descriptor then we're done */
157 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
158 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
159 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
160 (header.bDescriptorType == LIBUSB_DT_DEVICE))
163 usbi_dbg("skipping descriptor %x", header.bDescriptorType);
164 buffer += header.bLength;
165 size -= header.bLength;
166 parsed += header.bLength;
169 /* Copy any unknown descriptors into a storage area for drivers */
171 len = (int)(buffer - begin);
173 endpoint->extra = NULL;
174 endpoint->extra_length = 0;
179 endpoint->extra = extra;
181 endpoint->extra_length = 0;
182 return LIBUSB_ERROR_NO_MEM;
185 memcpy(extra, begin, len);
186 endpoint->extra_length = len;
191 static void clear_interface(struct libusb_interface *usb_interface)
196 if (usb_interface->altsetting) {
197 for (i = 0; i < usb_interface->num_altsetting; i++) {
198 struct libusb_interface_descriptor *ifp =
199 (struct libusb_interface_descriptor *)
200 usb_interface->altsetting + i;
202 free((void *) ifp->extra);
204 for (j = 0; j < ifp->bNumEndpoints; j++)
205 clear_endpoint((struct libusb_endpoint_descriptor *)
207 free((void *) ifp->endpoint);
210 free((void *) usb_interface->altsetting);
211 usb_interface->altsetting = NULL;
216 static int parse_interface(libusb_context *ctx,
217 struct libusb_interface *usb_interface, unsigned char *buffer, int size,
224 int interface_number = -1;
226 struct usb_descriptor_header header;
227 struct libusb_interface_descriptor *ifp;
228 unsigned char *begin;
230 usb_interface->num_altsetting = 0;
232 while (size >= INTERFACE_DESC_LENGTH) {
233 struct libusb_interface_descriptor *altsetting =
234 (struct libusb_interface_descriptor *) usb_interface->altsetting;
235 altsetting = usbi_reallocf(altsetting,
236 sizeof(struct libusb_interface_descriptor) *
237 (usb_interface->num_altsetting + 1));
239 r = LIBUSB_ERROR_NO_MEM;
242 usb_interface->altsetting = altsetting;
244 ifp = altsetting + usb_interface->num_altsetting;
245 usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
246 if (ifp->bDescriptorType != LIBUSB_DT_INTERFACE) {
247 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
248 ifp->bDescriptorType, LIBUSB_DT_INTERFACE);
251 if (ifp->bLength < INTERFACE_DESC_LENGTH) {
252 usbi_err(ctx, "invalid interface bLength (%d)",
257 if (ifp->bLength > size) {
258 usbi_warn(ctx, "short intf descriptor read %d/%d",
262 if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
263 usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);
268 usb_interface->num_altsetting++;
270 ifp->extra_length = 0;
271 ifp->endpoint = NULL;
273 if (interface_number == -1)
274 interface_number = ifp->bInterfaceNumber;
276 /* Skip over the interface */
277 buffer += ifp->bLength;
278 parsed += ifp->bLength;
279 size -= ifp->bLength;
283 /* Skip over any interface, class or vendor descriptors */
284 while (size >= DESC_HEADER_LENGTH) {
285 usbi_parse_descriptor(buffer, "bb", &header, 0);
286 if (header.bLength < DESC_HEADER_LENGTH) {
288 "invalid extra intf desc len (%d)",
292 } else if (header.bLength > size) {
294 "short extra intf desc read %d/%d",
295 size, header.bLength);
299 /* If we find another "proper" descriptor then we're done */
300 if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
301 (header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
302 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
303 (header.bDescriptorType == LIBUSB_DT_DEVICE))
306 buffer += header.bLength;
307 parsed += header.bLength;
308 size -= header.bLength;
311 /* Copy any unknown descriptors into a storage area for */
312 /* drivers to later parse */
313 len = (int)(buffer - begin);
315 ifp->extra = malloc(len);
317 r = LIBUSB_ERROR_NO_MEM;
320 memcpy((unsigned char *) ifp->extra, begin, len);
321 ifp->extra_length = len;
324 if (ifp->bNumEndpoints > 0) {
325 struct libusb_endpoint_descriptor *endpoint;
326 tmp = ifp->bNumEndpoints * sizeof(struct libusb_endpoint_descriptor);
327 endpoint = malloc(tmp);
328 ifp->endpoint = endpoint;
330 r = LIBUSB_ERROR_NO_MEM;
334 memset(endpoint, 0, tmp);
335 for (i = 0; i < ifp->bNumEndpoints; i++) {
336 r = parse_endpoint(ctx, endpoint + i, buffer, size,
341 ifp->bNumEndpoints = (uint8_t)i;
351 /* We check to see if it's an alternate to this one */
352 ifp = (struct libusb_interface_descriptor *) buffer;
353 if (size < LIBUSB_DT_INTERFACE_SIZE ||
354 ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
355 ifp->bInterfaceNumber != interface_number)
361 clear_interface(usb_interface);
365 static void clear_configuration(struct libusb_config_descriptor *config)
367 if (config->interface) {
369 for (i = 0; i < config->bNumInterfaces; i++)
370 clear_interface((struct libusb_interface *)
371 config->interface + i);
372 free((void *) config->interface);
375 free((void *) config->extra);
378 static int parse_configuration(struct libusb_context *ctx,
379 struct libusb_config_descriptor *config, unsigned char *buffer,
380 int size, int host_endian)
385 struct usb_descriptor_header header;
386 struct libusb_interface *usb_interface;
388 if (size < LIBUSB_DT_CONFIG_SIZE) {
389 usbi_err(ctx, "short config descriptor read %d/%d",
390 size, LIBUSB_DT_CONFIG_SIZE);
391 return LIBUSB_ERROR_IO;
394 usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
395 if (config->bDescriptorType != LIBUSB_DT_CONFIG) {
396 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
397 config->bDescriptorType, LIBUSB_DT_CONFIG);
398 return LIBUSB_ERROR_IO;
400 if (config->bLength < LIBUSB_DT_CONFIG_SIZE) {
401 usbi_err(ctx, "invalid config bLength (%d)", config->bLength);
402 return LIBUSB_ERROR_IO;
404 if (config->bLength > size) {
405 usbi_err(ctx, "short config descriptor read %d/%d",
406 size, config->bLength);
407 return LIBUSB_ERROR_IO;
409 if (config->bNumInterfaces > USB_MAXINTERFACES) {
410 usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
411 return LIBUSB_ERROR_IO;
414 tmp = config->bNumInterfaces * sizeof(struct libusb_interface);
415 usb_interface = malloc(tmp);
416 config->interface = usb_interface;
417 if (!config->interface)
418 return LIBUSB_ERROR_NO_MEM;
420 memset(usb_interface, 0, tmp);
421 buffer += config->bLength;
422 size -= config->bLength;
424 config->extra = NULL;
425 config->extra_length = 0;
427 for (i = 0; i < config->bNumInterfaces; i++) {
429 unsigned char *begin;
431 /* Skip over the rest of the Class Specific or Vendor */
432 /* Specific descriptors */
434 while (size >= DESC_HEADER_LENGTH) {
435 usbi_parse_descriptor(buffer, "bb", &header, 0);
437 if (header.bLength < DESC_HEADER_LENGTH) {
439 "invalid extra config desc len (%d)",
443 } else if (header.bLength > size) {
445 "short extra config desc read %d/%d",
446 size, header.bLength);
447 config->bNumInterfaces = (uint8_t)i;
451 /* If we find another "proper" descriptor then we're done */
452 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
453 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
454 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
455 (header.bDescriptorType == LIBUSB_DT_DEVICE))
458 usbi_dbg("skipping descriptor 0x%x", header.bDescriptorType);
459 buffer += header.bLength;
460 size -= header.bLength;
463 /* Copy any unknown descriptors into a storage area for */
464 /* drivers to later parse */
465 len = (int)(buffer - begin);
467 /* FIXME: We should realloc and append here */
468 if (!config->extra_length) {
469 config->extra = malloc(len);
470 if (!config->extra) {
471 r = LIBUSB_ERROR_NO_MEM;
475 memcpy((unsigned char *) config->extra, begin, len);
476 config->extra_length = len;
480 r = parse_interface(ctx, usb_interface + i, buffer, size, host_endian);
484 config->bNumInterfaces = (uint8_t)i;
495 clear_configuration(config);
499 static int raw_desc_to_config(struct libusb_context *ctx,
500 unsigned char *buf, int size, int host_endian,
501 struct libusb_config_descriptor **config)
503 struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
507 return LIBUSB_ERROR_NO_MEM;
509 r = parse_configuration(ctx, _config, buf, size, host_endian);
511 usbi_err(ctx, "parse_configuration failed with error %d", r);
515 usbi_warn(ctx, "still %d bytes of descriptor data left", r);
519 return LIBUSB_SUCCESS;
522 int usbi_device_cache_descriptor(libusb_device *dev)
524 int r, host_endian = 0;
526 r = usbi_backend->get_device_descriptor(dev, (unsigned char *) &dev->device_descriptor,
532 dev->device_descriptor.bcdUSB = libusb_le16_to_cpu(dev->device_descriptor.bcdUSB);
533 dev->device_descriptor.idVendor = libusb_le16_to_cpu(dev->device_descriptor.idVendor);
534 dev->device_descriptor.idProduct = libusb_le16_to_cpu(dev->device_descriptor.idProduct);
535 dev->device_descriptor.bcdDevice = libusb_le16_to_cpu(dev->device_descriptor.bcdDevice);
538 return LIBUSB_SUCCESS;
542 * Get the USB device descriptor for a given device.
544 * This is a non-blocking function; the device descriptor is cached in memory.
546 * Note since libusb-1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102, this
547 * function always succeeds.
549 * \param dev the device
550 * \param desc output location for the descriptor data
551 * \returns 0 on success or a LIBUSB_ERROR code on failure
553 int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
554 struct libusb_device_descriptor *desc)
557 memcpy((unsigned char *) desc, (unsigned char *) &dev->device_descriptor,
558 sizeof (dev->device_descriptor));
563 * Get the USB configuration descriptor for the currently active configuration.
564 * This is a non-blocking function which does not involve any requests being
565 * sent to the device.
567 * \param dev a device
568 * \param config output location for the USB configuration descriptor. Only
569 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
571 * \returns 0 on success
572 * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
573 * \returns another LIBUSB_ERROR code on error
574 * \see libusb_get_config_descriptor
576 int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
577 struct libusb_config_descriptor **config)
579 struct libusb_config_descriptor _config;
580 unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
581 unsigned char *buf = NULL;
585 r = usbi_backend->get_active_config_descriptor(dev, tmp,
586 LIBUSB_DT_CONFIG_SIZE, &host_endian);
589 if (r < LIBUSB_DT_CONFIG_SIZE) {
590 usbi_err(dev->ctx, "short config descriptor read %d/%d",
591 r, LIBUSB_DT_CONFIG_SIZE);
592 return LIBUSB_ERROR_IO;
595 usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
596 buf = malloc(_config.wTotalLength);
598 return LIBUSB_ERROR_NO_MEM;
600 r = usbi_backend->get_active_config_descriptor(dev, buf,
601 _config.wTotalLength, &host_endian);
603 r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
610 * Get a USB configuration descriptor based on its index.
611 * This is a non-blocking function which does not involve any requests being
612 * sent to the device.
614 * \param dev a device
615 * \param config_index the index of the configuration you wish to retrieve
616 * \param config output location for the USB configuration descriptor. Only
617 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
619 * \returns 0 on success
620 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
621 * \returns another LIBUSB_ERROR code on error
622 * \see libusb_get_active_config_descriptor()
623 * \see libusb_get_config_descriptor_by_value()
625 int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
626 uint8_t config_index, struct libusb_config_descriptor **config)
628 struct libusb_config_descriptor _config;
629 unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
630 unsigned char *buf = NULL;
634 usbi_dbg("index %d", config_index);
635 if (config_index >= dev->num_configurations)
636 return LIBUSB_ERROR_NOT_FOUND;
638 r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
639 LIBUSB_DT_CONFIG_SIZE, &host_endian);
642 if (r < LIBUSB_DT_CONFIG_SIZE) {
643 usbi_err(dev->ctx, "short config descriptor read %d/%d",
644 r, LIBUSB_DT_CONFIG_SIZE);
645 return LIBUSB_ERROR_IO;
648 usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
649 buf = malloc(_config.wTotalLength);
651 return LIBUSB_ERROR_NO_MEM;
653 r = usbi_backend->get_config_descriptor(dev, config_index, buf,
654 _config.wTotalLength, &host_endian);
656 r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
662 /* iterate through all configurations, returning the index of the configuration
663 * matching a specific bConfigurationValue in the idx output parameter, or -1
664 * if the config was not found.
665 * returns 0 on success or a LIBUSB_ERROR code
667 int usbi_get_config_index_by_value(struct libusb_device *dev,
668 uint8_t bConfigurationValue, int *idx)
672 usbi_dbg("value %d", bConfigurationValue);
673 for (i = 0; i < dev->num_configurations; i++) {
674 unsigned char tmp[6];
676 int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
682 if (tmp[5] == bConfigurationValue) {
693 * Get a USB configuration descriptor with a specific bConfigurationValue.
694 * This is a non-blocking function which does not involve any requests being
695 * sent to the device.
697 * \param dev a device
698 * \param bConfigurationValue the bConfigurationValue of the configuration you
700 * \param config output location for the USB configuration descriptor. Only
701 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
703 * \returns 0 on success
704 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
705 * \returns another LIBUSB_ERROR code on error
706 * \see libusb_get_active_config_descriptor()
707 * \see libusb_get_config_descriptor()
709 int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
710 uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
712 int r, idx, host_endian;
713 unsigned char *buf = NULL;
715 if (usbi_backend->get_config_descriptor_by_value) {
716 r = usbi_backend->get_config_descriptor_by_value(dev,
717 bConfigurationValue, &buf, &host_endian);
720 return raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
723 r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
727 return LIBUSB_ERROR_NOT_FOUND;
729 return libusb_get_config_descriptor(dev, (uint8_t) idx, config);
733 * Free a configuration descriptor obtained from
734 * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
735 * It is safe to call this function with a NULL config parameter, in which
736 * case the function simply returns.
738 * \param config the configuration descriptor to free
740 void API_EXPORTED libusb_free_config_descriptor(
741 struct libusb_config_descriptor *config)
746 clear_configuration(config);
751 * Get an endpoints superspeed endpoint companion descriptor (if any)
753 * \param ctx the context to operate on, or NULL for the default context
754 * \param endpoint endpoint descriptor from which to get the superspeed
755 * endpoint companion descriptor
756 * \param ep_comp output location for the superspeed endpoint companion
757 * descriptor. Only valid if 0 was returned. Must be freed with
758 * libusb_free_ss_endpoint_companion_descriptor() after use.
759 * \returns 0 on success
760 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
761 * \returns another LIBUSB_ERROR code on error
763 int API_EXPORTED libusb_get_ss_endpoint_companion_descriptor(
764 struct libusb_context *ctx,
765 const struct libusb_endpoint_descriptor *endpoint,
766 struct libusb_ss_endpoint_companion_descriptor **ep_comp)
768 struct usb_descriptor_header header;
769 int size = endpoint->extra_length;
770 const unsigned char *buffer = endpoint->extra;
774 while (size >= DESC_HEADER_LENGTH) {
775 usbi_parse_descriptor(buffer, "bb", &header, 0);
776 if (header.bLength < 2 || header.bLength > size) {
777 usbi_err(ctx, "invalid descriptor length %d",
779 return LIBUSB_ERROR_IO;
781 if (header.bDescriptorType != LIBUSB_DT_SS_ENDPOINT_COMPANION) {
782 buffer += header.bLength;
783 size -= header.bLength;
786 if (header.bLength < LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE) {
787 usbi_err(ctx, "invalid ss-ep-comp-desc length %d",
789 return LIBUSB_ERROR_IO;
791 *ep_comp = malloc(sizeof(**ep_comp));
792 if (*ep_comp == NULL)
793 return LIBUSB_ERROR_NO_MEM;
794 usbi_parse_descriptor(buffer, "bbbbw", *ep_comp, 0);
795 return LIBUSB_SUCCESS;
797 return LIBUSB_ERROR_NOT_FOUND;
801 * Free a superspeed endpoint companion descriptor obtained from
802 * libusb_get_ss_endpoint_companion_descriptor().
803 * It is safe to call this function with a NULL ep_comp parameter, in which
804 * case the function simply returns.
806 * \param ep_comp the superspeed endpoint companion descriptor to free
808 void API_EXPORTED libusb_free_ss_endpoint_companion_descriptor(
809 struct libusb_ss_endpoint_companion_descriptor *ep_comp)
814 static int parse_bos(struct libusb_context *ctx,
815 struct libusb_bos_descriptor **bos,
816 unsigned char *buffer, int size, int host_endian)
818 struct libusb_bos_descriptor bos_header, *_bos;
819 struct libusb_bos_dev_capability_descriptor dev_cap;
822 if (size < LIBUSB_DT_BOS_SIZE) {
823 usbi_err(ctx, "short bos descriptor read %d/%d",
824 size, LIBUSB_DT_BOS_SIZE);
825 return LIBUSB_ERROR_IO;
828 usbi_parse_descriptor(buffer, "bbwb", &bos_header, host_endian);
829 if (bos_header.bDescriptorType != LIBUSB_DT_BOS) {
830 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
831 bos_header.bDescriptorType, LIBUSB_DT_BOS);
832 return LIBUSB_ERROR_IO;
834 if (bos_header.bLength < LIBUSB_DT_BOS_SIZE) {
835 usbi_err(ctx, "invalid bos bLength (%d)", bos_header.bLength);
836 return LIBUSB_ERROR_IO;
838 if (bos_header.bLength > size) {
839 usbi_err(ctx, "short bos descriptor read %d/%d",
840 size, bos_header.bLength);
841 return LIBUSB_ERROR_IO;
845 sizeof(*_bos) + bos_header.bNumDeviceCaps * sizeof(void *));
847 return LIBUSB_ERROR_NO_MEM;
849 usbi_parse_descriptor(buffer, "bbwb", _bos, host_endian);
850 buffer += bos_header.bLength;
851 size -= bos_header.bLength;
853 /* Get the device capability descriptors */
854 for (i = 0; i < bos_header.bNumDeviceCaps; i++) {
855 if (size < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
856 usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
857 size, LIBUSB_DT_DEVICE_CAPABILITY_SIZE);
860 usbi_parse_descriptor(buffer, "bbb", &dev_cap, host_endian);
861 if (dev_cap.bDescriptorType != LIBUSB_DT_DEVICE_CAPABILITY) {
862 usbi_warn(ctx, "unexpected descriptor %x (expected %x)",
863 dev_cap.bDescriptorType, LIBUSB_DT_DEVICE_CAPABILITY);
866 if (dev_cap.bLength < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
867 usbi_err(ctx, "invalid dev-cap bLength (%d)",
869 libusb_free_bos_descriptor(_bos);
870 return LIBUSB_ERROR_IO;
872 if (dev_cap.bLength > size) {
873 usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
874 size, dev_cap.bLength);
878 _bos->dev_capability[i] = malloc(dev_cap.bLength);
879 if (!_bos->dev_capability[i]) {
880 libusb_free_bos_descriptor(_bos);
881 return LIBUSB_ERROR_NO_MEM;
883 memcpy(_bos->dev_capability[i], buffer, dev_cap.bLength);
884 buffer += dev_cap.bLength;
885 size -= dev_cap.bLength;
887 _bos->bNumDeviceCaps = (uint8_t)i;
890 return LIBUSB_SUCCESS;
894 * Get a Binary Object Store (BOS) descriptor
895 * This is a BLOCKING function, which will send requests to the device.
897 * \param handle the handle of an open libusb device
898 * \param bos output location for the BOS descriptor. Only valid if 0 was returned.
899 * Must be freed with \ref libusb_free_bos_descriptor() after use.
900 * \returns 0 on success
901 * \returns LIBUSB_ERROR_NOT_FOUND if the device doesn't have a BOS descriptor
902 * \returns another LIBUSB_ERROR code on error
904 int API_EXPORTED libusb_get_bos_descriptor(libusb_device_handle *handle,
905 struct libusb_bos_descriptor **bos)
907 struct libusb_bos_descriptor _bos;
908 uint8_t bos_header[LIBUSB_DT_BOS_SIZE] = {0};
909 unsigned char *bos_data = NULL;
910 const int host_endian = 0;
913 /* Read the BOS. This generates 2 requests on the bus,
914 * one for the header, and one for the full BOS */
915 r = libusb_get_descriptor(handle, LIBUSB_DT_BOS, 0, bos_header,
918 if (r != LIBUSB_ERROR_PIPE)
919 usbi_err(handle->dev->ctx, "failed to read BOS (%d)", r);
922 if (r < LIBUSB_DT_BOS_SIZE) {
923 usbi_err(handle->dev->ctx, "short BOS read %d/%d",
924 r, LIBUSB_DT_BOS_SIZE);
925 return LIBUSB_ERROR_IO;
928 usbi_parse_descriptor(bos_header, "bbwb", &_bos, host_endian);
929 usbi_dbg("found BOS descriptor: size %d bytes, %d capabilities",
930 _bos.wTotalLength, _bos.bNumDeviceCaps);
931 bos_data = calloc(_bos.wTotalLength, 1);
932 if (bos_data == NULL)
933 return LIBUSB_ERROR_NO_MEM;
935 r = libusb_get_descriptor(handle, LIBUSB_DT_BOS, 0, bos_data,
938 r = parse_bos(handle->dev->ctx, bos, bos_data, r, host_endian);
940 usbi_err(handle->dev->ctx, "failed to read BOS (%d)", r);
947 * Free a BOS descriptor obtained from libusb_get_bos_descriptor().
948 * It is safe to call this function with a NULL bos parameter, in which
949 * case the function simply returns.
951 * \param bos the BOS descriptor to free
953 void API_EXPORTED libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos)
960 for (i = 0; i < bos->bNumDeviceCaps; i++)
961 free(bos->dev_capability[i]);
966 * Get an USB 2.0 Extension descriptor
968 * \param ctx the context to operate on, or NULL for the default context
969 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
970 * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
971 * LIBUSB_BT_USB_2_0_EXTENSION
972 * \param usb_2_0_extension output location for the USB 2.0 Extension
973 * descriptor. Only valid if 0 was returned. Must be freed with
974 * libusb_free_usb_2_0_extension_descriptor() after use.
975 * \returns 0 on success
976 * \returns a LIBUSB_ERROR code on error
978 int API_EXPORTED libusb_get_usb_2_0_extension_descriptor(
979 struct libusb_context *ctx,
980 struct libusb_bos_dev_capability_descriptor *dev_cap,
981 struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension)
983 struct libusb_usb_2_0_extension_descriptor *_usb_2_0_extension;
984 const int host_endian = 0;
986 if (dev_cap->bDevCapabilityType != LIBUSB_BT_USB_2_0_EXTENSION) {
987 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
988 dev_cap->bDevCapabilityType,
989 LIBUSB_BT_USB_2_0_EXTENSION);
990 return LIBUSB_ERROR_INVALID_PARAM;
992 if (dev_cap->bLength < LIBUSB_BT_USB_2_0_EXTENSION_SIZE) {
993 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
994 dev_cap->bLength, LIBUSB_BT_USB_2_0_EXTENSION_SIZE);
995 return LIBUSB_ERROR_IO;
998 _usb_2_0_extension = malloc(sizeof(*_usb_2_0_extension));
999 if (!_usb_2_0_extension)
1000 return LIBUSB_ERROR_NO_MEM;
1002 usbi_parse_descriptor((unsigned char *)dev_cap, "bbbd",
1003 _usb_2_0_extension, host_endian);
1005 *usb_2_0_extension = _usb_2_0_extension;
1006 return LIBUSB_SUCCESS;
1010 * Free a USB 2.0 Extension descriptor obtained from
1011 * libusb_get_usb_2_0_extension_descriptor().
1012 * It is safe to call this function with a NULL usb_2_0_extension parameter,
1013 * in which case the function simply returns.
1015 * \param usb_2_0_extension the USB 2.0 Extension descriptor to free
1017 void API_EXPORTED libusb_free_usb_2_0_extension_descriptor(
1018 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension)
1020 free(usb_2_0_extension);
1024 * Get a SuperSpeed USB Device Capability descriptor
1026 * \param ctx the context to operate on, or NULL for the default context
1027 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1028 * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
1029 * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
1030 * \param ss_usb_device_cap output location for the SuperSpeed USB Device
1031 * Capability descriptor. Only valid if 0 was returned. Must be freed with
1032 * libusb_free_ss_usb_device_capability_descriptor() after use.
1033 * \returns 0 on success
1034 * \returns a LIBUSB_ERROR code on error
1036 int API_EXPORTED libusb_get_ss_usb_device_capability_descriptor(
1037 struct libusb_context *ctx,
1038 struct libusb_bos_dev_capability_descriptor *dev_cap,
1039 struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap)
1041 struct libusb_ss_usb_device_capability_descriptor *_ss_usb_device_cap;
1042 const int host_endian = 0;
1044 if (dev_cap->bDevCapabilityType != LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
1045 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
1046 dev_cap->bDevCapabilityType,
1047 LIBUSB_BT_SS_USB_DEVICE_CAPABILITY);
1048 return LIBUSB_ERROR_INVALID_PARAM;
1050 if (dev_cap->bLength < LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) {
1051 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
1052 dev_cap->bLength, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE);
1053 return LIBUSB_ERROR_IO;
1056 _ss_usb_device_cap = malloc(sizeof(*_ss_usb_device_cap));
1057 if (!_ss_usb_device_cap)
1058 return LIBUSB_ERROR_NO_MEM;
1060 usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbwbbw",
1061 _ss_usb_device_cap, host_endian);
1063 *ss_usb_device_cap = _ss_usb_device_cap;
1064 return LIBUSB_SUCCESS;
1068 * Free a SuperSpeed USB Device Capability descriptor obtained from
1069 * libusb_get_ss_usb_device_capability_descriptor().
1070 * It is safe to call this function with a NULL ss_usb_device_cap
1071 * parameter, in which case the function simply returns.
1073 * \param ss_usb_device_cap the USB 2.0 Extension descriptor to free
1075 void API_EXPORTED libusb_free_ss_usb_device_capability_descriptor(
1076 struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap)
1078 free(ss_usb_device_cap);
1082 * Get a Container ID descriptor
1084 * \param ctx the context to operate on, or NULL for the default context
1085 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1086 * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
1087 * LIBUSB_BT_CONTAINER_ID
1088 * \param container_id output location for the Container ID descriptor.
1089 * Only valid if 0 was returned. Must be freed with
1090 * libusb_free_container_id_descriptor() after use.
1091 * \returns 0 on success
1092 * \returns a LIBUSB_ERROR code on error
1094 int API_EXPORTED libusb_get_container_id_descriptor(struct libusb_context *ctx,
1095 struct libusb_bos_dev_capability_descriptor *dev_cap,
1096 struct libusb_container_id_descriptor **container_id)
1098 struct libusb_container_id_descriptor *_container_id;
1099 const int host_endian = 0;
1101 if (dev_cap->bDevCapabilityType != LIBUSB_BT_CONTAINER_ID) {
1102 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
1103 dev_cap->bDevCapabilityType,
1104 LIBUSB_BT_CONTAINER_ID);
1105 return LIBUSB_ERROR_INVALID_PARAM;
1107 if (dev_cap->bLength < LIBUSB_BT_CONTAINER_ID_SIZE) {
1108 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
1109 dev_cap->bLength, LIBUSB_BT_CONTAINER_ID_SIZE);
1110 return LIBUSB_ERROR_IO;
1113 _container_id = malloc(sizeof(*_container_id));
1115 return LIBUSB_ERROR_NO_MEM;
1117 usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbu",
1118 _container_id, host_endian);
1120 *container_id = _container_id;
1121 return LIBUSB_SUCCESS;
1125 * Free a Container ID descriptor obtained from
1126 * libusb_get_container_id_descriptor().
1127 * It is safe to call this function with a NULL container_id parameter,
1128 * in which case the function simply returns.
1130 * \param container_id the USB 2.0 Extension descriptor to free
1132 void API_EXPORTED libusb_free_container_id_descriptor(
1133 struct libusb_container_id_descriptor *container_id)
1139 * Retrieve a string descriptor in C style ASCII.
1141 * Wrapper around libusb_get_string_descriptor(). Uses the first language
1142 * supported by the device.
1144 * \param dev a device handle
1145 * \param desc_index the index of the descriptor to retrieve
1146 * \param data output buffer for ASCII string descriptor
1147 * \param length size of data buffer
1148 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1150 int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1151 uint8_t desc_index, unsigned char *data, int length)
1153 unsigned char tbuf[255]; /* Some devices choke on size > 255 */
1157 /* Asking for the zero'th index is special - it returns a string
1158 * descriptor that contains all the language IDs supported by the
1159 * device. Typically there aren't many - often only one. Language
1160 * IDs are 16 bit numbers, and they start at the third byte in the
1161 * descriptor. There's also no point in trying to read descriptor 0
1162 * with this function. See USB 2.0 specification section 9.6.7 for
1166 if (desc_index == 0)
1167 return LIBUSB_ERROR_INVALID_PARAM;
1169 r = libusb_get_string_descriptor(dev, 0, 0, tbuf, sizeof(tbuf));
1174 return LIBUSB_ERROR_IO;
1176 langid = tbuf[2] | (tbuf[3] << 8);
1178 r = libusb_get_string_descriptor(dev, desc_index, langid, tbuf,
1183 if (tbuf[1] != LIBUSB_DT_STRING)
1184 return LIBUSB_ERROR_IO;
1187 return LIBUSB_ERROR_IO;
1189 for (di = 0, si = 2; si < tbuf[0]; si += 2) {
1190 if (di >= (length - 1))
1193 if ((tbuf[si] & 0x80) || (tbuf[si + 1])) /* non-ASCII */
1196 data[di++] = tbuf[si];