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 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_asix_transmission_callback           PORTABLE C      */
38 /*                                                           6.2.0        */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is the callback from the USBX transfer functions,     */
46 /*    it is called when a full or partial transfer has been done for a    */
47 /*    bulk out transfer.                                                  */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    transfer_request                      Pointer to transfer request   */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_transfer_request       Process transfer request      */
60 /*    _ux_utility_short_put                 Put 16-bit value              */
61 /*    nx_packet_transmit_release            Release NetX packet           */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    Application                                                         */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  08-02-2021     Wen Wang                 Modified comment(s),          */
75 /*                                            fixed spelling error,       */
76 /*                                            resulting in version 6.1.8  */
77 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            fixed standalone compile,   */
79 /*                                            resulting in version 6.1.11 */
80 /*  10-31-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            supported NX packet chain,  */
82 /*                                            fixed empty queue handling, */
83 /*                                            resulting in version 6.2.0  */
84 /*                                                                        */
85 /**************************************************************************/
_ux_host_class_asix_transmission_callback(UX_TRANSFER * transfer_request)86 VOID  _ux_host_class_asix_transmission_callback (UX_TRANSFER *transfer_request)
87 {
88 #if defined(UX_HOST_STANDALONE)
89     UX_PARAMETER_NOT_USED(transfer_request);
90 #else
91 
92 UX_HOST_CLASS_ASIX              *asix;
93 NX_PACKET                       *current_packet;
94 NX_PACKET                       *next_packet;
95 UCHAR                           *packet_header;
96 #ifdef UX_HOST_CLASS_ASIX_PACKET_CHAIN_SUPPORT
97 ULONG                           copied;
98 #endif
99 
100     /* Get the class instance for this transfer request.  */
101     asix =  (UX_HOST_CLASS_ASIX *) transfer_request -> ux_transfer_request_class_instance;
102 
103     /* Check the state of the transfer.  If there is an error, we do not proceed with this report.  */
104     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
105     {
106 
107         /* We do not proceed.  */
108         return;
109 
110     }
111 
112     /* Check if the class is in shutdown.  */
113     if (asix -> ux_host_class_asix_state ==  UX_HOST_CLASS_INSTANCE_SHUTDOWN)
114 
115         /* We do not proceed.  */
116         return;
117 
118     /* Check if there is another transfer for this pipe.  */
119     current_packet =  asix -> ux_host_class_asix_xmit_queue;
120     if (current_packet == UX_NULL)
121         return;
122 
123     /* Check if a ZLP is needed.  */
124     if ((transfer_request -> ux_transfer_request_actual_length > 0) &&
125         ((transfer_request -> ux_transfer_request_actual_length %
126           transfer_request -> ux_transfer_request_packet_length) == 0))
127     {
128         transfer_request -> ux_transfer_request_requested_length = 0;
129         _ux_host_stack_transfer_request(transfer_request);
130         return;
131     }
132 
133     /* Get the next packet associated with the first packet.  */
134     next_packet = current_packet -> nx_packet_queue_next;
135 
136     /* Set the next packet (or a NULL value) as the head of the xmit queue. */
137     asix -> ux_host_class_asix_xmit_queue =  next_packet;
138 
139     /* If there is nothing else or if the link is down no need to rearm a packet. */
140     if (next_packet != UX_NULL && asix -> ux_host_class_asix_link_state == UX_HOST_CLASS_ASIX_LINK_STATE_UP)
141     {
142 
143         /* Load the address of the current packet header of the physical header.  */
144         packet_header =  next_packet -> nx_packet_prepend_ptr;
145 
146         /* Subtract 2 USHORT to store length of the packet.  */
147         packet_header -= sizeof(USHORT) * 2;
148 
149         /* Store the length of the payload in the first USHORT.  */
150         _ux_utility_short_put(packet_header, (USHORT)(next_packet -> nx_packet_length));
151 
152         /* Store the negative length of the payload in the first USHORT.  */
153         _ux_utility_short_put(packet_header+ sizeof(USHORT), (USHORT)(~next_packet -> nx_packet_length));
154 
155 #ifdef UX_HOST_CLASS_ASIX_PACKET_CHAIN_SUPPORT
156 
157         /* Check if the packets are chained.  */
158         if (next_packet -> nx_packet_next)
159         {
160 
161             next_packet -> nx_packet_length += sizeof(USHORT) * 2;
162             next_packet -> nx_packet_prepend_ptr -= sizeof(USHORT) * 2;
163             nx_packet_data_extract_offset(next_packet, 0, asix -> ux_host_class_asix_xmit_buffer, next_packet -> nx_packet_length, &copied);
164 
165             /* Setup the transaction parameters.  */
166             transfer_request -> ux_transfer_request_requested_length =  next_packet -> nx_packet_length;
167             transfer_request -> ux_transfer_request_data_pointer = asix -> ux_host_class_asix_xmit_buffer;
168 
169             /* Restore packet status.  */
170             next_packet -> nx_packet_length -= sizeof(USHORT) * 2;
171             next_packet -> nx_packet_prepend_ptr += sizeof(USHORT) * 2;
172         }
173         else
174 #endif
175         {
176 
177             /* Prepare the values for this new transmission.  */
178             transfer_request -> ux_transfer_request_data_pointer     =  packet_header;
179             transfer_request -> ux_transfer_request_requested_length =  next_packet -> nx_packet_length + (ULONG)sizeof(USHORT) * 2;
180         }
181 
182         /* Store the packet that owns this transaction.  */
183         transfer_request -> ux_transfer_request_user_specific = next_packet;
184 
185         /* Arm another transfer.  */
186         _ux_host_stack_transfer_request(transfer_request);
187     }
188 
189     /* Free the packet that was just sent.  First do some housekeeping.  */
190     current_packet -> nx_packet_prepend_ptr =  current_packet -> nx_packet_prepend_ptr + UX_HOST_CLASS_ASIX_ETHERNET_SIZE;
191     current_packet -> nx_packet_length =  current_packet -> nx_packet_length - UX_HOST_CLASS_ASIX_ETHERNET_SIZE;
192 
193     /* And ask Netx to release it.  */
194     nx_packet_transmit_release(current_packet);
195 
196     /* There is no status to be reported back to the stack.  */
197     return;
198 #endif
199 }
200