summaryrefslogtreecommitdiff
path: root/firmware/usbstack/core/config.c
blob: 1a9b12508ea16986b96e7c69c0188ec7b4c87584 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 by Christian Gmeiner
 *
 * Based on linux/drivers/usb/gadget/config.c
 *   Copyright (C) 2003 David Brownell
 * 
 * All files in this archive are subject to the GNU General Public License.
 * See the file COPYING in the source tree root for full license agreement.
 *
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
 * KIND, either express or implied.
 *
 ****************************************************************************/

#include <string.h>
#include "usbstack/core.h"

static int usb_descriptor_fillbuf(void* buf, unsigned buflen,
                                  struct usb_descriptor_header** src)
{
    uint8_t* dest = buf;

    if (!src) {
        return -EINVAL;
    }

    /* fill buffer from src[] until null descriptor ptr */
    for (; 0 != *src; src++) {
        unsigned len = (*src)->bLength;

        if (len > buflen)
            return -EINVAL;

        memcpy(dest, *src, len);
        buflen -= len;
        dest += len;
    }
    return dest - (uint8_t *)buf;
}

int usb_stack_configdesc(const struct usb_config_descriptor* config, void* buf,
                         unsigned length, struct usb_descriptor_header** desc)
{
    struct usb_config_descriptor* cp = buf;
    int len;

    if (length < USB_DT_CONFIG_SIZE || !desc) {
        return -EINVAL;
    }

    /* config descriptor first */
    *cp = *config; 

    /* then interface/endpoint/class/vendor/... */
    len = usb_descriptor_fillbuf(USB_DT_CONFIG_SIZE + (uint8_t*)buf,
                                 length - USB_DT_CONFIG_SIZE, desc);

    if (len < 0) {
        return len;
    }

    len += USB_DT_CONFIG_SIZE;
    if (len > 0xffff) {
        return -EINVAL;
    }

    /* patch up the config descriptor */
    cp->bLength = USB_DT_CONFIG_SIZE;
    cp->bDescriptorType = USB_DT_CONFIG;
    cp->wTotalLength = len;
    cp->bmAttributes |= USB_CONFIG_ATT_ONE;

    return len;
}