GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_bandwidth_claim.c Lines: 40 40 100.0 %
Date: 2024-12-12 17:16:36 Branches: 22 22 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** USBX Component                                                        */
16
/**                                                                       */
17
/**   Host Stack                                                          */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
23
/* Include necessary system files.  */
24
25
#define UX_SOURCE_CODE
26
27
#include "ux_api.h"
28
#include "ux_host_stack.h"
29
30
31
#if UX_MAX_DEVICES > 1
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_stack_bandwidth_claim                      PORTABLE C      */
37
/*                                                           6.1          */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function will reserve bandwidth for a periodic endpoint. The   */
45
/*    bandwidth requirement is calculated by the MaxPacketSize field of   */
46
/*    endpoint and the speed of the endpoint. If the device is on a 1.1   */
47
/*    bus or it is a 1.1 device behind a 2.0 hub on a 2.0 bus, the device */
48
/*    bandwidth must be multiplied by 8 on the 1.1 segment.               */
49
/*                                                                        */
50
/*    This algorithm takes into account both TT bandwidth and HCD         */
51
/*    bandwidth. The TTs are attached to the device structure and not     */
52
/*    the hub structure in order to make the stack agnostic of the hub    */
53
/*    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
/**************************************************************************/
83
1186
VOID  _ux_host_stack_bandwidth_claim(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
1186
USHORT          tt_bandwidth_claimed =  0;
92
ULONG           port_index;
93
ULONG           port_map;
94
ULONG           tt_index;
95
1186
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
1186
    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
1186
    max_packet_size  = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_PACKET_SIZE_MASK;
127
128
    /* Rough time for possible Bit Stuffing.  */
129
1186
    packet_size = (max_packet_size * 7 + 5) / 6;
130
131
    /* Add overhead.  */
132
1186
    packet_size += overheads[endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE][device -> ux_device_speed];
133
1186
    max_packet_size = (USHORT)packet_size;
134
135
    /* Check for high-speed endpoint.  */
136
1186
    if (device -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
137
    {
138
139
        /* Get number of transactions.  */
140
61
        max_packet_size = (USHORT)(max_packet_size *
141
61
                    (((endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_NUMBER_OF_TRANSACTIONS_MASK) >>
142
61
                        UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT) + 1));
143
    }
144
145
    /* Calculate the bandwidth claimed by this endpoint for the main bus.  */
146
1186
    if (hcd -> ux_hcd_version != 0x200)
147
    {
148
149
1169
        if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE)
150
            /* Low speed transfer takes 40x more units than high speed. */
151
3
            hcd_bandwidth_claimed =  (USHORT)(max_packet_size * 8 * 5);
152
        else
153
        {
154
155
1166
            if (device -> ux_device_speed == UX_FULL_SPEED_DEVICE)
156
                /* Full speed transfer takes 5x more units than high speed. */
157
1108
                hcd_bandwidth_claimed =  (USHORT)(max_packet_size * 5);
158
            else
159
                /* Use high speed timing as base for bus bandwidth calculation. */
160
58
                hcd_bandwidth_claimed =  (USHORT)max_packet_size;
161
        }
162
    }
163
    else
164
    {
165
166
17
        hcd_bandwidth_claimed =  (USHORT)max_packet_size;
167
17
        if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE)
168
            /* Low speed transfer takes 8x more units than full speed. */
169
3
            tt_bandwidth_claimed =  (USHORT)(max_packet_size * 8);
170
        else
171
            /* Use full speed timing as base for TT bandwidth calculation. */
172
14
            tt_bandwidth_claimed =  (USHORT)max_packet_size;
173
    }
174
175
    /* Allocate the HCD bandwidth, since it's already checked by _bandwidth_check.  */
176
1186
    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

1186
    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
1172
        return;
186
    }
187
188
    /* We have a 1.1 device, check if the parent is a 2.0 hub.  */
189
14
    parent_device =  device -> ux_device_parent;
190
14
    if (parent_device == UX_NULL)
191
    {
192
193
        /* We are at the root, must be a 1.1 controller then!  */
194
6
        return;
195
    }
196
197
    /* We get here when the parent is a hub. The problem occurs when the hub is
198
       itself connected to a chain of hubs. We need to find the first 2.0 hub
199
       parent to this chain to check the TT. We need to remember the port on
200
       which the first 1.1 device is hooked to.  */
201
8
    port_index =  device -> ux_device_port_location - 1;
202
203
    /* Scan the chain of hubs upward.  */
204
29
    while (parent_device != UX_NULL)
205
    {
206
207
        /* Is the device high speed?  */
208
24
        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
213
               mapping bit.  */
214
3
            port_map =  (ULONG)(1 << port_index);
215
216
            /* Parse all the TTs attached to the hub.
217
               Since we confirmed exist of TT in previous _check,
218
               just do while loop here.
219
             */
220
3
            tt_index = 0;
221
            while(1)
222
            {
223
                /* Check if this TT owns the port where the device is attached.  */
224
6
                if ((parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_port_mapping & port_map) != 0)
225
                {
226
227
                    /* We have found the port, check if the tt can give us the bandwidth
228
                       we want to claim.  */
229
3
                    parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_max_bandwidth -=  tt_bandwidth_claimed;
230
3
                    return;
231
                }
232
233
                /* Try next index.  */
234
3
                tt_index ++;
235
            }
236
        }
237
238
        /* We now remember where this hub is located on the parent.  */
239
21
        port_index =  parent_device -> ux_device_port_location - 1;
240
241
        /* We go up one level in the hub chain.  */
242
21
        parent_device =  parent_device -> ux_device_parent;
243
    }
244
245
    /* We get here when we have not found a 2.0 hub in the list and we got
246
       to the root port.  */
247
5
    return;
248
}
249
#endif /* #if UX_MAX_DEVICES > 1 */