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_read                           PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function reads from the audio streaming interface.             */
46 /*                                                                        */
47 /*    Note the request buffer should be ready for at least one packet,    */
48 /*    and request length is always limited to one packet size.            */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    audio                                 Pointer to audio class        */
53 /*    audio_transfer_request                Pointer to transfer request   */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_class_audio_transfer_request Audio transfer request        */
62 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
63 /*    _ux_host_mutex_on                     Get mutex                     */
64 /*    _ux_host_mutex_off                    Release mutex                 */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
69 /*    Audio Class                                                         */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            use pre-calculated value    */
80 /*                                            instead of wMaxPacketSize,  */
81 /*                                            resulting in version 6.1.9  */
82 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            refined macros names,       */
84 /*                                            resulting in version 6.1.10 */
85 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
86 /*                                            fixed standalone compile,   */
87 /*                                            resulting in version 6.1.11 */
88 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
89 /*                                            refined packet size manage, */
90 /*                                            protect reentry with mutex, */
91 /*                                            refined transfer implement, */
92 /*                                            fixed error return code,    */
93 /*                                            resulting in version 6.1.12 */
94 /*                                                                        */
95 /**************************************************************************/
_ux_host_class_audio_read(UX_HOST_CLASS_AUDIO * audio,UX_HOST_CLASS_AUDIO_TRANSFER_REQUEST * audio_transfer_request)96 UINT  _ux_host_class_audio_read(UX_HOST_CLASS_AUDIO *audio, UX_HOST_CLASS_AUDIO_TRANSFER_REQUEST *audio_transfer_request)
97 {
98 
99 UINT        status;
100 ULONG       mps;
101 
102     /* If trace is enabled, insert this event into the trace buffer.  */
103     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_AUDIO_READ, audio, audio_transfer_request -> ux_host_class_audio_transfer_request_data_pointer,
104                             audio_transfer_request -> ux_host_class_audio_transfer_request_requested_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
105 
106     /* Ensure the instance is valid.  */
107     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_audio_name, (VOID *) audio) != UX_SUCCESS)
108     {
109 
110         /* Error trap. */
111         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
112 
113         /* If trace is enabled, insert this event into the trace buffer.  */
114         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
115 
116         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
117     }
118 
119     /* Protect thread reentry to this instance.  */
120     _ux_host_mutex_on(&audio -> ux_host_class_audio_mutex);
121 
122     /* Ensure we have a selected interface that allows isoch transmission.  */
123     if (audio -> ux_host_class_audio_isochronous_endpoint -> ux_endpoint_descriptor.wMaxPacketSize == 0)
124     {
125 
126         /* Unprotect thread reentry to this instance.  */
127         _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
128 
129         /* Error trap. */
130         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_AUDIO_WRONG_INTERFACE);
131 
132         /* Return error status.  */
133         return(UX_HOST_CLASS_AUDIO_WRONG_INTERFACE);
134     }
135 
136     /* For audio in, we read packets at a time, so the transfer request size is the size of the
137        endpoint max packet size.  */
138     mps = _ux_host_class_audio_max_packet_size_get(audio);
139     audio_transfer_request -> ux_host_class_audio_transfer_request_packet_size = mps;
140     audio_transfer_request -> ux_host_class_audio_transfer_request_requested_length = mps;
141 
142     /* Ask the stack to hook this transfer request to the iso ED.  */
143     status =  _ux_host_class_audio_transfer_request(audio, audio_transfer_request);
144 
145     /* Unprotect thread reentry to this instance.  */
146     _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
147 
148     /* Return completion status.  */
149     return(status);
150 }
151