1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Asix Class                                                          */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_asix.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_asix_reception_callback              PORTABLE C      */
37 /*                                                           6.2.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is the callback from the USBX transfer functions,     */
45 /*    it is called when a full or partial transfer has been done for a    */
46 /*    bulk in transfer. It calls back the application.                    */
47 /*                                                                        */
48 /*    It's deprecated.                                                    */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    transfer_request                      Pointer to transfer request   */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_stack_transfer_request       Process transfer request      */
61 /*    _ux_utility_short_get_big_endian      Get 16-bit big endian         */
62 /*    _ux_network_driver_packet_received    Process received packet       */
63 /*    nx_packet_transmit_release            Release NetX packet           */
64 /*    nx_packet_allocate                    Allocate NetX packet          */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
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 /*                                            deprecated, no RX callback, */
82 /*                                            modified pool reference,    */
83 /*                                            resulting in version 6.2.0  */
84 /*                                                                        */
85 /**************************************************************************/
_ux_host_class_asix_reception_callback(UX_TRANSFER * transfer_request)86 VOID  _ux_host_class_asix_reception_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                       *packet;
94 ULONG                           ip_given_length;
95 UINT                            status;
96 
97     /* Get the class instance for this transfer request.  */
98     asix =  (UX_HOST_CLASS_ASIX *) transfer_request -> ux_transfer_request_class_instance;
99 
100     /* Load the packet that is associated with this reception.  */
101     packet = transfer_request -> ux_transfer_request_user_specific;
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         /* Adjust the prepend, length, append fields to take the Ether header out */
108         packet -> nx_packet_prepend_ptr += 4;
109         packet -> nx_packet_length = transfer_request -> ux_transfer_request_actual_length - 4;
110         packet -> nx_packet_append_ptr =
111             packet->nx_packet_prepend_ptr + transfer_request -> ux_transfer_request_actual_length ;
112 
113         /* Calculate the accurate packet length from ip header */
114         if((*(packet -> nx_packet_prepend_ptr + 12) == 0x08) &&
115             (*(packet -> nx_packet_prepend_ptr + 13) == 0))
116         {
117             ip_given_length = _ux_utility_short_get_big_endian(packet -> nx_packet_prepend_ptr + 16) + UX_HOST_CLASS_ASIX_ETHERNET_SIZE;
118             packet->nx_packet_length = ip_given_length ;
119             packet->nx_packet_append_ptr =  packet->nx_packet_prepend_ptr + ip_given_length;
120         }
121 
122         /* Send that packet to the NetX USB broker.  */
123         _ux_network_driver_packet_received(asix -> ux_host_class_asix_network_handle, packet);
124 
125     }
126 
127     else
128 
129         /* Free the packet that was not successfully received.  */
130         nx_packet_transmit_release(packet);
131 
132     /* We can accept new reception. Get a NX Packet */
133     if (nx_packet_allocate(asix -> ux_host_class_asix_packet_pool, &packet,
134                             NX_RECEIVE_PACKET, NX_NO_WAIT) == NX_SUCCESS)
135     {
136 
137         /* Adjust the prepend pointer to take into account the non 3 bit alignment of the ethernet header.  */
138         packet -> nx_packet_prepend_ptr += sizeof(USHORT);
139 
140         /* Set the data pointer.  */
141         transfer_request -> ux_transfer_request_data_pointer =  packet -> nx_packet_prepend_ptr;
142 
143         /* And length.  */
144         transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_ASIX_NX_PAYLOAD_SIZE;
145         transfer_request -> ux_transfer_request_actual_length =     0;
146 
147         /* Store the packet that owns this transaction.  */
148         transfer_request -> ux_transfer_request_user_specific = packet;
149 
150         /* Memorize this packet at the beginning of the queue.  */
151         asix -> ux_host_class_asix_receive_queue = packet;
152 
153         /* Reset the queue pointer of this packet.  */
154         packet -> nx_packet_queue_next = UX_NULL;
155 
156         /* Ask USB to schedule a reception.  */
157         status =  _ux_host_stack_transfer_request(transfer_request);
158 
159         /* Check if the transaction was armed successfully. We do not wait for the packet to be sent here. */
160         if (status != UX_SUCCESS)
161         {
162 
163             /* Cancel the packet.  */
164             asix -> ux_host_class_asix_receive_queue = UX_NULL;
165 
166         }
167     }
168     /* There is no status to be reported back to the stack.  */
169     return;
170 #endif
171 }
172