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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
|
/***************************************************************************
* 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 RNDIS Class */
/** */
/**************************************************************************/
/**************************************************************************/
#define UX_SOURCE_CODE
/* Include necessary system files. */
#include "ux_api.h"
#include "ux_device_class_rndis.h"
#include "ux_device_stack.h"
#if !defined(UX_DEVICE_STANDALONE)
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_device_class_rndis_bulkin_thread PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function is the thread of the rndis bulkin endpoint. The bulk */
/* IN endpoint is used when the device wants to write data to be sent */
/* to the host. */
/* */
/* INPUT */
/* */
/* rndis_class Address of rndis 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 Release mutex */
/* _ux_utility_long_put Put 32-bit value */
/* nx_packet_transmit_release Release NetX packet */
/* */
/* CALLED BY */
/* */
/* ThreadX */
/* */
/**************************************************************************/
VOID _ux_device_class_rndis_bulkin_thread(ULONG rndis_class)
{
UX_SLAVE_CLASS *class_ptr;
UX_SLAVE_CLASS_RNDIS *rndis;
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_RNDIS_ZERO_COPY) && !defined(NX_DISABLE_PACKET_CHAIN)
NX_PACKET *packet;
UINT do_copy;
#endif
/* Cast properly the rndis instance. */
UX_THREAD_EXTENSION_PTR_GET(class_ptr, UX_SLAVE_CLASS, rndis_class)
/* Get the rndis instance from this class container. */
rndis = (UX_SLAVE_CLASS_RNDIS *) 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)
{
/* Get the transfer request for the bulk IN pip. */
transfer_request = &rndis -> ux_slave_class_rndis_bulkin_endpoint -> ux_slave_endpoint_transfer_request;
/* As long as the device is in the CONFIGURED state. */
while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
{
/* Wait until we have a event sent by the application. We do not treat yet the case where a timeout based
on the interrupt pipe frequency or a change in the idle state forces us to send an empty report. */
status = _ux_utility_event_flags_get(&rndis -> ux_slave_class_rndis_event_flags_group, (UX_DEVICE_CLASS_RNDIS_NEW_BULKIN_EVENT |
UX_DEVICE_CLASS_RNDIS_NEW_DEVICE_STATE_CHANGE_EVENT),
UX_OR_CLEAR, &actual_flags, UX_WAIT_FOREVER);
/* Check the completion code and the actual flags returned. */
if (status == UX_SUCCESS && (actual_flags & UX_DEVICE_CLASS_RNDIS_NEW_DEVICE_STATE_CHANGE_EVENT) == 0)
{
/* Parse all packets. */
while(rndis -> ux_slave_class_rndis_xmit_queue != UX_NULL)
{
/* Protect this thread. */
_ux_device_mutex_on(&rndis -> ux_slave_class_rndis_mutex);
/* Get the current packet in the list. */
current_packet = rndis -> ux_slave_class_rndis_xmit_queue;
/* Set the next packet (or a NULL value) as the head of the xmit queue. */
rndis -> ux_slave_class_rndis_xmit_queue = current_packet -> nx_packet_queue_next;
/* Free Mutex resource. */
_ux_device_mutex_off(&rndis -> ux_slave_class_rndis_mutex);
/* If the link is down no need to rearm a packet. */
if (rndis -> ux_slave_class_rndis_link_state == UX_DEVICE_CLASS_RNDIS_LINK_STATE_UP)
{
/* Input packet mapping:
* | <----- nx_packet_length -----> |
* .. NX_PHYSICAL_HEADER? | NX_ETHERNET_SIZE | Packet ... |
* start prepend append
*/
/* Calculate the transfer length. */
transfer_length = current_packet -> nx_packet_length + UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH;
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_RNDIS_ZERO_COPY)
/* Default to success. */
status = UX_SUCCESS;
/* Check if there was enough space for RNDIS header, if not data should be copied. */
do_copy = (current_packet -> nx_packet_data_start + UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH > current_packet -> nx_packet_prepend_ptr);
#ifndef NX_DISABLE_PACKET_CHAIN
/* Check if packet is chained, chained packets must be joined (copied). */
if (current_packet -> nx_packet_next)
do_copy = 1;
#endif
/* Check if data is being copied to new packet. */
if (do_copy)
{
/* Check if packet pool is good for data collection. */
if (transfer_length > current_packet -> nx_packet_pool_owner -> nx_packet_pool_payload_size)
status = UX_TRANSFER_BUFFER_OVERFLOW;
/* Allocate a new packet for data collection. */
if (status == UX_SUCCESS)
{
status = nx_packet_allocate(current_packet -> nx_packet_pool_owner,
&packet, UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH,
UX_MS_TO_TICK(UX_DEVICE_CLASS_RNDIS_PACKET_POOL_WAIT));
/* Reserve space for RNDIS header. */
packet -> nx_packet_append_ptr = packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH;
}
/* Copy the data to allocated. */
if (status == UX_SUCCESS)
{
/* RNDIS packet header already reserved. */
/* Copy the data to allocated. */
status = nx_packet_data_extract_offset(current_packet, 0,
packet -> nx_packet_append_ptr,
current_packet -> nx_packet_length, &copied);
if (status == NX_SUCCESS)
{
packet -> nx_packet_length = copied;
/* Release the chained packet. */
current_packet -> nx_packet_length = current_packet -> nx_packet_length - UX_DEVICE_CLASS_RNDIS_ETHERNET_SIZE;
nx_packet_transmit_release(current_packet);
/* Use copied packet to transfer. */
current_packet = packet;
}
}
/* Can not copy/buffer issue. */
if (status != UX_SUCCESS)
status = UX_TRANSFER_BUFFER_OVERFLOW;
}
else
{
/* There is enough space for RNDIS header, move prepend_ptr for it. */
current_packet -> nx_packet_prepend_ptr -= UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH;
}
if (status == UX_SUCCESS)
{
/* Add the RNDIS header to this packet. */
/* Reset the RNDIS header. */
_ux_utility_memory_set(current_packet -> nx_packet_prepend_ptr, 0x00, UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH); /* Use case of memset is verified. */
/* Initialize fields in header. */
_ux_utility_long_put(current_packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_RNDIS_PACKET_MESSAGE_TYPE, UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_MSG);
_ux_utility_long_put(current_packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_RNDIS_PACKET_MESSAGE_LENGTH, transfer_length);
_ux_utility_long_put(current_packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_RNDIS_PACKET_DATA_OFFSET,
UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH - UX_DEVICE_CLASS_RNDIS_PACKET_DATA_OFFSET);
_ux_utility_long_put(current_packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_RNDIS_PACKET_DATA_LENGTH, current_packet -> nx_packet_length);
/* Set the transfer request data pointer to the packet buffer. */
transfer_request -> ux_slave_transfer_request_data_pointer = current_packet -> nx_packet_prepend_ptr;
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_RNDIS_PACKET_TRANSMIT, rndis, 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? (it's fine to be aborted) */
if (status != UX_TRANSFER_BUS_RESET)
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
}
}
#else
/* Is there enough space for this packet in the transfer buffer? */
if (transfer_length <= UX_DEVICE_CLASS_RNDIS_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 +
UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH,
current_packet -> nx_packet_length, &copied);
if (status == NX_SUCCESS)
{
/* Add the RNDIS header to this packet. */
_ux_utility_long_put(transfer_request -> ux_slave_transfer_request_data_pointer + UX_DEVICE_CLASS_RNDIS_PACKET_MESSAGE_TYPE, UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_MSG);
_ux_utility_long_put(transfer_request -> ux_slave_transfer_request_data_pointer + UX_DEVICE_CLASS_RNDIS_PACKET_MESSAGE_LENGTH, transfer_length);
_ux_utility_long_put(transfer_request -> ux_slave_transfer_request_data_pointer + UX_DEVICE_CLASS_RNDIS_PACKET_DATA_OFFSET,
UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH - UX_DEVICE_CLASS_RNDIS_PACKET_DATA_OFFSET);
_ux_utility_long_put(transfer_request -> ux_slave_transfer_request_data_pointer + UX_DEVICE_CLASS_RNDIS_PACKET_DATA_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_RNDIS_PACKET_TRANSMIT, rndis, 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_RNDIS_BULKIN_BUFFER_SIZE + 1);
}
/* Check for error. */
if (status != UX_SUCCESS)
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
}
else
{
/* No, there is not enough space. */
/* Report error to application. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
}
#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_RNDIS_ETHERNET_SIZE;
current_packet -> nx_packet_length = current_packet -> nx_packet_length - UX_DEVICE_CLASS_RNDIS_ETHERNET_SIZE;
/* And ask Netx to release it. */
nx_packet_transmit_release(current_packet);
}
}
else
{
/* We get here when the link is down or the last transmission failed. All packets pending must be freed. */
while(rndis -> ux_slave_class_rndis_xmit_queue != UX_NULL)
{
/* Protect the chain of packets. */
_ux_device_mutex_on(&rndis -> ux_slave_class_rndis_mutex);
/* Get the current packet in the list. */
current_packet = rndis -> ux_slave_class_rndis_xmit_queue;
/* Set the next packet (or a NULL value) as the head of the xmit queue. */
rndis -> ux_slave_class_rndis_xmit_queue = current_packet -> nx_packet_queue_next;
/* Free Mutex resource. */
_ux_device_mutex_off(&rndis -> ux_slave_class_rndis_mutex);
/* Free the packet */
current_packet -> nx_packet_prepend_ptr = current_packet -> nx_packet_prepend_ptr + UX_DEVICE_CLASS_RNDIS_ETHERNET_SIZE;
current_packet -> nx_packet_length = current_packet -> nx_packet_length - UX_DEVICE_CLASS_RNDIS_ETHERNET_SIZE;
/* And ask Netx to release it. */
nx_packet_transmit_release(current_packet);
}
}
}
/* We need to suspend ourselves. We will be resumed by the device enumeration module. */
_ux_device_thread_suspend(&rndis -> ux_slave_class_rndis_bulkin_thread);
}
}
#endif
|