3 * Copyright (c) 2008 Hans Petter Selasky. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/queue.h>
34 #include "libusb20_desc.h"
35 #include "libusb20_int.h"
37 static const uint32_t libusb20_me_encode_empty[2]; /* dummy */
39 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_DEVICE_DESC);
40 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_ENDPOINT_DESC);
41 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_INTERFACE_DESC);
42 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_CONFIG_DESC);
43 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_CONTROL_SETUP);
44 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_SS_ENDPT_COMP_DESC);
45 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_USB_20_DEVCAP_DESC);
46 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_SS_USB_DEVCAP_DESC);
47 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_BOS_DESCRIPTOR);
49 /*------------------------------------------------------------------------*
50 * libusb20_parse_config_desc
53 * NULL: Out of memory.
54 * Else: A valid config structure pointer which must be passed to "free()"
55 *------------------------------------------------------------------------*/
56 struct libusb20_config *
57 libusb20_parse_config_desc(const void *config_desc)
59 struct libusb20_config *lub_config;
60 struct libusb20_interface *lub_interface;
61 struct libusb20_interface *lub_alt_interface;
62 struct libusb20_interface *last_if;
63 struct libusb20_endpoint *lub_endpoint;
64 struct libusb20_endpoint *last_ep;
66 struct libusb20_me_struct pcdesc;
69 uint16_t niface_no_alt;
75 if (ptr[1] != LIBUSB20_DT_CONFIG) {
76 return (NULL); /* not config descriptor */
79 * The first "bInterfaceNumber" should never have the value 0xff.
88 /* get "wTotalLength" and setup "pcdesc" */
89 pcdesc.ptr = LIBUSB20_ADD_BYTES(config_desc, 0);
91 ((const uint8_t *)config_desc)[2] |
92 (((const uint8_t *)config_desc)[3] << 8);
93 pcdesc.type = LIBUSB20_ME_IS_RAW;
95 /* descriptor pre-scan */
96 while ((ptr = libusb20_desc_foreach(&pcdesc, ptr))) {
97 if (ptr[1] == LIBUSB20_DT_ENDPOINT) {
99 } else if ((ptr[1] == LIBUSB20_DT_INTERFACE) && (ptr[0] >= 4)) {
101 /* check "bInterfaceNumber" */
102 if (ptr[2] != iface_no) {
109 /* sanity checking */
111 return (NULL); /* corrupt */
113 if (nendpoint >= 256) {
114 return (NULL); /* corrupt */
116 size = sizeof(*lub_config) +
117 (niface * sizeof(*lub_interface)) +
118 (nendpoint * sizeof(*lub_endpoint)) +
121 lub_config = malloc(size);
122 if (lub_config == NULL) {
123 return (NULL); /* out of memory */
125 /* make sure memory is initialised */
126 memset(lub_config, 0, size);
128 lub_interface = (void *)(lub_config + 1);
129 lub_alt_interface = (void *)(lub_interface + niface_no_alt);
130 lub_endpoint = (void *)(lub_interface + niface);
133 * Make a copy of the config descriptor, so that the caller can free
134 * the inital config descriptor pointer!
136 memcpy((void *)(lub_endpoint + nendpoint), config_desc, pcdesc.len);
138 ptr = (const void *)(lub_endpoint + nendpoint);
139 pcdesc.ptr = LIBUSB20_ADD_BYTES(ptr, 0);
141 /* init config structure */
143 LIBUSB20_INIT(LIBUSB20_CONFIG_DESC, &lub_config->desc);
145 if (libusb20_me_decode(ptr, ptr[0], &lub_config->desc)) {
148 lub_config->num_interface = 0;
149 lub_config->interface = lub_interface;
150 lub_config->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
151 lub_config->extra.len = -ptr[0];
152 lub_config->extra.type = LIBUSB20_ME_IS_RAW;
163 /* descriptor pre-scan */
164 while ((ptr = libusb20_desc_foreach(&pcdesc, ptr))) {
165 if (ptr[1] == LIBUSB20_DT_ENDPOINT) {
168 last_ep = lub_endpoint;
169 last_if->num_endpoints++;
171 LIBUSB20_INIT(LIBUSB20_ENDPOINT_DESC, &last_ep->desc);
173 if (libusb20_me_decode(ptr, ptr[0], &last_ep->desc)) {
176 last_ep->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
177 last_ep->extra.len = 0;
178 last_ep->extra.type = LIBUSB20_ME_IS_RAW;
180 lub_config->extra.len += ptr[0];
183 } else if ((ptr[1] == LIBUSB20_DT_INTERFACE) && (ptr[0] >= 4)) {
184 if (ptr[2] != iface_no) {
188 lub_config->num_interface++;
189 last_if = lub_interface;
192 /* one more alternate setting */
193 lub_interface->num_altsetting++;
194 last_if = lub_alt_interface;
198 LIBUSB20_INIT(LIBUSB20_INTERFACE_DESC, &last_if->desc);
200 if (libusb20_me_decode(ptr, ptr[0], &last_if->desc)) {
204 * Sometimes USB devices have corrupt interface
205 * descriptors and we need to overwrite the provided
208 last_if->desc.bInterfaceNumber = niface - 1;
209 last_if->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
210 last_if->extra.len = 0;
211 last_if->extra.type = LIBUSB20_ME_IS_RAW;
212 last_if->endpoints = lub_endpoint + 1;
213 last_if->altsetting = lub_alt_interface;
214 last_if->num_altsetting = 0;
215 last_if->num_endpoints = 0;
218 /* unknown descriptor */
221 last_ep->extra.len += ptr[0];
223 last_if->extra.len += ptr[0];
226 lub_config->extra.len += ptr[0];
233 /*------------------------------------------------------------------------*
234 * libusb20_desc_foreach
236 * Safe traversal of USB descriptors.
239 * NULL: End of descriptors
240 * Else: Pointer to next descriptor
241 *------------------------------------------------------------------------*/
243 libusb20_desc_foreach(const struct libusb20_me_struct *pdesc,
244 const uint8_t *psubdesc)
246 const uint8_t *start;
248 const uint8_t *desc_next;
254 start = (const uint8_t *)pdesc->ptr;
255 end = LIBUSB20_ADD_BYTES(start, pdesc->len);
257 /* get start of next descriptor */
258 if (psubdesc == NULL)
261 psubdesc = psubdesc + psubdesc[0];
263 /* check that the next USB descriptor is within the range */
264 if ((psubdesc < start) || (psubdesc >= end))
265 return (NULL); /* out of range, or EOD */
267 /* check start of the second next USB descriptor, if any */
268 desc_next = psubdesc + psubdesc[0];
269 if ((desc_next < start) || (desc_next > end))
270 return (NULL); /* out of range */
272 /* check minimum descriptor length */
274 return (NULL); /* too short descriptor */
276 return (psubdesc); /* return start of next descriptor */
279 /*------------------------------------------------------------------------*
280 * libusb20_me_get_1 - safety wrapper to read out one byte
281 *------------------------------------------------------------------------*/
283 libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset)
285 if (offset < ie->len) {
286 return (*((uint8_t *)LIBUSB20_ADD_BYTES(ie->ptr, offset)));
291 /*------------------------------------------------------------------------*
292 * libusb20_me_get_2 - safety wrapper to read out one word
293 *------------------------------------------------------------------------*/
295 libusb20_me_get_2(const struct libusb20_me_struct *ie, uint16_t offset)
297 return (libusb20_me_get_1(ie, offset) |
298 (libusb20_me_get_1(ie, offset + 1) << 8));
301 /*------------------------------------------------------------------------*
302 * libusb20_me_encode - encode a message structure
304 * Description of parameters:
305 * "len" - maximum length of output buffer
306 * "ptr" - pointer to output buffer. If NULL, no data will be written
307 * "pd" - source structure
310 * 0..65535 - Number of bytes used, limited by the "len" input parameter.
311 *------------------------------------------------------------------------*/
313 libusb20_me_encode(void *ptr, uint16_t len, const void *pd)
315 const uint8_t *pf; /* pointer to format data */
316 uint8_t *buf; /* pointer to output buffer */
318 uint32_t pd_offset; /* decoded structure offset */
319 uint16_t len_old; /* old length */
320 uint16_t pd_count; /* decoded element count */
321 uint8_t me; /* message element */
327 pd_offset = sizeof(void *);
328 pf = (*((struct libusb20_me_format *const *)pd))->format;
334 /* get information element */
336 me = (pf[0]) & LIBUSB20_ME_MASK;
337 pd_count = pf[1] | (pf[2] << 8);
340 /* encode the message element */
343 case LIBUSB20_ME_INT8:
347 if (len < 1) /* overflow */
350 temp = *((const uint8_t *)
351 LIBUSB20_ADD_BYTES(pd, pd_offset));
360 case LIBUSB20_ME_INT16:
361 pd_offset = -((-pd_offset) & ~1); /* align */
365 if (len < 2) /* overflow */
369 temp = *((const uint16_t *)
370 LIBUSB20_ADD_BYTES(pd, pd_offset));
371 buf[1] = (temp >> 8) & 0xFF;
372 buf[0] = temp & 0xFF;
380 case LIBUSB20_ME_INT32:
381 pd_offset = -((-pd_offset) & ~3); /* align */
385 if (len < 4) /* overflow */
388 temp = *((const uint32_t *)
389 LIBUSB20_ADD_BYTES(pd, pd_offset));
390 buf[3] = (temp >> 24) & 0xFF;
391 buf[2] = (temp >> 16) & 0xFF;
392 buf[1] = (temp >> 8) & 0xFF;
393 buf[0] = temp & 0xFF;
401 case LIBUSB20_ME_INT64:
402 pd_offset = -((-pd_offset) & ~7); /* align */
406 if (len < 8) /* overflow */
410 temp = *((const uint64_t *)
411 LIBUSB20_ADD_BYTES(pd, pd_offset));
412 buf[7] = (temp >> 56) & 0xFF;
413 buf[6] = (temp >> 48) & 0xFF;
414 buf[5] = (temp >> 40) & 0xFF;
415 buf[4] = (temp >> 32) & 0xFF;
416 buf[3] = (temp >> 24) & 0xFF;
417 buf[2] = (temp >> 16) & 0xFF;
418 buf[1] = (temp >> 8) & 0xFF;
419 buf[0] = temp & 0xFF;
427 case LIBUSB20_ME_STRUCT:
428 pd_offset = -((-pd_offset) &
429 ~(LIBUSB20_ME_STRUCT_ALIGN - 1)); /* align */
433 struct libusb20_me_struct *ps;
435 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
438 case LIBUSB20_ME_IS_RAW:
443 case LIBUSB20_ME_IS_ENCODED:
453 src_len = libusb20_me_get_1(pd, 0);
454 src_ptr = LIBUSB20_ADD_BYTES(ps->ptr, 1);
455 if (src_len == 0xFF) {
456 /* length is escaped */
457 src_len = libusb20_me_get_2(pd, 1);
459 LIBUSB20_ADD_BYTES(ps->ptr, 3);
463 case LIBUSB20_ME_IS_DECODED:
464 /* reserve 3 length bytes */
465 src_len = libusb20_me_encode(NULL,
466 0xFFFF - 3, ps->ptr);
470 default: /* empty structure */
476 if (src_len > 0xFE) {
477 if (src_len > (0xFFFF - 3))
481 if (len < (src_len + 3))
487 buf[1] = (src_len & 0xFF);
488 buf[2] = (src_len >> 8) & 0xFF;
491 len -= (src_len + 3);
493 if (len < (src_len + 1))
498 buf[0] = (src_len & 0xFF);
501 len -= (src_len + 1);
504 /* check for buffer and non-zero length */
506 if (buf && src_len) {
507 if (ps->type == LIBUSB20_ME_IS_DECODED) {
510 * procedure - we have
512 * complete structure:
516 dummy = libusb20_me_encode(buf,
517 0xFFFF - 3, ps->ptr);
519 bcopy(src_ptr, buf, src_len);
523 pd_offset += sizeof(struct libusb20_me_struct);
532 return (len_old - len);
535 /*------------------------------------------------------------------------*
536 * libusb20_me_decode - decode a message into a decoded structure
538 * Description of parameters:
539 * "ptr" - message pointer
540 * "len" - message length
541 * "pd" - pointer to decoded structure
544 * "0..65535" - number of bytes decoded, limited by "len"
545 *------------------------------------------------------------------------*/
547 libusb20_me_decode(const void *ptr, uint16_t len, void *pd)
549 const uint8_t *pf; /* pointer to format data */
550 const uint8_t *buf; /* pointer to input buffer */
552 uint32_t pd_offset; /* decoded structure offset */
553 uint16_t len_old; /* old length */
554 uint16_t pd_count; /* decoded element count */
555 uint8_t me; /* message element */
561 pd_offset = sizeof(void *);
562 pf = (*((struct libusb20_me_format **)pd))->format;
568 /* get information element */
570 me = (pf[0]) & LIBUSB20_ME_MASK;
571 pd_count = pf[1] | (pf[2] << 8);
574 /* decode the message element by type */
577 case LIBUSB20_ME_INT8:
589 *((uint8_t *)LIBUSB20_ADD_BYTES(pd,
595 case LIBUSB20_ME_INT16:
596 pd_offset = -((-pd_offset) & ~1); /* align */
609 *((uint16_t *)LIBUSB20_ADD_BYTES(pd,
615 case LIBUSB20_ME_INT32:
616 pd_offset = -((-pd_offset) & ~3); /* align */
626 temp |= buf[2] << 16;
632 *((uint32_t *)LIBUSB20_ADD_BYTES(pd,
638 case LIBUSB20_ME_INT64:
639 pd_offset = -((-pd_offset) & ~7); /* align */
648 temp = ((uint64_t)buf[7]) << 56;
649 temp |= ((uint64_t)buf[6]) << 48;
650 temp |= ((uint64_t)buf[5]) << 40;
651 temp |= ((uint64_t)buf[4]) << 32;
652 temp |= buf[3] << 24;
653 temp |= buf[2] << 16;
659 *((uint64_t *)LIBUSB20_ADD_BYTES(pd,
665 case LIBUSB20_ME_STRUCT:
666 pd_offset = -((-pd_offset) &
667 ~(LIBUSB20_ME_STRUCT_ALIGN - 1)); /* align */
671 struct libusb20_me_struct *ps;
673 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
675 if (ps->type == LIBUSB20_ME_IS_ENCODED) {
677 * Pre-store a de-constified
681 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
684 * Get the correct number of
688 if (buf[0] == 0xFF) {
697 /* get the structure length */
700 if (buf[0] == 0xFF) {
719 /* check for invalid length */
725 /* check wanted structure type */
728 case LIBUSB20_ME_IS_ENCODED:
729 /* check for zero length */
735 ps->ptr = LIBUSB20_ADD_BYTES(
736 libusb20_me_encode_empty, 0);
743 case LIBUSB20_ME_IS_RAW:
744 /* update length and pointer */
746 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
749 case LIBUSB20_ME_IS_EMPTY:
750 case LIBUSB20_ME_IS_DECODED:
751 /* check for non-zero length */
754 ps->type = LIBUSB20_ME_IS_DECODED;
760 dummy = libusb20_me_decode(buf,
764 ps->type = LIBUSB20_ME_IS_EMPTY;
771 * nothing to do - should
780 pd_offset += sizeof(struct libusb20_me_struct);
789 return (len_old - len);