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_terminal_get             PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function finds the input terminal in the config descriptor.    */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    video                                 Pointer to video class        */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_system_error_handler              System error log              */
58 /*    _ux_utility_descriptor_parse          Parse descriptor              */
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_terminal_get(UX_HOST_CLASS_VIDEO * video)73 UINT  _ux_host_class_video_input_terminal_get(UX_HOST_CLASS_VIDEO *video)
74 {
75 
76 UCHAR                                           *descriptor;
77 UX_INTERFACE_DESCRIPTOR                         interface_descriptor;
78 UX_HOST_CLASS_VIDEO_INPUT_TERMINAL_DESCRIPTOR   input_terminal_descriptor;
79 ULONG                                           total_descriptor_length;
80 ULONG                                           descriptor_length;
81 ULONG                                           descriptor_type;
82 ULONG                                           descriptor_subtype;
83 ULONG                                           interface_found;
84 
85 
86     /* Get the descriptor to the selected format.  */
87     descriptor =  video -> ux_host_class_video_configuration_descriptor;
88     total_descriptor_length =  video -> ux_host_class_video_configuration_descriptor_length;
89 
90     /* Haven't found it yet.  */
91     interface_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 descriptor type.  */
116         switch (descriptor_type)
117         {
118 
119             case UX_INTERFACE_DESCRIPTOR_ITEM:
120 
121                 /* Parse the interface descriptor and make it machine independent.  */
122                 _ux_utility_descriptor_parse(descriptor, _ux_system_interface_descriptor_structure,
123                                              UX_INTERFACE_DESCRIPTOR_ENTRIES, (UCHAR *) &interface_descriptor);
124 
125                 /* Ensure we have the correct interface for Video Control.  */
126                 if ((interface_descriptor.bInterfaceClass == UX_HOST_CLASS_VIDEO_CLASS) &&
127                     (interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_VIDEO_SUBCLASS_CONTROL))
128                 {
129 
130                     /* Mark we have found it.  */
131                     interface_found =  UX_TRUE;
132 
133                     /* Get the interface number of this descriptor and save it in the video
134                        instance. This will be useful to program the video controls.  */
135                     video -> ux_host_class_video_control_interface_number =  interface_descriptor.bInterfaceNumber;
136                 }
137                 else
138                 {
139 
140                     /* Haven't found it.  */
141                     interface_found =  UX_FALSE;
142                 }
143                 break;
144 
145 
146             case UX_HOST_CLASS_VIDEO_CS_INTERFACE:
147 
148                 /* First make sure we have found the correct generic interface descriptor.  */
149                 if ((interface_found == UX_TRUE) && (descriptor_subtype == UX_HOST_CLASS_VIDEO_VC_INPUT_TERMINAL))
150                 {
151 
152                     /* Parse the interface descriptor and make it machine independent.  */
153                     _ux_utility_descriptor_parse(descriptor, _ux_system_class_video_input_terminal_descriptor_structure,
154                                                  UX_HOST_CLASS_VIDEO_INPUT_TERMINAL_DESCRIPTOR_ENTRIES, (UCHAR *) &input_terminal_descriptor);
155 
156                     /* Save the video terminal ID.  */
157                     video -> ux_host_class_video_terminal_id =  input_terminal_descriptor.bTerminalID;
158 
159                     /* Save the video terminal type.  */
160                     video -> ux_host_class_video_terminal_type =  input_terminal_descriptor.wTerminalType;
161 
162 
163                     /* We are done here.  */
164                     return(UX_SUCCESS);
165                 }
166 
167                 break;
168         }
169 
170         /* Verify if the descriptor is still valid.  */
171         if (descriptor_length > total_descriptor_length)
172         {
173 
174             /* Error trap. */
175             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_DESCRIPTOR_CORRUPTED);
176 
177             /* If trace is enabled, insert this event into the trace buffer.  */
178             //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DESCRIPTOR_CORRUPTED, descriptor, 0, 0, UX_TRACE_ERRORS, 0, 0)
179 
180             return(UX_DESCRIPTOR_CORRUPTED);
181         }
182 
183         /* Jump to the next descriptor if we have not reached the end.  */
184         descriptor +=  descriptor_length;
185 
186         /* And adjust the length left to parse in the descriptor.  */
187         total_descriptor_length -=  descriptor_length;
188     }
189 
190     /* Error trap. */
191     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_VIDEO_WRONG_TYPE);
192 
193     /* We get here when either the report descriptor has a problem or we could
194        not find the right video device.  */
195     return(UX_HOST_CLASS_VIDEO_WRONG_TYPE);
196 }
197 
198