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_REQUEST_DATA_MAX_LENGTH < UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH
33 /* #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH is 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_read_capacity              PORTABLE C      */
42 /*                                                           6.3.0        */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Chaoqiong Xiao, Microsoft Corporation                               */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function performs a READ_CAPACITY 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_device_stack_endpoint_stall       Stall endpoint                */
67 /*    _ux_utility_long_put_big_endian       Put 32-bit big endian         */
68 /*    _ux_utility_memory_copy               Copy memory                   */
69 /*    _ux_utility_memory_set                Set memory                    */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    Device Storage Class                                                */
74 /*                                                                        */
75 /*  RELEASE HISTORY                                                       */
76 /*                                                                        */
77 /*    DATE              NAME                      DESCRIPTION             */
78 /*                                                                        */
79 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
80 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            optimized command logic,    */
82 /*                                            verified memset and memcpy  */
83 /*                                            cases,                      */
84 /*                                            resulting in version 6.1    */
85 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
86 /*                                            added standalone support,   */
87 /*                                            resulting in version 6.1.10 */
88 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
89 /*                                            checked compiling options   */
90 /*                                            by runtime UX_ASSERT,       */
91 /*                                            resulting in version 6.3.0  */
92 /*                                                                        */
93 /**************************************************************************/
_ux_device_class_storage_read_capacity(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)94 UINT  _ux_device_class_storage_read_capacity(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
95                                             UX_SLAVE_ENDPOINT *endpoint_in,
96                                             UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
97 {
98 
99 UINT                    status;
100 ULONG                   media_status;
101 UX_SLAVE_TRANSFER       *transfer_request;
102 UCHAR                   *read_capacity_buffer;
103 
104     UX_PARAMETER_NOT_USED(cbwcb);
105     UX_PARAMETER_NOT_USED(endpoint_out);
106 
107     /* Build option check.  */
108     UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH);
109 
110     /* If trace is enabled, insert this event into the trace buffer.  */
111     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_READ_CAPACITY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
112 
113     /* Obtain the status of the device.  */
114     status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_status(storage, lun,
115                                 storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_id, &media_status);
116 
117     /* Update the request sense.  */
118     storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
119 
120     /* Check the status for error.  */
121     if (status != UX_SUCCESS)
122     {
123 
124 #if !defined(UX_DEVICE_STANDALONE)
125 
126         /* We need to STALL the IN endpoint.  The endpoint will be reset by the host.  */
127         _ux_device_stack_endpoint_stall(endpoint_in);
128 #endif
129 
130         /* Now we set the CSW with Error.  */
131         storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
132         status = UX_SUCCESS;
133     }
134     else
135     {
136 
137         /* Obtain the pointer to the transfer request.  */
138         transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
139 
140         /* Obtain read capacity response buffer.  */
141         read_capacity_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
142 
143         /* Ensure it is cleaned.  */
144         _ux_utility_memory_set(read_capacity_buffer, 0, UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH); /* Use case of memcpy is verified. */
145 
146         /* Insert the last LBA address in the response.  */
147         _ux_utility_long_put_big_endian(&read_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LAST_LBA],
148                                         storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_last_lba);
149 
150         /* Insert the block length in the response.  */
151         _ux_utility_long_put_big_endian(&read_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_BLOCK_SIZE],
152                                         storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_block_length);
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 =
162                         UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH;
163         storage -> ux_device_class_storage_data_length =
164                         UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH;
165         storage -> ux_device_class_storage_data_count = 0;
166         UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer);
167 
168 #else
169 
170         /* Send a data payload with the read_capacity response buffer.  */
171         _ux_device_stack_transfer_request(transfer_request,
172                                       UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH,
173                                       UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH);
174 #endif
175 
176         /* Now we set the CSW with success.  */
177         storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
178         status = UX_SUCCESS;
179     }
180 
181     /* Return completion status.  */
182     return(status);
183 }
184 
185