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_CLASS_STORAGE_BUFFER_SIZE < UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH
33 /* #error UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE 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_request_sense PORTABLE C */
42 /* 6.3.0 */
43 /* AUTHOR */
44 /* */
45 /* Chaoqiong Xiao, Microsoft Corporation */
46 /* */
47 /* DESCRIPTION */
48 /* */
49 /* This function performs a request sense 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_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 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
87 /* added standalone support, */
88 /* resulting in version 6.1.10 */
89 /* 10-31-2023 Chaoqiong Xiao Modified comment(s), */
90 /* checked compiling options */
91 /* by runtime UX_ASSERT, */
92 /* resulting in version 6.3.0 */
93 /* */
94 /**************************************************************************/
_ux_device_class_storage_request_sense(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)95 UINT _ux_device_class_storage_request_sense(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, UX_SLAVE_ENDPOINT *endpoint_in,
96 UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
97 {
98
99 UINT status = UX_SUCCESS;
100 UX_SLAVE_TRANSFER *transfer_request;
101 UCHAR *sense_buffer;
102 UCHAR key, code, qualifier;
103 ULONG sense_length;
104
105
106 UX_PARAMETER_NOT_USED(cbwcb);
107 UX_PARAMETER_NOT_USED(endpoint_out);
108
109 /* Build option check. */
110 UX_ASSERT(UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE >= UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
111
112 /* Obtain the pointer to the transfer request. */
113 transfer_request = &endpoint_in -> ux_slave_endpoint_transfer_request;
114
115 /* Get length. */
116 sense_length = storage -> ux_slave_class_storage_host_length;
117 if (sense_length > UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH)
118 sense_length = UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
119
120 /* Obtain sense buffer. */
121 sense_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
122
123 /* Ensure it is cleaned. */
124 _ux_utility_memory_set(sense_buffer, 0, sense_length); /* Use case of memset is verified. */
125
126 /* Initialize the response buffer with the error code. */
127 sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ERROR_CODE] =
128 UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ERROR_CODE_VALUE;
129
130 /* Extract sense key, code, qualifier. */
131 key = UX_DEVICE_CLASS_STORAGE_SENSE_KEY(storage -> ux_slave_class_storage_lun[lun].
132 ux_slave_class_storage_request_sense_status);
133 code = UX_DEVICE_CLASS_STORAGE_SENSE_CODE(storage -> ux_slave_class_storage_lun[lun].
134 ux_slave_class_storage_request_sense_status);
135 qualifier = UX_DEVICE_CLASS_STORAGE_SENSE_QUALIFIER(storage -> ux_slave_class_storage_lun[lun].
136 ux_slave_class_storage_request_sense_status);
137
138 /* Initialize the response buffer with the sense key. */
139 sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY] = key;
140
141 /* Initialize the response buffer with the code. */
142 sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE] = code;
143
144 /* Initialize the response buffer with the code qualifier. */
145 sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER] = qualifier;
146
147 /* If trace is enabled, insert this event into the trace buffer. */
148 UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_REQUEST_SENSE, storage, lun,
149 key, code, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
150
151 /* Initialize the response buffer with the additional length. */
152 sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ADD_LENGTH] = 10;
153
154 #if defined(UX_DEVICE_STANDALONE)
155
156 /* Next: Transfer (DATA). */
157 storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
158 storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
159
160 storage -> ux_device_class_storage_transfer = transfer_request;
161 storage -> ux_device_class_storage_device_length = sense_length;
162 storage -> ux_device_class_storage_data_length = sense_length;
163 storage -> ux_device_class_storage_data_count = 0;
164
165 #else
166
167 /* Send a data payload with the sense codes. */
168 if (sense_length)
169 _ux_device_stack_transfer_request(transfer_request, sense_length, sense_length);
170
171 /* Check length. */
172 if (storage -> ux_slave_class_storage_host_length != sense_length)
173 {
174 _ux_device_stack_endpoint_stall(endpoint_in);
175 storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
176 }
177 #endif
178
179 /* Return completion status. */
180 return(status);
181 }
182
183