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_write_frame_get              PORTABLE C      */
36 /*                                                           6.2.1        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function get a writable frame buffer in the Audio class.       */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    stream                                Address of audio stream       */
48 /*                                            instance                    */
49 /*    frame                                 Pointer to buffer to save     */
50 /*                                            frame buffer pointer        */
51 /*    length                                Pointer to save frame         */
52 /*                                            buffer length in bytes      */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.2.1  */
74 /*                                                                        */
75 /**************************************************************************/
_ux_device_class_audio_write_frame_get(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR ** frame,ULONG * length)76 UINT _ux_device_class_audio_write_frame_get(UX_DEVICE_CLASS_AUDIO_STREAM *stream, UCHAR **frame, ULONG *length)
77 {
78 
79 UX_SLAVE_ENDPOINT           *endpoint;
80 UX_SLAVE_DEVICE             *device;
81 
82 
83     /* Get the pointer to the device.  */
84     device =  &_ux_system_slave -> ux_system_slave_device;
85 
86     /* As long as the device is in the CONFIGURED state.  */
87     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
88     {
89 
90         /* Cannot proceed with command, the interface is down.  */
91         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
92     }
93 
94     /* Check if endpoint is available.  */
95     endpoint = stream -> ux_device_class_audio_stream_endpoint;
96     if (endpoint == UX_NULL)
97         return(UX_ERROR);
98 
99     /* Check if endpoint direction is OK.  */
100     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT)
101         return(UX_ERROR);
102 
103     /* Check overflow!!  */
104     if (stream -> ux_device_class_audio_stream_access_pos == stream -> ux_device_class_audio_stream_transfer_pos &&
105         stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_length != 0)
106         return(UX_BUFFER_OVERFLOW);
107 
108     /* Store frame buffer pointer.  */
109     *frame = stream -> ux_device_class_audio_stream_access_pos -> ux_device_class_audio_frame_data;
110 
111     /* Exclude header size in frame buffer size.  */
112     *length = stream -> ux_device_class_audio_stream_frame_buffer_size - 8;
113 
114     return(UX_SUCCESS);
115 }
116 
117 
118 /**************************************************************************/
119 /*                                                                        */
120 /*  FUNCTION                                               RELEASE        */
121 /*                                                                        */
122 /*    _uxe_device_class_audio_write_frame_get              PORTABLE C      */
123 /*                                                           6.2.1        */
124 /*  AUTHOR                                                                */
125 /*                                                                        */
126 /*    Chaoqiong Xiao, Microsoft Corporation                               */
127 /*                                                                        */
128 /*  DESCRIPTION                                                           */
129 /*                                                                        */
130 /*    This function checks errors in getting frame buffer function call.  */
131 /*                                                                        */
132 /*  INPUT                                                                 */
133 /*                                                                        */
134 /*    stream                                Address of audio stream       */
135 /*                                            instance                    */
136 /*    frame                                 Pointer to buffer to save     */
137 /*                                            frame buffer pointer        */
138 /*    length                                Pointer to save frame         */
139 /*                                            buffer length in bytes      */
140 /*                                                                        */
141 /*  OUTPUT                                                                */
142 /*                                                                        */
143 /*    None                                                                */
144 /*                                                                        */
145 /*  CALLS                                                                 */
146 /*                                                                        */
147 /*    _ux_device_class_audio_write_frame_get                              */
148 /*                                          Get frame buffer              */
149 /*                                                                        */
150 /*  CALLED BY                                                             */
151 /*                                                                        */
152 /*    Application                                                         */
153 /*                                                                        */
154 /*  RELEASE HISTORY                                                       */
155 /*                                                                        */
156 /*    DATE              NAME                      DESCRIPTION             */
157 /*                                                                        */
158 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
159 /*                                                                        */
160 /**************************************************************************/
_uxe_device_class_audio_write_frame_get(UX_DEVICE_CLASS_AUDIO_STREAM * stream,UCHAR ** frame,ULONG * length)161 UINT _uxe_device_class_audio_write_frame_get(UX_DEVICE_CLASS_AUDIO_STREAM *stream, UCHAR **frame, ULONG *length)
162 {
163 
164     /* Sanity checks.  */
165     if (stream == UX_NULL || frame == UX_NULL || length == UX_NULL)
166         return(UX_INVALID_PARAMETER);
167 
168     /* Get frame buffer and length.  */
169     return(_ux_device_class_audio_write_frame_get(stream, frame, length));
170 }
171