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