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 Storage Class                                                */
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_class_storage.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_storage_uninitialize               PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function deinitializes the USB storage device.                 */
45 /*                                                                        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    command                               Pointer to storage command    */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_utility_memory_free               Free memory                   */
57 /*    _ux_device_thread_delete              Delete thread                 */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Device Storage Class                                                */
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 /*                                            resulting in version 6.1.10 */
73 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            fixed parameter/variable    */
75 /*                                            names conflict C++ keyword, */
76 /*                                            resulting in version 6.1.12 */
77 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            added a new mode to manage  */
79 /*                                            endpoint buffer in classes, */
80 /*                                            resulting in version 6.3.0  */
81 /*                                                                        */
82 /**************************************************************************/
_ux_device_class_storage_uninitialize(UX_SLAVE_CLASS_COMMAND * command)83 UINT  _ux_device_class_storage_uninitialize(UX_SLAVE_CLASS_COMMAND *command)
84 {
85 
86 UX_SLAVE_CLASS_STORAGE                  *storage;
87 UX_SLAVE_CLASS                          *class_ptr;
88 
89     /* Get the class container.  */
90     class_ptr =  command -> ux_slave_class_command_class_ptr;
91 
92     /* Get the class instance in the container.  */
93     storage = (UX_SLAVE_CLASS_STORAGE *) class_ptr -> ux_slave_class_instance;
94 
95     /* Sanity check.  */
96     if (storage != UX_NULL)
97     {
98 
99         /* Remove STORAGE thread.  */
100         _ux_device_thread_delete(&class_ptr -> ux_slave_class_thread);
101 
102 #if !(defined(UX_DEVICE_STANDALONE) || defined(UX_STANDALONE))
103         /* Remove the thread used by STORAGE.  */
104         _ux_utility_memory_free(class_ptr -> ux_slave_class_thread_stack);
105 #endif
106 
107 #if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
108         _ux_utility_memory_free(storage -> ux_device_class_storage_endpoint_buffer);
109 #endif
110 
111         /* Free the resources.  */
112         _ux_utility_memory_free(storage);
113     }
114 
115     /* Return completion status.  */
116     return(UX_SUCCESS);
117 }
118 
119