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 #include "ux_host_class_dummy.h"
9 #include "ux_device_class_dummy.h"
10 #include "ux_test.h"
11 
12 
13 /* Define USBX demo constants.  */
14 
15 #define UX_DEMO_STACK_SIZE      4096
16 #define UX_DEMO_BUFFER_SIZE     2048
17 #define UX_DEMO_RUN             1
18 #define UX_DEMO_MEMORY_SIZE     (64*1024)
19 
20 
21 /* Define the counters used in the demo application...  */
22 
23 static ULONG                           thread_0_counter;
24 static ULONG                           thread_1_counter;
25 static ULONG                           error_counter;
26 
27 
28 /* Define USBX demo global variables.  */
29 
30 static UX_HOST_CLASS                   *class_driver;
31 static UX_HOST_CLASS_DUMMY             *dummy;
32 static UX_TRANSFER                     *dummy_transfer;
33 static UX_DEVICE_CLASS_DUMMY           *dummy_slave;
34 
35 static UINT                             expected_error;
36 
37 #define _W0(w)      ( (w)       & 0xFF)
38 #define _W1(w)      (((w) >> 8) & 0xFF)
39 
40 #define _DEVICE_DESCRIPTOR(cls, sub, protocol, pktsize, vid, pid, n_cfg)        \
41     0x12, 0x01, 0x10, 0x01,                                                     \
42     (cls), (sub), (protocol), (pktsize),                                        \
43     _W0(vid), _W1(vid), _W0(pid), _W1(pid),                                     \
44     0x00, 0x00, 0x00, 0x00, 0x00, (n_cfg),
45 
46 #define _QUALIFIER_DESCRIPTOR(cls, sub, protocol, n_cfg)                        \
47     0x0a, 0x06, 0x00, 0x02,                                                     \
48     (cls), (sub), (protocol), 0x40, (n_cfg), 0x00,
49 
50 #define _CONFIGURATION_DESCRIPTOR(total_len, n_ifc, cfg_val)                    \
51     0x09, 0x02, _W0(total_len), _W1(total_len), (n_ifc), (cfg_val),             \
52     0x00, 0xc0, 0x32,
53 
54 #define _INTERFACE_DESCRIPTOR(ifc_n, alt, n_ep, cls, sub, protocol)             \
55     0x09, 0x04, (ifc_n), (alt), (n_ep), (cls), (sub), (protocol), 0x00,
56 
57 #define _ENDPOINT_DESCRIPTOR(addr, attr, pktsize, interval)                     \
58     0x07, 0x05, (addr), (attr), _W0(pktsize), _W1(pktsize), (interval),
59 
60 #define _CONFIGURATION_TOTAL_LENGTH (9+9+2*7)
61 #define _CONFIGURATION_DESCRIPTORS(hs)                                          \
62     _CONFIGURATION_DESCRIPTOR(_CONFIGURATION_TOTAL_LENGTH, 1, 1)                \
63     _INTERFACE_DESCRIPTOR(0, 0, 2, 0x99, 0x99, 0x99)                            \
64     _ENDPOINT_DESCRIPTOR(0x01, 0x02, (hs) ? 512 : 64, 0x00)                     \
65     _ENDPOINT_DESCRIPTOR(0x82, 0x02, (hs) ? 512 : 64, 0x00)
66 
67 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
68 static UCHAR device_framework_full_speed[] = {
69     _DEVICE_DESCRIPTOR(0x00, 0x00, 0x00, 8, 0x08EC, 0x0001, 1)
70     _CONFIGURATION_DESCRIPTORS(0)
71 };
72 
73 
74 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
75 static UCHAR device_framework_high_speed[] = {
76     _DEVICE_DESCRIPTOR(0x00, 0x00, 0x00, 64, 0x08EC, 0x0001, 1)
77     _QUALIFIER_DESCRIPTOR(0, 0, 0, 1)
78     _CONFIGURATION_DESCRIPTORS(1)
79 };
80 
81 /* String Device Framework :
82     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
83     Byte 2       : Byte containing the index of the descriptor
84     Byte 3       : Byte containing the length of the descriptor string
85 */
86 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
87 static UCHAR string_framework[] = {
88 
89     /* Manufacturer string descriptor : Index 1 */
90     0x09, 0x04, 0x01, 0x0c,
91     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
92     0x6f, 0x67, 0x69, 0x63,
93 
94     /* Product string descriptor : Index 2 */
95     0x09, 0x04, 0x02, 0x0c,
96     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
97     0x44, 0x65, 0x6d, 0x6f,
98 
99     /* Serial Number string descriptor : Index 3 */
100     0x09, 0x04, 0x03, 0x04,
101     0x30, 0x30, 0x30, 0x31
102 };
103 
104 
105 /* Multiple languages are supported on the device, to add
106     a language besides English, the unicode language code must
107     be appended to the language_id_framework array and the length
108     adjusted accordingly. */
109 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
110 static UCHAR language_id_framework[] = {
111 
112     /* English. */
113     0x09, 0x04
114 };
115 
116 
117 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
118 
119 static VOID                tx_demo_instance_activate(VOID  *dummy_instance);
120 static VOID                tx_demo_instance_deactivate(VOID *dummy_instance);
121 static VOID                tx_demo_instance_change(UX_DEVICE_CLASS_DUMMY *dummy_instance);
122 
123 #if defined(UX_HOST_STANDALONE)
124 static UINT                tx_demo_host_change_function(ULONG e, UX_HOST_CLASS *c, VOID *p);
125 #else
126 #define                     tx_demo_host_change_function UX_NULL
127 #endif
128 
129 UINT                       ux_hcd_sim_initialize(UX_HCD *hcd);
130 
131 static TX_THREAD           tx_demo_thread_host_simulation;
132 static TX_THREAD           tx_demo_thread_slave_simulation;
133 static void                tx_demo_thread_host_simulation_entry(ULONG);
134 static void                tx_demo_thread_slave_simulation_entry(ULONG);
135 
136 static TX_THREAD           test_thread;
137 static void                test_thread_entry(ULONG);
138 static TX_SEMAPHORE        test_start_sem;
139 static TX_SEMAPHORE        test_end_sem;
140 
141 
142 /* Define the ISR dispatch.  */
143 
144 extern VOID    (*test_isr_dispatch)(void);
145 
146 
147 /* Prototype for test control return.  */
148 
149 void  test_control_return(UINT status);
150 
151 
152 /* Define the ISR dispatch routine.  */
153 
test_isr(void)154 static void    test_isr(void)
155 {
156 
157     /* For further expansion of interrupt-level testing.  */
158 }
159 
160 
error_callback(UINT system_level,UINT system_context,UINT error_code)161 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
162 {
163     if (expected_error == 0 || error_code != expected_error)
164     {
165         /* Failed test.  */
166         printf("Error on line %d, system_level: %d, system_context: %d, error code: %x\n", __LINE__, system_level, system_context, error_code);
167         // test_control_return(1);
168     }
169 }
170 
171 /* Define what the initial system looks like.  */
172 
173 #ifdef CTEST
test_application_define(void * first_unused_memory)174 void test_application_define(void *first_unused_memory)
175 #else
176 void usbx_ux_host_stack_transfer_request_abort_test_application_define(void *first_unused_memory)
177 #endif
178 {
179 
180 UINT status;
181 CHAR                            *stack_pointer;
182 CHAR                            *memory_pointer;
183 UX_DEVICE_CLASS_DUMMY_PARAMETER  parameter;
184 
185 
186     printf("Running ux_host_stack_transfer_request_abort Test................... ");
187 
188     /* Initialize the free memory pointer.  */
189     stack_pointer = (CHAR *) first_unused_memory;
190     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 3);
191 
192     /* Initialize USBX Memory.  */
193     status =  ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL, 0);
194 
195     /* Check for error.  */
196     if (status != UX_SUCCESS)
197     {
198 
199         printf("ERROR #%d\n", __LINE__);
200         test_control_return(1);
201     }
202 
203     /* Register the error callback. */
204     _ux_utility_error_callback_register(error_callback);
205 
206     /* The code below is required for installing the host portion of USBX.  */
207     status =  ux_host_stack_initialize(tx_demo_host_change_function);
208 
209     /* Check for error.  */
210     if (status != UX_SUCCESS)
211     {
212 
213         printf("ERROR #%d\n", __LINE__);
214         test_control_return(1);
215     }
216 
217     /* Register all the host class drivers for this USBX implementation.  */
218     status =  ux_host_stack_class_register(_ux_host_class_dummy_name, _ux_host_class_dummy_entry);
219 
220     /* Check for error.  */
221     if (status != UX_SUCCESS)
222     {
223 
224         printf("ERROR #%d\n", __LINE__);
225         test_control_return(1);
226     }
227 
228     /* The code below is required for installing the device portion of USBX */
229     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
230                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
231                                        string_framework, STRING_FRAMEWORK_LENGTH,
232                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
233 
234     /* Check for error.  */
235     if (status != UX_SUCCESS)
236     {
237 
238         printf("ERROR #%d\n", __LINE__);
239         test_control_return(1);
240     }
241 
242     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
243     _ux_utility_memory_set((void *)&parameter, 0x00, sizeof(parameter));
244     parameter.ux_device_class_dummy_parameter_callbacks.ux_device_class_dummy_instance_activate   =  tx_demo_instance_activate;
245     parameter.ux_device_class_dummy_parameter_callbacks.ux_device_class_dummy_instance_deactivate =  tx_demo_instance_deactivate;
246     parameter.ux_device_class_dummy_parameter_callbacks.ux_device_class_dummy_change              =  tx_demo_instance_change;
247 
248     /* Initialize the device dpump class. The class is connected with interface 0 */
249     status =  ux_device_stack_class_register(_ux_device_class_dummy_name, _ux_device_class_dummy_entry,
250                                               1, 0, &parameter);
251 
252     /* Check for error.  */
253     if (status != UX_SUCCESS)
254     {
255 
256         printf("ERROR #%d\n", __LINE__);
257         test_control_return(1);
258     }
259 
260     /* Initialize the simulated device controller.  */
261     status =  _ux_dcd_sim_slave_initialize();
262 
263     /* Check for error.  */
264     if (status != UX_SUCCESS)
265     {
266 
267         printf("ERROR #%d\n", __LINE__);
268         test_control_return(1);
269     }
270 
271     /* Register all the USB host controllers available in this system */
272     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
273 
274     /* Check for error.  */
275     if (status != UX_SUCCESS)
276     {
277 
278         printf("ERROR #%d\n", __LINE__);
279         test_control_return(1);
280     }
281 
282     /* Create the main host simulation thread.  */
283     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
284             stack_pointer, UX_DEMO_STACK_SIZE,
285             20, 20, 1, TX_AUTO_START);
286 
287     /* Check for error.  */
288     if (status != TX_SUCCESS)
289     {
290 
291         printf("ERROR #%d\n", __LINE__);
292         test_control_return(1);
293     }
294 
295     /* Create the main demo thread.  */
296     status =  tx_thread_create(&tx_demo_thread_slave_simulation, "tx demo slave simulation", tx_demo_thread_slave_simulation_entry, 0,
297             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
298             20, 20, 1, TX_AUTO_START);
299 
300     /* Check for error.  */
301     if (status != TX_SUCCESS)
302     {
303 
304         printf("ERROR #%d\n", __LINE__);
305         test_control_return(1);
306     }
307 
308     /* Create the test thread.  */
309     status =  tx_thread_create(&test_thread, "test thread", test_thread_entry, 0,
310             stack_pointer + UX_DEMO_STACK_SIZE * 2, UX_DEMO_STACK_SIZE,
311             20, 20, 1, TX_AUTO_START);
312 
313     /* Check for error.  */
314     if (status != TX_SUCCESS)
315     {
316 
317         printf("ERROR #%d\n", __LINE__);
318         test_control_return(1);
319     }
320 }
321 
ux_demo_dummy_instance_check(VOID)322 static UINT ux_demo_dummy_instance_check(VOID)
323 {
324 UINT            status;
325 UX_HOST_CLASS   *cls;
326     status = ux_host_stack_class_get(_ux_host_class_dummy_name, &cls);
327     if (status != UX_SUCCESS)
328         return(status);
329     status =  ux_host_stack_class_instance_get(cls, 0, (VOID **) &dummy);
330     if (status != UX_SUCCESS)
331         return(status);
332     if (dummy -> ux_host_class_dummy_state != UX_HOST_CLASS_INSTANCE_LIVE)
333         return(UX_NO_CLASS_MATCH);
334     return(UX_SUCCESS);
335 }
336 
ux_demo_dummy_instance_connect_wait(ULONG wait_ticks)337 static UINT ux_demo_dummy_instance_connect_wait(ULONG wait_ticks)
338 {
339 ULONG   t0 = tx_time_get(), t1;
340     while(1)
341     {
342 #if defined(UX_HOST_STANDALONE)
343         ux_system_tasks_run();
344 #endif
345         if (UX_SUCCESS == ux_demo_dummy_instance_check())
346             return(UX_SUCCESS);
347         tx_thread_relinquish();
348 
349         /* Wait forever.  */
350         if (wait_ticks == 0xFFFFFFFFul)
351             continue;
352 
353         /* No wait.  */
354         if (wait_ticks == 0)
355             break;
356 
357         /* Check timeout.  */
358         t1 = tx_time_get();
359         if (t1 >= t0)
360             t1 = t1 - t0;
361         else
362             t1 = 0xFFFFFFFFul - t0 + t1;
363         if (t1 > wait_ticks)
364             break;
365     }
366     return(UX_ERROR);
367 }
368 
tx_demo_thread_host_simulation_entry(ULONG arg)369 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
370 {
371 UINT                status;
372 UX_DEVICE           *device;
373 UX_CONFIGURATION    *configuration;
374 UX_INTERFACE        *interface;
375 UCHAR               buffer[64];
376 
377     stepinfo(">>>> Dummy Class Connection Wait\n");
378     status = ux_demo_dummy_instance_connect_wait(1000);
379     if (status != UX_SUCCESS)
380     {
381         printf("ERROR #%d\n", __LINE__);
382         test_control_return(1);
383     }
384 
385     stepinfo(">>>> Dummy Class Transfer Abort Test\n");
386     dummy_transfer = _ux_host_class_dummy_get_transfer_request(dummy, 0x82, 0);
387     UX_TEST_ASSERT(dummy_transfer != UX_NULL);
388     dummy_transfer -> ux_transfer_request_requested_length = 64;
389     dummy_transfer -> ux_transfer_request_data_pointer = buffer;
390 
391     tx_semaphore_put(&test_start_sem);
392 
393     status = tx_semaphore_get(&test_end_sem, 2);
394     UX_TEST_ASSERT(status != TX_SUCCESS);
395 
396     status = ux_host_stack_transfer_request_abort(dummy_transfer);
397     UX_TEST_CHECK_SUCCESS(status);
398 
399     status = tx_semaphore_get(&test_end_sem, 100);
400     UX_TEST_ASSERT(status == TX_SUCCESS);
401 
402     status = ux_host_stack_transfer_request_abort(dummy_transfer);
403     UX_TEST_CHECK_SUCCESS(status);
404 
405     expected_error = 0;
406 
407     /* Sleep for a tick to make sure everything is complete.  */
408     tx_thread_sleep(1);
409 
410     /* Check for errors from other threads.  */
411     if (error_counter)
412     {
413 
414         /* DPUMP error.  */
415         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
416         test_control_return(1);
417     }
418     else
419     {
420 
421         /* Successful test.  */
422         printf("SUCCESS!\n");
423         test_control_return(0);
424     }
425 }
426 
427 
tx_demo_thread_slave_simulation_entry(ULONG arg)428 static void  tx_demo_thread_slave_simulation_entry(ULONG arg)
429 {
430 
431 UINT    status;
432 ULONG   actual_length;
433 
434 
435     while(1)
436     {
437 #if defined(UX_DEVICE_STANDALONE)
438 
439         /* Run device tasks.  */
440         ux_system_tasks_run();
441 #endif
442         /* Increment thread counter.  */
443         thread_1_counter++;
444 
445         /* Relinquish to other thread.  */
446         tx_thread_relinquish();
447     }
448 }
449 
test_thread_entry(ULONG arg)450 static void test_thread_entry(ULONG arg)
451 {
452 UINT        status;
453 
454     tx_semaphore_create(&test_start_sem, "Test Start SEM", 0);
455     tx_semaphore_create(&test_end_sem, "Test End SEM", 0);
456     while(1)
457     {
458         tx_semaphore_get(&test_start_sem, TX_WAIT_FOREVER);
459         status = ux_host_stack_transfer_request(dummy_transfer);
460         UX_TEST_CHECK_SUCCESS(status);
461         _ux_host_semaphore_get(&dummy_transfer->ux_transfer_request_semaphore, UX_WAIT_FOREVER);
462         tx_semaphore_put(&test_end_sem);
463     }
464 }
465 
tx_demo_instance_activate(VOID * inst)466 static VOID  tx_demo_instance_activate(VOID *inst)
467 {
468     dummy_slave = (UX_DEVICE_CLASS_DUMMY *)inst;
469 }
470 
tx_demo_instance_deactivate(VOID * inst)471 static VOID  tx_demo_instance_deactivate(VOID *inst)
472 {
473     dummy_slave = UX_NULL;
474 }
475 
tx_demo_instance_change(UX_DEVICE_CLASS_DUMMY * dummy)476 static VOID  tx_demo_instance_change(UX_DEVICE_CLASS_DUMMY *dummy)
477 {
478     UX_PARAMETER_NOT_USED(dummy);
479 }
480 
481 #if defined(UX_HOST_STANDALONE)
tx_demo_host_change_function(ULONG e,UX_HOST_CLASS * c,VOID * p)482 static UINT  tx_demo_host_change_function(ULONG e, UX_HOST_CLASS *c, VOID *p)
483 {
484     if (e == UX_STANDALONE_WAIT_BACKGROUND_TASK)
485     {
486         tx_thread_relinquish();
487     }
488 }
489 #endif
490