summaryrefslogtreecommitdiffstats
path: root/firmware/usbstack/core/epsetup.c
blob: ef98935fe30f4c59994921e0b1c2043b7e710b39 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/***************************************************************************
 *             __________               __   ___.
 *   Open      \______   \ ____   ____ |  | _\_ |__   _______  ___
 *   Source     |       _//  _ \_/ ___\|  |/ /| __ \ /  _ \  \/  /
 *   Jukebox    |    |   (  <_> )  \___|    < | \_\ (  <_> > <  <
 *   Firmware   |____|_  /\____/ \___  >__|_ \|___  /\____/__/\_ \
 *                     \/            \/     \/    \/            \/
 * $Id$
 *
 * Copyright (C) 2007 by Christian Gmeiner
 *
 * 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 <ctype.h>
#include "usbstack/core.h"

/**
 * 
 * Naming Convention for Endpoint Names
 * 
 *    - ep1, ep2, ... address is fixed, not direction or type
 *    - ep1in, ep2out, ... address and direction are fixed, not type
 *    - ep1-bulk, ep2-bulk, ... address and type are fixed, not direction
 *    - ep1in-bulk, ep2out-iso, ... all three are fixed
 *    - ep-* ... no functionality restrictions
 *
 * Type suffixes are "-bulk", "-iso", or "-int".  Numbers are decimal.
 *
 */
static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc);

void usb_ep_autoconfig_reset(void)
{
    struct usb_ep* ep = NULL;
    if (usbcore.active_controller == NULL) {
        return;
    }

    logf("resetting endpoints");
    list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) {
        logf("reset %s", ep->name);
        ep->claimed = false;
    }
}

/**
 * Find a suitable endpoint for the requested endpoint descriptor.
 * @param desc usb descritpro to use for seraching.
 * @return NULL or a valid endpoint.
 */
struct usb_ep* usb_ep_autoconfig(struct usb_endpoint_descriptor* desc)
{
    struct usb_ep* ep = NULL;
    if (usbcore.active_controller == NULL) {
        logf("active controller NULL");
        return NULL;
    }

    list_for_each_entry(ep, &usbcore.active_controller->endpoints.list, list) {
        if (ep_matches (ep, desc)) {
            return ep;
        }
    }

    return NULL;
}

static int ep_matches(struct usb_ep* ep, struct usb_endpoint_descriptor* desc)
{
    uint8_t     type;
    const char* tmp;
    uint16_t    max;

    /* endpoint already claimed? */
    if (ep->claimed) {
        logf("!! claimed !!");
        return 0;
    }

    /* only support ep0 for portable CONTROL traffic */
    type = desc->bmAttributes & USB_ENDPOINT_XFERTYPE_MASK;
    if (type == USB_ENDPOINT_XFER_CONTROL) {
        logf("type == control");
        return 0;
    }

    /* some other naming convention */
    if (ep->name[0] != 'e') {
        logf("wrong name");
        return 0;
    }

    /* type-restriction:  "-iso", "-bulk", or "-int".
     * direction-restriction:  "in", "out".
     */
    if (ep->name[2] != '-' ) {
        tmp = strrchr (ep->name, '-');
        if (tmp) {
            switch (type) {
            case USB_ENDPOINT_XFER_INT:
                /* bulk endpoints handle interrupt transfers,
                 * except the toggle-quirky iso-synch kind
                 */
                if (tmp[2] == 's') { // == "-iso"
                    return 0;
                }
                break;
            case USB_ENDPOINT_XFER_BULK:
                if (tmp[1] != 'b') { // != "-bulk"
                    return 0;
                }
                break;
            case USB_ENDPOINT_XFER_ISOC:
                if (tmp[2] != 's') { // != "-iso"
                    return 0;
                }
            }
        } else {
            tmp = ep->name + strlen (ep->name);
        }

        /* direction-restriction:  "..in-..", "out-.." */
        tmp--;
        if (!isdigit(*tmp)) {
            if (desc->bEndpointAddress & USB_DIR_IN) {
                if ('n' != *tmp) {
                    return 0;
                }
            } else {
                if ('t' != *tmp) {
                    return 0;
                }
            }
        }
    }


    /* endpoint maxpacket size is an input parameter, except for bulk
     * where it's an output parameter representing the full speed limit.
     * the usb spec fixes high speed bulk maxpacket at 512 bytes.
     */
    max = 0x7ff & desc->wMaxPacketSize;

    switch (type) {
    case USB_ENDPOINT_XFER_INT:
        /* INT:  limit 64 bytes full speed, 1024 high speed */
        if ((usbcore.active_controller->speed != USB_SPEED_HIGH) && 
            (max > 64)) {
            return 0;
        }
        /* FALLTHROUGH */

    case USB_ENDPOINT_XFER_ISOC:
        if ((usbcore.active_controller->speed != USB_SPEED_HIGH) &&
            (max > 1023)) {
            return 0;
        }
        break;
    }

    /* MATCH!! */

    /* set address of used ep in desc */
    desc->bEndpointAddress |= ep->ep_num;
    ep->desc = desc;

    return 1;
}