GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_host_stack_device_configuration_deactivate.c Lines: 21 25 84.0 %
Date: 2026-03-06 18:57:10 Branches: 10 12 83.3 %

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_deactivate      PORTABLE C      */
37
/*                                                           6.1.12       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function deactivate current configuration for a device.        */
45
/*    When this configuration is deactivated, all the device interfaces   */
46
/*    are deactivated on the device.                                      */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    Completion Status                                                   */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _ux_utility_semaphore_get             Get semaphore                 */
58
/*    _ux_utility_semaphore_put             Put semaphore                 */
59
/*    (ux_host_class_entry_function)        Class entry function          */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    Application                                                         */
64
/*    USBX Components                                                     */
65
/*                                                                        */
66
/**************************************************************************/
67
6
UINT  _ux_host_stack_device_configuration_deactivate(UX_DEVICE *device)
68
{
69
70
#if defined(UX_HOST_STANDALONE)
71
UX_INTERRUPT_SAVE_AREA
72
#endif
73
UX_HOST_CLASS_COMMAND       command;
74
UX_CONFIGURATION            *configuration;
75
UX_INTERFACE                *interface_ptr;
76
UINT                        status;
77
78
79
    /* Do a sanity check on the device handle.  */
80
6
    if (device -> ux_device_handle != (ULONG) (ALIGN_TYPE) device)
81
    {
82
83
        /* Error trap. */
84
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_DEVICE_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_DEVICE_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
88
89
1
        return(UX_DEVICE_HANDLE_UNKNOWN);
90
    }
91
92
    /* If trace is enabled, insert this event into the trace buffer.  */
93
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_CONFIGURATION_DEACTIVATE, device, device -> ux_device_current_configuration, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
94
95
#if defined(UX_HOST_STANDALONE)
96
97
    /* Check device lock.  */
98
    UX_DISABLE
99
    if (device -> ux_device_flags & UX_DEVICE_FLAG_LOCK)
100
    {
101
        UX_RESTORE
102
        return(UX_BUSY);
103
    }
104
    device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
105
    UX_RESTORE
106
#else
107
108
    /* Protect the control endpoint semaphore here.  It will be unprotected in the
109
       transfer request function.  */
110
5
    status =  _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
111
112
    /* Check for status.  */
113
5
    if (status != UX_SUCCESS)
114
    {
115
116
        /* Error trap. */
117
1
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_SEMAPHORE_ERROR);
118
119
        /* If trace is enabled, insert this event into the trace buffer.  */
120
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_SEMAPHORE_ERROR, device -> ux_device_current_configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
121
122
1
        return(UX_SEMAPHORE_ERROR);
123
    }
124
#endif
125
126
    /* Check for the state of the device, if not configured, we are done.  */
127
4
    if (device -> ux_device_state != UX_DEVICE_CONFIGURED)
128
    {
129
#if defined(UX_HOST_STANDALONE)
130
        device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
131
#else
132
1
        _ux_host_semaphore_put(&device -> ux_device_protection_semaphore);
133
#endif
134
1
        return(UX_SUCCESS);
135
    }
136
137
    /* Deactivate classes by command.  */
138
3
    command.ux_host_class_command_request =  UX_HOST_CLASS_COMMAND_DEACTIVATE;
139
140
    /* Search for the active configuration.  */
141
3
    configuration =  device -> ux_device_current_configuration;
142
143
    /* If device configured configuration must be activated.  */
144
145
    /* We have the correct configuration, search the interface(s).  */
146
3
    interface_ptr =  configuration -> ux_configuration_first_interface;
147
148
    /* Loop to perform the search.  */
149
9
    while (interface_ptr != UX_NULL)
150
    {
151
152
        /* Check if an instance of the interface is present.  */
153
6
        if (interface_ptr -> ux_interface_class_instance != UX_NULL)
154
        {
155
156
            /* We need to stop the class instance for the device.  */
157
3
            command.ux_host_class_command_instance =  interface_ptr -> ux_interface_class_instance;
158
159
            /* Call the class.  */
160
3
            interface_ptr -> ux_interface_class -> ux_host_class_entry_function(&command);
161
        }
162
163
        /* Move to next interface.  */
164
6
        interface_ptr =  interface_ptr -> ux_interface_next_interface;
165
    }
166
167
    /* The device can now be un-configured.  */
168
3
    status =  _ux_host_stack_device_configuration_reset(device);
169
170
#if defined(UX_HOST_STANDALONE)
171
    device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
172
#endif
173
174
    /* Return completion status.  */
175
3
    return(status);
176
}
177
178
179
/**************************************************************************/
180
/*                                                                        */
181
/*  FUNCTION                                               RELEASE        */
182
/*                                                                        */
183
/*    _uxe_host_stack_device_configuration_deactivate     PORTABLE C      */
184
/*                                                           6.3.0        */
185
/*  AUTHOR                                                                */
186
/*                                                                        */
187
/*    Chaoqiong Xiao, Microsoft Corporation                               */
188
/*                                                                        */
189
/*  DESCRIPTION                                                           */
190
/*                                                                        */
191
/*    This function checks errors in host stack configuration deactivate  */
192
/*    function call.                                                      */
193
/*                                                                        */
194
/*  INPUT                                                                 */
195
/*                                                                        */
196
/*    device                                Pointer to device             */
197
/*                                                                        */
198
/*  OUTPUT                                                                */
199
/*                                                                        */
200
/*    None                                                                */
201
/*                                                                        */
202
/*  CALLS                                                                 */
203
/*                                                                        */
204
/*    _ux_host_stack_device_configuration_deactivate                      */
205
/*                                          Host stack config deactivate  */
206
/*                                                                        */
207
/*  CALLED BY                                                             */
208
/*                                                                        */
209
/*    Application                                                         */
210
/*                                                                        */
211
/**************************************************************************/
212
UINT  _uxe_host_stack_device_configuration_deactivate(UX_DEVICE *device)
213
{
214
215
    /* Sanity check.  */
216
    if (device == UX_NULL)
217
        return(UX_INVALID_PARAMETER);
218
219
    /* Invoke configuration deactivate function.  */
220
    return(_ux_host_stack_device_configuration_deactivate(device));
221
}