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 /**   Host Stack                                                          */
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_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_host_stack_rh_change_process                    PORTABLE C      */
36 /*                                                           6.1.2        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function checks for changes in topology in each of the         */
44 /*    installed HCDs.                                                     */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    None                                                                */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    None                                                                */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_host_stack_rh_device_insertion    Process device insertion      */
57 /*    _ux_host_stack_rh_device_extraction   Process device extraction     */
58 /*    (ux_hcd_entry_function)               HCD entry function            */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    USBX Components                                                     */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            used new interrupt macros,  */
71 /*                                            resulting in version 6.1    */
72 /*  11-09-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            fixed registered HCD scan,  */
74 /*                                            resulting in version 6.1.2  */
75 /*                                                                        */
76 /**************************************************************************/
_ux_host_stack_rh_change_process(VOID)77 VOID  _ux_host_stack_rh_change_process(VOID)
78 {
79 
80 UX_HCD      *hcd;
81 UINT        hcd_index;
82 ULONG       port_status;
83 UINT        port_index;
84 UX_INTERRUPT_SAVE_AREA
85 
86     /* This thread was maybe awaken by one or more HCD controllers. Check each
87        of the HCD to see where there has been a change of topology.  */
88     for(hcd_index = 0; hcd_index < UX_SYSTEM_HOST_MAX_HCD_GET(); hcd_index++)
89     {
90 
91         /* Pickup HCD pointer.  */
92         hcd =  &_ux_system_host -> ux_system_host_hcd_array[hcd_index];
93 
94         /* Is this HCD operational?  */
95         if (hcd -> ux_hcd_status == UX_HCD_STATUS_OPERATIONAL)
96         {
97 
98             /* On each port of the root hub of the controller, get the port status */
99             for (port_index = 0; port_index < hcd -> ux_hcd_nb_root_hubs; port_index++)
100             {
101 
102                 /* Was there any activity on this port ? */
103                 if (hcd -> ux_hcd_root_hub_signal[port_index] != 0)
104                 {
105 
106                     /* If trace is enabled, insert this event into the trace buffer.  */
107                     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_RH_CHANGE_PROCESS, port_index, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
108 
109                     /* Reset that port activity signal.  */
110                     UX_DISABLE
111                     hcd -> ux_hcd_root_hub_signal[port_index]--;
112                     UX_RESTORE
113 
114                     /* Call HCD for port status.  */
115                     port_status =  hcd -> ux_hcd_entry_function(hcd, UX_HCD_GET_PORT_STATUS, (VOID *)((ALIGN_TYPE)port_index));
116 
117                     /* Check return status.  */
118                     if (port_status != UX_PORT_INDEX_UNKNOWN)
119                     {
120 
121                         /* The port_status value is valid and will tell us if there is
122                            a device attached\detached on the downstream port.  */
123                         if (port_status & UX_PS_CCS)
124                         {
125 
126                             /* There is a device attached to this port. Check if we know
127                                about it. If not, this is a device insertion signal.  */
128                             if ((hcd -> ux_hcd_rh_device_connection & (ULONG)(1 << port_index)) == 0)
129                             {
130 
131                                 /* We have a simple device insertion, we have not lost any signals.
132                                    the root hub and the stack enumeration module are in synch.  */
133                                 _ux_host_stack_rh_device_insertion(hcd,port_index);
134                             }
135                             else
136                             {
137                                 /* We can get here when there has been multiple events on the hardware root hub
138                                    but we may have missed them of they were too close or the stack got too busy.
139                                    We check the number of events in the root hub signal. If it is not zero
140                                    we are out of synch, meaning we got a disconnection followed very quickly
141                                    by a insertion.  */
142 
143                                 if (hcd -> ux_hcd_root_hub_signal[port_index] != 0)
144                                 {
145 
146                                     /* We need to get back in synch now.  */
147                                     UX_DISABLE
148                                     hcd -> ux_hcd_root_hub_signal[port_index] = 0;
149                                     UX_RESTORE
150 
151                                     /* First extract the device.  */
152                                     _ux_host_stack_rh_device_extraction(hcd,port_index);
153 
154                                     /* Now, insert it again.  */
155                                     _ux_host_stack_rh_device_insertion(hcd,port_index);
156 
157 
158                                 }
159 
160                             }
161                         }
162                         else
163                         {
164 
165                             /* There is no device attached to this port. Check if we know
166                                about it. If not, this is a device removal signal.  */
167                             if ((hcd -> ux_hcd_rh_device_connection & (ULONG)(1 << port_index)) !=0)
168                             {
169                                 _ux_host_stack_rh_device_extraction(hcd,port_index);
170                             }
171                         }
172                     }
173                 }
174             }
175         }
176     }
177 }
178 
179