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 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  COMPONENT DEFINITION                                   RELEASE        */
27 /*                                                                        */
28 /*    ux_hcd_sim_host.h                                   PORTABLE C      */
29 /*                                                           6.1.10       */
30 /*  AUTHOR                                                                */
31 /*                                                                        */
32 /*    Chaoqiong Xiao, Microsoft Corporation                               */
33 /*                                                                        */
34 /*  DESCRIPTION                                                           */
35 /*                                                                        */
36 /*    This file contains all the header and extern functions used by the  */
37 /*    USBX host simulator. It is designed to work ONLY with the USBX      */
38 /*    device (slave) simulator.                                           */
39 /*                                                                        */
40 /*  RELEASE HISTORY                                                       */
41 /*                                                                        */
42 /*    DATE              NAME                      DESCRIPTION             */
43 /*                                                                        */
44 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
45 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
46 /*                                            added controller disable,   */
47 /*                                            used UX prefix to refer to  */
48 /*                                            TX symbols instead of using */
49 /*                                            them directly,              */
50 /*                                            resulting in version 6.1    */
51 /*  11-09-2020     Chaoqiong Xiao           Modified comment(s),          */
52 /*                                            added HCD uninitialize,     */
53 /*                                            resulting in version 6.1.2  */
54 /*  04-02-2021     Chaoqiong Xiao           Modified comment(s),          */
55 /*                                            added port status variable, */
56 /*                                            resulting in version 6.1.6  */
57 /*  08-02-2021     Chaoqiong Xiao           Modified comment(s),          */
58 /*                                            added extern "C" keyword    */
59 /*                                            for compatibility with C++, */
60 /*                                            resulting in version 6.1.8  */
61 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
62 /*                                            added standalone support,   */
63 /*                                            resulting in version 6.1.10 */
64 /*                                                                        */
65 /**************************************************************************/
66 
67 #ifndef UX_HCD_SIM_HOST_H
68 #define UX_HCD_SIM_HOST_H
69 
70 /* Determine if a C++ compiler is being used.  If so, ensure that standard
71    C is used to process the API information.  */
72 
73 #ifdef   __cplusplus
74 
75 /* Yes, C++ compiler is present.  Use standard C.  */
76 extern   "C" {
77 
78 #endif
79 
80 
81 /* Define simulator host generic definitions.  */
82 
83 #define UX_HCD_SIM_HOST_CONTROLLER                              99
84 #define UX_HCD_SIM_HOST_MAX_PAYLOAD                             4096
85 #define UX_HCD_SIM_HOST_FRAME_DELAY                             4
86 #define UX_HCD_SIM_HOST_PERIODIC_ENTRY_NB                       32
87 #define UX_HCD_SIM_HOST_PERIODIC_ENTRY_MASK                     0x1f
88 #define UX_HCD_SIM_HOST_AVAILABLE_BANDWIDTH                     6000
89 
90 
91 
92 /* Define simulator host completion code errors.  */
93 
94 #define UX_HCD_SIM_HOST_NO_ERROR                                0x00
95 #define UX_HCD_SIM_HOST_ERROR_CRC                               0x01
96 #define UX_HCD_SIM_HOST_ERROR_BIT_STUFFING                      0x02
97 #define UX_HCD_SIM_HOST_ERROR_DATA_TOGGLE                       0x03
98 #define UX_HCD_SIM_HOST_ERROR_STALL                             0x04
99 #define UX_HCD_SIM_HOST_ERROR_DEVICE_NOT_RESPONDING             0x05
100 #define UX_HCD_SIM_HOST_ERROR_PID_FAILURE                       0x06
101 #define UX_HCD_SIM_HOST_ERROR_PID_UNEXPECTED                    0x07
102 #define UX_HCD_SIM_HOST_ERROR_DATA_OVERRRUN                     0x08
103 #define UX_HCD_SIM_HOST_ERROR_DATA_UNDERRUN                     0x09
104 #define UX_HCD_SIM_HOST_ERROR_BUFFER_OVERRRUN                   0x0c
105 #define UX_HCD_SIM_HOST_ERROR_BUFFER_UNDERRUN                   0x0d
106 #define UX_HCD_SIM_HOST_NOT_ACCESSED                            0x0e
107 #define UX_HCD_SIM_HOST_NAK                                     0x0f
108 
109 
110 /* Define simulator host structure.  */
111 
112 typedef struct UX_HCD_SIM_HOST_STRUCT
113 {
114 
115     struct UX_HCD_STRUCT
116                     *ux_hcd_sim_host_hcd_owner;
117     ULONG           ux_hcd_sim_host_hcor;
118     UINT            ux_hcd_sim_host_nb_root_hubs;
119     ULONG           ux_hcd_sim_host_port_status[1];
120     struct UX_HCD_SIM_HOST_ED_STRUCT
121                     *ux_hcd_sim_host_ed_list;
122     struct UX_HCD_SIM_HOST_TD_STRUCT
123                     *ux_hcd_sim_host_td_list;
124     struct UX_HCD_SIM_HOST_ISO_TD_STRUCT
125                     *ux_hcd_sim_host_iso_td_list;
126     struct UX_HCD_SIM_HOST_ED_STRUCT
127                     *ux_hcd_sim_host_asynch_head_ed;
128     struct UX_HCD_SIM_HOST_ED_STRUCT
129                     *ux_hcd_sim_host_asynch_current_ed;
130     struct UX_HCD_SIM_HOST_ED_STRUCT
131                     *ux_hcd_sim_host_iso_head_ed;
132     struct UX_HCD_SIM_HOST_ED_STRUCT
133                     *ux_hcd_sim_host_interrupt_ed_list[32];
134     UINT            ux_hcd_sim_host_queue_empty;
135     UINT            ux_hcd_sim_host_periodic_scheduler_active;
136     UINT            ux_hcd_sim_host_interruptible;
137     ULONG           ux_hcd_sim_host_interrupt_count;
138 #if !defined(UX_HOST_STANDALONE)
139     UX_TIMER        ux_hcd_sim_host_timer;
140 #endif
141 } UX_HCD_SIM_HOST;
142 
143 
144 /* Define simulator host ED structure.  */
145 
146 typedef struct UX_HCD_SIM_HOST_ED_STRUCT
147 {
148 
149     struct UX_HCD_SIM_HOST_TD_STRUCT
150                     *ux_sim_host_ed_tail_td;
151     struct UX_HCD_SIM_HOST_TD_STRUCT
152                     *ux_sim_host_ed_head_td;
153     struct UX_HCD_SIM_HOST_ED_STRUCT
154                     *ux_sim_host_ed_next_ed;
155     struct UX_HCD_SIM_HOST_ED_STRUCT
156                     *ux_sim_host_ed_previous_ed;
157     ULONG           ux_sim_host_ed_status;
158     struct UX_ENDPOINT_STRUCT
159                     *ux_sim_host_ed_endpoint;
160     ULONG           ux_sim_host_ed_toggle;
161     ULONG           ux_sim_host_ed_frame;
162 } UX_HCD_SIM_HOST_ED;
163 
164 
165 /* Define simulator host ED bitmap.  */
166 
167 #define UX_HCD_SIM_HOST_ED_STATIC                               0x80000000u
168 #define UX_HCD_SIM_HOST_ED_SKIP                                 0x40000000u
169 #define UX_HCD_SIM_HOST_ED_TRANSFER                             0x00100000u
170 
171 
172 /* Define simulator host TD structure.  */
173 
174 typedef struct UX_HCD_SIM_HOST_TD_STRUCT
175 {
176 
177     UCHAR *         ux_sim_host_td_buffer;
178     ULONG           ux_sim_host_td_length;
179     struct UX_HCD_SIM_HOST_TD_STRUCT
180                     *ux_sim_host_td_next_td;
181     struct UX_TRANSFER_STRUCT
182                     *ux_sim_host_td_transfer_request;
183     struct UX_HCD_SIM_HOST_TD_STRUCT
184                     *ux_sim_host_td_next_td_transfer_request;
185     struct UX_HCD_SIM_HOST_ED_STRUCT
186                     *ux_sim_host_td_ed;
187     ULONG           ux_sim_host_td_actual_length;
188     ULONG           ux_sim_host_td_status;
189     ULONG           ux_sim_host_td_direction;
190     ULONG           ux_sim_host_td_toggle;
191 } UX_HCD_SIM_HOST_TD;
192 
193 
194 /* Define simulator host TD bitmap.  */
195 
196 #define UX_HCD_SIM_HOST_TD_SETUP_PHASE                          0x00010000
197 #define UX_HCD_SIM_HOST_TD_DATA_PHASE                           0x00020000
198 #define UX_HCD_SIM_HOST_TD_STATUS_PHASE                         0x00040000
199 #define UX_HCD_SIM_HOST_TD_OUT                                  0x00000800
200 #define UX_HCD_SIM_HOST_TD_IN                                   0x00001000
201 #define UX_HCD_SIM_HOST_TD_ACK_PENDING                          0x00002000
202 #define UX_HCD_SIM_HOST_TD_TOGGLE_FROM_ED                       0x80000000
203 
204 
205 /* Define simulator host ISOCHRONOUS TD structure.  */
206 
207 typedef struct UX_HCD_SIM_HOST_ISO_TD_STRUCT
208 {
209 
210     UCHAR *         ux_sim_host_iso_td_buffer;
211     ULONG           ux_sim_host_iso_td_length;
212     struct UX_HCD_SIM_HOST_ISO_TD_STRUCT
213                     *ux_sim_host_iso_td_next_td;
214     struct UX_TRANSFER_STRUCT
215                     *ux_sim_host_iso_td_transfer_request;
216     struct UX_HCD_SIM_HOST_ISO_TD_STRUCT
217                     *ux_sim_host_iso_td_next_td_transfer_request;
218     struct UX_HCD_SIM_HOST_ED_STRUCT
219                     *ux_sim_host_iso_td_ed;
220     ULONG           ux_sim_host_iso_td_actual_length;
221     ULONG           ux_sim_host_iso_td_status;
222     ULONG           ux_sim_host_iso_td_direction;
223 } UX_HCD_SIM_HOST_ISO_TD;
224 
225 
226 /* Define simulator host function prototypes.  */
227 
228 VOID    _ux_hcd_sim_host_asynch_queue_process(UX_HCD_SIM_HOST *hcd_sim_host);
229 VOID    _ux_hcd_sim_host_asynch_schedule(UX_HCD_SIM_HOST *hcd_sim_host);
230 UINT    _ux_hcd_sim_host_asynchronous_endpoint_create(UX_HCD_SIM_HOST *hcd_sim_host, UX_ENDPOINT *endpoint);
231 UINT    _ux_hcd_sim_host_asynchronous_endpoint_destroy(UX_HCD_SIM_HOST *hcd_sim_host, UX_ENDPOINT *endpoint);
232 UX_HCD_SIM_HOST_ED
233         *_ux_hcd_sim_host_ed_obtain(UX_HCD_SIM_HOST *hcd_sim_host);
234 VOID    _ux_hcd_sim_host_ed_td_clean(UX_HCD_SIM_HOST_ED *ed);
235 UINT    _ux_hcd_sim_host_endpoint_reset(UX_HCD_SIM_HOST *hcd_sim_host, UX_ENDPOINT *endpoint);
236 UINT    _ux_hcd_sim_host_entry(UX_HCD *hcd, UINT function, VOID *parameter);
237 UINT    _ux_hcd_sim_host_frame_number_get(UX_HCD_SIM_HOST *hcd_sim_host, ULONG *frame_number);
238 VOID    _ux_hcd_sim_host_frame_number_set(UX_HCD_SIM_HOST *hcd_sim_host, ULONG frame_number);
239 UINT    _ux_hcd_sim_host_initialize(UX_HCD *hcd);
240 UINT    _ux_hcd_sim_host_uninitialize(UX_HCD_SIM_HOST *hcd);
241 UINT    _ux_hcd_sim_host_controller_disable(UX_HCD_SIM_HOST *hcd);
242 UINT    _ux_hcd_sim_host_interrupt_endpoint_create(UX_HCD_SIM_HOST *hcd_sim_host, UX_ENDPOINT *endpoint);
243 VOID    _ux_hcd_sim_host_iso_queue_process(UX_HCD_SIM_HOST *hcd_sim_host);
244 VOID    _ux_hcd_sim_host_iso_schedule(UX_HCD_SIM_HOST *hcd_sim_host);
245 UINT    _ux_hcd_sim_host_isochronous_endpoint_create(UX_HCD_SIM_HOST *hcd_sim_host, UX_ENDPOINT *endpoint);
246 UX_HCD_SIM_HOST_ISO_TD
247         *_ux_hcd_sim_host_isochronous_td_obtain(UX_HCD_SIM_HOST *hcd_sim_host);
248 UX_HCD_SIM_HOST_ED
249         *_ux_hcd_sim_host_least_traffic_list_get(UX_HCD_SIM_HOST *hcd_sim_host);
250 UINT    _ux_hcd_sim_host_periodic_endpoint_destroy(UX_HCD_SIM_HOST *hcd_sim_host, UX_ENDPOINT *endpoint);
251 VOID    _ux_hcd_sim_host_periodic_schedule(UX_HCD_SIM_HOST *hcd_sim_host);
252 UINT    _ux_hcd_sim_host_periodic_tree_create(UX_HCD_SIM_HOST *hcd_sim_host);
253 ULONG   _ux_hcd_sim_host_port_status_get(UX_HCD_SIM_HOST *hcd_sim_host, ULONG port_index);
254 UX_HCD_SIM_HOST_TD
255         *_ux_hcd_sim_host_regular_td_obtain(UX_HCD_SIM_HOST *hcd_sim_host);
256 UINT    _ux_hcd_sim_host_request_bulk_transfer(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request);
257 UINT    _ux_hcd_sim_host_request_control_transfer(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request);
258 UINT    _ux_hcd_sim_host_request_interrupt_transfer(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request);
259 UINT    _ux_hcd_sim_host_request_isochronous_transfer(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request);
260 UINT    _ux_hcd_sim_host_request_transfer(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request);
261 VOID    _ux_hcd_sim_host_timer_function(ULONG hcd_sim_host_addr);
262 UINT    _ux_hcd_sim_host_transaction_schedule(UX_HCD_SIM_HOST *hcd_sim_host, UX_HCD_SIM_HOST_ED *ed);
263 UINT    _ux_hcd_sim_host_transfer_abort(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request);
264 UINT    _ux_hcd_sim_host_port_reset(UX_HCD_SIM_HOST *hcd_sim_host, ULONG port_index);
265 
266 UINT    _ux_hcd_sim_host_transfer_run(UX_HCD_SIM_HOST *hcd_sim_host, UX_TRANSFER *transfer_request);
267 
268 /* Define Device Simulator Class API prototypes.  */
269 
270 #define ux_hcd_sim_host_initialize                 _ux_hcd_sim_host_initialize
271 /* Determine if a C++ compiler is being used.  If so, complete the standard
272    C conditional started above.  */
273 #ifdef __cplusplus
274 }
275 #endif
276 
277 #endif
278 
279