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