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