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 /**   Asix 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_asix.h"
30 #include "ux_host_stack.h"
31 
32 
33 #if !defined(UX_HOST_STANDALONE)
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_host_class_asix_deactivate                      PORTABLE C      */
39 /*                                                           6.2.0        */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Chaoqiong Xiao, Microsoft Corporation                               */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function is called when this instance of the asix has been     */
47 /*    removed from the bus either directly or indirectly. The bulk in\out */
48 /*    and interrupt pipes will be destroyed and the instance              */
49 /*    removed.                                                            */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    command                              Asix class command pointer     */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_class_instance_destroy Destroy the class instance    */
62 /*    _ux_host_stack_endpoint_transfer_abort Abort endpoint transfer      */
63 /*    _ux_utility_memory_free               Free memory block             */
64 /*    _ux_host_semaphore_get                Get protection semaphore      */
65 /*    _ux_host_semaphore_delete             Delete protection semaphore   */
66 /*    _ux_utility_thread_delete             Delete thread                 */
67 /*    _ux_network_driver_deactivate         Deactivate NetX USB interface */
68 /*    nx_packet_transmit_release            Release NetX packet           */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    _ux_host_class_asix_entry             Entry of asix 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 /*                                            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 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
85 /*                                            internal clean up,          */
86 /*                                            fixed standalone compile,   */
87 /*                                            resulting in version 6.1.11 */
88 /*  10-31-2022     Chaoqiong Xiao           Modified comment(s),          */
89 /*                                            supported NX packet chain,  */
90 /*                                            added reception buffer,     */
91 /*                                            removed internal NX pool,   */
92 /*                                            resulting in version 6.2.0  */
93 /*                                                                        */
94 /**************************************************************************/
_ux_host_class_asix_deactivate(UX_HOST_CLASS_COMMAND * command)95 UINT  _ux_host_class_asix_deactivate(UX_HOST_CLASS_COMMAND *command)
96 {
97 
98 UX_HOST_CLASS_ASIX          *asix;
99 UX_TRANSFER                 *transfer_request;
100 NX_PACKET                   *current_packet;
101 NX_PACKET                   *next_packet;
102 UINT                        status;
103 
104     /* Get the instance for this class.  */
105     asix =  (UX_HOST_CLASS_ASIX *) command -> ux_host_class_command_instance;
106 
107     /* Protect thread reentry to this instance.  */
108     status =  _ux_host_semaphore_get(&asix -> ux_host_class_asix_semaphore, UX_WAIT_FOREVER);
109     if (status != UX_SUCCESS)
110 
111         /* Return error.  */
112         return(status);
113 
114     /* The asix is being shut down.  */
115     asix -> ux_host_class_asix_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;
116 
117 
118     /* Check if link was up.  */
119     if (asix -> ux_host_class_asix_link_state == UX_HOST_CLASS_ASIX_LINK_STATE_UP)
120     {
121         /* We need to free the packets that will not be sent.  */
122         current_packet =  asix -> ux_host_class_asix_xmit_queue;
123 
124         /* Get the next packet associated with the first packet.  */
125         next_packet = current_packet -> nx_packet_queue_next;
126 
127         /* Parse all these packets that were scheduled.  */
128         while (current_packet != UX_NULL)
129         {
130 
131             /* Free the packet that was just sent.  First do some housekeeping.  */
132             current_packet -> nx_packet_prepend_ptr =  current_packet -> nx_packet_prepend_ptr + UX_HOST_CLASS_ASIX_ETHERNET_SIZE;
133             current_packet -> nx_packet_length =  current_packet -> nx_packet_length - UX_HOST_CLASS_ASIX_ETHERNET_SIZE;
134 
135             /* And ask Netx to release it.  */
136             nx_packet_transmit_release(current_packet);
137 
138             /* Next packet becomes the current one.  */
139             current_packet = next_packet;
140 
141             /* Next packet now.  */
142             if (current_packet != UX_NULL)
143 
144                 /* Get the next packet associated with the first packet.  */
145                 next_packet = current_packet -> nx_packet_queue_next;
146 
147         }
148 
149     }
150 
151     /* Now the link is down.  */
152     asix -> ux_host_class_asix_link_state = UX_HOST_CLASS_ASIX_LINK_STATE_DOWN;
153 
154     /* Deregister this interface to the NetX USB interface broker.  */
155     _ux_network_driver_deactivate((VOID *) asix, asix -> ux_host_class_asix_network_handle);
156 
157     /* If the interrupt endpoint is defined, clean any pending transfer.  */
158     if (asix -> ux_host_class_asix_interrupt_endpoint != UX_NULL)
159     {
160 
161         /* Wait for any current transfer to be out of pending.  */
162         transfer_request =  &asix -> ux_host_class_asix_interrupt_endpoint -> ux_endpoint_transfer_request;
163         if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
164 
165             /* And abort any transfer.  */
166             _ux_host_stack_endpoint_transfer_abort(asix -> ux_host_class_asix_interrupt_endpoint);
167 
168         /* And free the memory used by the interrupt endpoint.  */
169         _ux_utility_memory_free(transfer_request -> ux_transfer_request_data_pointer);
170 
171     }
172 
173     /* First we take care of cleaning endpoint IN.  */
174     transfer_request =  &asix -> ux_host_class_asix_bulk_in_endpoint -> ux_endpoint_transfer_request;
175     if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
176 
177         /* We need to abort transactions on the bulk In pipe.  */
178         _ux_host_stack_endpoint_transfer_abort(asix -> ux_host_class_asix_bulk_in_endpoint);
179 
180     /* Then endpoint OUT.  */
181     transfer_request =  &asix -> ux_host_class_asix_bulk_out_endpoint -> ux_endpoint_transfer_request;
182     if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
183 
184         /* We need to abort transactions on the bulk Out pipe.  We normally don't need that anymore. */
185         _ux_host_stack_endpoint_transfer_abort(asix -> ux_host_class_asix_bulk_out_endpoint);
186 
187     /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
188        endpoints to exit properly.  */
189     _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
190 
191     /* Destroy the instance.  */
192     _ux_host_stack_class_instance_destroy(asix -> ux_host_class_asix_class, (VOID *) asix);
193 
194     /* Destroy the semaphores.  */
195     _ux_host_semaphore_delete(&asix -> ux_host_class_asix_semaphore);
196     _ux_host_semaphore_delete(&asix -> ux_host_class_asix_interrupt_notification_semaphore);
197 
198     /* Destroy the link monitoring thread.  */
199     _ux_utility_thread_delete(&asix -> ux_host_class_asix_thread);
200 
201     /* free its stack memory.  */
202     _ux_utility_memory_free(asix -> ux_host_class_asix_thread_stack);
203 
204     /* Free receive buffer memory.  */
205     _ux_utility_memory_free(asix -> ux_host_class_asix_receive_buffer);
206 
207 #ifdef UX_HOST_CLASS_ASIX_PACKET_CHAIN_SUPPORT
208 
209     /* Free transmit buffer memory.  */
210     if (asix -> ux_host_class_asix_xmit_buffer)
211         _ux_utility_memory_free(asix -> ux_host_class_asix_xmit_buffer);
212 #endif
213 
214     /* Before we free the device resources, we need to inform the application
215         that the device is removed.  */
216     if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
217     {
218 
219         /* Inform the application the device is removed.  */
220         _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, asix -> ux_host_class_asix_class, (VOID *) asix);
221     }
222 
223     /* If trace is enabled, insert this event into the trace buffer.  */
224     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_ASIX_DEACTIVATE, asix, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
225 
226     /* If trace is enabled, register this object.  */
227     UX_TRACE_OBJECT_UNREGISTER(asix);
228 
229     /* Free the asix instance memory.  */
230     _ux_utility_memory_free(asix);
231 
232     /* Return successful status.  */
233     return(UX_SUCCESS);
234 }
235 #endif
236