1 /* This test concentrates on keyboard device removal.
2    This is similar to the first extraction test, except there is no ux_system_host_change_function.
3    This allows us to hit the following false condition in _ux_host_class_hid_keyboard_deactivate:
4    if (_ux_system_host -> ux_system_host_change_function != UX_NULL) */
5 
6 #include "usbx_test_common_hid.h"
7 #include "ux_host_class_hid_keyboard.h"
8 
9 
10 static UCHAR hid_report_descriptor[] = {
11 
12     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
13     0x09, 0x06,                    // USAGE (Keyboard)
14     0xa1, 0x01,                    // COLLECTION (Application)
15     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
16     0x19, 0xe0,                    //   USAGE_MINIMUM (Keyboard LeftControl)
17     0x29, 0xe7,                    //   USAGE_MAXIMUM (Keyboard Right GUI)
18     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
19     0x25, 0x01,                    //   LOGICAL_MAXIMUM (1)
20     0x75, 0x01,                    //   REPORT_SIZE (1)
21     0x95, 0x08,                    //   REPORT_COUNT (8)
22     0x81, 0x02,                    //   INPUT (Data,Var,Abs)
23     0x95, 0x01,                    //   REPORT_COUNT (1)
24     0x75, 0x08,                    //   REPORT_SIZE (8)
25     0x81, 0x03,                    //   INPUT (Cnst,Var,Abs)
26     0x95, 0x05,                    //   REPORT_COUNT (5)
27     0x75, 0x01,                    //   REPORT_SIZE (1)
28     0x05, 0x08,                    //   USAGE_PAGE (LEDs)
29     0x19, 0x01,                    //   USAGE_MINIMUM (Num Lock)
30     0x29, 0x05,                    //   USAGE_MAXIMUM (Kana)
31     0x91, 0x02,                    //   OUTPUT (Data,Var,Abs)
32     0x95, 0x01,                    //   REPORT_COUNT (1)
33     0x75, 0x03,                    //   REPORT_SIZE (3)
34     0x91, 0x03,                    //   OUTPUT (Cnst,Var,Abs)
35     0x95, 0x06,                    //   REPORT_COUNT (6)
36     0x75, 0x08,                    //   REPORT_SIZE (8)
37     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
38     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
39     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
40     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
41     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
42     0x81, 0x00,                    //   INPUT (Data,Ary,Abs)
43                                    /*   Add a feature report to hit some code in _ux_host_class_hid_instance_clean(). */
44     0x95, 0x06,                    //   REPORT_COUNT (6)
45     0x75, 0x08,                    //   REPORT_SIZE (8)
46     0x15, 0x00,                    //   LOGICAL_MINIMUM (0)
47     0x25, 0x65,                    //   LOGICAL_MAXIMUM (101)
48     0x05, 0x07,                    //   USAGE_PAGE (Keyboard)
49     0x19, 0x00,                    //   USAGE_MINIMUM (Reserved (no event indicated))
50     0x29, 0x65,                    //   USAGE_MAXIMUM (Keyboard Application)
51     0xb1, 0x00,                    //   FEATURE (Data,Ary,Abs)
52     0xc0                           // END_COLLECTION
53 };
54 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
55 
56 
57 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
58 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_FULL_SPEED] = {
59 
60     /* Device descriptor */
61         0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
62         0x81, 0x0A, 0x01, 0x01, 0x00, 0x00, 0x00, 0x00,
63         0x00, 0x01,
64 
65     /* Configuration descriptor */
66         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
67         0x32,
68 
69     /* Interface descriptor */
70         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
71         0x00,
72 
73     /* HID descriptor */
74         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
75         MSB(HID_REPORT_LENGTH),
76 
77     /* Endpoint descriptor (Interrupt) */
78         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
79 
80     };
81 
82 
83 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED 62
84 static UCHAR device_framework_high_speed[DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED] = {
85 
86     /* Device descriptor */
87         0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
88         0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
89         0x03, 0x01,
90 
91     /* Device qualifier descriptor */
92         0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
93         0x01, 0x00,
94 
95     /* Configuration descriptor */
96         0x09, 0x02, 0x22, 0x00, 0x01, 0x01, 0x00, 0xc0,
97         0x32,
98 
99     /* Interface descriptor */
100         0x09, 0x04, 0x02, 0x00, 0x01, 0x03, 0x00, 0x00,
101         0x00,
102 
103     /* HID descriptor */
104         0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22, LSB(HID_REPORT_LENGTH),
105         MSB(HID_REPORT_LENGTH),
106 
107     /* Endpoint descriptor (Interrupt) */
108         0x07, 0x05, 0x82, 0x03, 0x08, 0x00, 0x08
109 
110     };
111 
112 
113     /* String Device Framework :
114      Byte 0 and 1 : Word containing the language ID : 0x0904 for US
115      Byte 2       : Byte containing the index of the descriptor
116      Byte 3       : Byte containing the length of the descriptor string
117     */
118 
119 #define STRING_FRAMEWORK_LENGTH 40
120 static UCHAR string_framework[] = {
121 
122     /* Manufacturer string descriptor : Index 1 */
123         0x09, 0x04, 0x01, 0x0c,
124         0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
125         0x6f, 0x67, 0x69, 0x63,
126 
127     /* Product string descriptor : Index 2 */
128         0x09, 0x04, 0x02, 0x0c,
129         0x55, 0x53, 0x42, 0x20, 0x4b, 0x65, 0x79, 0x62,
130         0x6f, 0x61, 0x72, 0x64,
131 
132     /* Serial Number string descriptor : Index 3 */
133         0x09, 0x04, 0x03, 0x04,
134         0x30, 0x30, 0x30, 0x31
135     };
136 
137 
138     /* Multiple languages are supported on the device, to add
139        a language besides english, the unicode language code must
140        be appended to the language_id_framework array and the length
141        adjusted accordingly. */
142 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
143 static UCHAR language_id_framework[] = {
144 
145     /* English. */
146         0x09, 0x04
147     };
148 
149 
150 UINT  _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
151 
152 
error_callback(UINT system_level,UINT system_context,UINT error_code)153 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
154 {
155 
156     /* Failed test.  */
157     printf("Error on line %d, system_level: %d, system_context: %d, error code: %d\n", __LINE__, system_level, system_context, error_code);
158     test_control_return(1);
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_hid_keyboard_extraction_test2_application_define(void *first_unused_memory)
167 #endif
168 {
169 
170 UINT status;
171 CHAR *                          stack_pointer;
172 CHAR *                          memory_pointer;
173 UINT                            descriptor_size = HID_REPORT_LENGTH;
174 
175     /* Inform user.  */
176     printf("Running HID Device Removal Keyboard Test2........................... ");
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\n", __LINE__);
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(NULL);
198     if (status != UX_SUCCESS)
199     {
200 
201         printf("Error on line %d\n", __LINE__);
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\n", __LINE__);
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\n", __LINE__);
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\n", __LINE__);
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\n", __LINE__);
247         test_control_return(1);
248     }
249 
250 
251     /* Initialize the simulated device controller.  */
252     status =  _ux_dcd_sim_slave_initialize();
253 
254     /* Check for error.  */
255     if (status != UX_SUCCESS)
256     {
257 
258         printf("Error on line %d\n", __LINE__);
259         test_control_return(1);
260     }
261 
262     /* Register all the USB host controllers available in this system */
263     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
264 
265     /* Check for error.  */
266     if (status != UX_SUCCESS)
267     {
268 
269         printf("Error on line %d\n", __LINE__);
270         test_control_return(1);
271     }
272 
273     /* Create the main host simulation thread.  */
274     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
275             stack_pointer, UX_DEMO_STACK_SIZE,
276             20, 20, 1, TX_AUTO_START);
277 
278     /* Check for error.  */
279     if (status != TX_SUCCESS)
280     {
281 
282         printf("Error on line %d\n", __LINE__);
283         test_control_return(1);
284     }
285 }
286 
ux_hcd_sim_host_entry_filter_device_extraction_test(UX_HCD * hcd,UINT function,VOID * parameter)287 static UINT ux_hcd_sim_host_entry_filter_device_extraction_test(UX_HCD *hcd, UINT function, VOID *parameter)
288 {
289 
290 UINT status;
291 
292     switch(function)
293     {
294     case UX_HCD_GET_PORT_STATUS:
295 
296         /* No device on this port. */
297         status = 0;
298 
299         /* This is a Full speed device.  */
300         status |=  UX_PS_DS_FS;
301 
302         break;
303 
304 
305     default:
306 
307         status = _ux_hcd_sim_host_entry(hcd, function, parameter);
308         break;
309     }
310 
311     return status;
312 }
313 
tx_demo_thread_host_simulation_entry(ULONG arg)314 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
315 {
316 
317 UINT        status;
318 UX_HCD     *hcd;
319 
320     /* Find the HID class */
321     status = demo_class_hid_get();
322     if (status != UX_SUCCESS)
323     {
324 
325         printf("Error on line %d\n", __LINE__);
326         test_control_return(1);
327     }
328 
329     /* Get the HID client */
330     hid_client = hid -> ux_host_class_hid_client;
331 
332     /* Check if the instance of the keyboard is live */
333     while (hid_client -> ux_host_class_hid_client_local_instance == UX_NULL)
334         tx_thread_sleep(10);
335 
336     hcd = UX_DEVICE_HCD_GET(hid -> ux_host_class_hid_device);
337 
338     /* Change the HCD entry function to our filter function. */
339     hcd -> ux_hcd_entry_function = ux_hcd_sim_host_entry_filter_device_extraction_test;
340 
341     /* Signal port activity to the enum thread.  */
342     hcd -> ux_hcd_root_hub_signal[0] =  1;
343 
344     /* Signal enum thread.  */
345     _ux_utility_semaphore_put(&_ux_system_host -> ux_system_host_enum_semaphore);
346 
347     /* Wait for hid class to shut down. */
348     while(hid -> ux_host_class_hid_state == UX_HOST_CLASS_INSTANCE_LIVE)
349         tx_thread_sleep(10);
350 
351     /* Now disconnect the device.  */
352     _ux_device_stack_disconnect();
353 
354     /* And deinitialize the class.  */
355     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
356 
357     /* Deinitialize the device side of usbx.  */
358     _ux_device_stack_uninitialize();
359 
360     /* And finally the usbx system resources.  */
361     _ux_system_uninitialize();
362 
363     /* Successful test.  */
364     printf("SUCCESS!\n");
365     test_control_return(0);
366 }
367 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)368 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
369 {
370     return(UX_SUCCESS);
371 }
372