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_initialize                 PORTABLE C      */
36 /*                                                           6.2.0        */
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_utility_memory_allocate           Allocate memory               */
56 /*    _ux_utility_memory_free               Free memory                   */
57 /*    _ux_utility_mutex_create              Create mutex                  */
58 /*    _ux_device_mutex_delete               Delete mutex                  */
59 /*                                                                        */
60 /*  CALLED BY                                                             */
61 /*                                                                        */
62 /*    Device Stack                                                        */
63 /*                                                                        */
64 /*  RELEASE HISTORY                                                       */
65 /*                                                                        */
66 /*    DATE              NAME                      DESCRIPTION             */
67 /*                                                                        */
68 /*  01-31-2022     Chaoqiong Xiao           Initial Version 6.1.10        */
69 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
70 /*                                            fixed standalone compile,   */
71 /*                                            resulting in version 6.1.11 */
72 /*  10-31-2022     Yajun Xia                Modified comment(s),          */
73 /*                                            added standalone support,   */
74 /*                                            resulting in version 6.2.0  */
75 /*                                                                        */
76 /**************************************************************************/
_ux_device_class_printer_initialize(UX_SLAVE_CLASS_COMMAND * command)77 UINT  _ux_device_class_printer_initialize(UX_SLAVE_CLASS_COMMAND *command)
78 {
79 
80 UX_DEVICE_CLASS_PRINTER                 *printer;
81 UX_DEVICE_CLASS_PRINTER_PARAMETER       *printer_parameter;
82 UX_SLAVE_CLASS                          *printer_class;
83 #if !defined(UX_DEVICE_STANDALONE)
84 UINT                                    status;
85 #endif
86 
87     /* Get the class container.  */
88     printer_class =  command -> ux_slave_class_command_class_ptr;
89 
90     /* Create an instance of the device printer class.  */
91     printer =  _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_DEVICE_CLASS_PRINTER));
92 
93     /* Check for successful allocation.  */
94     if (printer == UX_NULL)
95         return(UX_MEMORY_INSUFFICIENT);
96 
97     /* Save the address of the Printer instance inside the Printer container.  */
98     printer_class -> ux_slave_class_instance = (VOID *) printer;
99 
100     /* Get the pointer to the application parameters for the printer class.  */
101     printer_parameter =  command -> ux_slave_class_command_parameter;
102 
103     /* Store the start and stop signals if needed by the application.  */
104     printer -> ux_device_class_printer_parameter.ux_device_class_printer_device_id           = printer_parameter -> ux_device_class_printer_device_id;
105     printer -> ux_device_class_printer_parameter.ux_device_class_printer_instance_activate   = printer_parameter -> ux_device_class_printer_instance_activate;
106     printer -> ux_device_class_printer_parameter.ux_device_class_printer_instance_deactivate = printer_parameter -> ux_device_class_printer_instance_deactivate;
107     printer -> ux_device_class_printer_parameter.ux_device_class_printer_soft_reset          = printer_parameter -> ux_device_class_printer_soft_reset;
108 
109 #if !defined(UX_DEVICE_STANDALONE)
110     /* Create the Mutex for each endpoint as multiple threads cannot access each pipe at the same time.  */
111     status =  _ux_utility_mutex_create(&printer -> ux_device_class_printer_endpoint_in_mutex, "ux_device_class_printer_in_mutex");
112 
113     /* Check Mutex creation error.  */
114     if(status != UX_SUCCESS)
115     {
116 
117         /* Free the resources.  */
118         _ux_utility_memory_free(printer);
119 
120         /* Return fatal error.  */
121         return(UX_MUTEX_ERROR);
122     }
123 
124     /* Out Mutex. */
125     status =  _ux_utility_mutex_create(&printer -> ux_device_class_printer_endpoint_out_mutex, "ux_device_class_printer_out_mutex");
126 
127     /* Check Mutex creation error.  */
128     if(status != UX_SUCCESS)
129     {
130 
131         /* Delete the endpoint IN mutex.  */
132         _ux_device_mutex_delete(&printer -> ux_device_class_printer_endpoint_in_mutex);
133 
134         /* Free the resources.  */
135         _ux_utility_memory_free(printer);
136 
137         /* Return fatal error.  */
138         return(UX_MUTEX_ERROR);
139     }
140 #else
141     printer -> ux_device_class_printer_write_state = UX_STATE_RESET;
142     printer -> ux_device_class_printer_read_state = UX_STATE_RESET;
143 #endif
144 
145     /* Reset port status.  */
146     printer -> ux_device_class_printer_port_status = 0;
147 
148     /* Return completion status.  */
149     return(UX_SUCCESS);
150 }
151 
152 /**************************************************************************/
153 /*                                                                        */
154 /*  FUNCTION                                               RELEASE        */
155 /*                                                                        */
156 /*    _uxe_device_class_printer_initialize                  PORTABLE C    */
157 /*                                                           6.2.1        */
158 /*  AUTHOR                                                                */
159 /*                                                                        */
160 /*    Yajun Xia, Microsoft Corporation                                    */
161 /*                                                                        */
162 /*  DESCRIPTION                                                           */
163 /*                                                                        */
164 /*    This function checks errors in printer initialization function call.*/
165 /*                                                                        */
166 /*  INPUT                                                                 */
167 /*                                                                        */
168 /*    command                               Pointer to printer command    */
169 /*                                                                        */
170 /*  OUTPUT                                                                */
171 /*                                                                        */
172 /*    Completion Status                                                   */
173 /*                                                                        */
174 /*  CALLS                                                                 */
175 /*                                                                        */
176 /*    _ux_device_class_printer_initialize   Initialize printer instance   */
177 /*                                                                        */
178 /*  CALLED BY                                                             */
179 /*                                                                        */
180 /*    Device Stack                                                        */
181 /*                                                                        */
182 /*  RELEASE HISTORY                                                       */
183 /*                                                                        */
184 /*    DATE              NAME                      DESCRIPTION             */
185 /*                                                                        */
186 /*  03-08-2023     Yajun Xia                Initial Version 6.2.1         */
187 /*                                                                        */
188 /**************************************************************************/
_uxe_device_class_printer_initialize(UX_SLAVE_CLASS_COMMAND * command)189 UINT  _uxe_device_class_printer_initialize(UX_SLAVE_CLASS_COMMAND *command)
190 {
191 UX_DEVICE_CLASS_PRINTER_PARAMETER       *printer_parameter;
192 ULONG length;
193 
194     /* Get the pointer to the application parameters for the printer class.  */
195     printer_parameter =  command -> ux_slave_class_command_parameter;
196 
197     /* Sanity checks.  */
198 
199     /* Length of data (first two bytes in big endian).  */
200     length = _ux_utility_short_get_big_endian(printer_parameter -> ux_device_class_printer_device_id);
201 
202     if (length > UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH)
203     {
204         return(UX_INVALID_PARAMETER);
205     }
206 
207     return (_ux_device_class_printer_initialize(command));
208 }
209