GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_storage_synchronize_cache.c Lines: 25 25 100.0 %
Date: 2026-03-06 18:57:10 Branches: 10 10 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
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_synchronize_cache          PORTABLE C      */
38
/*                                                           6.1.10       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function performs a SYNCHRONIZE_CACHE command in 32 or 16      */
46
/*    bits.                                                               */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    storage                               Pointer to storage class      */
51
/*    endpoint_in                           Pointer to IN endpoint        */
52
/*    endpoint_out                          Pointer to OUT endpoint       */
53
/*    cbwcb                                 Pointer to the CBWCB          */
54
/*    scsi_command                          SCSI command                  */
55
/*                                                                        */
56
/*  OUTPUT                                                                */
57
/*                                                                        */
58
/*    Completion Status                                                   */
59
/*                                                                        */
60
/*  CALLS                                                                 */
61
/*                                                                        */
62
/*    (ux_slave_class_storage_media_status) Get media status              */
63
/*    (ux_slave_class_storage_media_flush)  Flush media                   */
64
/*    _ux_device_class_storage_csw_send     Send CSW                      */
65
/*    _ux_device_stack_endpoint_stall       Stall endpoint                */
66
/*    _ux_utility_long_get_big_endian       Get 32-bit big endian         */
67
/*    _ux_utility_short_get_big_endian      Get 16-bit big endian         */
68
/*                                                                        */
69
/*  CALLED BY                                                             */
70
/*                                                                        */
71
/*    Device Storage Class                                                */
72
/*                                                                        */
73
/**************************************************************************/
74
11
UINT  _ux_device_class_storage_synchronize_cache(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
75
                                                 UX_SLAVE_ENDPOINT *endpoint_in,
76
                                                 UX_SLAVE_ENDPOINT *endpoint_out, UCHAR *cbwcb, UCHAR scsi_command)
77
{
78
79
UINT                    status;
80
ULONG                   lba;
81
USHORT                  number_blocks;
82
ULONG                   media_status;
83
84
#if !defined(UX_DEVICE_STANDALONE)
85
UCHAR                   flags;
86
#endif
87
88
89
    UX_PARAMETER_NOT_USED(endpoint_out);
90
    UX_PARAMETER_NOT_USED(scsi_command);
91
92
    /* By default status is passed.  */
93
11
    storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
94
95
    /* Is there not an implementation?  */
96
11
    if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_flush == UX_NULL)
97
    {
98
99
        /* This means the application is not using a cache.  */
100
101
        /* Return success.  */
102
1
        return(UX_SUCCESS);
103
    }
104
105
    /* Get the LBA and number of blocks from the CBWCB in 16 bits.  */
106
10
    lba           =         _ux_utility_long_get_big_endian(cbwcb + UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_LBA);
107
10
    number_blocks = (USHORT)_ux_utility_short_get_big_endian(cbwcb + UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_NUMBER_OF_BLOCKS);
108
109
    /* If trace is enabled, insert this event into the trace buffer.  */
110
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_SYNCHRONIZE_CACHE, storage, lun, lba, number_blocks, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
111
112
    /* Obtain the status of the device.  */
113
10
    status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_status(storage,
114
                            lun, storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_id, &media_status);
115
116
    /* Update the request sense.  */
117
10
    storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
118
119
    /* If there is a problem, return a failed command.  */
120
10
    if (status != UX_SUCCESS)
121
    {
122
123
        /* We have a problem, media status error. Return a bad completion and wait for the
124
           REQUEST_SENSE command.  */
125
#if !defined(UX_DEVICE_STANDALONE)
126
1
        _ux_device_stack_endpoint_stall(endpoint_in);
127
#else
128
        UX_PARAMETER_NOT_USED(endpoint_in);
129
        storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_ERR;
130
#endif
131
132
1
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
133
134
        /* We are done here.  */
135
1
        return(UX_ERROR);
136
    }
137
138
    /* Now it's OK to perform synchronize cache.  */
139
140
#if defined(UX_DEVICE_STANDALONE)
141
142
    /* Next: Disk (SYNC).  */
143
    storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_DISK_WAIT;
144
    storage -> ux_device_class_storage_disk_state = UX_DEVICE_CLASS_STORAGE_DISK_OP_START;
145
    storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_DISK_OP;
146
147
    storage -> ux_device_class_storage_cmd_lba = lba;
148
    storage -> ux_device_class_storage_cmd_n_lb = number_blocks;
149
150
#else
151
152
    /* Get the flags.  */
153
9
    flags =  *(cbwcb + UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_FLAGS);
154
155
    /* If the immediate bit is set, we return a CSW before flush.  */
156
9
    if ((flags & UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_FLAGS_IMMED) != 0)
157
1
        _ux_device_class_storage_csw_send(storage, lun, endpoint_in, UX_SLAVE_CLASS_STORAGE_CSW_PASSED);
158
159
    /* Send the flush command to the local media.  */
160
9
    status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_flush(storage, lun, number_blocks, lba, &media_status);
161
162
    /* Update the request sense.  */
163
9
    storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
164
165
    /* If the immediate bit is set, we are already done, no matter what local operation status is.  */
166
9
    if ((flags & UX_SLAVE_CLASS_STORAGE_SYNCHRONIZE_CACHE_FLAGS_IMMED) != 0)
167
    {
168
169
        /* CSW skipped since already sent in this function.  */
170
1
        UX_DEVICE_CLASS_STORAGE_CSW_SKIP(&storage -> ux_slave_class_storage_csw_status) = UX_TRUE;
171
1
        return(status);
172
    }
173
174
    /* If there is a problem, return a failed command.  */
175
8
    if (status != UX_SUCCESS)
176
    {
177
178
        /* We have a problem, request error. Return a bad completion and wait for the
179
           REQUEST_SENSE command.  */
180
1
        _ux_device_stack_endpoint_stall(endpoint_in);
181
182
1
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
183
184
        /* Return an error.  */
185
1
        return(UX_ERROR);
186
    }
187
#endif
188
189
    /* Return completion status.  */
190
7
    return(status);
191
}