GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_storage_csw_send.c Lines: 13 13 100.0 %
Date: 2026-03-06 18:57:10 Branches: 2 2 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 * Copyright (c) 2026-present Eclipse ThreadX contributors
4
 *
5
 * This program and the accompanying materials are made available under the
6
 * terms of the MIT License which is available at
7
 * https://opensource.org/licenses/MIT.
8
 *
9
 * SPDX-License-Identifier: MIT
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
#if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < UX_SLAVE_CLASS_STORAGE_CSW_LENGTH
33
/* #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH too small, please check  */
34
/* Build option checked runtime by UX_ASSERT  */
35
#endif
36
37
/**************************************************************************/
38
/*                                                                        */
39
/*  FUNCTION                                               RELEASE        */
40
/*                                                                        */
41
/*    _ux_device_class_storage_csw_send                   PORTABLE C      */
42
/*                                                           6.3.0        */
43
/*  AUTHOR                                                                */
44
/*                                                                        */
45
/*    Chaoqiong Xiao, Microsoft Corporation                               */
46
/*                                                                        */
47
/*  DESCRIPTION                                                           */
48
/*                                                                        */
49
/*    This function sends the status phase of SCSI transaction. Note that */
50
/*    dCSWDataResidue is always set to 0 because we either transfer all   */
51
/*    the data, or report command failure.                                */
52
/*                                                                        */
53
/*  INPUT                                                                 */
54
/*                                                                        */
55
/*    storage                               Pointer to storage class      */
56
/*    endpoint_in                           Pointer to IN endpoint        */
57
/*    status                                Status of CSW                 */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    Completion Status                                                   */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    _ux_device_stack_transfer_request     Transfer request              */
66
/*    _ux_utility_long_put                  Put long word                 */
67
/*    _ux_utility_memory_copy               Copy memory                   */
68
/*    _ux_utility_memory_set                Set memory                    */
69
/*                                                                        */
70
/*  CALLED BY                                                             */
71
/*                                                                        */
72
/*    Device Storage Class                                                */
73
/*                                                                        */
74
/**************************************************************************/
75
2296
UINT  _ux_device_class_storage_csw_send(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
76
                                UX_SLAVE_ENDPOINT *endpoint_in, UCHAR csw_status)
77
{
78
79
2296
UINT                    status = UX_SUCCESS;
80
UX_SLAVE_TRANSFER       *transfer_request;
81
UCHAR                   *csw_buffer;
82
83
84
    UX_PARAMETER_NOT_USED(csw_status);
85
    UX_PARAMETER_NOT_USED(lun);
86
87
    /* Build option check.  */
88
    UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= UX_SLAVE_CLASS_STORAGE_CSW_LENGTH);
89
90
#if defined(UX_DEVICE_STANDALONE)
91
92
    /* Reset transfer request buffer pointers.  */
93
    storage -> ux_device_class_storage_ep_out -> ux_slave_endpoint_transfer_request.
94
            ux_slave_transfer_request_data_pointer = storage -> ux_device_class_storage_buffer[0];
95
    storage -> ux_device_class_storage_ep_in -> ux_slave_endpoint_transfer_request.
96
            ux_slave_transfer_request_data_pointer = storage -> ux_device_class_storage_buffer[1];
97
#endif
98
99
    /* If CSW skipped, just return.  */
100
2296
    if (UX_DEVICE_CLASS_STORAGE_CSW_SKIP(&storage -> ux_slave_class_storage_csw_status))
101
1
        return(UX_SUCCESS);
102
103
    /* Obtain the pointer to the transfer request.  */
104
2295
    transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
105
106
    /* Get CSW buffer pointer.  */
107
2295
    csw_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
108
109
    /* Ensure it is cleaned.  */
110
2295
    _ux_utility_memory_set(csw_buffer, 0, UX_SLAVE_CLASS_STORAGE_CSW_LENGTH); /* Use case of memset is verified. */
111
112
    /* Store the signature of the CSW.  */
113
2295
    _ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_SIGNATURE], UX_SLAVE_CLASS_STORAGE_CSW_SIGNATURE_MASK);
114
115
    /* Store the SCSI tag from the CBW.  */
116
2295
    _ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_TAG], storage -> ux_slave_class_storage_scsi_tag);
117
118
    /* Store the dCSWDataResidue.  */
119
2295
    _ux_utility_long_put(&csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_DATA_RESIDUE], storage -> ux_slave_class_storage_csw_residue);
120
121
    /* Store the status of the previous operation.  */
122
2295
    csw_buffer[UX_SLAVE_CLASS_STORAGE_CSW_STATUS] = (UCHAR)storage -> ux_slave_class_storage_csw_status;
123
124
#if defined(UX_DEVICE_STANDALONE)
125
126
    /* Next: Transfer (CSW).  */
127
    storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_CSW;
128
    storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
129
    storage -> ux_device_class_storage_transfer = transfer_request;
130
131
    storage -> ux_device_class_storage_device_length = UX_SLAVE_CLASS_STORAGE_CSW_LENGTH;
132
    storage -> ux_device_class_storage_data_length = UX_SLAVE_CLASS_STORAGE_CSW_LENGTH;
133
    storage -> ux_device_class_storage_data_count = 0;
134
135
#else
136
137
    /* We may be in a special state machine condition where the endpoint is stalled waiting for
138
       a CLEAR_FEATURE.  We will wait until the host clears the endpoint.
139
       The transfer_request function does that.  */
140
    /* Send the CSW back to the host.  */
141
2295
    status =  _ux_device_stack_transfer_request(transfer_request, UX_SLAVE_CLASS_STORAGE_CSW_LENGTH,
142
                                    UX_SLAVE_CLASS_STORAGE_CSW_LENGTH);
143
#endif
144
145
    /* Return completion status.  */
146
2292
    return(status);
147
}
148