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