GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_storage_write.c Lines: 53 56 94.6 %
Date: 2026-03-06 18:57:10 Branches: 21 22 95.5 %

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
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_device_class_storage_write                      PORTABLE C      */
37
/*                                                           6.1.10       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function performs a WRITE command in 32 or 16 bits.            */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    storage                               Pointer to storage class      */
49
/*    lun                                   Logical unit number           */
50
/*    endpoint_in                           Pointer to IN endpoint        */
51
/*    endpoint_out                          Pointer to OUT endpoint       */
52
/*    cbwcb                                 Pointer to the CBWCB          */
53
/*    scsi_command                          SCSI command                  */
54
/*                                                                        */
55
/*  OUTPUT                                                                */
56
/*                                                                        */
57
/*    Completion Status                                                   */
58
/*                                                                        */
59
/*  CALLS                                                                 */
60
/*                                                                        */
61
/*    (ux_slave_class_storage_media_status) Get media status              */
62
/*    (ux_slave_class_storage_media_write)  Write to media                */
63
/*    _ux_device_stack_endpoint_stall       Stall endpoint                */
64
/*    _ux_device_stack_transfer_request     Transfer request              */
65
/*    _ux_utility_long_get_big_endian       Get 32-bit big endian         */
66
/*    _ux_utility_short_get_big_endian      Get 16-bit big endian         */
67
/*                                                                        */
68
/*  CALLED BY                                                             */
69
/*                                                                        */
70
/*    Device Storage Class                                                */
71
/*                                                                        */
72
/**************************************************************************/
73
395
UINT  _ux_device_class_storage_write(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
74
                                    UX_SLAVE_ENDPOINT *endpoint_in,
75
                                    UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb, UCHAR scsi_command)
76
{
77
78
UINT                    status;
79
UX_SLAVE_TRANSFER       *transfer_request;
80
ULONG                   lba;
81
ULONG                   total_number_blocks;
82
ULONG                   media_status;
83
ULONG                   total_length;
84
85
#if !defined(UX_DEVICE_STANDALONE)
86
ULONG                   number_blocks;
87
ULONG                   transfer_length;
88
ULONG                   done_length;
89
#endif
90
91
92
    UX_PARAMETER_NOT_USED(endpoint_in);
93
94
395
    if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_medium_loaded_status == 0)
95
    {
96
        /* Media not loaded. Set NOT READY sense code.  */
97
        storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
98
            UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(UX_SLAVE_CLASS_STORAGE_SENSE_KEY_NOT_READY,
99
                                                 UX_SLAVE_CLASS_STORAGE_SENSE_CODE_NOT_PRESENT, 0x00);
100
101
        /* Return CSW with failure.  */
102
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
103
104
        /* Return completion status.  */
105
        return(UX_SUCCESS);
106
    }
107
108
    /* Get the LBA from the CBWCB.  */
109
395
    lba =  _ux_utility_long_get_big_endian(cbwcb + UX_SLAVE_CLASS_STORAGE_WRITE_LBA);
110
111
    /* The type of commands will tell us the width of the field containing the number
112
       of sectors to read.   */
113
395
    if (scsi_command == UX_SLAVE_CLASS_STORAGE_SCSI_WRITE16)
114
115
        /* Get the number of blocks from the CBWCB in 16 bits.  */
116
394
        total_number_blocks =  _ux_utility_short_get_big_endian(cbwcb + UX_SLAVE_CLASS_STORAGE_WRITE_TRANSFER_LENGTH_16);
117
118
    else
119
120
        /* Get the number of blocks from the CBWCB in 32 bits.  */
121
1
        total_number_blocks =  _ux_utility_long_get_big_endian(cbwcb + UX_SLAVE_CLASS_STORAGE_WRITE_TRANSFER_LENGTH_32);
122
123
    /* If trace is enabled, insert this event into the trace buffer.  */
124
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_WRITE, storage, lun, lba, total_number_blocks, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
125
126
    /* Obtain the pointer to the transfer request.  */
127
395
    transfer_request =  &endpoint_out -> ux_slave_endpoint_transfer_request;
128
129
    /* Obtain the status of the device.  */
130
395
    status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_status(storage,
131
                            lun, storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_id, &media_status);
132
133
    /* Update the request sense.  */
134
395
    storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
135
136
    /* Default CSW to failed.  */
137
395
    storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
138
139
    /* If there is a problem, return a failed command.  */
140
395
    if (status != UX_SUCCESS)
141
    {
142
143
        /* We have a problem, media status error. Return a bad completion and wait for the
144
           REQUEST_SENSE command.  */
145
#if !defined(UX_DEVICE_STANDALONE)
146
1
        _ux_device_stack_endpoint_stall(endpoint_out);
147
#endif
148
149
        /* We are done here.  */
150
1
        return(UX_ERROR);
151
    }
152
153
    /* Check Read Only flag.  */
154
394
    if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_read_only_flag == UX_TRUE)
155
    {
156
157
        /* Update the request sense.  */
158
1
        storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
159
                UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(UX_SLAVE_CLASS_STORAGE_SENSE_KEY_DATA_PROTECT,
160
                                            UX_SLAVE_CLASS_STORAGE_REQUEST_CODE_MEDIA_PROTECTED,0);
161
162
        /* We have a problem, cannot write to RO drive. Return a bad completion and wait for the
163
           REQUEST_SENSE command.  */
164
#if !defined(UX_DEVICE_STANDALONE)
165
1
        _ux_device_stack_endpoint_stall(endpoint_out);
166
#endif
167
168
        /* We are done here.  */
169
1
        return(UX_ERROR);
170
    }
171
172
    /* Compute the total length to transfer and how much remains.  */
173
393
    total_length =  total_number_blocks * storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_block_length;
174
175
#if defined(UX_DEVICE_STANDALONE)
176
177
    /* Next: Transfer (DATA) -> Disk write.  */
178
    storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
179
    storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_WRITE;
180
    storage -> ux_device_class_storage_disk_state = UX_DEVICE_CLASS_STORAGE_DISK_USB_WAIT;
181
    storage -> ux_device_class_storage_buffer_state[0] = UX_DEVICE_CLASS_STORAGE_BUFFER_EMPTY;
182
    storage -> ux_device_class_storage_buffer_state[1] = UX_DEVICE_CLASS_STORAGE_BUFFER_EMPTY;
183
    storage -> ux_device_class_storage_buffer_usb = 0;
184
    storage -> ux_device_class_storage_buffer_disk = 0;
185
186
    storage -> ux_device_class_storage_transfer = transfer_request;
187
    storage -> ux_device_class_storage_device_length = total_length;
188
    storage -> ux_device_class_storage_data_length = total_length;
189
    storage -> ux_device_class_storage_data_count = 0;
190
191
    storage -> ux_device_class_storage_cmd_lba = lba;
192
    storage -> ux_device_class_storage_cmd_n_lb = total_number_blocks;
193
194
#else
195
196
    /* Check transfer length.  */
197
198
    /* Case (3) Hn < Do.  */
199
393
    if (total_length > storage -> ux_slave_class_storage_host_length)
200
    {
201
1
        _ux_device_stack_endpoint_stall(endpoint_out);
202
1
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
203
1
        return(UX_ERROR);
204
    }
205
206
    /* Case (8). Hi <> Do.  */
207
392
    if ((storage -> ux_slave_class_storage_cbw_flags & 0x80) != 0)
208
    {
209
1
        _ux_device_stack_endpoint_stall(endpoint_in);
210
1
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
211
1
        return(UX_ERROR);
212
    }
213
214
    /* Default status to success.  */
215
391
    status =  UX_SUCCESS;
216
217
    /* It may take several transfers to send the requested data.  */
218
391
    done_length = 0;
219
750
    while (total_length)
220
    {
221
222
        /* How much can we receive in this transfer?  */
223
393
        if (total_length > UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE)
224
3
            transfer_length =  UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE;
225
        else
226
390
            transfer_length =  total_length;
227
228
        /* Get the data payload from the host.  */
229
393
        status =  _ux_device_stack_transfer_request(transfer_request, transfer_length, transfer_length);
230
231
        /* Check the status.  */
232
393
        if (status != UX_SUCCESS)
233
        {
234
235
            /* We have a problem, request error. Return a bad completion and wait for the
236
               REQUEST_SENSE command.  */
237
1
            _ux_device_stack_endpoint_stall(endpoint_out);
238
239
            /* Update residue.  */
240
1
            storage -> ux_slave_class_storage_csw_residue = storage -> ux_slave_class_storage_host_length - done_length;
241
242
            /* And update the REQUEST_SENSE codes.  */
243
1
            storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
244
                                                UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(0x02,0x54,0x00);
245
246
            /* Return an error.  */
247
1
            return(UX_ERROR);
248
        }
249
250
        /* Compute the number of blocks to transfer.  */
251
392
        number_blocks = transfer_length / storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_block_length;
252
253
        /* Execute the write command to the local media.  */
254
392
        status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_write(storage, lun, transfer_request -> ux_slave_transfer_request_data_pointer, number_blocks, lba, &media_status);
255
256
        /* If there is a problem, return a failed command.  */
257
392
        if (status != UX_SUCCESS)
258
        {
259
260
            /* We have a problem, request error. Return a bad completion and wait for the
261
               REQUEST_SENSE command.  */
262
33
            _ux_device_stack_endpoint_stall(endpoint_out);
263
264
            /* Update residue.  */
265
33
            storage -> ux_slave_class_storage_csw_residue = storage -> ux_slave_class_storage_host_length - done_length;
266
267
            /* And update the REQUEST_SENSE codes.  */
268
33
            storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
269
270
            /* Return an error.  */
271
33
            return(UX_ERROR);
272
        }
273
274
        /* Update the lba.  */
275
359
        lba += number_blocks;
276
277
        /* Update the length to remain.  */
278
359
        total_length -= transfer_length;
279
359
        done_length += transfer_length;
280
    }
281
282
    /* Update residue.  */
283
357
    storage -> ux_slave_class_storage_csw_residue = storage -> ux_slave_class_storage_host_length - done_length;
284
285
    /* Case (9), (11). If host expects more transfer, stall it.  */
286
357
    if (storage -> ux_slave_class_storage_csw_residue)
287
1
        _ux_device_stack_endpoint_stall(endpoint_out);
288
289
#endif /* else defined(UX_DEVICE_STANDALONE) */
290
291
    /* Now we set the CSW with success.  */
292
357
    storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
293
294
    /* Return completion status.  */
295
357
    return(status);
296
}