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