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
32 #if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < 24
33 /* #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH is too small, please check */
34 /* Build option checked runtime by UX_ASSERT */
35 #endif
36
37 /**************************************************************************/
38 /* */
39 /* FUNCTION RELEASE */
40 /* */
41 /* _ux_device_class_storage_inquiry PORTABLE C */
42 /* 6.3.0 */
43 /* AUTHOR */
44 /* */
45 /* Chaoqiong Xiao, Microsoft Corporation */
46 /* */
47 /* DESCRIPTION */
48 /* */
49 /* This function performs a INQUIRY command. */
50 /* */
51 /* INPUT */
52 /* */
53 /* storage Pointer to storage class */
54 /* endpoint_in Pointer to IN endpoint */
55 /* endpoint_out Pointer to OUT endpoint */
56 /* cbwcb Pointer to CBWCB */
57 /* */
58 /* OUTPUT */
59 /* */
60 /* Completion Status */
61 /* */
62 /* CALLS */
63 /* */
64 /* _ux_device_class_storage_csw_send Send CSW */
65 /* _ux_device_stack_transfer_request Transfer request */
66 /* _ux_device_stack_endpoint_stall Stall endpoint */
67 /* _ux_utility_memory_copy Copy memory */
68 /* _ux_utility_memory_set Set memory */
69 /* _ux_utility_short_put_big_endian Put 16-bit big endian */
70 /* */
71 /* CALLED BY */
72 /* */
73 /* Device Storage Class */
74 /* */
75 /* RELEASE HISTORY */
76 /* */
77 /* DATE NAME DESCRIPTION */
78 /* */
79 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
80 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
81 /* optimized command logic, */
82 /* verified memset and memcpy */
83 /* cases, */
84 /* resulting in version 6.1 */
85 /* 12-31-2020 Chaoqiong Xiao Modified comment(s), */
86 /* fixed USB CV test issues, */
87 /* resulting in version 6.1.3 */
88 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
89 /* added standalone support, */
90 /* resulting in version 6.1.10 */
91 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
92 /* updated dCSWDataResidue, */
93 /* resulting in version 6.1.12 */
94 /* 10-31-2023 Chaoqiong Xiao Modified comment(s), */
95 /* checked compiling options */
96 /* by runtime UX_ASSERT, */
97 /* resulting in version 6.3.0 */
98 /* */
99 /**************************************************************************/
_ux_device_class_storage_inquiry(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)100 UINT _ux_device_class_storage_inquiry(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, UX_SLAVE_ENDPOINT *endpoint_in,
101 UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
102 {
103
104 UINT status = UX_SUCCESS;
105 UX_SLAVE_TRANSFER *transfer_request;
106 UCHAR inquiry_page_code;
107 ULONG inquiry_length;
108 UCHAR *inquiry_buffer;
109
110 UX_PARAMETER_NOT_USED(endpoint_out);
111
112 /* Build option check. */
113 UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= 24);
114
115 /* If trace is enabled, insert this event into the trace buffer. */
116 UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_INQUIRY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
117
118 #if !defined(UX_DEVICE_STANDALONE)
119
120 /* Check direction. */
121 if (storage -> ux_slave_class_storage_host_length &&
122 (storage -> ux_slave_class_storage_cbw_flags & 0x80) == 0)
123 {
124 _ux_device_stack_endpoint_stall(endpoint_out);
125 storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
126 return(UX_ERROR);
127 }
128 #endif
129
130 /* From the SCSI Inquiry payload, get the page code. */
131 inquiry_page_code = *(cbwcb + UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE);
132
133 /* And the length to be returned. */
134 inquiry_length = storage -> ux_slave_class_storage_host_length;
135
136 /* Obtain the pointer to the transfer request. */
137 transfer_request = &endpoint_in -> ux_slave_endpoint_transfer_request;
138
139 /* Obtain inquiry buffer pointer. */
140 inquiry_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
141
142 /* Ensure the data buffer is cleaned. */
143 _ux_utility_memory_set(inquiry_buffer, 0, UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH); /* Use case of memset is verified. */
144
145 /* Check for the maximum length to be returned. */
146 if (inquiry_length > UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH)
147 inquiry_length = UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH;
148
149 /* Default CSW to passed. */
150 storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
151
152 /* Ensure we know about the page code. */
153 switch (inquiry_page_code)
154 {
155
156 case UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE_STANDARD:
157
158 /* Store the product type. */
159 inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PERIPHERAL_TYPE] = (UCHAR)storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_type;
160
161 /* Store the Media Removable bit. */
162 inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_REMOVABLE_MEDIA] = (UCHAR)storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_removable_flag;
163
164 /* Store the Data Format bit. */
165 if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_type == UX_SLAVE_CLASS_STORAGE_MEDIA_CDROM)
166 inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_DATA_FORMAT] = 0x32;
167 else
168 inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_DATA_FORMAT] = 0x00;
169
170 /* Store the length of the response. There is a hack here. For CD-ROM, the data lg is fixed to 0x5B ! */
171 if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_type != UX_SLAVE_CLASS_STORAGE_MEDIA_CDROM)
172 inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_ADDITIONAL_LENGTH] = UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH;
173 else
174 inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_ADDITIONAL_LENGTH] = UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH_CD_ROM;
175
176 /* Fill in the storage vendor ID. */
177 _ux_utility_memory_copy(inquiry_buffer + UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_VENDOR_INFORMATION,
178 storage -> ux_slave_class_storage_vendor_id, 8); /* Use case of memcpy is verified. */
179
180 /* Fill in the product vendor ID. */
181 _ux_utility_memory_copy(inquiry_buffer + UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PRODUCT_ID,
182 storage -> ux_slave_class_storage_product_id, 16); /* Use case of memcpy is verified. */
183
184 /* Fill in the product revision number. */
185 _ux_utility_memory_copy(inquiry_buffer + UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PRODUCT_REVISION,
186 storage -> ux_slave_class_storage_product_rev, 4); /* Use case of memcpy is verified. */
187
188 break;
189
190 case UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE_SERIAL:
191
192 /* Initialize the page code in response buffer. */
193 _ux_utility_short_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer, UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE_SERIAL);
194
195 /* Initialize the length of the serial number in response buffer. */
196 _ux_utility_short_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer + 2, 20);
197
198 /* Copy the serial number buffer into the transfer request memory. */
199 _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer + 4, storage -> ux_slave_class_storage_product_serial, 20); /* Use case of memcpy is verified. */
200
201 /* Send a data payload with the inquiry response buffer. */
202 if (inquiry_length > 24)
203 inquiry_length = 24;
204
205 break;
206
207 default:
208
209 #if !defined(UX_DEVICE_STANDALONE)
210 /* The page code is not supported. */
211 _ux_device_stack_endpoint_stall(endpoint_in);
212 #endif
213
214 /* And update the REQUEST_SENSE codes. */
215 storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
216 UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(0x05,0x26,0x01);
217
218 /* Now we set the CSW with failure. */
219 storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
220
221 /* Return error. */
222 status = UX_ERROR;
223
224 break;
225 }
226
227 /* Error cases. */
228 if (status != UX_SUCCESS)
229 return(status);
230
231 #if defined(UX_DEVICE_STANDALONE)
232
233 /* Next: Transfer (DATA). */
234 storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
235 storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
236
237 storage -> ux_device_class_storage_transfer = transfer_request;
238 storage -> ux_device_class_storage_device_length = inquiry_length;
239 storage -> ux_device_class_storage_data_length = inquiry_length;
240 storage -> ux_device_class_storage_data_count = 0;
241
242 #else
243
244 /* Send a data payload with the inquiry response buffer. */
245 if (inquiry_length)
246 _ux_device_stack_transfer_request(transfer_request, inquiry_length, inquiry_length);
247
248 /* Check length. */
249 if (storage -> ux_slave_class_storage_host_length != inquiry_length)
250 {
251 storage -> ux_slave_class_storage_csw_residue = storage -> ux_slave_class_storage_host_length - inquiry_length;
252 _ux_device_stack_endpoint_stall(endpoint_in);
253 }
254 #endif
255
256 /* Return completion status. */
257 return(status);
258 }
259
260