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