1 #include "usbx_test_common_hid.h"
2 #include "ux_host_class_hid_keyboard.h"
3 
4 
5 #define DUMMY_USBX_MEMORY_SIZE (64*1024)
6 static UCHAR dummy_usbx_memory[DUMMY_USBX_MEMORY_SIZE];
7 static UCHAR buffer[UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH * 2];
8 
9 static UCHAR hid_report_descriptor[] = {
10 
11     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
12     0x09, 0x06,                    // USAGE (Keyboard)
13     0xa1, 0x01,                    // COLLECTION (Application)
14     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
15     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
16     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
17     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
18     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
19     0x75, 0x01,                    //   REPORT_SIZE (1)
20     0x95, 0x08,                    //   REPORT_COUNT (8)
21     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
22     0x95, 0x01,                    //   REPORT_COUNT (1)
23     0x75, 0x08,                    //   REPORT_SIZE (8)
24     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
25     0x95, 0x05,                    //   REPORT_COUNT (5)
26     0x75, 0x01,                    //   REPORT_SIZE (1)
27     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
28     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
29     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
30     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
31     0x95, 0x01,                    //   REPORT_COUNT (1)
32     0x75, 0x03,                    //   REPORT_SIZE (3)
33     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
34     0x95, 0x06,                    //   REPORT_COUNT (6)
35     0x75, 0x08,                    //   REPORT_SIZE (8)
36     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
37     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
38     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
39     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
40     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
41     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
42     0xc0                           // END_COLLECTION
43 };
44 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
45 
46 
47 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
48 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
49 
50     /* Device descriptor */
51         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
52         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
53         0x00, 0x01,
54 
55     /* Configuration descriptor */
56         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
57         0x32,
58 
59     /* Interface descriptor */
60         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
61         0x00,
62 
63     /* HID descriptor */
64         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
65         MSB(HID_REPORT_LENGTH),
66 
67     /* Endpoint descriptor (Interrupt) */
68         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
69 
70     };
71 
72 
73 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
74 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
75 
76     /* Device descriptor */
77         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
78         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
79         0x03, 0x01,
80 
81     /* Device qualifier descriptor */
82         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
83         0x01, 0x00,
84 
85     /* Configuration descriptor */
86         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
87         0x32,
88 
89     /* Interface descriptor */
90         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
91         0x00,
92 
93     /* HID descriptor */
94         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
95         MSB(HID_REPORT_LENGTH),
96 
97     /* Endpoint descriptor (Interrupt) */
98         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
99 
100     };
101 
102 
103     /* String Device Framework :
104      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
105      Byte 2       : Byte containing the index of the descriptor
106      Byte 3       : Byte containing the length of the descriptor string
107     */
108 
109 #define STRING_FRAMEWORK_LENGTH 40
110 static UCHAR string_framework[] = {
111 
112     /* Manufacturer string descriptor : Index 1 */
113         0x09, 0x04, 0x01,
114         0xff, /* Set the length to something very large.  */
115         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
116         0x6f, 0x67, 0x69, 0x63,
117 
118     /* Product string descriptor : Index 2 */
119         0x09, 0x04, 0x02, 0x0c,
120         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
121         0x6f, 0x61, 0x72, 0x64,
122 
123     /* Serial Number string descriptor : Index 3 */
124         0x09, 0x04, 0x03, 0x04,
125         0x30, 0x30, 0x30, 0x31
126     };
127 
128 
129     /* Multiple languages are supported on the device, to add
130        a language besides english, the unicode language code must
131        be appended to the language_id_framework array and the length
132        adjusted accordingly. */
133 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
134 static UCHAR language_id_framework[] = {
135 
136     /* English. */
137         0x09, 0x04
138     };
139 
140 
141 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
142 
143 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)144 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
145 {
146     return 0;
147 }
148 
error_callback(UINT system_level,UINT system_context,UINT error_code)149 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
150 {
151 }
152 
set_configure_descriptor_length(ULONG length)153 static VOID set_configure_descriptor_length(ULONG length)
154 {
155     device_framework_full_speed[0x12        + 2] = LSB(length);
156     device_framework_full_speed[0x12        + 3] = MSB(length);
157     device_framework_high_speed[0x12 + 0x0a + 2] = LSB(length);
158     device_framework_high_speed[0x12 + 0x0a + 3] = LSB(length);
159 }
160 
161 /* Define what the initial system looks like.  */
162 
163 #ifdef CTEST
test_application_define(void * first_unused_memory)164 void test_application_define(void *first_unused_memory)
165 #else
166 void    usbx_ux_device_stack_descriptor_send_test_application_define(void *first_unused_memory)
167 #endif
168 {
169 
170 UINT status;
171 CHAR *                          stack_pointer;
172 CHAR *                          memory_pointer;
173 
174 
175     /* Inform user.  */
176     printf("Running ux_device_stack_descriptor_send test........................ ");
177 
178     /* Initialize the free memory pointer */
179     stack_pointer = (CHAR *) usbx_memory;
180     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
181 
182     /* Initialize USBX. Memory */
183     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
184 
185     /* Check for error.  */
186     if (status != UX_SUCCESS)
187     {
188 
189         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
190         test_control_return(1);
191     }
192 
193     /* Register the error callback. */
194     _ux_utility_error_callback_register(error_callback);
195 
196     /* The code below is required for installing the host portion of USBX */
197     status =  ux_host_stack_initialize(ux_system_host_change_function);
198     if (status != UX_SUCCESS)
199     {
200 
201         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
202         test_control_return(1);
203     }
204 
205     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
206     if (status != UX_SUCCESS)
207     {
208 
209         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
210         test_control_return(1);
211     }
212 
213     /* Register the HID client(s).  */
214     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
215     if (status != UX_SUCCESS)
216     {
217 
218         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
219         test_control_return(1);
220     }
221 
222     /* The code below is required for installing the device portion of USBX. No call back for
223        device status change in this example. */
224     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
225                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
226                                        string_framework, STRING_FRAMEWORK_LENGTH,
227                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
228     if(status!=UX_SUCCESS)
229     {
230 
231         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
232         test_control_return(1);
233     }
234 
235     /* Initialize the hid class parameters.  */
236     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
237     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
238     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
239 
240     /* Initilize the device hid class. The class is connected with interface 2 */
241     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
242                                                 1,2, (VOID *)&hid_parameter);
243     if(status!=UX_SUCCESS)
244     {
245 
246         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
247         test_control_return(1);
248     }
249 
250     /* Initialize the simulated device controller.  */
251     status =  _ux_dcd_sim_slave_initialize();
252 
253     /* Check for error.  */
254     if (status != UX_SUCCESS)
255     {
256 
257         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
258         test_control_return(1);
259     }
260 
261     /* Register all the USB host controllers available in this system */
262     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
263 
264     /* Check for error.  */
265     if (status != UX_SUCCESS)
266     {
267 
268         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
269         test_control_return(1);
270     }
271 
272     /* Create the main host simulation thread.  */
273     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
274             stack_pointer, UX_DEMO_STACK_SIZE,
275             20, 20, 1, TX_AUTO_START);
276 
277     /* Check for error.  */
278     if (status != TX_SUCCESS)
279     {
280 
281         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
282         test_control_return(1);
283     }
284 }
285 
tx_demo_thread_host_simulation_entry(ULONG arg)286 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
287 {
288 
289 UINT                            status;
290 UX_HOST_CLASS_HID_KEYBOARD     *keyboard;
291 UX_ENDPOINT                    *endpoint;
292 UX_TRANSFER                    *transfer_request;
293 UCHAR                          *hid_class_descriptor = device_framework_high_speed + 0x2E;
294 
295     /* Find the HID class */
296     status = demo_class_hid_get();
297     if (status != UX_SUCCESS)
298     {
299 
300         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
301         test_control_return(1);
302     }
303 
304     while(1)
305     {
306 
307         /* Get the HID client */
308         hid_client = hid -> ux_host_class_hid_client;
309 
310         /* Check if the instance of the keyboard is live */
311         if (hid_client && hid_client -> ux_host_class_hid_client_local_instance != UX_NULL)
312             break;
313 
314         tx_thread_sleep(10);
315     }
316 
317 
318 
319     /* Get the keyboard instance */
320     keyboard =  (UX_HOST_CLASS_HID_KEYBOARD *)hid_client -> ux_host_class_hid_client_local_instance;
321 
322     /* Get default endpoint. */
323     endpoint = &hid->ux_host_class_hid_device->ux_device_control_endpoint;
324 
325     /* Get the transfer request. */
326     transfer_request = &endpoint->ux_endpoint_transfer_request;
327 
328 #ifdef UX_DEVICE_ENABLE_GET_STRING_WITH_ZERO_LANGUAGE_ID
329     /**************************************************
330      * Test case: Get string descriptor with zero language ID. Only tested if UX_DEVICE_ENABLE_GET_STRING_WITH_ZERO_LANGUAGE_ID is defined.
331      **************************************************/
332     string_framework[3] = 12;
333     /* Create a transfer request for getting the string descriptor with zero .  */
334     transfer_request -> ux_transfer_request_data_pointer =      buffer;
335     transfer_request -> ux_transfer_request_requested_length =  64;
336     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
337     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
338     transfer_request -> ux_transfer_request_value =             UX_STRING_DESCRIPTOR_ITEM << 8 | 3;
339     transfer_request -> ux_transfer_request_index =             0;
340 
341     /* Send request to HCD layer.  */
342     status = ux_host_stack_transfer_request(transfer_request);
343     if (status != UX_SUCCESS)
344     {
345 
346         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
347         test_control_return(1);
348     }
349     string_framework[3] = 0xff;
350 #endif
351 
352     /* Modify configuration descriptor total length to generate error.  */
353     set_configure_descriptor_length(UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH+1);
354 
355     /**************************************************
356      * Test case: 'case: UX_OTHER_SPEED_DESCRIPTOR_ITEM'
357      *     'if (length > UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)'
358      **************************************************/
359 
360     /* Create a transfer request for the HID class descriptor.  */
361     transfer_request -> ux_transfer_request_data_pointer =      buffer;
362     transfer_request -> ux_transfer_request_requested_length =  UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH+1;
363     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
364     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
365     transfer_request -> ux_transfer_request_value =             UX_OTHER_SPEED_DESCRIPTOR_ITEM << 8;
366     transfer_request -> ux_transfer_request_index =             0;
367 
368     /* Send request to HCD layer.  */
369     status = ux_host_stack_transfer_request(transfer_request);
370     if (status == UX_SUCCESS)
371     {
372 
373         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
374         test_control_return(1);
375     }
376 
377     /**************************************************
378      * Test case: 'case: UX_CONFIGURATION_DESCRIPTOR_ITEM'
379      *     'if (length > UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)'
380      **************************************************/
381 
382     /* Create a transfer request for the HID class descriptor.  */
383     transfer_request -> ux_transfer_request_data_pointer =      buffer;
384     transfer_request -> ux_transfer_request_requested_length =  UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH+1;
385     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
386     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
387     transfer_request -> ux_transfer_request_value =             UX_CONFIGURATION_DESCRIPTOR_ITEM << 8;
388     transfer_request -> ux_transfer_request_index =             0;
389 
390     /* Send request to HCD layer.  */
391     status = ux_host_stack_transfer_request(transfer_request);
392     if (status == UX_SUCCESS)
393     {
394 
395         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
396         test_control_return(1);
397     }
398 
399     /**************************************************
400      * Test case: language ID framework is too large for device to send.
401      **************************************************/
402 
403     /* Make the language ID framework too large.  */
404     _ux_system_slave->ux_system_slave_language_id_framework_length = UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH + 1;
405 
406     /* Create a transfer request for the HID class descriptor.  */
407     transfer_request -> ux_transfer_request_data_pointer =      buffer;
408     transfer_request -> ux_transfer_request_requested_length =  0xffff;
409     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
410     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
411     transfer_request -> ux_transfer_request_value =             UX_STRING_DESCRIPTOR_ITEM << 8 | 0;
412     transfer_request -> ux_transfer_request_index =             0;
413 
414     /* Send request to HCD layer.  */
415     status = ux_host_stack_transfer_request(transfer_request);
416     if (status == UX_SUCCESS)
417     {
418 
419         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
420         test_control_return(1);
421     }
422 
423     /**************************************************
424      * Test case: string descriptor is too large for device to send.
425      **************************************************/
426 #if UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH > 255
427 #warning String descriptor too large not tested due to control buffer too big
428 #else
429 
430     /* Make the language ID framework too large.  */
431     _ux_system_slave->ux_system_slave_language_id_framework_length = UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH + 1;
432 
433     /* Create a transfer request for the HID class descriptor.  */
434     transfer_request -> ux_transfer_request_data_pointer =      buffer;
435     transfer_request -> ux_transfer_request_requested_length =  0xffff;
436     transfer_request -> ux_transfer_request_function =          UX_GET_DESCRIPTOR;
437     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_STANDARD | UX_REQUEST_TARGET_DEVICE;
438     transfer_request -> ux_transfer_request_value =             UX_STRING_DESCRIPTOR_ITEM << 8 | 1;
439     transfer_request -> ux_transfer_request_index =             0x0409;
440 
441     /* Send request to HCD layer.  */
442     status = ux_host_stack_transfer_request(transfer_request);
443     if (status == UX_SUCCESS)
444     {
445 
446         printf("Error on line %d, error code: 0x%x\n", __LINE__, status);
447         test_control_return(1);
448     }
449 #endif
450 
451     /* Now disconnect the device.  */
452     _ux_device_stack_disconnect();
453 
454     /* And deinitialize the class.  */
455     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
456 
457     /* Deinitialize the device side of usbx.  */
458     _ux_device_stack_uninitialize();
459 
460     /* And finally the usbx system resources.  */
461     _ux_system_uninitialize();
462 
463     /* Successful test.  */
464     printf("SUCCESS!\n");
465     test_control_return(0);
466 }
467 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)468 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
469 {
470     return(UX_SUCCESS);
471 }
472