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_alternate_setting_locate       PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function finds the alternate setting for the specific format.  */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    video                                 Pointer to video class        */
50 /*    alternate_setting                     Pointer to located alternate  */
51 /*                                            setting                     */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_system_error_handler              Log system error              */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Video Class                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            use pre-calculated value    */
74 /*                                            instead of wMaxPacketSize,  */
75 /*                                            resulting in version 6.1.9  */
76 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            fixed parameter/variable    */
78 /*                                            names conflict C++ keyword, */
79 /*                                            resulting in version 6.1.12 */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_video_alternate_setting_locate(UX_HOST_CLASS_VIDEO * video,UINT max_payload_size,UINT * alternate_setting)82 UINT  _ux_host_class_video_alternate_setting_locate(UX_HOST_CLASS_VIDEO *video, UINT max_payload_size, UINT *alternate_setting)
83 {
84 
85 UX_CONFIGURATION        *configuration;
86 UX_INTERFACE            *interface_ptr;
87 UX_ENDPOINT             *endpoint;
88 UINT                    streaming_interface;
89 UINT                    payload_size;
90 UINT                    current_payload_size = 0;
91 UINT                    alternate_setting_found;
92 
93 
94     configuration =        video -> ux_host_class_video_streaming_interface -> ux_interface_configuration;
95     interface_ptr =            configuration -> ux_configuration_first_interface;
96     streaming_interface =  video -> ux_host_class_video_streaming_interface -> ux_interface_descriptor.bInterfaceNumber;
97 
98     alternate_setting_found = UX_FALSE;
99 
100     /* Scan all interfaces.  */
101     while (interface_ptr != UX_NULL)
102     {
103 
104         /* Search for the streaming interface with a endpoint.  */
105         if ((interface_ptr -> ux_interface_descriptor.bInterfaceNumber == streaming_interface) &&
106             (interface_ptr -> ux_interface_first_endpoint != 0))
107         {
108 
109             /* Get the max packet size of the endpoint.  */
110             endpoint = interface_ptr -> ux_interface_first_endpoint;
111             payload_size = endpoint -> ux_endpoint_transfer_request.ux_transfer_request_packet_length;
112 
113             /* Check if the payload size is equal or greater than the required payload size.  */
114             if (payload_size >= max_payload_size)
115             {
116 
117                 /* Check if we have found any alternate settings yet.  */
118                 if(alternate_setting_found == UX_FALSE)
119                 {
120 
121                     /* Save the current payload size and mark the found flag.  */
122                     alternate_setting_found = UX_TRUE;
123                     current_payload_size = payload_size;
124 
125                     /* Save the found alternate setting number.  */
126                     *alternate_setting = interface_ptr -> ux_interface_descriptor.bAlternateSetting;
127                 }
128                 else
129                 {
130 
131                     /* Check if the payload size is smaller.  */
132                     if (payload_size < current_payload_size)
133                     {
134 
135                         /* Smaller payload size found, select the current setting.  */
136                         current_payload_size = payload_size;
137 
138                         /* Save the found alternate setting number.  */
139                         *alternate_setting = interface_ptr -> ux_interface_descriptor.bAlternateSetting;
140                     }
141                 }
142             }
143         }
144 
145         /* Move to next interface.  */
146         interface_ptr =  interface_ptr -> ux_interface_next_interface;
147     }
148 
149     /* Check if we found the alternate setting.  */
150     if (alternate_setting_found)
151     {
152 
153         /* Return successful completion.  */
154         return(UX_SUCCESS);
155     }
156 
157     /* Error trap. */
158     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_VIDEO_WRONG_TYPE);
159 
160     /* We get here when either the report descriptor has a problem or we could
161        not find the right video device.  */
162     return(UX_HOST_CLASS_VIDEO_WRONG_TYPE);
163 }
164