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 /**   Device Storage Class                                                */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_device_class_storage.h"
30 #include "ux_device_stack.h"
31 
32 #if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < UX_SLAVE_CLASS_STORAGE_CSW_LENGTH
33 #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH too small, please check
34 #endif
35 
36 /**************************************************************************/
37 /*                                                                        */
38 /*  FUNCTION                                               RELEASE        */
39 /*                                                                        */
40 /*    _ux_device_class_storage_csw_send                   PORTABLE C      */
41 /*                                                           6.1.10       */
42 /*  AUTHOR                                                                */
43 /*                                                                        */
44 /*    Chaoqiong Xiao, Microsoft Corporation                               */
45 /*                                                                        */
46 /*  DESCRIPTION                                                           */
47 /*                                                                        */
48 /*    This function sends the status phase of SCSI transaction. Note that */
49 /*    dCSWDataResidue is always set to 0 because we either transfer all   */
50 /*    the data, or report command failure.                                */
51 /*                                                                        */
52 /*  INPUT                                                                 */
53 /*                                                                        */
54 /*    storage                               Pointer to storage class      */
55 /*    endpoint_in                           Pointer to IN endpoint        */
56 /*    status                                Status of CSW                 */
57 /*                                                                        */
58 /*  OUTPUT                                                                */
59 /*                                                                        */
60 /*    Completion Status                                                   */
61 /*                                                                        */
62 /*  CALLS                                                                 */
63 /*                                                                        */
64 /*    _ux_device_stack_transfer_request     Transfer request              */
65 /*    _ux_utility_long_put                  Put long word                 */
66 /*    _ux_utility_memory_copy               Copy memory                   */
67 /*    _ux_utility_memory_set                Set memory                    */
68 /*                                                                        */
69 /*  CALLED BY                                                             */
70 /*                                                                        */
71 /*    Device Storage Class                                                */
72 /*                                                                        */
73 /*  RELEASE HISTORY                                                       */
74 /*                                                                        */
75 /*    DATE              NAME                      DESCRIPTION             */
76 /*                                                                        */
77 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
78 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            optimized command logic,    */
80 /*                                            verified memset and memcpy  */
81 /*                                            cases,                      */
82 /*                                            resulting in version 6.1    */
83 /*  12-31-2020     Chaoqiong Xiao           Modified comment(s),          */
84 /*                                            fixed USB CV test issues,   */
85 /*                                            resulting in version 6.1.3  */
86 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
87 /*                                            improved TAG management,    */
88 /*                                            resulting in version 6.1.9  */
89 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
90 /*                                            added standalone support,   */
91 /*                                            resulting in version 6.1.10 */
92 /*                                                                        */
93 /**************************************************************************/
_ux_device_class_storage_csw_send(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UCHAR csw_status)94 UINT  _ux_device_class_storage_csw_send(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
95                                 UX_SLAVE_ENDPOINT *endpoint_in, UCHAR csw_status)
96 {
97 
98 UINT                    status = UX_SUCCESS;
99 UX_SLAVE_TRANSFER       *transfer_request;
100 UCHAR                   *csw_buffer;
101 
102 
103     UX_PARAMETER_NOT_USED(csw_status);
104     UX_PARAMETER_NOT_USED(lun);
105 
106 #if defined(UX_DEVICE_STANDALONE)
107 
108     /* Reset transfer request buffer pointers.  */
109     storage -> ux_device_class_storage_ep_out -> ux_slave_endpoint_transfer_request.
110             ux_slave_transfer_request_data_pointer = storage -> ux_device_class_storage_buffer[0];
111     storage -> ux_device_class_storage_ep_in -> ux_slave_endpoint_transfer_request.
112             ux_slave_transfer_request_data_pointer = storage -> ux_device_class_storage_buffer[1];
113 #endif
114 
115     /* If CSW skipped, just return.  */
116     if (UX_DEVICE_CLASS_STORAGE_CSW_SKIP(&storage -> ux_slave_class_storage_csw_status))
117         return(UX_SUCCESS);
118 
119     /* Obtain the pointer to the transfer request.  */
120     transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
121 
122     /* Get CSW buffer pointer.  */
123     csw_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
124 
125     /* Ensure it is cleaned.  */
126     _ux_utility_memory_set(csw_buffer, 0, UX_SLAVE_CLASS_STORAGE_CSW_LENGTH); /* Use case of memset is verified. */
127 
128     /* Store the signature of the CSW.  */
129     _ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_SIGNATURE], UX_SLAVE_CLASS_STORAGE_CSW_SIGNATURE_MASK);
130 
131     /* Store the SCSI tag from the CBW.  */
132     _ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_TAG], storage -> ux_slave_class_storage_scsi_tag);
133 
134     /* Store the dCSWDataResidue.  */
135     _ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_DATA_RESIDUE], storage -> ux_slave_class_storage_csw_residue);
136 
137     /* Store the status of the previous operation.  */
138     csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_STATUS] = (UCHAR)storage -> ux_slave_class_storage_csw_status;
139 
140 #if defined(UX_DEVICE_STANDALONE)
141 
142     /* Next: Transfer (CSW).  */
143     storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_CSW;
144     storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
145     storage -> ux_device_class_storage_transfer = transfer_request;
146 
147     storage -> ux_device_class_storage_device_length = UX_SLAVE_CLASS_STORAGE_CSW_LENGTH;
148     storage -> ux_device_class_storage_data_length = UX_SLAVE_CLASS_STORAGE_CSW_LENGTH;
149     storage -> ux_device_class_storage_data_count = 0;
150 
151 #else
152 
153     /* We may be in a special state machine condition where the endpoint is stalled waiting for
154        a CLEAR_FEATURE.  We will wait until the host clears the endpoint.
155        The transfer_request function does that.  */
156     /* Send the CSW back to the host.  */
157     status =  _ux_device_stack_transfer_request(transfer_request, UX_SLAVE_CLASS_STORAGE_CSW_LENGTH,
158                                     UX_SLAVE_CLASS_STORAGE_CSW_LENGTH);
159 #endif
160 
161     /* Return completion status.  */
162     return(status);
163 }
164 
165