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_frame_parameters_set           PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function sets the video parameters for the video device.       */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    video                                 Pointer to video class        */
49 /*    frame_format                          Video format to use           */
50 /*    width                                 Video frame width             */
51 /*    height                                Video frame height            */
52 /*    frame_interval                        Video frame interval          */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_class_video_format_data_get  Get format data               */
61 /*    _ux_host_class_video_frame_data_get   Get frame data                */
62 /*    _ux_host_stack_transfer_request       Process transfer request      */
63 /*    _ux_utility_descriptor_parse          Parse descriptor              */
64 /*    _ux_utility_memory_allocate           Allocate memory block         */
65 /*    _ux_utility_memory_free               Release memory block          */
66 /*    _ux_host_semaphore_get                Get semaphore                 */
67 /*    _ux_host_semaphore_put                Put semaphore                 */
68 /*    _ux_utility_long_get                  Read 32-bit value             */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Application                                                         */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
79 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            resulting in version 6.1    */
81 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            validated max payload size, */
83 /*                                            resulting in version 6.1.9  */
84 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
85 /*                                            refined macros names,       */
86 /*                                            resulting in version 6.1.10 */
87 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
88 /*                                            internal clean up,          */
89 /*                                            fixed standalone compile,   */
90 /*                                            resulting in version 6.1.11 */
91 /*  10-31-2023     Yajun xia, CQ Xiao       Modified comment(s),          */
92 /*                                            improved interval checking, */
93 /*                                            resulting in version 6.3.0  */
94 /*                                                                        */
95 /**************************************************************************/
_ux_host_class_video_frame_parameters_set(UX_HOST_CLASS_VIDEO * video,ULONG frame_format,ULONG width,ULONG height,ULONG frame_interval)96 UINT  _ux_host_class_video_frame_parameters_set(UX_HOST_CLASS_VIDEO *video, ULONG frame_format, ULONG width, ULONG height, ULONG frame_interval)
97 {
98 
99 UX_HOST_CLASS_VIDEO_PARAMETER_FORMAT_DATA       format_parameter;
100 UX_HOST_CLASS_VIDEO_PARAMETER_FRAME_DATA        frame_parameter;
101 ULONG                                           format_index;
102 ULONG                                           frame_index;
103 UINT                                            status;
104 ULONG                                           min_frame_interval;
105 ULONG                                           max_frame_interval;
106 ULONG                                           frame_interval_step;
107 ULONG                                           i;
108 UX_ENDPOINT                                     *control_endpoint;
109 UX_TRANSFER                                     *transfer_request;
110 UINT                                            streaming_interface;
111 UCHAR                                           *control_buffer;
112 ULONG                                           max_payload_size;
113 
114 
115     /* Find the requested frame format.  */
116     for (format_index = 1; format_index <= video -> ux_host_class_video_number_formats; format_index++)
117     {
118 
119         /* Get format data for current format index.  */
120         format_parameter.ux_host_class_video_parameter_format_requested = format_index;
121         status = _ux_host_class_video_format_data_get(video, &format_parameter);
122         if (status != UX_SUCCESS)
123         {
124             return(status);
125         }
126 
127         /* Check if the format type is the one requested.  */
128         if (format_parameter.ux_host_class_video_parameter_format_subtype == frame_format)
129         {
130 
131             /* Save number of frames in the video instance.  */
132             video -> ux_host_class_video_number_frames = format_parameter.ux_host_class_video_parameter_number_frame_descriptors;
133 
134             /* The format is found and break the loop.  */
135             break;
136         }
137     }
138 
139     /* Check if the requested format is found.  */
140     if (format_index > video -> ux_host_class_video_number_formats)
141     {
142         return(UX_HOST_CLASS_VIDEO_PARAMETER_ERROR);
143     }
144 
145     /* Find the requested frame resolution.  */
146     for (frame_index = 1; frame_index <= video -> ux_host_class_video_number_frames; frame_index++)
147     {
148 
149         /* Get frame data for current frame index.  */
150         frame_parameter.ux_host_class_video_parameter_frame_requested = frame_index;
151         status = _ux_host_class_video_frame_data_get(video, &frame_parameter);
152         if (status != UX_SUCCESS)
153         {
154             return(status);
155         }
156 
157         /* Check the frame resolution.  */
158         if (frame_parameter.ux_host_class_video_parameter_frame_width == width &&
159             frame_parameter.ux_host_class_video_parameter_frame_height == height)
160         {
161 
162             /* Save the current frame index.  */
163             video -> ux_host_class_video_current_frame = frame_index;
164 
165             /* The requested resolution is found, break the loop.  */
166             break;
167         }
168     }
169 
170     /* Check if the requested resolution is found.  */
171     if (frame_index > video -> ux_host_class_video_number_frames)
172     {
173         return(UX_HOST_CLASS_VIDEO_PARAMETER_ERROR);
174     }
175 
176     /* Initial status for frame interval checking.  */
177     status = UX_HOST_CLASS_VIDEO_PARAMETER_ERROR;
178 
179     /* Check the frame interval type.  */
180     if (frame_parameter.ux_host_class_video_parameter_frame_interval_type == 0)
181     {
182 
183         /* Frame interval type is continuous.  */
184         min_frame_interval = _ux_utility_long_get(frame_parameter.ux_host_class_video_parameter_frame_intervals);
185         max_frame_interval = _ux_utility_long_get(frame_parameter.ux_host_class_video_parameter_frame_intervals + 4);
186         frame_interval_step = _ux_utility_long_get(frame_parameter.ux_host_class_video_parameter_frame_intervals + 8);
187 
188         /* Check if the frame interval is valid.  */
189         if (frame_interval >= min_frame_interval && frame_interval <= max_frame_interval &&
190            ((frame_interval_step == 0) ||
191             (frame_interval - min_frame_interval) % frame_interval_step == 0))
192         {
193 
194             /* Save the frame interval.  */
195             video -> ux_host_class_video_current_frame_interval = frame_interval;
196             status = UX_SUCCESS;
197         }
198     }
199     else
200     {
201 
202         /* Frame interval type is discrete.  */
203         for(i = 0; i < frame_parameter.ux_host_class_video_parameter_frame_interval_type; i++)
204         {
205 
206             /* Check if the requested frame interval is valid.  */
207             if (frame_interval == _ux_utility_long_get(frame_parameter.ux_host_class_video_parameter_frame_intervals + i * sizeof(ULONG)))
208             {
209 
210                 /* Save the frame interval.  */
211                 video -> ux_host_class_video_current_frame_interval = frame_interval;
212                 status = UX_SUCCESS;
213             }
214         }
215     }
216 
217     if (status != UX_SUCCESS)
218         return(UX_HOST_CLASS_VIDEO_PARAMETER_ERROR);
219 
220     /* Protect thread reentry to this instance.  */
221     status =  _ux_host_semaphore_get(&video -> ux_host_class_video_semaphore, UX_WAIT_FOREVER);
222     if (status != UX_SUCCESS)
223         return(status);
224 
225     /* We need to get the default control endpoint transfer request pointer.  */
226     control_endpoint =  &video -> ux_host_class_video_device -> ux_device_control_endpoint;
227     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
228 
229     /* Get the interface number of the video streaming interface.  */
230     streaming_interface =  video -> ux_host_class_video_streaming_interface -> ux_interface_descriptor.bInterfaceNumber;
231 
232     /* Need to allocate memory for the control_buffer.  */
233     control_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_VIDEO_PROBE_COMMIT_LENGTH);
234     if (control_buffer == UX_NULL)
235     {
236 
237         /* Unprotect thread reentry to this instance.  */
238         _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
239 
240         /* Return error.  */
241         return(UX_MEMORY_INSUFFICIENT);
242     }
243 
244     *(control_buffer + UX_HOST_CLASS_VIDEO_PROBE_COMMIT_FORMAT_INDEX) = (UCHAR)format_index;
245     *(control_buffer + UX_HOST_CLASS_VIDEO_PROBE_COMMIT_FRAME_INDEX) = (UCHAR)frame_index;
246     _ux_utility_long_put(control_buffer + UX_HOST_CLASS_VIDEO_PROBE_COMMIT_FRAME_INTERVAL, frame_interval);
247 
248     /* Create a transfer request for the SET_CUR buffer request.  */
249     transfer_request -> ux_transfer_request_data_pointer =      control_buffer;
250     transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_VIDEO_PROBE_COMMIT_LENGTH;
251     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_VIDEO_SET_CUR;
252     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
253     transfer_request -> ux_transfer_request_value =             UX_HOST_CLASS_VIDEO_VS_PROBE_CONTROL << 8;
254     transfer_request -> ux_transfer_request_index =             streaming_interface;
255 
256     /* Send request to HCD layer.  */
257     status =  _ux_host_stack_transfer_request(transfer_request);
258 
259     /* Check for correct transfer. Buffer may not be all what we asked for.  */
260     if (status == UX_SUCCESS)
261     {
262 
263 
264         /* Create a transfer request for the GET_CUR buffer request.  */
265         transfer_request -> ux_transfer_request_data_pointer =      control_buffer;
266         transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_VIDEO_PROBE_COMMIT_LENGTH;
267         transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_VIDEO_GET_CUR;
268         transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
269         transfer_request -> ux_transfer_request_value =             UX_HOST_CLASS_VIDEO_VS_PROBE_CONTROL << 8;
270         transfer_request -> ux_transfer_request_index =             streaming_interface;
271 
272         /* Send request to HCD layer.  */
273         status =  _ux_host_stack_transfer_request(transfer_request);
274 
275     }
276 
277     /* Get the max payload transfer size returned from video device.  */
278     max_payload_size = _ux_utility_long_get(control_buffer + UX_HOST_CLASS_VIDEO_PROBE_COMMIT_MAX_PAYLOAD_TRANSFER_SIZE);
279 
280     /* Validate if the payload size is inside isochronouse packet payload.  */
281     if (max_payload_size == 0)
282         status = UX_HOST_CLASS_VIDEO_PARAMETER_ERROR;
283     else
284     {
285         if (video -> ux_host_class_video_device -> ux_device_speed != UX_HIGH_SPEED_DEVICE)
286         {
287             if (max_payload_size > 1023)
288                 status = UX_HOST_CLASS_VIDEO_PARAMETER_ERROR;
289         }
290         else
291         {
292             if (max_payload_size > (1024 * 3))
293                 status = UX_HOST_CLASS_VIDEO_PARAMETER_ERROR;
294         }
295     }
296 
297     /* Free all used resources.  */
298     _ux_utility_memory_free(control_buffer);
299 
300     /* Unprotect thread reentry to this instance.  */
301     _ux_host_semaphore_put(&video -> ux_host_class_video_semaphore);
302 
303     /* Check the transfer status.  */
304     if (status == UX_SUCCESS)
305     {
306 
307         /* Save the maximum payload size returned by the device.  */
308         video -> ux_host_class_video_current_max_payload_size = max_payload_size;
309         return(UX_SUCCESS);
310     }
311     else
312     {
313 
314         /* The probe process failed. Return error.  */
315         return(UX_HOST_CLASS_VIDEO_PARAMETER_ERROR);
316     }
317 }
318 
319 /**************************************************************************/
320 /*                                                                        */
321 /*  FUNCTION                                               RELEASE        */
322 /*                                                                        */
323 /*    _uxe_host_class_video_frame_parameters_set          PORTABLE C      */
324 /*                                                           6.3.0        */
325 /*  AUTHOR                                                                */
326 /*                                                                        */
327 /*    Yajun Xia, Microsoft Corporation                                    */
328 /*                                                                        */
329 /*  DESCRIPTION                                                           */
330 /*                                                                        */
331 /*    This function checks errors in video frame parameters set function  */
332 /*    call.                                                               */
333 /*                                                                        */
334 /*  INPUT                                                                 */
335 /*                                                                        */
336 /*    video                                 Pointer to video class        */
337 /*    frame_format                          Video format to use           */
338 /*    width                                 Video frame width             */
339 /*    height                                Video frame height            */
340 /*    frame_interval                        Video frame interval          */
341 /*                                                                        */
342 /*  OUTPUT                                                                */
343 /*                                                                        */
344 /*    Completion Status                                                   */
345 /*                                                                        */
346 /*  CALLS                                                                 */
347 /*                                                                        */
348 /*    _ux_host_class_video_frame_parameters_set                           */
349 /*                                          Video frame parameters set    */
350 /*                                                                        */
351 /*  CALLED BY                                                             */
352 /*                                                                        */
353 /*    Application                                                         */
354 /*                                                                        */
355 /*  RELEASE HISTORY                                                       */
356 /*                                                                        */
357 /*    DATE              NAME                      DESCRIPTION             */
358 /*                                                                        */
359 /*  10-31-2023        Yajun xia             Initial Version 6.3.0         */
360 /*                                                                        */
361 /**************************************************************************/
_uxe_host_class_video_frame_parameters_set(UX_HOST_CLASS_VIDEO * video,ULONG frame_format,ULONG width,ULONG height,ULONG frame_interval)362 UINT  _uxe_host_class_video_frame_parameters_set(UX_HOST_CLASS_VIDEO *video, ULONG frame_format, ULONG width, ULONG height, ULONG frame_interval)
363 {
364 
365     /* Sanity checks.  */
366     if (video == UX_NULL)
367         return(UX_INVALID_PARAMETER);
368 
369     /* Call the actual video frame parameters set function.  */
370     return(_ux_host_class_video_frame_parameters_set(video, frame_format, width, height, frame_interval));
371 }
372