Select one of the symbols to view example projects that use it.
 
Outline
...
...
...
...
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_stack.h"
...
...
_ux_host_stack_bandwidth_check(UX_HCD *, UX_ENDPOINT *)
Files
loading...
CodeScopeSTM32 Libraries and Samplesusbxcommon/core/src/ux_host_stack_bandwidth_check.c
 
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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
/**************************************************************************/ /* */ /* Copyright (c) Microsoft Corporation. All rights reserved. */ /* */ /* This software is licensed under the Microsoft Software License */ /* Terms for Microsoft Azure RTOS. Full text of the license can be */ /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */ /* and in the root directory of this software. */ /* */... /**************************************************************************/ ... /**************************************************************************/ /**************************************************************************/ /** */ /** USBX Component */ /** */ /** Host Stack */ /** */... /**************************************************************************/ /**************************************************************************/ /* Include necessary system files. */ #define UX_SOURCE_CODE #include "ux_api.h" #include "ux_host_stack.h" #if UX_MAX_DEVICES > 1... /**************************************************************************/ /* */ /* FUNCTION RELEASE */ /* */ /* _ux_host_stack_bandwidth_check PORTABLE C */ /* 6.1 */ /* AUTHOR */ /* */ /* Chaoqiong Xiao, Microsoft Corporation */ /* */ /* DESCRIPTION */ /* */ /* This function will check if there is enough bandwidth on the USB */ /* for the specified endpoint. The bandwidth requirement is calculated */ /* by the MaxPacketSize field of endpoint and the speed of the */ /* endpoint. If the device is on a 1.1 bus or it is a 1.1 device */ /* behind a 2.0 hub on a 2.0 bus, the device bandwidth must be */ /* multiplied by 8 on the 1.1 segment. */ /* */ /* This algorithm takes into account both TT bandwidth and HCD */ /* bandwidth. The TTs are attached to the device structure and not */ /* the hub structure in order to make the stack agnostic of the hub */ /* class. */ /* */ /* INPUT */ /* */ /* HCD Pointer to HCD */ /* endpoint Pointer to endpoint */ /* */ /* OUTPUT */ /* */ /* Completion Status */ /* */ /* CALLS */ /* */ /* None */ /* */ /* CALLED BY */ /* */ /* USBX Components */ /* */ /* RELEASE HISTORY */ /* */ /* DATE NAME DESCRIPTION */ /* */ /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */ /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */ /* optimized based on compile */ /* definitions, */ /* resulting in version 6.1 */ /* */... /**************************************************************************/ UINT _ux_host_stack_bandwidth_check(UX_HCD *hcd, UX_ENDPOINT *endpoint) { UX_DEVICE *device; UX_DEVICE *parent_device; USHORT hcd_bandwidth_claimed; USHORT max_packet_size; LONG packet_size; USHORT tt_bandwidth_claimed = 0; ULONG port_index; ULONG port_map; ULONG tt_index; const UCHAR overheads[4][3] = { /* LS FS HS */ {63, 45, 173}, /* Control */ { 0, 9, 38}, /* Isochronous */ { 0, 13, 55}, /* Bulk */ {19, 13, 55} /* Interrupt */ ...}; /* Get the pointer to the device. */ device = endpoint -> ux_endpoint_device; /* Calculate the bandwidth. From USB spec. * * The frame unit consumed per byte is like follow: * Bytes/FrameUnit FrameUnit/byte FrameUnit/byte * (Overhead included) (HS baseline) (FS baseline) * Low Speed 187.5 40 8 * Full Speed 1500 5 1 * High Speed 7500 1 1/5 * * The overhead is like follow: * Control Isochronous Bulk Interrupt * bmAttribute (0) (1) (2) (3) * Low Speed 63 -- -- 19 * Full Speed 45 9 13 13 * High Speed 173 38 55 55 * * Worst case bit stuffing is calculated as 1.1667 (7/6) times the raw time. *//* ... */ /* Get maximum packet size. */ max_packet_size = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_PACKET_SIZE_MASK; /* Rough time for possible Bit Stuffing. */ packet_size = (max_packet_size * 7 + 5) / 6; /* Add overhead. */ packet_size += overheads[endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE][device -> ux_device_speed]; max_packet_size = (USHORT)packet_size; /* Check for high-speed endpoint. */ if (device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) { /* Get number of transactions. */ max_packet_size = (USHORT)(max_packet_size * (((endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_NUMBER_OF_TRANSACTIONS_MASK) >> UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT) + 1)); }if (device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) { ... } /* Calculate the bandwidth claimed by this endpoint for the main bus. */ if (hcd -> ux_hcd_version != 0x200) { if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE) /* Low speed transfer takes 40x more units than high speed. */ hcd_bandwidth_claimed = (USHORT)(max_packet_size * 8 * 5); else { if (device -> ux_device_speed == UX_FULL_SPEED_DEVICE) /* Full speed transfer takes 5x more units than high speed. */ hcd_bandwidth_claimed = (USHORT)(max_packet_size * 5); else /* Use high speed timing as base for bus bandwidth calculation. */ hcd_bandwidth_claimed = (USHORT)max_packet_size; }else { ... } }if (hcd -> ux_hcd_version != 0x200) { ... } else { hcd_bandwidth_claimed = (USHORT)max_packet_size; if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE) /* Low speed transfer takes 8x more units than full speed. */ tt_bandwidth_claimed = (USHORT)(max_packet_size * 8); else /* Use full speed timing as base for TT bandwidth calculation. */ tt_bandwidth_claimed = (USHORT)max_packet_size; }else { ... } /* Do we have enough on the bus for this new endpoint? */ if (hcd -> ux_hcd_available_bandwidth < hcd_bandwidth_claimed) { /* If trace is enabled, insert this event into the trace buffer. */ UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_NO_BANDWIDTH_AVAILABLE, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0) /* Error trap. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_NO_BANDWIDTH_AVAILABLE); return(UX_NO_BANDWIDTH_AVAILABLE); }if (hcd -> ux_hcd_available_bandwidth < hcd_bandwidth_claimed) { ... } /* We need to take care of the case where the endpoint belongs to a USB 1.1 device that sits behind a 2.0 hub. We ignore cases where the device is either high speed or the bus is 1.1. *//* ... */ if ((device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) || (hcd -> ux_hcd_version != 0x200)) { /* The device is high speed, therefore no need for TT. */ return(UX_SUCCESS); }if ((device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) || (hcd -> ux_hcd_version != 0x200)) { ... } /* We have a 1.1 device, check if the parent is a 2.0 hub. */ parent_device = device -> ux_device_parent; if (parent_device == UX_NULL) { /* We are at the root, this controller must support 1.1 then! */ return(UX_SUCCESS); }if (parent_device == UX_NULL) { ... } /* We get here when the parent is a hub. The problem occurs when the hub is itself connected to a chain of hubs. We need to find the first 2.0 hub parent to this chain to check the TT. We need to remember the port on which the first 1.1 device is hooked to. *//* ... */ port_index = device -> ux_device_port_location - 1; /* Scan the chain of hubs upward. */ while (parent_device != UX_NULL) { /* Determine if the device is high speed. */ if (parent_device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) { /* The device is a high speed hub, find the TT that manages the port. The first 1.1 device is connected to. First we calculate the port mapping bit. *//* ... */ port_map = (ULONG)(1 << port_index); /* Parse all the TTs attached to the hub. */ for (tt_index = 0; tt_index < UX_MAX_TT; tt_index++) { /* Check if this TT owns the port where the device is attached. */ if ((parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_port_mapping & port_map) != 0) { /* We have found the port, check if the tt can give us the bandwidth we want to claim. *//* ... */ if (parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_max_bandwidth < tt_bandwidth_claimed) { /* If trace is enabled, insert this event into the trace buffer. */ UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_NO_BANDWIDTH_AVAILABLE, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0) /* Error trap. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_NO_BANDWIDTH_AVAILABLE); return(UX_NO_BANDWIDTH_AVAILABLE); }if (parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_max_bandwidth < tt_bandwidth_claimed) { ... } else return(UX_SUCCESS); }if ((parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_port_mapping & port_map) != 0) { ... } }for (tt_index = 0; tt_index < UX_MAX_TT; tt_index++) { ... } /* If trace is enabled, insert this event into the trace buffer. */ UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_NO_BANDWIDTH_AVAILABLE, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0) /* Error trap. */ _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_NO_BANDWIDTH_AVAILABLE); /* We should never get here !!!!! */ return(UX_NO_BANDWIDTH_AVAILABLE); }if (parent_device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) { ... } /* We now remember where this hub is located on the parent. */ port_index = parent_device -> ux_device_port_location - 1; /* We go up one level in the hub chain. */ parent_device = parent_device -> ux_device_parent; }while (parent_device != UX_NULL) { ... } /* We get here when we have not found a 2.0 hub in the list and we got to the root port. */ return(UX_SUCCESS); }{ ... } #endif... /* ... *//* #if UX_MAX_DEVICES > 1 */