GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_hub_activate.c Lines: 23 23 100.0 %
Date: 2026-03-06 18:57:10 Branches: 10 10 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
/**   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_activate                         PORTABLE C      */
38
/*                                                           6.1.12       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function performs the enumeration of the HUB. The HUB          */
46
/*    descriptor is read, the interrupt endpoint activated, power is set  */
47
/*    to the downstream ports and the HUB instance will be awaken when    */
48
/*    there is a status change on the HUB or one of the ports.            */
49
/*                                                                        */
50
/*  INPUT                                                                 */
51
/*                                                                        */
52
/*    command                               Pointer to command            */
53
/*                                                                        */
54
/*  OUTPUT                                                                */
55
/*                                                                        */
56
/*    Completion Status                                                   */
57
/*                                                                        */
58
/*  CALLS                                                                 */
59
/*                                                                        */
60
/*    _ux_host_class_hub_configure          Configure HUB                 */
61
/*    _ux_host_class_hub_descriptor_get     Get descriptor                */
62
/*    _ux_host_class_hub_interrupt_endpoint_start                         */
63
/*                                          Start interrupt endpoint      */
64
/*    _ux_host_class_hub_ports_power        Power ports                   */
65
/*    _ux_host_stack_class_instance_create  Create class instance         */
66
/*    _ux_host_stack_class_instance_destroy Destroy class instance        */
67
/*    _ux_utility_memory_allocate           Allocate memory block         */
68
/*    _ux_utility_memory_free               Free memory block             */
69
/*    _ux_host_semaphore_create             Create semaphore              */
70
/*                                                                        */
71
/*  CALLED BY                                                             */
72
/*                                                                        */
73
/*    HUB Class                                                           */
74
/*                                                                        */
75
/**************************************************************************/
76
70
UINT  _ux_host_class_hub_activate(UX_HOST_CLASS_COMMAND *command)
77
{
78
79
UX_DEVICE           *device;
80
UX_HOST_CLASS_HUB   *hub;
81
UINT                status;
82
83
84
#if UX_MAX_DEVICES > 1
85
    /* We need to make sure that the enumeration thread knows about at least
86
       one active HUB instance and the function to call when the thread
87
       is awaken.  */
88
70
    _ux_system_host -> ux_system_host_enum_hub_function =  _ux_host_class_hub_change_detect;
89
#endif
90
91
    /* The HUB is always activated by the device descriptor and not the
92
       instance descriptor.  */
93
70
    device =  (UX_DEVICE *) command -> ux_host_class_command_container;
94
95
    /* Instantiate this HUB class.  */
96
70
    hub =  (UX_HOST_CLASS_HUB *) _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_HUB));
97
70
    if (hub == UX_NULL)
98
1
        return(UX_MEMORY_INSUFFICIENT);
99
100
    /* Store the class container into this instance.  */
101
69
    hub -> ux_host_class_hub_class =  command -> ux_host_class_command_class_ptr;
102
103
    /* Store the device container instance in the HUB instance, this is for
104
       the class instance when it needs to talk to the USBX stack.  */
105
69
    hub -> ux_host_class_hub_device =  device;
106
107
#if defined(UX_HOST_STANDALONE)
108
109
    /* Store the instance in the device container, this is for the USBX stack
110
        when it needs to invoke the class.  */
111
    device -> ux_device_class_instance =  (VOID *) hub;
112
113
    /* Store the hub interface.  */
114
    hub -> ux_host_class_hub_interface = device ->
115
            ux_device_first_configuration -> ux_configuration_first_interface;
116
117
    /* Store the class task function.  */
118
    hub -> ux_host_class_hub_class -> ux_host_class_task_function = _ux_host_class_hub_tasks_run;
119
120
    /* During activation and tasks, control transfer is used for requests.  */
121
    hub -> ux_host_class_hub_transfer = &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
122
123
    /* The HUB is configured and activated in ACTIVATE_WAIT.  */
124
    hub -> ux_host_class_hub_run_status = UX_SUCCESS;
125
    hub -> ux_host_class_hub_enum_state = UX_HOST_CLASS_HUB_ENUM_GET_STATUS;
126
    status = UX_SUCCESS;
127
#else
128
129
    /* Configure the HUB.  */
130
69
    status =  _ux_host_class_hub_configure(hub);
131
69
    if (status == UX_SUCCESS)
132
    {
133
134
        /* Get the HUB descriptor.  */
135
64
        status =  _ux_host_class_hub_descriptor_get(hub);
136
64
        if (status == UX_SUCCESS)
137
        {
138
139
            /* Power up the HUB downstream ports. This function always returns
140
               success since we may be dealing with multiple ports.  */
141
48
            _ux_host_class_hub_ports_power(hub);
142
143
            /* Search the HUB interrupt endpoint and start it.  */
144
48
            status =  _ux_host_class_hub_interrupt_endpoint_start(hub);
145
48
            if (status == UX_SUCCESS)
146
            {
147
148
                /* Create this class instance.  */
149
36
                _ux_host_stack_class_instance_create(hub -> ux_host_class_hub_class, (VOID *) hub);
150
151
                /* Store the instance in the device container, this is for the USBX stack
152
                   when it needs to invoke the class.  */
153
36
                device -> ux_device_class_instance =  (VOID *) hub;
154
155
                /* Mark the HUB as live now.  */
156
36
                hub -> ux_host_class_hub_state =  UX_HOST_CLASS_INSTANCE_LIVE;
157
158
                /* If all is fine and the device is mounted, we may need to inform the application
159
                   if a function has been programmed in the system structure.  */
160
36
                if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
161
                {
162
163
                    /* Call system change function.  */
164
35
                    _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, hub -> ux_host_class_hub_class, (VOID *) hub);
165
                }
166
167
                /* Return success.  */
168
36
                return(UX_SUCCESS);
169
            }
170
        }
171
    }
172
173
    /* We get here when an error occurred.  */
174
175
    /* Free the hub instance.  */
176
33
    _ux_utility_memory_free(hub);
177
178
    /* If trace is enabled, insert this event into the trace buffer.  */
179
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HUB_ACTIVATE, hub, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
180
181
    /* If trace is enabled, register this object.  */
182
    UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, hub, 0, 0, 0)
183
184
#endif
185
186
    /* Return completion status.  */
187
33
    return(status);
188
}