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_device_id_get                PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function obtains the printer IEEE 1284 device ID string        */
45 /*    (including length in the first two bytes in big endian format).     */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    printer                               Pointer to printer class      */
50 /*    descriptor_buffer                     Pointer to a buffer to fill   */
51 /*                                          IEEE 1284 device ID string    */
52 /*                                          (including length in the      */
53 /*                                          first two bytes in BE format) */
54 /*    length                                Length of buffer in bytes     */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_transfer_request       Process transfer request      */
63 /*    _ux_utility_memory_allocate           Allocate memory block         */
64 /*    _ux_utility_memory_compare            Compare memory block          */
65 /*    _ux_utility_memory_copy               Copy memory block             */
66 /*    _ux_utility_memory_free               Free memory block             */
67 /*    _ux_utility_short_get_big_endian      Get 16-bit value              */
68 /*                                                                        */
69 /*  CALLED BY                                                             */
70 /*                                                                        */
71 /*    Application                                                         */
72 /*                                                                        */
73 /*  RELEASE HISTORY                                                       */
74 /*                                                                        */
75 /*    DATE              NAME                      DESCRIPTION             */
76 /*                                                                        */
77 /*  02-02-2021     Chaoqiong Xiao           Initial Version 6.1.4         */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            added standalone support,   */
80 /*                                            resulting in version 6.1.10 */
81 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            fixed parameter/variable    */
83 /*                                            names conflict C++ keyword, */
84 /*                                            resulting in version 6.1.12 */
85 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
86 /*                                            fixed compile warnings,     */
87 /*                                            resulting in version 6.3.0  */
88 /*                                                                        */
89 /**************************************************************************/
_ux_host_class_printer_device_id_get(UX_HOST_CLASS_PRINTER * printer,UCHAR * descriptor_buffer,ULONG length)90 UINT  _ux_host_class_printer_device_id_get(UX_HOST_CLASS_PRINTER *printer, UCHAR *descriptor_buffer, ULONG length)
91 {
92 #if defined(UX_HOST_STANDALONE)
93 UX_INTERRUPT_SAVE_AREA
94 #endif
95 UX_INTERFACE         *interface_ptr;
96 UX_ENDPOINT          *control_endpoint;
97 UX_TRANSFER          *transfer_request;
98 UINT                 status;
99 
100 
101     /* If trace is enabled, insert this event into the trace buffer.  */
102     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PRINTER_DEVICE_ID_GET, printer, descriptor_buffer, length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
103 
104     /* Protect thread reentry to this instance.  */
105 #if defined(UX_HOST_STANDALONE)
106     UX_DISABLE
107     if ((printer -> ux_host_class_printer_flags & UX_HOST_CLASS_PRINTER_FLAG_LOCK) ||
108         (printer -> ux_host_class_printer_device -> ux_device_flags & UX_DEVICE_FLAG_LOCK))
109     {
110         UX_RESTORE
111         return(UX_BUSY);
112     }
113     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     UX_RESTORE
116 #else
117 
118     status =  _ux_host_semaphore_get(&printer -> ux_host_class_printer_semaphore, UX_WAIT_FOREVER);
119     if (status != UX_SUCCESS)
120         return(status);
121 
122     /* Protect the control endpoint semaphore here.  It will be unprotected in the
123        transfer request function.  */
124     status =  _ux_host_semaphore_get(&printer -> ux_host_class_printer_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
125 
126     /* Check for status.  */
127     if (status != UX_SUCCESS)
128     {
129         _ux_host_semaphore_put(&printer -> ux_host_class_printer_semaphore);
130         return(status);
131     }
132 #endif
133 
134     /* We need to get the default control endpoint transfer request pointer.  */
135     control_endpoint =  &printer -> ux_host_class_printer_device -> ux_device_control_endpoint;
136     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
137 
138     /* Need interface for wIndex.  */
139     interface_ptr = printer -> ux_host_class_printer_interface;
140 
141     /* Create a transfer request for the GET_DEVICE_ID request.  */
142     transfer_request -> ux_transfer_request_data_pointer =      descriptor_buffer;
143     transfer_request -> ux_transfer_request_requested_length =  length;
144     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_PRINTER_GET_DEVICE_ID;
145     transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
146     transfer_request -> ux_transfer_request_value =             0; /* Do not support multiple configuration for now.  */
147     transfer_request -> ux_transfer_request_index =             ((UINT)interface_ptr -> ux_interface_descriptor.bInterfaceNumber  << 8) |
148                                                                 (interface_ptr -> ux_interface_descriptor.bAlternateSetting     );
149 
150 #if defined(UX_HOST_STANDALONE)
151 
152     /* Enable auto unlock device.  */
153     transfer_request -> ux_transfer_request_flags |= UX_TRANSFER_FLAG_AUTO_DEVICE_UNLOCK;
154 #endif
155 
156     /* Send request to HCD layer.  */
157     status =  _ux_host_stack_transfer_request(transfer_request);
158 
159     /* Unprotect thread reentry to this instance.  */
160     _ux_host_class_printer_unlock(printer);
161 
162     /* Return completion status.  */
163     return(status);
164 }
165 
166 /**************************************************************************/
167 /*                                                                        */
168 /*  FUNCTION                                               RELEASE        */
169 /*                                                                        */
170 /*    _uxe_host_class_printer_device_id_get               PORTABLE C      */
171 /*                                                           6.3.0        */
172 /*  AUTHOR                                                                */
173 /*                                                                        */
174 /*    Yajun Xia, Microsoft Corporation                                    */
175 /*                                                                        */
176 /*  DESCRIPTION                                                           */
177 /*                                                                        */
178 /*    This function checks errors in printer device ID get function call. */
179 /*                                                                        */
180 /*  INPUT                                                                 */
181 /*                                                                        */
182 /*    printer                               Pointer to printer class      */
183 /*    descriptor_buffer                     Pointer to a buffer to fill   */
184 /*                                          IEEE 1284 device ID string    */
185 /*                                          (including length in the      */
186 /*                                          first two bytes in BE format) */
187 /*    length                                Length of buffer in bytes     */
188 /*                                                                        */
189 /*  OUTPUT                                                                */
190 /*                                                                        */
191 /*    Completion Status                                                   */
192 /*                                                                        */
193 /*  CALLS                                                                 */
194 /*                                                                        */
195 /*    _ux_host_class_printer_device_id_get  Obtains the printer device ID */
196 /*                                                                        */
197 /*  CALLED BY                                                             */
198 /*                                                                        */
199 /*    Application                                                         */
200 /*                                                                        */
201 /*  RELEASE HISTORY                                                       */
202 /*                                                                        */
203 /*    DATE              NAME                      DESCRIPTION             */
204 /*                                                                        */
205 /*  10-31-2023        Yajun xia             Initial Version 6.3.0         */
206 /*                                                                        */
207 /**************************************************************************/
_uxe_host_class_printer_device_id_get(UX_HOST_CLASS_PRINTER * printer,UCHAR * descriptor_buffer,ULONG length)208 UINT  _uxe_host_class_printer_device_id_get(UX_HOST_CLASS_PRINTER *printer, UCHAR *descriptor_buffer, ULONG length)
209 {
210 
211     /* Sanity checks.  */
212     if ((printer == UX_NULL) || (descriptor_buffer == UX_NULL) || (length == 0))
213         return(UX_INVALID_PARAMETER);
214 
215     /* Call the actual printer device ID get function.  */
216     return(_ux_host_class_printer_device_id_get(printer, descriptor_buffer, length));
217 }