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 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Device Audio Class                                                  */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_class_audio.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_audio_frame_write                  PORTABLE C      */
37 /*                                                           6.2.1        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function writes frame to the Audio class.                      */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    stream                                Address of audio stream       */
49 /*                                            instance                    */
50 /*    frame                                 Pointer to buffer to save     */
51 /*                                            frame data                  */
52 /*    length                                Frame length in bytes         */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_utility_memory_copy               Copy data                     */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Application                                                         */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            verified memset and memcpy  */
73 /*                                            cases,                      */
74 /*                                            resulting in version 6.1    */
75 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            fixed frame length check,   */
77 /*                                            resulting in version 6.1.10 */
78 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            resulting in version 6.2.1  */
80 /*                                                                        */
81 /**************************************************************************/
_ux_device_class_audio_frame_write(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR * frame,ULONG length)82 UINT _ux_device_class_audio_frame_write(UX_DEVICE_CLASS_AUDIO_STREAM *stream, UCHAR *frame, ULONG length)
83 {
84 
85 UX_SLAVE_ENDPOINT           *endpoint;
86 UX_SLAVE_DEVICE             *device;
87 UCHAR                       *next_frame_buffer;
88 ULONG                       frame_buffer_size;
89 
90 
91     /* Get the pointer to the device.  */
92     device =  &_ux_system_slave -> ux_system_slave_device;
93 
94     /* As long as the device is in the CONFIGURED state.  */
95     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
96     {
97 
98         /* Cannot proceed with command, the interface is down.  */
99         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
100     }
101 
102     /* Check if endpoint is available.  */
103     endpoint = stream -> ux_device_class_audio_stream_endpoint;
104     if (endpoint == UX_NULL)
105         return(UX_ERROR);
106 
107     /* Check if endpoint direction is OK (IN).  */
108     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT)
109         return(UX_ERROR);
110 
111     /* Check frame length.  */
112     frame_buffer_size = stream -> ux_device_class_audio_stream_frame_buffer_size;
113     if ((frame_buffer_size - 8) < length)
114         return(UX_ERROR);
115 
116     /* Check overflow!!  */
117     if (stream -> ux_device_class_audio_stream_access_pos == stream -> ux_device_class_audio_stream_transfer_pos &&
118         stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length != 0)
119         return(UX_BUFFER_OVERFLOW);
120 
121     /* Calculate next frame buffer.  */
122     next_frame_buffer = (UCHAR *)stream -> ux_device_class_audio_stream_access_pos;
123     next_frame_buffer += frame_buffer_size;
124     if (next_frame_buffer >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
125         next_frame_buffer = stream -> ux_device_class_audio_stream_buffer;
126 
127     /* Copy frame.  */
128     _ux_utility_memory_copy(stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_data, frame, length); /* Use case of memcpy is verified. */
129     stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length = length;
130 
131     /* Move frame position.  */
132     stream -> ux_device_class_audio_stream_access_pos = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_frame_buffer;
133 
134     return(UX_SUCCESS);
135 }
136 
137 /**************************************************************************/
138 /*                                                                        */
139 /*  FUNCTION                                               RELEASE        */
140 /*                                                                        */
141 /*    _uxe_device_class_audio_frame_write                 PORTABLE C      */
142 /*                                                           6.2.1        */
143 /*  AUTHOR                                                                */
144 /*                                                                        */
145 /*    Chaoqiong Xiao, Microsoft Corporation                               */
146 /*                                                                        */
147 /*  DESCRIPTION                                                           */
148 /*                                                                        */
149 /*    This function checks errors in writing frame function call.         */
150 /*                                                                        */
151 /*  INPUT                                                                 */
152 /*                                                                        */
153 /*    stream                                Address of audio stream       */
154 /*                                            instance                    */
155 /*    frame                                 Pointer to buffer to save     */
156 /*                                            frame data                  */
157 /*    length                                Frame length in bytes         */
158 /*                                                                        */
159 /*  OUTPUT                                                                */
160 /*                                                                        */
161 /*    None                                                                */
162 /*                                                                        */
163 /*  CALLS                                                                 */
164 /*                                                                        */
165 /*    _ux_device_class_audio_frame_write    Write frame to buffer to send */
166 /*                                                                        */
167 /*  CALLED BY                                                             */
168 /*                                                                        */
169 /*    Application                                                         */
170 /*                                                                        */
171 /*  RELEASE HISTORY                                                       */
172 /*                                                                        */
173 /*    DATE              NAME                      DESCRIPTION             */
174 /*                                                                        */
175 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
176 /*                                                                        */
177 /**************************************************************************/
_uxe_device_class_audio_frame_write(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR * frame,ULONG length)178 UINT _uxe_device_class_audio_frame_write(UX_DEVICE_CLASS_AUDIO_STREAM *stream, UCHAR *frame, ULONG length)
179 {
180 
181     /* Sanity checks.  */
182     if (stream == UX_NULL || frame == UX_NULL || length == 0)
183         return(UX_INVALID_PARAMETER);
184 
185     /* Write to frame.  */
186     return(_ux_device_class_audio_frame_write(stream, frame, length));
187 }
188