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
|
/***************************************************************************
* 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_bulkout_thread PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function is the thread of the rndis bulk out endpoint. It */
/* is waiting for the host to send data on the bulk out endpoint to */
/* the device. */
/* */
/* INPUT */
/* */
/* rndis_class Address of rndis class */
/* container */
/* */
/* OUTPUT */
/* */
/* None */
/* */
/* CALLS */
/* */
/* _ux_device_stack_transfer_request Request transfer */
/* _ux_network_driver_packet_received Process received packet */
/* _ux_utility_long_get Get 32-bit value */
/* _ux_device_thread_suspend Suspend thread */
/* nx_packet_allocate Allocate NetX packet */
/* nx_packet_release Release NetX packet */
/* */
/* CALLED BY */
/* */
/* ThreadX */
/* */
/**************************************************************************/
VOID _ux_device_class_rndis_bulkout_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;
NX_PACKET *packet;
ULONG packet_payload;
USB_NETWORK_DEVICE_TYPE *ux_nx_device;
/* 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)
{
/* Select the transfer request associated with BULK OUT endpoint. */
transfer_request = &rndis -> ux_slave_class_rndis_bulkout_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)
{
/* Check if packet pool is ready. */
if (rndis -> ux_slave_class_rndis_packet_pool == UX_NULL)
{
/* Get the network device handle. */
ux_nx_device = (USB_NETWORK_DEVICE_TYPE *)(rndis -> ux_slave_class_rndis_network_handle);
/* Get packet pool from IP instance (if available). */
if (ux_nx_device -> ux_network_device_ip_instance != UX_NULL)
{
rndis -> ux_slave_class_rndis_packet_pool = ux_nx_device -> ux_network_device_ip_instance -> nx_ip_default_packet_pool;
}
else
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_ETH_PACKET_POOL_ERROR);
_ux_utility_delay_ms(UX_DEVICE_CLASS_RNDIS_PACKET_POOL_INST_WAIT);
continue;
}
}
/* We can accept new reception. Get a NX Packet. */
status = nx_packet_allocate(rndis -> ux_slave_class_rndis_packet_pool, &packet,
NX_RECEIVE_PACKET, UX_MS_TO_TICK(UX_DEVICE_CLASS_RNDIS_PACKET_POOL_WAIT));
if (status == NX_SUCCESS)
{
/* And length. */
transfer_request -> ux_slave_transfer_request_requested_length = UX_DEVICE_CLASS_RNDIS_BULKOUT_BUFFER_SIZE;
transfer_request -> ux_slave_transfer_request_actual_length = 0;
/* Memorize this packet at the beginning of the queue. */
rndis -> ux_slave_class_rndis_receive_queue = packet;
/* Reset the queue pointer of this packet. */
packet -> nx_packet_queue_next = UX_NULL;
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_RNDIS_ZERO_COPY)
/* Adjust the prepend pointer to take into account the RNDIS header and
non 3 bit alignment of the ethernet header. */
packet -> nx_packet_append_ptr += UX_DEVICE_CLASS_RNDIS_PACKET_BUFFER;
switch(((ALIGN_TYPE)packet -> nx_packet_append_ptr) & 0x3)
{
case 0:
packet -> nx_packet_append_ptr += 2;
break;
case 1:
packet -> nx_packet_append_ptr += 1;
break;
case 3:
packet -> nx_packet_append_ptr += 3;
break;
default:
break;
}
packet -> nx_packet_prepend_ptr = packet -> nx_packet_append_ptr - UX_DEVICE_CLASS_RNDIS_PACKET_BUFFER;
/* Get actual size of transfer buffer. */
packet_payload = (ULONG)(packet -> nx_packet_data_end - packet -> nx_packet_prepend_ptr);
/* Send the request to the device controller. */
transfer_request -> ux_slave_transfer_request_data_pointer = packet -> nx_packet_prepend_ptr;
status = _ux_device_stack_transfer_request(transfer_request,
packet_payload, packet_payload);
#else
/* Send the request to the device controller. */
status = _ux_device_stack_transfer_request(transfer_request, UX_DEVICE_CLASS_RNDIS_BULKOUT_BUFFER_SIZE,
UX_DEVICE_CLASS_RNDIS_BULKOUT_BUFFER_SIZE);
#endif
/* Check the completion code. */
if (status == UX_SUCCESS)
{
/* We only proceed with packets that are received OK, if error, ignore the packet. */
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_RNDIS_PACKET_RECEIVE, rndis, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
/* Check the state of the transfer. If there is an error, we do not proceed with this report.
Ensure this packet is at least larger than the header.
Also ensure the header has a valid ID of 1. */
if (transfer_request -> ux_slave_transfer_request_actual_length > UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH &&
_ux_utility_long_get(transfer_request -> ux_slave_transfer_request_data_pointer + UX_DEVICE_CLASS_RNDIS_PACKET_MESSAGE_TYPE) == UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_MSG)
{
/* Get the size of the payload. */
packet_payload = _ux_utility_long_get(transfer_request -> ux_slave_transfer_request_data_pointer + UX_DEVICE_CLASS_RNDIS_PACKET_DATA_LENGTH);
/* Ensure the length reported in the RNDIS header is not larger than it actually is.
The reason we can't check to see if the length reported in the header and the
actual length are exactly equal is because there might other data after the payload
(padding, or even a message). */
if (packet_payload <= transfer_request -> ux_slave_transfer_request_actual_length - UX_DEVICE_CLASS_RNDIS_PACKET_HEADER_LENGTH)
{
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_RNDIS_ZERO_COPY)
/* Data already in buffer, adjust packet start and save length. */
packet -> nx_packet_prepend_ptr += UX_DEVICE_CLASS_RNDIS_PACKET_BUFFER;
packet -> nx_packet_length = packet_payload;
packet -> nx_packet_append_ptr += packet -> nx_packet_length;
/* Send that packet to the NetX USB broker. */
_ux_network_driver_packet_received(rndis -> ux_slave_class_rndis_network_handle, packet);
#else
/* Adjust the prepend pointer to take into account the non 3 bit alignment of the ethernet header. */
packet -> nx_packet_prepend_ptr += sizeof(USHORT);
packet -> nx_packet_append_ptr += sizeof(USHORT);
/* Copy the received packet in the IP packet data area. */
status = nx_packet_data_append(packet,
transfer_request -> ux_slave_transfer_request_data_pointer +
UX_DEVICE_CLASS_RNDIS_PACKET_BUFFER,
packet_payload,
rndis -> ux_slave_class_rndis_packet_pool,
UX_MS_TO_TICK(UX_DEVICE_CLASS_RNDIS_PACKET_POOL_WAIT));
if (status == UX_SUCCESS)
{
/* Send that packet to the NetX USB broker. */
_ux_network_driver_packet_received(rndis -> ux_slave_class_rndis_network_handle, packet);
}
else
{
/* Error. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_ETH_PACKET_ERROR);
nx_packet_release(packet);
}
#endif
}
else
{
/* We received a malformed packet. Report to application. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_MALFORMED_PACKET_RECEIVED_ERROR);
nx_packet_release(packet);
}
}
else
{
/* We received a malformed packet. Report to application. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_MALFORMED_PACKET_RECEIVED_ERROR);
nx_packet_release(packet);
}
}
else
{
/* Free the packet that was not successfully received. */
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);
}
}
/* We need to suspend ourselves. We will be resumed by the device enumeration module. */
_ux_device_thread_suspend(&rndis -> ux_slave_class_rndis_bulkout_thread);
}
}
#endif
|