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 /**   HUB Class                                                           */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 
24 /**************************************************************************/
25 /*                                                                        */
26 /*  COMPONENT DEFINITION                                   RELEASE        */
27 /*                                                                        */
28 /*    ux_host_class_hub.h                                 PORTABLE C      */
29 /*                                                           6.1.12       */
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 HUB class.                                                     */
38 /*                                                                        */
39 /*  RELEASE HISTORY                                                       */
40 /*                                                                        */
41 /*    DATE              NAME                      DESCRIPTION             */
42 /*                                                                        */
43 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
44 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
45 /*                                            resulting in version 6.1    */
46 /*  08-02-2021     Wen Wang                 Modified comment(s),          */
47 /*                                            added extern "C" keyword    */
48 /*                                            for compatibility with C++, */
49 /*                                            resulting in version 6.1.8  */
50 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
51 /*                                            added standalone support,   */
52 /*                                            resulting in version 6.1.12 */
53 /*                                                                        */
54 /**************************************************************************/
55 
56 #ifndef UX_HOST_CLASS_HUB_H
57 #define UX_HOST_CLASS_HUB_H
58 
59 /* Determine if a C++ compiler is being used.  If so, ensure that standard
60    C is used to process the API information.  */
61 
62 #ifdef   __cplusplus
63 
64 /* Yes, C++ compiler is present.  Use standard C.  */
65 extern   "C" {
66 
67 #endif
68 
69 
70 /* Define HUB Class constants.  */
71 
72 #define UX_HOST_CLASS_HUB_CLASS                                 9
73 #define UX_HOST_CLASS_HUB_PROTOCOL_FS                           0
74 #define UX_HOST_CLASS_HUB_PROTOCOL_SINGLE_TT                    1
75 #define UX_HOST_CLASS_HUB_PROTOCOL_MULTIPLE_TT                  2
76 
77 
78 /* Define HUB Class descriptor field constants.  */
79 
80 #define UX_HOST_CLASS_HUB_GANG_POWER_SWITCHING                  0x00
81 #define UX_HOST_CLASS_HUB_INDIVIDUAL_POWER_SWITCHING            0x01
82 #define UX_HOST_CLASS_HUB_NO_POWER_SWITCHING                    0x02
83 
84 #define UX_HOST_CLASS_HUB_COMPOUND_DEVICE                       0x04
85 
86 #define UX_HOST_CLASS_HUB_GLOBAL_OVERCURRENT                    0x00
87 #define UX_HOST_CLASS_HUB_INDIVIDUAL_OVERCURRENT                0x08
88 #define UX_HOST_CLASS_HUB_NO_OVERCURRENT                        0x10
89 
90 
91 /* Define HUB Class command constants.  */
92 
93 #define UX_HOST_CLASS_HUB_GET_STATUS                            0x00
94 #define UX_HOST_CLASS_HUB_CLEAR_FEATURE                         0x01
95 #define UX_HOST_CLASS_HUB_GET_STATE                             0x02
96 #define UX_HOST_CLASS_HUB_SET_FEATURE                           0x03
97 #define UX_HOST_CLASS_HUB_GET_DESCRIPTOR                        0x06
98 #define UX_HOST_CLASS_HUB_SET_DESCRIPTOR                        0x07
99 
100 
101 /* Define HUB Class set_feature command constants.  */
102 
103 #define UX_HOST_CLASS_HUB_PORT_CONNECTION                       0x00
104 #define UX_HOST_CLASS_HUB_PORT_ENABLE                           0x01
105 #define UX_HOST_CLASS_HUB_PORT_SUSPEND                          0x02
106 #define UX_HOST_CLASS_HUB_PORT_OVER_CURRENT                     0x03
107 #define UX_HOST_CLASS_HUB_PORT_RESET                            0x04
108 #define UX_HOST_CLASS_HUB_PORT_POWER                            0x08
109 #define UX_HOST_CLASS_HUB_PORT_LOW_SPEED                        0x09
110 #define UX_HOST_CLASS_HUB_C_PORT_CONNECTION                     0x10
111 #define UX_HOST_CLASS_HUB_C_PORT_ENABLE                         0x11
112 #define UX_HOST_CLASS_HUB_C_PORT_SUSPEND                        0x12
113 #define UX_HOST_CLASS_HUB_C_PORT_OVER_CURRENT                   0x13
114 #define UX_HOST_CLASS_HUB_C_PORT_RESET                          0x14
115 
116 
117 /* Define HUB Class port status constants.  */
118 
119 #define UX_HOST_CLASS_HUB_PORT_STATUS_CONNECTION                0x0001
120 #define UX_HOST_CLASS_HUB_PORT_STATUS_ENABLE                    0x0002
121 #define UX_HOST_CLASS_HUB_PORT_STATUS_SUSPEND                   0x0004
122 #define UX_HOST_CLASS_HUB_PORT_STATUS_OVER_CURRENT              0x0008
123 #define UX_HOST_CLASS_HUB_PORT_STATUS_RESET                     0x0010
124 #define UX_HOST_CLASS_HUB_PORT_STATUS_POWER                     0x0100
125 #define UX_HOST_CLASS_HUB_PORT_STATUS_LOW_SPEED                 0x0200
126 #define UX_HOST_CLASS_HUB_PORT_STATUS_HIGH_SPEED                0x0400
127 
128 
129 /* Define HUB Class port change constants.  */
130 
131 #define UX_HOST_CLASS_HUB_PORT_CHANGE_CONNECTION                0x00001u
132 #define UX_HOST_CLASS_HUB_PORT_CHANGE_ENABLE                    0x00002u
133 #define UX_HOST_CLASS_HUB_PORT_CHANGE_SUSPEND                   0x00004u
134 #define UX_HOST_CLASS_HUB_PORT_CHANGE_OVER_CURRENT              0x00008u
135 #define UX_HOST_CLASS_HUB_PORT_CHANGE_RESET                     0x00010u
136 
137 
138 /* Define HUB Class other constants.  */
139 
140 #define UX_HOST_CLASS_HUB_ENABLE_RETRY_COUNT                    3
141 #define UX_HOST_CLASS_HUB_ENABLE_RETRY_DELAY                    100
142 #define UX_HOST_CLASS_HUB_ENUMERATION_RETRY                     3
143 #define UX_HOST_CLASS_HUB_ENUMERATION_DEBOUNCE_DELAY            100
144 #define UX_HOST_CLASS_HUB_ENUMERATION_RESET_RECOVERY_DELAY      10
145 #define UX_HOST_CLASS_HUB_ENUMERATION_RETRY_DELAY               300
146 
147 
148 /* Define HUB Descriptor.  */
149 #define UX_HUB_DESCRIPTOR_ENTRIES                               8
150 #define UX_HUB_DESCRIPTOR_LENGTH                                9
151 
152 /* Define HUB Class structure.  */
153 
154 #define UX_MAX_HUB_PORTS                                        15
155 
156 
157 /* Define HUB state machine states.  */
158 
159 #define UX_HOST_CLASS_HUB_ENUM_GET_STATUS                       (UX_STATE_STEP + 0)
160 #define UX_HOST_CLASS_HUB_ENUM_POWER_CHECK                      (UX_STATE_STEP + 1)
161 #define UX_HOST_CLASS_HUB_ENUM_SET_CONFIG                       (UX_STATE_STEP + 2)
162 #define UX_HOST_CLASS_HUB_ENUM_SET_CONFIG_DONE                  (UX_STATE_STEP + 3)
163 #define UX_HOST_CLASS_HUB_ENUM_GET_HUB_DESC                     (UX_STATE_STEP + 4)
164 #define UX_HOST_CLASS_HUB_ENUM_GET_HUB_DESC_DONE                (UX_STATE_STEP + 5)
165 #define UX_HOST_CLASS_HUB_ENUM_PORT_POWER                       (UX_STATE_STEP + 6)
166 #define UX_HOST_CLASS_HUB_ENUM_PORT_POWER_DELAY                 (UX_STATE_STEP + 7)
167 #define UX_HOST_CLASS_HUB_ENUM_PORT_POWER_ON                    (UX_STATE_STEP + 8)
168 #define UX_HOST_CLASS_HUB_ENUM_PORT_NEXT                        (UX_STATE_STEP + 9)
169 #define UX_HOST_CLASS_HUB_ENUM_INTERRUPT_START                  (UX_STATE_STEP + 10)
170 #define UX_HOST_CLASS_HUB_ENUM_DONE                             (UX_STATE_STEP + 11)
171 #define UX_HOST_CLASS_HUB_ENUM_TRANS_WAIT                       (UX_STATE_STEP + 12)
172 #define UX_HOST_CLASS_HUB_ENUM_DELAY_WAIT                       (UX_STATE_STEP + 13)
173 
174 #define UX_HOST_CLASS_HUB_CHANGE_CHECK                          (UX_STATE_STEP + 0)
175 #define UX_HOST_CLASS_HUB_CHANGE_NEXT                           (UX_STATE_STEP + 1)
176 #define UX_HOST_CLASS_HUB_RESET                                 (UX_STATE_STEP + 2)
177 #define UX_HOST_CLASS_HUB_STATUS_GET                            (UX_STATE_STEP + 3)
178 #define UX_HOST_CLASS_HUB_STATUS_GET_DONE                       (UX_STATE_STEP + 4)
179 #define UX_HOST_CLASS_HUB_STATUS_PROCESS                        (UX_STATE_STEP + 5)
180 #define UX_HOST_CLASS_HUB_RESET_PROCESS                         (UX_STATE_STEP + 6)
181 #define UX_HOST_CLASS_HUB_CONNECT_PROCESS                       (UX_STATE_STEP + 7)
182 #define UX_HOST_CLASS_HUB_DISC_DISABLED                         (UX_STATE_STEP + 8)
183 #define UX_HOST_CLASS_HUB_DISC_CLEAR_1                          (UX_STATE_STEP + 9)
184 #define UX_HOST_CLASS_HUB_TRANS_WAIT                            (UX_STATE_STEP + 10)
185 #define UX_HOST_CLASS_HUB_DELAY_WAIT                            (UX_STATE_STEP + 11)
186 
187 
188 typedef struct UX_HUB_DESCRIPTOR_STRUCT
189 {
190 
191     ULONG           bLength;
192     ULONG           bDescriptorType;
193     ULONG           bNbPorts;
194     ULONG           wHubCharacteristics;
195     ULONG           bPwrOn2PwrGood;
196     ULONG           bHubContrCurrent;
197     ULONG           bDeviceRemovable;
198     ULONG           bPortPwrCtrlMask;
199 } UX_HUB_DESCRIPTOR;
200 
201 
202 /* Define HUB Class instance structure.  */
203 
204 typedef struct UX_HOST_CLASS_HUB_STRUCT
205 {
206 
207     struct UX_HOST_CLASS_HUB_STRUCT
208                     *ux_host_class_hub_next_instance;
209     UX_HOST_CLASS   *ux_host_class_hub_class;
210     UX_DEVICE       *ux_host_class_hub_device;
211     UX_ENDPOINT     *ux_host_class_hub_interrupt_endpoint;
212     UX_INTERFACE    *ux_host_class_hub_interface;
213     UINT            ux_host_class_hub_instance_status;
214     UINT            ux_host_class_hub_state;
215     UINT            ux_host_class_hub_change_semaphore;
216     struct UX_HUB_DESCRIPTOR_STRUCT
217                     ux_host_class_hub_descriptor;
218     UINT            ux_host_class_hub_port_state;
219     UINT            ux_host_class_hub_port_power;
220 
221 #if defined(UX_HOST_STANDALONE)
222     UINT            ux_host_class_hub_run_status;
223     UCHAR           *ux_host_class_hub_allocated;
224     UX_TRANSFER     *ux_host_class_hub_transfer;
225 
226     USHORT          ux_host_class_hub_run_port_change;
227     USHORT          ux_host_class_hub_run_port_status;
228 
229     ULONG           ux_host_class_hub_wait_start;
230     ULONG           ux_host_class_hub_wait_ms;
231 
232     UCHAR           ux_host_class_hub_enum_state;
233     UCHAR           ux_host_class_hub_run_state;
234     UCHAR           ux_host_class_hub_next_state;
235     UCHAR           ux_host_class_hub_run_port;
236 #endif
237 } UX_HOST_CLASS_HUB;
238 
239 
240 /* Define HUB Class function prototypes.  */
241 
242 UINT    _ux_host_class_hub_activate(UX_HOST_CLASS_COMMAND *command);
243 VOID    _ux_host_class_hub_change_detect(VOID);
244 UINT    _ux_host_class_hub_change_process(UX_HOST_CLASS_HUB *hub);
245 UINT    _ux_host_class_hub_configure(UX_HOST_CLASS_HUB *hub);
246 UINT    _ux_host_class_hub_deactivate(UX_HOST_CLASS_COMMAND *command);
247 UINT    _ux_host_class_hub_descriptor_get(UX_HOST_CLASS_HUB *hub);
248 UINT    _ux_host_class_hub_entry(UX_HOST_CLASS_COMMAND *command);
249 UINT    _ux_host_class_hub_feature(UX_HOST_CLASS_HUB *hub, UINT port, UINT command, UINT function);
250 UINT    _ux_host_class_hub_hub_change_process(UX_HOST_CLASS_HUB *hub);
251 UINT    _ux_host_class_hub_interrupt_endpoint_start(UX_HOST_CLASS_HUB *hub);
252 VOID    _ux_host_class_hub_port_change_connection_process(UX_HOST_CLASS_HUB *hub, UINT port, UINT port_status);
253 VOID    _ux_host_class_hub_port_change_enable_process(UX_HOST_CLASS_HUB *hub, UINT port, UINT port_status);
254 VOID    _ux_host_class_hub_port_change_over_current_process(UX_HOST_CLASS_HUB *hub, UINT port, UINT port_status);
255 UINT    _ux_host_class_hub_port_change_process(UX_HOST_CLASS_HUB *hub, UINT port);
256 VOID    _ux_host_class_hub_port_change_reset_process(UX_HOST_CLASS_HUB *hub, UINT port, UINT port_status);
257 VOID    _ux_host_class_hub_port_change_suspend_process(UX_HOST_CLASS_HUB *hub, UINT port, UINT port_status);
258 UINT    _ux_host_class_hub_port_reset(UX_HOST_CLASS_HUB *hub, UINT port);
259 UINT    _ux_host_class_hub_ports_power(UX_HOST_CLASS_HUB *hub);
260 UINT    _ux_host_class_hub_status_get(UX_HOST_CLASS_HUB *hub, UINT port, USHORT *port_status, USHORT *port_change);
261 VOID    _ux_host_class_hub_transfer_request_completed(UX_TRANSFER *transfer_request);
262 
263 UINT    _ux_host_class_hub_tasks_run(UX_HOST_CLASS *hub_class);
264 
265 /* Determine if a C++ compiler is being used.  If so, complete the standard
266    C conditional started above.  */
267 #ifdef __cplusplus
268 }
269 #endif
270 
271 #endif
272 
273