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