GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_storage_request_sense.c Lines: 20 20 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
/**   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_host_class_storage.h"
30
#include "ux_host_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_host_class_storage_request_sense                PORTABLE C      */
38
/*                                                           6.1.10       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function will send a request sense to the device to see what   */
46
/*    error happened during the last command. Request sense commands      */
47
/*    cannot be nested.                                                   */
48
/*                                                                        */
49
/*  INPUT                                                                 */
50
/*                                                                        */
51
/*    storage                               Pointer to storage class      */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    _ux_host_class_storage_cbw_initialize Initialize CBW                */
60
/*    _ux_host_class_storage_transport      Send command                  */
61
/*    _ux_utility_memory_allocate           Allocate memory block         */
62
/*    _ux_utility_memory_copy               Copy memory block             */
63
/*    _ux_utility_memory_free               Release memory block          */
64
/*                                                                        */
65
/*  CALLED BY                                                             */
66
/*                                                                        */
67
/*    Storage Class                                                       */
68
/*                                                                        */
69
/**************************************************************************/
70
198
UINT  _ux_host_class_storage_request_sense(UX_HOST_CLASS_STORAGE *storage)
71
{
72
73
UCHAR           *cbw;
74
UCHAR           *request_sense_response;
75
UINT            command_length;
76
#if !defined(UX_HOST_STANDALONE)
77
UINT            status;
78
ULONG           sense_code;
79
#endif
80
81
    /* If trace is enabled, insert this event into the trace buffer.  */
82
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_REQUEST_SENSE, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
83
84
    /* Clear the former sense code value.  */
85
198
    storage -> ux_host_class_storage_sense_code =  0;
86
87
    /* Use a pointer for the cbw, easier to manipulate.  */
88
198
    cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
89
90
    /* Get the Request Sense Command Length.  */
91
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
92
    if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
93
        command_length =  UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_UFI;
94
    else
95
        command_length =  UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
96
#else
97
198
    command_length =  UX_HOST_CLASS_STORAGE_REQUEST_SENSE_COMMAND_LENGTH_SBC;
98
#endif
99
100
    /* Check of we are reentering a REQUEST SENSE command which is illegal.  */
101
198
    if (*(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) == UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE)
102
1
        return(UX_ERROR);
103
104
    /* Save the current command so that we can re-initiate it after the
105
       REQUEST_SENSE command.  */
106
197
    _ux_utility_memory_copy(storage -> ux_host_class_storage_saved_cbw, storage -> ux_host_class_storage_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH); /* Use case of memcpy is verified. */
107
108
    /* Initialize the CBW for this command.  */
109
197
    _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH, command_length);
110
111
    /* Prepare the REQUEST SENSE command block.  */
112
197
    *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_REQUEST_SENSE;
113
114
    /* Store the length of the Request Sense Response.  */
115
197
    *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_REQUEST_SENSE_ALLOCATION_LENGTH) =  UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH;
116
117
    /* Obtain a block of memory for the answer.  */
118
197
    request_sense_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_LENGTH);
119
197
    if (request_sense_response == UX_NULL)
120
1
        return(UX_MEMORY_INSUFFICIENT);
121
122
#if defined(UX_HOST_STANDALONE)
123
124
    /* Prepare data buffer for request sense.  */
125
    storage -> ux_host_class_storage_trans_data_bak = storage -> ux_host_class_storage_trans_data;
126
    storage -> ux_host_class_storage_trans_data = request_sense_response;
127
128
    /* Perform CBW-DATA-CSW transport.  */
129
    storage -> ux_host_class_storage_trans_state = UX_HOST_CLASS_STORAGE_TRANS_CBW;
130
131
    return(UX_SUCCESS);
132
#else
133
134
    /* Send the command to transport layer.  */
135
196
    status =  _ux_host_class_storage_transport(storage, request_sense_response);
136
137
    /* If we have a transport error, there is not much we can do, simply return the error.  */
138
196
    if (status == UX_SUCCESS)
139
    {
140
141
        /* We have a successful transaction, even though the sense code could reflect an error. The sense code
142
           will be assembled and store in the device instance.  */
143
192
        sense_code = UX_HOST_CLASS_STORAGE_SENSE_STATUS(
144
            (ULONG) *(request_sense_response +
145
                      UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_SENSE_KEY),
146
            (ULONG) *(request_sense_response +
147
                      UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE),
148
            (ULONG) *(request_sense_response +
149
                      UX_HOST_CLASS_STORAGE_REQUEST_SENSE_RESPONSE_CODE_QUALIFIER));
150
151
        /* Store the sense code in the storage instance.  */
152
192
        storage -> ux_host_class_storage_sense_code =  sense_code;
153
    }
154
155
    /* Free the memory resource used for the command response.  */
156
196
    _ux_utility_memory_free(request_sense_response);
157
158
    /* Restore the current CBW command.  */
159
196
    _ux_utility_memory_copy(storage -> ux_host_class_storage_cbw, storage -> ux_host_class_storage_saved_cbw, UX_HOST_CLASS_STORAGE_CBW_LENGTH); /* Use case of memcpy is verified. */
160
161
    /* Return completion code.  */
162
196
    return(status);
163
#endif
164
}
165