1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
10 /**************************************************************************/
11 
12 
13 /**************************************************************************/
14 /**************************************************************************/
15 /**                                                                       */
16 /** USBX Component                                                        */
17 /**                                                                       */
18 /**   Host Simulator Controller Driver                                    */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_hcd_sim_host.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_hcd_sim_host_periodic_tree_create               PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*     This function creates the periodic static tree for the interrupt   */
45 /*     and isochronous EDs.                                               */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    hcd_sim_host                          Pointer to host controller    */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_hcd_sim_host_ed_obtain            Obtain host ED                */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    Host Simulator Controller Driver                                    */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
68 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*                                                                        */
71 /**************************************************************************/
_ux_hcd_sim_host_periodic_tree_create(UX_HCD_SIM_HOST * hcd_sim_host)72 UINT  _ux_hcd_sim_host_periodic_tree_create(UX_HCD_SIM_HOST *hcd_sim_host)
73 {
74 
75 UX_HCD_SIM_HOST_ED      *ed;
76 UINT                    list_index;
77 UINT                    list_entries;
78 UINT                    current_list_entry;
79 UX_HCD_SIM_HOST_ED      *ed_list[32];
80 UX_HCD_SIM_HOST_ED      *ed_start_list[32];
81 
82 
83     /* Start with the 1st list - it has 32 entries.  */
84     list_entries =  32;
85 
86     /* Create each list one by one starting from the 32ms list.  */
87     for (list_index = 0; list_index < 6; list_index++)
88     {
89 
90         for (current_list_entry = 0; current_list_entry < list_entries; current_list_entry++)
91         {
92 
93             /* In each list, insert an static ED as the anchor. There should not
94                be any errors when obtaining a new ED, still we do a sanity check.  */
95             ed =  _ux_hcd_sim_host_ed_obtain(hcd_sim_host);
96             if (ed == UX_NULL)
97                 return(UX_NO_ED_AVAILABLE);
98 
99             /* Mark this anchor ED as static by putting it as SKIPPED, the host simulator  controller will
100                not look into its tail and head list and will simply jump to the next ED.  */
101             ed -> ux_sim_host_ed_status =  UX_HCD_SIM_HOST_ED_STATIC;
102 
103             /* Either we hook this new ED to the start list for further processing
104                or we hook it to the 2 successive entries in the previous list.  */
105             if (list_index == 0)
106             {
107 
108                 ed_start_list[current_list_entry] =  ed;
109             }
110             else
111             {
112 
113                 ed_list[current_list_entry * 2] -> ux_sim_host_ed_next_ed =  ed;
114                 ed_list[(current_list_entry * 2) + 1] -> ux_sim_host_ed_next_ed =  ed;
115             }
116 
117             /* Memorize this ED in the local list. We do this operation now, otherwise
118                we would erase the previous list EDs.  */
119             ed_list[current_list_entry] =  ed;
120         }
121 
122         /*  Shift the number of entries in the next list by 1 (i.e. divide by 2).  */
123         list_entries =  list_entries >> 1;
124     }
125 
126     /* The tree has been completed but the entries in the interrupt list are in the wrong order.
127        We need to swap each entry according to the host simulator specified entry order list
128        so that we have a fair interval frequency for each periodic ED. The primary EDs are
129        fetched from the start list, and stored in the interrupt list.  */
130     for (current_list_entry = 0; current_list_entry < 32; current_list_entry++)
131     {
132 
133         ed =  ed_start_list[_ux_system_host_hcd_periodic_tree_entries[current_list_entry]];
134         hcd_sim_host -> ux_hcd_sim_host_interrupt_ed_list[current_list_entry] =  ed;
135     }
136 
137     /* Return successful completion.  */
138     return(UX_SUCCESS);
139 }
140 
141