1 /* This test is designed to test the simple dpump host/device class operation.  */
2 
3 #include <stdio.h>
4 #include "tx_api.h"
5 #include "ux_api.h"
6 #include "ux_system.h"
7 #include "ux_utility.h"
8 
9 #include "fx_api.h"
10 
11 #include "ux_device_stack.h"
12 #include "ux_device_class_dummy.h"
13 
14 #include "ux_host_stack.h"
15 
16 #include "ux_test_dcd_sim_slave.h"
17 #include "ux_test_hcd_sim_host.h"
18 #include "ux_test_utility_sim.h"
19 
20 /* Define constants.  */
21 #define                             UX_DEMO_DEBUG_SIZE  (4096*8)
22 #define                             UX_DEMO_STACK_SIZE  1024
23 #define                             UX_DEMO_BUFFER_SIZE (UX_SLAVE_REQUEST_DATA_MAX_LENGTH + 1)
24 #define                             UX_DEMO_XMIT_BUFFER_SIZE 512
25 #define                             UX_DEMO_RECEPTION_BUFFER_SIZE 512
26 #define                             UX_DEMO_FILE_BUFFER_SIZE 512
27 #define                             UX_DEMO_RECEPTION_BLOCK_SIZE 64
28 #define                             UX_DEMO_MEMORY_SIZE     (96*1024)
29 #define                             UX_DEMO_FILE_SIZE       (128 * 1024)
30 #define                             UX_RAM_DISK_MEMORY      (256 * 1024)
31 
32 #define                             UX_DEMO_VENDOR_REQUEST 0x54
33 
34 /* Define local/extern function prototypes.  */
35 static VOID                                test_thread_entry(ULONG);
36 static TX_THREAD                           tx_test_thread_simulation0;
37 static TX_THREAD                           tx_test_thread_simulation1;
38 static VOID                                tx_test_thread_simulation0_entry(ULONG);
39 static VOID                                tx_test_thread_simulation1_entry(ULONG);
40 
41 static UINT                                test_ms_vendor_request(ULONG request, ULONG request_value, ULONG request_index, ULONG request_length,
42                                                                   UCHAR *transfer_request_buffer, ULONG *transfer_request_length);
43 
44 
45 /* Define global data structures.  */
46 static UCHAR                               usbx_memory[UX_DEMO_MEMORY_SIZE + (UX_DEMO_STACK_SIZE * 2)];
47 
48 static UX_DEVICE_CLASS_DUMMY_PARAMETER     *parameter;
49 
50 static UX_DEVICE                           *host_device = UX_NULL;
51 
52 static UCHAR                               buffer[UX_DEMO_BUFFER_SIZE];
53 
54 static ULONG                               error_counter;
55 
56 static ULONG                               set_cfg_counter;
57 
58 static ULONG                               rsc_mem_alloc_cnt_on_set_cfg;
59 static ULONG                               rsc_sem_on_set_cfg;
60 static ULONG                               rsc_sem_get_on_set_cfg;
61 static ULONG                               rsc_mutex_on_set_cfg;
62 
63 static ULONG                               rsc_enum_mem_alloc_count;
64 static ULONG                               rsc_enum_sem_usage;
65 static ULONG                               rsc_enum_sem_get_count;
66 static ULONG                               rsc_enum_mutex_usage;
67 
68 static UINT                                class_entry_rc = UX_SUCCESS;
69 
70 static UINT                                vendor_req_call_count = 0;
71 static UINT                                vendor_req_rc = UX_SUCCESS;
72 static UINT                                vendor_req_ret_len = 0;
73 static ULONG                               vendor_req_req_len;
74 static ULONG                               vendor_req_buf_len;
75 
76 #define     LSB(x) ((x) & 0x00ff)
77 #define     MSB(x) (((x) & 0xff00) >> 8)
78 
79 /* Storage related descriptors 9+7+7=23 bytes */
80 #define MS_IFC_DESC_ALL(ifc, bulk_in_epa, bulk_out_epa) \
81     /* Interface descriptor */\
82         0x09, 0x04, (ifc), 0x00, 0x03, 0x08, 0x06, 0x50, 0x00,\
83     /* Endpoint descriptor (Bulk In) */\
84         0x07, 0x05, (bulk_in_epa), 0x02, 0x40, 0x00, 0x00,\
85     /* Endpoint descriptor (Bulk Out) */\
86         0x07, 0x05, (bulk_out_epa), 0x02, 0x40, 0x00, 0x00,
87 #define MS_IFC_DESC_ALL_LEN 23
88 
89 /* CDC IAD 8 bytes */
90 #define CDC_IAD_DESC(comm_ifc) \
91     /* Interface association descriptor. 8 bytes.  */\
92     0x08, 0x0b, (comm_ifc), 0x02, 0x02, 0x02, 0x00, 0x00,
93 #define CDC_IAD_DESC_LEN 8
94 
95 /* CDC Communication interface descriptors 9+5+4+5+5+7=35 bytes */
96 #define CDC_COMM_IFC_DESC_ALL(comm_ifc, data_ifc, interrupt_epa) \
97     /* Communication Class Interface Descriptor. 9 bytes. */\
98     0x09, 0x04, (comm_ifc), 0x00, 0x01, 0x02, 0x02, 0x01, 0x00,\
99     /* Header Functional Descriptor 5 bytes */\
100     0x05, 0x24, 0x00, 0x10, 0x01,\
101     /* ACM Functional Descriptor 4 bytes */\
102     0x04, 0x24, 0x02, 0x0f,\
103     /* Union Functional Descriptor 5 bytes */\
104     0x05, 0x24, 0x06,\
105     (comm_ifc),                          /* Master interface */\
106     (data_ifc),                          /* Slave interface  */\
107     /* Call Management Functional Descriptor 5 bytes */\
108     0x05, 0x24, 0x01, 0x03,\
109     (data_ifc),                          /* Data interface   */\
110     /* Endpoint 0x83 descriptor 7 bytes */\
111     0x07, 0x05, (interrupt_epa),\
112     0x03,\
113     0x08, 0x00,\
114     0xFF,
115 #define CDC_COMM_IFC_DESC_ALL_LEN 35
116 
117 /* CDC Data interface descriptors 9+7+7=23 bytes */
118 #define CDC_DATA_IFC_DESC_ALL(ifc, bulk_in_epa, bulk_out_epa) \
119     /* Data Class Interface Descriptor Requirement 9 bytes */\
120     0x09, 0x04, (ifc),\
121     0x00, /* bAlternateSetting */\
122     0x02, /* bNumEndpoints */\
123     0x0A, 0x00, 0x00,\
124     0x00,\
125     /* Endpoint bulk IN descriptor 7 bytes */\
126     0x07, 0x05, (bulk_in_epa),\
127     0x02,\
128     0x40, 0x00,\
129     0x00,\
130     /* Endpoint bulk OUT descriptor 7 bytes */\
131     0x07, 0x05, (bulk_out_epa),\
132     0x02,\
133     0x40, 0x00,\
134     0x00,
135 #define CDC_DATA_IFC_DESC_ALL_LEN 23
136 
137 /* Configuration descriptor 9 bytes */
138 #define CFG_DESC(wTotalLength, bNumInterfaces, bConfigurationValue)\
139     /* Configuration 1 descriptor 9 bytes */\
140     0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength),\
141     (bNumInterfaces), (bConfigurationValue), 0x00,\
142     0x40, 0x00,
143 #define CFG_DESC_LEN 9
144 
145 /* Define device framework.  */
146 
147 static unsigned char device_framework_full_speed[] = {
148 
149     /* Device descriptor     18 bytes
150        0x02 bDeviceClass:    CDC class code
151        0x00 bDeviceSubclass: CDC class sub code
152        0x00 bDeviceProtocol: CDC Device protocol
153 
154        idVendor & idProduct - http://www.linux-usb.org/usb.ids
155     */
156     0x12, 0x01, 0x10, 0x01,
157     0xEF, 0x02, 0x01,
158     0x08,
159     0x84, 0x84, 0x00, 0x00,
160     0x00, 0x01,
161     0x01, 0x02, 03,
162     0x01, /* bNumConfigurations */
163 
164     /* Configuration 1 descriptor 9 bytes */
165     0x09, 0x02, 0x4b, 0x00,
166     0x02, 0x01, 0x00,
167     0xE0, 0x00,
168 
169     /* Interface association descriptor. 8 bytes.  */
170     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
171 
172     /* Communication Class Interface Descriptor. 9 bytes.   */
173     0x09, 0x04, 0x00,
174     0x00,
175     0x01,
176     0x02, 0x02, 0x01,
177     0x00,
178 
179     /* Header Functional Descriptor 5 bytes */
180     0x05, 0x24, 0x00,
181     0x10, 0x01,
182 
183     /* ACM Functional Descriptor 4 bytes */
184     0x04, 0x24, 0x02,
185     0x0f,
186 
187     /* Union Functional Descriptor 5 bytes */
188     0x05, 0x24, 0x06,
189     0x00,                          /* Master interface */
190     0x01,                          /* Slave interface  */
191 
192     /* Call Management Functional Descriptor 5 bytes */
193     0x05, 0x24, 0x01,
194     0x03,
195     0x01,                          /* Data interface   */
196 
197     /* Endpoint 0x83 descriptor 7 bytes */
198     0x07, 0x05, 0x83,
199     0x03,
200     0x08, 0x00,
201     0xFF,
202 
203     /* Data Class Interface Descriptor Requirement 9 bytes */
204     0x09, 0x04, 0x01,
205     0x00,
206     0x02,
207     0x0A, 0x00, 0x00,
208     0x00,
209 
210     /* Endpoint 0x81 descriptor 7 bytes */
211     0x07, 0x05, 0x81,
212     0x02,
213     0x40, 0x00,
214     0x00,
215 
216     /* Endpoint 0x02 descriptor 7 bytes */
217     0x07, 0x05, 0x02,
218     0x02,
219     0x40, 0x00,
220     0x00,
221 
222     /* Configuration 2 descriptor 9 bytes */
223     0x09, 0x02, (0x4b + 28 + 46), 0x00,
224     0x02, 0x02, 0x00,
225     0x40, 0x00,
226 
227     /* Interface association descriptor. 8 bytes.  */
228     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
229 
230     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
231     0x09, 0x04, 0x00,
232     0x00, /* bAlternateSetting */
233     0x00, /* bNumEndpoints */
234     0x02, 0x02, 0x01,
235     0x00,
236 
237     /* Header Functional Descriptor 5 bytes */
238     0x05, 0x24, 0x00,
239     0x10, 0x01,
240 
241     /* ACM Functional Descriptor 4 bytes */
242     0x04, 0x24, 0x02,
243     0x0f,
244 
245     /* Union Functional Descriptor 5 bytes */
246     0x05, 0x24, 0x06,
247     0x00,                          /* Master interface */
248     0x01,                          /* Slave interface  */
249 
250     /* Call Management Functional Descriptor 5 bytes */
251     0x05, 0x24, 0x01,
252     0x03,
253     0x01,                          /* Data interface   */
254 
255     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
256     0x09, 0x04, 0x00,
257     0x01, /* bAlternateSetting */
258     0x01, /* bNumEndpoints */
259     0x02, 0x02, 0x01,
260     0x00,
261 
262     /* Header Functional Descriptor 5 bytes */
263     0x05, 0x24, 0x00,
264     0x10, 0x01,
265 
266     /* ACM Functional Descriptor 4 bytes */
267     0x04, 0x24, 0x02,
268     0x0f,
269 
270     /* Union Functional Descriptor 5 bytes */
271     0x05, 0x24, 0x06,
272     0x00,                          /* Master interface */
273     0x01,                          /* Slave interface  */
274 
275     /* Call Management Functional Descriptor 5 bytes */
276     0x05, 0x24, 0x01,
277     0x03,
278     0x01,                          /* Data interface   */
279 
280     /* Endpoint 0x83 descriptor 7 bytes */
281     0x07, 0x05, 0x83,
282     0x03,
283     0x08, 0x00,
284     0xFF,
285 
286     /* Data Class Interface Descriptor Requirement 9 bytes */
287     0x09, 0x04, 0x01,
288     0x00, /* bAlternateSetting */
289     0x00, /* bNumEndpoints */
290     0x0A, 0x00, 0x00,
291     0x00,
292 
293     /* Data Class Interface Descriptor Requirement 9 bytes */
294     0x09, 0x04, 0x01,
295     0x01, /* bAlternateSetting */
296     0x02, /* bNumEndpoints */
297     0x0A, 0x00, 0x00,
298     0x00,
299 
300     /* Endpoint 0x81 descriptor 7 bytes */
301     0x07, 0x05, 0x81,
302     0x02,
303     0x40, 0x00,
304     0x00,
305 
306     /* Endpoint 0x02 descriptor 7 bytes */
307     0x07, 0x05, 0x02,
308     0x02,
309     0x40, 0x00,
310     0x00,
311 
312     /* Data Class Interface Descriptor Requirement 9 bytes */
313     0x09, 0x04, 0x01,
314     0x02, /* bAlternateSetting */
315     0x04, /* bNumEndpoints */
316     0x0A, 0x00, 0x00,
317     0x00,
318 
319     /* Endpoint 0x81 descriptor 7 bytes */
320     0x07, 0x05, 0x81,
321     0x02,
322     0x40, 0x00,
323     0x00,
324 
325     /* Endpoint 0x02 descriptor 7 bytes */
326     0x07, 0x05, 0x02,
327     0x02,
328     0x40, 0x00,
329     0x00,
330 
331     /* Endpoint 0x85 descriptor 7 bytes */
332     0x07, 0x05, 0x85,
333     0x02,
334     0x40, 0x00,
335     0x00,
336 
337     /* Endpoint 0x04 descriptor 7 bytes */
338     0x07, 0x05, 0x04,
339     0x02,
340     0x40, 0x00,
341     0x00,
342 
343     /* Configuration 3: CDC + CDC */
344     CFG_DESC(CFG_DESC_LEN+2*(CDC_IAD_DESC_LEN+CDC_COMM_IFC_DESC_ALL_LEN+CDC_DATA_IFC_DESC_ALL_LEN), 4, 3)
345     CDC_IAD_DESC(0)
346     CDC_COMM_IFC_DESC_ALL(0, 1, 0x83)
347     CDC_DATA_IFC_DESC_ALL(1, 0x81, 0x02)
348     CDC_IAD_DESC(2)
349     CDC_COMM_IFC_DESC_ALL(2, 3, 0x86)
350     CDC_DATA_IFC_DESC_ALL(3, 0x84, 0x05)
351 };
352 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
353 
354 static unsigned char device_framework_high_speed[] = {
355 
356     /* Device descriptor
357        0x02 bDeviceClass:    CDC class code
358        0x00 bDeviceSubclass: CDC class sub code
359        0x00 bDeviceProtocol: CDC Device protocol
360 
361        idVendor & idProduct - http://www.linux-usb.org/usb.ids
362     */
363     0x12, 0x01, 0x00, 0x02,
364     0xEF, 0x02, 0x01,
365     0x40,
366     0x84, 0x84, 0x00, 0x00,
367     0x00, 0x01,
368     0x01, 0x02, 03,
369     0x01, /* bNumConfigurations */
370 
371     /* Device qualifier descriptor */
372     0x0a, 0x06, 0x00, 0x02,
373     0x02, 0x00, 0x00,
374     0x40,
375     0x01,
376     0x00,
377 
378     /* Configuration 1 descriptor */
379     0x09, 0x02, (0x4b+5), 0x00,
380     0x02, 0x01, 0x00,
381     0xE0, 0x00,
382 
383     /* OTG Descriptor.  */
384     0x05, 0x09, 0x03, 0x02, 0x00,
385 
386     /* Interface association descriptor. */
387     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
388 
389     /* Communication Class Interface Descriptor Requirement */
390     0x09, 0x04, 0x00,
391     0x00,
392     0x01,
393     0x02, 0x02, 0x01,
394     0x00,
395 
396     /* Header Functional Descriptor */
397     0x05, 0x24, 0x00,
398     0x10, 0x01,
399 
400     /* ACM Functional Descriptor */
401     0x04, 0x24, 0x02,
402     0x0f,
403 
404     /* Union Functional Descriptor */
405     0x05, 0x24, 0x06,
406     0x00,
407     0x01,
408 
409     /* Call Management Functional Descriptor */
410     0x05, 0x24, 0x01,
411     0x00,
412     0x01,
413 
414     /* Endpoint 0x83 descriptor */
415     0x07, 0x05, 0x83,
416     0x03,
417     0x08, 0x00,
418     0xFF,
419 
420     /* Data Class Interface Descriptor Requirement */
421     0x09, 0x04, 0x01,
422     0x00,
423     0x02,
424     0x0A, 0x00, 0x00,
425     0x00,
426 
427     /* Endpoint 0x81 descriptor */
428     0x07, 0x05, 0x81,
429     0x02,
430     0x40, 0x00,
431     0x00,
432 
433     /* Endpoint 0x02 descriptor */
434     0x07, 0x05, 0x02,
435     0x02,
436     0x40, 0x00,
437     0x00,
438 
439     /* Configuration 2 descriptor 9 bytes */
440     0x09, 0x02, (0x4b + 28 + 46), 0x00,
441     0x02, 0x02, 0x00,
442     0x40, 0x00,
443 
444     /* Interface association descriptor. 8 bytes.  */
445     0x08, 0x0b, 0x00, 0x02, 0x02, 0x02, 0x00, 0x00,
446 
447     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
448     0x09, 0x04, 0x00,
449     0x00, /* bAlternateSetting */
450     0x00, /* bNumEndpoints */
451     0x02, 0x02, 0x01,
452     0x00,
453 
454     /* Header Functional Descriptor 5 bytes */
455     0x05, 0x24, 0x00,
456     0x10, 0x01,
457 
458     /* ACM Functional Descriptor 4 bytes */
459     0x04, 0x24, 0x02,
460     0x0f,
461 
462     /* Union Functional Descriptor 5 bytes */
463     0x05, 0x24, 0x06,
464     0x00,                          /* Master interface */
465     0x01,                          /* Slave interface  */
466 
467     /* Call Management Functional Descriptor 5 bytes */
468     0x05, 0x24, 0x01,
469     0x03,
470     0x01,                          /* Data interface   */
471 
472     /* Communication Class Interface Descriptor Requirement. 9 bytes.   */
473     0x09, 0x04, 0x00,
474     0x01, /* bAlternateSetting */
475     0x01, /* bNumEndpoints */
476     0x02, 0x02, 0x01,
477     0x00,
478 
479     /* Header Functional Descriptor 5 bytes */
480     0x05, 0x24, 0x00,
481     0x10, 0x01,
482 
483     /* ACM Functional Descriptor 4 bytes */
484     0x04, 0x24, 0x02,
485     0x0f,
486 
487     /* Union Functional Descriptor 5 bytes */
488     0x05, 0x24, 0x06,
489     0x00,                          /* Master interface */
490     0x01,                          /* Slave interface  */
491 
492     /* Call Management Functional Descriptor 5 bytes */
493     0x05, 0x24, 0x01,
494     0x03,
495     0x01,                          /* Data interface   */
496 
497     /* Endpoint 0x83 descriptor 7 bytes */
498     0x07, 0x05, 0x83,
499     0x03,
500     0x08, 0x00,
501     0xFF,
502 
503     /* Data Class Interface Descriptor Requirement 9 bytes */
504     0x09, 0x04, 0x01,
505     0x00,
506     0x00,
507     0x0A, 0x00, 0x00,
508     0x00,
509 
510     /* Data Class Interface Descriptor Requirement 9 bytes */
511     0x09, 0x04, 0x01,
512     0x01,
513     0x02,
514     0x0A, 0x00, 0x00,
515     0x00,
516 
517     /* Endpoint 0x81 descriptor 7 bytes */
518     0x07, 0x05, 0x81,
519     0x02,
520     0x40, 0x00,
521     0x00,
522 
523     /* Endpoint 0x02 descriptor 7 bytes */
524     0x07, 0x05, 0x02,
525     0x02,
526     0x40, 0x00,
527     0x00,
528 
529     /* Data Class Interface Descriptor Requirement 9 bytes */
530     0x09, 0x04, 0x01,
531     0x02, /* bAlternateSetting */
532     0x04, /* bNumEndpoints */
533     0x0A, 0x00, 0x00,
534     0x00,
535 
536     /* Endpoint 0x81 descriptor 7 bytes */
537     0x07, 0x05, 0x81,
538     0x02,
539     0x40, 0x00,
540     0x00,
541 
542     /* Endpoint 0x02 descriptor 7 bytes */
543     0x07, 0x05, 0x02,
544     0x02,
545     0x40, 0x00,
546     0x00,
547 
548     /* Endpoint 0x85 descriptor 7 bytes */
549     0x07, 0x05, 0x85,
550     0x02,
551     0x40, 0x00,
552     0x00,
553 
554     /* Endpoint 0x04 descriptor 7 bytes */
555     0x07, 0x05, 0x04,
556     0x02,
557     0x40, 0x00,
558     0x00,
559 
560     /* Configuration 3: CDC + CDC */
561     CFG_DESC(CFG_DESC_LEN+2*(CDC_IAD_DESC_LEN+CDC_COMM_IFC_DESC_ALL_LEN+CDC_DATA_IFC_DESC_ALL_LEN), 4, 3)
562     CDC_IAD_DESC(0)
563     CDC_COMM_IFC_DESC_ALL(0, 1, 0x83)
564     CDC_DATA_IFC_DESC_ALL(1, 0x81, 0x02)
565     CDC_IAD_DESC(2)
566     CDC_COMM_IFC_DESC_ALL(2, 3, 0x86)
567     CDC_DATA_IFC_DESC_ALL(3, 0x84, 0x05)
568 };
569 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
570 
571 
572 static unsigned char string_framework[] = {
573 
574     /* Manufacturer string descriptor : Index 1 - "Express Logic" */
575         0x09, 0x04, 0x01, 0x0c,
576         0x45, 0x78, 0x70, 0x72, 0x65, 0x73, 0x20, 0x4c,
577         0x6f, 0x67, 0x69, 0x63,
578 
579     /* Product string descriptor : Index 2 - "EL Composite device" */
580         0x09, 0x04, 0x02, 0x13,
581         0x45, 0x4c, 0x20, 0x43, 0x6f, 0x6d, 0x70, 0x6f,
582         0x73, 0x69, 0x74, 0x65, 0x20, 0x64, 0x65, 0x76,
583         0x69, 0x63, 0x65,
584 
585     /* Serial Number string descriptor : Index 3 - "0001" */
586         0x09, 0x04, 0x03, 0x04,
587         0x30, 0x30, 0x30, 0x31,
588 
589     /* Microsoft OS string descriptor : Index 0xEE. String is MSFT100.
590        The last byte is the vendor code used to filter Vendor specific commands.
591        The vendor commands will be executed in the class.
592        This code can be anything but must not be 0x66 or 0x67 which are PIMA class commands.  */
593         0x00, 0x00, 0xEE, 0x08,
594         0x4D, 0x53, 0x46, 0x54,
595         0x31, 0x30, 0x30,
596         UX_DEMO_VENDOR_REQUEST
597 };
598 #define STRING_FRAMEWORK_LENGTH sizeof(string_framework)
599 
600 /* Multiple languages are supported on the device, to add
601     a language besides english, the unicode language code must
602     be appended to the language_id_framework array and the length
603     adjusted accordingly. */
604 static unsigned char language_id_framework[] = {
605 
606     /* English. */
607         0x09, 0x04
608 };
609 #define LANGUAGE_ID_FRAMEWORK_LENGTH sizeof(language_id_framework)
610 
611 
612 /* Define the ISR dispatch.  */
613 
614 extern VOID    (*test_isr_dispatch)(void);
615 
616 
617 /* Prototype for test control return.  */
618 
619 void  test_control_return(UINT status);
620 
621 
622 /* Define the ISR dispatch routine.  */
623 
test_isr(void)624 static void test_isr(void)
625 {
626 
627     /* For further expansion of interrupt-level testing.  */
628 }
629 
test_ms_vendor_request(ULONG request,ULONG request_value,ULONG request_index,ULONG request_length,UCHAR * transfer_request_buffer,ULONG * transfer_request_length)630 static UINT test_ms_vendor_request(ULONG request, ULONG request_value, ULONG request_index, ULONG request_length,
631                                    UCHAR *transfer_request_buffer, ULONG *transfer_request_length)
632 {
633     vendor_req_req_len = request_length;
634     vendor_req_buf_len = *transfer_request_length;
635     *transfer_request_length = vendor_req_ret_len;
636     return vendor_req_rc;
637 }
638 
test_host_change_function(ULONG event,UX_HOST_CLASS * cls,VOID * inst)639 static UINT test_host_change_function(ULONG event, UX_HOST_CLASS *cls, VOID *inst)
640 {
641 
642     switch(event)
643     {
644 
645         case UX_DEVICE_INSERTION:
646             break;
647 
648         case UX_DEVICE_REMOVAL:
649             break;
650 
651         case UX_DEVICE_CONNECTION:
652             host_device = (UX_DEVICE *)inst;
653             break;
654         case UX_DEVICE_DISCONNECTION:
655             if (host_device == (UX_DEVICE *)inst)
656                 host_device = UX_NULL;
657             break;
658 
659         default:
660             break;
661     }
662     return 0;
663 }
664 
test_ux_error_callback(UINT system_level,UINT system_context,UINT error_code)665 static VOID test_ux_error_callback(UINT system_level, UINT system_context, UINT error_code)
666 {
667     if (error_code == UX_DEVICE_ENUMERATION_FAILURE ||
668         error_code == UX_TRANSFER_STALLED)
669     {
670         /* It's normal.  */
671         return;
672     }
673     printf("error 0x%x, 0x%x, 0x%x\n", system_level, system_context, error_code);
674     test_control_return(1);
675 }
676 
677 /* Define what the initial system looks like.  */
678 
679 #ifdef CTEST
test_application_define(void * first_unused_memory)680 void test_application_define(void *first_unused_memory)
681 #else
682 void    usbx_ux_device_stack_vendor_request_test_application_define(void *first_unused_memory)
683 #endif
684 {
685 
686 UINT                    status;
687 CHAR *                  stack_pointer;
688 CHAR *                  memory_pointer;
689 
690     printf("Running Device Stack Vendor Request Test............................ ");
691 
692     /* Reset testing counts. */
693     ux_test_utility_sim_mem_alloc_count_reset();
694     ux_test_utility_sim_mutex_create_count_reset();
695     ux_test_utility_sim_sem_create_count_reset();
696     ux_test_utility_sim_sem_get_count_reset();
697     /* Reset error generations */
698     ux_test_utility_sim_mem_alloc_error_generation_stop();
699     ux_test_utility_sim_sem_error_generation_stop();
700     ux_test_utility_sim_mutex_error_generation_stop();
701     ux_test_utility_sim_sem_get_error_generation_stop();
702 
703     /* Initialize the free memory pointer */
704     stack_pointer = (CHAR *) usbx_memory;
705     memory_pointer = stack_pointer + (UX_DEMO_STACK_SIZE * 2);
706 
707     /* Initialize USBX Memory */
708     status = ux_system_initialize(memory_pointer, UX_DEMO_MEMORY_SIZE, UX_NULL,0);
709 
710     /* Check for error.  */
711     if (status != UX_SUCCESS)
712     {
713 
714         printf(" ERROR #1\n");
715         test_control_return(1);
716     }
717 
718     /* Register the error callback. */
719     _ux_utility_error_callback_register(test_ux_error_callback);
720 
721     /* The code below is required for installing the device portion of USBX. No call back for
722        device status change in this example. */
723     status =  ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
724                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
725                                        string_framework, STRING_FRAMEWORK_LENGTH,
726                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH,UX_NULL);
727     if(status!=UX_SUCCESS)
728     {
729 
730         printf(" ERROR #5\n");
731         test_control_return(1);
732     }
733 
734     /* Set the parameters for callback when insertion/extraction of a CDC device.  */
735 
736     /* Initialize the device cdc class. This class owns both interfaces starting with 0. */
737     status  =  ux_device_stack_class_register(_ux_device_class_dummy_name, _ux_device_class_dummy_entry,
738                                              1, 0,  &parameter);
739     if(status!=UX_SUCCESS)
740     {
741 
742         printf(" ERROR #7\n");
743         test_control_return(1);
744     }
745 
746     /* MS extensions.  */
747     status = _ux_device_stack_microsoft_extension_register(UX_DEMO_VENDOR_REQUEST, test_ms_vendor_request);
748 
749     if(status!=UX_SUCCESS)
750     {
751 
752         printf(" ERROR #%d\n", __LINE__);
753         test_control_return(1);
754     }
755 
756     /* Initialize the simulated device controller.  */
757     status =  _ux_test_dcd_sim_slave_initialize();
758 
759     /* Check for error.  */
760     if (status != TX_SUCCESS)
761     {
762 
763         printf(" ERROR #8\n");
764         test_control_return(1);
765     }
766 
767     /* The code below is required for installing the host portion of USBX */
768     status =  ux_host_stack_initialize(test_host_change_function);
769     if (status != UX_SUCCESS)
770     {
771 
772         printf(" ERROR #2\n");
773         test_control_return(1);
774     }
775 
776     /* Register all the USB host controllers available in this system */
777     status =  ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_test_hcd_sim_host_initialize,0,0);
778     if (status != UX_SUCCESS)
779     {
780 
781         printf(" ERROR #4\n");
782         test_control_return(1);
783     }
784 
785     /* Create the main host simulation thread.  */
786     status =  tx_thread_create(&tx_test_thread_simulation0, "tx test simulation 0", tx_test_thread_simulation0_entry, 0,
787             stack_pointer, UX_DEMO_STACK_SIZE,
788             20, 20, 1, TX_AUTO_START);
789 
790     /* Check for error.  */
791     if (status != TX_SUCCESS)
792     {
793 
794         printf(" ERROR #9\n");
795         test_control_return(1);
796     }
797 
798     /* Create the main slave simulation  thread.  */
799     status =  tx_thread_create(&tx_test_thread_simulation1, "tx test simulation 1", tx_test_thread_simulation1_entry, 0,
800             stack_pointer + UX_DEMO_STACK_SIZE, UX_DEMO_STACK_SIZE,
801             20, 20, 1, TX_AUTO_START);
802 
803     /* Check for error.  */
804     if (status != TX_SUCCESS)
805     {
806 
807         printf(" ERROR #10\n");
808         test_control_return(1);
809     }
810 }
811 
tx_test_thread_simulation0_entry(ULONG arg)812 void  tx_test_thread_simulation0_entry(ULONG arg)
813 {
814 UINT                                                status;
815 ULONG                                               actual_length;
816 UX_DEVICE                                          *device;
817 UX_ENDPOINT                                        *control_endpoint;
818 UX_TRANSFER                                        *transfer_request;
819 UX_SLAVE_TRANSFER                                  *slave_transfer_request;
820 
821     stepinfo("\n");
822 
823     /* Wait for first enumeration to complete.  */
824     stepinfo(">>>>>>>>>>>>>>>> Wait for first enumeration completion\n");
825     while (host_device == UX_NULL)
826         tx_thread_sleep(10);
827 
828     /* Wait for instances to be live. */
829     tx_thread_sleep(10);
830 
831     ux_test_dcd_sim_slave_disconnect();
832     ux_test_hcd_sim_host_disconnect();
833 
834     /* Test connect. Note that we must switch to high speed for tests. */
835     stepinfo(">>>>>>>>>>>>>>>> Test connect\n");
836     ux_test_dcd_sim_slave_connect(UX_HIGH_SPEED_DEVICE);
837     ux_test_hcd_sim_host_connect(UX_HIGH_SPEED_DEVICE);
838     tx_thread_sleep(100);
839     if (host_device == UX_NULL)
840     {
841 
842         printf("ERROR #%d: connection not detected\n", __LINE__);
843         test_control_return(1);
844     }
845 
846     device = host_device;
847     control_endpoint = &device->ux_device_control_endpoint;
848     transfer_request = &control_endpoint->ux_endpoint_transfer_request;
849 
850     slave_transfer_request = &_ux_system_slave->ux_system_slave_device.ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
851 
852     stepinfo(">>>>>>>>>>>>>>>> Test Vendor Request\n");
853     transfer_request -> ux_transfer_request_function =          0xEE;
854     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_VENDOR | UX_REQUEST_TARGET_DEVICE;
855     status = ux_host_stack_transfer_request(transfer_request);
856     if (status != UX_TRANSFER_STALLED)
857     {
858 
859         printf("ERROR #%d: Vendor request status %x\n", __LINE__, status);
860         test_control_return(1);
861     }
862 
863     stepinfo(">>>>>>>>>>>>>>>> Test MS Vendor Request\n");
864     transfer_request -> ux_transfer_request_function =          UX_DEMO_VENDOR_REQUEST;
865     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_VENDOR | UX_REQUEST_TARGET_DEVICE;
866 
867     vendor_req_rc = UX_ERROR;
868     status = ux_host_stack_transfer_request(transfer_request);
869     if (status != UX_TRANSFER_STALLED)
870     {
871 
872         printf("ERROR #%d: Vendor request status %x\n", __LINE__, status);
873         test_control_return(1);
874     }
875     vendor_req_rc = UX_SUCCESS;
876 
877     status = ux_host_stack_transfer_request(transfer_request);
878     if (status != UX_SUCCESS)
879     {
880 
881         printf("ERROR #%d: Vendor request status %x\n", __LINE__, status);
882         test_control_return(1);
883     }
884 
885     stepinfo(">>>>>>>>>>>>>>>> Test vendor request NULL case\n");
886     status = _ux_device_stack_microsoft_extension_register(UX_DEMO_VENDOR_REQUEST, UX_NULL);
887     if(status!=UX_SUCCESS)
888     {
889 
890         printf("ERROR #%d: Vendor callback clear fail\n", __LINE__);
891         test_control_return(1);
892     }
893     vendor_req_call_count = 0;
894     status = ux_host_stack_transfer_request(transfer_request);
895     UX_TEST_ASSERT_MESSAGE(status == UX_TRANSFER_STALLED, "ERROR #%d: Vendor request status %x\n", __LINE__, status);
896     UX_TEST_ASSERT(vendor_req_call_count == 0);
897 
898     /* Finally disconnect the device. */
899     ux_device_stack_disconnect();
900 
901     /* Deinitialize the device side of usbx.  */
902     _ux_device_stack_uninitialize();
903 
904     /* And finally the usbx system resources.  */
905     _ux_system_uninitialize();
906 
907     /* Successful test.  */
908     printf("SUCCESS!\n");
909     test_control_return(0);
910 
911 }
912 
tx_test_thread_simulation1_entry(ULONG arg)913 void  tx_test_thread_simulation1_entry(ULONG arg)
914 {
915 
916 UINT status;
917 ULONG                                               actual_length;
918 
919     while(1)
920     {
921 
922         /* Sleep so ThreadX on Win32 will delete this thread. */
923         tx_thread_sleep(10);
924     }
925 }
926