1 /* This test is designed to test the ux_host_stack_hcd_transfer_request.  */
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=23 bytes. */
87 #define DPUMP_IFC_DESC_ALL(ifc, bulk_in_epa, bulk_out_epa) \
88     /* Interface descriptor */\
89     0x09, 0x04, (ifc), 0x00, 0x02, 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 #define DPUMP_IFC_DESC_ALL_LEN 23
95 
96 /* HID Mouse interface descriptors 9+9=18 bytes */
97 #define HID_MOUSE_IFC0_DESC_ALL(ifc)     \
98     /* Interface descriptor */\
99     0x09, 0x04, (ifc), 0x00, 0x00, 0x03, 0x00, 0x00, 0x00,\
100     /* HID descriptor */\
101     0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),\
102     MSB(HID_MOUSE_REPORT_LENGTH),
103 #define HID_MOUSE_IFC0_DESC_ALL_LEN 18
104 
105 /* HID Mouse interface descriptors 9+9+7=25 bytes */
106 #define HID_MOUSE_IFC1_DESC_ALL(ifc, interrupt_epa)     \
107     /* Interface descriptor */\
108     0x09, 0x04, (ifc), 0x01, 0x01, 0x03, 0x00, 0x00, 0x00,\
109     /* HID descriptor */\
110     0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),\
111     MSB(HID_MOUSE_REPORT_LENGTH),\
112     /* Endpoint descriptor (Interrupt) */\
113     0x07, 0x05, (interrupt_epa), 0x03, 0x08, 0x00, 0x08,
114 #define HID_MOUSE_IFC1_DESC_ALL_LEN 25
115 
116 /* Define the counters used in the test application...  */
117 
118 static ULONG                           thread_0_counter;
119 static ULONG                           thread_1_counter;
120 static ULONG                           error_counter;
121 
122 static UCHAR                           expect_errors = UX_FALSE;
123 
124 
125 /* Define USBX test global variables.  */
126 
127 static UX_HOST_CLASS                   *class_driver;
128 static UX_HOST_CLASS_DPUMP             *dpump;
129 static UX_SLAVE_CLASS_DPUMP            *dpump_slave = UX_NULL;
130 
131 static UCHAR                            buffer[UX_TEST_BUFFER_SIZE];
132 
133 static UCHAR device_framework_full_speed[] = {
134 
135     /* Device descriptor 18 bytes */
136     0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
137     0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
138     0x00, 0x01,
139 
140     /* Configuration descriptor 9 bytes */
141     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC0_DESC_ALL_LEN + HID_MOUSE_IFC1_DESC_ALL_LEN, 2, 1)
142     /* DPUMP 9+9+7      =25 */
143     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02)
144     /* HID   9+9 + 9+9+7=43 */
145     HID_MOUSE_IFC0_DESC_ALL(1)
146     HID_MOUSE_IFC1_DESC_ALL(1, 0x83)
147 };
148 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
149 
150 static UCHAR device_framework_high_speed[] = {
151 
152     /* Device descriptor */
153     0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
154     0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
155     0x03, 0x01,
156 
157     /* Device qualifier descriptor */
158     0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
159     0x01, 0x00,
160 
161     /* Configuration descriptor */
162     CFG_DESC(CFG_DESC_LEN + DPUMP_IFC_DESC_ALL_LEN + HID_MOUSE_IFC0_DESC_ALL_LEN + HID_MOUSE_IFC1_DESC_ALL_LEN, 2, 1)
163     /* DPUMP */
164     DPUMP_IFC_DESC_ALL(0, 0x81, 0x02)
165     /* HID */
166     HID_MOUSE_IFC0_DESC_ALL(1)
167     HID_MOUSE_IFC1_DESC_ALL(1, 0x83)
168 };
169 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
170 
171 /* String Device Framework :
172     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
173     Byte 2       : Byte containing the index of the descriptor
174     Byte 3       : Byte containing the length of the descriptor string
175 */
176 
177 static UCHAR string_framework[] = {
178 
179     /* Manufacturer string descriptor : Index 1 */
180     0x09, 0x04, 0x01, 0x0c,
181     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
182     0x6f, 0x67, 0x69, 0x63,
183 
184     /* Product string descriptor : Index 2 */
185     0x09, 0x04, 0x02, 0x0c,
186     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
187     0x44, 0x65, 0x6d, 0x6f,
188 
189     /* Serial Number string descriptor : Index 3 */
190     0x09, 0x04, 0x03, 0x04,
191     0x30, 0x30, 0x30, 0x31
192 };
193 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
194 
195     /* Multiple languages are supported on the device, to add
196        a language besides English, the unicode language code must
197        be appended to the language_id_framework array and the length
198        adjusted accordingly. */
199 static UCHAR language_id_framework[] = {
200 
201 /* English. */
202     0x09, 0x04
203 };
204 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
205 
206 /* Simulation actions. */
207 
208 static UX_TEST_SIM_ENTRY_ACTION dcd_endpoint_create_fail[] = {
209 /* function, request to match,
210    port action, port status,
211    request action, request EP, request data, request actual length, request status,
212    status, additional callback,
213    no_return */
214 {   UX_DCD_CREATE_ENDPOINT, NULL,
215         UX_FALSE, 0,
216         0         , 0, UX_NULL, 0, 0,
217         UX_ERROR , UX_NULL},
218 {   0   }
219 };
220 
221 
222 /* Define prototypes for external Host Controller's (HCDs), classes and clients.  */
223 
224 static VOID                ux_test_instance_activate(VOID  *dpump_instance);
225 static VOID                ux_test_instance_deactivate(VOID *dpump_instance);
226 
227 UINT                       _ux_host_class_dpump_entry(UX_HOST_CLASS_COMMAND *command);
228 UINT                       ux_hcd_sim_initialize(UX_HCD *hcd);
229 UINT                       _ux_host_class_dpump_write(UX_HOST_CLASS_DPUMP *dpump, UCHAR * data_pointer,
230                                     ULONG requested_length, ULONG *actual_length);
231 UINT                       _ux_host_class_dpump_read (UX_HOST_CLASS_DPUMP *dpump, UCHAR *data_pointer,
232                                     ULONG requested_length, ULONG *actual_length);
233 
234 static TX_THREAD           ux_test_thread_simulation_0;
235 static TX_THREAD           ux_test_thread_simulation_1;
236 static void                ux_test_thread_simulation_0_entry(ULONG);
237 static void                ux_test_thread_simulation_1_entry(ULONG);
238 
239 
240 /* Define the ISR dispatch.  */
241 
242 extern VOID    (*test_isr_dispatch)(void);
243 
244 
245 /* Prototype for test control return.  */
246 
247 void  test_control_return(UINT status);
248 
249 
250 /* Define the ISR dispatch routine.  */
251 
test_isr(void)252 static void    test_isr(void)
253 {
254 
255     /* For further expansion of interrupt-level testing.  */
256 }
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     if (!expect_errors)
262     {
263         if (error_code != UX_DEVICE_HANDLE_UNKNOWN)
264         {
265             /* Failed test.  */
266             printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
267             test_control_return(1);
268         }
269     }
270 }
271 
test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND * command)272 static UINT test_ux_device_class_hid_entry(UX_SLAVE_CLASS_COMMAND *command)
273 {
274     return UX_SUCCESS;
275     // return _ux_device_class_hid_entry(command);
276 }
277 
278 /* Define what the initial system looks like.  */
279 
280 #ifdef CTEST
test_application_define(void * first_unused_memory)281 void test_application_define(void *first_unused_memory)
282 #else
283 void    usbx_ux_host_stack_hcd_transfer_request_test_application_define(void *first_unused_memory)
284 #endif
285 {
286 
287 UINT status;
288 CHAR                            *stack_pointer;
289 CHAR                            *memory_pointer;
290 UX_SLAVE_CLASS_DPUMP_PARAMETER  parameter;
291 UX_SLAVE_CLASS_HID_PARAMETER    hid_parameter;
292 
293     /* Initialize the free memory pointer.  */
294     stack_pointer = (CHAR *) first_unused_memory;
295     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
296 
297     /* Initialize USBX Memory.  */
298     status =  ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
299 
300     /* Check for error.  */
301     if (status != UX_SUCCESS)
302     {
303 
304         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #1\n");
305         test_control_return(1);
306     }
307 
308     /* Register the error callback. */
309     _ux_utility_error_callback_register(error_callback);
310 
311     /* The code below is required for installing the host portion of USBX.  */
312     status =  ux_host_stack_initialize(UX_NULL);
313 
314     /* Check for error.  */
315     if (status != UX_SUCCESS)
316     {
317 
318         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #2\n");
319         test_control_return(1);
320     }
321 
322     /* Register all the host class drivers for this USBX implementation.  */
323     status =  ux_host_stack_class_register(_ux_system_host_class_dpump_name, ux_host_class_dpump_entry);
324 
325     /* Check for error.  */
326     if (status != UX_SUCCESS)
327     {
328 
329         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #3\n");
330         test_control_return(1);
331     }
332 
333     /* The code below is required for installing the device portion of USBX */
334     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
335                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
336                                        string_framework, STRING_FRAMEWORK_LENGTH,
337                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
338 
339     /* Check for error.  */
340     if (status != UX_SUCCESS)
341     {
342 
343         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #5\n");
344         test_control_return(1);
345     }
346 
347     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
348     parameter.ux_slave_class_dpump_instance_activate   =  ux_test_instance_activate;
349     parameter.ux_slave_class_dpump_instance_deactivate =  ux_test_instance_deactivate;
350 
351     /* Initialize the device dpump class. The class is connected with interface 0 */
352     status =  ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
353                                               1, 0, &parameter);
354 
355     /* Initialize the hid class parameters.  */
356     hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
357     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_MOUSE_REPORT_LENGTH;
358     hid_parameter.ux_device_class_hid_parameter_callback       = UX_NULL;
359     hid_parameter.ux_slave_class_hid_instance_activate         = UX_NULL;
360 
361     status |= ux_device_stack_class_register(_ux_system_slave_class_hid_name, test_ux_device_class_hid_entry,
362                                               1, 1, &parameter);
363 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
364     /* Check for error.  */
365     if (status != UX_SUCCESS)
366     {
367 
368         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #6\n");
369         test_control_return(1);
370     }
371 #endif
372     /* Initialize the simulated device controller.  */
373     status =  _ux_test_dcd_sim_slave_initialize();
374 
375     /* Check for error.  */
376     if (status != UX_SUCCESS)
377     {
378 
379         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #7\n");
380         test_control_return(1);
381     }
382 
383     /* Register all the USB host controllers available in this system */
384     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
385 
386     /* Check for error.  */
387     if (status != UX_SUCCESS)
388     {
389 
390         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #4\n");
391         test_control_return(1);
392     }
393 
394     /* Create the main host simulation thread.  */
395     status =  tx_thread_create(&ux_test_thread_simulation_0, "test host simulation", ux_test_thread_simulation_0_entry, 0,
396             stack_pointer, UX_TEST_STACK_SIZE,
397             20, 20, 1, TX_AUTO_START);
398 
399     /* Check for error.  */
400     if (status != TX_SUCCESS)
401     {
402 
403         printf("Running ux_host_stack_hcd_transfer_request Test..................... ERROR #8\n");
404         test_control_return(1);
405     }
406 }
407 
408 
ux_test_thread_simulation_0_entry(ULONG arg)409 static void  ux_test_thread_simulation_0_entry(ULONG arg)
410 {
411 
412 UINT                     status;
413 UX_DEVICE                                          *device;
414 UX_ENDPOINT                                        *control_endpoint;
415 UX_TRANSFER                                        *transfer_request;
416 
417     /* Inform user.  */
418     printf("Running ux_host_stack_hcd_transfer_request Test..................... ");
419 
420     ux_test_dcd_sim_slave_connect(UX_HIGH_SPEED_DEVICE);
421     ux_test_hcd_sim_host_connect(UX_HIGH_SPEED_DEVICE);
422 
423     /* Enumeration check. */
424     if (dpump_slave == UX_NULL)
425     {
426 
427         printf("ERROR #%d: Enum fail\n", __LINE__);
428         test_control_return(1);
429     }
430 
431     status = ux_host_stack_device_get(0, &device);
432     if (status != UX_SUCCESS)
433     {
434 
435         printf("ERROR #%d: device_get fail\n", __LINE__);
436         test_control_return(1);
437     }
438     control_endpoint = &device->ux_device_control_endpoint;
439     transfer_request = &control_endpoint->ux_endpoint_transfer_request;
440 
441     /* Test normal requests */
442     transfer_request -> ux_transfer_request_data_pointer =      buffer;
443     transfer_request -> ux_transfer_request_requested_length =  64;
444     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
445     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
446     transfer_request -> ux_transfer_request_value =             UX_DEVICE_DESCRIPTOR_ITEM << 8;
447     transfer_request -> ux_transfer_request_index =             0;
448     status = _ux_host_stack_hcd_transfer_request(transfer_request);
449     if (status != UX_SUCCESS)
450     {
451 
452         printf("ERROR #%d: GetDeviceDescriptor(64) fail\n", __LINE__);
453         error_counter ++;
454     }
455     if (transfer_request->ux_transfer_request_actual_length != 18)
456     {
457 
458         printf("ERROR #%d: GetDeviceDescriptor(64) actual length is not 18\n", __LINE__);
459         error_counter ++;
460     }
461 
462     /* Sleep for a tick to make sure everything is complete.  */
463     tx_thread_sleep(1);
464 
465     /* Check for errors from other threads.  */
466     if (error_counter)
467     {
468 
469         /* Test error.  */
470         printf("ERROR #14\n");
471         test_control_return(1);
472     }
473     else
474     {
475 
476         /* Successful test.  */
477         printf("SUCCESS!\n");
478         test_control_return(0);
479     }
480 }
481 
ux_test_instance_activate(VOID * dpump_instance)482 static VOID  ux_test_instance_activate(VOID *dpump_instance)
483 {
484 
485     /* Save the DPUMP instance.  */
486     dpump_slave = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
487 }
488 
ux_test_instance_deactivate(VOID * dpump_instance)489 static VOID  ux_test_instance_deactivate(VOID *dpump_instance)
490 {
491 
492     /* Reset the DPUMP instance.  */
493     dpump_slave = UX_NULL;
494 }
495 
496