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_unregister                     PORTABLE C      */
36 /*                                                           6.1          */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function unregisters a USB class from the USB stack.           */
44 /*                                                                        */
45 /*    Note following steps must be done before host class unregister:     */
46 /*    All devices related to the class must be removed.                   */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    class_name                            Name of class                 */
51 /*    class_entry_function                  Entry function of the class   */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*    UX_SUCCESS                            Class unregistered            */
57 /*    UX_NO_CLASS_MATCH                     Class not found               */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    (class_entry_function)                Entry function of the class   */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application                                                         */
66 /*    USBX Components                                                     */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  09-30-2020     Chaoqiong Xiao           Initial Version 6.1           */
73 /*                                                                        */
74 /**************************************************************************/
_ux_host_stack_class_unregister(UINT (* class_entry_function)(struct UX_HOST_CLASS_COMMAND_STRUCT *))75 UINT  _ux_host_stack_class_unregister(UINT (*class_entry_function)(struct UX_HOST_CLASS_COMMAND_STRUCT *))
76 {
77 
78 UX_HOST_CLASS           *class_inst;
79 UX_HOST_CLASS_COMMAND   class_command;
80 #if UX_MAX_CLASS_DRIVER > 1
81 ULONG                   class_index;
82 #endif
83 
84 
85     /* If trace is enabled, insert this event into the trace buffer.  */
86     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_CLASS_UNREGISTER, class_entry_function, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)
87 
88     /* Get first class.  */
89     class_inst =  _ux_system_host -> ux_system_host_class_array;
90 
91 #if UX_MAX_CLASS_DRIVER > 1
92     /* We need to parse the class table to find right class instance.  */
93     for (class_index = 0; class_index < _ux_system_host -> ux_system_host_max_class; class_index++)
94     {
95 #endif
96 
97         /* Check if the class is expected.  */
98         if (class_inst -> ux_host_class_entry_function == class_entry_function)
99         {
100 
101             /* Initialize the class command with the generic parameters.  */
102             class_command.ux_host_class_command_request =  UX_HOST_CLASS_COMMAND_DESTROY;
103             class_command.ux_host_class_command_class_ptr = (VOID *)class_inst;
104 
105             /* Invoke command for class destroy.  */
106             class_inst -> ux_host_class_entry_function(&class_command);
107 
108             /* Mark as free.  */
109             class_inst -> ux_host_class_entry_function = UX_NULL;
110             class_inst -> ux_host_class_status = UX_UNUSED;
111 
112             /* Class unregistered success.  */
113             return(UX_SUCCESS);
114         }
115 
116 #if UX_MAX_CLASS_DRIVER > 1
117         /* Move to the next class.  */
118         class_inst ++;
119     }
120 #endif
121 
122     /* No more entries in the class table.  */
123     return(UX_NO_CLASS_MATCH);
124 }
125