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