1 /* This test is designed to test the extraction of a mouse device.  */
2 
3 #include "usbx_test_common_hid.h"
4 #include "ux_host_class_hid_mouse.h"
5 
6 
7 static UX_HOST_CLASS_HID_MOUSE             *mouse;
8 
9 
10 static UCHAR hid_mouse_report[] = {
11 
12     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
13     0x09, 0x02,                    // USAGE (Mouse)
14     0xa1, 0x01,                    // COLLECTION (Application)
15     0x09, 0x01,                    //   USAGE (Pointer)
16     0xa1, 0x00,                    //   COLLECTION (Physical)
17     0x05, 0x09,                    //     USAGE_PAGE (Button)
18     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
19     0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)
20     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
21     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
22     0x95, 0x03,                    //     REPORT_COUNT (3)
23     0x75, 0x01,                    //     REPORT_SIZE (1)
24     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
25     0x95, 0x01,                    //     REPORT_COUNT (1)
26     0x75, 0x05,                    //     REPORT_SIZE (5)
27     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
28     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
29     0x09, 0x30,                    //     USAGE (X)
30     0x09, 0x31,                    //     USAGE (Y)
31     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
32     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
33     0x75, 0x08,                    //     REPORT_SIZE (8)
34     0x95, 0x02,                    //     REPORT_COUNT (2)
35     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
36     0x09, 0x38,                    //     USAGE (Mouse Wheel)
37     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
38     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
39     0x75, 0x08,                    //     REPORT_SIZE (8)
40     0x95, 0x01,                    //     REPORT_COUNT (1)
41     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
42     0xc0,                          //   END_COLLECTION
43     0xc0                           // END_COLLECTION
44 };
45 #define HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0]))
46 
47 
48 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
49 static UCHAR device_framework_full_speed[] = {
50 
51     /* Device descriptor */
52         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
53         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
54         0x00, 0x01,
55 
56     /* Configuration descriptor */
57         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
58         0x32,
59 
60     /* Interface descriptor */
61         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
62         0x00,
63 
64     /* HID descriptor */
65         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),
66         MSB(HID_MOUSE_REPORT_LENGTH),
67 
68     /* Endpoint descriptor (Interrupt) */
69         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
70 
71     };
72 
73 
74 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
75 static UCHAR device_framework_high_speed[] = {
76 
77     /* Device descriptor */
78         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
79         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
80         0x03, 0x01,
81 
82     /* Device qualifier descriptor */
83         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
84         0x01, 0x00,
85 
86     /* Configuration descriptor */
87         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
88         0x32,
89 
90     /* Interface descriptor */
91         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
92         0x00,
93 
94     /* HID descriptor */
95         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_MOUSE_REPORT_LENGTH),
96         MSB(HID_MOUSE_REPORT_LENGTH),
97 
98     /* Endpoint descriptor (Interrupt) */
99         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
100 
101     };
102 
103 
104     /* String Device Framework :
105      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
106      Byte 2       : Byte containing the index of the descriptor
107      Byte 3       : Byte containing the length of the descriptor string
108     */
109 
110 #define STRING_FRAMEWORK_LENGTH 40
111 static UCHAR string_framework[] = {
112 
113     /* Manufacturer string descriptor : Index 1 */
114         0x09, 0x04, 0x01, 0x0c,
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 
ux_system_host_change_function(ULONG a,UX_HOST_CLASS * b,VOID * c)143 static UINT ux_system_host_change_function(ULONG a, UX_HOST_CLASS *b, VOID *c)
144 {
145     return 0;
146 }
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     /* Failed test.  */
153     printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
154     test_control_return(1);
155 }
156 
157 /* Define what the initial system looks like.  */
158 
159 #ifdef CTEST
test_application_define(void * first_unused_memory)160 void test_application_define(void *first_unused_memory)
161 #else
162 void    usbx_hid_mouse_extraction_test_application_define(void *first_unused_memory)
163 #endif
164 {
165 
166 UINT status;
167 CHAR *                          stack_pointer;
168 CHAR *                          memory_pointer;
169 
170     /* Inform user.  */
171     printf("Running HID Mouse Extraction Test................................... ");
172 
173     /* Initialize the free memory pointer */
174     stack_pointer = (CHAR *) usbx_memory;
175     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
176 
177     /* Initialize USBX. Memory */
178     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
179 
180     /* Check for error.  */
181     if (status != UX_SUCCESS)
182     {
183 
184         printf("Error on line %d\n", __LINE__);
185         test_control_return(1);
186     }
187 
188     /* Register the error callback. */
189     _ux_utility_error_callback_register(error_callback);
190 
191     /* The code below is required for installing the host portion of USBX */
192     status =  ux_host_stack_initialize(ux_system_host_change_function);
193     if (status != UX_SUCCESS)
194     {
195 
196         printf("Error on line %d\n", __LINE__);
197         test_control_return(1);
198     }
199 
200     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
201     if (status != UX_SUCCESS)
202     {
203 
204         printf("Error on line %d\n", __LINE__);
205         test_control_return(1);
206     }
207 
208     /* Register the HID client(s).  */
209     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_mouse_name, ux_host_class_hid_mouse_entry);
210     if (status != UX_SUCCESS)
211     {
212 
213         printf("Error on line %d\n", __LINE__);
214         test_control_return(1);
215     }
216 
217     /* The code below is required for installing the device portion of USBX. No call back for
218        device status change in this example. */
219     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
220                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
221                                        string_framework, STRING_FRAMEWORK_LENGTH,
222                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
223     if(status!=UX_SUCCESS)
224     {
225 
226         printf("Error on line %d\n", __LINE__);
227         test_control_return(1);
228     }
229 
230     /* Initialize the hid class parameters for a mouse.  */
231     hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
232     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_MOUSE_REPORT_LENGTH;
233     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
234 
235     /* Initilize the device hid class. The class is connected with interface 2 */
236     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
237                                                 1,2, (VOID *)&hid_parameter);
238     if(status!=UX_SUCCESS)
239     {
240 
241         printf("Error on line %d\n", __LINE__);
242         test_control_return(1);
243     }
244 
245 
246     /* Initialize the simulated device controller.  */
247     status =  _ux_dcd_sim_slave_initialize();
248 
249     /* Check for error.  */
250     if (status != UX_SUCCESS)
251     {
252 
253         printf("Error on line %d\n", __LINE__);
254         test_control_return(1);
255     }
256 
257     /* Register all the USB host controllers available in this system */
258     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
259 
260     /* Check for error.  */
261     if (status != UX_SUCCESS)
262     {
263 
264         printf("Error on line %d\n", __LINE__);
265         test_control_return(1);
266     }
267 
268     /* Create the main host simulation thread.  */
269     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
270             stack_pointer, UX_DEMO_STACK_SIZE,
271             20, 20, 1, TX_AUTO_START);
272 
273     /* Check for error.  */
274     if (status != TX_SUCCESS)
275     {
276 
277         printf("Error on line %d\n", __LINE__);
278         test_control_return(1);
279     }
280 
281 }
282 
ux_hcd_sim_host_entry_filter_device_extraction_test(UX_HCD * hcd,UINT function,VOID * parameter)283 static UINT ux_hcd_sim_host_entry_filter_device_extraction_test(UX_HCD *hcd, UINT function, VOID *parameter)
284 {
285 
286 UINT status;
287 
288     switch(function)
289     {
290     case UX_HCD_GET_PORT_STATUS:
291 
292         /* No device on this port. */
293         status = 0;
294 
295         /* This is a Full speed device.  */
296         status |=  UX_PS_DS_FS;
297 
298         break;
299 
300 
301     default:
302 
303         status = _ux_hcd_sim_host_entry(hcd, function, parameter);
304         break;
305     }
306 
307     return status;
308 }
309 
tx_demo_thread_host_simulation_entry(ULONG arg)310 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
311 {
312 
313 UINT        status;
314 UX_HCD     *hcd;
315 
316     /* Find the HID class */
317     status =  demo_class_hid_get();
318     if (status != UX_SUCCESS)
319     {
320 
321         printf("Error on line %d\n", __LINE__);
322         test_control_return(1);
323     }
324 
325     /* Get the HID client */
326     hid_client = hid -> ux_host_class_hid_client;
327 
328     /* Check if the instance of the keyboard is live */
329     while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
330         tx_thread_sleep(10);
331 
332     /* Get the mouse instance */
333     mouse =  (UX_HOST_CLASS_HID_MOUSE *)hid_client -> ux_host_class_hid_client_local_instance;
334 
335     hcd = UX_DEVICE_HCD_GET(hid -> ux_host_class_hid_device);
336 
337     /* Change the HCD entry function to our filter function. */
338     hcd -> ux_hcd_entry_function = ux_hcd_sim_host_entry_filter_device_extraction_test;
339 
340     /* Signal port activity to the enum thread.  */
341     hcd -> ux_hcd_root_hub_signal[0] =  1;
342 
343     /* Signal enum thread.  */
344     _ux_utility_semaphore_put(&_ux_system_host -> ux_system_host_enum_semaphore);
345 
346     /* Wait for hid class to shut down. */
347     while(hid -> ux_host_class_hid_state == UX_HOST_CLASS_INSTANCE_LIVE)
348         tx_thread_sleep(10);
349 
350     /* Now disconnect the device.  */
351     _ux_device_stack_disconnect();
352 
353     /* And deinitialize the class.  */
354     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
355 
356     /* Deinitialize the device side of usbx.  */
357     _ux_device_stack_uninitialize();
358 
359     /* And finally the usbx system resources.  */
360     _ux_system_uninitialize();
361 
362     /* Successful test.  */
363     printf("SUCCESS!\n");
364     test_control_return(0);
365 }
366 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)367 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
368 {
369     return(UX_SUCCESS);
370 }
371