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_read_thread_entry            PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function is thread of ISO OUT from the Audio class.            */
44 /*                                                                        */
45 /*    It's for RTOS mode.                                                 */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    audio_stream                          Address of audio stream       */
50 /*                                            instance                    */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    None                                                                */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_system_error_handler              System error trap             */
59 /*    _ux_device_thread_suspend             Suspend thread used           */
60 /*    _ux_device_stack_transfer_request     Issue transfer request        */
61 /*    _ux_utility_memory_copy               Copy data                     */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    ThreadX                                                             */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            verified memset and memcpy  */
74 /*                                            cases,                      */
75 /*                                            resulting in version 6.1    */
76 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            replaced wMaxPacketSize by  */
78 /*                                            calculated payload size,    */
79 /*                                            resulting in version 6.1.9  */
80 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            refined macros names,       */
82 /*                                            resulting in version 6.1.10 */
83 /*  10-31-2022     Yajun Xia                Modified comment(s),          */
84 /*                                            added standalone support,   */
85 /*                                            resulting in version 6.2.0  */
86 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
87 /*                                            added a new mode to manage  */
88 /*                                            endpoint buffer in classes  */
89 /*                                            with zero copy enabled,     */
90 /*                                            resulting in version 6.3.0  */
91 /*                                                                        */
92 /**************************************************************************/
_ux_device_class_audio_read_thread_entry(ULONG audio_stream)93 VOID _ux_device_class_audio_read_thread_entry(ULONG audio_stream)
94 {
95 
96 UINT                            status;
97 UX_DEVICE_CLASS_AUDIO_STREAM    *stream;
98 UX_SLAVE_DEVICE                 *device;
99 UX_SLAVE_ENDPOINT               *endpoint;
100 UX_SLAVE_TRANSFER               *transfer;
101 UCHAR                           *next_pos;
102 UX_DEVICE_CLASS_AUDIO_FRAME     *next_frame;
103 ULONG                           max_packet_size;
104 ULONG                           actual_length;
105 
106 
107     /* Get Audio class instance.  */
108     UX_THREAD_EXTENSION_PTR_GET(stream, UX_DEVICE_CLASS_AUDIO_STREAM, audio_stream)
109 
110     /* Get stack device instance.  */
111     device = stream -> ux_device_class_audio_stream_audio -> ux_device_class_audio_device;
112 
113     /* This thread runs forever but can be suspended or resumed.  */
114     while(1)
115     {
116         max_packet_size = 0;
117         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
118         {
119 
120             /* Get endpoint instance.  */
121             endpoint = stream -> ux_device_class_audio_stream_endpoint;
122 
123             /* Endpoint not available, maybe it's alternate setting 0.  */
124             if (endpoint == UX_NULL)
125                 break;
126 
127             /* Calculate transfer size based on packet size and number transactions once endpoint is available.  */
128             if (max_packet_size == 0)
129                 max_packet_size = endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_transfer_length;
130 
131             /* Get transfer instance.  */
132             transfer = &endpoint -> ux_slave_endpoint_transfer_request;
133 
134 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
135 
136             /* Zero copy: directly use frame buffer.  */
137             transfer -> ux_slave_transfer_request_data_pointer = stream ->
138                     ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_data;
139 #endif
140 
141             /* Start frame transfer anyway.  */
142             status = _ux_device_stack_transfer_request(transfer, max_packet_size, max_packet_size);
143 
144             /* Check error.  */
145             if (status != UX_SUCCESS)
146             {
147 
148                 /* Error notification!  */
149                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
150                 stream -> ux_device_class_audio_stream_buffer_error_count ++;
151                 break;
152             }
153 
154             /* Get actual transfer length.  */
155             actual_length = transfer -> ux_slave_transfer_request_actual_length;
156 
157             /* Frame received, log it.  */
158             stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_length = actual_length;
159             stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_pos = 0;
160 
161 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 0
162 
163             /* Copy data from endpoint buffer.  */
164             _ux_utility_memory_copy(stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_data,
165                             transfer -> ux_slave_transfer_request_data_pointer,
166                             actual_length); /* Use case of memcpy is verified. */
167 #endif
168 
169             /* For simple, do not advance the transfer position if there is overflow.  */
170             next_pos = (UCHAR *)stream -> ux_device_class_audio_stream_transfer_pos;
171             next_pos += stream -> ux_device_class_audio_stream_frame_buffer_size;
172             if (next_pos >= stream -> ux_device_class_audio_stream_buffer + stream -> ux_device_class_audio_stream_buffer_size)
173                 next_pos = stream -> ux_device_class_audio_stream_buffer;
174             next_frame = (UX_DEVICE_CLASS_AUDIO_FRAME *)next_pos;
175 
176             /* Check overflow!  */
177             if (next_frame -> ux_device_class_audio_frame_length > 0)
178             {
179 
180                 /* Error notification!  */
181                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_BUFFER_OVERFLOW);
182                 stream -> ux_device_class_audio_stream_buffer_error_count ++;
183             }
184             else
185 
186                 /* Update transfer position.  */
187                 stream -> ux_device_class_audio_stream_transfer_pos = next_frame;
188 
189             /* Invoke notification callback. */
190             if (stream -> ux_device_class_audio_stream_callbacks.ux_device_class_audio_stream_frame_done != UX_NULL)
191                 stream -> ux_device_class_audio_stream_callbacks.ux_device_class_audio_stream_frame_done(stream, actual_length);
192         }
193 
194         /* We need to suspend ourselves. We will be resumed by the device enumeration module or when a change of alternate setting happens.  */
195         _ux_device_thread_suspend(&stream -> ux_device_class_audio_stream_thread);
196     }
197 }
198 
199