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 /**   Storage 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_storage.h"
30 #include "ux_host_stack.h"
31 
32 
33 #if !defined(UX_HOST_STANDALONE)
34 /**************************************************************************/
35 /*                                                                        */
36 /*  FUNCTION                                               RELEASE        */
37 /*                                                                        */
38 /*    _ux_host_class_storage_transport_cb                 PORTABLE C      */
39 /*                                                           6.1.11       */
40 /*  AUTHOR                                                                */
41 /*                                                                        */
42 /*    Chaoqiong Xiao, Microsoft Corporation                               */
43 /*                                                                        */
44 /*  DESCRIPTION                                                           */
45 /*                                                                        */
46 /*    This function is the transport layer for the Control/Bulk           */
47 /*    transport. The command is sent on the control endpoint and the      */
48 /*    data payload on the bulk endpoint.                                  */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    storage                               Pointer to storage class      */
53 /*    data_pointer                          Pointer to data               */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    _ux_host_stack_transfer_request       Process host stack transfer   */
62 /*    _ux_host_stack_transfer_request_abort Abort transfer request        */
63 /*    _ux_utility_long_get                  Get 32-bit word               */
64 /*    _ux_host_semaphore_get                Get semaphore                 */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Storage Class                                                       */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            prefixed UX to MS_TO_TICK,  */
77 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            fixed CB request index,     */
80 /*                                            refined macros names,       */
81 /*                                            resulting in version 6.1.10 */
82 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            internal clean up,          */
84 /*                                            resulting in version 6.1.11 */
85 /*                                                                        */
86 /**************************************************************************/
_ux_host_class_storage_transport_cb(UX_HOST_CLASS_STORAGE * storage,UCHAR * data_pointer)87 UINT  _ux_host_class_storage_transport_cb(UX_HOST_CLASS_STORAGE *storage, UCHAR *data_pointer)
88 {
89 
90 UX_TRANSFER     *transfer_request;
91 UINT            status;
92 UCHAR           *ufi;
93 UCHAR           *cbw;
94 ULONG           data_phase_requested_length;
95 UX_ENDPOINT     *control_endpoint;
96 
97 
98     /* Reset the data phase memory size.  */
99     storage -> ux_host_class_storage_data_phase_length =  0;
100 
101     /* We need to get the default control endpoint transfer request pointer */
102     control_endpoint =  &storage -> ux_host_class_storage_device -> ux_device_control_endpoint;
103     transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
104 
105     /* Initialize the transfer request with the control SETUP values.  */
106     transfer_request -> ux_transfer_request_data_pointer =      UX_NULL;
107     transfer_request -> ux_transfer_request_requested_length =  0;
108     transfer_request -> ux_transfer_request_function =          0;
109     transfer_request -> ux_transfer_request_type =              UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
110     transfer_request -> ux_transfer_request_value =             0;
111     transfer_request -> ux_transfer_request_index =             storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceNumber;
112 
113     /* Use a pointer for the ufi portion of the command.  */
114     cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
115     ufi =  cbw + UX_HOST_CLASS_STORAGE_CBW_CB;
116 
117     /* Fill in the transfer request parameters.  */
118     transfer_request -> ux_transfer_request_data_pointer =      ufi;
119     transfer_request -> ux_transfer_request_requested_length =  (ULONG) * (cbw + UX_HOST_CLASS_STORAGE_CBW_CB_LENGTH);
120 
121     /* Send the ufi block on the control endpoint.  */
122     _ux_host_stack_transfer_request(transfer_request);
123 
124     /* Check the transfer status. If there is a transport error, the host must perform
125        a reset recovery.  */
126     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
127         return(transfer_request -> ux_transfer_request_completion_code);
128 
129     /* Get the length of the data payload.  */
130     data_phase_requested_length =  _ux_utility_long_get(cbw + UX_HOST_CLASS_STORAGE_CBW_DATA_LENGTH);
131 
132     /* Perform the data stage - if there is any.  */
133     if (data_phase_requested_length != 0)
134     {
135 
136         /* Check the direction and determine which endpoint to use.  */
137         if (*(cbw + UX_HOST_CLASS_STORAGE_CBW_FLAGS) == UX_HOST_CLASS_STORAGE_DATA_IN)
138             transfer_request =  &storage -> ux_host_class_storage_bulk_in_endpoint -> ux_endpoint_transfer_request;
139         else
140             transfer_request =  &storage -> ux_host_class_storage_bulk_out_endpoint -> ux_endpoint_transfer_request;
141 
142         /* Fill in the transfer request data payload buffer.  */
143         transfer_request -> ux_transfer_request_data_pointer =  data_pointer;
144 
145         /* Store the requested length in the transfer request.  */
146         transfer_request -> ux_transfer_request_requested_length =  data_phase_requested_length;
147 
148         /* Perform data payload transfer (in or out).  */
149         status =  _ux_host_stack_transfer_request(transfer_request);
150 
151         /* Check the status of the data payload.  */
152         if (status != UX_SUCCESS)
153             return(status);
154 
155         /* Wait for the completion of the transfer request.  */
156         status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT));
157 
158         /* Get the actual transfer length and update the cumulated stored value for upper layers.
159            This could be a non complete packet. But we don't test here because it only matters for
160            sector read.  */
161         storage -> ux_host_class_storage_data_phase_length += transfer_request -> ux_transfer_request_actual_length;
162 
163         /* If the semaphore did not succeed we probably have a timeout.  */
164         if (status != UX_SUCCESS)
165         {
166 
167             /* All transfers pending need to abort. There may have been a partial transfer.  */
168             _ux_host_stack_transfer_request_abort(transfer_request);
169 
170             /* Set the completion code.  */
171             transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
172 
173             /* Error trap. */
174             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_TIMEOUT);
175 
176             /* If trace is enabled, insert this event into the trace buffer.  */
177             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
178 
179             /* There was an error, return to the caller.  */
180             return(UX_TRANSFER_TIMEOUT);
181         }
182     }
183 
184     /* Return the status code.  */
185     return(transfer_request -> ux_transfer_request_completion_code);
186 }
187 #endif
188