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 /**   Video 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_video.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_video_frame_interval_get             PORTABLE C      */
38 /*                                                           6.1.2        */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function finds the frame intervals within the frame.           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    video                                 Pointer to video class        */
50 /*    interval_parameter                    Interval request structure    */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_system_error_handler              Log system error              */
59 /*    _ux_utility_descriptor_parse          Parse descriptor              */
60 /*    _ux_utility_long_get                  Get 32-bit value              */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    Video Class                                                         */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*  11-09-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            fixed compile warnings 64b, */
75 /*                                            resulting in version 6.1.2  */
76 /*                                                                        */
77 /**************************************************************************/
_ux_host_class_video_frame_interval_get(UX_HOST_CLASS_VIDEO * video,UX_HOST_CLASS_VIDEO_PARAMETER_FRAME_INTERVAL * interval_parameter)78 UINT  _ux_host_class_video_frame_interval_get(UX_HOST_CLASS_VIDEO *video, UX_HOST_CLASS_VIDEO_PARAMETER_FRAME_INTERVAL *interval_parameter)
79 {
80 
81 UCHAR                                           *descriptor;
82 UX_HOST_CLASS_VIDEO_FRAME_DESCRIPTOR            frame_descriptor;
83 ULONG                                           total_descriptor_length;
84 ULONG                                           descriptor_length;
85 ULONG                                           descriptor_type;
86 ULONG                                           descriptor_subtype;
87 ULONG                                           intervals_to_copy;
88 ULONG                                           i;
89 
90     /* Get the descriptor to the selected format.  */
91     descriptor =  video -> ux_host_class_video_current_format_address;
92     total_descriptor_length =  video -> ux_host_class_video_length_formats;
93 
94     /* Descriptors are arranged in order. First FORMAT then FRAME.  */
95     while (total_descriptor_length)
96     {
97 
98         /* Gather the length, type and subtype of the descriptor.  */
99         descriptor_length =   *descriptor;
100         descriptor_type =     *(descriptor + 1);
101         descriptor_subtype =  *(descriptor + 2);
102 
103         /* Make sure this descriptor has at least the minimum length.  */
104         if (descriptor_length < 3)
105         {
106 
107             /* Error trap. */
108             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
109 
110             /* If trace is enabled, insert this event into the trace buffer.  */
111             //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
112 
113             return(UX_DESCRIPTOR_CORRUPTED);
114         }
115 
116         /* Must be CS_INTERFACE.  */
117         if (descriptor_type == UX_HOST_CLASS_VIDEO_CS_INTERFACE)
118         {
119 
120             /* Process relative to descriptor type.  */
121             switch (descriptor_subtype)
122             {
123 
124                 case UX_HOST_CLASS_VIDEO_VS_FRAME_UNCOMPRESSED  :
125                 case UX_HOST_CLASS_VIDEO_VS_FRAME_MJPEG         :
126                 case UX_HOST_CLASS_VIDEO_VS_FRAME_FRAME_BASED   :
127 
128                     /* We found a Frame descriptor.  Is it the right one ? */
129                     if (interval_parameter -> ux_host_class_video_parameter_frame_requested == *(descriptor + 3))
130                     {
131 
132                         /* Make the descriptor machine independent.  */
133                         _ux_utility_descriptor_parse(descriptor, _ux_system_class_video_frame_descriptor_structure,
134                                                      UX_HOST_CLASS_VIDEO_FRAME_DESCRIPTOR_ENTRIES, (UCHAR *) &frame_descriptor);
135 
136                         /* Check the frame interval type.  */
137                         if (frame_descriptor.bFrameIntervalType == 0)
138                         {
139 
140                             /* Frame type is continuous, copy Min, Max and Step.  */
141                             intervals_to_copy = 3;
142                         }
143                         else
144                         {
145 
146                             /* Frame type is discrete, copy number of frame intervals.  */
147                             intervals_to_copy = frame_descriptor.bFrameIntervalType;
148                         }
149 
150                         /* Check if we have enough space to copy.  */
151                         if (intervals_to_copy * sizeof(ULONG) > interval_parameter -> ux_host_class_video_parameter_frame_interval_buffer_length)
152                         {
153                             intervals_to_copy = (ULONG)(interval_parameter -> ux_host_class_video_parameter_frame_interval_buffer_length / sizeof(ULONG));
154                         }
155 
156                         /* Return bytes copied.  */
157                         interval_parameter -> ux_host_class_video_parameter_frame_interval_buffer_length_written = (ULONG)(intervals_to_copy * sizeof(ULONG));
158 
159                         /* Loop to copy interval data.  */
160                         for (i = 0; i < intervals_to_copy; i++)
161                         {
162 
163                             /* Copy intervals to user buffer.  */
164                             interval_parameter -> ux_host_class_video_parameter_frame_interval_buffer[i] =
165                                 _ux_utility_long_get(descriptor + 26 + i * sizeof(ULONG));
166                         }
167 
168                         /* We are done here. */
169                         return(UX_SUCCESS);
170                     }
171 
172                     break;
173 
174             }
175         }
176 
177         /* Verify if the descriptor is still valid.  */
178         if (descriptor_length > total_descriptor_length)
179         {
180 
181             /* Error trap. */
182             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
183 
184             /* If trace is enabled, insert this event into the trace buffer.  */
185             //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
186 
187             return(UX_DESCRIPTOR_CORRUPTED);
188         }
189 
190         /* Jump to the next descriptor if we have not reached the end.  */
191         descriptor +=  descriptor_length;
192 
193         /* And adjust the length left to parse in the descriptor.  */
194         total_descriptor_length -=  descriptor_length;
195     }
196 
197     /* Error trap. */
198     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_VIDEO_WRONG_TYPE);
199 
200     /* We get here when either the report descriptor has a problem or we could
201        not find the right video device.  */
202     return(UX_HOST_CLASS_VIDEO_WRONG_TYPE);
203 }
204 
205