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