summaryrefslogtreecommitdiff
path: root/common/usbx_device_classes/src/ux_device_class_printer_write.c
blob: 39cd03952837ab987e19f0864f538f716b54ec8f (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
279
280
281
282
283
284
285
286
287
288
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   Device Printer Class                                                */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_device_class_printer.h"
#include "ux_device_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_printer_write                      PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function writes to  the Printer class.                         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    printer                               Address of printer class      */
/*                                            instance                    */
/*    buffer                                Pointer to data to write      */
/*    requested_length                      Length of bytes to write,     */
/*                                            set to 0 to issue ZLP       */
/*    actual_length                         Pointer to save number of     */
/*                                            bytes written               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*   _ux_utility_memory_copy                Copy memory                   */
/*   _ux_device_stack_transfer_request      Transfer request              */
/*   _ux_device_mutex_on                    Take Mutex                    */
/*   _ux_device_mutex_off                   Release Mutex                 */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT _ux_device_class_printer_write(UX_DEVICE_CLASS_PRINTER *printer, UCHAR *buffer,
                                ULONG requested_length, ULONG *actual_length)
{

UX_SLAVE_ENDPOINT           *endpoint;
UX_SLAVE_DEVICE             *device;
UX_SLAVE_TRANSFER           *transfer_request;
ULONG                       local_requested_length;
ULONG                       local_host_length;
UINT                        status = 0;

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PRINTER_WRITE, printer, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)

    /* Get the pointer to the device.  */
    device =  &_ux_system_slave -> ux_system_slave_device;

    /* As long as the device is in the CONFIGURED state.  */
    if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
    {

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

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

        /* Cannot proceed with command, the interface is down.  */
        return(UX_CONFIGURATION_HANDLE_UNKNOWN);
    }

    /* Locate the endpoints.  */
    endpoint = printer -> ux_device_class_printer_endpoint_in;

    /* Check if it's available.  */
    if (endpoint == UX_NULL)
        return(UX_FUNCTION_NOT_SUPPORTED);

    /* Protect this thread.  */
    _ux_device_mutex_on(&printer -> ux_device_class_printer_endpoint_in_mutex);

    /* We are writing to the IN endpoint.  */
    transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;

    /* Reset the actual length.  */
    *actual_length =  0;

    /* Check if the application forces a 0 length packet.  */
    if (requested_length == 0)
    {

        /* Send the request for 0 byte packet to the device controller.  */
        status =  _ux_device_stack_transfer_request(transfer_request, 0, 0);

        /* Free Mutex resource.  */
        _ux_device_mutex_off(&printer -> ux_device_class_printer_endpoint_in_mutex);

        /* Return the status.  */
        return(status);
    }

#if (UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1) && defined(UX_DEVICE_CLASS_PRINTER_ZERO_COPY)

    /* Check if device is configured.  */
    if (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED)
    {

#if defined(UX_DEVICE_CLASS_PRINTER_WRITE_AUTO_ZLP)

        /* Issue with larger host length to append zlp if necessary.  */
        local_host_length = requested_length + 1;
#else
        local_host_length = requested_length;
#endif
        local_requested_length = requested_length;

        /* Issue the transfer request.  */
        transfer_request -> ux_slave_transfer_request_data_pointer =  buffer;
        status = _ux_device_stack_transfer_request(transfer_request, local_requested_length, local_host_length);
        *actual_length = transfer_request -> ux_slave_transfer_request_actual_length;
    }
#else

    /* Check if we need more transactions.  */
    local_host_length = UX_DEVICE_CLASS_PRINTER_WRITE_BUFFER_SIZE;
    while (device -> ux_slave_device_state == UX_DEVICE_CONFIGURED &&
            requested_length != 0)
    {

        /* Check if we have enough in the local buffer.  */
        if (requested_length > UX_DEVICE_CLASS_PRINTER_WRITE_BUFFER_SIZE)

            /* We have too much to transfer.  */
            local_requested_length = UX_DEVICE_CLASS_PRINTER_WRITE_BUFFER_SIZE;

        else
        {

            /* We can proceed with the demanded length.  */
            local_requested_length = requested_length;

#if !defined(UX_DEVICE_CLASS_PRINTER_WRITE_AUTO_ZLP)

            /* Assume expected length matches.  */
            local_host_length = requested_length;
#else

            /* Assume expected more so stack appends ZLP if needed.  */
            local_host_length = UX_DEVICE_CLASS_PRINTER_WRITE_BUFFER_SIZE + 1;
#endif
        }

        /* On a out, we copy the buffer to the caller. Not very efficient but it makes the API
            easier.  */
        _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
                            buffer, local_requested_length); /* Use case of memcpy is verified. */

        /* Send the request to the device controller.  */
        status =  _ux_device_stack_transfer_request(transfer_request,
                                local_requested_length, local_host_length);

        /* Check the status */
        if (status == UX_SUCCESS)
        {

            /* Next buffer address.  */
            buffer += transfer_request -> ux_slave_transfer_request_actual_length;

            /* Set the length actually received. */
            *actual_length += transfer_request -> ux_slave_transfer_request_actual_length;

            /* Decrement what left has to be done.  */
            requested_length -= transfer_request -> ux_slave_transfer_request_actual_length;
        }
        else
        {

            /* Free Mutex resource.  */
            _ux_device_mutex_off(&printer -> ux_device_class_printer_endpoint_in_mutex);

            /* We had an error, abort.  */
            return(status);
        }
    }
#endif

    /* Free Mutex resource.  */
    _ux_device_mutex_off(&printer -> ux_device_class_printer_endpoint_in_mutex);

    /* Check why we got here, either completion or device was extracted.  */
    if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
    {

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

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

        /* Device must have been extracted.  */
        return (UX_TRANSFER_NO_ANSWER);
    }
    else

        /* Simply return the last transaction result.  */
        return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _uxe_device_class_printer_write                       PORTABLE C    */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yajun Xia, Microsoft Corporation                                    */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks errors in printer class write function         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    printer                               Address of printer class      */
/*                                            instance                    */
/*    buffer                                Pointer to data to write      */
/*    requested_length                      Length of bytes to write,     */
/*                                            set to 0 to issue ZLP       */
/*    actual_length                         Pointer to save number of     */
/*                                            bytes written               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_device_class_printer_write        Printer class write function  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT _uxe_device_class_printer_write(UX_DEVICE_CLASS_PRINTER *printer, UCHAR *buffer,
                                     ULONG requested_length, ULONG *actual_length)
{

    /* Sanity checks.  */
    if ((printer == UX_NULL) || ((buffer == UX_NULL) && (requested_length > 0)) || (actual_length == UX_NULL))
    {
        return (UX_INVALID_PARAMETER);
    }

    return (_ux_device_class_printer_write(printer, buffer, requested_length, actual_length));
}