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_CLASS_AUDIO_FEEDBACK_SUPPORT)
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_audio_feedback_thread_entry        PORTABLE C      */
37 /*                                                           6.2.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is thread of ISO OUT/IN (feedback of IN/OUT)          */
45 /*    for the Audio class.                                                */
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_utility_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 /*  01-31-2022     Chaoqiong Xiao           Initial 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 /*                                                                        */
76 /**************************************************************************/
_ux_device_class_audio_feedback_thread_entry(ULONG audio_stream)77 VOID _ux_device_class_audio_feedback_thread_entry(ULONG audio_stream)
78 {
79 #if defined(UX_DEVICE_STANDALONE)
80     UX_PARAMETER_NOT_USED(audio_stream);
81     return;
82 #else
83 UINT                            status;
84 UX_DEVICE_CLASS_AUDIO_STREAM    *stream;
85 UX_SLAVE_DEVICE                 *device;
86 UX_SLAVE_ENDPOINT               *endpoint;
87 UX_SLAVE_TRANSFER               *transfer;
88 ULONG                           transfer_length;
89 
90 
91     /* Get Audio class stream instance.  */
92     UX_THREAD_EXTENSION_PTR_GET(stream, UX_DEVICE_CLASS_AUDIO_STREAM, audio_stream)
93 
94     /* Get stack device instance.  */
95     device = stream -> ux_device_class_audio_stream_audio -> ux_device_class_audio_device;
96 
97     /* This thread runs forever but can be suspended or resumed.  */
98     while (1)
99     {
100         while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
101         {
102 
103             /* Get endpoint instance.  */
104             endpoint = stream -> ux_device_class_audio_stream_feedback;
105 
106             /* Endpoint not available, maybe it's alternate setting 0.  */
107             if (endpoint == UX_NULL)
108                 break;
109 
110             /* Get transfer instance.  */
111             transfer = &endpoint -> ux_slave_endpoint_transfer_request;
112 
113             /* Length is pre-set on interface alternate setting activate.  */
114             transfer_length = transfer -> ux_slave_transfer_request_requested_length;
115 
116             /* Issue transfer request, thread blocked until transfer done.  */
117             status = _ux_device_stack_transfer_request(transfer, transfer_length, transfer_length);
118 
119             /* Check error.  */
120             if (status == UX_TRANSFER_BUS_RESET)
121                 continue;
122             if (status != UX_SUCCESS)
123             {
124 
125                 /* Error notification!  */
126                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_ERROR);
127                 break;
128             }
129 
130             /* Transfer done, keep data in transfer buffer.  */
131         }
132 
133         /* We need to suspend ourselves. We will be resumed by the device enumeration module or when a change of alternate setting happens.  */
134         _ux_utility_thread_suspend(&stream -> ux_device_class_audio_stream_feedback_thread);
135     }
136 #endif
137 }
138 #endif
139