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 Out) */
108         0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
109 
110     /* Endpoint descriptor (Bulk In) */
111         0x07, 0x05, 0x81, 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 Out) */
137         0x07, 0x05, 0x02, 0x02, 0x00, 0x01, 0x00,
138 
139     /* Endpoint descriptor (Bulk In) */
140         0x07, 0x05, 0x81, 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_initialize_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_initialize 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_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE * storage,UCHAR page_code,UCHAR * response,ULONG response_length)431 static UINT test_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE *storage, UCHAR page_code, UCHAR *response, ULONG response_length)
432 {
433 UINT            status;
434 UCHAR           *cbw;
435 UINT            command_length;
436 
437     /* Use a pointer for the cbw, easier to manipulate.  */
438     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
439 
440     /* Get the Write Command Length.  */
441     command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
442 
443     /* Initialize the CBW for this command.  */
444     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH, command_length);
445 
446     /* Prepare the INQUIRY command block.  */
447     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_INQUIRY;
448 
449     /* Store the page code.  */
450     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_PAGE_CODE) = page_code;
451 
452     /* Store the length of the Inquiry Response.  */
453     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_ALLOCATION_LENGTH) =  (UCHAR)response_length;
454 
455     /* Send the command to transport layer.  */
456     status =  _ux_host_class_storage_transport(storage, response);
457 
458     /* Return completion status.  */
459     return(status);
460 }
461 
_test_init_cbw_MODE_SENSE(UCHAR op6,UCHAR page_code,ULONG buffer_length)462 static void  _test_init_cbw_MODE_SENSE(UCHAR op6, UCHAR page_code, ULONG buffer_length)
463 {
464 
465 UCHAR           *cbw;
466 UINT            command_length;
467 
468 
469     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
470     command_length =  UX_HOST_CLASS_STORAGE_TEST_READY_COMMAND_LENGTH_SBC;
471     _ux_host_class_storage_cbw_initialize(storage, 0, 0, command_length);
472     if (op6)
473     {
474         *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + 0) =  UX_SLAVE_CLASS_STORAGE_SCSI_MODE_SENSE_SHORT;
475         *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_SLAVE_CLASS_STORAGE_MODE_SENSE_PARAMETER_LIST_LENGTH_6) =  (UCHAR)buffer_length;
476     }
477     else
478     {
479         *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + 0) =  UX_SLAVE_CLASS_STORAGE_SCSI_MODE_SENSE;
480         _ux_utility_short_put_big_endian(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_SLAVE_CLASS_STORAGE_MODE_SENSE_PARAMETER_LIST_LENGTH_10, (USHORT)buffer_length);
481     }
482     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_SLAVE_CLASS_STORAGE_MODE_SENSE_PC_PAGE_CODE) = page_code;
483 }
484 
_test_send_cbw(void)485 static UINT _test_send_cbw(void)
486 {
487 
488 UX_TRANSFER     *transfer_request;
489 UINT            status;
490 UCHAR           *cbw;
491 
492 
493     transfer_request =  &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
494     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
495 
496     transfer_request -> ux_transfer_request_data_pointer =      cbw;
497     transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_STORAGE_CBW_LENGTH;
498     status =  ux_host_stack_transfer_request(transfer_request);
499 
500     /* There is error, return the error code.  */
501     if (status != UX_SUCCESS)
502         return(status);
503 
504     /* Wait transfer done.  */
505     status =  _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
506 
507     /* No error, it's done.  */
508     if (status == UX_SUCCESS)
509         return(transfer_request->ux_transfer_request_completion_code);
510 
511     /* All transfers pending need to abort. There may have been a partial transfer.  */
512     ux_host_stack_transfer_request_abort(transfer_request);
513 
514     /* Set the completion code.  */
515     transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
516 
517     /* There was an error, return to the caller.  */
518     return(UX_TRANSFER_TIMEOUT);
519 }
520 
_test_transfer_data(UCHAR * data,ULONG size,UCHAR do_read)521 static UINT _test_transfer_data(UCHAR *data, ULONG size, UCHAR do_read)
522 {
523 
524 UX_TRANSFER     *transfer_request;
525 UINT            status;
526 
527 
528     transfer_request =  do_read ?
529             &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request :
530             &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
531     transfer_request -> ux_transfer_request_data_pointer = data;
532     transfer_request -> ux_transfer_request_requested_length =  size;
533 
534     status =  ux_host_stack_transfer_request(transfer_request);
535 
536     /* There is error, return the error code.  */
537     if (status != UX_SUCCESS)
538         return(status);
539 
540     /* Wait transfer done.  */
541     status =  _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
542 
543     /* No error, it's done.  */
544     if (status == UX_SUCCESS)
545         return(transfer_request->ux_transfer_request_completion_code);
546 
547     /* All transfers pending need to abort. There may have been a partial transfer.  */
548     ux_host_stack_transfer_request_abort(transfer_request);
549 
550     /* Set the completion code.  */
551     transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
552 
553     /* There was an error, return to the caller.  */
554     return(UX_TRANSFER_TIMEOUT);
555 }
556 
_test_wait_csw(void)557 static UINT _test_wait_csw(void)
558 {
559 
560 UX_TRANSFER     *transfer_request;
561 UINT            status;
562 
563 
564     /* Get the pointer to the transfer request, on the bulk in endpoint.  */
565     transfer_request =  &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request;
566 
567     /* Fill in the transfer_request parameters.  */
568     transfer_request -> ux_transfer_request_data_pointer =      (UCHAR *) &storage -> ux_host_class_storage_csw;
569     transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_STORAGE_CSW_LENGTH;
570 
571     /* Get the CSW on the bulk in endpoint.  */
572     status =  ux_host_stack_transfer_request(transfer_request);
573     if (status != UX_SUCCESS)
574         return(status);
575 
576     /* Wait for the completion of the transfer request.  */
577     status =  _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
578 
579     /* If OK, we are done.  */
580     if (status == UX_SUCCESS)
581         return(transfer_request->ux_transfer_request_completion_code);
582 
583     /* All transfers pending need to abort. There may have been a partial transfer.  */
584     ux_host_stack_transfer_request_abort(transfer_request);
585 
586     /* Set the completion code.  */
587     transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
588 
589     /* There was an error, return to the caller.  */
590     return(UX_TRANSFER_TIMEOUT);
591 }
592 
_test_clear_stall(UCHAR clear_read_stall)593 static UINT _test_clear_stall(UCHAR clear_read_stall)
594 {
595 
596 UX_ENDPOINT     *endpoint;
597 
598 
599     endpoint =  clear_read_stall ?
600             storage -> ux_host_class_storage_bulk_in_endpoint :
601             storage -> ux_host_class_storage_bulk_out_endpoint;
602     _ux_host_stack_endpoint_reset(endpoint);
603 }
604 
_test_request_sense(void)605 static UINT _test_request_sense(void)
606 {
607 
608 UINT            status;
609 UX_TRANSFER     *transfer_request;
610 UCHAR           *cbw;
611 UCHAR           *request_sense_response;
612 ULONG           sense_code;
613 UINT            command_length = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
614 
615 
616     transfer_request =  &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
617     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
618 
619     _ux_utility_memory_copy(storage -> ux_host_class_storage_saved_cbw, storage -> ux_host_class_storage_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
620     _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, command_length);
621     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE;
622     *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_ALLOCATION_LENGTH) =  UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
623     request_sense_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
624     if (request_sense_response == UX_NULL)
625         return(UX_MEMORY_INSUFFICIENT);
626     status = _test_send_cbw();
627     if (status == UX_SUCCESS)
628     {
629         status = _test_transfer_data(request_sense_response, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, UX_TRUE);
630         if (status == UX_SUCCESS)
631         {
632             status = _test_wait_csw();
633             if (status == UX_SUCCESS)
634             {
635 
636                 sense_code =  (((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY)) & 0x0f) << 16;
637                 sense_code |=  ((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE)) << 8;
638                 sense_code |=  (ULONG)  *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER);
639 
640                 storage -> ux_host_class_storage_sense_code =  sense_code;
641             }
642         }
643     }
644     _ux_utility_memory_free(request_sense_response);
645     _ux_utility_memory_copy(storage -> ux_host_class_storage_cbw, storage -> ux_host_class_storage_saved_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
646     return(status);
647 }
648 
tx_demo_thread_host_simulation_entry(ULONG arg)649 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
650 {
651 
652 UINT                                        status;
653 UX_HOST_CLASS                               *class;
654 UX_HOST_CLASS_STORAGE_MEDIA                 *storage_media;
655 UX_ENDPOINT                                 *control_endpoint;
656 UX_TRANSFER                                 *transfer_request;
657 UX_DEVICE                                   *device;
658 UX_SLAVE_DEVICE                             *slave_device;
659 UX_SLAVE_CLASS                              *slave_class;
660 
661 
662     /* Find the storage class. */
663     status =  host_storage_instance_get(100);
664     if (status != UX_SUCCESS)
665     {
666         printf("ERROR #%d\n", __LINE__);
667         test_control_return(1);
668     }
669 
670     status = ux_host_stack_device_get(0, &device);
671     if (status != UX_SUCCESS)
672     {
673 
674         printf("ERROR #%d: device_get fail\n", __LINE__);
675         test_control_return(1);
676     }
677     control_endpoint = &device->ux_device_control_endpoint;
678     transfer_request = &control_endpoint->ux_endpoint_transfer_request;
679 
680     /* Wait enough time for media mounting.  */
681     _ux_utility_delay_ms(UX_HOST_CLASS_STORAGE_DEVICE_INIT_DELAY);
682 
683     class = storage->ux_host_class_storage_class;
684     storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *)class->ux_host_class_media;
685 
686     /* Confirm media enum done.  */
687     status = storage_media_status_wait(storage_media, UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED, 100);
688     if (status != UX_SUCCESS)
689     {
690         printf("ERROR #%d\n", __LINE__);
691         test_control_return(1);
692     }
693 
694     /* Pause the class driver thread.  */
695     _ux_utility_thread_suspend(&((UX_HOST_CLASS_STORAGE_EXT*)class->ux_host_class_ext)->ux_host_class_thread);
696 
697     stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_initialize - lun exceed UX_MAX_SLAVE_LUN\n");
698     global_storage_parameter.ux_slave_class_storage_parameter_number_lun = UX_MAX_SLAVE_LUN + 1;
699     status =  ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
700                                             1, 1, (VOID *)&global_storage_parameter);
701     if (status == UX_SUCCESS)
702     {
703         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
704         test_control_return(1);
705     }
706     global_storage_parameter.ux_slave_class_storage_parameter_number_lun = 1;
707 
708     stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_initialize - storage memory error\n");
709     ux_test_utility_sim_mem_allocate_until(0);
710     status =  ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
711                                             1, 1, (VOID *)&global_storage_parameter);
712     ux_test_utility_sim_mem_free_all();
713     if (status == UX_SUCCESS)
714     {
715         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
716         test_control_return(1);
717     }
718 
719     stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_initialize - thread stack memory error\n");
720     ux_test_utility_sim_mem_allocate_until(sizeof(UX_SLAVE_CLASS_STORAGE) + UX_THREAD_STACK_SIZE / 2);
721     status =  ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
722                                             1, 1, (VOID *)&global_storage_parameter);
723     ux_test_utility_sim_mem_free_all();
724     if (status == UX_SUCCESS)
725     {
726         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
727         test_control_return(1);
728     }
729 
730     stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_initialize - thread create error\n");
731     slave_device =  &_ux_system_slave->ux_system_slave_device;
732     slave_class = &_ux_system_slave->ux_system_slave_class_array[1];
733     status =  _ux_utility_thread_create(&slave_class -> ux_slave_class_thread, "ux_slave_storage_thread",
734                                         _ux_device_class_storage_thread,
735                                         (ULONG) (ALIGN_TYPE) slave_class, (VOID *) buffer,
736                                         UX_THREAD_STACK_SIZE, UX_THREAD_PRIORITY_CLASS,
737                                         UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, TX_DONT_START);
738     if (status != UX_SUCCESS)
739     {
740         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
741         test_control_return(1);
742     }
743     UX_THREAD_EXTENSION_PTR_SET(&slave_class -> ux_slave_class_thread, slave_class)
744     status =  ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
745                                             1, 1, (VOID *)&global_storage_parameter);
746     if (status == UX_SUCCESS)
747     {
748         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
749         test_control_return(1);
750     }
751     _ux_utility_thread_delete(&slave_class -> ux_slave_class_thread);
752 
753     stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_initialize - lun block length error\n");
754     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length = UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE * 2;
755     status =  ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
756                                             1, 1, (VOID *)&global_storage_parameter);
757     if (status == UX_SUCCESS)
758     {
759         printf("ERROR #%d: code 0x%x\n", __LINE__, status);
760         test_control_return(1);
761     }
762     global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length = 512;
763 
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