1 /**************************************************************************/
2 /* */
3 /* Copyright (c) Microsoft Corporation. All rights reserved. */
4 /* */
5 /* This software is licensed under the Microsoft Software License */
6 /* Terms for Microsoft Azure RTOS. Full text of the license can be */
7 /* found in the LICENSE file at https://aka.ms/AzureRTOS_EULA */
8 /* and in the root directory of this software. */
9 /* */
10 /**************************************************************************/
11
12
13 /**************************************************************************/
14 /**************************************************************************/
15 /** */
16 /** USBX Component */
17 /** */
18 /** Host Stack */
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_stack.h"
30
31
32 #if UX_MAX_DEVICES > 1
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _ux_host_stack_bandwidth_release PORTABLE C */
38 /* 6.1 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function will release bandwidth for a periodic endpoint. The */
46 /* bandwidth requirement is calculated by the MaxPacketSize field of */
47 /* endpoint and the speed of the endpoint. If the device is on a 1.1 */
48 /* bus or it is a 1.1 device behind a 2.0 hub on a 2.0 bus, the device */
49 /* bandwidth must be multiplied by 8 on the 1.1 segment. */
50 /* */
51 /* This algorithm takes into account both TT bandwidth and HCD */
52 /* bandwidth. The TTs are attached to the device structure and not the */
53 /* hub structure in order to make the stack agnostic of the hub class. */
54 /* */
55 /* INPUT */
56 /* */
57 /* HCD Pointer to HCD */
58 /* endpoint Pointer to endpoint */
59 /* */
60 /* OUTPUT */
61 /* */
62 /* None */
63 /* */
64 /* CALLS */
65 /* */
66 /* None */
67 /* */
68 /* CALLED BY */
69 /* */
70 /* USBX Components */
71 /* */
72 /* RELEASE HISTORY */
73 /* */
74 /* DATE NAME DESCRIPTION */
75 /* */
76 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
77 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
78 /* optimized based on compile */
79 /* definitions, */
80 /* resulting in version 6.1 */
81 /* */
82 /**************************************************************************/
_ux_host_stack_bandwidth_release(UX_HCD * hcd,UX_ENDPOINT * endpoint)83 VOID _ux_host_stack_bandwidth_release(UX_HCD *hcd, UX_ENDPOINT *endpoint)
84 {
85
86 UX_DEVICE *device;
87 UX_DEVICE *parent_device;
88 USHORT hcd_bandwidth_claimed;
89 USHORT max_packet_size;
90 LONG packet_size;
91 USHORT tt_bandwidth_claimed = 0;
92 ULONG port_index;
93 ULONG port_map;
94 ULONG tt_index;
95 const UCHAR overheads[4][3] = {
96 /* LS FS HS */
97 {63, 45, 173}, /* Control */
98 { 0, 9, 38}, /* Isochronous */
99 { 0, 13, 55}, /* Bulk */
100 {19, 13, 55} /* Interrupt */
101 };
102
103 /* Get the pointer to the device. */
104 device = endpoint -> ux_endpoint_device;
105
106 /* Calculate the bandwidth. From USB spec.
107 *
108 * The frame unit consumed per byte is like follow:
109 * Bytes/FrameUnit FrameUnit/byte FrameUnit/byte
110 * (Overhead included) (HS baseline) (FS baseline)
111 * Low Speed 187.5 40 8
112 * Full Speed 1500 5 1
113 * High Speed 7500 1 1/5
114 *
115 * The overhead is like follow:
116 * Control Isochronous Bulk Interrupt
117 * bmAttribute (0) (1) (2) (3)
118 * Low Speed 63 -- -- 19
119 * Full Speed 45 9 13 13
120 * High Speed 173 38 55 55
121 *
122 * Worst case bit stuffing is calculated as 1.1667 (7/6) times the raw time.
123 */
124
125 /* Get maximum packet size. */
126 max_packet_size = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_PACKET_SIZE_MASK;
127
128 /* Rough time for possible Bit Stuffing. */
129 packet_size = (max_packet_size * 7 + 5) / 6;
130
131 /* Add overhead. */
132 packet_size += overheads[endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE][device -> ux_device_speed];
133 max_packet_size = (USHORT)packet_size;
134
135 /* Check for high-speed endpoint. */
136 if (device -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
137 {
138
139 /* Get number of transactions. */
140 max_packet_size = (USHORT)(max_packet_size *
141 (((endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_NUMBER_OF_TRANSACTIONS_MASK) >>
142 UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT) + 1));
143 }
144
145 /* Calculate the bandwidth claimed by this endpoint for the main bus. */
146 if (hcd -> ux_hcd_version != 0x200)
147 {
148
149 if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE)
150 /* Low speed transfer takes 40x more units than high speed. */
151 hcd_bandwidth_claimed = (USHORT)(max_packet_size * 8 * 5);
152 else
153 {
154
155 if (device -> ux_device_speed == UX_FULL_SPEED_DEVICE)
156 /* Full speed transfer takes 5x more units than high speed. */
157 hcd_bandwidth_claimed = (USHORT)(max_packet_size * 5);
158 else
159 /* Use high speed timing as base for bus bandwidth calculation. */
160 hcd_bandwidth_claimed = (USHORT)max_packet_size;
161 }
162 }
163 else
164 {
165
166 hcd_bandwidth_claimed = (USHORT)max_packet_size;
167 if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE)
168 /* Low speed transfer takes 8x more units than full speed. */
169 tt_bandwidth_claimed = (USHORT)(max_packet_size * 8);
170 else
171 /* Use full speed timing as base for TT bandwidth calculation. */
172 tt_bandwidth_claimed = (USHORT)max_packet_size;
173 }
174
175 /* Free the HCD bandwidth. */
176 hcd -> ux_hcd_available_bandwidth += hcd_bandwidth_claimed;
177
178 /* We need to take care of the case where the endpoint belongs to a USB 1.1
179 device that sits behind a 2.0 hub. We ignore cases where the device
180 is either high speed or the bus is 1.1. */
181 if ((device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) || (hcd -> ux_hcd_version != 0x200))
182 {
183
184 /* The device is high speed, therefore no need for TT. */
185 return;
186 }
187
188 /* We have a 1.1 device, check if the parent is a 2.0 hub. */
189 parent_device = device -> ux_device_parent;
190 if (parent_device == UX_NULL)
191 {
192
193 /* We are at the root, must be a 1.1 controller then! */
194 return;
195 }
196
197 /* We get here when the parent is a hub. The problem occurs when the hub is itself
198 connected to a chain of hubs. We need to find the first 2.0 hub parent to this chain
199 to check the TT. We need to remember the port on which the first 1.1 device is
200 hooked to. */
201 port_index = device -> ux_device_port_location - 1;
202
203 /* Scan the chain of hubs upward. */
204 while (parent_device != UX_NULL)
205 {
206
207 /* Check for a high speed device. */
208 if (parent_device -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
209 {
210
211 /* The device is a high speed hub, find the TT that manages the port.
212 The first 1.1 device is connected to. First we calculate the port mapping bit. */
213 port_map = (ULONG)(1 << port_index);
214
215 /* Parse all the TTs attached to the hub. */
216 for (tt_index = 0; tt_index < UX_MAX_TT; tt_index++)
217 {
218
219 /* Check if this TT owns the port where the device is attached. */
220 if ((parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_port_mapping & port_map) != 0)
221 {
222
223 /* We have found the port, check if the tt can give us the bandwidth
224 we want to claim. */
225 parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_max_bandwidth += tt_bandwidth_claimed;
226 return;
227 }
228 }
229
230 /* We should never get here!!!!! */
231 return;
232 }
233
234 /* We now remember where this hub is located on the parent. */
235 port_index = parent_device -> ux_device_port_location - 1;
236
237 /* We go up one level in the hub chain. */
238 parent_device = parent_device -> ux_device_parent;
239 }
240
241 /* We get here when we have not found a 2.0 hub in the list and we got
242 to the root port. */
243 return;
244 }
245 #endif /* #if UX_MAX_DEVICES > 1 */
246