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_streaming_terminal_get         PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function searches and retrieves the streaming terminal. This   */
46 /*    terminal is used to further search which of the input\output        */
47 /*    terminals describes if the device is audio IN or audio OUT.         */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    audio                                 Pointer to audio class        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_utility_descriptor_parse          Parse descriptor              */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Audio Class                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*                                                                        */
73 /**************************************************************************/
_ux_host_class_audio_streaming_terminal_get(UX_HOST_CLASS_AUDIO * audio)74 UINT  _ux_host_class_audio_streaming_terminal_get(UX_HOST_CLASS_AUDIO *audio)
75 {
76 
77 UCHAR *                                             descriptor;
78 UX_INTERFACE_DESCRIPTOR                             interface_descriptor;
79 UX_HOST_CLASS_AUDIO_STREAMING_INTERFACE_DESCRIPTOR  streaming_interface_descriptor;
80 ULONG                                               total_descriptor_length;
81 ULONG                                               descriptor_length;
82 ULONG                                               descriptor_type;
83 ULONG                                               descriptor_subtype;
84 ULONG                                               descriptor_found;
85 
86 
87     /* Get the descriptor to the entire configuration */
88     descriptor =  audio -> ux_host_class_audio_configuration_descriptor;
89     total_descriptor_length =  audio -> ux_host_class_audio_configuration_descriptor_length;
90 
91     /* Default is Interface descriptor not yet found.  */
92     descriptor_found =  UX_FALSE;
93     audio -> ux_host_class_audio_terminal_link =  0xffffffff;
94 
95     /* Scan the descriptor for the Audio Streaming interface.  */
96     while (total_descriptor_length)
97     {
98 
99         /* Gather the length, type and subtype of the descriptor.  */
100         descriptor_length =   *descriptor;
101         descriptor_type =     *(descriptor + 1);
102         descriptor_subtype =  *(descriptor + 2);
103 
104         /* Make sure this descriptor has at least the minimum length.  */
105         if (descriptor_length < 3)
106         {
107 
108             /* Error trap. */
109             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
110 
111             /* If trace is enabled, insert this event into the trace buffer.  */
112             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
113 
114             return(UX_DESCRIPTOR_CORRUPTED);
115         }
116 
117         /* Process relative to the descriptor type.  */
118         switch(descriptor_type)
119         {
120 
121 
122         case UX_INTERFACE_DESCRIPTOR_ITEM:
123 
124             /* Parse the interface descriptor and make it machine independent.  */
125             _ux_utility_descriptor_parse(descriptor, _ux_system_interface_descriptor_structure,
126                                             UX_INTERFACE_DESCRIPTOR_ENTRIES, (UCHAR *) &interface_descriptor);
127 
128             /* Ensure we have the correct interface for Audio streaming.  */
129             if ((interface_descriptor.bInterfaceClass == UX_HOST_CLASS_AUDIO_CLASS) &&
130                 (interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_AUDIO_SUBCLASS_STREAMING) &&
131                 (interface_descriptor.bInterfaceNumber == audio -> ux_host_class_audio_streaming_interface -> ux_interface_descriptor.bInterfaceNumber))
132 
133                 /* Mark we have found it.  */
134                 descriptor_found =  UX_TRUE;
135             else
136 
137                 descriptor_found =  UX_FALSE;
138             break;
139 
140 
141         case UX_HOST_CLASS_AUDIO_CS_INTERFACE:
142 
143             /* First make sure we have found the correct generic interface descriptor.  */
144             if (descriptor_found == UX_TRUE)
145             {
146 
147                 /* Check the sub type.  */
148                 switch(descriptor_subtype)
149                 {
150 
151 
152                 case UX_HOST_CLASS_AUDIO_CS_AS_GENERAL:
153 
154                     /* Make the descriptor machine independent.  */
155                     _ux_utility_descriptor_parse(descriptor, _ux_system_class_audio_streaming_interface_descriptor_structure,
156                                             UX_HOST_CLASS_AUDIO_STREAMING_INTERFACE_DESCRIPTOR_ENTRIES, (UCHAR *) &streaming_interface_descriptor);
157 
158                     /* Save the terminal link.  */
159                     audio -> ux_host_class_audio_terminal_link =  streaming_interface_descriptor.bTerminalLink;
160                     return(UX_SUCCESS);
161                 }
162             }
163             break;
164         }
165 
166         /* Verify if the descriptor is still valid.  */
167         if (descriptor_length > total_descriptor_length)
168         {
169 
170             /* Error trap. */
171             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
172 
173             /* If trace is enabled, insert this event into the trace buffer.  */
174             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
175 
176             return(UX_DESCRIPTOR_CORRUPTED);
177         }
178 
179         /* Jump to the next descriptor if we have not reached the end.  */
180         descriptor +=  descriptor_length;
181 
182         /* And adjust the length left to parse in the descriptor.  */
183         total_descriptor_length -=  descriptor_length;
184     }
185 
186     /* Error trap. */
187     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_AUDIO_WRONG_TYPE);
188 
189     /* We get here when either the report descriptor has a problem or we could
190        not find the right audio device.  */
191     return(UX_HOST_CLASS_AUDIO_WRONG_TYPE);
192 }
193 
194