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 /**   PIMA 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_pima.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_pima_object_close                    PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function closes an object,                                     */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    pima                                       Pointer to pima class    */
49 /*    pima_session                               Pointer to pima session  */
50 /*    object_handle                              The object handle        */
51 /*    object                                     Pointer to object info   */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_transfer_request            Transfer request         */
60 /*    _ux_host_stack_transfer_request_abort      Abort transfer           */
61 /*    _ux_host_stack_endpoint_reset              Reset endpoint           */
62 /*    _ux_host_semaphore_get                     Get semaphore            */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    USB application                                                     */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            prefixed UX to MS_TO_TICK,  */
75 /*                                            resulting in version 6.1    */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            refined macros names,       */
78 /*                                            resulting in version 6.1.10 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_pima_object_close(UX_HOST_CLASS_PIMA * pima,UX_HOST_CLASS_PIMA_SESSION * pima_session,ULONG object_handle,UX_HOST_CLASS_PIMA_OBJECT * object)81 UINT  _ux_host_class_pima_object_close(UX_HOST_CLASS_PIMA *pima,
82                                         UX_HOST_CLASS_PIMA_SESSION *pima_session,
83                                         ULONG object_handle, UX_HOST_CLASS_PIMA_OBJECT *object)
84 {
85 
86 UX_TRANSFER                         *transfer_request;
87 UCHAR                                 *ptp_payload;
88 ULONG                                requested_length;
89 UINT                                status;
90 
91     UX_PARAMETER_NOT_USED(object_handle);
92 
93     /* If trace is enabled, insert this event into the trace buffer.  */
94     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_OBJECT_CLOSE, pima, object, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
95 
96     /* Check if this session is valid or not.  */
97     if (pima_session -> ux_host_class_pima_session_magic != UX_HOST_CLASS_PIMA_MAGIC_NUMBER)
98         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
99 
100     /* Check if this session is opened or not.  */
101     if (pima_session -> ux_host_class_pima_session_state != UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED)
102         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
103 
104     /* Check if the object is already closed.  */
105     if (object -> ux_host_class_pima_object_state != UX_HOST_CLASS_PIMA_OBJECT_STATE_OPENED)
106         return (UX_HOST_CLASS_PIMA_RC_OBJECT_ALREADY_CLOSED );
107 
108     /* Close the object.  */
109     object -> ux_host_class_pima_object_state = UX_HOST_CLASS_PIMA_OBJECT_STATE_CLOSED;
110 
111     /* If the transfer was not ended prematurely, we need to do the status phase.  */
112     if ( object -> ux_host_class_pima_object_transfer_status == UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_COMPLETED)
113     {
114 
115         /* The transfer for this transaction is now inactive.  */
116         object -> ux_host_class_pima_object_transfer_status = UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_INACTIVE;
117 
118         /* Check if we had a ZLP condition during the data phase.  */
119         if (pima -> ux_host_class_pima_zlp_flag != UX_HOST_CLASS_PIMA_ZLP_NONE)
120         {
121 
122             /* We had a ZLP, so we now expect a zero length packet prior to the status phase.
123                We need to determine the direction.  */
124             if (pima -> ux_host_class_pima_zlp_flag == UX_HOST_CLASS_PIMA_ZLP_IN)
125 
126                 /* We use the Bulk In pipe for receiving the zlp.  */
127                 transfer_request =  &pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_transfer_request;
128 
129             else
130 
131                 /* We use the Bulk Out pipe for sending the zlp.  */
132                 transfer_request =  &pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_transfer_request;
133 
134             /* Initialize the payload to zero length. */
135             transfer_request -> ux_transfer_request_data_pointer =  UX_NULL;
136             transfer_request -> ux_transfer_request_requested_length =  0;
137 
138             /* Reset the ZLP now.  */
139             pima -> ux_host_class_pima_zlp_flag = UX_HOST_CLASS_PIMA_ZLP_NONE;
140 
141             /* Send request to HCD layer.  */
142             status =  _ux_host_stack_transfer_request(transfer_request);
143 
144             /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
145             if (status == UX_SUCCESS)
146             {
147 
148                 /* Wait for the completion of the transfer request.  */
149                 status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PIMA_CLASS_TRANSFER_TIMEOUT));
150 
151                 /* If the semaphore did not succeed we probably have a time out.  */
152                 if (status != UX_SUCCESS)
153                 {
154 
155                     /* All transfers pending need to abort. There may have been a partial transfer.  */
156                     _ux_host_stack_transfer_request_abort(transfer_request);
157 
158                     /* The endpoint was halted by a transfer error and needs to be reset.  */
159                     _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
160 
161                     /* The endpoint was halted by a transfer error  and needs to be reset.  */
162                     _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
163 
164                     /* There was an error, return to the caller.  */
165                     return(status);
166                 }
167             }
168         }
169 
170         /* We use the Bulk In pipe for receiving the response payload.  */
171         transfer_request =  &pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_transfer_request;
172 
173         /* Get the pointer to the ptp payload.  */
174         ptp_payload =  pima -> ux_host_class_pima_container ;
175 
176         /* Calculate the requested length for this payload.  */
177         requested_length =  UX_HOST_CLASS_PIMA_RESPONSE_HEADER_SIZE;
178 
179         /* Initialize the transfer_request.  */
180         transfer_request -> ux_transfer_request_data_pointer =  ptp_payload;
181         transfer_request -> ux_transfer_request_requested_length =  requested_length;
182 
183         /* Send request to HCD layer.  */
184         status =  _ux_host_stack_transfer_request(transfer_request);
185 
186         /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
187         if (status == UX_SUCCESS)
188         {
189 
190             /* Wait for the completion of the transfer request.  */
191             status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PIMA_CLASS_TRANSFER_TIMEOUT));
192 
193             /* If the semaphore did not succeed we probably have a time out.  */
194             if (status != UX_SUCCESS)
195             {
196 
197                 /* All transfers pending need to abort. There may have been a partial transfer.  */
198                 _ux_host_stack_transfer_request_abort(transfer_request);
199 
200                 /* The endpoint was halted by a transfer error and needs to be reset.  */
201                 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
202 
203                 /* The endpoint was halted by a transfer error  and needs to be reset.  */
204                 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
205 
206                 /* There was an error, return to the caller.  */
207                 return(status);
208             }
209         }
210         else
211         {
212 
213             /* There was a non transfer error, no partial transfer to be checked */
214             return(status);
215         }
216 
217     }
218 
219     /* The transfer for this transaction is now inactive.  */
220     object -> ux_host_class_pima_object_transfer_status = UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_INACTIVE;
221 
222     /* Return completion status.  */
223     return(UX_SUCCESS);
224 }
225 
226