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 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_device_class_storage.h"
29 #include "ux_device_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_device_class_storage_test_ready                 PORTABLE C      */
37 /*                                                           6.1.10       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function tests if the SCSI slave device is ready.              */
45 /*    The status function of the storage devices is called. If there is   */
46 /*    an error, the request sense parameters are set for the host to      */
47 /*    investigate.                                                        */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    storage                               Pointer to storage class      */
52 /*    endpoint_in                           Pointer to IN endpoint        */
53 /*    endpoint_out                          Pointer to OUT endpoint       */
54 /*    cbwcb                                 Pointer to CBWCB              */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    (ux_slave_class_storage_media_status) Get media status              */
63 /*    _ux_device_class_storage_csw_send     Send CSW                      */
64 /*                                                                        */
65 /*  CALLED BY                                                             */
66 /*                                                                        */
67 /*    Device Storage Class                                                */
68 /*                                                                        */
69 /*  RELEASE HISTORY                                                       */
70 /*                                                                        */
71 /*    DATE              NAME                      DESCRIPTION             */
72 /*                                                                        */
73 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
74 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            optimized command logic,    */
76 /*                                            resulting in version 6.1    */
77 /*  12-31-2020     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            fixed USB CV test issues,   */
79 /*                                            resulting in version 6.1.3  */
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_test_ready(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_test_ready(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, UX_SLAVE_ENDPOINT *endpoint_in,
86                                           UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
87 {
88 
89 UINT        status;
90 ULONG        media_status;
91 
92     UX_PARAMETER_NOT_USED(lun);
93     UX_PARAMETER_NOT_USED(endpoint_in);
94     UX_PARAMETER_NOT_USED(endpoint_out);
95     UX_PARAMETER_NOT_USED(cbwcb);
96 
97     /* If trace is enabled, insert this event into the trace buffer.  */
98     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_TEST_READY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
99 
100     /* Obtain the status of the device.  */
101     status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_status(storage, lun,
102                                 storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_id, &media_status);
103 
104     /* Set the sense/code/qualifier codes for the REQUEST_SENSE command.  */
105     storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
106 
107     /* Return CSW with success/error.  */
108     storage -> ux_slave_class_storage_csw_status = (status == UX_SUCCESS) ?
109                             UX_SLAVE_CLASS_STORAGE_CSW_PASSED : UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
110     status = UX_SUCCESS;
111 
112 #if !defined(UX_DEVICE_STANDALONE)
113 
114     /* Case (9) Ho > Dn.  */
115     if (storage -> ux_slave_class_storage_host_length)
116     {
117         _ux_device_stack_endpoint_stall(endpoint_out);
118         storage -> ux_slave_class_storage_csw_residue = storage -> ux_slave_class_storage_host_length;
119     }
120 #endif
121 
122     /* Return completion status.  */
123     return(status);
124 }
125 
126