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_reception_start              PORTABLE C      */
36 /*                                                           6.2.1        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function start receiving frames in the Audio class.            */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    stream                                Address of audio stream       */
48 /*                                            instance                    */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    None                                                                */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_device_thread_resume              Resume thread used            */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    Application                                                         */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
67 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            resulting in version 6.1    */
69 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            refined macros names,       */
71 /*                                            resulting in version 6.1.10 */
72 /*  10-31-2022     Yajun Xia                Modified comment(s),          */
73 /*                                            added standalone support,   */
74 /*                                            resulting in version 6.2.0  */
75 /*  03-08-2023     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            resulting in version 6.2.1  */
77 /*                                                                        */
78 /**************************************************************************/
_ux_device_class_audio_reception_start(UX_DEVICE_CLASS_AUDIO_STREAM * stream)79 UINT _ux_device_class_audio_reception_start(UX_DEVICE_CLASS_AUDIO_STREAM *stream)
80 {
81 
82 UX_SLAVE_ENDPOINT           *endpoint;
83 UX_SLAVE_DEVICE             *device;
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         /* Cannot proceed with command, the interface is down.  */
94         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
95     }
96 
97     /* Check if endpoint is available.  */
98     endpoint = stream -> ux_device_class_audio_stream_endpoint;
99     if (endpoint == UX_NULL)
100         return(UX_ERROR);
101 
102     /* Check if endpoint direction is OK.  */
103     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_OUT)
104         return(UX_ERROR);
105 
106     /* Check if overflow.  */
107     if (stream -> ux_device_class_audio_stream_transfer_pos -> ux_device_class_audio_frame_length > 0)
108         return(UX_BUFFER_OVERFLOW);
109 
110 #if defined(UX_DEVICE_STANDALONE)
111 
112     /* Start read task.  */
113     if (stream -> ux_device_class_audio_stream_task_state == UX_DEVICE_CLASS_AUDIO_STREAM_RW_STOP)
114         stream -> ux_device_class_audio_stream_task_state = UX_DEVICE_CLASS_AUDIO_STREAM_RW_START;
115 #else
116 
117     /* Start read thread.  */
118     _ux_device_thread_resume(&stream -> ux_device_class_audio_stream_thread);
119 #endif
120     return(UX_SUCCESS);
121 }
122 
123 
124 /**************************************************************************/
125 /*                                                                        */
126 /*  FUNCTION                                               RELEASE        */
127 /*                                                                        */
128 /*    _uxe_device_class_audio_reception_start             PORTABLE C      */
129 /*                                                           6.2.1        */
130 /*  AUTHOR                                                                */
131 /*                                                                        */
132 /*    Chaoqiong Xiao, Microsoft Corporation                               */
133 /*                                                                        */
134 /*  DESCRIPTION                                                           */
135 /*                                                                        */
136 /*    This function checks errors in stream reception start function.     */
137 /*                                                                        */
138 /*  INPUT                                                                 */
139 /*                                                                        */
140 /*    stream                                Address of audio stream       */
141 /*                                            instance                    */
142 /*                                                                        */
143 /*  OUTPUT                                                                */
144 /*                                                                        */
145 /*    None                                                                */
146 /*                                                                        */
147 /*  CALLS                                                                 */
148 /*                                                                        */
149 /*    _ux_device_class_audio_reception_start                              */
150 /*                                          Audio stream reception start  */
151 /*                                                                        */
152 /*  CALLED BY                                                             */
153 /*                                                                        */
154 /*    Application                                                         */
155 /*                                                                        */
156 /*  RELEASE HISTORY                                                       */
157 /*                                                                        */
158 /*    DATE              NAME                      DESCRIPTION             */
159 /*                                                                        */
160 /*  03-08-2023     Chaoqiong Xiao           Initial Version 6.2.1         */
161 /*                                                                        */
162 /**************************************************************************/
_uxe_device_class_audio_reception_start(UX_DEVICE_CLASS_AUDIO_STREAM * stream)163 UINT _uxe_device_class_audio_reception_start(UX_DEVICE_CLASS_AUDIO_STREAM *stream)
164 {
165 
166     /* Sanity check.  */
167     if (stream == UX_NULL)
168         return(UX_INVALID_PARAMETER);
169 
170     /* Start reception.  */
171     return(_ux_device_class_audio_reception_start(stream));
172 }
173