GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_storage_max_lun_get.c Lines: 24 24 100.0 %
Date: 2024-12-12 17:16:36 Branches: 10 10 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_max_lun_get                  PORTABLE C      */
37
/*                                                           6.1.10       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function retrieves the maximum number of LUNs from the device. */
45
/*                                                                        */
46
/*  INPUT                                                                 */
47
/*                                                                        */
48
/*    storage                               Pointer to storage class      */
49
/*                                                                        */
50
/*  OUTPUT                                                                */
51
/*                                                                        */
52
/*    Completion Status                                                   */
53
/*                                                                        */
54
/*  CALLS                                                                 */
55
/*                                                                        */
56
/*    _ux_host_stack_transfer_request       Process transfer request      */
57
/*    _ux_utility_memory_allocate           Allocate memory block         */
58
/*    _ux_utility_memory_free               Release memory block          */
59
/*    _ux_host_semaphore_get                Get semaphore                 */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    Storage Class                                                       */
64
/*                                                                        */
65
/*  RELEASE HISTORY                                                       */
66
/*                                                                        */
67
/*    DATE              NAME                      DESCRIPTION             */
68
/*                                                                        */
69
/*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70
/*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71
/*                                            resulting in version 6.1    */
72
/*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
73
/*                                            added standalone support,   */
74
/*                                            resulting in version 6.1.10 */
75
/*                                                                        */
76
/**************************************************************************/
77
130
UINT  _ux_host_class_storage_max_lun_get(UX_HOST_CLASS_STORAGE *storage)
78
{
79
80
UX_ENDPOINT     *control_endpoint;
81
UX_TRANSFER     *transfer_request;
82
UINT            status;
83
UCHAR           *storage_data_buffer;
84
85
86
    /* In the case of non BO device or an error on the command the number of lun should be
87
       set to 0, indicating 1 lun.  */
88
130
    storage -> ux_host_class_storage_max_lun =  0;
89
90
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
91
    /* Check the device type. */
92
    if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceProtocol == UX_HOST_CLASS_STORAGE_PROTOCOL_BO)
93
    {
94
#endif
95
96
    /* We need to get the default control endpoint transfer_request pointer.  */
97
130
    control_endpoint =  &storage -> ux_host_class_storage_device -> ux_device_control_endpoint;
98
130
    transfer_request =  &control_endpoint -> ux_endpoint_transfer_request;
99
100
    /* We need to prevent other threads from simultaneously using the control endpoint. */
101
130
    status = _ux_host_semaphore_get(&control_endpoint -> ux_endpoint_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
102
130
    if (status != UX_SUCCESS)
103
1
        return(status);
104
105
    /* Need to allocate memory for the descriptor.  */
106
129
    storage_data_buffer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, 1);
107
129
    if (storage_data_buffer == UX_NULL)
108
1
        return(UX_MEMORY_INSUFFICIENT);
109
110
    /* Create a transfer_request for the GET_MAX_LUN request.  */
111
128
    transfer_request -> ux_transfer_request_data_pointer =      storage_data_buffer;
112
128
    transfer_request -> ux_transfer_request_requested_length =  1;
113
128
    transfer_request -> ux_transfer_request_function =          UX_HOST_CLASS_STORAGE_GET_MAX_LUN;
114
128
    transfer_request -> ux_transfer_request_type =              UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
115
128
    transfer_request -> ux_transfer_request_value =             0;
116
128
    transfer_request -> ux_transfer_request_index =             storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceNumber;
117
118
#if defined(UX_HOST_STANDALONE)
119
    storage -> ux_host_class_storage_memory = storage_data_buffer;
120
    storage -> ux_host_class_storage_trans = transfer_request;
121
    storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_TRANSFER;
122
    storage -> ux_host_class_storage_state_next = UX_HOST_CLASS_STORAGE_STATE_MAX_LUN_SAVE;
123
    UX_TRANSFER_STATE_RESET(transfer_request);
124
#else
125
126
    /* Send request to HCD layer.  */
127
128
    status =  _ux_host_stack_transfer_request(transfer_request);
128
129
    /* Check for correct transfer and entire descriptor returned.  */
130

128
    if ((status == UX_SUCCESS) && (transfer_request -> ux_transfer_request_actual_length == 1))
131
    {
132
133
        /* Retrieve the number of max LUN for this device.  */
134
125
        storage -> ux_host_class_storage_max_lun =  *storage_data_buffer;
135
136
        /* Is the max LUN index greater than our LUN array's?  */
137
125
        if (storage -> ux_host_class_storage_max_lun > UX_MAX_HOST_LUN - 1)
138
        {
139
140
            /* Cap it off.  */
141
1
            storage -> ux_host_class_storage_max_lun =  UX_MAX_HOST_LUN - 1;
142
143
            /* Notify application so it knows to increase UX_MAX_HOST_LUN.  */
144
1
            _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_MEMORY_ERROR);
145
146
            /* If trace is enabled, insert this event into the trace buffer.  */
147
            UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_MEMORY_ERROR, storage, 0, 0, UX_TRACE_ERRORS, 0, 0)
148
        }
149
    }
150
151
    /* Free all used resources.  */
152
128
    _ux_utility_memory_free(storage_data_buffer);
153
#endif
154
155
#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
156
    }
157
#if defined(UX_HOST_STANDALONE)
158
    else
159
    {
160
        storage -> ux_host_class_storage_trans = UX_NULL;
161
        storage -> ux_host_class_storage_state_state = UX_HOST_CLASS_STORAGE_STATE_MAX_LUN_SAVE;
162
    }
163
#endif
164
#endif
165
166
    /* We always succeed.  */
167
128
    return(UX_SUCCESS);
168
}
169