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

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