Debian Codegraph @

libusb/descriptor.c

raw
1 /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
2 /*
3 * USB descriptor handling functions for libusb
4 * Copyright © 2007 Daniel Drake <[email protected]>
5 * Copyright © 2001 Johannes Erdfelt <[email protected]>
6 *
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.
11 *
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.
16 *
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
20 */
21
22 #include "libusbi.h"
23
24 #include <string.h>
25
26 #define DESC_HEADER_LENGTH 2
27
28 /** @defgroup libusb_desc USB descriptors
29 * This page details how to examine the various standard USB descriptors
30 * for detected devices
31 */
32
33 static inline uint16_t ReadLittleEndian16(const uint8_t p[2])
34 {
35 return (uint16_t)((uint16_t)p[1] << 8 |
36 (uint16_t)p[0]);
37 }
38
39 static inline uint32_t ReadLittleEndian32(const uint8_t p[4])
40 {
41 return ((uint32_t)p[3] << 24 |
42 (uint32_t)p[2] << 16 |
43 (uint32_t)p[1] << 8 |
44 (uint32_t)p[0]);
45 }
46
47 static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
48 {
49 free((void *)endpoint->extra);
50 }
51
52 static int parse_endpoint(struct libusb_context *ctx,
53 struct libusb_endpoint_descriptor *endpoint, const uint8_t *buffer, int size)
54 {
55 const struct usbi_descriptor_header *header;
56 const uint8_t *begin;
57 void *extra;
58 int parsed = 0;
59
60 if (size < DESC_HEADER_LENGTH) {
61 usbi_err(ctx, "short endpoint descriptor read %d/%d",
62 size, DESC_HEADER_LENGTH);
63 return LIBUSB_ERROR_IO;
64 }
65
66 header = (const struct usbi_descriptor_header *)buffer;
67 if (header->bDescriptorType != LIBUSB_DT_ENDPOINT) {
68 usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
69 header->bDescriptorType, LIBUSB_DT_ENDPOINT);
70 return parsed;
71 } else if (header->bLength < LIBUSB_DT_ENDPOINT_SIZE) {
72 usbi_err(ctx, "invalid endpoint bLength (%u)", header->bLength);
73 return LIBUSB_ERROR_IO;
74 } else if (header->bLength > size) {
75 usbi_warn(ctx, "short endpoint descriptor read %d/%u",
76 size, header->bLength);
77 return parsed;
78 }
79
80 endpoint->bLength = buffer[0];
81 endpoint->bDescriptorType = buffer[1];
82 endpoint->bEndpointAddress = buffer[2];
83 endpoint->bmAttributes = buffer[3];
84 endpoint->wMaxPacketSize = ReadLittleEndian16(&buffer[4]);
85 endpoint->bInterval = buffer[6];
86 if (header->bLength >= LIBUSB_DT_ENDPOINT_AUDIO_SIZE) {
87 endpoint->bRefresh = buffer[7];
88 endpoint->bSynchAddress = buffer[8];
89 }
90
91 buffer += header->bLength;
92 size -= header->bLength;
93 parsed += header->bLength;
94
95 /* Skip over the rest of the Class Specific or Vendor Specific */
96 /* descriptors */
97 begin = buffer;
98 while (size >= DESC_HEADER_LENGTH) {
99 header = (const struct usbi_descriptor_header *)buffer;
100 if (header->bLength < DESC_HEADER_LENGTH) {
101 usbi_err(ctx, "invalid extra ep desc len (%u)",
102 header->bLength);
103 return LIBUSB_ERROR_IO;
104 } else if (header->bLength > size) {
105 usbi_warn(ctx, "short extra ep desc read %d/%u",
106 size, header->bLength);
107 return parsed;
108 }
109
110 /* If we find another "proper" descriptor then we're done */
111 if (header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
112 header->bDescriptorType == LIBUSB_DT_INTERFACE ||
113 header->bDescriptorType == LIBUSB_DT_CONFIG ||
114 header->bDescriptorType == LIBUSB_DT_DEVICE)
115 break;
116
117 usbi_dbg(ctx, "skipping descriptor 0x%x", header->bDescriptorType);
118 buffer += header->bLength;
119 size -= header->bLength;
120 parsed += header->bLength;
121 }
122
123 /* Copy any unknown descriptors into a storage area for drivers */
124 /* to later parse */
125 ptrdiff_t len = buffer - begin;
126 if (len <= 0)
127 return parsed;
128
129 extra = malloc((size_t)len);
130 if (!extra)
131 return LIBUSB_ERROR_NO_MEM;
132
133 memcpy(extra, begin, (size_t)len);
134 endpoint->extra = extra;
135 endpoint->extra_length = (int)len;
136
137 return parsed;
138 }
139
140 static void clear_interface(struct libusb_interface *usb_interface)
141 {
142 int i;
143
144 if (usb_interface->altsetting) {
145 for (i = 0; i < usb_interface->num_altsetting; i++) {
146 struct libusb_interface_descriptor *ifp =
147 (struct libusb_interface_descriptor *)
148 usb_interface->altsetting + i;
149
150 free((void *)ifp->extra);
151 ifp->extra = NULL;
152 ifp->extra_length = 0;
153 if (ifp->endpoint) {
154 uint8_t j;
155
156 for (j = 0; j < ifp->bNumEndpoints; j++)
157 clear_endpoint((struct libusb_endpoint_descriptor *)
158 ifp->endpoint + j);
159 }
160 free((void *)ifp->endpoint);
161 ifp->endpoint = NULL;
162 ifp->bNumEndpoints = 0;
163 }
164 }
165 free((void *)usb_interface->altsetting);
166 usb_interface->altsetting = NULL;
167 usb_interface->num_altsetting = 0;
168 }
169
170 static int parse_interface(libusb_context *ctx,
171 struct libusb_interface *usb_interface, const uint8_t *buffer, int size)
172 {
173 int r;
174 int parsed = 0;
175 int interface_number = -1;
176 const struct usbi_descriptor_header *header;
177 const struct usbi_interface_descriptor *if_desc;
178 struct libusb_interface_descriptor *ifp;
179 const uint8_t *begin;
180
181 while (size >= LIBUSB_DT_INTERFACE_SIZE) {
182 struct libusb_interface_descriptor *altsetting;
183
184 altsetting = realloc((void *)usb_interface->altsetting,
185 sizeof(*altsetting) * (size_t)(usb_interface->num_altsetting + 1));
186 if (!altsetting) {
187 r = LIBUSB_ERROR_NO_MEM;
188 goto err;
189 }
190 usb_interface->altsetting = altsetting;
191
192 ifp = altsetting + usb_interface->num_altsetting;
193 ifp->bLength = buffer[0];
194 ifp->bDescriptorType = buffer[1];
195 ifp->bInterfaceNumber = buffer[2];
196 ifp->bAlternateSetting = buffer[3];
197 ifp->bNumEndpoints = buffer[4];
198 ifp->bInterfaceClass = buffer[5];
199 ifp->bInterfaceSubClass = buffer[6];
200 ifp->bInterfaceProtocol = buffer[7];
201 ifp->iInterface = buffer[8];
202 if (ifp->bDescriptorType != LIBUSB_DT_INTERFACE) {
203 usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
204 ifp->bDescriptorType, LIBUSB_DT_INTERFACE);
205 return parsed;
206 } else if (ifp->bLength < LIBUSB_DT_INTERFACE_SIZE) {
207 usbi_err(ctx, "invalid interface bLength (%u)",
208 ifp->bLength);
209 r = LIBUSB_ERROR_IO;
210 goto err;
211 } else if (ifp->bLength > size) {
212 usbi_warn(ctx, "short intf descriptor read %d/%u",
213 size, ifp->bLength);
214 return parsed;
215 } else if (ifp->bNumEndpoints > USB_MAXENDPOINTS) {
216 usbi_err(ctx, "too many endpoints (%u)", ifp->bNumEndpoints);
217 r = LIBUSB_ERROR_IO;
218 goto err;
219 }
220
221 usb_interface->num_altsetting++;
222 ifp->extra = NULL;
223 ifp->extra_length = 0;
224 ifp->endpoint = NULL;
225
226 if (interface_number == -1)
227 interface_number = ifp->bInterfaceNumber;
228
229 /* Skip over the interface */
230 buffer += ifp->bLength;
231 parsed += ifp->bLength;
232 size -= ifp->bLength;
233
234 begin = buffer;
235
236 /* Skip over any interface, class or vendor descriptors */
237 while (size >= DESC_HEADER_LENGTH) {
238 header = (const struct usbi_descriptor_header *)buffer;
239 if (header->bLength < DESC_HEADER_LENGTH) {
240 usbi_err(ctx,
241 "invalid extra intf desc len (%u)",
242 header->bLength);
243 r = LIBUSB_ERROR_IO;
244 goto err;
245 } else if (header->bLength > size) {
246 usbi_warn(ctx,
247 "short extra intf desc read %d/%u",
248 size, header->bLength);
249 /* Keep the invariant: bNumEndpoints > 0 implies
250 * endpoint != NULL. The endpoint array isn't
251 * allocated yet on this early return. */
252 ifp->bNumEndpoints = 0;
253 return parsed;
254 }
255
256 /* If we find another "proper" descriptor then we're done */
257 if (header->bDescriptorType == LIBUSB_DT_INTERFACE ||
258 header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
259 header->bDescriptorType == LIBUSB_DT_CONFIG ||
260 header->bDescriptorType == LIBUSB_DT_DEVICE)
261 break;
262
263 buffer += header->bLength;
264 parsed += header->bLength;
265 size -= header->bLength;
266 }
267
268 /* Copy any unknown descriptors into a storage area for */
269 /* drivers to later parse */
270 ptrdiff_t len = buffer - begin;
271 if (len > 0) {
272 void *extra = malloc((size_t)len);
273
274 if (!extra) {
275 r = LIBUSB_ERROR_NO_MEM;
276 goto err;
277 }
278
279 memcpy(extra, begin, (size_t)len);
280 ifp->extra = extra;
281 ifp->extra_length = (int)len;
282 }
283
284 if (ifp->bNumEndpoints > 0) {
285 struct libusb_endpoint_descriptor *endpoint;
286 uint8_t i;
287
288 endpoint = calloc(ifp->bNumEndpoints, sizeof(*endpoint));
289 if (!endpoint) {
290 r = LIBUSB_ERROR_NO_MEM;
291 goto err;
292 }
293
294 ifp->endpoint = endpoint;
295 for (i = 0; i < ifp->bNumEndpoints; i++) {
296 r = parse_endpoint(ctx, endpoint + i, buffer, size);
297 if (r < 0)
298 goto err;
299 if (r == 0) {
300 ifp->bNumEndpoints = i;
301 break;
302 }
303
304 buffer += r;
305 parsed += r;
306 size -= r;
307 }
308 }
309
310 /* We check to see if it's an alternate to this one */
311 if_desc = (const struct usbi_interface_descriptor *)buffer;
312 if (size < LIBUSB_DT_INTERFACE_SIZE ||
313 if_desc->bDescriptorType != LIBUSB_DT_INTERFACE ||
314 if_desc->bInterfaceNumber != interface_number)
315 return parsed;
316 }
317
318 return parsed;
319 err:
320 clear_interface(usb_interface);
321 return r;
322 }
323
324 static void clear_configuration(struct libusb_config_descriptor *config)
325 {
326 uint8_t i;
327
328 if (config->interface) {
329 for (i = 0; i < config->bNumInterfaces; i++)
330 clear_interface((struct libusb_interface *)
331 config->interface + i);
332 }
333 free((void *)config->interface);
334 config->interface = NULL;
335 config->bNumInterfaces = 0;
336 free((void *)config->extra);
337 config->extra = NULL;
338 config->extra_length = 0;
339 }
340
341 static int parse_configuration(struct libusb_context *ctx,
342 struct libusb_config_descriptor *config, const uint8_t *buffer, int size)
343 {
344 uint8_t i;
345 int r;
346 const struct usbi_descriptor_header *header;
347 struct libusb_interface *usb_interface;
348
349 if (size < LIBUSB_DT_CONFIG_SIZE) {
350 usbi_err(ctx, "short config descriptor read %d/%d",
351 size, LIBUSB_DT_CONFIG_SIZE);
352 return LIBUSB_ERROR_IO;
353 }
354
355 config->bLength = buffer[0];
356 config->bDescriptorType = buffer[1];
357 config->wTotalLength = ReadLittleEndian16(&buffer[2]);
358 config->bNumInterfaces = buffer[4];
359 config->bConfigurationValue = buffer[5];
360 config->iConfiguration = buffer[6];
361 config->bmAttributes = buffer[7];
362 config->MaxPower = buffer[8];
363 if (config->bDescriptorType != LIBUSB_DT_CONFIG) {
364 usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
365 config->bDescriptorType, LIBUSB_DT_CONFIG);
366 return LIBUSB_ERROR_IO;
367 } else if (config->bLength < LIBUSB_DT_CONFIG_SIZE) {
368 usbi_err(ctx, "invalid config bLength (%u)", config->bLength);
369 return LIBUSB_ERROR_IO;
370 } else if (config->bLength > size) {
371 usbi_err(ctx, "short config descriptor read %d/%u",
372 size, config->bLength);
373 return LIBUSB_ERROR_IO;
374 } else if (config->bNumInterfaces > USB_MAXINTERFACES) {
375 usbi_err(ctx, "too many interfaces (%u)", config->bNumInterfaces);
376 return LIBUSB_ERROR_IO;
377 }
378
379 usb_interface = calloc(config->bNumInterfaces, sizeof(*usb_interface));
380 if (!usb_interface)
381 return LIBUSB_ERROR_NO_MEM;
382
383 config->interface = usb_interface;
384
385 buffer += config->bLength;
386 size -= config->bLength;
387
388 for (i = 0; i < config->bNumInterfaces; i++) {
389 const uint8_t *begin;
390
391 /* Skip over the rest of the Class Specific or Vendor */
392 /* Specific descriptors */
393 begin = buffer;
394 while (size >= DESC_HEADER_LENGTH) {
395 header = (const struct usbi_descriptor_header *)buffer;
396 if (header->bLength < DESC_HEADER_LENGTH) {
397 usbi_err(ctx,
398 "invalid extra config desc len (%u)",
399 header->bLength);
400 r = LIBUSB_ERROR_IO;
401 goto err;
402 } else if (header->bLength > size) {
403 usbi_warn(ctx,
404 "short extra config desc read %d/%u",
405 size, header->bLength);
406 config->bNumInterfaces = i;
407 return size;
408 }
409
410 /* If we find another "proper" descriptor then we're done */
411 if (header->bDescriptorType == LIBUSB_DT_ENDPOINT ||
412 header->bDescriptorType == LIBUSB_DT_INTERFACE ||
413 header->bDescriptorType == LIBUSB_DT_CONFIG ||
414 header->bDescriptorType == LIBUSB_DT_DEVICE)
415 break;
416
417 usbi_dbg(ctx, "skipping descriptor 0x%x", header->bDescriptorType);
418 buffer += header->bLength;
419 size -= header->bLength;
420 }
421
422 /* Copy any unknown descriptors into a storage area for */
423 /* drivers to later parse */
424 ptrdiff_t len = buffer - begin;
425 if (len > 0) {
426 uint8_t *extra = realloc((void *)config->extra,
427 (size_t)(config->extra_length) + (size_t)len);
428
429 if (!extra) {
430 r = LIBUSB_ERROR_NO_MEM;
431 goto err;
432 }
433
434 memcpy(extra + config->extra_length, begin, (size_t)len);
435 config->extra = extra;
436 config->extra_length += (int)len;
437 }
438
439 r = parse_interface(ctx, usb_interface + i, buffer, size);
440 if (r < 0)
441 goto err;
442 if (r == 0) {
443 config->bNumInterfaces = i;
444 break;
445 }
446
447 buffer += r;
448 size -= r;
449 }
450
451 return size;
452
453 err:
454 clear_configuration(config);
455 return r;
456 }
457
458 static int raw_desc_to_config(struct libusb_context *ctx,
459 const uint8_t *buf, int size, struct libusb_config_descriptor **config)
460 {
461 struct libusb_config_descriptor *_config = calloc(1, sizeof(*_config));
462 int r;
463
464 if (!_config)
465 return LIBUSB_ERROR_NO_MEM;
466
467 r = parse_configuration(ctx, _config, buf, size);
468 if (r < 0) {
469 usbi_err(ctx, "parse_configuration failed with error %d", r);
470 free(_config);
471 return r;
472 } else if (r > 0) {
473 usbi_warn(ctx, "still %d bytes of descriptor data left", r);
474 }
475
476 *config = _config;
477 return LIBUSB_SUCCESS;
478 }
479
480 static int get_active_config_descriptor(struct libusb_device *dev,
481 uint8_t *buffer, size_t size)
482 {
483 int r = usbi_backend.get_active_config_descriptor(dev, buffer, size);
484
485 if (r < 0)
486 return r;
487
488 if (r < LIBUSB_DT_CONFIG_SIZE) {
489 usbi_err(DEVICE_CTX(dev), "short config descriptor read %d/%d",
490 r, LIBUSB_DT_CONFIG_SIZE);
491 return LIBUSB_ERROR_IO;
492 } else if (r != (int)size) {
493 usbi_warn(DEVICE_CTX(dev), "short config descriptor read %d/%d",
494 r, (int)size);
495 }
496
497 return r;
498 }
499
500 static int get_config_descriptor(struct libusb_device *dev, uint8_t config_idx,
501 uint8_t *buffer, size_t size)
502 {
503 int r = usbi_backend.get_config_descriptor(dev, config_idx, buffer, size);
504
505 if (r < 0)
506 return r;
507 if (r < LIBUSB_DT_CONFIG_SIZE) {
508 usbi_err(DEVICE_CTX(dev), "short config descriptor read %d/%d",
509 r, LIBUSB_DT_CONFIG_SIZE);
510 return LIBUSB_ERROR_IO;
511 } else if (r != (int)size) {
512 usbi_warn(DEVICE_CTX(dev), "short config descriptor read %d/%d",
513 r, (int)size);
514 }
515
516 return r;
517 }
518
519 /** \ingroup libusb_desc
520 * Get the USB device descriptor for a given device.
521 *
522 * This is a non-blocking function; the device descriptor is cached in memory.
523 *
524 * Note since libusb-1.0.16, \ref LIBUSBX_API_VERSION >= 0x01000102, this
525 * function always succeeds.
526 *
527 * \param dev the device
528 * \param desc output location for the descriptor data
529 * \returns 0 on success or a LIBUSB_ERROR code on failure
530 */
531 int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
532 struct libusb_device_descriptor *desc)
533 {
534 usbi_dbg(DEVICE_CTX(dev), " ");
535 static_assert(sizeof(dev->device_descriptor) == LIBUSB_DT_DEVICE_SIZE,
536 "struct libusb_device_descriptor is not expected size");
537 *desc = dev->device_descriptor;
538 return 0;
539 }
540
541 /** \ingroup libusb_desc
542 * Get the USB configuration descriptor for the currently active configuration.
543 * This is a non-blocking function which does not involve any requests being
544 * sent to the device.
545 *
546 * \param dev a device
547 * \param config output location for the USB configuration descriptor. Only
548 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
549 * after use.
550 * \returns 0 on success
551 * \returns \ref LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
552 * \returns another LIBUSB_ERROR code on error
553 * \see libusb_get_config_descriptor
554 */
555 int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
556 struct libusb_config_descriptor **config)
557 {
558 union usbi_config_desc_buf _config;
559 uint16_t config_len;
560 uint8_t *buf;
561 int r;
562
563 r = get_active_config_descriptor(dev, _config.buf, sizeof(_config.buf));
564 if (r < 0)
565 return r;
566
567 config_len = libusb_le16_to_cpu(_config.desc.wTotalLength);
568 buf = malloc(config_len);
569 if (!buf)
570 return LIBUSB_ERROR_NO_MEM;
571
572 r = get_active_config_descriptor(dev, buf, config_len);
573 if (r >= 0)
574 r = raw_desc_to_config(DEVICE_CTX(dev), buf, r, config);
575
576 free(buf);
577 return r;
578 }
579
580 /** \ingroup libusb_desc
581 * Get a USB configuration descriptor based on its index.
582 * This is a non-blocking function which does not involve any requests being
583 * sent to the device.
584 *
585 * \param dev a device
586 * \param config_index the index of the configuration you wish to retrieve
587 * \param config output location for the USB configuration descriptor. Only
588 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
589 * after use.
590 * \returns 0 on success
591 * \returns \ref LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
592 * \returns another LIBUSB_ERROR code on error
593 * \see libusb_get_active_config_descriptor()
594 * \see libusb_get_config_descriptor_by_value()
595 */
596 int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
597 uint8_t config_index, struct libusb_config_descriptor **config)
598 {
599 union usbi_config_desc_buf _config;
600 uint16_t config_len;
601 uint8_t *buf;
602 int r;
603
604 usbi_dbg(DEVICE_CTX(dev), "index %u", config_index);
605 if (config_index >= dev->device_descriptor.bNumConfigurations)
606 return LIBUSB_ERROR_NOT_FOUND;
607
608 r = get_config_descriptor(dev, config_index, _config.buf, sizeof(_config.buf));
609 if (r < 0)
610 return r;
611
612 config_len = libusb_le16_to_cpu(_config.desc.wTotalLength);
613 buf = malloc(config_len);
614 if (!buf)
615 return LIBUSB_ERROR_NO_MEM;
616
617 r = get_config_descriptor(dev, config_index, buf, config_len);
618 if (r >= 0)
619 r = raw_desc_to_config(DEVICE_CTX(dev), buf, r, config);
620
621 free(buf);
622 return r;
623 }
624
625 /** \ingroup libusb_desc
626 * Get a USB configuration descriptor with a specific bConfigurationValue.
627 * This is a non-blocking function which does not involve any requests being
628 * sent to the device.
629 *
630 * \param dev a device
631 * \param bConfigurationValue the bConfigurationValue of the configuration you
632 * wish to retrieve
633 * \param config output location for the USB configuration descriptor. Only
634 * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
635 * after use.
636 * \returns 0 on success
637 * \returns \ref LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
638 * \returns another LIBUSB_ERROR code on error
639 * \see libusb_get_active_config_descriptor()
640 * \see libusb_get_config_descriptor()
641 */
642 int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
643 uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
644 {
645 uint8_t idx;
646 int r;
647
648 if (usbi_backend.get_config_descriptor_by_value) {
649 void *buf;
650
651 r = usbi_backend.get_config_descriptor_by_value(dev,
652 bConfigurationValue, &buf);
653 if (r < 0)
654 return r;
655
656 return raw_desc_to_config(DEVICE_CTX(dev), buf, r, config);
657 }
658
659 usbi_dbg(DEVICE_CTX(dev), "value %u", bConfigurationValue);
660 for (idx = 0; idx < dev->device_descriptor.bNumConfigurations; idx++) {
661 union usbi_config_desc_buf _config;
662
663 r = get_config_descriptor(dev, idx, _config.buf, sizeof(_config.buf));
664 if (r < 0)
665 return r;
666
667 if (_config.desc.bConfigurationValue == bConfigurationValue)
668 return libusb_get_config_descriptor(dev, idx, config);
669 }
670
671 return LIBUSB_ERROR_NOT_FOUND;
672 }
673
674 /** \ingroup libusb_desc
675 * Free a configuration descriptor obtained from
676 * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
677 * It is safe to call this function with a NULL config parameter, in which
678 * case the function simply returns.
679 *
680 * \param config the configuration descriptor to free
681 */
682 void API_EXPORTED libusb_free_config_descriptor(
683 struct libusb_config_descriptor *config)
684 {
685 if (!config)
686 return;
687
688 clear_configuration(config);
689 free(config);
690 }
691
692 /** \ingroup libusb_desc
693 * Get an endpoints superspeed endpoint companion descriptor (if any)
694 *
695 * \param ctx the context to operate on, or NULL for the default context
696 * \param endpoint endpoint descriptor from which to get the superspeed
697 * endpoint companion descriptor
698 * \param ep_comp output location for the superspeed endpoint companion
699 * descriptor. Only valid if 0 was returned. Must be freed with
700 * libusb_free_ss_endpoint_companion_descriptor() after use.
701 * \returns 0 on success
702 * \returns \ref LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
703 * \returns another LIBUSB_ERROR code on error
704 */
705 int API_EXPORTED libusb_get_ss_endpoint_companion_descriptor(
706 libusb_context *ctx,
707 const struct libusb_endpoint_descriptor *endpoint,
708 struct libusb_ss_endpoint_companion_descriptor **ep_comp)
709 {
710 const struct usbi_descriptor_header *header;
711 const uint8_t *buffer = endpoint->extra;
712 int size = endpoint->extra_length;
713
714 *ep_comp = NULL;
715
716 while (size >= DESC_HEADER_LENGTH) {
717 header = (const struct usbi_descriptor_header *)buffer;
718 if (header->bDescriptorType != LIBUSB_DT_SS_ENDPOINT_COMPANION) {
719 if (header->bLength < DESC_HEADER_LENGTH) {
720 usbi_err(ctx, "invalid descriptor length %u",
721 header->bLength);
722 return LIBUSB_ERROR_IO;
723 }
724 buffer += header->bLength;
725 size -= header->bLength;
726 continue;
727 } else if (header->bLength < LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE) {
728 usbi_err(ctx, "invalid ss-ep-comp-desc length %u",
729 header->bLength);
730 return LIBUSB_ERROR_IO;
731 } else if (header->bLength > size) {
732 usbi_err(ctx, "short ss-ep-comp-desc read %d/%u",
733 size, header->bLength);
734 return LIBUSB_ERROR_IO;
735 }
736
737 *ep_comp = malloc(sizeof(**ep_comp));
738 if (!*ep_comp)
739 return LIBUSB_ERROR_NO_MEM;
740 (*ep_comp)->bLength = buffer[0];
741 (*ep_comp)->bDescriptorType = buffer[1];
742 (*ep_comp)->bMaxBurst = buffer[2];
743 (*ep_comp)->bmAttributes = buffer[3];
744 (*ep_comp)->wBytesPerInterval = ReadLittleEndian16(&buffer[4]);
745 return LIBUSB_SUCCESS;
746 }
747 return LIBUSB_ERROR_NOT_FOUND;
748 }
749
750 /** \ingroup libusb_desc
751 * Free a superspeed endpoint companion descriptor obtained from
752 * libusb_get_ss_endpoint_companion_descriptor().
753 * It is safe to call this function with a NULL ep_comp parameter, in which
754 * case the function simply returns.
755 *
756 * \param ep_comp the superspeed endpoint companion descriptor to free
757 */
758 void API_EXPORTED libusb_free_ss_endpoint_companion_descriptor(
759 struct libusb_ss_endpoint_companion_descriptor *ep_comp)
760 {
761 free(ep_comp);
762 }
763
764 static int parse_bos(struct libusb_context *ctx,
765 struct libusb_bos_descriptor **bos,
766 const uint8_t *buffer, int size)
767 {
768 struct libusb_bos_descriptor *_bos;
769 const struct usbi_bos_descriptor *bos_desc;
770 const struct usbi_descriptor_header *header;
771 uint8_t i;
772
773 if (size < LIBUSB_DT_BOS_SIZE) {
774 usbi_err(ctx, "short bos descriptor read %d/%d",
775 size, LIBUSB_DT_BOS_SIZE);
776 return LIBUSB_ERROR_IO;
777 }
778
779 bos_desc = (const struct usbi_bos_descriptor *)buffer;
780 if (bos_desc->bDescriptorType != LIBUSB_DT_BOS) {
781 usbi_err(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
782 bos_desc->bDescriptorType, LIBUSB_DT_BOS);
783 return LIBUSB_ERROR_IO;
784 } else if (bos_desc->bLength < LIBUSB_DT_BOS_SIZE) {
785 usbi_err(ctx, "invalid bos bLength (%u)", bos_desc->bLength);
786 return LIBUSB_ERROR_IO;
787 } else if (bos_desc->bLength > size) {
788 usbi_err(ctx, "short bos descriptor read %d/%u",
789 size, bos_desc->bLength);
790 return LIBUSB_ERROR_IO;
791 }
792
793 _bos = calloc(1, sizeof(*_bos) + (bos_desc->bNumDeviceCaps * sizeof(void *)));
794 if (!_bos)
795 return LIBUSB_ERROR_NO_MEM;
796
797 _bos->bLength = buffer[0];
798 _bos->bDescriptorType = buffer[1];
799 _bos->wTotalLength = ReadLittleEndian16(&buffer[2]);
800 _bos->bNumDeviceCaps = buffer[4];
801 buffer += _bos->bLength;
802 size -= _bos->bLength;
803
804 /* Get the device capability descriptors */
805 for (i = 0; i < _bos->bNumDeviceCaps; i++) {
806 if (size < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
807 usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
808 size, LIBUSB_DT_DEVICE_CAPABILITY_SIZE);
809 break;
810 }
811 header = (const struct usbi_descriptor_header *)buffer;
812 if (header->bDescriptorType != LIBUSB_DT_DEVICE_CAPABILITY) {
813 usbi_warn(ctx, "unexpected descriptor 0x%x (expected 0x%x)",
814 header->bDescriptorType, LIBUSB_DT_DEVICE_CAPABILITY);
815 break;
816 } else if (header->bLength < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
817 usbi_err(ctx, "invalid dev-cap bLength (%u)",
818 header->bLength);
819 libusb_free_bos_descriptor(_bos);
820 return LIBUSB_ERROR_IO;
821 } else if (header->bLength > size) {
822 usbi_warn(ctx, "short dev-cap descriptor read %d/%u",
823 size, header->bLength);
824 break;
825 }
826
827 _bos->dev_capability[i] = malloc(header->bLength);
828 if (!_bos->dev_capability[i]) {
829 libusb_free_bos_descriptor(_bos);
830 return LIBUSB_ERROR_NO_MEM;
831 }
832 memcpy(_bos->dev_capability[i], buffer, header->bLength);
833 buffer += header->bLength;
834 size -= header->bLength;
835 }
836 _bos->bNumDeviceCaps = i;
837 *bos = _bos;
838
839 return LIBUSB_SUCCESS;
840 }
841
842 /** \ingroup libusb_desc
843 * Get a Binary Object Store (BOS) descriptor
844 * This is a BLOCKING function, which will send requests to the device.
845 *
846 * \param dev_handle the handle of an open libusb device
847 * \param bos output location for the BOS descriptor. Only valid if 0 was returned.
848 * Must be freed with \ref libusb_free_bos_descriptor() after use.
849 * \returns 0 on success
850 * \returns \ref LIBUSB_ERROR_NOT_FOUND if the device doesn't have a BOS descriptor
851 * \returns another LIBUSB_ERROR code on error
852 */
853 int API_EXPORTED libusb_get_bos_descriptor(libusb_device_handle *dev_handle,
854 struct libusb_bos_descriptor **bos)
855 {
856 union usbi_bos_desc_buf _bos;
857 uint16_t bos_len;
858 uint8_t *bos_data;
859 int r;
860 struct libusb_context *ctx = HANDLE_CTX(dev_handle);
861
862 /* Read the BOS. This generates 2 requests on the bus,
863 * one for the header, and one for the full BOS */
864 r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, _bos.buf, sizeof(_bos.buf));
865 if (r < 0) {
866 if (r != LIBUSB_ERROR_PIPE)
867 usbi_err(ctx, "failed to read BOS (%d)", r);
868 return r;
869 }
870 if (r < LIBUSB_DT_BOS_SIZE) {
871 usbi_err(ctx, "short BOS read %d/%d",
872 r, LIBUSB_DT_BOS_SIZE);
873 return LIBUSB_ERROR_IO;
874 }
875
876 bos_len = libusb_le16_to_cpu(_bos.desc.wTotalLength);
877 usbi_dbg(ctx, "found BOS descriptor: size %u bytes, %u capabilities",
878 bos_len, _bos.desc.bNumDeviceCaps);
879 bos_data = calloc(1, bos_len);
880 if (!bos_data)
881 return LIBUSB_ERROR_NO_MEM;
882
883 r = libusb_get_descriptor(dev_handle, LIBUSB_DT_BOS, 0, bos_data, bos_len);
884 if (r >= 0) {
885 if (r != (int)bos_len)
886 usbi_warn(ctx, "short BOS read %d/%u", r, bos_len);
887 r = parse_bos(HANDLE_CTX(dev_handle), bos, bos_data, r);
888 } else {
889 usbi_err(ctx, "failed to read BOS (%d)", r);
890 }
891
892 free(bos_data);
893 return r;
894 }
895
896 /** \ingroup libusb_desc
897 * Free a BOS descriptor obtained from libusb_get_bos_descriptor().
898 * It is safe to call this function with a NULL bos parameter, in which
899 * case the function simply returns.
900 *
901 * \param bos the BOS descriptor to free
902 */
903 void API_EXPORTED libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos)
904 {
905 uint8_t i;
906
907 if (!bos)
908 return;
909
910 for (i = 0; i < bos->bNumDeviceCaps; i++)
911 free(bos->dev_capability[i]);
912 free(bos);
913 }
914
915 /** \ingroup libusb_desc
916 * Get an USB 2.0 Extension descriptor
917 *
918 * \param ctx the context to operate on, or NULL for the default context
919 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
920 * \ref libusb_bos_type::LIBUSB_BT_USB_2_0_EXTENSION
921 * LIBUSB_BT_USB_2_0_EXTENSION
922 * \param usb_2_0_extension output location for the USB 2.0 Extension
923 * descriptor. Only valid if 0 was returned. Must be freed with
924 * libusb_free_usb_2_0_extension_descriptor() after use.
925 * \returns 0 on success
926 * \returns a LIBUSB_ERROR code on error
927 */
928 int API_EXPORTED libusb_get_usb_2_0_extension_descriptor(
929 libusb_context *ctx,
930 struct libusb_bos_dev_capability_descriptor *dev_cap,
931 struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension)
932 {
933 struct libusb_usb_2_0_extension_descriptor *_usb_2_0_extension;
934
935 if (dev_cap->bDevCapabilityType != LIBUSB_BT_USB_2_0_EXTENSION) {
936 usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
937 dev_cap->bDevCapabilityType,
938 LIBUSB_BT_USB_2_0_EXTENSION);
939 return LIBUSB_ERROR_INVALID_PARAM;
940 } else if (dev_cap->bLength < LIBUSB_BT_USB_2_0_EXTENSION_SIZE) {
941 usbi_err(ctx, "short dev-cap descriptor read %u/%d",
942 dev_cap->bLength, LIBUSB_BT_USB_2_0_EXTENSION_SIZE);
943 return LIBUSB_ERROR_IO;
944 }
945
946 _usb_2_0_extension = malloc(sizeof(*_usb_2_0_extension));
947 if (!_usb_2_0_extension)
948 return LIBUSB_ERROR_NO_MEM;
949
950 _usb_2_0_extension->bLength = dev_cap->bLength;
951 _usb_2_0_extension->bDescriptorType = dev_cap->bDescriptorType;
952 _usb_2_0_extension->bDevCapabilityType = dev_cap->bDevCapabilityType;
953 _usb_2_0_extension->bmAttributes = ReadLittleEndian32(dev_cap->dev_capability_data);
954
955 *usb_2_0_extension = _usb_2_0_extension;
956 return LIBUSB_SUCCESS;
957 }
958
959 /** \ingroup libusb_desc
960 * Free a USB 2.0 Extension descriptor obtained from
961 * libusb_get_usb_2_0_extension_descriptor().
962 * It is safe to call this function with a NULL usb_2_0_extension parameter,
963 * in which case the function simply returns.
964 *
965 * \param usb_2_0_extension the USB 2.0 Extension descriptor to free
966 */
967 void API_EXPORTED libusb_free_usb_2_0_extension_descriptor(
968 struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension)
969 {
970 free(usb_2_0_extension);
971 }
972
973 /** \ingroup libusb_desc
974 * Get a SuperSpeed USB Device Capability descriptor
975 *
976 * \param ctx the context to operate on, or NULL for the default context
977 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
978 * \ref libusb_bos_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
979 * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
980 * \param ss_usb_device_cap output location for the SuperSpeed USB Device
981 * Capability descriptor. Only valid if 0 was returned. Must be freed with
982 * libusb_free_ss_usb_device_capability_descriptor() after use.
983 * \returns 0 on success
984 * \returns a LIBUSB_ERROR code on error
985 */
986 int API_EXPORTED libusb_get_ss_usb_device_capability_descriptor(
987 libusb_context *ctx,
988 struct libusb_bos_dev_capability_descriptor *dev_cap,
989 struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap)
990 {
991 struct libusb_ss_usb_device_capability_descriptor *_ss_usb_device_cap;
992
993 if (dev_cap->bDevCapabilityType != LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
994 usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
995 dev_cap->bDevCapabilityType,
996 LIBUSB_BT_SS_USB_DEVICE_CAPABILITY);
997 return LIBUSB_ERROR_INVALID_PARAM;
998 } else if (dev_cap->bLength < LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) {
999 usbi_err(ctx, "short dev-cap descriptor read %u/%d",
1000 dev_cap->bLength, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE);
1001 return LIBUSB_ERROR_IO;
1002 }
1003
1004 _ss_usb_device_cap = malloc(sizeof(*_ss_usb_device_cap));
1005 if (!_ss_usb_device_cap)
1006 return LIBUSB_ERROR_NO_MEM;
1007
1008 _ss_usb_device_cap->bLength = dev_cap->bLength;
1009 _ss_usb_device_cap->bDescriptorType = dev_cap->bDescriptorType;
1010 _ss_usb_device_cap->bDevCapabilityType = dev_cap->bDevCapabilityType;
1011 _ss_usb_device_cap->bmAttributes = dev_cap->dev_capability_data[0];
1012 _ss_usb_device_cap->wSpeedSupported = ReadLittleEndian16(&dev_cap->dev_capability_data[1]);
1013 _ss_usb_device_cap->bFunctionalitySupport = dev_cap->dev_capability_data[3];
1014 _ss_usb_device_cap->bU1DevExitLat = dev_cap->dev_capability_data[4];
1015 _ss_usb_device_cap->bU2DevExitLat = ReadLittleEndian16(&dev_cap->dev_capability_data[5]);
1016
1017 *ss_usb_device_cap = _ss_usb_device_cap;
1018 return LIBUSB_SUCCESS;
1019 }
1020
1021 /// @cond DEV
1022 /** \internal \ingroup libusb_desc
1023 * We use this private struct only to parse a SuperSpeedPlus device capability
1024 * descriptor according to section 9.6.2.5 of the USB 3.1 specification.
1025 * We don't expose it.
1026 */
1027 struct internal_ssplus_capability_descriptor {
1028 /** The length of the descriptor. Must be equal to LIBUSB_BT_SSPLUS_USB_DEVICE_CAPABILITY_SIZE */
1029 uint8_t bLength;
1030
1031 /** The type of the descriptor */
1032 uint8_t bDescriptorType;
1033
1034 /** Must be equal to LIBUSB_BT_SUPERSPEED_PLUS_CAPABILITY */
1035 uint8_t bDevCapabilityType;
1036
1037 /** Unused */
1038 uint8_t bReserved;
1039
1040 /** Contains the number of SublinkSpeedIDs */
1041 uint32_t bmAttributes;
1042
1043 /** Contains the ssid, minRxLaneCount, and minTxLaneCount */
1044 uint16_t wFunctionalitySupport;
1045
1046 /** Unused */
1047 uint16_t wReserved;
1048 };
1049 /// @endcond
1050
1051 /** \ingroup libusb_desc
1052 * Get a SuperSpeedPlus USB Device Capability descriptor
1053 *
1054 * Since version 1.0.28, \ref LIBUSB_API_VERSION >= 0x0100010B
1055 *
1056 * \param ctx the context to operate on, or NULL for the default context
1057 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1058 * \ref libusb_bos_type::LIBUSB_BT_SUPERSPEED_PLUS_CAPABILITY
1059 * LIBUSB_BT_SUPERSPEED_PLUS_CAPABILITY
1060 * \param ssplus_usb_device_cap output location for the SuperSpeedPlus USB Device
1061 * Capability descriptor. Only valid if 0 was returned. Must be freed with
1062 * libusb_free_ssplus_usb_device_capability_descriptor() after use.
1063 * \returns 0 on success
1064 * \returns a LIBUSB_ERROR code on error
1065 */
1066 int API_EXPORTED libusb_get_ssplus_usb_device_capability_descriptor(
1067 libusb_context *ctx,
1068 struct libusb_bos_dev_capability_descriptor *dev_cap,
1069 struct libusb_ssplus_usb_device_capability_descriptor **ssplus_usb_device_cap)
1070 {
1071 struct libusb_ssplus_usb_device_capability_descriptor *_ssplus_cap;
1072
1073 /* Use a private struct to reuse our descriptor parsing system. */
1074 struct internal_ssplus_capability_descriptor parsedDescriptor;
1075
1076 /* Some size/type checks to make sure everything is in order */
1077 if (dev_cap->bDevCapabilityType != LIBUSB_BT_SUPERSPEED_PLUS_CAPABILITY) {
1078 usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
1079 dev_cap->bDevCapabilityType,
1080 LIBUSB_BT_SUPERSPEED_PLUS_CAPABILITY);
1081 return LIBUSB_ERROR_INVALID_PARAM;
1082 } else if (dev_cap->bLength < LIBUSB_BT_SSPLUS_USB_DEVICE_CAPABILITY_SIZE) {
1083 usbi_err(ctx, "short dev-cap descriptor read %u/%d",
1084 dev_cap->bLength, LIBUSB_BT_SSPLUS_USB_DEVICE_CAPABILITY_SIZE);
1085 return LIBUSB_ERROR_IO;
1086 }
1087
1088 const uint8_t* dev_capability_data = dev_cap->dev_capability_data;
1089 parsedDescriptor.bLength = dev_cap->bLength;
1090 parsedDescriptor.bDescriptorType = dev_cap->bDescriptorType;
1091 parsedDescriptor.bDevCapabilityType = dev_cap->bDevCapabilityType;
1092 parsedDescriptor.bReserved = dev_capability_data[0];
1093 parsedDescriptor.bmAttributes = ReadLittleEndian32(&dev_capability_data[1]);
1094 parsedDescriptor.wFunctionalitySupport = ReadLittleEndian16(&dev_capability_data[5]);
1095 parsedDescriptor.wReserved = ReadLittleEndian16(&dev_capability_data[7]);
1096
1097 uint8_t numSublikSpeedAttributes = (parsedDescriptor.bmAttributes & 0xF) + 1;
1098 _ssplus_cap = malloc(sizeof(struct libusb_ssplus_usb_device_capability_descriptor) + (numSublikSpeedAttributes * sizeof(struct libusb_ssplus_sublink_attribute)));
1099 if (!_ssplus_cap)
1100 return LIBUSB_ERROR_NO_MEM;
1101
1102 /* Parse bmAttributes */
1103 _ssplus_cap->numSublinkSpeedAttributes = numSublikSpeedAttributes;
1104 _ssplus_cap->numSublinkSpeedIDs = ((parsedDescriptor.bmAttributes & 0xF0) >> 4) + 1;
1105
1106 /* Parse wFunctionalitySupport */
1107 _ssplus_cap->ssid = parsedDescriptor.wFunctionalitySupport & 0xF;
1108 _ssplus_cap->minRxLaneCount = (parsedDescriptor.wFunctionalitySupport & 0x0F00) >> 8;
1109 _ssplus_cap->minTxLaneCount = (parsedDescriptor.wFunctionalitySupport & 0xF000) >> 12;
1110
1111 /* Check that we have enough to read all the sublink attributes */
1112 if (dev_cap->bLength < LIBUSB_BT_SSPLUS_USB_DEVICE_CAPABILITY_SIZE + (_ssplus_cap->numSublinkSpeedAttributes * sizeof(uint32_t))) {
1113 free(_ssplus_cap);
1114 usbi_err(ctx, "short ssplus capability descriptor, unable to read sublinks: Not enough data");
1115 return LIBUSB_ERROR_IO;
1116 }
1117
1118 /* Read the attributes */
1119 uint8_t* base = ((uint8_t*)dev_cap) + LIBUSB_BT_SSPLUS_USB_DEVICE_CAPABILITY_SIZE;
1120 for(uint8_t i = 0 ; i < _ssplus_cap->numSublinkSpeedAttributes ; i++) {
1121 uint32_t attr = ReadLittleEndian32(base + (i * sizeof(uint32_t)));
1122 _ssplus_cap->sublinkSpeedAttributes[i].ssid = attr & 0x0f;
1123 _ssplus_cap->sublinkSpeedAttributes[i].mantissa = attr >> 16;
1124 _ssplus_cap->sublinkSpeedAttributes[i].exponent = (attr >> 4) & 0x3 ;
1125 _ssplus_cap->sublinkSpeedAttributes[i].type = attr & 0x40 ? LIBUSB_SSPLUS_ATTR_TYPE_ASYM : LIBUSB_SSPLUS_ATTR_TYPE_SYM;
1126 _ssplus_cap->sublinkSpeedAttributes[i].direction = attr & 0x80 ? LIBUSB_SSPLUS_ATTR_DIR_TX : LIBUSB_SSPLUS_ATTR_DIR_RX;
1127 _ssplus_cap->sublinkSpeedAttributes[i].protocol = attr & 0x4000 ? LIBUSB_SSPLUS_ATTR_PROT_SSPLUS: LIBUSB_SSPLUS_ATTR_PROT_SS;
1128 }
1129
1130 *ssplus_usb_device_cap = _ssplus_cap;
1131 return LIBUSB_SUCCESS;
1132 }
1133
1134 /** \ingroup libusb_desc
1135 * Free a SuperSpeedPlus USB Device Capability descriptor obtained from
1136 * libusb_get_ssplus_usb_device_capability_descriptor().
1137 * It is safe to call this function with a NULL ssplus_usb_device_cap
1138 * parameter, in which case the function simply returns.
1139 *
1140 * Since version 1.0.28, \ref LIBUSB_API_VERSION >= 0x0100010B
1141 *
1142 * \param ssplus_usb_device_cap the SuperSpeedPlus USB Device Capability descriptor
1143 * to free
1144 */
1145 void API_EXPORTED libusb_free_ssplus_usb_device_capability_descriptor(
1146 struct libusb_ssplus_usb_device_capability_descriptor *ssplus_usb_device_cap)
1147 {
1148 free(ssplus_usb_device_cap);
1149 }
1150
1151
1152
1153 /** \ingroup libusb_desc
1154 * Free a SuperSpeed USB Device Capability descriptor obtained from
1155 * libusb_get_ss_usb_device_capability_descriptor().
1156 * It is safe to call this function with a NULL ss_usb_device_cap
1157 * parameter, in which case the function simply returns.
1158 *
1159 * \param ss_usb_device_cap the SuperSpeed USB Device Capability descriptor
1160 * to free
1161 */
1162 void API_EXPORTED libusb_free_ss_usb_device_capability_descriptor(
1163 struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap)
1164 {
1165 free(ss_usb_device_cap);
1166 }
1167
1168 /** \ingroup libusb_desc
1169 * Get a Container ID descriptor
1170 *
1171 * \param ctx the context to operate on, or NULL for the default context
1172 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1173 * \ref libusb_bos_type::LIBUSB_BT_CONTAINER_ID
1174 * LIBUSB_BT_CONTAINER_ID
1175 * \param container_id output location for the Container ID descriptor.
1176 * Only valid if 0 was returned. Must be freed with
1177 * libusb_free_container_id_descriptor() after use.
1178 * \returns 0 on success
1179 * \returns a LIBUSB_ERROR code on error
1180 */
1181 int API_EXPORTED libusb_get_container_id_descriptor(libusb_context *ctx,
1182 struct libusb_bos_dev_capability_descriptor *dev_cap,
1183 struct libusb_container_id_descriptor **container_id)
1184 {
1185 struct libusb_container_id_descriptor *_container_id;
1186
1187 if (dev_cap->bDevCapabilityType != LIBUSB_BT_CONTAINER_ID) {
1188 usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
1189 dev_cap->bDevCapabilityType,
1190 LIBUSB_BT_CONTAINER_ID);
1191 return LIBUSB_ERROR_INVALID_PARAM;
1192 } else if (dev_cap->bLength < LIBUSB_BT_CONTAINER_ID_SIZE) {
1193 usbi_err(ctx, "short dev-cap descriptor read %u/%d",
1194 dev_cap->bLength, LIBUSB_BT_CONTAINER_ID_SIZE);
1195 return LIBUSB_ERROR_IO;
1196 }
1197
1198 _container_id = malloc(sizeof(*_container_id));
1199 if (!_container_id)
1200 return LIBUSB_ERROR_NO_MEM;
1201
1202 _container_id->bLength = dev_cap->bLength;
1203 _container_id->bDescriptorType = dev_cap->bDescriptorType;
1204 _container_id->bDevCapabilityType = dev_cap->bDevCapabilityType;
1205 _container_id->bReserved = dev_cap->dev_capability_data[0];
1206 memcpy(_container_id->ContainerID, &dev_cap->dev_capability_data[1], 16);
1207
1208 *container_id = _container_id;
1209 return LIBUSB_SUCCESS;
1210 }
1211
1212 /** \ingroup libusb_desc
1213 * Free a Container ID descriptor obtained from
1214 * libusb_get_container_id_descriptor().
1215 * It is safe to call this function with a NULL container_id parameter,
1216 * in which case the function simply returns.
1217 *
1218 * \param container_id the Container ID descriptor to free
1219 */
1220 void API_EXPORTED libusb_free_container_id_descriptor(
1221 struct libusb_container_id_descriptor *container_id)
1222 {
1223 free(container_id);
1224 }
1225
1226 /** \ingroup libusb_desc
1227 * Get a platform descriptor
1228 *
1229 * Since version 1.0.27, \ref LIBUSB_API_VERSION >= 0x0100010A
1230 *
1231 * \param ctx the context to operate on, or NULL for the default context
1232 * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1233 * \ref libusb_bos_type::LIBUSB_BT_PLATFORM_DESCRIPTOR
1234 * LIBUSB_BT_PLATFORM_DESCRIPTOR
1235 * \param platform_descriptor output location for the Platform descriptor.
1236 * Only valid if 0 was returned. Must be freed with
1237 * libusb_free_platform_descriptor() after use.
1238 * \returns 0 on success
1239 * \returns a LIBUSB_ERROR code on error
1240 */
1241 int API_EXPORTED libusb_get_platform_descriptor(libusb_context *ctx,
1242 struct libusb_bos_dev_capability_descriptor *dev_cap,
1243 struct libusb_platform_descriptor **platform_descriptor)
1244 {
1245 struct libusb_platform_descriptor *_platform_descriptor;
1246
1247 if (dev_cap->bDevCapabilityType != LIBUSB_BT_PLATFORM_DESCRIPTOR) {
1248 usbi_err(ctx, "unexpected bDevCapabilityType 0x%x (expected 0x%x)",
1249 dev_cap->bDevCapabilityType,
1250 LIBUSB_BT_PLATFORM_DESCRIPTOR);
1251 return LIBUSB_ERROR_INVALID_PARAM;
1252 } else if (dev_cap->bLength < LIBUSB_BT_PLATFORM_DESCRIPTOR_MIN_SIZE) {
1253 usbi_err(ctx, "short dev-cap descriptor read %u/%d",
1254 dev_cap->bLength, LIBUSB_BT_PLATFORM_DESCRIPTOR_MIN_SIZE);
1255 return LIBUSB_ERROR_IO;
1256 }
1257
1258 _platform_descriptor = malloc(dev_cap->bLength);
1259 if (!_platform_descriptor)
1260 return LIBUSB_ERROR_NO_MEM;
1261
1262 _platform_descriptor->bLength = dev_cap->bLength;
1263 _platform_descriptor->bDescriptorType = dev_cap->bDescriptorType;
1264 _platform_descriptor->bDevCapabilityType = dev_cap->bDevCapabilityType;
1265 _platform_descriptor->bReserved = dev_cap->dev_capability_data[0];
1266 memcpy(_platform_descriptor->PlatformCapabilityUUID, &(dev_cap->dev_capability_data[1]), 16);
1267
1268 /* Capability data is located after reserved byte and 16 byte UUID */
1269 uint8_t* capability_data = dev_cap->dev_capability_data + 1 + 16;
1270
1271 /* Capability data length is total descriptor length minus initial fields */
1272 size_t capability_data_length = dev_cap->bLength - (3 + 1 + 16);
1273
1274 memcpy(_platform_descriptor->CapabilityData, capability_data, capability_data_length);
1275
1276 *platform_descriptor = _platform_descriptor;
1277 return LIBUSB_SUCCESS;
1278 }
1279
1280 /** \ingroup libusb_desc
1281 * Free a platform descriptor obtained from
1282 * libusb_get_platform_descriptor().
1283 * It is safe to call this function with a NULL platform_descriptor parameter,
1284 * in which case the function simply returns.
1285 *
1286 * \param platform_descriptor the Platform descriptor to free
1287 */
1288 void API_EXPORTED libusb_free_platform_descriptor(
1289 struct libusb_platform_descriptor *platform_descriptor)
1290 {
1291 free(platform_descriptor);
1292 }
1293
1294 /** \ingroup libusb_desc
1295 * Retrieve a string descriptor in C style ASCII.
1296 *
1297 * Wrapper around libusb_get_string_descriptor(). Uses the first language
1298 * supported by the device.
1299 *
1300 * \param dev_handle a device handle
1301 * \param desc_index the index of the descriptor to retrieve
1302 * \param data output buffer for ASCII string descriptor
1303 * \param length size of data buffer
1304 * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1305 */
1306 int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev_handle,
1307 uint8_t desc_index, unsigned char *data, int length)
1308 {
1309 union usbi_string_desc_buf str;
1310 int r;
1311 uint16_t langid, wdata;
1312
1313 /* Asking for the zero'th index is special - it returns a string
1314 * descriptor that contains all the language IDs supported by the
1315 * device. Typically there aren't many - often only one. Language
1316 * IDs are 16 bit numbers, and they start at the third byte in the
1317 * descriptor. There's also no point in trying to read descriptor 0
1318 * with this function. See USB 2.0 specification section 9.6.7 for
1319 * more information.
1320 */
1321
1322 if (desc_index == 0)
1323 return LIBUSB_ERROR_INVALID_PARAM;
1324
1325 r = libusb_get_string_descriptor(dev_handle, 0, 0, str.buf, 4);
1326 if (r < 0)
1327 return r;
1328 else if (r != 4 || str.desc.bLength < 4 || str.desc.bDescriptorType != LIBUSB_DT_STRING) {
1329 usbi_warn(HANDLE_CTX(dev_handle), "invalid language ID string descriptor");
1330 return LIBUSB_ERROR_IO;
1331 } else if (str.desc.bLength & 1)
1332 usbi_warn(HANDLE_CTX(dev_handle), "suspicious bLength %u for language ID string descriptor", str.desc.bLength);
1333
1334 langid = libusb_le16_to_cpu(str.desc.wData[0]);
1335 r = libusb_get_string_descriptor(dev_handle, desc_index, langid, str.buf, sizeof(str.buf));
1336 if (r < 0)
1337 return r;
1338 else if (r < DESC_HEADER_LENGTH || str.desc.bLength > r || str.desc.bDescriptorType != LIBUSB_DT_STRING)
1339 return LIBUSB_ERROR_IO;
1340 else if ((str.desc.bLength & 1) || str.desc.bLength != r)
1341 usbi_warn(HANDLE_CTX(dev_handle), "suspicious bLength %u for string descriptor (read %d)", str.desc.bLength, r);
1342
1343 /* Stop one byte before the end to leave room for null termination. */
1344 int dest_max = length - 1;
1345
1346 /* The descriptor has this number of wide characters */
1347 int src_max = (str.desc.bLength - 1 - 1) / 2;
1348
1349 /* Neither read nor write more than the smallest buffer */
1350 int idx_max = MIN(dest_max, src_max);
1351
1352 int idx;
1353 for (idx = 0; idx < idx_max; ++idx) {
1354 wdata = libusb_le16_to_cpu(str.desc.wData[idx]);
1355 if (wdata < 0x80)
1356 data[idx] = (unsigned char)wdata;
1357 else
1358 data[idx] = '?'; /* non-ASCII */
1359 }
1360
1361 data[idx] = 0; /* null-terminate string */
1362 return idx;
1363 }
1364
1365 static int parse_iad_array(struct libusb_context *ctx,
1366 struct libusb_interface_association_descriptor_array *iad_array,
1367 const uint8_t *buffer, int size)
1368 {
1369 uint8_t i;
1370 struct usbi_descriptor_header header;
1371 int consumed = 0;
1372 const uint8_t *buf = buffer;
1373 struct libusb_interface_association_descriptor *iad;
1374
1375 if (size < LIBUSB_DT_CONFIG_SIZE) {
1376 usbi_err(ctx, "short config descriptor read %d/%d",
1377 size, LIBUSB_DT_CONFIG_SIZE);
1378 return LIBUSB_ERROR_IO;
1379 }
1380
1381 /* First pass: Iterate through desc list, count number of IADs */
1382 iad_array->length = 0;
1383 while (size - consumed >= DESC_HEADER_LENGTH) {
1384 header.bLength = buf[0];
1385 header.bDescriptorType = buf[1];
1386 if (header.bLength < DESC_HEADER_LENGTH) {
1387 usbi_err(ctx, "invalid descriptor bLength %d",
1388 header.bLength);
1389 return LIBUSB_ERROR_IO;
1390 }
1391 else if (header.bLength > size - consumed) {
1392 usbi_warn(ctx, "short config descriptor read %d/%u",
1393 size - consumed, header.bLength);
1394 return LIBUSB_ERROR_IO;
1395 }
1396 if (header.bDescriptorType == LIBUSB_DT_INTERFACE_ASSOCIATION)
1397 iad_array->length++;
1398 buf += header.bLength;
1399 consumed += header.bLength;
1400 }
1401
1402 iad_array->iad = NULL;
1403 if (iad_array->length > 0) {
1404 iad = calloc((size_t)iad_array->length, sizeof(*iad));
1405 if (!iad)
1406 return LIBUSB_ERROR_NO_MEM;
1407
1408 iad_array->iad = iad;
1409
1410 /* Second pass: Iterate through desc list, fill IAD structures */
1411 int remaining = size;
1412 i = 0;
1413 do {
1414 header.bLength = buffer[0];
1415 header.bDescriptorType = buffer[1];
1416 if (header.bDescriptorType == LIBUSB_DT_INTERFACE_ASSOCIATION && (remaining >= LIBUSB_DT_INTERFACE_ASSOCIATION_SIZE)) {
1417 iad[i].bLength = buffer[0];
1418 iad[i].bDescriptorType = buffer[1];
1419 iad[i].bFirstInterface = buffer[2];
1420 iad[i].bInterfaceCount = buffer[3];
1421 iad[i].bFunctionClass = buffer[4];
1422 iad[i].bFunctionSubClass = buffer[5];
1423 iad[i].bFunctionProtocol = buffer[6];
1424 iad[i].iFunction = buffer[7];
1425 i++;
1426 }
1427
1428 remaining -= header.bLength;
1429 if (remaining < DESC_HEADER_LENGTH) {
1430 break;
1431 }
1432 buffer += header.bLength;
1433 } while (1);
1434 }
1435
1436 return LIBUSB_SUCCESS;
1437 }
1438
1439 static int raw_desc_to_iad_array(struct libusb_context *ctx, const uint8_t *buf,
1440 int size, struct libusb_interface_association_descriptor_array **iad_array)
1441 {
1442 struct libusb_interface_association_descriptor_array *_iad_array
1443 = calloc(1, sizeof(*_iad_array));
1444 int r;
1445
1446 if (!_iad_array)
1447 return LIBUSB_ERROR_NO_MEM;
1448
1449 r = parse_iad_array(ctx, _iad_array, buf, size);
1450 if (r < 0) {
1451 usbi_err(ctx, "parse_iad_array failed with error %d", r);
1452 free(_iad_array);
1453 return r;
1454 }
1455
1456 *iad_array = _iad_array;
1457 return LIBUSB_SUCCESS;
1458 }
1459
1460 /** \ingroup libusb_desc
1461 * Get an array of interface association descriptors (IAD) for a given
1462 * configuration.
1463 * This is a non-blocking function which does not involve any requests being
1464 * sent to the device.
1465 *
1466 * \param dev a device
1467 * \param config_index the index of the configuration you wish to retrieve the
1468 * IADs for.
1469 * \param iad_array output location for the array of IADs. Only valid if 0 was
1470 * returned. Must be freed with libusb_free_interface_association_descriptors()
1471 * after use. It's possible that a given configuration contains no IADs. In this
1472 * case the iad_array is still output, but will have 'length' field set to 0, and
1473 * iad field set to NULL.
1474 * \returns 0 on success
1475 * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
1476 * \returns another LIBUSB_ERROR code on error
1477 * \see libusb_get_active_interface_association_descriptors()
1478 */
1479 int API_EXPORTED libusb_get_interface_association_descriptors(libusb_device *dev,
1480 uint8_t config_index, struct libusb_interface_association_descriptor_array **iad_array)
1481 {
1482 union usbi_config_desc_buf _config;
1483 uint16_t config_len;
1484 uint8_t *buf;
1485 int r;
1486
1487 if (!iad_array)
1488 return LIBUSB_ERROR_INVALID_PARAM;
1489
1490 usbi_dbg(DEVICE_CTX(dev), "IADs for config index %u", config_index);
1491 if (config_index >= dev->device_descriptor.bNumConfigurations)
1492 return LIBUSB_ERROR_NOT_FOUND;
1493
1494 r = get_config_descriptor(dev, config_index, _config.buf, sizeof(_config.buf));
1495 if (r < 0)
1496 return r;
1497
1498 config_len = libusb_le16_to_cpu(_config.desc.wTotalLength);
1499 buf = malloc(config_len);
1500 if (!buf)
1501 return LIBUSB_ERROR_NO_MEM;
1502
1503 r = get_config_descriptor(dev, config_index, buf, config_len);
1504 if (r >= 0)
1505 r = raw_desc_to_iad_array(DEVICE_CTX(dev), buf, r, iad_array);
1506
1507 free(buf);
1508 return r;
1509 }
1510
1511 /** \ingroup libusb_desc
1512 * Get an array of interface association descriptors (IAD) for the currently
1513 * active configuration.
1514 * This is a non-blocking function which does not involve any requests being
1515 * sent to the device.
1516 *
1517 * \param dev a device
1518 * \param iad_array output location for the array of IADs. Only valid if 0 was
1519 * returned. Must be freed with libusb_free_interface_association_descriptors()
1520 * after use. It's possible that a given configuration contains no IADs. In this
1521 * case the iad_array is still output, but will have 'length' field set to 0, and
1522 * iad field set to NULL.
1523 * \returns 0 on success
1524 * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
1525 * \returns another LIBUSB_ERROR code on error
1526 * \see libusb_get_interface_association_descriptors
1527 */
1528 int API_EXPORTED libusb_get_active_interface_association_descriptors(libusb_device *dev,
1529 struct libusb_interface_association_descriptor_array **iad_array)
1530 {
1531 union usbi_config_desc_buf _config;
1532 uint16_t config_len;
1533 uint8_t *buf;
1534 int r;
1535
1536 if (!iad_array)
1537 return LIBUSB_ERROR_INVALID_PARAM;
1538
1539 r = get_active_config_descriptor(dev, _config.buf, sizeof(_config.buf));
1540 if (r < 0)
1541 return r;
1542
1543 config_len = libusb_le16_to_cpu(_config.desc.wTotalLength);
1544 buf = malloc(config_len);
1545 if (!buf)
1546 return LIBUSB_ERROR_NO_MEM;
1547
1548 r = get_active_config_descriptor(dev, buf, config_len);
1549 if (r >= 0)
1550 r = raw_desc_to_iad_array(DEVICE_CTX(dev), buf, r, iad_array);
1551 free(buf);
1552 return r;
1553 }
1554
1555 /** \ingroup libusb_desc
1556 * Free an array of interface association descriptors (IADs) obtained from
1557 * libusb_get_interface_association_descriptors() or
1558 * libusb_get_active_interface_association_descriptors().
1559 * It is safe to call this function with a NULL iad_array parameter, in which
1560 * case the function simply returns.
1561 *
1562 * \param iad_array the IAD array to free
1563 */
1564 void API_EXPORTED libusb_free_interface_association_descriptors(
1565 struct libusb_interface_association_descriptor_array *iad_array)
1566 {
1567 if (!iad_array)
1568 return;
1569
1570 if (iad_array->iad)
1571 free((void*)iad_array->iad);
1572 free(iad_array);
1573 }
1574
1575 /*
1576 * \brief Copy a UTF-8 string with proper truncation if needed.
1577 *
1578 * \param tgt The target utf-8 string. If NULL, then tgt is ignored,
1579 * tgt_size is forced to 0, and this function returns the
1580 * required tgt_size for a subsequent call to this function.
1581 * \param src The source utf-8 string. If NULL, then
1582 * the source string is empty, the return length is 1,
1583 * and, if tgt is not NULL, this function
1584 * sets tgt[0] to the null terminator.
1585 * \param tgt_size The size of target in bytes.
1586 * \return the length of src in bytes, including the null terminator.
1587 *
1588 * utf8_copy(NULL, src, 0) is equivalent to strlen(src) + 1.
1589 */
1590 static int usbi_utf8_copy(char *tgt, char const *src, int tgt_size) {
1591 uint8_t* t = (uint8_t*)tgt;
1592 uint8_t const* s = (uint8_t const*)src;
1593
1594 if (NULL == src) {
1595 if ((NULL != tgt) && (tgt_size > 0)) {
1596 tgt[0] = 0;
1597 }
1598 return 1;
1599 }
1600
1601 if ((NULL == tgt) || (tgt_size <= 0)) {
1602 return (int)(strlen(src) + 1);
1603 }
1604
1605 // copy UTF-8 string and compute length
1606 int k = 0;
1607 for (k = 0; s[k]; ++k) {
1608 if (k < tgt_size) {
1609 t[k] = s[k];
1610 } else {
1611 break;
1612 }
1613 }
1614
1615 if (k >= tgt_size) {
1616 // truncate respecting UTF-8 character boundaries
1617 int idx = tgt_size - 1;
1618 while (idx && (0x80 == (t[idx] & 0xC0))) { // utf-8 continuation byte
1619 --idx;
1620 }
1621 t[idx] = 0;
1622 return (int)(strlen(src) + 1);
1623 } else {
1624 t[k++] = 0;
1625 return k;
1626 }
1627 }
1628
1629 /** \ingroup libusb_desc
1630 * Retrieve a device string without needing to open the device.
1631 *
1632 * Since version v1.0.30 \ref LIBUSB_API_VERSION >= 0x0100010C
1633 *
1634 * \param dev the target device
1635 * \param string_type the string type to retrieve
1636 * \param data the data buffer for the UTF-8 encoded string.
1637 * \param length the size of the data buffer in bytes.
1638 * USB string descriptors cannot be longer than
1639 * LIBUSB_DEVICE_STRING_BYTES_MAX.
1640 * \returns a negative error code or
1641 * the actual string length in bytes including the null terminator.
1642 * \see libusb_get_string_descriptor()
1643 * \see libusb_get_string_descriptor_ascii()
1644 *
1645 * This function works when the device is still closed since it relies
1646 * on the operating system to provide the string. The operating system
1647 * normally reads and caches the common string descriptors during
1648 * USB enumeration.
1649 *
1650 * Since the USB string descriptor could be processed by the OS,
1651 * this function returns a UTF-8 encoded string.
1652 *
1653 * The string will be returned untranslated or in the default OS language
1654 * when supported by the OS and USB device.
1655 *
1656 * One way to call this function is using a buffer on the stack:
1657 *
1658 * char buffer[LIBUSB_DEVICE_STRING_BYTES_MAX];
1659 * int ret = libusb_get_device_string(dev, LIBUSB_DEVICE_STRING_SERIAL_NUMBER, buffer, sizeof(buffer));
1660 * if (ret < 0) {
1661 * // handle error
1662 * }
1663 *
1664 * This function is commonly used to get the serial number to allow
1665 * for device selection before opening the selected device.
1666 */
1667 int API_EXPORTED libusb_get_device_string(libusb_device *dev,
1668 enum libusb_device_string_type string_type, char *data, int length) {
1669 char * s;
1670 if (NULL == dev) {
1671 return LIBUSB_ERROR_INVALID_PARAM;
1672 }
1673 if ((string_type < 0) || (string_type >= LIBUSB_DEVICE_STRING_COUNT)) {
1674 return LIBUSB_ERROR_INVALID_PARAM;
1675 }
1676 if (length <= 0) {
1677 return LIBUSB_ERROR_INVALID_PARAM;
1678 }
1679 if (NULL == data) {
1680 length = 0;
1681 data = NULL;
1682 } else if (length > 0) {
1683 *data = 0; // return an empty string on errors when possible
1684 }
1685
1686 if (NULL == dev->device_strings_utf8[string_type]) {
1687 if (usbi_backend.get_device_string) {
1688 s = malloc(LIBUSB_DEVICE_STRING_BYTES_MAX);
1689 int rv = usbi_backend.get_device_string(dev, string_type, s, LIBUSB_DEVICE_STRING_BYTES_MAX);
1690 if (rv < 0) {
1691 free(s);
1692 return rv;
1693 } else {
1694 dev->device_strings_utf8[string_type] = s;
1695 }
1696 } else {
1697 return LIBUSB_ERROR_NOT_SUPPORTED;
1698 }
1699 }
1700
1701 s = dev->device_strings_utf8[string_type];
1702 if (NULL == s) {
1703 return LIBUSB_ERROR_NOT_SUPPORTED;
1704 }
1705
1706 return usbi_utf8_copy(data, s, length);
1707 }