1 /* This test is designed to test the simple dpump host/device class operation. */
2
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "ux_api.h"
6 #include "ux_system.h"
7 #include "ux_utility.h"
8
9 #include "fx_api.h"
10
11 #include "ux_device_class_storage.h"
12 #include "ux_device_stack.h"
13 #include "ux_host_stack.h"
14 #include "ux_host_class_storage.h"
15
16 #include "ux_test_dcd_sim_slave.h"
17 #include "ux_test_hcd_sim_host.h"
18 #include "ux_test_utility_sim.h"
19
20 /* Define constants. */
21 #define UX_DEMO_STACK_SIZE 2048
22 #define UX_DEMO_MEMORY_SIZE (256*1024)
23 #define UX_DEMO_BUFFER_SIZE 2048
24
25 #define UX_RAM_DISK_SIZE (200 * 1024)
26 #define UX_RAM_DISK_LAST_LBA ((UX_RAM_DISK_SIZE / 512) -1)
27
28 /* Define local/extern function prototypes. */
29
30 VOID _fx_ram_driver(FX_MEDIA *media_ptr);
31 void _fx_ram_drive_format(ULONG disk_size, UINT sector_size, UINT sectors_per_cluster,
32 UINT fat_entries, UINT root_directory_entries);
33
34 static void demo_thread_entry(ULONG);
35 static TX_THREAD tx_demo_thread_host_simulation;
36 static TX_THREAD tx_demo_thread_slave_simulation;
37 static void tx_demo_thread_host_simulation_entry(ULONG);
38
39 static UINT demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
40 static UINT demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
41 static UINT demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status);
42 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status);
43
44 /* Define global data structures. */
45
46 static UCHAR usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
47 static UCHAR buffer[UX_DEMO_BUFFER_SIZE];
48 static UCHAR stack_buffer[UX_HOST_CLASS_STORAGE_THREAD_STACK_SIZE];
49 static UCHAR cbw_buffer[UX_SLAVE_CLASS_STORAGE_CBW_LENGTH];
50 static UCHAR csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_LENGTH];
51
52 static ULONG error_counter;
53
54 static TX_THREAD demo_thread;
55
56 static UX_HOST_CLASS_STORAGE *storage;
57 static UX_SLAVE_CLASS_STORAGE_PARAMETER global_storage_parameter;
58
59 static FX_MEDIA ram_disk_media1;
60 static FX_MEDIA ram_disk_media2;
61 static CHAR ram_disk_buffer1[512];
62 static CHAR ram_disk_buffer2[512];
63 static CHAR ram_disk_memory1[UX_RAM_DISK_SIZE];
64 static CHAR ram_disk_memory2[UX_RAM_DISK_SIZE];
65 static CHAR *ram_disk_memory[] =
66 {
67 ram_disk_memory1, ram_disk_memory2
68 };
69 static UINT ram_disk_status = UX_SUCCESS;
70 static ULONG ram_disk_media_status = 0;
71 static CHAR ram_disk_status_sent = 0;
72
73 static UINT ram_disk_read_status = UX_SUCCESS;
74 static ULONG ram_disk_read_media_status = 0;
75 static CHAR ram_disk_read_sent = 0;
76
77 static UINT ram_disk_write_status = UX_SUCCESS;
78 static ULONG ram_disk_write_media_status = 0;
79 static CHAR ram_disk_write_sent = 0;
80
81 static CHAR ram_disk_flush = 0;
82 static UINT ram_disk_flush_status = UX_SUCCESS;
83
84 static ULONG set_cfg_counter;
85
86 static ULONG rsc_mem_alloc_cnt_on_set_cfg;
87 static ULONG rsc_sem_on_set_cfg;
88 static ULONG rsc_sem_get_on_set_cfg;
89 static ULONG rsc_mutex_on_set_cfg;
90
91 static ULONG rsc_enum_sem_usage;
92 static ULONG rsc_enum_sem_get_count;
93 static ULONG rsc_enum_mutex_usage;
94 static ULONG rsc_enum_mem_alloc_count;
95
96 static ULONG rsc_storage_sem_usage;
97 static ULONG rsc_storage_sem_get_count;
98 static ULONG rsc_storage_mutex_usage;
99 static ULONG rsc_storage_mem_alloc_count;
100
101 static ULONG interaction_count;
102
103 static UCHAR error_callback_ignore = UX_TRUE;
104 static ULONG error_callback_counter;
105
106 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 50
107 static UCHAR device_framework_full_speed[] = {
108
109 /* Device descriptor */
110 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
111 0x81, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
112 0x03, 0x01,
113
114 /* Configuration descriptor */
115 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0,
116 0x32,
117
118 /* Interface descriptor */
119 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50,
120 0x00,
121
122 /* Endpoint descriptor (Bulk Out) */
123 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
124
125 /* Endpoint descriptor (Bulk In) */
126 0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00,
127
128 };
129
130
131 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 60
132 static UCHAR device_framework_high_speed[] = {
133
134 /* Device descriptor */
135 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
136 0x81, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x02,
137 0x03, 0x01,
138
139 /* Device qualifier descriptor */
140 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
141 0x01, 0x00,
142
143 /* Configuration descriptor */
144 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0,
145 0x32,
146
147 /* Interface descriptor */
148 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50,
149 0x00,
150
151 /* Endpoint descriptor (Bulk Out) */
152 0x07, 0x05, 0x02, 0x02, 0x00, 0x01, 0x00,
153
154 /* Endpoint descriptor (Bulk In) */
155 0x07, 0x05, 0x81, 0x02, 0x00, 0x01, 0x00,
156
157 };
158
159
160 /* String Device Framework :
161 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
162 Byte 2 : Byte containing the index of the descriptor
163 Byte 3 : Byte containing the length of the descriptor string
164 */
165
166 #define STRING_FRAMEWORK_LENGTH 38
167 static UCHAR string_framework[] = {
168
169 /* Manufacturer string descriptor : Index 1 */
170 0x09, 0x04, 0x01, 0x0c,
171 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
172 0x6f, 0x67, 0x69, 0x63,
173
174 /* Product string descriptor : Index 2 */
175 0x09, 0x04, 0x02, 0x0a,
176 0x46, 0x6c, 0x61, 0x73, 0x68, 0x20, 0x44, 0x69,
177 0x73, 0x6b,
178
179 /* Serial Number string descriptor : Index 3 */
180 0x09, 0x04, 0x03, 0x04,
181 0x30, 0x30, 0x30, 0x31
182 };
183
184
185 /* Multiple languages are supported on the device, to add
186 a language besides english, the unicode language code must
187 be appended to the language_id_framework array and the length
188 adjusted accordingly. */
189 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
190 static UCHAR language_id_framework[] = {
191
192 /* English. */
193 0x09, 0x04
194 };
195
196 static UX_TEST_SETUP _GetMaxLun = UX_TEST_SETUP_STORAGE_GetMaxLun;
197
198 static UX_TEST_HCD_SIM_ACTION stall_GetMaxLun[] = {
199 /* function, request to match,
200 port action, port status,
201 request action, request EP, request data, request actual length, request status,
202 status, additional callback,
203 no_return */
204 { UX_HCD_TRANSFER_REQUEST, &_GetMaxLun,
205 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
206 UX_TEST_SETUP_MATCH_REQ | UX_TEST_SIM_REQ_ANSWER, 0, buffer, 1, UX_TRANSFER_STALLED,
207 UX_TRANSFER_STALLED, UX_NULL},
208 { 0 }
209 };
210
211 static UX_TEST_HCD_SIM_ACTION length_error_GetMaxLun[] = {
212 /* function, request to match,
213 port action, port status,
214 request action, request EP, request data, request actual length, request status,
215 status, additional callback,
216 no_return */
217 { UX_HCD_TRANSFER_REQUEST, &_GetMaxLun,
218 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
219 UX_TEST_SETUP_MATCH_REQ | UX_TEST_SIM_REQ_ANSWER, 0, buffer, 2, UX_SUCCESS,
220 UX_SUCCESS, UX_NULL},
221 { 0 }
222 };
223
224 static UX_TEST_HCD_SIM_ACTION replaced_GetMaxLun[] = {
225 /* function, request to match,
226 port action, port status,
227 request action, request EP, request data, request actual length, request status,
228 status, additional callback,
229 no_return */
230 { UX_HCD_TRANSFER_REQUEST, &_GetMaxLun,
231 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
232 UX_TEST_SETUP_MATCH_REQ | UX_TEST_SIM_REQ_ANSWER, 0, buffer, 1, UX_SUCCESS,
233 UX_SUCCESS, UX_NULL},
234 { 0 }
235 };
236
237 static UX_TEST_HCD_SIM_ACTION fail_bulk_out[] = {
238 /* function, request to match,
239 port action, port status,
240 request action, request EP, request data, request actual length, request status,
241 status, additional callback,
242 no_return */
243 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
244 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
245 UX_TEST_MATCH_EP, 0x02, UX_NULL, 0, UX_ERROR,
246 UX_ERROR, UX_NULL},
247 { 0 }
248 };
249
250 static UX_TEST_HCD_SIM_ACTION fail_2nd_bulk_out[] = {
251 /* function, request to match,
252 port action, port status,
253 request action, request EP, request data, request actual length, request status,
254 status, additional callback,
255 no_return */
256 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
257 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
258 UX_TEST_MATCH_EP, 0x02, UX_NULL, 0, UX_SUCCESS,
259 UX_SUCCESS, UX_NULL,
260 UX_TRUE},
261 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
262 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
263 UX_TEST_MATCH_EP, 0x02, UX_NULL, 0, UX_ERROR,
264 UX_ERROR, UX_NULL},
265 { 0 }
266 };
267
sending_cbw(struct UX_TEST_ACTION_STRUCT * action,VOID * params)268 static VOID sending_cbw(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
269 {
270
271 UCHAR *cbw = (UCHAR *)storage -> ux_host_class_storage_cbw;
272 UX_TRANSFER *transfer_request = &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
273
274
275 _ux_utility_memory_copy(cbw_buffer, cbw, 31);
276
277 /* Prepare CSW. */
278 _ux_utility_memory_copy(csw_buffer + 0, cbw_buffer + 0, 4); /* dCBWSignature -> dCSWSignature */
279 _ux_utility_memory_copy(csw_buffer + 4, cbw_buffer + 4, 4); /* dCBWTag -> dCSWTag */
280 _ux_utility_memory_set (csw_buffer + 8, 0x00, 4); /* dCSWDataResidue <= 0 */
281 _ux_utility_memory_set (csw_buffer + 12, 0x00, 1); /* bCSWStatus <= 0 */
282
283 _ux_utility_semaphore_put(&transfer_request -> ux_transfer_request_semaphore);
284 }
285
getting_data(struct UX_TEST_ACTION_STRUCT * action,VOID * params)286 static VOID getting_data(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
287 {
288
289 UX_TRANSFER *transfer_request = &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request;
290
291
292 _ux_utility_long_put(csw_buffer + 8, 1); /* dCSWDataResidue <= 1 for error test */
293 _ux_utility_semaphore_put(&transfer_request -> ux_transfer_request_semaphore);
294 }
295
getting_csw(struct UX_TEST_ACTION_STRUCT * action,VOID * params)296 static VOID getting_csw(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
297 {
298
299 UCHAR *csw = (UCHAR *)storage -> ux_host_class_storage_csw;
300 UX_TRANSFER *transfer_request = &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request;
301
302
303 _ux_utility_memory_copy(csw, csw_buffer, 13);
304 _ux_utility_semaphore_put(&transfer_request -> ux_transfer_request_semaphore);
305 }
306
307 static UX_TEST_HCD_SIM_ACTION replace_cbw_data_csw[] = {
308 /* function, request to match,
309 port action, port status,
310 request action, request EP, request data, request actual length, request status,
311 status, additional callback,
312 no_return */
313 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
314 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
315 UX_TEST_MATCH_EP, 0x02, UX_NULL, 31, UX_SUCCESS,
316 UX_SUCCESS,
317 .action_func = sending_cbw,
318 .no_return = UX_FALSE,
319 .do_after = UX_FALSE},
320 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
321 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
322 UX_TEST_MATCH_EP | UX_TEST_SIM_REQ_ANSWER, 0x81, buffer, 63, UX_SUCCESS,
323 UX_SUCCESS,
324 .action_func = getting_data,
325 .no_return = UX_FALSE,
326 .do_after = UX_FALSE},
327 { UX_HCD_TRANSFER_REQUEST, UX_NULL,
328 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
329 UX_TEST_MATCH_EP | UX_TEST_SIM_REQ_ANSWER, 0x81, csw_buffer, 13, UX_SUCCESS,
330 UX_SUCCESS,
331 .action_func = getting_csw,
332 .no_return = UX_FALSE,
333 .do_after = UX_FALSE},
334 { 0 }
335 };
336
337 /* Define the ISR dispatch. */
338
339 extern VOID (*test_isr_dispatch)(void);
340
341
342 /* Prototype for test control return. */
343
344 void test_control_return(UINT status);
345
346
347 /* Define the ISR dispatch routine. */
348
test_isr(void)349 static void test_isr(void)
350 {
351
352 /* For further expansion of interrupt-level testing. */
353 }
354
355
error_callback(UINT system_level,UINT system_context,UINT error_code)356 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
357 {
358
359 error_callback_counter ++;
360
361 if (!error_callback_ignore)
362 {
363 {
364 /* Failed test. */
365 printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
366 test_control_return(1);
367 }
368 }
369 }
370
host_storage_instance_get(ULONG timeout_x10ms)371 static UINT host_storage_instance_get(ULONG timeout_x10ms)
372 {
373
374 UINT status;
375 UX_HOST_CLASS *class;
376
377
378 /* Find the main storage container */
379 status = ux_host_stack_class_get(_ux_system_host_class_storage_name, &class);
380 if (status != UX_SUCCESS)
381 return(status);
382
383 /* Get storage instance, wait it to be live and media attached. */
384 do
385 {
386 if (timeout_x10ms)
387 {
388 ux_utility_delay_ms(10);
389 if (timeout_x10ms != 0xFFFFFFFF)
390 timeout_x10ms --;
391 }
392
393 status = ux_host_stack_class_instance_get(class, 0, (void **) &storage);
394 if (status == UX_SUCCESS)
395 {
396 if (storage -> ux_host_class_storage_state == UX_HOST_CLASS_INSTANCE_LIVE &&
397 class -> ux_host_class_media != UX_NULL)
398 return(UX_SUCCESS);
399 }
400
401 } while(timeout_x10ms > 0);
402
403 return(UX_ERROR);
404 }
405
sleep_break_on_error(VOID)406 static UINT sleep_break_on_error(VOID)
407 {
408
409 if (error_callback_counter >= 3)
410 return error_callback_counter;
411
412 return UX_SUCCESS;
413 }
414
415
416 /* Define what the initial system looks like. */
417
418 #ifdef CTEST
test_application_define(void * first_unused_memory)419 void test_application_define(void *first_unused_memory)
420 #else
421 void usbx_ux_host_class_storage_entry_test_application_define(void *first_unused_memory)
422 #endif
423 {
424
425 UINT status;
426 CHAR * stack_pointer;
427 CHAR * memory_pointer;
428
429
430 /* Inform user. */
431 printf("Running ux_host_class_storage_entry Test............................ ");
432 stepinfo("\n");
433
434 /* Initialize the free memory pointer */
435 stack_pointer = (CHAR *) usbx_memory;
436 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
437
438 /* Initialize USBX. Memory */
439 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
440
441 /* Check for error. */
442 if (status != UX_SUCCESS)
443 {
444
445 printf("ERROR #%d\n", __LINE__);
446 test_control_return(1);
447 }
448
449 /* Register the error callback. */
450 _ux_utility_error_callback_register(error_callback);
451
452 /* Reset ram disks memory. */
453 ux_utility_memory_set(ram_disk_memory1, 0, UX_RAM_DISK_SIZE);
454 ux_utility_memory_set(ram_disk_memory2, 0, UX_RAM_DISK_SIZE);
455
456 /* Initialize FileX. */
457 fx_system_initialize();
458
459 /* Change the ram drive values. */
460 fx_media_format(&ram_disk_media1, _fx_ram_driver, ram_disk_memory1, ram_disk_buffer1, 512, "RAM DISK1", 2, 512, 0, UX_RAM_DISK_SIZE/512, 512, 4, 1, 1);
461 fx_media_format(&ram_disk_media2, _fx_ram_driver, ram_disk_memory2, ram_disk_buffer2, 512, "RAM DISK2", 2, 512, 0, UX_RAM_DISK_SIZE/512, 512, 4, 1, 1);
462
463 /* The code below is required for installing the device portion of USBX.
464 In this demo, DFU is possible and we have a call back for state change. */
465 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
466 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
467 string_framework, STRING_FRAMEWORK_LENGTH,
468 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
469 if(status!=UX_SUCCESS)
470 {
471
472 printf("ERROR #%d\n", __LINE__);
473 test_control_return(1);
474 }
475
476 /* Store the number of LUN in this device storage instance. */
477 global_storage_parameter.ux_slave_class_storage_parameter_number_lun = 2;
478
479 /* Initialize the storage class parameters for reading/writing to the first Flash Disk. */
480 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_last_lba = UX_RAM_DISK_LAST_LBA;
481 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length = 512;
482 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_type = 0;
483 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_removable_flag = 0x80;
484 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_read = demo_thread_media_read;
485 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_write = demo_thread_media_write;
486 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_status = demo_thread_media_status;
487 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_flush = demo_thread_media_flush;
488
489 /* Initialize the storage class parameters for reading/writing to the second Flash Disk. */
490 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_last_lba = UX_RAM_DISK_LAST_LBA;
491 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_block_length = 512;
492 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_type = 0;
493 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_removable_flag = 0x80;
494 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_read = demo_thread_media_read;
495 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_write = demo_thread_media_write;
496 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_status = demo_thread_media_status;
497 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_flush = demo_thread_media_flush;
498
499 /* Initialize the device storage class. The class is connected with interface 0 on configuration 1. */
500 status = ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
501 1, 0, (VOID *)&global_storage_parameter);
502 if(status!=UX_SUCCESS)
503 {
504
505 printf("ERROR #%d\n", __LINE__);
506 test_control_return(1);
507 }
508
509 /* Initialize the simulated device controller. */
510 // status = _ux_test_dcd_sim_slave_initialize();
511 status = ux_dcd_sim_slave_initialize();
512
513 /* Check for error. */
514 if (status != UX_SUCCESS)
515 {
516
517 printf("ERROR #%d\n", __LINE__);
518 test_control_return(1);
519 }
520
521 /* The code below is required for installing the host portion of USBX */
522 status = ux_host_stack_initialize(UX_NULL);
523 if (status != UX_SUCCESS)
524 {
525
526 printf("ERROR #%d\n", __LINE__);
527 test_control_return(1);
528 }
529
530 /* Register storage class. */
531 status = ux_host_stack_class_register(_ux_system_host_class_storage_name, ux_host_class_storage_entry);
532 if (status != UX_SUCCESS)
533 {
534
535 printf("ERROR #%d\n", __LINE__);
536 test_control_return(1);
537 }
538
539 /* Register all the USB host controllers available in this system */
540 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
541 // status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
542
543 /* Check for error. */
544 if (status != UX_SUCCESS)
545 {
546
547 printf("ERROR #%d\n", __LINE__);
548 test_control_return(1);
549 }
550
551 /* Create the main host simulation thread. */
552 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
553 stack_pointer, UX_DEMO_STACK_SIZE,
554 20, 20, 1, TX_AUTO_START);
555
556 /* Check for error. */
557 if (status != TX_SUCCESS)
558 {
559
560 printf("ERROR #%d\n", __LINE__);
561 test_control_return(1);
562 }
563
564 }
565
storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA * storage_media,ULONG status,ULONG timeout)566 static UINT storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA *storage_media, ULONG status, ULONG timeout)
567 {
568
569 while(1)
570 {
571 #if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
572 if (storage_media->ux_host_class_storage_media_status == status)
573 return UX_SUCCESS;
574 #else
575 if ((status == UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED &&
576 storage_media->ux_host_class_storage_media_storage != UX_NULL) ||
577 (status == UX_HOST_CLASS_STORAGE_MEDIA_UNMOUNTED &&
578 storage_media->ux_host_class_storage_media_storage == UX_NULL))
579 return(UX_SUCCESS);
580 #endif
581 if (timeout == 0)
582 break;
583 if (timeout != 0xFFFFFFFF)
584 timeout --;
585 _ux_utility_delay_ms(10);
586 }
587 return UX_ERROR;
588 }
589
590
tx_demo_thread_host_simulation_entry(ULONG arg)591 static void tx_demo_thread_host_simulation_entry(ULONG arg)
592 {
593
594 UINT status;
595 UX_HOST_CLASS *class;
596 UX_HOST_CLASS_STORAGE_EXT *storage_ext0, *storage_ext1;
597 UX_HOST_CLASS_STORAGE_MEDIA *storage_media;
598 UX_ENDPOINT *control_endpoint;
599 UX_TRANSFER *transfer_request;
600 UX_DEVICE *device;
601 UX_CONFIGURATION *configuration;
602 UX_INTERFACE *interface;
603 ULONG rfree, cfree;
604 FX_MEDIA *media;
605 UX_HOST_CLASS_COMMAND command;
606
607
608 /* Find the storage class. */
609 status = host_storage_instance_get(100);
610 if (status != UX_SUCCESS)
611 {
612 printf("ERROR #%d, code 0x%x\n", __LINE__, status);
613 test_control_return(1);
614 }
615
616 status = ux_host_stack_device_get(0, &device);
617 if (status != UX_SUCCESS)
618 {
619
620 printf("ERROR #%d: device_get fail\n", __LINE__);
621 test_control_return(1);
622 }
623 control_endpoint = &device->ux_device_control_endpoint;
624 transfer_request = &control_endpoint->ux_endpoint_transfer_request;
625
626 /* Wait enough time for media mounting. */
627 _ux_utility_delay_ms(UX_HOST_CLASS_STORAGE_DEVICE_INIT_DELAY);
628
629 class = storage->ux_host_class_storage_class;
630 storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *)class->ux_host_class_media;
631 #if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
632 media = &storage_media->ux_host_class_storage_media;
633 #endif
634
635 /* Confirm media enum done (). */
636 status = storage_media_status_wait(storage_media, UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED, 100);
637 if (status != UX_SUCCESS)
638 {
639 printf("ERROR #%d, code 0x%x\n", __LINE__, status);
640 test_control_return(1);
641 }
642
643 /* Pause the class driver thread. */
644 _ux_utility_thread_suspend(&((UX_HOST_CLASS_STORAGE_EXT*)class->ux_host_class_ext)->ux_host_class_thread);
645
646 configuration = device -> ux_device_first_configuration;
647 interface = configuration -> ux_configuration_first_interface;
648
649 /* Disconnect. */
650 // Seems there is memory problem if do resource free here???!!!
651 // _ux_utility_thread_delete(&((UX_HOST_CLASS_STORAGE_EXT*)class->ux_host_class_ext)->ux_host_class_thread);
652 // _ux_utility_memory_free(class->ux_host_class_media);
653 // _ux_utility_memory_free(class->ux_host_class_ext);
654 class->ux_host_class_media = UX_NULL;
655 class->ux_host_class_ext = UX_NULL;
656
657 /* Command. */
658 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_ACTIVATE;
659 command.ux_host_class_command_class_ptr = class;
660 command.ux_host_class_command_container = interface;
661
662 rfree = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
663 cfree = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_CACHE_SAFE] -> ux_byte_pool_available;
664
665 stepinfo(">>>>>>>>>>>>>>> ux_host_class_storage_entry - UX_HOST_CLASS_COMMAND_ACTIVATE ext memory ERROR\n");
666 ux_test_utility_sim_mem_alloc_fail_all_start();
667 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_ACTIVATE;
668 status = _ux_host_class_storage_entry(&command);
669 ux_test_utility_sim_mem_alloc_fail_all_stop();
670 if (status != UX_MEMORY_INSUFFICIENT)
671 {
672 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
673 test_control_return(1);
674 }
675 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_DESTROY;
676 _ux_host_class_storage_entry(&command);
677
678 stepinfo(">>>>>>>>>>>>>>> ux_host_class_storage_entry - UX_HOST_CLASS_COMMAND_ACTIVATE media memory ERROR\n");
679 ux_test_utility_sim_mem_allocate_until(sizeof(UX_HOST_CLASS_STORAGE_EXT) + sizeof(UX_HOST_CLASS_STORAGE_MEDIA) / 2);
680 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_ACTIVATE;
681 status = _ux_host_class_storage_entry(&command);
682 ux_test_utility_sim_mem_free_all();
683 if (status != UX_MEMORY_INSUFFICIENT)
684 {
685 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
686 test_control_return(1);
687 }
688 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_DESTROY;
689 _ux_host_class_storage_entry(&command);
690
691 #if 0 /* No need since stack is allocated with ext. */
692 stepinfo(">>>>>>>>>>>>>>> ux_host_class_storage_entry - UX_HOST_CLASS_COMMAND_ACTIVATE stack memory ERROR\n");
693 ux_test_utility_sim_mem_allocate_until(UX_HOST_CLASS_STORAGE_MAX_MEDIA*sizeof(UX_HOST_CLASS_STORAGE_MEDIA));
694 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_ACTIVATE;
695 status = _ux_host_class_storage_entry(&command);
696 ux_test_utility_sim_mem_free_all();
697 if (status != UX_MEMORY_INSUFFICIENT)
698 {
699 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
700 test_control_return(1);
701 }
702 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_DESTROY;
703 _ux_host_class_storage_entry(&command);
704 #endif
705
706 stepinfo(">>>>>>>>>>>>>>> ux_host_class_storage_entry - UX_HOST_CLASS_COMMAND_ACTIVATE thread ERROR\n");
707 storage_ext0=_ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_STORAGE_EXT) + 128);
708 storage_ext1=_ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_STORAGE_EXT));
709 _ux_utility_thread_create(&storage_ext1->ux_host_class_thread,
710 "tmp", _ux_host_class_storage_thread_entry, (ULONG)(ALIGN_TYPE)class,
711 storage_ext0->ux_host_class_thread_stack, UX_HOST_CLASS_STORAGE_THREAD_STACK_SIZE,
712 UX_HOST_CLASS_STORAGE_THREAD_PRIORITY_CLASS, UX_HOST_CLASS_STORAGE_THREAD_PRIORITY_CLASS,
713 TX_NO_TIME_SLICE, TX_DONT_START);
714 UX_THREAD_EXTENSION_PTR_SET(&storage_ext1->ux_host_class_thread, class)
715 _ux_utility_memory_free(storage_ext0);
716 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_ACTIVATE;
717 status = _ux_host_class_storage_entry(&command);
718 if (status != UX_THREAD_ERROR)
719 {
720 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
721 test_control_return(1);
722 }
723 _ux_utility_thread_delete(&storage_ext1->ux_host_class_thread);
724 _ux_utility_memory_free(storage_ext1);
725 command.ux_host_class_command_request = UX_HOST_CLASS_COMMAND_DESTROY;
726 _ux_host_class_storage_entry(&command);
727
728 /* Wait a while so the thread goes. */
729 _ux_utility_delay_ms(10);
730
731 /* Finally disconnect the device. */
732 ux_device_stack_disconnect();
733
734 /* And deinitialize the class. */
735 status = ux_device_stack_class_unregister(_ux_system_slave_class_storage_name, ux_device_class_storage_entry);
736
737 /* Deinitialize the device side of usbx. */
738 _ux_device_stack_uninitialize();
739
740 /* And finally the usbx system resources. */
741 _ux_system_uninitialize();
742
743 /* Successful test. */
744 printf("SUCCESS!\n");
745 test_control_return(0);
746 }
747
748
demo_thread_media_status(VOID * storage,ULONG lun,ULONG media_id,ULONG * media_status)749 static UINT demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
750 {
751
752 UINT status = ram_disk_status;
753
754
755 (void)storage;
756 (void)media_id;
757
758 if (media_status)
759 *media_status = ram_disk_media_status;
760
761 /* If there is attention, it must be changed to ready after reported. */
762 if (ram_disk_media_status == (UX_SLAVE_CLASS_STORAGE_SENSE_KEY_UNIT_ATTENTION | (0x28 << 8)))
763 {
764 ram_disk_status = UX_SUCCESS;
765 ram_disk_media_status = 0;
766 }
767 else
768 ram_disk_status_sent = UX_TRUE;
769
770 return status;
771 }
772
demo_thread_media_read(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)773 static UINT demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
774 {
775 (void)storage;
776
777 if (lun > 1)
778 return UX_ERROR;
779
780 ram_disk_read_sent = UX_TRUE;
781 if (ram_disk_read_status != UX_SUCCESS)
782 {
783 if (media_status != UX_NULL)
784 *media_status = ram_disk_read_media_status;
785
786 return ram_disk_read_status;
787 }
788
789 ux_utility_memory_copy(data_pointer, &ram_disk_memory[lun][lba * 512], number_blocks * 512);
790
791 return UX_SUCCESS;
792 }
793
demo_thread_media_write(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)794 static UINT demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
795 {
796 (void)storage;
797
798 if (lun > 1)
799 return UX_ERROR;
800
801 ram_disk_write_sent = UX_TRUE;
802 if (ram_disk_write_status != UX_SUCCESS)
803 {
804 if (media_status != UX_NULL)
805 *media_status = ram_disk_write_media_status;
806
807 return ram_disk_write_status;
808 }
809
810 ux_utility_memory_copy(&ram_disk_memory[lun][lba * 512], data_pointer, number_blocks * 512);
811
812 return UX_SUCCESS;
813 }
814
demo_thread_media_flush(VOID * storage,ULONG lun,ULONG number_blocks,ULONG lba,ULONG * media_status)815 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status)
816 {
817 (void)storage;
818 (void)number_blocks;
819 (void)lba;
820 (void)media_status;
821
822 if (lun > 1)
823 return UX_ERROR;
824
825 ram_disk_flush = UX_TRUE;
826 return ram_disk_flush_status;
827 }