summaryrefslogtreecommitdiff
path: root/common/core/src/ux_host_stack_rh_device_insertion.c
blob: d00e526334e1c16b7872154b7bdbf4bd4e70677d (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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   Host Stack                                                          */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

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


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_stack_rh_device_insertion                  PORTABLE C      */
/*                                                           6.1.11       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function handles a device insertion on a downstream port of    */
/*    the root hub pointed by HCD.                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    HCD                                   Pointer to HCD structure      */
/*    port_index                            Port index of insertion       */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_utility_delay_ms                  Thread sleep                  */
/*    _ux_host_stack_new_device_create      New device create             */
/*    _ux_host_stack_device_remove          Device remove                 */
/*    (hcd_entry_function)                  Entry function of HCD driver  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    USBX Components                                                     */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_stack_rh_device_insertion(UX_HCD *hcd, UINT port_index)
{
#if defined(UX_HOST_STANDALONE)
UX_DEVICE   *device;
UINT        status;


    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_RH_DEVICE_INSERTION, hcd, port_index, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)

    /* Anyway there is device connected.  */
    hcd -> ux_hcd_rh_device_connection |= (ULONG)(1 << port_index);

    /* Create a new device slot for enumeration.  */
    status =  _ux_host_stack_new_device_create(hcd, UX_NULL, port_index,
                                        UX_FULL_SPEED_DEVICE, UX_MAX_SELF_POWER,
                                        &device);

    /* Link the device in enumeration list.  */
    if (status == UX_SUCCESS)
    {

        /* Set enumeration flag to process enumeration sequence.  */
        device -> ux_device_flags |= UX_DEVICE_FLAG_ENUM;

        /* Done success.  */
        return(UX_SUCCESS);
    }

#else
UX_DEVICE   *device = UX_NULL;
UINT        index_loop;
UINT        device_speed;
ULONG       port_status;
UINT        status;

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_RH_DEVICE_INSERTION, hcd, port_index, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)

    /* Perform a PORT_ENABLE command.  */
    port_status =  hcd -> ux_hcd_entry_function(hcd, UX_HCD_ENABLE_PORT, (VOID *)((ALIGN_TYPE)port_index));

    /* Check return status.  */
    if (port_status == UX_PORT_INDEX_UNKNOWN)
        return(port_status);

    /* A debounce interval with a minimum duration of 100 ms on attach.  */
    _ux_utility_delay_ms(100);

    /* The first attempts to do a device enumeration may fail.
       Typically, after the port is reset and the first command is sent to
       the device, there is no answer. In this case, we reset the port again
       and retry. Usually that does the trick!  */
    for (index_loop = 0; index_loop < UX_RH_ENUMERATION_RETRY; index_loop++)
    {

        /* Now we have to do a PORT_RESET command.  */
        port_status =  hcd -> ux_hcd_entry_function(hcd, UX_HCD_RESET_PORT, (VOID *)((ALIGN_TYPE)port_index));
        if (port_status == UX_SUCCESS)
        {

            /* The port reset phase was successful. Before we invoke the device enumeration function,
               we need to know the speed of the device.  */
            port_status =  hcd -> ux_hcd_entry_function(hcd, UX_HCD_GET_PORT_STATUS, (VOID *)((ALIGN_TYPE)port_index));

            /* Check return status.  */
            if (port_status == UX_PORT_INDEX_UNKNOWN)
            {

                /* Error trap. */
                _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ROOT_HUB, UX_DEVICE_ENUMERATION_FAILURE);

                /* If trace is enabled, insert this event into the trace buffer.  */
                UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_ENUMERATION_FAILURE, port_index, 0, 0, UX_TRACE_ERRORS, 0, 0)

                return(UX_DEVICE_ENUMERATION_FAILURE);
            }

            /* Check if device is still connected.  */
            if ((port_status & UX_PS_CCS) == 0)
            {

                /* Device disconnected during enumeration retries.  */
                return(UX_DEVICE_ENUMERATION_FAILURE);
            }

            /* Set the device speed.  */
            device_speed =  port_status >> UX_PS_DS;

            /* Ask the USB stack to enumerate this device. A root hub is considered self
               powered. */
            status =  _ux_host_stack_new_device_create(hcd, UX_NULL, port_index, device_speed, UX_MAX_SELF_POWER, &device);

            /* Check return status.  */
            if (status == UX_SUCCESS)
            {

                /* Successful device create.  */

                /* The device has been mounted properly, we have to remember this
                   so when the device is removed, we have to invoke the enumeration
                   function again */
                hcd -> ux_hcd_rh_device_connection |= (ULONG)(1 << port_index);

                /* If the device instance is ready, notify application for connection.  */
                if (_ux_system_host -> ux_system_host_change_function)
                {
                    _ux_system_host -> ux_system_host_change_function(UX_DEVICE_CONNECTION, UX_NULL, (VOID*)device);
                }

                /* Return success to the caller.  */
                return(UX_SUCCESS);
            }
            else
            {

                /* Return error if HCD is dead.  */
                if (hcd -> ux_hcd_status != UX_HCD_STATUS_OPERATIONAL)
                    return(UX_CONTROLLER_DEAD);

                /* No retry if there are too many devices.  */
                if (status == UX_TOO_MANY_DEVICES)
                    break;

                /* No retry if there is no class found.  */
                if (status == UX_NO_CLASS_MATCH)
                    break;

                /* Simulate remove to free allocated resources if retry.  */
                if (index_loop < UX_RH_ENUMERATION_RETRY - 1)
                    _ux_host_stack_device_remove(hcd, UX_NULL, port_index);
            }
        }

        /* We get here if something did not go well. Either the port did not respond
           well to the ENABLE\RESET phases or the device did not enumerate well
           so we try again ! */
        _ux_utility_delay_ms(UX_RH_ENUMERATION_RETRY_DELAY);
    }
#endif /* defined(UX_HOST_STANDALONE)  */

    /* If we get here, the device did not enumerate completely.
       The device is still attached to the root hub and therefore
       there could be a physical connection with a unconfigured device.  */
    hcd -> ux_hcd_rh_device_connection |= (ULONG)(1 << port_index);

    /* Notify application for a physical connection failed to be enumed.
       Device instance NULL indicates too many devices.
       Device state unconfigured indicates enumeration fail.  */
    if (_ux_system_host -> ux_system_host_change_function)
    {
        _ux_system_host -> ux_system_host_change_function(UX_DEVICE_CONNECTION, UX_NULL, (VOID*)device);
    }

    /* Error trap. */
    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ROOT_HUB, UX_DEVICE_ENUMERATION_FAILURE);

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_ENUMERATION_FAILURE, port_index, 0, 0, UX_TRACE_ERRORS, 0, 0)

    /* Return a failed enumeration.  */
    return(UX_DEVICE_ENUMERATION_FAILURE);
}