summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_cdc_ecm_endpoints_get.c
blob: 59811dcd91eecc7e13dffa3832eccea2929c9d97 (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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   CDC ECM Class                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

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


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_cdc_ecm_endpoints_get                PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function distinguishes for either the Data or Control Class.   */
/*    For the data class, we mount the bulk in and bulk out endpoints.    */
/*    For the control class, we mount the optional interrupt endpoint.    */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cdc_ecm                                Pointer to cdc_ecm class     */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_interface_endpoint_get Get interface endpoint        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    _ux_host_class_cdc_ecm_activate        Activate cdc_ecm class       */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_cdc_ecm_endpoints_get(UX_HOST_CLASS_CDC_ECM *cdc_ecm)
{

UINT            status;
UINT            endpoint_index;
UX_ENDPOINT     *endpoint;
UX_TRANSFER     *transfer_request;
UX_INTERFACE    *data_interface;


    /* Get the endpoints from the data interface.  */

    /* Default data interface.  */
    data_interface =  cdc_ecm -> ux_host_class_cdc_ecm_interface_data;

    /* Some versions of cdc-ecm contain a default interface for data with 0 endpoints. Check if this the case and if so,
       look for the next interface that has the 2 bulk endpoints.  */

    if (data_interface -> ux_interface_descriptor.bNumEndpoints == 0)
    {

        /* We are in the case where the interface has the default set to 0 endpoints.  */
        data_interface =  data_interface -> ux_interface_next_interface;

        /* Check if invalid.  */
        if (data_interface == UX_NULL ||
            data_interface -> ux_interface_descriptor.bInterfaceClass != UX_HOST_CLASS_CDC_DATA_CLASS ||
            data_interface -> ux_interface_descriptor.bAlternateSetting != 1)
        {

            /* Error trap. */
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);

            /* If trace is enabled, insert this event into the trace buffer.  */
            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)

            /* Descriptor is corrupted.  */
            return(UX_DESCRIPTOR_CORRUPTED);
        }

        /* We have found the right alternate setting. Now we need to select it. */
        status = _ux_host_stack_interface_setting_select(data_interface);

        /* Check status. We don't continue if there is a problem with the selection.  */
        if (status != UX_SUCCESS)

            /* Something went wrong.  */
            return(status);
    }

    /* Search the bulk OUT endpoint. It is attached to the interface container.  */
    for (endpoint_index = 0; endpoint_index < data_interface -> ux_interface_descriptor.bNumEndpoints;
                        endpoint_index++)
    {

        /* Get interface endpoint.  */
        status = _ux_host_stack_interface_endpoint_get(data_interface, endpoint_index, &endpoint);

        /* Check status.  */
        if (status != UX_SUCCESS)
            continue;

        /* Check if endpoint is bulk and OUT.  */
        if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
            ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
        {

            /* This transfer_request always have the OUT direction.  */
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;

            /* There is a callback function associated with the transfer request, so we need the class instance.  */
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_class_instance =  (VOID *) cdc_ecm;

            /* The transfer request has a callback function.  */
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_function =  _ux_host_class_cdc_ecm_transmission_callback;

            /* We have found the bulk endpoint, save it.  */
            cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_endpoint =  endpoint;

            break;
        }
    }

    /* The bulk out endpoint is mandatory.  */
    if (cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_endpoint == UX_NULL)
    {

        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, cdc_ecm, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_ENDPOINT_HANDLE_UNKNOWN);
    }

    /* Search the bulk IN endpoint. It is attached to the interface container.  */
    for (endpoint_index = 0; endpoint_index < data_interface -> ux_interface_descriptor.bNumEndpoints;
                        endpoint_index++)
    {

        /* Get the endpoint handle.  */
        status = _ux_host_stack_interface_endpoint_get(data_interface, endpoint_index, &endpoint);

        /* Check status.  */
        if (status != UX_SUCCESS)
            continue;

        /* Check if endpoint is bulk and IN.  */
        if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
            ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
        {

            /* This transfer_request always have the IN direction.  */
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;

            /* Set the class instance in the transfer request.  */
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_class_instance =  (VOID *) cdc_ecm;

            /* The transfer request has NO callback function.  */
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_function =  UX_NULL;

            /* We have found the bulk endpoint, save it.  */
            cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_endpoint =  endpoint;

            break;
        }
    }

    /* The bulk in endpoint is mandatory.  */
    if (cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_endpoint == UX_NULL)
    {

        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, cdc_ecm, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_ENDPOINT_HANDLE_UNKNOWN);
    }

    /* Now get the endpoints from the control interface.  */

    /* Search the Interrupt endpoint. It is NOT mandatory.  */
    for (endpoint_index = 0; endpoint_index < cdc_ecm -> ux_host_class_cdc_ecm_interface_control -> ux_interface_descriptor.bNumEndpoints;
                        endpoint_index++)
    {

        /* Get the endpoint handle.  */
        status = _ux_host_stack_interface_endpoint_get(cdc_ecm -> ux_host_class_cdc_ecm_interface_control, endpoint_index, &endpoint);

        /* Check status.  */
        if (status != UX_SUCCESS)
            continue;

        /* Check if endpoint is Interrupt and IN.  */
        if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
            ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
        {

            /* This transfer_request always have the IN direction.  */
            endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;

            /* We have found the interrupt endpoint, save it.  */
            cdc_ecm -> ux_host_class_cdc_ecm_interrupt_endpoint =  endpoint;

            /* The endpoint is correct, Fill in the transfer request with the length requested for this endpoint.  */
            transfer_request =  &cdc_ecm -> ux_host_class_cdc_ecm_interrupt_endpoint -> ux_endpoint_transfer_request;
            transfer_request -> ux_transfer_request_requested_length =  transfer_request -> ux_transfer_request_packet_length;
            transfer_request -> ux_transfer_request_actual_length =     0;

            /* The direction is always IN for the CDC interrupt endpoint.  */
            transfer_request -> ux_transfer_request_type =  UX_REQUEST_IN;

            /* There is a callback function associated with the transfer request, so we need the class instance.  */
            transfer_request -> ux_transfer_request_class_instance =  (VOID *) cdc_ecm;

            /* Interrupt transactions have a completion routine. */
            transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_cdc_ecm_interrupt_notification;

            /* Obtain a buffer for this transaction. The buffer will always be reused.  */
            transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
                                                            transfer_request -> ux_transfer_request_requested_length);

            /* If the endpoint is available and we have memory, we start the interrupt endpoint.  */
            if (transfer_request -> ux_transfer_request_data_pointer == UX_NULL)
            {

                /* Error trap. */
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);

                /* If trace is enabled, insert this event into the trace buffer.  */
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)

                /* We must return an error.  */
                return(UX_ENDPOINT_HANDLE_UNKNOWN);
            }

            break;
        }
    }

    /* All endpoints have been mounted.  */
    return(UX_SUCCESS);
}