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