GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_storage_read_capacity.c Lines: 17 21 81.0 %
Date: 2026-03-06 18:57:10 Branches: 3 4 75.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
33
#if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH
34
/* #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH is too small, please check  */
35
/* Build option checked runtime by UX_ASSERT  */
36
#endif
37
38
/**************************************************************************/
39
/*                                                                        */
40
/*  FUNCTION                                               RELEASE        */
41
/*                                                                        */
42
/*    _ux_device_class_storage_read_capacity              PORTABLE C      */
43
/*                                                           6.3.0        */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    Chaoqiong Xiao, Microsoft Corporation                               */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function performs a READ_CAPACITY command.                     */
51
/*                                                                        */
52
/*  INPUT                                                                 */
53
/*                                                                        */
54
/*    storage                               Pointer to storage class      */
55
/*    lun                                   Logical unit number           */
56
/*    endpoint_in                           Pointer to IN endpoint        */
57
/*    endpoint_out                          Pointer to OUT endpoint       */
58
/*    cbwcb                                 Pointer to CBWCB              */
59
/*                                                                        */
60
/*  OUTPUT                                                                */
61
/*                                                                        */
62
/*    Completion Status                                                   */
63
/*                                                                        */
64
/*  CALLS                                                                 */
65
/*                                                                        */
66
/*    _ux_device_stack_transfer_request     Transfer request              */
67
/*    _ux_device_stack_endpoint_stall       Stall endpoint                */
68
/*    _ux_utility_long_put_big_endian       Put 32-bit big endian         */
69
/*    _ux_utility_memory_set                Set memory                    */
70
/*                                                                        */
71
/*  CALLED BY                                                             */
72
/*                                                                        */
73
/*    Device Storage Class                                                */
74
/*                                                                        */
75
/**************************************************************************/
76
184
UINT  _ux_device_class_storage_read_capacity(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun,
77
                                            UX_SLAVE_ENDPOINT *endpoint_in,
78
                                            UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
79
{
80
81
UINT                    status;
82
ULONG                   media_status;
83
UX_SLAVE_TRANSFER       *transfer_request;
84
UCHAR                   *read_capacity_buffer;
85
86
    UX_PARAMETER_NOT_USED(cbwcb);
87
    UX_PARAMETER_NOT_USED(endpoint_out);
88
89
    /* Build option check.  */
90
    UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH);
91
92
    /* If trace is enabled, insert this event into the trace buffer.  */
93
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_READ_CAPACITY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
94
95
184
    if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_medium_loaded_status == 0)
96
    {
97
        /* Media not loaded. Set NOT READY sense code.  */
98
        storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
99
            UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(UX_SLAVE_CLASS_STORAGE_SENSE_KEY_NOT_READY,
100
                                                 UX_SLAVE_CLASS_STORAGE_SENSE_CODE_NOT_PRESENT, 0x00);
101
102
        /* Return CSW with failure.  */
103
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
104
105
        /* We need to STALL the IN endpoint.  The endpoint will be reset by the host.  */
106
        _ux_device_stack_endpoint_stall(endpoint_in);
107
108
        /* Return error.  */
109
        return(UX_SUCCESS);
110
    }
111
112
    /* Obtain the status of the device.  */
113
184
    status =  storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_status(storage, lun,
114
                                storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_id, &media_status);
115
116
    /* Update the request sense.  */
117
184
    storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status = media_status;
118
119
    /* Check the status for error.  */
120
184
    if (status != UX_SUCCESS)
121
    {
122
123
#if !defined(UX_DEVICE_STANDALONE)
124
125
        /* We need to STALL the IN endpoint.  The endpoint will be reset by the host.  */
126
13
        _ux_device_stack_endpoint_stall(endpoint_in);
127
#endif
128
129
        /* Now we set the CSW with Error.  */
130
13
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
131
13
        status = UX_SUCCESS;
132
    }
133
    else
134
    {
135
136
        /* Obtain the pointer to the transfer request.  */
137
171
        transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
138
139
        /* Obtain read capacity response buffer.  */
140
171
        read_capacity_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
141
142
        /* Ensure it is cleaned.  */
143
171
        _ux_utility_memory_set(read_capacity_buffer, 0, UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH); /* Use case of memcpy is verified. */
144
145
        /* Insert the last LBA address in the response.  */
146
171
        _ux_utility_long_put_big_endian(&read_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LAST_LBA],
147
                                        storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_last_lba);
148
149
        /* Insert the block length in the response.  */
150
171
        _ux_utility_long_put_big_endian(&read_capacity_buffer[UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_BLOCK_SIZE],
151
                                        storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_block_length);
152
153
#if defined(UX_DEVICE_STANDALONE)
154
155
        /* Next: Transfer (DATA).  */
156
        storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
157
        storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
158
159
        storage -> ux_device_class_storage_transfer = transfer_request;
160
        storage -> ux_device_class_storage_device_length =
161
                        UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH;
162
        storage -> ux_device_class_storage_data_length =
163
                        UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH;
164
        storage -> ux_device_class_storage_data_count = 0;
165
        UX_SLAVE_TRANSFER_STATE_RESET(storage -> ux_device_class_storage_transfer);
166
167
#else
168
169
        /* Send a data payload with the read_capacity response buffer.  */
170
171
        _ux_device_stack_transfer_request(transfer_request,
171
                                      UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH,
172
                                      UX_SLAVE_CLASS_STORAGE_READ_CAPACITY_RESPONSE_LENGTH);
173
#endif
174
175
        /* Now we set the CSW with success.  */
176
171
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
177
171
        status = UX_SUCCESS;
178
    }
179
180
    /* Return completion status.  */
181
184
    return(status);
182
}