stream pushing ok without access permission of /dev/video0
[rtmpclient.git] / app / src / main / jni / libusb / libusb / descriptor.c
1 /**
2  * modified to improve compatibility with some cameras.
3  * Copyright(c) 2014 saki saki@serenegiant.com
4  */
5 /* -*- Mode: C; indent-tabs-mode:t ; c-basic-offset:8 -*- */
6 /*
7  * USB descriptor handling functions for libusb
8  * Copyright © 2007 Daniel Drake <dsd@gentoo.org>
9  * Copyright © 2001 Johannes Erdfelt <johannes@erdfelt.com>
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
24  */
25
26 #include <errno.h>
27 #include <stdint.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include "libusbi.h"
32
33 // comment out because duplicate definitions already exit in libusb.h
34 //#define DESC_HEADER_LENGTH                    2       // XXX this is same as LIBUSB_DT_HEADER_SIZE in libusb.h
35 //#define DEVICE_DESC_LENGTH                    18      // XXX this is same as LIBUSB_DT_DEVICE_SIZE in libusb.h
36 //#define CONFIG_DESC_LENGTH                    9       // XXX this is same as LIBUSB_DT_CONFIG_SIZE in libusb.h
37 //#define INTERFACE_DESC_LENGTH                 9       // XXX this is same as LIBUSB_DT_INTERFACE_SIZE in libusb.h
38 //#define ENDPOINT_DESC_LENGTH                  7       // XXX this is same as LIBUSB_DT_ENDPOINT_SIZE in libusb.h
39 //#define ENDPOINT_AUDIO_DESC_LENGTH    9       // XXX this is same as LIBUSB_DT_ENDPOINT_AUDIO_SIZE in libusb.h
40 //#define ASSOCIATION_DESC_LENGTH               8       // XXX this is same as LIBUSB_DT_ASSOCIATION_SIZE in libusb.h
41
42 /** @defgroup desc USB descriptors
43  * This page details how to examine the various standard USB descriptors
44  * for detected devices
45  */
46
47 static inline int is_known_descriptor_type(int type) {
48         return ((type == LIBUSB_DT_ENDPOINT)
49                 || (type == LIBUSB_DT_INTERFACE)
50                 || (type == LIBUSB_DT_CONFIG)
51                 || (type == LIBUSB_DT_DEVICE)
52                 || (type == LIBUSB_DT_ASSOCIATION) );
53 }
54
55 /* set host_endian if the w values are already in host endian format,
56  * as opposed to bus endian. */
57 int usbi_parse_descriptor(const unsigned char *source, const char *descriptor,
58         void *dest, int host_endian)
59 {
60         const unsigned char *sp = source;
61         unsigned char *dp = dest;
62         uint16_t w;
63         const char *cp;
64         uint32_t d;
65
66         for (cp = descriptor; *cp; cp++) {
67                 switch (*cp) {
68                         case 'b':       /* 8-bit byte */
69                                 *dp++ = *sp++;
70                                 break;
71                         case 'w':       /* 16-bit word, convert from little endian to CPU */
72                                 dp += ((uintptr_t)dp & 1);      /* Align to word boundary */
73
74                                 if (host_endian) {
75                                         memcpy(dp, sp, 2);
76                                 } else {
77                                         w = (sp[1] << 8) | sp[0];
78                                         *((uint16_t *)dp) = w;
79                                 }
80                                 sp += 2;
81                                 dp += 2;
82                                 break;
83                         case 'd':       /* 32-bit word, convert from little endian to CPU */
84                                 dp += ((uintptr_t)dp & 1);      /* Align to word boundary */
85
86                                 if (host_endian) {
87                                         memcpy(dp, sp, 4);
88                                 } else {
89                                         d = (sp[3] << 24) | (sp[2] << 16) |
90                                                 (sp[1] << 8) | sp[0];
91                                         *((uint32_t *)dp) = d;
92                                 }
93                                 sp += 4;
94                                 dp += 4;
95                                 break;
96                         case 'u':       /* 16 byte UUID */
97                                 memcpy(dp, sp, 16);
98                                 sp += 16;
99                                 dp += 16;
100                                 break;
101                 }
102         }
103
104         return (int) (sp - source);
105 }
106
107 static void clear_endpoint(struct libusb_endpoint_descriptor *endpoint)
108 {
109         if LIKELY(endpoint && endpoint->extra) {
110                 free((unsigned char *) endpoint->extra);
111                 endpoint->extra = NULL; // XXX
112                 endpoint->extra_length = 0;
113         }
114 }
115
116 static int parse_endpoint(struct libusb_context *ctx,
117         struct libusb_endpoint_descriptor *endpoint, unsigned char *buffer,
118         int size, int host_endian)
119 {
120         ENTER();
121
122         struct usb_descriptor_header header;
123         unsigned char *extra;
124         unsigned char *begin;
125         int parsed = 0;
126         int len;
127
128         if UNLIKELY(size < LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
129                 usbi_err(ctx, "short endpoint descriptor read %d/%d",
130                          size, LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/);
131                 RETURN(LIBUSB_ERROR_IO, int);
132         }
133
134         usbi_parse_descriptor(buffer, "bb", &header, 0);
135         if UNLIKELY(header.bDescriptorType != LIBUSB_DT_ENDPOINT) {
136                 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
137                         header.bDescriptorType, LIBUSB_DT_ENDPOINT);
138                 RETURN(parsed, int);
139         }
140         if UNLIKELY(header.bLength > size) {
141                 usbi_warn(ctx, "short endpoint descriptor read %d/%d",
142                           size, header.bLength);
143                 RETURN(parsed, int);
144         }
145         if (header.bLength >= LIBUSB_DT_ENDPOINT_AUDIO_SIZE/*ENDPOINT_AUDIO_DESC_LENGTH*/)
146                 usbi_parse_descriptor(buffer, "bbbbwbbb", endpoint, host_endian);
147         else if (header.bLength >= LIBUSB_DT_ENDPOINT_SIZE/*ENDPOINT_DESC_LENGTH*/)
148                 usbi_parse_descriptor(buffer, "bbbbwb", endpoint, host_endian);
149         else {
150                 usbi_err(ctx, "invalid endpoint bLength (%d)", header.bLength);
151                 RETURN(LIBUSB_ERROR_IO, int);
152         }
153
154         buffer += header.bLength;
155         size -= header.bLength;
156         parsed += header.bLength;
157
158         /* Skip over the rest of the Class Specific or Vendor Specific */
159         /*  descriptors */
160         begin = buffer;
161         while (size >= LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
162                 usbi_parse_descriptor(buffer, "bb", &header, 0);
163                 if UNLIKELY(header.bLength < LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
164                         usbi_err(ctx, "invalid extra ep desc len (%d)",
165                                  header.bLength);
166                         RETURN(LIBUSB_ERROR_IO, int);
167                 } else if (header.bLength > size) {
168                         usbi_warn(ctx, "short extra ep desc read %d/%d",
169                                   size, header.bLength);
170                         RETURN(parsed, int);
171                 }
172
173                 /* If we find another "proper" descriptor then we're done  */
174                 if (is_known_descriptor_type(header.bDescriptorType))
175                         break;
176
177                 usbi_dbg("skipping descriptor 0x%02x", header.bDescriptorType);
178                 buffer += header.bLength;
179                 size -= header.bLength;
180                 parsed += header.bLength;
181         }
182
183         /* Copy any unknown descriptors into a storage area for drivers */
184         /*  to later parse */
185         len = (int)(buffer - begin);
186         if (!len) {
187                 endpoint->extra = NULL;
188                 endpoint->extra_length = 0;
189                 RETURN(parsed, int);
190         }
191
192         endpoint->extra = extra = malloc(len);
193         if UNLIKELY(!extra) {
194                 endpoint->extra_length = 0;
195                 RETURN(LIBUSB_ERROR_NO_MEM, int);
196         }
197
198         memcpy(extra, begin, len);
199         endpoint->extra_length = len;
200
201         RETURN(parsed, int);
202 }
203
204 static void clear_interface(struct libusb_interface *usb_interface)
205 {
206         int i;
207         int j;
208
209         if (usb_interface->altsetting) {
210                 for (i = 0; i < usb_interface->num_altsetting; i++) {
211                         struct libusb_interface_descriptor *ifp =
212                                 (struct libusb_interface_descriptor *)
213                                 usb_interface->altsetting + i;
214                         if (ifp->extra)
215                                 free((void *) ifp->extra);
216                         if (ifp->endpoint) {
217                                 for (j = 0; j < ifp->bNumEndpoints; j++)
218                                         clear_endpoint((struct libusb_endpoint_descriptor *)
219                                                 ifp->endpoint + j);
220                                 free((void *) ifp->endpoint);
221                         }
222                 }
223                 free((void *) usb_interface->altsetting);
224                 usb_interface->altsetting = NULL;
225         }
226
227 }
228
229 static int parse_interface(libusb_context *ctx,
230         struct libusb_interface *usb_interface, unsigned char *buffer, int size,
231         int host_endian)
232 {
233         ENTER();
234
235         int i;
236         int len;
237         int r;
238         int parsed = 0;
239         int interface_number = -1;
240         size_t tmp;
241         struct usb_descriptor_header header;
242         struct libusb_interface_descriptor *ifp;
243         unsigned char *begin;
244
245         usb_interface->num_altsetting = 0;
246
247         while (size >= LIBUSB_DT_INTERFACE_SIZE/*INTERFACE_DESC_LENGTH*/) {
248                 struct libusb_interface_descriptor *altsetting =
249                         (struct libusb_interface_descriptor *) usb_interface->altsetting;
250                 altsetting = usbi_reallocf(altsetting,
251                         sizeof(struct libusb_interface_descriptor) * (usb_interface->num_altsetting + 1));
252                 if UNLIKELY(!altsetting) {
253                         r = LIBUSB_ERROR_NO_MEM;
254                         goto err;
255                 }
256                 usb_interface->altsetting = altsetting;
257
258                 ifp = altsetting + usb_interface->num_altsetting;
259                 usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0);
260                 if UNLIKELY(ifp->bDescriptorType != LIBUSB_DT_INTERFACE) {
261                         usbi_err(ctx, "unexpected descriptor %x (expected %x)",
262                                  ifp->bDescriptorType, LIBUSB_DT_INTERFACE);
263                         RETURN(parsed, int);
264                 }
265                 if UNLIKELY(ifp->bLength < LIBUSB_DT_INTERFACE_SIZE/*INTERFACE_DESC_LENGTH*/) {
266                         usbi_err(ctx, "invalid interface bLength (%d)",
267                                  ifp->bLength);
268                         r = LIBUSB_ERROR_IO;
269                         goto err;
270                 }
271                 if UNLIKELY(ifp->bLength > size) {
272                         usbi_warn(ctx, "short intf descriptor read %d/%d",
273                                  size, ifp->bLength);
274                         RETURN(parsed, int);
275                 }
276                 if UNLIKELY(ifp->bNumEndpoints > USB_MAXENDPOINTS) {
277                         usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints);
278                         r = LIBUSB_ERROR_IO;
279                         goto err;
280                 }
281
282                 usb_interface->num_altsetting++;
283                 ifp->extra = NULL;
284                 ifp->extra_length = 0;
285                 ifp->endpoint = NULL;
286
287                 if (interface_number == -1)
288                         interface_number = ifp->bInterfaceNumber;
289
290                 /* Skip over the interface */
291                 buffer += ifp->bLength;
292                 parsed += ifp->bLength;
293                 size -= ifp->bLength;
294
295                 begin = buffer;
296
297                 /* Skip over any interface, class or vendor descriptors */
298                 while (size >= LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
299                         usbi_parse_descriptor(buffer, "bb", &header, 0);
300                         if UNLIKELY(header.bLength < LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
301                                 usbi_err(ctx,
302                                          "invalid extra intf desc len (%d)",
303                                          header.bLength);
304                                 r = LIBUSB_ERROR_IO;
305                                 goto err;
306                         } else if (header.bLength > size) {
307                                 usbi_warn(ctx,
308                                           "short extra intf desc read %d/%d",
309                                           size, header.bLength);
310                                 RETURN(parsed, int);
311                         }
312
313                         MARK("bDescriptorType=0x%02x", header.bDescriptorType);
314                         /* If we find another "proper" descriptor then we're done */
315                         if (is_known_descriptor_type(header.bDescriptorType))
316                                 break;
317
318                         buffer += header.bLength;
319                         parsed += header.bLength;
320                         size -= header.bLength;
321                 }
322
323                 /* Copy any unknown descriptors into a storage area for */
324                 /*  drivers to later parse */
325                 len = (int)(buffer - begin);
326                 if (len) {
327                         MARK("save unknown descriptors into ifp->extra:lebgth=%d", len);
328                         ifp->extra = usbi_reallocf((unsigned char *)ifp->extra, ifp->extra_length + len);
329                         if UNLIKELY(!ifp->extra) {
330                                 r = LIBUSB_ERROR_NO_MEM;
331                                 goto err;
332                         }
333                         memcpy((unsigned char *)(ifp->extra + ifp->extra_length), begin, len);
334                         ifp->extra_length += len;
335                 }
336
337                 MARK("bNumEndpoints=%d", ifp->bNumEndpoints);
338                 if (ifp->bNumEndpoints > 0) {
339                         struct libusb_endpoint_descriptor *endpoint;
340                         tmp = ifp->bNumEndpoints * sizeof(struct libusb_endpoint_descriptor);
341                         ifp->endpoint = endpoint = malloc(tmp);
342                         if UNLIKELY(!endpoint) {
343                                 r = LIBUSB_ERROR_NO_MEM;
344                                 goto err;
345                         }
346
347                         memset(endpoint, 0, tmp);
348                         for (i = 0; i < ifp->bNumEndpoints; i++) {
349                                 MARK("parse endpoint%d", i);
350                                 r = parse_endpoint(ctx, endpoint + i, buffer, size,
351                                         host_endian);
352                                 if UNLIKELY(r < 0)
353                                         goto err;
354                                 if (r == 0) {
355                                         ifp->bNumEndpoints = (uint8_t)i;
356                                         break;;
357                                 }
358
359                                 buffer += r;
360                                 parsed += r;
361                                 size -= r;
362                         }
363                 }
364
365                 /* We check to see if it's an alternate to this one */
366                 ifp = (struct libusb_interface_descriptor *) buffer;
367                 if (size < LIBUSB_DT_INTERFACE_SIZE ||
368                                 ifp->bDescriptorType != LIBUSB_DT_INTERFACE ||
369                                 ifp->bInterfaceNumber != interface_number)
370                         RETURN(parsed, int);
371         }
372
373         RETURN(parsed, int);
374 err:
375         clear_interface(usb_interface);
376         RETURN(r, int);
377 }
378
379 static void clear_association(struct libusb_association_descriptor *association) {
380         if LIKELY(association && association->extra) {
381                 free((unsigned char *) association->extra);
382                 association->extra = NULL;
383                 association->extra_length = 0;
384         }
385 }
386
387 static int parse_association(struct libusb_context *ctx,
388                 struct libusb_config_descriptor *config, unsigned char *buffer,
389         int size, int host_endian) {
390
391         ENTER();
392
393         struct usb_descriptor_header header;
394         struct libusb_association_descriptor *association, *temp;
395         unsigned char *begin;
396         int parsed = 0;
397         int len;
398
399         if UNLIKELY(size < LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
400                 usbi_err(ctx, "short association descriptor read %d/%d",
401                          size, LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/);
402                 RETURN(LIBUSB_ERROR_IO, int);
403         }
404         // ディスクリプタの先頭2バイトだけ解析して長さとディスクリプタの種類を取得
405         usbi_parse_descriptor(buffer, "bb", &header, 0);
406         if UNLIKELY(header.bDescriptorType != LIBUSB_DT_ASSOCIATION) {  // 種類が違う時
407                 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
408                         header.bDescriptorType, LIBUSB_DT_ASSOCIATION);
409                 RETURN(parsed, int);    // return 0;
410         }
411         if UNLIKELY(header.bLength > size) {    // IADに長さが足りない時
412                 usbi_warn(ctx, "short association descriptor read %d/%d",
413                           size, header.bLength);
414                 RETURN(parsed, int);    // return 0;
415         }
416         if (header.bLength >= LIBUSB_DT_ASSOCIATION_SIZE/*ASSOCIATION_DESC_LENGTH*/) {
417                 config->association_descriptor = usbi_reallocf(config->association_descriptor,
418                         sizeof(struct libusb_association_descriptor) * (config->num_associations + 1));
419                 if UNLIKELY(!config->association_descriptor) {
420                         parsed = LIBUSB_ERROR_NO_MEM;
421                         goto err;
422                 }
423                 association = config->association_descriptor + config->num_associations;
424                 association->extra = NULL;
425                 association->extra_length = 0;
426                 len = usbi_parse_descriptor(buffer, "bbbbbbbb", association, host_endian);
427                 if LIKELY(len > 0) {
428                         config->num_associations++;
429 #if 0
430                         LOGI("\t association:bLength=%d", association->bLength);
431                         LOGI("\t association:bDescriptorType=0x%02d", association->bDescriptorType);
432                         LOGI("\t association:bFirstInterface=%d", association->bFirstInterface);
433                         LOGI("\t association:bInterfaceCount=%d", association->bInterfaceCount);
434                         LOGI("\t association:bFunctionClass=0x%02x", association->bFunctionClass);
435                         LOGI("\t association:bFunctionSubClass=0x%02x", association->bFunctionSubClass);
436                         LOGI("\t association:bFunctionProtocol=0x%02x", association->bFunctionProtocol);
437                         LOGI("\t association:iFunction=%d", association->iFunction);
438 #endif
439                 } else {
440                         // 解析に失敗した時は未使用部分を削除
441                         config->association_descriptor = usbi_reallocf(association,
442                                 sizeof(struct libusb_association_descriptor) * config->num_associations);
443                 }
444         } else {
445                 // 種類はIADで有るにも関わらず長さが足りない時
446                 usbi_err(ctx, "invalid interface association descriptor bLength (%d)", header.bLength);
447                 RETURN(LIBUSB_ERROR_IO, int);
448         }
449         // 次の解析開始位置・残りサイズをセット
450         buffer += header.bLength;
451         size -= header.bLength;
452         parsed += header.bLength;
453
454         /* Skip over the rest of the Class Specific or Vendor Specific descriptors */
455         begin = buffer;
456         while (size >= LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
457                 usbi_parse_descriptor(buffer, "bb", &header, 0);
458                 if UNLIKELY(header.bLength < LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
459                         usbi_err(ctx, "invalid extra ia desc len (%d)",
460                                  header.bLength);
461                         RETURN(LIBUSB_ERROR_IO, int);
462                 } else if (header.bLength > size) {
463                         usbi_warn(ctx, "short extra ia desc read %d/%d",
464                                   size, header.bLength);
465                         RETURN(parsed, int);
466                 }
467
468                 MARK("bDescriptorType=0x%02x", header.bDescriptorType);
469                 /* If we find another "proper" descriptor then we're done  */
470                 if (is_known_descriptor_type(header.bDescriptorType))
471                         break;
472
473                 usbi_dbg("skipping descriptor 0x%02x", header.bDescriptorType);
474                 buffer += header.bLength;
475                 size -= header.bLength;
476                 parsed += header.bLength;
477         }
478
479         // Append/Copy any unknown descriptors into a storage area for drivers to later parse
480         len = (int)(buffer - begin);
481         if (!len) {
482                 RETURN(parsed, int);
483         }
484
485         MARK("save unknown descriptors into config->extra:length=%d", len);
486         config->extra = usbi_reallocf((unsigned char *)config->extra, config->extra_length + len);
487         if UNLIKELY(!config->extra) {
488                 config->extra_length = 0;
489                 RETURN(LIBUSB_ERROR_NO_MEM, int);
490         }
491         memcpy((unsigned char *)config->extra + config->extra_length, begin, len);
492         config->extra_length += len;
493
494         RETURN(parsed, int);
495 err:
496         clear_association(config->association_descriptor);
497         config->association_descriptor = NULL;
498         RETURN(parsed, int);
499 }
500
501 static void clear_configuration(struct libusb_config_descriptor *config)
502 {
503         if UNLIKELY(!config) return;
504
505         if LIKELY(config->interface) {
506                 int i;
507                 for (i = 0; i < config->bNumInterfaces; i++)
508                         clear_interface((struct libusb_interface *)
509                                 config->interface + i);
510                 free((void *) config->interface);
511                 config->interface = NULL;       // XXX
512         }
513         if (config->extra) {
514                 free((void *) config->extra);
515                 config->extra = NULL;   // XXX
516         }
517         if LIKELY(config->association_descriptor) {
518                 int i;
519                 for (i = 0; i < config->num_associations; i++)
520                         clear_association(config->association_descriptor + i);
521                 free((void *)config->association_descriptor);
522                 config->association_descriptor = NULL;
523         }
524 }
525
526 static int parse_configuration(struct libusb_context *ctx,
527         struct libusb_config_descriptor *config, unsigned char *buffer,
528         int size, int host_endian) {
529
530         ENTER();
531
532         int parsed_if;
533         int r;
534         size_t tmp;
535         struct usb_descriptor_header header;
536         struct libusb_interface *usb_interface;
537         struct libusb_association_descriptor *association_desc;
538
539         if UNLIKELY(size < LIBUSB_DT_CONFIG_SIZE) {
540                 usbi_err(ctx, "short config descriptor read %d/%d",
541                          size, LIBUSB_DT_CONFIG_SIZE);
542                 RETURN(LIBUSB_ERROR_IO, int);
543         }
544
545         usbi_parse_descriptor(buffer, "bbwbbbbb", config, host_endian);
546         if UNLIKELY(config->bDescriptorType != LIBUSB_DT_CONFIG) {
547                 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
548                          config->bDescriptorType, LIBUSB_DT_CONFIG);
549                 RETURN(LIBUSB_ERROR_IO, int);
550         }
551         if UNLIKELY(config->bLength < LIBUSB_DT_CONFIG_SIZE) {
552                 usbi_err(ctx, "invalid config bLength (%d)", config->bLength);
553                 RETURN(LIBUSB_ERROR_IO, int);
554         }
555         if UNLIKELY(config->bLength > size) {
556                 usbi_err(ctx, "short config descriptor read %d/%d",
557                          size, config->bLength);
558                 RETURN(LIBUSB_ERROR_IO, int);
559         }
560         if UNLIKELY(config->bNumInterfaces > USB_MAXINTERFACES) {
561                 usbi_err(ctx, "too many interfaces (%d)", config->bNumInterfaces);
562                 RETURN(LIBUSB_ERROR_IO, int);
563         }
564         // インターフェースディスクリプタ配列を確保(長さはconfig->bNumInterfaces)
565         tmp = config->bNumInterfaces * sizeof(struct libusb_interface);
566         config->interface = usb_interface = malloc(tmp);
567         // インターフェースディスクリプタ配列を確保できなかった
568         if UNLIKELY(!config->interface)
569                 RETURN(LIBUSB_ERROR_NO_MEM, int);
570
571         config->association_descriptor = NULL;
572         config->num_associations = 0;
573
574         memset(usb_interface, 0, tmp);
575         buffer += config->bLength;
576         size -= config->bLength;
577
578         config->extra = NULL;
579         config->extra_length = 0;
580         MARK("bNumInterfaces=%d", config->bNumInterfaces);
581         for (parsed_if = 0; (parsed_if < config->bNumInterfaces) && (size > 0); /*parsed_if++*/) {
582                 int len;
583                 unsigned char *begin;
584
585                 /* Skip over the rest of the Class Specific or Vendor Specific descriptors */
586                 begin = buffer;
587                 while (size >= LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
588                         usbi_parse_descriptor(buffer, "bb", &header, 0);
589
590                         if UNLIKELY(header.bLength < LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
591                                 usbi_err(ctx,
592                                          "invalid extra config desc len (%d)",
593                                          header.bLength);
594                                 r = LIBUSB_ERROR_IO;
595                                 goto err;
596                         } else if UNLIKELY(header.bLength > size) {
597                                 usbi_warn(ctx,
598                                           "short extra config desc read %d/%d",
599                                           size, header.bLength);
600                                 config->bNumInterfaces = (uint8_t)parsed_if;
601                                 return size;
602                         }
603
604                         MARK("bDescriptorType=0x%02x", header.bDescriptorType);
605                         /* If we find another "proper" descriptor then we're done */
606                         if (is_known_descriptor_type(header.bDescriptorType))
607                                 break;
608
609                         usbi_dbg("skipping descriptor 0x%02x\n", header.bDescriptorType);
610                         buffer += header.bLength;
611                         size -= header.bLength;
612                 }
613
614                 /* Copy any unknown descriptors into a storage area for */
615                 /*  drivers to later parse */
616                 len = (int)(buffer - begin);
617                 if (len) {
618                         MARK("save skipped unknown descriptors into config->extra:len=%d", len);
619                         config->extra = usbi_reallocf((void *) config->extra, config->extra_length + len);
620                         if UNLIKELY(!config->extra) {
621                                 r = LIBUSB_ERROR_NO_MEM;
622                                 goto err;
623                         }
624                         memcpy((unsigned char *)(config->extra + config->extra_length), begin, len);
625                         config->extra_length += len;
626                 }
627                 switch (header.bDescriptorType) {
628                 case LIBUSB_DT_ASSOCIATION:
629                         r = parse_association(ctx, config, buffer, size, host_endian);
630                         if (r < 0)
631                                 goto err;
632                         break;
633                 default:
634                 case LIBUSB_DT_INTERFACE:
635                         r = parse_interface(ctx, usb_interface + parsed_if, buffer, size, host_endian);
636                         parsed_if++;
637                         if (r < 0)
638                                 goto err;
639                         break;
640                 }
641                 if (r == 0) {
642                         config->bNumInterfaces = (uint8_t)parsed_if;
643                         break;
644                 }
645
646                 buffer += r;
647                 size -= r;
648         }
649         RETURN(size, int);
650
651 err:
652         clear_configuration(config);
653         RETURN(r, int);
654 }
655
656 #if PRINT_DIAG
657 static void dump_descriptors(unsigned char *buffer, int size) {
658         struct usb_descriptor_header header;
659         struct libusb_config_descriptor config;
660         struct libusb_interface_descriptor interface;
661         struct libusb_endpoint_descriptor endpoint;
662         int i;
663
664         LOGI("DUMP DESCRIPTIONS");
665         for (i = 0; size >= 0; i += header.bLength, size -= header.bLength) {
666                 if (size == 0) {
667                         LOGI("END");
668                         return;
669                 }
670
671                 if (size < LIBUSB_DT_HEADER_SIZE) {
672                         LOGE("short descriptor read %d/2", size);
673                         return;
674                 }
675                 usbi_parse_descriptor(buffer + i, "bb", &header, 0);
676                 switch (header.bDescriptorType) {
677                 case LIBUSB_DT_DEVICE:
678                         LOGI("LIBUSB_DT_DEVICE(0x%02x),length=%d", header.bDescriptorType, header.bLength);
679                         break;
680                 case LIBUSB_DT_CONFIG:
681                         usbi_parse_descriptor(buffer, "bbwbbbbb", &config, 0);
682                         LOGI("LIBUSB_DT_CONFIG(0x%02x)", config.bDescriptorType);
683                         LOGI("\tbLength=%d", config.bLength);
684                         LOGI("\tbDescriptorType=0x%02x", config.bDescriptorType);
685                         LOGI("\twTotalLength=%d", config.wTotalLength);
686                         LOGI("\tbNumInterfaces=%d", config.bNumInterfaces);
687                         LOGI("\tbConfigurationValue=%d", config.bConfigurationValue);
688                         LOGI("\tiConfiguration=%d", config.iConfiguration);
689                         LOGI("\tbmAttributes=%d", config.bmAttributes);
690                         LOGI("\tMaxPower=%d", config.MaxPower);
691                         LOGI("\textra_length=%d", config.bLength - LIBUSB_DT_CONFIG_SIZE);
692                         break;
693                 case LIBUSB_DT_STRING:
694                         LOGI("LIBUSB_DT_STRING(0x%02x),length=%d", header.bDescriptorType, header.bLength);
695                         break;
696                 case LIBUSB_DT_INTERFACE:
697                         usbi_parse_descriptor(buffer + i, "bbbbbbbbb", &interface, 0);
698                         LOGI("LIBUSB_DT_INTERFACE(0x%02x):", header.bDescriptorType);
699                         LOGI("\tbLength=%d", interface.bLength);
700                         LOGI("\tbDescriptorType=0x%02x", interface.bDescriptorType);
701                         LOGI("\tbInterfaceNumber=%d", interface.bInterfaceNumber);
702                         LOGI("\tbAlternateSetting=%d", interface.bAlternateSetting);
703                         LOGI("\tbNumEndpoints=%d", interface.bNumEndpoints);
704                         LOGI("\tbInterfaceClass=0x%02x", interface.bInterfaceClass);
705                         LOGI("\tbInterfaceSubClass=0x%02x", interface.bInterfaceSubClass);
706                         LOGI("\tbInterfaceProtocol=0x%02x", interface.bInterfaceProtocol);
707                         LOGI("\tiInterface=%d", interface.iInterface);
708                         LOGI("\textra_length=%d", interface.bLength - LIBUSB_DT_INTERFACE_SIZE);
709                         break;
710                 case LIBUSB_DT_ENDPOINT:
711                         usbi_parse_descriptor(buffer + i, "bbbbwbbb", &endpoint, 0);
712                         LOGI("LIBUSB_DT_ENDPOINT(0x%02x):", header.bDescriptorType);
713                         LOGI("\tbLength=%d", endpoint.bLength);
714                         LOGI("\tbDescriptorType=0x%02x", endpoint.bDescriptorType);
715                         LOGI("\tbEndpointAddress=%d", endpoint.bEndpointAddress);
716                         LOGI("\tbmAttributes=%d", endpoint.bmAttributes);
717                         LOGI("\twMaxPacketSize=%d", endpoint.wMaxPacketSize);
718                         LOGI("\tbInterval=%d", endpoint.bInterval);
719                         LOGI("\tbRefresh=%d", endpoint.bRefresh);
720                         LOGI("\tbSynchAddress=%d", endpoint.bSynchAddress);
721                         LOGI("\textra_length=%d", endpoint.bLength - LIBUSB_DT_ENDPOINT_SIZE);
722                         break;
723                 case LIBUSB_DT_DEVICE_QUALIFIER:
724                         LOGI("LIBUSB_DT_DEVICE_QUALIFIER(0x%02x),length=%d", header.bDescriptorType, header.bLength);
725                         LOGI("\textra_length=%d", header.bLength - LIBUSB_DT_QUALIFER_SIZE);
726                         break;
727                 case LIBUSB_DT_OTHER_SPEED_CONFIGURATION:
728                         LOGI("LIBUSB_DT_OTHER_SPEED_CONFIGURATION(0x%02x),length=%d", header.bDescriptorType, header.bLength);
729                         LOGI("\textra_length=%d", header.bLength - LIBUSB_DT_OTHER_SPEED_SIZE);
730                         break;
731                 case LIBUSB_DT_INTERFACE_POWER:
732                         LOGI("LIBUSB_DT_INTERFACE_POWER(0x%02x),length=%d", header.bDescriptorType, header.bLength);
733                         break;
734                 case LIBUSB_DT_OTG:
735                         LOGI("LIBUSB_DT_OTG(0x%02x),length=%d", header.bDescriptorType, header.bLength);
736                         break;
737                 case LIBUSB_DT_DEBUG:
738                         LOGI("LIBUSB_DT_DEBUG(0x%02x),length=%d", header.bDescriptorType, header.bLength);
739                         break;
740                 case LIBUSB_DT_ASSOCIATION:
741                         LOGI("LIBUSB_DT_ASSOCIATION(0x%02x),length=%d", header.bDescriptorType, header.bLength);
742                         LOGI("\textra_length=%d", header.bLength - LIBUSB_DT_ASSOCIATION_SIZE);
743                         break;
744                 case LIBUSB_DT_BOS:
745                         LOGI("LIBUSB_DT_BOS(0x%02x),length=%d", header.bDescriptorType, header.bLength);
746                         LOGI("\textra_length=%d", header.bLength - LIBUSB_DT_BOS_SIZE);
747                         break;
748                 case LIBUSB_DT_DEVICE_CAPABILITY:
749                         LOGI("LIBUSB_DT_DEVICE_CAPABILITY(0x%02x),length=%d", header.bDescriptorType, header.bLength);
750                         LOGI("\textra_length=%d", header.bLength - LIBUSB_DT_DEVICE_CAPABILITY_SIZE);
751                         break;
752                 case LIBUSB_DT_HID:
753                         LOGI("LIBUSB_DT_HID(0x%02x),length=%d", header.bDescriptorType, header.bLength);
754                         break;
755                 case LIBUSB_DT_HID_REPORT:
756                         LOGI("LIBUSB_DT_REPORT(0x%02x),length=%d", header.bDescriptorType, header.bLength);
757                         break;
758                 case LIBUSB_DT_HID_PHYSICAL:
759                         LOGI("LIBUSB_DT_PHYSICAL(0x%02x),length=%d", header.bDescriptorType, header.bLength);
760                         break;
761                 case LIBUSB_DT_CS_INTERFACE:
762                         LOGI("LIBUSB_DT_CS_INTERFACE(0x%02x),length=%d", header.bDescriptorType, header.bLength);
763                         break;
764                 case LIBUSB_DT_CS_ENDPOINT:
765                         LOGI("LIBUSB_DT_CS_ENDPOINT(0x%02x),length=%d", header.bDescriptorType, header.bLength);
766                         break;
767                 case LIBUSB_DT_HUB:
768                         LOGI("LIBUSB_DT_HUB(0x%02x),length=%d", header.bDescriptorType, header.bLength);
769                         break;
770                 case LIBUSB_DT_SUPERSPEED_HUB:
771                         LOGI("LIBUSB_DT_SUPERSPEED_HUB(0x%02x),length=%d", header.bDescriptorType, header.bLength);
772                         break;
773                 case LIBUSB_DT_SS_ENDPOINT_COMPANION:
774                         LOGI("LIBUSB_DT_SS_ENDPOINT_COMPANION(0x%02x),length=%d", header.bDescriptorType, header.bLength);
775                         break;
776                 default:
777                         LOGI("unknown Descriptor(0x%02x),length=0x%02x", header.bDescriptorType, header.bLength);
778                         break;
779                 }
780         }
781 }
782 #endif
783
784
785 static int raw_desc_to_config(struct libusb_context *ctx,
786         unsigned char *buf, int size, int host_endian,
787         struct libusb_config_descriptor **config)
788 {
789         ENTER();
790
791         struct libusb_config_descriptor *_config = malloc(sizeof(*_config));
792         int r;
793         
794         if UNLIKELY(!_config)
795                 RETURN(LIBUSB_ERROR_NO_MEM, int);
796
797 #if PRINT_DIAG
798         dump_descriptors(buf, size);
799 #endif
800         r = parse_configuration(ctx, _config, buf, size, host_endian);
801         if UNLIKELY(r < 0) {
802                 usbi_err(ctx, "parse_configuration failed with error %d", r);
803                 free(_config);
804                 return r;
805         } else if (r > 0) {
806                 usbi_warn(ctx, "still %d bytes of descriptor data left", r);
807         }
808         
809         *config = _config;
810         RETURN(LIBUSB_SUCCESS, int);
811 }
812
813 int usbi_device_cache_descriptor(libusb_device *dev)
814 {
815         int r, host_endian = 0;
816
817         r = usbi_backend->get_device_descriptor(dev, (unsigned char *) &dev->device_descriptor,
818                                                 &host_endian);
819         if UNLIKELY(r < 0)
820                 return r;
821
822         if (!host_endian) {
823                 dev->device_descriptor.bcdUSB = libusb_le16_to_cpu(dev->device_descriptor.bcdUSB);
824                 dev->device_descriptor.idVendor = libusb_le16_to_cpu(dev->device_descriptor.idVendor);
825                 dev->device_descriptor.idProduct = libusb_le16_to_cpu(dev->device_descriptor.idProduct);
826                 dev->device_descriptor.bcdDevice = libusb_le16_to_cpu(dev->device_descriptor.bcdDevice);
827         }
828
829         return LIBUSB_SUCCESS;
830 }
831
832 int API_EXPORTED libusb_get_raw_descriptor(libusb_device *dev,
833                 unsigned char **buffer, int *descriptors_len, int *host_endian)
834 {
835         if UNLIKELY(!buffer || !descriptors_len || !host_endian)
836                 return LIBUSB_ERROR_INVALID_PARAM;
837
838         int len, r;
839         r = usbi_backend->get_raw_descriptor(dev, NULL, &len, host_endian);
840         if (!r) {
841                 unsigned char *temp = realloc(*buffer, len);
842                 if UNLIKELY(!temp)
843                         return LIBUSB_ERROR_NO_MEM;
844                 *buffer = temp;
845                 *descriptors_len = len;
846                 r = usbi_backend->get_raw_descriptor(dev, temp, &len, host_endian);
847         }
848         return r;
849 }
850
851 /** \ingroup desc
852  * Get the USB device descriptor for a given device.
853  *
854  * This is a non-blocking function; the device descriptor is cached in memory.
855  *
856  * Note since libusb-1.0.16, \ref LIBUSB_API_VERSION >= 0x01000102, this
857  * function always succeeds.
858  *
859  * \param dev the device
860  * \param desc output location for the descriptor data
861  * \returns 0 on success or a LIBUSB_ERROR code on failure
862  */
863 int API_EXPORTED libusb_get_device_descriptor(libusb_device *dev,
864         struct libusb_device_descriptor *desc)
865 {
866         usbi_dbg("");
867         // FIXME add IAD support
868         LOGD("desc=%p,dev=%p,device_descriptor=%p", desc, dev, &dev->device_descriptor);
869         memcpy((unsigned char *) desc, (unsigned char *) &dev->device_descriptor,
870                sizeof (dev->device_descriptor));
871         return 0;
872 }
873
874 /** \ingroup desc
875  * Get the USB configuration descriptor for the currently active configuration.
876  * This is a non-blocking function which does not involve any requests being
877  * sent to the device.
878  *
879  * \param dev a device
880  * \param config output location for the USB configuration descriptor. Only
881  * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
882  * after use.
883  * \returns 0 on success
884  * \returns LIBUSB_ERROR_NOT_FOUND if the device is in unconfigured state
885  * \returns another LIBUSB_ERROR code on error
886  * \see libusb_get_config_descriptor
887  */
888 int API_EXPORTED libusb_get_active_config_descriptor(libusb_device *dev,
889         struct libusb_config_descriptor **config)
890 {
891         struct libusb_config_descriptor _config;
892         unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
893         unsigned char *buf = NULL;
894         int host_endian = 0;
895         int r;
896
897         r = usbi_backend->get_active_config_descriptor(dev, tmp,        // XXX this function will return error on some buggy device
898                 LIBUSB_DT_CONFIG_SIZE, &host_endian);
899         if UNLIKELY(r < 0)
900                 return r;
901         if UNLIKELY(r < LIBUSB_DT_CONFIG_SIZE) {
902                 usbi_err(dev->ctx, "short config descriptor read %d/%d",
903                          r, LIBUSB_DT_CONFIG_SIZE);
904                 return LIBUSB_ERROR_IO;
905         }
906
907         usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
908         buf = malloc(_config.wTotalLength);
909         if UNLIKELY(!buf)
910                 return LIBUSB_ERROR_NO_MEM;
911
912         r = usbi_backend->get_active_config_descriptor(dev, buf,        // XXX this function will return error on some buggy device
913                 _config.wTotalLength, &host_endian);
914         if (r >= 0)
915                 r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
916
917         free(buf);
918         return r;
919 }
920
921 /** \ingroup desc
922  * Get a USB configuration descriptor based on its index.
923  * This is a non-blocking function which does not involve any requests being
924  * sent to the device.
925  *
926  * \param dev a device
927  * \param config_index the index of the configuration you wish to retrieve
928  * \param config output location for the USB configuration descriptor. Only
929  * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
930  * after use.
931  * \returns 0 on success
932  * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
933  * \returns another LIBUSB_ERROR code on error
934  * \see libusb_get_active_config_descriptor()
935  * \see libusb_get_config_descriptor_by_value()
936  */
937 int API_EXPORTED libusb_get_config_descriptor(libusb_device *dev,
938         uint8_t config_index, struct libusb_config_descriptor **config)
939 {
940         struct libusb_config_descriptor _config;
941         unsigned char tmp[LIBUSB_DT_CONFIG_SIZE];
942         unsigned char *buf = NULL;
943         int host_endian = 0;
944         int r;
945
946         usbi_dbg("index %d", config_index);
947         if UNLIKELY(config_index >= dev->num_configurations)
948                 return LIBUSB_ERROR_NOT_FOUND;
949
950         r = usbi_backend->get_config_descriptor(dev, config_index, tmp,
951                 LIBUSB_DT_CONFIG_SIZE, &host_endian);
952         if UNLIKELY(r < 0)
953                 return r;
954         if UNLIKELY(r < LIBUSB_DT_CONFIG_SIZE) {
955                 usbi_err(dev->ctx, "short config descriptor read %d/%d",
956                          r, LIBUSB_DT_CONFIG_SIZE);
957                 return LIBUSB_ERROR_IO;
958         }
959
960         usbi_parse_descriptor(tmp, "bbw", &_config, host_endian);
961         buf = malloc(_config.wTotalLength);
962         if UNLIKELY(!buf)
963                 return LIBUSB_ERROR_NO_MEM;
964
965         r = usbi_backend->get_config_descriptor(dev, config_index, buf,
966                 _config.wTotalLength, &host_endian);
967         if LIKELY(r >= 0)
968                 r = raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
969
970         free(buf);
971         return r;
972 }
973
974 /* iterate through all configurations, returning the index of the configuration
975  * matching a specific bConfigurationValue in the idx output parameter, or -1
976  * if the config was not found.
977  * returns 0 on success or a LIBUSB_ERROR code
978  */
979 int usbi_get_config_index_by_value(struct libusb_device *dev,
980         uint8_t bConfigurationValue, int *idx)
981 {
982         uint8_t i;
983
984         usbi_dbg("value %d", bConfigurationValue);
985         for (i = 0; i < dev->num_configurations; i++) {
986                 unsigned char tmp[6];
987                 int host_endian;
988                 int r = usbi_backend->get_config_descriptor(dev, i, tmp, sizeof(tmp),
989                         &host_endian);
990                 if UNLIKELY(r < 0) {
991                         *idx = -1;
992                         return r;
993                 }
994                 if (tmp[5] == bConfigurationValue) {
995                         *idx = i;
996                         return 0;
997                 }
998         }
999
1000         *idx = -1;
1001         return 0;
1002 }
1003
1004 /** \ingroup desc
1005  * Get a USB configuration descriptor with a specific bConfigurationValue.
1006  * This is a non-blocking function which does not involve any requests being
1007  * sent to the device.
1008  *
1009  * \param dev a device
1010  * \param bConfigurationValue the bConfigurationValue of the configuration you
1011  * wish to retrieve
1012  * \param config output location for the USB configuration descriptor. Only
1013  * valid if 0 was returned. Must be freed with libusb_free_config_descriptor()
1014  * after use.
1015  * \returns 0 on success
1016  * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
1017  * \returns another LIBUSB_ERROR code on error
1018  * \see libusb_get_active_config_descriptor()
1019  * \see libusb_get_config_descriptor()
1020  */
1021 int API_EXPORTED libusb_get_config_descriptor_by_value(libusb_device *dev,
1022         uint8_t bConfigurationValue, struct libusb_config_descriptor **config)
1023 {
1024         int r, idx, host_endian;
1025         unsigned char *buf = NULL;
1026
1027         if (usbi_backend->get_config_descriptor_by_value) {
1028                 r = usbi_backend->get_config_descriptor_by_value(dev,
1029                         bConfigurationValue, &buf, &host_endian);
1030                 if UNLIKELY(r < 0)
1031                         return r;
1032                 return raw_desc_to_config(dev->ctx, buf, r, host_endian, config);
1033         }
1034
1035         r = usbi_get_config_index_by_value(dev, bConfigurationValue, &idx);
1036         if UNLIKELY(r < 0)
1037                 return r;
1038         else if UNLIKELY(idx == -1)
1039                 return LIBUSB_ERROR_NOT_FOUND;
1040         else
1041                 return libusb_get_config_descriptor(dev, (uint8_t) idx, config);
1042 }
1043
1044 /** \ingroup desc
1045  * Free a configuration descriptor obtained from
1046  * libusb_get_active_config_descriptor() or libusb_get_config_descriptor().
1047  * It is safe to call this function with a NULL config parameter, in which
1048  * case the function simply returns.
1049  *
1050  * \param config the configuration descriptor to free
1051  */
1052 void API_EXPORTED libusb_free_config_descriptor(
1053         struct libusb_config_descriptor *config)
1054 {
1055         if UNLIKELY(!config)
1056                 return;
1057
1058         clear_configuration(config);
1059         free(config);
1060 }
1061
1062 /** \ingroup desc
1063  * Get an endpoints superspeed endpoint companion descriptor (if any)
1064  *
1065  * \param ctx the context to operate on, or NULL for the default context
1066  * \param endpoint endpoint descriptor from which to get the superspeed
1067  * endpoint companion descriptor
1068  * \param ep_comp output location for the superspeed endpoint companion
1069  * descriptor. Only valid if 0 was returned. Must be freed with
1070  * libusb_free_ss_endpoint_companion_descriptor() after use.
1071  * \returns 0 on success
1072  * \returns LIBUSB_ERROR_NOT_FOUND if the configuration does not exist
1073  * \returns another LIBUSB_ERROR code on error
1074  */
1075 int API_EXPORTED libusb_get_ss_endpoint_companion_descriptor(
1076         struct libusb_context *ctx,
1077         const struct libusb_endpoint_descriptor *endpoint,
1078         struct libusb_ss_endpoint_companion_descriptor **ep_comp)
1079 {
1080         struct usb_descriptor_header header;
1081         int size = endpoint->extra_length;
1082         const unsigned char *buffer = endpoint->extra;
1083
1084         *ep_comp = NULL;
1085
1086         while (size >= LIBUSB_DT_HEADER_SIZE/*DESC_HEADER_LENGTH*/) {
1087                 usbi_parse_descriptor(buffer, "bb", &header, 0);
1088                 if UNLIKELY(header.bLength < 2 || header.bLength > size) {
1089                         usbi_err(ctx, "invalid descriptor length %d",
1090                                  header.bLength);
1091                         return LIBUSB_ERROR_IO;
1092                 }
1093                 if (header.bDescriptorType != LIBUSB_DT_SS_ENDPOINT_COMPANION) {
1094                         buffer += header.bLength;
1095                         size -= header.bLength;
1096                         continue;
1097                 }
1098                 if UNLIKELY(header.bLength < LIBUSB_DT_SS_ENDPOINT_COMPANION_SIZE) {
1099                         usbi_err(ctx, "invalid ss-ep-comp-desc length %d",
1100                                  header.bLength);
1101                         return LIBUSB_ERROR_IO;
1102                 }
1103                 *ep_comp = malloc(sizeof(**ep_comp));
1104                 if UNLIKELY(!*ep_comp)
1105                         return LIBUSB_ERROR_NO_MEM;
1106                 usbi_parse_descriptor(buffer, "bbbbw", *ep_comp, 0);
1107                 return LIBUSB_SUCCESS;
1108         }
1109         return LIBUSB_ERROR_NOT_FOUND;
1110 }
1111
1112 /** \ingroup desc
1113  * Free a superspeed endpoint companion descriptor obtained from
1114  * libusb_get_ss_endpoint_companion_descriptor().
1115  * It is safe to call this function with a NULL ep_comp parameter, in which
1116  * case the function simply returns.
1117  *
1118  * \param ep_comp the superspeed endpoint companion descriptor to free
1119  */
1120 void API_EXPORTED libusb_free_ss_endpoint_companion_descriptor(
1121         struct libusb_ss_endpoint_companion_descriptor *ep_comp)
1122 {
1123         free(ep_comp);
1124 }
1125
1126 static int parse_bos(struct libusb_context *ctx,
1127         struct libusb_bos_descriptor **bos,
1128         unsigned char *buffer, int size, int host_endian)
1129 {
1130         struct libusb_bos_descriptor bos_header, *_bos;
1131         struct libusb_bos_dev_capability_descriptor dev_cap;
1132         int i;
1133
1134         if UNLIKELY(size < LIBUSB_DT_BOS_SIZE) {
1135                 usbi_err(ctx, "short bos descriptor read %d/%d",
1136                          size, LIBUSB_DT_BOS_SIZE);
1137                 return LIBUSB_ERROR_IO;
1138         }
1139
1140         usbi_parse_descriptor(buffer, "bbwb", &bos_header, host_endian);
1141         if UNLIKELY(bos_header.bDescriptorType != LIBUSB_DT_BOS) {
1142                 usbi_err(ctx, "unexpected descriptor %x (expected %x)",
1143                          bos_header.bDescriptorType, LIBUSB_DT_BOS);
1144                 return LIBUSB_ERROR_IO;
1145         }
1146         if UNLIKELY(bos_header.bLength < LIBUSB_DT_BOS_SIZE) {
1147                 usbi_err(ctx, "invalid bos bLength (%d)", bos_header.bLength);
1148                 return LIBUSB_ERROR_IO;
1149         }
1150         if UNLIKELY(bos_header.bLength > size) {
1151                 usbi_err(ctx, "short bos descriptor read %d/%d",
1152                          size, bos_header.bLength);
1153                 return LIBUSB_ERROR_IO;
1154         }
1155
1156         _bos = calloc (1,
1157                 sizeof(*_bos) + bos_header.bNumDeviceCaps * sizeof(void *));
1158         if UNLIKELY(!_bos)
1159                 return LIBUSB_ERROR_NO_MEM;
1160
1161         usbi_parse_descriptor(buffer, "bbwb", _bos, host_endian);
1162         buffer += bos_header.bLength;
1163         size -= bos_header.bLength;
1164
1165         /* Get the device capability descriptors */
1166         for (i = 0; i < bos_header.bNumDeviceCaps; i++) {
1167                 if (size < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
1168                         usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
1169                                   size, LIBUSB_DT_DEVICE_CAPABILITY_SIZE);
1170                         break;
1171                 }
1172                 usbi_parse_descriptor(buffer, "bbb", &dev_cap, host_endian);
1173                 if (dev_cap.bDescriptorType != LIBUSB_DT_DEVICE_CAPABILITY) {
1174                         usbi_warn(ctx, "unexpected descriptor %x (expected %x)",
1175                                   dev_cap.bDescriptorType, LIBUSB_DT_DEVICE_CAPABILITY);
1176                         break;
1177                 }
1178                 if UNLIKELY(dev_cap.bLength < LIBUSB_DT_DEVICE_CAPABILITY_SIZE) {
1179                         usbi_err(ctx, "invalid dev-cap bLength (%d)",
1180                                  dev_cap.bLength);
1181                         libusb_free_bos_descriptor(_bos);
1182                         return LIBUSB_ERROR_IO;
1183                 }
1184                 if (dev_cap.bLength > size) {
1185                         usbi_warn(ctx, "short dev-cap descriptor read %d/%d",
1186                                   size, dev_cap.bLength);
1187                         break;
1188                 }
1189
1190                 _bos->dev_capability[i] = malloc(dev_cap.bLength);
1191                 if UNLIKELY(!_bos->dev_capability[i]) {
1192                         libusb_free_bos_descriptor(_bos);
1193                         return LIBUSB_ERROR_NO_MEM;
1194                 }
1195                 memcpy(_bos->dev_capability[i], buffer, dev_cap.bLength);
1196                 buffer += dev_cap.bLength;
1197                 size -= dev_cap.bLength;
1198         }
1199         _bos->bNumDeviceCaps = (uint8_t)i;
1200         *bos = _bos;
1201
1202         return LIBUSB_SUCCESS;
1203 }
1204
1205 /** \ingroup desc
1206  * Get a Binary Object Store (BOS) descriptor
1207  * This is a BLOCKING function, which will send requests to the device.
1208  *
1209  * \param handle the handle of an open libusb device
1210  * \param bos output location for the BOS descriptor. Only valid if 0 was returned.
1211  * Must be freed with \ref libusb_free_bos_descriptor() after use.
1212  * \returns 0 on success
1213  * \returns LIBUSB_ERROR_NOT_FOUND if the device doesn't have a BOS descriptor
1214  * \returns another LIBUSB_ERROR code on error
1215  */
1216 int API_EXPORTED libusb_get_bos_descriptor(libusb_device_handle *handle,
1217         struct libusb_bos_descriptor **bos)
1218 {
1219         struct libusb_bos_descriptor _bos;
1220         uint8_t bos_header[LIBUSB_DT_BOS_SIZE] = {0};
1221         unsigned char *bos_data = NULL;
1222         const int host_endian = 0;
1223         int r;
1224
1225         /* Read the BOS. This generates 2 requests on the bus,
1226          * one for the header, and one for the full BOS */
1227         r = libusb_get_descriptor(handle, LIBUSB_DT_BOS, 0, bos_header,
1228                                   LIBUSB_DT_BOS_SIZE);
1229         if UNLIKELY(r < 0) {
1230                 if (r != LIBUSB_ERROR_PIPE)
1231                         usbi_err(handle->dev->ctx, "failed to read BOS (%d)", r);
1232                 return r;
1233         }
1234         if UNLIKELY(r < LIBUSB_DT_BOS_SIZE) {
1235                 usbi_err(handle->dev->ctx, "short BOS read %d/%d",
1236                          r, LIBUSB_DT_BOS_SIZE);
1237                 return LIBUSB_ERROR_IO;
1238         }
1239
1240         usbi_parse_descriptor(bos_header, "bbwb", &_bos, host_endian);
1241         usbi_dbg("found BOS descriptor: size %d bytes, %d capabilities",
1242                  _bos.wTotalLength, _bos.bNumDeviceCaps);
1243         bos_data = calloc(_bos.wTotalLength, 1);
1244         if UNLIKELY(!bos_data)
1245                 return LIBUSB_ERROR_NO_MEM;
1246
1247         r = libusb_get_descriptor(handle, LIBUSB_DT_BOS, 0, bos_data,
1248                                   _bos.wTotalLength);
1249         if LIKELY(r >= 0)
1250                 r = parse_bos(handle->dev->ctx, bos, bos_data, r, host_endian);
1251         else
1252                 usbi_err(handle->dev->ctx, "failed to read BOS (%d)", r);
1253
1254         free(bos_data);
1255         return r;
1256 }
1257
1258 /** \ingroup desc
1259  * Free a BOS descriptor obtained from libusb_get_bos_descriptor().
1260  * It is safe to call this function with a NULL bos parameter, in which
1261  * case the function simply returns.
1262  *
1263  * \param bos the BOS descriptor to free
1264  */
1265 void API_EXPORTED libusb_free_bos_descriptor(struct libusb_bos_descriptor *bos)
1266 {
1267         int i;
1268
1269         if (!bos)
1270                 return;
1271
1272         for (i = 0; i < bos->bNumDeviceCaps; i++)
1273                 free(bos->dev_capability[i]);
1274         free(bos);
1275 }
1276
1277 /** \ingroup desc
1278  * Get an USB 2.0 Extension descriptor
1279  *
1280  * \param ctx the context to operate on, or NULL for the default context
1281  * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1282  * \ref libusb_capability_type::LIBUSB_BT_USB_2_0_EXTENSION
1283  * LIBUSB_BT_USB_2_0_EXTENSION
1284  * \param usb_2_0_extension output location for the USB 2.0 Extension
1285  * descriptor. Only valid if 0 was returned. Must be freed with
1286  * libusb_free_usb_2_0_extension_descriptor() after use.
1287  * \returns 0 on success
1288  * \returns a LIBUSB_ERROR code on error
1289  */
1290 int API_EXPORTED libusb_get_usb_2_0_extension_descriptor(
1291         struct libusb_context *ctx,
1292         struct libusb_bos_dev_capability_descriptor *dev_cap,
1293         struct libusb_usb_2_0_extension_descriptor **usb_2_0_extension)
1294 {
1295         struct libusb_usb_2_0_extension_descriptor *_usb_2_0_extension;
1296         const int host_endian = 0;
1297
1298         if UNLIKELY(dev_cap->bDevCapabilityType != LIBUSB_BT_USB_2_0_EXTENSION) {
1299                 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
1300                          dev_cap->bDevCapabilityType,
1301                          LIBUSB_BT_USB_2_0_EXTENSION);
1302                 return LIBUSB_ERROR_INVALID_PARAM;
1303         }
1304         if UNLIKELY(dev_cap->bLength < LIBUSB_BT_USB_2_0_EXTENSION_SIZE) {
1305                 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
1306                          dev_cap->bLength, LIBUSB_BT_USB_2_0_EXTENSION_SIZE);
1307                 return LIBUSB_ERROR_IO;
1308         }
1309
1310         _usb_2_0_extension = malloc(sizeof(*_usb_2_0_extension));
1311         if UNLIKELY(!_usb_2_0_extension)
1312                 return LIBUSB_ERROR_NO_MEM;
1313
1314         usbi_parse_descriptor((unsigned char *)dev_cap, "bbbd",
1315                               _usb_2_0_extension, host_endian);
1316
1317         *usb_2_0_extension = _usb_2_0_extension;
1318         return LIBUSB_SUCCESS;
1319 }
1320
1321 /** \ingroup desc
1322  * Free a USB 2.0 Extension descriptor obtained from
1323  * libusb_get_usb_2_0_extension_descriptor().
1324  * It is safe to call this function with a NULL usb_2_0_extension parameter,
1325  * in which case the function simply returns.
1326  *
1327  * \param usb_2_0_extension the USB 2.0 Extension descriptor to free
1328  */
1329 void API_EXPORTED libusb_free_usb_2_0_extension_descriptor(
1330         struct libusb_usb_2_0_extension_descriptor *usb_2_0_extension)
1331 {
1332         free(usb_2_0_extension);
1333 }
1334
1335 /** \ingroup desc
1336  * Get a SuperSpeed USB Device Capability descriptor
1337  *
1338  * \param ctx the context to operate on, or NULL for the default context
1339  * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1340  * \ref libusb_capability_type::LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
1341  * LIBUSB_BT_SS_USB_DEVICE_CAPABILITY
1342  * \param ss_usb_device_cap output location for the SuperSpeed USB Device
1343  * Capability descriptor. Only valid if 0 was returned. Must be freed with
1344  * libusb_free_ss_usb_device_capability_descriptor() after use.
1345  * \returns 0 on success
1346  * \returns a LIBUSB_ERROR code on error
1347  */
1348 int API_EXPORTED libusb_get_ss_usb_device_capability_descriptor(
1349         struct libusb_context *ctx,
1350         struct libusb_bos_dev_capability_descriptor *dev_cap,
1351         struct libusb_ss_usb_device_capability_descriptor **ss_usb_device_cap)
1352 {
1353         struct libusb_ss_usb_device_capability_descriptor *_ss_usb_device_cap;
1354         const int host_endian = 0;
1355
1356         if UNLIKELY(dev_cap->bDevCapabilityType != LIBUSB_BT_SS_USB_DEVICE_CAPABILITY) {
1357                 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
1358                          dev_cap->bDevCapabilityType,
1359                          LIBUSB_BT_SS_USB_DEVICE_CAPABILITY);
1360                 return LIBUSB_ERROR_INVALID_PARAM;
1361         }
1362         if UNLIKELY(dev_cap->bLength < LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE) {
1363                 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
1364                          dev_cap->bLength, LIBUSB_BT_SS_USB_DEVICE_CAPABILITY_SIZE);
1365                 return LIBUSB_ERROR_IO;
1366         }
1367
1368         _ss_usb_device_cap = malloc(sizeof(*_ss_usb_device_cap));
1369         if UNLIKELY(!_ss_usb_device_cap)
1370                 return LIBUSB_ERROR_NO_MEM;
1371
1372         usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbwbbw",
1373                               _ss_usb_device_cap, host_endian);
1374
1375         *ss_usb_device_cap = _ss_usb_device_cap;
1376         return LIBUSB_SUCCESS;
1377 }
1378
1379 /** \ingroup desc
1380  * Free a SuperSpeed USB Device Capability descriptor obtained from
1381  * libusb_get_ss_usb_device_capability_descriptor().
1382  * It is safe to call this function with a NULL ss_usb_device_cap
1383  * parameter, in which case the function simply returns.
1384  *
1385  * \param ss_usb_device_cap the USB 2.0 Extension descriptor to free
1386  */
1387 void API_EXPORTED libusb_free_ss_usb_device_capability_descriptor(
1388         struct libusb_ss_usb_device_capability_descriptor *ss_usb_device_cap)
1389 {
1390         free(ss_usb_device_cap);
1391 }
1392
1393 /** \ingroup desc
1394  * Get a Container ID descriptor
1395  *
1396  * \param ctx the context to operate on, or NULL for the default context
1397  * \param dev_cap Device Capability descriptor with a bDevCapabilityType of
1398  * \ref libusb_capability_type::LIBUSB_BT_CONTAINER_ID
1399  * LIBUSB_BT_CONTAINER_ID
1400  * \param container_id output location for the Container ID descriptor.
1401  * Only valid if 0 was returned. Must be freed with
1402  * libusb_free_container_id_descriptor() after use.
1403  * \returns 0 on success
1404  * \returns a LIBUSB_ERROR code on error
1405  */
1406 int API_EXPORTED libusb_get_container_id_descriptor(struct libusb_context *ctx,
1407         struct libusb_bos_dev_capability_descriptor *dev_cap,
1408         struct libusb_container_id_descriptor **container_id)
1409 {
1410         struct libusb_container_id_descriptor *_container_id;
1411         const int host_endian = 0;
1412
1413         if UNLIKELY(dev_cap->bDevCapabilityType != LIBUSB_BT_CONTAINER_ID) {
1414                 usbi_err(ctx, "unexpected bDevCapabilityType %x (expected %x)",
1415                          dev_cap->bDevCapabilityType,
1416                          LIBUSB_BT_CONTAINER_ID);
1417                 return LIBUSB_ERROR_INVALID_PARAM;
1418         }
1419         if UNLIKELY(dev_cap->bLength < LIBUSB_BT_CONTAINER_ID_SIZE) {
1420                 usbi_err(ctx, "short dev-cap descriptor read %d/%d",
1421                          dev_cap->bLength, LIBUSB_BT_CONTAINER_ID_SIZE);
1422                 return LIBUSB_ERROR_IO;
1423         }
1424
1425         _container_id = malloc(sizeof(*_container_id));
1426         if UNLIKELY(!_container_id)
1427                 return LIBUSB_ERROR_NO_MEM;
1428
1429         usbi_parse_descriptor((unsigned char *)dev_cap, "bbbbu",
1430                               _container_id, host_endian);
1431
1432         *container_id = _container_id;
1433         return LIBUSB_SUCCESS;
1434 }
1435
1436 /** \ingroup desc
1437  * Free a Container ID descriptor obtained from
1438  * libusb_get_container_id_descriptor().
1439  * It is safe to call this function with a NULL container_id parameter,
1440  * in which case the function simply returns.
1441  *
1442  * \param container_id the USB 2.0 Extension descriptor to free
1443  */
1444 void API_EXPORTED libusb_free_container_id_descriptor(
1445         struct libusb_container_id_descriptor *container_id)
1446 {
1447         free(container_id);
1448 }
1449
1450 /** \ingroup desc
1451  * Retrieve a string descriptor in C style ASCII.
1452  *
1453  * Wrapper around libusb_get_string_descriptor(). Uses the first language
1454  * supported by the device.
1455  *
1456  * \param dev a device handle
1457  * \param desc_index the index of the descriptor to retrieve
1458  * \param data output buffer for ASCII string descriptor
1459  * \param length size of data buffer
1460  * \returns number of bytes returned in data, or LIBUSB_ERROR code on failure
1461  */
1462 int API_EXPORTED libusb_get_string_descriptor_ascii(libusb_device_handle *dev,
1463         uint8_t desc_index, unsigned char *data, int length)
1464 {
1465         unsigned char tbuf[255]; /* Some devices choke on size > 255 */
1466         int r, si, di;
1467         uint16_t langid;
1468
1469         /* Asking for the zero'th index is special - it returns a string
1470          * descriptor that contains all the language IDs supported by the
1471          * device. Typically there aren't many - often only one. Language
1472          * IDs are 16 bit numbers, and they start at the third byte in the
1473          * descriptor. There's also no point in trying to read descriptor 0
1474          * with this function. See USB 2.0 specification section 9.6.7 for
1475          * more information.
1476          */
1477
1478         if UNLIKELY(!desc_index)
1479                 return LIBUSB_ERROR_INVALID_PARAM;
1480
1481         r = libusb_get_string_descriptor(dev, 0, 0, tbuf, sizeof(tbuf));
1482         if UNLIKELY(r < 0)
1483                 return r;
1484
1485         if UNLIKELY(r < 4)
1486                 return LIBUSB_ERROR_IO;
1487
1488         langid = tbuf[2] | (tbuf[3] << 8);
1489
1490         r = libusb_get_string_descriptor(dev, desc_index, langid, tbuf, sizeof(tbuf));
1491         if UNLIKELY(r < 0)
1492                 return r;
1493
1494         if UNLIKELY(tbuf[1] != LIBUSB_DT_STRING)
1495                 return LIBUSB_ERROR_IO;
1496
1497         if UNLIKELY(tbuf[0] > r)
1498                 return LIBUSB_ERROR_IO;
1499
1500         for (di = 0, si = 2; si < tbuf[0]; si += 2) {
1501                 if (di >= (length - 1))
1502                         break;
1503
1504                 if ((tbuf[si] & 0x80) || (tbuf[si + 1])) /* non-ASCII */
1505                         data[di++] = '?';
1506                 else
1507                         data[di++] = tbuf[si];
1508         }
1509
1510         data[di] = 0;
1511         return di;
1512 }