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: 2024-12-12 17:16:36 Branches: 4 4 100.0 %

Line Branch Exec Source
1
/***************************************************************************
2
 * Copyright (c) 2024 Microsoft Corporation
3
 *
4
 * This program and the accompanying materials are made available under the
5
 * terms of the MIT License which is available at
6
 * https://opensource.org/licenses/MIT.
7
 *
8
 * SPDX-License-Identifier: MIT
9
 **************************************************************************/
10
11
12
/**************************************************************************/
13
/**************************************************************************/
14
/**                                                                       */
15
/** USBX Component                                                        */
16
/**                                                                       */
17
/**   Storage Class                                                       */
18
/**                                                                       */
19
/**************************************************************************/
20
/**************************************************************************/
21
22
23
/* Include necessary system files.  */
24
25
#define UX_SOURCE_CODE
26
27
#include "ux_api.h"
28
#include "ux_host_class_storage.h"
29
#include "ux_host_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_host_class_storage_media_characteristics_get    PORTABLE C      */
37
/*                                                           6.1.10       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function will send a INQUIRY command to get the type of        */
45
/*    device/media we are dealing with.                                   */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    storage                               Pointer to storage class      */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    Completion Status                                                   */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _ux_host_class_storage_cbw_initialize Initialize CBW                */
58
/*    _ux_host_class_storage_transport      Send command                  */
59
/*    _ux_host_class_storage_media_capacity_get                           */
60
/*                                          Get media capacity            */
61
/*    _ux_utility_memory_allocate           Allocate memory block         */
62
/*    _ux_utility_memory_free               Release memory block          */
63
/*                                                                        */
64
/*  CALLED BY                                                             */
65
/*                                                                        */
66
/*    Storage Class                                                       */
67
/*                                                                        */
68
/*  RELEASE HISTORY                                                       */
69
/*                                                                        */
70
/*    DATE              NAME                      DESCRIPTION             */
71
/*                                                                        */
72
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74
/*                                            resulting in version 6.1    */
75
/*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
76
/*                                            added standalone support,   */
77
/*                                            resulting in version 6.1.10 */
78
/*                                                                        */
79
/**************************************************************************/
80
184
UINT  _ux_host_class_storage_media_characteristics_get(UX_HOST_CLASS_STORAGE *storage)
81
{
82
83
UINT            status;
84
UCHAR           *cbw;
85
UCHAR           *inquiry_response;
86
UINT            command_length;
87
88
    /* Use a pointer for the cbw, easier to manipulate.  */
89
184
    cbw =  (UCHAR *) storage -> ux_host_class_storage_cbw;
90
91
    /* Get the Write Command Length.  */
92
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
93
    if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_STORAGE_SUBCLASS_UFI)
94
        command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_UFI;
95
    else
96
        command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
97
#else
98
184
    command_length =  UX_HOST_CLASS_STORAGE_INQUIRY_COMMAND_LENGTH_SBC;
99
#endif
100
101
    /* Initialize the CBW for this command.  */
102
184
    _ux_host_class_storage_cbw_initialize(storage, UX_HOST_CLASS_STORAGE_DATA_IN, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH, command_length);
103
104
    /* Prepare the INQUIRY command block.  */
105
184
    *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_OPERATION) =  UX_HOST_CLASS_STORAGE_SCSI_INQUIRY;
106
107
    /* Store the length of the Inquiry Response.  */
108
184
    *(cbw + UX_HOST_CLASS_STORAGE_CBW_CB + UX_HOST_CLASS_STORAGE_INQUIRY_ALLOCATION_LENGTH) =  UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH;
109
110
    /* Obtain a block of memory for the answer.  */
111
184
    inquiry_response =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_LENGTH);
112
184
    if (inquiry_response == UX_NULL)
113
5
        return(UX_MEMORY_INSUFFICIENT);
114
115
#if defined(UX_HOST_STANDALONE)
116
    UX_HOST_CLASS_STORAGE_TRANS_STATE_RESET(storage);
117
    storage -> ux_host_class_storage_memory = inquiry_response;
118
    storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_TRANSPORT;
119
    storage -> ux_host_class_storage_state_next = UX_HOST_CLASS_STORAGE_STATE_INQUIRY_SAVE;
120
    storage -> ux_host_class_storage_trans_data = inquiry_response;
121
    status = UX_SUCCESS;
122
    return(status);
123
#else
124
    /* Send the command to transport layer.  */
125
179
    status =  _ux_host_class_storage_transport(storage, inquiry_response);
126
127
    /* If we have a transport error, there is not much we can do, simply return the
128
       error.  */
129
179
    if (status == UX_SUCCESS)
130
    {
131
132
        /* The Inquiry response contains the type of device/media and a media removable flag.  */
133
177
        storage -> ux_host_class_storage_media_type =                                                       *(inquiry_response + UX_HOST_CLASS_STORAGE_INQUIRY_RESPONSE_PERIPHERAL_TYPE);
134
177
        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);
135
136
        /* Attempt to read the device capacity in order to retrieve the Sector Size. If the command fails,
137
           we will default to 512 bytes for a regular drive and 2048 bytes for a CD-ROM or optical drive.  */
138
177
        status =  _ux_host_class_storage_media_capacity_get(storage);
139
    }
140
141
    /* Free the memory resource used for the command response.  */
142
179
    _ux_utility_memory_free(inquiry_response);
143
144
    /* Return completion status.  */
145
179
    return(status);
146
#endif
147
}
148