GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_storage_media_characteristics_get.c Lines: 16 16 100.0 %
Date: 2026-03-06 18:57:10 Branches: 4 4 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_media_characteristics_get    PORTABLE C      */
38
/*                                                           6.1.10       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function will send a INQUIRY command to get the type of        */
46
/*    device/media we are dealing with.                                   */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    storage                               Pointer to storage class      */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    Completion Status                                                   */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    _ux_host_class_storage_cbw_initialize Initialize CBW                */
59
/*    _ux_host_class_storage_transport      Send command                  */
60
/*    _ux_host_class_storage_media_capacity_get                           */
61
/*                                          Get media capacity            */
62
/*    _ux_utility_memory_allocate           Allocate memory block         */
63
/*    _ux_utility_memory_free               Release memory block          */
64
/*                                                                        */
65
/*  CALLED BY                                                             */
66
/*                                                                        */
67
/*    Storage Class                                                       */
68
/*                                                                        */
69
/**************************************************************************/
70
185
UINT  _ux_host_class_storage_media_characteristics_get(UX_HOST_CLASS_STORAGE *storage)
71
{
72
73
UINT            status;
74
UCHAR           *cbw;
75
UCHAR           *inquiry_response;
76
UINT            command_length;
77
78
    /* Use a pointer for the cbw, easier to manipulate.  */
79
185
    cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
80
81
    /* Get the Write Command Length.  */
82
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
83
    if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
84
        command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_UFI;
85
    else
86
        command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
87
#else
88
185
    command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
89
#endif
90
91
    /* Initialize the CBW for this command.  */
92
185
    _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH, command_length);
93
94
    /* Prepare the INQUIRY command block.  */
95
185
    *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_INQUIRY;
96
97
    /* Store the length of the Inquiry Response.  */
98
185
    *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_ALLOCATION_LENGTH) =  UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH;
99
100
    /* Obtain a block of memory for the answer.  */
101
185
    inquiry_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH);
102
185
    if (inquiry_response == UX_NULL)
103
5
        return(UX_MEMORY_INSUFFICIENT);
104
105
#if defined(UX_HOST_STANDALONE)
106
    UX_HOST_CLASS_STORAGE_TRANS_STATE_RESET(storage);
107
    storage -> ux_host_class_storage_memory = inquiry_response;
108
    storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_TRANSPORT;
109
    storage -> ux_host_class_storage_state_next = UX_HOST_CLASS_STORAGE_STATE_INQUIRY_SAVE;
110
    storage -> ux_host_class_storage_trans_data = inquiry_response;
111
    status = UX_SUCCESS;
112
    return(status);
113
#else
114
    /* Send the command to transport layer.  */
115
180
    status =  _ux_host_class_storage_transport(storage, inquiry_response);
116
117
    /* If we have a transport error, there is not much we can do, simply return the
118
       error.  */
119
180
    if (status == UX_SUCCESS)
120
    {
121
122
        /* The Inquiry response contains the type of device/media and a media removable flag.  */
123
178
        storage -> ux_host_class_storage_media_type =                                                       *(inquiry_response + UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_PERIPHERAL_TYPE);
124
178
        storage -> ux_host_class_storage_lun_removable_media_flags[storage -> ux_host_class_storage_lun] =  *(inquiry_response + UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_REMOVABLE_MEDIA);
125
126
        /* Attempt to read the device capacity in order to retrieve the Sector Size. If the command fails,
127
           we will default to 512 bytes for a regular drive and 2048 bytes for a CD-ROM or optical drive.  */
128
178
        status =  _ux_host_class_storage_media_capacity_get(storage);
129
    }
130
131
    /* Free the memory resource used for the command response.  */
132
180
    _ux_utility_memory_free(inquiry_response);
133
134
    /* Return completion status.  */
135
180
    return(status);
136
#endif
137
}
138