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 
8 static UINT device_removed;
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     0xc0                           // END_COLLECTION
44 };
45 #define HID_REPORT_LENGTH sizeof(hid_report_descriptor)/sizeof(hid_report_descriptor[0])
46 
47 
48 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED 52
49 static UCHAR device_framework_full_speed[DEVICE_FRAMEWORK_LENGTH_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_REPORT_LENGTH),
66         MSB(HID_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[DEVICE_FRAMEWORK_LENGTH_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_REPORT_LENGTH),
96         MSB(HID_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 
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 
147     if(a == UX_DEVICE_REMOVAL)
148     {
149         device_removed = 1;
150     }
151 
152     return 0;
153 }
154 
error_callback(UINT system_level,UINT system_context,UINT error_code)155 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
156 {
157 
158     if (error_code != UX_HOST_CLASS_HID_UNKNOWN)
159     {
160 
161         /* Something went wrong.  */
162         printf("Error on line %d\n", __LINE__);
163         test_control_return(1);
164     }
165 }
166 
167 /* Define what the initial system looks like.  */
168 
169 #ifdef CTEST
test_application_define(void * first_unused_memory)170 void test_application_define(void *first_unused_memory)
171 #else
172 void    usbx_ux_host_class_hid_deactivate_test_application_define(void *first_unused_memory)
173 #endif
174 {
175 
176 UINT status;
177 CHAR *stack_pointer;
178 CHAR *memory_pointer;
179 
180 
181     /* Inform user.  */
182     printf("Running ux_host_class_hid_deactivate Test........................... ");
183 
184     /* Initialize the free memory pointer */
185     stack_pointer = (CHAR *) usbx_memory;
186     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
187 
188     /* Initialize USBX. Memory */
189     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
190 
191     /* Check for error.  */
192     if (status != UX_SUCCESS)
193     {
194 
195         printf("Error on line %d, error code: %d\n", __LINE__, status);
196         test_control_return(1);
197     }
198 
199     /* Register the error callback. */
200     _ux_utility_error_callback_register(error_callback);
201 
202     /* The code below is required for installing the host portion of USBX */
203     status =  ux_host_stack_initialize(UX_NULL);
204     if (status != UX_SUCCESS)
205     {
206 
207         printf("Error on line %d, error code: %d\n", __LINE__, status);
208         test_control_return(1);
209     }
210 
211     status =  ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
212     if (status != UX_SUCCESS)
213     {
214 
215         printf("Error on line %d, error code: %d\n", __LINE__, status);
216         test_control_return(1);
217     }
218 
219     /* The code below is required for installing the device portion of USBX. No call back for
220        device status change in this example. */
221     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
222                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
223                                        string_framework, STRING_FRAMEWORK_LENGTH,
224                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
225     if(status!=UX_SUCCESS)
226     {
227 
228         printf("Error on line %d, error code: %d\n", __LINE__, status);
229         test_control_return(1);
230     }
231 
232     /* Initialize the hid class parameters.  */
233     hid_parameter.ux_device_class_hid_parameter_report_address = hid_report_descriptor;
234     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_REPORT_LENGTH;
235     hid_parameter.ux_device_class_hid_parameter_callback       = demo_thread_hid_callback;
236 
237     /* Initilize the device hid class. The class is connected with interface 2 */
238     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
239                                                 1,2, (VOID *)&hid_parameter);
240     if(status!=UX_SUCCESS)
241     {
242 
243         printf("Error on line %d, error code: %d\n", __LINE__, status);
244         test_control_return(1);
245     }
246 
247     /* Initialize the simulated device controller.  */
248     status =  _ux_dcd_sim_slave_initialize();
249 
250     /* Check for error.  */
251     if (status != UX_SUCCESS)
252     {
253 
254         printf("Error on line %d, error code: %d\n", __LINE__, status);
255         test_control_return(1);
256     }
257 
258     /* Register all the USB host controllers available in this system */
259     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, ux_hcd_sim_host_initialize,0,0);
260 
261     /* Check for error.  */
262     if (status != UX_SUCCESS)
263     {
264 
265         printf("Error on line %d, error code: %d\n", __LINE__, status);
266         test_control_return(1);
267     }
268 
269     /* Create the main host simulation thread.  */
270     status =  tx_thread_create(&tx_demo_thread_host_simulation, "tx demo host simulation", tx_demo_thread_host_simulation_entry, 0,
271             stack_pointer, UX_DEMO_STACK_SIZE,
272             20, 20, 1, TX_AUTO_START);
273 
274     /* Check for error.  */
275     if (status != TX_SUCCESS)
276     {
277 
278         printf("Error on line %d, error code: %d\n", __LINE__, status);
279         test_control_return(1);
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 
demo_class_hid_check(void)310 static UINT  demo_class_hid_check(void)
311 {
312 
313 UINT status;
314 UX_HOST_CLASS       *class;
315 
316     /* Find the main HID container */
317     status =  ux_host_stack_class_get(_ux_system_host_class_hid_name, &class);
318     if (status != UX_SUCCESS)
319         return(status);
320 
321     /* We get the first instance of the hid device */
322     status =  ux_host_stack_class_instance_get(class, 0, (void **) &hid);
323     if (status != UX_SUCCESS)
324         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
325 
326     /* We still need to wait for the hid status to be live */
327     if (hid -> ux_host_class_hid_state != UX_HOST_CLASS_INSTANCE_LIVE)
328         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
329 
330     return(UX_SUCCESS);
331 }
332 
tx_demo_thread_host_simulation_entry(ULONG arg)333 static void  tx_demo_thread_host_simulation_entry(ULONG arg)
334 {
335 
336 UINT        status;
337 UX_HCD     *hcd;
338 
339     /* Find the HID class */
340     do {
341         status = demo_class_hid_get();
342     } while(status == UX_HOST_CLASS_INSTANCE_UNKNOWN);
343     if (status != UX_SUCCESS)
344     {
345 
346         printf("Error on line %d, error code: %d\n", __LINE__, status);
347         test_control_return(1);
348     }
349 
350     /**************************************************/
351     /** Test case: if (hid -> ux_host_class_hid_client != UX_NULL) failure **/
352     /** How: we do this by not registering any hid clients. **
353     /**************************************************/
354 
355     hcd = UX_DEVICE_HCD_GET(hid -> ux_host_class_hid_device);
356 
357     /* Change the HCD entry function to our filter function. */
358     hcd -> ux_hcd_entry_function = ux_hcd_sim_host_entry_filter_device_extraction_test;
359 
360     /* Signal port activity to the enum thread.  */
361     hcd -> ux_hcd_root_hub_signal[0] =  1;
362 
363     /* Signal enum thread.  */
364     _ux_utility_semaphore_put(&_ux_system_host -> ux_system_host_enum_semaphore);
365 
366     /* Wait for device removed signal. */
367     while(demo_class_hid_get() == UX_SUCCESS)
368         tx_thread_sleep(10);
369 
370     /* Now disconnect the device.  */
371     _ux_device_stack_disconnect();
372 
373     /* And deinitialize the class.  */
374     status =  ux_device_stack_class_unregister(_ux_system_slave_class_hid_name, ux_device_class_hid_entry);
375 
376     /* Deinitialize the device side of usbx.  */
377     _ux_device_stack_uninitialize();
378 
379     /* And finally the usbx system resources.  */
380     _ux_system_uninitialize();
381 
382     /* Successful test.  */
383     printf("SUCCESS!\n");
384     test_control_return(0);
385 }
386 
demo_thread_hid_callback(UX_SLAVE_CLASS_HID * class,UX_SLAVE_CLASS_HID_EVENT * event)387 static UINT    demo_thread_hid_callback(UX_SLAVE_CLASS_HID *class, UX_SLAVE_CLASS_HID_EVENT *event)
388 {
389     return(UX_SUCCESS);
390 }
391