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 
186 /* Define the ISR dispatch.  */
187 
188 extern VOID    (*test_isr_dispatch)(void);
189 
190 
191 /* Prototype for test control return.  */
192 
193 void  test_control_return(UINT status);
194 
195 
196 /* Define the ISR dispatch routine.  */
197 
test_isr(void)198 static void    test_isr(void)
199 {
200 
201     /* For further expansion of interrupt-level testing.  */
202 }
203 
204 
error_callback(UINT system_level,UINT system_context,UINT error_code)205 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
206 {
207 
208     error_callback_counter ++;
209 
210     if (!error_callback_ignore)
211     {
212         {
213             /* Failed test.  */
214             printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
215             test_control_return(1);
216         }
217     }
218 }
219 
host_storage_instance_get(ULONG timeout_x10ms)220 static UINT host_storage_instance_get(ULONG timeout_x10ms)
221 {
222 
223 UINT                status;
224 UX_HOST_CLASS       *class;
225 
226 
227     /* Find the main storage container */
228     status =  ux_host_stack_class_get(_ux_system_host_class_storage_name, &class);
229     if (status != UX_SUCCESS)
230         return(status);
231 
232     /* Get storage instance, wait it to be live and media attached.  */
233     do
234     {
235         if (timeout_x10ms)
236         {
237             ux_utility_delay_ms(10);
238             if (timeout_x10ms != 0xFFFFFFFF)
239                 timeout_x10ms --;
240         }
241 
242         status =  ux_host_stack_class_instance_get(class, 0, (void **) &storage);
243         if (status == UX_SUCCESS)
244         {
245             if (storage -> ux_host_class_storage_state == UX_HOST_CLASS_INSTANCE_LIVE &&
246                 class -> ux_host_class_media != UX_NULL)
247                 return(UX_SUCCESS);
248         }
249 
250     } while(timeout_x10ms > 0);
251 
252     return(UX_ERROR);
253 }
254 
sleep_break_on_error(VOID)255 static UINT  sleep_break_on_error(VOID)
256 {
257 
258     if (error_callback_counter >= 3)
259         return error_callback_counter;
260 
261     return UX_SUCCESS;
262 }
263 
264 
265 /* Define what the initial system looks like.  */
266 
267 #ifdef CTEST
test_application_define(void * first_unused_memory)268 void test_application_define(void *first_unused_memory)
269 #else
270 void    usbx_ux_device_class_storage_synchronize_cache_test_application_define(void *first_unused_memory)
271 #endif
272 {
273 
274 UINT                            status;
275 CHAR *                          stack_pointer;
276 CHAR *                          memory_pointer;
277 
278 
279     /* Inform user.  */
280     printf("Running ux_device_class_storage_synchronize_cache Test.............. ");
281     stepinfo("\n");
282 
283     /* Initialize the free memory pointer */
284     stack_pointer = (CHAR *) usbx_memory;
285     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
286 
287     /* Initialize USBX. Memory */
288     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
289 
290     /* Check for error.  */
291     if (status != UX_SUCCESS)
292     {
293 
294         printf("ERROR #%d\n", __LINE__);
295         test_control_return(1);
296     }
297 
298     /* Register the error callback. */
299     _ux_utility_error_callback_register(error_callback);
300 
301     /* Reset ram disks memory.  */
302     ux_utility_memory_set(ram_disk_memory1, 0, UX_RAM_DISK_SIZE);
303     ux_utility_memory_set(ram_disk_memory2, 0, UX_RAM_DISK_SIZE);
304 
305     /* Initialize FileX.  */
306     fx_system_initialize();
307 
308     /* Change the ram drive values. */
309     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);
310     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);
311 
312     /* The code below is required for installing the device portion of USBX.
313        In this demo, DFU is possible and we have a call back for state change. */
314     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
315                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
316                                        string_framework, STRING_FRAMEWORK_LENGTH,
317                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
318     if(status!=UX_SUCCESS)
319     {
320 
321         printf("ERROR #%d\n", __LINE__);
322         test_control_return(1);
323     }
324 
325     /* Store the number of LUN in this device storage instance.  */
326     global_storage_parameter.ux_slave_class_storage_parameter_number_lun = 1;
327 
328     /* Initialize the storage class parameters for reading/writing to the first Flash Disk.  */
329     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_last_lba        =  UX_RAM_DISK_LAST_LBA;
330     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length    =  512;
331     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_type            =  0;
332     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_removable_flag  =  0x80;
333     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_read            =  demo_thread_media_read;
334     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_write           =  demo_thread_media_write;
335     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_status          =  demo_thread_media_status;
336     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_flush           =  demo_thread_media_flush;
337 
338     /* Initialize the storage class parameters for reading/writing to the second Flash Disk.  */
339     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_last_lba        =  UX_RAM_DISK_LAST_LBA;
340     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_block_length    =  512;
341     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_type            =  0;
342     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_removable_flag  =  0x80;
343     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_read            =  demo_thread_media_read;
344     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_write           =  demo_thread_media_write;
345     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_status          =  demo_thread_media_status;
346     global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_flush           =  demo_thread_media_flush;
347 
348     /* Initialize the device storage class. The class is connected with interface 0 on configuration 1. */
349     status =  ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
350                                                 1, 0, (VOID *)&global_storage_parameter);
351     if(status!=UX_SUCCESS)
352     {
353 
354         printf("ERROR #%d\n", __LINE__);
355         test_control_return(1);
356     }
357 
358     /* Initialize the simulated device controller.  */
359     status =  _ux_dcd_sim_slave_initialize();
360 
361     /* Check for error.  */
362     if (status != UX_SUCCESS)
363     {
364 
365         printf("ERROR #%d\n", __LINE__);
366         test_control_return(1);
367     }
368 
369     /* The code below is required for installing the host portion of USBX */
370     status =  ux_host_stack_initialize(UX_NULL);
371     if (status != UX_SUCCESS)
372     {
373 
374         printf("ERROR #%d\n", __LINE__);
375         test_control_return(1);
376     }
377 
378     /* Register storage class.  */
379     status =  ux_host_stack_class_register(_ux_system_host_class_storage_name, ux_host_class_storage_entry);
380     if (status != UX_SUCCESS)
381     {
382 
383         printf("ERROR #%d\n", __LINE__);
384         test_control_return(1);
385     }
386 
387     /* Register all the USB host controllers available in this system */
388     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
389 
390     /* Check for error.  */
391     if (status != UX_SUCCESS)
392     {
393 
394         printf("ERROR #%d\n", __LINE__);
395         test_control_return(1);
396     }
397 
398     /* Create the main host simulation thread.  */
399     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
400             stack_pointer, UX_DEMO_STACK_SIZE,
401             20, 20, 1, TX_AUTO_START);
402 
403     /* Check for error.  */
404     if (status != TX_SUCCESS)
405     {
406 
407         printf("ERROR #%d\n", __LINE__);
408         test_control_return(1);
409     }
410 
411 }
412 
storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA * storage_media,ULONG status,ULONG timeout)413 static UINT storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA *storage_media, ULONG status, ULONG timeout)
414 {
415 
416     while(1)
417     {
418 #if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
419         if (storage_media->ux_host_class_storage_media_status == status)
420             return UX_SUCCESS;
421 #else
422         if ((status == UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED &&
423             storage_media->ux_host_class_storage_media_storage != UX_NULL) ||
424             (status == UX_HOST_CLASS_STORAGE_MEDIA_UNMOUNTED &&
425             storage_media->ux_host_class_storage_media_storage == UX_NULL))
426             return(UX_SUCCESS);
427 #endif
428         if (timeout == 0)
429             break;
430         if (timeout != 0xFFFFFFFF)
431             timeout --;
432         _ux_utility_delay_ms(10);
433     }
434     return UX_ERROR;
435 }
436 
test_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE * storage,UCHAR page_code,UCHAR * response,ULONG response_length)437 static UINT test_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE *storage, UCHAR page_code, UCHAR *response, ULONG response_length)
438 {
439 UINT            status;
440 UCHAR           *cbw;
441 UINT            command_length;
442 
443     /* Use a pointer for the cbw, easier to manipulate.  */
444     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
445 
446     /* Get the Write Command Length.  */
447     command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
448 
449     /* Initialize the CBW for this command.  */
450     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH, command_length);
451 
452     /* Prepare the INQUIRY command block.  */
453     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_INQUIRY;
454 
455     /* Store the page code.  */
456     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_PAGE_CODE) = page_code;
457 
458     /* Store the length of the Inquiry Response.  */
459     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_ALLOCATION_LENGTH) = (UCHAR)response_length;
460 
461     /* Send the command to transport layer.  */
462     status =  _ux_host_class_storage_transport(storage, response);
463 
464     /* Return completion status.  */
465     return(status);
466 }
467 
_test_init_cbw_SYNCHRONIZE_CACHE(UCHAR op16,UCHAR immed,ULONG lba,ULONG nb_blocks)468 static void  _test_init_cbw_SYNCHRONIZE_CACHE(UCHAR op16, UCHAR immed, ULONG lba, ULONG nb_blocks)
469 {
470 
471 UCHAR           *cbw;
472 UINT            command_length;
473 
474 
475     (void)op16;
476 
477     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
478     command_length =  UX_HOST_CLASS_STORAGE_TEST_READY_COMMAND_LENGTH_SBC;
479     _ux_host_class_storage_cbw_initialize(storage, 0, 0, command_length);
480     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + 0) = UX_SLAVE_CLASS_STORAGE_SCSI_SYNCHRONIZE_CACHE;
481     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_FLAGS) = immed ? UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_FLAGS_IMMED : 0;
482     _ux_utility_long_put_big_endian(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_LBA, lba);
483     _ux_utility_short_put_big_endian(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_NUMBER_OF_BLOCKS, (USHORT)nb_blocks);
484 }
485 
_test_send_cbw(void)486 static UINT _test_send_cbw(void)
487 {
488 
489 UX_TRANSFER     *transfer_request;
490 UINT            status;
491 UCHAR           *cbw;
492 
493 
494     transfer_request =  &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
495     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
496 
497     transfer_request -> ux_transfer_request_data_pointer =      cbw;
498     transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_STORAGE_CBW_LENGTH;
499     status =  ux_host_stack_transfer_request(transfer_request);
500 
501     /* There is error, return the error code.  */
502     if (status != UX_SUCCESS)
503         return(status);
504 
505     /* Wait transfer done.  */
506     status =  _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
507 
508     /* No error, it's done.  */
509     if (status == UX_SUCCESS)
510         return(transfer_request->ux_transfer_request_completion_code);
511 
512     /* All transfers pending need to abort. There may have been a partial transfer.  */
513     ux_host_stack_transfer_request_abort(transfer_request);
514 
515     /* Set the completion code.  */
516     transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
517 
518     /* There was an error, return to the caller.  */
519     return(UX_TRANSFER_TIMEOUT);
520 }
521 
_test_transfer_data(UCHAR * data,ULONG size,UCHAR do_read)522 static UINT _test_transfer_data(UCHAR *data, ULONG size, UCHAR do_read)
523 {
524 
525 UX_TRANSFER     *transfer_request;
526 UINT            status;
527 
528 
529     transfer_request =  do_read ?
530             &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request :
531             &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
532     transfer_request -> ux_transfer_request_data_pointer = data;
533     transfer_request -> ux_transfer_request_requested_length =  size;
534 
535     status =  ux_host_stack_transfer_request(transfer_request);
536 
537     /* There is error, return the error code.  */
538     if (status != UX_SUCCESS)
539         return(status);
540 
541     /* Wait transfer done.  */
542     status =  _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
543 
544     /* No error, it's done.  */
545     if (status == UX_SUCCESS)
546         return(transfer_request->ux_transfer_request_completion_code);
547 
548     /* All transfers pending need to abort. There may have been a partial transfer.  */
549     ux_host_stack_transfer_request_abort(transfer_request);
550 
551     /* Set the completion code.  */
552     transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
553 
554     /* There was an error, return to the caller.  */
555     return(UX_TRANSFER_TIMEOUT);
556 }
557 
_test_wait_csw(void)558 static UINT _test_wait_csw(void)
559 {
560 
561 UX_TRANSFER     *transfer_request;
562 UINT            status;
563 
564 
565     /* Get the pointer to the transfer request, on the bulk in endpoint.  */
566     transfer_request =  &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request;
567 
568     /* Fill in the transfer_request parameters.  */
569     transfer_request -> ux_transfer_request_data_pointer =      (UCHAR *) &storage -> ux_host_class_storage_csw;
570     transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_STORAGE_CSW_LENGTH;
571 
572     /* Get the CSW on the bulk in endpoint.  */
573     status =  ux_host_stack_transfer_request(transfer_request);
574     if (status != UX_SUCCESS)
575         return(status);
576 
577     /* Wait for the completion of the transfer request.  */
578     status =  _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
579 
580     /* If OK, we are done.  */
581     if (status == UX_SUCCESS)
582         return(transfer_request->ux_transfer_request_completion_code);
583 
584     /* All transfers pending need to abort. There may have been a partial transfer.  */
585     ux_host_stack_transfer_request_abort(transfer_request);
586 
587     /* Set the completion code.  */
588     transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
589 
590     /* There was an error, return to the caller.  */
591     return(UX_TRANSFER_TIMEOUT);
592 }
593 
_test_clear_stall(UCHAR clear_read_stall)594 static VOID _test_clear_stall(UCHAR clear_read_stall)
595 {
596 
597 UX_ENDPOINT     *endpoint;
598 
599 
600     endpoint =  clear_read_stall ?
601             storage -> ux_host_class_storage_bulk_in_endpoint :
602             storage -> ux_host_class_storage_bulk_out_endpoint;
603     _ux_host_stack_endpoint_reset(endpoint);
604 }
605 
_test_request_sense(void)606 static UINT _test_request_sense(void)
607 {
608 
609 UINT            status;
610 UX_TRANSFER     *transfer_request;
611 UCHAR           *cbw;
612 UCHAR           *request_sense_response;
613 ULONG           sense_code;
614 UINT            command_length = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
615 
616 
617     transfer_request =  &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
618     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
619 
620     _ux_utility_memory_copy(storage -> ux_host_class_storage_saved_cbw, storage -> ux_host_class_storage_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
621     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, command_length);
622     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE;
623     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_ALLOCATION_LENGTH) =  UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
624     request_sense_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
625     if (request_sense_response == UX_NULL)
626         return(UX_MEMORY_INSUFFICIENT);
627     status = _test_send_cbw();
628     if (status == UX_SUCCESS)
629     {
630         status = _test_transfer_data(request_sense_response, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, UX_TRUE);
631         if (status == UX_SUCCESS)
632         {
633             status = _test_wait_csw();
634             if (status == UX_SUCCESS)
635             {
636 
637                 sense_code =  (((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY)) & 0x0f) << 16;
638                 sense_code |=  ((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE)) << 8;
639                 sense_code |=  (ULONG)  *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER);
640 
641                 storage -> ux_host_class_storage_sense_code =  sense_code;
642             }
643         }
644     }
645     _ux_utility_memory_free(request_sense_response);
646     _ux_utility_memory_copy(storage -> ux_host_class_storage_cbw, storage -> ux_host_class_storage_saved_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
647     return(status);
648 }
649 
tx_demo_thread_host_simulation_entry(ULONG arg)650 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
651 {
652 
653 UINT                                        status;
654 UX_HOST_CLASS                               *class;
655 UX_HOST_CLASS_STORAGE_MEDIA                 *storage_media;
656 UX_ENDPOINT                                 *control_endpoint;
657 UX_TRANSFER                                 *transfer_request;
658 UX_DEVICE                                   *device;
659 
660 
661     /* Find the storage class. */
662     status =  host_storage_instance_get(100);
663     if (status != UX_SUCCESS)
664     {
665         printf("ERROR #%d\n", __LINE__);
666         test_control_return(1);
667     }
668 
669     status = ux_host_stack_device_get(0, &device);
670     if (status != UX_SUCCESS)
671     {
672 
673         printf("ERROR #%d: device_get fail\n", __LINE__);
674         test_control_return(1);
675     }
676     control_endpoint = &device->ux_device_control_endpoint;
677     transfer_request = &control_endpoint->ux_endpoint_transfer_request;
678 
679     /* Wait enough time for media mounting.  */
680     _ux_utility_delay_ms(UX_HOST_CLASS_STORAGE_DEVICE_INIT_DELAY);
681 
682     class = storage->ux_host_class_storage_class;
683     storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *)class->ux_host_class_media;
684 
685     /* Confirm media enum done.  */
686     status = storage_media_status_wait(storage_media, UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED, 100);
687     if (status != UX_SUCCESS)
688     {
689         printf("ERROR #%d\n", __LINE__);
690         test_control_return(1);
691     }
692 
693     /* Pause the class driver thread.  */
694     _ux_utility_thread_suspend(&((UX_HOST_CLASS_STORAGE_EXT*)class->ux_host_class_ext)->ux_host_class_thread);
695 
696     stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_SYNCHRONIZE_CACHE - success\n");
697     _test_init_cbw_SYNCHRONIZE_CACHE(UX_FALSE, UX_FALSE, 0, 1);
698     status = _test_send_cbw();
699     if (status != UX_SUCCESS)
700     {
701         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
702         test_control_return(1);
703     }
704     status = _test_wait_csw();
705     if (status != UX_SUCCESS)
706     {
707         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
708         test_control_return(1);
709     }
710 
711     stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_SYNCHRONIZE_CACHE - status fail\n");
712     _test_init_cbw_SYNCHRONIZE_CACHE(UX_FALSE, UX_FALSE, 0, 1);
713     ram_disk_status = UX_ERROR;
714     status = _test_send_cbw();
715     if (status != UX_SUCCESS)
716     {
717         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
718         test_control_return(1);
719     }
720     status = _test_wait_csw();
721     if (status != UX_TRANSFER_STALLED)
722     {
723         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
724         test_control_return(1);
725     }
726     _test_clear_stall(UX_TRUE);
727     status = _test_wait_csw();
728     if (status != UX_SUCCESS)
729     {
730         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
731         test_control_return(1);
732     }
733     status = _test_request_sense();
734     if (status != UX_SUCCESS)
735     {
736         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
737         test_control_return(1);
738     }
739     ram_disk_status = UX_SUCCESS;
740 
741     stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_SYNCHRONIZE_CACHE - flush fail\n");
742     _test_init_cbw_SYNCHRONIZE_CACHE(UX_FALSE, UX_FALSE, 0, 1);
743     ram_disk_flush_status = UX_ERROR;
744     status = _test_send_cbw();
745     if (status != UX_SUCCESS)
746     {
747         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
748         test_control_return(1);
749     }
750     status = _test_wait_csw();
751     if (status != UX_TRANSFER_STALLED)
752     {
753         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
754         test_control_return(1);
755     }
756     _test_clear_stall(UX_TRUE);
757     status = _test_wait_csw();
758     if (status != UX_SUCCESS)
759     {
760         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
761         test_control_return(1);
762     }
763     ram_disk_flush_status = UX_SUCCESS;
764 
765     /* Finally disconnect the device. */
766     ux_device_stack_disconnect();
767 
768     /* And deinitialize the class.  */
769     status =  ux_device_stack_class_unregister(_ux_system_slave_class_storage_name, ux_device_class_storage_entry);
770 
771     /* Deinitialize the device side of usbx.  */
772     _ux_device_stack_uninitialize();
773 
774     /* And finally the usbx system resources.  */
775     _ux_system_uninitialize();
776 
777     /* Successful test.  */
778     printf("SUCCESS!\n");
779     test_control_return(0);
780 }
781 
782 
demo_thread_media_status(VOID * storage,ULONG lun,ULONG media_id,ULONG * media_status)783 static UINT    demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
784 {
785 
786 UINT status = ram_disk_status;
787 
788 
789     (void)storage;
790     (void)media_id;
791 
792     if (media_status)
793         *media_status = ram_disk_media_status;
794 
795     /* If there is attention, it must be changed to ready after reported.  */
796     if (ram_disk_media_status == (UX_SLAVE_CLASS_STORAGE_SENSE_KEY_UNIT_ATTENTION | (0x28 << 8)))
797     {
798         ram_disk_status = UX_SUCCESS;
799         ram_disk_media_status = 0;
800     }
801     else
802         ram_disk_status_sent = UX_TRUE;
803 
804     return status;
805 }
806 
demo_thread_media_read(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)807 static UINT    demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
808 {
809     (void)storage;
810     (void)media_status;
811 
812     if (lun > 1)
813         return UX_ERROR;
814 
815     ux_utility_memory_copy(data_pointer, &ram_disk_memory[lun][lba * 512], number_blocks * 512);
816 
817     return UX_SUCCESS;
818 }
819 
demo_thread_media_write(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)820 static UINT    demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
821 {
822     (void)storage;
823     (void)media_status;
824 
825     if (lun > 1)
826         return UX_ERROR;
827 
828     ux_utility_memory_copy(&ram_disk_memory[lun][lba * 512], data_pointer, number_blocks * 512);
829 
830     return UX_SUCCESS;
831 }
832 
demo_thread_media_flush(VOID * storage,ULONG lun,ULONG number_blocks,ULONG lba,ULONG * media_status)833 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status)
834 {
835     (void)storage;
836     (void)number_blocks;
837     (void)lba;
838     (void)media_status;
839 
840     if (lun > 1)
841         return UX_ERROR;
842 
843     ram_disk_flush = UX_TRUE;
844     return ram_disk_flush_status;
845 }