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 /**   Prolific 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_prolific.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_prolific_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 prolific has been */
46 /*    removed from the bus either directly or indirectly. The bulk in\out */
47 /*    and optional interrupt pipes will be destroyed and the instance     */
48 /*    removed.                                                            */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    command                              Prolific class command pointer */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_host_stack_class_instance_destroy Destroy the class instance    */
61 /*    _ux_host_stack_endpoint_transfer_abort 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_prolific_entry         Entry of prolific 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 /*                                            refined macros names,       */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_prolific_deactivate(UX_HOST_CLASS_COMMAND * command)83 UINT  _ux_host_class_prolific_deactivate(UX_HOST_CLASS_COMMAND *command)
84 {
85 
86 UX_HOST_CLASS_PROLIFIC      *prolific;
87 UX_TRANSFER                 *transfer_request;
88 UINT                        status;
89 
90 
91     /* Get the instance for this class.  */
92     prolific =  (UX_HOST_CLASS_PROLIFIC *) command -> ux_host_class_command_instance;
93 
94     /* The prolific is being shut down.  */
95     prolific -> ux_host_class_prolific_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;
96 
97     /* Protect thread reentry to this instance.  */
98     status =  _ux_host_semaphore_get(&prolific -> ux_host_class_prolific_semaphore, UX_WAIT_FOREVER);
99     if (status != UX_SUCCESS)
100 
101         /* Return error.  */
102         return(status);
103 
104     /* If the interrupt endpoint is defined, clean any pending transfer.  */
105     if (prolific -> ux_host_class_prolific_interrupt_endpoint != UX_NULL)
106     {
107 
108         /* Wait for any current transfer to be out of pending.  */
109         transfer_request =  &prolific -> ux_host_class_prolific_interrupt_endpoint -> ux_endpoint_transfer_request;
110         if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
111 
112             /* And abort any transfer.  */
113             _ux_host_stack_endpoint_transfer_abort(prolific -> ux_host_class_prolific_interrupt_endpoint);
114 
115         /* And free the memory used by the interrupt endpoint.  */
116         _ux_utility_memory_free(transfer_request -> ux_transfer_request_data_pointer);
117 
118     }
119 
120 
121     /* First we take care of cleaning endpoint IN.  */
122     transfer_request =  &prolific -> ux_host_class_prolific_bulk_in_endpoint -> ux_endpoint_transfer_request;
123     if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
124 
125         /* We need to abort transactions on the bulk In pipe.  */
126         _ux_host_stack_endpoint_transfer_abort(prolific -> ux_host_class_prolific_bulk_in_endpoint);
127 
128 
129     /* Then endpoint OUT.  */
130     transfer_request =  &prolific -> ux_host_class_prolific_bulk_out_endpoint -> ux_endpoint_transfer_request;
131     if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
132 
133         /* We need to abort transactions on the bulk Out pipe. */
134         _ux_host_stack_endpoint_transfer_abort(prolific -> ux_host_class_prolific_bulk_out_endpoint);
135 
136 
137     /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
138        endpoints to exit properly.  */
139     _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
140 
141     /* Destroy the instance.  */
142     _ux_host_stack_class_instance_destroy(prolific -> ux_host_class_prolific_class, (VOID *) prolific);
143 
144     /* Destroy the semaphore.  */
145     _ux_host_semaphore_delete(&prolific -> ux_host_class_prolific_semaphore);
146 
147     /* Before we free the device resources, we need to inform the application
148         that the device is removed.  */
149     if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
150     {
151 
152         /* Inform the application the device is removed.  */
153         _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, prolific -> ux_host_class_prolific_class, (VOID *) prolific);
154     }
155 
156     /* If trace is enabled, insert this event into the trace buffer.  */
157     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PROLIFIC_DEACTIVATE, prolific, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
158 
159     /* If trace is enabled, register this object.  */
160     UX_TRACE_OBJECT_UNREGISTER(prolific);
161 
162     /* Free the prolific instance memory.  */
163     _ux_utility_memory_free(prolific);
164 
165     /* Return successful status.  */
166     return(UX_SUCCESS);
167 }
168 
169