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 #if defined(UX_DEVICE_STANDALONE)
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_device_class_audio_read_task_function PORTABLE C */
37 /* 6.3.0 */
38 /* AUTHOR */
39 /* */
40 /* Yajun Xia, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function is the background task of the audio stream read. */
45 /* */
46 /* It's for standalone mode. */
47 /* */
48 /* INPUT */
49 /* */
50 /* stream Pointer to audio stream */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* State machine status */
55 /* UX_STATE_EXIT Device not configured */
56 /* UX_STATE_IDLE No streaming transfer running */
57 /* UX_STATE_WAIT Streaming transfer running */
58 /* */
59 /* CALLS */
60 /* */
61 /* _ux_device_stack_transfer_run Run transfer state machine */
62 /* _ux_utility_memory_copy Copy memory */
63 /* */
64 /* CALLED BY */
65 /* */
66 /* USBX Device Stack */
67 /* */
68 /* RELEASE HISTORY */
69 /* */
70 /* DATE NAME DESCRIPTION */
71 /* */
72 /* 10-31-2022 Yajun Xia Initial Version 6.2.0 */
73 /* 10-31-2023 Chaoqiong Xiao Modified comment(s), */
74 /* added a new mode to manage */
75 /* endpoint buffer in classes */
76 /* with zero copy enabled, */
77 /* resulting in version 6.3.0 */
78 /* */
79 /**************************************************************************/
_ux_device_class_audio_read_task_function(UX_DEVICE_CLASS_AUDIO_STREAM * stream)80 UINT _ux_device_class_audio_read_task_function(UX_DEVICE_CLASS_AUDIO_STREAM *stream)
81 {
82 UX_SLAVE_DEVICE *device;
83 UX_SLAVE_ENDPOINT *endpoint;
84 UX_SLAVE_TRANSFER *transfer;
85 UCHAR *next_pos;
86 UX_DEVICE_CLASS_AUDIO_FRAME *next_frame;
87 ULONG max_packet_size;
88 ULONG actual_length;
89 UINT status;
90
91
92 /* Get the pointer to the device. */
93 device = stream -> ux_device_class_audio_stream_audio -> ux_device_class_audio_device;
94
95 /* Check if the device is configured. */
96 if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
97 {
98 stream -> ux_device_class_audio_stream_task_state = UX_STATE_EXIT;
99 return(UX_STATE_EXIT);
100 }
101
102 /* Get the endpoint. */
103 endpoint = stream -> ux_device_class_audio_stream_endpoint;
104
105 /* No endpoint ready, maybe it's alternate setting 0. */
106 if (endpoint == UX_NULL)
107 return(UX_STATE_IDLE);
108
109 /* Check if background transfer task is started. */
110 if (stream -> ux_device_class_audio_stream_task_state == UX_DEVICE_CLASS_AUDIO_STREAM_RW_STOP)
111 return(UX_STATE_IDLE);
112
113 /* Get transfer instance. */
114 transfer = &endpoint -> ux_slave_endpoint_transfer_request;
115
116 /* If not started yet, reset transfer and start polling. */
117 if (stream -> ux_device_class_audio_stream_task_state == UX_DEVICE_CLASS_AUDIO_STREAM_RW_START)
118 {
119
120 /* Next state: transfer wait. */
121 stream -> ux_device_class_audio_stream_task_state = UX_DEVICE_CLASS_AUDIO_STREAM_RW_WAIT;
122
123 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
124
125 /* Zero copy: directly use stream buffer for transfer. */
126 transfer -> ux_slave_transfer_request_data_pointer =
127 stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_data;
128 #endif
129
130 /* Reset transfer state. */
131 UX_SLAVE_TRANSFER_STATE_RESET(transfer);
132 }
133
134 /* Run transfer state machine. */
135 max_packet_size = endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_transfer_length;
136 status = _ux_device_stack_transfer_run(transfer, max_packet_size, max_packet_size);
137
138 /* Error case. */
139 if (status < UX_STATE_NEXT)
140 {
141
142 /* Error on background transfer task start. */
143 stream -> ux_device_class_audio_stream_task_state = UX_STATE_RESET;
144 stream -> ux_device_class_audio_stream_task_status =
145 transfer -> ux_slave_transfer_request_completion_code;
146
147 /* Error notification! */
148 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
149 return(UX_STATE_EXIT);
150 }
151
152 /* Success case. */
153 if (status == UX_STATE_NEXT)
154 {
155
156 /* Next state: start. */
157 stream -> ux_device_class_audio_stream_task_state = UX_DEVICE_CLASS_AUDIO_STREAM_RW_START;
158 stream -> ux_device_class_audio_stream_task_status =
159 transfer -> ux_slave_transfer_request_completion_code;
160
161 /* Get actual transfer length. */
162 actual_length = transfer -> ux_slave_transfer_request_actual_length;
163
164 /* Frame received, log it. */
165 stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_length = actual_length;
166 stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_pos = 0;
167
168 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 0
169
170 /* Copy data from endpoint buffer. */
171 _ux_utility_memory_copy(stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_data,
172 transfer -> ux_slave_transfer_request_data_pointer,
173 actual_length); /* Use case of memcpy is verified. */
174 #endif
175
176 /* For simple, do not advance the transfer position if there is overflow. */
177 next_pos = (UCHAR *)stream -> ux_device_class_audio_stream_transfer_pos;
178 next_pos += stream -> ux_device_class_audio_stream_frame_buffer_size;
179 if (next_pos >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
180 next_pos = stream -> ux_device_class_audio_stream_buffer;
181 next_frame = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_pos;
182
183 /* Check overflow! */
184 if (next_frame -> ux_device_class_audio_frame_length > 0)
185 {
186
187 /* Error notification! */
188 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
189 stream -> ux_device_class_audio_stream_buffer_error_count ++;
190 }
191 else
192
193 /* Update transfer position. */
194 stream -> ux_device_class_audio_stream_transfer_pos = next_frame;
195
196 /* Invoke notification callback. */
197 if (stream -> ux_device_class_audio_stream_callbacks.ux_device_class_audio_stream_frame_done != UX_NULL)
198 stream -> ux_device_class_audio_stream_callbacks.ux_device_class_audio_stream_frame_done(stream, actual_length);
199 }
200
201 /* Keep waiting. */
202 return(UX_STATE_WAIT);
203 }
204 #endif
205