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_reception_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 in transfer. It calls back the application.                    */
48 /*                                                                        */
49 /*    It's deprecated.                                                    */
50 /*                                                                        */
51 /*  INPUT                                                                 */
52 /*                                                                        */
53 /*    transfer_request                      Pointer to transfer request   */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    None                                                                */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_transfer_request       Process transfer request      */
62 /*    _ux_utility_short_get_big_endian      Get 16-bit big endian         */
63 /*    _ux_network_driver_packet_received    Process received packet       */
64 /*    nx_packet_transmit_release            Release NetX packet           */
65 /*    nx_packet_allocate                    Allocate NetX packet          */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    Application                                                         */
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 /*                                            resulting in version 6.1    */
78 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            fixed standalone compile,   */
80 /*                                            resulting in version 6.1.11 */
81 /*  10-31-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            deprecated, no RX callback, */
83 /*                                            modified pool reference,    */
84 /*                                            resulting in version 6.2.0  */
85 /*                                                                        */
86 /**************************************************************************/
_ux_host_class_asix_reception_callback(UX_TRANSFER * transfer_request)87 VOID  _ux_host_class_asix_reception_callback (UX_TRANSFER *transfer_request)
88 {
89 #if defined(UX_HOST_STANDALONE)
90     UX_PARAMETER_NOT_USED(transfer_request);
91 #else
92 
93 UX_HOST_CLASS_ASIX              *asix;
94 NX_PACKET                       *packet;
95 ULONG                           ip_given_length;
96 UINT                            status;
97 
98     /* Get the class instance for this transfer request.  */
99     asix =  (UX_HOST_CLASS_ASIX *) transfer_request -> ux_transfer_request_class_instance;
100 
101     /* Load the packet that is associated with this reception.  */
102     packet = transfer_request -> ux_transfer_request_user_specific;
103 
104     /* Check the state of the transfer.  If there is an error, we do not proceed with this report.  */
105     if (transfer_request -> ux_transfer_request_completion_code == UX_SUCCESS)
106     {
107 
108         /* Adjust the prepend, length, append fields to take the Ether header out */
109         packet -> nx_packet_prepend_ptr += 4;
110         packet -> nx_packet_length = transfer_request -> ux_transfer_request_actual_length - 4;
111         packet -> nx_packet_append_ptr =
112             packet->nx_packet_prepend_ptr + transfer_request -> ux_transfer_request_actual_length ;
113 
114         /* Calculate the accurate packet length from ip header */
115         if((*(packet -> nx_packet_prepend_ptr + 12) == 0x08) &&
116             (*(packet -> nx_packet_prepend_ptr + 13) == 0))
117         {
118             ip_given_length = _ux_utility_short_get_big_endian(packet -> nx_packet_prepend_ptr + 16) + UX_HOST_CLASS_ASIX_ETHERNET_SIZE;
119             packet->nx_packet_length = ip_given_length ;
120             packet->nx_packet_append_ptr =  packet->nx_packet_prepend_ptr + ip_given_length;
121         }
122 
123         /* Send that packet to the NetX USB broker.  */
124         _ux_network_driver_packet_received(asix -> ux_host_class_asix_network_handle, packet);
125 
126     }
127 
128     else
129 
130         /* Free the packet that was not successfully received.  */
131         nx_packet_transmit_release(packet);
132 
133     /* We can accept new reception. Get a NX Packet */
134     if (nx_packet_allocate(asix -> ux_host_class_asix_packet_pool, &packet,
135                             NX_RECEIVE_PACKET, NX_NO_WAIT) == NX_SUCCESS)
136     {
137 
138         /* Adjust the prepend pointer to take into account the non 3 bit alignment of the ethernet header.  */
139         packet -> nx_packet_prepend_ptr += sizeof(USHORT);
140 
141         /* Set the data pointer.  */
142         transfer_request -> ux_transfer_request_data_pointer =  packet -> nx_packet_prepend_ptr;
143 
144         /* And length.  */
145         transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_ASIX_NX_PAYLOAD_SIZE;
146         transfer_request -> ux_transfer_request_actual_length =     0;
147 
148         /* Store the packet that owns this transaction.  */
149         transfer_request -> ux_transfer_request_user_specific = packet;
150 
151         /* Memorize this packet at the beginning of the queue.  */
152         asix -> ux_host_class_asix_receive_queue = packet;
153 
154         /* Reset the queue pointer of this packet.  */
155         packet -> nx_packet_queue_next = UX_NULL;
156 
157         /* Ask USB to schedule a reception.  */
158         status =  _ux_host_stack_transfer_request(transfer_request);
159 
160         /* Check if the transaction was armed successfully. We do not wait for the packet to be sent here. */
161         if (status != UX_SUCCESS)
162         {
163 
164             /* Cancel the packet.  */
165             asix -> ux_host_class_asix_receive_queue = UX_NULL;
166 
167         }
168     }
169     /* There is no status to be reported back to the stack.  */
170     return;
171 #endif
172 }
173