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_stop                           PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function stops the audio streaming (select alt interface 0).   */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    audio                                 Pointer to audio class        */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_stack_endpoint_transfer_abort                              */
58 /*                                          Abort transfer                */
59 /*    _ux_host_stack_interface_setting_select                             */
60 /*                                          Select interface              */
61 /*    _ux_host_mutex_on                     Get mutex                     */
62 /*    _ux_host_mutex_off                    Release mutex                 */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application                                                         */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  07-29-2022     Chaoqiong Xiao           Initial Version 6.1.12        */
73 /*                                                                        */
74 /**************************************************************************/
_ux_host_class_audio_stop(UX_HOST_CLASS_AUDIO * audio)75 UINT  _ux_host_class_audio_stop(UX_HOST_CLASS_AUDIO *audio)
76 {
77 
78 UINT                    status;
79 UX_CONFIGURATION        *configuration;
80 UX_INTERFACE            *interface_ptr;
81 UINT                    streaming_interface;
82 
83 
84     /* Ensure the instance is valid.  */
85     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_audio_name, (VOID *) audio) != UX_SUCCESS)
86     {
87 
88         /* Error trap. */
89         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
90 
91         /* If trace is enabled, insert this event into the trace buffer.  */
92         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
93 
94         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
95     }
96 
97     /* Protect thread reentry to this instance.  */
98     _ux_host_mutex_on(&audio -> ux_host_class_audio_mutex);
99 
100     /* Get the interface number of the audio streaming interface.  */
101     streaming_interface =  audio -> ux_host_class_audio_streaming_interface -> ux_interface_descriptor.bInterfaceNumber;
102 
103     /* We need to abort transactions on the ISO pipes.  */
104     if (audio -> ux_host_class_audio_isochronous_endpoint != UX_NULL)
105     {
106 
107         /* Abort the iso transfer.  */
108         _ux_host_stack_endpoint_transfer_abort(audio -> ux_host_class_audio_isochronous_endpoint);
109     }
110 
111 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
112     if (audio -> ux_host_class_audio_feedback_endpoint)
113         _ux_host_stack_endpoint_transfer_abort(audio -> ux_host_class_audio_feedback_endpoint);
114 #endif
115 
116     /* We found the alternate setting for the sampling values demanded, now we need
117         to search its container.  */
118     configuration =        audio -> ux_host_class_audio_streaming_interface -> ux_interface_configuration;
119     interface_ptr =        configuration -> ux_configuration_first_interface;
120 
121     /* Scan all interfaces.  */
122     while (interface_ptr != UX_NULL)
123     {
124 
125         /* We search for both the right interface and alternate setting.  */
126         if ((interface_ptr -> ux_interface_descriptor.bInterfaceNumber == streaming_interface) &&
127             (interface_ptr -> ux_interface_descriptor.bAlternateSetting == 0))
128         {
129 
130             /* We have found the right interface/alternate setting combination
131                The stack will select it for us.  */
132             status =  _ux_host_stack_interface_setting_select(interface_ptr);
133 
134             /* If the alternate setting for the streaming interface could be selected, we memorize it.  */
135             if (status == UX_SUCCESS)
136             {
137 
138                 /* Memorize the interface.  */
139                 audio -> ux_host_class_audio_streaming_interface =  interface_ptr;
140 
141                 /* There is no endpoint for the alternate setting 0.  */
142                 _ux_host_stack_endpoint_transfer_abort(audio -> ux_host_class_audio_isochronous_endpoint);
143                 audio -> ux_host_class_audio_isochronous_endpoint = UX_NULL;
144 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
145                 if (audio -> ux_host_class_audio_feedback_endpoint)
146                 {
147                     _ux_host_stack_endpoint_transfer_abort(audio -> ux_host_class_audio_feedback_endpoint);
148                     audio -> ux_host_class_audio_feedback_endpoint = UX_NULL;
149                 }
150 #endif
151 
152                 /* Unprotect thread reentry to this instance.  */
153                 _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
154 
155                 /* Return successful completion.  */
156                 return(UX_SUCCESS);
157             }
158         }
159 
160         /* Move to next interface.  */
161         interface_ptr =  interface_ptr -> ux_interface_next_interface;
162     }
163 
164     /* Unprotect thread reentry to this instance.  */
165     _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
166 
167     /* Return failed completion status.  */
168     return(UX_NO_ALTERNATE_SETTING);
169 }
170