GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_device_classes/src/ux_device_class_storage_control_request.c Lines: 36 36 100.0 %
Date: 2026-03-06 18:57:10 Branches: 11 11 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
/**   Device Storage Class                                                */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
#define UX_SOURCE_CODE
24
25
26
/* Include necessary system files.  */
27
28
#include "ux_api.h"
29
#include "ux_device_class_storage.h"
30
#include "ux_device_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_device_class_storage_control_request            PORTABLE C      */
38
/*                                                           6.1.12       */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function manages the based sent by the host on the control     */
46
/*    endpoints with a CLASS or VENDOR SPECIFIC type.                     */
47
/*                                                                        */
48
/*  INPUT                                                                 */
49
/*                                                                        */
50
/*    storage                               Pointer to storage class      */
51
/*                                                                        */
52
/*  OUTPUT                                                                */
53
/*                                                                        */
54
/*    None                                                                */
55
/*                                                                        */
56
/*  CALLS                                                                 */
57
/*                                                                        */
58
/*    _ux_device_stack_transfer_request     Transfer request              */
59
/*    _ux_device_stack_transfer_abort       Abort Transfer                */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    Device Storage Class                                                */
64
/*                                                                        */
65
/**************************************************************************/
66
407
UINT  _ux_device_class_storage_control_request(UX_SLAVE_CLASS_COMMAND *command)
67
{
68
69
UX_SLAVE_TRANSFER           *transfer_request;
70
UX_SLAVE_DEVICE             *device;
71
UX_SLAVE_CLASS              *class_ptr;
72
ULONG                       request;
73
ULONG                       request_value;
74
ULONG                       request_length;
75
UX_SLAVE_CLASS_STORAGE      *storage;
76
#if !defined(UX_DEVICE_STANDALONE)
77
UX_SLAVE_INTERFACE          *interface_ptr;
78
#endif
79
UX_SLAVE_ENDPOINT           *endpoint_in;
80
UX_SLAVE_ENDPOINT           *endpoint_out;
81
82
83
    /* Get the pointer to the device.  */
84
407
    device =  &_ux_system_slave -> ux_system_slave_device;
85
86
    /* Get the pointer to the transfer request associated with the control endpoint.  */
87
407
    transfer_request =  &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
88
89
    /* Extract the request type from the SETUP packet..   */
90
407
    request =  *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_REQUEST);
91
407
    request_value = _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_VALUE);
92
407
    request_length = _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_LENGTH);
93
94
    /* Check if wValue is valid.  */
95
407
    if (request_value != 0)
96
2
        return(UX_ERROR);
97
98
    /* Get the class container.  */
99
405
    class_ptr =  command -> ux_slave_class_command_class_ptr;
100
101
    /* Get the storage instance from this class container.  */
102
405
    storage =  (UX_SLAVE_CLASS_STORAGE *) class_ptr -> ux_slave_class_instance;
103
104
    /* Here we proceed only the standard request we know of at the device level.  */
105
405
    switch (request)
106
    {
107
108
278
    case UX_SLAVE_CLASS_STORAGE_RESET:
109
110
        /* Check if wLength is valid.  */
111
278
        if (request_length != 0)
112
1
            return(UX_ERROR);
113
114
#if defined(UX_DEVICE_STANDALONE)
115
        endpoint_in = storage -> ux_device_class_storage_ep_in;
116
        endpoint_out = storage -> ux_device_class_storage_ep_out;
117
#else
118
119
        /* We need the interface to the class.  */
120
277
        interface_ptr =  storage -> ux_slave_class_storage_interface;
121
122
        /* Locate the endpoints.  */
123
277
        endpoint_in =  interface_ptr -> ux_slave_interface_first_endpoint;
124
125
        /* Check the endpoint direction, if IN we have the correct endpoint.  */
126
277
        if ((endpoint_in -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) != UX_ENDPOINT_IN)
127
        {
128
129
            /* Wrong direction, we found the OUT endpoint first.  */
130
16
            endpoint_out =  endpoint_in;
131
132
            /* So the next endpoint has to be the IN endpoint.  */
133
16
            endpoint_in =  endpoint_out -> ux_slave_endpoint_next_endpoint;
134
        }
135
        else
136
        {
137
138
            /* We found the endpoint IN first, so next endpoint is OUT.  */
139
261
            endpoint_out =  endpoint_in -> ux_slave_endpoint_next_endpoint;
140
        }
141
#endif
142
143
        /* First cancel any transfer on the endpoint OUT, from the host.  */
144
277
        transfer_request =  &endpoint_out -> ux_slave_endpoint_transfer_request;
145
277
        _ux_device_stack_transfer_abort(transfer_request, UX_TRANSFER_APPLICATION_RESET);
146
147
        /* Then cancel any transfer on the endpoint IN, from the host.  */
148
277
        transfer_request =  &endpoint_in -> ux_slave_endpoint_transfer_request;
149
277
        _ux_device_stack_transfer_abort(transfer_request, UX_TRANSFER_APPLICATION_RESET);
150
151
        /* Reset phase error.  */
152
277
        storage -> ux_slave_class_storage_csw_status = UX_SLAVE_CLASS_STORAGE_CSW_PASSED;
153
154
277
        break;
155
156
126
    case UX_SLAVE_CLASS_STORAGE_GET_MAX_LUN:
157
158
        /* Check if wLength is valid.  */
159
126
        if (request_length < 1)
160
1
            return(UX_ERROR);
161
162
        /* Set the value of the number of LUN in the buffer. The max number of LUN is the
163
           number of declared LUN - 1.  */
164
125
        *transfer_request -> ux_slave_transfer_request_data_pointer =  (UCHAR)(storage -> ux_slave_class_storage_number_lun -1);
165
166
        /* Set the phase of the transfer to data out.  */
167
125
        transfer_request -> ux_slave_transfer_request_phase =  UX_TRANSFER_PHASE_DATA_OUT;
168
169
        /* We can return the LUN number.  */
170
125
        _ux_device_stack_transfer_request(transfer_request, 1, 1);
171
125
        break;
172
173
1
    default:
174
175
        /* Unknown function. It's not handled.  */
176
1
        return(UX_ERROR);
177
    }
178
179
    /* It's handled.  */
180
402
    return(UX_SUCCESS);
181
}