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 /**   Device Storage Class                                                */
19 /**                                                                       */
20 /**************************************************************************/
21 /**************************************************************************/
22 
23 #define UX_SOURCE_CODE
24 
25 
26 /* Include necessary system files.  */
27 
28 #include "ux_api.h"
29 #include "ux_device_class_storage.h"
30 #include "ux_device_stack.h"
31 
32 
33 #if UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE < UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH
34 #error UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE too small, please check
35 #endif
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _ux_device_class_storage_request_sense              PORTABLE C      */
42 /*                                                           6.1.10       */
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 /*                                                                        */
90 /**************************************************************************/
_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)91 UINT  _ux_device_class_storage_request_sense(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, UX_SLAVE_ENDPOINT *endpoint_in,
92                                             UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
93 {
94 
95 UINT                    status = UX_SUCCESS;
96 UX_SLAVE_TRANSFER       *transfer_request;
97 UCHAR                   *sense_buffer;
98 UCHAR                   key, code, qualifier;
99 ULONG                   sense_length;
100 
101 
102     UX_PARAMETER_NOT_USED(cbwcb);
103     UX_PARAMETER_NOT_USED(endpoint_out);
104 
105     /* Obtain the pointer to the transfer request.  */
106     transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
107 
108     /* Get length.  */
109     sense_length = storage -> ux_slave_class_storage_host_length;
110     if (sense_length > UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH)
111         sense_length = UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
112 
113     /* Obtain sense buffer.  */
114     sense_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
115 
116     /* Ensure it is cleaned.  */
117     _ux_utility_memory_set(sense_buffer, 0, sense_length); /* Use case of memset is verified. */
118 
119     /* Initialize the response buffer with the error code.  */
120     sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ERROR_CODE] =
121                     UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ERROR_CODE_VALUE;
122 
123     /* Extract sense key, code, qualifier.  */
124     key = UX_DEVICE_CLASS_STORAGE_SENSE_KEY(storage -> ux_slave_class_storage_lun[lun].
125                                             ux_slave_class_storage_request_sense_status);
126     code = UX_DEVICE_CLASS_STORAGE_SENSE_CODE(storage -> ux_slave_class_storage_lun[lun].
127                                             ux_slave_class_storage_request_sense_status);
128     qualifier = UX_DEVICE_CLASS_STORAGE_SENSE_QUALIFIER(storage -> ux_slave_class_storage_lun[lun].
129                                             ux_slave_class_storage_request_sense_status);
130 
131     /* Initialize the response buffer with the sense key.  */
132     sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY] = key;
133 
134     /* Initialize the response buffer with the code.  */
135     sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE] = code;
136 
137     /* Initialize the response buffer with the code qualifier.  */
138     sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER] = qualifier;
139 
140     /* If trace is enabled, insert this event into the trace buffer.  */
141     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_REQUEST_SENSE, storage, lun,
142                             key, code, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
143 
144     /* Initialize the response buffer with the additional length.  */
145     sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ADD_LENGTH] =  10;
146 
147 #if defined(UX_DEVICE_STANDALONE)
148 
149     /* Next: Transfer (DATA).  */
150     storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
151     storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
152 
153     storage -> ux_device_class_storage_transfer = transfer_request;
154     storage -> ux_device_class_storage_device_length = sense_length;
155     storage -> ux_device_class_storage_data_length = sense_length;
156     storage -> ux_device_class_storage_data_count = 0;
157 
158 #else
159 
160     /* Send a data payload with the sense codes.  */
161     if (sense_length)
162         _ux_device_stack_transfer_request(transfer_request, sense_length, sense_length);
163 
164     /* Check length.  */
165     if (storage -> ux_slave_class_storage_host_length != sense_length)
166     {
167         _ux_device_stack_endpoint_stall(endpoint_in);
168         storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
169     }
170 #endif
171 
172     /* Return completion status.  */
173     return(status);
174 }
175 
176