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
/* set host_endian if the w values are already in host endian format,
40
* as opposed to bus endian. */
41
int usbi_parse_descriptor(unsigned char *source, char *descriptor, void *dest,
44
unsigned char *sp = source, *dp = dest;
48
for (cp = descriptor; *cp; cp++) {
50
case 'b': /* 8-bit byte */
53
case 'w': /* 16-bit word, convert from little endian to CPU */
54
dp += ((uintptr_t)dp & 1); /* Align to word boundary */
59
w = (sp[1] << 8) | sp[0];
60
*((uint16_t *)dp) = w;
68
return (int)(sp - source);
71
static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
74
free((unsigned char *) endpoint->extra);
77
static int parse_endpoint(struct libusb_context *ctx,
78
struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
79
int size, int host_endian)
81
struct usb_descriptor_header header;
87
usbi_parse_descriptor(buffer, "bb", &header, 0);
89
/* Everything should be fine being passed into here, but we sanity */
91
if (header.bLength > size) {
92
usbi_err(ctx, "ran out of descriptors parsing");
96
if (header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
97
usbi_err(ctx, "unexpected descriptor %x (expected %x)",
98
header.bDescriptorType, LIBUSB_DT_ENDPOINT);
102
if (header.bLength >= ENDPOINT_AUDIO_DESC_LENGTH)
103
usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
104
else if (header.bLength >= ENDPOINT_DESC_LENGTH)
105
usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
107
buffer += header.bLength;
108
size -= header.bLength;
109
parsed += header.bLength;
111
/* Skip over the rest of the Class Specific or Vendor Specific */
114
while (size >= DESC_HEADER_LENGTH) {
115
usbi_parse_descriptor(buffer, "bb", &header, 0);
117
if (header.bLength < 2) {
118
usbi_err(ctx, "invalid descriptor length %d", header.bLength);
122
/* If we find another "proper" descriptor then we're done */
123
if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
124
(header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
125
(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
126
(header.bDescriptorType == LIBUSB_DT_DEVICE))
129
usbi_dbg("skipping descriptor %x", header.bDescriptorType);
130
buffer += header.bLength;
131
size -= header.bLength;
132
parsed += header.bLength;
135
/* Copy any unknown descriptors into a storage area for drivers */
137
len = (int)(buffer - begin);
139
endpoint->extra = NULL;
140
endpoint->extra_length = 0;
145
endpoint->extra = extra;
147
endpoint->extra_length = 0;
148
return LIBUSB_ERROR_NO_MEM;
151
memcpy(extra, begin, len);
152
endpoint->extra_length = len;
157
static void clear_interface(struct libusb_interface *usb_interface)
162
if (usb_interface->altsetting) {
163
for (i = 0; i < usb_interface->num_altsetting; i++) {
164
struct libusb_interface_descriptor *ifp =
165
(struct libusb_interface_descriptor *)
166
usb_interface->altsetting + i;
168
free((void *) ifp->extra);
170
for (j = 0; j < ifp->bNumEndpoints; j++)
171
clear_endpoint((struct libusb_endpoint_descriptor *)
173
free((void *) ifp->endpoint);
176
free((void *) usb_interface->altsetting);
177
usb_interface->altsetting = NULL;
182
static int parse_interface(libusb_context *ctx,
183
struct libusb_interface *usb_interface, unsigned char *buffer, int size,
191
struct usb_descriptor_header header;
192
struct libusb_interface_descriptor *ifp;
193
unsigned char *begin;
195
usb_interface->num_altsetting = 0;
197
while (size >= INTERFACE_DESC_LENGTH) {
198
struct libusb_interface_descriptor *altsetting =
199
(struct libusb_interface_descriptor *) usb_interface->altsetting;
200
altsetting = realloc(altsetting,
201
sizeof(struct libusb_interface_descriptor) *
202
(usb_interface->num_altsetting + 1));
204
r = LIBUSB_ERROR_NO_MEM;
207
usb_interface->altsetting = altsetting;
209
ifp = altsetting + usb_interface->num_altsetting;
210
usb_interface->num_altsetting++;
211
usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
213
ifp->extra_length = 0;
214
ifp->endpoint = NULL;
216
/* Skip over the interface */
217
buffer += ifp->bLength;
218
parsed += ifp->bLength;
219
size -= ifp->bLength;
223
/* Skip over any interface, class or vendor descriptors */
224
while (size >= DESC_HEADER_LENGTH) {
225
usbi_parse_descriptor(buffer, "bb", &header, 0);
226
if (header.bLength < 2) {
227
usbi_err(ctx, "invalid descriptor of length %d",
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);
255
ifp->extra_length = len;
258
/* Did we hit an unexpected descriptor? */
259
usbi_parse_descriptor(buffer, "bb", &header, 0);
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(ctx, "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, 0);
285
if (header.bLength > size) {
286
usbi_err(ctx, "ran out of descriptors parsing");
291
r = parse_endpoint(ctx, endpoint + i, buffer, size,
302
/* We check to see if it's an alternate to this one */
303
ifp = (struct libusb_interface_descriptor *) buffer;
304
if (size < LIBUSB_DT_INTERFACE_SIZE ||
305
ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
306
!ifp->bAlternateSetting)
312
clear_interface(usb_interface);
316
static void clear_configuration(struct libusb_config_descriptor *config)
318
if (config->interface) {
320
for (i = 0; i < config->bNumInterfaces; i++)
321
clear_interface((struct libusb_interface *)
322
config->interface + i);
323
free((void *) config->interface);
326
free((void *) config->extra);
329
static int parse_configuration(struct libusb_context *ctx,
330
struct libusb_config_descriptor *config, unsigned char *buffer,
337
struct usb_descriptor_header header;
338
struct libusb_interface *usb_interface;
340
usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
341
size = config->wTotalLength;
343
if (config->bNumInterfaces > USB_MAXINTERFACES) {
344
usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
345
return LIBUSB_ERROR_IO;
348
tmp = config->bNumInterfaces * sizeof(struct libusb_interface);
349
usb_interface = malloc(tmp);
350
config->interface = usb_interface;
351
if (!config->interface)
352
return LIBUSB_ERROR_NO_MEM;
354
memset(usb_interface, 0, tmp);
355
buffer += config->bLength;
356
size -= config->bLength;
358
config->extra = NULL;
359
config->extra_length = 0;
361
for (i = 0; i < config->bNumInterfaces; i++) {
363
unsigned char *begin;
365
/* Skip over the rest of the Class Specific or Vendor */
366
/* Specific descriptors */
368
while (size >= DESC_HEADER_LENGTH) {
369
usbi_parse_descriptor(buffer, "bb", &header, 0);
371
if ((header.bLength > size) ||
372
(header.bLength < DESC_HEADER_LENGTH)) {
373
usbi_err(ctx, "invalid descriptor length of %d",
379
/* If we find another "proper" descriptor then we're done */
380
if ((header.bDescriptorType == LIBUSB_DT_ENDPOINT) ||
381
(header.bDescriptorType == LIBUSB_DT_INTERFACE) ||
382
(header.bDescriptorType == LIBUSB_DT_CONFIG) ||
383
(header.bDescriptorType == LIBUSB_DT_DEVICE))
386
usbi_dbg("skipping descriptor 0x%x\n", header.bDescriptorType);
387
buffer += header.bLength;
388
size -= header.bLength;
391
/* Copy any unknown descriptors into a storage area for */
392
/* drivers to later parse */
393
len = (int)(buffer - begin);
395
/* FIXME: We should realloc and append here */
396
if (!config->extra_length) {
397
config->extra = malloc(len);
398
if (!config->extra) {
399
r = LIBUSB_ERROR_NO_MEM;
403
memcpy((unsigned char *) config->extra, begin, len);
404
config->extra_length = len;
408
r = parse_interface(ctx, usb_interface + i, buffer, size, host_endian);
419
clear_configuration(config);
424
* Get the USB device descriptor for a given device.
426
* This is a non-blocking function; the device descriptor is cached in memory.
428
* \param dev the device
429
* \param desc output location for the descriptor data
430
* \returns 0 on success or a LIBUSB_ERROR code on failure
432
API_EXPORTED int libusb_get_device_descriptor(libusb_device *dev,
433
struct libusb_device_descriptor *desc)
435
unsigned char raw_desc[DEVICE_DESC_LENGTH];
440
r = usbi_backend->get_device_descriptor(dev, raw_desc, &host_endian);
444
memcpy((unsigned char *) desc, raw_desc, sizeof(raw_desc));
446
desc->bcdUSB = libusb_le16_to_cpu(desc->bcdUSB);
447
desc->idVendor = libusb_le16_to_cpu(desc->idVendor);
448
desc->idProduct = libusb_le16_to_cpu(desc->idProduct);
449
desc->bcdDevice = libusb_le16_to_cpu(desc->bcdDevice);
455
* Get the USB configuration descriptor for the currently active configuration.
456
* This is a non-blocking function which does not involve any requests being
457
* sent to the device.
459
* \param dev a device
460
* \param config output location for the USB configuration descriptor. Only
461
* valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
463
* \returns 0 on success
464
* \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
465
* \returns another LIBUSB_ERROR code on error
466
* \see libusb_get_config_descriptor
468
API_EXPORTED int libusb_get_active_config_descriptor(libusb_device *dev,
469
struct libusb_config_descriptor **config)
471
struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
472
unsigned char tmp[8];
473
unsigned char *buf = NULL;
479
return LIBUSB_ERROR_NO_MEM;
481
r = usbi_backend->get_active_config_descriptor(dev, tmp, sizeof(tmp),
486
usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
487
buf = malloc(_config->wTotalLength);
489
r = LIBUSB_ERROR_NO_MEM;
493
r = usbi_backend->get_active_config_descriptor(dev, buf,
494
_config->wTotalLength, &host_endian);
498
r = parse_configuration(dev->ctx, _config, buf, host_endian);
500
usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
503
usbi_warn(dev->ctx, "descriptor data still left");
518
* Get a USB configuration descriptor based on its index.
519
* This is a non-blocking function which does not involve any requests being
520
* sent to the device.
522
* \param dev a device
523
* \param config_index the index of the configuration you wish to retrieve
524
* \param config output location for the USB configuration descriptor. Only
525
* valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
527
* \returns 0 on success
528
* \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
529
* \returns another LIBUSB_ERROR code on error
530
* \see libusb_get_active_config_descriptor()
531
* \see libusb_get_config_descriptor_by_value()
533
API_EXPORTED int libusb_get_config_descriptor(libusb_device *dev,
534
uint8_t config_index, struct libusb_config_descriptor **config)
536
struct libusb_config_descriptor *_config;
537
unsigned char tmp[8];
538
unsigned char *buf = NULL;
542
usbi_dbg("index %d", config_index);
543
if (config_index >= dev->num_configurations)
544
return LIBUSB_ERROR_NOT_FOUND;
546
_config = malloc(sizeof(*_config));
548
return LIBUSB_ERROR_NO_MEM;
550
r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
551
sizeof(tmp), &host_endian);
555
usbi_parse_descriptor(tmp, "bbw", _config, host_endian);
556
buf = malloc(_config->wTotalLength);
558
r = LIBUSB_ERROR_NO_MEM;
563
r = usbi_backend->get_config_descriptor(dev, config_index, buf,
564
_config->wTotalLength, &host_endian);
568
r = parse_configuration(dev->ctx, _config, buf, host_endian);
570
usbi_err(dev->ctx, "parse_configuration failed with error %d", r);
573
usbi_warn(dev->ctx, "descriptor data still left");
587
/* iterate through all configurations, returning the index of the configuration
588
* matching a specific bConfigurationValue in the idx output parameter, or -1
589
* if the config was not found.
590
* returns 0 or a LIBUSB_ERROR code
592
int usbi_get_config_index_by_value(struct libusb_device *dev,
593
uint8_t bConfigurationValue, int *idx)
597
usbi_dbg("value %d", bConfigurationValue);
598
for (i = 0; i < dev->num_configurations; i++) {
599
unsigned char tmp[6];
601
int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
605
if (tmp[5] == bConfigurationValue) {
616
* Get a USB configuration descriptor with a specific bConfigurationValue.
617
* This is a non-blocking function which does not involve any requests being
618
* sent to the device.
620
* \param dev a device
621
* \param bConfigurationValue the bConfigurationValue of the configuration you
623
* \param config output location for the USB configuration descriptor. Only
624
* valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
626
* \returns 0 on success
627
* \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
628
* \returns another LIBUSB_ERROR code on error
629
* \see libusb_get_active_config_descriptor()
630
* \see libusb_get_config_descriptor()
632
API_EXPORTED int libusb_get_config_descriptor_by_value(libusb_device *dev,
633
uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
636
int r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
640
return LIBUSB_ERROR_NOT_FOUND;
642
return libusb_get_config_descriptor(dev, (uint8_t)idx, config);
646
* Free a configuration descriptor obtained from
647
* libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
648
* It is safe to call this function with a NULL config parameter, in which
649
* case the function simply returns.
651
* \param config the configuration descriptor to free
653
API_EXPORTED void libusb_free_config_descriptor(
654
struct libusb_config_descriptor *config)
659
clear_configuration(config);
664
* Retrieve a string descriptor in C style ASCII.
666
* Wrapper around libusb_get_string_descriptor(). Uses the first language
667
* supported by the device.
669
* \param dev a device handle
670
* \param desc_index the index of the descriptor to retrieve
671
* \param data output buffer for ASCII string descriptor
672
* \param length size of data buffer
673
* \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
675
API_EXPORTED int libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
676
uint8_t desc_index, unsigned char *data, int length)
678
unsigned char tbuf[255]; /* Some devices choke on size > 255 */
682
/* Asking for the zero'th index is special - it returns a string
683
* descriptor that contains all the language IDs supported by the device.
684
* Typically there aren't many - often only one. The language IDs are 16
685
* bit numbers, and they start at the third byte in the descriptor. See
686
* USB 2.0 specification section 9.6.7 for more information. */
687
r = libusb_get_string_descriptor(dev, 0, 0, tbuf, sizeof(tbuf));
692
return LIBUSB_ERROR_IO;
694
langid = tbuf[2] | (tbuf[3] << 8);
696
r = libusb_get_string_descriptor(dev, desc_index, langid, tbuf,
701
if (tbuf[1] != LIBUSB_DT_STRING)
702
return LIBUSB_ERROR_IO;
705
return LIBUSB_ERROR_IO;
707
for (di = 0, si = 2; si < tbuf[0]; si += 2) {
708
if (di >= (length - 1))
711
if (tbuf[si + 1]) /* high byte */
714
data[di++] = tbuf[si];