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 |
|
|
/** */ |
16 |
|
|
/** USBX Component */ |
17 |
|
|
/** */ |
18 |
|
|
/** CDC ACM Class */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
/* Include necessary system files. */ |
25 |
|
|
|
26 |
|
|
#define UX_SOURCE_CODE |
27 |
|
|
|
28 |
|
|
#include "ux_api.h" |
29 |
|
|
#include "ux_host_class_cdc_acm.h" |
30 |
|
|
#include "ux_host_stack.h" |
31 |
|
|
|
32 |
|
|
|
33 |
|
|
/**************************************************************************/ |
34 |
|
|
/* */ |
35 |
|
|
/* FUNCTION RELEASE */ |
36 |
|
|
/* */ |
37 |
|
|
/* _ux_host_class_cdc_acm_endpoints_get PORTABLE C */ |
38 |
|
|
/* 6.3.0 */ |
39 |
|
|
/* AUTHOR */ |
40 |
|
|
/* */ |
41 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
42 |
|
|
/* */ |
43 |
|
|
/* DESCRIPTION */ |
44 |
|
|
/* */ |
45 |
|
|
/* This function distinguishes for either the Data or Control Class. */ |
46 |
|
|
/* For the data class, we mount the bulk in and bulk out endpoints. */ |
47 |
|
|
/* For the control class, we mount the optional interrupt endpoint. */ |
48 |
|
|
/* */ |
49 |
|
|
/* INPUT */ |
50 |
|
|
/* */ |
51 |
|
|
/* cdc_acm Pointer to cdc_acm class */ |
52 |
|
|
/* */ |
53 |
|
|
/* OUTPUT */ |
54 |
|
|
/* */ |
55 |
|
|
/* Completion Status */ |
56 |
|
|
/* */ |
57 |
|
|
/* CALLS */ |
58 |
|
|
/* */ |
59 |
|
|
/* _ux_host_stack_interface_endpoint_get Get interface endpoint */ |
60 |
|
|
/* */ |
61 |
|
|
/* CALLED BY */ |
62 |
|
|
/* */ |
63 |
|
|
/* _ux_host_class_cdc_acm_activate Activate cdc_acm class */ |
64 |
|
|
/* */ |
65 |
|
|
/**************************************************************************/ |
66 |
|
149 |
UINT _ux_host_class_cdc_acm_endpoints_get(UX_HOST_CLASS_CDC_ACM *cdc_acm) |
67 |
|
|
{ |
68 |
|
|
|
69 |
|
|
UINT status; |
70 |
|
|
UINT endpoint_index; |
71 |
|
|
UX_ENDPOINT *endpoint; |
72 |
|
|
UX_TRANSFER *transfer_request; |
73 |
|
|
|
74 |
|
|
|
75 |
|
|
/* Check what interface we are mounting. */ |
76 |
✓✓ |
149 |
if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_DATA_CLASS) |
77 |
|
|
{ |
78 |
|
|
|
79 |
|
|
/* Search the bulk OUT endpoint. It is attached to the interface container. */ |
80 |
✓✓ |
143 |
for (endpoint_index = 0; endpoint_index < cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bNumEndpoints; |
81 |
|
73 |
endpoint_index++) |
82 |
|
|
{ |
83 |
|
|
|
84 |
|
|
/* Get interface endpoint. */ |
85 |
|
134 |
status = _ux_host_stack_interface_endpoint_get(cdc_acm -> ux_host_class_cdc_acm_interface, endpoint_index, &endpoint); |
86 |
|
|
|
87 |
|
|
/* Check status. */ |
88 |
✗✓ |
134 |
if (status != UX_SUCCESS) |
89 |
|
|
continue; |
90 |
|
|
|
91 |
|
|
/* Check if endpoint is bulk and OUT. */ |
92 |
✓✓ |
134 |
if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) && |
93 |
✓✓ |
67 |
((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT)) |
94 |
|
|
{ |
95 |
|
|
|
96 |
|
|
/* This transfer_request always have the OUT direction. */ |
97 |
|
65 |
endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type = UX_REQUEST_OUT; |
98 |
|
|
|
99 |
|
|
/* Set default timeout for transfer. */ |
100 |
|
65 |
endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value = UX_HOST_CLASS_CDC_ACM_CLASS_TRANSFER_TIMEOUT; |
101 |
|
|
|
102 |
|
|
/* We have found the bulk endpoint, save it. */ |
103 |
|
65 |
cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint = endpoint; |
104 |
|
|
|
105 |
|
|
/* If found all, we break. */ |
106 |
✓✓ |
65 |
if (cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint) |
107 |
|
43 |
break; |
108 |
|
|
} |
109 |
|
|
|
110 |
|
|
/* Check if endpoint is bulk and IN. */ |
111 |
✓✓ |
91 |
if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) && |
112 |
✓✓ |
67 |
((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT)) |
113 |
|
|
{ |
114 |
|
|
|
115 |
|
|
/* This transfer_request always have the IN direction. */ |
116 |
|
66 |
endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type = UX_REQUEST_IN; |
117 |
|
|
|
118 |
|
|
/* Set default timeout for transfer. */ |
119 |
|
66 |
endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value = UX_HOST_CLASS_CDC_ACM_CLASS_TRANSFER_TIMEOUT; |
120 |
|
|
|
121 |
|
|
/* We have found the bulk endpoint, save it. */ |
122 |
|
66 |
cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint = endpoint; |
123 |
|
|
|
124 |
|
|
/* If found all, we break. */ |
125 |
✓✓ |
66 |
if (cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint) |
126 |
|
18 |
break; |
127 |
|
|
} |
128 |
|
|
} |
129 |
|
|
|
130 |
|
|
/* The both bulk endpoints are mandatory. */ |
131 |
✓✓ |
70 |
if (cdc_acm -> ux_host_class_cdc_acm_bulk_out_endpoint == UX_NULL || |
132 |
✓✓ |
65 |
cdc_acm -> ux_host_class_cdc_acm_bulk_in_endpoint == UX_NULL) |
133 |
|
|
{ |
134 |
|
|
|
135 |
|
|
/* Error trap. */ |
136 |
|
9 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN); |
137 |
|
|
|
138 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
139 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0) |
140 |
|
|
|
141 |
|
9 |
return(UX_ENDPOINT_HANDLE_UNKNOWN); |
142 |
|
|
} |
143 |
|
|
} |
144 |
|
|
else |
145 |
|
|
{ |
146 |
|
|
/* Search the Interrupt endpoint. It is attached to the interface container of the control interface. It is not mandatory. */ |
147 |
✓✓ |
98 |
for (endpoint_index = 0; endpoint_index < cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bNumEndpoints; |
148 |
|
19 |
endpoint_index++) |
149 |
|
|
{ |
150 |
|
|
|
151 |
|
|
/* Get the endpoint handle. */ |
152 |
|
89 |
status = _ux_host_stack_interface_endpoint_get(cdc_acm -> ux_host_class_cdc_acm_interface, endpoint_index, &endpoint); |
153 |
|
|
|
154 |
|
|
/* Check status. */ |
155 |
✗✓ |
89 |
if (status != UX_SUCCESS) |
156 |
|
|
continue; |
157 |
|
|
|
158 |
|
|
/* Check if endpoint is Interrupt and IN. */ |
159 |
✓✓ |
89 |
if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) && |
160 |
✓✓ |
75 |
((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT)) |
161 |
|
|
{ |
162 |
|
|
|
163 |
|
|
/* This transfer_request always have the IN direction. */ |
164 |
|
70 |
endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type = UX_REQUEST_IN; |
165 |
|
|
|
166 |
|
|
/* We have found the interrupt endpoint, save it. */ |
167 |
|
70 |
cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint = endpoint; |
168 |
|
|
|
169 |
|
|
/* The endpoint is correct, Fill in the transfer request with the length requested for this endpoint. */ |
170 |
|
70 |
transfer_request = &cdc_acm -> ux_host_class_cdc_acm_interrupt_endpoint -> ux_endpoint_transfer_request; |
171 |
|
70 |
transfer_request -> ux_transfer_request_requested_length = transfer_request -> ux_transfer_request_packet_length; |
172 |
|
70 |
transfer_request -> ux_transfer_request_actual_length = 0; |
173 |
|
|
|
174 |
|
|
/* The direction is always IN for the CDC interrupt endpoint. */ |
175 |
|
70 |
transfer_request -> ux_transfer_request_type = UX_REQUEST_IN; |
176 |
|
|
|
177 |
|
|
/* There is a callback function associated with the transfer request, so we need the class instance. */ |
178 |
|
70 |
transfer_request -> ux_transfer_request_class_instance = (VOID *) cdc_acm; |
179 |
|
|
|
180 |
|
|
/* Interrupt transactions have a completion routine. */ |
181 |
|
70 |
transfer_request -> ux_transfer_request_completion_function = _ux_host_class_cdc_acm_transfer_request_completed; |
182 |
|
|
|
183 |
|
|
/* Obtain a buffer for this transaction. The buffer will always be reused. */ |
184 |
|
70 |
transfer_request -> ux_transfer_request_data_pointer = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, |
185 |
|
|
transfer_request -> ux_transfer_request_requested_length); |
186 |
|
|
|
187 |
|
|
/* If the endpoint is available and we have memory, we start the interrupt endpoint. */ |
188 |
✓✓ |
70 |
if (transfer_request -> ux_transfer_request_data_pointer != UX_NULL) |
189 |
|
|
{ |
190 |
|
|
|
191 |
|
|
/* The transfer on the interrupt endpoint can be started. */ |
192 |
|
69 |
status = _ux_host_stack_transfer_request(transfer_request); |
193 |
|
|
|
194 |
|
|
/* Check error, if endpoint interrupt IN transfer not successful, do not proceed. */ |
195 |
✓✓ |
69 |
if (status != UX_SUCCESS) |
196 |
|
|
|
197 |
|
|
/* Error, do not proceed. */ |
198 |
|
1 |
return(status); |
199 |
|
|
} |
200 |
|
|
else |
201 |
|
|
{ |
202 |
|
|
|
203 |
|
|
/* Error trap. */ |
204 |
|
1 |
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT); |
205 |
|
|
|
206 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
207 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, cdc_acm, 0, 0, UX_TRACE_ERRORS, 0, 0) |
208 |
|
|
|
209 |
|
|
/* We must return an error. */ |
210 |
|
1 |
return(UX_ENDPOINT_HANDLE_UNKNOWN); |
211 |
|
|
} |
212 |
|
68 |
break; |
213 |
|
|
} |
214 |
|
|
} |
215 |
|
|
} |
216 |
|
|
|
217 |
|
|
/* All endpoints have been mounted. */ |
218 |
|
138 |
return(UX_SUCCESS); |
219 |
|
|
} |
220 |
|
|
|