1 /* This test is designed to test the ux_utility_memory_....  */
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 "ux_device_class_dpump.h"
10 #include "ux_host_class_dpump.h"
11 
12 #include "fx_api.h"
13 #include "ux_device_class_storage.h"
14 #include "ux_host_class_storage.h"
15 
16 #include "ux_device_class_hid.h"
17 #include "ux_host_class_hid.h"
18 #include "ux_host_class_hid_mouse.h"
19 
20 #include "ux_host_stack.h"
21 #include "ux_hcd_sim_host.h"
22 #include "ux_test_hcd_sim_host.h"
23 #include "ux_test_dcd_sim_slave.h"
24 #include "ux_test_utility_sim.h"
25 
26 
27 /* Define USBX test constants.  */
28 
29 #define UX_TEST_STACK_SIZE              4096
30 #define UX_TEST_MEMORY_SIZE             (256*1024)
31 
32 #define UX_RAM_DISK_SIZE                (200 * 1024)
33 #define UX_RAM_DISK_LAST_LBA            ((UX_RAM_DISK_SIZE / 512) -1)
34 
35 #define     LSB(x) ( (x) & 0x00ff)
36 #define     MSB(x) (((x) & 0xff00) >> 8)
37 
38 /* Define the counters used in the test application...  */
39 
40 static ULONG                            thread_0_counter = 0;
41 static ULONG                            thread_1_counter = 0;
42 
43 static ULONG                            error_counter = 0;
44 
45 static UCHAR                            error_callback_ignore = UX_FALSE;
46 static ULONG                            error_callback_counter = 0;
47 
48 static UCHAR                            bad_name[UX_MAX_HCD_NAME_LENGTH + 1];
49 
50 static FX_MEDIA                         ram_disk1 = {0};
51 static CHAR                             ram_disk_memory1[UX_RAM_DISK_SIZE];
52 static UCHAR                            ram_disk_buffer1[512];
53 
54 static FX_MEDIA                         *ram_disks[] = {&ram_disk1};
55 static UCHAR                            *ram_disk_buffers[] = {ram_disk_buffer1};
56 static CHAR                             *ram_disk_memories[] = {ram_disk_memory1};
57 
58 static UX_SLAVE_CLASS_DPUMP_PARAMETER       dpump_parameter = {0};
59 static UX_SLAVE_CLASS_STORAGE_PARAMETER     storage_parameter = {0};
60 static UX_SLAVE_CLASS_HID_PARAMETER         hid_parameter = {0};
61 
62 UX_HOST_CLASS_DPUMP                         *host_dpump = UX_NULL;
63 UX_HOST_CLASS_STORAGE                       *host_storage = UX_NULL;
64 UX_HOST_CLASS_HID                           *host_hid = UX_NULL;
65 
66 
67 /* Define USBX test global variables.  */
68 
69 static UCHAR hid_mouse_report[] = {
70 
71     0x05, 0x01,                    // USAGE_PAGE (Generic Desktop)
72     0x09, 0x02,                    // USAGE (Mouse)
73     0xa1, 0x01,                    // COLLECTION (Application)
74     0x09, 0x01,                    //   USAGE (Pointer)
75     0xa1, 0x00,                    //   COLLECTION (Physical)
76     0x05, 0x09,                    //     USAGE_PAGE (Button)
77     0x19, 0x01,                    //     USAGE_MINIMUM (Button 1)
78     0x29, 0x03,                    //     USAGE_MAXIMUM (Button 3)
79     0x15, 0x00,                    //     LOGICAL_MINIMUM (0)
80     0x25, 0x01,                    //     LOGICAL_MAXIMUM (1)
81     0x95, 0x03,                    //     REPORT_COUNT (3)
82     0x75, 0x01,                    //     REPORT_SIZE (1)
83     0x81, 0x02,                    //     INPUT (Data,Var,Abs)
84     0x95, 0x01,                    //     REPORT_COUNT (1)
85     0x75, 0x05,                    //     REPORT_SIZE (5)
86     0x81, 0x03,                    //     INPUT (Cnst,Var,Abs)
87     0x05, 0x01,                    //     USAGE_PAGE (Generic Desktop)
88     0x09, 0x30,                    //     USAGE (X)
89     0x09, 0x31,                    //     USAGE (Y)
90     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
91     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
92     0x75, 0x08,                    //     REPORT_SIZE (8)
93     0x95, 0x02,                    //     REPORT_COUNT (2)
94     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
95     0x09, 0x38,                    //     USAGE (Mouse Wheel)
96     0x15, 0x81,                    //     LOGICAL_MINIMUM (-127)
97     0x25, 0x7f,                    //     LOGICAL_MAXIMUM (127)
98     0x75, 0x08,                    //     REPORT_SIZE (8)
99     0x95, 0x01,                    //     REPORT_COUNT (1)
100     0x81, 0x06,                    //     INPUT (Data,Var,Rel)
101     0xc0,                          //   END_COLLECTION
102     0xc0                           // END_COLLECTION
103 };
104 #define HID_MOUSE_REPORT_LENGTH (sizeof(hid_mouse_report)/sizeof(hid_mouse_report[0]))
105 
106 
107 #define CONFIG_DESCRIPTOR(wTotalLength, n_iface) \
108     0x09, 0x02, LSB(wTotalLength), MSB(wTotalLength), (n_iface), 0x01, 0x00, 0xc0, 0x32,
109 
110 #define DPUMP_DESCRIPTORS_LENGTH                                (9+7+7)
111 #define DPUMP_DESCRIPTORS(iface, ep_out, ep_in, mps)            \
112     /* Interface descriptor */                                  \
113     0x09, 0x04, (iface), 0x00, 0x02, 0x99, 0x99, 0x99, 0x00,    \
114     /* Endpoint descriptor (Bulk Out) */                        \
115     0x07, 0x05, (ep_out), 0x02, LSB(mps), MSB(mps), 0x00,       \
116     /* Endpoint descriptor (Bulk In) */                         \
117     0x07, 0x05, (ep_in), 0x02, LSB(mps), MSB(mps), 0x00,
118 
119 #define STORAGE_DESCRIPTORS_LENGTH                              (9+7+7)
120 #define STORAGE_DESCRIPTORS(iface, ep_out, ep_in, mps)          \
121     /* Interface descriptor */                                  \
122     0x09, 0x04, (iface), 0x00, 0x02, 0x08, 0x06, 0x50, 0x00,    \
123     /* Endpoint descriptor (Bulk In) */                         \
124     0x07, 0x05, (ep_in), 0x02, 0x00, 0x01, 0x00,                \
125     /* Endpoint descriptor (Bulk Out) */                        \
126     0x07, 0x05, (ep_out), 0x02, 0x00, 0x01, 0x00,
127 
128 #define HID_DESCRIPTORS_LENGTH                                  (9+9+7)
129 #define HID_DESCRIPTORS(iface, ep_in, mps, interval, rpt_len)   \
130     /* Interface descriptor */                                  \
131     0x09, 0x04, (iface), 0x00, 0x01, 0x03, 0x00, 0x00, 0x00,    \
132     /* HID descriptor */                                        \
133     0x09, 0x21, 0x10, 0x01, 0x21, 0x01, 0x22,                   \
134     LSB(rpt_len), MSB(rpt_len),                                 \
135     /* Endpoint descriptor (Interrupt) */                       \
136     0x07, 0x05, (ep_in), 0x03, LSB(mps), MSB(mps), (interval),
137 
138 
139 static UCHAR device_framework_full_speed[] = {
140 
141     /* Device descriptor */
142     0x12, 0x01, 0x10, 0x01, 0x00, 0x00, 0x00, 0x08,
143     0xec, 0x08, 0x10, 0x00, 0x00, 0x00, 0x00, 0x00,
144     0x00, 0x01,
145 
146     CONFIG_DESCRIPTOR(DPUMP_DESCRIPTORS_LENGTH +
147                       STORAGE_DESCRIPTORS_LENGTH +
148                       HID_DESCRIPTORS_LENGTH + 9, 0x03)
149     DPUMP_DESCRIPTORS(0x00, 0x01, 0x82, 64)
150     STORAGE_DESCRIPTORS(0x01, 0x03, 0x84, 64)
151     HID_DESCRIPTORS(0x02, 0x85, 8, 8, HID_MOUSE_REPORT_LENGTH)
152 };
153 #define DEVICE_FRAMEWORK_LENGTH_FULL_SPEED sizeof(device_framework_full_speed)
154 
155 
156 static UCHAR device_framework_high_speed[] = {
157 
158     /* Device descriptor */
159     0x12, 0x01, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
160     0x0a, 0x07, 0x25, 0x40, 0x01, 0x00, 0x01, 0x02,
161     0x03, 0x01,
162 
163     /* Device qualifier descriptor */
164     0x0a, 0x06, 0x00, 0x02, 0x00, 0x00, 0x00, 0x40,
165     0x01, 0x00,
166 
167     CONFIG_DESCRIPTOR(DPUMP_DESCRIPTORS_LENGTH +
168                       STORAGE_DESCRIPTORS_LENGTH +
169                       HID_DESCRIPTORS_LENGTH + 9, 0x03)
170     DPUMP_DESCRIPTORS(0x00, 0x01, 0x82, 512)
171     STORAGE_DESCRIPTORS(0x01, 0x03, 0x84, 512)
172     HID_DESCRIPTORS(0x02, 0x85, 8, 8, HID_MOUSE_REPORT_LENGTH)
173 };
174 #define DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED sizeof(device_framework_high_speed)
175 
176 /* String Device Framework :
177     Byte 0 and 1 : Word containing the language ID : 0x0904 for US
178     Byte 2       : Byte containing the index of the descriptor
179     Byte 3       : Byte containing the length of the descriptor string
180 */
181 #define STRING_FRAMEWORK_LENGTH 38
182 static UCHAR string_framework[] = {
183 
184     /* Manufacturer string descriptor : Index 1 */
185     0x09, 0x04, 0x01, 0x0c,
186     0x45, 0x78, 0x70, 0x72,0x65, 0x73, 0x20, 0x4c,
187     0x6f, 0x67, 0x69, 0x63,
188 
189     /* Product string descriptor : Index 2 */
190     0x09, 0x04, 0x02, 0x0c,
191     0x44, 0x61, 0x74, 0x61, 0x50, 0x75, 0x6d, 0x70,
192     0x44, 0x65, 0x6d, 0x6f,
193 
194     /* Serial Number string descriptor : Index 3 */
195     0x09, 0x04, 0x03, 0x04,
196     0x30, 0x30, 0x30, 0x31
197 };
198 
199 
200 /* Multiple languages are supported on the device, to add
201     a language besides English, the unicode language code must
202     be appended to the language_id_framework array and the length
203     adjusted accordingly. */
204 #define LANGUAGE_ID_FRAMEWORK_LENGTH 2
205 static UCHAR language_id_framework[] = {
206 
207     /* English. */
208         0x09, 0x04
209 };
210 
211 
212 /* Define prototypes for external Controller's (HCD/DCDs), classes and clients.  */
213 
214 extern VOID                 _fx_ram_driver(FX_MEDIA *media_ptr);
215 
216 static TX_THREAD            ux_test_thread_simulation_0;
217 static void                 ux_test_thread_simulation_0_entry(ULONG);
218 
219 static TX_THREAD            ux_test_thread_simulation_1;
220 static void                 ux_test_thread_simulation_1_entry(ULONG);
221 
222 static UX_SLAVE_CLASS_DPUMP *dpump_device = UX_NULL;
223 static VOID                 ux_test_dpump_instance_activate(VOID *dpump_instance);
224 static VOID                 ux_test_dpump_instance_deactivate(VOID *dpump_instance);
225 
226 static UINT                 ux_test_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
227 static UINT                 ux_test_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status);
228 static UINT                 ux_test_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status);
229 
230 /* Prototype for test control return.  */
231 
232 void  test_control_return(UINT status);
233 
error_callback(UINT system_level,UINT system_context,UINT error_code)234 static VOID error_callback(UINT system_level, UINT system_context, UINT error_code)
235 {
236 
237     error_callback_counter ++;
238 
239     if (!error_callback_ignore)
240     {
241         {
242             /* Failed test.  */
243             printf("#%d, system_level: %d, system_context: %d, error_code: 0x%x\n", __LINE__, system_level, system_context, error_code);
244         }
245     }
246 }
247 
248 /* Define what the initial system looks like.  */
249 
250 #ifdef CTEST
test_application_define(void * first_unused_memory)251 void test_application_define(void *first_unused_memory)
252 #else
253 void    usbx_ux_host_stack_class_unregister_test_application_define(void *first_unused_memory)
254 #endif
255 {
256 
257 UINT                                status;
258 UCHAR                               *stack_pointer;
259 UCHAR                               *memory_pointer;
260 
261 
262     /* Inform user.  */
263     printf("Running ux_host_stack_class_unregister Test......................... ");
264 
265     /* Initialize the free memory pointer.  */
266     stack_pointer = (CHAR *) first_unused_memory;
267     memory_pointer = stack_pointer + (UX_TEST_STACK_SIZE * 2);
268 
269     /* Initialize USBX Memory.  */
270     status =  ux_system_initialize(memory_pointer, UX_TEST_MEMORY_SIZE, UX_NULL, 0);
271 
272     /* Check for error.  */
273     if (status != UX_SUCCESS)
274     {
275 
276         printf("ERROR #%d: 0x%x\n", __LINE__, status);
277         test_control_return(1);
278     }
279 
280 #if UX_TEST_MULTI_CLS_OVER(2) && UX_TEST_MULTI_IFC_OVER(2) /* At least 3 classes used.  */
281 
282     /* Register the error callback. */
283     _ux_utility_error_callback_register(error_callback);
284 #endif
285 
286     /* The code below is required for installing the device portion of USBX */
287     status = ux_device_stack_initialize(device_framework_high_speed, DEVICE_FRAMEWORK_LENGTH_HIGH_SPEED,
288                                        device_framework_full_speed, DEVICE_FRAMEWORK_LENGTH_FULL_SPEED,
289                                        string_framework, STRING_FRAMEWORK_LENGTH,
290                                        language_id_framework, LANGUAGE_ID_FRAMEWORK_LENGTH, UX_NULL);
291     if (status != UX_SUCCESS)
292     {
293 
294         printf("ERROR #%d: 0x%x\n", __LINE__, status);
295         test_control_return(1);
296     }
297 
298     /* Set the parameters for callback when insertion/extraction of a Data Pump device.  */
299     dpump_parameter.ux_slave_class_dpump_instance_activate   =  ux_test_dpump_instance_activate;
300     dpump_parameter.ux_slave_class_dpump_instance_deactivate =  ux_test_dpump_instance_deactivate;
301 
302     /* Initialize the device dpump class. The class is connected with interface 0 */
303     status = ux_device_stack_class_register(_ux_system_slave_class_dpump_name, _ux_device_class_dpump_entry,
304                                             1, 0, &dpump_parameter);
305     if (status != UX_SUCCESS)
306     {
307 
308         printf("ERROR #%d: 0x%x\n", __LINE__, status);
309         test_control_return(1);
310     }
311 
312     /* Store the number of LUN in this device storage instance.  */
313     storage_parameter.ux_slave_class_storage_parameter_number_lun = 1;
314 
315     /* Initialize the storage class parameters for reading/writing to the first Flash Disk.  */
316     storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_last_lba        =  UX_RAM_DISK_LAST_LBA;
317     storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_block_length    =  512;
318     storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_type            =  0;
319     storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_removable_flag  =  0x80;
320     storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_read            =  ux_test_media_read;
321     storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_write           =  ux_test_media_write;
322     storage_parameter.ux_slave_class_storage_parameter_lun[0].ux_slave_class_storage_media_status          =  ux_test_media_status;
323 
324     /* Initialize the device storage class. The class is connected with interface 0 on configuration 1. */
325     status = ux_device_stack_class_register(_ux_system_slave_class_storage_name, ux_device_class_storage_entry,
326                                             1, 1, (VOID *)&storage_parameter);
327 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
328     if(status!=UX_SUCCESS)
329     {
330 
331         printf("ERROR #%d\n", __LINE__);
332         test_control_return(1);
333     }
334 #endif
335 
336     /* Initialize the hid class parameters for a mouse.  */
337     hid_parameter.ux_device_class_hid_parameter_report_address = hid_mouse_report;
338     hid_parameter.ux_device_class_hid_parameter_report_length  = HID_MOUSE_REPORT_LENGTH;
339     hid_parameter.ux_device_class_hid_parameter_callback       = UX_NULL;
340 
341     /* Initilize the device hid class. The class is connected with interface 2 */
342     status =  ux_device_stack_class_register(_ux_system_slave_class_hid_name, ux_device_class_hid_entry,
343                                              1, 2, (VOID *)&hid_parameter);
344 #if UX_MAX_SLAVE_CLASS_DRIVER > 1
345     if(status!=UX_SUCCESS)
346     {
347 
348         printf("Error on line %d\n", __LINE__);
349         test_control_return(1);
350     }
351 #endif
352 
353     /* Initialize the simulated device controller.  */
354     status =  _ux_test_dcd_sim_slave_initialize();
355 
356     /* Check for error.  */
357     if (status != UX_SUCCESS)
358     {
359 
360         printf("ERROR #%d: 0x%x\n", __LINE__, status);
361         test_control_return(1);
362     }
363 
364     /* Create the simulation thread.  */
365     status =  tx_thread_create(&ux_test_thread_simulation_0, "test simulation", ux_test_thread_simulation_0_entry, 0,
366             stack_pointer, UX_TEST_STACK_SIZE,
367             20, 20, 1, TX_AUTO_START);
368 
369     /* Check for error.  */
370     if (status != UX_SUCCESS)
371     {
372         printf("ERROR #%d: 0x%x\n", __LINE__, status);
373         test_control_return(1);
374     }
375 }
376 
ux_test_ram_disk_initialize(INT line)377 static void ux_test_ram_disk_initialize(INT line)
378 {
379 UINT        status;
380 
381     /* Initialize FileX.  */
382     fx_system_initialize();
383 
384     /* Reset ram disks memory.  */
385     ux_utility_memory_set(ram_disk_memory1, 0, UX_RAM_DISK_SIZE);
386 
387     /* Format the ram drive. */
388     status =   fx_media_format(&ram_disk1, _fx_ram_driver, ram_disk_memory1, ram_disk_buffer1, 512, "RAM DISK1", 2, 512, 0, UX_RAM_DISK_SIZE/512, 512, 4, 1, 1);
389     if (status != FX_SUCCESS)
390     {
391         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
392         test_control_return(1);
393     }
394 
395     /* Open the ram_disk.  */
396     status =   fx_media_open(&ram_disk1, "RAM DISK1", _fx_ram_driver, ram_disk_memory1, ram_disk_buffer1, 512);
397     if (status != FX_SUCCESS)
398     {
399         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
400         test_control_return(1);
401     }
402 
403 }
404 
ux_test_host_stack_class_register(INT line)405 static void ux_test_host_stack_class_register(INT line)
406 {
407 
408 UINT        status;
409 
410     // printf("#%d.%d: register classes\n", line, __LINE__);
411 
412     status = ux_host_stack_class_register(_ux_system_host_class_dpump_name, ux_host_class_dpump_entry);
413     if (status != UX_SUCCESS)
414     {
415         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
416         test_control_return(1);
417     }
418 
419 #if UX_MAX_CLASS_DRIVER == 1
420     error_callback_ignore = UX_TRUE;
421 #endif
422 
423     status = ux_host_stack_class_register(_ux_system_host_class_storage_name, ux_host_class_storage_entry);
424 #if UX_MAX_CLASS_DRIVER > 1
425     if (status != UX_SUCCESS)
426     {
427         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
428         test_control_return(1);
429     }
430 #endif
431 
432     status = ux_host_stack_class_register(_ux_system_host_class_hid_name, ux_host_class_hid_entry);
433 #if UX_MAX_CLASS_DRIVER > 1
434     if (status != UX_SUCCESS)
435     {
436         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
437         test_control_return(1);
438     }
439 
440     /* Register the HID client(s).  */
441     status =  ux_host_class_hid_client_register(_ux_system_host_class_hid_client_mouse_name, ux_host_class_hid_mouse_entry);
442     if (status != UX_SUCCESS)
443     {
444         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
445         test_control_return(1);
446     }
447 #endif
448 
449 #if UX_MAX_CLASS_DRIVER == 1
450     error_callback_ignore = UX_FALSE;
451 #endif
452 }
453 
ux_test_host_stack_class_unregister(INT line)454 static void ux_test_host_stack_class_unregister(INT line)
455 {
456 
457 UINT        status;
458 
459     // printf("#%d.%d: unregister classes\n", line, __LINE__);
460 
461     /* Dpump has no "destroy", ignore UX_FUNCTION_NOT_SUPPORTED print.  */
462     error_callback_ignore = UX_TRUE;
463     status = _ux_host_stack_class_unregister(ux_host_class_dpump_entry);
464     if (status != UX_SUCCESS)
465     {
466         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
467         test_control_return(1);
468     }
469     error_callback_ignore = UX_FALSE;
470 
471     status = _ux_host_stack_class_unregister(ux_host_class_storage_entry);
472 #if UX_MAX_CLASS_DRIVER > 1
473     if (status != UX_SUCCESS)
474     {
475         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
476         test_control_return(1);
477     }
478 #endif
479 
480     status = _ux_host_stack_class_unregister(ux_host_class_hid_entry);
481 #if UX_MAX_CLASS_DRIVER > 1
482     if (status != UX_SUCCESS)
483     {
484         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
485         test_control_return(1);
486     }
487 #endif
488 }
489 
ux_test_host_stack_hcd_register(INT line)490 static void ux_test_host_stack_hcd_register(INT line)
491 {
492 UINT        status;
493 
494     /* Register HCD.  */
495     status = _ux_host_stack_hcd_register(_ux_system_host_hcd_simulator_name, _ux_hcd_sim_host_initialize,0,0);
496     if (status != UX_SUCCESS)
497     {
498         printf("ERROR #%d.%d: 0x%x\n", line, __LINE__, status);
499         test_control_return(1);
500     }
501 }
502 
ux_test_host_stack_hcd_unregister(INT line)503 static void ux_test_host_stack_hcd_unregister(INT line)
504 {
505 UINT                status;
506 UX_DEVICE           *device;
507 
508     /* Unregister HCD.  */
509     status = _ux_host_stack_hcd_unregister(_ux_system_host_hcd_simulator_name, 0, 0);
510     if (status != UX_SUCCESS)
511     {
512         printf("ERROR #%d: 0x%x\n", __LINE__, status);
513         test_control_return(1);
514     }
515 
516     /* Check host device.  */
517     status = _ux_host_stack_device_get(0, &device);
518     if (status != UX_DEVICE_HANDLE_UNKNOWN)
519     {
520         printf("ERROR #%d: 0x%x\n", __LINE__, status);
521         test_control_return(1);
522     }
523 }
524 
ux_test_wait_connect(INT line)525 static void ux_test_wait_connect(INT line)
526 {
527 
528 UINT                        status;
529 INT                         i;
530 UX_DEVICE                   *device;
531 UX_HOST_CLASS               *host_class;
532 
533     host_dpump = UX_NULL;
534     host_storage = UX_NULL;
535     host_hid = UX_NULL;
536 
537     /* Wait for connection.  */
538     for (i = 0; i < 100; i ++)
539     {
540         tx_thread_sleep(UX_MS_TO_TICK_NON_ZERO(10));
541         status = _ux_host_stack_device_get(0, &device);
542         if (status != UX_SUCCESS)
543             continue;
544         if (dpump_device == UX_NULL)
545             continue;
546 
547         /* DPUMP.  */
548         ux_host_stack_class_get(_ux_system_host_class_dpump_name, &host_class);
549         status = ux_host_stack_class_instance_get(host_class, 0, (VOID **)&host_dpump);
550         if (status != UX_SUCCESS)
551             continue;
552         if (host_dpump -> ux_host_class_dpump_state != UX_HOST_CLASS_INSTANCE_LIVE)
553             continue;
554 
555         /* Storage.  */
556         ux_host_stack_class_get(_ux_system_host_class_storage_name, &host_class);
557         status = ux_host_stack_class_instance_get(host_class, 0, (VOID **)&host_storage);
558         if (status != UX_SUCCESS)
559             continue;
560         if (host_storage -> ux_host_class_storage_state != UX_HOST_CLASS_INSTANCE_LIVE)
561             continue;
562 
563         /* HID.  */
564         ux_host_stack_class_get(_ux_system_host_class_hid_name, &host_class);
565         status = ux_host_stack_class_instance_get(host_class, 0, (VOID **)&host_hid);
566         if (status != UX_SUCCESS)
567             continue;
568         if (host_hid -> ux_host_class_hid_state != UX_HOST_CLASS_INSTANCE_LIVE)
569             continue;
570 
571         /* All ready.  */
572         break;
573     }
574     if (dpump_device == UX_NULL)
575     {
576         printf("ERROR #%d.%d\n", line, __LINE__);
577         test_control_return(1);
578     }
579     if (status != UX_SUCCESS)
580     {
581         printf("ERROR #%d.%d\n", line, __LINE__);
582         test_control_return(1);
583     }
584 }
585 
ux_test_thread_simulation_0_entry(ULONG arg)586 static void  ux_test_thread_simulation_0_entry(ULONG arg)
587 {
588 
589 UINT                status;
590 ULONG               rfree;
591 INT                 i;
592 UX_DEVICE           *device;
593 
594     /* Initialize RAM disk.  */
595     ux_test_ram_disk_initialize(__LINE__);
596 
597     /* Initialize host stack.  */
598     status = ux_host_stack_initialize(UX_NULL);
599     if (status != UX_SUCCESS)
600     {
601         printf("ERROR #%d: 0x%x\n", __LINE__, status);
602         test_control_return(1);
603     }
604 
605     /************************** Register & unregister (no device connected).  */
606 
607     /* Log memory level.  */
608     rfree = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
609 
610     ux_test_host_stack_class_register(__LINE__);
611     ux_test_host_stack_class_unregister(__LINE__);
612 
613     /* Check memory level.  */
614     if (rfree != _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available)
615     {
616         printf("Error on line %d\n", __LINE__);
617         test_control_return(1);
618     }
619 
620     /************************** Unregister (after device connect & disconnect).  */
621 
622     error_callback_ignore = UX_TRUE;
623 
624     // ux_test_dcd_sim_slave_disconnect();
625     // ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE);
626 
627     ux_test_host_stack_class_register(__LINE__);
628     ux_test_host_stack_hcd_register(__LINE__);
629     ux_test_wait_connect(__LINE__);
630 
631     /* Unregister.  */
632     error_callback_ignore = UX_TRUE;
633     ux_test_host_stack_hcd_unregister(__LINE__);
634     error_callback_ignore = UX_FALSE;
635 
636     ux_test_host_stack_hcd_register(__LINE__);
637     ux_test_wait_connect(__LINE__);
638 
639     /* Unregister.  */
640     error_callback_ignore = UX_TRUE;
641     ux_test_host_stack_hcd_unregister(__LINE__);
642     ux_test_host_stack_class_unregister(__LINE__);
643     error_callback_ignore = UX_FALSE;
644 
645     // ux_test_dcd_sim_slave_disconnect();
646     // ux_test_dcd_sim_slave_connect(UX_FULL_SPEED_DEVICE);
647 
648     /* Log memory level.  */
649     rfree = _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available;
650 
651     ux_test_host_stack_class_register(__LINE__);
652     ux_test_host_stack_hcd_register(__LINE__);
653     ux_test_wait_connect(__LINE__);
654 
655     /* Unregister.  */
656     error_callback_ignore = UX_TRUE;
657     ux_test_host_stack_hcd_unregister(__LINE__);
658     ux_test_host_stack_class_unregister(__LINE__);
659 
660     /* Check memory level.  */
661     if (rfree != _ux_system -> ux_system_memory_byte_pool[UX_MEMORY_BYTE_POOL_REGULAR] -> ux_byte_pool_available)
662     {
663         printf("Error on line %d\n", __LINE__);
664         test_control_return(1);
665     }
666     error_callback_ignore = UX_FALSE;
667 
668     /* Check for errors from other threads.  */
669     if (error_counter)
670     {
671 
672         /* Test error.  */
673         printf("ERROR #%d: total %ld errors\n", __LINE__, error_counter);
674         test_control_return(1);
675     }
676     else
677     {
678 
679         /* Successful test.  */
680         printf("SUCCESS!\n");
681         test_control_return(0);
682     }
683 }
684 
685 
ux_test_dpump_instance_activate(VOID * dpump_instance)686 static VOID  ux_test_dpump_instance_activate(VOID *dpump_instance)
687 {
688 
689     /* Save the DPUMP instance.  */
690     dpump_device = (UX_SLAVE_CLASS_DPUMP *) dpump_instance;
691 }
692 
ux_test_dpump_instance_deactivate(VOID * dpump_instance)693 static VOID  ux_test_dpump_instance_deactivate(VOID *dpump_instance)
694 {
695 
696     /* Reset the DPUMP instance.  */
697     dpump_device = UX_NULL;
698 }
699 
ux_test_media_status(VOID * storage,ULONG lun,ULONG media_id,ULONG * media_status)700 static UINT ux_test_media_status(VOID *storage, ULONG lun, ULONG media_id, ULONG *media_status)
701 {
702 
703 static UCHAR lun_init_done[2] = {0, 0};
704 UINT         status;
705 ULONG        mstatus = UX_SLAVE_CLASS_STORAGE_SENSE_KEY_NO_SENSE;
706 
707 
708     (void)storage;
709     (void)media_id;
710 
711 
712     if (lun > 0)
713         status = (UX_ERROR);
714     else if (lun_init_done[lun] > 0)
715         status = (UX_SUCCESS);
716     else
717     {
718         lun_init_done[lun] ++;
719         mstatus = UX_SLAVE_CLASS_STORAGE_SENSE_KEY_UNIT_ATTENTION | (0x28 << 8);
720         status = (UX_ERROR);
721     }
722 
723     if (media_status)
724         *media_status = mstatus;
725 
726     return status;
727 }
728 
ux_test_media_read(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)729 UINT ux_test_media_read(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
730 {
731 
732 UINT    status =  0;
733 
734     if(lba == 0)
735     {
736         ram_disks[lun]->fx_media_driver_logical_sector =  0;
737         ram_disks[lun]->fx_media_driver_sectors =  1;
738         ram_disks[lun]->fx_media_driver_request =  FX_DRIVER_BOOT_READ;
739         ram_disks[lun]->fx_media_driver_buffer =  data_pointer;
740         _fx_ram_driver(ram_disks[lun]);
741         *(data_pointer) =  0xeb;
742         *(data_pointer+1) =  0x3c;
743         *(data_pointer+2) =  0x90;
744         *(data_pointer+21) =  0xF8;
745 
746         *(data_pointer+24) =  0x01;
747         *(data_pointer+26) =  0x10;
748         *(data_pointer+28) =  0x01;
749 
750         *(data_pointer+510) =  0x55;
751         *(data_pointer+511) =  0xaa;
752         ux_utility_memory_copy(data_pointer+0x36,"FAT12",5);
753 
754 
755         status = ram_disks[lun]->fx_media_driver_status;
756     }
757     else
758     {
759         while(number_blocks--)
760         {
761             status =  fx_media_read(ram_disks[lun],lba,data_pointer);
762             data_pointer+=512;
763             lba++;
764         }
765     }
766     return(status);
767 }
768 
ux_test_media_write(VOID * storage,ULONG lun,UCHAR * data_pointer,ULONG number_blocks,ULONG lba,ULONG * media_status)769 UINT ux_test_media_write(VOID *storage, ULONG lun, UCHAR * data_pointer, ULONG number_blocks, ULONG lba, ULONG *media_status)
770 {
771 
772 UINT    status =  0;
773 
774     if(lba == 0)
775     {
776         ram_disks[lun]->fx_media_driver_logical_sector =  0;
777         ram_disks[lun]->fx_media_driver_sectors =  1;
778         ram_disks[lun]->fx_media_driver_request =  FX_DRIVER_BOOT_WRITE;
779         ram_disks[lun]->fx_media_driver_buffer =  data_pointer;
780         _fx_ram_driver(ram_disks[lun]);
781 
782         status = ram_disks[lun]->fx_media_driver_status;
783 
784     }
785     else
786     {
787 
788         while(number_blocks--)
789         {
790             status =  fx_media_write(ram_disks[lun],lba,data_pointer);
791             data_pointer+=512;
792             lba++;
793         }
794         return(status);
795     }
796     return(status);
797 }
798