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 #define USBX_DEVICE_CLASS_STORAGE_GET_PERFORMANCE_0_LENGTH                    16
32 #if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < (USBX_DEVICE_CLASS_STORAGE_GET_PERFORMANCE_0_LENGTH + 8)
33 /* #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH too small, please check  */
34 /* Build option checked runtime by UX_ASSERT  */
35 #endif
36 UCHAR usbx_device_class_storage_performance[] = {
37 
38     0x08, 0x00, 0x00, 0x00, 0x00, 0x23, 0x12, 0x80,
39     0x00, 0x00, 0x10, 0x89, 0x00, 0x00, 0x10, 0x89
40 };
41 
42 /**************************************************************************/
43 /*                                                                        */
44 /*  FUNCTION                                               RELEASE        */
45 /*                                                                        */
46 /*    _ux_device_class_storage_get_performance            PORTABLE C      */
47 /*                                                           6.3.0        */
48 /*  AUTHOR                                                                */
49 /*                                                                        */
50 /*    Chaoqiong Xiao, Microsoft Corporation                               */
51 /*                                                                        */
52 /*  DESCRIPTION                                                           */
53 /*                                                                        */
54 /*    This function performs a GET_PERFORMANCE SCSI command.              */
55 /*                                                                        */
56 /*  INPUT                                                                 */
57 /*                                                                        */
58 /*    storage                               Pointer to storage class      */
59 /*    endpoint_in                           Pointer to IN endpoint        */
60 /*    endpoint_out                          Pointer to OUT endpoint       */
61 /*    cbwcb                                 Pointer to CBWCB              */
62 /*                                                                        */
63 /*  OUTPUT                                                                */
64 /*                                                                        */
65 /*    Completion Status                                                   */
66 /*                                                                        */
67 /*  CALLS                                                                 */
68 /*                                                                        */
69 /*    _ux_device_stack_transfer_request     Transfer request              */
70 /*    _ux_device_class_storage_csw_send     Send CSW                      */
71 /*    _ux_utility_memory_set                Set memory                    */
72 /*    _ux_utility_memory_copy               Copy memory                   */
73 /*    _ux_utility_long_put_big_endian       Put 32-bit big endian         */
74 /*                                                                        */
75 /*  CALLED BY                                                             */
76 /*                                                                        */
77 /*    Device Storage Class                                                */
78 /*                                                                        */
79 /*  RELEASE HISTORY                                                       */
80 /*                                                                        */
81 /*    DATE              NAME                      DESCRIPTION             */
82 /*                                                                        */
83 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
84 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
85 /*                                            optimized command logic,    */
86 /*                                            verified memset and memcpy  */
87 /*                                            cases,                      */
88 /*                                            resulting in version 6.1    */
89 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
90 /*                                            added standalone support,   */
91 /*                                            resulting in version 6.1.10 */
92 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
93 /*                                            checked compiling options   */
94 /*                                            by runtime UX_ASSERT,       */
95 /*                                            resulting in version 6.3.0  */
96 /*                                                                        */
97 /**************************************************************************/
_ux_device_class_storage_get_performance(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)98 UINT  _ux_device_class_storage_get_performance(UX_SLAVE_CLASS_STORAGE *storage,
99                                             ULONG               lun,
100                                             UX_SLAVE_ENDPOINT   *endpoint_in,
101                                             UX_SLAVE_ENDPOINT   *endpoint_out,
102                                             UCHAR               *cbwcb)
103 {
104 
105 UINT                    status;
106 UX_SLAVE_TRANSFER       *transfer_request;
107 ULONG                   performance_page;
108 ULONG                   data_length = 0;
109 
110 
111     UX_PARAMETER_NOT_USED(lun);
112     UX_PARAMETER_NOT_USED(endpoint_out);
113 
114     /* Build option check.  */
115     UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= (USBX_DEVICE_CLASS_STORAGE_GET_PERFORMANCE_0_LENGTH + 8));
116 
117     /* If trace is enabled, insert this event into the trace buffer.  */
118     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_OTHER, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
119 
120     /* Obtain the pointer to the transfer request.  */
121     transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
122 
123     /* Ensure memory buffer cleaned.  */
124     _ux_utility_memory_set(transfer_request -> ux_slave_transfer_request_data_pointer, 0, 64); /* Use case of memset is verified. */
125 
126     /* Get the performance page code.  */
127     performance_page =  (ULONG) *(cbwcb + UX_SLAVE_CLASS_STORAGE_GET_PERFORMANCE_PAGE);
128 
129     /* Filter it as the response depends on it.  */
130     switch (performance_page)
131     {
132 
133         case UX_SLAVE_CLASS_STORAGE_GET_PERFORMANCE_PAGE_14  :
134 
135             /* Put the length to be returned. */
136             _ux_utility_long_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer,
137                                             UX_SLAVE_CLASS_STORAGE_GET_PERFORMANCE_PAYLOAD_LENGTH);
138 
139             /* Put the payload to be returned. */
140             _ux_utility_long_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer + 4,
141                                             UX_SLAVE_CLASS_STORAGE_GET_PERFORMANCE_PAYLOAD);
142 
143             data_length = UX_SLAVE_CLASS_STORAGE_GET_PERFORMANCE_RESPONSE_LENGTH;
144             break;
145 
146         case UX_SLAVE_CLASS_STORAGE_GET_PERFORMANCE_PAGE_0 :
147 
148             /* Put the length to be returned. */
149             _ux_utility_long_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer,
150                                             USBX_DEVICE_CLASS_STORAGE_GET_PERFORMANCE_0_LENGTH + 8);
151 
152             /* Copy the CSW into the transfer request memory.  */
153             _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer + 8,
154                                         usbx_device_class_storage_performance, USBX_DEVICE_CLASS_STORAGE_GET_PERFORMANCE_0_LENGTH); /* Use case of memcpy is verified. */
155 
156             data_length = USBX_DEVICE_CLASS_STORAGE_GET_PERFORMANCE_0_LENGTH + 8;
157             break;
158 
159         default:
160             data_length = 0;
161     }
162 
163 #if defined(UX_DEVICE_STANDALONE)
164 
165     /* Next: Transfer (DATA).  */
166     storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
167     storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
168 
169     storage -> ux_device_class_storage_transfer = transfer_request;
170     storage -> ux_device_class_storage_device_length = data_length;
171     storage -> ux_device_class_storage_data_length = data_length;
172     storage -> ux_device_class_storage_data_count = 0;
173     UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer);
174 
175 #else
176 
177     /* Send a data payload with the read_capacity response buffer.  */
178     _ux_device_stack_transfer_request(transfer_request, data_length, data_length);
179 #endif
180 
181     /* Now we set the CSW with success.  */
182     storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
183     status = UX_SUCCESS;
184 
185     /* Return completion status.  */
186     return(status);
187 }
188 
189