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 /**   Device Stack                                                        */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_stack_class_unregister                   PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function unregisters a slave class to the slave stack.         */
44 /*                                                                        */
45 /*    Note: The C string of class_name must be NULL-terminated and the    */
46 /*    length of it (without the NULL-terminator itself) must be no larger */
47 /*    than UX_MAX_CLASS_NAME_LENGTH.                                      */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    class_name                            Name of class                 */
52 /*    class_function_entry                  Class entry function          */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_utility_string_length_check       Check C string and return     */
61 /*                                          its length if null-terminated */
62 /*    _ux_utility_memory_compare            Memory compare                */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    Application                                                         */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            optimized based on compile  */
75 /*                                            definitions,                */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_ux_device_stack_class_unregister(UCHAR * class_name,UINT (* class_entry_function)(struct UX_SLAVE_CLASS_COMMAND_STRUCT *))79 UINT  _ux_device_stack_class_unregister(UCHAR *class_name,
80                         UINT (*class_entry_function)(struct UX_SLAVE_CLASS_COMMAND_STRUCT *))
81 {
82 
83 UX_SLAVE_CLASS              *class_inst;
84 UINT                        status;
85 UX_SLAVE_CLASS_COMMAND      command;
86 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
87 UINT                        class_name_length =  0;
88 #endif
89 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
90 ULONG                       class_index;
91 #endif
92 
93 
94     /* If trace is enabled, insert this event into the trace buffer.  */
95     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_CLASS_UNREGISTER, class_name, 0, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
96 
97 #if !defined(UX_NAME_REFERENCED_BY_POINTER)
98     /* Get the length of the class name (exclude null-terminator).  */
99     status =  _ux_utility_string_length_check(class_name, &class_name_length, UX_MAX_CLASS_NAME_LENGTH);
100     if (status)
101         return(status);
102 #endif
103 
104     class_inst =  _ux_system_slave -> ux_system_slave_class_array;
105 
106 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
107     /* We need to parse the class table to find the right class.  */
108     for (class_index = 0; class_index < _ux_system_slave -> ux_system_slave_max_class; class_index++)
109     {
110 #endif
111 
112         /* Check if this class is the right one.  */
113         if (class_inst -> ux_slave_class_status == UX_USED)
114         {
115 
116             /* We have found a used container with a  class. Compare the name (include null-terminator).  */
117             if (ux_utility_name_match(class_inst -> ux_slave_class_name, class_name, class_name_length + 1))
118             {
119 
120                 /* Build all the fields of the Class Command to uninitialize the class.  */
121                 command.ux_slave_class_command_request    =  UX_SLAVE_CLASS_COMMAND_UNINITIALIZE;
122                 command.ux_slave_class_command_class_ptr  =  class_inst;
123 
124                 /* Call the class uninitialization routine.  */
125                 status = class_entry_function(&command);
126 
127                 /* Check the status.  */
128                 if (status != UX_SUCCESS)
129                     return(status);
130 
131                 /* Make this class unused now.  */
132                 class_inst -> ux_slave_class_status = UX_UNUSED;
133 
134                 /* Erase the instance of the class.  */
135                 class_inst -> ux_slave_class_instance = UX_NULL;
136 
137                 /* Return successful completion.  */
138                 return(UX_SUCCESS);
139             }
140         }
141 
142 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
143         /* Move to the next class.  */
144         class_inst ++;
145     }
146 #endif
147 
148     /* No class match.  */
149     return(UX_NO_CLASS_MATCH);
150 }
151 
152 
153 /**************************************************************************/
154 /*                                                                        */
155 /*  FUNCTION                                               RELEASE        */
156 /*                                                                        */
157 /*    _uxe_device_stack_class_unregister                  PORTABLE C      */
158 /*                                                           6.3.0        */
159 /*  AUTHOR                                                                */
160 /*                                                                        */
161 /*    Chaoqiong Xiao, Microsoft Corporation                               */
162 /*                                                                        */
163 /*  DESCRIPTION                                                           */
164 /*                                                                        */
165 /*    This function checks errors in device stack class unregister        */
166 /*    function call.                                                      */
167 /*                                                                        */
168 /*  INPUT                                                                 */
169 /*                                                                        */
170 /*    class_name                            Name of class                 */
171 /*    class_function_entry                  Class entry function          */
172 /*                                                                        */
173 /*  OUTPUT                                                                */
174 /*                                                                        */
175 /*    None                                                                */
176 /*                                                                        */
177 /*  CALLS                                                                 */
178 /*                                                                        */
179 /*    _ux_device_stack_class_unregister     Class unregister              */
180 /*                                                                        */
181 /*  CALLED BY                                                             */
182 /*                                                                        */
183 /*    Application                                                         */
184 /*                                                                        */
185 /*  RELEASE HISTORY                                                       */
186 /*                                                                        */
187 /*    DATE              NAME                      DESCRIPTION             */
188 /*                                                                        */
189 /*  10-31-2023     Chaoqiong Xiao           Initial Version 6.3.0         */
190 /*                                                                        */
191 /**************************************************************************/
_uxe_device_stack_class_unregister(UCHAR * class_name,UINT (* class_entry_function)(struct UX_SLAVE_CLASS_COMMAND_STRUCT *))192 UINT  _uxe_device_stack_class_unregister(UCHAR *class_name,
193                         UINT (*class_entry_function)(struct UX_SLAVE_CLASS_COMMAND_STRUCT *))
194 {
195 
196     /* Sanity checks.  */
197     if ((class_name == UX_NULL) || (class_entry_function == UX_NULL))
198         return(UX_INVALID_PARAMETER);
199 
200     /* Invoke unregister function.  */
201     return(_ux_device_stack_class_unregister(class_name, class_entry_function));
202 }
203