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