GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: core/src/ux_device_stack_set_feature.c Lines: 24 24 100.0 %
Date: 2026-03-06 18:57:10 Branches: 13 13 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 Stack                                                        */
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_stack.h"
30
31
32
/**************************************************************************/
33
/*                                                                        */
34
/*  FUNCTION                                               RELEASE        */
35
/*                                                                        */
36
/*    _ux_device_stack_set_feature                        PORTABLE C      */
37
/*                                                           6.1.12       */
38
/*  AUTHOR                                                                */
39
/*                                                                        */
40
/*    Chaoqiong Xiao, Microsoft Corporation                               */
41
/*                                                                        */
42
/*  DESCRIPTION                                                           */
43
/*                                                                        */
44
/*    This function sets a specific feature (Device, Interface,           */
45
/*    Endpoint ....).                                                     */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    request_type                          Request type                  */
50
/*    request_value                         Request value                 */
51
/*    request_index                         Request index                 */
52
/*                                                                        */
53
/*  OUTPUT                                                                */
54
/*                                                                        */
55
/*    Completion Status                                                   */
56
/*                                                                        */
57
/*  CALLS                                                                 */
58
/*                                                                        */
59
/*    (ux_slave_dcd_function)               DCD controller function       */
60
/*                                                                        */
61
/*  CALLED BY                                                             */
62
/*                                                                        */
63
/*    Device Stack                                                        */
64
/*                                                                        */
65
/**************************************************************************/
66
9
UINT  _ux_device_stack_set_feature(ULONG request_type, ULONG request_value, ULONG request_index)
67
{
68
69
UX_SLAVE_DCD            *dcd;
70
UX_SLAVE_DEVICE         *device;
71
UX_SLAVE_INTERFACE      *interface_ptr;
72
UX_SLAVE_ENDPOINT       *endpoint;
73
UX_SLAVE_ENDPOINT       *endpoint_target;
74
75
    UX_PARAMETER_NOT_USED(request_value);
76
77
    /* If trace is enabled, insert this event into the trace buffer.  */
78
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_STACK_SET_FEATURE, request_value, request_index, 0, 0, UX_TRACE_DEVICE_STACK_EVENTS, 0, 0)
79
80
    /* Get the pointer to the DCD.  */
81
9
    dcd =  &_ux_system_slave -> ux_system_slave_dcd;
82
83
    /* Get the pointer to the device.  */
84
9
    device =  &_ux_system_slave -> ux_system_slave_device;
85
86
    /* Get the control endpoint for the device.  */
87
9
    endpoint =  &device -> ux_slave_device_control_endpoint;
88
89
    /* The feature can be for either the device or the endpoint.  */
90
9
    switch (request_type & UX_REQUEST_TARGET)
91
    {
92
93
6
    case UX_REQUEST_TARGET_DEVICE:
94
95
        /* Check if we have a DEVICE_REMOTE_WAKEUP Feature.  */
96
6
        if (request_value == UX_REQUEST_FEATURE_DEVICE_REMOTE_WAKEUP)
97
        {
98
99
            /* Check if we have the capability. */
100
3
            if (_ux_system_slave -> ux_system_slave_remote_wakeup_capability)
101
            {
102
103
                /* Enable the feature. */
104
2
                _ux_system_slave -> ux_system_slave_remote_wakeup_enabled = UX_TRUE;
105
106
                /* OK. */
107
2
                return (UX_SUCCESS);
108
            }
109
            else
110
111
                /* Protocol error. */
112
1
                return (UX_FUNCTION_NOT_SUPPORTED);
113
        }
114
115
#ifdef UX_OTG_SUPPORT
116
        /* Check if we have a A_HNP_SUPPORT Feature. This is set when the Host is HNP capable. */
117
        if (request_value == UX_OTG_FEATURE_A_HNP_SUPPORT)
118
        {
119
120
            /* Store the A_HNP_SUPPORT flag.  */
121
            _ux_system_otg -> ux_system_otg_slave_set_feature_flag |= UX_OTG_FEATURE_A_HNP_SUPPORT;
122
123
            /* OK.  */
124
            return(UX_SUCCESS);
125
        }
126
127
        /* Check if the host asks us to perform HNP.  If also we become the host.  */
128
        if (request_value == UX_OTG_FEATURE_B_HNP_ENABLE)
129
        {
130
131
            /* The ISR will pick up the suspend event and check if we need to become IDLE or HOST.  */
132
            _ux_system_otg -> ux_system_otg_slave_set_feature_flag |= UX_OTG_FEATURE_B_HNP_ENABLE;
133
134
            /* OK.  */
135
            return(UX_SUCCESS);
136
        }
137
#endif
138
139
        /* Request value not supported.  */
140
3
        return(UX_FUNCTION_NOT_SUPPORTED);
141
142
2
    case UX_REQUEST_TARGET_ENDPOINT:
143
144
        /* The only set feature for endpoint is ENDPOINT_STALL. This forces
145
           the endpoint to the stall situation.
146
           We need to find the endpoint through the interface(s). */
147
2
        interface_ptr =  device -> ux_slave_device_first_interface;
148
149
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
150
5
        while (interface_ptr != UX_NULL)
151
        {
152
#endif
153
            /* Get the first endpoint for this interface.  */
154
4
            endpoint_target =  interface_ptr -> ux_slave_interface_first_endpoint;
155
156
            /* Parse all the endpoints.  */
157
9
            while (endpoint_target != UX_NULL)
158
            {
159
160
                /* Check the endpoint index.  */
161
6
                if (endpoint_target -> ux_slave_endpoint_descriptor.bEndpointAddress == request_index)
162
                {
163
164
                    /* Stall the endpoint.  */
165
1
                    dcd -> ux_slave_dcd_function(dcd, UX_DCD_STALL_ENDPOINT, endpoint_target);
166
167
                    /* Return the function status.  */
168
1
                    return(UX_SUCCESS);
169
                }
170
171
                /* Next endpoint.  */
172
5
                endpoint_target =  endpoint_target -> ux_slave_endpoint_next_endpoint;
173
            }
174
175
#if !defined(UX_DEVICE_INITIALIZE_FRAMEWORK_SCAN_DISABLE) || UX_MAX_DEVICE_INTERFACES > 1
176
            /* Next interface.  */
177
3
            interface_ptr =  interface_ptr -> ux_slave_interface_next_interface;
178
        }
179
#endif
180
181
        /* We get here when the endpoint is wrong. Should not happen though.  */
182
        /* Intentionally fall through into the default case. */
183
        /* fall through */
184
    default:
185
186
        /* We stall the command.  */
187
2
        dcd -> ux_slave_dcd_function(dcd, UX_DCD_STALL_ENDPOINT, endpoint);
188
189
        /* No more work to do here.  The command failed but the upper layer does not depend on it.  */
190
2
        return(UX_SUCCESS);
191
    }
192
}