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