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 |
|
|
/**************************************************************************/ |
33 |
|
|
/* */ |
34 |
|
|
/* FUNCTION RELEASE */ |
35 |
|
|
/* */ |
36 |
|
|
/* _ux_device_class_cdc_acm_ioctl PORTABLE C */ |
37 |
|
|
/* 6.3.0 */ |
38 |
|
|
/* AUTHOR */ |
39 |
|
|
/* */ |
40 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
41 |
|
|
/* */ |
42 |
|
|
/* DESCRIPTION */ |
43 |
|
|
/* */ |
44 |
|
|
/* This function performs certain functions on the cdc acm instance */ |
45 |
|
|
/* */ |
46 |
|
|
/* INPUT */ |
47 |
|
|
/* */ |
48 |
|
|
/* cdc_acm Address of cdc_acm class */ |
49 |
|
|
/* instance */ |
50 |
|
|
/* */ |
51 |
|
|
/* OUTPUT */ |
52 |
|
|
/* */ |
53 |
|
|
/* Status */ |
54 |
|
|
/* */ |
55 |
|
|
/* CALLS */ |
56 |
|
|
/* */ |
57 |
|
|
/* _ux_device_stack_transfer_abort Abort transfer */ |
58 |
|
|
/* _ux_utility_memory_allocate Allocate memory */ |
59 |
|
|
/* _ux_utility_memory_free Free memory */ |
60 |
|
|
/* _ux_utility_event_flags_create Create event flags */ |
61 |
|
|
/* _ux_utility_event_flags_delete Delete event flags */ |
62 |
|
|
/* _ux_device_thread_create Create thread */ |
63 |
|
|
/* _ux_device_thread_delete Delete thread */ |
64 |
|
|
/* */ |
65 |
|
|
/* CALLED BY */ |
66 |
|
|
/* */ |
67 |
|
|
/* Application */ |
68 |
|
|
/* */ |
69 |
|
|
/**************************************************************************/ |
70 |
|
125 |
UINT _ux_device_class_cdc_acm_ioctl(UX_SLAVE_CLASS_CDC_ACM *cdc_acm, ULONG ioctl_function, |
71 |
|
|
VOID *parameter) |
72 |
|
|
{ |
73 |
|
|
|
74 |
|
|
UINT status; |
75 |
|
|
UX_SLAVE_CLASS_CDC_ACM_LINE_CODING_PARAMETER *line_coding; |
76 |
|
|
UX_SLAVE_CLASS_CDC_ACM_LINE_STATE_PARAMETER *line_state; |
77 |
|
|
#ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE |
78 |
|
|
UX_SLAVE_CLASS_CDC_ACM_CALLBACK_PARAMETER *callback; |
79 |
|
|
#endif |
80 |
|
|
UX_SLAVE_ENDPOINT *endpoint; |
81 |
|
|
UX_SLAVE_INTERFACE *interface_ptr; |
82 |
|
|
UX_SLAVE_TRANSFER *transfer_request; |
83 |
|
|
|
84 |
|
|
/* Let's be optimist ! */ |
85 |
|
125 |
status = UX_SUCCESS; |
86 |
|
|
|
87 |
|
|
/* The command request will tell us what we need to do here. */ |
88 |
✓✓✓✓ ✓✓✓✓ ✓ |
125 |
switch (ioctl_function) |
89 |
|
|
{ |
90 |
|
|
|
91 |
|
1 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_SET_LINE_CODING: |
92 |
|
|
|
93 |
|
|
/* Properly cast the parameter pointer. */ |
94 |
|
1 |
line_coding = (UX_SLAVE_CLASS_CDC_ACM_LINE_CODING_PARAMETER *) parameter; |
95 |
|
|
|
96 |
|
|
/* Save the parameters in the cdc_acm function. */ |
97 |
|
1 |
cdc_acm -> ux_slave_class_cdc_acm_baudrate = line_coding -> ux_slave_class_cdc_acm_parameter_baudrate; |
98 |
|
1 |
cdc_acm -> ux_slave_class_cdc_acm_stop_bit = line_coding -> ux_slave_class_cdc_acm_parameter_stop_bit; |
99 |
|
1 |
cdc_acm -> ux_slave_class_cdc_acm_parity = line_coding -> ux_slave_class_cdc_acm_parameter_parity; |
100 |
|
1 |
cdc_acm -> ux_slave_class_cdc_acm_data_bit = line_coding -> ux_slave_class_cdc_acm_parameter_data_bit; |
101 |
|
|
|
102 |
|
1 |
break; |
103 |
|
|
|
104 |
|
9 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_GET_LINE_CODING: |
105 |
|
|
|
106 |
|
|
/* Properly cast the parameter pointer. */ |
107 |
|
9 |
line_coding = (UX_SLAVE_CLASS_CDC_ACM_LINE_CODING_PARAMETER *) parameter; |
108 |
|
|
|
109 |
|
|
/* Save the parameters in the cdc_acm function. */ |
110 |
|
9 |
line_coding -> ux_slave_class_cdc_acm_parameter_baudrate = cdc_acm -> ux_slave_class_cdc_acm_baudrate; |
111 |
|
9 |
line_coding -> ux_slave_class_cdc_acm_parameter_stop_bit = cdc_acm -> ux_slave_class_cdc_acm_stop_bit; |
112 |
|
9 |
line_coding -> ux_slave_class_cdc_acm_parameter_parity = cdc_acm -> ux_slave_class_cdc_acm_parity; |
113 |
|
9 |
line_coding -> ux_slave_class_cdc_acm_parameter_data_bit = cdc_acm -> ux_slave_class_cdc_acm_data_bit; |
114 |
|
|
|
115 |
|
9 |
break; |
116 |
|
|
|
117 |
|
|
|
118 |
|
10 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_GET_LINE_STATE: |
119 |
|
|
|
120 |
|
|
/* Properly cast the parameter pointer. */ |
121 |
|
10 |
line_state = (UX_SLAVE_CLASS_CDC_ACM_LINE_STATE_PARAMETER *) parameter; |
122 |
|
|
|
123 |
|
|
/* Return the DTR/RTS signals. */ |
124 |
|
10 |
line_state -> ux_slave_class_cdc_acm_parameter_rts = cdc_acm -> ux_slave_class_cdc_acm_data_rts_state; |
125 |
|
10 |
line_state -> ux_slave_class_cdc_acm_parameter_dtr = cdc_acm -> ux_slave_class_cdc_acm_data_dtr_state; |
126 |
|
|
|
127 |
|
10 |
break; |
128 |
|
|
|
129 |
|
3 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_SET_LINE_STATE: |
130 |
|
|
|
131 |
|
|
/* Properly cast the parameter pointer. */ |
132 |
|
3 |
line_state = (UX_SLAVE_CLASS_CDC_ACM_LINE_STATE_PARAMETER *) parameter; |
133 |
|
|
|
134 |
|
|
/* Set the DTR/RTS signals. */ |
135 |
|
3 |
cdc_acm -> ux_slave_class_cdc_acm_data_rts_state = line_state -> ux_slave_class_cdc_acm_parameter_rts; |
136 |
|
3 |
cdc_acm -> ux_slave_class_cdc_acm_data_dtr_state = line_state -> ux_slave_class_cdc_acm_parameter_dtr; |
137 |
|
|
|
138 |
|
3 |
break; |
139 |
|
|
|
140 |
|
|
|
141 |
|
12 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_ABORT_PIPE: |
142 |
|
|
|
143 |
|
|
/* Get the interface from the instance. */ |
144 |
|
12 |
interface_ptr = cdc_acm -> ux_slave_class_cdc_acm_interface; |
145 |
|
|
|
146 |
|
|
/* Locate the endpoints. */ |
147 |
|
12 |
endpoint = interface_ptr -> ux_slave_interface_first_endpoint; |
148 |
|
|
|
149 |
|
|
/* What direction ? */ |
150 |
✓✓✓ |
12 |
switch( (ULONG) (ALIGN_TYPE) parameter) |
151 |
|
|
{ |
152 |
|
4 |
case UX_SLAVE_CLASS_CDC_ACM_ENDPOINT_XMIT : |
153 |
|
|
|
154 |
|
|
/* Check the endpoint direction, if IN we have the correct endpoint. */ |
155 |
✓✓ |
4 |
if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN) |
156 |
|
|
{ |
157 |
|
|
|
158 |
|
|
/* So the next endpoint has to be the XMIT endpoint. */ |
159 |
|
1 |
endpoint = endpoint -> ux_slave_endpoint_next_endpoint; |
160 |
|
|
} |
161 |
|
4 |
break; |
162 |
|
|
|
163 |
|
5 |
case UX_SLAVE_CLASS_CDC_ACM_ENDPOINT_RCV : |
164 |
|
|
|
165 |
|
|
/* Check the endpoint direction, if OUT we have the correct endpoint. */ |
166 |
✓✓ |
5 |
if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_OUT) |
167 |
|
|
{ |
168 |
|
|
|
169 |
|
|
/* So the next endpoint has to be the RCV endpoint. */ |
170 |
|
4 |
endpoint = endpoint -> ux_slave_endpoint_next_endpoint; |
171 |
|
|
} |
172 |
|
5 |
break; |
173 |
|
|
|
174 |
|
|
|
175 |
|
|
|
176 |
|
3 |
default : |
177 |
|
|
|
178 |
|
|
/* Parameter not supported. Return an error. */ |
179 |
|
3 |
status = UX_ENDPOINT_HANDLE_UNKNOWN; |
180 |
|
|
} |
181 |
|
|
|
182 |
|
|
/* Get the transfer request associated with the endpoint. */ |
183 |
|
12 |
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request; |
184 |
|
|
|
185 |
|
|
#if defined(UX_DEVICE_STANDALONE) |
186 |
|
|
|
187 |
|
|
/* Abort the transfer. */ |
188 |
|
|
_ux_device_stack_transfer_abort(transfer_request, UX_TRANSFER_STATUS_ABORT); |
189 |
|
|
if ((ULONG) (ALIGN_TYPE) parameter == UX_SLAVE_CLASS_CDC_ACM_ENDPOINT_XMIT) |
190 |
|
|
cdc_acm -> ux_device_class_cdc_acm_write_state = UX_STATE_RESET; |
191 |
|
|
else |
192 |
|
|
cdc_acm -> ux_device_class_cdc_acm_read_state = UX_STATE_RESET; |
193 |
|
|
#else |
194 |
|
|
|
195 |
|
|
/* Check the status of the transfer. */ |
196 |
✓✓ |
12 |
if (transfer_request -> ux_slave_transfer_request_status == UX_TRANSFER_STATUS_PENDING) |
197 |
|
|
{ |
198 |
|
|
|
199 |
|
|
/* Abort the transfer. */ |
200 |
|
3 |
_ux_device_stack_transfer_abort(transfer_request, UX_ABORTED); |
201 |
|
|
|
202 |
|
|
} |
203 |
|
|
#endif |
204 |
|
12 |
break; |
205 |
|
|
|
206 |
|
6 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_SET_READ_TIMEOUT: |
207 |
|
|
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_SET_WRITE_TIMEOUT: |
208 |
|
|
|
209 |
|
|
/* Get the interface from the instance. */ |
210 |
|
6 |
interface_ptr = cdc_acm -> ux_slave_class_cdc_acm_interface; |
211 |
|
|
|
212 |
|
|
/* Locate the endpoints. */ |
213 |
|
6 |
endpoint = interface_ptr -> ux_slave_interface_first_endpoint; |
214 |
|
|
|
215 |
|
|
/* If it's reading timeout but endpoint is OUT, it should be the next one. */ |
216 |
✓✓ |
6 |
if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != |
217 |
✓✓ |
6 |
(ULONG)((ioctl_function == UX_SLAVE_CLASS_CDC_ACM_IOCTL_SET_READ_TIMEOUT) ? UX_ENDPOINT_OUT : UX_ENDPOINT_IN)) |
218 |
|
3 |
endpoint = endpoint -> ux_slave_endpoint_next_endpoint; |
219 |
|
|
|
220 |
|
|
/* Get the transfer request associated with the endpoint. */ |
221 |
|
6 |
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request; |
222 |
|
|
|
223 |
|
|
/* Check the status of the transfer. */ |
224 |
✓✓ |
6 |
if (transfer_request -> ux_slave_transfer_request_status == UX_TRANSFER_STATUS_PENDING) |
225 |
|
4 |
status = UX_ERROR; |
226 |
|
|
else |
227 |
|
2 |
transfer_request -> ux_slave_transfer_request_timeout = (ULONG) (ALIGN_TYPE) parameter; |
228 |
|
|
|
229 |
|
6 |
break; |
230 |
|
|
|
231 |
|
|
#ifndef UX_DEVICE_CLASS_CDC_ACM_TRANSMISSION_DISABLE |
232 |
|
|
|
233 |
|
6 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_TRANSMISSION_START: |
234 |
|
|
|
235 |
|
|
/* Check if we are in callback transmission already. */ |
236 |
✓✓ |
6 |
if (cdc_acm -> ux_slave_class_cdc_acm_transmission_status == UX_TRUE) |
237 |
|
|
{ |
238 |
|
|
/* We should not to that ! */ |
239 |
|
1 |
return(UX_ERROR); |
240 |
|
|
|
241 |
|
|
} |
242 |
|
|
|
243 |
|
|
/* Properly cast the parameter pointer. */ |
244 |
|
5 |
callback = (UX_SLAVE_CLASS_CDC_ACM_CALLBACK_PARAMETER *) parameter; |
245 |
|
|
|
246 |
|
|
/* Save the callback function for write. */ |
247 |
|
5 |
cdc_acm -> ux_device_class_cdc_acm_write_callback = callback -> ux_device_class_cdc_acm_parameter_write_callback; |
248 |
|
|
|
249 |
|
|
/* Save the callback function for read. */ |
250 |
|
5 |
cdc_acm -> ux_device_class_cdc_acm_read_callback = callback -> ux_device_class_cdc_acm_parameter_read_callback; |
251 |
|
|
|
252 |
|
|
#if !defined(UX_DEVICE_STANDALONE) |
253 |
|
|
|
254 |
|
|
/* Start transmission threads. */ |
255 |
|
5 |
_ux_utility_thread_resume(&cdc_acm -> ux_slave_class_cdc_acm_bulkin_thread); |
256 |
|
5 |
_ux_utility_thread_resume(&cdc_acm -> ux_slave_class_cdc_acm_bulkout_thread); |
257 |
|
|
#endif |
258 |
|
|
|
259 |
|
|
/* Declare the transmission with callback on. */ |
260 |
|
5 |
cdc_acm -> ux_slave_class_cdc_acm_transmission_status = UX_TRUE; |
261 |
|
|
|
262 |
|
|
/* We are done here. */ |
263 |
|
5 |
return(UX_SUCCESS); |
264 |
|
|
|
265 |
|
77 |
case UX_SLAVE_CLASS_CDC_ACM_IOCTL_TRANSMISSION_STOP: |
266 |
|
|
|
267 |
|
|
/* Check if we are in callback transmission already. */ |
268 |
✓✓ |
77 |
if (cdc_acm -> ux_slave_class_cdc_acm_transmission_status == UX_TRUE) |
269 |
|
|
{ |
270 |
|
|
|
271 |
|
|
/* Get the interface from the instance. */ |
272 |
|
3 |
interface_ptr = cdc_acm -> ux_slave_class_cdc_acm_interface; |
273 |
|
|
|
274 |
|
|
/* Locate the endpoints. */ |
275 |
|
3 |
endpoint = interface_ptr -> ux_slave_interface_first_endpoint; |
276 |
|
|
|
277 |
|
|
/* Get the transfer request associated with the endpoint. */ |
278 |
|
3 |
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request; |
279 |
|
|
|
280 |
|
|
/* Abort the transfer. */ |
281 |
|
3 |
_ux_device_stack_transfer_abort(transfer_request, UX_ABORTED); |
282 |
|
|
|
283 |
|
|
/* Next endpoint. */ |
284 |
|
3 |
endpoint = endpoint -> ux_slave_endpoint_next_endpoint; |
285 |
|
|
|
286 |
|
|
/* Get the transfer request associated with the endpoint. */ |
287 |
|
3 |
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request; |
288 |
|
|
|
289 |
|
|
/* Abort the transfer. */ |
290 |
|
3 |
_ux_device_stack_transfer_abort(transfer_request, UX_ABORTED); |
291 |
|
|
|
292 |
|
|
#if !defined(UX_DEVICE_STANDALONE) |
293 |
|
|
|
294 |
|
|
/* Suspend threads. */ |
295 |
|
3 |
_ux_device_thread_suspend(&cdc_acm -> ux_slave_class_cdc_acm_bulkin_thread); |
296 |
|
3 |
_ux_device_thread_suspend(&cdc_acm -> ux_slave_class_cdc_acm_bulkout_thread); |
297 |
|
|
#endif |
298 |
|
|
|
299 |
|
|
/* Clear scheduled write flag. */ |
300 |
|
3 |
cdc_acm -> ux_slave_class_cdc_acm_scheduled_write = UX_FALSE; |
301 |
|
|
|
302 |
|
|
/* Declare the transmission with callback off. */ |
303 |
|
3 |
cdc_acm -> ux_slave_class_cdc_acm_transmission_status = UX_FALSE; |
304 |
|
|
} |
305 |
|
|
else |
306 |
|
|
|
307 |
|
|
/* We should not try to stop an non existing transmission. */ |
308 |
|
74 |
return(UX_ERROR); |
309 |
|
|
|
310 |
|
3 |
break; |
311 |
|
|
#endif |
312 |
|
|
|
313 |
|
1 |
default: |
314 |
|
|
|
315 |
|
|
/* Error trap. */ |
316 |
|
1 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_FUNCTION_NOT_SUPPORTED); |
317 |
|
|
|
318 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
319 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_FUNCTION_NOT_SUPPORTED, 0, 0, 0, UX_TRACE_ERRORS, 0, 0) |
320 |
|
|
|
321 |
|
|
/* Function not supported. Return an error. */ |
322 |
|
1 |
status = UX_FUNCTION_NOT_SUPPORTED; |
323 |
|
|
} |
324 |
|
|
|
325 |
|
|
/* Return status to caller. */ |
326 |
|
45 |
return(status); |
327 |
|
|
|
328 |
|
|
} |
329 |
|
|
|
330 |
|
|
/**************************************************************************/ |
331 |
|
|
/* */ |
332 |
|
|
/* FUNCTION RELEASE */ |
333 |
|
|
/* */ |
334 |
|
|
/* _uxe_device_class_cdc_acm_ioctl PORTABLE C */ |
335 |
|
|
/* 6.3.0 */ |
336 |
|
|
/* AUTHOR */ |
337 |
|
|
/* */ |
338 |
|
|
/* Yajun Xia, Microsoft Corporation */ |
339 |
|
|
/* */ |
340 |
|
|
/* DESCRIPTION */ |
341 |
|
|
/* */ |
342 |
|
|
/* This function checks errors in CDC ACM class ioctl function. */ |
343 |
|
|
/* */ |
344 |
|
|
/* INPUT */ |
345 |
|
|
/* */ |
346 |
|
|
/* cdc_acm Address of cdc_acm class */ |
347 |
|
|
/* instance */ |
348 |
|
|
/* ioctl_function Ioctl function */ |
349 |
|
|
/* Parameter Parameter of ioctl function */ |
350 |
|
|
/* */ |
351 |
|
|
/* OUTPUT */ |
352 |
|
|
/* */ |
353 |
|
|
/* Status */ |
354 |
|
|
/* */ |
355 |
|
|
/* CALLS */ |
356 |
|
|
/* */ |
357 |
|
|
/* _ux_device_class_cdc_acm_ioctl CDC ACM class ioctl function */ |
358 |
|
|
/* */ |
359 |
|
|
/* CALLED BY */ |
360 |
|
|
/* */ |
361 |
|
|
/* Application */ |
362 |
|
|
/* */ |
363 |
|
|
/**************************************************************************/ |
364 |
|
|
UINT _uxe_device_class_cdc_acm_ioctl(UX_SLAVE_CLASS_CDC_ACM *cdc_acm, ULONG ioctl_function, |
365 |
|
|
VOID *parameter) |
366 |
|
|
{ |
367 |
|
|
|
368 |
|
|
/* Sanity checks. */ |
369 |
|
|
if (cdc_acm == UX_NULL) |
370 |
|
|
{ |
371 |
|
|
return (UX_INVALID_PARAMETER); |
372 |
|
|
} |
373 |
|
|
|
374 |
|
|
return (_ux_device_class_cdc_acm_ioctl(cdc_acm, ioctl_function, parameter)); |
375 |
|
|
} |