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 /**   Audio 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_audio.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_audio_deactivate                     PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is called when this instance of the audio has been    */
46 /*    removed from the bus either directly or indirectly. The iso pipes   */
47 /*    will be destroyed and the instanced removed.                        */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    command                               Pointer to command            */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_class_instance_destroy Destroy class instance        */
60 /*    _ux_host_stack_endpoint_transfer_abort Abort outstanding transfer   */
61 /*    _ux_host_mutex_on                     Get mutex                     */
62 /*    _ux_host_mutex_delete                 Delete mutex                  */
63 /*    _ux_utility_memory_free               Release memory block          */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Audio Class                                                         */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            refined macros names,       */
78 /*                                            resulting in version 6.1.10 */
79 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            added interrupt support,    */
81 /*                                            protect reentry with mutex, */
82 /*                                            added feedback support,     */
83 /*                                            resulting in version 6.1.12 */
84 /*                                                                        */
85 /**************************************************************************/
_ux_host_class_audio_deactivate(UX_HOST_CLASS_COMMAND * command)86 UINT  _ux_host_class_audio_deactivate(UX_HOST_CLASS_COMMAND *command)
87 {
88 
89 UX_HOST_CLASS_AUDIO     *audio;
90 #if defined(UX_HOST_CLASS_AUDIO_INTERRUPT_SUPPORT)
91 UX_HOST_CLASS_AUDIO_AC  *ac;
92 #endif
93 
94     /* Get the instance for this class.  */
95     audio = (UX_HOST_CLASS_AUDIO *) command -> ux_host_class_command_instance;
96 
97 #if defined(UX_HOST_CLASS_AUDIO_INTERRUPT_SUPPORT)
98     if (_ux_host_class_audio_subclass_get(audio) == UX_HOST_CLASS_AUDIO_SUBCLASS_CONTROL)
99     {
100         ac = (UX_HOST_CLASS_AUDIO_AC *)audio;
101 
102         /* Stop interrupt and free allocated buffer for it.  */
103         if (ac -> ux_host_class_audio_interrupt_endpoint)
104         {
105             _ux_host_stack_endpoint_transfer_abort(ac -> ux_host_class_audio_interrupt_endpoint);
106             _ux_utility_memory_free(ac -> ux_host_class_audio_interrupt_endpoint ->
107                                     ux_endpoint_transfer_request.ux_transfer_request_data_pointer);
108         }
109 
110         /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
111         endpoints to exit properly.  */
112         _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
113 
114         /* Destroy the instance.  */
115         _ux_host_stack_class_instance_destroy(audio -> ux_host_class_audio_class, (VOID *) audio);
116     }
117     else
118 #endif
119     {
120 
121         /* The audio is being shut down.  */
122         audio -> ux_host_class_audio_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;
123 
124         /* Protect thread reentry to this instance.  */
125         _ux_host_mutex_on(&audio -> ux_host_class_audio_mutex);
126 
127         /* We need to abort transactions on the iso pipe.  */
128         if (audio -> ux_host_class_audio_isochronous_endpoint)
129             _ux_host_stack_endpoint_transfer_abort(audio -> ux_host_class_audio_isochronous_endpoint);
130 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
131     if (audio -> ux_host_class_audio_feedback_endpoint)
132     {
133         _ux_host_stack_endpoint_transfer_abort(audio -> ux_host_class_audio_feedback_endpoint);
134         _ux_utility_memory_free(audio -> ux_host_class_audio_feedback_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer);
135     }
136 #endif
137 
138         /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
139         endpoints to exit properly.  */
140         _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
141 
142         /* Destroy the instance.  */
143         _ux_host_stack_class_instance_destroy(audio -> ux_host_class_audio_class, (VOID *) audio);
144 
145         /* Destroy the semaphore.  */
146         _ux_host_mutex_delete(&audio -> ux_host_class_audio_mutex);
147     }
148 
149     /* Before we free the device resources, we need to inform the application
150         that the device is removed.  */
151     if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
152     {
153 
154         /* Inform the application the device is removed.  */
155         _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, audio -> ux_host_class_audio_class, (VOID *) audio);
156     }
157 
158     /* If trace is enabled, insert this event into the trace buffer.  */
159     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_AUDIO_DEACTIVATE, audio, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
160 
161     /* If trace is enabled, register this object.  */
162     UX_TRACE_OBJECT_UNREGISTER(audio);
163 
164     /* Free the audio instance memory.  */
165     _ux_utility_memory_free(audio);
166 
167     /* Return successful completion.  */
168     return(UX_SUCCESS);
169 }
170 
171