GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_bandwidth_release.c Lines: 40 40 100.0 %
Date: 2026-03-06 18:57:10 Branches: 24 24 100.0 %

Line Branch Exec Source
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
/**   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
/**************************************************************************/
73
1139
VOID  _ux_host_stack_bandwidth_release(UX_HCD *hcd, UX_ENDPOINT *endpoint)
74
{
75
76
UX_DEVICE       *device;
77
UX_DEVICE       *parent_device;
78
USHORT          hcd_bandwidth_claimed;
79
USHORT          max_packet_size;
80
LONG            packet_size;
81
1139
USHORT          tt_bandwidth_claimed =  0;
82
ULONG           port_index;
83
ULONG           port_map;
84
ULONG           tt_index;
85
1139
const UCHAR     overheads[4][3] = {
86
/*   LS  FS   HS   */
87
    {63, 45, 173}, /* Control */
88
    { 0,  9,  38}, /* Isochronous */
89
    { 0, 13,  55}, /* Bulk */
90
    {19, 13,  55}  /* Interrupt */
91
};
92
93
    /* Get the pointer to the device.  */
94
1139
    device =  endpoint -> ux_endpoint_device;
95
96
    /* Calculate the bandwidth. From USB spec.
97
     *
98
     * The frame unit consumed per byte is like follow:
99
     *              Bytes/FrameUnit     FrameUnit/byte  FrameUnit/byte
100
     *              (Overhead included) (HS baseline)   (FS baseline)
101
     * Low Speed       187.5                40             8
102
     * Full Speed     1500                   5             1
103
     * High Speed     7500                   1            1/5
104
     *
105
     * The overhead is like follow:
106
     *               Control Isochronous Bulk Interrupt
107
     * bmAttribute     (0)       (1)     (2)     (3)
108
     * Low Speed        63       --      --      19
109
     * Full Speed       45        9      13      13
110
     * High Speed      173       38      55      55
111
     *
112
     * Worst case bit stuffing is calculated as 1.1667 (7/6) times the raw time.
113
     */
114
115
    /* Get maximum packet size.  */
116
1139
    max_packet_size  = endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_PACKET_SIZE_MASK;
117
118
    /* Rough time for possible Bit Stuffing.  */
119
1139
    packet_size = (max_packet_size * 7 + 5) / 6;
120
121
    /* Add overhead.  */
122
1139
    packet_size += overheads[endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE][device -> ux_device_speed];
123
1139
    max_packet_size = (USHORT)packet_size;
124
125
    /* Check for high-speed endpoint.  */
126
1139
    if (device -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
127
    {
128
129
        /* Get number of transactions.  */
130
119
        max_packet_size = (USHORT)(max_packet_size *
131
119
                    (((endpoint -> ux_endpoint_descriptor.wMaxPacketSize & UX_MAX_NUMBER_OF_TRANSACTIONS_MASK) >>
132
119
                        UX_MAX_NUMBER_OF_TRANSACTIONS_SHIFT) + 1));
133
    }
134
135
    /* Calculate the bandwidth claimed by this endpoint for the main bus.  */
136
1139
    if (hcd -> ux_hcd_version != 0x200)
137
    {
138
139
1109
        if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE)
140
            /* Low speed transfer takes 40x more units than high speed. */
141
6
            hcd_bandwidth_claimed =  (USHORT)(max_packet_size * 8 * 5);
142
        else
143
        {
144
145
1103
            if (device -> ux_device_speed == UX_FULL_SPEED_DEVICE)
146
                /* Full speed transfer takes 5x more units than high speed. */
147
990
                hcd_bandwidth_claimed =  (USHORT)(max_packet_size * 5);
148
            else
149
                /* Use high speed timing as base for bus bandwidth calculation. */
150
113
                hcd_bandwidth_claimed =  (USHORT)max_packet_size;
151
        }
152
    }
153
    else
154
    {
155
156
30
        hcd_bandwidth_claimed =  (USHORT)max_packet_size;
157
30
        if (device -> ux_device_speed == UX_LOW_SPEED_DEVICE)
158
            /* Low speed transfer takes 8x more units than full speed. */
159
6
            tt_bandwidth_claimed =  (USHORT)(max_packet_size * 8);
160
        else
161
            /* Use full speed timing as base for TT bandwidth calculation. */
162
24
            tt_bandwidth_claimed =  (USHORT)max_packet_size;
163
    }
164
165
    /* Free the HCD bandwidth.  */
166
1139
    hcd -> ux_hcd_available_bandwidth +=  hcd_bandwidth_claimed;
167
168
    /* We need to take care of the case where the endpoint belongs to a USB 1.1
169
       device that sits behind a 2.0 hub. We ignore cases where the device
170
       is either high speed or the bus is 1.1.  */
171

1139
    if ((device -> ux_device_speed == UX_HIGH_SPEED_DEVICE) || (hcd -> ux_hcd_version != 0x200))
172
    {
173
174
        /* The device is high speed, therefore no need for TT.  */
175
1115
        return;
176
    }
177
178
    /* We have a 1.1 device, check if the parent is a 2.0 hub.  */
179
24
    parent_device =  device -> ux_device_parent;
180
24
    if (parent_device == UX_NULL)
181
    {
182
183
        /* We are at the root, must be a 1.1 controller then! */
184
13
        return;
185
    }
186
187
    /* We get here when the parent is a hub. The problem occurs when the hub is itself
188
       connected to a chain of hubs. We need to find the first 2.0 hub parent to this chain
189
       to check the TT. We need to remember the port on which the first 1.1 device is
190
       hooked to.  */
191
11
    port_index =  device -> ux_device_port_location - 1;
192
193
    /* Scan the chain of hubs upward.  */
194
34
    while (parent_device != UX_NULL)
195
    {
196
197
        /* Check for a high speed device.  */
198
33
        if (parent_device -> ux_device_speed == UX_HIGH_SPEED_DEVICE)
199
        {
200
201
            /* The device is a high speed hub, find the TT that manages the port.
202
               The first 1.1 device is connected to. First we calculate the port mapping bit.  */
203
10
            port_map =  (ULONG)(1 << port_index);
204
205
            /* Parse all the TTs attached to the hub.  */
206
55
            for (tt_index = 0; tt_index < UX_MAX_TT; tt_index++)
207
            {
208
209
                /* Check if this TT owns the port where the device is attached.  */
210
50
                if ((parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_port_mapping & port_map) != 0)
211
                {
212
213
                    /* We have found the port, check if the tt can give us the bandwidth
214
                       we want to claim.  */
215
5
                    parent_device -> ux_device_hub_tt[tt_index].ux_hub_tt_max_bandwidth +=  tt_bandwidth_claimed;
216
5
                    return;
217
                }
218
            }
219
220
            /* We should never get here!!!!! */
221
5
            return;
222
        }
223
224
        /* We now remember where this hub is located on the parent.  */
225
23
        port_index =  parent_device -> ux_device_port_location - 1;
226
227
        /* We go up one level in the hub chain.  */
228
23
        parent_device =  parent_device -> ux_device_parent;
229
    }
230
231
    /* We get here when we have not found a 2.0 hub in the list and we got
232
       to the root port.  */
233
1
    return;
234
}
235
#endif /* #if UX_MAX_DEVICES > 1 */