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 /**   Host Stack                                                          */
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_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_stack_configuration_interface_get          PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function returns an interface container based on a             */
45 /*    configuration handle, an interface index and an alternate setting   */
46 /*    index.                                                              */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    configuration                         Pointer to configuration      */
51 /*    interface_index                       Index of interface            */
52 /*    alternate_setting_index               Index of alternate setting    */
53 /*    interface                             Destination of interface      */
54 /*                                            pointer                     */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    None                                                                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application                                                         */
67 /*    USBX Components                                                     */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            resulting in version 6.1    */
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_stack_configuration_interface_get(UX_CONFIGURATION * configuration,UINT interface_index,UINT alternate_setting_index,UX_INTERFACE ** ux_interface)82 UINT  _ux_host_stack_configuration_interface_get(UX_CONFIGURATION *configuration,
83                                                 UINT interface_index, UINT alternate_setting_index,
84                                                 UX_INTERFACE **ux_interface)
85 {
86 
87 UINT                current_interface_number;
88 UINT                container_index;
89 UX_INTERFACE        *current_interface;
90 
91 
92     /* Do a sanity check on the configuration handle.  */
93     if (configuration -> ux_configuration_handle != (ULONG) (ALIGN_TYPE) configuration)
94     {
95 
96         /* If trace is enabled, insert this event into the trace buffer.  */
97         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, configuration, 0, 0, UX_TRACE_ERRORS, 0, 0)
98 
99         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
100     }
101 
102     /* Start with the interface attached to the configuration.  */
103     current_interface =  configuration -> ux_configuration_first_interface;
104 
105     /* The first interface has the index 0 */
106     container_index =  0;
107 
108     /* Reset the interface number */
109     current_interface_number =  0;
110 
111     /* Traverse the list of the interfaces until we found the right one */
112     while (current_interface != UX_NULL)
113     {
114 
115         /* Check if the interface index matches the current one.  */
116         if (interface_index == container_index)
117         {
118 
119             /* We have found the correct interface, now search for the alternate setting.  */
120             current_interface_number =  current_interface -> ux_interface_descriptor.bInterfaceNumber;
121 
122             /* The first alternate setting has the index 0.  */
123             container_index =  0;
124 
125             /* Loop on all the alternate settings for this interface.  */
126             while (current_interface != UX_NULL)
127             {
128 
129                 /* Check if the index is matched */
130                 if (alternate_setting_index == container_index)
131                 {
132 
133                     /* We have found the right interface/alternate setting combination. Set the
134                        interface return pointer.  */
135                     *ux_interface =  current_interface;
136 
137                     /* Return success to caller.  */
138                     return(UX_SUCCESS);
139                 }
140 
141                 /* Move to next alternate setting index.  */
142                 container_index++;
143 
144                 /* Move to the next alternate setting.   */
145                 current_interface =  current_interface -> ux_interface_next_interface;
146 
147 
148                 /* Check new interface pointer, might be the end.  */
149                 if (current_interface != UX_NULL)
150                 {
151 
152                     /* And verify that we are still in the same interface.  */
153                     if (current_interface -> ux_interface_descriptor.bInterfaceNumber != current_interface_number)
154                     {
155 
156                         /* Error trap. */
157                         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_INTERFACE_HANDLE_UNKNOWN);
158 
159                         /* If trace is enabled, insert this event into the trace buffer.  */
160                         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_INTERFACE_HANDLE_UNKNOWN, ux_interface, 0, 0, UX_TRACE_ERRORS, 0, 0)
161 
162                         return(UX_INTERFACE_HANDLE_UNKNOWN);
163                     }
164                 }
165             }
166         }
167 
168         /* Check the current interface, we may already be at the end ... */
169         if (current_interface != UX_NULL)
170         {
171 
172             /* Move to the next interface.  */
173             current_interface =  current_interface -> ux_interface_next_interface;
174 
175             /* Move to the next interface index. */
176             container_index++;
177         }
178     }
179 
180     /* Error trap. */
181     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, 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, ux_interface, 0, 0, UX_TRACE_ERRORS, 0, 0)
185 
186     /* Didn't find the right interface/alternate setting, return an error!  */
187     return(UX_INTERFACE_HANDLE_UNKNOWN);
188 }
189 
190