GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_cdc_ecm_activate.c Lines: 46 46 100.0 %
Date: 2026-03-06 18:57:10 Branches: 28 28 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
/** USBX Component                                                        */
15
/**                                                                       */
16
/**   Device CDC_ECM Class                                                */
17
/**                                                                       */
18
/**************************************************************************/
19
/**************************************************************************/
20
21
#define UX_SOURCE_CODE
22
23
24
/* Include necessary system files.  */
25
26
#include "ux_api.h"
27
#include "ux_device_class_cdc_ecm.h"
28
#include "ux_device_stack.h"
29
30
31
/**************************************************************************/
32
/*                                                                        */
33
/*  FUNCTION                                               RELEASE        */
34
/*                                                                        */
35
/*    _ux_device_class_cdc_ecm_activate                   PORTABLE C      */
36
/*                                                           6.3.0        */
37
/*  AUTHOR                                                                */
38
/*                                                                        */
39
/*    Chaoqiong Xiao, Microsoft Corporation                               */
40
/*                                                                        */
41
/*  DESCRIPTION                                                           */
42
/*                                                                        */
43
/*    This function activates the USB CDC_ECM device.                     */
44
/*                                                                        */
45
/*  INPUT                                                                 */
46
/*                                                                        */
47
/*    command                           Pointer to cdc_ecm command        */
48
/*                                                                        */
49
/*  OUTPUT                                                                */
50
/*                                                                        */
51
/*    Completion Status                                                   */
52
/*                                                                        */
53
/*  CALLS                                                                 */
54
/*                                                                        */
55
/*    None                                                                */
56
/*                                                                        */
57
/*  CALLED BY                                                             */
58
/*                                                                        */
59
/*    USBX Source Code                                                    */
60
/*                                                                        */
61
/**************************************************************************/
62
242
UINT  _ux_device_class_cdc_ecm_activate(UX_SLAVE_CLASS_COMMAND *command)
63
{
64
#if defined(UX_DEVICE_STANDALONE)
65
    UX_PARAMETER_NOT_USED(command);
66
    return(UX_FUNCTION_NOT_SUPPORTED);
67
#else
68
69
UX_SLAVE_INTERFACE          *interface_ptr;
70
UX_SLAVE_CLASS              *class_ptr;
71
UX_SLAVE_CLASS_CDC_ECM      *cdc_ecm;
72
UX_SLAVE_ENDPOINT           *endpoint;
73
ULONG                       physical_address_msw;
74
ULONG                       physical_address_lsw;
75
76
    /* Get the class container.  */
77
242
    class_ptr =  command -> ux_slave_class_command_class_ptr;
78
79
    /* Get the class instance in the container.  */
80
242
    cdc_ecm = (UX_SLAVE_CLASS_CDC_ECM *) class_ptr -> ux_slave_class_instance;
81
82
    /* Get the interface that owns this instance.  */
83
242
    interface_ptr =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
84
85
    /* Check if this is the Control or Data interface.  */
86
242
    if (command -> ux_slave_class_command_class == UX_DEVICE_CLASS_CDC_ECM_CLASS_COMMUNICATION_CONTROL)
87
    {
88
89
        /* Store the class instance into the interface.  */
90
121
        interface_ptr -> ux_slave_interface_class_instance =  (VOID *)cdc_ecm;
91
92
        /* Now the opposite, store the interface in the class instance.  */
93
121
        cdc_ecm -> ux_slave_class_cdc_ecm_interface =  interface_ptr;
94
95
        /* Locate the interrupt endpoint. */
96
121
        endpoint =  interface_ptr -> ux_slave_interface_first_endpoint;
97
98
        /* Parse all endpoints.  */
99
243
        while (endpoint != UX_NULL)
100
        {
101
102
            /* Check the endpoint direction, and type.  */
103
122
            if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN)
104
            {
105
106
                /* Look at type.  */
107
120
                if ((endpoint -> ux_slave_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT)
108
                {
109
110
                    /* We have found the interrupt endpoint, save it.  */
111
118
                    cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint =  endpoint;
112
#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
113
114
                    /* Set the endpoint buffer to the endpoint.  */
115
                    endpoint -> ux_slave_endpoint_transfer_request.
116
                        ux_slave_transfer_request_data_pointer =
117
                                UX_DEVICE_CLASS_CDC_ECM_INTERRUPTIN_BUFFER(cdc_ecm);
118
#endif
119
120
                    /* Reset the endpoint buffers.  */
121
118
                    _ux_utility_memory_set(cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_endpoint -> ux_slave_endpoint_transfer_request.
122
                                        ux_slave_transfer_request_data_pointer, 0, UX_DEVICE_CLASS_CDC_ECM_INTERRUPTIN_BUFFER_SIZE); /* Use case of memset is verified. */
123
124
                    /* Resume the interrupt endpoint threads.  */
125
118
                    _ux_device_thread_resume(&cdc_ecm -> ux_slave_class_cdc_ecm_interrupt_thread);
126
127
                }
128
129
            }
130
131
            /* Next endpoint.  */
132
122
            endpoint =  endpoint -> ux_slave_endpoint_next_endpoint;
133
        }
134
135
    }
136
    else
137
138
        /* This is the DATA Class, only store the cdc_ecm instance in the interface.  */
139
121
        interface_ptr -> ux_slave_interface_class_instance =  (VOID *)cdc_ecm;
140
141
    /* Reset the CDC ECM alternate setting to 0.  */
142
242
    cdc_ecm -> ux_slave_class_cdc_ecm_current_alternate_setting =  0;
143
144
    /* Check if this is the Control or Data interface.  */
145
242
    if (command -> ux_slave_class_command_class == UX_DEVICE_CLASS_CDC_ECM_CLASS_COMMUNICATION_DATA)
146
    {
147
148
        /* Reset endpoint instance pointers.  */
149
121
        cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint = UX_NULL;
150
121
        cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint = UX_NULL;
151
152
        /* Does the data class have bulk endpoint declared ? If yes we need to start link.
153
           If not, the host will change the alternate setting at a later stage.  */
154
121
        if (interface_ptr -> ux_slave_interface_descriptor.bNumEndpoints != 0)
155
        {
156
157
            /* Locate the endpoints.  Control and Bulk in/out for Data Interface.  */
158
9
            endpoint =  interface_ptr -> ux_slave_interface_first_endpoint;
159
160
            /* Parse all endpoints.  */
161
27
            while (endpoint != UX_NULL)
162
            {
163
164
                /* Check the endpoint direction, and type.  */
165
18
                if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN)
166
                {
167
168
                    /* Look at type.  */
169
9
                    if ((endpoint -> ux_slave_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT)
170
                    {
171
172
                        /* We have found the bulk in endpoint, save it.  */
173
6
                        cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint =  endpoint;
174
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && !defined(UX_DEVICE_CLASS_CDC_ECM_ZERO_COPY)
175
                        endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
176
                                UX_DEVICE_CLASS_CDC_ECM_BULKIN_BUFFER(cdc_ecm);
177
#endif
178
                    }
179
180
                }
181
                else
182
                {
183
                    /* Look at type for out endpoint.  */
184
9
                    if ((endpoint -> ux_slave_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT)
185
                    {
186
187
                        /* We have found the bulk out endpoint, save it.  */
188
6
                        cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint =  endpoint;
189
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && !defined(UX_DEVICE_CLASS_CDC_ECM_ZERO_COPY)
190
                        endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
191
                                UX_DEVICE_CLASS_CDC_ECM_BULKOUT_BUFFER(cdc_ecm);
192
#endif
193
                    }
194
                }
195
196
                /* Next endpoint.  */
197
18
                endpoint =  endpoint -> ux_slave_endpoint_next_endpoint;
198
            }
199
200
201
            /* Now check if all endpoints have been found.  */
202

9
            if (cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint == UX_NULL || cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint == UX_NULL)
203
204
                /* Not all endpoints have been found. Major error, do not proceed.  */
205
6
                return(UX_ERROR);
206
207
            /* Declare the link to be up. That may need to change later to make it dependent on the
208
               WAN/Wireless modem.  */
209
3
            cdc_ecm -> ux_slave_class_cdc_ecm_link_state = UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP;
210
211
            /* Wake up the Interrupt thread and send a network notification to the host.  */
212
3
            _ux_device_event_flags_set(&cdc_ecm -> ux_slave_class_cdc_ecm_event_flags_group, UX_DEVICE_CLASS_CDC_ECM_NETWORK_NOTIFICATION_EVENT, UX_OR);
213
214
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_CDC_ECM_ZERO_COPY)
215
216
            /* There is no endpoint buffer.  */
217
#else
218
219
            /* Reset the endpoint buffers.  */
220
3
            _ux_utility_memory_set(cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_endpoint -> ux_slave_endpoint_transfer_request.
221
                                            ux_slave_transfer_request_data_pointer, 0, UX_DEVICE_CLASS_CDC_ECM_BULKOUT_BUFFER_SIZE); /* Use case of memset is verified. */
222
3
            _ux_utility_memory_set(cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint -> ux_slave_endpoint_transfer_request.
223
                                            ux_slave_transfer_request_data_pointer, 0, UX_DEVICE_CLASS_CDC_ECM_BULKIN_BUFFER_SIZE); /* Use case of memset is verified. */
224
#endif
225
226
            /* Resume the endpoint threads.  */
227
3
            _ux_device_thread_resume(&cdc_ecm -> ux_slave_class_cdc_ecm_bulkout_thread);
228
3
            _ux_device_thread_resume(&cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_thread);
229
230
        }
231
232
        /* Setup the physical address of this IP instance.  */
233
115
        physical_address_msw =  (ULONG)((cdc_ecm -> ux_slave_class_cdc_ecm_local_node_id[0] << 8) | (cdc_ecm -> ux_slave_class_cdc_ecm_local_node_id[1]));
234
115
        physical_address_lsw =  (ULONG)((cdc_ecm -> ux_slave_class_cdc_ecm_local_node_id[2] << 24) | (cdc_ecm -> ux_slave_class_cdc_ecm_local_node_id[3] << 16) |
235
115
                                                       (cdc_ecm -> ux_slave_class_cdc_ecm_local_node_id[4] << 8) | (cdc_ecm -> ux_slave_class_cdc_ecm_local_node_id[5]));
236
237
        /* Register this interface to the NetX USB interface broker.  */
238
115
        _ux_network_driver_activate((VOID *) cdc_ecm, _ux_device_class_cdc_ecm_write,
239
                                        &cdc_ecm -> ux_slave_class_cdc_ecm_network_handle,
240
                                        physical_address_msw,
241
                                        physical_address_lsw);
242
243
        /* Check Link.  */
244
115
        if (cdc_ecm -> ux_slave_class_cdc_ecm_link_state == UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP)
245
        {
246
247
            /* Communicate the state with the network driver.  */
248
3
            _ux_network_driver_link_up(cdc_ecm -> ux_slave_class_cdc_ecm_network_handle);
249
250
            /* If there is an activate function call it.  */
251
3
            if (cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_activate != UX_NULL)
252
253
                /* Invoke the application.  */
254
1
                cdc_ecm -> ux_slave_class_cdc_ecm_parameter.ux_slave_class_cdc_ecm_instance_activate(cdc_ecm);
255
        }
256
    }
257
258
    /* If trace is enabled, insert this event into the trace buffer.  */
259
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ECM_ACTIVATE, cdc_ecm, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
260
261
    /* If trace is enabled, register this object.  */
262
    UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, cdc_ecm, 0, 0, 0)
263
264
    /* Return completion status.  */
265
236
    return(UX_SUCCESS);
266
#endif
267
}