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