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_device_get                           PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function returns a device container based on its index. The    */
44 /*    device index start with device 0. Note that the index is a ULONG    */
45 /*    because we could have several controllers and a byte index might    */
46 /*    not be enough.                                                      */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    device_index                          Index of device               */
51 /*    device                                Destination for device pointer*/
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    None                                                                */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*    USBX Components                                                     */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            optimized based on compile  */
73 /*                                            definitions,                */
74 /*                                            resulting in version 6.1    */
75 /*                                                                        */
76 /**************************************************************************/
_ux_host_stack_device_get(ULONG device_index,UX_DEVICE ** device)77 UINT  _ux_host_stack_device_get(ULONG device_index, UX_DEVICE **device)
78 {
79 
80 UX_DEVICE   *current_device;
81 #if UX_MAX_DEVICES > 1
82 ULONG       current_device_index;
83 #endif
84 
85     /* If trace is enabled, insert this event into the trace buffer.  */
86     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_DEVICE_GET, device_index, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
87 
88     /* Check if the device index is still within the limits.  */
89     if (device_index >= UX_SYSTEM_HOST_MAX_DEVICES_GET())
90     {
91 
92         /* Error trap. */
93         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_ENUMERATOR, UX_DEVICE_HANDLE_UNKNOWN);
94 
95         /* If trace is enabled, insert this event into the trace buffer.  */
96         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
97 
98         return(UX_DEVICE_HANDLE_UNKNOWN);
99     }
100 
101 #if UX_MAX_DEVICES > 1
102 
103     /* Start with the first device.  */
104     current_device =        _ux_system_host -> ux_system_host_device_array;
105     current_device_index =  0;
106 
107     /* Search the list until the end.  */
108     while (current_device_index < _ux_system_host -> ux_system_host_max_devices)
109     {
110 
111         /* Check to see if this device is existing.  */
112         if (current_device -> ux_device_handle != UX_UNUSED)
113         {
114 
115             /* Have we reached the index we are looking for?  */
116             if (device_index == current_device_index)
117             {
118 
119                 /* Yes, return the device pointer.  */
120                 *device =  current_device;
121 
122                 /* Return successful completion.  */
123                 return(UX_SUCCESS);
124             }
125         }
126 
127         /* Move to next device index.  */
128         current_device_index++;
129 
130         /* Move to next device.  */
131         current_device++;
132     }
133 #else
134 
135     /* Only one device, just check if it's used.  */
136     current_device = _ux_system_host -> ux_system_host_device_array;
137     if (current_device -> ux_device_handle != UX_UNUSED)
138     {
139         *device = current_device;
140         return(UX_SUCCESS);
141     }
142 #endif
143 
144     /* If trace is enabled, insert this event into the trace buffer.  */
145     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_DEVICE_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
146 
147     /* Return error.  */
148     return(UX_DEVICE_HANDLE_UNKNOWN);
149 }
150 
151 
152 /**************************************************************************/
153 /*                                                                        */
154 /*  FUNCTION                                               RELEASE        */
155 /*                                                                        */
156 /*    _uxe_host_stack_device_get                          PORTABLE C      */
157 /*                                                           6.3.0        */
158 /*  AUTHOR                                                                */
159 /*                                                                        */
160 /*    Chaoqiong Xiao, Microsoft Corporation                               */
161 /*                                                                        */
162 /*  DESCRIPTION                                                           */
163 /*                                                                        */
164 /*    This function checks errors in host stack device get function call. */
165 /*                                                                        */
166 /*  INPUT                                                                 */
167 /*                                                                        */
168 /*    device_index                          Index of device               */
169 /*    device                                Destination for device pointer*/
170 /*                                                                        */
171 /*  OUTPUT                                                                */
172 /*                                                                        */
173 /*    None                                                                */
174 /*                                                                        */
175 /*  CALLS                                                                 */
176 /*                                                                        */
177 /*    _ux_host_stack_device_get             Host stack device get         */
178 /*                                                                        */
179 /*  CALLED BY                                                             */
180 /*                                                                        */
181 /*    Application                                                         */
182 /*                                                                        */
183 /*  RELEASE HISTORY                                                       */
184 /*                                                                        */
185 /*    DATE              NAME                      DESCRIPTION             */
186 /*                                                                        */
187 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
188 /*                                                                        */
189 /**************************************************************************/
_uxe_host_stack_device_get(ULONG device_index,UX_DEVICE ** device)190 UINT  _uxe_host_stack_device_get(ULONG device_index, UX_DEVICE **device)
191 {
192 
193     /* Sanity check.  */
194     if (device == UX_NULL)
195         return(UX_INVALID_PARAMETER);
196 
197     /* Invoke device get function.  */
198     return(_ux_host_stack_device_get(device_index, device));
199 }
200