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_input_format_get               PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function finds the input format(s) for the input terminal.     */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    video                                 Pointer to video class        */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_utility_descriptor_parse          Parse descriptor              */
58 /*    _ux_system_error_handler              Log system error              */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Video Class                                                         */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
69 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            resulting in version 6.1    */
71 /*                                                                        */
72 /**************************************************************************/
_ux_host_class_video_input_format_get(UX_HOST_CLASS_VIDEO * video)73 UINT  _ux_host_class_video_input_format_get(UX_HOST_CLASS_VIDEO *video)
74 {
75 
76 UCHAR                                           *descriptor;
77 UX_INTERFACE_DESCRIPTOR                         interface_descriptor;
78 UX_HOST_CLASS_VIDEO_INPUT_HEADER_DESCRIPTOR     input_header_descriptor;
79 ULONG                                           total_descriptor_length;
80 ULONG                                           descriptor_length;
81 ULONG                                           descriptor_type;
82 ULONG                                           descriptor_subtype;
83 ULONG                                           descriptor_found;
84 
85 
86     /* Get the descriptor to the entire configuration.  */
87     descriptor =               video -> ux_host_class_video_configuration_descriptor;
88     total_descriptor_length =  video -> ux_host_class_video_configuration_descriptor_length;
89 
90     /* Default is Interface descriptor not yet found.  */
91     descriptor_found =  UX_FALSE;
92 
93     /* Scan the descriptor for the Video Streaming interface.  */
94     while (total_descriptor_length)
95     {
96 
97         /* Gather the length, type and subtype of the descriptor.  */
98         descriptor_length =   *descriptor;
99         descriptor_type =     *(descriptor + 1);
100         descriptor_subtype =  *(descriptor + 2);
101 
102         /* Make sure this descriptor has at least the minimum length.  */
103         if (descriptor_length < 3)
104         {
105 
106             /* Error trap. */
107             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
108 
109             /* If trace is enabled, insert this event into the trace buffer.  */
110             //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
111 
112             return(UX_DESCRIPTOR_CORRUPTED);
113         }
114 
115         /* Process relative to the descriptor type.  */
116         switch (descriptor_type)
117         {
118 
119 
120         case UX_INTERFACE_DESCRIPTOR_ITEM:
121 
122             /* Parse the interface descriptor and make it machine independent.  */
123             _ux_utility_descriptor_parse(descriptor, _ux_system_interface_descriptor_structure,
124                                             UX_INTERFACE_DESCRIPTOR_ENTRIES, (UCHAR *) &interface_descriptor);
125 
126             /* Ensure we have the correct interface for Video Streaming.  */
127             if ((interface_descriptor.bInterfaceClass == UX_HOST_CLASS_VIDEO_CLASS) &&
128                 (interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_VIDEO_SUBCLASS_STREAMING))
129             {
130 
131                 /* Mark we have found it.  */
132                 descriptor_found =  UX_TRUE;
133             }
134             else
135             {
136 
137                 descriptor_found =  UX_FALSE;
138             }
139             break;
140 
141 
142         case UX_HOST_CLASS_VIDEO_CS_INTERFACE:
143 
144             /* First make sure we have found the correct generic interface descriptor.  */
145             if (descriptor_found == UX_TRUE)
146             {
147 
148                 /* Check the sub type.  */
149                 switch (descriptor_subtype)
150                 {
151 
152 
153                 case UX_HOST_CLASS_VIDEO_VS_INPUT_HEADER:
154 
155                     /* Make the descriptor machine independent.  */
156                     _ux_utility_descriptor_parse(descriptor, _ux_system_class_video_input_header_descriptor_structure,
157                                                  UX_HOST_CLASS_VIDEO_INPUT_HEADER_DESCRIPTOR_ENTRIES, (UCHAR *) &input_header_descriptor);
158 
159                     /* Get the number of formats.  */
160                     video -> ux_host_class_video_number_formats = input_header_descriptor.bNumFormats;
161 
162                     /* Get the length of formats.  */
163                     video -> ux_host_class_video_length_formats = input_header_descriptor.wTotalLength;
164 
165                     /* Save the descriptor where the formats reside.  */
166                     video -> ux_host_class_video_format_address = descriptor;
167 
168                     /* We are done here.  */
169                     return(UX_SUCCESS);
170 
171                 }
172             }
173             break;
174         }
175 
176         /* Verify if the descriptor is still valid.  */
177         if (descriptor_length > total_descriptor_length)
178         {
179 
180             /* Error trap. */
181             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
182 
183             /* If trace is enabled, insert this event into the trace buffer.  */
184             //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
185 
186             return(UX_DESCRIPTOR_CORRUPTED);
187         }
188 
189         /* Jump to the next descriptor if we have not reached the end.  */
190         descriptor +=  descriptor_length;
191 
192         /* And adjust the length left to parse in the descriptor.  */
193         total_descriptor_length -=  descriptor_length;
194     }
195 
196     /* Error trap. */
197     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_VIDEO_WRONG_TYPE);
198 
199     /* We get here when either the report descriptor has a problem or we could
200        not find the right video device.  */
201     return(UX_HOST_CLASS_VIDEO_WRONG_TYPE);
202 }
203 
204