1 /* This test ensures that the parser handles the example monitor report descriptor
2    from the USB HID report descriptor tool. */
3 
4 #include "usbx_test_common_hid.h"
5 #include "ux_host_class_hid_keyboard.h"
6 
7 
8 static UCHAR hid_report_descriptor[] = {
9 
10     0x05, 0x80,                    // USAGE_PAGE (Monitor)
11     0x09, 0x01,                    // USAGE (Monitor Control)
12     0xa1, 0x01,                    // COLLECTION (Application)
13     0x85, 0x01,                    //   REPORT_ID (1)
14     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
15     0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
16     0x75, 0x08,                    //   REPORT_SIZE (8)
17     0x95, 0x80,                    //   REPORT_COUNT (128)
18     0x09, 0x02,                    //   USAGE (EDID Information)
19     0xb2, 0x02, 0x01,              //   FEATURE (Data,Var,Abs,Buf)
20     0x85, 0x02,                    //   REPORT_ID (2)
21     0x95, 0xf3,                    //   REPORT_COUNT (243)
22     0x09, 0x03,                    //   USAGE (VDIF Information)
23     0xb2, 0x02, 0x01,              //   FEATURE (Data,Var,Abs,Buf)
24     0x85, 0x03,                    //   REPORT_ID (3)
25     0x05, 0x82,                    //   USAGE_PAGE (VESA Virtual Controls)
26     0x95, 0x01,                    //   REPORT_COUNT (1)
27     0x75, 0x10,                    //   REPORT_SIZE (16)
28     0x26, 0xc8, 0x00,              //   LOGICAL_MAXIMUM (200)
29     0x09, 0x10,                    //   USAGE (Brightness)
30     0xb1, 0x02,                    //   FEATURE (Data,Var,Abs)
31     0x85, 0x04,                    //   REPORT_ID (4)
32     0x25, 0x64,                    //   LOGICAL_MAXIMUM (100)
33     0x09, 0x12,                    //   USAGE (Contrast)
34     0xb1, 0x02,                    //   FEATURE (Data,Var,Abs)
35     0x95, 0x06,                    //   REPORT_COUNT (6)
36     0x26, 0xff, 0x00,              //   LOGICAL_MAXIMUM (255)
37     0x09, 0x16,                    //   USAGE (Video Gain Red)
38     0x09, 0x18,                    //   USAGE (Video Gain Green)
39     0x09, 0x1a,                    //   USAGE (Video Gain Blue)
40     0x09, 0x6c,                    //   USAGE (Video Black Level Red)
41     0x09, 0x6e,                    //   USAGE (Video Black Level Green)
42     0x09, 0x70,                    //   USAGE (Video Black Level Blue)
43     0xb1, 0x02,                    //   FEATURE (Data,Var,Abs)
44     0x85, 0x05,                    //   REPORT_ID (5)
45     0x25, 0x7f,                    //   LOGICAL_MAXIMUM (127)
46     0x09, 0x20,                    //   USAGE (Horizontal Position )
47     0x09, 0x22,                    //   USAGE (Horizontal Size )
48     0x09, 0x30,                    //   USAGE (Vertical Position )
49     0x09, 0x32,                    //   USAGE (Vertical Size )
50     0x09, 0x42,                    //   USAGE (Trapezoidal Distortion)
51     0x09, 0x44,                    //   USAGE (Tilt)
52     0xb1, 0x02,                    //   FEATURE (Data,Var,Abs)
53     0xc0                           // END_COLLECTION
54 };
55 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
56 
57 
58 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
59 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
60 
61     /* Device descriptor */
62         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
63         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
64         0x00, 0x01,
65 
66     /* Configuration descriptor */
67         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
68         0x32,
69 
70     /* Interface descriptor */
71         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
72         0x00,
73 
74     /* HID descriptor */
75         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
76         MSB(HID_REPORT_LENGTH),
77 
78     /* Endpoint descriptor (Interrupt) */
79         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
80 
81     };
82 
83 
84 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
85 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
86 
87     /* Device descriptor */
88         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
89         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
90         0x03, 0x01,
91 
92     /* Device qualifier descriptor */
93         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
94         0x01, 0x00,
95 
96     /* Configuration descriptor */
97         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
98         0x32,
99 
100     /* Interface descriptor */
101         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
102         0x00,
103 
104     /* HID descriptor */
105         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
106         MSB(HID_REPORT_LENGTH),
107 
108     /* Endpoint descriptor (Interrupt) */
109         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
110 
111     };
112 
113 
114     /* String Device Framework :
115      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
116      Byte 2       : Byte containing the index of the descriptor
117      Byte 3       : Byte containing the length of the descriptor string
118     */
119 
120 #define STRING_FRAMEWORK_LENGTH 40
121 static UCHAR string_framework[] = {
122 
123     /* Manufacturer string descriptor : Index 1 */
124         0x09, 0x04, 0x01, 0x0c,
125         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
126         0x6f, 0x67, 0x69, 0x63,
127 
128     /* Product string descriptor : Index 2 */
129         0x09, 0x04, 0x02, 0x0c,
130         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
131         0x6f, 0x61, 0x72, 0x64,
132 
133     /* Serial Number string descriptor : Index 3 */
134         0x09, 0x04, 0x03, 0x04,
135         0x30, 0x30, 0x30, 0x31
136     };
137 
138 
139     /* Multiple languages are supported on the device, to add
140        a language besides english, the unicode language code must
141        be appended to the language_id_framework array and the length
142        adjusted accordingly. */
143 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
144 static UCHAR language_id_framework[] = {
145 
146     /* English. */
147         0x09, 0x04
148     };
149 
150 
error_callback(UINT system_level,UINT system_context,UINT error_code)151 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
152 {
153 
154     /* USBX currently doesn't support this device. However, USBX parses the report descriptor before searching for clients, so this isn't a waste of time. */
155     if (error_code != UX_HOST_CLASS_HID_UNKNOWN)
156     {
157 
158         /* Failed test.  */
159         printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
160         test_control_return(1);
161     }
162 }
163 
164 /* Define what the initial system looks like.  */
165 
166 #ifdef CTEST
test_application_define(void * first_unused_memory)167 void test_application_define(void *first_unused_memory)
168 #else
169 void    usbx_hid_report_descriptor_example_monitor_test_application_define(void *first_unused_memory)
170 #endif
171 {
172 
173 UINT                            status;
174 CHAR *                          stack_pointer;
175 CHAR *                          memory_pointer;
176 UINT                            descriptor_size = HID_REPORT_LENGTH;
177 
178 
179     /* Inform user.  */
180     printf("Running HID Report Descriptor Example monitor Test.................. ");
181 
182     /* Initialize the free memory pointer */
183     stack_pointer = (CHAR *) usbx_memory;
184     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
185 
186     /* Initialize USBX. Memory */
187     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
188 
189     /* Check for error.  */
190     if (status != UX_SUCCESS)
191     {
192 
193         printf("Error on line %d\n", __LINE__);
194         test_control_return(1);
195     }
196 
197     /* Register the error callback. */
198     _ux_utility_error_callback_register(error_callback);
199 
200     /* The code below is required for installing the host portion of USBX */
201     status =  ux_host_stack_initialize(UX_NULL);
202     if (status != UX_SUCCESS)
203     {
204 
205         printf("Error on line %d\n", __LINE__);
206         test_control_return(1);
207     }
208 
209     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
210     if (status != UX_SUCCESS)
211     {
212 
213         printf("Error on line %d\n", __LINE__);
214         test_control_return(1);
215     }
216 
217     /* Register the HID client(s).  */
218     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_keyboard_name, ux_host_class_hid_keyboard_entry);
219     if (status != UX_SUCCESS)
220     {
221 
222         printf("Error on line %d, error code: %d\n", __LINE__, status);
223         test_control_return(1);
224     }
225 
226     /* The code below is required for installing the device portion of USBX. No call back for
227        device status change in this example. */
228     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
229                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
230                                        string_framework, STRING_FRAMEWORK_LENGTH,
231                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
232     if(status!=UX_SUCCESS)
233     {
234 
235         printf("Error on line %d\n", __LINE__);
236         test_control_return(1);
237     }
238 
239     /* Initialize the hid class parameters.  */
240     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
241     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
242     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
243 
244     /* Initilize the device hid class. The class is connected with interface 2 */
245     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
246                                                 1,2, (VOID *)&hid_parameter);
247     if(status!=UX_SUCCESS)
248     {
249 
250         printf("Error on line %d\n", __LINE__);
251         test_control_return(1);
252     }
253 
254 
255     /* Initialize the simulated device controller.  */
256     status =  _ux_dcd_sim_slave_initialize();
257 
258     /* Check for error.  */
259     if (status != UX_SUCCESS)
260     {
261 
262         printf("Error on line %d\n", __LINE__);
263         test_control_return(1);
264     }
265 
266     /* Register all the USB host controllers available in this system */
267     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
268 
269     /* Check for error.  */
270     if (status != UX_SUCCESS)
271     {
272 
273         printf("Error on line %d\n", __LINE__);
274         test_control_return(1);
275     }
276 
277     /* Create the main host simulation thread.  */
278     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
279             stack_pointer, UX_DEMO_STACK_SIZE,
280             20, 20, 1, TX_AUTO_START);
281 
282     /* Check for error.  */
283     if (status != TX_SUCCESS)
284     {
285 
286         printf("Error on line %d\n", __LINE__);
287         test_control_return(1);
288     }
289 }
290 
291 
tx_demo_thread_host_simulation_entry(ULONG arg)292 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
293 {
294 
295 UINT status;
296 UX_HOST_CLASS_HID_REPORT_GET_ID     report_id;
297 
298 
299     /* Find the HID class */
300     status = demo_class_hid_get();
301     if (status != UX_SUCCESS)
302     {
303 
304         printf("Error on line %d. ERROR 0\n", __LINE__);
305         test_control_return(1);
306     }
307 
308     /* Get the input report descriptor. */
309     report_id.ux_host_class_hid_report_get_report = UX_NULL;
310     report_id.ux_host_class_hid_report_get_type = UX_HOST_CLASS_HID_REPORT_TYPE_FEATURE;
311     status = ux_host_class_hid_report_id_get(hid, &report_id);
312     if (status != UX_SUCCESS)
313     {
314 
315         printf("Error on line %d. ERROR 1\n", __LINE__);
316         test_control_return(1);
317     }
318 
319     /* Do minimal error-checking. */
320     if (report_id.ux_host_class_hid_report_get_report == UX_NULL)
321     {
322 
323         printf("Error on line %d. ERROR 2\n", __LINE__);
324         test_control_return(1);
325     }
326 
327     /* Now disconnect the device.  */
328     _ux_device_stack_disconnect();
329 
330     /* And deinitialize the class.  */
331     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
332 
333     /* Deinitialize the device side of usbx.  */
334     _ux_device_stack_uninitialize();
335 
336     /* And finally the usbx system resources.  */
337     _ux_system_uninitialize();
338 
339     /* Successful test.  */
340     printf("SUCCESS!\n");
341     test_control_return(0);
342 }
343 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)344 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
345 {
346     return(UX_SUCCESS);
347 }
348