1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   HUB Class                                                           */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_hub.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_hub_activate                         PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function performs the enumeration of the HUB. The HUB          */
45 /*    descriptor is read, the interrupt endpoint activated, power is set  */
46 /*    to the downstream ports and the HUB instance will be awaken when    */
47 /*    there is a status change on the HUB or one of the ports.            */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    command                               Pointer to command            */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_class_hub_configure          Configure HUB                 */
60 /*    _ux_host_class_hub_descriptor_get     Get descriptor                */
61 /*    _ux_host_class_hub_interrupt_endpoint_start                         */
62 /*                                          Start interrupt endpoint      */
63 /*    _ux_host_class_hub_ports_power        Power ports                   */
64 /*    _ux_host_stack_class_instance_create  Create class instance         */
65 /*    _ux_host_stack_class_instance_destroy Destroy class instance        */
66 /*    _ux_utility_memory_allocate           Allocate memory block         */
67 /*    _ux_utility_memory_free               Free memory block             */
68 /*    _ux_host_semaphore_create             Create semaphore              */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    HUB Class                                                           */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
79 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            optimized based on compile  */
81 /*                                            definitions,                */
82 /*                                            resulting in version 6.1    */
83 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
84 /*                                            refined macros names,       */
85 /*                                            resulting in version 6.1.10 */
86 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
87 /*                                            added standalone support,   */
88 /*                                            resulting in version 6.1.12 */
89 /*                                                                        */
90 /**************************************************************************/
_ux_host_class_hub_activate(UX_HOST_CLASS_COMMAND * command)91 UINT  _ux_host_class_hub_activate(UX_HOST_CLASS_COMMAND *command)
92 {
93 
94 UX_DEVICE           *device;
95 UX_HOST_CLASS_HUB   *hub;
96 UINT                status;
97 
98 
99 #if UX_MAX_DEVICES > 1
100     /* We need to make sure that the enumeration thread knows about at least
101        one active HUB instance and the function to call when the thread
102        is awaken.  */
103     _ux_system_host -> ux_system_host_enum_hub_function =  _ux_host_class_hub_change_detect;
104 #endif
105 
106     /* The HUB is always activated by the device descriptor and not the
107        instance descriptor.  */
108     device =  (UX_DEVICE *) command -> ux_host_class_command_container;
109 
110     /* Instantiate this HUB class.  */
111     hub =  (UX_HOST_CLASS_HUB *) _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_HUB));
112     if (hub == UX_NULL)
113         return(UX_MEMORY_INSUFFICIENT);
114 
115     /* Store the class container into this instance.  */
116     hub -> ux_host_class_hub_class =  command -> ux_host_class_command_class_ptr;
117 
118     /* Store the device container instance in the HUB instance, this is for
119        the class instance when it needs to talk to the USBX stack.  */
120     hub -> ux_host_class_hub_device =  device;
121 
122 #if defined(UX_HOST_STANDALONE)
123 
124     /* Store the instance in the device container, this is for the USBX stack
125         when it needs to invoke the class.  */
126     device -> ux_device_class_instance =  (VOID *) hub;
127 
128     /* Store the hub interface.  */
129     hub -> ux_host_class_hub_interface = device ->
130             ux_device_first_configuration -> ux_configuration_first_interface;
131 
132     /* Store the class task function.  */
133     hub -> ux_host_class_hub_class -> ux_host_class_task_function = _ux_host_class_hub_tasks_run;
134 
135     /* During activation and tasks, control transfer is used for requests.  */
136     hub -> ux_host_class_hub_transfer = &device -> ux_device_control_endpoint.ux_endpoint_transfer_request;
137 
138     /* The HUB is configured and activated in ACTIVATE_WAIT.  */
139     hub -> ux_host_class_hub_run_status = UX_SUCCESS;
140     hub -> ux_host_class_hub_enum_state = UX_HOST_CLASS_HUB_ENUM_GET_STATUS;
141     status = UX_SUCCESS;
142 #else
143 
144     /* Configure the HUB.  */
145     status =  _ux_host_class_hub_configure(hub);
146     if (status == UX_SUCCESS)
147     {
148 
149         /* Get the HUB descriptor.  */
150         status =  _ux_host_class_hub_descriptor_get(hub);
151         if (status == UX_SUCCESS)
152         {
153 
154             /* Power up the HUB downstream ports. This function always returns
155                success since we may be dealing with multiple ports.  */
156             _ux_host_class_hub_ports_power(hub);
157 
158             /* Search the HUB interrupt endpoint and start it.  */
159             status =  _ux_host_class_hub_interrupt_endpoint_start(hub);
160             if (status == UX_SUCCESS)
161             {
162 
163                 /* Create this class instance.  */
164                 _ux_host_stack_class_instance_create(hub -> ux_host_class_hub_class, (VOID *) hub);
165 
166                 /* Store the instance in the device container, this is for the USBX stack
167                    when it needs to invoke the class.  */
168                 device -> ux_device_class_instance =  (VOID *) hub;
169 
170                 /* Mark the HUB as live now.  */
171                 hub -> ux_host_class_hub_state =  UX_HOST_CLASS_INSTANCE_LIVE;
172 
173                 /* If all is fine and the device is mounted, we may need to inform the application
174                    if a function has been programmed in the system structure.  */
175                 if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
176                 {
177 
178                     /* Call system change function.  */
179                     _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, hub -> ux_host_class_hub_class, (VOID *) hub);
180                 }
181 
182                 /* Return success.  */
183                 return(UX_SUCCESS);
184             }
185         }
186     }
187 
188     /* We get here when an error occurred.  */
189 
190     /* Free the hub instance.  */
191     _ux_utility_memory_free(hub);
192 
193     /* If trace is enabled, insert this event into the trace buffer.  */
194     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HUB_ACTIVATE, hub, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
195 
196     /* If trace is enabled, register this object.  */
197     UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, hub, 0, 0, 0)
198 
199 #endif
200 
201     /* Return completion status.  */
202     return(status);
203 }
204