1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   Audio Class                                                         */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
27 
28 #include "ux_api.h"
29 #include "ux_host_class_audio.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_audio_entity_control_value_get       PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function obtains the dynamic values for a single audio         */
46 /*    control on either the master channel or a specific channel.         */
47 /*                                                                        */
48 /*    Note only control value of BYTE, WORD and DWORD (<4) is supported.  */
49 /*    E.g., Graphic Equalizer Control is not supported.                   */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    audio                                 Pointer to audio class        */
54 /*    audio_control                         Pointer to audio control      */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
63 /*    _ux_host_class_audio_control_request  Issue audio control request   */
64 /*    _ux_host_mutex_on                     Get mutex                     */
65 /*    _ux_host_mutex_off                    Release mutex                 */
66 /*    _ux_utility_memory_allocate           Allocate memory block         */
67 /*    _ux_utility_memory_free               Release memory block          */
68 /*    _ux_utility_long_get                  Read 32-bit value             */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Audio Class                                                         */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  07-29-2022     Chaoqiong Xiao           Initial Version 6.1.12        */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_audio_entity_control_value_get(UX_HOST_CLASS_AUDIO * audio,UX_HOST_CLASS_AUDIO_CONTROL * audio_control)81 UINT  _ux_host_class_audio_entity_control_value_get(UX_HOST_CLASS_AUDIO *audio, UX_HOST_CLASS_AUDIO_CONTROL *audio_control)
82 {
83 
84 UINT            status;
85 UCHAR           *control_buffer;
86 ULONG           actual_size;
87 
88     /* If trace is enabled, insert this event into the trace buffer.  */
89     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_AUDIO_CONTROL_VALUE_GET, audio, audio_control -> ux_host_class_audio_control_entity, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
90 
91     /* Ensure the instance is valid.  */
92     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_audio_name, (VOID *) audio) != UX_SUCCESS)
93     {
94 
95         /* Error trap. */
96         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
97 
98         /* If trace is enabled, insert this event into the trace buffer.  */
99         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, audio, 0, 0, UX_TRACE_ERRORS, 0, 0)
100 
101         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
102     }
103 
104     /* Protect thread reentry to this instance.  */
105     _ux_host_mutex_on(&audio -> ux_host_class_audio_mutex);
106 
107     /* Need to allocate memory for the control_buffer.  */
108     control_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 4);
109     if (control_buffer == UX_NULL)
110     {
111 
112         /* Unprotect thread reentry to this instance.  */
113         _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
114 
115         /* Return error.  */
116         return(UX_MEMORY_INSUFFICIENT);
117     }
118 
119     /* The buffer should be aligned. Returned data is little endian so DWord can be used
120         to copy any returned data of BYTE/WORD/DWORD.  */
121     * (ULONG *)control_buffer = 0;
122 
123     /* Issue GET CUR request.  */
124     status = _ux_host_class_audio_control_request(audio, 0,
125             UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE,
126 #if defined(UX_HOST_CLASS_AUDIO_2_SUPPORT)
127             (_ux_host_class_audio_protocol_get(audio) == UX_HOST_CLASS_AUDIO_PROTOCOL_IP_VERSION_02_00) ?
128             UX_CLASS_AUDIO20_CUR : UX_HOST_CLASS_AUDIO_GET_CUR,
129 #else
130             UX_HOST_CLASS_AUDIO_GET_CUR,
131 #endif
132             audio_control -> ux_host_class_audio_control_channel |
133                             (audio_control -> ux_host_class_audio_control << 8),
134             audio_control -> ux_host_class_audio_control_entity,
135             control_buffer, 4, &actual_size);
136 
137     /* Check for correct transfer and entire control buffer returned.  */
138     if (status == UX_SUCCESS)
139     {
140 
141         /* Update the CUR static value for the caller.  */
142         audio_control -> ux_host_class_audio_control_cur = _ux_utility_long_get(control_buffer);
143     }
144 
145     /* Free all used resources.  */
146     _ux_utility_memory_free(control_buffer);
147 
148     /* Unprotect thread reentry to this instance.  */
149     _ux_host_mutex_off(&audio -> ux_host_class_audio_mutex);
150 
151     /* Return completion status.  */
152     return(status);
153 }
154