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_request_cancel                  PORTABLE C      */
37 /*                                                           6.1          */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function cancels a request.                                    */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    pima                                       Pointer to pima class    */
48 /*                                                                        */
49 /*  OUTPUT                                                                */
50 /*                                                                        */
51 /*    Completion Status                                                   */
52 /*                                                                        */
53 /*  CALLS                                                                 */
54 /*                                                                        */
55 /*    _ux_host_stack_transfer_request       Transfer request              */
56 /*    _ux_utility_memory_allocate           Allocate memory               */
57 /*    _ux_utility_memory_free               Free memory                   */
58 /*    _ux_utility_short_put                 Put 16-bit value              */
59 /*    _ux_utility_short_get                 Get 16-bit value              */
60 /*    _ux_utility_long_put                  Put 32-bit value              */
61 /*    _ux_utility_delay_ms                  Delay ms                      */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    USB application                                                     */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*                                                                        */
75 /**************************************************************************/
_ux_host_class_pima_request_cancel(UX_HOST_CLASS_PIMA * pima)76 UINT  _ux_host_class_pima_request_cancel(UX_HOST_CLASS_PIMA *pima)
77 {
78 
79 UX_TRANSFER                         *transfer_request;
80 UX_ENDPOINT                         *control_endpoint;
81 UCHAR                                 *request_payload;
82 UINT                                status;
83 UINT                                device_status;
84 ULONG                               payload_length;
85 ULONG                               command_retry_counter;
86 
87     /* If trace is enabled, insert this event into the trace buffer.  */
88     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_REQUEST_CANCEL, pima, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
89 
90     /* Allocate some DMA safe memory for the command data payload.  We use mode than needed because
91        we use this buffer for both the command and the status phase.  */
92     request_payload =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_REQUEST_STATUS_DATA_LENGTH);
93     if (request_payload == UX_NULL)
94         return(UX_MEMORY_INSUFFICIENT);
95 
96     /* Fill in the payload buffer with cancellation code.  */
97     _ux_utility_short_put(request_payload + UX_HOST_CLASS_PIMA_REQUEST_CANCEL_OFFSET_CODE,
98                         UX_HOST_CLASS_PIMA_REQUEST_CANCEL_CODE );
99 
100     /* Fill in the payload buffer with transactionID.  */
101     _ux_utility_long_put(request_payload + UX_HOST_CLASS_PIMA_REQUEST_CANCEL_OFFSET_TRANSACTION_ID,
102                         pima -> ux_host_class_pima_transaction_id++);
103 
104     /* We need to get the default control endpoint transfer request pointer.  */
105     control_endpoint =  &pima -> ux_host_class_pima_device -> ux_device_control_endpoint;
106     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
107 
108     /* Create a transfer_request for the CANCEL request.  */
109     transfer_request -> ux_transfer_request_data_pointer =      request_payload;
110     transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_PIMA_REQUEST_CANCEL_DATA_LENGTH;
111     transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_PIMA_REQUEST_CANCEL_COMMAND;
112     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
113     transfer_request -> ux_transfer_request_value =             0;
114     transfer_request -> ux_transfer_request_index  =            0;
115 
116     /* Send request to HCD layer.  */
117     status =  _ux_host_stack_transfer_request(transfer_request);
118 
119     /* Check if status is OK. If the command did not work, we have a major problem.  */
120     if(status == UX_SUCCESS)
121     {
122 
123         /* Initialize the command retry counter.  */
124         command_retry_counter = UX_HOST_CLASS_PIMA_REQUEST_STATUS_COMMAND_COUNTER;
125 
126         /* This command may be retried a few times.  */
127         while (command_retry_counter-- != 0)
128         {
129 
130             /* We need to wait for the device to be OK. For that we issue a GET_DEVICE_STATUS command.  */
131             transfer_request -> ux_transfer_request_data_pointer =      request_payload;
132             transfer_request -> ux_transfer_request_requested_length =  UX_HOST_CLASS_PIMA_REQUEST_CANCEL_DATA_LENGTH;
133             transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_PIMA_REQUEST_STATUS_COMMAND;
134             transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
135             transfer_request -> ux_transfer_request_value =             0;
136             transfer_request -> ux_transfer_request_index  =            0;
137 
138             /* Send request to HCD layer.  */
139             status =  _ux_host_stack_transfer_request(transfer_request);
140 
141             /* Check status, abort if there is an error.  */
142             if (status != UX_SUCCESS)
143                 break;
144 
145             /* Extract the payload length from the status buffer.  */
146             payload_length =  _ux_utility_short_get(request_payload + UX_HOST_CLASS_PIMA_REQUEST_STATUS_OFFSET_LENGTH);
147 
148             /* Check the data payload length.  */
149             if (payload_length < UX_HOST_CLASS_PIMA_REQUEST_STATUS_OFFSET_CODE + sizeof(UINT))
150             {
151 
152                 /* We have a data format error.  */
153                 status =  UX_ERROR;
154                 break;
155             }
156 
157             /* Extract the device status.  */
158             device_status =  _ux_utility_short_get(request_payload + UX_HOST_CLASS_PIMA_REQUEST_STATUS_OFFSET_CODE);
159 
160             /* If the device status is OK, we have a successful cancellation.  */
161             if (device_status == UX_HOST_CLASS_PIMA_RC_OK)
162             {
163 
164                 /* Status is OK. exit the command loop.  */
165                 status = UX_SUCCESS;
166                 break;
167             }
168             else
169             {
170                 /* Force the status to error.  */
171                 status = UX_ERROR;
172 
173 
174                 /* We should wait a little bit before re-issuing the command.  */
175                 _ux_utility_delay_ms(UX_HOST_CLASS_PIMA_REQUEST_STATUS_COMMAND_DELAY);
176 
177             }
178         }
179 
180     }
181 
182     /* Free allocated resources.  */
183     _ux_utility_memory_free(request_payload);
184 
185     /* Return completion status.  */
186     return(status);
187 
188 }
189 
190