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 CCID 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_ccid.h"
27 #include "ux_device_stack.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_device_class_ccid_uninitialize                  PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function uninitialize the USB CCID device.                     */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    command                               Pointer to ccid command       */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    Completion Status                                                   */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*    _ux_utility_memory_free               Free memory                   */
55 /*    _ux_device_mutex_delete               Delete mutex                  */
56 /*    _ux_utility_event_flags_delete        Delete event flags            */
57 /*    _ux_utility_thread_delete             Delete thread                 */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    USBX Source Code                                                    */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  04-25-2022     Chaoqiong Xiao           Initial Version 6.1.11        */
68 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            added a new mode to manage  */
70 /*                                            endpoint buffer in classes, */
71 /*                                            resulting in version 6.3.0  */
72 /*                                                                        */
73 /**************************************************************************/
_ux_device_class_ccid_uninitialize(UX_SLAVE_CLASS_COMMAND * command)74 UINT  _ux_device_class_ccid_uninitialize(UX_SLAVE_CLASS_COMMAND *command)
75 {
76 
77 UX_DEVICE_CLASS_CCID                    *ccid;
78 UX_SLAVE_CLASS                          *ccid_class;
79 #if !defined(UX_DEVICE_STANDALONE)
80 UX_DEVICE_CLASS_CCID_RUNNER             *runner;
81 ULONG                                   i;
82 #endif
83 
84     /* Get the class container.  */
85     ccid_class =  command -> ux_slave_class_command_class_ptr;
86 
87     /* Get the CCID instance.  */
88     ccid = (UX_DEVICE_CLASS_CCID *)ccid_class -> ux_slave_class_instance;
89 
90     /* Sanity check.  */
91     if (ccid != UX_NULL)
92     {
93 
94         /* Free allocated resources.  */
95 #if !defined(UX_DEVICE_STANDALONE)
96         _ux_device_thread_delete(&ccid -> ux_device_class_ccid_notify_thread);
97         _ux_device_thread_delete(&ccid -> ux_device_class_ccid_thread);
98         for (i = 0;
99             i < ccid -> ux_device_class_ccid_parameter.ux_device_class_ccid_max_n_busy_slots;
100             i ++)
101         {
102             runner = &ccid -> ux_device_class_ccid_runners[i];
103             _ux_device_thread_delete(&runner -> ux_device_class_ccid_runner_thread);
104         }
105         _ux_device_event_flags_delete(&ccid -> ux_device_class_ccid_events);
106         _ux_device_mutex_delete(&ccid -> ux_device_class_ccid_mutex);
107         _ux_device_semaphore_delete(&ccid -> ux_device_class_ccid_notify_semaphore);
108         _ux_device_mutex_delete(&ccid -> ux_device_class_ccid_response_mutex);
109 #endif
110 
111 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
112         /* Free the bulk in endpoint memory.  */
113         _ux_utility_memory_free(ccid -> ux_device_class_ccid_endpoint_buffer);
114 #endif
115 
116         /* Free instance memory.  */
117         _ux_utility_memory_free(ccid);
118     }
119 
120     /* Return completion status.  */
121     return(UX_SUCCESS);
122 }
123