1 /* This test is designed to test the ux_host_stack_device_configuration_reset/select.  */
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_dpump.h"
9 #include "ux_device_class_dpump.h"
10 
11 
12 /* Define USBX test constants.  */
13 
14 #define UX_TEST_STACK_SIZE      4096
15 #define UX_TEST_BUFFER_SIZE     2048
16 #define UX_TEST_RUN             1
17 #define UX_TEST_MEMORY_SIZE     (64*1024)
18 
19 
20 /* Define the counters used in the test application...  */
21 
22 static ULONG                           thread_0_counter;
23 static ULONG                           thread_1_counter;
24 static ULONG                           error_counter;
25 
26 
27 /* Define USBX test global variables.  */
28 
29 static unsigned char                   host_out_buffer[UX_HOST_CLASS_DPUMP_PACKET_SIZE];
30 static unsigned char                   host_in_buffer[UX_HOST_CLASS_DPUMP_PACKET_SIZE];
31 static unsigned char                   slave_buffer[UX_HOST_CLASS_DPUMP_PACKET_SIZE];
32 
33 static UX_HOST_CLASS                   *class_driver;
34 static UX_HOST_CLASS_DPUMP             *dpump;
35 static UX_SLAVE_CLASS_DPUMP            *dpump_slave;
36 
37 
38 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 114
39 static UCHAR device_framework_full_speed[] = {
40 
41     /* Device descriptor */
42         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
43         0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
44         0x00, 0x03,
45 
46     /* Configuration descriptor */
47         0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80,
48         0x32,
49 
50     /* Interface descriptor */
51         0x09, 0x04, 0x00, 0x00, 0x02, 0x99, 0x99, 0x99,
52         0x00,
53 
54     /* Endpoint descriptor (Bulk Out) */
55         0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00,
56 
57     /* Endpoint descriptor (Bulk In) */
58         0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00,
59 
60     /* Configuration descriptor */
61         0x09, 0x02, 0x20, 0x00, 0x01, 0x02, 0x00, 0x80,
62         0xFA,
63 
64     /* Interface descriptor */
65         0x09, 0x04, 0x00, 0x00, 0x02, 0x99, 0x99, 0x99,
66         0x00,
67 
68     /* Endpoint descriptor (Bulk Out) */
69         0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00,
70 
71     /* Endpoint descriptor (Bulk In) */
72         0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00,
73 
74     /* Configuration descriptor */
75         0x09, 0x02, 0x20, 0x00, 0x01, 0x03, 0x00, 0xC0,
76         0xFA,
77 
78     /* Interface descriptor */
79         0x09, 0x04, 0x00, 0x00, 0x02, 0x99, 0x99, 0x99,
80         0x00,
81 
82     /* Endpoint descriptor (Bulk Out) */
83         0x07, 0x05, 0x01, 0x02, 0x40, 0x00, 0x00,
84 
85     /* Endpoint descriptor (Bulk In) */
86         0x07, 0x05, 0x82, 0x02, 0x40, 0x00, 0x00,
87 
88 };
89 
90 
91 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 60
92 static UCHAR device_framework_high_speed[] = {
93 
94     /* Device descriptor */
95         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
96         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
97         0x03, 0x01,
98 
99     /* Device qualifier descriptor */
100         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
101         0x01, 0x00,
102 
103     /* Configuration descriptor */
104         0x09, 0x02, 0x20, 0x00, 0x01, 0x01, 0x00, 0x80,
105         0x32,
106 
107     /* Interface descriptor */
108         0x09, 0x04, 0x00, 0x00, 0x02, 0x99, 0x99, 0x99,
109         0x00,
110 
111     /* Endpoint descriptor (Bulk Out) */
112         0x07, 0x05, 0x01, 0x02, 0x00, 0x02, 0x00,
113 
114     /* Endpoint descriptor (Bulk In) */
115         0x07, 0x05, 0x82, 0x02, 0x00, 0x02, 0x00
116     };
117 
118     /* String Device Framework :
119      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
120      Byte 2       : Byte containing the index of the descriptor
121      Byte 3       : Byte containing the length of the descriptor string
122     */
123 
124 #define STRING_FRAMEWORK_LENGTH 38
125 static UCHAR string_framework[] = {
126 
127     /* Manufacturer string descriptor : Index 1 */
128         0x09, 0x04, 0x01, 0x0c,
129         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
130         0x6f, 0x67, 0x69, 0x63,
131 
132     /* Product string descriptor : Index 2 */
133         0x09, 0x04, 0x02, 0x0c,
134         0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
135         0x44, 0x65, 0x6d, 0x6f,
136 
137     /* Serial Number string descriptor : Index 3 */
138         0x09, 0x04, 0x03, 0x04,
139         0x30, 0x30, 0x30, 0x31
140     };
141 
142 
143     /* Multiple languages are supported on the device, to add
144        a language besides English, the unicode language code must
145        be appended to the language_id_framework array and the length
146        adjusted accordingly. */
147 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
148 static UCHAR language_id_framework[] = {
149 
150     /* English. */
151         0x09, 0x04
152     };
153 
154 
155 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
156 
157 static VOID                ux_test_instance_activate(VOID  *dpump_instance);
158 static VOID                ux_test_instance_deactivate(VOID *dpump_instance);
159 
160 UINT                       _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
161 UINT                       _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
162                                     ULONG requested_length, ULONG *actual_length);
163 UINT                       _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
164                                     ULONG requested_length, ULONG *actual_length);
165 
166 static TX_THREAD           ux_test_thread_host_simulation;
167 static TX_THREAD           ux_test_thread_slave_simulation;
168 static void                ux_test_thread_host_simulation_entry(ULONG);
169 static void                ux_test_thread_slave_simulation_entry(ULONG);
170 
171 
172 /* Define the ISR dispatch.  */
173 
174 extern VOID    (*test_isr_dispatch)(void);
175 
176 
177 /* Prototype for test control return.  */
178 
179 void  test_control_return(UINT status);
180 
181 
182 /* Define the ISR dispatch routine.  */
183 
test_isr(void)184 static void    test_isr(void)
185 {
186 
187     /* For further expansion of interrupt-level testing.  */
188 }
189 
190 
error_callback(UINT system_level,UINT system_context,UINT error_code)191 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
192 {
193     if ((error_code != UX_CONFIGURATION_HANDLE_UNKNOWN) &&
194         (error_code != UX_OVER_CURRENT_CONDITION))
195     {
196         /* Failed test.  */
197         printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
198         test_control_return(1);
199     }
200 }
201 
202 /* Define what the initial system looks like.  */
203 
204 #ifdef CTEST
test_application_define(void * first_unused_memory)205 void test_application_define(void *first_unused_memory)
206 #else
207 void    usbx_ux_host_stack_device_configuration_reset_select_test_application_define(void *first_unused_memory)
208 #endif
209 {
210 
211 UINT status;
212 CHAR                            *stack_pointer;
213 CHAR                            *memory_pointer;
214 UX_SLAVE_CLASS_DPUMP_PARAMETER  parameter;
215 
216 
217     /* Inform user.  */
218     printf("Running ux_host_stack_device_configuration_reset/select Test........ ");
219 
220     /* Initialize the free memory pointer.  */
221     stack_pointer = (CHAR *) first_unused_memory;
222     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
223 
224     /* Initialize USBX Memory.  */
225     status =  ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
226 
227     /* Check for error.  */
228     if (status != UX_SUCCESS)
229     {
230 
231         printf("ERROR #1\n");
232         test_control_return(1);
233     }
234 
235     /* Register the error callback. */
236     _ux_utility_error_callback_register(error_callback);
237 
238     /* The code below is required for installing the host portion of USBX.  */
239     status =  ux_host_stack_initialize(UX_NULL);
240 
241     /* Check for error.  */
242     if (status != UX_SUCCESS)
243     {
244 
245         printf("ERROR #2\n");
246         test_control_return(1);
247     }
248 
249     /* Register all the host class drivers for this USBX implementation.  */
250     status =  ux_host_stack_class_register(_ux_system_host_class_dpump_name, ux_host_class_dpump_entry);
251 
252     /* Check for error.  */
253     if (status != UX_SUCCESS)
254     {
255 
256         printf("ERROR #3\n");
257         test_control_return(1);
258     }
259 
260     /* The code below is required for installing the device portion of USBX */
261     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
262                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
263                                        string_framework, STRING_FRAMEWORK_LENGTH,
264                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
265 
266     /* Check for error.  */
267     if (status != UX_SUCCESS)
268     {
269 
270         printf("ERROR #5\n");
271         test_control_return(1);
272     }
273 
274     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
275     parameter.ux_slave_class_dpump_instance_activate   =  ux_test_instance_activate;
276     parameter.ux_slave_class_dpump_instance_deactivate =  ux_test_instance_deactivate;
277 
278     /* Initialize the device dpump class. The class is connected with interface 0 */
279     status =  ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
280                                                1, 0, &parameter);
281 
282     /* Check for error.  */
283     if (status != UX_SUCCESS)
284     {
285 
286         printf("ERROR #6\n");
287         test_control_return(1);
288     }
289 
290     /* Initialize the simulated device controller.  */
291     status =  _ux_dcd_sim_slave_initialize();
292 
293     /* Check for error.  */
294     if (status != UX_SUCCESS)
295     {
296 
297         printf("ERROR #7\n");
298         test_control_return(1);
299     }
300 
301     /* Register all the USB host controllers available in this system */
302     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
303 
304     /* Check for error.  */
305     if (status != UX_SUCCESS)
306     {
307 
308         printf("ERROR #4\n");
309         test_control_return(1);
310     }
311 
312     /* Create the main host simulation thread.  */
313     status =  tx_thread_create(&ux_test_thread_host_simulation, "test host simulation", ux_test_thread_host_simulation_entry, 0,
314             stack_pointer, UX_TEST_STACK_SIZE,
315             20, 20, 1, TX_AUTO_START);
316 
317     /* Check for error.  */
318     if (status != TX_SUCCESS)
319     {
320 
321         printf("ERROR #8\n");
322         test_control_return(1);
323     }
324 }
325 
326 
ux_test_thread_host_simulation_entry(ULONG arg)327 static void  ux_test_thread_host_simulation_entry(ULONG arg)
328 {
329 
330 UINT                        status;
331 UX_HOST_CLASS               *class;
332 static UX_CONFIGURATION     test_configuration;
333 UX_CONFIGURATION            *configuration1;
334 UX_CONFIGURATION            *configuration2;
335 UX_CONFIGURATION            *configuration3;
336 UX_DEVICE                   *device;
337 
338 
339     /* Find the main data pump container.  */
340     status =  ux_host_stack_class_get(_ux_system_host_class_dpump_name, &class);
341 
342     /* Check for error.  */
343     if (status != UX_SUCCESS)
344     {
345 
346         /* DPUMP basic test error.  */
347         printf("ERROR #10\n");
348         test_control_return(1);
349     }
350 
351     /* We get the first instance of the data pump device.  */
352     do
353     {
354 
355         status =  ux_host_stack_class_instance_get(class, 0, (VOID **) &dpump);
356         tx_thread_relinquish();
357     } while (status != UX_SUCCESS);
358 
359     /* We still need to wait for the data pump status to be live.  */
360     while (dpump -> ux_host_class_dpump_state != UX_HOST_CLASS_INSTANCE_LIVE)
361     {
362 
363         tx_thread_relinquish();
364     }
365 
366     /* At this point, the data pump class has been found.  Now call _ux_host_stack_device_configuration_select
367        with invalid configuration.  */
368     status = ux_host_stack_device_configuration_select(&test_configuration);
369     if (status != UX_CONFIGURATION_HANDLE_UNKNOWN)
370     {
371 
372         /* ux_host_stack_device_get test error.  */
373         error_counter++;
374     }
375 
376     device = dpump -> ux_host_class_dpump_device;
377     configuration1 = dpump -> ux_host_class_dpump_interface -> ux_interface_configuration;
378     configuration2 = configuration1 -> ux_configuration_next_configuration;
379     configuration3 = configuration2 -> ux_configuration_next_configuration;
380 
381     /* Select configuration 2.  */
382     status = ux_host_stack_device_configuration_select(configuration2);
383     if (status != UX_SUCCESS)
384     {
385 
386         /* Test error.  */
387         error_counter++;
388     }
389 
390     /* Select configuration 1.  */
391     status = ux_host_stack_device_configuration_select(configuration1);
392     if (status != UX_SUCCESS)
393     {
394 
395         /* Test error.  */
396         error_counter++;
397     }
398 #if UX_MAX_DEVICES > 1
399     /* Set max power to test over current condition.  */
400     device -> ux_device_max_power = 50;
401 
402     /* Select configuration 2 again.  */
403     status = ux_host_stack_device_configuration_select(configuration2);
404     if (status != UX_OVER_CURRENT_CONDITION)
405     {
406 
407         /* Test error.  */
408         error_counter++;
409     }
410 
411     /* Select configuration 3.  */
412     status = ux_host_stack_device_configuration_select(configuration3);
413     if (status != UX_SUCCESS)
414     {
415 
416         /* Test error.  */
417         error_counter++;
418     }
419 
420     /* Set max power back.  */
421     device -> ux_device_max_power = 250;
422 
423     /* Select configuration 2 for configuration_reset test.  */
424     status = ux_host_stack_device_configuration_select(configuration2);
425     if (status != UX_SUCCESS)
426     {
427 
428         /* Test error.  */
429         error_counter++;
430     }
431 #endif
432     /* Reset configuration.  */
433     status = ux_host_stack_device_configuration_reset(device);
434     if (status != UX_SUCCESS)
435     {
436 printf("%s:%d\n", __FILE__, __LINE__);
437         /* Test error.  */
438         error_counter++;
439     }
440 
441     /* Reset configuration again.  */
442     status = ux_host_stack_device_configuration_reset(device);
443     if (status != UX_SUCCESS)
444     {
445 printf("%s:%d\n", __FILE__, __LINE__);
446         /* Test error.  */
447         error_counter++;
448     }
449 
450     /* Sleep for a tick to make sure everything is complete.  */
451     tx_thread_sleep(1);
452 
453     /* Check for errors from other threads.  */
454     if (error_counter)
455     {
456 
457         /* Test error.  */
458         printf("ERROR #14\n");
459         test_control_return(1);
460     }
461     else
462     {
463 
464         /* Successful test.  */
465         printf("SUCCESS!\n");
466         test_control_return(0);
467     }
468 }
469 
ux_test_instance_activate(VOID * dpump_instance)470 static VOID  ux_test_instance_activate(VOID *dpump_instance)
471 {
472 
473     /* Save the DPUMP instance.  */
474     dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
475 }
476 
ux_test_instance_deactivate(VOID * dpump_instance)477 static VOID  ux_test_instance_deactivate(VOID *dpump_instance)
478 {
479 
480     /* Reset the DPUMP instance.  */
481     dpump_slave = UX_NULL;
482 }
483 
484