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_deactivate                       PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is called when this instance of the HUB has been      */
45 /*    removed from the bus either directly or indirectly. The interrupt   */
46 /*    pipe will be destroyed and the instance removed.                    */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    command                               Pointer to class command      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_class_instance_destroy Destroy class instance        */
59 /*    _ux_host_stack_device_remove          Remove device                 */
60 /*    _ux_host_stack_endpoint_transfer_abort                              */
61 /*                                          Abort transfer                */
62 /*    _ux_utility_memory_free               Release memory block          */
63 /*    _ux_host_semaphore_get                Get semaphore                 */
64 /*    _ux_host_semaphore_put                Release semaphore             */
65 /*    _ux_utility_thread_schedule_other     Schedule other threads        */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    HUB Class                                                           */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            optimized based on compile  */
78 /*                                            definitions,                */
79 /*                                            resulting in version 6.1    */
80 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            refined macros names,       */
82 /*                                            resulting in version 6.1.10 */
83 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
84 /*                                            added standalone support,   */
85 /*                                            resulting in version 6.1.12 */
86 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
87 /*                                            improved deactivate flow,   */
88 /*                                            resulting in version 6.3.0  */
89 /*                                                                        */
90 /**************************************************************************/
_ux_host_class_hub_deactivate(UX_HOST_CLASS_COMMAND * command)91 UINT  _ux_host_class_hub_deactivate(UX_HOST_CLASS_COMMAND *command)
92 {
93 
94 UX_HOST_CLASS_HUB       *hub;
95 UX_HCD                  *hcd;
96 UX_TRANSFER             *transfer_request = UX_NULL;
97 UINT                    port_index;
98 
99 
100     /* Get the instance to the class.  */
101     hub =  (UX_HOST_CLASS_HUB *) command -> ux_host_class_command_instance;
102 
103     /* Get the HCD used by this instance.  */
104     hcd = UX_DEVICE_HCD_GET(hub -> ux_host_class_hub_device);
105 
106     /* The HUB is being shut down.  */
107     hub -> ux_host_class_hub_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;
108 
109     /* We need to abort transactions on the interrupt pipe.  */
110 #if defined(UX_HOST_STANDALONE)
111     if (hub -> ux_host_class_hub_interrupt_endpoint)
112 #endif
113     {
114         _ux_host_stack_endpoint_transfer_abort(hub -> ux_host_class_hub_interrupt_endpoint);
115 
116         /* If the Hub class instance has a interrupt pipe with a data payload associated with it
117            it must be freed.  First get the transfer request. */
118         transfer_request =  &hub -> ux_host_class_hub_interrupt_endpoint -> ux_endpoint_transfer_request;
119     }
120 
121     /* Each device which is downstream on the HUB ports must be removed.  */
122     for (port_index = 1; port_index <= hub -> ux_host_class_hub_descriptor.bNbPorts; port_index++)
123     {
124 
125         /* Is there a device on this port?  */
126         if (hub -> ux_host_class_hub_port_state & (1UL << port_index))
127         {
128 
129             /* The stack will remove the device and its resources.  */
130             _ux_host_stack_device_remove(hcd, hub -> ux_host_class_hub_device, port_index);
131         }
132     }
133 
134 
135     /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
136        endpoints to exit properly.  */
137     _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
138 
139     /* Then de allocate the memory.  */
140 #if defined(UX_HOST_STANDALONE)
141     if (transfer_request)
142 #endif
143     _ux_utility_memory_free(transfer_request -> ux_transfer_request_data_pointer);
144 
145 #if defined(UX_HOST_STANDALONE)
146     if (hub -> ux_host_class_hub_allocated)
147     {
148         _ux_utility_memory_free(hub -> ux_host_class_hub_allocated);
149     }
150 #endif
151 
152     /* Destroy the instance.  */
153     _ux_host_stack_class_instance_destroy(hub -> ux_host_class_hub_class, (VOID *) hub);
154 
155     /* Before we free the device resources, we need to inform the application
156         that the device is removed.  */
157     if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
158     {
159 
160         /* Inform the application the device is removed.  */
161         _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, hub -> ux_host_class_hub_class, (VOID *) hub);
162     }
163 
164     /* If trace is enabled, insert this event into the trace buffer.  */
165     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_HUB_DEACTIVATE, hub, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
166 
167     /* If trace is enabled, register this object.  */
168     UX_TRACE_OBJECT_UNREGISTER(hub);
169 
170 #if defined(UX_HOST_STANDALONE)
171 
172     /* Unlink from device class instance.  */
173     hub -> ux_host_class_hub_device -> ux_device_class_instance =  (VOID *) hub;
174 #endif
175 
176     /* Free the memory block used by the class.  */
177     _ux_utility_memory_free(hub);
178 
179     /* Return successful completion.  */
180     return(UX_SUCCESS);
181 }
182