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 |
|
|
/** Device Stack */ |
19 |
|
|
/** */ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
/**************************************************************************/ |
22 |
|
|
|
23 |
|
|
#define UX_SOURCE_CODE |
24 |
|
|
|
25 |
|
|
|
26 |
|
|
/* Include necessary system files. */ |
27 |
|
|
|
28 |
|
|
#include "ux_api.h" |
29 |
|
|
#include "ux_device_stack.h" |
30 |
|
|
|
31 |
|
|
|
32 |
|
|
/**************************************************************************/ |
33 |
|
|
/* */ |
34 |
|
|
/* FUNCTION RELEASE */ |
35 |
|
|
/* */ |
36 |
|
|
/* _ux_device_stack_interface_set PORTABLE C */ |
37 |
|
|
/* 6.1.12 */ |
38 |
|
|
/* AUTHOR */ |
39 |
|
|
/* */ |
40 |
|
|
/* Chaoqiong Xiao, Microsoft Corporation */ |
41 |
|
|
/* */ |
42 |
|
|
/* DESCRIPTION */ |
43 |
|
|
/* */ |
44 |
|
|
/* This function sets one alternate setting of one interface and */ |
45 |
|
|
/* enable all endpoints associated with this alternate setting. */ |
46 |
|
|
/* configuration. */ |
47 |
|
|
/* */ |
48 |
|
|
/* INPUT */ |
49 |
|
|
/* */ |
50 |
|
|
/* device_framework Address in device framework */ |
51 |
|
|
/* for selected alternate setting*/ |
52 |
|
|
/* device_framework_length Length of device framework */ |
53 |
|
|
/* alternate_setting_value Alternate setting */ |
54 |
|
|
/* */ |
55 |
|
|
/* OUTPUT */ |
56 |
|
|
/* */ |
57 |
|
|
/* Completion Status */ |
58 |
|
|
/* */ |
59 |
|
|
/* CALLS */ |
60 |
|
|
/* */ |
61 |
|
|
/* (ux_slave_dcd_function) DCD dispatch function */ |
62 |
|
|
/* _ux_device_stack_interface_start Start interface */ |
63 |
|
|
/* _ux_utility_descriptor_parse Parse descriptor */ |
64 |
|
|
/* */ |
65 |
|
|
/* CALLED BY */ |
66 |
|
|
/* */ |
67 |
|
|
/* Application */ |
68 |
|
|
/* Device Stack */ |
69 |
|
|
/* */ |
70 |
|
|
/**************************************************************************/ |
71 |
|
1544 |
UINT _ux_device_stack_interface_set(UCHAR * device_framework, ULONG device_framework_length, |
72 |
|
|
ULONG alternate_setting_value) |
73 |
|
|
{ |
74 |
|
|
|
75 |
|
|
UX_SLAVE_DCD *dcd; |
76 |
|
|
UX_SLAVE_DEVICE *device; |
77 |
|
|
UX_SLAVE_TRANSFER *transfer_request; |
78 |
|
|
UX_SLAVE_INTERFACE *interface_ptr; |
79 |
|
|
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1 |
80 |
|
|
UX_SLAVE_INTERFACE *interface_link; |
81 |
|
|
ULONG interfaces_pool_number; |
82 |
|
|
#endif |
83 |
|
|
UX_SLAVE_ENDPOINT *endpoint; |
84 |
|
|
UX_SLAVE_ENDPOINT *endpoint_link; |
85 |
|
|
ULONG descriptor_length; |
86 |
|
|
UCHAR descriptor_type; |
87 |
|
|
ULONG endpoints_pool_number; |
88 |
|
|
UINT status; |
89 |
|
|
ULONG max_transfer_length, n_trans; |
90 |
|
|
|
91 |
|
|
UX_PARAMETER_NOT_USED(alternate_setting_value); |
92 |
|
|
|
93 |
|
|
/* If trace is enabled, insert this event into the trace buffer. */ |
94 |
|
|
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_INTERFACE_SET, alternate_setting_value, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0) |
95 |
|
|
|
96 |
|
|
/* Get the pointer to the DCD. */ |
97 |
|
1544 |
dcd = &_ux_system_slave -> ux_system_slave_dcd; |
98 |
|
|
|
99 |
|
|
/* Get the pointer to the device. */ |
100 |
|
1544 |
device = &_ux_system_slave -> ux_system_slave_device; |
101 |
|
|
|
102 |
|
|
/* Find a free interface in the pool and hook it to the |
103 |
|
|
existing interface. */ |
104 |
|
1544 |
interface_ptr = device -> ux_slave_device_interfaces_pool; |
105 |
|
|
|
106 |
|
|
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1 |
107 |
|
1544 |
interfaces_pool_number = device -> ux_slave_device_interfaces_pool_number; |
108 |
✓✓ |
2394 |
while (interfaces_pool_number != 0) |
109 |
|
|
{ |
110 |
|
|
/* Check if this interface is free. */ |
111 |
✓✓ |
2393 |
if (interface_ptr -> ux_slave_interface_status == UX_UNUSED) |
112 |
|
1543 |
break; |
113 |
|
|
|
114 |
|
|
/* Try the next interface. */ |
115 |
|
850 |
interface_ptr++; |
116 |
|
|
|
117 |
|
|
/* Decrement the number of interfaces left to scan in the pool. */ |
118 |
|
850 |
interfaces_pool_number--; |
119 |
|
|
} |
120 |
|
|
|
121 |
|
|
/* Did we find a free interface ? */ |
122 |
✓✓ |
1544 |
if (interfaces_pool_number == 0) |
123 |
|
1 |
return(UX_MEMORY_INSUFFICIENT); |
124 |
|
|
#else |
125 |
|
|
|
126 |
|
|
/* Check if this interface is free. */ |
127 |
|
|
if (interface_ptr -> ux_slave_interface_status != UX_UNUSED) |
128 |
|
|
return(UX_MEMORY_INSUFFICIENT); |
129 |
|
|
|
130 |
|
|
#endif |
131 |
|
|
|
132 |
|
|
/* Mark this interface as used now. */ |
133 |
|
1543 |
interface_ptr -> ux_slave_interface_status = UX_USED; |
134 |
|
|
|
135 |
|
|
/* If trace is enabled, register this object. */ |
136 |
|
|
UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, interface_ptr, 0, 0, 0) |
137 |
|
|
|
138 |
|
|
/* Parse the descriptor in something more readable. */ |
139 |
|
1543 |
_ux_utility_descriptor_parse(device_framework, |
140 |
|
|
_ux_system_interface_descriptor_structure, |
141 |
|
|
UX_INTERFACE_DESCRIPTOR_ENTRIES, |
142 |
|
1543 |
(UCHAR *) &interface_ptr -> ux_slave_interface_descriptor); |
143 |
|
|
|
144 |
|
|
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1 |
145 |
|
|
|
146 |
|
|
/* Attach this interface to the end of the interface chain. */ |
147 |
✓✓ |
1543 |
if (device -> ux_slave_device_first_interface == UX_NULL) |
148 |
|
|
{ |
149 |
|
|
|
150 |
|
907 |
device -> ux_slave_device_first_interface = interface_ptr; |
151 |
|
|
} |
152 |
|
|
else |
153 |
|
|
{ |
154 |
|
|
/* Multiple interfaces exist, so find the end of the chain. */ |
155 |
|
636 |
interface_link = device -> ux_slave_device_first_interface; |
156 |
✓✓ |
849 |
while (interface_link -> ux_slave_interface_next_interface != UX_NULL) |
157 |
|
213 |
interface_link = interface_link -> ux_slave_interface_next_interface; |
158 |
|
636 |
interface_link -> ux_slave_interface_next_interface = interface_ptr; |
159 |
|
|
} |
160 |
|
|
#else |
161 |
|
|
|
162 |
|
|
/* It must be very first one. */ |
163 |
|
|
device -> ux_slave_device_first_interface = interface_ptr; |
164 |
|
|
#endif |
165 |
|
|
|
166 |
|
|
/* Point beyond the interface descriptor. */ |
167 |
|
1543 |
device_framework_length -= (ULONG) *device_framework; |
168 |
|
1543 |
device_framework += (ULONG) *device_framework; |
169 |
|
|
|
170 |
|
|
/* Parse the device framework and locate endpoint descriptor(s). */ |
171 |
✓✓ |
5031 |
while (device_framework_length != 0) |
172 |
|
|
{ |
173 |
|
|
|
174 |
|
|
/* Get the length of the current descriptor. */ |
175 |
|
4351 |
descriptor_length = (ULONG) *device_framework; |
176 |
|
|
|
177 |
|
|
/* And its type. */ |
178 |
|
4351 |
descriptor_type = *(device_framework + 1); |
179 |
|
|
|
180 |
|
|
/* Check if this is an endpoint descriptor. */ |
181 |
✓✓✓ |
4351 |
switch(descriptor_type) |
182 |
|
|
{ |
183 |
|
|
|
184 |
|
1861 |
case UX_ENDPOINT_DESCRIPTOR_ITEM: |
185 |
|
|
|
186 |
|
|
/* Find a free endpoint in the pool and hook it to the |
187 |
|
|
existing interface after it's created by DCD. */ |
188 |
|
1861 |
endpoint = device -> ux_slave_device_endpoints_pool; |
189 |
|
1861 |
endpoints_pool_number = device -> ux_slave_device_endpoints_pool_number; |
190 |
✓✓ |
3427 |
while (endpoints_pool_number != 0) |
191 |
|
|
{ |
192 |
|
|
/* Check if this endpoint is free. */ |
193 |
✓✓ |
3426 |
if (endpoint -> ux_slave_endpoint_status == UX_UNUSED) |
194 |
|
|
{ |
195 |
|
|
/* Mark this endpoint as used now. */ |
196 |
|
1860 |
endpoint -> ux_slave_endpoint_status = UX_USED; |
197 |
|
1860 |
break; |
198 |
|
|
} |
199 |
|
|
|
200 |
|
|
/* Try the next endpoint. */ |
201 |
|
1566 |
endpoint++; |
202 |
|
|
|
203 |
|
|
/* Decrement the number of endpoints to scan from the pool. */ |
204 |
|
1566 |
endpoints_pool_number--; |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
/* Did we find a free endpoint ? */ |
208 |
✓✓ |
1861 |
if (endpoints_pool_number == 0) |
209 |
|
1 |
return(UX_MEMORY_INSUFFICIENT); |
210 |
|
|
|
211 |
|
|
/* Parse the descriptor in something more readable. */ |
212 |
|
1860 |
_ux_utility_descriptor_parse(device_framework, |
213 |
|
|
_ux_system_endpoint_descriptor_structure, |
214 |
|
|
UX_ENDPOINT_DESCRIPTOR_ENTRIES, |
215 |
|
1860 |
(UCHAR *) &endpoint -> ux_slave_endpoint_descriptor); |
216 |
|
|
|
217 |
|
|
/* Now we create a transfer request to accept transfer on this endpoint. */ |
218 |
|
1860 |
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request; |
219 |
|
|
|
220 |
|
|
/* Validate endpoint descriptor wMaxPacketSize. */ |
221 |
|
|
UX_ASSERT(endpoint -> ux_slave_endpoint_descriptor.wMaxPacketSize != 0); |
222 |
|
|
|
223 |
|
|
/* Calculate endpoint transfer payload max size. */ |
224 |
|
1860 |
max_transfer_length = |
225 |
|
1860 |
endpoint -> ux_slave_endpoint_descriptor.wMaxPacketSize & |
226 |
|
|
UX_MAX_PACKET_SIZE_MASK; |
227 |
✓✓ |
1860 |
if ((_ux_system_slave -> ux_system_slave_speed == UX_HIGH_SPEED_DEVICE) && |
228 |
✓✓ |
163 |
(endpoint -> ux_slave_endpoint_descriptor.bmAttributes & 0x1u)) |
229 |
|
|
{ |
230 |
|
57 |
n_trans = endpoint -> ux_slave_endpoint_descriptor.wMaxPacketSize & |
231 |
|
|
UX_MAX_NUMBER_OF_TRANSACTIONS_MASK; |
232 |
✓✓ |
57 |
if (n_trans) |
233 |
|
|
{ |
234 |
|
5 |
n_trans >>= UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT; |
235 |
|
5 |
n_trans ++; |
236 |
|
5 |
max_transfer_length *= n_trans; |
237 |
|
|
} |
238 |
|
|
} |
239 |
|
|
|
240 |
|
|
/* Validate max transfer size and save it. */ |
241 |
|
|
UX_ASSERT(max_transfer_length <= UX_SLAVE_REQUEST_DATA_MAX_LENGTH); |
242 |
|
1860 |
transfer_request -> ux_slave_transfer_request_transfer_length = max_transfer_length; |
243 |
|
|
|
244 |
|
|
/* We store the endpoint in the transfer request as well. */ |
245 |
|
1860 |
transfer_request -> ux_slave_transfer_request_endpoint = endpoint; |
246 |
|
|
|
247 |
|
|
/* By default the timeout is infinite on request. */ |
248 |
|
1860 |
transfer_request -> ux_slave_transfer_request_timeout = UX_WAIT_FOREVER; |
249 |
|
|
|
250 |
|
|
/* Attach the interface to the endpoint. */ |
251 |
|
1860 |
endpoint -> ux_slave_endpoint_interface = interface_ptr; |
252 |
|
|
|
253 |
|
|
/* Attach the device to the endpoint. */ |
254 |
|
1860 |
endpoint -> ux_slave_endpoint_device = device; |
255 |
|
|
|
256 |
|
|
/* Create the endpoint at the DCD level. */ |
257 |
|
1860 |
status = dcd -> ux_slave_dcd_function(dcd, UX_DCD_CREATE_ENDPOINT, (VOID *) endpoint); |
258 |
|
|
|
259 |
|
|
/* Do a sanity check on endpoint creation. */ |
260 |
✓✓ |
1860 |
if (status != UX_SUCCESS) |
261 |
|
|
{ |
262 |
|
|
|
263 |
|
|
/* Error was returned, endpoint cannot be created. */ |
264 |
|
1 |
endpoint -> ux_slave_endpoint_status = UX_UNUSED; |
265 |
|
1 |
return(status); |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
/* Attach this endpoint to the end of the endpoint chain. */ |
269 |
✓✓ |
1859 |
if (interface_ptr -> ux_slave_interface_first_endpoint == UX_NULL) |
270 |
|
|
{ |
271 |
|
|
|
272 |
|
1346 |
interface_ptr -> ux_slave_interface_first_endpoint = endpoint; |
273 |
|
|
} |
274 |
|
|
else |
275 |
|
|
{ |
276 |
|
|
/* Multiple endpoints exist, so find the end of the chain. */ |
277 |
|
513 |
endpoint_link = interface_ptr -> ux_slave_interface_first_endpoint; |
278 |
✓✓ |
634 |
while (endpoint_link -> ux_slave_endpoint_next_endpoint != UX_NULL) |
279 |
|
121 |
endpoint_link = endpoint_link -> ux_slave_endpoint_next_endpoint; |
280 |
|
513 |
endpoint_link -> ux_slave_endpoint_next_endpoint = endpoint; |
281 |
|
|
} |
282 |
|
1859 |
break; |
283 |
|
|
|
284 |
|
861 |
case UX_CONFIGURATION_DESCRIPTOR_ITEM: |
285 |
|
|
case UX_INTERFACE_DESCRIPTOR_ITEM: |
286 |
|
|
|
287 |
|
|
/* If the descriptor is a configuration or interface, |
288 |
|
|
we have parsed and mounted all endpoints. |
289 |
|
|
The interface attached to this configuration must be started at the class level. */ |
290 |
|
861 |
status = _ux_device_stack_interface_start(interface_ptr); |
291 |
|
|
|
292 |
|
|
/* Return the status to the caller. */ |
293 |
|
861 |
return(status); |
294 |
|
|
|
295 |
|
1629 |
default: |
296 |
|
1629 |
break; |
297 |
|
|
} |
298 |
|
|
|
299 |
|
|
/* Adjust what is left of the device framework. */ |
300 |
|
3488 |
device_framework_length -= descriptor_length; |
301 |
|
|
|
302 |
|
|
/* Point to the next descriptor. */ |
303 |
|
3488 |
device_framework += descriptor_length; |
304 |
|
|
} |
305 |
|
|
|
306 |
|
|
/* The interface attached to this configuration must be started at the class |
307 |
|
|
level. */ |
308 |
|
680 |
status = _ux_device_stack_interface_start(interface_ptr); |
309 |
|
|
|
310 |
|
|
/* Return the status to the caller. */ |
311 |
|
680 |
return(status); |
312 |
|
|
} |
313 |
|
|
|