summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_hub_ports_power.c
blob: 72b76d3ea46afedf7f4588000554df90990043d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   HUB Class                                                           */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_hub.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_hub_ports_power                      PORTABLE C      */
/*                                                           6.1          */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function will power up each downstream port attached to the    */
/*    HUB. There is a delay after powering up each port, otherwise we may */
/*    not detect device insertion.                                        */
/*                                                                        */
/*    There are 3 port power modes:                                       */
/*                                                                        */
/*      1) Gang power:          In this case we only power the first      */
/*                              port and all ports should be powered at   */
/*                              the same time                             */
/*                                                                        */
/*      2) Individual power:    In this case we power individually each   */
/*                              port                                      */
/*                                                                        */
/*      3) No power switching:  In this case the power is applied to the  */
/*                              downstream ports when the upstream port   */
/*                              receives power.                           */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    hub                                   Pointer to HUB class          */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_hub_feature            Set HUB class feature         */
/*    _ux_utility_delay_ms                  Thread sleep                  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    HUB Class                                                           */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_hub_ports_power(UX_HOST_CLASS_HUB *hub)
{

UINT        nb_ports;
UINT        port_index;
UINT        status;


    /* Check for the power management mode: no power switching.  */
    if(hub -> ux_host_class_hub_descriptor.wHubCharacteristics & UX_HOST_CLASS_HUB_NO_POWER_SWITCHING)
        return(UX_SUCCESS);

    /* All ports must be powered individually.  */
    nb_ports =  hub -> ux_host_class_hub_descriptor.bNbPorts;

    /* Perform the function to all ports: the port index starts from 1 as the port 0 is for the HUB.  */
    for (port_index = 1; port_index <= nb_ports; port_index++)
    {

        /* To apply port power, we send a SET_FEATURE to the port on the HUB.  */
        status =  _ux_host_class_hub_feature(hub, port_index, UX_SET_FEATURE, UX_HOST_CLASS_HUB_PORT_POWER);

        /* Check the function result and update HUB status if there was a problem.  */
        if (status != UX_SUCCESS)
        {

            /* Set the HUB status to not powered.  */
            hub -> ux_host_class_hub_port_power  &= (UINT)~(1 << port_index);

        }
        else
        {

            /* Now we need to wait for the power to be stable.  */
            _ux_utility_delay_ms(((ULONG) (hub -> ux_host_class_hub_descriptor.bPwrOn2PwrGood) * 2));

            /* Set the HUB status to powered.  */
            hub -> ux_host_class_hub_port_power  |= (UINT)(1 << port_index);
        }
    }

    /* Return successful completion.  */
    return(UX_SUCCESS);
}