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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
|
/***************************************************************************
* 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 Class */
/** */
/**************************************************************************/
/**************************************************************************/
#define UX_SOURCE_CODE
/* Include necessary system files. */
#include "ux_api.h"
#include "ux_device_class_cdc_acm.h"
#include "ux_device_stack.h"
#if defined(UX_DEVICE_STANDALONE)
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_device_class_cdc_acm_write_run PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function writes to the CDC class. */
/* */
/* It's for standalone mode. */
/* */
/* INPUT */
/* */
/* cdc_acm Address of cdc_acm class */
/* instance */
/* buffer Pointer to data to write */
/* requested_length Length of bytes to write */
/* actual_length Pointer to save number of */
/* bytes written */
/* */
/* OUTPUT */
/* */
/* State machine Status to check */
/* UX_STATE_NEXT Transfer done, to next state */
/* UX_STATE_EXIT Abnormal, to reset state */
/* UX_STATE_ERROR Error occurred */
/* (others) Keep running, waiting */
/* */
/* CALLS */
/* */
/* _ux_device_stack_transfer_request Transfer request */
/* _ux_utility_memory_copy Copy memory */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/**************************************************************************/
UINT _ux_device_class_cdc_acm_write_run(UX_SLAVE_CLASS_CDC_ACM *cdc_acm,
UCHAR *buffer, ULONG requested_length, ULONG *actual_length)
{
UX_SLAVE_ENDPOINT *endpoint;
UX_SLAVE_DEVICE *device;
UX_SLAVE_INTERFACE *interface_ptr;
UX_SLAVE_TRANSFER *transfer_request;
UINT status = 0;
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER != 1) || !defined(UX_DEVICE_CLASS_CDC_ACM_ZERO_COPY)
UINT zlp = UX_FALSE;
#endif
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ACM_WRITE, cdc_acm, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
#ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE
/* Check if current cdc-acm is using callback or not. We cannot use direct writes with callback on. */
if (cdc_acm -> ux_slave_class_cdc_acm_transmission_status == UX_TRUE)
/* Not allowed. */
return(UX_STATE_ERROR);
#endif
/* Get the pointer to the device. */
device = &_ux_system_slave -> ux_system_slave_device;
/* As long as the device is in the CONFIGURED state. */
if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
/* Cannot proceed with command, the interface is down. */
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET;
cdc_acm -> ux_device_class_cdc_acm_write_status = UX_CONFIGURATION_HANDLE_UNKNOWN;
return(UX_STATE_EXIT);
}
/* We need the interface to the class. */
interface_ptr = cdc_acm -> ux_slave_class_cdc_acm_interface;
/* Locate the endpoints. */
endpoint = interface_ptr -> ux_slave_interface_first_endpoint;
/* Check the endpoint direction, if IN we have the correct endpoint. */
if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
{
/* So the next endpoint has to be the IN endpoint. */
endpoint = endpoint -> ux_slave_endpoint_next_endpoint;
}
/* We are writing to the IN endpoint. */
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request;
#if defined(UX_DEVICE_CLASS_CDC_ACM_OWN_ENDPOINT_BUFFER)
transfer_request -> ux_slave_transfer_request_data_pointer =
UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER(cdc_acm);
#endif
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_CDC_ACM_ZERO_COPY)
/* Run the transfer state machine. */
if(cdc_acm -> ux_device_class_cdc_acm_write_state == UX_STATE_RESET)
{
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_CDC_ACM_WRITE, cdc_acm, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_DEVICE_CLASS_CDC_ACM_WRITE_WAIT;
cdc_acm -> ux_device_class_cdc_acm_write_status = UX_TRANSFER_NO_ANSWER;
transfer_request -> ux_slave_transfer_request_data_pointer = buffer;
UX_SLAVE_TRANSFER_STATE_RESET(transfer_request);
}
/* Issue the transfer request. */
#if defined(UX_DEVICE_CLASS_CDC_ACM_WRITE_AUTO_ZLP)
status = _ux_device_stack_transfer_run(transfer_request, requested_length, requested_length + 1);
#else
status = _ux_device_stack_transfer_run(transfer_request, requested_length, requested_length);
#endif
/* Error case. */
if (status < UX_STATE_NEXT)
{
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET;
cdc_acm -> ux_device_class_cdc_acm_write_status =
transfer_request -> ux_slave_transfer_request_completion_code;
return(UX_STATE_ERROR);
}
/* Success case. */
if (status == UX_STATE_NEXT)
{
/* Last transfer status. */
cdc_acm -> ux_device_class_cdc_acm_write_status =
transfer_request -> ux_slave_transfer_request_completion_code;
/* Update actual length. */
*actual_length = transfer_request -> ux_slave_transfer_request_actual_length;
/* It's done. */
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET;
}
return(status);
#else
/* Handle state cases. */
switch(cdc_acm -> ux_device_class_cdc_acm_write_state)
{
case UX_STATE_RESET:
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_DEVICE_CLASS_CDC_ACM_WRITE_START;
cdc_acm -> ux_device_class_cdc_acm_write_status = UX_TRANSFER_NO_ANSWER;
cdc_acm -> ux_device_class_cdc_acm_write_buffer = buffer;
cdc_acm -> ux_device_class_cdc_acm_write_requested_length = requested_length;
cdc_acm -> ux_device_class_cdc_acm_write_actual_length = 0;
cdc_acm -> ux_device_class_cdc_acm_write_host_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE;
if (requested_length == 0)
zlp = UX_TRUE;
/* Fall through. */
case UX_DEVICE_CLASS_CDC_ACM_WRITE_START:
/* Get remaining requested length. */
requested_length = cdc_acm -> ux_device_class_cdc_acm_write_requested_length -
cdc_acm -> ux_device_class_cdc_acm_write_actual_length;
/* There is no remaining, we are done. */
if (requested_length == 0 && !zlp)
{
*actual_length = cdc_acm -> ux_device_class_cdc_acm_write_actual_length;
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET;
cdc_acm -> ux_device_class_cdc_acm_write_status = UX_SUCCESS;
return(UX_STATE_NEXT);
}
/* Check if we have enough in the local buffer. */
if (requested_length > UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE)
/* We have too much to transfer. */
cdc_acm -> ux_device_class_cdc_acm_write_transfer_length =
UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE;
else
{
/* We can proceed with the demanded length. */
cdc_acm -> ux_device_class_cdc_acm_write_transfer_length = requested_length;
#if !defined(UX_DEVICE_CLASS_CDC_ACM_WRITE_AUTO_ZLP)
/* Assume expected length and transfer length match. */
cdc_acm -> ux_device_class_cdc_acm_write_host_length = requested_length;
#else
/* Assume expected more than transfer to let stack append ZLP if needed. */
cdc_acm -> ux_device_class_cdc_acm_write_host_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE + 1;
#endif
}
/* On a out, we copy the buffer to the caller. Not very efficient but it makes the API
easier. */
_ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
cdc_acm -> ux_device_class_cdc_acm_write_buffer,
cdc_acm -> ux_device_class_cdc_acm_write_transfer_length); /* Use case of memcpy is verified. */
/* Next state. */
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_DEVICE_CLASS_CDC_ACM_WRITE_WAIT;
UX_SLAVE_TRANSFER_STATE_RESET(transfer_request);
/* Fall through. */
case UX_DEVICE_CLASS_CDC_ACM_WRITE_WAIT:
/* Send the request to the device controller. */
status = _ux_device_stack_transfer_run(transfer_request,
cdc_acm -> ux_device_class_cdc_acm_write_transfer_length,
cdc_acm -> ux_device_class_cdc_acm_write_host_length);
/* Error case. */
if (status < UX_STATE_NEXT)
{
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET;
cdc_acm -> ux_device_class_cdc_acm_write_status =
transfer_request -> ux_slave_transfer_request_completion_code;
return(UX_STATE_ERROR);
}
/* Success case. */
if (status == UX_STATE_NEXT)
{
/* Next buffer address. */
cdc_acm -> ux_device_class_cdc_acm_write_buffer +=
transfer_request -> ux_slave_transfer_request_actual_length;
/* Set the length actually received. */
cdc_acm -> ux_device_class_cdc_acm_write_actual_length +=
transfer_request -> ux_slave_transfer_request_actual_length;
/* Last transfer status. */
cdc_acm -> ux_device_class_cdc_acm_write_status =
transfer_request -> ux_slave_transfer_request_completion_code;
/* Update actual done length. */
*actual_length = cdc_acm -> ux_device_class_cdc_acm_write_actual_length;
/* Check ZLP case. */
if (cdc_acm -> ux_device_class_cdc_acm_write_requested_length == 0)
{
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET;
return(UX_STATE_NEXT);
}
/* Next state. */
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_DEVICE_CLASS_CDC_ACM_WRITE_START;
}
/* Keep waiting. */
return(UX_STATE_WAIT);
default: /* Error. */
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET;
break;
}
/* Error case. */
return(UX_STATE_EXIT);
#endif
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _uxe_device_class_cdc_acm_write_run PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Yajun Xia, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks errors in CDC ACM class write process. */
/* */
/* It's for standalone mode. */
/* */
/* INPUT */
/* */
/* cdc_acm Address of cdc_acm class */
/* instance */
/* buffer Pointer to data to write */
/* requested_length Length of bytes to write */
/* actual_length Pointer to save number of */
/* bytes written */
/* */
/* OUTPUT */
/* */
/* State machine Status to check */
/* UX_STATE_NEXT Transfer done, to next state */
/* UX_STATE_EXIT Abnormal, to reset state */
/* UX_STATE_ERROR Error occurred */
/* (others) Keep running, waiting */
/* */
/* CALLS */
/* */
/* _ux_device_class_cdc_acm_write_run CDC ACM class write process */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/**************************************************************************/
UINT _uxe_device_class_cdc_acm_write_run(UX_SLAVE_CLASS_CDC_ACM *cdc_acm,
UCHAR *buffer, ULONG requested_length, ULONG *actual_length)
{
/* Sanity checks. */
if ((cdc_acm == UX_NULL) || ((buffer == UX_NULL) && (requested_length > 0)) || (actual_length == UX_NULL))
{
return(UX_STATE_ERROR);
}
return (_ux_device_class_cdc_acm_write_run(cdc_acm, buffer, requested_length, actual_length));
}
#endif
|