1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
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 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  02-02-2021     Chaoqiong Xiao           Initial Version 6.1.4         */
71 /*  06-02-2021     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            fixed trace enabled error,  */
73 /*                                            resulting in version 6.1.7  */
74 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            added standalone support,   */
76 /*                                            resulting in version 6.1.10 */
77 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            internal clean up,          */
79 /*                                            resulting in version 6.1.11 */
80 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            fixed parameter/variable    */
82 /*                                            names conflict C++ keyword, */
83 /*                                            resulting in version 6.1.12 */
84 /*                                                                        */
85 /**************************************************************************/
_ux_host_stack_device_configuration_deactivate(UX_DEVICE * device)86 UINT  _ux_host_stack_device_configuration_deactivate(UX_DEVICE *device)
87 {
88 
89 #if defined(UX_HOST_STANDALONE)
90 UX_INTERRUPT_SAVE_AREA
91 #endif
92 UX_HOST_CLASS_COMMAND       command;
93 UX_CONFIGURATION            *configuration;
94 UX_INTERFACE                *interface_ptr;
95 UINT                        status;
96 
97 
98     /* Do a sanity check on the device handle.  */
99     if (device -> ux_device_handle != (ULONG) (ALIGN_TYPE) device)
100     {
101 
102         /* Error trap. */
103         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_DEVICE_HANDLE_UNKNOWN);
104 
105         /* If trace is enabled, insert this event into the trace buffer.  */
106         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
107 
108         return(UX_DEVICE_HANDLE_UNKNOWN);
109     }
110 
111     /* If trace is enabled, insert this event into the trace buffer.  */
112     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)
113 
114 #if defined(UX_HOST_STANDALONE)
115 
116     /* Check device lock.  */
117     UX_DISABLE
118     if (device -> ux_device_flags & UX_DEVICE_FLAG_LOCK)
119     {
120         UX_RESTORE
121         return(UX_BUSY);
122     }
123     device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
124     UX_RESTORE
125 #else
126 
127     /* Protect the control endpoint semaphore here.  It will be unprotected in the
128        transfer request function.  */
129     status =  _ux_host_semaphore_get(&device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
130 
131     /* Check for status.  */
132     if (status != UX_SUCCESS)
133     {
134 
135         /* Error trap. */
136         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_SEMAPHORE_ERROR);
137 
138         /* If trace is enabled, insert this event into the trace buffer.  */
139         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_SEMAPHORE_ERROR, device -> ux_device_current_configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
140 
141         return(UX_SEMAPHORE_ERROR);
142     }
143 #endif
144 
145     /* Check for the state of the device, if not configured, we are done.  */
146     if (device -> ux_device_state != UX_DEVICE_CONFIGURED)
147     {
148 #if defined(UX_HOST_STANDALONE)
149         device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
150 #else
151         _ux_host_semaphore_put(&device -> ux_device_protection_semaphore);
152 #endif
153         return(UX_SUCCESS);
154     }
155 
156     /* Deactivate classes by command.  */
157     command.ux_host_class_command_request =  UX_HOST_CLASS_COMMAND_DEACTIVATE;
158 
159     /* Search for the active configuration.  */
160     configuration =  device -> ux_device_current_configuration;
161 
162     /* If device configured configuration must be activated.  */
163 
164     /* We have the correct configuration, search the interface(s).  */
165     interface_ptr =  configuration -> ux_configuration_first_interface;
166 
167     /* Loop to perform the search.  */
168     while (interface_ptr != UX_NULL)
169     {
170 
171         /* Check if an instance of the interface is present.  */
172         if (interface_ptr -> ux_interface_class_instance != UX_NULL)
173         {
174 
175             /* We need to stop the class instance for the device.  */
176             command.ux_host_class_command_instance =  interface_ptr -> ux_interface_class_instance;
177 
178             /* Call the class.  */
179             interface_ptr -> ux_interface_class -> ux_host_class_entry_function(&command);
180         }
181 
182         /* Move to next interface.  */
183         interface_ptr =  interface_ptr -> ux_interface_next_interface;
184     }
185 
186     /* The device can now be un-configured.  */
187     status =  _ux_host_stack_device_configuration_reset(device);
188 
189 #if defined(UX_HOST_STANDALONE)
190     device -> ux_device_flags &= ~UX_DEVICE_FLAG_LOCK;
191 #endif
192 
193     /* Return completion status.  */
194     return(status);
195 }
196 
197 
198 /**************************************************************************/
199 /*                                                                        */
200 /*  FUNCTION                                               RELEASE        */
201 /*                                                                        */
202 /*    _uxe_host_stack_device_configuration_deactivate     PORTABLE C      */
203 /*                                                           6.3.0        */
204 /*  AUTHOR                                                                */
205 /*                                                                        */
206 /*    Chaoqiong Xiao, Microsoft Corporation                               */
207 /*                                                                        */
208 /*  DESCRIPTION                                                           */
209 /*                                                                        */
210 /*    This function checks errors in host stack configuration deactivate  */
211 /*    function call.                                                      */
212 /*                                                                        */
213 /*  INPUT                                                                 */
214 /*                                                                        */
215 /*    device                                Pointer to device             */
216 /*                                                                        */
217 /*  OUTPUT                                                                */
218 /*                                                                        */
219 /*    None                                                                */
220 /*                                                                        */
221 /*  CALLS                                                                 */
222 /*                                                                        */
223 /*    _ux_host_stack_device_configuration_deactivate                      */
224 /*                                          Host stack config deactivate  */
225 /*                                                                        */
226 /*  CALLED BY                                                             */
227 /*                                                                        */
228 /*    Application                                                         */
229 /*                                                                        */
230 /*  RELEASE HISTORY                                                       */
231 /*                                                                        */
232 /*    DATE              NAME                      DESCRIPTION             */
233 /*                                                                        */
234 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
235 /*                                                                        */
236 /**************************************************************************/
_uxe_host_stack_device_configuration_deactivate(UX_DEVICE * device)237 UINT  _uxe_host_stack_device_configuration_deactivate(UX_DEVICE *device)
238 {
239 
240     /* Sanity check.  */
241     if (device == UX_NULL)
242         return(UX_INVALID_PARAMETER);
243 
244     /* Invoke configuration deactivate function.  */
245     return(_ux_host_stack_device_configuration_deactivate(device));
246 }
247