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