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 /** Storage Class */
19 /** */
20 /**************************************************************************/
21 /**************************************************************************/
22
23
24 /* Include necessary system files. */
25
26 #define UX_SOURCE_CODE
27
28 #include "ux_api.h"
29 #include "ux_host_class_storage.h"
30 #include "ux_host_stack.h"
31
32
33 /**************************************************************************/
34 /* */
35 /* FUNCTION RELEASE */
36 /* */
37 /* _ux_host_class_storage_activate PORTABLE C */
38 /* 6.1.12 */
39 /* AUTHOR */
40 /* */
41 /* Chaoqiong Xiao, Microsoft Corporation */
42 /* */
43 /* DESCRIPTION */
44 /* */
45 /* This function activates an instance of the storage class. */
46 /* */
47 /* INPUT */
48 /* */
49 /* command Pointer to class command */
50 /* */
51 /* OUTPUT */
52 /* */
53 /* Completion Status */
54 /* */
55 /* CALLS */
56 /* */
57 /* _ux_host_class_storage_configure Configure storage device */
58 /* _ux_host_class_storage_device_initialize */
59 /* Initialize storage device */
60 /* _ux_host_stack_class_instance_create Create class instance */
61 /* _ux_host_stack_class_instance_destroy Destroy class instance */
62 /* _ux_utility_memory_allocate Allocate memory block */
63 /* _ux_utility_memory_free Free memory block */
64 /* _ux_host_semaphore_create Create semaphore */
65 /* */
66 /* CALLED BY */
67 /* */
68 /* Storage Class */
69 /* */
70 /* RELEASE HISTORY */
71 /* */
72 /* DATE NAME DESCRIPTION */
73 /* */
74 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
75 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
76 /* resulting in version 6.1 */
77 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
78 /* added standalone support. */
79 /* resulting in version 6.1.10 */
80 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
81 /* fixed parameter/variable */
82 /* names conflict C++ keyword, */
83 /* resulting in version 6.1.12 */
84 /* */
85 /**************************************************************************/
_ux_host_class_storage_activate(UX_HOST_CLASS_COMMAND * command)86 UINT _ux_host_class_storage_activate(UX_HOST_CLASS_COMMAND *command)
87 {
88
89 UX_INTERFACE *interface_ptr;
90 UX_HOST_CLASS_STORAGE *storage;
91 UINT status;
92
93
94 /* The storage is always activated by the interface descriptor and not the
95 device descriptor. */
96 interface_ptr = (UX_INTERFACE *) command -> ux_host_class_command_container;
97
98 /* Obtain memory for this class instance. The memory used MUST BE allocated from a CACHE SAFE memory
99 since the buffer for the CSW is an array contained within each storage instance. */
100 storage = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, sizeof(UX_HOST_CLASS_STORAGE));
101 if (storage == UX_NULL)
102 return(UX_MEMORY_INSUFFICIENT);
103
104 /* Store the class container into this instance */
105 storage -> ux_host_class_storage_class = command -> ux_host_class_command_class_ptr;
106
107 /* Store the interface container into the storage class instance. */
108 storage -> ux_host_class_storage_interface = interface_ptr;
109
110 /* Store the device container into the storage class instance. */
111 storage -> ux_host_class_storage_device = interface_ptr -> ux_interface_configuration -> ux_configuration_device;
112
113 /* Create this class instance. */
114 _ux_host_stack_class_instance_create(command -> ux_host_class_command_class_ptr, (VOID *) storage);
115
116 /* This instance of the device must also be stored in the interface container. */
117 interface_ptr -> ux_interface_class_instance = (VOID *) storage;
118
119 #if defined(UX_HOST_STANDALONE)
120
121 /* Check class,sub class, protocol. */
122 status = _ux_host_class_storage_device_support_check(storage);
123 if (status != UX_SUCCESS)
124 {
125 _ux_utility_memory_free(storage);
126 return(status);
127 }
128
129 /* Search all the endpoints for the storage interface (Bulk Out, Bulk in,
130 and optional Interrupt endpoint). */
131 status = _ux_host_class_storage_endpoints_get(storage);
132 if (status != UX_SUCCESS)
133 {
134 _ux_utility_memory_free(storage);
135 return(status);
136 }
137
138 /* Activate storage class task function. */
139 storage -> ux_host_class_storage_class -> ux_host_class_task_function = _ux_host_class_storage_tasks_run;
140
141 /* Mark the storage as live now (media mounts in task). */
142 storage -> ux_host_class_storage_state = UX_HOST_CLASS_INSTANCE_MOUNTING;
143
144 /* Keep storage locked before it's initialized. */
145 storage -> ux_host_class_storage_flags |= UX_HOST_CLASS_STORAGE_FLAG_LOCK;
146 #else
147
148 /* Configure the USB storage device. */
149 status = _ux_host_class_storage_configure(storage);
150
151 /* Create the semaphore to protect multiple threads from accessing the same storage instance. */
152 if (status == UX_SUCCESS)
153 {
154 status = _ux_host_semaphore_create(&storage -> ux_host_class_storage_semaphore, "ux_host_class_storage_semaphore", 1);
155 if (status != UX_SUCCESS)
156 status = UX_SEMAPHORE_ERROR;
157 }
158
159 /* Error case, free resources. */
160 if (status != UX_SUCCESS)
161 {
162
163 /* Last one, semaphore not created or created error, no need to free. */
164
165 /* Error, destroy the class and return an error. */
166 _ux_host_stack_class_instance_destroy(storage -> ux_host_class_storage_class, (VOID *) storage);
167
168 /* This instance of the device must also be removed in the interface container. */
169 interface_ptr -> ux_interface_class_instance = (VOID *) UX_NULL;
170
171 /* Free memory for class instance. */
172 _ux_utility_memory_free(storage);
173
174 return(status);
175 }
176
177 /* Mark the storage as mounting now. */
178 storage -> ux_host_class_storage_state = UX_HOST_CLASS_INSTANCE_MOUNTING;
179
180 /* Initialize the USB storage device. We do not check the status at this stage. We let the instance of this
181 class live even if there was a failure. Because the storage class has many media instance, we will let the
182 disconnection signal clean the instance at a later stage. */
183 _ux_host_class_storage_device_initialize(storage);
184
185 /* Mark the storage as live now. */
186 storage -> ux_host_class_storage_state = UX_HOST_CLASS_INSTANCE_LIVE;
187
188 #endif
189
190 /* If all is fine and the device is mounted, we may need to inform the application
191 if a function has been programmed in the system structure. */
192 if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
193 {
194
195 /* Call system change function. */
196 _ux_system_host -> ux_system_host_change_function(UX_DEVICE_INSERTION, storage -> ux_host_class_storage_class, (VOID *) storage);
197 }
198
199 /* If trace is enabled, insert this event into the trace buffer. */
200 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_ACTIVATE, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
201
202 /* If trace is enabled, register this object. */
203 UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, storage, 0, 0, 0)
204
205 /* Return completion status. Force it to success. */
206 return(UX_SUCCESS);
207 }
208
209