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 /**   PIMA 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_pima.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_pima_deactivate                      PORTABLE C      */
37 /*                                                           6.3.0        */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function is called when this instance of the pima has been     */
45 /*    removed from the bus either directly or indirectly. The bulk in\out */
46 /*    and interrupt pipes will be destroyed and the instance removed.     */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    command                                PIMA   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 Abort endpoint transfer      */
60 /*    _ux_utility_memory_free               Free memory block             */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _ux_host_class_pima_entry                Entry of pima class        */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            refined macros names,       */
75 /*                                            resulting in version 6.1.10 */
76 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            improved INT EP support,    */
78 /*                                            removed unused semaphore,   */
79 /*                                            resulting in version 6.3.0  */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_pima_deactivate(UX_HOST_CLASS_COMMAND * command)82 UINT  _ux_host_class_pima_deactivate(UX_HOST_CLASS_COMMAND *command)
83 {
84 
85 UX_HOST_CLASS_PIMA              *pima;
86 UX_HOST_CLASS_PIMA_SESSION      *pima_session;
87 UX_TRANSFER                     *transfer_request;
88 
89 
90     /* Get the instance for this class.  */
91     pima =  (UX_HOST_CLASS_PIMA *) command -> ux_host_class_command_instance;
92 
93     /* The pima is being shut down.  */
94     pima -> ux_host_class_pima_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;
95 
96     /* We come to this point when the device has been extracted. So there may have been a transaction
97        being scheduled. We make sure the transaction has been completed by the controller driver.
98        When the device is extracted, the controller tries multiple times the transaction and retires it
99        with a DEVICE_NOT_RESPONDING error code.
100 
101        First we take care of endpoint IN.  */
102     transfer_request =  &pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_transfer_request;
103     if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
104 
105         /* We need to abort transactions on the bulk In pipe.  */
106         _ux_host_stack_endpoint_transfer_abort(pima -> ux_host_class_pima_bulk_in_endpoint);
107 
108 
109     /* Then endpoint OUT.  */
110     transfer_request =  &pima -> ux_host_class_pima_bulk_out_endpoint -> ux_endpoint_transfer_request;
111     if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
112 
113         /* We need to abort transactions on the bulk Out pipe.  We normally don't need that anymore. */
114         _ux_host_stack_endpoint_transfer_abort(pima -> ux_host_class_pima_bulk_out_endpoint);
115 
116 
117     /* Then interrupt endpoint.  */
118     if (pima -> ux_host_class_pima_interrupt_endpoint != UX_NULL)
119     {
120         transfer_request =  &pima -> ux_host_class_pima_interrupt_endpoint -> ux_endpoint_transfer_request;
121         if (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_PENDING)
122 
123             /* We need to abort transactions on the Interrupt pipe.  */
124             _ux_host_stack_endpoint_transfer_abort(pima -> ux_host_class_pima_interrupt_endpoint);
125 
126         /* Free the interrupt transfer buffer.  */
127         _ux_utility_memory_free(transfer_request -> ux_transfer_request_data_pointer);
128     }
129 
130     /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
131        endpoints to exit properly.  */
132     _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);
133 
134     /* Free the header container buffer.  */
135     if (pima -> ux_host_class_pima_container != UX_NULL)
136         _ux_utility_memory_free(pima -> ux_host_class_pima_container);
137 
138     /* Free the event data buffer.  */
139     if (pima -> ux_host_class_pima_event_buffer != UX_NULL)
140         _ux_utility_memory_free(pima -> ux_host_class_pima_event_buffer);
141 
142     /* Get the pointer to the PIMA session.  */
143     pima_session = pima -> ux_host_class_pima_session;
144 
145     /* Was there a PIMA session ?  */
146     if(pima_session != UX_NULL)
147     {
148         /* Clean the PIMA session and free the storage ID buffer in the session if it is opened.  */
149         if ((pima_session -> ux_host_class_pima_session_magic == UX_HOST_CLASS_PIMA_MAGIC_NUMBER) &&
150             (pima_session -> ux_host_class_pima_session_state == UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED))
151 
152         {
153 
154             /* Reset the magic field.  */
155             pima_session -> ux_host_class_pima_session_magic =  0;
156 
157             /* Declare the session closed.  */
158             pima_session -> ux_host_class_pima_session_state = UX_HOST_CLASS_PIMA_SESSION_STATE_CLOSED;
159 
160         }
161     }
162 
163     /* Destroy the instance.  */
164     _ux_host_stack_class_instance_destroy(pima -> ux_host_class_pima_class, (VOID *) pima);
165 
166     /* Before we free the device resources, we need to inform the application
167         that the device is removed.  */
168     if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
169     {
170 
171         /* Inform the application the device is removed.  */
172         _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, pima -> ux_host_class_pima_class, (VOID *) pima);
173     }
174 
175     /* If trace is enabled, insert this event into the trace buffer.  */
176     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_DEACTIVATE, pima, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
177 
178     /* If trace is enabled, register this object.  */
179     UX_TRACE_OBJECT_UNREGISTER(pima);
180 
181     /* Free the pima instance memory.  */
182     _ux_utility_memory_free(pima);
183 
184     /* Return successful status.  */
185     return(UX_SUCCESS);
186 }
187 
188