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 #ifdef LIBUSB_GLOBAL_INCLUDE_FILE
28 #include LIBUSB_GLOBAL_INCLUDE_FILE
34 #include <sys/queue.h>
38 #include "libusb20_desc.h"
39 #include "libusb20_int.h"
41 static const uint32_t libusb20_me_encode_empty[2]; /* dummy */
43 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_DEVICE_DESC);
44 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_ENDPOINT_DESC);
45 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_INTERFACE_DESC);
46 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_CONFIG_DESC);
47 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_CONTROL_SETUP);
48 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_SS_ENDPT_COMP_DESC);
49 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_USB_20_DEVCAP_DESC);
50 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_SS_USB_DEVCAP_DESC);
51 LIBUSB20_MAKE_STRUCT_FORMAT(LIBUSB20_BOS_DESCRIPTOR);
53 /*------------------------------------------------------------------------*
54 * libusb20_parse_config_desc
57 * NULL: Out of memory.
58 * Else: A valid config structure pointer which must be passed to "free()"
59 *------------------------------------------------------------------------*/
60 struct libusb20_config *
61 libusb20_parse_config_desc(const void *config_desc)
63 struct libusb20_config *lub_config;
64 struct libusb20_interface *lub_interface;
65 struct libusb20_interface *lub_alt_interface;
66 struct libusb20_interface *last_if;
67 struct libusb20_endpoint *lub_endpoint;
68 struct libusb20_endpoint *last_ep;
70 struct libusb20_me_struct pcdesc;
73 uint16_t niface_no_alt;
79 if (ptr[1] != LIBUSB20_DT_CONFIG) {
80 return (NULL); /* not config descriptor */
83 * The first "bInterfaceNumber" should never have the value 0xff.
92 /* get "wTotalLength" and setup "pcdesc" */
93 pcdesc.ptr = LIBUSB20_ADD_BYTES(config_desc, 0);
95 ((const uint8_t *)config_desc)[2] |
96 (((const uint8_t *)config_desc)[3] << 8);
97 pcdesc.type = LIBUSB20_ME_IS_RAW;
99 /* descriptor pre-scan */
100 while ((ptr = libusb20_desc_foreach(&pcdesc, ptr))) {
101 if (ptr[1] == LIBUSB20_DT_ENDPOINT) {
103 } else if ((ptr[1] == LIBUSB20_DT_INTERFACE) && (ptr[0] >= 4)) {
105 /* check "bInterfaceNumber" */
106 if (ptr[2] != iface_no) {
113 /* sanity checking */
115 return (NULL); /* corrupt */
117 if (nendpoint >= 256) {
118 return (NULL); /* corrupt */
120 size = sizeof(*lub_config) +
121 (niface * sizeof(*lub_interface)) +
122 (nendpoint * sizeof(*lub_endpoint)) +
125 lub_config = malloc(size);
126 if (lub_config == NULL) {
127 return (NULL); /* out of memory */
129 /* make sure memory is initialised */
130 memset(lub_config, 0, size);
132 lub_interface = (void *)(lub_config + 1);
133 lub_alt_interface = (void *)(lub_interface + niface_no_alt);
134 lub_endpoint = (void *)(lub_interface + niface);
137 * Make a copy of the config descriptor, so that the caller can free
138 * the initial config descriptor pointer!
140 memcpy((void *)(lub_endpoint + nendpoint), config_desc, pcdesc.len);
142 ptr = (const void *)(lub_endpoint + nendpoint);
143 pcdesc.ptr = LIBUSB20_ADD_BYTES(ptr, 0);
145 /* init config structure */
147 LIBUSB20_INIT(LIBUSB20_CONFIG_DESC, &lub_config->desc);
149 if (libusb20_me_decode(ptr, ptr[0], &lub_config->desc)) {
152 lub_config->num_interface = 0;
153 lub_config->interface = lub_interface;
154 lub_config->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
155 lub_config->extra.len = -ptr[0];
156 lub_config->extra.type = LIBUSB20_ME_IS_RAW;
167 /* descriptor pre-scan */
168 while ((ptr = libusb20_desc_foreach(&pcdesc, ptr))) {
169 if (ptr[1] == LIBUSB20_DT_ENDPOINT) {
172 last_ep = lub_endpoint;
173 last_if->num_endpoints++;
175 LIBUSB20_INIT(LIBUSB20_ENDPOINT_DESC, &last_ep->desc);
177 if (libusb20_me_decode(ptr, ptr[0], &last_ep->desc)) {
180 last_ep->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
181 last_ep->extra.len = 0;
182 last_ep->extra.type = LIBUSB20_ME_IS_RAW;
184 lub_config->extra.len += ptr[0];
187 } else if ((ptr[1] == LIBUSB20_DT_INTERFACE) && (ptr[0] >= 4)) {
188 if (ptr[2] != iface_no) {
192 lub_config->num_interface++;
193 last_if = lub_interface;
196 /* one more alternate setting */
197 lub_interface->num_altsetting++;
198 last_if = lub_alt_interface;
202 LIBUSB20_INIT(LIBUSB20_INTERFACE_DESC, &last_if->desc);
204 if (libusb20_me_decode(ptr, ptr[0], &last_if->desc)) {
208 * Sometimes USB devices have corrupt interface
209 * descriptors and we need to overwrite the provided
212 last_if->desc.bInterfaceNumber = niface - 1;
213 last_if->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
214 last_if->extra.len = 0;
215 last_if->extra.type = LIBUSB20_ME_IS_RAW;
216 last_if->endpoints = lub_endpoint + 1;
217 last_if->altsetting = lub_alt_interface;
218 last_if->num_altsetting = 0;
219 last_if->num_endpoints = 0;
222 /* unknown descriptor */
225 last_ep->extra.len += ptr[0];
227 last_if->extra.len += ptr[0];
230 lub_config->extra.len += ptr[0];
237 /*------------------------------------------------------------------------*
238 * libusb20_desc_foreach
240 * Safe traversal of USB descriptors.
243 * NULL: End of descriptors
244 * Else: Pointer to next descriptor
245 *------------------------------------------------------------------------*/
247 libusb20_desc_foreach(const struct libusb20_me_struct *pdesc,
248 const uint8_t *psubdesc)
250 const uint8_t *start;
252 const uint8_t *desc_next;
258 start = (const uint8_t *)pdesc->ptr;
259 end = LIBUSB20_ADD_BYTES(start, pdesc->len);
261 /* get start of next descriptor */
262 if (psubdesc == NULL)
265 psubdesc = psubdesc + psubdesc[0];
267 /* check that the next USB descriptor is within the range */
268 if ((psubdesc < start) || (psubdesc >= end))
269 return (NULL); /* out of range, or EOD */
271 /* check start of the second next USB descriptor, if any */
272 desc_next = psubdesc + psubdesc[0];
273 if ((desc_next < start) || (desc_next > end))
274 return (NULL); /* out of range */
276 /* check minimum descriptor length */
278 return (NULL); /* too short descriptor */
280 return (psubdesc); /* return start of next descriptor */
283 /*------------------------------------------------------------------------*
284 * libusb20_me_get_1 - safety wrapper to read out one byte
285 *------------------------------------------------------------------------*/
287 libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset)
289 if (offset < ie->len) {
290 return (*((uint8_t *)LIBUSB20_ADD_BYTES(ie->ptr, offset)));
295 /*------------------------------------------------------------------------*
296 * libusb20_me_get_2 - safety wrapper to read out one word
297 *------------------------------------------------------------------------*/
299 libusb20_me_get_2(const struct libusb20_me_struct *ie, uint16_t offset)
301 return (libusb20_me_get_1(ie, offset) |
302 (libusb20_me_get_1(ie, offset + 1) << 8));
305 /*------------------------------------------------------------------------*
306 * libusb20_me_encode - encode a message structure
308 * Description of parameters:
309 * "len" - maximum length of output buffer
310 * "ptr" - pointer to output buffer. If NULL, no data will be written
311 * "pd" - source structure
314 * 0..65535 - Number of bytes used, limited by the "len" input parameter.
315 *------------------------------------------------------------------------*/
317 libusb20_me_encode(void *ptr, uint16_t len, const void *pd)
319 const uint8_t *pf; /* pointer to format data */
320 uint8_t *buf; /* pointer to output buffer */
322 uint32_t pd_offset; /* decoded structure offset */
323 uint16_t len_old; /* old length */
324 uint16_t pd_count; /* decoded element count */
325 uint8_t me; /* message element */
331 pd_offset = sizeof(void *);
332 pf = (*((struct libusb20_me_format *const *)pd))->format;
338 /* get information element */
340 me = (pf[0]) & LIBUSB20_ME_MASK;
341 pd_count = pf[1] | (pf[2] << 8);
344 /* encode the message element */
347 case LIBUSB20_ME_INT8:
351 if (len < 1) /* overflow */
354 temp = *((const uint8_t *)
355 LIBUSB20_ADD_BYTES(pd, pd_offset));
364 case LIBUSB20_ME_INT16:
365 pd_offset = -((-pd_offset) & ~1); /* align */
369 if (len < 2) /* overflow */
373 temp = *((const uint16_t *)
374 LIBUSB20_ADD_BYTES(pd, pd_offset));
375 buf[1] = (temp >> 8) & 0xFF;
376 buf[0] = temp & 0xFF;
384 case LIBUSB20_ME_INT32:
385 pd_offset = -((-pd_offset) & ~3); /* align */
389 if (len < 4) /* overflow */
392 temp = *((const uint32_t *)
393 LIBUSB20_ADD_BYTES(pd, pd_offset));
394 buf[3] = (temp >> 24) & 0xFF;
395 buf[2] = (temp >> 16) & 0xFF;
396 buf[1] = (temp >> 8) & 0xFF;
397 buf[0] = temp & 0xFF;
405 case LIBUSB20_ME_INT64:
406 pd_offset = -((-pd_offset) & ~7); /* align */
410 if (len < 8) /* overflow */
414 temp = *((const uint64_t *)
415 LIBUSB20_ADD_BYTES(pd, pd_offset));
416 buf[7] = (temp >> 56) & 0xFF;
417 buf[6] = (temp >> 48) & 0xFF;
418 buf[5] = (temp >> 40) & 0xFF;
419 buf[4] = (temp >> 32) & 0xFF;
420 buf[3] = (temp >> 24) & 0xFF;
421 buf[2] = (temp >> 16) & 0xFF;
422 buf[1] = (temp >> 8) & 0xFF;
423 buf[0] = temp & 0xFF;
431 case LIBUSB20_ME_STRUCT:
432 pd_offset = -((-pd_offset) &
433 ~(LIBUSB20_ME_STRUCT_ALIGN - 1)); /* align */
437 struct libusb20_me_struct *ps;
439 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
442 case LIBUSB20_ME_IS_RAW:
447 case LIBUSB20_ME_IS_ENCODED:
457 src_len = libusb20_me_get_1(pd, 0);
458 src_ptr = LIBUSB20_ADD_BYTES(ps->ptr, 1);
459 if (src_len == 0xFF) {
460 /* length is escaped */
461 src_len = libusb20_me_get_2(pd, 1);
463 LIBUSB20_ADD_BYTES(ps->ptr, 3);
467 case LIBUSB20_ME_IS_DECODED:
468 /* reserve 3 length bytes */
469 src_len = libusb20_me_encode(NULL,
470 0xFFFF - 3, ps->ptr);
474 default: /* empty structure */
480 if (src_len > 0xFE) {
481 if (src_len > (0xFFFF - 3))
485 if (len < (src_len + 3))
491 buf[1] = (src_len & 0xFF);
492 buf[2] = (src_len >> 8) & 0xFF;
495 len -= (src_len + 3);
497 if (len < (src_len + 1))
502 buf[0] = (src_len & 0xFF);
505 len -= (src_len + 1);
508 /* check for buffer and non-zero length */
510 if (buf && src_len) {
511 if (ps->type == LIBUSB20_ME_IS_DECODED) {
514 * procedure - we have
516 * complete structure:
518 (void) libusb20_me_encode(buf,
519 0xFFFF - 3, ps->ptr);
521 bcopy(src_ptr, buf, src_len);
525 pd_offset += sizeof(struct libusb20_me_struct);
534 return (len_old - len);
537 /*------------------------------------------------------------------------*
538 * libusb20_me_decode - decode a message into a decoded structure
540 * Description of parameters:
541 * "ptr" - message pointer
542 * "len" - message length
543 * "pd" - pointer to decoded structure
546 * "0..65535" - number of bytes decoded, limited by "len"
547 *------------------------------------------------------------------------*/
549 libusb20_me_decode(const void *ptr, uint16_t len, void *pd)
551 const uint8_t *pf; /* pointer to format data */
552 const uint8_t *buf; /* pointer to input buffer */
554 uint32_t pd_offset; /* decoded structure offset */
555 uint16_t len_old; /* old length */
556 uint16_t pd_count; /* decoded element count */
557 uint8_t me; /* message element */
563 pd_offset = sizeof(void *);
564 pf = (*((struct libusb20_me_format **)pd))->format;
570 /* get information element */
572 me = (pf[0]) & LIBUSB20_ME_MASK;
573 pd_count = pf[1] | (pf[2] << 8);
576 /* decode the message element by type */
579 case LIBUSB20_ME_INT8:
591 *((uint8_t *)LIBUSB20_ADD_BYTES(pd,
597 case LIBUSB20_ME_INT16:
598 pd_offset = -((-pd_offset) & ~1); /* align */
611 *((uint16_t *)LIBUSB20_ADD_BYTES(pd,
617 case LIBUSB20_ME_INT32:
618 pd_offset = -((-pd_offset) & ~3); /* align */
628 temp |= buf[2] << 16;
634 *((uint32_t *)LIBUSB20_ADD_BYTES(pd,
640 case LIBUSB20_ME_INT64:
641 pd_offset = -((-pd_offset) & ~7); /* align */
650 temp = ((uint64_t)buf[7]) << 56;
651 temp |= ((uint64_t)buf[6]) << 48;
652 temp |= ((uint64_t)buf[5]) << 40;
653 temp |= ((uint64_t)buf[4]) << 32;
654 temp |= buf[3] << 24;
655 temp |= buf[2] << 16;
661 *((uint64_t *)LIBUSB20_ADD_BYTES(pd,
667 case LIBUSB20_ME_STRUCT:
668 pd_offset = -((-pd_offset) &
669 ~(LIBUSB20_ME_STRUCT_ALIGN - 1)); /* align */
672 struct libusb20_me_struct *ps;
674 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
676 if (ps->type == LIBUSB20_ME_IS_ENCODED) {
678 * Pre-store a de-constified
682 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
685 * Get the correct number of
689 if (buf[0] == 0xFF) {
698 /* get the structure length */
701 if (buf[0] == 0xFF) {
720 /* check for invalid length */
726 /* check wanted structure type */
729 case LIBUSB20_ME_IS_ENCODED:
730 /* check for zero length */
736 ps->ptr = LIBUSB20_ADD_BYTES(
737 libusb20_me_encode_empty, 0);
744 case LIBUSB20_ME_IS_RAW:
745 /* update length and pointer */
747 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
750 case LIBUSB20_ME_IS_EMPTY:
751 case LIBUSB20_ME_IS_DECODED:
752 /* check for non-zero length */
755 ps->type = LIBUSB20_ME_IS_DECODED;
761 (void) libusb20_me_decode(buf,
765 ps->type = LIBUSB20_ME_IS_EMPTY;
772 * nothing to do - should
781 pd_offset += sizeof(struct libusb20_me_struct);
790 return (len_old - len);