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 /** USBX Component                                                        */
14 /**                                                                       */
15 /**   Device Printer Class                                                */
16 /**                                                                       */
17 /**************************************************************************/
18 /**************************************************************************/
19 
20 #define UX_SOURCE_CODE
21 
22 
23 /* Include necessary system files.  */
24 
25 #include "ux_api.h"
26 #include "ux_device_class_printer.h"
27 #include "ux_device_stack.h"
28 
29 
30 /**************************************************************************/
31 /*                                                                        */
32 /*  FUNCTION                                               RELEASE        */
33 /*                                                                        */
34 /*    _ux_device_class_printer_activate                   PORTABLE C      */
35 /*                                                           6.3.0        */
36 /*  AUTHOR                                                                */
37 /*                                                                        */
38 /*    Chaoqiong Xiao, Microsoft Corporation                               */
39 /*                                                                        */
40 /*  DESCRIPTION                                                           */
41 /*                                                                        */
42 /*    This function initializes the USB Printer device.                   */
43 /*                                                                        */
44 /*  INPUT                                                                 */
45 /*                                                                        */
46 /*    command                               Pointer to printer command    */
47 /*                                                                        */
48 /*  OUTPUT                                                                */
49 /*                                                                        */
50 /*    Completion Status                                                   */
51 /*                                                                        */
52 /*  CALLS                                                                 */
53 /*                                                                        */
54 /*    (ux_device_class_printer_instance_deactivate)                       */
55 /*                                          Notify activation             */
56 /*                                                                        */
57 /*  CALLED BY                                                             */
58 /*                                                                        */
59 /*    Device Printer Class                                                */
60 /*                                                                        */
61 /*  RELEASE HISTORY                                                       */
62 /*                                                                        */
63 /*    DATE              NAME                      DESCRIPTION             */
64 /*                                                                        */
65 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
66 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
67 /*                                            fixed parameter/variable    */
68 /*                                            names conflict C++ keyword, */
69 /*                                            resulting in version 6.1.12 */
70 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            added a new mode to manage  */
72 /*                                            endpoint buffer in classes, */
73 /*                                            resulting in version 6.3.0  */
74 /*                                                                        */
75 /**************************************************************************/
_ux_device_class_printer_activate(UX_SLAVE_CLASS_COMMAND * command)76 UINT  _ux_device_class_printer_activate(UX_SLAVE_CLASS_COMMAND *command)
77 {
78 
79 UX_SLAVE_INTERFACE                      *printer_interface;
80 UX_SLAVE_CLASS                          *printer_class;
81 UX_DEVICE_CLASS_PRINTER                 *printer;
82 UX_SLAVE_ENDPOINT                       *endpoint;
83 
84     /* Get the class container.  */
85     printer_class = command -> ux_slave_class_command_class_ptr;
86 
87     /* Get the class instance in the container.  */
88     printer = (UX_DEVICE_CLASS_PRINTER *) printer_class -> ux_slave_class_instance;
89 
90     /* Get the interface that owns this instance.  */
91     printer_interface =  (UX_SLAVE_INTERFACE  *) command -> ux_slave_class_command_interface;
92 
93     /* Store the class instance into the interface.  */
94     printer_interface -> ux_slave_interface_class_instance =  (VOID *)printer;
95 
96     /* Now the opposite, store the interface in the class instance.  */
97     printer -> ux_device_class_printer_interface =  printer_interface;
98 
99     /* Save endpoints for future use.  */
100     printer -> ux_device_class_printer_endpoint_in = UX_NULL;
101     printer -> ux_device_class_printer_endpoint_out = UX_NULL;
102     endpoint = printer_interface -> ux_slave_interface_first_endpoint;
103     while(endpoint)
104     {
105         if (endpoint -> ux_slave_endpoint_descriptor.bmAttributes == UX_BULK_ENDPOINT)
106         {
107             if (endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_IN)
108             {
109                 printer -> ux_device_class_printer_endpoint_in = endpoint;
110 #if defined(UX_DEVICE_CLASS_PRINTER_OWN_ENDPOINT_BUFFER)
111                 endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
112                                 UX_DEVICE_CLASS_PRINTER_WRITE_BUFFER(printer);
113 #endif
114                 if (printer -> ux_device_class_printer_endpoint_out)
115                     break;
116             }
117             else
118             {
119                 printer -> ux_device_class_printer_endpoint_out = endpoint;
120 #if defined(UX_DEVICE_CLASS_PRINTER_OWN_ENDPOINT_BUFFER)
121                 endpoint -> ux_slave_endpoint_transfer_request.ux_slave_transfer_request_data_pointer =
122                                 UX_DEVICE_CLASS_PRINTER_READ_BUFFER(printer);
123 #endif
124                 if (printer -> ux_device_class_printer_endpoint_in)
125                     break;
126             }
127         }
128 
129         /* Next endpoint.  */
130         endpoint = endpoint -> ux_slave_endpoint_next_endpoint;
131     }
132 
133     /* Check error, there must be Bulk OUT.  */
134     if (printer -> ux_device_class_printer_endpoint_out == UX_NULL)
135         return(UX_DESCRIPTOR_CORRUPTED);
136 
137     /* Initialize port status.
138         Benign status of "Paper Not Empty", "Selected", and "No Error".  */
139     printer -> ux_device_class_printer_port_status = UX_DEVICE_CLASS_PRINTER_SELECT |
140                                                      UX_DEVICE_CLASS_PRINTER_NOT_ERROR;
141 
142     /* If there is a activate function call it.  */
143     if (printer -> ux_device_class_printer_parameter.ux_device_class_printer_instance_activate != UX_NULL)
144     {
145         /* Invoke the application.  */
146         printer -> ux_device_class_printer_parameter.ux_device_class_printer_instance_activate(printer);
147     }
148 
149     /* If trace is enabled, insert this event into the trace buffer.  */
150     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PRINTER_ACTIVATE, printer, 0, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
151 
152     /* If trace is enabled, register this object.  */
153     UX_TRACE_OBJECT_REGISTER(UX_TRACE_DEVICE_OBJECT_TYPE_INTERFACE, printer, 0, 0, 0)
154 
155     /* Return completion status.  */
156     return(UX_SUCCESS);
157 }
158 
159