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