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 
24 /* Include necessary system files.  */
25 
26 #define UX_SOURCE_CODE
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_test_ready                 PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function tests if the SCSI slave device is ready.              */
46 /*    The status function of the storage devices is called. If there is   */
47 /*    an error, the request sense parameters are set for the host to      */
48 /*    investigate.                                                        */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    storage                               Pointer to storage class      */
53 /*    endpoint_in                           Pointer to IN endpoint        */
54 /*    endpoint_out                          Pointer to OUT endpoint       */
55 /*    cbwcb                                 Pointer to CBWCB              */
56 /*                                                                        */
57 /*  OUTPUT                                                                */
58 /*                                                                        */
59 /*    Completion Status                                                   */
60 /*                                                                        */
61 /*  CALLS                                                                 */
62 /*                                                                        */
63 /*    (ux_slave_class_storage_media_status) Get media status              */
64 /*    _ux_device_class_storage_csw_send     Send CSW                      */
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 /*                                            resulting in version 6.1    */
78 /*  12-31-2020     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            fixed USB CV test issues,   */
80 /*                                            resulting in version 6.1.3  */
81 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
82 /*                                            added standalone support,   */
83 /*                                            resulting in version 6.1.10 */
84 /*                                                                        */
85 /**************************************************************************/
_ux_device_class_storage_test_ready(UX_SLAVE_CLASS_STORAGE * storage,ULONG lun,UX_SLAVE_ENDPOINT * endpoint_in,UX_SLAVE_ENDPOINT * endpoint_out,UCHAR * cbwcb)86 UINT  _ux_device_class_storage_test_ready(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, UX_SLAVE_ENDPOINT *endpoint_in,
87                                           UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
88 {
89 
90 UINT        status;
91 ULONG        media_status;
92 
93     UX_PARAMETER_NOT_USED(lun);
94     UX_PARAMETER_NOT_USED(endpoint_in);
95     UX_PARAMETER_NOT_USED(endpoint_out);
96     UX_PARAMETER_NOT_USED(cbwcb);
97 
98     /* If trace is enabled, insert this event into the trace buffer.  */
99     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_TEST_READY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
100 
101     /* Obtain the status of the device.  */
102     status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_status(storage, lun,
103                                 storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_id, &media_status);
104 
105     /* Set the sense/code/qualifier codes for the REQUEST_SENSE command.  */
106     storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
107 
108     /* Return CSW with success/error.  */
109     storage -> ux_slave_class_storage_csw_status = (status == UX_SUCCESS) ?
110                             UX_SLAVE_CLASS_STORAGE_CSW_PASSED : UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
111     status = UX_SUCCESS;
112 
113 #if !defined(UX_DEVICE_STANDALONE)
114 
115     /* Case (9) Ho > Dn.  */
116     if (storage -> ux_slave_class_storage_host_length)
117     {
118         _ux_device_stack_endpoint_stall(endpoint_out);
119         storage -> ux_slave_class_storage_csw_residue = storage -> ux_slave_class_storage_host_length;
120     }
121 #endif
122 
123     /* Return completion status.  */
124     return(status);
125 }
126 
127