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