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 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Video Class                                                         */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_video.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_video_control_get                    PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function obtains the static values for a single video control  */
45 /*    on either the master channel or a specific channel.                 */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    video                                 Pointer to video class        */
50 /*    video_control                         Pointer to video control      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_class_instance_verify  Verify instance is valid      */
59 /*    _ux_host_stack_transfer_request       Process transfer request      */
60 /*    _ux_host_semaphore_get                Get semaphore                 */
61 /*    _ux_host_semaphore_put                Release semaphore             */
62 /*    _ux_utility_memory_allocate           Allocate memory block         */
63 /*    _ux_utility_memory_free               Release memory block          */
64 /*    _ux_utility_short_get                 Read 16-bit value             */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
69 /*    Video Class                                                         */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            refined macros names,       */
80 /*                                            resulting in version 6.1.10 */
81 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            fixed standalone compile,   */
83 /*                                            resulting in version 6.1.11 */
84 /*                                                                        */
85 /**************************************************************************/
_ux_host_class_video_control_get(UX_HOST_CLASS_VIDEO * video,UX_HOST_CLASS_VIDEO_CONTROL * video_control)86 UINT  _ux_host_class_video_control_get(UX_HOST_CLASS_VIDEO *video, UX_HOST_CLASS_VIDEO_CONTROL *video_control)
87 {
88 
89 UX_ENDPOINT     *control_endpoint;
90 UX_TRANSFER     *transfer_request;
91 UINT            status;
92 UCHAR *         control_buffer;
93 
94 
95     /* Ensure the instance is valid.  */
96     if (_ux_host_stack_class_instance_verify(_ux_system_host_class_video_name, (VOID *) video) != UX_SUCCESS)
97     {
98 
99         /* Error trap. */
100         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
101 
102         /* If trace is enabled, insert this event into the trace buffer.  */
103         //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, video, 0, 0, UX_TRACE_ERRORS, 0, 0)
104 
105         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
106     }
107 
108     /* Protect thread reentry to this instance.  */
109     status =  _ux_host_semaphore_get(&video -> ux_host_class_video_semaphore, UX_WAIT_FOREVER);
110     if (status != UX_SUCCESS)
111         return(status);
112 
113     /* We need to get the default control endpoint transfer request pointer.  */
114     control_endpoint =  &video -> ux_host_class_video_device -> ux_device_control_endpoint;
115     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
116 
117     /* Need to allocate memory for the control buffer.  */
118     control_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 2);
119     if (control_buffer == UX_NULL)
120     {
121 
122         /* Unprotect thread reentry to this instance.  */
123         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
124 
125         /* Return an error.  */
126         return(UX_MEMORY_INSUFFICIENT);
127     }
128 
129     /* Protect the control endpoint semaphore here.  It will be unprotected in the
130        transfer request function.  */
131     status =  _ux_host_semaphore_get(&video -> ux_host_class_video_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
132 
133     /* Check for status.  */
134     if (status != UX_SUCCESS)
135 
136         /* Something went wrong. */
137         return(status);
138 
139     /* Create a transfer request for the GET_MIN request.  */
140     transfer_request -> ux_transfer_request_data_pointer =      control_buffer;
141     transfer_request -> ux_transfer_request_requested_length =  2;
142     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_VIDEO_GET_MIN;
143     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
144     transfer_request -> ux_transfer_request_value =             (video_control -> ux_host_class_video_control << 8);
145     transfer_request -> ux_transfer_request_index =             video -> ux_host_class_video_control_interface_number | (video -> ux_host_class_video_feature_unit_id << 8);
146 
147     /* Send request to HCD layer.  */
148     status =  _ux_host_stack_transfer_request(transfer_request);
149 
150     /* Check for correct transfer and entire control buffer returned.  */
151     if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 2))
152     {
153 
154         /* Update the MIN static value for the caller.  */
155         video_control -> ux_host_class_video_control_min =  (LONG)(SHORT)_ux_utility_short_get(control_buffer);
156     }
157     else
158     {
159 
160         /* Free the previous control buffer.  */
161         _ux_utility_memory_free(control_buffer);
162 
163         /* Unprotect thread reentry to this instance.  */
164         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
165 
166         /* Return completion status.  */
167         return(UX_TRANSFER_ERROR);
168     }
169 
170     /* Create a transfer request for the GET_MAX request.  */
171     transfer_request -> ux_transfer_request_function =  UX_HOST_CLASS_VIDEO_GET_MAX;
172 
173     /* Send request to HCD layer.  */
174     status =  _ux_host_stack_transfer_request(transfer_request);
175 
176     /* Check for correct transfer and entire control buffer returned.  */
177     if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 2))
178     {
179 
180         /* Update the MAX static value for the caller.  */
181         video_control -> ux_host_class_video_control_max =  (LONG)(SHORT)_ux_utility_short_get(control_buffer);
182     }
183     else
184     {
185 
186         /* Free the previous control buffer.  */
187         _ux_utility_memory_free(control_buffer);
188 
189         /* Unprotect thread reentry to this instance.  */
190         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
191 
192         /* Return completion status.  */
193         return(UX_TRANSFER_ERROR);
194     }
195 
196     /* Create a transfer request for the GET_RES request.  */
197     transfer_request -> ux_transfer_request_function =  UX_HOST_CLASS_VIDEO_GET_RES;
198 
199     /* Send request to HCD layer.  */
200     status =  _ux_host_stack_transfer_request(transfer_request);
201 
202     /* Check for correct transfer and entire control buffer returned.  */
203     if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 2))
204     {
205 
206         /* Update the RES static value for the caller.  */
207         video_control -> ux_host_class_video_control_res =  (LONG)(SHORT)_ux_utility_short_get(control_buffer);
208     }
209 
210     /* Free all used resources.  */
211     _ux_utility_memory_free(control_buffer);
212 
213     /* Unprotect thread reentry to this instance.  */
214     _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
215 
216     /* Return completion status.  */
217     return(status);
218 }
219 
220 /**************************************************************************/
221 /*                                                                        */
222 /*  FUNCTION                                               RELEASE        */
223 /*                                                                        */
224 /*    _uxe_host_class_video_control_get                   PORTABLE C      */
225 /*                                                           6.3.0        */
226 /*  AUTHOR                                                                */
227 /*                                                                        */
228 /*    Yajun Xia, Microsoft Corporation                                    */
229 /*                                                                        */
230 /*  DESCRIPTION                                                           */
231 /*                                                                        */
232 /*    This function checks errors in video control get function call.     */
233 /*                                                                        */
234 /*  INPUT                                                                 */
235 /*                                                                        */
236 /*    video                                 Pointer to video class        */
237 /*    video_control                         Pointer to video control      */
238 /*                                                                        */
239 /*  OUTPUT                                                                */
240 /*                                                                        */
241 /*    Completion Status                                                   */
242 /*                                                                        */
243 /*  CALLS                                                                 */
244 /*                                                                        */
245 /*    _ux_host_class_video_control_get      Get the video control         */
246 /*                                                                        */
247 /*  CALLED BY                                                             */
248 /*                                                                        */
249 /*    Application                                                         */
250 /*    Video Class                                                         */
251 /*                                                                        */
252 /*  RELEASE HISTORY                                                       */
253 /*                                                                        */
254 /*    DATE              NAME                      DESCRIPTION             */
255 /*                                                                        */
256 /*  10-31-2023        Yajun xia             Initial Version 6.3.0         */
257 /*                                                                        */
258 /**************************************************************************/
_uxe_host_class_video_control_get(UX_HOST_CLASS_VIDEO * video,UX_HOST_CLASS_VIDEO_CONTROL * video_control)259 UINT  _uxe_host_class_video_control_get(UX_HOST_CLASS_VIDEO *video, UX_HOST_CLASS_VIDEO_CONTROL *video_control)
260 {
261     /* Sanity Checks.  */
262     if ((video == UX_NULL) || (video_control == UX_NULL))
263         return(UX_INVALID_PARAMETER);
264 
265     /* Call the actual video control get function.  */
266     return(_ux_host_class_video_control_get(video, video_control));
267 }
268