1 /* This test is designed to test the ux_device_stack_transfer_request. */
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 "ux_host_stack.h"
10 #include "ux_device_stack.h"
11
12 #include "ux_device_class_cdc_acm.h"
13 #include "ux_host_class_cdc_acm.h"
14
15 #include "ux_host_class_dpump.h"
16 #include "ux_device_class_dpump.h"
17
18 #include "ux_host_class_hid.h"
19 #include "ux_device_class_hid.h"
20
21 #include "ux_host_class_storage.h"
22 #include "ux_device_class_storage.h"
23
24 #include "ux_test_dcd_sim_slave.h"
25 #include "ux_test_hcd_sim_host.h"
26 #include "ux_test_utility_sim.h"
27
28
29 /* Define USBX test constants. */
30
31 #define UX_TEST_STACK_SIZE 4096
32 #define UX_TEST_BUFFER_SIZE 2048
33 #define UX_TEST_RUN 1
34 #define UX_TEST_MEMORY_SIZE (64*1024)
35
36 /* HID mouse related descriptors */
37
38 static UCHAR hid_mouse_report[] = {
39
40 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
41 0x09, 0x02, // USAGE (Mouse)
42 0xa1, 0x01, // COLLECTION (Application)
43 0x09, 0x01, // USAGE (Pointer)
44 0xa1, 0x00, // COLLECTION (Physical)
45 0x05, 0x09, // USAGE_PAGE (Button)
46 0x19, 0x01, // USAGE_MINIMUM (Button 1)
47 0x29, 0x03, // USAGE_MAXIMUM (Button 3)
48 0x15, 0x00, // LOGICAL_MINIMUM (0)
49 0x25, 0x01, // LOGICAL_MAXIMUM (1)
50 0x95, 0x03, // REPORT_COUNT (3)
51 0x75, 0x01, // REPORT_SIZE (1)
52 0x81, 0x02, // INPUT (Data,Var,Abs)
53 0x95, 0x01, // REPORT_COUNT (1)
54 0x75, 0x05, // REPORT_SIZE (5)
55 0x81, 0x03, // INPUT (Cnst,Var,Abs)
56 0x05, 0x01, // USAGE_PAGE (Generic Desktop)
57 0x09, 0x30, // USAGE (X)
58 0x09, 0x31, // USAGE (Y)
59 0x15, 0x81, // LOGICAL_MINIMUM (-127)
60 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
61 0x75, 0x08, // REPORT_SIZE (8)
62 0x95, 0x02, // REPORT_COUNT (2)
63 0x81, 0x06, // INPUT (Data,Var,Rel)
64 0x09, 0x38, // USAGE (Mouse Wheel)
65 0x15, 0x81, // LOGICAL_MINIMUM (-127)
66 0x25, 0x7f, // LOGICAL_MAXIMUM (127)
67 0x75, 0x08, // REPORT_SIZE (8)
68 0x95, 0x01, // REPORT_COUNT (1)
69 0x81, 0x06, // INPUT (Data,Var,Rel)
70 0xc0, // END_COLLECTION
71 0xc0 // END_COLLECTION
72 };
73 #define HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0]))
74
75 #define LSB(x) (x & 0x00ff)
76 #define MSB(x) ((x & 0xff00) >> 8)
77
78 /* Configuration descriptor 9 bytes */
79 #define CFG_DESC(wTotalLength, bNumInterfaces, bConfigurationValue)\
80 /* Configuration 1 descriptor 9 bytes */\
81 0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
82 (bNumInterfaces), (bConfigurationValue), 0x00,\
83 0x40, 0x00,
84 #define CFG_DESC_LEN 9
85
86 /* DPUMP interface descriptors 9+7+7+7=30 bytes. */
87 #define DPUMP_IFC_DESC_ALL(ifc, bulk_in_epa, bulk_out_epa, int_in_epa) \
88 /* Interface descriptor */\
89 0x09, 0x04, (ifc), 0x00, 0x03, 0x99, 0x99, 0x99, 0x00,\
90 /* Endpoint descriptor (Bulk Out) */\
91 0x07, 0x05, (bulk_out_epa), 0x02, 0x40, 0x00, 0x00,\
92 /* Endpoint descriptor (Bulk In) */\
93 0x07, 0x05, (bulk_in_epa), 0x02, 0x40, 0x00, 0x00,\
94 /* Endpoint descriptor (Interrupt In) */\
95 0x07, 0x05, (int_in_epa), 0x02, 0x40, 0x00, 0x00,
96 #define DPUMP_IFC_DESC_ALL_LEN 30
97
98 /* HID Mouse interface descriptors 9+9+7=25 bytes */
99 #define HID_MOUSE_IFC_DESC_ALL(ifc, interrupt_epa) \
100 /* Interface descriptor */\
101 0x09, 0x04, (ifc), 0x01, 0x01, 0x03, 0x00, 0x00, 0x00,\
102 /* HID descriptor */\
103 0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),\
104 MSB(HID_MOUSE_REPORT_LENGTH),\
105 /* Endpoint descriptor (Interrupt) */\
106 0x07, 0x05, (interrupt_epa), 0x03, 0x08, 0x00, 0x08,
107 #define HID_MOUSE_IFC_DESC_ALL_LEN 25
108
109 /* Define the counters used in the test application... */
110
111 static ULONG thread_0_counter;
112 static ULONG thread_1_counter;
113 static ULONG error_counter;
114
115 static UCHAR expect_errors = UX_FALSE;
116
117 static UCHAR buffer[UX_TEST_BUFFER_SIZE];
118
119 /* Define USBX test global variables. */
120
121 static UX_HOST_CLASS *class_driver;
122 static UX_HOST_CLASS_DPUMP *dpump;
123 static UX_SLAVE_CLASS_DPUMP *dpump_slave = UX_NULL;
124
125
126 static UCHAR device_framework_full_speed[] = {
127
128 /* Device descriptor 18 bytes */
129 0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
130 0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
131 0x00, 0x01,
132
133 /* Configuration descriptor 9 bytes */
134 CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC_DESC_ALL_LEN, 2, 1)
135 /* DPUMP 9+7+7+7 =30 */
136 DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83)
137 /* HID 9+9+7 =25 */
138 HID_MOUSE_IFC_DESC_ALL(1, 0x84)
139 };
140 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
141
142 static UCHAR device_framework_high_speed[] = {
143
144 /* Device descriptor */
145 0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
146 0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
147 0x03, 0x01,
148
149 /* Device qualifier descriptor */
150 0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
151 0x01, 0x00,
152
153 /* Configuration descriptor 9 bytes */
154 CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC_DESC_ALL_LEN, 2, 1)
155 /* DPUMP 9+7+7+7 =30 */
156 DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83)
157 /* HID 9+9+7 =25 */
158 HID_MOUSE_IFC_DESC_ALL(1, 0x84)
159 };
160 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
161
162 /* String Device Framework :
163 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
164 Byte 2 : Byte containing the index of the descriptor
165 Byte 3 : Byte containing the length of the descriptor string
166 */
167
168 static UCHAR string_framework[] = {
169
170 /* Manufacturer string descriptor : Index 1 */
171 0x09, 0x04, 0x01, 0x0c,
172 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
173 0x6f, 0x67, 0x69, 0x63,
174
175 /* Product string descriptor : Index 2 */
176 0x09, 0x04, 0x02, 0x0c,
177 0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
178 0x44, 0x65, 0x6d, 0x6f,
179
180 /* Serial Number string descriptor : Index 3 */
181 0x09, 0x04, 0x03, 0x04,
182 0x30, 0x30, 0x30, 0x31
183 };
184 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
185
186 /* Multiple languages are supported on the device, to add
187 a language besides English, the unicode language code must
188 be appended to the language_id_framework array and the length
189 adjusted accordingly. */
190 static UCHAR language_id_framework[] = {
191
192 /* English. */
193 0x09, 0x04
194 };
195 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
196
197 /* Simulation actions. */
198
199 static UX_TEST_SIM_ENTRY_ACTION dcd_endpoint_create_fail[] = {
200 /* function, request to match,
201 port action, port status,
202 request action, request EP, request data, request actual length, request status,
203 status, additional callback,
204 no_return */
205 { UX_DCD_CREATE_ENDPOINT, NULL,
206 UX_FALSE, 0,
207 0 , 0, UX_NULL, 0, 0,
208 UX_SUCCESS , UX_NULL,
209 UX_TRUE},
210 { UX_DCD_CREATE_ENDPOINT, NULL,
211 UX_FALSE, 0,
212 0 , 0, UX_NULL, 0, 0,
213 UX_ERROR , UX_NULL},
214 { UX_DCD_CREATE_ENDPOINT, NULL,
215 UX_FALSE, 0,
216 0 , 0, UX_NULL, 0, 0,
217 UX_ERROR , UX_NULL},
218 { UX_DCD_CREATE_ENDPOINT, NULL,
219 UX_FALSE, 0,
220 0 , 0, UX_NULL, 0, 0,
221 UX_ERROR , UX_NULL},
222 { UX_DCD_CREATE_ENDPOINT, NULL,
223 UX_FALSE, 0,
224 0 , 0, UX_NULL, 0, 0,
225 UX_ERROR , UX_NULL},
226 { 0 }
227 };
228
229
230 /* Define prototypes for external Host Controller's (HCDs), classes and clients. */
231
232 static VOID ux_test_instance_activate(VOID *dpump_instance);
233 static VOID ux_test_instance_deactivate(VOID *dpump_instance);
234
235 UINT _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
236 UINT ux_hcd_sim_initialize(UX_HCD *hcd);
237 UINT _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
238 ULONG requested_length, ULONG *actual_length);
239 UINT _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
240 ULONG requested_length, ULONG *actual_length);
241
242 static TX_THREAD ux_test_thread_simulation_0;
243 static TX_THREAD ux_test_thread_simulation_1;
244 static void ux_test_thread_simulation_0_entry(ULONG);
245 static void ux_test_thread_simulation_1_entry(ULONG);
246
247
248 /* Define the ISR dispatch. */
249
250 extern VOID (*test_isr_dispatch)(void);
251
252
253 /* Prototype for test control return. */
254
255 void test_control_return(UINT status);
256
257
258 /* Define the ISR dispatch routine. */
259
test_isr(void)260 static void test_isr(void)
261 {
262
263 /* For further expansion of interrupt-level testing. */
264 }
265
266
error_callback(UINT system_level,UINT system_context,UINT error_code)267 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
268 {
269 if (!expect_errors)
270 {
271 if (error_code != UX_DEVICE_HANDLE_UNKNOWN)
272 {
273 /* Failed test. */
274 printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
275 test_control_return(1);
276 }
277 }
278 }
279
test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND * command)280 static UINT test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND *command)
281 {
282 switch(command->ux_slave_class_command_request)
283 {
284 case UX_SLAVE_CLASS_COMMAND_INITIALIZE:
285 case UX_SLAVE_CLASS_COMMAND_QUERY:
286 return UX_SUCCESS;
287
288 default:
289 return UX_NO_CLASS_MATCH;
290 }
291 }
292
293 /* Define what the initial system looks like. */
294
295 #ifdef CTEST
test_application_define(void * first_unused_memory)296 void test_application_define(void *first_unused_memory)
297 #else
298 void usbx_ux_device_stack_transfer_request_test_application_define(void *first_unused_memory)
299 #endif
300 {
301
302 UINT status;
303 CHAR *stack_pointer;
304 CHAR *memory_pointer;
305 UX_SLAVE_CLASS_DPUMP_PARAMETER parameter;
306 UX_SLAVE_CLASS_HID_PARAMETER hid_parameter;
307
308 /* Inform user. */
309 printf("Running ux_device_stack_transfer_request Test....................... ");
310 #if !(UX_TEST_MULTI_IFC_ON)
311 printf("SKIP!");
312 test_control_return(0);
313 return;
314 #endif
315
316 /* Initialize the free memory pointer. */
317 stack_pointer = (CHAR *) first_unused_memory;
318 memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
319
320 /* Initialize USBX Memory. */
321 status = ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
322
323 /* Check for error. */
324 if (status != UX_SUCCESS)
325 {
326
327 printf("ERROR #1\n");
328 test_control_return(1);
329 }
330
331 /* Register the error callback. */
332 _ux_utility_error_callback_register(error_callback);
333
334 /* The code below is required for installing the host portion of USBX. */
335 status = ux_host_stack_initialize(UX_NULL);
336
337 /* Check for error. */
338 if (status != UX_SUCCESS)
339 {
340
341 printf("ERROR #2\n");
342 test_control_return(1);
343 }
344
345 /* Register all the host class drivers for this USBX implementation. */
346 status = ux_host_stack_class_register(_ux_system_host_class_dpump_name, ux_host_class_dpump_entry);
347
348 /* Check for error. */
349 if (status != UX_SUCCESS)
350 {
351
352 printf("ERROR #3\n");
353 test_control_return(1);
354 }
355
356 /* The code below is required for installing the device portion of USBX */
357 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
358 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
359 string_framework, STRING_FRAMEWORK_LENGTH,
360 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
361
362 /* Check for error. */
363 if (status != UX_SUCCESS)
364 {
365
366 printf("ERROR #5\n");
367 test_control_return(1);
368 }
369
370 /* Set the parameters for callback when insertion/extraction of a Data Pump device. */
371 parameter.ux_slave_class_dpump_instance_activate = ux_test_instance_activate;
372 parameter.ux_slave_class_dpump_instance_deactivate = ux_test_instance_deactivate;
373
374 /* Initialize the device dpump class. The class is connected with interface 0 */
375 status = ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
376 1, 0, ¶meter);
377
378 /* Initialize the hid class parameters. */
379 hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
380 hid_parameter.ux_device_class_hid_parameter_report_length = HID_MOUSE_REPORT_LENGTH;
381 hid_parameter.ux_device_class_hid_parameter_callback = UX_NULL;
382 hid_parameter.ux_slave_class_hid_instance_activate = UX_NULL;
383
384 status |= ux_device_stack_class_register(_ux_system_slave_class_hid_name, test_ux_device_class_hid_entry,
385 1, 1, ¶meter);
386 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
387 /* Check for error. */
388 if (status != UX_SUCCESS)
389 {
390
391 printf("ERROR #6\n");
392 test_control_return(1);
393 }
394 #endif
395 /* Initialize the simulated device controller. */
396 status = _ux_test_dcd_sim_slave_initialize();
397
398 /* Check for error. */
399 if (status != UX_SUCCESS)
400 {
401
402 printf("ERROR #7\n");
403 test_control_return(1);
404 }
405
406 /* Register all the USB host controllers available in this system */
407 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
408
409 /* Check for error. */
410 if (status != UX_SUCCESS)
411 {
412
413 printf("ERROR #4\n");
414 test_control_return(1);
415 }
416
417 /* Create the main host simulation thread. */
418 status = tx_thread_create(&ux_test_thread_simulation_0, "test host simulation", ux_test_thread_simulation_0_entry, 0,
419 stack_pointer, UX_TEST_STACK_SIZE,
420 20, 20, 1, TX_AUTO_START);
421
422 /* Check for error. */
423 if (status != TX_SUCCESS)
424 {
425
426 printf("ERROR #8\n");
427 test_control_return(1);
428 }
429 }
430
431
ux_test_thread_simulation_0_entry(ULONG arg)432 static void ux_test_thread_simulation_0_entry(ULONG arg)
433 {
434
435 UINT status;
436 UX_DEVICE *device;
437 UX_ENDPOINT *control_endpoint;
438 UX_TRANSFER *transfer_request;
439 UX_SLAVE_DEVICE *slave_device;
440 UX_SLAVE_TRANSFER *slave_transfer_request;
441
442 ux_test_dcd_sim_slave_connect(UX_HIGH_SPEED_DEVICE);
443 ux_test_hcd_sim_host_connect(UX_HIGH_SPEED_DEVICE);
444
445 /* Enumeration check. */
446 if (dpump_slave == UX_NULL)
447 {
448
449 printf("ERROR #%d: Enum fail\n", __LINE__);
450 test_control_return(1);
451 }
452
453 status = ux_host_stack_device_get(0, &device);
454 if (status != UX_SUCCESS)
455 {
456
457 printf("ERROR #%d: device_get fail\n", __LINE__);
458 test_control_return(1);
459 }
460 control_endpoint = &device->ux_device_control_endpoint;
461 transfer_request = &control_endpoint->ux_endpoint_transfer_request;
462
463 /* Get existing configuration */
464 transfer_request -> ux_transfer_request_data_pointer = buffer;
465 transfer_request -> ux_transfer_request_requested_length = 256;
466 transfer_request -> ux_transfer_request_function = UX_GET_DESCRIPTOR;
467 transfer_request -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
468 transfer_request -> ux_transfer_request_index = 0;
469
470 transfer_request -> ux_transfer_request_value = UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
471 status = ux_host_stack_transfer_request(transfer_request);
472 if (status != UX_SUCCESS)
473 {
474
475 printf("ERROR #%d: GetConfigurationDescriptor(0, 256) fail\n", __LINE__);
476 error_counter ++;
477 }
478
479 /* Get slave device and transfer request. */
480 slave_device = &_ux_system_slave->ux_system_slave_device;
481 slave_transfer_request = &slave_device->ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
482
483 /* Reset configuration. */
484 status = ux_host_stack_device_configuration_reset(device);
485 if (status != UX_SUCCESS)
486 {
487 printf("ERROR #%d: Configuration Reset fail\n", __LINE__);
488 test_control_return(1);
489 }
490 /* Confirm it's in addressed state.
491 See USBX-188, USBX-161
492 */
493 if (slave_device->ux_slave_device_state != UX_DEVICE_ADDRESSED)
494 {
495 // printf("Correct state: %x -> %x (UX_DEVICE_ADDRESSED)\n", slave_device->ux_slave_device_state, UX_DEVICE_ADDRESSED);
496 slave_device->ux_slave_device_state = UX_DEVICE_ADDRESSED;
497 }
498
499 /* Get existing configuration */
500 transfer_request -> ux_transfer_request_data_pointer = buffer;
501 transfer_request -> ux_transfer_request_requested_length = 256;
502 transfer_request -> ux_transfer_request_function = UX_GET_DESCRIPTOR;
503 transfer_request -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
504 transfer_request -> ux_transfer_request_index = 0;
505
506 transfer_request -> ux_transfer_request_value = UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
507 status = ux_host_stack_transfer_request(transfer_request);
508 if (status != UX_SUCCESS)
509 {
510
511 printf("ERROR #%d: GetConfigurationDescriptor(0, 256) fail\n", __LINE__);
512 error_counter ++;
513 }
514
515 /* Disconnect. */
516 ux_test_dcd_sim_slave_disconnect();
517
518 if (slave_device->ux_slave_device_state != UX_DEVICE_RESET)
519 {
520 printf("ERROR #%d: slave state not UX_DEVICE_RESET but 0x%lx\n", __LINE__, slave_device->ux_slave_device_state);
521 test_control_return(1);
522 }
523
524 status = ux_device_stack_transfer_request(slave_transfer_request, 8, 8);
525 if (status == UX_SUCCESS)
526 {
527
528 printf("ERROR #%d: ux_device_stack_transfer_request(8, 8) should fail\n", __LINE__);
529 error_counter ++;
530 }
531
532 /* Sleep for a tick to make sure everything is complete. */
533 tx_thread_sleep(1);
534
535 /* Check for errors from other threads. */
536 if (error_counter)
537 {
538
539 /* Test error. */
540 printf("ERROR #14\n");
541 test_control_return(1);
542 }
543 else
544 {
545
546 /* Successful test. */
547 printf("SUCCESS!\n");
548 test_control_return(0);
549 }
550 }
551
ux_test_instance_activate(VOID * dpump_instance)552 static VOID ux_test_instance_activate(VOID *dpump_instance)
553 {
554
555 /* Save the DPUMP instance. */
556 dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
557 }
558
ux_test_instance_deactivate(VOID * dpump_instance)559 static VOID ux_test_instance_deactivate(VOID *dpump_instance)
560 {
561
562 /* Reset the DPUMP instance. */
563 dpump_slave = UX_NULL;
564 }
565
566