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 |
|
|
/** Device Storage Class */ |
18 |
|
|
/** */ |
19 |
|
|
/**************************************************************************/ |
20 |
|
|
/**************************************************************************/ |
21 |
|
|
|
22 |
|
|
#define UX_SOURCE_CODE |
23 |
|
|
|
24 |
|
|
|
25 |
|
|
/* Include necessary system files. */ |
26 |
|
|
|
27 |
|
|
#include "ux_api.h" |
28 |
|
|
#include "ux_device_class_storage.h" |
29 |
|
|
#include "ux_device_stack.h" |
30 |
|
|
|
31 |
|
|
#if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < UX_SLAVE_CLASS_STORAGE_CSW_LENGTH |
32 |
|
|
/* #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH too small, please check */ |
33 |
|
|
/* Build option checked runtime by UX_ASSERT */ |
34 |
|
|
#endif |
35 |
|
|
|
36 |
|
|
/**************************************************************************/ |
37 |
|
|
/* */ |
38 |
|
|
/* FUNCTION RELEASE */ |
39 |
|
|
/* */ |
40 |
|
|
/* _ux_device_class_storage_csw_send PORTABLE C */ |
41 |
|
|
/* 6.3.0 */ |
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 |
|
|
/* 10-31-2023 Chaoqiong Xiao Modified comment(s), */ |
93 |
|
|
/* checked compiling options */ |
94 |
|
|
/* by runtime UX_ASSERT, */ |
95 |
|
|
/* resulting in version 6.3.0 */ |
96 |
|
|
/* */ |
97 |
|
|
/**************************************************************************/ |
98 |
|
2286 |
UINT _ux_device_class_storage_csw_send(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, |
99 |
|
|
UX_SLAVE_ENDPOINT *endpoint_in, UCHAR csw_status) |
100 |
|
|
{ |
101 |
|
|
|
102 |
|
2286 |
UINT status = UX_SUCCESS; |
103 |
|
|
UX_SLAVE_TRANSFER *transfer_request; |
104 |
|
|
UCHAR *csw_buffer; |
105 |
|
|
|
106 |
|
|
|
107 |
|
|
UX_PARAMETER_NOT_USED(csw_status); |
108 |
|
|
UX_PARAMETER_NOT_USED(lun); |
109 |
|
|
|
110 |
|
|
/* Build option check. */ |
111 |
|
|
UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= UX_SLAVE_CLASS_STORAGE_CSW_LENGTH); |
112 |
|
|
|
113 |
|
|
#if defined(UX_DEVICE_STANDALONE) |
114 |
|
|
|
115 |
|
|
/* Reset transfer request buffer pointers. */ |
116 |
|
|
storage -> ux_device_class_storage_ep_out -> ux_slave_endpoint_transfer_request. |
117 |
|
|
ux_slave_transfer_request_data_pointer = storage -> ux_device_class_storage_buffer[0]; |
118 |
|
|
storage -> ux_device_class_storage_ep_in -> ux_slave_endpoint_transfer_request. |
119 |
|
|
ux_slave_transfer_request_data_pointer = storage -> ux_device_class_storage_buffer[1]; |
120 |
|
|
#endif |
121 |
|
|
|
122 |
|
|
/* If CSW skipped, just return. */ |
123 |
✓✓ |
2286 |
if (UX_DEVICE_CLASS_STORAGE_CSW_SKIP(&storage -> ux_slave_class_storage_csw_status)) |
124 |
|
1 |
return(UX_SUCCESS); |
125 |
|
|
|
126 |
|
|
/* Obtain the pointer to the transfer request. */ |
127 |
|
2285 |
transfer_request = &endpoint_in -> ux_slave_endpoint_transfer_request; |
128 |
|
|
|
129 |
|
|
/* Get CSW buffer pointer. */ |
130 |
|
2285 |
csw_buffer = transfer_request -> ux_slave_transfer_request_data_pointer; |
131 |
|
|
|
132 |
|
|
/* Ensure it is cleaned. */ |
133 |
|
2285 |
_ux_utility_memory_set(csw_buffer, 0, UX_SLAVE_CLASS_STORAGE_CSW_LENGTH); /* Use case of memset is verified. */ |
134 |
|
|
|
135 |
|
|
/* Store the signature of the CSW. */ |
136 |
|
2285 |
_ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_SIGNATURE], UX_SLAVE_CLASS_STORAGE_CSW_SIGNATURE_MASK); |
137 |
|
|
|
138 |
|
|
/* Store the SCSI tag from the CBW. */ |
139 |
|
2285 |
_ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_TAG], storage -> ux_slave_class_storage_scsi_tag); |
140 |
|
|
|
141 |
|
|
/* Store the dCSWDataResidue. */ |
142 |
|
2285 |
_ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_DATA_RESIDUE], storage -> ux_slave_class_storage_csw_residue); |
143 |
|
|
|
144 |
|
|
/* Store the status of the previous operation. */ |
145 |
|
2285 |
csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_STATUS] = (UCHAR)storage -> ux_slave_class_storage_csw_status; |
146 |
|
|
|
147 |
|
|
#if defined(UX_DEVICE_STANDALONE) |
148 |
|
|
|
149 |
|
|
/* Next: Transfer (CSW). */ |
150 |
|
|
storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_CSW; |
151 |
|
|
storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START; |
152 |
|
|
storage -> ux_device_class_storage_transfer = transfer_request; |
153 |
|
|
|
154 |
|
|
storage -> ux_device_class_storage_device_length = UX_SLAVE_CLASS_STORAGE_CSW_LENGTH; |
155 |
|
|
storage -> ux_device_class_storage_data_length = UX_SLAVE_CLASS_STORAGE_CSW_LENGTH; |
156 |
|
|
storage -> ux_device_class_storage_data_count = 0; |
157 |
|
|
|
158 |
|
|
#else |
159 |
|
|
|
160 |
|
|
/* We may be in a special state machine condition where the endpoint is stalled waiting for |
161 |
|
|
a CLEAR_FEATURE. We will wait until the host clears the endpoint. |
162 |
|
|
The transfer_request function does that. */ |
163 |
|
|
/* Send the CSW back to the host. */ |
164 |
|
2285 |
status = _ux_device_stack_transfer_request(transfer_request, UX_SLAVE_CLASS_STORAGE_CSW_LENGTH, |
165 |
|
|
UX_SLAVE_CLASS_STORAGE_CSW_LENGTH); |
166 |
|
|
#endif |
167 |
|
|
|
168 |
|
|
/* Return completion status. */ |
169 |
|
2282 |
return(status); |
170 |
|
|
} |
171 |
|
|
|