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 /** USBX Component                                                        */
14 /**                                                                       */
15 /**   Device HID Class                                                    */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 
20 #define UX_SOURCE_CODE
21 
22 
23 /* Include necessary system files.  */
24 
25 #include "ux_api.h"
26 #include "ux_device_class_hid.h"
27 #include "ux_device_stack.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_device_class_hid_uninitialize                   PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function uninitializes the USB HID device.                     */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    command                              Pointer to hid command         */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    Completion Status                                                   */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*    _ux_device_thread_delete             Remove storage thread.         */
55 /*    _ux_utility_memory_free              Free memory used by storage    */
56 /*    _ux_utility_event_flags_delete       Remove flag event structure    */
57 /*                                                                        */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    USBX Source Code                                                    */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
68 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            added standalone support,   */
72 /*                                            added interrupt OUT support,*/
73 /*                                            resulting in version 6.1.10 */
74 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            resulting in version 6.1.11 */
76 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            fixed parameter/variable    */
78 /*                                            names conflict C++ keyword, */
79 /*                                            resulting in version 6.1.12 */
80 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            added zero copy support,    */
82 /*                                            added a new mode to manage  */
83 /*                                            endpoint buffer in classes, */
84 /*                                            resulting in version 6.3.0  */
85 /*                                                                        */
86 /**************************************************************************/
_ux_device_class_hid_uninitialize(UX_SLAVE_CLASS_COMMAND * command)87 UINT  _ux_device_class_hid_uninitialize(UX_SLAVE_CLASS_COMMAND *command)
88 {
89 
90 UX_SLAVE_CLASS_HID                      *hid;
91 UX_SLAVE_CLASS                          *class_ptr;
92 
93 
94     /* Get the class container.  */
95     class_ptr =  command -> ux_slave_class_command_class_ptr;
96 
97     /* Get the class instance in the container.  */
98     hid = (UX_SLAVE_CLASS_HID *) class_ptr -> ux_slave_class_instance;
99 
100 #if !defined(UX_DEVICE_STANDALONE)
101 
102     /* Remove HID thread.  */
103     _ux_device_thread_delete(&class_ptr -> ux_slave_class_thread);
104 
105     /* Remove the thread used by HID.  */
106     _ux_utility_memory_free(class_ptr -> ux_slave_class_thread_stack);
107 
108     /* Delete the event flag group for the hid class.  */
109     _ux_device_event_flags_delete(&hid -> ux_device_class_hid_event_flags_group);
110 #endif
111 
112     /* Free memory for the array. */
113 #if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_HID_ZERO_COPY)
114     _ux_utility_memory_free(hid -> ux_device_class_hid_event_array -> ux_device_class_hid_event_buffer);
115 #endif
116     _ux_utility_memory_free(hid -> ux_device_class_hid_event_array);
117 
118 #if defined(UX_DEVICE_CLASS_HID_INTERRUPT_OUT_SUPPORT)
119 
120 #if !defined(UX_DEVICE_STANDALONE)
121 
122     /* Free read mutex.  */
123     _ux_device_mutex_delete(&hid -> ux_device_class_hid_read_mutex);
124 #endif
125 
126     /* Uninitialize receiver.  */
127     if (hid -> ux_device_class_hid_receiver)
128         hid -> ux_device_class_hid_receiver ->
129             ux_device_class_hid_receiver_uninitialize(hid -> ux_device_class_hid_receiver);
130 #endif
131 
132 #if defined(UX_DEVICE_CLASS_HID_OWN_ENDPOINT_BUFFER)
133     _ux_utility_memory_free(hid -> ux_device_class_hid_endpoint_buffer);
134 #endif
135 
136     /* Free the resources.  */
137     _ux_utility_memory_free(hid);
138 
139     /* Return completion status.  */
140     return(UX_SUCCESS);
141 }
142 
143