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