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_cdc_acm.h"
12 #include "ux_device_stack.h"
13 
14 #include "ux_host_class_cdc_acm.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 #include "ux_host_stack.h"
21 
22 /* Define constants.  */
23 #define                             UX_DEMO_DEBUG_SIZE  (4096*8)
24 #define                             UX_DEMO_STACK_SIZE  1024
25 #define                             UX_DEMO_BUFFER_SIZE (UX_SLAVE_REQUEST_DATA_MAX_LENGTH + 1)
26 #define                             UX_DEMO_XMIT_BUFFER_SIZE 512
27 #define                             UX_DEMO_RECEPTION_BUFFER_SIZE 512
28 #define                             UX_DEMO_FILE_BUFFER_SIZE 512
29 #define                             UX_DEMO_RECEPTION_BLOCK_SIZE 64
30 #define                             UX_DEMO_MEMORY_SIZE     (64*1024)
31 #define                             UX_DEMO_FILE_SIZE       (128 * 1024)
32 #define                             UX_RAM_DISK_MEMORY      (256 * 1024)
33 
34 #define     LSB(x) ( (x) & 0x00ff)
35 #define     MSB(x) (((x) & 0xff00) >> 8)
36 
37 /* Configuration descriptor 9 bytes */
38 #define CFG_DESC(wTotalLength, bNumInterfaces, bmAttributes, bConfigurationValue)\
39     /* Configuration 1 descriptor 9 bytes */\
40     0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
41     (bNumInterfaces), (bConfigurationValue), 0x00,\
42     (bmAttributes), 0x00,
43 #define CFG_DESC_LEN 9
44 
45 #define IAD_DESC(bIfc) \
46     /* Interface association descriptor. 8 bytes.  */\
47     0x08, 0x0b, (bIfc), 0x02, 0x02, 0x02, 0x00, 0x00,
48 #define IAD_DESC_LEN 8
49 
50 #define CDC_IFC_DESC_ALL(bIfc, bIntIn, bBulkIn, bBulkOut)\
51     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */\
52     0x09, 0x04, (bIfc), 0x00, 0x01, 0x02, 0x02, 0x01, 0x00,\
53     /* Header Functional Descriptor 5 bytes */\
54     0x05, 0x24, 0x00, 0x10, 0x01,\
55     /* ACM Functional Descriptor 4 bytes */\
56     0x04, 0x24, 0x02, 0x0f,\
57     /* Union Functional Descriptor 5 bytes */\
58     0x05, 0x24, 0x06, (bIfc), (bIfc + 1),\
59     /* Call Management Functional Descriptor 5 bytes */\
60     0x05, 0x24, 0x01, 0x03, (bIfc + 1),\
61     /* Endpoint interrupt in descriptor 7 bytes */\
62     0x07, 0x05, (bIntIn), 0x03, 0x40, 0x00, 0x10,\
63     /* Data Class Interface Descriptor Requirement 9 bytes */\
64     0x09, 0x04, (bIfc + 1), 0x00, 0x02, 0x0A, 0x00, 0x00, 0x00,\
65     /* Endpoint bulk in descriptor 7 bytes */\
66     0x07, 0x05, (bBulkIn), 0x02, 0x40, 0x00, 0x01,\
67     /* Endpoint bulk out descriptor 7 bytes */\
68     0x07, 0x05, (bBulkOut), 0x02, 0x40, 0x00, 0x01,
69 #define CDC_IFC_DESC_ALL_LEN (9+5+4+5+5+7+ 9+7+7)
70 
71 /* Define local/extern function prototypes.  */
72 static VOID                                test_thread_entry(ULONG);
73 static TX_THREAD                           tx_test_thread_host_simulation;
74 static TX_THREAD                           tx_test_thread_slave_simulation;
75 static VOID                                tx_test_thread_host_simulation_entry(ULONG);
76 static VOID                                tx_test_thread_slave_simulation_entry(ULONG);
77 static VOID                                test_cdc_instance_activate(VOID  *cdc_instance);
78 static VOID                                test_cdc_instance_deactivate(VOID *cdc_instance);
79 static VOID                                test_cdc_instance_parameter_change(VOID *cdc_instance);
80 
81 /* Define global data structures.  */
82 static UCHAR                               usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
83 static UX_HOST_CLASS                       *class_driver;
84 static UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_control;
85 static UX_HOST_CLASS_CDC_ACM               *cdc_acm_host_data;
86 
87 static UX_SLAVE_CLASS_CDC_ACM              *cdc_acm_slave;
88 static UCHAR                               cdc_acm_slave_change;
89 static UX_SLAVE_CLASS_CDC_ACM_PARAMETER    parameter;
90 
91 static ULONG                               error_counter;
92 
93 static ULONG                               error_callback_counter;
94 static UCHAR                               error_callback_ignore;
95 
96 static ULONG                               call_counter;
97 
98 static UCHAR                               buffer[UX_DEMO_BUFFER_SIZE];
99 
100 /* Define device framework.  */
101 
102 static unsigned char device_framework_full_speed[] = {
103 
104     /* Device descriptor     18 bytes
105        0x02 bDeviceClass:    CDC class code
106        0x00 bDeviceSubclass: CDC class sub code
107        0x00 bDeviceProtocol: CDC Device protocol
108 
109        idVendor & idProduct - http://www.linux-usb.org/usb.ids
110     */
111     0x12, 0x01, 0x10, 0x01,
112     0xEF, 0x02, 0x01,
113     0x08,
114     0x84, 0x84, 0x00, 0x00,
115     0x00, 0x01,
116     0x01, 0x02, 03,
117     0x01, /* bNumConfigurations */
118 
119     /* Configuration 1 descriptor 9 bytes, total 75 bytes */
120     CFG_DESC(CFG_DESC_LEN + IAD_DESC_LEN + CDC_IFC_DESC_ALL_LEN, 2, 0x40, 1)
121     /* IAD 8 bytes */
122     IAD_DESC(0)
123     /* CDC_ACM interfaces */
124     CDC_IFC_DESC_ALL(0, 0x83, 0x81, 0x02)
125 };
126 #define             DEVICE_FRAMEWORK_LENGTH_FULL_SPEED      sizeof(device_framework_full_speed)
127 
128 static unsigned char device_framework_high_speed[] = {
129 
130     /* Device descriptor     18 bytes
131        0x02 bDeviceClass:    CDC class code
132        0x00 bDeviceSubclass: CDC class sub code
133        0x00 bDeviceProtocol: CDC Device protocol
134 
135        idVendor & idProduct - http://www.linux-usb.org/usb.ids
136     */
137     0x12, 0x01, 0x00, 0x02,
138     0xEF, 0x02, 0x01,
139     0x40,
140     0x84, 0x84, 0x00, 0x00,
141     0x00, 0x01,
142     0x01, 0x02, 03,
143     0x01, /* bNumConfigurations */
144 
145     /* Device qualifier descriptor 10 bytes */
146     0x0a, 0x06, 0x00, 0x02,
147     0x02, 0x00, 0x00,
148     0x40,
149     0x01,
150     0x00,
151 
152     /* Configuration 1 descriptor 9 bytes, total 75-8=67 bytes */
153     CFG_DESC(CFG_DESC_LEN + CDC_IFC_DESC_ALL_LEN, 2, 0x60, 1)
154     CDC_IFC_DESC_ALL(0, 0x83, 0x81, 0x02)
155 };
156 #define             DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED      sizeof(device_framework_high_speed)
157 
158 static unsigned char string_framework[] = {
159 
160     /* Manufacturer string descriptor : Index 1 - "Express Logic" */
161     0x09, 0x04, 0x01, 0x0c,
162     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
163     0x6f, 0x67, 0x69, 0x63,
164 
165     /* Product string descriptor : Index 2 - "EL Composite device" */
166     0x09, 0x04, 0x02, 0x13,
167     0x45, 0x4c, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x6f,
168     0x73, 0x69, 0x74, 0x65, 0x20, 0x64, 0x65, 0x76,
169     0x69, 0x63, 0x65,
170 
171     /* Serial Number string descriptor : Index 3 - "0001" */
172     0x09, 0x04, 0x03, 0x04,
173     0x30, 0x30, 0x30, 0x31
174 };
175 #define             STRING_FRAMEWORK_LENGTH                 sizeof(string_framework)
176 
177 /* Multiple languages are supported on the device, to add
178     a language besides english, the unicode language code must
179     be appended to the language_id_framework array and the length
180     adjusted accordingly. */
181 static unsigned char language_id_framework[] = {
182 
183     /* English. */
184     0x09, 0x04
185 };
186 #define             LANGUAGE_ID_FRAMEWORK_LENGTH            sizeof(language_id_framework)
187 
188 /* Define the ISR dispatch.  */
189 
190 extern VOID    (*test_isr_dispatch)(void);
191 
192 
193 /* Prototype for test control return.  */
194 
195 void  test_control_return(UINT status);
196 
197 
198 /* Define the ISR dispatch routine.  */
199 
test_isr(void)200 static void    test_isr(void)
201 {
202 
203     /* For further expansion of interrupt-level testing.  */
204 }
205 
break_on_cdc_acm_all_ready(VOID)206 static UINT break_on_cdc_acm_all_ready(VOID)
207 {
208 UINT                                 status;
209 UINT                                 i;
210 UX_HOST_CLASS                       *class;
211 UX_HOST_CLASS_CDC_ACM               *cdc_acm;
212 
213     /* Find the main cdc_acm container */
214     status = ux_host_stack_class_get(_ux_system_host_class_cdc_acm_name, &class);
215     if (status != UX_SUCCESS)
216         /* Do not break. */
217         return 0;
218 
219     /* Find class instances. */
220     for (i = 0; i < 2; i ++)
221     {
222         status = ux_host_stack_class_instance_get(class, i, (void **) &cdc_acm);
223         if (status != UX_SUCCESS)
224             /* Do not break. */
225             return 0;
226 
227         switch(cdc_acm->ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass)
228         {
229 
230         case UX_HOST_CLASS_CDC_CONTROL_CLASS:
231             cdc_acm_host_control = cdc_acm;
232             break;
233 
234         case UX_HOST_CLASS_CDC_DATA_CLASS:
235             cdc_acm_host_data = cdc_acm;
236             break;
237 
238         default:
239             break;
240         }
241     }
242 
243     if (cdc_acm_host_control == UX_NULL || cdc_acm_host_data == UX_NULL)
244         /* Do not break. */
245         return 0;
246 
247     if (cdc_acm_host_control->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
248         /* Do not break. */
249         return 0;
250 
251     if (cdc_acm_host_data->ux_host_class_cdc_acm_state != UX_HOST_CLASS_INSTANCE_LIVE)
252         /* Do not break. */
253         return 0;
254 
255     if (_ux_system_slave->ux_system_slave_class_array[0].ux_slave_class_instance == UX_NULL)
256         /* Do not break. */
257         return 0;
258 
259     cdc_acm_slave = (UX_SLAVE_CLASS_CDC_ACM *)_ux_system_slave->ux_system_slave_class_array[0].ux_slave_class_instance;
260 
261     /* All found, break. */
262     return 1;
263 }
264 
break_on_removal(VOID)265 static UINT break_on_removal(VOID)
266 {
267 
268 UINT                     status;
269 UX_DEVICE               *device;
270 
271     status = ux_host_stack_device_get(0, &device);
272     if (status == UX_SUCCESS)
273         /* Do not break. */
274         return UX_SUCCESS;
275 
276     if (_ux_system_slave->ux_system_slave_class_array[0].ux_slave_class_instance != UX_NULL)
277         /* Do not break. */
278         return 0;
279 
280     cdc_acm_host_data = UX_NULL;
281     cdc_acm_host_control = UX_NULL;
282     cdc_acm_slave = UX_NULL;
283 
284     return 1;
285 }
286 
test_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)287 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
288 {
289 
290 UX_HOST_CLASS_CDC_ACM *cdc_acm = (UX_HOST_CLASS_CDC_ACM *) inst;
291 
292     switch(event)
293     {
294 
295         case UX_DEVICE_INSERTION:
296 
297             if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
298                 cdc_acm_host_control = cdc_acm;
299             else
300                 cdc_acm_host_data = cdc_acm;
301             break;
302 
303         case UX_DEVICE_REMOVAL:
304 
305             if (cdc_acm -> ux_host_class_cdc_acm_interface -> ux_interface_descriptor.bInterfaceClass == UX_HOST_CLASS_CDC_CONTROL_CLASS)
306                 cdc_acm_host_control = UX_NULL;
307             else
308                 cdc_acm_host_data = UX_NULL;
309             break;
310 
311         default:
312             break;
313     }
314     return 0;
315 }
316 
test_cdc_instance_activate(VOID * cdc_instance)317 static VOID    test_cdc_instance_activate(VOID *cdc_instance)
318 {
319 
320     /* Save the CDC instance.  */
321     cdc_acm_slave = (UX_SLAVE_CLASS_CDC_ACM *) cdc_instance;
322 }
test_cdc_instance_deactivate(VOID * cdc_instance)323 static VOID    test_cdc_instance_deactivate(VOID *cdc_instance)
324 {
325 
326     /* Reset the CDC instance.  */
327     cdc_acm_slave = UX_NULL;
328 }
329 
test_cdc_instance_parameter_change(VOID * cdc_instance)330 static VOID test_cdc_instance_parameter_change(VOID *cdc_instance)
331 {
332 
333     /* Set CDC parameter change flag. */
334     cdc_acm_slave_change = UX_TRUE;
335 }
336 
test_ux_error_callback(UINT system_level,UINT system_context,UINT error_code)337 static VOID test_ux_error_callback(UINT system_level, UINT system_context, UINT error_code)
338 {
339     error_callback_counter ++;
340 
341     if (!error_callback_ignore)
342     {
343         {
344             /* Failed test.  */
345             printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
346             // test_control_return(1);
347         }
348     }
349 }
350 
351 /* Define what the initial system looks like.  */
352 
353 #ifdef CTEST
test_application_define(void * first_unused_memory)354 void test_application_define(void *first_unused_memory)
355 #else
356 void usbx_ux_device_class_cdc_acm_activate_test_application_define(void *first_unused_memory)
357 #endif
358 {
359 
360 UINT                    status;
361 CHAR *                  stack_pointer;
362 CHAR *                  memory_pointer;
363 
364     printf("Running ux_device_class_cdc_acm_activate Test....................... ");
365 
366     /* Initialize the free memory pointer */
367     stack_pointer = (CHAR *) usbx_memory;
368     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
369 
370     /* Initialize USBX Memory */
371     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
372     /* Check for error.  */
373     if (status != UX_SUCCESS)
374     {
375 
376         printf("ERROR #%d\n", __LINE__);
377         test_control_return(1);
378     }
379 
380     /* Register the error callback. */
381     _ux_utility_error_callback_register(test_ux_error_callback);
382 
383     /* The code below is required for installing the host portion of USBX */
384     status =  ux_host_stack_initialize(UX_NULL);
385     if (status != UX_SUCCESS)
386     {
387 
388         printf("ERROR #%d\n", __LINE__);
389         test_control_return(1);
390     }
391 
392     /* Register CDC ACM class */
393     status =  ux_host_stack_class_register(_ux_system_host_class_cdc_acm_name, ux_host_class_cdc_acm_entry);
394     if (status != UX_SUCCESS)
395     {
396 
397         printf("ERROR #%d\n", __LINE__);
398         test_control_return(1);
399     }
400 
401     /* The code below is required for installing the device portion of USBX. No call back for
402        device status change in this example. */
403     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
404                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
405                                        string_framework, STRING_FRAMEWORK_LENGTH,
406                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
407     if(status!=UX_SUCCESS)
408     {
409 
410         printf("ERROR #%d\n", __LINE__);
411         test_control_return(1);
412     }
413 
414     /* Set the parameters for callback when insertion/extraction of a CDC device.  */
415     parameter.ux_slave_class_cdc_acm_instance_activate   =  UX_NULL;
416     parameter.ux_slave_class_cdc_acm_instance_deactivate =  UX_NULL;
417     parameter.ux_slave_class_cdc_acm_parameter_change    =  test_cdc_instance_parameter_change;
418 
419     /* Initialize the device cdc class. This class owns both interfaces starting with 0. */
420     status  =  ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
421                                              1,0,  &parameter);
422     status |= ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
423                                              2,0,  &parameter);
424     status |= ux_device_stack_class_register(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry,
425                                              3,0,  &parameter);
426 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
427     if(status!=UX_SUCCESS)
428     {
429 
430         printf("ERROR #%d\n", __LINE__);
431         test_control_return(1);
432     }
433 #endif
434     /* Initialize the simulated device controller.  */
435     status =  _ux_test_dcd_sim_slave_initialize();
436 
437     /* Check for error.  */
438     if (status != TX_SUCCESS)
439     {
440 
441         printf("ERROR #%d\n", __LINE__);
442         test_control_return(1);
443     }
444 
445     /* Register HCD for test */
446     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
447     if (status != UX_SUCCESS)
448     {
449 
450         printf("ERROR #%d\n", __LINE__);
451         test_control_return(1);
452     }
453 
454     /* Create the main host simulation thread.  */
455     status =  tx_thread_create(&tx_test_thread_host_simulation, "tx test host simulation", tx_test_thread_host_simulation_entry, 0,
456             stack_pointer, UX_DEMO_STACK_SIZE,
457             20, 20, 1, TX_AUTO_START);
458 
459     /* Check for error.  */
460     if (status != TX_SUCCESS)
461     {
462 
463         printf("ERROR #%d\n", __LINE__);
464         test_control_return(1);
465     }
466 
467     /* Create the main slave simulation  thread.  */
468     status =  tx_thread_create(&tx_test_thread_slave_simulation, "tx test slave simulation", tx_test_thread_slave_simulation_entry, 0,
469             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
470             20, 20, 1, TX_AUTO_START);
471 
472     /* Check for error.  */
473     if (status != TX_SUCCESS)
474     {
475 
476         printf("ERROR #%d\n", __LINE__);
477         test_control_return(1);
478     }
479 }
480 
tx_test_thread_host_simulation_entry(ULONG arg)481 void  tx_test_thread_host_simulation_entry(ULONG arg)
482 {
483 
484 UX_SLAVE_ENDPOINT                                   *slave_interrupt_endpoint = UX_NULL;
485 
486     stepinfo("\n");
487 
488     /* Test connect. */
489     stepinfo(">>>>>>>>>>>>>>>> Test connect (FS)\n");
490     ux_test_hcd_sim_host_connect(UX_FULL_SPEED_DEVICE);
491     ux_test_breakable_sleep(100, break_on_cdc_acm_all_ready);
492     if (!(cdc_acm_host_control && cdc_acm_host_data && cdc_acm_slave))
493     {
494 
495         printf("ERROR #%d: connect fail\n", __LINE__);
496         test_control_return(1);
497     }
498 
499     stepinfo(">>>>>>>>>>>>>>>> Deinitialize\n");
500 
501     /* Deinitialize the class.  */
502     ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
503     ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
504     ux_device_stack_class_unregister(_ux_system_slave_class_cdc_acm_name, ux_device_class_cdc_acm_entry);
505 
506     /* Deinitialize the device side of usbx.  */
507     _ux_device_stack_uninitialize();
508 
509     /* And finally the usbx system resources.  */
510     _ux_system_uninitialize();
511 
512     stepinfo(">>>>>>>>>>>>>>>> Dump results\n");
513 
514     if (error_counter > 0)
515     {
516 
517         /* Test error.  */
518         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
519         test_control_return(1);
520     }
521 
522     /* Successful test.  */
523     printf("SUCCESS!\n");
524     test_control_return(0);
525 }
526 
tx_test_thread_slave_simulation_entry(ULONG arg)527 void  tx_test_thread_slave_simulation_entry(ULONG arg)
528 {
529 
530     while(1)
531     {
532 
533         /* Sleep so ThreadX on Win32 will delete this thread. */
534         ux_utility_delay_ms(100);
535     }
536 }
537