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