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_endpoints_get                PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function search for the handle of the bulk out endpoint and    */
45 /*    optionally the bulk in endpoint of the printer is bidirectional.    */
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_interface_endpoint_get Get interface endpoint        */
58 /*                                                                        */
59 /*  CALLED BY                                                             */
60 /*                                                                        */
61 /*    _ux_host_class_printer_activate       Activate printer class        */
62 /*                                                                        */
63 /*  RELEASE HISTORY                                                       */
64 /*                                                                        */
65 /*    DATE              NAME                      DESCRIPTION             */
66 /*                                                                        */
67 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
68 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
69 /*                                            resulting in version 6.1    */
70 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            initialized timeout values, */
72 /*                                            resulting in version 6.1.10 */
73 /*                                                                        */
74 /**************************************************************************/
_ux_host_class_printer_endpoints_get(UX_HOST_CLASS_PRINTER * printer)75 UINT  _ux_host_class_printer_endpoints_get(UX_HOST_CLASS_PRINTER *printer)
76 {
77 
78 UINT            status;
79 UINT            endpoint_index;
80 UX_ENDPOINT     *endpoint;
81 
82 
83     /* Search the bulk OUT endpoint. It is attached to the interface container.  */
84     for (endpoint_index = 0; endpoint_index < printer -> ux_host_class_printer_interface -> ux_interface_descriptor.bNumEndpoints;
85                         endpoint_index++)
86     {
87 
88         /* Get interface endpoint.  */
89         status =  _ux_host_stack_interface_endpoint_get(printer -> ux_host_class_printer_interface, endpoint_index, &endpoint);
90 
91         /* Check the completion status.  */
92         if (status == UX_SUCCESS)
93         {
94 
95             /* Check if endpoint is bulk and OUT.  */
96             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
97                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
98             {
99 
100                 /* This transfer_request always have the OUT direction.  */
101                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;
102 
103                 /* By default wait UX_HOST_CLASS_PRINTER_CLASS_TRANSFER_TIMEOUT.  */
104                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value =
105                                 UX_MS_TO_TICK(UX_HOST_CLASS_PRINTER_CLASS_TRANSFER_TIMEOUT);
106 
107                 /* We have found the bulk endpoint, save it.  */
108                 printer -> ux_host_class_printer_bulk_out_endpoint =  endpoint;
109                 break;
110             }
111         }
112     }
113 
114     /* The bulk out endpoint is mandatory.  */
115     if (printer -> ux_host_class_printer_bulk_out_endpoint == UX_NULL)
116     {
117 
118         /* If trace is enabled, insert this event into the trace buffer.  */
119         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, printer, 0, 0, UX_TRACE_ERRORS, 0, 0)
120 
121         return(UX_ENDPOINT_HANDLE_UNKNOWN);
122     }
123 
124     /* Search the bulk IN endpoint. This endpoint is optional and only valid for
125        bidirectional printers. It is attached to the interface container.  */
126     if ((printer -> ux_host_class_printer_interface -> ux_interface_descriptor.bInterfaceProtocol ==
127                                                 UX_HOST_CLASS_PRINTER_PROTOCOL_BI_DIRECTIONAL) ||
128         (printer -> ux_host_class_printer_interface -> ux_interface_descriptor.bInterfaceProtocol ==
129                                                 UX_HOST_CLASS_PRINTER_PROTOCOL_IEEE_1284_4_BI_DIR))
130     {
131 
132         for (endpoint_index = 0; endpoint_index < printer -> ux_host_class_printer_interface -> ux_interface_descriptor.bNumEndpoints;
133                             endpoint_index++)
134         {
135 
136             /* Get the endpoint handle.  */
137             status =  _ux_host_stack_interface_endpoint_get(printer -> ux_host_class_printer_interface, endpoint_index, &endpoint);
138 
139             /* Check the completion status.  */
140             if (status == UX_SUCCESS)
141             {
142 
143                 /* Check if endpoint is bulk and IN.  */
144                 if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
145                     ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
146                 {
147 
148                     /* This transfer_request always have the IN direction.  */
149                     endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
150 
151                     /* By default wait UX_HOST_CLASS_PRINTER_CLASS_TRANSFER_TIMEOUT.  */
152                     endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value =
153                                     UX_MS_TO_TICK(UX_HOST_CLASS_PRINTER_CLASS_TRANSFER_TIMEOUT);
154 
155                     /* We have found the bulk endpoint, save it.  */
156                     printer -> ux_host_class_printer_bulk_in_endpoint =  endpoint;
157                     break;
158                 }
159             }
160         }
161 
162         /* The bulk in endpoint is mandatory for these protocol.  */
163         if (printer -> ux_host_class_printer_bulk_in_endpoint == UX_NULL)
164         {
165 
166             /* If trace is enabled, insert this event into the trace buffer.  */
167             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, printer, 0, 0, UX_TRACE_ERRORS, 0, 0)
168 
169             return(UX_ENDPOINT_HANDLE_UNKNOWN);
170         }
171     }
172 
173     /* All endpoints have been mounted.  */
174     return(UX_SUCCESS);
175 }
176 
177