1 /* This test is designed to test the ux_host_stack_device_descriptor_read.  */
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_stack.h"
10 #include "ux_device_stack.h"
11 
12 #include "ux_device_class_cdc_acm.h"
13 #include "ux_host_class_cdc_acm.h"
14 
15 #include "ux_host_class_dpump.h"
16 #include "ux_device_class_dpump.h"
17 
18 #include "ux_host_class_hid.h"
19 #include "ux_device_class_hid.h"
20 
21 #include "ux_host_class_storage.h"
22 #include "ux_device_class_storage.h"
23 
24 #include "ux_test_dcd_sim_slave.h"
25 #include "ux_test_hcd_sim_host.h"
26 #include "ux_test_utility_sim.h"
27 
28 
29 /* Define USBX test constants.  */
30 
31 #define UX_TEST_STACK_SIZE      4096
32 #define UX_TEST_BUFFER_SIZE     2048
33 #define UX_TEST_RUN             1
34 #define UX_TEST_MEMORY_SIZE     (64*1024)
35 
36 /* HID mouse related descriptors */
37 
38 static UCHAR hid_mouse_report[] = {
39 
40     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
41     0x09, 0x02,                    // USAGE (Mouse)
42     0xa1, 0x01,                    // COLLECTION (Application)
43     0x09, 0x01,                    //   USAGE (Pointer)
44     0xa1, 0x00,                    //   COLLECTION (Physical)
45     0x05, 0x09,                    //     USAGE_PAGE (Button)
46     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
47     0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)
48     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
49     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
50     0x95, 0x03,                    //     REPORT_COUNT (3)
51     0x75, 0x01,                    //     REPORT_SIZE (1)
52     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
53     0x95, 0x01,                    //     REPORT_COUNT (1)
54     0x75, 0x05,                    //     REPORT_SIZE (5)
55     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
56     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
57     0x09, 0x30,                    //     USAGE (X)
58     0x09, 0x31,                    //     USAGE (Y)
59     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
60     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
61     0x75, 0x08,                    //     REPORT_SIZE (8)
62     0x95, 0x02,                    //     REPORT_COUNT (2)
63     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
64     0x09, 0x38,                    //     USAGE (Mouse Wheel)
65     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
66     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
67     0x75, 0x08,                    //     REPORT_SIZE (8)
68     0x95, 0x01,                    //     REPORT_COUNT (1)
69     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
70     0xc0,                          //   END_COLLECTION
71     0xc0                           // END_COLLECTION
72 };
73 #define HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0]))
74 
75 #define     LSB(x) (x & 0x00ff)
76 #define     MSB(x) ((x & 0xff00) >> 8)
77 
78 /* Configuration descriptor 9 bytes */
79 #define CFG_DESC(wTotalLength, bNumInterfaces, bConfigurationValue)\
80     /* Configuration 1 descriptor 9 bytes */\
81     0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
82     (bNumInterfaces), (bConfigurationValue), 0x00,\
83     0x40, 0x00,
84 #define CFG_DESC_LEN 9
85 
86 /* DPUMP interface descriptors 9+7+7+7=30 bytes. */
87 #define DPUMP_IFC_DESC_ALL(ifc, bulk_in_epa, bulk_out_epa, int_in_epa) \
88     /* Interface descriptor */\
89     0x09, 0x04, (ifc), 0x00, 0x03, 0x99, 0x99, 0x99, 0x00,\
90     /* Endpoint descriptor (Bulk Out) */\
91     0x07, 0x05, (bulk_out_epa), 0x02, 0x40, 0x00, 0x00,\
92     /* Endpoint descriptor (Bulk In) */\
93     0x07, 0x05, (bulk_in_epa), 0x02, 0x40, 0x00, 0x00,\
94     /* Endpoint descriptor (Interrupt In) */\
95     0x07, 0x05, (int_in_epa), 0x02, 0x40, 0x00, 0x00,
96 #define DPUMP_IFC_DESC_ALL_LEN 30
97 
98 /* HID Mouse interface descriptors 9+9+7=25 bytes */
99 #define HID_MOUSE_IFC_DESC_ALL(ifc, interrupt_epa)     \
100     /* Interface descriptor */\
101     0x09, 0x04, (ifc), 0x01, 0x01, 0x03, 0x00, 0x00, 0x00,\
102     /* HID descriptor */\
103     0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),\
104     MSB(HID_MOUSE_REPORT_LENGTH),\
105     /* Endpoint descriptor (Interrupt) */\
106     0x07, 0x05, (interrupt_epa), 0x03, 0x08, 0x00, 0x08,
107 #define HID_MOUSE_IFC_DESC_ALL_LEN 25
108 
109 /* Define the counters used in the test application...  */
110 
111 static ULONG                           thread_0_counter;
112 static ULONG                           thread_1_counter;
113 static ULONG                           error_counter;
114 
115 static UCHAR                           error_callback_ignore = UX_FALSE;
116 static ULONG                           error_callback_counter;
117 
118 static UCHAR                           buffer[UX_TEST_BUFFER_SIZE];
119 
120 /* Define USBX test global variables.  */
121 
122 static UX_HOST_CLASS                   *class_driver;
123 static UX_HOST_CLASS_DPUMP             *dpump;
124 static UX_SLAVE_CLASS_DPUMP            *dpump_slave = UX_NULL;
125 
126 
127 static UCHAR device_framework_full_speed[] = {
128 
129     /* Device descriptor 18 bytes */
130     0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
131     0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
132     0x00, 0x01,
133 
134     /* Configuration descriptor 9 bytes */
135     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC_DESC_ALL_LEN, 2, 1)
136     /* DPUMP 9+7+7+7  =30 */
137     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83)
138     /* HID   9+9+7    =25 */
139     HID_MOUSE_IFC_DESC_ALL(1, 0x84)
140 };
141 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
142 
143 static UCHAR device_framework_high_speed[] = {
144 
145     /* Device descriptor */
146     0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
147     0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
148     0x03, 0x01,
149 
150     /* Device qualifier descriptor */
151     0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
152     0x01, 0x00,
153 
154     /* Configuration descriptor 9 bytes */
155     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC_DESC_ALL_LEN, 2, 1)
156     /* DPUMP 9+7+7+7  =30 */
157     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02, 0x83)
158     /* HID   9+9+7    =25 */
159     HID_MOUSE_IFC_DESC_ALL(1, 0x84)
160 };
161 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
162 
163 /* String Device Framework :
164     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
165     Byte 2       : Byte containing the index of the descriptor
166     Byte 3       : Byte containing the length of the descriptor string
167 */
168 
169 static UCHAR string_framework[] = {
170 
171     /* Manufacturer string descriptor : Index 1 */
172     0x09, 0x04, 0x01, 0x0c,
173     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
174     0x6f, 0x67, 0x69, 0x63,
175 
176     /* Product string descriptor : Index 2 */
177     0x09, 0x04, 0x02, 0x0c,
178     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
179     0x44, 0x65, 0x6d, 0x6f,
180 
181     /* Serial Number string descriptor : Index 3 */
182     0x09, 0x04, 0x03, 0x04,
183     0x30, 0x30, 0x30, 0x31
184 };
185 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
186 
187     /* Multiple languages are supported on the device, to add
188        a language besides English, the unicode language code must
189        be appended to the language_id_framework array and the length
190        adjusted accordingly. */
191 static UCHAR language_id_framework[] = {
192 
193 /* English. */
194     0x09, 0x04
195 };
196 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
197 
198 /* Simulation actions. */
199 
200 static UX_TEST_SETUP _GetDeviceDescriptor = UX_TEST_SETUP_GetDevDescr;
201 
202 static UX_TEST_HCD_SIM_ACTION get_dev_desc_len_error[] = {
203 /* function, request to match,
204    port action, port status,
205    request action, request EP, request data, request actual length, request status,
206    status, additional callback,
207    no_return */
208 {   UX_HCD_TRANSFER_REQUEST, &_GetDeviceDescriptor,
209         UX_FALSE, 0,
210         UX_TEST_SIM_REQ_ANSWER | UX_TEST_SETUP_MATCH_REQ_V, 0, device_framework_full_speed + 0, 7, 0,
211         UX_SUCCESS, UX_NULL},
212 {   UX_HCD_TRANSFER_REQUEST, &_GetDeviceDescriptor,
213         UX_FALSE, 0,
214         UX_TEST_SIM_REQ_ANSWER | UX_TEST_SETUP_MATCH_REQ_V, 0, device_framework_full_speed + 0, 8, 0,
215         UX_SUCCESS, UX_NULL},
216 {   UX_HCD_TRANSFER_REQUEST, &_GetDeviceDescriptor,
217         UX_FALSE, 0,
218         UX_TEST_SIM_REQ_ANSWER | UX_TEST_SETUP_MATCH_REQ_V, 0, device_framework_full_speed + 0, UX_DEVICE_DESCRIPTOR_LENGTH - 1, 0,
219         UX_SUCCESS, UX_NULL},
220 {   0   }
221 };
222 
223 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
224 
225 static VOID                ux_test_instance_activate(VOID  *dpump_instance);
226 static VOID                ux_test_instance_deactivate(VOID *dpump_instance);
227 
228 UINT                       _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
229 UINT                       ux_hcd_sim_initialize(UX_HCD *hcd);
230 UINT                       _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
231                                     ULONG requested_length, ULONG *actual_length);
232 UINT                       _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
233                                     ULONG requested_length, ULONG *actual_length);
234 
235 static TX_THREAD           ux_test_thread_simulation_0;
236 static TX_THREAD           ux_test_thread_simulation_1;
237 static void                ux_test_thread_simulation_0_entry(ULONG);
238 static void                ux_test_thread_simulation_1_entry(ULONG);
239 
240 
241 /* Define the ISR dispatch.  */
242 
243 extern VOID    (*test_isr_dispatch)(void);
244 
245 
246 /* Prototype for test control return.  */
247 
248 void  test_control_return(UINT status);
249 
250 
251 /* Define the ISR dispatch routine.  */
252 
test_isr(void)253 static void    test_isr(void)
254 {
255 
256     /* For further expansion of interrupt-level testing.  */
257 }
258 
error_callback(UINT system_level,UINT system_context,UINT error_code)259 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
260 {
261 
262     error_callback_counter ++;
263 
264     if (!error_callback_ignore)
265     {
266         {
267             /* Failed test.  */
268             printf("Error #%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
269             test_control_return(1);
270         }
271     }
272 }
273 
sleep_break_on_error(VOID)274 static UINT  sleep_break_on_error(VOID)
275 {
276 
277     if (error_callback_counter >= 2)
278         return error_callback_counter;
279 
280     return UX_SUCCESS;
281 }
282 
test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND * command)283 static UINT test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND *command)
284 {
285     switch(command->ux_slave_class_command_request)
286     {
287         case UX_SLAVE_CLASS_COMMAND_INITIALIZE:
288         case UX_SLAVE_CLASS_COMMAND_QUERY:
289             return UX_SUCCESS;
290 
291         default:
292             return UX_NO_CLASS_MATCH;
293     }
294 }
295 
296 /* Define what the initial system looks like.  */
297 
298 #ifdef CTEST
test_application_define(void * first_unused_memory)299 void test_application_define(void *first_unused_memory)
300 #else
301 void    usbx_ux_host_stack_device_descriptor_read_test_application_define(void *first_unused_memory)
302 #endif
303 {
304 
305 UINT status;
306 CHAR                            *stack_pointer;
307 CHAR                            *memory_pointer;
308 UX_SLAVE_CLASS_DPUMP_PARAMETER  parameter;
309 UX_SLAVE_CLASS_HID_PARAMETER    hid_parameter;
310 
311     /* Initialize the free memory pointer.  */
312     stack_pointer = (CHAR *) first_unused_memory;
313     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
314 
315     /* Initialize USBX Memory.  */
316     status =  ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
317 
318     /* Check for error.  */
319     if (status != UX_SUCCESS)
320     {
321 
322         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #1\n");
323         test_control_return(1);
324     }
325 
326     /* Register the error callback. */
327     _ux_utility_error_callback_register(error_callback);
328 
329     /* The code below is required for installing the host portion of USBX.  */
330     status =  ux_host_stack_initialize(UX_NULL);
331 
332     /* Check for error.  */
333     if (status != UX_SUCCESS)
334     {
335 
336         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #2\n");
337         test_control_return(1);
338     }
339 
340     /* Register all the host class drivers for this USBX implementation.  */
341     status =  ux_host_stack_class_register(_ux_system_host_class_dpump_name, ux_host_class_dpump_entry);
342 
343     /* Check for error.  */
344     if (status != UX_SUCCESS)
345     {
346 
347         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #3\n");
348         test_control_return(1);
349     }
350 
351     /* The code below is required for installing the device portion of USBX */
352     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
353                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
354                                        string_framework, STRING_FRAMEWORK_LENGTH,
355                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
356 
357     /* Check for error.  */
358     if (status != UX_SUCCESS)
359     {
360 
361         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #5\n");
362         test_control_return(1);
363     }
364 
365     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
366     parameter.ux_slave_class_dpump_instance_activate   =  ux_test_instance_activate;
367     parameter.ux_slave_class_dpump_instance_deactivate =  ux_test_instance_deactivate;
368 
369     /* Initialize the device dpump class. The class is connected with interface 0 */
370     status =  ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
371                                               1, 0, &parameter);
372 
373     /* Initialize the hid class parameters.  */
374     hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
375     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_MOUSE_REPORT_LENGTH;
376     hid_parameter.ux_device_class_hid_parameter_callback       = UX_NULL;
377     hid_parameter.ux_slave_class_hid_instance_activate         = UX_NULL;
378 
379     status |= ux_device_stack_class_register(_ux_system_slave_class_hid_name, test_ux_device_class_hid_entry,
380                                               1, 1, &parameter);
381 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
382     /* Check for error.  */
383     if (status != UX_SUCCESS)
384     {
385 
386         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #6\n");
387         test_control_return(1);
388     }
389 #endif
390     /* Initialize the simulated device controller.  */
391     status =  _ux_test_dcd_sim_slave_initialize();
392 
393     /* Check for error.  */
394     if (status != UX_SUCCESS)
395     {
396 
397         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #7\n");
398         test_control_return(1);
399     }
400 
401     /* Register all the USB host controllers available in this system */
402     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
403 
404     /* Check for error.  */
405     if (status != UX_SUCCESS)
406     {
407 
408         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #4\n");
409         test_control_return(1);
410     }
411 
412     /* Create the main host simulation thread.  */
413     status =  tx_thread_create(&ux_test_thread_simulation_0, "test host simulation", ux_test_thread_simulation_0_entry, 0,
414             stack_pointer, UX_TEST_STACK_SIZE,
415             20, 20, 1, TX_AUTO_START);
416 
417     /* Check for error.  */
418     if (status != TX_SUCCESS)
419     {
420 
421         printf("Running ux_host_stack_device_descriptor_read Test................... ERROR #8\n");
422         test_control_return(1);
423     }
424 }
425 
426 
ux_test_thread_simulation_0_entry(ULONG arg)427 static void  ux_test_thread_simulation_0_entry(ULONG arg)
428 {
429 
430     /* Inform user.  */
431     printf("Running ux_host_stack_device_descriptor_read Test................... ");
432 
433     /* Simulate GetConfiguration length error. */
434     ux_test_hcd_sim_host_set_actions(get_dev_desc_len_error);
435 
436     /* No break if error callback is called. */
437     error_callback_ignore = UX_TRUE;
438     error_callback_counter = 0;
439 
440     /* Connect. */
441     ux_test_hcd_sim_host_connect(UX_HIGH_SPEED_DEVICE);
442     ux_test_breakable_sleep(100, sleep_break_on_error);
443 
444     /* Enumeration check, may fail if no retry and success on more than one retry. */
445     /* There is error reported. */
446     if (error_callback_counter == 0)
447     {
448 
449         printf("ERROR #%d: Expect error\n", __LINE__);
450         error_counter ++;
451     }
452 
453     /* Sleep for a tick to make sure everything is complete.  */
454     tx_thread_sleep(1);
455 
456     /* Check for errors from other threads.  */
457     if (error_counter)
458     {
459 
460         /* Test error.  */
461         printf("ERROR #14\n");
462         test_control_return(1);
463     }
464     else
465     {
466 
467         /* Successful test.  */
468         printf("SUCCESS!\n");
469         test_control_return(0);
470     }
471 }
472 
ux_test_instance_activate(VOID * dpump_instance)473 static VOID  ux_test_instance_activate(VOID *dpump_instance)
474 {
475 
476     /* Save the DPUMP instance.  */
477     dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
478 }
479 
ux_test_instance_deactivate(VOID * dpump_instance)480 static VOID  ux_test_instance_deactivate(VOID *dpump_instance)
481 {
482 
483     /* Reset the DPUMP instance.  */
484     dpump_slave = UX_NULL;
485 }
486 
487