GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_new_endpoint_create.c Lines: 48 48 100.0 %
Date: 2026-03-06 18:57:10 Branches: 30 30 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
10
 **************************************************************************/
11
12
13
/**************************************************************************/
14
/**************************************************************************/
15
/**                                                                       */
16
/** USBX Component                                                        */
17
/**                                                                       */
18
/**   Host Stack                                                          */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
24
/* Include necessary system files.  */
25
26
#define UX_SOURCE_CODE
27
28
#include "ux_api.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_stack_new_endpoint_create                  PORTABLE C      */
37
/*                                                           6.1.11       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function creates a new endpoint for the current interface      */
45
/*    scanned. The endpoint is hooked to the interface that owns it.      */
46
/*    It is not active yet until either the default interface for the     */
47
/*    configuration is selected by a SET_CONFIGURATION or when an         */
48
/*    alternate setting for this interface is set.                        */
49
/*                                                                        */
50
/*  INPUT                                                                 */
51
/*                                                                        */
52
/*    interface                             Interface container that owns */
53
/*                                            this endpoint               */
54
/*    endpoint_pointer                      Pointer to a unparsed         */
55
/*                                            endpoint descriptor         */
56
/*                                                                        */
57
/*  OUTPUT                                                                */
58
/*                                                                        */
59
/*    Completion Status                                                   */
60
/*                                                                        */
61
/*  CALLS                                                                 */
62
/*                                                                        */
63
/*    _ux_utility_descriptor_parse          Parse the descriptor          */
64
/*    _ux_utility_memory_allocate           Allocate memory block         */
65
/*                                                                        */
66
/*  CALLED BY                                                             */
67
/*                                                                        */
68
/*    USBX Components                                                     */
69
/*                                                                        */
70
/**************************************************************************/
71
2710
UINT  _ux_host_stack_new_endpoint_create(UX_INTERFACE *interface_ptr,
72
                                                 UCHAR * interface_endpoint)
73
{
74
75
UX_ENDPOINT     *endpoint;
76
UX_ENDPOINT     *list_endpoint;
77
ULONG           endpoint_type;
78
ULONG           packet_size;
79
ULONG           n_tran;
80
81
    /* Obtain memory for storing this new endpoint.  */
82
2710
    endpoint =  (UX_ENDPOINT *) _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_ENDPOINT));
83
2710
    if (endpoint == UX_NULL)
84
4
        return(UX_MEMORY_INSUFFICIENT);
85
86
    /* If trace is enabled, insert this event into the trace buffer.  */
87
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_NEW_ENDPOINT_CREATE, interface_ptr, endpoint, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
88
89
    /* Save the endpoint handle in the container, this is for ensuring the
90
       endpoint container is not corrupted.  */
91
2706
    endpoint -> ux_endpoint =  (ULONG) (ALIGN_TYPE) endpoint;
92
93
    /* The endpoint container has a built in transfer_request.
94
       The transfer_request needs to point to the endpoint as well.  */
95
2706
    endpoint -> ux_endpoint_transfer_request.ux_transfer_request_endpoint =  endpoint;
96
97
    /* Save the pointer to the device. This is useful for the HCD layer.  */
98
2706
    endpoint -> ux_endpoint_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
99
100
    /* Parse the interface descriptor and make it machine independent.  */
101
2706
    _ux_utility_descriptor_parse(interface_endpoint,
102
                            _ux_system_endpoint_descriptor_structure,
103
                            UX_ENDPOINT_DESCRIPTOR_ENTRIES,
104
2706
                            (UCHAR *) &endpoint -> ux_endpoint_descriptor);
105
106
    /* Check endpoint size and interval to see if they are valid.  */
107
2706
    endpoint_type = endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE;
108
109
    /* Endpoint size should not be zero.  */
110
2706
    if (endpoint -> ux_endpoint_descriptor.wMaxPacketSize == 0)
111
    {
112
6
        _ux_utility_memory_free(endpoint);
113
6
        return(UX_DESCRIPTOR_CORRUPTED);
114
    }
115
116
    /* Control/bulk endpoint, 8, 16, 32, 64, ... 512 can be accepted.
117
       Note non-standard size in 2^N is accepted since they really works.  */
118

2700
    if (endpoint_type == UX_CONTROL_ENDPOINT || endpoint_type == UX_BULK_ENDPOINT)
119
    {
120
5234
        for (packet_size = 8; packet_size <= 512; packet_size <<= 1)
121
        {
122
5153
            if (packet_size == endpoint -> ux_endpoint_descriptor.wMaxPacketSize)
123
1181
                break;
124
        }
125
126
        /* If endpoint size not valid, return error.  */
127
1262
        if (packet_size > 512)
128
        {
129
81
            _ux_utility_memory_free(endpoint);
130
81
            return(UX_DESCRIPTOR_CORRUPTED);
131
        }
132
    }
133
134
    /* Interrupt/isochronous endpoint, max 1024 and 3 transactions can be accepted.  */
135
    else
136
    {
137
138
        /* Max size over 1024 is not allowed.  */
139
1438
        packet_size = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_PACKET_SIZE_MASK;
140
1438
        if (packet_size > 1024)
141
        {
142
9
            _ux_utility_memory_free(endpoint);
143
9
            return(UX_DESCRIPTOR_CORRUPTED);
144
        }
145
146
        /* Number transaction over 2 additional is not allowed.  */
147
1429
        n_tran = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_NUMBER_OF_TRANSACTIONS_MASK;
148
1429
        if (n_tran >= UX_MAX_NUMBER_OF_TRANSACTIONS_MASK)
149
        {
150
3
            _ux_utility_memory_free(endpoint);
151
3
            return(UX_DESCRIPTOR_CORRUPTED);
152
        }
153
154
        /* Isochronous/high speed interrupt interval should be 1~16.  */
155
1426
        if (endpoint -> ux_endpoint_descriptor.bInterval < 1)
156
        {
157
1
            _ux_utility_memory_free(endpoint);
158
1
            return(UX_DESCRIPTOR_CORRUPTED);
159
        }
160
1425
        if ((endpoint_type == UX_ISOCHRONOUS_ENDPOINT) ||
161
1360
            (interface_ptr -> ux_interface_configuration -> ux_configuration_device
162
1360
                                    -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
163
            )
164
        {
165
158
            if (endpoint -> ux_endpoint_descriptor.bInterval > 16)
166
            {
167
6
                _ux_utility_memory_free(endpoint);
168
6
                return(UX_DESCRIPTOR_CORRUPTED);
169
            }
170
        }
171
172
        /* Save final packet size.  */
173
1419
        n_tran >>= UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT;
174
1419
        packet_size *= (n_tran + 1);
175
    }
176
177
    /* Save transfer packet size.  */
178
2600
    endpoint -> ux_endpoint_transfer_request.ux_transfer_request_packet_length = packet_size;
179
180
    /* The interface that owns this endpoint is memorized in the
181
       endpoint container itself, easier for back chaining.  */
182
2600
    endpoint -> ux_endpoint_interface =  interface_ptr;
183
184
    /* There is 2 cases for the creation of the endpoint descriptor
185
       if this is the first one, the endpoint descriptor is hooked
186
       to the interface.
187
       If it is not the first one, the endpoint is hooked to the
188
       end of the chain of endpoints.  */
189
2600
    if (interface_ptr -> ux_interface_first_endpoint == UX_NULL)
190
    {
191
192
1705
        interface_ptr -> ux_interface_first_endpoint =  endpoint;
193
    }
194
    else
195
    {
196
197
895
        list_endpoint =  interface_ptr -> ux_interface_first_endpoint;
198
199
        /* Traverse the list until the end.  */
200
1139
        while (list_endpoint -> ux_endpoint_next_endpoint != UX_NULL)
201
244
            list_endpoint =  list_endpoint -> ux_endpoint_next_endpoint;
202
203
        /* Hook the endpoint.  */
204
895
        list_endpoint -> ux_endpoint_next_endpoint =  endpoint;
205
    }
206
207
    /* Return successful status.  */
208
2600
    return(UX_SUCCESS);
209
}
210