summaryrefslogtreecommitdiff
path: root/common/core/src/ux_hcd_sim_host_initialize.c
blob: 09658190ffa58ab8c7e354fcb0cccac7d1540f18 (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
/***************************************************************************
 * 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 Simulator Controller Driver                                    */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_hcd_sim_host.h"
#include "ux_dcd_sim_slave.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_hcd_sim_host_initialize                         PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function initializes the simulated host controller             */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    HCD                                   Pointer to HCD                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_hcd_sim_host_periodic_tree_create Create periodic tree          */
/*    _ux_utility_memory_allocate           Allocate memory block         */
/*    _ux_utility_semaphore_put             Semaphore put                 */
/*    _ux_utility_timer_create              Create timer                  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Host Simulator Controller Driver                                    */
/*                                                                        */
/**************************************************************************/
UINT  _ux_hcd_sim_host_initialize(UX_HCD *hcd)
{

UX_SLAVE_DCD        *dcd;
UX_DCD_SIM_SLAVE    *dcd_sim_slave;
UX_HCD_SIM_HOST     *hcd_sim_host;
UINT                status = UX_ERROR;


    /* The controller initialized here is of host simulator type.  */
    hcd -> ux_hcd_controller_type =  UX_HCD_SIM_HOST_CONTROLLER;

    /* Allocate memory for this host simulator HCD instance.  */
    hcd_sim_host =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HCD_SIM_HOST));
    if (hcd_sim_host == UX_NULL)
        return(UX_MEMORY_INSUFFICIENT);

    /* Set the pointer to the host simulator HCD.  */
    hcd -> ux_hcd_controller_hardware =  (VOID *) hcd_sim_host;

    /* Set the generic HCD owner for the host simulator HCD.  */
    hcd_sim_host -> ux_hcd_sim_host_hcd_owner =  hcd;

    /* Initialize the function collector for this HCD.  */
    hcd -> ux_hcd_entry_function =  _ux_hcd_sim_host_entry;

#if UX_MAX_DEVICES > 1
    /* Initialize the max bandwidth for periodic endpoints. In simulation this is
       not very important.  */
    hcd -> ux_hcd_available_bandwidth =  UX_HCD_SIM_HOST_AVAILABLE_BANDWIDTH;
#endif

    /* Set the state of the controller to HALTED first.  */
    hcd -> ux_hcd_status =  UX_HCD_STATUS_HALTED;

    /* Allocate the list of EDs. All EDs are allocated on 16 byte memory boundary.  */
    if (_ux_system_host -> ux_system_host_max_ed != 0)
    {
        hcd_sim_host -> ux_hcd_sim_host_ed_list =  _ux_utility_memory_allocate(UX_ALIGN_16, UX_REGULAR_MEMORY, (ULONG)sizeof(UX_HCD_SIM_HOST_ED) * _ux_system_host -> ux_system_host_max_ed);
        if (hcd_sim_host -> ux_hcd_sim_host_ed_list == UX_NULL)
            status = UX_MEMORY_INSUFFICIENT;
        else
            status = UX_SUCCESS;
    }

    /* Allocate the list of TDs. All TDs are allocated on 32 byte memory boundary.  */
    if ((status == UX_SUCCESS) && (_ux_system_host -> ux_system_host_max_td != 0))
    {
        hcd_sim_host -> ux_hcd_sim_host_td_list =  _ux_utility_memory_allocate(UX_ALIGN_32, UX_REGULAR_MEMORY, (ULONG)sizeof(UX_HCD_SIM_HOST_TD) * _ux_system_host -> ux_system_host_max_td);
        if (hcd_sim_host -> ux_hcd_sim_host_td_list == UX_NULL)
            status = UX_MEMORY_INSUFFICIENT;
    }

    /* Allocate the list of isochronous TDs. All TDs are allocated on 32 byte memory boundary.  */
    if ((status == UX_SUCCESS) && (_ux_system_host -> ux_system_host_max_iso_td != 0))
    {
        hcd_sim_host -> ux_hcd_sim_host_iso_td_list =  _ux_utility_memory_allocate(UX_ALIGN_32, UX_REGULAR_MEMORY, (ULONG)sizeof(UX_HCD_SIM_HOST_ISO_TD) * _ux_system_host -> ux_system_host_max_iso_td);
        if (hcd_sim_host -> ux_hcd_sim_host_iso_td_list == UX_NULL)
            status = UX_MEMORY_INSUFFICIENT;
    }

    /* Initialize the periodic tree.  */
    if (status == UX_SUCCESS)
        status =  _ux_hcd_sim_host_periodic_tree_create(hcd_sim_host);

    /* Initialize the scheduler.  */
    if (status == UX_SUCCESS)
    {
        /* Set the host controller into the operational state.  */
        hcd -> ux_hcd_status =  UX_HCD_STATUS_OPERATIONAL;

        /* The asynchronous queues are empty for now.  */
        hcd_sim_host -> ux_hcd_sim_host_queue_empty =  UX_TRUE;

        /* The periodic scheduler is not active.  */
        hcd_sim_host -> ux_hcd_sim_host_periodic_scheduler_active =  0;

        /* We start a timer that will invoke the simulator every timer tick.  */
        status = _ux_host_timer_create(&hcd_sim_host -> ux_hcd_sim_host_timer, "USBX Simulation Timer",
                        _ux_hcd_sim_host_timer_function, (ULONG) (ALIGN_TYPE) hcd_sim_host, 1, 1, UX_AUTO_ACTIVATE);
    }

    UX_TIMER_EXTENSION_PTR_SET(&(hcd_sim_host -> ux_hcd_sim_host_timer), hcd_sim_host)

    /* Free up resources and return when there is error.  */
    if (status != UX_SUCCESS)
    {

        /* Set the host controller into the halt state.  */
        hcd -> ux_hcd_status =  UX_HCD_STATUS_HALTED;

        /* The last resource, timer is not created or created error,
         * no need to delete.  */

        if (hcd_sim_host -> ux_hcd_sim_host_iso_td_list)
            _ux_utility_memory_free(hcd_sim_host -> ux_hcd_sim_host_iso_td_list);
        if (hcd_sim_host -> ux_hcd_sim_host_td_list)
            _ux_utility_memory_free(hcd_sim_host -> ux_hcd_sim_host_td_list);
        if (hcd_sim_host -> ux_hcd_sim_host_ed_list)
            _ux_utility_memory_free(hcd_sim_host -> ux_hcd_sim_host_ed_list);
        _ux_utility_memory_free(hcd_sim_host);

        return(status);
    }

    /* Link the HCD to DCD driver.  */
    if (_ux_system_slave)
    {
        dcd = &_ux_system_slave -> ux_system_slave_dcd;
        if (dcd)
        {
            dcd_sim_slave = (UX_DCD_SIM_SLAVE *) dcd -> ux_slave_dcd_controller_hardware;
            if (dcd_sim_slave)
                dcd_sim_slave -> ux_dcd_sim_slave_hcd = (VOID *)hcd;
        }
    }

    /* Get the number of ports on the controller. The number of ports needs to be reflected both
       for the generic HCD container and the local sim_host container. In the simulator,
       the number of ports is hardwired to 1 only.  */
    hcd -> ux_hcd_nb_root_hubs =  1;
    hcd_sim_host -> ux_hcd_sim_host_nb_root_hubs =  1;
    hcd_sim_host -> ux_hcd_sim_host_port_status[0] = UX_PS_CCS | UX_PS_DS_FS;

    /* Something happened on this port. Signal it to the root hub thread.  */
    hcd -> ux_hcd_root_hub_signal[0] =  1;

    /* We need to simulate a Root HUB Status Change for the USB stack since the simulator
       has not root HUB per se.  */
    status = _ux_host_semaphore_put_rc(&_ux_system_host -> ux_system_host_enum_semaphore);
    if (status != UX_SUCCESS)

        /* Resources are still ready but
         * failed to simulate Root HUB change!  */
        return(UX_SEMAPHORE_ERROR);

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