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 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_device_class_storage_get_status_notification    PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function performs a GET_STATUS_NOTIFICATION command.           */
46 /*                                                                        */
47 /*  INPUT                                                                 */
48 /*                                                                        */
49 /*    storage                               Pointer to storage class      */
50 /*    endpoint_in                           Pointer to IN endpoint        */
51 /*    endpoint_out                          Pointer to OUT endpoint       */
52 /*    cbwcb                                 Pointer to CBWCB              */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    Completion Status                                                   */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*    _ux_device_class_storage_csw_send     Send CSW                      */
61 /*    _ux_device_stack_transfer_request     Transfer request              */
62 /*    _ux_device_stack_endpoint_stall       Stall endpoint                */
63 /*    _ux_utility_short_put_big_endian      Put 16-bit big endian         */
64 /*    _ux_utility_memory_copy               Copy memory                   */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Device Storage Class                                                */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            optimized command logic,    */
77 /*                                            verified memset and memcpy  */
78 /*                                            cases,                      */
79 /*                                            resulting in version 6.1    */
80 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
81 /*                                            added standalone support,   */
82 /*                                            resulting in version 6.1.10 */
83 /*                                                                        */
84 /**************************************************************************/
_ux_device_class_storage_get_status_notification(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)85 UINT  _ux_device_class_storage_get_status_notification(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
86                                             UX_SLAVE_ENDPOINT *endpoint_in,
87                                             UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
88 {
89 
90 UINT                    status;
91 UX_SLAVE_TRANSFER       *transfer_request;
92 UCHAR                   *media_notification;
93 ULONG                   media_notification_length;
94 ULONG                   notification_class;
95 
96     UX_PARAMETER_NOT_USED(endpoint_out);
97 
98     /* If trace is enabled, insert this event into the trace buffer.  */
99     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_READ_CAPACITY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0);
100 
101     /* Default CSW to failed.  */
102     storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
103 
104     /* Ensure the callback has been initialized.  */
105     if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_notification == UX_NULL)
106     {
107 
108 #if !defined(UX_DEVICE_STANDALONE)
109 
110         /* We need to STALL the IN endpoint.  The endpoint will be reset by the host.  */
111         _ux_device_stack_endpoint_stall(endpoint_in);
112 #endif
113 
114         /* And update the REQUEST_SENSE codes.  */
115         storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
116                                                UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(0x05,0x26,0x01);
117 
118         /* Return error.  */
119         return(UX_FUNCTION_NOT_SUPPORTED);
120     }
121 
122     /* Extract the notification from the cbwcb.  */
123     notification_class = (ULONG) *(cbwcb + UX_SLAVE_CLASS_STORAGE_EVENT_NOTIFICATION_CLASS_REQUEST);
124 
125     /* Obtain the notification of the device.  */
126     status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_notification(storage, lun,
127                                 storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_id,
128                                 notification_class,
129                                 &media_notification,
130                                 &media_notification_length);
131 
132     /* Check the notification length.  */
133     if (media_notification_length > UX_SLAVE_REQUEST_DATA_MAX_LENGTH - sizeof(USHORT))
134     {
135 
136         /* If trace is enabled, insert this event into the trace buffer.  */
137         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, 0, 0, 0, UX_TRACE_ERRORS, 0, 0)
138 
139         /* Error callback.  */
140         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
141 
142         /* Set status code.  */
143         status = UX_MEMORY_INSUFFICIENT;
144     }
145 
146     /* Check the status for error.  */
147     if (status != UX_SUCCESS)
148     {
149 
150 #if !defined(UX_DEVICE_STANDALONE)
151 
152         /* We need to STALL the IN endpoint.  The endpoint will be reset by the host.  */
153         _ux_device_stack_endpoint_stall(endpoint_in);
154 #endif
155     }
156     else
157     {
158 
159         /* Obtain the pointer to the transfer request.  */
160         transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
161 
162         /* Put the length of the notification length in the buffer.  */
163         _ux_utility_short_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer, (USHORT)media_notification_length);
164 
165         /* Copy the CSW into the transfer request memory.  */
166         _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer + sizeof (USHORT),
167                                             media_notification,
168                                             media_notification_length); /* Use case of memcpy is verified. */
169 
170         /* Update the notification length. */
171         media_notification_length += (ULONG)sizeof (USHORT);
172 
173 #if !defined(UX_DEVICE_STANDALONE)
174         /* Send a data payload with the notification buffer.  */
175         _ux_device_stack_transfer_request(transfer_request,
176                                   media_notification_length,
177                                   media_notification_length);
178 #else
179         /* Next: Transfer (DATA).  */
180         storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
181         storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
182 
183         storage -> ux_device_class_storage_transfer = transfer_request;
184         storage -> ux_device_class_storage_device_length = media_notification_length;
185         storage -> ux_device_class_storage_data_length = media_notification_length;
186         storage -> ux_device_class_storage_data_count = 0;
187         UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer);
188 #endif
189 
190         /* Now we set the CSW with success.  */
191         storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
192         status = UX_SUCCESS;
193     }
194 
195     /* Return completion status.  */
196     return(status);
197 }
198 
199