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