summaryrefslogtreecommitdiff
path: root/common/core/src/ux_host_stack_new_endpoint_create.c
blob: bd5d3b71433da8ad5188b2d8cc17f735f36040ec (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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Host Stack                                                          */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_stack_new_endpoint_create                  PORTABLE C      */
/*                                                           6.1.11       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function creates a new endpoint for the current interface      */
/*    scanned. The endpoint is hooked to the interface that owns it.      */
/*    It is not active yet until either the default interface for the     */
/*    configuration is selected by a SET_CONFIGURATION or when an         */
/*    alternate setting for this interface is set.                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    interface                             Interface container that owns */
/*                                            this endpoint               */
/*    endpoint_pointer                      Pointer to a unparsed         */
/*                                            endpoint descriptor         */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_utility_descriptor_parse          Parse the descriptor          */
/*    _ux_utility_memory_allocate           Allocate memory block         */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    USBX Components                                                     */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_stack_new_endpoint_create(UX_INTERFACE *interface_ptr,
                                                 UCHAR * interface_endpoint)
{

UX_ENDPOINT     *endpoint;
UX_ENDPOINT     *list_endpoint;
ULONG           endpoint_type;
ULONG           packet_size;
ULONG           n_tran;

    /* Obtain memory for storing this new endpoint.  */
    endpoint =  (UX_ENDPOINT *) _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_ENDPOINT));
    if (endpoint == UX_NULL)
        return(UX_MEMORY_INSUFFICIENT);

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_NEW_ENDPOINT_CREATE, interface_ptr, endpoint, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)

    /* Save the endpoint handle in the container, this is for ensuring the
       endpoint container is not corrupted.  */
    endpoint -> ux_endpoint =  (ULONG) (ALIGN_TYPE) endpoint;

    /* The endpoint container has a built in transfer_request.
       The transfer_request needs to point to the endpoint as well.  */
    endpoint -> ux_endpoint_transfer_request.ux_transfer_request_endpoint =  endpoint;

    /* Save the pointer to the device. This is useful for the HCD layer.  */
    endpoint -> ux_endpoint_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;

    /* Parse the interface descriptor and make it machine independent.  */
    _ux_utility_descriptor_parse(interface_endpoint,
                            _ux_system_endpoint_descriptor_structure,
                            UX_ENDPOINT_DESCRIPTOR_ENTRIES,
                            (UCHAR *) &endpoint -> ux_endpoint_descriptor);

    /* Check endpoint size and interval to see if they are valid.  */
    endpoint_type = endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE;

    /* Endpoint size should not be zero.  */
    if (endpoint -> ux_endpoint_descriptor.wMaxPacketSize == 0)
    {
        _ux_utility_memory_free(endpoint);
        return(UX_DESCRIPTOR_CORRUPTED);
    }

    /* Control/bulk endpoint, 8, 16, 32, 64, ... 512 can be accepted.
       Note non-standard size in 2^N is accepted since they really works.  */
    if (endpoint_type == UX_CONTROL_ENDPOINT || endpoint_type == UX_BULK_ENDPOINT)
    {
        for (packet_size = 8; packet_size <= 512; packet_size <<= 1)
        {
            if (packet_size == endpoint -> ux_endpoint_descriptor.wMaxPacketSize)
                break;
        }

        /* If endpoint size not valid, return error.  */
        if (packet_size > 512)
        {
            _ux_utility_memory_free(endpoint);
            return(UX_DESCRIPTOR_CORRUPTED);
        }
    }

    /* Interrupt/isochronous endpoint, max 1024 and 3 transactions can be accepted.  */
    else
    {

        /* Max size over 1024 is not allowed.  */
        packet_size = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_PACKET_SIZE_MASK;
        if (packet_size > 1024)
        {
            _ux_utility_memory_free(endpoint);
            return(UX_DESCRIPTOR_CORRUPTED);
        }

        /* Number transaction over 2 additional is not allowed.  */
        n_tran = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_NUMBER_OF_TRANSACTIONS_MASK;
        if (n_tran >= UX_MAX_NUMBER_OF_TRANSACTIONS_MASK)
        {
            _ux_utility_memory_free(endpoint);
            return(UX_DESCRIPTOR_CORRUPTED);
        }

        /* Isochronous/high speed interrupt interval should be 1~16.  */
        if (endpoint -> ux_endpoint_descriptor.bInterval < 1)
        {
            _ux_utility_memory_free(endpoint);
            return(UX_DESCRIPTOR_CORRUPTED);
        }
        if ((endpoint_type == UX_ISOCHRONOUS_ENDPOINT) ||
            (interface_ptr -> ux_interface_configuration -> ux_configuration_device
                                    -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
            )
        {
            if (endpoint -> ux_endpoint_descriptor.bInterval > 16)
            {
                _ux_utility_memory_free(endpoint);
                return(UX_DESCRIPTOR_CORRUPTED);
            }
        }

        /* Save final packet size.  */
        n_tran >>= UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT;
        packet_size *= (n_tran + 1);
    }

    /* Save transfer packet size.  */
    endpoint -> ux_endpoint_transfer_request.ux_transfer_request_packet_length = packet_size;

    /* The interface that owns this endpoint is memorized in the
       endpoint container itself, easier for back chaining.  */
    endpoint -> ux_endpoint_interface =  interface_ptr;

    /* There is 2 cases for the creation of the endpoint descriptor
       if this is the first one, the endpoint descriptor is hooked
       to the interface.
       If it is not the first one, the endpoint is hooked to the
       end of the chain of endpoints.  */
    if (interface_ptr -> ux_interface_first_endpoint == UX_NULL)
    {

        interface_ptr -> ux_interface_first_endpoint =  endpoint;
    }
    else
    {

        list_endpoint =  interface_ptr -> ux_interface_first_endpoint;

        /* Traverse the list until the end.  */
        while (list_endpoint -> ux_endpoint_next_endpoint != UX_NULL)
            list_endpoint =  list_endpoint -> ux_endpoint_next_endpoint;

        /* Hook the endpoint.  */
        list_endpoint -> ux_endpoint_next_endpoint =  endpoint;
    }

    /* Return successful status.  */
    return(UX_SUCCESS);
}