1 /* This test is designed to test the BOS. */
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_test.h"
10 #include "ux_test_utility_sim.h"
11 #include "ux_test_hcd_sim_host.h"
12 #include "ux_test_dcd_sim_slave.h"
13 #include "ux_host_class_dummy.h"
14
15
16 /* Define USBX test constants. */
17
18 #define UX_TEST_STACK_SIZE 4096
19 #define UX_TEST_BUFFER_SIZE 2048
20 #define UX_TEST_RUN 1
21 #define UX_TEST_MEMORY_SIZE (64*1024)
22
23
24 /* Define the counters used in the test application... */
25
26 static ULONG thread_0_counter;
27 static ULONG thread_1_counter;
28 static ULONG error_counter;
29
30 static UCHAR buffer[UX_TEST_BUFFER_SIZE];
31
32 static UX_HOST_CLASS_DUMMY *dummy;
33
34 static UX_DEVICE *device;
35
36 /* Define USBX test global variables. */
37
38 #define DW3(x) (((x) >> 24) & 0xFFu)
39 #define DW2(x) (((x) >> 16) & 0xFFu)
40 #define DW1(x) (((x) >> 8) & 0xFFu)
41 #define DW0(x) ( (x) & 0xFFu)
42 #define W1(x) (((x) >> 8) & 0xFFu)
43 #define W0(x) ( (x) & 0xFFu)
44
45 #define _DEVICE_DESCRIPTOR \
46 /* Device descriptor. */ \
47 18, UX_DEVICE_DESCRIPTOR_ITEM, \
48 W0(0x201), W1(0x201), \
49 UX_CLASS_BILLBOARD_CLASS, \
50 UX_CLASS_BILLBOARD_SUBCLASS, \
51 UX_CLASS_BILLBOARD_PROTOCOL, \
52 64, \
53 W0(0xec08), W1(0xec08), \
54 W0(0x0001), W1(0x0001), \
55 W0(0x0001), W1(0x0001), \
56 0, 0, 0, \
57 1
58
59 #define _DEVICE_QUALIFIER_DESCRIPTOR \
60 /* Device Qualifier Descriptor. */ \
61 10, UX_DEVICE_QUALIFIER_DESCRIPTOR_ITEM,\
62 W0(0x201), W1(0x201), \
63 UX_CLASS_BILLBOARD_CLASS, \
64 UX_CLASS_BILLBOARD_SUBCLASS, \
65 UX_CLASS_BILLBOARD_PROTOCOL, \
66 8, \
67 1, \
68 0
69
70 #define _CONFIGURATION_DESCRIPTOR \
71 /* Configuration Descriptor. */ \
72 9, UX_CONFIGURATION_DESCRIPTOR_ITEM,\
73 W0(18), W1(18), \
74 1, 1, \
75 0, 0xc0, 0x32
76
77 #define _INTERFACE_DESCRIPTOR \
78 /* Interface Descriptor. */ \
79 9, UX_INTERFACE_DESCRIPTOR_ITEM,\
80 0, 0, 0, \
81 UX_CLASS_BILLBOARD_CLASS, \
82 UX_CLASS_BILLBOARD_SUBCLASS, \
83 UX_CLASS_BILLBOARD_PROTOCOL, \
84 0
85
86 #define _BOS_DESCRIPTORS_LENGTH (5+7+56+8+8+8)
87 #define _BOS_DESCRIPTORS \
88 /* BOS descriptor. */ \
89 5, UX_BOS_DESCRIPTOR_ITEM, \
90 W0(_BOS_DESCRIPTORS_LENGTH), W1(_BOS_DESCRIPTORS_LENGTH), 1,\
91 /* USB 2.0 Extension descriptor */ \
92 7, UX_DEVICE_CAPABILITY_DESCRIPTOR_ITEM, UX_CAPABILITY_USB_2_0_EXTENSION, \
93 DW0(0x00000002u), DW1(0x00000002u), DW2(0x00000002u), DW3(0x00000002u), \
94 /* Billboard Capability Descriptor Example (44+3*4=56) */\
95 56, UX_DEVICE_CAPABILITY_DESCRIPTOR_ITEM, UX_CAPABILITY_BILLBOARD, 0, \
96 1, 0, \
97 W0(0), W1(0), \
98 0x3F, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
99 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
100 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
101 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, \
102 W0(0x0000), W0(0x0000), \
103 0x00, 0x00, \
104 W0(0x8087), W1(0x8087), 0x00, 0x00, \
105 W0(0x8087), W1(0x8087), 0x01, 0x00, \
106 W0(0xFF01), W1(0xFF01), 0x00, 0x00, \
107 /* Billboard Alternate Mode Capability Descriptor Examples */\
108 8, UX_DEVICE_CAPABILITY_DESCRIPTOR_ITEM, UX_CAPABILITY_BILLBOARD_EX, \
109 0, DW0(0x00000010), DW1(0x00000010), DW2(0x00000010), DW3(0x00000010), \
110 8, UX_DEVICE_CAPABILITY_DESCRIPTOR_ITEM, UX_CAPABILITY_BILLBOARD_EX, \
111 1, DW0(0x00000002), DW1(0x00000002), DW2(0x00000002), DW3(0x00000002), \
112 8, UX_DEVICE_CAPABILITY_DESCRIPTOR_ITEM, UX_CAPABILITY_BILLBOARD_EX, \
113 2, DW0(0x000C00C5), DW1(0x000C00C5), DW2(0x000C00C5), DW3(0x000C00C5)
114
115
116 static UCHAR device_framework_full_speed[] = {
117
118 _DEVICE_DESCRIPTOR,
119 _BOS_DESCRIPTORS,
120 _CONFIGURATION_DESCRIPTOR,
121 _INTERFACE_DESCRIPTOR,
122 };
123 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
124
125 static UCHAR device_framework_high_speed[] = {
126
127 _DEVICE_DESCRIPTOR,
128 _BOS_DESCRIPTORS,
129 _DEVICE_QUALIFIER_DESCRIPTOR,
130 _CONFIGURATION_DESCRIPTOR,
131 _INTERFACE_DESCRIPTOR,
132 };
133 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
134
135
136 /* String Device Framework :
137 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
138 Byte 2 : Byte containing the index of the descriptor
139 Byte 3 : Byte containing the length of the descriptor string
140 */
141 static UCHAR string_framework[] = {
142
143 /* Manufacturer string descriptor : Index 1 */
144 0x09, 0x04, 0x01, 0x0c,
145 0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x20, 0x4c,
146 0x6f, 0x67, 0x69, 0x63,
147
148 /* Product string descriptor : Index 2 */
149 0x09, 0x04, 0x02, 0x0c,
150 0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
151 0x44, 0x65, 0x6d, 0x6f,
152
153 /* Serial Number string descriptor : Index 3 */
154 0x09, 0x04, 0x03, 0x04,
155 0x30, 0x30, 0x30, 0x31
156 };
157 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
158
159
160 /* Multiple languages are supported on the device, to add
161 a language besides English, the unicode language code must
162 be appended to the language_id_framework array and the length
163 adjusted accordingly. */
164 static UCHAR language_id_framework[] = {
165
166 /* English. */
167 0x09, 0x04
168 };
169 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
170
171
172 /* Define prototypes for external Host Controller's (HCDs), classes and clients. */
173
174 static TX_THREAD ux_test_thread_host_simulation;
175 static TX_THREAD ux_test_thread_slave_simulation;
176 static void ux_test_thread_host_simulation_entry(ULONG);
177 static void ux_test_thread_slave_simulation_entry(ULONG);
178
179 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst);
180
181 /* Define the ISR dispatch. */
182
183 extern VOID (*test_isr_dispatch)(void);
184
185
186 /* Prototype for test control return. */
187
188 void test_control_return(UINT status);
189
190
191 /* Define the ISR dispatch routine. */
192
test_isr(void)193 static void test_isr(void)
194 {
195
196 /* For further expansion of interrupt-level testing. */
197 }
198
199 static ULONG enum_done_count = 0;
error_callback(UINT system_level,UINT system_context,UINT error_code)200 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
201 {
202 if (error_code == UX_DEVICE_ENUMERATION_FAILURE)
203 enum_done_count ++;
204
205 if (error_code != UX_DEVICE_HANDLE_UNKNOWN &&
206 error_code != UX_DEVICE_ENUMERATION_FAILURE)
207 {
208 /* Failed test. */
209 printf("Error #%d, system_level: %d, system_context: %d, error code: 0x%x\n", __LINE__, system_level, system_context, error_code);
210 test_control_return(1);
211 }
212 }
213
214 /* Define what the initial system looks like. */
215
216 #ifdef CTEST
test_application_define(void * first_unused_memory)217 void test_application_define(void *first_unused_memory)
218 #else
219 void usbx_ux_host_stack_enum_bMaxPacketSize0_application_define(void *first_unused_memory)
220 #endif
221 {
222
223 UINT status;
224 CHAR *stack_pointer;
225 CHAR *memory_pointer;
226
227
228 printf("Running ux_host_stack bMaxPacketSize0 Test..........................");
229
230 /* Initialize the free memory pointer. */
231 stack_pointer = (CHAR *) first_unused_memory;
232 memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
233
234 /* Initialize USBX Memory. */
235 status = ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
236
237 /* Check for error. */
238 if (status != UX_SUCCESS)
239 {
240
241 printf("ERROR #%d\n", __LINE__);
242 test_control_return(1);
243 }
244
245 /* Register the error callback. */
246 _ux_utility_error_callback_register(error_callback);
247
248 /* The code below is required for installing the host portion of USBX. */
249 status = ux_host_stack_initialize(test_host_change_function);
250
251 /* Check for error. */
252 if (status != UX_SUCCESS)
253 {
254
255 printf("ERROR #%d\n", __LINE__);
256 test_control_return(1);
257 }
258
259 /* Register a dummy class. */
260 status = ux_host_stack_class_register(_ux_host_class_dummy_name, _ux_host_class_dummy_entry);
261 UX_TEST_CHECK_SUCCESS(status);
262
263 /* The code below is required for installing the device portion of USBX */
264 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
265 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
266 string_framework, STRING_FRAMEWORK_LENGTH,
267 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
268
269 /* Check for error. */
270 if (status != UX_SUCCESS)
271 {
272
273 printf("ERROR #%d\n", __LINE__);
274 test_control_return(1);
275 }
276
277 /* Initialize the simulated device controller. */
278 status = _ux_dcd_sim_slave_initialize();
279
280 /* Check for error. */
281 if (status != UX_SUCCESS)
282 {
283
284 printf("ERROR #%d\n", __LINE__);
285 test_control_return(1);
286 }
287
288 /* Register all the USB host controllers available in this system */
289 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
290
291 /* Check for error. */
292 if (status != UX_SUCCESS)
293 {
294
295 printf("ERROR #%d\n", __LINE__);
296 test_control_return(1);
297 }
298
299 /* Create the main host simulation thread. */
300 status = tx_thread_create(&ux_test_thread_host_simulation, "test host simulation", ux_test_thread_host_simulation_entry, 0,
301 stack_pointer, UX_TEST_STACK_SIZE,
302 20, 20, 1, TX_AUTO_START);
303
304 /* Check for error. */
305 if (status != TX_SUCCESS)
306 {
307
308 printf("ERROR #%d\n", __LINE__);
309 test_control_return(1);
310 }
311 }
312
ux_test_thread_host_simulation_entry(ULONG arg)313 static void ux_test_thread_host_simulation_entry(ULONG arg)
314 {
315
316 UINT status;
317 INT i;
318 ULONG mem_free;
319 const struct _test {
320 ULONG bMaxPacketSize0;
321 UINT expect_no_device;
322 } tests[] = {
323 { 0, UX_FALSE},
324 { 8, UX_FALSE},
325 { 16, UX_FALSE},
326 { 32, UX_FALSE},
327 { 64, UX_FALSE},
328 {128, UX_FALSE},
329 { 20, UX_FALSE},
330 };
331 #define N_TESTS (sizeof(tests)/sizeof(struct _test))
332
333 /* Wait device connection. */
334 stepinfo(">>>>>>>>>>>>>>>> Test connect\n");
335 status = ux_test_wait_for_value_ulong(&enum_done_count, 1);
336 if (status != UX_SUCCESS)
337 {
338 printf("ERROR #%d\n", __LINE__);
339 test_control_return(1);
340 }
341
342 /* Test disconnect. */
343 stepinfo(">>>>>>>>>>>>>>>> Test disconnect\n");
344 ux_test_dcd_sim_slave_disconnect();
345 ux_test_hcd_sim_host_disconnect();
346 status = ux_test_wait_for_null_wait_time((VOID**)&device, 1000);
347 if (status != UX_SUCCESS)
348 {
349 printf("ERROR #%d\n", __LINE__);
350 test_control_return(1);
351 }
352 mem_free = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
353
354 for (i = 0; i < N_TESTS; i ++)
355 {
356 // printf("Test %d\n", i);
357
358 enum_done_count = 0;
359 device_framework_full_speed[7] = tests[i].bMaxPacketSize0;
360 device_framework_high_speed[7] = tests[i].bMaxPacketSize0;
361 ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE);
362 ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
363
364 status = ux_test_wait_for_value_ulong(&enum_done_count, 1);
365 if (status != UX_SUCCESS)
366 {
367 printf("ERROR #%d\n", __LINE__);
368 test_control_return(1);
369 }
370
371 status = ux_host_stack_device_get(0, &device);
372 if (tests[i].expect_no_device)
373 {
374 if (status == UX_SUCCESS)
375 {
376 printf("ERROR #%d\n", __LINE__);
377 test_control_return(1);
378 }
379 }
380 else
381 {
382 if (status != UX_SUCCESS)
383 {
384 printf("ERROR #%d\n", __LINE__);
385 test_control_return(1);
386 }
387
388 ux_test_dcd_sim_slave_disconnect();
389 ux_test_hcd_sim_host_disconnect();
390 status = ux_test_wait_for_null_wait_time((VOID**)&device, 3000);
391 if (status != UX_SUCCESS)
392 {
393 printf("ERROR #%d\n", __LINE__);
394 test_control_return(1);
395 }
396 }
397
398 if (_ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available != mem_free)
399 {
400 printf("ERROR #%d\n", __LINE__);
401 test_control_return(1);
402 }
403 }
404
405 /* Sleep for a tick to make sure everything is complete. */
406 tx_thread_sleep(1);
407
408 /* Check for errors from other threads. */
409 if (error_counter)
410 {
411
412 /* Test error. */
413 printf("ERROR #%d\n", __LINE__);
414 test_control_return(1);
415 }
416 else
417 {
418
419 /* Successful test. */
420 printf("SUCCESS\n");
421 test_control_return(0);
422 }
423 }
424
test_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)425 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
426 {
427
428 UX_HOST_CLASS_DUMMY *dummy_ptr = (UX_HOST_CLASS_DUMMY *) inst;
429
430 // printf("hCHG:%lx,%p,%p\n", event, cls, inst);
431 switch(event)
432 {
433
434 case UX_DEVICE_INSERTION:
435
436 // printf("hINS:%p,%p:%ld\n", cls, inst, dummy -> ux_host_class_dummy_interface -> ux_interface_descriptor.bInterfaceNumber);
437 if (dummy == UX_NULL)
438 {
439 dummy = dummy_ptr;
440 enum_done_count ++;
441 }
442 break;
443
444 case UX_DEVICE_REMOVAL:
445
446 // printf("hRMV:%p,%p:%ld\n", cls, inst, dummy -> ux_host_class_dummy_interface -> ux_interface_descriptor.bInterfaceNumber);
447 if (dummy == dummy_ptr)
448 dummy = UX_NULL;
449 break;
450
451 case UX_DEVICE_CONNECTION:
452 if (device == UX_NULL)
453 device = (UX_DEVICE *)inst;
454 break;
455
456 case UX_DEVICE_DISCONNECTION:
457 if ((VOID *)device == inst)
458 device = UX_NULL;
459 break;
460
461 default:
462 break;
463 }
464 return 0;
465 }
466