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_read16                PORTABLE C      */
36 /*                                                           6.2.1        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function reads 16-bit sample value 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_read16(UX_DEVICE_CLASS_AUDIO_STREAM * stream,USHORT * buffer)74 UINT _ux_device_class_audio_sample_read16(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
75                                           USHORT *buffer)
76 {
77 
78 UX_SLAVE_ENDPOINT           *endpoint;
79 UX_SLAVE_DEVICE             *device;
80 UCHAR                       *sample_ptr;
81 UCHAR                       *next_frame_buffer;
82 ULONG                       next_frame_sample;
83 
84 
85     /* Get the pointer to the device.  */
86     device =  &_ux_system_slave -> ux_system_slave_device;
87 
88     /* As long as the device is in the CONFIGURED state.  */
89     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
90     {
91 
92         /* Cannot proceed with command, the interface is down.  */
93         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
94     }
95 
96     /* Check if endpoint is available.  */
97     endpoint = stream -> ux_device_class_audio_stream_endpoint;
98     if (endpoint == UX_NULL)
99         return(UX_ERROR);
100 
101     /* Check if endpoint direction is OK.  */
102     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_OUT)
103         return(UX_ERROR);
104 
105     /* Underflow!!  */
106     if (stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length == 0)
107     {
108         return(UX_BUFFER_OVERFLOW);
109     }
110 
111     /* Try to read a sample.  */
112     sample_ptr = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_data +
113                     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos;
114     if (buffer)
115         *buffer = *(USHORT *)(sample_ptr);
116 
117     /* Update sample read state.  */
118     next_frame_sample = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos + 2;
119     if (next_frame_sample >= stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length)
120     {
121 
122         /* Set frame length to 0 to indicate no data.  */
123         stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length = 0;
124 
125         /* Move to next frame buffer.  */
126         next_frame_sample = 0;
127 
128         /* Move frame if it's not the last one.  */
129         if (stream -> ux_device_class_audio_stream_access_pos != stream -> ux_device_class_audio_stream_transfer_pos)
130         {
131             next_frame_buffer = (UCHAR *)stream -> ux_device_class_audio_stream_access_pos;
132             next_frame_buffer += stream -> ux_device_class_audio_stream_frame_buffer_size;
133             if (next_frame_buffer >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
134                 next_frame_buffer = stream -> ux_device_class_audio_stream_buffer;
135             stream -> ux_device_class_audio_stream_access_pos = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_frame_buffer;
136         }
137     }
138 
139     /* Update next sample position.  */
140     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_pos = next_frame_sample;
141 
142     return(UX_SUCCESS);
143 }
144 
145 
146 /**************************************************************************/
147 /*                                                                        */
148 /*  FUNCTION                                               RELEASE        */
149 /*                                                                        */
150 /*    _uxe_device_class_audio_sample_read16                PORTABLE C     */
151 /*                                                            6.2.1       */
152 /*  AUTHOR                                                                */
153 /*                                                                        */
154 /*    Chaoqiong Xiao, Microsoft Corporation                               */
155 /*                                                                        */
156 /*  DESCRIPTION                                                           */
157 /*                                                                        */
158 /*    This function checks errors in reading 16-bit sample function call. */
159 /*                                                                        */
160 /*  INPUT                                                                 */
161 /*                                                                        */
162 /*    stream                                Address of audio stream       */
163 /*                                            instance                    */
164 /*    buffer                                Pointer to buffer to save     */
165 /*                                            sample data                 */
166 /*                                                                        */
167 /*  OUTPUT                                                                */
168 /*                                                                        */
169 /*    None                                                                */
170 /*                                                                        */
171 /*  CALLS                                                                 */
172 /*                                                                        */
173 /*    _ux_device_class_audio_sample_read16   Read 16-bit sample           */
174 /*                                                                        */
175 /*  CALLED BY                                                             */
176 /*                                                                        */
177 /*    Application                                                         */
178 /*                                                                        */
179 /*  RELEASE HISTORY                                                       */
180 /*                                                                        */
181 /*    DATE              NAME                      DESCRIPTION             */
182 /*                                                                        */
183 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
184 /*                                                                        */
185 /**************************************************************************/
_uxe_device_class_audio_sample_read16(UX_DEVICE_CLASS_AUDIO_STREAM * stream,USHORT * buffer)186 UINT _uxe_device_class_audio_sample_read16(UX_DEVICE_CLASS_AUDIO_STREAM *stream,
187                                          USHORT *buffer)
188 {
189 
190     /* Sanity check.  */
191     if (stream == UX_NULL)
192         return(UX_INVALID_PARAMETER);
193 
194     /* Read 16-bit sample.  */
195     return(_ux_device_class_audio_sample_read16(stream, buffer));
196 }
197