GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_cdc_ecm_endpoints_get.c Lines: 60 63 95.2 %
Date: 2024-12-12 17:16:36 Branches: 37 40 92.5 %

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
/**   CDC ECM Class                                                       */
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_class_cdc_ecm.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_class_cdc_ecm_endpoints_get                PORTABLE C      */
37
/*                                                           6.3.0        */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function distinguishes for either the Data or Control Class.   */
45
/*    For the data class, we mount the bulk in and bulk out endpoints.    */
46
/*    For the control class, we mount the optional interrupt endpoint.    */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    cdc_ecm                                Pointer to cdc_ecm class     */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    Completion Status                                                   */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    _ux_host_stack_interface_endpoint_get Get interface endpoint        */
59
/*                                                                        */
60
/*  CALLED BY                                                             */
61
/*                                                                        */
62
/*    _ux_host_class_cdc_ecm_activate        Activate cdc_ecm class       */
63
/*                                                                        */
64
/*  RELEASE HISTORY                                                       */
65
/*                                                                        */
66
/*    DATE              NAME                      DESCRIPTION             */
67
/*                                                                        */
68
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70
/*                                            resulting in version 6.1    */
71
/*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
72
/*                                            use pre-calculated value    */
73
/*                                            instead of wMaxPacketSize,  */
74
/*                                            resulting in version 6.1.9  */
75
/*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
76
/*                                            checked endpoint get status,*/
77
/*                                            resulting in version 6.3.0  */
78
/*                                                                        */
79
/**************************************************************************/
80
121
UINT  _ux_host_class_cdc_ecm_endpoints_get(UX_HOST_CLASS_CDC_ECM *cdc_ecm)
81
{
82
83
UINT            status;
84
UINT            endpoint_index;
85
UX_ENDPOINT     *endpoint;
86
UX_TRANSFER     *transfer_request;
87
UX_INTERFACE    *data_interface;
88
89
90
    /* Get the endpoints from the data interface.  */
91
92
    /* Default data interface.  */
93
121
    data_interface =  cdc_ecm -> ux_host_class_cdc_ecm_interface_data;
94
95
    /* Some versions of cdc-ecm contain a default interface for data with 0 endpoints. Check if this the case and if so,
96
       look for the next interface that has the 2 bulk endpoints.  */
97
98
121
    if (data_interface -> ux_interface_descriptor.bNumEndpoints == 0)
99
    {
100
101
        /* We are in the case where the interface has the default set to 0 endpoints.  */
102
114
        data_interface =  data_interface -> ux_interface_next_interface;
103
104
        /* Check if invalid.  */
105
114
        if (data_interface == UX_NULL ||
106
111
            data_interface -> ux_interface_descriptor.bInterfaceClass != UX_HOST_CLASS_CDC_DATA_CLASS ||
107
108
            data_interface -> ux_interface_descriptor.bAlternateSetting != 1)
108
        {
109
110
            /* Error trap. */
111
9
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
112
113
            /* If trace is enabled, insert this event into the trace buffer.  */
114
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, cdc_ecm -> ux_host_class_cdc_ecm_interface_data, 0, 0, UX_TRACE_ERRORS, 0, 0)
115
116
            /* Descriptor is corrupted.  */
117
9
            return(UX_DESCRIPTOR_CORRUPTED);
118
        }
119
120
        /* We have found the right alternate setting. Now we need to select it. */
121
105
        status = _ux_host_stack_interface_setting_select(data_interface);
122
123
        /* Check status. We don't continue if there is a problem with the selection.  */
124
105
        if (status != UX_SUCCESS)
125
126
            /* Something went wrong.  */
127
13
            return(status);
128
    }
129
130
    /* Search the bulk OUT endpoint. It is attached to the interface container.  */
131
112
    for (endpoint_index = 0; endpoint_index < data_interface -> ux_interface_descriptor.bNumEndpoints;
132
13
                        endpoint_index++)
133
    {
134
135
        /* Get interface endpoint.  */
136
107
        status = _ux_host_stack_interface_endpoint_get(data_interface, endpoint_index, &endpoint);
137
138
        /* Check status.  */
139
107
        if (status != UX_SUCCESS)
140
            continue;
141
142
        /* Check if endpoint is bulk and OUT.  */
143
107
        if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
144
97
            ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
145
        {
146
147
            /* This transfer_request always have the OUT direction.  */
148
94
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;
149
150
            /* There is a callback function associated with the transfer request, so we need the class instance.  */
151
94
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_class_instance =  (VOID *) cdc_ecm;
152
153
            /* The transfer request has a callback function.  */
154
94
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_function =  _ux_host_class_cdc_ecm_transmission_callback;
155
156
            /* We have found the bulk endpoint, save it.  */
157
94
            cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_endpoint =  endpoint;
158
159
94
            break;
160
        }
161
    }
162
163
    /* The bulk out endpoint is mandatory.  */
164
99
    if (cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_endpoint == UX_NULL)
165
    {
166
167
        /* Error trap. */
168
5
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
169
170
        /* If trace is enabled, insert this event into the trace buffer.  */
171
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, cdc_ecm, 0, 0, UX_TRACE_ERRORS, 0, 0)
172
173
5
        return(UX_ENDPOINT_HANDLE_UNKNOWN);
174
    }
175
176
    /* Search the bulk IN endpoint. It is attached to the interface container.  */
177
194
    for (endpoint_index = 0; endpoint_index < data_interface -> ux_interface_descriptor.bNumEndpoints;
178
100
                        endpoint_index++)
179
    {
180
181
        /* Get the endpoint handle.  */
182
189
        status = _ux_host_stack_interface_endpoint_get(data_interface, endpoint_index, &endpoint);
183
184
        /* Check status.  */
185
189
        if (status != UX_SUCCESS)
186
            continue;
187
188
        /* Check if endpoint is bulk and IN.  */
189
189
        if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
190
92
            ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
191
        {
192
193
            /* This transfer_request always have the IN direction.  */
194
89
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
195
196
            /* Set the class instance in the transfer request.  */
197
89
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_class_instance =  (VOID *) cdc_ecm;
198
199
            /* The transfer request has NO callback function.  */
200
89
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_function =  UX_NULL;
201
202
            /* We have found the bulk endpoint, save it.  */
203
89
            cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_endpoint =  endpoint;
204
205
89
            break;
206
        }
207
    }
208
209
    /* The bulk in endpoint is mandatory.  */
210
94
    if (cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_endpoint == UX_NULL)
211
    {
212
213
        /* Error trap. */
214
5
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
215
216
        /* If trace is enabled, insert this event into the trace buffer.  */
217
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, cdc_ecm, 0, 0, UX_TRACE_ERRORS, 0, 0)
218
219
5
        return(UX_ENDPOINT_HANDLE_UNKNOWN);
220
    }
221
222
    /* Now get the endpoints from the control interface.  */
223
224
    /* Search the Interrupt endpoint. It is NOT mandatory.  */
225
93
    for (endpoint_index = 0; endpoint_index < cdc_ecm -> ux_host_class_cdc_ecm_interface_control -> ux_interface_descriptor.bNumEndpoints;
226
4
                        endpoint_index++)
227
    {
228
229
        /* Get the endpoint handle.  */
230
90
        status = _ux_host_stack_interface_endpoint_get(cdc_ecm -> ux_host_class_cdc_ecm_interface_control, endpoint_index, &endpoint);
231
232
        /* Check status.  */
233
90
        if (status != UX_SUCCESS)
234
            continue;
235
236
        /* Check if endpoint is Interrupt and IN.  */
237
90
        if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
238
88
            ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
239
        {
240
241
            /* This transfer_request always have the IN direction.  */
242
86
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
243
244
            /* We have found the interrupt endpoint, save it.  */
245
86
            cdc_ecm -> ux_host_class_cdc_ecm_interrupt_endpoint =  endpoint;
246
247
            /* The endpoint is correct, Fill in the transfer request with the length requested for this endpoint.  */
248
86
            transfer_request =  &cdc_ecm -> ux_host_class_cdc_ecm_interrupt_endpoint -> ux_endpoint_transfer_request;
249
86
            transfer_request -> ux_transfer_request_requested_length =  transfer_request -> ux_transfer_request_packet_length;
250
86
            transfer_request -> ux_transfer_request_actual_length =     0;
251
252
            /* The direction is always IN for the CDC interrupt endpoint.  */
253
86
            transfer_request -> ux_transfer_request_type =  UX_REQUEST_IN;
254
255
            /* There is a callback function associated with the transfer request, so we need the class instance.  */
256
86
            transfer_request -> ux_transfer_request_class_instance =  (VOID *) cdc_ecm;
257
258
            /* Interrupt transactions have a completion routine. */
259
86
            transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_cdc_ecm_interrupt_notification;
260
261
            /* Obtain a buffer for this transaction. The buffer will always be reused.  */
262
86
            transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
263
                                                            transfer_request -> ux_transfer_request_requested_length);
264
265
            /* If the endpoint is available and we have memory, we start the interrupt endpoint.  */
266
86
            if (transfer_request -> ux_transfer_request_data_pointer == UX_NULL)
267
            {
268
269
                /* Error trap. */
270
1
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
271
272
                /* If trace is enabled, insert this event into the trace buffer.  */
273
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
274
275
                /* We must return an error.  */
276
1
                return(UX_ENDPOINT_HANDLE_UNKNOWN);
277
            }
278
279
85
            break;
280
        }
281
    }
282
283
    /* All endpoints have been mounted.  */
284
88
    return(UX_SUCCESS);
285
}
286