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_new_device_get                       PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function obtains a free device container for the new device.   */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    None                                                                */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    UX_DEVICE pointer                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_utility_memory_set                Set memory to a value         */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    USBX Components                                                     */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
67 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
68 /*                                            optimized based on compile  */
69 /*                                            definitions, verified       */
70 /*                                            memset and memcpy cases,    */
71 /*                                            resulting in version 6.1    */
72 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            fixed standalone enum init, */
74 /*                                            resulting in version 6.1.12 */
75 /*                                                                        */
76 /**************************************************************************/
_ux_host_stack_new_device_get(VOID)77 UX_DEVICE  *_ux_host_stack_new_device_get(VOID)
78 {
79 
80 #if UX_MAX_DEVICES > 1
81 ULONG           container_index;
82 #endif
83 UX_DEVICE       *device;
84 #if defined(UX_HOST_STANDALONE)
85 UX_DEVICE       *enum_next;
86 #endif
87 
88     /* Start with the first device.  */
89     device =  _ux_system_host -> ux_system_host_device_array;
90 
91 #if UX_MAX_DEVICES > 1
92     /* Reset the container index.  */
93     container_index =  0;
94 
95     /* Search the list until the end.  */
96     while (container_index++ < _ux_system_host -> ux_system_host_max_devices)
97 #endif
98     {
99 
100         /* Until we have found an unused entry.  */
101         if (device -> ux_device_handle == UX_UNUSED)
102         {
103 
104 #if defined(UX_HOST_STANDALONE)
105 
106             /* Reset the entire entry except enum link.  */
107             enum_next = device -> ux_device_enum_next;
108             _ux_utility_memory_set(device, 0, sizeof(UX_DEVICE)); /* Use case of memset is verified. */
109             device -> ux_device_enum_next = enum_next;
110 #else
111 
112             /* Reset the entire entry.  */
113             _ux_utility_memory_set(device, 0, sizeof(UX_DEVICE)); /* Use case of memset is verified. */
114 #endif
115 
116             /* This entry is now used.  */
117             device -> ux_device_handle =  UX_USED;
118 
119             /* Return the device pointer.  */
120             return(device);
121         }
122 #if UX_MAX_DEVICES > 1
123 
124         /* Move to the next device entry.  */
125         device++;
126 #endif
127     }
128 
129     /* No unused devices, return NULL.  */
130     return(UX_NULL);
131 }
132 
133