1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** USBX Component */
/** */
/** Device Storage Class */
/** */
/**************************************************************************/
/**************************************************************************/
#define UX_SOURCE_CODE
/* Include necessary system files. */
#include "ux_api.h"
#include "ux_device_class_storage.h"
#include "ux_device_stack.h"
/* Define the Slave Storage Class Inquiry data : DO NOT CHANGE THE LENGTH OF THESE ITEMS */
UCHAR _ux_system_slave_class_storage_vendor_id[] = "Eclipse ";
UCHAR _ux_system_slave_class_storage_product_id[] = "USBX storage dev";
UCHAR _ux_system_slave_class_storage_product_rev[] = "2000";
UCHAR _ux_system_slave_class_storage_product_serial[] = "12345678901234567890";
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_device_class_storage_initialize PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function initializes the USB storage device. */
/* */
/* INPUT */
/* */
/* command Pointer to storage command */
/* */
/* OUTPUT */
/* */
/* Completion Status */
/* */
/* CALLS */
/* */
/* _ux_utility_memory_allocate Allocate memory */
/* _ux_utility_memory_free Free memory */
/* _ux_device_thread_create Create thread */
/* _ux_device_thread_delete Delete thread */
/* */
/* CALLED BY */
/* */
/* Device Storage Class */
/* */
/**************************************************************************/
UINT _ux_device_class_storage_initialize(UX_SLAVE_CLASS_COMMAND *command)
{
UINT status = UX_SUCCESS;
UX_SLAVE_CLASS_STORAGE *storage;
UX_SLAVE_CLASS_STORAGE_PARAMETER *storage_parameter;
UX_SLAVE_CLASS *class_inst;
ULONG lun_index;
/* Get the pointer to the application parameters for the storage class. */
storage_parameter = command -> ux_slave_class_command_parameter;
/* Ensure the number of LUN declared by the caller does not exceed the
max number allowed for LUN storage. */
if (storage_parameter -> ux_slave_class_storage_parameter_number_lun > UX_MAX_SLAVE_LUN)
return UX_ERROR;
/* Get the class container. */
class_inst = command -> ux_slave_class_command_class_ptr;
/* Create an instance of the device storage class. */
storage = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_SLAVE_CLASS_STORAGE));
/* Check for successful allocation. */
if (storage == UX_NULL)
return(UX_MEMORY_INSUFFICIENT);
#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
/* Allocate bulk endpoint buffer. */
UX_ASSERT(!UX_DEVICE_CLASS_STORAGE_ENDPOINT_BUFFER_SIZE_CALC_OVERFLOW);
storage -> ux_device_class_storage_endpoint_buffer = _ux_utility_memory_allocate(UX_NO_ALIGN,
UX_CACHE_SAFE_MEMORY, UX_DEVICE_CLASS_STORAGE_ENDPOINT_BUFFER_SIZE);
#else
status = UX_SUCCESS;
#endif
#if !defined(UX_DEVICE_STANDALONE)
/* Allocate some memory for the thread stack. */
if (status == UX_SUCCESS)
{
class_inst -> ux_slave_class_thread_stack = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, UX_THREAD_STACK_SIZE);
/* If it's OK, create thread. */
if (class_inst -> ux_slave_class_thread_stack != UX_NULL)
/* This instance needs to be running in a different thread. So start
a new thread. We pass a pointer to the class to the new thread. This thread
does not start until we have a instance of the class. */
status = _ux_device_thread_create(&class_inst -> ux_slave_class_thread, "ux_slave_storage_thread",
_ux_device_class_storage_thread,
(ULONG) (ALIGN_TYPE) class_inst, (VOID *) class_inst -> ux_slave_class_thread_stack,
UX_THREAD_STACK_SIZE, UX_THREAD_PRIORITY_CLASS,
UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, UX_DONT_START);
else
status = UX_MEMORY_INSUFFICIENT;
}
#else
/* Save tasks run entry. */
class_inst -> ux_slave_class_task_function = _ux_device_class_storage_tasks_run;
#endif
/* If thread resources allocated, go on. */
if (status == UX_SUCCESS)
{
UX_THREAD_EXTENSION_PTR_SET(&(class_inst -> ux_slave_class_thread), class_inst)
/* Store the number of LUN declared. */
storage -> ux_slave_class_storage_number_lun = storage_parameter -> ux_slave_class_storage_parameter_number_lun;
/* Copy each individual LUN parameters. */
for (lun_index = 0; lun_index < storage -> ux_slave_class_storage_number_lun; lun_index++)
{
/* Check block length size. */
if (storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_block_length > UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE)
{
/* Cannot proceed. */
status = (UX_MEMORY_INSUFFICIENT);
break;
}
/* Store all the application parameter information about the media. */
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_last_lba = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_last_lba;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_block_length = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_block_length;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_type = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_type;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_removable_flag = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_removable_flag;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_read_only_flag = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_read_only_flag;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_read = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_read;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_flush = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_flush;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_write = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_write;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_status = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_status;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_notification = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_notification;
storage -> ux_slave_class_storage_lun[lun_index].ux_slave_class_storage_media_start_stop = storage_parameter -> ux_slave_class_storage_parameter_lun[lun_index].ux_slave_class_storage_media_start_stop;
}
/* If it's OK, complete it. */
if (status == UX_SUCCESS)
{
/* Store the start and stop signals if needed by the application. */
storage -> ux_slave_class_storage_instance_activate = storage_parameter -> ux_slave_class_storage_instance_activate;
storage -> ux_slave_class_storage_instance_deactivate = storage_parameter -> ux_slave_class_storage_instance_deactivate;
/* Store the vendor id, product id, product revision and product serial. */
if (storage_parameter -> ux_slave_class_storage_parameter_vendor_id)
storage -> ux_slave_class_storage_vendor_id = storage_parameter -> ux_slave_class_storage_parameter_vendor_id;
else
storage -> ux_slave_class_storage_vendor_id = _ux_system_slave_class_storage_vendor_id;
if (storage_parameter -> ux_slave_class_storage_parameter_product_id)
storage -> ux_slave_class_storage_product_id = storage_parameter -> ux_slave_class_storage_parameter_product_id;
else
storage -> ux_slave_class_storage_product_id = _ux_system_slave_class_storage_product_id;
if (storage_parameter -> ux_slave_class_storage_parameter_product_rev)
storage -> ux_slave_class_storage_product_rev = storage_parameter -> ux_slave_class_storage_parameter_product_rev;
else
storage -> ux_slave_class_storage_product_rev = _ux_system_slave_class_storage_product_rev;
if (storage_parameter -> ux_slave_class_storage_parameter_product_serial)
storage -> ux_slave_class_storage_product_serial = storage_parameter -> ux_slave_class_storage_parameter_product_serial;
else
storage -> ux_slave_class_storage_product_serial = _ux_system_slave_class_storage_product_serial;
/* Save the address of the STORAGE instance inside the STORAGE container. */
class_inst -> ux_slave_class_instance = (VOID *) storage;
return(UX_SUCCESS);
}
/* Free thread resources. */
_ux_device_thread_delete(&class_inst -> ux_slave_class_thread);
}
#if !defined(UX_DEVICE_STANDALONE)
if (class_inst -> ux_slave_class_thread_stack != UX_NULL)
_ux_utility_memory_free(&class_inst -> ux_slave_class_thread_stack);
#endif
#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
if (storage -> ux_device_class_storage_endpoint_buffer != UX_NULL)
_ux_utility_memory_free(storage -> ux_device_class_storage_endpoint_buffer);
#endif
/* Free instance. */
_ux_utility_memory_free(storage);
/* Return completion status. */
return(status);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _uxe_device_class_storage_initialize PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function checks errors in storage initialization function call.*/
/* */
/* INPUT */
/* */
/* command Pointer to storage command */
/* */
/* OUTPUT */
/* */
/* Completion Status */
/* */
/* CALLS */
/* */
/* _ux_device_class_storage_initialize Initialize storage instance */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/**************************************************************************/
UINT _uxe_device_class_storage_initialize(UX_SLAVE_CLASS_COMMAND *command)
{
UX_SLAVE_CLASS_STORAGE_PARAMETER *storage_parameter;
UINT i;
/* Get the pointer to the application parameters for the storage class. */
storage_parameter = command -> ux_slave_class_command_parameter;
/* Sanity checks. */
if ((storage_parameter -> ux_slave_class_storage_parameter_number_lun > UX_MAX_SLAVE_LUN) ||
((storage_parameter -> ux_slave_class_storage_parameter_vendor_id != UX_NULL) &&
( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_vendor_id) != 8)) ||
((storage_parameter -> ux_slave_class_storage_parameter_product_id != UX_NULL) &&
( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_product_id) != 16)) ||
((storage_parameter -> ux_slave_class_storage_parameter_product_rev != UX_NULL) &&
( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_product_rev) != 4)) ||
((storage_parameter -> ux_slave_class_storage_parameter_product_serial != UX_NULL) &&
( _ux_utility_string_length_get(storage_parameter -> ux_slave_class_storage_parameter_product_serial) != 20)))
return(UX_INVALID_PARAMETER);
for (i = 0; i < storage_parameter -> ux_slave_class_storage_parameter_number_lun; i ++)
{
if ((storage_parameter -> ux_slave_class_storage_parameter_lun[i].
ux_slave_class_storage_media_read == UX_NULL) ||
(storage_parameter -> ux_slave_class_storage_parameter_lun[i].
ux_slave_class_storage_media_write == UX_NULL) ||
(storage_parameter -> ux_slave_class_storage_parameter_lun[i].
ux_slave_class_storage_media_status == UX_NULL)
#if defined(UX_SLAVE_CLASS_STORAGE_INCLUDE_MMC)
|| (storage_parameter -> ux_slave_class_storage_parameter_lun[i].
ux_slave_class_storage_media_notification == UX_NULL)
#endif
)
{
return(UX_INVALID_PARAMETER);
}
}
/* Invoke storage initialize function. */
return(_ux_device_class_storage_initialize(command));
}
|