GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hub_ports_power.c Lines: 11 11 100.0 %
Date: 2026-03-06 18:57:10 Branches: 6 6 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
/**   HUB Class                                                           */
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_class_hub.h"
30
#include "ux_host_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_host_class_hub_ports_power                      PORTABLE C      */
38
/*                                                           6.1          */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function will power up each downstream port attached to the    */
46
/*    HUB. There is a delay after powering up each port, otherwise we may */
47
/*    not detect device insertion.                                        */
48
/*                                                                        */
49
/*    There are 3 port power modes:                                       */
50
/*                                                                        */
51
/*      1) Gang power:          In this case we only power the first      */
52
/*                              port and all ports should be powered at   */
53
/*                              the same time                             */
54
/*                                                                        */
55
/*      2) Individual power:    In this case we power individually each   */
56
/*                              port                                      */
57
/*                                                                        */
58
/*      3) No power switching:  In this case the power is applied to the  */
59
/*                              downstream ports when the upstream port   */
60
/*                              receives power.                           */
61
/*                                                                        */
62
/*  INPUT                                                                 */
63
/*                                                                        */
64
/*    hub                                   Pointer to HUB class          */
65
/*                                                                        */
66
/*  OUTPUT                                                                */
67
/*                                                                        */
68
/*    Completion Status                                                   */
69
/*                                                                        */
70
/*  CALLS                                                                 */
71
/*                                                                        */
72
/*    _ux_host_class_hub_feature            Set HUB class feature         */
73
/*    _ux_utility_delay_ms                  Thread sleep                  */
74
/*                                                                        */
75
/*  CALLED BY                                                             */
76
/*                                                                        */
77
/*    HUB Class                                                           */
78
/*                                                                        */
79
/**************************************************************************/
80
48
UINT  _ux_host_class_hub_ports_power(UX_HOST_CLASS_HUB *hub)
81
{
82
83
UINT        nb_ports;
84
UINT        port_index;
85
UINT        status;
86
87
88
    /* Check for the power management mode: no power switching.  */
89
48
    if(hub -> ux_host_class_hub_descriptor.wHubCharacteristics & UX_HOST_CLASS_HUB_NO_POWER_SWITCHING)
90
1
        return(UX_SUCCESS);
91
92
    /* All ports must be powered individually.  */
93
47
    nb_ports =  hub -> ux_host_class_hub_descriptor.bNbPorts;
94
95
    /* Perform the function to all ports: the port index starts from 1 as the port 0 is for the HUB.  */
96
141
    for (port_index = 1; port_index <= nb_ports; port_index++)
97
    {
98
99
        /* To apply port power, we send a SET_FEATURE to the port on the HUB.  */
100
94
        status =  _ux_host_class_hub_feature(hub, port_index, UX_SET_FEATURE, UX_HOST_CLASS_HUB_PORT_POWER);
101
102
        /* Check the function result and update HUB status if there was a problem.  */
103
94
        if (status != UX_SUCCESS)
104
        {
105
106
            /* Set the HUB status to not powered.  */
107
3
            hub -> ux_host_class_hub_port_power  &= (UINT)~(1 << port_index);
108
109
        }
110
        else
111
        {
112
113
            /* Now we need to wait for the power to be stable.  */
114
91
            _ux_utility_delay_ms(((ULONG) (hub -> ux_host_class_hub_descriptor.bPwrOn2PwrGood) * 2));
115
116
            /* Set the HUB status to powered.  */
117
91
            hub -> ux_host_class_hub_port_power  |= (UINT)(1 << port_index);
118
        }
119
    }
120
121
    /* Return successful completion.  */
122
47
    return(UX_SUCCESS);
123
}
124