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_DISK_INFORMATION_LENGTH 34
32 #if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < USBX_DEVICE_CLASS_STORAGE_DISK_INFORMATION_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 UCHAR usbx_device_class_storage_disk_information[] = {
37 
38         0x00, 0x00,                     /* Entire length of disk_information             */
39         0x0e,                           /* Erasable/state of last session/disk status ...*/
40         0x01,                           /* Number of first track on disk.                */
41         0x01,                           /* Number of sessions.                           */
42         0x01,                           /* First track number in last session.           */
43         0x01,                           /* Last track number in last session.            */
44 
45         0x00,                           /* DID_V, DBC_V, URU ....                        */
46         0x00,                           /* Disk type                                     */
47         0x00,                           /* Number of sessions                            */
48         0x00,                           /* First Track Number in last session            */
49         0x00,                           /* Last Track Number in last session             */
50 
51         0x00, 0x00, 0x00, 0x00,         /* Disk Identification                           */
52         0xff, 0xff, 0xff, 0xff,         /* Last session lead in start time               */
53         0xff, 0xff, 0xff, 0xff,         /* Last possible start time for SOLO             */
54         0x00, 0x00, 0x00, 0x00,         /* Disk Bar Code                                 */
55         0x00,                           /* Reserved                                      */
56         0x00,                           /* Number of OPC table entries.                  */
57         0x00, 0x00, 0x00, 0x00,         /* Some padding ?                                */
58         0x00, 0x00                      /*                                               */
59 
60     };
61 
62 /**************************************************************************/
63 /*                                                                        */
64 /*  FUNCTION                                               RELEASE        */
65 /*                                                                        */
66 /*    _ux_device_class_storage_read_disk_information      PORTABLE C      */
67 /*                                                           6.3.0        */
68 /*  AUTHOR                                                                */
69 /*                                                                        */
70 /*    Chaoqiong Xiao, Microsoft Corporation                               */
71 /*                                                                        */
72 /*  DESCRIPTION                                                           */
73 /*                                                                        */
74 /*    This function performs a READ_DISK_INFORMATION command.             */
75 /*                                                                        */
76 /*  INPUT                                                                 */
77 /*                                                                        */
78 /*    storage                               Pointer to storage class      */
79 /*    endpoint_in                           Pointer to IN endpoint        */
80 /*    endpoint_out                          Pointer to OUT endpoint       */
81 /*    cbwcb                                 Pointer to CBWCB              */
82 /*                                                                        */
83 /*  OUTPUT                                                                */
84 /*                                                                        */
85 /*    Completion Status                                                   */
86 /*                                                                        */
87 /*  CALLS                                                                 */
88 /*                                                                        */
89 /*    _ux_device_class_storage_csw_send     Send CSW                      */
90 /*    _ux_device_stack_transfer_request     Transfer request              */
91 /*    _ux_utility_short_put_big_endian      Put 16-bit big endian         */
92 /*    _ux_utility_memory_copy               Copy memory                   */
93 /*                                                                        */
94 /*  CALLED BY                                                             */
95 /*                                                                        */
96 /*    Device Storage Class                                                */
97 /*                                                                        */
98 /*  RELEASE HISTORY                                                       */
99 /*                                                                        */
100 /*    DATE              NAME                      DESCRIPTION             */
101 /*                                                                        */
102 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
103 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
104 /*                                            optimized command logic,    */
105 /*                                            verified memset and memcpy  */
106 /*                                            cases,                      */
107 /*                                            resulting in version 6.1    */
108 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
109 /*                                            added standalone support,   */
110 /*                                            resulting in version 6.1.10 */
111 /*  10-31-2023     Chaoqiong Xiao           Modified comment(s),          */
112 /*                                            checked compiling options   */
113 /*                                            by runtime UX_ASSERT,       */
114 /*                                            resulting in version 6.3.0  */
115 /*                                                                        */
116 /**************************************************************************/
_ux_device_class_storage_read_disk_information(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)117 UINT  _ux_device_class_storage_read_disk_information(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
118                                             UX_SLAVE_ENDPOINT *endpoint_in,
119                                             UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
120 {
121 
122 UINT                    status;
123 UX_SLAVE_TRANSFER       *transfer_request;
124 ULONG                   allocation_length;
125 
126     UX_PARAMETER_NOT_USED(endpoint_out);
127 
128     /* Build option check.  */
129     UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= USBX_DEVICE_CLASS_STORAGE_DISK_INFORMATION_LENGTH);
130 
131     /* If trace is enabled, insert this event into the trace buffer.  */
132     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_GET_CONFIGURATION, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
133 
134     /* Initialize the length of the disk information field.  */
135     _ux_utility_short_put_big_endian(usbx_device_class_storage_disk_information, (USBX_DEVICE_CLASS_STORAGE_DISK_INFORMATION_LENGTH - 2));
136 
137     /* Clean the disk status.  */
138     usbx_device_class_storage_disk_information[2] &= (UCHAR)~3;
139 
140     /* Update the disk status.  */
141     usbx_device_class_storage_disk_information[2] = (UCHAR)(usbx_device_class_storage_disk_information[2] | (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_disk_status & 3));
142 
143     /* Clean the last session state.  */
144     usbx_device_class_storage_disk_information[2] &= (UCHAR)~0x0c;
145 
146     /* Update the last session state.  */
147     usbx_device_class_storage_disk_information[2] = (UCHAR)(usbx_device_class_storage_disk_information[2] | ((storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_last_session_state << 2) & 0x0c));
148 
149     /* Obtain the pointer to the transfer request.  */
150     transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
151 
152     /* Get the allocation length.  */
153     allocation_length =  _ux_utility_short_get_big_endian(cbwcb + UX_SLAVE_CLASS_STORAGE_READ_DISK_INFORMATION_ALLOCATION_LENGTH);
154 
155     /* Can we send all the disk information ? */
156     if (allocation_length > USBX_DEVICE_CLASS_STORAGE_DISK_INFORMATION_LENGTH)
157 
158         /* Yes, so send only the disk information profile.  */
159         allocation_length = USBX_DEVICE_CLASS_STORAGE_DISK_INFORMATION_LENGTH;
160 
161     /* Copy the CSW into the transfer request memory.  */
162     _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
163                                         usbx_device_class_storage_disk_information,
164                                         allocation_length); /* Use case of memcpy is verified. */
165 
166 #if defined(UX_DEVICE_STANDALONE)
167 
168         /* Next: Transfer (DATA).  */
169         storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
170         storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
171 
172         storage -> ux_device_class_storage_transfer = transfer_request;
173         storage -> ux_device_class_storage_device_length = allocation_length;
174         storage -> ux_device_class_storage_data_length = allocation_length;
175         storage -> ux_device_class_storage_data_count = 0;
176         UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer);
177 
178 #else
179 
180     /* Send a data payload with the read_capacity response buffer.  */
181     _ux_device_stack_transfer_request(transfer_request,
182                               allocation_length,
183                               allocation_length);
184 #endif
185 
186     /* Now we set the CSW with success.  */
187     storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
188     status = UX_SUCCESS;
189 
190     /* Return completion status.  */
191     return(status);
192 }
193 
194