summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_cdc_ecm_thread.c
blob: 454349b869edebf9a93bc74c227053f5825e0404 (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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
/***************************************************************************
 * 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"


#if !defined(UX_HOST_STANDALONE)
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_cdc_ecm_thread                       PORTABLE C      */
/*                                                           6.2.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This is the CDC ECM thread that monitors the link change flag,      */
/*    receives data from the device, and passes the data to the NetX-USB  */
/*    broker.                                                             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    cdc_ecm                               CDC ECM instance              */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_cdc_ecm_transmit_queue_clean                         */
/*                                          Clean transmit queue          */
/*    _ux_host_stack_transfer_request       Transfer request              */
/*    _ux_host_semaphore_get                Get semaphore                 */
/*    _ux_host_semaphore_put                Put semaphore                 */
/*    _ux_utility_short_get_big_endian      Get 16-bit big endian         */
/*    _ux_network_driver_link_up            Set state link up             */
/*    _ux_network_driver_link_down          Set state link down           */
/*    _ux_network_driver_packet_received    Process received packet       */
/*    nx_packet_allocate                    Allocate NetX packet          */
/*    nx_packet_release                     Free NetX packet              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    CDC ECM class initialization                                        */
/*                                                                        */
/**************************************************************************/
VOID  _ux_host_class_cdc_ecm_thread(ULONG parameter)
{

UX_HOST_CLASS_CDC_ECM       *cdc_ecm;
UX_TRANSFER                 *transfer_request;
NX_PACKET                   *packet;
UINT                        status;
USB_NETWORK_DEVICE_TYPE     *usb_network_device_ptr;
ULONG                       packet_buffer_size;


    /* Cast the parameter passed in the thread into the cdc_ecm pointer.  */
    UX_THREAD_EXTENSION_PTR_GET(cdc_ecm, UX_HOST_CLASS_CDC_ECM, parameter)

    /* Loop forever waiting for changes signaled through the semaphore. */
    while (1)
    {

        /* Wait for the semaphore to be put by the cdc_ecm interrupt event.  */
        _ux_host_semaphore_get_norc(&cdc_ecm -> ux_host_class_cdc_ecm_interrupt_notification_semaphore, UX_WAIT_FOREVER);

        /* Check the link state. It is either pending up or down.  */
        if (cdc_ecm -> ux_host_class_cdc_ecm_link_state == UX_HOST_CLASS_CDC_ECM_LINK_STATE_PENDING_UP)
        {

            /* Now the link is up.  */
            cdc_ecm -> ux_host_class_cdc_ecm_link_state = UX_HOST_CLASS_CDC_ECM_LINK_STATE_UP;

            /* Communicate the state with the network driver.  */
            _ux_network_driver_link_up(cdc_ecm -> ux_host_class_cdc_ecm_network_handle);

            /* As long as we are connected, configured and link up ... do some work.... */
            while ((cdc_ecm -> ux_host_class_cdc_ecm_link_state == UX_HOST_CLASS_CDC_ECM_LINK_STATE_UP) &&
                   (cdc_ecm -> ux_host_class_cdc_ecm_device -> ux_device_state == UX_DEVICE_CONFIGURED))
            {

                /* Check if we have packet pool available.  */
                if (cdc_ecm -> ux_host_class_cdc_ecm_packet_pool == UX_NULL)
                {

                    /* Get the network device handle.  */
                    usb_network_device_ptr = (USB_NETWORK_DEVICE_TYPE *)(cdc_ecm -> ux_host_class_cdc_ecm_network_handle);

                    /* Check if IP instance is available.  */
                    if (usb_network_device_ptr -> ux_network_device_ip_instance != UX_NULL)
                    {

                        /* Get the packet pool from IP instance.  */
                        cdc_ecm -> ux_host_class_cdc_ecm_packet_pool = usb_network_device_ptr -> ux_network_device_ip_instance -> nx_ip_default_packet_pool;
                    }
                    else
                    {

                        /* IP instance is not available, wait for application to attach the interface.  */
                        _ux_utility_delay_ms(UX_HOST_CLASS_CDC_ECM_PACKET_POOL_INSTANCE_WAIT);
                    }
                    continue;
                }

                /* We can accept reception. Get a NX Packet. */
                status =  nx_packet_allocate(cdc_ecm -> ux_host_class_cdc_ecm_packet_pool, &packet,
                                             NX_RECEIVE_PACKET, UX_MS_TO_TICK(UX_HOST_CLASS_CDC_ECM_PACKET_POOL_WAIT));

                if (status == NX_SUCCESS)
                {

                    /* Adjust the prepend pointer to take into account the non 3 bit alignment of the ethernet header.  */
                    packet -> nx_packet_prepend_ptr += sizeof(USHORT);

                    /* We have a packet.  Link this packet to the reception transfer request on the bulk in endpoint. */
                    transfer_request =  &cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_endpoint -> ux_endpoint_transfer_request;

#ifdef UX_HOST_CLASS_CDC_ECM_PACKET_CHAIN_SUPPORT

                    /* Check packet buffer size, if too small chain is used.  */
                    packet_buffer_size = (ULONG)(packet -> nx_packet_data_end - packet -> nx_packet_prepend_ptr);
                    if (packet_buffer_size < UX_HOST_CLASS_CDC_ECM_NX_PAYLOAD_SIZE)
                    {
                        if (cdc_ecm -> ux_host_class_cdc_ecm_receive_buffer == UX_NULL)
                        {
                            cdc_ecm -> ux_host_class_cdc_ecm_receive_buffer =
                                    _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
                                                            UX_HOST_CLASS_CDC_ECM_NX_PAYLOAD_SIZE);
                            if (cdc_ecm -> ux_host_class_cdc_ecm_receive_buffer == UX_NULL)
                            {

                                /* Memory allocation fail.  */
                                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);

                                /* Release packet.  */
                                nx_packet_release(packet);

                                /* Delay to let other threads to run.  */
                                _ux_utility_delay_ms(1);
                                continue;
                            }

                        }

                        /* Set the data pointer.  */
                        transfer_request -> ux_transfer_request_data_pointer = cdc_ecm -> ux_host_class_cdc_ecm_receive_buffer;
                    }
                    else
#endif
                    {

                        /* Set the data pointer.  */
                        transfer_request -> ux_transfer_request_data_pointer =  packet -> nx_packet_prepend_ptr;

                    }

                    /* And length.  */
                    transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_CDC_ECM_NX_PAYLOAD_SIZE;
                    transfer_request -> ux_transfer_request_actual_length =     0;

                    /* Store the packet that owns this transaction.  */
                    transfer_request -> ux_transfer_request_user_specific = packet;

                    /* Reset the queue pointer of this packet.  */
                    packet -> nx_packet_queue_next =  UX_NULL;

                    /* We're arming the transfer now.  */
                    cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_transfer_check_and_arm_in_process =  UX_TRUE;

                    /* Is the link up?  */
                    if (cdc_ecm -> ux_host_class_cdc_ecm_link_state == UX_HOST_CLASS_CDC_ECM_LINK_STATE_UP)
                    {

                        /* Ask USB to schedule a reception.  */
                        status =  _ux_host_stack_transfer_request(transfer_request);

                        /* Signal that we are done arming and resume waiting thread if necessary.  */
                        cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_transfer_check_and_arm_in_process =  UX_FALSE;
                        if (cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_transfer_waiting_for_check_and_arm_to_finish == UX_TRUE)
                            _ux_host_semaphore_put(&cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_transfer_waiting_for_check_and_arm_to_finish_semaphore);

                        /* Check if the transaction was armed successfully.  */
                        if (status == UX_SUCCESS)
                        {

                            /* Wait for the completion of the transfer request.  */
                            _ux_host_semaphore_get_norc(&transfer_request -> ux_transfer_request_semaphore, UX_WAIT_FOREVER);

                            /* Check the transfer status. If there is a transport error, we ignore the packet
                               and restart it. */
                            if (transfer_request -> ux_transfer_request_completion_code == UX_SUCCESS)
                            {

#ifdef UX_HOST_CLASS_CDC_ECM_PACKET_CHAIN_SUPPORT

                                /* Check if transfer buffer is used.  */
                                if (packet -> nx_packet_prepend_ptr !=
                                    transfer_request -> ux_transfer_request_data_pointer)
                                {

                                    /* Adjust append_ptr for copy.  */
                                    packet -> nx_packet_append_ptr = packet -> nx_packet_prepend_ptr;

                                    /* Append data to packet.  */
                                    status = nx_packet_data_append(packet,
                                            transfer_request -> ux_transfer_request_data_pointer,
                                            transfer_request -> ux_transfer_request_actual_length,
                                            cdc_ecm -> ux_host_class_cdc_ecm_packet_pool,
                                            UX_MS_TO_TICK(UX_HOST_CLASS_CDC_ECM_PACKET_POOL_WAIT));
                                    if (status != NX_SUCCESS)
                                    {

                                        /* Release packet.  */
                                        nx_packet_release(packet);

                                        /* Error trap.  */
                                        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_ETH_PACKET_ERROR);
                                        continue;
                                    }
                                }
                                else
#endif
                                {

                                    /* Get the packet length. */
                                    packet -> nx_packet_length = transfer_request -> ux_transfer_request_actual_length;

                                    /* Adjust the prepend, length, and append fields.  */
                                    packet -> nx_packet_append_ptr =
                                        packet->nx_packet_prepend_ptr + transfer_request -> ux_transfer_request_actual_length;
                                }

                                /* Send that packet to the NetX USB broker.  */
                                _ux_network_driver_packet_received(cdc_ecm -> ux_host_class_cdc_ecm_network_handle, packet);
                            }
                            else
                            {

                                /* Free the packet that was not successfully received.  */
                                nx_packet_release(packet);
                            }
                        }
                        else
                        {

                            /* Error arming transfer.  */

                            /* Release packet.  */
                            nx_packet_release(packet);
                        }
                    }
                    else
                    {

                        /* Link is down.  */

                        /* Signal that we are done arming and resume waiting thread if necessary.  */
                        cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_transfer_check_and_arm_in_process =  UX_FALSE;
                        if (cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_transfer_waiting_for_check_and_arm_to_finish == UX_TRUE)
                            _ux_host_semaphore_put(&cdc_ecm -> ux_host_class_cdc_ecm_bulk_in_transfer_waiting_for_check_and_arm_to_finish_semaphore);

                        /* Release packet.  */
                        nx_packet_release(packet);
                    }
                }
                else
                {

                    /* Packet allocation timed out. Note that the timeout value is
                       configurable.  */

                    /* Error trap. No need for trace, since NetX does it.  */
                    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
                }
            }
        }
        else
        {

            /* The link state is pending down. We need to free the xmit queue.  */
            _ux_host_class_cdc_ecm_transmit_queue_clean(cdc_ecm);

            /* Link state can now be set to down.  */

            /* Notify the network driver.  */
            _ux_network_driver_link_down(cdc_ecm -> ux_host_class_cdc_ecm_network_handle);

            /* Set the link state.  */
            cdc_ecm -> ux_host_class_cdc_ecm_link_state =  UX_HOST_CLASS_CDC_ECM_LINK_STATE_DOWN;
        }
    }
}
#endif