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