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
|
/***************************************************************************
* 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 */
/** */
/** Device CDC_ECM Class */
/** */
/**************************************************************************/
/**************************************************************************/
#define UX_SOURCE_CODE
/* Include necessary system files. */
#include "ux_api.h"
#include "ux_device_class_cdc_ecm.h"
#include "ux_device_stack.h"
#if !defined(UX_DEVICE_STANDALONE)
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_device_class_cdc_ecm_bulkin_thread PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function is the thread of the cdc_ecm bulkin endpoint. The bulk*/
/* IN endpoint is used when the device wants to write data to be sent */
/* to the host. */
/* */
/* INPUT */
/* */
/* cdc_ecm_class Address of cdc_ecm class */
/* container */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* _ux_device_stack_transfer_request Request transfer */
/* _ux_utility_event_flags_get Get event flags */
/* _ux_device_mutex_on Take mutex */
/* _ux_device_mutex_off Free mutex */
/* */
/* CALLED BY */
/* */
/* ThreadX */
/* */
/**************************************************************************/
VOID _ux_device_class_cdc_ecm_bulkin_thread(ULONG cdc_ecm_class)
{
UX_SLAVE_CLASS *class_ptr;
UX_SLAVE_CLASS_CDC_ECM *cdc_ecm;
UX_SLAVE_DEVICE *device;
UX_SLAVE_TRANSFER *transfer_request;
UINT status;
ULONG actual_flags;
NX_PACKET *current_packet;
ULONG transfer_length;
ULONG copied;
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_CDC_ECM_ZERO_COPY) && !defined(NX_DISABLE_PACKET_CHAIN)
NX_PACKET *packet;
#endif
/* Cast properly the cdc_ecm instance. */
UX_THREAD_EXTENSION_PTR_GET(class_ptr, UX_SLAVE_CLASS, cdc_ecm_class)
/* Get the cdc_ecm instance from this class container. */
cdc_ecm = (UX_SLAVE_CLASS_CDC_ECM *) class_ptr -> ux_slave_class_instance;
/* Get the pointer to the device. */
device = &_ux_system_slave -> ux_system_slave_device;
/* This thread runs forever but can be suspended or resumed. */
while (1)
{
/* For as long we are configured. */
while (1)
{
/* Wait until either a new packet has been added to the xmit queue,
or until there has been a change in the device state (i.e. disconnection). */
_ux_utility_event_flags_get(&cdc_ecm -> ux_slave_class_cdc_ecm_event_flags_group, (UX_DEVICE_CLASS_CDC_ECM_NEW_BULKIN_EVENT |
UX_DEVICE_CLASS_CDC_ECM_NEW_DEVICE_STATE_CHANGE_EVENT),
UX_OR_CLEAR, &actual_flags, UX_WAIT_FOREVER);
/* Check the completion code and the actual flags returned. */
if ((actual_flags & UX_DEVICE_CLASS_CDC_ECM_NEW_DEVICE_STATE_CHANGE_EVENT) == 0)
{
/* Get the transfer request for the bulk IN pipe. */
transfer_request = &cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_endpoint -> ux_slave_endpoint_transfer_request;
/* Parse all packets. */
while (cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue != UX_NULL)
{
/* Ensure no other threads are modifying the xmit queue. */
_ux_device_mutex_on(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
/* Get the current packet in the list. */
current_packet = cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue;
/* Set the next packet (or a NULL value) as the head of the xmit queue. */
cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue = current_packet -> nx_packet_queue_next;
/* Free Mutex resource. */
_ux_device_mutex_off(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
/* If the link is down no need to rearm a packet. */
if (cdc_ecm -> ux_slave_class_cdc_ecm_link_state == UX_DEVICE_CLASS_CDC_ECM_LINK_STATE_UP)
{
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_CDC_ECM_ZERO_COPY)
/* Default to success. */
status = UX_SUCCESS;
#ifndef NX_DISABLE_PACKET_CHAIN
/* Check if packet is chained, allocate a new packet to fill the total data. */
if (current_packet -> nx_packet_next)
{
/* Check if collection of chained data can fit in one packet. */
if (current_packet -> nx_packet_length >
current_packet -> nx_packet_pool_owner -> nx_packet_pool_payload_size)
status = UX_TRANSFER_BUFFER_OVERFLOW;
else
{
/* Allocate a new packet for chain data collection. */
status = nx_packet_allocate(cdc_ecm -> ux_slave_class_cdc_ecm_packet_pool, &packet,
NX_RECEIVE_PACKET, UX_MS_TO_TICK(UX_DEVICE_CLASS_CDC_ECM_PACKET_POOL_WAIT));
if (status == UX_SUCCESS)
{
/* Copy the packet to the buffer. */
status = nx_packet_data_extract_offset(current_packet, 0,
packet -> nx_packet_prepend_ptr,
current_packet -> nx_packet_length, &copied);
if (status == NX_SUCCESS)
{
packet -> nx_packet_length = current_packet -> nx_packet_length;
/* Release the chained packet. */
nx_packet_transmit_release(current_packet);
/* Use copied packet instead. */
current_packet = packet;
}
}
}
/* Can not copy/buffer issue. */
if (status != UX_SUCCESS)
status = UX_TRANSFER_BUFFER_OVERFLOW;
}
#endif
if (status == UX_SUCCESS)
{
/* Set the transfer request data pointer to the packet buffer. */
transfer_request -> ux_slave_transfer_request_data_pointer = current_packet -> nx_packet_prepend_ptr;
/* Calculate the transfer length. */
transfer_length = current_packet -> nx_packet_length;
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ECM_PACKET_TRANSMIT, cdc_ecm, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
/* Send the request to the device controller. */
status = _ux_device_stack_transfer_request(transfer_request, transfer_length, transfer_length + 1);
}
/* Check error code. */
if (status != UX_SUCCESS)
{
/* Is this not a transfer abort? (this is expected to happen) */
if (status != UX_TRANSFER_BUS_RESET)
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
}
}
#else
/* Can the packet fit in the transfer requests data buffer? */
if (current_packet -> nx_packet_length <= UX_DEVICE_CLASS_CDC_ECM_BULKIN_BUFFER_SIZE)
{
/* Copy the packet in the transfer descriptor buffer. */
status = nx_packet_data_extract_offset(current_packet, 0,
transfer_request -> ux_slave_transfer_request_data_pointer,
current_packet -> nx_packet_length, &copied);
if (status == UX_SUCCESS)
{
/* Calculate the transfer length. */
transfer_length = current_packet -> nx_packet_length;
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ECM_PACKET_TRANSMIT, cdc_ecm, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
/* Send the request to the device controller. */
status = _ux_device_stack_transfer_request(transfer_request, transfer_length, UX_DEVICE_CLASS_CDC_ECM_BULKIN_BUFFER_SIZE + 1);
}
/* Check error code. */
if (status != UX_SUCCESS)
{
/* Is this not a transfer abort? (this is expected to happen) */
if (status != UX_TRANSFER_BUS_RESET)
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
}
}
}
else
{
/* Packet is too large. */
/* Report error to application. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_BUFFER_OVERFLOW);
}
#endif
}
/* Free the packet that was just sent. First do some housekeeping. */
current_packet -> nx_packet_prepend_ptr = current_packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_CDC_ECM_ETHERNET_SIZE;
current_packet -> nx_packet_length = current_packet -> nx_packet_length - UX_DEVICE_CLASS_CDC_ECM_ETHERNET_SIZE;
/* And ask Netx to release it. */
nx_packet_transmit_release(current_packet);
}
}
else
{
/* We need to ensure nobody is adding to the queue, so get the mutex protection. */
_ux_device_mutex_on(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
/* Since we got the mutex, we know no one is trying to modify the queue; we also know
no one can start modifying the queue since the link state is down, so we can just
release the mutex. */
_ux_device_mutex_off(&cdc_ecm -> ux_slave_class_cdc_ecm_mutex);
/* We get here when the link is down. All packets pending must be freed. */
while (cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue != UX_NULL)
{
/* Get the current packet in the list. */
current_packet = cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue;
/* Set the next packet (or a NULL value) as the head of the xmit queue. */
cdc_ecm -> ux_slave_class_cdc_ecm_xmit_queue = current_packet -> nx_packet_queue_next;
/* Free the packet. */
current_packet -> nx_packet_prepend_ptr = current_packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_CDC_ECM_ETHERNET_SIZE;
current_packet -> nx_packet_length = current_packet -> nx_packet_length - UX_DEVICE_CLASS_CDC_ECM_ETHERNET_SIZE;
/* And ask Netx to release it. */
nx_packet_transmit_release(current_packet);
}
/* Was the change in the device state caused by a disconnection? */
if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
{
/* Yes. Break out of the loop and suspend ourselves, waiting for the next configuration. */
break;
}
}
}
/* We need to suspend ourselves. We will be resumed by the device enumeration module or when a change of alternate setting happens. */
_ux_device_thread_suspend(&cdc_ecm -> ux_slave_class_cdc_ecm_bulkin_thread);
}
}
#endif
|