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 /**   Printer 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_printer.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_printer_soft_reset                   PORTABLE C      */
37 /*                                                           6.1.12       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function performs a soft reset of the printer in case the      */
45 /*    printer gets into an error mode.                                    */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    printer                               Pointer to printer class      */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_stack_transfer_request       Process transfer request      */
58 /*    _ux_host_semaphore_get                Get protection semaphore      */
59 /*    _ux_host_semaphore_put                Release protection semaphore  */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*  02-02-2021     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            supported interface other   */
74 /*                                            than number zero,           */
75 /*                                            resulting in version 6.1.4  */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            added standalone support,   */
78 /*                                            resulting in version 6.1.10 */
79 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            fixed parameter/variable    */
81 /*                                            names conflict C++ keyword, */
82 /*                                            resulting in version 6.1.12 */
83 /*                                                                        */
84 /**************************************************************************/
_ux_host_class_printer_soft_reset(UX_HOST_CLASS_PRINTER * printer)85 UINT   _ux_host_class_printer_soft_reset(UX_HOST_CLASS_PRINTER *printer)
86 {
87 #if defined(UX_HOST_STANDALONE)
88 UX_INTERRUPT_SAVE_AREA
89 #endif
90 UX_INTERFACE    *interface_ptr;
91 UX_ENDPOINT     *control_endpoint;
92 UX_TRANSFER     *transfer_request;
93 UINT            status;
94 
95     /* If trace is enabled, insert this event into the trace buffer.  */
96     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PRINTER_SOFT_RESET, printer, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
97 
98     /* Ensure the instance is valid.  */
99     if (printer -> ux_host_class_printer_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
100     {
101 
102         /* Error trap. */
103         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
104 
105         /* If trace is enabled, insert this event into the trace buffer.  */
106         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, printer, 0, 0, UX_TRACE_ERRORS, 0, 0)
107 
108         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
109     }
110 
111 #if defined(UX_HOST_STANDALONE)
112     UX_DISABLE
113     if ((printer -> ux_host_class_printer_flags & UX_HOST_CLASS_PRINTER_FLAG_LOCK) ||
114         (printer -> ux_host_class_printer_device -> ux_device_flags & UX_DEVICE_FLAG_LOCK))
115     {
116         UX_RESTORE
117         return(UX_BUSY);
118     }
119     printer -> ux_host_class_printer_flags |= UX_HOST_CLASS_PRINTER_FLAG_LOCK;
120     printer -> ux_host_class_printer_device -> ux_device_flags |= UX_DEVICE_FLAG_LOCK;
121     UX_RESTORE
122 #else
123 
124     /* Protect thread reentry to this instance.  */
125     status =  _ux_host_semaphore_get(&printer -> ux_host_class_printer_semaphore, UX_WAIT_FOREVER);
126     if (status != UX_SUCCESS)
127 
128         /* Return error.  */
129         return(status);
130 
131     /* Protect the control endpoint semaphore here.  It will be unprotected in the
132        transfer request function.  */
133     status =  _ux_utility_semaphore_get(&printer -> ux_host_class_printer_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
134 
135     /* Check for status.  */
136     if (status != UX_SUCCESS)
137     {
138         _ux_utility_semaphore_put(&printer -> ux_host_class_printer_semaphore);
139         return(status);
140     }
141 #endif
142 
143     /* We need to get the default control endpoint transfer request pointer.  */
144     control_endpoint =  &printer -> ux_host_class_printer_device -> ux_device_control_endpoint;
145     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
146 
147     /* Need interface for wIndex.  */
148     interface_ptr = printer -> ux_host_class_printer_interface;
149 
150     /* Create a transfer_request for the SOFT_RESET request.  */
151     transfer_request -> ux_transfer_request_data_pointer =      UX_NULL;
152     transfer_request -> ux_transfer_request_requested_length =  0;
153     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_PRINTER_SOFT_RESET;
154     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
155     transfer_request -> ux_transfer_request_value =             0;
156     transfer_request -> ux_transfer_request_index =             interface_ptr -> ux_interface_descriptor.bInterfaceNumber;
157 
158 #if defined(UX_HOST_STANDALONE)
159 
160     /* Enable auto unlock device.  */
161     transfer_request -> ux_transfer_request_flags |= UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK;
162 #endif
163 
164     /* Send request to HCD layer.  */
165     status =  _ux_host_stack_transfer_request(transfer_request);
166 
167     /* Unprotect thread reentry to this instance.  */
168     _ux_host_class_printer_unlock(printer);
169 
170     /* Return completion status.  */
171     return(status);
172 }
173 
174 /**************************************************************************/
175 /*                                                                        */
176 /*  FUNCTION                                               RELEASE        */
177 /*                                                                        */
178 /*    _uxe_host_class_printer_soft_reset                  PORTABLE C      */
179 /*                                                           6.3.0        */
180 /*  AUTHOR                                                                */
181 /*                                                                        */
182 /*    Yajun Xia, Microsoft Corporation                                    */
183 /*                                                                        */
184 /*  DESCRIPTION                                                           */
185 /*                                                                        */
186 /*    This function checks errors in printer soft reset function call.    */
187 /*                                                                        */
188 /*  INPUT                                                                 */
189 /*                                                                        */
190 /*    printer                               Pointer to printer class      */
191 /*                                                                        */
192 /*  OUTPUT                                                                */
193 /*                                                                        */
194 /*    Completion Status                                                   */
195 /*                                                                        */
196 /*  CALLS                                                                 */
197 /*                                                                        */
198 /*    _ux_host_class_printer_soft_reset     Printer soft reset            */
199 /*                                                                        */
200 /*  CALLED BY                                                             */
201 /*                                                                        */
202 /*    Application                                                         */
203 /*                                                                        */
204 /*  RELEASE HISTORY                                                       */
205 /*                                                                        */
206 /*    DATE              NAME                      DESCRIPTION             */
207 /*                                                                        */
208 /*  10-31-2023        Yajun xia             Initial Version 6.3.0         */
209 /*                                                                        */
210 /**************************************************************************/
_uxe_host_class_printer_soft_reset(UX_HOST_CLASS_PRINTER * printer)211 UINT   _uxe_host_class_printer_soft_reset(UX_HOST_CLASS_PRINTER *printer)
212 {
213 
214     /* Sanity checks.  */
215     if (printer == UX_NULL)
216         return(UX_INVALID_PARAMETER);
217 
218     /* Call the actual printer soft reset function.  */
219     return(_ux_host_class_printer_soft_reset(printer));
220 }