GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_new_endpoint_create.c Lines: 48 48 100.0 %
Date: 2024-12-12 17:16:36 Branches: 30 30 100.0 %

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

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