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_REQUEST_DATA_MAX_LENGTH < UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_LENGTH
34 #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH too small, please check
35 #endif
36 
37 /**************************************************************************/
38 /*                                                                        */
39 /*  FUNCTION                                               RELEASE        */
40 /*                                                                        */
41 /*    _ux_device_class_storage_report_key                 PORTABLE C      */
42 /*                                                           6.1.10       */
43 /*  AUTHOR                                                                */
44 /*                                                                        */
45 /*    Chaoqiong Xiao, Microsoft Corporation                               */
46 /*                                                                        */
47 /*  DESCRIPTION                                                           */
48 /*                                                                        */
49 /*    This function performs a REPORT_KEY SCSI 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_stack_transfer_request     Transfer request              */
65 /*    _ux_device_class_storage_csw_send     Send CSW                      */
66 /*    _ux_utility_memory_set                Set memory                    */
67 /*                                                                        */
68 /*  CALLED BY                                                             */
69 /*                                                                        */
70 /*    Device Storage Class                                                */
71 /*                                                                        */
72 /*  RELEASE HISTORY                                                       */
73 /*                                                                        */
74 /*    DATE              NAME                      DESCRIPTION             */
75 /*                                                                        */
76 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
77 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            optimized command logic,    */
79 /*                                            verified memset and memcpy  */
80 /*                                            cases,                      */
81 /*                                            resulting in version 6.1    */
82 /*  12-31-2020     Chaoqiong Xiao           Modified comment(s),          */
83 /*                                            fixed compile warning,      */
84 /*                                            resulting in version 6.1.3  */
85 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
86 /*                                            added standalone support,   */
87 /*                                            resulting in version 6.1.10 */
88 /*                                                                        */
89 /**************************************************************************/
_ux_device_class_storage_report_key(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)90 UINT  _ux_device_class_storage_report_key(UX_SLAVE_CLASS_STORAGE *storage,
91                       ULONG               lun,
92                       UX_SLAVE_ENDPOINT   *endpoint_in,
93                       UX_SLAVE_ENDPOINT   *endpoint_out,
94                       UCHAR               *cbwcb)
95 {
96 
97 UINT                    status = UX_SUCCESS;
98 UX_SLAVE_TRANSFER       *transfer_request;
99 ULONG                   allocation_length;
100 ULONG                   key_format;
101 
102 
103     UX_PARAMETER_NOT_USED(lun);
104     UX_PARAMETER_NOT_USED(endpoint_out);
105 
106     /* If trace is enabled, insert this event into the trace buffer.  */
107     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_OTHER, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
108 
109     /* Obtain the pointer to the transfer request.  */
110     transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
111 
112     /* Get the report key.  */
113     key_format =  (ULONG) *(cbwcb + UX_SLAVE_CLASS_STORAGE_REPORT_KEY_FORMAT);
114 
115     /* Extract the length to be returned by the cbwcb.  */
116     allocation_length =  _ux_utility_short_get_big_endian(cbwcb + UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ALLOCATION_LENGTH);
117 
118     /* Ensure memory buffer cleaned.  */
119     _ux_utility_memory_set(transfer_request -> ux_slave_transfer_request_data_pointer, 0, UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_LENGTH); /* Use case of memset is verified. */
120 
121     /* Filter page code. This is necessary to isolate the CD-ROM mode sense response.  */
122     switch (key_format)
123     {
124 
125         case UX_SLAVE_CLASS_STORAGE_REPORT_KEY_FORMAT_RPC :
126 
127 
128             /* Put the length to be returned. */
129             _ux_utility_short_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer,
130                                             UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_PAYLOAD);
131 
132             /* Put the reset field. */
133             *(transfer_request -> ux_slave_transfer_request_data_pointer + UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_RESET_FIELD) =  0x25;
134 
135             /* Put the region mask. */
136             *(transfer_request -> ux_slave_transfer_request_data_pointer + UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_REGION_MASK) =  0xFF;
137 
138             /* No region code. */
139             *(transfer_request -> ux_slave_transfer_request_data_pointer + UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_RPC_SCHEME)  =  0x00;
140 
141             /* Compute the payload to return. Depends on the request.  */
142             if (allocation_length >  UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_LENGTH)
143 
144                 /* Adjust the reply.  */
145                 allocation_length = UX_SLAVE_CLASS_STORAGE_REPORT_KEY_ANSWER_LENGTH;
146 
147             break;
148 
149         default :
150             allocation_length = 0;
151             break;
152 
153     }
154 
155 #if defined(UX_DEVICE_STANDALONE)
156 
157     /* Next: Transfer (DATA).  */
158     storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
159     storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
160 
161     storage -> ux_device_class_storage_transfer = transfer_request;
162     storage -> ux_device_class_storage_device_length = allocation_length;
163     storage -> ux_device_class_storage_data_length = allocation_length;
164     storage -> ux_device_class_storage_data_count = 0;
165     UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer);
166 
167 #else
168 
169     /* Send a data payload with the read_capacity response buffer.  */
170     status = _ux_device_stack_transfer_request(transfer_request, allocation_length, allocation_length);  /* Use case of memset is verified. */
171 #endif
172 
173     /* Now we set the CSW with success.  */
174     storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
175 
176     /* Return completion status.  */
177     return(status);
178 }
179 
180