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_descriptor_get                 PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function obtains the entire audio configuration descriptors.   */
46 /*    This is needed because the audio class has many class specific      */
47 /*    descriptors which describe the alternate settings that can be used. */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    audio                                 Pointer to audio class        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_transfer_request       Process transfer request      */
60 /*    _ux_utility_descriptor_parse          Parse descriptor              */
61 /*    _ux_utility_memory_allocate           Allocate memory block         */
62 /*    _ux_utility_memory_free               Release memory block          */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Audio Class                                                         */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            used shared device config   */
77 /*                                            descriptor for enum scan,   */
78 /*                                            resulting in version 6.1.12 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_audio_descriptor_get(UX_HOST_CLASS_AUDIO * audio)81 UINT  _ux_host_class_audio_descriptor_get(UX_HOST_CLASS_AUDIO *audio)
82 {
83 
84 UX_DEVICE               *device;
85 UX_CONFIGURATION        *configuration;
86 UCHAR *                 descriptor;
87 UX_ENDPOINT             *control_endpoint;
88 UX_TRANSFER             *transfer_request;
89 UINT                    status;
90 ULONG                   total_configuration_length;
91 
92 
93     /* Get device, current configuration.  */
94     device = audio -> ux_host_class_audio_device;
95     configuration = device -> ux_device_current_configuration;
96 
97     /* Check if descriptor is previously saved.  */
98     if (device -> ux_device_packed_configuration != UX_NULL)
99     {
100         audio -> ux_host_class_audio_configuration_descriptor =
101                                     device -> ux_device_packed_configuration;
102         audio -> ux_host_class_audio_configuration_descriptor_length =
103                                     configuration -> ux_configuration_descriptor.wTotalLength;
104 
105         /* Descriptor must be kept after enum.  */
106         device -> ux_device_packed_configuration_keep_count ++;
107         return(UX_SUCCESS);
108     }
109 
110     /* Get total length of configuration descriptors.  */
111     total_configuration_length = configuration -> ux_configuration_descriptor.wTotalLength;
112 
113     /* We need to get the default control endpoint transfer request pointer.  */
114     control_endpoint =  &device -> ux_device_control_endpoint;
115     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
116 
117     /* Need to allocate memory for the descriptor.  */
118     descriptor =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, total_configuration_length);
119     if (descriptor == UX_NULL)
120         return(UX_MEMORY_INSUFFICIENT);
121 
122     /* Create a transfer request for the GET_DESCRIPTOR request.  */
123     transfer_request -> ux_transfer_request_data_pointer =      descriptor;
124     transfer_request -> ux_transfer_request_requested_length =  total_configuration_length;
125     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
126     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
127     transfer_request -> ux_transfer_request_value =             UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
128     transfer_request -> ux_transfer_request_index =             0;
129 
130     /* Send request to HCD layer.  */
131     status =  _ux_host_stack_transfer_request(transfer_request);
132 
133     /* Check for correct transfer and entire descriptor returned.  */
134     if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == total_configuration_length))
135     {
136 
137         /* Save the address of the entire configuration descriptor of the audio device.  */
138         audio -> ux_host_class_audio_configuration_descriptor =  descriptor;
139 
140         /* Save the length of the entire descriptor too.  */
141         audio -> ux_host_class_audio_configuration_descriptor_length =  total_configuration_length;
142 
143         /* Keep descriptor to be shared for this device.  */
144         device -> ux_device_packed_configuration = descriptor;
145         device -> ux_device_packed_configuration_keep_count ++;
146 
147         /* We do not free the resource for the descriptor until the device is
148             plugged out.  */
149         return(UX_SUCCESS);
150     }
151 
152     /* Free all used resources.  */
153     _ux_utility_memory_free(descriptor);
154 
155     /* Return completion status.  */
156     return(status);
157 }
158 
159