1 /* This test is designed to test the simple dpump host/device class operation. */
2
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "ux_api.h"
6 #include "ux_system.h"
7 #include "ux_utility.h"
8
9 #include "fx_api.h"
10
11 #include "ux_device_class_storage.h"
12 #include "ux_device_stack.h"
13 #include "ux_host_stack.h"
14 #include "ux_host_class_storage.h"
15
16 #include "ux_test_dcd_sim_slave.h"
17 #include "ux_test_hcd_sim_host.h"
18 #include "ux_test_utility_sim.h"
19
20 /* Define constants. */
21 #define UX_DEMO_STACK_SIZE 2048
22 #define UX_DEMO_MEMORY_SIZE (256*1024)
23 #define UX_DEMO_BUFFER_SIZE 2048
24
25 #define UX_RAM_DISK_SIZE (200 * 1024)
26 #define UX_RAM_DISK_LAST_LBA ((UX_RAM_DISK_SIZE / 512) -1)
27
28 /* Define local/extern function prototypes. */
29
30 VOID _fx_ram_driver(FX_MEDIA *media_ptr);
31 void _fx_ram_drive_format(ULONG disk_size, UINT sector_size, UINT sectors_per_cluster,
32 UINT fat_entries, UINT root_directory_entries);
33
34 static void demo_thread_entry(ULONG);
35 static TX_THREAD tx_demo_thread_host_simulation;
36 static TX_THREAD tx_demo_thread_slave_simulation;
37 static void tx_demo_thread_host_simulation_entry(ULONG);
38
39 static UINT demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
40 static UINT demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
41 static UINT demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status);
42 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status);
43
44 /* Define global data structures. */
45
46 static UCHAR usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
47 static UCHAR buffer[UX_DEMO_BUFFER_SIZE];
48
49 static ULONG error_counter;
50
51 static TX_THREAD demo_thread;
52
53 static UX_HOST_CLASS_STORAGE *storage;
54 static UX_SLAVE_CLASS_STORAGE_PARAMETER global_storage_parameter;
55
56 static FX_MEDIA ram_disk_media1;
57 static FX_MEDIA ram_disk_media2;
58 static CHAR ram_disk_buffer1[512];
59 static CHAR ram_disk_buffer2[512];
60 static CHAR ram_disk_memory1[UX_RAM_DISK_SIZE];
61 static CHAR ram_disk_memory2[UX_RAM_DISK_SIZE];
62 static CHAR *ram_disk_memory[] =
63 {
64 ram_disk_memory1, ram_disk_memory2
65 };
66 static UINT ram_disk_status = UX_SUCCESS;
67 static ULONG ram_disk_media_status = 0;
68 static CHAR ram_disk_status_sent = 0;
69
70 static CHAR ram_disk_flush = 0;
71 static UINT ram_disk_flush_status = UX_SUCCESS;
72
73 static ULONG set_cfg_counter;
74
75 static ULONG rsc_mem_alloc_cnt_on_set_cfg;
76 static ULONG rsc_sem_on_set_cfg;
77 static ULONG rsc_sem_get_on_set_cfg;
78 static ULONG rsc_mutex_on_set_cfg;
79
80 static ULONG rsc_enum_sem_usage;
81 static ULONG rsc_enum_sem_get_count;
82 static ULONG rsc_enum_mutex_usage;
83 static ULONG rsc_enum_mem_alloc_count;
84
85 static ULONG rsc_storage_sem_usage;
86 static ULONG rsc_storage_sem_get_count;
87 static ULONG rsc_storage_mutex_usage;
88 static ULONG rsc_storage_mem_alloc_count;
89
90 static ULONG interaction_count;
91
92 static UCHAR error_callback_ignore = UX_TRUE;
93 static ULONG error_callback_counter;
94
95 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 50
96 static UCHAR device_framework_full_speed[] = {
97
98 /* Device descriptor */
99 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
100 0x81, 0x07, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02,
101 0x03, 0x01,
102
103 /* Configuration descriptor */
104 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0,
105 0x32,
106
107 /* Interface descriptor */
108 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50,
109 0x00,
110
111 /* Endpoint descriptor (Bulk Out) */
112 0x07, 0x05, 0x02, 0x02, 0x40, 0x00, 0x00,
113
114 /* Endpoint descriptor (Bulk In) */
115 0x07, 0x05, 0x81, 0x02, 0x40, 0x00, 0x00,
116
117 };
118
119
120 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 60
121 static UCHAR device_framework_high_speed[] = {
122
123 /* Device descriptor */
124 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
125 0x81, 0x07, 0x00, 0x00, 0x01, 0x00, 0x01, 0x02,
126 0x03, 0x01,
127
128 /* Device qualifier descriptor */
129 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
130 0x01, 0x00,
131
132 /* Configuration descriptor */
133 0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0xc0,
134 0x32,
135
136 /* Interface descriptor */
137 0x09, 0x04, 0x00, 0x00, 0x02, 0x08, 0x06, 0x50,
138 0x00,
139
140 /* Endpoint descriptor (Bulk Out) */
141 0x07, 0x05, 0x02, 0x02, 0x00, 0x01, 0x00,
142
143 /* Endpoint descriptor (Bulk In) */
144 0x07, 0x05, 0x81, 0x02, 0x00, 0x01, 0x00,
145
146 };
147
148
149 /* String Device Framework :
150 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
151 Byte 2 : Byte containing the index of the descriptor
152 Byte 3 : Byte containing the length of the descriptor string
153 */
154
155 #define STRING_FRAMEWORK_LENGTH 38
156 static UCHAR string_framework[] = {
157
158 /* Manufacturer string descriptor : Index 1 */
159 0x09, 0x04, 0x01, 0x0c,
160 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
161 0x6f, 0x67, 0x69, 0x63,
162
163 /* Product string descriptor : Index 2 */
164 0x09, 0x04, 0x02, 0x0a,
165 0x46, 0x6c, 0x61, 0x73, 0x68, 0x20, 0x44, 0x69,
166 0x73, 0x6b,
167
168 /* Serial Number string descriptor : Index 3 */
169 0x09, 0x04, 0x03, 0x04,
170 0x30, 0x30, 0x30, 0x31
171 };
172
173
174 /* Multiple languages are supported on the device, to add
175 a language besides english, the unicode language code must
176 be appended to the language_id_framework array and the length
177 adjusted accordingly. */
178 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
179 static UCHAR language_id_framework[] = {
180
181 /* English. */
182 0x09, 0x04
183 };
184
185
186 static UX_TEST_HCD_SIM_ACTION fail_on_bulkin[] = {
187 /* function, request to match,
188 port action, port status,
189 request action, request EP, request data, request actual length, request status,
190 status, additional callback,
191 no_return */
192 { UX_DCD_TRANSFER_REQUEST, UX_NULL,
193 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
194 UX_TEST_MATCH_EP, 0x81, UX_NULL, 0, UX_ERROR,
195 UX_ERROR, UX_NULL,
196 UX_FALSE}, /* Invoke callback & no continue */
197 { 0 }
198 };
199
200
201 /* Define the ISR dispatch. */
202
203 extern VOID (*test_isr_dispatch)(void);
204
205
206 /* Prototype for test control return. */
207
208 void test_control_return(UINT status);
209
210
211 /* Define the ISR dispatch routine. */
212
test_isr(void)213 static void test_isr(void)
214 {
215
216 /* For further expansion of interrupt-level testing. */
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_read_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_read 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 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_flush = demo_thread_media_flush;
352
353 /* Initialize the storage class parameters for reading/writing to the second Flash Disk. */
354 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_last_lba = UX_RAM_DISK_LAST_LBA;
355 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_block_length = 512;
356 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_type = 0;
357 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_removable_flag = 0x80;
358 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_read = demo_thread_media_read;
359 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_write = demo_thread_media_write;
360 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_status = demo_thread_media_status;
361 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_flush = demo_thread_media_flush;
362
363 /* Initialize the device storage class. The class is connected with interface 0 on configuration 1. */
364 status = ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
365 1, 0, (VOID *)&global_storage_parameter);
366 if(status!=UX_SUCCESS)
367 {
368
369 printf("ERROR #%d\n", __LINE__);
370 test_control_return(1);
371 }
372
373 /* Initialize the simulated device controller. */
374 status = _ux_test_dcd_sim_slave_initialize();
375
376 /* Check for error. */
377 if (status != UX_SUCCESS)
378 {
379
380 printf("ERROR #%d\n", __LINE__);
381 test_control_return(1);
382 }
383
384 /* The code below is required for installing the host portion of USBX */
385 status = ux_host_stack_initialize(UX_NULL);
386 if (status != UX_SUCCESS)
387 {
388
389 printf("ERROR #%d\n", __LINE__);
390 test_control_return(1);
391 }
392
393 /* Register storage class. */
394 status = ux_host_stack_class_register(_ux_system_host_class_storage_name, ux_host_class_storage_entry);
395 if (status != UX_SUCCESS)
396 {
397
398 printf("ERROR #%d\n", __LINE__);
399 test_control_return(1);
400 }
401
402 /* Register all the USB host controllers available in this system */
403 // status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
404 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
405
406 /* Check for error. */
407 if (status != UX_SUCCESS)
408 {
409
410 printf("ERROR #%d\n", __LINE__);
411 test_control_return(1);
412 }
413
414 /* Create the main host simulation thread. */
415 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
416 stack_pointer, UX_DEMO_STACK_SIZE,
417 20, 20, 1, TX_AUTO_START);
418
419 /* Check for error. */
420 if (status != TX_SUCCESS)
421 {
422
423 printf("ERROR #%d\n", __LINE__);
424 test_control_return(1);
425 }
426
427 }
428
storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA * storage_media,ULONG status,ULONG timeout)429 static UINT storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA *storage_media, ULONG status, ULONG timeout)
430 {
431
432 while(1)
433 {
434 #if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
435 if (storage_media->ux_host_class_storage_media_status == status)
436 return UX_SUCCESS;
437 #else
438 if ((status == UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED &&
439 storage_media->ux_host_class_storage_media_storage != UX_NULL) ||
440 (status == UX_HOST_CLASS_STORAGE_MEDIA_UNMOUNTED &&
441 storage_media->ux_host_class_storage_media_storage == UX_NULL))
442 return(UX_SUCCESS);
443 #endif
444 if (timeout == 0)
445 break;
446 if (timeout != 0xFFFFFFFF)
447 timeout --;
448 _ux_utility_delay_ms(10);
449 }
450 return UX_ERROR;
451 }
452
test_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE * storage,UCHAR page_code,UCHAR * response,ULONG response_length)453 static UINT test_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE *storage, UCHAR page_code, UCHAR *response, ULONG response_length)
454 {
455 UINT status;
456 UCHAR *cbw;
457 UINT command_length;
458
459 /* Use a pointer for the cbw, easier to manipulate. */
460 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
461
462 /* Get the Write Command Length. */
463 command_length = UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
464
465 /* Initialize the CBW for this command. */
466 _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH, command_length);
467
468 /* Prepare the INQUIRY command block. */
469 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_INQUIRY;
470
471 /* Store the page code. */
472 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_PAGE_CODE) = page_code;
473
474 /* Store the length of the Inquiry Response. */
475 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_ALLOCATION_LENGTH) = (UCHAR)response_length;
476
477 /* Send the command to transport layer. */
478 status = _ux_host_class_storage_transport(storage, response);
479
480 /* Return completion status. */
481 return(status);
482 }
483
484 typedef struct cbw_read_struct {
485 UCHAR flags_pos;
486 UCHAR lba_pos;
487 UCHAR lba_size;
488 UCHAR len_pos;
489 UCHAR len_size;
490 } cbw_read_struct_t;
491 static cbw_read_struct_t cbw_READ_info[] =
492 {
493 { 1, 2, 2, 4, 1},
494 { 1, 2, 4, 7, 2},
495 { 1, 2, 4, 6, 4},
496 { 1, 2, 8, 10, 4},
497 {10, 12, 8, 28, 4}
498 };
_read_op(UCHAR op_code)499 static UCHAR _read_op(UCHAR op_code)
500 {
501 switch(op_code)
502 {
503 case 0x28: return 1;
504 case 0xA8: return 2;
505 case 0x88: return 3;
506 case 0x7F: return 4;
507 case 0x08: return 0;
508 default: return 0xFF;
509 }
510 }
511
_test_init_cbw_READ_EX(UCHAR flags,ULONG data_length,UCHAR op6_10_12_16_32,ULONG lba,ULONG len)512 static void _test_init_cbw_READ_EX(
513 UCHAR flags, ULONG data_length,
514 UCHAR op6_10_12_16_32, ULONG lba, ULONG len)
515 {
516 UCHAR *cbw;
517 UINT command_length;
518 UCHAR op = _read_op(op6_10_12_16_32);
519 UINT i;
520
521
522 if (op >= 5)
523 return;
524
525 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
526 command_length = UX_HOST_CLASS_STORAGE_TEST_READY_COMMAND_LENGTH_SBC;
527 _ux_host_class_storage_cbw_initialize(storage, flags, data_length, command_length);
528 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + 0) = op6_10_12_16_32;
529 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + cbw_READ_info[op].flags_pos) = 0;
530 for (i = cbw_READ_info[op].lba_pos + cbw_READ_info[op].lba_size - 1; i >= cbw_READ_info[op].lba_pos; i --)
531 {
532 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + i) = (UCHAR)lba;
533 lba >>= 8;
534 }
535 for (i = cbw_READ_info[op].len_pos + cbw_READ_info[op].len_size - 1; i >= cbw_READ_info[op].len_size; i --)
536 {
537 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + i) = (UCHAR)len;
538 len >>= 8;
539 }
540 }
541
_test_init_cbw_READ(UCHAR op6_10_12_16_32,ULONG lba,ULONG len)542 static void _test_init_cbw_READ(UCHAR op6_10_12_16_32, ULONG lba, ULONG len)
543 {
544 _test_init_cbw_READ_EX(0x80, len * 512, op6_10_12_16_32, lba, len);
545 }
546
_test_send_cbw(void)547 static UINT _test_send_cbw(void)
548 {
549
550 UX_TRANSFER *transfer_request;
551 UINT status;
552 UCHAR *cbw;
553
554
555 transfer_request = &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
556 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
557
558 transfer_request -> ux_transfer_request_data_pointer = cbw;
559 transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_STORAGE_CBW_LENGTH;
560 status = ux_host_stack_transfer_request(transfer_request);
561
562 /* There is error, return the error code. */
563 if (status != UX_SUCCESS)
564 return(status);
565
566 /* Wait transfer done. */
567 status = _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
568
569 /* No error, it's done. */
570 if (status == UX_SUCCESS)
571 return(transfer_request->ux_transfer_request_completion_code);
572
573 /* All transfers pending need to abort. There may have been a partial transfer. */
574 ux_host_stack_transfer_request_abort(transfer_request);
575
576 /* Set the completion code. */
577 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
578
579 /* There was an error, return to the caller. */
580 return(UX_TRANSFER_TIMEOUT);
581 }
582
_test_transfer_data(UCHAR * data,ULONG size,UCHAR do_read)583 static UINT _test_transfer_data(UCHAR *data, ULONG size, UCHAR do_read)
584 {
585
586 UX_TRANSFER *transfer_request;
587 UINT status;
588
589
590 transfer_request = do_read ?
591 &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request :
592 &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
593 transfer_request -> ux_transfer_request_data_pointer = data;
594 transfer_request -> ux_transfer_request_requested_length = size;
595
596 status = ux_host_stack_transfer_request(transfer_request);
597
598 /* There is error, return the error code. */
599 if (status != UX_SUCCESS)
600 return(status);
601
602 /* Wait transfer done. */
603 status = _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
604
605 /* No error, it's done. */
606 if (status == UX_SUCCESS)
607 return(transfer_request->ux_transfer_request_completion_code);
608
609 /* All transfers pending need to abort. There may have been a partial transfer. */
610 ux_host_stack_transfer_request_abort(transfer_request);
611
612 /* Set the completion code. */
613 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
614
615 /* There was an error, return to the caller. */
616 return(UX_TRANSFER_TIMEOUT);
617 }
618
_test_wait_csw(void)619 static UINT _test_wait_csw(void)
620 {
621
622 UX_TRANSFER *transfer_request;
623 UINT status;
624
625
626 /* Get the pointer to the transfer request, on the bulk in endpoint. */
627 transfer_request = &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request;
628
629 /* Fill in the transfer_request parameters. */
630 transfer_request -> ux_transfer_request_data_pointer = (UCHAR *) &storage -> ux_host_class_storage_csw;
631 transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_STORAGE_CSW_LENGTH;
632
633 /* Get the CSW on the bulk in endpoint. */
634 status = ux_host_stack_transfer_request(transfer_request);
635 if (status != UX_SUCCESS)
636 return(status);
637
638 /* Wait for the completion of the transfer request. */
639 status = _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
640
641 /* If OK, we are done. */
642 if (status == UX_SUCCESS)
643 return(transfer_request->ux_transfer_request_completion_code);
644
645 /* All transfers pending need to abort. There may have been a partial transfer. */
646 ux_host_stack_transfer_request_abort(transfer_request);
647
648 /* Set the completion code. */
649 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
650
651 /* There was an error, return to the caller. */
652 return(UX_TRANSFER_TIMEOUT);
653 }
654
_test_clear_stall(UCHAR clear_read_stall)655 static VOID _test_clear_stall(UCHAR clear_read_stall)
656 {
657
658 UX_ENDPOINT *endpoint;
659
660
661 endpoint = clear_read_stall ?
662 storage -> ux_host_class_storage_bulk_in_endpoint :
663 storage -> ux_host_class_storage_bulk_out_endpoint;
664 _ux_host_stack_endpoint_reset(endpoint);
665 }
666
_test_request_sense(void)667 static UINT _test_request_sense(void)
668 {
669
670 UINT status;
671 UX_TRANSFER *transfer_request;
672 UCHAR *cbw;
673 UCHAR *request_sense_response;
674 ULONG sense_code;
675 UINT command_length = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
676
677
678 transfer_request = &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
679 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
680
681 _ux_utility_memory_copy(storage -> ux_host_class_storage_saved_cbw, storage -> ux_host_class_storage_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
682 _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, command_length);
683 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE;
684 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_ALLOCATION_LENGTH) = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
685 request_sense_response = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
686 if (request_sense_response == UX_NULL)
687 return(UX_MEMORY_INSUFFICIENT);
688 status = _test_send_cbw();
689 if (status == UX_SUCCESS)
690 {
691 status = _test_transfer_data(request_sense_response, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, UX_TRUE);
692 if (status == UX_SUCCESS)
693 {
694 status = _test_wait_csw();
695 if (status == UX_SUCCESS)
696 {
697
698 sense_code = (((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY)) & 0x0f) << 16;
699 sense_code |= ((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE)) << 8;
700 sense_code |= (ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER);
701
702 storage -> ux_host_class_storage_sense_code = sense_code;
703 }
704 }
705 }
706 _ux_utility_memory_free(request_sense_response);
707 _ux_utility_memory_copy(storage -> ux_host_class_storage_cbw, storage -> ux_host_class_storage_saved_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
708 return(status);
709 }
710
tx_demo_thread_host_simulation_entry(ULONG arg)711 static void tx_demo_thread_host_simulation_entry(ULONG arg)
712 {
713
714 UINT status;
715 UX_HOST_CLASS *class;
716 UX_HOST_CLASS_STORAGE_MEDIA *storage_media;
717 UX_ENDPOINT *control_endpoint;
718 UX_TRANSFER *transfer_request;
719 UX_DEVICE *device;
720
721
722 /* Find the storage class. */
723 status = host_storage_instance_get(100);
724 if (status != UX_SUCCESS)
725 {
726 printf("ERROR #%d\n", __LINE__);
727 test_control_return(1);
728 }
729
730 status = ux_host_stack_device_get(0, &device);
731 if (status != UX_SUCCESS)
732 {
733
734 printf("ERROR #%d: device_get fail\n", __LINE__);
735 test_control_return(1);
736 }
737 control_endpoint = &device->ux_device_control_endpoint;
738 transfer_request = &control_endpoint->ux_endpoint_transfer_request;
739
740 /* Wait enough time for media mounting. */
741 _ux_utility_delay_ms(UX_HOST_CLASS_STORAGE_DEVICE_INIT_DELAY);
742
743 class = storage->ux_host_class_storage_class;
744 storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *)class->ux_host_class_media;
745
746 /* Confirm media enum done. */
747 status = storage_media_status_wait(storage_media, UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED, 100);
748 if (status != UX_SUCCESS)
749 {
750 printf("ERROR #%d\n", __LINE__);
751 test_control_return(1);
752 }
753
754 /* Pause the class driver thread. */
755 _ux_utility_thread_suspend(&((UX_HOST_CLASS_STORAGE_EXT*)class->ux_host_class_ext)->ux_host_class_thread);
756
757 stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_READ16 - success\n");
758 _test_init_cbw_READ(UX_SLAVE_CLASS_STORAGE_SCSI_READ16, 0, 1);
759 status = _test_send_cbw();
760 if (status != UX_SUCCESS)
761 {
762 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
763 test_control_return(1);
764 }
765 status = _test_transfer_data(buffer, 512, UX_TRUE);
766 if (status != UX_SUCCESS)
767 {
768 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
769 test_control_return(1);
770 }
771 status = _test_wait_csw();
772 if (status != UX_SUCCESS)
773 {
774 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
775 test_control_return(1);
776 }
777
778 stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_READ32 - success\n");
779 _test_init_cbw_READ(UX_SLAVE_CLASS_STORAGE_SCSI_READ32, 0, 1);
780 status = _test_send_cbw();
781 if (status != UX_SUCCESS)
782 {
783 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
784 test_control_return(1);
785 }
786 status = _test_transfer_data(buffer, 512, UX_TRUE);
787 if (status != UX_SUCCESS)
788 {
789 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
790 test_control_return(1);
791 }
792 status = _test_wait_csw();
793 if (status != UX_SUCCESS)
794 {
795 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
796 test_control_return(1);
797 }
798
799 stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_READ16 - status fail\n");
800 ram_disk_status = UX_ERROR;
801 _test_init_cbw_READ(UX_SLAVE_CLASS_STORAGE_SCSI_READ16, 0, 1);
802 status = _test_send_cbw();
803 if (status != UX_SUCCESS)
804 {
805 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
806 test_control_return(1);
807 }
808 status = _test_transfer_data(buffer, 512, UX_TRUE);
809 if (status != UX_TRANSFER_STALLED)
810 {
811 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
812 test_control_return(1);
813 }
814 ram_disk_status = UX_SUCCESS;
815 _test_clear_stall(UX_TRUE);
816 status = _test_wait_csw();
817 if (status != UX_SUCCESS)
818 {
819 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
820 test_control_return(1);
821 }
822
823 stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_READ16 - transfer fail\n");
824 _test_init_cbw_READ(UX_SLAVE_CLASS_STORAGE_SCSI_READ16, 0, 1);
825 ux_test_dcd_sim_slave_set_actions(fail_on_bulkin);
826 status = _test_send_cbw();
827 if (status != UX_SUCCESS)
828 {
829 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
830 test_control_return(1);
831 }
832 status = _test_transfer_data(buffer, 512, UX_TRUE);
833 if (status != UX_TRANSFER_STALLED)
834 {
835 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
836 test_control_return(1);
837 }
838 _test_clear_stall(UX_TRUE);
839 status = _test_wait_csw();
840 if (status != UX_SUCCESS)
841 {
842 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
843 test_control_return(1);
844 }
845
846 stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_READ16 - Case(7) Hi < Di\n");
847 _test_init_cbw_READ_EX(0x80, 0, UX_SLAVE_CLASS_STORAGE_SCSI_READ16, 0, 1);
848 status = _test_send_cbw();
849 if (status != UX_SUCCESS)
850 {
851 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
852 test_control_return(1);
853 }
854 status = _test_transfer_data(buffer, 512, UX_TRUE);
855 if (status != UX_TRANSFER_STALLED)
856 {
857 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
858 test_control_return(1);
859 }
860 _test_clear_stall(UX_TRUE);
861 status = _test_wait_csw();
862 if (status != UX_SUCCESS)
863 {
864 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
865 test_control_return(1);
866 }
867 _ux_host_class_storage_device_reset(storage);
868
869 stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_READ16 - Case(8) Hi <> Do\n");
870 _test_init_cbw_READ_EX(0x00, 512, UX_SLAVE_CLASS_STORAGE_SCSI_READ16, 0, 1);
871 status = _test_send_cbw();
872 if (status != UX_SUCCESS)
873 {
874 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
875 test_control_return(1);
876 }
877 status = _test_transfer_data(buffer, 512, UX_FALSE);
878 if (status != UX_TRANSFER_STALLED)
879 {
880 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
881 test_control_return(1);
882 }
883 _test_clear_stall(UX_FALSE);
884 status = _test_wait_csw();
885 if (status != UX_SUCCESS)
886 {
887 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
888 test_control_return(1);
889 }
890 _ux_host_class_storage_device_reset(storage);
891
892 stepinfo(">>>>>>>>>>>>>>> UX_SLAVE_CLASS_STORAGE_SCSI_READ16 - Case(5) Hi > Di\n");
893 _test_init_cbw_READ_EX(0x80, 1024, UX_SLAVE_CLASS_STORAGE_SCSI_READ16, 0, 1);
894 status = _test_send_cbw();
895 if (status != UX_SUCCESS)
896 {
897 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
898 test_control_return(1);
899 }
900 status = _test_transfer_data(buffer, 1024, UX_TRUE);
901 if (status != UX_TRANSFER_STALLED)
902 {
903 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
904 test_control_return(1);
905 }
906 _test_clear_stall(UX_TRUE);
907 status = _test_wait_csw();
908 if (status != UX_SUCCESS)
909 {
910 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
911 test_control_return(1);
912 }
913 _ux_host_class_storage_device_reset(storage);
914
915 /* Finally disconnect the device. */
916 ux_device_stack_disconnect();
917
918 /* And deinitialize the class. */
919 status = ux_device_stack_class_unregister(_ux_system_slave_class_storage_name, ux_device_class_storage_entry);
920
921 /* Deinitialize the device side of usbx. */
922 _ux_device_stack_uninitialize();
923
924 /* And finally the usbx system resources. */
925 _ux_system_uninitialize();
926
927 /* Successful test. */
928 printf("SUCCESS!\n");
929 test_control_return(0);
930 }
931
932
demo_thread_media_status(VOID * storage,ULONG lun,ULONG media_id,ULONG * media_status)933 static UINT demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
934 {
935
936 UINT status = ram_disk_status;
937
938
939 (void)storage;
940 (void)media_id;
941
942 if (media_status)
943 *media_status = ram_disk_media_status;
944
945 /* If there is attention, it must be changed to ready after reported. */
946 if (ram_disk_media_status == (UX_SLAVE_CLASS_STORAGE_SENSE_KEY_UNIT_ATTENTION | (0x28 << 8)))
947 {
948 ram_disk_status = UX_SUCCESS;
949 ram_disk_media_status = 0;
950 }
951 else
952 ram_disk_status_sent = UX_TRUE;
953
954 return status;
955 }
956
demo_thread_media_read(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)957 static UINT demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
958 {
959 (void)storage;
960 (void)media_status;
961
962 if (lun > 1)
963 return UX_ERROR;
964
965 ux_utility_memory_copy(data_pointer, &ram_disk_memory[lun][lba * 512], number_blocks * 512);
966
967 return UX_SUCCESS;
968 }
969
demo_thread_media_write(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)970 static UINT demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
971 {
972 (void)storage;
973 (void)media_status;
974
975 if (lun > 1)
976 return UX_ERROR;
977
978 ux_utility_memory_copy(&ram_disk_memory[lun][lba * 512], data_pointer, number_blocks * 512);
979
980 return UX_SUCCESS;
981 }
982
demo_thread_media_flush(VOID * storage,ULONG lun,ULONG number_blocks,ULONG lba,ULONG * media_status)983 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status)
984 {
985 (void)storage;
986 (void)number_blocks;
987 (void)lba;
988 (void)media_status;
989
990 if (lun > 1)
991 return UX_ERROR;
992
993 ram_disk_flush = UX_TRUE;
994 return ram_disk_flush_status;
995 }