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);
45 /*------------------------------------------------------------------------*
46 * libusb20_parse_config_desc
49 * NULL: Out of memory.
50 * Else: A valid config structure pointer which must be passed to "free()"
51 *------------------------------------------------------------------------*/
52 struct libusb20_config *
53 libusb20_parse_config_desc(const void *config_desc)
55 struct libusb20_config *lub_config;
56 struct libusb20_interface *lub_interface;
57 struct libusb20_interface *lub_alt_interface;
58 struct libusb20_interface *last_if;
59 struct libusb20_endpoint *lub_endpoint;
60 struct libusb20_endpoint *last_ep;
62 struct libusb20_me_struct pcdesc;
65 uint16_t niface_no_alt;
71 if (ptr[1] != LIBUSB20_DT_CONFIG) {
72 return (NULL); /* not config descriptor */
75 * The first "bInterfaceNumber" should never have the value 0xff.
84 /* get "wTotalLength" and setup "pcdesc" */
85 pcdesc.ptr = LIBUSB20_ADD_BYTES(config_desc, 0);
87 ((const uint8_t *)config_desc)[2] |
88 (((const uint8_t *)config_desc)[3] << 8);
89 pcdesc.type = LIBUSB20_ME_IS_RAW;
91 /* descriptor pre-scan */
92 while ((ptr = libusb20_desc_foreach(&pcdesc, ptr))) {
93 if (ptr[1] == LIBUSB20_DT_ENDPOINT) {
95 } else if ((ptr[1] == LIBUSB20_DT_INTERFACE) && (ptr[0] >= 4)) {
97 /* check "bInterfaceNumber" */
98 if (ptr[2] != iface_no) {
105 /* sanity checking */
107 return (NULL); /* corrupt */
109 if (nendpoint >= 256) {
110 return (NULL); /* corrupt */
112 size = sizeof(*lub_config) +
113 (niface * sizeof(*lub_interface)) +
114 (nendpoint * sizeof(*lub_endpoint)) +
117 lub_config = malloc(size);
118 if (lub_config == NULL) {
119 return (NULL); /* out of memory */
121 /* make sure memory is initialised */
122 memset(lub_config, 0, size);
124 lub_interface = (void *)(lub_config + 1);
125 lub_alt_interface = (void *)(lub_interface + niface_no_alt);
126 lub_endpoint = (void *)(lub_interface + niface);
129 * Make a copy of the config descriptor, so that the caller can free
130 * the inital config descriptor pointer!
132 ptr = (void *)(lub_endpoint + nendpoint);
133 memcpy(LIBUSB20_ADD_BYTES(ptr, 0), config_desc, pcdesc.len);
134 pcdesc.ptr = LIBUSB20_ADD_BYTES(ptr, 0);
135 config_desc = LIBUSB20_ADD_BYTES(ptr, 0);
137 /* init config structure */
141 LIBUSB20_INIT(LIBUSB20_CONFIG_DESC, &lub_config->desc);
143 if (libusb20_me_decode(ptr, ptr[0], &lub_config->desc)) {
146 lub_config->num_interface = 0;
147 lub_config->interface = lub_interface;
148 lub_config->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
149 lub_config->extra.len = -ptr[0];
150 lub_config->extra.type = LIBUSB20_ME_IS_RAW;
161 /* descriptor pre-scan */
162 while ((ptr = libusb20_desc_foreach(&pcdesc, ptr))) {
163 if (ptr[1] == LIBUSB20_DT_ENDPOINT) {
166 last_ep = lub_endpoint;
167 last_if->num_endpoints++;
169 LIBUSB20_INIT(LIBUSB20_ENDPOINT_DESC, &last_ep->desc);
171 if (libusb20_me_decode(ptr, ptr[0], &last_ep->desc)) {
174 last_ep->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
175 last_ep->extra.len = 0;
176 last_ep->extra.type = LIBUSB20_ME_IS_RAW;
178 lub_config->extra.len += ptr[0];
181 } else if ((ptr[1] == LIBUSB20_DT_INTERFACE) && (ptr[0] >= 4)) {
182 if (ptr[2] != iface_no) {
186 lub_config->num_interface++;
187 last_if = lub_interface;
190 /* one more alternate setting */
191 lub_interface->num_altsetting++;
192 last_if = lub_alt_interface;
196 LIBUSB20_INIT(LIBUSB20_INTERFACE_DESC, &last_if->desc);
198 if (libusb20_me_decode(ptr, ptr[0], &last_if->desc)) {
202 * Sometimes USB devices have corrupt interface
203 * descriptors and we need to overwrite the provided
206 last_if->desc.bInterfaceNumber = niface - 1;
207 last_if->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
208 last_if->extra.len = 0;
209 last_if->extra.type = LIBUSB20_ME_IS_RAW;
210 last_if->endpoints = lub_endpoint + 1;
211 last_if->altsetting = lub_alt_interface;
212 last_if->num_altsetting = 0;
213 last_if->num_endpoints = 0;
216 /* unknown descriptor */
219 last_ep->extra.len += ptr[0];
221 last_if->extra.len += ptr[0];
224 lub_config->extra.len += ptr[0];
231 /*------------------------------------------------------------------------*
232 * libusb20_desc_foreach
234 * Safe traversal of USB descriptors.
237 * NULL: End of descriptors
238 * Else: Pointer to next descriptor
239 *------------------------------------------------------------------------*/
241 libusb20_desc_foreach(const struct libusb20_me_struct *pdesc,
242 const uint8_t *psubdesc)
244 const uint8_t *start;
246 const uint8_t *desc_next;
252 start = (const uint8_t *)pdesc->ptr;
253 end = LIBUSB20_ADD_BYTES(start, pdesc->len);
255 /* get start of next descriptor */
256 if (psubdesc == NULL)
259 psubdesc = psubdesc + psubdesc[0];
261 /* check that the next USB descriptor is within the range */
262 if ((psubdesc < start) || (psubdesc >= end))
263 return (NULL); /* out of range, or EOD */
265 /* check start of the second next USB descriptor, if any */
266 desc_next = psubdesc + psubdesc[0];
267 if ((desc_next < start) || (desc_next > end))
268 return (NULL); /* out of range */
270 /* check minimum descriptor length */
272 return (NULL); /* too short descriptor */
274 return (psubdesc); /* return start of next descriptor */
277 /*------------------------------------------------------------------------*
278 * libusb20_me_get_1 - safety wrapper to read out one byte
279 *------------------------------------------------------------------------*/
281 libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset)
283 if (offset < ie->len) {
284 return (*((uint8_t *)LIBUSB20_ADD_BYTES(ie->ptr, offset)));
289 /*------------------------------------------------------------------------*
290 * libusb20_me_get_2 - safety wrapper to read out one word
291 *------------------------------------------------------------------------*/
293 libusb20_me_get_2(const struct libusb20_me_struct *ie, uint16_t offset)
295 return (libusb20_me_get_1(ie, offset) |
296 (libusb20_me_get_1(ie, offset + 1) << 8));
299 /*------------------------------------------------------------------------*
300 * libusb20_me_encode - encode a message structure
302 * Description of parameters:
303 * "len" - maximum length of output buffer
304 * "ptr" - pointer to output buffer. If NULL, no data will be written
305 * "pd" - source structure
308 * 0..65535 - Number of bytes used, limited by the "len" input parameter.
309 *------------------------------------------------------------------------*/
311 libusb20_me_encode(void *ptr, uint16_t len, const void *pd)
313 const uint8_t *pf; /* pointer to format data */
314 uint8_t *buf; /* pointer to output buffer */
316 uint32_t pd_offset; /* decoded structure offset */
317 uint16_t len_old; /* old length */
318 uint16_t pd_count; /* decoded element count */
319 uint8_t me; /* message element */
325 pd_offset = sizeof(void *);
326 pf = (*((struct libusb20_me_format *const *)pd))->format;
332 /* get information element */
334 me = (pf[0]) & LIBUSB20_ME_MASK;
335 pd_count = pf[1] | (pf[2] << 8);
338 /* encode the message element */
341 case LIBUSB20_ME_INT8:
345 if (len < 1) /* overflow */
348 temp = *((const uint8_t *)
349 LIBUSB20_ADD_BYTES(pd, pd_offset));
358 case LIBUSB20_ME_INT16:
359 pd_offset = -((-pd_offset) & ~1); /* align */
363 if (len < 2) /* overflow */
367 temp = *((const uint16_t *)
368 LIBUSB20_ADD_BYTES(pd, pd_offset));
369 buf[1] = (temp >> 8) & 0xFF;
370 buf[0] = temp & 0xFF;
378 case LIBUSB20_ME_INT32:
379 pd_offset = -((-pd_offset) & ~3); /* align */
383 if (len < 4) /* overflow */
386 temp = *((const uint32_t *)
387 LIBUSB20_ADD_BYTES(pd, pd_offset));
388 buf[3] = (temp >> 24) & 0xFF;
389 buf[2] = (temp >> 16) & 0xFF;
390 buf[1] = (temp >> 8) & 0xFF;
391 buf[0] = temp & 0xFF;
399 case LIBUSB20_ME_INT64:
400 pd_offset = -((-pd_offset) & ~7); /* align */
404 if (len < 8) /* overflow */
408 temp = *((const uint64_t *)
409 LIBUSB20_ADD_BYTES(pd, pd_offset));
410 buf[7] = (temp >> 56) & 0xFF;
411 buf[6] = (temp >> 48) & 0xFF;
412 buf[5] = (temp >> 40) & 0xFF;
413 buf[4] = (temp >> 32) & 0xFF;
414 buf[3] = (temp >> 24) & 0xFF;
415 buf[2] = (temp >> 16) & 0xFF;
416 buf[1] = (temp >> 8) & 0xFF;
417 buf[0] = temp & 0xFF;
425 case LIBUSB20_ME_STRUCT:
426 pd_offset = -((-pd_offset) &
427 ~(LIBUSB20_ME_STRUCT_ALIGN - 1)); /* align */
431 struct libusb20_me_struct *ps;
433 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
436 case LIBUSB20_ME_IS_RAW:
441 case LIBUSB20_ME_IS_ENCODED:
451 src_len = libusb20_me_get_1(pd, 0);
452 src_ptr = LIBUSB20_ADD_BYTES(ps->ptr, 1);
453 if (src_len == 0xFF) {
454 /* length is escaped */
455 src_len = libusb20_me_get_2(pd, 1);
457 LIBUSB20_ADD_BYTES(ps->ptr, 3);
461 case LIBUSB20_ME_IS_DECODED:
462 /* reserve 3 length bytes */
463 src_len = libusb20_me_encode(NULL,
468 default: /* empty structure */
474 if (src_len > 0xFE) {
475 if (src_len > (uint16_t)(0 - 1 - 3))
479 if (len < (src_len + 3))
485 buf[1] = (src_len & 0xFF);
486 buf[2] = (src_len >> 8) & 0xFF;
489 len -= (src_len + 3);
491 if (len < (src_len + 1))
496 buf[0] = (src_len & 0xFF);
499 len -= (src_len + 1);
502 /* check for buffer and non-zero length */
504 if (buf && src_len) {
505 if (ps->type == LIBUSB20_ME_IS_DECODED) {
508 * procedure - we have
510 * complete structure:
514 dummy = libusb20_me_encode(buf,
517 bcopy(src_ptr, buf, src_len);
521 pd_offset += sizeof(struct libusb20_me_struct);
530 return (len_old - len);
533 /*------------------------------------------------------------------------*
534 * libusb20_me_decode - decode a message into a decoded structure
536 * Description of parameters:
537 * "ptr" - message pointer
538 * "len" - message length
539 * "pd" - pointer to decoded structure
542 * "0..65535" - number of bytes decoded, limited by "len"
543 *------------------------------------------------------------------------*/
545 libusb20_me_decode(const void *ptr, uint16_t len, void *pd)
547 const uint8_t *pf; /* pointer to format data */
548 const uint8_t *buf; /* pointer to input buffer */
550 uint32_t pd_offset; /* decoded structure offset */
551 uint16_t len_old; /* old length */
552 uint16_t pd_count; /* decoded element count */
553 uint8_t me; /* message element */
559 pd_offset = sizeof(void *);
560 pf = (*((struct libusb20_me_format **)pd))->format;
566 /* get information element */
568 me = (pf[0]) & LIBUSB20_ME_MASK;
569 pd_count = pf[1] | (pf[2] << 8);
572 /* decode the message element by type */
575 case LIBUSB20_ME_INT8:
587 *((uint8_t *)LIBUSB20_ADD_BYTES(pd,
593 case LIBUSB20_ME_INT16:
594 pd_offset = -((-pd_offset) & ~1); /* align */
607 *((uint16_t *)LIBUSB20_ADD_BYTES(pd,
613 case LIBUSB20_ME_INT32:
614 pd_offset = -((-pd_offset) & ~3); /* align */
624 temp |= buf[2] << 16;
630 *((uint32_t *)LIBUSB20_ADD_BYTES(pd,
636 case LIBUSB20_ME_INT64:
637 pd_offset = -((-pd_offset) & ~7); /* align */
646 temp = ((uint64_t)buf[7]) << 56;
647 temp |= ((uint64_t)buf[6]) << 48;
648 temp |= ((uint64_t)buf[5]) << 40;
649 temp |= ((uint64_t)buf[4]) << 32;
650 temp |= buf[3] << 24;
651 temp |= buf[2] << 16;
657 *((uint64_t *)LIBUSB20_ADD_BYTES(pd,
663 case LIBUSB20_ME_STRUCT:
664 pd_offset = -((-pd_offset) &
665 ~(LIBUSB20_ME_STRUCT_ALIGN - 1)); /* align */
669 struct libusb20_me_struct *ps;
671 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
673 if (ps->type == LIBUSB20_ME_IS_ENCODED) {
675 * Pre-store a de-constified
679 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
682 * Get the correct number of
686 if (buf[0] == 0xFF) {
695 /* get the structure length */
698 if (buf[0] == 0xFF) {
717 /* check for invalid length */
723 /* check wanted structure type */
726 case LIBUSB20_ME_IS_ENCODED:
727 /* check for zero length */
733 ps->ptr = LIBUSB20_ADD_BYTES(
734 libusb20_me_encode_empty, 0);
741 case LIBUSB20_ME_IS_RAW:
742 /* update length and pointer */
744 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
747 case LIBUSB20_ME_IS_EMPTY:
748 case LIBUSB20_ME_IS_DECODED:
749 /* check for non-zero length */
752 ps->type = LIBUSB20_ME_IS_DECODED;
758 dummy = libusb20_me_decode(buf,
762 ps->type = LIBUSB20_ME_IS_EMPTY;
769 * nothing to do - should
778 pd_offset += sizeof(struct libusb20_me_struct);
787 return (len_old - len);