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