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 /**   CDC_ECM 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_cdc_ecm.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_cdc_ecm_transmit_queue_clean         PORTABLE C      */
39 /*                                                           6.1.11       */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Chaoqiong Xiao, Microsoft Corporation                               */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function cleans the transmit queue.                            */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    cdc_ecm                             CDC ECM instance                */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    No return value                                                     */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_semaphore_get                Get bulk out semaphore        */
59 /*    _ux_host_stack_endpoint_transfer_abort Abort endpoint transfer      */
60 /*    nx_packet_transmit_release            Release NetX packet           */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    CDC ECM thread and deactivation                                     */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            used UX prefix to refer to  */
73 /*                                            TX symbols instead of using */
74 /*                                            them directly,              */
75 /*                                            resulting in version 6.1    */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            refined macros names,       */
78 /*                                            resulting in version 6.1.10 */
79 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            fixed standalone compile,   */
81 /*                                            resulting in version 6.1.11 */
82 /*                                                                        */
83 /**************************************************************************/
_ux_host_class_cdc_ecm_transmit_queue_clean(UX_HOST_CLASS_CDC_ECM * cdc_ecm)84 VOID  _ux_host_class_cdc_ecm_transmit_queue_clean(UX_HOST_CLASS_CDC_ECM *cdc_ecm)
85 {
86 
87 UX_INTERRUPT_SAVE_AREA
88 
89 NX_PACKET               *current_packet;
90 NX_PACKET               *next_packet;
91 
92     /* Disable interrupts while we check the write in process flag and
93        set our own state.  */
94     UX_DISABLE
95 
96     /* Is there a write in process?  */
97     if (cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_transfer_check_and_arm_in_process == UX_TRUE)
98     {
99 
100         /* Wait for writes to complete. Note that once these writes complete,
101            no more should occur since the link state is pending down.  */
102 
103         /* Mark this thread as suspended so it will be woken up.  */
104         cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_transfer_waiting_for_check_and_arm_to_finish =  UX_TRUE;
105 
106         /* Restore interrupts while we wait.  */
107         UX_RESTORE
108 
109         /* Wait for write function to resume us.  */
110         _ux_host_semaphore_get_norc(&cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_transfer_waiting_for_check_and_arm_to_finish_semaphore, UX_WAIT_FOREVER);
111 
112         /* We're done waiting.  */
113         cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_transfer_waiting_for_check_and_arm_to_finish =  UX_FALSE;
114     }
115     else
116     {
117 
118         /* No writes are in process. Restore interrupts and go on to free
119            the xmit queue.  */
120         UX_RESTORE
121     }
122 
123     /* Abort transfers on the bulk out endpoint. Note we need to do this
124        before accessing the queue since the transmission callback might
125        modify it.  */
126     _ux_host_stack_transfer_request_abort(&cdc_ecm -> ux_host_class_cdc_ecm_bulk_out_endpoint -> ux_endpoint_transfer_request);
127 
128     /* Get the first packet.  */
129     current_packet =  cdc_ecm -> ux_host_class_cdc_ecm_xmit_queue_head;
130 
131     /* We need to free the packets that will not be sent.  */
132     while (current_packet != UX_NULL)
133     {
134 
135         /* We must get the next packet before releasing the current packet
136            because nxe_packet_transmit_release sets the pointer we pass
137            to null.  */
138         next_packet =  current_packet -> nx_packet_queue_next;
139 
140         /* Free the packet. First do some housekeeping.  */
141         current_packet -> nx_packet_prepend_ptr =  current_packet -> nx_packet_prepend_ptr + UX_HOST_CLASS_CDC_ECM_ETHERNET_SIZE;
142         current_packet -> nx_packet_length =  current_packet -> nx_packet_length - UX_HOST_CLASS_CDC_ECM_ETHERNET_SIZE;
143 
144         /* And ask Netx to release it.  */
145         nx_packet_transmit_release(current_packet);
146 
147         /* Next packet becomes the current one.  */
148         current_packet =  next_packet;
149     }
150 
151     /* Clear the queue.  */
152     cdc_ecm -> ux_host_class_cdc_ecm_xmit_queue_head =  UX_NULL;
153 }
154 #endif
155