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_deactivate                   PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is called when this instance of the printer has been  */
46 /*    removed from the bus either directly or indirectly. The bulk in\out */
47 /*    pipes will be destroyed and the instanced removed.                  */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    command                               Printer class command pointer */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_class_instance_destroy Destroy the class instance    */
60 /*    _ux_host_stack_endpoint_transfer_abort                              */
61 /*                                          Abort endpoint transfer       */
62 /*    _ux_utility_memory_free               Free memory block             */
63 /*    _ux_host_semaphore_get                Get protection semaphore      */
64 /*    _ux_host_semaphore_delete             Delete protection semaphore   */
65 /*    _ux_utility_thread_schedule_other     Schedule other threads        */
66 /*                                                                        */
67 /*  CALLED BY                                                             */
68 /*                                                                        */
69 /*    _ux_host_class_printer_entry          Entry of printer class        */
70 /*                                                                        */
71 /*  RELEASE HISTORY                                                       */
72 /*                                                                        */
73 /*    DATE              NAME                      DESCRIPTION             */
74 /*                                                                        */
75 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
76 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            added standalone support,   */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_printer_deactivate(UX_HOST_CLASS_COMMAND * command)83 UINT  _ux_host_class_printer_deactivate(UX_HOST_CLASS_COMMAND *command)
84 {
85 
86 UX_HOST_CLASS_PRINTER       *printer;
87 #if !defined(UX_HOST_STANDALONE)
88 UINT                        status;
89 #endif
90 
91 
92     /* Get the instance for this class.  */
93     printer =  (UX_HOST_CLASS_PRINTER *) command -> ux_host_class_command_instance;
94 
95     /* The printer is being shut down.  */
96     printer -> ux_host_class_printer_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;
97 
98 #if !defined(UX_HOST_STANDALONE)
99 
100     /* Protect thread reentry to this instance.  */
101     status =  _ux_host_semaphore_get(&printer -> ux_host_class_printer_semaphore, UX_WAIT_FOREVER);
102     if (status != UX_SUCCESS)
103 
104         /* Return error.  */
105         return(status);
106 #endif
107 
108     /* We need to abort transactions on the bulk out pipe.  */
109     _ux_host_stack_endpoint_transfer_abort(printer -> ux_host_class_printer_bulk_out_endpoint);
110 
111     /* If the printer is bidirectional, we need to abort transactions on the bulk in pipe.  */
112     if (printer -> ux_host_class_printer_interface -> ux_interface_descriptor.bInterfaceProtocol == UX_HOST_CLASS_PRINTER_PROTOCOL_BI_DIRECTIONAL)
113         _ux_host_stack_endpoint_transfer_abort(printer -> ux_host_class_printer_bulk_in_endpoint);
114 
115 #if !defined(UX_HOST_STANDALONE)
116 
117     /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
118        endpoints to exit properly.  */
119     _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
120 #else
121 
122     /* Free allocated memory.  */
123     if (printer -> ux_host_class_printer_allocated)
124         _ux_utility_memory_free(printer -> ux_host_class_printer_allocated);
125 #endif
126 
127     /* Destroy the instance.  */
128     _ux_host_stack_class_instance_destroy(printer -> ux_host_class_printer_class, (VOID *) printer);
129 
130 #if !defined(UX_HOST_STANDALONE)
131 
132     /* Destroy the semaphore.  */
133     _ux_host_semaphore_delete(&printer -> ux_host_class_printer_semaphore);
134 #endif
135 
136     /* Before we free the device resources, we need to inform the application
137         that the device is removed.  */
138     if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
139     {
140 
141         /* Inform the application the device is removed.  */
142         _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, printer -> ux_host_class_printer_class, (VOID *) printer);
143     }
144 
145     /* If trace is enabled, insert this event into the trace buffer.  */
146     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PRINTER_DEACTIVATE, printer, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
147 
148     /* If trace is enabled, register this object.  */
149     UX_TRACE_OBJECT_UNREGISTER(printer);
150 
151     /* Free the printer instance memory.  */
152     _ux_utility_memory_free(printer);
153 
154     /* Return successful status.  */
155     return(UX_SUCCESS);
156 }
157 
158