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_READ_FORMAT_CAPACITY_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_read_format_capacity       PORTABLE C      */
42 /*                                                           6.1.10       */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Chaoqiong Xiao, Microsoft Corporation                               */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function performs a READ_FORMAT_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_utility_memory_set                Set memory                    */
67 /*    _ux_utility_long_put_big_endian       Put 32-bit big endian         */
68 /*    _ux_utility_memory_copy               Copy memory                   */
69 /*                                                                        */
70 /*  CALLED BY                                                             */
71 /*                                                                        */
72 /*    Device Storage Class                                                */
73 /*                                                                        */
74 /*  RELEASE HISTORY                                                       */
75 /*                                                                        */
76 /*    DATE              NAME                      DESCRIPTION             */
77 /*                                                                        */
78 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
79 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
80 /*                                            optimized command logic,    */
81 /*                                            verified memset and memcpy  */
82 /*                                            cases,                      */
83 /*                                            resulting in version 6.1    */
84 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
85 /*                                            added standalone support,   */
86 /*                                            resulting in version 6.1.10 */
87 /*                                                                        */
88 /**************************************************************************/
_ux_device_class_storage_read_format_capacity(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)89 UINT  _ux_device_class_storage_read_format_capacity(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
90                                             UX_SLAVE_ENDPOINT *endpoint_in,
91                                             UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
92 {
93 
94 UINT                    status;
95 UX_SLAVE_TRANSFER       *transfer_request;
96 UCHAR                   *read_format_capacity_buffer;
97 
98     UX_PARAMETER_NOT_USED(cbwcb);
99     UX_PARAMETER_NOT_USED(endpoint_out);
100 
101     /* If trace is enabled, insert this event into the trace buffer.  */
102     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_READ_FORMAT_CAPACITY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
103 
104     /* Obtain the pointer to the transfer request.  */
105     transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
106 
107     /* Get read format capacity response buffer.  */
108     read_format_capacity_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
109 
110     /* Ensure it is cleaned.  */
111     _ux_utility_memory_set(read_format_capacity_buffer, 0, UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH); /* Use case of memset is verified. */
112 
113     /* Insert the size of the response block.  */
114     _ux_utility_long_put_big_endian(&read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_SIZE], 8);
115 
116     /* Insert the last LBA address in the response.  */
117     _ux_utility_long_put_big_endian(&read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LAST_LBA],
118                                     storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_last_lba);
119 
120     /* Insert the block length in the response.  This is in 3 bytes. */
121     _ux_utility_long_put_big_endian(&read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_BLOCK_SIZE],
122                                     storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_block_length);
123 
124     /* Insert the response code : always 2.  */
125     read_format_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_DESC_CODE] =  2;
126 
127 #if defined(UX_DEVICE_STANDALONE)
128 
129     /* Next: Transfer (DATA).  */
130     storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
131     storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
132 
133     storage -> ux_device_class_storage_transfer = transfer_request;
134     storage -> ux_device_class_storage_device_length =
135                     UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH;
136     storage -> ux_device_class_storage_data_length =
137                     UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH;
138     storage -> ux_device_class_storage_data_count = 0;
139     UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer);
140 
141 #else
142 
143     /* Send a data payload with the read_capacity response buffer.  */
144     _ux_device_stack_transfer_request(transfer_request,
145                                   UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH,
146                                   UX_SLAVE_CLASS_STORAGE_READ_FORMAT_CAPACITY_RESPONSE_LENGTH);
147 #endif
148 
149     /* Now we set the CSW with success.  */
150     storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
151     status = UX_SUCCESS;
152 
153     /* Return completion status.  */
154     return(status);
155 }
156 
157