1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
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 /*  RELEASE HISTORY                                                       */
80 /*                                                                        */
81 /*    DATE              NAME                      DESCRIPTION             */
82 /*                                                                        */
83 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
84 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
85 /*                                            resulting in version 6.1    */
86 /*                                                                        */
87 /**************************************************************************/
_ux_host_class_hub_ports_power(UX_HOST_CLASS_HUB * hub)88 UINT  _ux_host_class_hub_ports_power(UX_HOST_CLASS_HUB *hub)
89 {
90 
91 UINT        nb_ports;
92 UINT        port_index;
93 UINT        status;
94 
95 
96     /* Check for the power management mode: no power switching.  */
97     if(hub -> ux_host_class_hub_descriptor.wHubCharacteristics & UX_HOST_CLASS_HUB_NO_POWER_SWITCHING)
98         return(UX_SUCCESS);
99 
100     /* All ports must be powered individually.  */
101     nb_ports =  hub -> ux_host_class_hub_descriptor.bNbPorts;
102 
103     /* Perform the function to all ports: the port index starts from 1 as the port 0 is for the HUB.  */
104     for (port_index = 1; port_index <= nb_ports; port_index++)
105     {
106 
107         /* To apply port power, we send a SET_FEATURE to the port on the HUB.  */
108         status =  _ux_host_class_hub_feature(hub, port_index, UX_SET_FEATURE, UX_HOST_CLASS_HUB_PORT_POWER);
109 
110         /* Check the function result and update HUB status if there was a problem.  */
111         if (status != UX_SUCCESS)
112         {
113 
114             /* Set the HUB status to not powered.  */
115             hub -> ux_host_class_hub_port_power  &= (UINT)~(1 << port_index);
116 
117         }
118         else
119         {
120 
121             /* Now we need to wait for the power to be stable.  */
122             _ux_utility_delay_ms(((ULONG) (hub -> ux_host_class_hub_descriptor.bPwrOn2PwrGood) * 2));
123 
124             /* Set the HUB status to powered.  */
125             hub -> ux_host_class_hub_port_power  |= (UINT)(1 << port_index);
126         }
127     }
128 
129     /* Return successful completion.  */
130     return(UX_SUCCESS);
131 }
132 
133