GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_storage_request_sense.c Lines: 22 22 100.0 %
Date: 2026-03-06 18:57:10 Branches: 6 6 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_CLASS_STORAGE_BUFFER_SIZE < UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH
34
/* #error UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE 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_request_sense              PORTABLE C      */
43
/*                                                           6.3.0        */
44
/*  AUTHOR                                                                */
45
/*                                                                        */
46
/*    Chaoqiong Xiao, Microsoft Corporation                               */
47
/*                                                                        */
48
/*  DESCRIPTION                                                           */
49
/*                                                                        */
50
/*    This function performs a request sense 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_utility_memory_copy               Copy memory                   */
68
/*    _ux_utility_memory_set                Set memory                    */
69
/*                                                                        */
70
/*  CALLED BY                                                             */
71
/*                                                                        */
72
/*    Device Storage Class                                                */
73
/*                                                                        */
74
/**************************************************************************/
75
149
UINT  _ux_device_class_storage_request_sense(UX_SLAVE_CLASS_STORAGE *storage, ULONG lun, UX_SLAVE_ENDPOINT *endpoint_in,
76
                                            UX_SLAVE_ENDPOINT *endpoint_out, UCHAR * cbwcb)
77
{
78
79
149
UINT                    status = UX_SUCCESS;
80
UX_SLAVE_TRANSFER       *transfer_request;
81
UCHAR                   *sense_buffer;
82
UCHAR                   key, code, qualifier;
83
ULONG                   sense_length;
84
85
86
    UX_PARAMETER_NOT_USED(cbwcb);
87
    UX_PARAMETER_NOT_USED(endpoint_out);
88
89
    /* Build option check.  */
90
    UX_ASSERT(UX_SLAVE_CLASS_STORAGE_BUFFER_SIZE >= UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
91
92
    /* Obtain the pointer to the transfer request.  */
93
149
    transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
94
95
    /* Get length.  */
96
149
    sense_length = storage -> ux_slave_class_storage_host_length;
97
149
    if (sense_length > UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH)
98
1
        sense_length = UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
99
100
    /* Obtain sense buffer.  */
101
149
    sense_buffer = transfer_request -> ux_slave_transfer_request_data_pointer;
102
103
    /* Ensure it is cleaned.  */
104
149
    _ux_utility_memory_set(sense_buffer, 0, sense_length); /* Use case of memset is verified. */
105
106
    /* Initialize the response buffer with the error code.  */
107
149
    sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ERROR_CODE] =
108
                    UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ERROR_CODE_VALUE;
109
110
    /* Extract sense key, code, qualifier.  */
111
149
    key = UX_DEVICE_CLASS_STORAGE_SENSE_KEY(storage -> ux_slave_class_storage_lun[lun].
112
                                            ux_slave_class_storage_request_sense_status);
113
149
    code = UX_DEVICE_CLASS_STORAGE_SENSE_CODE(storage -> ux_slave_class_storage_lun[lun].
114
                                            ux_slave_class_storage_request_sense_status);
115
149
    qualifier = UX_DEVICE_CLASS_STORAGE_SENSE_QUALIFIER(storage -> ux_slave_class_storage_lun[lun].
116
                                            ux_slave_class_storage_request_sense_status);
117
118
    /* Initialize the response buffer with the sense key.  */
119
149
    sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY] = key;
120
121
    /* Initialize the response buffer with the code.  */
122
149
    sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE] = code;
123
124
    /* Initialize the response buffer with the code qualifier.  */
125
149
    sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER] = qualifier;
126
127
    /* If trace is enabled, insert this event into the trace buffer.  */
128
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_STORAGE_REQUEST_SENSE, storage, lun,
129
                            key, code, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
130
131
    /* Initialize the response buffer with the additional length.  */
132
149
    sense_buffer[UX_SLAVE_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_ADD_LENGTH] =  10;
133
134
#if defined(UX_DEVICE_STANDALONE)
135
136
    /* Next: Transfer (DATA).  */
137
    storage -> ux_device_class_storage_state = UX_DEVICE_CLASS_STORAGE_STATE_TRANS_START;
138
    storage -> ux_device_class_storage_cmd_state = UX_DEVICE_CLASS_STORAGE_CMD_READ;
139
140
    storage -> ux_device_class_storage_transfer = transfer_request;
141
    storage -> ux_device_class_storage_device_length = sense_length;
142
    storage -> ux_device_class_storage_data_length = sense_length;
143
    storage -> ux_device_class_storage_data_count = 0;
144
145
#else
146
147
    /* Send a data payload with the sense codes.  */
148
149
    if (sense_length)
149
148
        _ux_device_stack_transfer_request(transfer_request, sense_length, sense_length);
150
151
    /* Check length.  */
152
149
    if (storage -> ux_slave_class_storage_host_length != sense_length)
153
    {
154
1
        _ux_device_stack_endpoint_stall(endpoint_in);
155
1
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PHASE_ERROR;
156
    }
157
#endif
158
159
    /* Return completion status.  */
160
149
    return(status);
161
}
162