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
action_disconnect_on_stall(struct UX_TEST_ACTION_STRUCT * action,VOID * params)200 static VOID action_disconnect_on_stall(struct UX_TEST_ACTION_STRUCT *action, VOID *params)
201 {
202
203 UX_SLAVE_DEVICE *slave_device;
204
205
206 slave_device = &_ux_system_slave->ux_system_slave_device;
207 slave_device->ux_slave_device_state = UX_DEVICE_ADDRESSED;
208 }
209
210 static UX_TEST_HCD_SIM_ACTION disconnect_on_stall[] = {
211 /* function, request to match,
212 port action, port status,
213 request action, request EP, request data, request actual length, request status,
214 status, additional callback,
215 no_return */
216 { UX_DCD_STALL_ENDPOINT, UX_NULL,
217 UX_FALSE, UX_TEST_PORT_STATUS_DISC,
218 UX_TEST_MATCH_EP, 0x81, UX_NULL, 0, UX_SUCCESS,
219 UX_SUCCESS, action_disconnect_on_stall,
220 UX_FALSE}, /* Invoke callback & no continue */
221 { 0 }
222 };
223
224
225 /* Define the ISR dispatch. */
226
227 extern VOID (*test_isr_dispatch)(void);
228
229
230 /* Prototype for test control return. */
231
232 void test_control_return(UINT status);
233
234
235 /* Define the ISR dispatch routine. */
236
test_isr(void)237 static void test_isr(void)
238 {
239
240 /* For further expansion of interrupt-level testing. */
241 }
242
243
error_callback(UINT system_level,UINT system_context,UINT error_code)244 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
245 {
246
247 error_callback_counter ++;
248
249 if (!error_callback_ignore)
250 {
251 {
252 /* Failed test. */
253 printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
254 test_control_return(1);
255 }
256 }
257 }
258
host_storage_instance_get(ULONG timeout_x10ms)259 static UINT host_storage_instance_get(ULONG timeout_x10ms)
260 {
261
262 UINT status;
263 UX_HOST_CLASS *class;
264
265
266 /* Find the main storage container */
267 status = ux_host_stack_class_get(_ux_system_host_class_storage_name, &class);
268 if (status != UX_SUCCESS)
269 return(status);
270
271 /* Get storage instance, wait it to be live and media attached. */
272 do
273 {
274 if (timeout_x10ms)
275 {
276 ux_utility_delay_ms(10);
277 if (timeout_x10ms != 0xFFFFFFFF)
278 timeout_x10ms --;
279 }
280
281 status = ux_host_stack_class_instance_get(class, 0, (void **) &storage);
282 if (status == UX_SUCCESS)
283 {
284 if (storage -> ux_host_class_storage_state == UX_HOST_CLASS_INSTANCE_LIVE &&
285 class -> ux_host_class_media != UX_NULL)
286 return(UX_SUCCESS);
287 }
288
289 } while(timeout_x10ms > 0);
290
291 return(UX_ERROR);
292 }
293
sleep_break_on_error(VOID)294 static UINT sleep_break_on_error(VOID)
295 {
296
297 if (error_callback_counter >= 3)
298 return error_callback_counter;
299
300 return UX_SUCCESS;
301 }
302
303
304 /* Define what the initial system looks like. */
305
306 #ifdef CTEST
test_application_define(void * first_unused_memory)307 void test_application_define(void *first_unused_memory)
308 #else
309 void usbx_ux_device_class_storage_thread_test_application_define(void *first_unused_memory)
310 #endif
311 {
312
313 UINT status;
314 CHAR * stack_pointer;
315 CHAR * memory_pointer;
316
317
318 /* Inform user. */
319 printf("Running ux_device_class_storage_thread Test......................... ");
320 stepinfo("\n");
321
322 /* Initialize the free memory pointer */
323 stack_pointer = (CHAR *) usbx_memory;
324 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
325
326 /* Initialize USBX. Memory */
327 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
328
329 /* Check for error. */
330 if (status != UX_SUCCESS)
331 {
332
333 printf("ERROR #%d\n", __LINE__);
334 test_control_return(1);
335 }
336
337 /* Register the error callback. */
338 _ux_utility_error_callback_register(error_callback);
339
340 /* Reset ram disks memory. */
341 ux_utility_memory_set(ram_disk_memory1, 0, UX_RAM_DISK_SIZE);
342 ux_utility_memory_set(ram_disk_memory2, 0, UX_RAM_DISK_SIZE);
343
344 /* Initialize FileX. */
345 fx_system_initialize();
346
347 /* Change the ram drive values. */
348 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);
349 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);
350
351 /* The code below is required for installing the device portion of USBX.
352 In this demo, DFU is possible and we have a call back for state change. */
353 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
354 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
355 string_framework, STRING_FRAMEWORK_LENGTH,
356 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
357 if(status!=UX_SUCCESS)
358 {
359
360 printf("ERROR #%d\n", __LINE__);
361 test_control_return(1);
362 }
363
364 /* Store the number of LUN in this device storage instance. */
365 global_storage_parameter.ux_slave_class_storage_parameter_number_lun = 1;
366
367 /* Initialize the storage class parameters for reading/writing to the first Flash Disk. */
368 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_last_lba = UX_RAM_DISK_LAST_LBA;
369 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length = 512;
370 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_type = 0;
371 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_removable_flag = 0x80;
372 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_read = demo_thread_media_read;
373 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_write = demo_thread_media_write;
374 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_status = demo_thread_media_status;
375 global_storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_flush = demo_thread_media_flush;
376
377 /* Initialize the storage class parameters for reading/writing to the second Flash Disk. */
378 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_last_lba = UX_RAM_DISK_LAST_LBA;
379 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_block_length = 512;
380 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_type = 0;
381 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_removable_flag = 0x80;
382 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_read = demo_thread_media_read;
383 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_write = demo_thread_media_write;
384 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_status = demo_thread_media_status;
385 global_storage_parameter.ux_slave_class_storage_parameter_lun[1].ux_slave_class_storage_media_flush = demo_thread_media_flush;
386
387 /* Initialize the device storage class. The class is connected with interface 0 on configuration 1. */
388 status = ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
389 1, 0, (VOID *)&global_storage_parameter);
390 if(status!=UX_SUCCESS)
391 {
392
393 printf("ERROR #%d\n", __LINE__);
394 test_control_return(1);
395 }
396
397 /* Initialize the simulated device controller. */
398 status = _ux_test_dcd_sim_slave_initialize();
399
400 /* Check for error. */
401 if (status != UX_SUCCESS)
402 {
403
404 printf("ERROR #%d\n", __LINE__);
405 test_control_return(1);
406 }
407
408 /* The code below is required for installing the host portion of USBX */
409 status = ux_host_stack_initialize(UX_NULL);
410 if (status != UX_SUCCESS)
411 {
412
413 printf("ERROR #%d\n", __LINE__);
414 test_control_return(1);
415 }
416
417 /* Register storage class. */
418 status = ux_host_stack_class_register(_ux_system_host_class_storage_name, ux_host_class_storage_entry);
419 if (status != UX_SUCCESS)
420 {
421
422 printf("ERROR #%d\n", __LINE__);
423 test_control_return(1);
424 }
425
426 /* Register all the USB host controllers available in this system */
427 // status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
428 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
429
430 /* Check for error. */
431 if (status != UX_SUCCESS)
432 {
433
434 printf("ERROR #%d\n", __LINE__);
435 test_control_return(1);
436 }
437
438 /* Create the main host simulation thread. */
439 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
440 stack_pointer, UX_DEMO_STACK_SIZE,
441 20, 20, 1, TX_AUTO_START);
442
443 /* Check for error. */
444 if (status != TX_SUCCESS)
445 {
446
447 printf("ERROR #%d\n", __LINE__);
448 test_control_return(1);
449 }
450
451 }
452
storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA * storage_media,ULONG status,ULONG timeout)453 static UINT storage_media_status_wait(UX_HOST_CLASS_STORAGE_MEDIA *storage_media, ULONG status, ULONG timeout)
454 {
455
456 while(1)
457 {
458 #if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
459 if (storage_media->ux_host_class_storage_media_status == status)
460 return UX_SUCCESS;
461 #else
462 if ((status == UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED &&
463 storage_media->ux_host_class_storage_media_storage != UX_NULL) ||
464 (status == UX_HOST_CLASS_STORAGE_MEDIA_UNMOUNTED &&
465 storage_media->ux_host_class_storage_media_storage == UX_NULL))
466 return(UX_SUCCESS);
467 #endif
468 if (timeout == 0)
469 break;
470 if (timeout != 0xFFFFFFFF)
471 timeout --;
472 _ux_utility_delay_ms(10);
473 }
474 return UX_ERROR;
475 }
476
test_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE * storage,UCHAR page_code,UCHAR * response,ULONG response_length)477 static UINT test_host_class_storage_inquiry(UX_HOST_CLASS_STORAGE *storage, UCHAR page_code, UCHAR *response, ULONG response_length)
478 {
479 UINT status;
480 UCHAR *cbw;
481 UINT command_length;
482
483 /* Use a pointer for the cbw, easier to manipulate. */
484 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
485
486 /* Get the Write Command Length. */
487 command_length = UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
488
489 /* Initialize the CBW for this command. */
490 _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH, command_length);
491
492 /* Prepare the INQUIRY command block. */
493 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_INQUIRY;
494
495 /* Store the page code. */
496 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_PAGE_CODE) = page_code;
497
498 /* Store the length of the Inquiry Response. */
499 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_ALLOCATION_LENGTH) = (UCHAR)response_length;
500
501 /* Send the command to transport layer. */
502 status = _ux_host_class_storage_transport(storage, response);
503
504 /* Return completion status. */
505 return(status);
506 }
507
_test_init_cbw(UCHAR dCBWSignature_error,ULONG dCBWDataTransferLength,UCHAR dir,UCHAR lun,UCHAR bCBWCBLength,UCHAR cb_op)508 static void _test_init_cbw(UCHAR dCBWSignature_error,
509 ULONG dCBWDataTransferLength, UCHAR dir, UCHAR lun, UCHAR bCBWCBLength,
510 UCHAR cb_op)
511 {
512
513 UCHAR *cbw;
514
515
516 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
517 _ux_host_class_storage_cbw_initialize(storage, dir ? 0x80 : 0, dCBWDataTransferLength, bCBWCBLength);
518
519 if (dCBWSignature_error)
520 *(cbw + UX_HOST_CLASS_STORAGE_CBW_SIGNATURE) = 0x00;
521
522 *(cbw + UX_HOST_CLASS_STORAGE_CBW_LUN) = lun;
523
524 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + 0) = cb_op;
525 }
526
_test_send_cbw(UCHAR length_error)527 static UINT _test_send_cbw(UCHAR length_error)
528 {
529
530 UX_TRANSFER *transfer_request;
531 UINT status;
532 UCHAR *cbw;
533
534
535 transfer_request = &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
536 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
537
538 transfer_request -> ux_transfer_request_data_pointer = cbw;
539 transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_STORAGE_CBW_LENGTH - length_error;
540 status = ux_host_stack_transfer_request(transfer_request);
541
542 /* There is error, return the error code. */
543 if (status != UX_SUCCESS)
544 return(status);
545
546 /* Wait transfer done. */
547 status = _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
548
549 /* No error, it's done. */
550 if (status == UX_SUCCESS)
551 return(transfer_request->ux_transfer_request_completion_code);
552
553 /* All transfers pending need to abort. There may have been a partial transfer. */
554 ux_host_stack_transfer_request_abort(transfer_request);
555
556 /* Set the completion code. */
557 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
558
559 /* There was an error, return to the caller. */
560 return(UX_TRANSFER_TIMEOUT);
561 }
562
_test_transfer_data(UCHAR * data,ULONG size,UCHAR do_read)563 static UINT _test_transfer_data(UCHAR *data, ULONG size, UCHAR do_read)
564 {
565
566 UX_TRANSFER *transfer_request;
567 UINT status;
568
569
570 transfer_request = do_read ?
571 &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request :
572 &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
573 transfer_request -> ux_transfer_request_data_pointer = data;
574 transfer_request -> ux_transfer_request_requested_length = size;
575
576 status = ux_host_stack_transfer_request(transfer_request);
577
578 /* There is error, return the error code. */
579 if (status != UX_SUCCESS)
580 return(status);
581
582 /* Wait transfer done. */
583 status = _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
584
585 /* No error, it's done. */
586 if (status == UX_SUCCESS)
587 return(transfer_request->ux_transfer_request_completion_code);
588
589 /* All transfers pending need to abort. There may have been a partial transfer. */
590 ux_host_stack_transfer_request_abort(transfer_request);
591
592 /* Set the completion code. */
593 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
594
595 /* There was an error, return to the caller. */
596 return(UX_TRANSFER_TIMEOUT);
597 }
598
_test_wait_csw(void)599 static UINT _test_wait_csw(void)
600 {
601
602 UX_TRANSFER *transfer_request;
603 UINT status;
604
605
606 /* Get the pointer to the transfer request, on the bulk in endpoint. */
607 transfer_request = &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request;
608
609 /* Fill in the transfer_request parameters. */
610 transfer_request -> ux_transfer_request_data_pointer = (UCHAR *) &storage -> ux_host_class_storage_csw;
611 transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_STORAGE_CSW_LENGTH;
612
613 /* Get the CSW on the bulk in endpoint. */
614 status = ux_host_stack_transfer_request(transfer_request);
615 if (status != UX_SUCCESS)
616 return(status);
617
618 /* Wait for the completion of the transfer request. */
619 status = _ux_utility_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
620
621 /* If OK, we are done. */
622 if (status == UX_SUCCESS)
623 return(transfer_request->ux_transfer_request_completion_code);
624
625 /* All transfers pending need to abort. There may have been a partial transfer. */
626 ux_host_stack_transfer_request_abort(transfer_request);
627
628 /* Set the completion code. */
629 transfer_request -> ux_transfer_request_completion_code = UX_TRANSFER_TIMEOUT;
630
631 /* There was an error, return to the caller. */
632 return(UX_TRANSFER_TIMEOUT);
633 }
634
_test_clear_stall(UCHAR clear_read_stall)635 static VOID _test_clear_stall(UCHAR clear_read_stall)
636 {
637
638 UX_ENDPOINT *endpoint;
639
640
641 endpoint = clear_read_stall ?
642 storage -> ux_host_class_storage_bulk_in_endpoint :
643 storage -> ux_host_class_storage_bulk_out_endpoint;
644 _ux_host_stack_endpoint_reset(endpoint);
645 }
646
_test_request_sense(void)647 static UINT _test_request_sense(void)
648 {
649
650 UINT status;
651 UX_TRANSFER *transfer_request;
652 UCHAR *cbw;
653 UCHAR *request_sense_response;
654 ULONG sense_code;
655 UINT command_length = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
656
657
658 transfer_request = &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
659 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
660
661 _ux_utility_memory_copy(storage -> ux_host_class_storage_saved_cbw, storage -> ux_host_class_storage_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
662 _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, command_length);
663 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE;
664 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_ALLOCATION_LENGTH) = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
665 request_sense_response = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
666 if (request_sense_response == UX_NULL)
667 return(UX_MEMORY_INSUFFICIENT);
668 status = _test_send_cbw(UX_FALSE);
669 if (status == UX_SUCCESS)
670 {
671 status = _test_transfer_data(request_sense_response, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, UX_TRUE);
672 if (status == UX_SUCCESS)
673 {
674 status = _test_wait_csw();
675 if (status == UX_SUCCESS)
676 {
677
678 sense_code = (((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY)) & 0x0f) << 16;
679 sense_code |= ((ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE)) << 8;
680 sense_code |= (ULONG) *(request_sense_response + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER);
681
682 storage -> ux_host_class_storage_sense_code = sense_code;
683 }
684 }
685 }
686 _ux_utility_memory_free(request_sense_response);
687 _ux_utility_memory_copy(storage -> ux_host_class_storage_cbw, storage -> ux_host_class_storage_saved_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH);
688 return(status);
689 }
690
tx_demo_thread_host_simulation_entry(ULONG arg)691 static void tx_demo_thread_host_simulation_entry(ULONG arg)
692 {
693
694 UINT status;
695 UX_HOST_CLASS *class;
696 UX_HOST_CLASS_STORAGE_MEDIA *storage_media;
697 UX_ENDPOINT *control_endpoint;
698 UX_TRANSFER *transfer_request;
699 UX_DEVICE *device;
700 UX_SLAVE_DEVICE *slave_device;
701 UX_SLAVE_CLASS *slave_class;
702 UX_SLAVE_CLASS_STORAGE *slave_storage;
703
704
705 /* Find the storage class. */
706 status = host_storage_instance_get(100);
707 if (status != UX_SUCCESS)
708 {
709 printf("ERROR #%d\n", __LINE__);
710 test_control_return(1);
711 }
712
713 status = ux_host_stack_device_get(0, &device);
714 if (status != UX_SUCCESS)
715 {
716
717 printf("ERROR #%d: device_get fail\n", __LINE__);
718 test_control_return(1);
719 }
720 control_endpoint = &device->ux_device_control_endpoint;
721 transfer_request = &control_endpoint->ux_endpoint_transfer_request;
722
723 /* Wait enough time for media mounting. */
724 _ux_utility_delay_ms(UX_HOST_CLASS_STORAGE_DEVICE_INIT_DELAY);
725
726 class = storage->ux_host_class_storage_class;
727 storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *)class->ux_host_class_media;
728
729 /* Confirm media enum done. */
730 status = storage_media_status_wait(storage_media, UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED, 100);
731 if (status != UX_SUCCESS)
732 {
733 printf("ERROR #%d\n", __LINE__);
734 test_control_return(1);
735 }
736
737 /* Pause the class driver thread. */
738 _ux_utility_thread_suspend(&((UX_HOST_CLASS_STORAGE_EXT*)class->ux_host_class_ext)->ux_host_class_thread);
739
740 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - command unknown (write)\n");
741 _test_init_cbw(UX_FALSE, 10, UX_FALSE, 0, 6, 0xFF);
742 status = _test_send_cbw(UX_FALSE);
743 if (status != UX_SUCCESS)
744 {
745 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
746 test_control_return(1);
747 }
748 status = _test_transfer_data(buffer, 512, UX_FALSE);
749 if (status != UX_TRANSFER_STALLED)
750 {
751 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
752 test_control_return(1);
753 }
754 _test_clear_stall(UX_FALSE);
755 status = _test_wait_csw();
756 if (status != UX_SUCCESS)
757 {
758 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
759 test_control_return(1);
760 }
761
762 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - command unknown (read)\n");
763 _test_init_cbw(UX_FALSE, 10, UX_TRUE, 0, 6, 0xFF);
764 status = _test_send_cbw(UX_FALSE);
765 if (status != UX_SUCCESS)
766 {
767 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
768 test_control_return(1);
769 }
770 status = _test_transfer_data(buffer, 512, UX_TRUE);
771 if (status != UX_TRANSFER_STALLED)
772 {
773 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
774 test_control_return(1);
775 }
776 _test_clear_stall(UX_TRUE);
777 status = _test_wait_csw();
778 if (status != UX_SUCCESS)
779 {
780 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
781 test_control_return(1);
782 }
783
784 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - command unknown (no data)\n");
785 _test_init_cbw(UX_FALSE, 0, UX_TRUE, 0, 6, 0xFF);
786 status = _test_send_cbw(UX_FALSE);
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_TRANSFER_STALLED)
794 {
795 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
796 test_control_return(1);
797 }
798 _test_clear_stall(UX_TRUE);
799 status = _test_wait_csw();
800 if (status != UX_SUCCESS)
801 {
802 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
803 test_control_return(1);
804 }
805
806 /* Get slave information. */
807 slave_device = &_ux_system_slave->ux_system_slave_device;
808 slave_class = _ux_system_slave->ux_system_slave_interface_class_array[0];
809 slave_storage = (UX_SLAVE_CLASS_STORAGE *)slave_class->ux_slave_class_instance;
810
811 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - bad LUN\n");
812 _test_init_cbw(UX_FALSE, 0, UX_TRUE, (UCHAR)slave_storage->ux_slave_class_storage_number_lun, UX_HOST_CLASS_STORAGE_TEST_READY_COMMAND_LENGTH_SBC, UX_HOST_CLASS_STORAGE_SCSI_TEST_READY);
813 status = _test_send_cbw(UX_FALSE);
814 if (status != UX_SUCCESS)
815 {
816 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
817 test_control_return(1);
818 }
819 status = _test_wait_csw();
820 if (status != UX_TRANSFER_STALLED)
821 {
822 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
823 test_control_return(1);
824 }
825 _test_clear_stall(UX_TRUE);
826 status = _test_wait_csw();
827 if (status != UX_TRANSFER_STALLED)
828 {
829 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
830 test_control_return(1);
831 }
832 status = _test_send_cbw(UX_FALSE);
833 if (status != UX_TRANSFER_STALLED)
834 {
835 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
836 test_control_return(1);
837 }
838
839 /* Reset recovery. */
840 _ux_host_class_storage_device_reset(storage);
841 _test_clear_stall(UX_TRUE);
842 _test_clear_stall(UX_FALSE);
843
844 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - bad received length\n");
845 _test_init_cbw(UX_FALSE, 0, UX_TRUE, 0, UX_HOST_CLASS_STORAGE_TEST_READY_COMMAND_LENGTH_SBC, UX_HOST_CLASS_STORAGE_SCSI_TEST_READY);
846 status = _test_send_cbw(UX_TRUE);
847 if (status != UX_SUCCESS)
848 {
849 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
850 test_control_return(1);
851 }
852 status = _test_wait_csw();
853 if (status != UX_TRANSFER_STALLED)
854 {
855 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
856 test_control_return(1);
857 }
858 _test_clear_stall(UX_TRUE);
859 status = _test_wait_csw();
860 if (status != UX_TRANSFER_STALLED)
861 {
862 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
863 test_control_return(1);
864 }
865 status = _test_send_cbw(UX_FALSE);
866 if (status != UX_TRANSFER_STALLED)
867 {
868 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
869 test_control_return(1);
870 }
871
872 /* Reset recovery. */
873 _ux_host_class_storage_device_reset(storage);
874 _test_clear_stall(UX_TRUE);
875 _test_clear_stall(UX_FALSE);
876
877 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - bad signature\n");
878 _test_init_cbw(UX_TRUE, 0, UX_TRUE, 0, UX_HOST_CLASS_STORAGE_TEST_READY_COMMAND_LENGTH_SBC, UX_HOST_CLASS_STORAGE_SCSI_TEST_READY);
879 status = _test_send_cbw(UX_FALSE);
880 if (status != UX_SUCCESS)
881 {
882 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
883 test_control_return(1);
884 }
885 status = _test_wait_csw();
886 if (status != UX_TRANSFER_STALLED)
887 {
888 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
889 test_control_return(1);
890 }
891 _test_clear_stall(UX_TRUE);
892 status = _test_wait_csw();
893 if (status != UX_TRANSFER_STALLED)
894 {
895 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
896 test_control_return(1);
897 }
898 status = _test_send_cbw(UX_FALSE);
899 if (status != UX_TRANSFER_STALLED)
900 {
901 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
902 test_control_return(1);
903 }
904
905 /* Reset recovery. */
906 _ux_host_class_storage_device_reset(storage);
907 _test_clear_stall(UX_TRUE);
908 _test_clear_stall(UX_FALSE);
909
910 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - bad CBW length\n");
911 _test_init_cbw(UX_FALSE, 0, UX_TRUE, 0, 0, UX_HOST_CLASS_STORAGE_SCSI_TEST_READY);
912 status = _test_send_cbw(UX_FALSE);
913 if (status != UX_SUCCESS)
914 {
915 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
916 test_control_return(1);
917 }
918 status = _test_wait_csw();
919 if (status != UX_TRANSFER_STALLED)
920 {
921 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
922 test_control_return(1);
923 }
924 _test_clear_stall(UX_TRUE);
925 status = _test_wait_csw();
926 if (status != UX_TRANSFER_STALLED)
927 {
928 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
929 test_control_return(1);
930 }
931 status = _test_send_cbw(UX_FALSE);
932 if (status != UX_TRANSFER_STALLED)
933 {
934 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
935 test_control_return(1);
936 }
937
938 /* Reset recovery. */
939 _ux_host_class_storage_device_reset(storage);
940 _test_clear_stall(UX_TRUE);
941 _test_clear_stall(UX_FALSE);
942
943 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - CSW transfer error after stall\n");
944 _test_init_cbw(UX_FALSE, 0, UX_TRUE, 0, 6, 0xFF);
945 ux_test_dcd_sim_slave_set_actions(fail_on_bulkin);
946 error_callback_counter = 0;
947 status = _test_send_cbw(UX_FALSE);
948 if (status != UX_SUCCESS)
949 {
950 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
951 test_control_return(1);
952 }
953 status = _test_wait_csw();
954 if (status != UX_TRANSFER_STALLED)
955 {
956 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
957 test_control_return(1);
958 }
959 _test_clear_stall(UX_TRUE);
960 if (error_callback_counter == 0)
961 {
962 printf("ERROR #%d: error callback not detected\n", __LINE__);
963 test_control_return(1);
964 }
965 status = _test_wait_csw();
966 if (status == UX_SUCCESS)
967 {
968 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
969 test_control_return(1);
970 }
971
972 stepinfo(">>>>>>>>>>>>>>> ux_device_class_storage_thread - disconnect after stall\n");
973 _test_init_cbw(UX_FALSE, 0, UX_TRUE, 0, 6, 0xFF);
974 ux_test_dcd_sim_slave_set_actions(disconnect_on_stall);
975 status = _test_send_cbw(UX_FALSE);
976 if (status != UX_SUCCESS)
977 {
978 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
979 test_control_return(1);
980 }
981 status = _test_wait_csw();
982 if (status == UX_SUCCESS)
983 {
984 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
985 test_control_return(1);
986 }
987 status = _test_wait_csw();
988 if (status == UX_SUCCESS)
989 {
990 printf("ERROR #%d: code 0x%x\n", __LINE__, status);
991 test_control_return(1);
992 }
993
994 /* Wait a while so the thread goes. */
995 _ux_utility_delay_ms(10);
996
997 /* Restore. */
998 slave_device->ux_slave_device_state = UX_DEVICE_CONFIGURED;
999
1000 /* Finally disconnect the device. */
1001 ux_device_stack_disconnect();
1002
1003 /* And deinitialize the class. */
1004 status = ux_device_stack_class_unregister(_ux_system_slave_class_storage_name, ux_device_class_storage_entry);
1005
1006 /* Deinitialize the device side of usbx. */
1007 _ux_device_stack_uninitialize();
1008
1009 /* And finally the usbx system resources. */
1010 _ux_system_uninitialize();
1011
1012 /* Successful test. */
1013 printf("SUCCESS!\n");
1014 test_control_return(0);
1015 }
1016
1017
demo_thread_media_status(VOID * storage,ULONG lun,ULONG media_id,ULONG * media_status)1018 static UINT demo_thread_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
1019 {
1020
1021 UINT status = ram_disk_status;
1022
1023
1024 (void)storage;
1025 (void)media_id;
1026
1027 if (media_status)
1028 *media_status = ram_disk_media_status;
1029
1030 /* If there is attention, it must be changed to ready after reported. */
1031 if (ram_disk_media_status == (UX_SLAVE_CLASS_STORAGE_SENSE_KEY_UNIT_ATTENTION | (0x28 << 8)))
1032 {
1033 ram_disk_status = UX_SUCCESS;
1034 ram_disk_media_status = 0;
1035 }
1036 else
1037 ram_disk_status_sent = UX_TRUE;
1038
1039 return status;
1040 }
1041
demo_thread_media_read(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)1042 static UINT demo_thread_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
1043 {
1044 (void)storage;
1045 (void)media_status;
1046
1047 if (lun > 1)
1048 return UX_ERROR;
1049
1050 ux_utility_memory_copy(data_pointer, &ram_disk_memory[lun][lba * 512], number_blocks * 512);
1051
1052 return UX_SUCCESS;
1053 }
1054
demo_thread_media_write(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)1055 static UINT demo_thread_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
1056 {
1057 (void)storage;
1058 (void)media_status;
1059
1060 if (lun > 1)
1061 return UX_ERROR;
1062
1063 ux_utility_memory_copy(&ram_disk_memory[lun][lba * 512], data_pointer, number_blocks * 512);
1064
1065 return UX_SUCCESS;
1066 }
1067
demo_thread_media_flush(VOID * storage,ULONG lun,ULONG number_blocks,ULONG lba,ULONG * media_status)1068 static UINT demo_thread_media_flush(VOID *storage, ULONG lun, ULONG number_blocks, ULONG lba, ULONG *media_status)
1069 {
1070 (void)storage;
1071 (void)number_blocks;
1072 (void)lba;
1073 (void)media_status;
1074
1075 if (lun > 1)
1076 return UX_ERROR;
1077
1078 ram_disk_flush = UX_TRUE;
1079 return ram_disk_flush_status;
1080 }