1 |
|
|
/*************************************************************************** |
2 |
|
|
* Copyright (c) 2024 Microsoft Corporation |
3 |
|
|
* Copyright (c) 2026-present Eclipse ThreadX contributors |
4 |
|
|
* |
5 |
|
|
* This program and the accompanying materials are made available under the |
6 |
|
|
* terms of the MIT License which is available at |
7 |
|
|
* https://opensource.org/licenses/MIT. |
8 |
|
|
* |
9 |
|
|
* SPDX-License-Identifier: MIT |
10 |
|
|
**************************************************************************/ |
11 |
|
|
|
12 |
|
|
/**************************************************************************/ |
13 |
|
|
/**************************************************************************/ |
14 |
|
|
/** */ |
15 |
|
|
/** USBX Component */ |
16 |
|
|
/** */ |
17 |
|
|
/** Device CDC Class */ |
18 |
|
|
/** */ |
19 |
|
|
/**************************************************************************/ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
|
22 |
|
|
#define UX_SOURCE_CODE |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
/* Include necessary system files. */ |
26 |
|
|
|
27 |
|
|
#include "ux_api.h" |
28 |
|
|
#include "ux_device_class_cdc_acm.h" |
29 |
|
|
#include "ux_device_stack.h" |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
#if !defined(UX_DEVICE_STANDALONE) |
33 |
|
|
/**************************************************************************/ |
34 |
|
|
/* */ |
35 |
|
|
/* FUNCTION RELEASE */ |
36 |
|
|
/* */ |
37 |
|
|
/* _ux_device_class_cdc_acm_write PORTABLE C */ |
38 |
|
|
/* 6.3.0 */ |
39 |
|
|
/* AUTHOR */ |
40 |
|
|
/* */ |
41 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
42 |
|
|
/* */ |
43 |
|
|
/* DESCRIPTION */ |
44 |
|
|
/* */ |
45 |
|
|
/* This function writes to the CDC class. */ |
46 |
|
|
/* */ |
47 |
|
|
/* It's for RTOS mode. */ |
48 |
|
|
/* */ |
49 |
|
|
/* INPUT */ |
50 |
|
|
/* */ |
51 |
|
|
/* cdc_acm Address of cdc_acm class */ |
52 |
|
|
/* instance */ |
53 |
|
|
/* buffer Pointer to data to write */ |
54 |
|
|
/* requested_length Length of bytes to write, */ |
55 |
|
|
/* set to 0 to issue ZLP */ |
56 |
|
|
/* actual_length Pointer to save number of */ |
57 |
|
|
/* bytes written */ |
58 |
|
|
/* */ |
59 |
|
|
/* OUTPUT */ |
60 |
|
|
/* */ |
61 |
|
|
/* None */ |
62 |
|
|
/* */ |
63 |
|
|
/* CALLS */ |
64 |
|
|
/* */ |
65 |
|
|
/* _ux_utility_memory_copy Copy memory */ |
66 |
|
|
/* _ux_device_stack_transfer_request Transfer request */ |
67 |
|
|
/* _ux_device_mutex_on Take Mutex */ |
68 |
|
|
/* _ux_device_mutex_off Release Mutex */ |
69 |
|
|
/* */ |
70 |
|
|
/* CALLED BY */ |
71 |
|
|
/* */ |
72 |
|
|
/* Application */ |
73 |
|
|
/* */ |
74 |
|
|
/**************************************************************************/ |
75 |
|
32 |
UINT _ux_device_class_cdc_acm_write(UX_SLAVE_CLASS_CDC_ACM *cdc_acm, UCHAR *buffer, |
76 |
|
|
ULONG requested_length, ULONG *actual_length) |
77 |
|
|
{ |
78 |
|
|
|
79 |
|
|
UX_SLAVE_ENDPOINT *endpoint; |
80 |
|
|
UX_SLAVE_DEVICE *device; |
81 |
|
|
UX_SLAVE_INTERFACE *interface_ptr; |
82 |
|
|
UX_SLAVE_TRANSFER *transfer_request; |
83 |
|
|
ULONG local_requested_length; |
84 |
|
|
ULONG local_host_length; |
85 |
|
32 |
UINT status = 0; |
86 |
|
|
|
87 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
88 |
|
|
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) |
89 |
|
|
|
90 |
|
|
#ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE |
91 |
|
|
|
92 |
|
|
/* Check if current cdc-acm is using callback or not. We cannot use direct reads with callback on. */ |
93 |
✓✓ |
32 |
if (cdc_acm -> ux_slave_class_cdc_acm_transmission_status == UX_TRUE) |
94 |
|
|
|
95 |
|
|
/* Not allowed. */ |
96 |
|
1 |
return(UX_ERROR); |
97 |
|
|
#endif |
98 |
|
|
|
99 |
|
|
/* Get the pointer to the device. */ |
100 |
|
31 |
device = &_ux_system_slave -> ux_system_slave_device; |
101 |
|
|
|
102 |
|
|
/* As long as the device is in the CONFIGURED state. */ |
103 |
✓✓ |
31 |
if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED) |
104 |
|
|
{ |
105 |
|
|
|
106 |
|
|
/* Error trap. */ |
107 |
|
1 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN); |
108 |
|
|
|
109 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
110 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0) |
111 |
|
|
|
112 |
|
|
/* Cannot proceed with command, the interface is down. */ |
113 |
|
1 |
return(UX_CONFIGURATION_HANDLE_UNKNOWN); |
114 |
|
|
} |
115 |
|
|
|
116 |
|
|
/* We need the interface to the class. */ |
117 |
|
30 |
interface_ptr = cdc_acm -> ux_slave_class_cdc_acm_interface; |
118 |
|
|
|
119 |
|
|
/* Locate the endpoints. */ |
120 |
|
30 |
endpoint = interface_ptr -> ux_slave_interface_first_endpoint; |
121 |
|
|
|
122 |
|
|
/* Check the endpoint direction, if IN we have the correct endpoint. */ |
123 |
✓✓ |
30 |
if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN) |
124 |
|
|
{ |
125 |
|
|
|
126 |
|
|
/* So the next endpoint has to be the IN endpoint. */ |
127 |
|
13 |
endpoint = endpoint -> ux_slave_endpoint_next_endpoint; |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/* Protect this thread. */ |
131 |
|
30 |
_ux_device_mutex_on(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex); |
132 |
|
|
|
133 |
|
|
/* We are writing to the IN endpoint. */ |
134 |
|
30 |
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request; |
135 |
|
|
|
136 |
|
|
#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1 |
137 |
|
|
#if !defined(UX_DEVICE_CLASS_CDC_ACM_ZERO_COPY) |
138 |
|
|
transfer_request -> ux_slave_transfer_request_data_pointer = |
139 |
|
|
UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER(cdc_acm); |
140 |
|
|
#else |
141 |
|
|
transfer_request -> ux_slave_transfer_request_data_pointer = buffer; |
142 |
|
|
#endif |
143 |
|
|
#endif |
144 |
|
|
|
145 |
|
|
/* Reset the actual length. */ |
146 |
|
30 |
*actual_length = 0; |
147 |
|
|
|
148 |
|
|
/* Check if the application forces a 0 length packet. */ |
149 |
✓✓✓✓
|
30 |
if (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED && requested_length == 0) |
150 |
|
|
{ |
151 |
|
|
|
152 |
|
|
/* Send the request for 0 byte packet to the device controller. */ |
153 |
|
2 |
status = _ux_device_stack_transfer_request(transfer_request, 0, 0); |
154 |
|
|
|
155 |
|
|
/* Free Mutex resource. */ |
156 |
|
2 |
_ux_device_mutex_off(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex); |
157 |
|
|
|
158 |
|
|
/* Return the status. */ |
159 |
|
2 |
return(status); |
160 |
|
|
|
161 |
|
|
|
162 |
|
|
} |
163 |
|
|
else |
164 |
|
|
{ |
165 |
|
|
|
166 |
|
|
#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_CDC_ACM_ZERO_COPY) |
167 |
|
|
|
168 |
|
|
/* Check if device is configured. */ |
169 |
|
|
if (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED) |
170 |
|
|
{ |
171 |
|
|
|
172 |
|
|
#if defined(UX_DEVICE_CLASS_CDC_ACM_WRITE_AUTO_ZLP) |
173 |
|
|
|
174 |
|
|
/* Issue with larger host length to append zlp if necessary. */ |
175 |
|
|
local_host_length = requested_length + 1; |
176 |
|
|
#else |
177 |
|
|
local_host_length = requested_length; |
178 |
|
|
#endif |
179 |
|
|
local_requested_length = requested_length; |
180 |
|
|
|
181 |
|
|
/* Issue the transfer request. */ |
182 |
|
|
status = _ux_device_stack_transfer_request(transfer_request, local_requested_length, local_host_length); |
183 |
|
|
if (status == UX_SUCCESS) |
184 |
|
|
*actual_length = transfer_request -> ux_slave_transfer_request_actual_length; |
185 |
|
|
} |
186 |
|
|
#else |
187 |
|
|
|
188 |
|
|
/* Check if we need more transactions. */ |
189 |
|
28 |
local_host_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE; |
190 |
✓✓✓✓
|
58 |
while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED && requested_length != 0) |
191 |
|
|
{ |
192 |
|
|
|
193 |
|
|
/* Check if we have enough in the local buffer. */ |
194 |
✓✓ |
33 |
if (requested_length > UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE) |
195 |
|
|
|
196 |
|
|
/* We have too much to transfer. */ |
197 |
|
7 |
local_requested_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE; |
198 |
|
|
|
199 |
|
|
else |
200 |
|
|
{ |
201 |
|
|
|
202 |
|
|
/* We can proceed with the demanded length. */ |
203 |
|
26 |
local_requested_length = requested_length; |
204 |
|
|
|
205 |
|
|
#if !defined(UX_DEVICE_CLASS_CDC_ACM_WRITE_AUTO_ZLP) |
206 |
|
|
|
207 |
|
|
/* Assume the length match expectation. */ |
208 |
|
26 |
local_host_length = requested_length; |
209 |
|
|
#else |
210 |
|
|
|
211 |
|
|
/* Assume expecting more, so ZLP is appended in stack. */ |
212 |
|
|
local_host_length = UX_DEVICE_CLASS_CDC_ACM_WRITE_BUFFER_SIZE + 1; |
213 |
|
|
#endif |
214 |
|
|
} |
215 |
|
|
|
216 |
|
|
/* On a out, we copy the buffer to the caller. Not very efficient but it makes the API |
217 |
|
|
easier. */ |
218 |
|
33 |
_ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer, |
219 |
|
|
buffer, local_requested_length); /* Use case of memcpy is verified. */ |
220 |
|
|
|
221 |
|
|
/* Send the request to the device controller. */ |
222 |
|
33 |
status = _ux_device_stack_transfer_request(transfer_request, local_requested_length, local_host_length); |
223 |
|
|
|
224 |
|
|
/* Check the status */ |
225 |
✓✓ |
33 |
if (status == UX_SUCCESS) |
226 |
|
|
{ |
227 |
|
|
|
228 |
|
|
/* Next buffer address. */ |
229 |
|
30 |
buffer += transfer_request -> ux_slave_transfer_request_actual_length; |
230 |
|
|
|
231 |
|
|
/* Set the length actually received. */ |
232 |
|
30 |
*actual_length += transfer_request -> ux_slave_transfer_request_actual_length; |
233 |
|
|
|
234 |
|
|
/* Decrement what left has to be done. */ |
235 |
|
30 |
requested_length -= transfer_request -> ux_slave_transfer_request_actual_length; |
236 |
|
|
|
237 |
|
|
} |
238 |
|
|
|
239 |
|
|
else |
240 |
|
|
{ |
241 |
|
|
|
242 |
|
|
/* Free Mutex resource. */ |
243 |
|
3 |
_ux_device_mutex_off(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex); |
244 |
|
|
|
245 |
|
|
/* We had an error, abort. */ |
246 |
|
3 |
return(status); |
247 |
|
|
} |
248 |
|
|
} |
249 |
|
|
#endif /* _BUFF_OWNER && _ZERO_COPY */ |
250 |
|
|
} |
251 |
|
|
|
252 |
|
|
|
253 |
|
|
/* Free Mutex resource. */ |
254 |
|
25 |
_ux_device_mutex_off(&cdc_acm -> ux_slave_class_cdc_acm_endpoint_in_mutex); |
255 |
|
|
|
256 |
|
|
/* Check why we got here, either completion or device was extracted. */ |
257 |
✓✓ |
25 |
if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED) |
258 |
|
|
{ |
259 |
|
|
|
260 |
|
|
/* Error trap. */ |
261 |
|
2 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_NO_ANSWER); |
262 |
|
|
|
263 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
264 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_NO_ANSWER, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0) |
265 |
|
|
|
266 |
|
|
/* Device must have been extracted. */ |
267 |
|
2 |
return (UX_TRANSFER_NO_ANSWER); |
268 |
|
|
} |
269 |
|
|
else |
270 |
|
|
|
271 |
|
|
/* Simply return the last transaction result. */ |
272 |
|
23 |
return(status); |
273 |
|
|
|
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
/**************************************************************************/ |
277 |
|
|
/* */ |
278 |
|
|
/* FUNCTION RELEASE */ |
279 |
|
|
/* */ |
280 |
|
|
/* _uxe_device_class_cdc_acm_write PORTABLE C */ |
281 |
|
|
/* 6.3.0 */ |
282 |
|
|
/* AUTHOR */ |
283 |
|
|
/* */ |
284 |
|
|
/* Yajun Xia, Microsoft Corporation */ |
285 |
|
|
/* */ |
286 |
|
|
/* DESCRIPTION */ |
287 |
|
|
/* */ |
288 |
|
|
/* This function checks errors in CDC ACM class write function. */ |
289 |
|
|
/* */ |
290 |
|
|
/* INPUT */ |
291 |
|
|
/* */ |
292 |
|
|
/* cdc_acm Address of cdc_acm class */ |
293 |
|
|
/* instance */ |
294 |
|
|
/* buffer Pointer to data to write */ |
295 |
|
|
/* requested_length Length of bytes to write, */ |
296 |
|
|
/* set to 0 to issue ZLP */ |
297 |
|
|
/* actual_length Pointer to save number of */ |
298 |
|
|
/* bytes written */ |
299 |
|
|
/* */ |
300 |
|
|
/* OUTPUT */ |
301 |
|
|
/* */ |
302 |
|
|
/* None */ |
303 |
|
|
/* */ |
304 |
|
|
/* CALLS */ |
305 |
|
|
/* */ |
306 |
|
|
/* _ux_device_class_cdc_acm_write CDC ACM class write function */ |
307 |
|
|
/* */ |
308 |
|
|
/* CALLED BY */ |
309 |
|
|
/* */ |
310 |
|
|
/* Application */ |
311 |
|
|
/* */ |
312 |
|
|
/**************************************************************************/ |
313 |
|
|
UINT _uxe_device_class_cdc_acm_write(UX_SLAVE_CLASS_CDC_ACM *cdc_acm, UCHAR *buffer, |
314 |
|
|
ULONG requested_length, ULONG *actual_length) |
315 |
|
|
{ |
316 |
|
|
|
317 |
|
|
/* Sanity checks. */ |
318 |
|
|
if ((cdc_acm == UX_NULL) || ((buffer == UX_NULL) && (requested_length > 0)) || (actual_length == UX_NULL)) |
319 |
|
|
{ |
320 |
|
|
return (UX_INVALID_PARAMETER); |
321 |
|
|
} |
322 |
|
|
|
323 |
|
|
return (_ux_device_class_cdc_acm_write(cdc_acm, buffer, requested_length, actual_length)); |
324 |
|
|
} |
325 |
|
|
|
326 |
|
|
#endif |