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 /** Storage Class */
18 /** */
19 /**************************************************************************/
20 /**************************************************************************/
21
22
23 /* Include necessary system files. */
24
25 #define UX_SOURCE_CODE
26
27 #include "ux_api.h"
28 #include "ux_host_class_storage.h"
29 #include "ux_host_stack.h"
30
31
32 /**************************************************************************/
33 /* */
34 /* FUNCTION RELEASE */
35 /* */
36 /* _ux_host_class_storage_request_sense PORTABLE C */
37 /* 6.1.10 */
38 /* AUTHOR */
39 /* */
40 /* Chaoqiong Xiao, Microsoft Corporation */
41 /* */
42 /* DESCRIPTION */
43 /* */
44 /* This function will send a request sense to the device to see what */
45 /* error happened during the last command. Request sense commands */
46 /* cannot be nested. */
47 /* */
48 /* INPUT */
49 /* */
50 /* storage Pointer to storage class */
51 /* */
52 /* OUTPUT */
53 /* */
54 /* Completion Status */
55 /* */
56 /* CALLS */
57 /* */
58 /* _ux_host_class_storage_cbw_initialize Initialize CBW */
59 /* _ux_host_class_storage_transport Send command */
60 /* _ux_utility_memory_allocate Allocate memory block */
61 /* _ux_utility_memory_copy Copy memory block */
62 /* _ux_utility_memory_free Release memory block */
63 /* */
64 /* CALLED BY */
65 /* */
66 /* Storage Class */
67 /* */
68 /* RELEASE HISTORY */
69 /* */
70 /* DATE NAME DESCRIPTION */
71 /* */
72 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
73 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
74 /* verified memset and memcpy */
75 /* cases, */
76 /* resulting in version 6.1 */
77 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
78 /* added standalone support, */
79 /* resulting in version 6.1.10 */
80 /* */
81 /**************************************************************************/
_ux_host_class_storage_request_sense(UX_HOST_CLASS_STORAGE * storage)82 UINT _ux_host_class_storage_request_sense(UX_HOST_CLASS_STORAGE *storage)
83 {
84
85 UCHAR *cbw;
86 UCHAR *request_sense_response;
87 UINT command_length;
88 #if !defined(UX_HOST_STANDALONE)
89 UINT status;
90 ULONG sense_code;
91 #endif
92
93 /* If trace is enabled, insert this event into the trace buffer. */
94 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_REQUEST_SENSE, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
95
96 /* Clear the former sense code value. */
97 storage -> ux_host_class_storage_sense_code = 0;
98
99 /* Use a pointer for the cbw, easier to manipulate. */
100 cbw = (UCHAR *) storage -> ux_host_class_storage_cbw;
101
102 /* Get the Request Sense Command Length. */
103 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
104 if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
105 command_length = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_UFI;
106 else
107 command_length = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
108 #else
109 command_length = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
110 #endif
111
112 /* Check of we are reentering a REQUEST SENSE command which is illegal. */
113 if (*(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) == UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE)
114 return(UX_ERROR);
115
116 /* Save the current command so that we can re-initiate it after the
117 REQUEST_SENSE command. */
118 _ux_utility_memory_copy(storage -> ux_host_class_storage_saved_cbw, storage -> ux_host_class_storage_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH); /* Use case of memcpy is verified. */
119
120 /* Initialize the CBW for this command. */
121 _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, command_length);
122
123 /* Prepare the REQUEST SENSE command block. */
124 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) = UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE;
125
126 /* Store the length of the Request Sense Response. */
127 *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_ALLOCATION_LENGTH) = UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
128
129 /* Obtain a block of memory for the answer. */
130 request_sense_response = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
131 if (request_sense_response == UX_NULL)
132 return(UX_MEMORY_INSUFFICIENT);
133
134 #if defined(UX_HOST_STANDALONE)
135
136 /* Prepare data buffer for request sense. */
137 storage -> ux_host_class_storage_trans_data_bak = storage -> ux_host_class_storage_trans_data;
138 storage -> ux_host_class_storage_trans_data = request_sense_response;
139
140 /* Perform CBW-DATA-CSW transport. */
141 storage -> ux_host_class_storage_trans_state = UX_HOST_CLASS_STORAGE_TRANS_CBW;
142
143 return(UX_SUCCESS);
144 #else
145
146 /* Send the command to transport layer. */
147 status = _ux_host_class_storage_transport(storage, request_sense_response);
148
149 /* If we have a transport error, there is not much we can do, simply return the error. */
150 if (status == UX_SUCCESS)
151 {
152
153 /* We have a successful transaction, even though the sense code could reflect an error. The sense code
154 will be assembled and store in the device instance. */
155 sense_code = UX_HOST_CLASS_STORAGE_SENSE_STATUS(
156 (ULONG) *(request_sense_response +
157 UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY),
158 (ULONG) *(request_sense_response +
159 UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE),
160 (ULONG) *(request_sense_response +
161 UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER));
162
163 /* Store the sense code in the storage instance. */
164 storage -> ux_host_class_storage_sense_code = sense_code;
165 }
166
167 /* Free the memory resource used for the command response. */
168 _ux_utility_memory_free(request_sense_response);
169
170 /* Restore the current CBW command. */
171 _ux_utility_memory_copy(storage -> ux_host_class_storage_cbw, storage -> ux_host_class_storage_saved_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH); /* Use case of memcpy is verified. */
172
173 /* Return completion code. */
174 return(status);
175 #endif
176 }
177
178