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