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_endpoints_get                  PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function searches for the handle of the isochronous endpoints. */
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_interface_endpoint_get Get interface endpoint        */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Audio Class                                                         */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
68 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            saved transfer direction,   */
72 /*                                            added feedback support,     */
73 /*                                            refined ISO support logic,  */
74 /*                                            resulting in version 6.1.12 */
75 /*                                                                        */
76 /**************************************************************************/
_ux_host_class_audio_endpoints_get(UX_HOST_CLASS_AUDIO * audio)77 UINT  _ux_host_class_audio_endpoints_get(UX_HOST_CLASS_AUDIO *audio)
78 {
79 
80 UINT            status;
81 UINT            endpoint_index;
82 UX_ENDPOINT     *endpoint;
83 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
84 UX_TRANSFER     *transfer;
85 ULONG           packet_length;
86 #endif
87 
88     /* Reset endpoints.  */
89     audio -> ux_host_class_audio_isochronous_endpoint = UX_NULL;
90 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
91     audio -> ux_host_class_audio_feedback_endpoint = UX_NULL;
92 #endif
93 
94     /* Search the ISO OUT endpoint. It is attached to the interface container.  */
95     for (endpoint_index = 0; endpoint_index < audio -> ux_host_class_audio_streaming_interface -> ux_interface_descriptor.bNumEndpoints;
96                         endpoint_index++)
97     {
98 
99         /* Get interface endpoint.  */
100         status =  _ux_host_stack_interface_endpoint_get(audio -> ux_host_class_audio_streaming_interface, endpoint_index, &endpoint);
101 
102         /* Check completion status.  */
103         if (status == UX_SUCCESS)
104         {
105 
106             /* Check if endpoint is ISO.  */
107             if ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_ISOCHRONOUS_ENDPOINT)
108             {
109 
110                 /* Check endpoint direction.  */
111                 if ((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT)
112                 {
113 
114                     /* We have found the ISO OUT endpoint.  */
115                     endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type = UX_REQUEST_OUT;
116 
117                     /* For OUTPUT, it's data endpoint, else feedback.  */
118                     if (audio -> ux_host_class_audio_type == UX_HOST_CLASS_AUDIO_OUTPUT)
119                         audio -> ux_host_class_audio_isochronous_endpoint =  endpoint;
120 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
121                     else
122                         audio -> ux_host_class_audio_feedback_endpoint = endpoint;
123 #endif
124                 }
125                 else
126                 {
127 
128                     /* We have found the ISO IN endpoint.  */
129                     endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type = UX_REQUEST_IN;
130 
131                     /* For INPUT, it's data endpoint, else feedback.  */
132                     if (audio -> ux_host_class_audio_type == UX_HOST_CLASS_AUDIO_INPUT)
133                         audio -> ux_host_class_audio_isochronous_endpoint =  endpoint;
134 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
135                     else
136                         audio -> ux_host_class_audio_feedback_endpoint = endpoint;
137 #endif
138                 }
139             }
140         }
141     }
142 
143     /* The isochronous endpoint is mandatory. If we didn't find it, return an error.  */
144     if (audio -> ux_host_class_audio_isochronous_endpoint == UX_NULL)
145     {
146 
147         /* Error trap. */
148         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
149 
150         /* If trace is enabled, insert this event into the trace buffer.  */
151         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
152 
153         return(UX_ENDPOINT_HANDLE_UNKNOWN);
154     }
155 
156 #if defined(UX_HOST_CLASS_AUDIO_FEEDBACK_SUPPORT)
157 
158     /* With feedback, allocate buffer for packed feedback data.  */
159     if (audio -> ux_host_class_audio_feedback_endpoint)
160     {
161 
162         /* Get transfer.  */
163         endpoint = audio -> ux_host_class_audio_feedback_endpoint;
164         transfer = &endpoint -> ux_endpoint_transfer_request;
165 
166         /* Allocate buffer for feedback transfer (minimum 4 to fill both FS and HS).  */
167         packet_length = transfer -> ux_transfer_request_packet_length;
168         if (packet_length < sizeof(audio -> ux_host_class_audio_feedback_buffer))
169             packet_length = sizeof(audio -> ux_host_class_audio_feedback_buffer);
170         transfer -> ux_transfer_request_data_pointer = _ux_utility_memory_allocate(
171                                 UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY,
172                                 packet_length);
173         if (transfer -> ux_transfer_request_data_pointer == UX_NULL)
174         {
175 
176             /* Error trap. */
177             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
178 
179             /* If trace is enabled, insert this event into the trace buffer.  */
180             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
181 
182             return(UX_MEMORY_INSUFFICIENT);
183         }
184         else
185         {
186 
187             /* Sync previous feedback data.  */
188             _ux_utility_memory_copy(transfer -> ux_transfer_request_data_pointer,
189                                     audio -> ux_host_class_audio_feedback_buffer,
190                                     sizeof(audio -> ux_host_class_audio_feedback_buffer)); /* Use case of memcpy is verified. */
191         }
192 
193         /* Fill the transfer request with all the required fields.  */
194         transfer -> ux_transfer_request_endpoint =                  audio -> ux_host_class_audio_feedback_endpoint;
195         transfer -> ux_transfer_request_requested_length =          (endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) ?
196                                                                     transfer -> ux_transfer_request_packet_length :
197                                                                     ((_ux_host_class_audio_speed_get(audio) == UX_HIGH_SPEED_DEVICE) ?
198                                                                      UX_FEEDBACK_SIZE_HIGH_SPEED : UX_FEEDBACK_SIZE_FULL_SPEED);
199         transfer -> ux_transfer_request_completion_function =       _ux_host_class_audio_feedback_transfer_completed;
200         transfer -> ux_transfer_request_class_instance =            audio;
201 
202         /* Start feedback transfer in background.  */
203         status = _ux_host_stack_transfer_request(transfer);
204         if (status != UX_SUCCESS)
205             return(status);
206     }
207 #endif
208 
209     /* Return successful status.  */
210     return(UX_SUCCESS);
211 }
212