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.c90
1 files changed, 45 insertions, 45 deletions
diff --git a/firmware/usbstack/core/config.c b/firmware/usbstack/core/config.c
index a05a508bf9..277156d0d9 100644
--- a/firmware/usbstack/core/config.c
+++ b/firmware/usbstack/core/config.c
@@ -23,58 +23,58 @@
23#include <string.h> 23#include <string.h>
24#include "usbstack/core.h" 24#include "usbstack/core.h"
25 25
26static int usb_descriptor_fillbuf(void* buf, unsigned buflen, struct usb_descriptor_header** src) { 26static int usb_descriptor_fillbuf(void* buf, unsigned buflen, struct usb_descriptor_header** src)
27 27{
28 uint8_t* dest = buf; 28 uint8_t* dest = buf;
29 29
30 if (!src) { 30 if (!src) {
31 return -EINVAL; 31 return -EINVAL;
32 } 32 }
33 33
34 /* fill buffer from src[] until null descriptor ptr */ 34 /* fill buffer from src[] until null descriptor ptr */
35 for (; 0 != *src; src++) { 35 for (; 0 != *src; src++) {
36 unsigned len = (*src)->bLength; 36 unsigned len = (*src)->bLength;
37 37
38 logf("len: %d", len); 38 logf("len: %d", len);
39 39
40 if (len > buflen) 40 if (len > buflen)
41 return -EINVAL; 41 return -EINVAL;
42 memcpy(dest, *src, len); 42 memcpy(dest, *src, len);
43 buflen -= len; 43 buflen -= len;
44 dest += len; 44 dest += len;
45 } 45 }
46 return dest - (uint8_t *)buf; 46 return dest - (uint8_t *)buf;
47} 47}
48 48
49int usb_stack_configdesc(const struct usb_config_descriptor* config, void* buf, unsigned length, struct usb_descriptor_header** desc) { 49int usb_stack_configdesc(const struct usb_config_descriptor* config, void* buf, unsigned length, struct usb_descriptor_header** desc)
50 50{
51 struct usb_config_descriptor* cp = buf; 51 struct usb_config_descriptor* cp = buf;
52 int len; 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 }
53 67
54 if (length < USB_DT_CONFIG_SIZE || !desc) { 68 len += USB_DT_CONFIG_SIZE;
55 return -EINVAL; 69 if (len > 0xffff) {
56 } 70 return -EINVAL;
57 71 }
58 /* config descriptor first */
59 *cp = *config;
60 72
61 /* then interface/endpoint/class/vendor/... */ 73 /* patch up the config descriptor */
62 len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (uint8_t*)buf, length - USB_DT_CONFIG_SIZE, desc); 74 cp->bLength = USB_DT_CONFIG_SIZE;
63 75 cp->bDescriptorType = USB_DT_CONFIG;
64 if (len < 0) { 76 cp->wTotalLength = len;
65 return len; 77 cp->bmAttributes |= USB_CONFIG_ATT_ONE;
66 }
67
68 len += USB_DT_CONFIG_SIZE;
69 if (len > 0xffff) {
70 return -EINVAL;
71 }
72 78
73 /* patch up the config descriptor */ 79 return len;
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} 80}