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_configure                      PORTABLE C      */
38 /*                                                           6.1.11       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*     This function calls the USBX stack to do a SET_CONFIGURATION to    */
46 /*     the device. Once the device is configured, its interface(s) will   */
47 /*     be activated.                                                      */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    video                                 Pointer to video class        */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_configuration_interface_get                          */
60 /*                                          Get interface                 */
61 /*    _ux_host_stack_device_configuration_get                             */
62 /*                                          Get configuration             */
63 /*    _ux_host_stack_device_configuration_select                          */
64 /*                                          Select configuration          */
65 /*    _ux_system_error_handler              Log system error              */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
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 /*                                            optimized based on compile  */
78 /*                                            definitions,                */
79 /*                                            resulting in version 6.1    */
80 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            internal clean up,          */
82 /*                                            resulting in version 6.1.11 */
83 /*                                                                        */
84 /**************************************************************************/
_ux_host_class_video_configure(UX_HOST_CLASS_VIDEO * video)85 UINT  _ux_host_class_video_configure(UX_HOST_CLASS_VIDEO *video)
86 {
87 
88 UINT                    status;
89 UX_CONFIGURATION        *configuration;
90 UX_INTERFACE            *interface;
91 ULONG                   interface_number;
92 #if UX_MAX_DEVICES > 1
93 UX_DEVICE               *parent_device;
94 #endif
95 
96 
97     /* If the device has been configured already, we don't need to do it
98        again. */
99     if (video -> ux_host_class_video_device -> ux_device_state == UX_DEVICE_CONFIGURED)
100         return(UX_SUCCESS);
101 
102     /* An video device normally has one configuration. So retrieve the 1st configuration
103        only.  */
104     status =  _ux_host_stack_device_configuration_get(video -> ux_host_class_video_device, 0, &configuration);
105     if (status != UX_SUCCESS)
106     {
107 
108         /* Error trap. */
109         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
110 
111         /* If trace is enabled, insert this event into the trace buffer.  */
112         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, video -> ux_host_class_video_device, 0, 0, UX_TRACE_ERRORS, 0, 0)
113 
114         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
115 
116     }
117 
118 #if UX_MAX_DEVICES > 1
119     /* Check the video power source and check the parent power source for
120        incompatible connections.  */
121     if (video -> ux_host_class_video_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED)
122     {
123 
124         /* Get parent device.  */
125         parent_device =  video -> ux_host_class_video_device -> ux_device_parent;
126 
127         /* If the device is NULL, the parent is the root video and we don't have to worry
128            if the parent is not the root video, check for its power source.  */
129         if ((parent_device != UX_NULL) && (parent_device -> ux_device_power_source == UX_DEVICE_BUS_POWERED))
130         {
131 
132             /* Error trap. */
133             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
134 
135             /* If trace is enabled, insert this event into the trace buffer.  */
136             //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONNECTION_INCOMPATIBLE, video, 0, 0, UX_TRACE_ERRORS, 0, 0)
137 
138             return(UX_CONNECTION_INCOMPATIBLE);
139         }
140     }
141 #endif
142 
143     /* We have the valid configuration. Ask the USBX stack to set this configuration */
144     status =  _ux_host_stack_device_configuration_select(configuration);
145     if (status != UX_SUCCESS)
146         return(status);
147 
148     /* Start with interface number 0.  */
149     interface_number =  0;
150 
151     /* We only need to retrieve the video streaming interface.  */
152     do
153     {
154 
155         /* Pickup interface.  */
156         status =  _ux_host_stack_configuration_interface_get(configuration, (UINT) interface_number, 0, &interface);
157 
158         /* Check completion status.  */
159         if (status == UX_SUCCESS)
160         {
161 
162             /* Check the type of interface we have found - is it streaming?  */
163             if (interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_VIDEO_SUBCLASS_STREAMING)
164             {
165 
166                 video -> ux_host_class_video_streaming_interface =  interface;
167                 video -> ux_host_class_video_streaming_interface -> ux_interface_class_instance =  (VOID *)video;
168                 break;
169             }
170         }
171 
172         /* Move to next interface.  */
173         interface_number++;
174     } while(status == UX_SUCCESS);
175 
176     /* After we have parsed the video interfaces, ensure the streaming interfaces is resent.  */
177     if (video -> ux_host_class_video_streaming_interface == UX_NULL)
178     {
179 
180         /* Error trap. */
181         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_INTERFACE_HANDLE_UNKNOWN);
182 
183         /* If trace is enabled, insert this event into the trace buffer.  */
184         //UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_INTERFACE_HANDLE_UNKNOWN, interface, 0, 0, UX_TRACE_ERRORS, 0, 0)
185 
186         /* We get here when we could not locate the interface(s) handle */
187         return(UX_INTERFACE_HANDLE_UNKNOWN);
188     }
189 
190     return(UX_SUCCESS);
191 
192 }
193 
194