summaryrefslogtreecommitdiff
path: root/firmware/usbstack/core/config.c
diff options
context:
space:
mode:
Diffstat (limited to 'firmware/usbstack/core/config.c')
-rw-r--r--firmware/usbstack/core/config.c80
1 files changed, 80 insertions, 0 deletions
diff --git a/firmware/usbstack/core/config.c b/firmware/usbstack/core/config.c
new file mode 100644
index 0000000000..a05a508bf9
--- /dev/null
+++ b/firmware/usbstack/core/config.c
@@ -0,0 +1,80 @@
1/***************************************************************************
2 * __________ __ ___.
3 * Open \______ \ ____ ____ | | _\_ |__ _______ ___
4 * Source | _// _ \_/ ___\| |/ /| __ \ / _ \ \/ /
5 * Jukebox | | ( <_> ) \___| < | \_\ ( <_> > < <
6 * Firmware |____|_ /\____/ \___ >__|_ \|___ /\____/__/\_ \
7 * \/ \/ \/ \/ \/
8 * $Id: $
9 *
10 * Copyright (C) 2007 by Christian Gmeiner
11 *
12 * Based on linux/drivers/usb/gadget/config.c
13 * Copyright (C) 2003 David Brownell
14 *
15 * All files in this archive are subject to the GNU General Public License.
16 * See the file COPYING in the source tree root for full license agreement.
17 *
18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19 * KIND, either express or implied.
20 *
21 ****************************************************************************/
22
23#include <string.h>
24#include "usbstack/core.h"
25
26static int usb_descriptor_fillbuf(void* buf, unsigned buflen, struct usb_descriptor_header** src) {
27
28 uint8_t* dest = buf;
29
30 if (!src) {
31 return -EINVAL;
32 }
33
34 /* fill buffer from src[] until null descriptor ptr */
35 for (; 0 != *src; src++) {
36 unsigned len = (*src)->bLength;
37
38 logf("len: %d", len);
39
40 if (len > buflen)
41 return -EINVAL;
42 memcpy(dest, *src, len);
43 buflen -= len;
44 dest += len;
45 }
46 return dest - (uint8_t *)buf;
47}
48
49int usb_stack_configdesc(const struct usb_config_descriptor* config, void* buf, unsigned length, struct usb_descriptor_header** desc) {
50
51 struct usb_config_descriptor* cp = buf;
52 int len;
53
54 if (length < USB_DT_CONFIG_SIZE || !desc) {
55 return -EINVAL;
56 }
57
58 /* config descriptor first */
59 *cp = *config;
60
61 /* then interface/endpoint/class/vendor/... */
62 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (uint8_t*)buf, length - USB_DT_CONFIG_SIZE, desc);
63
64 if (len < 0) {
65 return len;
66 }
67
68 len += USB_DT_CONFIG_SIZE;
69 if (len > 0xffff) {
70 return -EINVAL;
71 }
72
73 /* patch up the config descriptor */
74 cp->bLength = USB_DT_CONFIG_SIZE;
75 cp->bDescriptorType = USB_DT_CONFIG;
76 cp->wTotalLength = len;
77 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
78
79 return len;
80}