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_configure                      PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*     This function calls the USBX stack to do a SET_CONFIGURATION to    */
46 /*     the device. Once the device is configured, its interface(s) will   */
47 /*     be activated.                                                      */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    audio                                 Pointer to audio class        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_configuration_interface_get    Get interface         */
60 /*    _ux_host_stack_device_configuration_get       Get configuration     */
61 /*    _ux_host_stack_device_configuration_select    Select configuration  */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Audio Class                                                         */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            optimized based on compile  */
74 /*                                            definitions,                */
75 /*                                            resulting in version 6.1    */
76 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            internal clean up,          */
78 /*                                            resulting in version 6.1.11 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_audio_configure(UX_HOST_CLASS_AUDIO * audio)81 UINT  _ux_host_class_audio_configure(UX_HOST_CLASS_AUDIO *audio)
82 {
83 
84 UINT                    status;
85 UX_CONFIGURATION        *configuration;
86 UX_INTERFACE            *interface;
87 ULONG                   interface_number;
88 #if UX_MAX_DEVICES > 1
89 UX_DEVICE               *parent_device;
90 #endif
91 
92 
93     /* If the device has been configured already, we don't need to do it
94        again. */
95     if (audio -> ux_host_class_audio_device -> ux_device_state == UX_DEVICE_CONFIGURED)
96         return(UX_SUCCESS);
97 
98     /* An audio device normally has one configuration. So retrieve the 1st configuration
99        only.  */
100     status =  _ux_host_stack_device_configuration_get(audio -> ux_host_class_audio_device, 0, &configuration);
101     if (status != UX_SUCCESS)
102     {
103 
104         /* Error trap. */
105         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
106 
107         /* If trace is enabled, insert this event into the trace buffer.  */
108         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, audio -> ux_host_class_audio_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
109 
110         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
111 
112     }
113 
114 #if UX_MAX_DEVICES > 1
115     /* Check the audio power source and check the parent power source for
116        incompatible connections.  */
117     if (audio -> ux_host_class_audio_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
118     {
119 
120         /* Get parent device.  */
121         parent_device =  audio -> ux_host_class_audio_device -> ux_device_parent;
122 
123         /* If the device is NULL, the parent is the root audio and we don't have to worry
124            if the parent is not the root audio, check for its power source.  */
125         if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
126         {
127 
128             /* Error trap. */
129             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
130 
131             /* If trace is enabled, insert this event into the trace buffer.  */
132             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
133 
134             return(UX_CONNECTION_INCOMPATIBLE);
135         }
136     }
137 #endif
138 
139     /* We have the valid configuration. Ask the USBX stack to set this configuration */
140     status =  _ux_host_stack_device_configuration_select(configuration);
141     if (status != UX_SUCCESS)
142     {
143 
144         /* Error trap. */
145         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);
146 
147         /* If trace is enabled, insert this event into the trace buffer.  */
148         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, status, audio -> ux_host_class_audio_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
149 
150         return(status);
151     }
152 
153     /* Start with interface number 0.  */
154     interface_number =  0;
155 
156     /* We only need to retrieve the audio streaming interface.  */
157     do
158     {
159 
160         /* Pickup interface.  */
161         status =  _ux_host_stack_configuration_interface_get(configuration, (UINT) interface_number, 0, &interface);
162 
163         /* Check completion status.  */
164         if (status == UX_SUCCESS)
165         {
166 
167             /* Check the type of interface we have found - is it streaming?  */
168             if (interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_AUDIO_SUBCLASS_STREAMING)
169             {
170 
171                 audio -> ux_host_class_audio_streaming_interface =  interface;
172                 audio -> ux_host_class_audio_streaming_interface -> ux_interface_class_instance =  (VOID *)audio;
173                 break;
174             }
175         }
176     } while(status == UX_SUCCESS);
177 
178     /* After we have parsed the audio interfaces, ensure the streaming interfaces is resent.  */
179     if (audio -> ux_host_class_audio_streaming_interface == UX_NULL)
180     {
181 
182         /* Error trap. */
183         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_INTERFACE_HANDLE_UNKNOWN);
184 
185         /* If trace is enabled, insert this event into the trace buffer.  */
186         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_INTERFACE_HANDLE_UNKNOWN, interface, 0, 0, UX_TRACE_ERRORS, 0, 0)
187 
188         /* We get here when we could not locate the interface(s) handle */
189         return(UX_INTERFACE_HANDLE_UNKNOWN);
190     }
191     else
192     {
193 
194         return(UX_SUCCESS);
195     }
196 }
197 
198