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