1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Host Stack                                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_host_stack_configuration_interface_get          PORTABLE C      */
36 /*                                                           6.1.12       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function returns an interface container based on a             */
44 /*    configuration handle, an interface index and an alternate setting   */
45 /*    index.                                                              */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    configuration                         Pointer to configuration      */
50 /*    interface_index                       Index of interface            */
51 /*    alternate_setting_index               Index of alternate setting    */
52 /*    interface                             Destination of interface      */
53 /*                                            pointer                     */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    None                                                                */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application                                                         */
66 /*    USBX Components                                                     */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            resulting in version 6.1    */
75 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            fixed parameter/variable    */
77 /*                                            names conflict C++ keyword, */
78 /*                                            resulting in version 6.1.12 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_stack_configuration_interface_get(UX_CONFIGURATION * configuration,UINT interface_index,UINT alternate_setting_index,UX_INTERFACE ** ux_interface)81 UINT  _ux_host_stack_configuration_interface_get(UX_CONFIGURATION *configuration,
82                                                 UINT interface_index, UINT alternate_setting_index,
83                                                 UX_INTERFACE **ux_interface)
84 {
85 
86 UINT                current_interface_number;
87 UINT                container_index;
88 UX_INTERFACE        *current_interface;
89 
90 
91     /* Do a sanity check on the configuration handle.  */
92     if (configuration -> ux_configuration_handle != (ULONG) (ALIGN_TYPE) configuration)
93     {
94 
95         /* If trace is enabled, insert this event into the trace buffer.  */
96         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
97 
98         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
99     }
100 
101     /* Start with the interface attached to the configuration.  */
102     current_interface =  configuration -> ux_configuration_first_interface;
103 
104     /* The first interface has the index 0 */
105     container_index =  0;
106 
107     /* Reset the interface number */
108     current_interface_number =  0;
109 
110     /* Traverse the list of the interfaces until we found the right one */
111     while (current_interface != UX_NULL)
112     {
113 
114         /* Check if the interface index matches the current one.  */
115         if (interface_index == container_index)
116         {
117 
118             /* We have found the correct interface, now search for the alternate setting.  */
119             current_interface_number =  current_interface -> ux_interface_descriptor.bInterfaceNumber;
120 
121             /* The first alternate setting has the index 0.  */
122             container_index =  0;
123 
124             /* Loop on all the alternate settings for this interface.  */
125             while (current_interface != UX_NULL)
126             {
127 
128                 /* Check if the index is matched */
129                 if (alternate_setting_index == container_index)
130                 {
131 
132                     /* We have found the right interface/alternate setting combination. Set the
133                        interface return pointer.  */
134                     *ux_interface =  current_interface;
135 
136                     /* Return success to caller.  */
137                     return(UX_SUCCESS);
138                 }
139 
140                 /* Move to next alternate setting index.  */
141                 container_index++;
142 
143                 /* Move to the next alternate setting.   */
144                 current_interface =  current_interface -> ux_interface_next_interface;
145 
146 
147                 /* Check new interface pointer, might be the end.  */
148                 if (current_interface != UX_NULL)
149                 {
150 
151                     /* And verify that we are still in the same interface.  */
152                     if (current_interface -> ux_interface_descriptor.bInterfaceNumber != current_interface_number)
153                     {
154 
155                         /* Error trap. */
156                         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_INTERFACE_HANDLE_UNKNOWN);
157 
158                         /* If trace is enabled, insert this event into the trace buffer.  */
159                         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_INTERFACE_HANDLE_UNKNOWN, ux_interface, 0, 0, UX_TRACE_ERRORS, 0, 0)
160 
161                         return(UX_INTERFACE_HANDLE_UNKNOWN);
162                     }
163                 }
164             }
165         }
166 
167         /* Check the current interface, we may already be at the end ... */
168         if (current_interface != UX_NULL)
169         {
170 
171             /* Move to the next interface.  */
172             current_interface =  current_interface -> ux_interface_next_interface;
173 
174             /* Move to the next interface index. */
175             container_index++;
176         }
177     }
178 
179     /* Error trap. */
180     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_INTERFACE_HANDLE_UNKNOWN);
181 
182     /* If trace is enabled, insert this event into the trace buffer.  */
183     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_INTERFACE_HANDLE_UNKNOWN, ux_interface, 0, 0, UX_TRACE_ERRORS, 0, 0)
184 
185     /* Didn't find the right interface/alternate setting, return an error!  */
186     return(UX_INTERFACE_HANDLE_UNKNOWN);
187 }
188 
189 
190 /**************************************************************************/
191 /*                                                                        */
192 /*  FUNCTION                                               RELEASE        */
193 /*                                                                        */
194 /*    _uxe_host_stack_configuration_interface_get         PORTABLE C      */
195 /*                                                           6.3.0        */
196 /*  AUTHOR                                                                */
197 /*                                                                        */
198 /*    Chaoqiong Xiao, Microsoft Corporation                               */
199 /*                                                                        */
200 /*  DESCRIPTION                                                           */
201 /*                                                                        */
202 /*    This function checks errors in host stack interface get function    */
203 /*    call.                                                               */
204 /*                                                                        */
205 /*  INPUT                                                                 */
206 /*                                                                        */
207 /*    configuration                         Pointer to configuration      */
208 /*    interface_index                       Index of interface            */
209 /*    alternate_setting_index               Index of alternate setting    */
210 /*    interface                             Destination of interface      */
211 /*                                                                        */
212 /*  OUTPUT                                                                */
213 /*                                                                        */
214 /*    None                                                                */
215 /*                                                                        */
216 /*  CALLS                                                                 */
217 /*                                                                        */
218 /*    _ux_host_stack_configuration_interface_get                          */
219 /*                                          Host stack interface get      */
220 /*                                                                        */
221 /*  CALLED BY                                                             */
222 /*                                                                        */
223 /*    Application                                                         */
224 /*                                                                        */
225 /*  RELEASE HISTORY                                                       */
226 /*                                                                        */
227 /*    DATE              NAME                      DESCRIPTION             */
228 /*                                                                        */
229 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
230 /*                                                                        */
231 /**************************************************************************/
_uxe_host_stack_configuration_interface_get(UX_CONFIGURATION * configuration,UINT interface_index,UINT alternate_setting_index,UX_INTERFACE ** ux_interface)232 UINT  _uxe_host_stack_configuration_interface_get(UX_CONFIGURATION *configuration,
233                                                 UINT interface_index, UINT alternate_setting_index,
234                                                 UX_INTERFACE **ux_interface)
235 {
236 
237     /* Sanity checks.  */
238     if ((configuration == UX_NULL) || (ux_interface == UX_NULL))
239         return(UX_INVALID_PARAMETER);
240 
241     /* Invoke interface get function.  */
242     return(_ux_host_stack_configuration_interface_get(configuration,
243                     interface_index, alternate_setting_index, ux_interface));
244 }
245