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