GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_storage_inquiry.c Lines: 53 53 100.0 %
Date: 2026-03-06 18:57:10 Branches: 21 21 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
33
#if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < 24
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_inquiry                    PORTABLE C      */
43
/*                                                           6.3.0        */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    Chaoqiong Xiao, Microsoft Corporation                               */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function performs a INQUIRY command.                           */
51
/*                                                                        */
52
/*  INPUT                                                                 */
53
/*                                                                        */
54
/*    storage                               Pointer to storage class      */
55
/*    endpoint_in                           Pointer to IN endpoint        */
56
/*    endpoint_out                          Pointer to OUT endpoint       */
57
/*    cbwcb                                 Pointer to CBWCB              */
58
/*                                                                        */
59
/*  OUTPUT                                                                */
60
/*                                                                        */
61
/*    Completion Status                                                   */
62
/*                                                                        */
63
/*  CALLS                                                                 */
64
/*                                                                        */
65
/*    _ux_device_class_storage_csw_send     Send CSW                      */
66
/*    _ux_device_stack_transfer_request     Transfer request              */
67
/*    _ux_device_stack_endpoint_stall       Stall endpoint                */
68
/*    _ux_utility_memory_copy               Copy memory                   */
69
/*    _ux_utility_memory_set                Set memory                    */
70
/*    _ux_utility_short_put_big_endian      Put 16-bit big endian         */
71
/*                                                                        */
72
/*  CALLED BY                                                             */
73
/*                                                                        */
74
/*    Device Storage Class                                                */
75
/*                                                                        */
76
/**************************************************************************/
77
192
UINT  _ux_device_class_storage_inquiry(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, UX_SLAVE_ENDPOINT *endpoint_in,
78
                                            UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
79
{
80
81
192
UINT                    status = UX_SUCCESS;
82
UX_SLAVE_TRANSFER       *transfer_request;
83
UCHAR                   inquiry_page_code;
84
ULONG                   inquiry_length;
85
UCHAR                   *inquiry_buffer;
86
87
    UX_PARAMETER_NOT_USED(endpoint_out);
88
89
    /* Build option check.  */
90
    UX_ASSERT(UX_SLAVE_REQUEST_DATA_MAX_LENGTH >= 24);
91
92
    /* If trace is enabled, insert this event into the trace buffer.  */
93
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_INQUIRY, storage, lun, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
94
95
#if !defined(UX_DEVICE_STANDALONE)
96
97
    /* Check direction.  */
98
192
    if (storage -> ux_slave_class_storage_host_length &&
99
191
        (storage -> ux_slave_class_storage_cbw_flags & 0x80) == 0)
100
    {
101
1
        _ux_device_stack_endpoint_stall(endpoint_out);
102
1
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
103
1
        return(UX_ERROR);
104
    }
105
#endif
106
107
    /* From the SCSI Inquiry payload, get the page code.  */
108
191
    inquiry_page_code =  *(cbwcb + UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE);
109
110
    /* And the length to be returned. */
111
191
    inquiry_length =  storage -> ux_slave_class_storage_host_length;
112
113
    /* Obtain the pointer to the transfer request.  */
114
191
    transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
115
116
    /* Obtain inquiry buffer pointer.  */
117
191
    inquiry_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
118
119
    /* Ensure the data buffer is cleaned.  */
120
191
    _ux_utility_memory_set(inquiry_buffer, 0, UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH); /* Use case of memset is verified. */
121
122
    /* Check for the maximum length to be returned. */
123
191
    if (inquiry_length > UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH)
124
1
        inquiry_length = UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH;
125
126
    /* Default CSW to passed.  */
127
191
    storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
128
129
    /* Ensure we know about the page code.  */
130
191
    switch (inquiry_page_code)
131
    {
132
133
187
    case UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE_STANDARD:
134
135
        /* Store the product type.  */
136
187
        inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PERIPHERAL_TYPE] =  (UCHAR)storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_type;
137
138
        /* Store the Media Removable bit.  */
139
187
        inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_REMOVABLE_MEDIA] =  (UCHAR)storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_removable_flag;
140
141
        /* Store the Data Format bit.  */
142
187
        if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_type == UX_SLAVE_CLASS_STORAGE_MEDIA_CDROM)
143
5
            inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_DATA_FORMAT] =  0x32;
144
        else
145
182
            inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_DATA_FORMAT] =  0x00;
146
147
        /* Store the length of the response.  There is a hack here. For CD-ROM, the data lg is fixed to 0x5B !  */
148
187
        if (storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_media_type != UX_SLAVE_CLASS_STORAGE_MEDIA_CDROM)
149
182
            inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_ADDITIONAL_LENGTH] =  UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH;
150
        else
151
5
            inquiry_buffer[UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_ADDITIONAL_LENGTH] =  UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH_CD_ROM;
152
153
        /* Fill in the storage vendor ID.  */
154
187
        _ux_utility_memory_copy(inquiry_buffer + UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_VENDOR_INFORMATION,
155
187
                                                                    storage -> ux_slave_class_storage_vendor_id, 8); /* Use case of memcpy is verified. */
156
157
        /* Fill in the product vendor ID.  */
158
187
        _ux_utility_memory_copy(inquiry_buffer + UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PRODUCT_ID,
159
187
                                                                    storage -> ux_slave_class_storage_product_id, 16); /* Use case of memcpy is verified. */
160
161
        /* Fill in the product revision number.  */
162
187
        _ux_utility_memory_copy(inquiry_buffer + UX_SLAVE_CLASS_STORAGE_INQUIRY_RESPONSE_PRODUCT_REVISION,
163
187
                                                                    storage -> ux_slave_class_storage_product_rev, 4); /* Use case of memcpy is verified. */
164
165
187
        break;
166
167
3
    case UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE_SERIAL:
168
169
        /* Initialize the page code in response buffer.  */
170
3
        _ux_utility_short_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer, UX_SLAVE_CLASS_STORAGE_INQUIRY_PAGE_CODE_SERIAL);
171
172
        /* Initialize the length of the serial number in response buffer.  */
173
3
        _ux_utility_short_put_big_endian(transfer_request -> ux_slave_transfer_request_data_pointer + 2, 20);
174
175
        /* Copy the serial number buffer into the transfer request memory.  */
176
3
        _ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer + 4, storage -> ux_slave_class_storage_product_serial, 20); /* Use case of memcpy is verified. */
177
178
        /* Send a data payload with the inquiry response buffer.  */
179
3
        if (inquiry_length > 24)
180
2
            inquiry_length = 24;
181
182
3
        break;
183
184
1
    default:
185
186
#if !defined(UX_DEVICE_STANDALONE)
187
        /* The page code is not supported.  */
188
1
        _ux_device_stack_endpoint_stall(endpoint_in);
189
#endif
190
191
        /* And update the REQUEST_SENSE codes.  */
192
1
        storage -> ux_slave_class_storage_lun[lun].ux_slave_class_storage_request_sense_status =
193
                                               UX_DEVICE_CLASS_STORAGE_SENSE_STATUS(0x05,0x26,0x01);
194
195
        /* Now we set the CSW with failure.  */
196
1
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_FAILED;
197
198
        /* Return error.  */
199
1
        status =  UX_ERROR;
200
201
1
        break;
202
    }
203
204
    /* Error cases.  */
205
191
    if (status != UX_SUCCESS)
206
1
        return(status);
207
208
#if defined(UX_DEVICE_STANDALONE)
209
210
    /* Next: Transfer (DATA).  */
211
    storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
212
    storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
213
214
    storage -> ux_device_class_storage_transfer = transfer_request;
215
    storage -> ux_device_class_storage_device_length = inquiry_length;
216
    storage -> ux_device_class_storage_data_length = inquiry_length;
217
    storage -> ux_device_class_storage_data_count = 0;
218
219
#else
220
221
    /* Send a data payload with the inquiry response buffer.  */
222
190
    if (inquiry_length)
223
189
        _ux_device_stack_transfer_request(transfer_request, inquiry_length, inquiry_length);
224
225
    /* Check length.  */
226
190
    if (storage -> ux_slave_class_storage_host_length != inquiry_length)
227
    {
228
3
        storage -> ux_slave_class_storage_csw_residue = storage -> ux_slave_class_storage_host_length - inquiry_length;
229
3
        _ux_device_stack_endpoint_stall(endpoint_in);
230
    }
231
#endif
232
233
    /* Return completion status.  */
234
190
    return(status);
235
}
236