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