2 * USB descriptor handling functions for libusb
3 * Copyright (C) 2007 Daniel Drake <dsd@gentoo.org>
4 * Copyright (c) 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
27 #define DESC_HEADER_LENGTH 2
28 #define DEVICE_DESC_LENGTH 18
29 #define CONFIG_DESC_LENGTH 9
30 #define INTERFACE_DESC_LENGTH 9
31 #define ENDPOINT_DESC_LENGTH 7
32 #define ENDPOINT_AUDIO_DESC_LENGTH 9
34 /** @defgroup desc USB descriptors
35 * This page details how to examine the various standard USB descriptors
36 * for detected devices
39 int usbi_parse_descriptor(unsigned char *source, char *descriptor, void *dest)
41 unsigned char *sp = source, *dp = dest;
46 for (cp = descriptor; *cp; cp++) {
48 case 'b': /* 8-bit byte */
51 case 'w': /* 16-bit word, convert from little endian to CPU */
52 w = (sp[1] << 8) | sp[0]; sp += 2;
53 dp += ((unsigned long)dp & 1); /* Align to word boundary */
54 *((uint16_t *)dp) = w; dp += 2;
56 case 'd': /* 32-bit dword, convert from little endian to CPU */
57 d = (sp[3] << 24) | (sp[2] << 16) | (sp[1] << 8) | sp[0]; sp += 4;
58 dp += ((unsigned long)dp & 2); /* Align to dword boundary */
59 *((uint32_t *)dp) = d; dp += 4;
61 case 'W': /* 16-bit word, keep CPU endianess */
62 dp += ((unsigned long)dp & 1); /* Align to word boundary */
63 memcpy(dp, sp, 2); sp += 2; dp += 2;
65 case 'D': /* 32-bit dword, keep CPU endianess */
66 dp += ((unsigned long)dp & 2); /* Align to dword boundary */
67 memcpy(dp, sp, 4); sp += 4; dp += 4;
75 static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
78 free((unsigned char *) endpoint->extra);
81 static int parse_endpoint(struct libusb_endpoint_descriptor *endpoint,
82 unsigned char *buffer, int size)
84 struct usb_descriptor_header header;
90 usbi_parse_descriptor(buffer, "bb", &header);
92 /* Everything should be fine being passed into here, but we sanity */
94 if (header.bLength > size) {
95 usbi_err("ran out of descriptors parsing");
99 if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
100 usbi_err("unexpected descriptor %x (expected %x)",
101 header.bDescriptorType, LIBUSB_DT_ENDPOINT);
105 if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
106 usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint);
107 else if (header.bLength >= ENDPOINT_DESC_LENGTH)
108 usbi_parse_descriptor(buffer, "bbbbwb", endpoint);
110 buffer += header.bLength;
111 size -= header.bLength;
112 parsed += header.bLength;
114 /* Skip over the rest of the Class Specific or Vendor Specific */
117 while (size >= DESC_HEADER_LENGTH) {
118 usbi_parse_descriptor(buffer, "bb", &header);
120 if (header.bLength < 2) {
121 usbi_err("invalid descriptor length %d", header.bLength);
125 /* If we find another "proper" descriptor then we're done */
126 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
127 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
128 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
129 (header.bDescriptorType == LIBUSB_DT_DEVICE))
132 usbi_dbg("skipping descriptor %x", header.bDescriptorType);
133 buffer += header.bLength;
134 size -= header.bLength;
135 parsed += header.bLength;
138 /* Copy any unknown descriptors into a storage area for drivers */
140 len = (int)(buffer - begin);
142 endpoint->extra = NULL;
143 endpoint->extralen = 0;
148 endpoint->extra = extra;
150 endpoint->extralen = 0;
151 return LIBUSB_ERROR_NO_MEM;
154 memcpy(extra, begin, len);
155 endpoint->extralen = len;
160 static void clear_interface(struct libusb_interface *interface)
165 if (interface->altsetting) {
166 for (i = 0; i < interface->num_altsetting; i++) {
167 struct libusb_interface_descriptor *ifp =
168 (struct libusb_interface_descriptor *)
169 interface->altsetting + i;
171 free((void *) ifp->extra);
173 for (j = 0; j < ifp->bNumEndpoints; j++)
174 clear_endpoint((struct libusb_endpoint_descriptor *)
176 free((void *) ifp->endpoint);
179 free((void *) interface->altsetting);
184 static int parse_interface(struct libusb_interface *interface,
185 unsigned char *buffer, int size)
192 struct usb_descriptor_header header;
193 struct libusb_interface_descriptor *ifp;
194 unsigned char *begin;
196 interface->num_altsetting = 0;
198 while (size >= INTERFACE_DESC_LENGTH) {
199 struct libusb_interface_descriptor *altsetting =
200 (struct libusb_interface_descriptor *) interface->altsetting;
201 altsetting = realloc(altsetting,
202 sizeof(struct libusb_interface_descriptor) *
203 (interface->num_altsetting + 1));
205 r = LIBUSB_ERROR_NO_MEM;
208 interface->altsetting = altsetting;
210 ifp = altsetting + interface->num_altsetting;
211 interface->num_altsetting++;
212 usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp);
215 ifp->endpoint = NULL;
217 /* Skip over the interface */
218 buffer += ifp->bLength;
219 parsed += ifp->bLength;
220 size -= ifp->bLength;
224 /* Skip over any interface, class or vendor descriptors */
225 while (size >= DESC_HEADER_LENGTH) {
226 usbi_parse_descriptor(buffer, "bb", &header);
227 if (header.bLength < 2) {
228 usbi_err("invalid descriptor of length %d", header.bLength);
233 /* If we find another "proper" descriptor then we're done */
234 if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
235 (header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
236 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
237 (header.bDescriptorType == LIBUSB_DT_DEVICE))
240 buffer += header.bLength;
241 parsed += header.bLength;
242 size -= header.bLength;
245 /* Copy any unknown descriptors into a storage area for */
246 /* drivers to later parse */
247 len = (int)(buffer - begin);
249 ifp->extra = malloc(len);
251 r = LIBUSB_ERROR_NO_MEM;
254 memcpy((unsigned char *) ifp->extra, begin, len);
258 /* Did we hit an unexpected descriptor? */
259 usbi_parse_descriptor(buffer, "bb", &header);
260 if ((size >= DESC_HEADER_LENGTH) &&
261 ((header.bDescriptorType == LIBUSB_DT_CONFIG) ||
262 (header.bDescriptorType == LIBUSB_DT_DEVICE)))
265 if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
266 usbi_err("too many endpoints (%d)", ifp->bNumEndpoints);
271 if (ifp->bNumEndpoints > 0) {
272 struct libusb_endpoint_descriptor *endpoint;
273 tmp = ifp->bNumEndpoints * sizeof(struct libusb_endpoint_descriptor);
274 endpoint = malloc(tmp);
275 ifp->endpoint = endpoint;
277 r = LIBUSB_ERROR_NO_MEM;
281 memset(endpoint, 0, tmp);
282 for (i = 0; i < ifp->bNumEndpoints; i++) {
283 usbi_parse_descriptor(buffer, "bb", &header);
285 if (header.bLength > size) {
286 usbi_err("ran out of descriptors parsing");
291 r = parse_endpoint(endpoint + i, buffer, size);
301 /* We check to see if it's an alternate to this one */
302 ifp = (struct libusb_interface_descriptor *) buffer;
303 if (size < LIBUSB_DT_INTERFACE_SIZE ||
304 ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
305 !ifp->bAlternateSetting)
311 clear_interface(interface);
315 static void clear_configuration(struct libusb_config_descriptor *config)
317 if (config->interface) {
319 for (i = 0; i < config->bNumInterfaces; i++)
320 clear_interface((struct libusb_interface *)
321 config->interface + i);
322 free((void *) config->interface);
325 free((void *) config->extra);
328 void usbi_clear_configurations(struct libusb_device *dev)
331 for (i = 0; i < dev->desc.bNumConfigurations; i++)
332 clear_configuration(dev->config + i);
335 int usbi_parse_configuration(struct libusb_config_descriptor *config,
336 unsigned char *buffer)
342 struct usb_descriptor_header header;
343 struct libusb_interface *interface;
345 usbi_parse_descriptor(buffer, "bbwbbbbb", config);
346 size = config->wTotalLength;
348 if (config->bNumInterfaces > USB_MAXINTERFACES) {
349 usbi_err("too many interfaces (%d)", config->bNumInterfaces);
350 return LIBUSB_ERROR_IO;
353 tmp = config->bNumInterfaces * sizeof(struct libusb_interface);
354 interface = malloc(tmp);
355 config->interface = interface;
356 if (!config->interface)
357 return LIBUSB_ERROR_NO_MEM;
359 memset(interface, 0, tmp);
360 buffer += config->bLength;
361 size -= config->bLength;
363 config->extra = NULL;
364 config->extralen = 0;
366 for (i = 0; i < config->bNumInterfaces; i++) {
368 unsigned char *begin;
370 /* Skip over the rest of the Class Specific or Vendor */
371 /* Specific descriptors */
373 while (size >= DESC_HEADER_LENGTH) {
374 usbi_parse_descriptor(buffer, "bb", &header);
376 if ((header.bLength > size) ||
377 (header.bLength < DESC_HEADER_LENGTH)) {
378 usbi_err("invalid descriptor length of %d", header.bLength);
383 /* If we find another "proper" descriptor then we're done */
384 if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
385 (header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
386 (header.bDescriptorType == LIBUSB_DT_CONFIG) ||
387 (header.bDescriptorType == LIBUSB_DT_DEVICE))
390 usbi_dbg("skipping descriptor 0x%x\n", header.bDescriptorType);
391 buffer += header.bLength;
392 size -= header.bLength;
395 /* Copy any unknown descriptors into a storage area for */
396 /* drivers to later parse */
397 len = (int)(buffer - begin);
399 /* FIXME: We should realloc and append here */
400 if (!config->extralen) {
401 config->extra = malloc(len);
402 if (!config->extra) {
403 r = LIBUSB_ERROR_NO_MEM;
407 memcpy((unsigned char *) config->extra, begin, len);
408 config->extralen = len;
412 r = parse_interface(interface + i, buffer, size);
423 clear_configuration(config);
428 * Get the USB device descriptor for a given device.
429 * \param dev the device
430 * \returns the USB device descriptor
432 API_EXPORTED const struct libusb_device_descriptor *libusb_get_device_descriptor(
439 * Get the USB configuration descriptor for a given device.
440 * \param dev the device
441 * \returns the USB configuration descriptor
443 API_EXPORTED const struct libusb_config_descriptor *libusb_get_config_descriptor(
450 * Retrieve a string descriptor in C style ASCII.
452 * Wrapper around libusb_get_string_descriptor(). Uses the first language
453 * supported by the device.
455 * \param dev a device handle
456 * \param desc_index the index of the descriptor to retrieve
457 * \param data output buffer for ASCII string descriptor
458 * \param length size of data buffer
459 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
461 API_EXPORTED int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
462 uint8_t desc_index, unsigned char *data, int length)
464 unsigned char tbuf[255]; /* Some devices choke on size > 255 */
465 int r, langid, si, di;
467 /* Asking for the zero'th index is special - it returns a string
468 * descriptor that contains all the language IDs supported by the device.
469 * Typically there aren't many - often only one. The language IDs are 16
470 * bit numbers, and they start at the third byte in the descriptor. See
471 * USB 2.0 specification section 9.6.7 for more information. */
472 r = libusb_get_string_descriptor(dev, 0, 0, tbuf, sizeof(tbuf));
477 return LIBUSB_ERROR_IO;
479 langid = tbuf[2] | (tbuf[3] << 8);
481 r = libusb_get_string_descriptor(dev, desc_index, langid, tbuf,
486 if (tbuf[1] != LIBUSB_DT_STRING)
487 return LIBUSB_ERROR_IO;
490 return LIBUSB_ERROR_IO;
492 for (di = 0, si = 2; si < tbuf[0]; si += 2) {
493 if (di >= (length - 1))
496 if (tbuf[si + 1]) /* high byte */
499 data[di++] = tbuf[si];