summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_asix_write.c
blob: 65949b86e7d57df82a4d5826d492a0eb7b7b59f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Asix Class                                                          */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_class_asix.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_asix_write                           PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function writes to the asix interface. The call is blocking    */
/*    and only returns when there is either an error or when the transfer */
/*    is complete.                                                        */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    asix                                  Pointer to asix class         */
/*    packet                                Packet to write or queue      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_transfer_request       Process transfer request      */
/*    _ux_utility_short_put                 Put 16-bit value              */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_asix_write(VOID *asix_class, NX_PACKET *packet)
{
#if defined(UX_HOST_STANDALONE)
    UX_PARAMETER_NOT_USED(asix_class);
    UX_PARAMETER_NOT_USED(packet);
    return(UX_FUNCTION_NOT_SUPPORTED);
#else

UX_INTERRUPT_SAVE_AREA
UX_TRANSFER         *transfer_request;
UINT                status;
NX_PACKET           *current_packet;
NX_PACKET           *next_packet;
UCHAR               *packet_header;
UX_HOST_CLASS_ASIX  *asix;
ULONG               adjusted_length;
#ifdef UX_HOST_CLASS_ASIX_PACKET_CHAIN_SUPPORT
ULONG               copied;
#endif

    /* Proper class casting.  */
    asix = (UX_HOST_CLASS_ASIX *) asix_class;

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_ASIX_WRITE, asix, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)

    /* Critical enter.  */
    UX_DISABLE

    /* Ensure the instance is valid.  */
    if (asix -> ux_host_class_asix_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
    {

        /* Error trap. */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);

        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, asix, 0, 0, UX_TRACE_ERRORS, 0, 0)

        /* Critical exit.  */
        UX_RESTORE;
        return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
    }

    /* Ensure link state is OK.  */
    if (asix -> ux_host_class_asix_link_state != UX_HOST_CLASS_ASIX_LINK_STATE_UP)
    {

        /* Critical exit.  */
        UX_RESTORE;

        /* Release the packet.  */
        packet -> nx_packet_prepend_ptr =  packet -> nx_packet_prepend_ptr + NX_ETHERNET_SIZE;
        packet -> nx_packet_length =  packet -> nx_packet_length - NX_ETHERNET_SIZE;
        nx_packet_transmit_release(packet);

        /* Report error to application.  */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_ETH_LINK_STATE_DOWN_ERROR);
        return(UX_CLASS_ETH_LINK_STATE_DOWN_ERROR);
    }

    /* Load the address of the current packet header at the physical header.  */
    packet_header =  packet -> nx_packet_prepend_ptr;

    /* Subtract 2 USHORT to store length of the packet.  */
    packet_header -= sizeof(USHORT) * 2;

#if defined(UX_HOST_CLASS_ASIX_HEADER_CHECK_ENABLE)

    /* Check packet compatibility to avoid writing to unexpected area.  */
    if (packet_header < packet -> nx_packet_data_start)
    {

        /* Error trap.  */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR);

        UX_RESTORE;
        return(UX_HOST_CLASS_MEMORY_ERROR);
    }
#endif

    /* Packet length validation.  */
    if (UX_OVERFLOW_CHECK_ADD_ULONG(packet -> nx_packet_length, sizeof(USHORT) * 2))
    {

        /* Error trap.  */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MATH_OVERFLOW);
        UX_RESTORE
        return(UX_MATH_OVERFLOW);
    }
    adjusted_length = packet -> nx_packet_length + (ULONG)sizeof(USHORT) * 2;
    if (adjusted_length > UX_HOST_CLASS_ASIX_TRANSMIT_BUFFER_SIZE)
    {

        /* Error trap.  */
        _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_ETH_SIZE_ERROR);
        UX_RESTORE
        return(UX_CLASS_ETH_SIZE_ERROR);
    }

    /* Store the length of the payload in the first USHORT.  */
    _ux_utility_short_put(packet_header, (USHORT)(packet -> nx_packet_length));

    /* Store the negative length of the payload in the first USHORT.  */
    _ux_utility_short_put(packet_header + sizeof(USHORT), (USHORT)(~packet -> nx_packet_length));

    /* Check the queue. See if there is something that is being sent. */
    if (asix -> ux_host_class_asix_xmit_queue == UX_NULL)
    {

        /* Reset the queue pointer of this packet.  */
        packet -> nx_packet_queue_next = UX_NULL;

        /* Memorize this packet at the beginning of the queue.  */
        asix -> ux_host_class_asix_xmit_queue = packet;

        UX_RESTORE

        /* Nothing is in the queue. We need to arm this transfer. */
        /* Get the pointer to the bulk out endpoint transfer request.  */
        transfer_request =  &asix -> ux_host_class_asix_bulk_out_endpoint -> ux_endpoint_transfer_request;

#ifdef UX_HOST_CLASS_ASIX_PACKET_CHAIN_SUPPORT

        /* Check if the packets are chained.  */
        if (packet -> nx_packet_next)
        {

            /* Create buffer.  */
            if (asix -> ux_host_class_asix_xmit_buffer == UX_NULL)
            {
                asix -> ux_host_class_asix_xmit_buffer = _ux_utility_memory_allocate(UX_NO_ALIGN,
                                    UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_ASIX_TRANSMIT_BUFFER_SIZE);
                if (asix -> ux_host_class_asix_xmit_buffer == UX_NULL)
                {
                    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
                    return(UX_MEMORY_INSUFFICIENT);
                }
            }

            packet -> nx_packet_length = adjusted_length;
            packet -> nx_packet_prepend_ptr -= sizeof(USHORT) * 2;
            nx_packet_data_extract_offset(packet, 0, asix -> ux_host_class_asix_xmit_buffer, packet -> nx_packet_length, &copied);

            /* Setup the transaction parameters.  */
            transfer_request -> ux_transfer_request_requested_length =  packet -> nx_packet_length;
            transfer_request -> ux_transfer_request_data_pointer = asix -> ux_host_class_asix_xmit_buffer;

            /* Restore packet status.  */
            packet -> nx_packet_length -= (ULONG)sizeof(USHORT) * 2;
            packet -> nx_packet_prepend_ptr += sizeof(USHORT) * 2;
        }
        else
#endif
        {

            /* Setup the transaction parameters.  */
            transfer_request -> ux_transfer_request_data_pointer     =  packet_header;
            transfer_request -> ux_transfer_request_requested_length =  adjusted_length;
        }

        /* Store the packet that owns this transaction.  */
        transfer_request -> ux_transfer_request_user_specific = packet;

        /* Perform the transfer.  */
        status =  _ux_host_stack_transfer_request(transfer_request);

        /* Check if the transaction was armed successfully. We do not wait for the packet to be sent here. */
        if (status != UX_SUCCESS)
        {

            /* Could not arm this transfer.  */
            asix -> ux_host_class_asix_xmit_queue = UX_NULL;
            return(UX_ERROR);
        }
    }

    else

    {

        /* We get here when there is something in the queue.  */
        current_packet =  asix -> ux_host_class_asix_xmit_queue;

        /* Get the next packet associated with the first packet.  */
        next_packet = current_packet -> nx_packet_queue_next;

        /* Parse the current chain for the end.  */
        while (next_packet != NX_NULL)
        {
            /* Remember the current packet.  */
            current_packet = next_packet;

            /* See what the next packet in the chain is.  */
            next_packet = current_packet -> nx_packet_queue_next;
        }

        /* Memorize the packet to be sent.  */
        current_packet -> nx_packet_queue_next = packet;

        /* The packet to be sent is the last in the chain.  */
        packet -> nx_packet_queue_next = NX_NULL;


        UX_RESTORE
    }

    /* We are done here.  */
    return(UX_SUCCESS);
#endif
}