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 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Printer Class                                                       */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_printer.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_printer_deactivate                   PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is called when this instance of the printer has been  */
45 /*    removed from the bus either directly or indirectly. The bulk in\out */
46 /*    pipes will be destroyed and the instanced removed.                  */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    command                               Printer class command pointer */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_class_instance_destroy Destroy the class instance    */
59 /*    _ux_host_stack_endpoint_transfer_abort                              */
60 /*                                          Abort endpoint transfer       */
61 /*    _ux_utility_memory_free               Free memory block             */
62 /*    _ux_host_semaphore_get                Get protection semaphore      */
63 /*    _ux_host_semaphore_delete             Delete protection semaphore   */
64 /*    _ux_utility_thread_schedule_other     Schedule other threads        */
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 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_printer_deactivate(UX_HOST_CLASS_COMMAND * command)82 UINT  _ux_host_class_printer_deactivate(UX_HOST_CLASS_COMMAND *command)
83 {
84 
85 UX_HOST_CLASS_PRINTER       *printer;
86 #if !defined(UX_HOST_STANDALONE)
87 UINT                        status;
88 #endif
89 
90 
91     /* Get the instance for this class.  */
92     printer =  (UX_HOST_CLASS_PRINTER *) command -> ux_host_class_command_instance;
93 
94     /* The printer is being shut down.  */
95     printer -> ux_host_class_printer_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;
96 
97 #if !defined(UX_HOST_STANDALONE)
98 
99     /* Protect thread reentry to this instance.  */
100     status =  _ux_host_semaphore_get(&printer -> ux_host_class_printer_semaphore, UX_WAIT_FOREVER);
101     if (status != UX_SUCCESS)
102 
103         /* Return error.  */
104         return(status);
105 #endif
106 
107     /* We need to abort transactions on the bulk out pipe.  */
108     _ux_host_stack_endpoint_transfer_abort(printer -> ux_host_class_printer_bulk_out_endpoint);
109 
110     /* If the printer is bidirectional, we need to abort transactions on the bulk in pipe.  */
111     if (printer -> ux_host_class_printer_interface -> ux_interface_descriptor.bInterfaceProtocol == UX_HOST_CLASS_PRINTER_PROTOCOL_BI_DIRECTIONAL)
112         _ux_host_stack_endpoint_transfer_abort(printer -> ux_host_class_printer_bulk_in_endpoint);
113 
114 #if !defined(UX_HOST_STANDALONE)
115 
116     /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
117        endpoints to exit properly.  */
118     _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
119 #else
120 
121     /* Free allocated memory.  */
122     if (printer -> ux_host_class_printer_allocated)
123         _ux_utility_memory_free(printer -> ux_host_class_printer_allocated);
124 #endif
125 
126     /* Destroy the instance.  */
127     _ux_host_stack_class_instance_destroy(printer -> ux_host_class_printer_class, (VOID *) printer);
128 
129 #if !defined(UX_HOST_STANDALONE)
130 
131     /* Destroy the semaphore.  */
132     _ux_host_semaphore_delete(&printer -> ux_host_class_printer_semaphore);
133 #endif
134 
135     /* Before we free the device resources, we need to inform the application
136         that the device is removed.  */
137     if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
138     {
139 
140         /* Inform the application the device is removed.  */
141         _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, printer -> ux_host_class_printer_class, (VOID *) printer);
142     }
143 
144     /* If trace is enabled, insert this event into the trace buffer.  */
145     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PRINTER_DEACTIVATE, printer, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
146 
147     /* If trace is enabled, register this object.  */
148     UX_TRACE_OBJECT_UNREGISTER(printer);
149 
150     /* Free the printer instance memory.  */
151     _ux_utility_memory_free(printer);
152 
153     /* Return successful status.  */
154     return(UX_SUCCESS);
155 }
156 
157