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_class_instance_create                PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function creates a new class instance for a class container.   */
44 /*    The instance of a class is not contained in the class code to       */
45 /*    reduce the class driver complexity. Rather, each class instance is  */
46 /*    attached to class container.                                        */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    class                                 Pointer to class              */
51 /*    class_instance                        Pointer to class instance     */
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 /*                                            resulting in version 6.1    */
73 /*                                                                        */
74 /**************************************************************************/
_ux_host_stack_class_instance_create(UX_HOST_CLASS * host_class,VOID * class_instance)75 UINT  _ux_host_stack_class_instance_create(UX_HOST_CLASS *host_class, VOID *class_instance)
76 {
77 
78 VOID    **current_class_instance;
79 
80     /* If trace is enabled, insert this event into the trace buffer.  */
81     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_CLASS_INSTANCE_CREATE, host_class, class_instance, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
82 
83     /* If trace is enabled, register this object.  */
84     UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_CLASS_INSTANCE, class_instance, 0, 0, 0)
85 
86     /* Start with the first class instance attached to the class container.  */
87     current_class_instance =  host_class -> ux_host_class_first_instance;
88 
89     /* Check if there are any instances attached.  */
90     if (current_class_instance == UX_NULL)
91     {
92 
93         /* Since it is the first class, attach it to the class container.  */
94         host_class -> ux_host_class_first_instance =  class_instance;
95 
96         /* Return successful completion.  */
97         return(UX_SUCCESS);
98     }
99 
100     /* Traverse the list of the class instances until we find the last class.  */
101     while (*current_class_instance != UX_NULL)
102     {
103 
104         /* Point to the next class instance.  */
105         current_class_instance =  *current_class_instance;
106     }
107 
108     /* We have reached the last class, hook the new class to the end. This way, we preserve
109        the chronological order of the class instances.  */
110     *current_class_instance =  class_instance;
111 
112     /* Return successful completion to caller.  */
113     return(UX_SUCCESS);
114 }
115 
116