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 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Device Printer Class                                                */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_class_printer.h"
29 #include "ux_device_stack.h"
30 
31 
32 #if defined(UX_DEVICE_STANDALONE)
33 
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_device_class_printer_write_run                   PORTABLE C     */
39 /*                                                           6.2.0        */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Yajun Xia, Microsoft Corporation                                    */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function writes to the Printer class.                          */
47 /*                                                                        */
48 /*    It's for standalone mode.                                           */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    printer                               Address of printer class      */
53 /*                                            instance                    */
54 /*    buffer                                Pointer to data to write      */
55 /*    requested_length                      Length of bytes to write,     */
56 /*                                            set to 0 to issue ZLP       */
57 /*    actual_length                         Pointer to save number of     */
58 /*                                            bytes written               */
59 /*                                                                        */
60 /*  OUTPUT                                                                */
61 /*                                                                        */
62 /*    State machine Status to check                                       */
63 /*    UX_STATE_NEXT                         Transfer done, to next state  */
64 /*    UX_STATE_EXIT                         Abnormal, to reset state      */
65 /*    (others)                              Keep running, waiting         */
66 /*                                                                        */
67 /*  CALLS                                                                 */
68 /*                                                                        */
69 /*    _ux_device_stack_transfer_run         Run Transfer state machine    */
70 /*    _ux_utility_memory_copy               Copy memory                   */
71 /*                                                                        */
72 /*  CALLED BY                                                             */
73 /*                                                                        */
74 /*    Application                                                         */
75 /*                                                                        */
76 /*  RELEASE HISTORY                                                       */
77 /*                                                                        */
78 /*    DATE              NAME                      DESCRIPTION             */
79 /*                                                                        */
80 /*  10-31-2022         Yajun Xia            Initial Version 6.2.0         */
81 /*                                                                        */
82 /**************************************************************************/
_ux_device_class_printer_write_run(UX_DEVICE_CLASS_PRINTER * printer,UCHAR * buffer,ULONG requested_length,ULONG * actual_length)83 UINT _ux_device_class_printer_write_run(UX_DEVICE_CLASS_PRINTER *printer, UCHAR *buffer,
84                                 ULONG requested_length, ULONG *actual_length)
85 {
86 
87 UX_SLAVE_ENDPOINT           *endpoint;
88 UX_SLAVE_DEVICE             *device;
89 UX_SLAVE_TRANSFER           *transfer_request;
90 UINT                        zlp = UX_FALSE;
91 UINT                        status = 0;
92 
93     /* If trace is enabled, insert this event into the trace buffer.  */
94     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PRINTER_WRITE, printer, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
95 
96     /* Get the pointer to the device.  */
97     device =  &_ux_system_slave -> ux_system_slave_device;
98 
99     /* As long as the device is in the CONFIGURED state.  */
100     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
101     {
102 
103         /* Error trap. */
104         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
105 
106         /* If trace is enabled, insert this event into the trace buffer.  */
107         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
108 
109         /* Cannot proceed with command, the interface is down.  */
110         printer -> ux_device_class_printer_write_state = UX_STATE_RESET;
111         printer -> ux_device_class_printer_write_status = UX_CONFIGURATION_HANDLE_UNKNOWN;
112 
113         return(UX_STATE_EXIT);
114     }
115 
116     /* Locate the endpoints.  */
117     endpoint = printer -> ux_device_class_printer_endpoint_in;
118 
119     /* Check if it's available.  */
120     if (endpoint == UX_NULL)
121     {
122         /* Error trap. */
123         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
124 
125         printer -> ux_device_class_printer_write_state = UX_STATE_RESET;
126         printer -> ux_device_class_printer_write_status = UX_ENDPOINT_HANDLE_UNKNOWN;
127 
128         return(UX_STATE_EXIT);
129     }
130 
131     /* We are writing to the IN endpoint.  */
132     transfer_request =  &endpoint -> ux_slave_endpoint_transfer_request;
133 
134     /* Handle state cases.  */
135     switch(printer -> ux_device_class_printer_write_state)
136     {
137     case UX_STATE_RESET:
138         printer -> ux_device_class_printer_write_state = UX_DEVICE_CLASS_PRINTER_WRITE_START;
139         printer -> ux_device_class_printer_write_status = UX_TRANSFER_NO_ANSWER;
140         printer -> ux_device_class_printer_write_buffer = buffer;
141         printer -> ux_device_class_printer_write_requested_length = requested_length;
142         printer -> ux_device_class_printer_write_actual_length = 0;
143         printer -> ux_device_class_printer_write_host_length = UX_SLAVE_REQUEST_DATA_MAX_LENGTH;
144         if (requested_length == 0)
145             zlp = UX_TRUE;
146 
147         /* Fall through.  */
148     case UX_DEVICE_CLASS_PRINTER_WRITE_START:
149 
150         /* Get remaining requested length.  */
151         requested_length = printer -> ux_device_class_printer_write_requested_length -
152                         printer -> ux_device_class_printer_write_actual_length;
153 
154         /* There is no remaining, we are done.  */
155         if (requested_length == 0 && !zlp)
156         {
157             *actual_length = printer -> ux_device_class_printer_write_actual_length;
158             printer -> ux_device_class_printer_write_state = UX_STATE_RESET;
159             printer -> ux_device_class_printer_write_status = UX_SUCCESS;
160             return(UX_STATE_NEXT);
161         }
162 
163         /* Check if we have enough in the local buffer.  */
164         if (requested_length > UX_SLAVE_REQUEST_DATA_MAX_LENGTH)
165 
166             /* We have too much to transfer.  */
167             printer -> ux_device_class_printer_write_transfer_length =
168                                             UX_SLAVE_REQUEST_DATA_MAX_LENGTH;
169 
170         else
171         {
172 
173             /* We can proceed with the demanded length.  */
174             printer -> ux_device_class_printer_write_transfer_length = requested_length;
175 
176 #if !defined(UX_DEVICE_CLASS_PRINTER_WRITE_AUTO_ZLP)
177 
178             /* Assume expected length and transfer length match.  */
179             printer -> ux_device_class_printer_write_host_length = requested_length;
180 #else
181 
182             /* Assume expected more than transfer to let stack append ZLP if needed.  */
183             printer -> ux_device_class_printer_write_host_length = UX_SLAVE_REQUEST_DATA_MAX_LENGTH + 1;
184 #endif
185         }
186 
187 
188         /* On a out, we copy the buffer to the caller. Not very efficient but it makes the API
189            easier.  */
190         _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
191                             printer -> ux_device_class_printer_write_buffer,
192                             printer -> ux_device_class_printer_write_transfer_length); /* Use case of memcpy is verified. */
193 
194         /* Next state.  */
195         printer -> ux_device_class_printer_write_state = UX_DEVICE_CLASS_PRINTER_WRITE_WAIT;
196         UX_SLAVE_TRANSFER_STATE_RESET(transfer_request);
197 
198         /* Fall through.  */
199     case UX_DEVICE_CLASS_PRINTER_WRITE_WAIT:
200 
201         /* Send the request to the device controller.  */
202         status =  _ux_device_stack_transfer_run(transfer_request,
203                             printer -> ux_device_class_printer_write_transfer_length,
204                             printer -> ux_device_class_printer_write_host_length);
205 
206         /* Error case.  */
207         if (status < UX_STATE_NEXT)
208         {
209 
210             printer -> ux_device_class_printer_write_state = UX_STATE_RESET;
211             printer -> ux_device_class_printer_write_status =
212                 transfer_request -> ux_slave_transfer_request_completion_code;
213             return(UX_STATE_ERROR);
214         }
215 
216         /* Success case.  */
217         if (status == UX_STATE_NEXT)
218         {
219 
220             /* Next buffer address.  */
221             printer -> ux_device_class_printer_write_buffer +=
222                     transfer_request -> ux_slave_transfer_request_actual_length;
223 
224             /* Set the length actually received. */
225             printer -> ux_device_class_printer_write_actual_length +=
226                     transfer_request -> ux_slave_transfer_request_actual_length;
227 
228             /* Last transfer status.  */
229             printer -> ux_device_class_printer_write_status =
230                 transfer_request -> ux_slave_transfer_request_completion_code;
231 
232             /* Update actual done length.  */
233             *actual_length = printer -> ux_device_class_printer_write_actual_length;
234 
235             /* Check ZLP case.  */
236             if (printer -> ux_device_class_printer_write_requested_length == 0)
237             {
238                 printer -> ux_device_class_printer_write_state = UX_STATE_RESET;
239                 return(UX_STATE_NEXT);
240             }
241 
242             /* Next state.  */
243             printer -> ux_device_class_printer_write_state = UX_DEVICE_CLASS_PRINTER_WRITE_START;
244         }
245 
246         /* Keep waiting.  */
247         return(UX_STATE_WAIT);
248 
249     default: /* Error.  */
250         printer -> ux_device_class_printer_write_state = UX_STATE_RESET;
251         break;
252     }
253 
254     /* Error case.  */
255     return(UX_STATE_EXIT);
256 }
257 
258 #endif
259