GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_device_configuration_activate.c Lines: 16 20 80.0 %
Date: 2026-03-06 18:57:10 Branches: 8 10 80.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_device_configuration_activate        PORTABLE C      */
37
/*                                                           6.1.10       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function selects a specific configuration for a device.        */
45
/*    When this configuration is set to the device, by default all the    */
46
/*    device interface and their associated alternate setting 0 is        */
47
/*    activated on the device.                                            */
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    configuration                          Pointer to configuration     */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _ux_utility_semaphore_get                    Get semaphore          */
60
/*    _ux_utility_semaphore_put                    Put semaphore          */
61
/*    _ux_host_stack_configuration_interface_scan  Scan and activate      */
62
/*                                                 interfaces             */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    Application                                                         */
67
/*    USBX Components                                                     */
68
/*                                                                        */
69
/**************************************************************************/
70
7
UINT  _ux_host_stack_device_configuration_activate(UX_CONFIGURATION *configuration)
71
{
72
#if defined(UX_HOST_STANDALONE)
73
UX_INTERRUPT_SAVE_AREA
74
#endif
75
UX_DEVICE               *device;
76
UINT                    status;
77
78
79
    /* Check for validity of the configuration handle.  */
80
7
    if (configuration -> ux_configuration_handle != (ULONG) (ALIGN_TYPE) configuration)
81
    {
82
83
        /* Error trap. */
84
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_CONFIGURATION_HANDLE_UNKNOWN);
85
86
        /* If trace is enabled, insert this event into the trace buffer.  */
87
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
88
89
1
        return(UX_CONFIGURATION_HANDLE_UNKNOWN);
90
    }
91
92
    /* Get the device container for this configuration.  */
93
6
    device =  configuration -> ux_configuration_device;
94
95
    /* If trace is enabled, insert this event into the trace buffer.  */
96
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_CONFIGURATION_ACTIVATE, device, configuration, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
97
98
#if defined(UX_HOST_STANDALONE)
99
100
    /* Check device lock.  */
101
    UX_DISABLE
102
    if (device -> ux_device_flags & UX_DEVICE_FLAG_LOCK)
103
    {
104
        UX_RESTORE
105
        return(UX_BUSY);
106
    }
107
    device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
108
    UX_RESTORE
109
#else
110
111
    /* Protect the control endpoint semaphore here.  It will be unprotected in the
112
       transfer request function.  */
113
6
    status =  _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
114
115
    /* Check for status.  */
116
6
    if (status != UX_SUCCESS)
117
    {
118
119
        /* Error trap. */
120
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_SEMAPHORE_ERROR);
121
122
        /* If trace is enabled, insert this event into the trace buffer.  */
123
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_SEMAPHORE_ERROR, configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
124
125
1
        return(UX_SEMAPHORE_ERROR);
126
    }
127
#endif
128
129
    /* Check for the state of the device . If the device is already configured,
130
       we need to cancel the existing configuration before enabling this one.   */
131
5
    if (device -> ux_device_state == UX_DEVICE_CONFIGURED)
132
    {
133
134
        /* If this configuration is already activated, we are good,
135
           otherwise report error.  */
136
4
        status = (device -> ux_device_current_configuration == configuration) ?
137
2
                    UX_SUCCESS : UX_ALREADY_ACTIVATED;
138
#if defined(UX_HOST_STANDALONE)
139
        device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
140
#else
141
2
        _ux_host_semaphore_put(&device -> ux_device_protection_semaphore);
142
#endif
143
2
        return(status);
144
    }
145
146
    /* Scan and activate the interfaces.  */
147
3
    status =  _ux_host_stack_configuration_interface_scan(configuration);
148
149
#if defined(UX_HOST_STANDALONE)
150
151
    if (status == UX_SUCCESS)
152
    {
153
154
        /* Place device enum state: LOCK -> SET_CONFIGURE.  */
155
        device -> ux_device_enum_trans =
156
            &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
157
        device -> ux_device_enum_state = UX_HOST_STACK_ENUM_TRANS_LOCK_WAIT;
158
        device -> ux_device_enum_inst.configuration = configuration;
159
        device -> ux_device_enum_next_state = UX_HOST_STACK_ENUM_CONFIG_SET;
160
161
        /* Set enumeration flag to process enumeration sequence.  */
162
        device -> ux_device_flags |= UX_DEVICE_FLAG_ENUM;
163
164
        /* Wait until enumeration done and device removed.  */
165
        while(device -> ux_device_enum_state != UX_STATE_IDLE)
166
        {
167
            _ux_system_host_tasks_run();
168
        }
169
    }
170
#endif
171
172
    /* Return completion status.  */
173
3
    return(status);
174
}
175
176
177
/**************************************************************************/
178
/*                                                                        */
179
/*  FUNCTION                                               RELEASE        */
180
/*                                                                        */
181
/*    _uxe_host_stack_device_configuration_activate       PORTABLE C      */
182
/*                                                           6.3.0        */
183
/*  AUTHOR                                                                */
184
/*                                                                        */
185
/*    Chaoqiong Xiao, Microsoft Corporation                               */
186
/*                                                                        */
187
/*  DESCRIPTION                                                           */
188
/*                                                                        */
189
/*    This function checks errors in host stack configuration activate    */
190
/*    function call.                                                      */
191
/*                                                                        */
192
/*  INPUT                                                                 */
193
/*                                                                        */
194
/*    configuration                         Pointer to configuration      */
195
/*                                                                        */
196
/*  OUTPUT                                                                */
197
/*                                                                        */
198
/*    None                                                                */
199
/*                                                                        */
200
/*  CALLS                                                                 */
201
/*                                                                        */
202
/*    _ux_host_stack_device_configuration_activate                        */
203
/*                                          Host stack config activate    */
204
/*                                                                        */
205
/*  CALLED BY                                                             */
206
/*                                                                        */
207
/*    Application                                                         */
208
/*                                                                        */
209
/**************************************************************************/
210
UINT  _uxe_host_stack_device_configuration_activate(UX_CONFIGURATION *configuration)
211
{
212
213
    /* Sanity check.  */
214
    if (configuration == UX_NULL)
215
        return(UX_INVALID_PARAMETER);
216
217
    /* Invoke configuration activate function.  */
218
    return(_ux_host_stack_device_configuration_activate(configuration));
219
}