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

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