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_activate                     PORTABLE C      */
38 /*                                                           6.1.12       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function calls the USBX stack to activate the class.           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    command                               Printer class command pointer */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    Completion Status                                                   */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*    _ux_host_class_printer_configure      Configure printer class       */
58 /*    _ux_host_class_printer_endpoints_get  Get endpoints of printer      */
59 /*    _ux_host_class_printer_name_get       Get printer name              */
60 /*    _ux_host_stack_class_instance_create  Create class instance         */
61 /*    _ux_host_stack_class_instance_destroy Destroy the class instance    */
62 /*    _ux_utility_memory_allocate           Allocate memory block         */
63 /*    _ux_utility_memory_free               Free memory block             */
64 /*    _ux_host_semaphore_create             Create printer semaphore      */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    _ux_host_class_printer_entry          Entry of printer class        */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            added standalone support,   */
79 /*                                            resulting in version 6.1.10 */
80 /*  07-29-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            fixed parameter/variable    */
82 /*                                            names conflict C++ keyword, */
83 /*                                            resulting in version 6.1.12 */
84 /*                                                                        */
85 /**************************************************************************/
_ux_host_class_printer_activate(UX_HOST_CLASS_COMMAND * command)86 UINT  _ux_host_class_printer_activate(UX_HOST_CLASS_COMMAND *command)
87 {
88 
89 UX_INTERFACE            *interface_ptr;
90 UX_HOST_CLASS_PRINTER   *printer;
91 #if !defined(UX_HOST_STANDALONE)
92 UINT                    status;
93 #endif
94 
95 
96     /* The printer is always activated by the interface descriptor and not the
97        device descriptor.  */
98     interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;
99 
100     /* Obtain memory for this class instance.  */
101     printer =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_PRINTER));
102     if (printer == UX_NULL)
103         return(UX_MEMORY_INSUFFICIENT);
104 
105     /* Store the class container into this instance.  */
106     printer -> ux_host_class_printer_class =  command -> ux_host_class_command_class_ptr;
107 
108     /* Store the interface container into the printer class instance.  */
109     printer -> ux_host_class_printer_interface =  interface_ptr;
110 
111     /* Store the device container into the printer class instance.  */
112     printer -> ux_host_class_printer_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
113 
114     /* This instance of the device must also be stored in the interface container.  */
115     interface_ptr -> ux_interface_class_instance =  (VOID *) printer;
116 
117     /* Create this class instance.  */
118     _ux_host_stack_class_instance_create(printer -> ux_host_class_printer_class, (VOID *) printer);
119 
120 #if defined(UX_HOST_STANDALONE)
121 
122     /* Do activate steps.  */
123     printer -> ux_host_class_printer_enum_state = UX_STATE_WAIT;
124     printer -> ux_host_class_printer_state = UX_HOST_CLASS_INSTANCE_MOUNTING;
125     return(UX_SUCCESS);
126 #else
127 
128     /* Configure the printer.  */
129     status =  _ux_host_class_printer_configure(printer);
130 
131     /* Get the printer endpoint(s). We may need to search for Bulk Out and Bulk In endpoints.  */
132     if (status == UX_SUCCESS)
133         status =  _ux_host_class_printer_endpoints_get(printer);
134 
135     /* Get the name of the printer from the 1284 descriptor.  */
136     if (status == UX_SUCCESS)
137         status =  _ux_host_class_printer_name_get(printer);
138 
139     /* Create the semaphore to protect 2 threads from accessing the same printer instance.  */
140     if (status == UX_SUCCESS)
141     {
142         status =  _ux_host_semaphore_create(&printer -> ux_host_class_printer_semaphore, "ux_host_class_printer_semaphore", 1);
143         if (status != UX_SUCCESS)
144             status = UX_SEMAPHORE_ERROR;
145     }
146 
147     /* Success things.  */
148     if (status == UX_SUCCESS)
149     {
150 
151         /* Mark the printer as live now.  */
152         printer -> ux_host_class_printer_state =  UX_HOST_CLASS_INSTANCE_LIVE;
153 
154         /* If all is fine and the device is mounted, we may need to inform the application
155         if a function has been programmed in the system structure.  */
156         if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
157         {
158 
159             /* Call system change function.  */
160             _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, printer -> ux_host_class_printer_class, (VOID *) printer);
161         }
162 
163         /* If trace is enabled, insert this event into the trace buffer.  */
164         UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PRINTER_ACTIVATE, printer, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
165 
166         /* If trace is enabled, register this object.  */
167         UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, printer, 0, 0, 0)
168 
169         /* Return success.  */
170         return(UX_SUCCESS);
171     }
172 
173     /* On error, free resources.  */
174     _ux_host_stack_class_instance_destroy(printer -> ux_host_class_printer_class, (VOID *) printer);
175     interface_ptr -> ux_interface_class_instance = UX_NULL;
176     _ux_utility_memory_free(printer);
177 
178     /* Return completion status.  */
179     return(status);
180 
181 #endif
182 }
183 
184