1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device Audio Class                                                  */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_audio.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_audio_sample_read32                PORTABLE C      */
36 /*                                                           6.2.1        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function reads 32-bit sample from the Audio class.             */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    stream                                Address of audio stream       */
48 /*                                            instance                    */
49 /*    buffer                                Pointer to buffer to save     */
50 /*                                            sample data                 */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Application                                                         */
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 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.2.1  */
72 /*                                                                        */
73 /**************************************************************************/
_ux_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM * stream,ULONG * buffer)74 UINT _ux_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM *stream, ULONG *buffer)
75 {
76 
77 UX_SLAVE_ENDPOINT           *endpoint;
78 UX_SLAVE_DEVICE             *device;
79 UCHAR                       *sample_ptr;
80 UCHAR                       *next_frame_buffer;
81 ULONG                       next_frame_sample;
82 
83 
84     /* Get the pointer to the device.  */
85     device =  &_ux_system_slave -> ux_system_slave_device;
86 
87     /* As long as the device is in the CONFIGURED state.  */
88     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
89     {
90 
91         /* Error trap. */
92         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
93 
94         /* Cannot proceed with command, the interface is down.  */
95         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
96     }
97 
98     /* Check if endpoint is available.  */
99     endpoint = stream -> ux_device_class_audio_stream_endpoint;
100     if (endpoint == UX_NULL)
101         return(UX_ERROR);
102 
103     /* Check if endpoint direction is OK.  */
104     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_OUT)
105         return(UX_ERROR);
106 
107     /* Underflow!!  */
108     if (stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length == 0)
109     {
110         return(UX_BUFFER_OVERFLOW);
111     }
112 
113     /* Try to read a sample.  */
114     sample_ptr = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_data +
115                     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos;
116     if (buffer)
117         *buffer = *(ULONG *)(sample_ptr);
118 
119     /* Update sample read state.  */
120     next_frame_sample = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos + 4;
121     if (next_frame_sample >= stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length)
122     {
123 
124         /* Set frame length to 0 to indicate no data.  */
125         stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length = 0;
126 
127         /* Move to next frame buffer.  */
128         next_frame_sample = 0;
129 
130         /* Move frame if it's not the last one.  */
131         if (stream -> ux_device_class_audio_stream_access_pos != stream -> ux_device_class_audio_stream_transfer_pos)
132         {
133             next_frame_buffer = (UCHAR *)stream -> ux_device_class_audio_stream_access_pos;
134             next_frame_buffer += stream -> ux_device_class_audio_stream_frame_buffer_size;
135             if (next_frame_buffer >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
136                 next_frame_buffer = stream -> ux_device_class_audio_stream_buffer;
137             stream -> ux_device_class_audio_stream_access_pos = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_frame_buffer;
138         }
139     }
140 
141     /* Update next sample position.  */
142     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos = next_frame_sample;
143 
144     return(UX_SUCCESS);
145 }
146 
147 
148 /**************************************************************************/
149 /*                                                                        */
150 /*  FUNCTION                                               RELEASE        */
151 /*                                                                        */
152 /*    _uxe_device_class_audio_sample_read32                PORTABLE C     */
153 /*                                                            6.2.1       */
154 /*  AUTHOR                                                                */
155 /*                                                                        */
156 /*    Chaoqiong Xiao, Microsoft Corporation                               */
157 /*                                                                        */
158 /*  DESCRIPTION                                                           */
159 /*                                                                        */
160 /*    This function checks errors in reading 32-bit sample function call. */
161 /*                                                                        */
162 /*  INPUT                                                                 */
163 /*                                                                        */
164 /*    stream                                Address of audio stream       */
165 /*                                            instance                    */
166 /*    buffer                                Pointer to buffer to save     */
167 /*                                            sample data                 */
168 /*                                                                        */
169 /*  OUTPUT                                                                */
170 /*                                                                        */
171 /*    None                                                                */
172 /*                                                                        */
173 /*  CALLS                                                                 */
174 /*                                                                        */
175 /*    _ux_device_class_audio_sample_read32   Read 32-bit sample           */
176 /*                                                                        */
177 /*  CALLED BY                                                             */
178 /*                                                                        */
179 /*    Application                                                         */
180 /*                                                                        */
181 /*  RELEASE HISTORY                                                       */
182 /*                                                                        */
183 /*    DATE              NAME                      DESCRIPTION             */
184 /*                                                                        */
185 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
186 /*                                                                        */
187 /**************************************************************************/
_uxe_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM * stream,ULONG * buffer)188 UINT _uxe_device_class_audio_sample_read32(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
189                                          ULONG *buffer)
190 {
191 
192     /* Sanity check.  */
193     if (stream == UX_NULL)
194         return(UX_INVALID_PARAMETER);
195 
196     /* Read 32-bit sample.  */
197     return(_ux_device_class_audio_sample_read32(stream, buffer));
198 }
199