1 /* $FreeBSD: src/lib/libusb/libusb20_desc.c,v 1.6 2012/04/20 14:29:45 hselasky Exp $ */
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 ptr = (void *)(lub_endpoint + nendpoint);
137 memcpy(LIBUSB20_ADD_BYTES(ptr, 0), config_desc, pcdesc.len);
138 pcdesc.ptr = LIBUSB20_ADD_BYTES(ptr, 0);
139 config_desc = LIBUSB20_ADD_BYTES(ptr, 0);
141 /* init config structure */
145 LIBUSB20_INIT(LIBUSB20_CONFIG_DESC, &lub_config->desc);
147 if (libusb20_me_decode(ptr, ptr[0], &lub_config->desc)) {
150 lub_config->num_interface = 0;
151 lub_config->interface = lub_interface;
152 lub_config->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
153 lub_config->extra.len = -ptr[0];
154 lub_config->extra.type = LIBUSB20_ME_IS_RAW;
165 /* descriptor pre-scan */
166 while ((ptr = libusb20_desc_foreach(&pcdesc, ptr))) {
167 if (ptr[1] == LIBUSB20_DT_ENDPOINT) {
170 last_ep = lub_endpoint;
171 last_if->num_endpoints++;
173 LIBUSB20_INIT(LIBUSB20_ENDPOINT_DESC, &last_ep->desc);
175 if (libusb20_me_decode(ptr, ptr[0], &last_ep->desc)) {
178 last_ep->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
179 last_ep->extra.len = 0;
180 last_ep->extra.type = LIBUSB20_ME_IS_RAW;
182 lub_config->extra.len += ptr[0];
185 } else if ((ptr[1] == LIBUSB20_DT_INTERFACE) && (ptr[0] >= 4)) {
186 if (ptr[2] != iface_no) {
190 lub_config->num_interface++;
191 last_if = lub_interface;
194 /* one more alternate setting */
195 lub_interface->num_altsetting++;
196 last_if = lub_alt_interface;
200 LIBUSB20_INIT(LIBUSB20_INTERFACE_DESC, &last_if->desc);
202 if (libusb20_me_decode(ptr, ptr[0], &last_if->desc)) {
206 * Sometimes USB devices have corrupt interface
207 * descriptors and we need to overwrite the provided
210 last_if->desc.bInterfaceNumber = niface - 1;
211 last_if->extra.ptr = LIBUSB20_ADD_BYTES(ptr, ptr[0]);
212 last_if->extra.len = 0;
213 last_if->extra.type = LIBUSB20_ME_IS_RAW;
214 last_if->endpoints = lub_endpoint + 1;
215 last_if->altsetting = lub_alt_interface;
216 last_if->num_altsetting = 0;
217 last_if->num_endpoints = 0;
220 /* unknown descriptor */
223 last_ep->extra.len += ptr[0];
225 last_if->extra.len += ptr[0];
228 lub_config->extra.len += ptr[0];
235 /*------------------------------------------------------------------------*
236 * libusb20_desc_foreach
238 * Safe traversal of USB descriptors.
241 * NULL: End of descriptors
242 * Else: Pointer to next descriptor
243 *------------------------------------------------------------------------*/
245 libusb20_desc_foreach(const struct libusb20_me_struct *pdesc,
246 const uint8_t *psubdesc)
248 const uint8_t *start;
250 const uint8_t *desc_next;
256 start = (const uint8_t *)pdesc->ptr;
257 end = LIBUSB20_ADD_BYTES(start, pdesc->len);
259 /* get start of next descriptor */
260 if (psubdesc == NULL)
263 psubdesc = psubdesc + psubdesc[0];
265 /* check that the next USB descriptor is within the range */
266 if ((psubdesc < start) || (psubdesc >= end))
267 return (NULL); /* out of range, or EOD */
269 /* check start of the second next USB descriptor, if any */
270 desc_next = psubdesc + psubdesc[0];
271 if ((desc_next < start) || (desc_next > end))
272 return (NULL); /* out of range */
274 /* check minimum descriptor length */
276 return (NULL); /* too short descriptor */
278 return (psubdesc); /* return start of next descriptor */
281 /*------------------------------------------------------------------------*
282 * libusb20_me_get_1 - safety wrapper to read out one byte
283 *------------------------------------------------------------------------*/
285 libusb20_me_get_1(const struct libusb20_me_struct *ie, uint16_t offset)
287 if (offset < ie->len) {
288 return (*((uint8_t *)LIBUSB20_ADD_BYTES(ie->ptr, offset)));
293 /*------------------------------------------------------------------------*
294 * libusb20_me_get_2 - safety wrapper to read out one word
295 *------------------------------------------------------------------------*/
297 libusb20_me_get_2(const struct libusb20_me_struct *ie, uint16_t offset)
299 return (libusb20_me_get_1(ie, offset) |
300 (libusb20_me_get_1(ie, offset + 1) << 8));
303 /*------------------------------------------------------------------------*
304 * libusb20_me_encode - encode a message structure
306 * Description of parameters:
307 * "len" - maximum length of output buffer
308 * "ptr" - pointer to output buffer. If NULL, no data will be written
309 * "pd" - source structure
312 * 0..65535 - Number of bytes used, limited by the "len" input parameter.
313 *------------------------------------------------------------------------*/
315 libusb20_me_encode(void *ptr, uint16_t len, const void *pd)
317 const uint8_t *pf; /* pointer to format data */
318 uint8_t *buf; /* pointer to output buffer */
320 uint32_t pd_offset; /* decoded structure offset */
321 uint16_t len_old; /* old length */
322 uint16_t pd_count; /* decoded element count */
323 uint8_t me; /* message element */
329 pd_offset = sizeof(void *);
330 pf = (*((struct libusb20_me_format *const *)pd))->format;
336 /* get information element */
338 me = (pf[0]) & LIBUSB20_ME_MASK;
339 pd_count = pf[1] | (pf[2] << 8);
342 /* encode the message element */
345 case LIBUSB20_ME_INT8:
349 if (len < 1) /* overflow */
352 temp = *((const uint8_t *)
353 LIBUSB20_ADD_BYTES(pd, pd_offset));
362 case LIBUSB20_ME_INT16:
363 pd_offset = -((-pd_offset) & ~1); /* align */
367 if (len < 2) /* overflow */
371 temp = *((const uint16_t *)
372 LIBUSB20_ADD_BYTES(pd, pd_offset));
373 buf[1] = (temp >> 8) & 0xFF;
374 buf[0] = temp & 0xFF;
382 case LIBUSB20_ME_INT32:
383 pd_offset = -((-pd_offset) & ~3); /* align */
387 if (len < 4) /* overflow */
390 temp = *((const uint32_t *)
391 LIBUSB20_ADD_BYTES(pd, pd_offset));
392 buf[3] = (temp >> 24) & 0xFF;
393 buf[2] = (temp >> 16) & 0xFF;
394 buf[1] = (temp >> 8) & 0xFF;
395 buf[0] = temp & 0xFF;
403 case LIBUSB20_ME_INT64:
404 pd_offset = -((-pd_offset) & ~7); /* align */
408 if (len < 8) /* overflow */
412 temp = *((const uint64_t *)
413 LIBUSB20_ADD_BYTES(pd, pd_offset));
414 buf[7] = (temp >> 56) & 0xFF;
415 buf[6] = (temp >> 48) & 0xFF;
416 buf[5] = (temp >> 40) & 0xFF;
417 buf[4] = (temp >> 32) & 0xFF;
418 buf[3] = (temp >> 24) & 0xFF;
419 buf[2] = (temp >> 16) & 0xFF;
420 buf[1] = (temp >> 8) & 0xFF;
421 buf[0] = temp & 0xFF;
429 case LIBUSB20_ME_STRUCT:
430 pd_offset = -((-pd_offset) &
431 ~(LIBUSB20_ME_STRUCT_ALIGN - 1)); /* align */
435 struct libusb20_me_struct *ps;
437 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
440 case LIBUSB20_ME_IS_RAW:
445 case LIBUSB20_ME_IS_ENCODED:
455 src_len = libusb20_me_get_1(pd, 0);
456 src_ptr = LIBUSB20_ADD_BYTES(ps->ptr, 1);
457 if (src_len == 0xFF) {
458 /* length is escaped */
459 src_len = libusb20_me_get_2(pd, 1);
461 LIBUSB20_ADD_BYTES(ps->ptr, 3);
465 case LIBUSB20_ME_IS_DECODED:
466 /* reserve 3 length bytes */
467 src_len = libusb20_me_encode(NULL,
468 0xFFFF - 3, ps->ptr);
472 default: /* empty structure */
478 if (src_len > 0xFE) {
479 if (src_len > (0xFFFF - 3))
483 if (len < (src_len + 3))
489 buf[1] = (src_len & 0xFF);
490 buf[2] = (src_len >> 8) & 0xFF;
493 len -= (src_len + 3);
495 if (len < (src_len + 1))
500 buf[0] = (src_len & 0xFF);
503 len -= (src_len + 1);
506 /* check for buffer and non-zero length */
508 if (buf && src_len) {
509 if (ps->type == LIBUSB20_ME_IS_DECODED) {
512 * procedure - we have
514 * complete structure:
516 __unused uint16_t dummy;
518 dummy = 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 __unused uint16_t dummy;
673 struct libusb20_me_struct *ps;
675 ps = LIBUSB20_ADD_BYTES(pd, pd_offset);
677 if (ps->type == LIBUSB20_ME_IS_ENCODED) {
679 * Pre-store a de-constified
683 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
686 * Get the correct number of
690 if (buf[0] == 0xFF) {
699 /* get the structure length */
702 if (buf[0] == 0xFF) {
721 /* check for invalid length */
727 /* check wanted structure type */
730 case LIBUSB20_ME_IS_ENCODED:
731 /* check for zero length */
737 ps->ptr = LIBUSB20_ADD_BYTES(
738 libusb20_me_encode_empty, 0);
745 case LIBUSB20_ME_IS_RAW:
746 /* update length and pointer */
748 ps->ptr = LIBUSB20_ADD_BYTES(buf, 0);
751 case LIBUSB20_ME_IS_EMPTY:
752 case LIBUSB20_ME_IS_DECODED:
753 /* check for non-zero length */
756 ps->type = LIBUSB20_ME_IS_DECODED;
762 dummy = libusb20_me_decode(buf,
766 ps->type = LIBUSB20_ME_IS_EMPTY;
773 * nothing to do - should
782 pd_offset += sizeof(struct libusb20_me_struct);
791 return (len_old - len);