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 "ux_host_class_dpump.h"
10 #include "ux_device_class_dpump.h"
11
12 #include "ux_host_stack.h"
13
14 #include "ux_test_hcd_sim_host.h"
15 #include "ux_test_utility_sim.h"
16
17 /* Define USBX demo constants. */
18
19 #define UX_DEMO_STACK_SIZE 4096
20 #define UX_DEMO_BUFFER_SIZE 2048
21 #define UX_DEMO_RUN 1
22 #define UX_DEMO_MEMORY_SIZE (64*1024)
23
24
25 /* Define the counters used in the demo application... */
26
27 static ULONG test_error_cases = UX_FALSE;
28
29 static ULONG thread_0_counter;
30 static ULONG thread_1_counter;
31 static ULONG error_counter;
32
33 /* Define USBX demo global variables. */
34
35 static UX_HOST_CLASS *class_driver;
36 static UX_HOST_CLASS_DPUMP *dpump;
37 static UX_SLAVE_CLASS_DPUMP *dpump_slave;
38
39 static UCHAR test_sim_entry;
40 static ULONG test_sim_query_usage;
41 static ULONG test_sim_query_count;
42 static ULONG test_sim_activate_count;
43 static ULONG test_sim_deactivate_count;
44
45 static UCHAR device_framework_full_speed[] = {
46
47 /* Device descriptor */
48 0x12, 0x01, 0x10, 0x01,
49 0x00, 0x00, 0x00, /* bDeviceClass, bDeviceSubClass, bDeviceProtocol */
50 0x08, /* bMaxPacketSize0 */
51 0xec, 0x08, 0x10, 0x00, /* idVendor, idProduct */
52 0x00, 0x00,
53 0x00, 0x00, 0x00,
54 0x01, /* bNumConfigurations */
55
56 /* Configuration descriptor */
57 0x09, 0x02,
58 0x20, 0x00, /* wTotalLength */
59 0x01, 0x01, /* bNumInterfaces, bConfigurationValue */
60 0x00,
61 0xc0, 0x32, /* bmAttributes, bMaxPower */
62
63 /* Interface descriptor */
64 0x09, 0x04,
65 0x00, 0x00, 0x02, /* bInterfaceNumber, bAlternateSetting, bNumEndpoints */
66 0x99, 0x99, 0x99, /* bInterfaceClass, bInterfaceSubClass, bInterfaceProtocol */
67 0x00,
68
69 /* Endpoint descriptor (Bulk Out) */
70 0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00,
71
72 /* Endpoint descriptor (Bulk In) */
73 0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00
74 };
75 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
76
77
78 static UCHAR device_framework_high_speed[] = {
79
80 /* Device descriptor */
81 0x12, 0x01, 0x00, 0x02,
82 0x00, 0x00, 0x00, /* bDeviceClass, bDeviceSubClass, bDeviceProtocol */
83 0x40, /* bMaxPacketSize0 */
84 0x0a, 0x07, /* idVendor */
85 0x25, 0x40, /* idProduct */
86 0x01, 0x00,
87 0x01, 0x02, 0x03,
88 0x01, /* bNumConfigurations */
89
90 /* Device qualifier descriptor */
91 0x0a, 0x06, 0x00, 0x02,
92 0x00, 0x00, 0x00, /* bDeviceClass, bDeviceSubClass, bDeviceProtocol */
93 0x40, /* bMaxPacketSize0 */
94 0x01, /* bNumConfigurations */
95 0x00,
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, 0x99, 0x99, 0x99,
103 0x00,
104
105 /* Endpoint descriptor (Bulk Out) */
106 0x07, 0x05, 0x01, 0x02, 0x00, 0x02, 0x00,
107
108 /* Endpoint descriptor (Bulk In) */
109 0x07, 0x05, 0x82, 0x02, 0x00, 0x02, 0x00
110 };
111 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
112
113 /* String Device Framework :
114 Byte 0 and 1 : Word containing the language ID : 0x0904 for US
115 Byte 2 : Byte containing the index of the descriptor
116 Byte 3 : Byte containing the length of the descriptor string
117 */
118 static UCHAR string_framework[] = {
119
120 /* Manufacturer string descriptor : Index 1 */
121 0x09, 0x04, 0x01, 0x0c,
122 0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
123 0x6f, 0x67, 0x69, 0x63,
124
125 /* Product string descriptor : Index 2 */
126 0x09, 0x04, 0x02, 0x0c,
127 0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
128 0x44, 0x65, 0x6d, 0x6f,
129
130 /* Serial Number string descriptor : Index 3 */
131 0x09, 0x04, 0x03, 0x04,
132 0x30, 0x30, 0x30, 0x31
133 };
134 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
135
136 /* Multiple languages are supported on the device, to add
137 a language besides English, the unicode language code must
138 be appended to the language_id_framework array and the length
139 adjusted accordingly. */
140 static UCHAR language_id_framework[] = {
141
142 /* English. */
143 0x09, 0x04
144 };
145 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
146
147
148 /* Define prototypes for external Host Controller's (HCDs), classes and clients. */
149
150 static VOID tx_demo_instance_activate(VOID *dpump_instance);
151 static VOID tx_demo_instance_deactivate(VOID *dpump_instance);
152
153 UINT _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
154 UINT ux_hcd_sim_initialize(UX_HCD *hcd);
155 UINT _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
156 ULONG requested_length, ULONG *actual_length);
157 UINT _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
158 ULONG requested_length, ULONG *actual_length);
159
160 static TX_THREAD tx_demo_thread_host_simulation;
161 static TX_THREAD tx_demo_thread_slave_simulation;
162 static void tx_demo_thread_host_simulation_entry(ULONG);
163 static void tx_demo_thread_slave_simulation_entry(ULONG);
164
test_ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND * command)165 static UINT test_ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command)
166 {
167
168 UX_DEVICE *device;
169
170 if (test_sim_entry)
171 {
172
173 switch (command -> ux_host_class_command_request)
174 {
175 case UX_HOST_CLASS_COMMAND_QUERY:
176 test_sim_query_count ++;
177 if (command -> ux_host_class_command_usage != test_sim_query_usage)
178 return UX_NO_CLASS_MATCH;
179 break;
180
181 case UX_HOST_CLASS_COMMAND_ACTIVATE:
182 device = (UX_DEVICE *) command -> ux_host_class_command_container;
183 device -> ux_device_class_instance = (VOID *)&device;
184 test_sim_activate_count ++;
185 break;
186
187 case UX_HOST_CLASS_COMMAND_DEACTIVATE:
188 test_sim_deactivate_count ++;
189 break;
190
191 default:
192 break;
193 }
194 return UX_SUCCESS;
195 }
196 return ux_host_class_dpump_entry(command);
197 }
198
199 /* Define the ISR dispatch. */
200
201 extern VOID (*test_isr_dispatch)(void);
202
203
204 /* Prototype for test control return. */
205
206 void test_control_return(UINT status);
207
208
209 /* Define the ISR dispatch routine. */
210
test_isr(void)211 static void test_isr(void)
212 {
213
214 /* For further expansion of interrupt-level testing. */
215 }
216
217
error_callback(UINT system_level,UINT system_context,UINT error_code)218 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
219 {
220
221 /* Failed test. */
222 if (!test_error_cases)
223 {
224
225 printf("Error on line %d, system_level: %d, system_context: %d, error code: 0x%x\n", __LINE__, system_level, system_context, error_code);
226 test_control_return(1);
227 }
228 }
229
230 /* Define what the initial system looks like. */
231
232 #ifdef CTEST
test_application_define(void * first_unused_memory)233 void test_application_define(void *first_unused_memory)
234 #else
235 void usbx_ux_host_stack_device_remove_test_application_define(void *first_unused_memory)
236 #endif
237 {
238
239 UINT status;
240 CHAR *stack_pointer;
241 CHAR *memory_pointer;
242 UX_SLAVE_CLASS_DPUMP_PARAMETER parameter;
243
244
245 /* Initialize the free memory pointer. */
246 stack_pointer = (CHAR *) first_unused_memory;
247 memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
248
249 /* Initialize USBX Memory. */
250 status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
251
252 /* Check for error. */
253 if (status != UX_SUCCESS)
254 {
255
256 printf("Running ux_host_stack_device_remove Test............................ ERROR #1\n");
257 test_control_return(1);
258 }
259
260 /* Register the error callback. */
261 _ux_utility_error_callback_register(error_callback);
262
263 /* The code below is required for installing the host portion of USBX. */
264 status = ux_host_stack_initialize(UX_NULL);
265
266 /* Check for error. */
267 if (status != UX_SUCCESS)
268 {
269
270 printf("Running ux_host_stack_device_remove Test............................ ERROR #2\n");
271 test_control_return(1);
272 }
273
274 /* Register all the host class drivers for this USBX implementation. */
275 status = ux_host_stack_class_register(_ux_system_host_class_dpump_name, test_ux_host_class_dpump_entry);
276
277 /* Check for error. */
278 if (status != UX_SUCCESS)
279 {
280
281 printf("Running ux_host_stack_device_remove Test............................ ERROR #3\n");
282 test_control_return(1);
283 }
284
285 /* The code below is required for installing the device portion of USBX */
286 status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
287 device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
288 string_framework, STRING_FRAMEWORK_LENGTH,
289 language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
290
291 /* Check for error. */
292 if (status != UX_SUCCESS)
293 {
294
295 printf("Running ux_host_stack_device_remove Test............................ ERROR #5\n");
296 test_control_return(1);
297 }
298
299 /* Set the parameters for callback when insertion/extraction of a Data Pump device. */
300 parameter.ux_slave_class_dpump_instance_activate = tx_demo_instance_activate;
301 parameter.ux_slave_class_dpump_instance_deactivate = tx_demo_instance_deactivate;
302
303 /* Initialize the device dpump class. The class is connected with interface 0 */
304 status = ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
305 1, 0, ¶meter);
306
307 /* Check for error. */
308 if (status != UX_SUCCESS)
309 {
310
311 printf("Running ux_host_stack_device_remove Test............................ ERROR #6\n");
312 test_control_return(1);
313 }
314
315 /* Initialize the simulated device controller. */
316 status = _ux_dcd_sim_slave_initialize();
317
318 /* Check for error. */
319 if (status != UX_SUCCESS)
320 {
321
322 printf("Running ux_host_stack_device_remove Test............................ ERROR #7\n");
323 test_control_return(1);
324 }
325
326 /* Register all the USB host controllers available in this system */
327 status = ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
328
329 /* Check for error. */
330 if (status != UX_SUCCESS)
331 {
332
333 printf("Running ux_host_stack_device_remove Test............................ ERROR #4\n");
334 test_control_return(1);
335 }
336
337 /* Create the main host simulation thread. */
338 status = tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
339 stack_pointer, UX_DEMO_STACK_SIZE,
340 20, 20, 1, TX_AUTO_START);
341
342 /* Check for error. */
343 if (status != TX_SUCCESS)
344 {
345
346 printf("Running ux_host_stack_device_remove Test............................ ERROR #8\n");
347 test_control_return(1);
348 }
349
350 /* Create the main demo thread. */
351 status = tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
352 stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
353 20, 20, 1, TX_AUTO_START);
354
355 /* Check for error. */
356 if (status != TX_SUCCESS)
357 {
358
359 printf("Running ux_host_stack_device_remove Test............................ ERROR #9\n");
360 test_control_return(1);
361 }
362 }
363
364
tx_demo_thread_host_simulation_entry(ULONG arg)365 static void tx_demo_thread_host_simulation_entry(ULONG arg)
366 {
367
368 UINT status;
369 ULONG tmp;
370 UX_HCD *hcd;
371 UX_DEVICE *device;
372 UX_DEVICE device_backup;
373
374 /* Inform user. */
375 printf("Running ux_host_stack_device_remove Test............................ ");
376
377 /* Simulate query good on device connect - associate class with device. */
378 test_sim_entry = UX_TRUE;
379 test_sim_query_usage = UX_HOST_CLASS_COMMAND_USAGE_PIDVID;
380 test_sim_query_count = 0;
381 test_sim_activate_count = 0;
382 test_sim_deactivate_count = 0;
383
384 /* Connect. */
385 ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
386 tx_thread_sleep(100);
387
388 /* Check device connection. */
389 status = ux_host_stack_device_get(0, &device);
390 if (status != UX_SUCCESS)
391 {
392
393 printf("ERROR #%d: fail to get device instance\n", __LINE__);
394 test_control_return(1);
395 }
396 if (test_sim_query_count == 0)
397 {
398
399 printf("ERROR #%d: class query not happen\n", __LINE__);
400 error_counter ++;
401 }
402 if (test_sim_activate_count == 0)
403 {
404
405 printf("ERROR #%d: class activate not happen\n", __LINE__);
406 error_counter ++;
407 }
408 if (test_sim_deactivate_count != 0)
409 {
410
411 printf("ERROR #%d: class deactivate not expected\n", __LINE__);
412 error_counter ++;
413 }
414
415 /* Get HCD instance. */
416 hcd = UX_DEVICE_HCD_GET(device);
417
418 /* Disconnect so device associated class call deactivate. */
419 ux_test_hcd_sim_host_disconnect();
420 if (test_sim_deactivate_count == 0)
421 {
422
423 printf("ERROR #%d: class deactivate not happen\n", __LINE__);
424 error_counter ++;
425 }
426
427 #if UX_MAX_DEVICES > 1
428 /* Simulate 3 invalid devices in ux_system_host_device_array. */
429
430 /* Handle OK, parent error. */
431 _ux_system_host -> ux_system_host_device_array[0].ux_device_handle = (ULONG)(ALIGN_TYPE)device;
432 _ux_system_host -> ux_system_host_device_array[0].ux_device_parent = &device_backup;
433
434 /* Handle OK, parent OK, port location error. */
435 _ux_system_host -> ux_system_host_device_array[1].ux_device_handle = (ULONG)(ALIGN_TYPE)device;
436 _ux_system_host -> ux_system_host_device_array[1].ux_device_parent = UX_NULL;
437 _ux_system_host -> ux_system_host_device_array[1].ux_device_port_location = 3;
438
439 /* Handle OK, parent OK, port location OK, hcd error. */
440 _ux_system_host -> ux_system_host_device_array[2].ux_device_handle = (ULONG)(ALIGN_TYPE)device;
441 _ux_system_host -> ux_system_host_device_array[2].ux_device_parent = UX_NULL;
442 _ux_system_host -> ux_system_host_device_array[2].ux_device_port_location = 0;
443 #if UX_MAX_HCD > 1
444 _ux_system_host -> ux_system_host_device_array[2].ux_device_hcd = UX_NULL;
445 #endif
446 /* Connect. */
447 ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
448 tx_thread_sleep(100);
449 if (_ux_system_host -> ux_system_host_device_array[3].ux_device_handle == 0)
450 {
451
452 printf("ERROR #%d: device[3] must be used\n", __LINE__);
453 error_counter ++;
454 }
455
456 test_error_cases = UX_TRUE;
457
458 /* backup and invalidate device[3] */
459 tmp = _ux_system_host -> ux_system_host_device_array[3].ux_device_handle;
460 _ux_system_host -> ux_system_host_device_array[3].ux_device_handle = 0;
461
462 /* Disconnect. */
463 ux_test_hcd_sim_host_disconnect();
464 if (_ux_system_host -> ux_system_host_device_array[0].ux_device_handle == 0)
465 {
466
467 printf("ERROR #%d: device[0] must not be removed\n", __LINE__);
468 error_counter ++;
469 }
470 if (_ux_system_host -> ux_system_host_device_array[1].ux_device_handle == 0)
471 {
472
473 printf("ERROR #%d: device[1] must not be removed\n", __LINE__);
474 error_counter ++;
475 }
476 if (_ux_system_host -> ux_system_host_device_array[2].ux_device_handle == 0)
477 {
478
479 printf("ERROR #%d: device[2] must not be removed\n", __LINE__);
480 error_counter ++;
481 }
482 _ux_system_host -> ux_system_host_device_array[3].ux_device_handle = tmp;
483 #endif
484
485 test_error_cases = UX_FALSE;
486
487 /* Connect. */
488 ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
489 tx_thread_sleep(100);
490
491 /* Disconnect. */
492 ux_test_hcd_sim_host_disconnect();
493
494 /* Sleep for a tick to make sure everything is complete. */
495 tx_thread_sleep(1);
496
497 /* Check for errors from other threads. */
498 if (error_counter)
499 {
500
501 /* DPUMP error. */
502 printf("ERROR #14\n");
503 test_control_return(1);
504 }
505 else
506 {
507
508 /* Successful test. */
509 printf("SUCCESS!\n");
510 test_control_return(0);
511 }
512 }
513
514
tx_demo_thread_slave_simulation_entry(ULONG arg)515 static void tx_demo_thread_slave_simulation_entry(ULONG arg)
516 {
517
518 while(1)
519 {
520
521 /* Ensure the dpump class on the device is still alive. */
522 if (dpump_slave != UX_NULL)
523 {
524
525 /* Increment thread counter. */
526 thread_1_counter++;
527 }
528
529 /* Let other thread run. */
530 tx_thread_sleep(10);
531 }
532 }
533
tx_demo_instance_activate(VOID * dpump_instance)534 static VOID tx_demo_instance_activate(VOID *dpump_instance)
535 {
536
537 /* Save the DPUMP instance. */
538 dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
539 }
540
tx_demo_instance_deactivate(VOID * dpump_instance)541 static VOID tx_demo_instance_deactivate(VOID *dpump_instance)
542 {
543
544 /* Reset the DPUMP instance. */
545 dpump_slave = UX_NULL;
546 }
547
548