Core: Cache device descriptors in the core
[libusbx.git] / libusb / descriptor.c
blob505fc2810ea151d8830f9235920ea2f051a776dd
1 /*
2 * USB descriptor handling functions for libusbx
3 * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
4 * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #include <errno.h>
22 #include <stdint.h>
23 #include <stdlib.h>
24 #include <string.h>
26 #include "libusbi.h"
28 #define DESC_HEADER_LENGTH 2
29 #define DEVICE_DESC_LENGTH 18
30 #define CONFIG_DESC_LENGTH 9
31 #define INTERFACE_DESC_LENGTH 9
32 #define ENDPOINT_DESC_LENGTH 7
33 #define ENDPOINT_AUDIO_DESC_LENGTH 9
35 /** @defgroup desc USB descriptors
36 * This page details how to examine the various standard USB descriptors
37 * for detected devices
40 /* set host_endian if the w values are already in host endian format,
41 * as opposed to bus endian. */
42 int usbi_parse_descriptor(unsigned char *source, const char *descriptor,
43 void *dest, int host_endian)
45 unsigned char *sp = source, *dp = dest;
46 uint16_t w;
47 const char *cp;
49 for (cp = descriptor; *cp; cp++) {
50 switch (*cp) {
51 case 'b': /* 8-bit byte */
52 *dp++ = *sp++;
53 break;
54 case 'w': /* 16-bit word, convert from little endian to CPU */
55 dp += ((uintptr_t)dp & 1); /* Align to word boundary */
57 if (host_endian) {
58 memcpy(dp, sp, 2);
59 } else {
60 w = (sp[1] << 8) | sp[0];
61 *((uint16_t *)dp) = w;
63 sp += 2;
64 dp += 2;
65 break;
69 return (int) (sp - source);
72 static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
74 if (endpoint->extra)
75 free((unsigned char *) endpoint->extra);
78 static int parse_endpoint(struct libusb_context *ctx,
79 struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
80 int size, int host_endian)
82 struct usb_descriptor_header header;
83 unsigned char *extra;
84 unsigned char *begin;
85 int parsed = 0;
86 int len;
88 usbi_parse_descriptor(buffer, "bb", &header, 0);
90 /* Everything should be fine being passed into here, but we sanity */
91 /* check JIC */
92 if (header.bLength > size) {
93 usbi_err(ctx, "ran out of descriptors parsing");
94 return -1;
97 if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
98 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
99 header.bDescriptorType, LIBUSB_DT_ENDPOINT);
100 return parsed;
103 if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
104 usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
105 else if (header.bLength >= ENDPOINT_DESC_LENGTH)
106 usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
108 buffer += header.bLength;
109 size -= header.bLength;
110 parsed += header.bLength;
112 /* Skip over the rest of the Class Specific or Vendor Specific */
113 /* descriptors */
114 begin = buffer;
115 while (size >= DESC_HEADER_LENGTH) {
116 usbi_parse_descriptor(buffer, "bb", &header, 0);
118 if (header.bLength < 2) {
119 usbi_err(ctx, "invalid descriptor length %d", header.bLength);
120 return -1;
123 /* If we find another "proper" descriptor then we're done */
124 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
125 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
126 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
127 (header.bDescriptorType == LIBUSB_DT_DEVICE))
128 break;
130 usbi_dbg("skipping descriptor %x", header.bDescriptorType);
131 buffer += header.bLength;
132 size -= header.bLength;
133 parsed += header.bLength;
136 /* Copy any unknown descriptors into a storage area for drivers */
137 /* to later parse */
138 len = (int)(buffer - begin);
139 if (!len) {
140 endpoint->extra = NULL;
141 endpoint->extra_length = 0;
142 return parsed;
145 extra = malloc(len);
146 endpoint->extra = extra;
147 if (!extra) {
148 endpoint->extra_length = 0;
149 return LIBUSB_ERROR_NO_MEM;
152 memcpy(extra, begin, len);
153 endpoint->extra_length = len;
155 return parsed;
158 static void clear_interface(struct libusb_interface *usb_interface)
160 int i;
161 int j;
163 if (usb_interface->altsetting) {
164 for (i = 0; i < usb_interface->num_altsetting; i++) {
165 struct libusb_interface_descriptor *ifp =
166 (struct libusb_interface_descriptor *)
167 usb_interface->altsetting + i;
168 if (ifp->extra)
169 free((void *) ifp->extra);
170 if (ifp->endpoint) {
171 for (j = 0; j < ifp->bNumEndpoints; j++)
172 clear_endpoint((struct libusb_endpoint_descriptor *)
173 ifp->endpoint + j);
174 free((void *) ifp->endpoint);
177 free((void *) usb_interface->altsetting);
178 usb_interface->altsetting = NULL;
183 static int parse_interface(libusb_context *ctx,
184 struct libusb_interface *usb_interface, unsigned char *buffer, int size,
185 int host_endian)
187 int i;
188 int len;
189 int r;
190 int parsed = 0;
191 size_t tmp;
192 struct usb_descriptor_header header;
193 struct libusb_interface_descriptor *ifp;
194 unsigned char *begin;
196 usb_interface->num_altsetting = 0;
198 while (size >= INTERFACE_DESC_LENGTH) {
199 struct libusb_interface_descriptor *altsetting =
200 (struct libusb_interface_descriptor *) usb_interface->altsetting;
201 altsetting = usbi_reallocf(altsetting,
202 sizeof(struct libusb_interface_descriptor) *
203 (usb_interface->num_altsetting + 1));
204 if (!altsetting) {
205 r = LIBUSB_ERROR_NO_MEM;
206 goto err;
208 usb_interface->altsetting = altsetting;
210 ifp = altsetting + usb_interface->num_altsetting;
211 usb_interface->num_altsetting++;
212 usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
213 ifp->extra = NULL;
214 ifp->extra_length = 0;
215 ifp->endpoint = NULL;
217 /* Skip over the interface */
218 buffer += ifp->bLength;
219 parsed += ifp->bLength;
220 size -= ifp->bLength;
222 begin = buffer;
224 /* Skip over any interface, class or vendor descriptors */
225 while (size >= DESC_HEADER_LENGTH) {
226 usbi_parse_descriptor(buffer, "bb", &header, 0);
227 if (header.bLength < 2) {
228 usbi_err(ctx, "invalid descriptor of length %d",
229 header.bLength);
230 r = LIBUSB_ERROR_IO;
231 goto err;
232 } else if (header.bLength > size) {
233 usbi_warn(ctx, "invalid descriptor of length %d",
234 header.bLength);
235 /* The remaining bytes are bogus, but at least
236 * one interface is OK, so let's continue. */
237 break;
240 /* If we find another "proper" descriptor then we're done */
241 if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
242 (header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
243 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
244 (header.bDescriptorType == LIBUSB_DT_DEVICE))
245 break;
247 buffer += header.bLength;
248 parsed += header.bLength;
249 size -= header.bLength;
252 /* Copy any unknown descriptors into a storage area for */
253 /* drivers to later parse */
254 len = (int)(buffer - begin);
255 if (len) {
256 ifp->extra = malloc(len);
257 if (!ifp->extra) {
258 r = LIBUSB_ERROR_NO_MEM;
259 goto err;
261 memcpy((unsigned char *) ifp->extra, begin, len);
262 ifp->extra_length = len;
265 /* Did we hit an unexpected descriptor? */
266 if (size >= DESC_HEADER_LENGTH) {
267 usbi_parse_descriptor(buffer, "bb", &header, 0);
268 if ((header.bDescriptorType == LIBUSB_DT_CONFIG) ||
269 (header.bDescriptorType == LIBUSB_DT_DEVICE)) {
270 return parsed;
274 if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
275 usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);
276 r = LIBUSB_ERROR_IO;
277 goto err;
280 if (ifp->bNumEndpoints > 0) {
281 struct libusb_endpoint_descriptor *endpoint;
282 tmp = ifp->bNumEndpoints * sizeof(struct libusb_endpoint_descriptor);
283 endpoint = malloc(tmp);
284 ifp->endpoint = endpoint;
285 if (!endpoint) {
286 r = LIBUSB_ERROR_NO_MEM;
287 goto err;
290 memset(endpoint, 0, tmp);
291 for (i = 0; i < ifp->bNumEndpoints; i++) {
292 usbi_parse_descriptor(buffer, "bb", &header, 0);
294 if (header.bLength > size) {
295 usbi_err(ctx, "ran out of descriptors parsing");
296 r = LIBUSB_ERROR_IO;
297 goto err;
300 r = parse_endpoint(ctx, endpoint + i, buffer, size,
301 host_endian);
302 if (r < 0)
303 goto err;
305 buffer += r;
306 parsed += r;
307 size -= r;
311 /* We check to see if it's an alternate to this one */
312 ifp = (struct libusb_interface_descriptor *) buffer;
313 if (size < LIBUSB_DT_INTERFACE_SIZE ||
314 ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
315 !ifp->bAlternateSetting)
316 return parsed;
319 return parsed;
320 err:
321 clear_interface(usb_interface);
322 return r;
325 static void clear_configuration(struct libusb_config_descriptor *config)
327 if (config->interface) {
328 int i;
329 for (i = 0; i < config->bNumInterfaces; i++)
330 clear_interface((struct libusb_interface *)
331 config->interface + i);
332 free((void *) config->interface);
334 if (config->extra)
335 free((void *) config->extra);
338 static int parse_configuration(struct libusb_context *ctx,
339 struct libusb_config_descriptor *config, unsigned char *buffer,
340 int host_endian)
342 int i;
343 int r;
344 int size;
345 size_t tmp;
346 struct usb_descriptor_header header;
347 struct libusb_interface *usb_interface;
349 usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
350 size = config->wTotalLength;
352 if (config->bNumInterfaces > USB_MAXINTERFACES) {
353 usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
354 return LIBUSB_ERROR_IO;
357 tmp = config->bNumInterfaces * sizeof(struct libusb_interface);
358 usb_interface = malloc(tmp);
359 config->interface = usb_interface;
360 if (!config->interface)
361 return LIBUSB_ERROR_NO_MEM;
363 memset(usb_interface, 0, tmp);
364 buffer += config->bLength;
365 size -= config->bLength;
367 config->extra = NULL;
368 config->extra_length = 0;
370 for (i = 0; i < config->bNumInterfaces; i++) {
371 int len;
372 unsigned char *begin;
374 /* Skip over the rest of the Class Specific or Vendor */
375 /* Specific descriptors */
376 begin = buffer;
377 while (size >= DESC_HEADER_LENGTH) {
378 usbi_parse_descriptor(buffer, "bb", &header, 0);
380 /* If we've parsed at least one config descriptor then
381 * let's return that. */
382 if (header.bLength > size && i) {
383 usbi_warn(ctx, "invalid descriptor length of %d",
384 header.bLength);
385 return size;
388 if ((header.bLength > size) ||
389 (header.bLength < DESC_HEADER_LENGTH)) {
390 usbi_err(ctx, "invalid descriptor length of %d",
391 header.bLength);
392 r = LIBUSB_ERROR_IO;
393 goto err;
396 /* If we find another "proper" descriptor then we're done */
397 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
398 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
399 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
400 (header.bDescriptorType == LIBUSB_DT_DEVICE))
401 break;
403 usbi_dbg("skipping descriptor 0x%x\n", header.bDescriptorType);
404 buffer += header.bLength;
405 size -= header.bLength;
408 /* Copy any unknown descriptors into a storage area for */
409 /* drivers to later parse */
410 len = (int)(buffer - begin);
411 if (len) {
412 /* FIXME: We should realloc and append here */
413 if (!config->extra_length) {
414 config->extra = malloc(len);
415 if (!config->extra) {
416 r = LIBUSB_ERROR_NO_MEM;
417 goto err;
420 memcpy((unsigned char *) config->extra, begin, len);
421 config->extra_length = len;
425 r = parse_interface(ctx, usb_interface + i, buffer, size, host_endian);
426 if (r < 0)
427 goto err;
429 buffer += r;
430 size -= r;
433 return size;
435 err:
436 clear_configuration(config);
437 return r;
440 int usbi_device_cache_descriptor(libusb_device *dev)
442 int r, host_endian;
444 r = usbi_backend->get_device_descriptor(dev, (unsigned char *) &dev->device_descriptor,
445 &host_endian);
446 if (r < 0)
447 return r;
449 if (!host_endian) {
450 dev->device_descriptor.bcdUSB = libusb_le16_to_cpu(dev->device_descriptor.bcdUSB);
451 dev->device_descriptor.idVendor = libusb_le16_to_cpu(dev->device_descriptor.idVendor);
452 dev->device_descriptor.idProduct = libusb_le16_to_cpu(dev->device_descriptor.idProduct);
453 dev->device_descriptor.bcdDevice = libusb_le16_to_cpu(dev->device_descriptor.bcdDevice);
456 return LIBUSB_SUCCESS;
459 /** \ingroup desc
460 * Get the USB device descriptor for a given device.
462 * This is a non-blocking function; the device descriptor is cached in memory.
464 * \param dev the device
465 * \param desc output location for the descriptor data
466 * \returns 0 on success or a LIBUSB_ERROR code on failure
468 int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
469 struct libusb_device_descriptor *desc)
471 usbi_dbg("");
472 memcpy((unsigned char *) desc, (unsigned char *) &dev->device_descriptor,
473 sizeof (dev->device_descriptor));
474 return 0;
477 /** \ingroup desc
478 * Get the USB configuration descriptor for the currently active configuration.
479 * This is a non-blocking function which does not involve any requests being
480 * sent to the device.
482 * \param dev a device
483 * \param config output location for the USB configuration descriptor. Only
484 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
485 * after use.
486 * \returns 0 on success
487 * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
488 * \returns another LIBUSB_ERROR code on error
489 * \see libusb_get_config_descriptor
491 int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
492 struct libusb_config_descriptor **config)
494 struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
495 unsigned char tmp[8];
496 unsigned char *buf = NULL;
497 int host_endian = 0;
498 int r;
500 usbi_dbg("");
501 if (!_config)
502 return LIBUSB_ERROR_NO_MEM;
504 r = usbi_backend->get_active_config_descriptor(dev, tmp, sizeof(tmp),
505 &host_endian);
506 if (r < 0)
507 goto err;
509 _config->wTotalLength = 0;
510 usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
511 if (_config->wTotalLength != 0)
512 buf = malloc(_config->wTotalLength);
513 if (!buf) {
514 r = LIBUSB_ERROR_NO_MEM;
515 goto err;
518 r = usbi_backend->get_active_config_descriptor(dev, buf,
519 _config->wTotalLength, &host_endian);
520 if (r < 0)
521 goto err;
523 r = parse_configuration(dev->ctx, _config, buf, host_endian);
524 if (r < 0) {
525 usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
526 goto err;
527 } else if (r > 0) {
528 usbi_warn(dev->ctx, "descriptor data still left");
531 free(buf);
532 *config = _config;
533 return 0;
535 err:
536 free(_config);
537 if (buf)
538 free(buf);
539 return r;
542 /** \ingroup desc
543 * Get a USB configuration descriptor based on its index.
544 * This is a non-blocking function which does not involve any requests being
545 * sent to the device.
547 * \param dev a device
548 * \param config_index the index of the configuration you wish to retrieve
549 * \param config output location for the USB configuration descriptor. Only
550 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
551 * after use.
552 * \returns 0 on success
553 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
554 * \returns another LIBUSB_ERROR code on error
555 * \see libusb_get_active_config_descriptor()
556 * \see libusb_get_config_descriptor_by_value()
558 int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
559 uint8_t config_index, struct libusb_config_descriptor **config)
561 struct libusb_config_descriptor *_config;
562 unsigned char tmp[8];
563 unsigned char *buf = NULL;
564 int host_endian = 0;
565 int r;
567 usbi_dbg("index %d", config_index);
568 if (config_index >= dev->num_configurations)
569 return LIBUSB_ERROR_NOT_FOUND;
571 _config = malloc(sizeof(*_config));
572 if (!_config)
573 return LIBUSB_ERROR_NO_MEM;
575 r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
576 sizeof(tmp), &host_endian);
577 if (r < 0)
578 goto err;
580 usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
581 buf = malloc(_config->wTotalLength);
582 if (!buf) {
583 r = LIBUSB_ERROR_NO_MEM;
584 goto err;
587 host_endian = 0;
588 r = usbi_backend->get_config_descriptor(dev, config_index, buf,
589 _config->wTotalLength, &host_endian);
590 if (r < 0)
591 goto err;
593 r = parse_configuration(dev->ctx, _config, buf, host_endian);
594 if (r < 0) {
595 usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
596 goto err;
597 } else if (r > 0) {
598 usbi_warn(dev->ctx, "descriptor data still left");
601 free(buf);
602 *config = _config;
603 return 0;
605 err:
606 free(_config);
607 if (buf)
608 free(buf);
609 return r;
612 /* iterate through all configurations, returning the index of the configuration
613 * matching a specific bConfigurationValue in the idx output parameter, or -1
614 * if the config was not found.
615 * returns 0 or a LIBUSB_ERROR code
617 int usbi_get_config_index_by_value(struct libusb_device *dev,
618 uint8_t bConfigurationValue, int *idx)
620 uint8_t i;
622 usbi_dbg("value %d", bConfigurationValue);
623 for (i = 0; i < dev->num_configurations; i++) {
624 unsigned char tmp[6];
625 int host_endian;
626 int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
627 &host_endian);
628 if (r < 0)
629 return r;
630 if (tmp[5] == bConfigurationValue) {
631 *idx = i;
632 return 0;
636 *idx = -1;
637 return 0;
640 /** \ingroup desc
641 * Get a USB configuration descriptor with a specific bConfigurationValue.
642 * This is a non-blocking function which does not involve any requests being
643 * sent to the device.
645 * \param dev a device
646 * \param bConfigurationValue the bConfigurationValue of the configuration you
647 * wish to retrieve
648 * \param config output location for the USB configuration descriptor. Only
649 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
650 * after use.
651 * \returns 0 on success
652 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
653 * \returns another LIBUSB_ERROR code on error
654 * \see libusb_get_active_config_descriptor()
655 * \see libusb_get_config_descriptor()
657 int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
658 uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
660 int idx;
661 int r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
662 if (r < 0)
663 return r;
664 else if (idx == -1)
665 return LIBUSB_ERROR_NOT_FOUND;
666 else
667 return libusb_get_config_descriptor(dev, (uint8_t) idx, config);
670 /** \ingroup desc
671 * Free a configuration descriptor obtained from
672 * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
673 * It is safe to call this function with a NULL config parameter, in which
674 * case the function simply returns.
676 * \param config the configuration descriptor to free
678 void API_EXPORTED libusb_free_config_descriptor(
679 struct libusb_config_descriptor *config)
681 if (!config)
682 return;
684 clear_configuration(config);
685 free(config);
688 /** \ingroup desc
689 * Retrieve a string descriptor in C style ASCII.
691 * Wrapper around libusb_get_string_descriptor(). Uses the first language
692 * supported by the device.
694 * \param dev a device handle
695 * \param desc_index the index of the descriptor to retrieve
696 * \param data output buffer for ASCII string descriptor
697 * \param length size of data buffer
698 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
700 int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
701 uint8_t desc_index, unsigned char *data, int length)
703 unsigned char tbuf[255]; /* Some devices choke on size > 255 */
704 int r, si, di;
705 uint16_t langid;
707 /* Asking for the zero'th index is special - it returns a string
708 * descriptor that contains all the language IDs supported by the
709 * device. Typically there aren't many - often only one. Language
710 * IDs are 16 bit numbers, and they start at the third byte in the
711 * descriptor. There's also no point in trying to read descriptor 0
712 * with this function. See USB 2.0 specification section 9.6.7 for
713 * more information.
716 if (desc_index == 0)
717 return LIBUSB_ERROR_INVALID_PARAM;
719 r = libusb_get_string_descriptor(dev, 0, 0, tbuf, sizeof(tbuf));
720 if (r < 0)
721 return r;
723 if (r < 4)
724 return LIBUSB_ERROR_IO;
726 langid = tbuf[2] | (tbuf[3] << 8);
728 r = libusb_get_string_descriptor(dev, desc_index, langid, tbuf,
729 sizeof(tbuf));
730 if (r < 0)
731 return r;
733 if (tbuf[1] != LIBUSB_DT_STRING)
734 return LIBUSB_ERROR_IO;
736 if (tbuf[0] > r)
737 return LIBUSB_ERROR_IO;
739 for (di = 0, si = 2; si < tbuf[0]; si += 2) {
740 if (di >= (length - 1))
741 break;
743 if ((tbuf[si] & 0x80) || (tbuf[si + 1])) /* non-ASCII */
744 data[di++] = '?';
745 else
746 data[di++] = tbuf[si];
749 data[di] = 0;
750 return di;