summaryrefslogtreecommitdiff
path: root/common/core/src/ux_host_stack_interface_setting_select.c
blob: bb1a2ec1b4f8c110371e63f43b17623f69b31ddb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
/***************************************************************************
 * Copyright (c) 2024 Microsoft Corporation
 * Copyright (c) 2026-present Eclipse ThreadX contributors
 *
 * This program and the accompanying materials are made available under the
 * terms of the MIT License which is available at
 * https://opensource.org/licenses/MIT.
 *
 * SPDX-License-Identifier: MIT
 **************************************************************************/


/**************************************************************************/
/**************************************************************************/
/**                                                                       */
/** USBX Component                                                        */
/**                                                                       */
/**   Host Stack                                                          */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

#include "ux_api.h"
#include "ux_host_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_stack_interface_setting_select             PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function selects a specific alternate setting for a given      */
/*    interface belonging to the selected configuration.                  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    interface                             Pointer to interface          */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_interface_instance_create  Create interface instance */
/*    _ux_host_stack_interface_instance_delete  Delete interface instance */
/*    _ux_host_stack_interface_set              Set interface instance    */
/*    _ux_host_semaphore_get                    Get semaphore             */
/*    _ux_host_semaphore_put                    Put semaphore             */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*    USBX Components                                                     */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_stack_interface_setting_select(UX_INTERFACE *interface_ptr)
{

UX_CONFIGURATION    *configuration;
UX_INTERFACE        *current_interface;
UX_INTERFACE        *previous_interface;
UX_INTERFACE        *main_interface;
UINT                current_interface_number;
UINT                current_alternate_setting;
UINT                status;

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_STACK_INTERFACE_SETTING_SELECT, interface_ptr, 0, 0, 0, UX_TRACE_HOST_STACK_EVENTS, 0, 0)

    /* Check this alternate setting container. It must be valid before
       we continue.  */
    if (interface_ptr -> ux_interface_handle != (ULONG) (ALIGN_TYPE) interface_ptr)
    {
        /* If trace is enabled, insert this event into the trace buffer.  */
        UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_INTERFACE_HANDLE_UNKNOWN, interface_ptr, 0, 0, UX_TRACE_ERRORS, 0, 0)

        return(UX_INTERFACE_HANDLE_UNKNOWN);
    }

    /* From the interface, get the configuration container and the first
       interface hooked to this configuration.  */
    configuration =             interface_ptr -> ux_interface_configuration;
    current_interface_number =  interface_ptr -> ux_interface_descriptor.bInterfaceNumber;
    current_interface =         configuration -> ux_configuration_first_interface;

    /* Remember the main interface to store the next alternate setting.  We set the main interface
       to the first interface to keep the compiler happy. */
    main_interface =  current_interface;

    /* In order to keep the compiler happy, we reset the alternate setting.  */
    current_alternate_setting =  0;
    previous_interface = UX_NULL;

    /* Search for the current alternate setting for this interface.
       All its endpoints will need to be destroyed.
       Since interfaces in the parent configuration should be linked together
       in correct way, just find it in while loop.
     */
    while (1)
    {

        /* Try to locate the first interface container in the interface chain which
           has the same interface number as the one supplied by the caller.  */
        if (current_interface -> ux_interface_descriptor.bInterfaceNumber == current_interface_number)
        {

            /* The alternate setting 0 of this interface has the current selected
               alternate setting.  */
            if (current_interface -> ux_interface_descriptor.bAlternateSetting == 0)
            {

                /* Set the alternate setting.  */
                current_alternate_setting =  current_interface -> ux_interface_current_alternate_setting;

                /* Remember the main interface to store the next alternate setting.  */
                main_interface =  current_interface;

            }

            /* See if the current alternate setting matches that of the interface alternate setting.  */
            if (current_alternate_setting == current_interface -> ux_interface_descriptor.bAlternateSetting)
            {

                /* Yes, save the current alternate setting.  */
                previous_interface = current_interface;

                /* Then delete the current interface.  */
                _ux_host_stack_interface_instance_delete(current_interface);

                /* We are done in this loop.  */
                break;

            }
        }

        /* Move to the next interface. */
        current_interface =  current_interface -> ux_interface_next_interface;
    }

    /* Remember the new alternate setting.  */
    main_interface -> ux_interface_current_alternate_setting =  interface_ptr -> ux_interface_descriptor.bAlternateSetting;

    /* Now, the interface must be created with the new alternate setting.  */
    status =  _ux_host_stack_interface_instance_create(interface_ptr);

    /* If we could not create it, we return to the default one.  */
    if (status != UX_SUCCESS)
    {

        /* Then delete the failed interface.  */
        _ux_host_stack_interface_instance_delete(interface_ptr);

        /* Error, reset the main interface alternate setting to the default.  */
        main_interface -> ux_interface_current_alternate_setting =  current_alternate_setting;

        /* Re-create the previous interface with the old alternate setting.  */
        _ux_host_stack_interface_instance_create(previous_interface);

        /* Return error status.  */
        return(status);
    }

    /* Protect the control endpoint semaphore here.  It will be unprotected in the
       transfer request function.  */
    status =  _ux_host_semaphore_get(&configuration -> ux_configuration_device -> ux_device_protection_semaphore, UX_WAIT_FOREVER);
    if (status != UX_SUCCESS)
        return(status);

    /* Issue a SET_INTERFACE command to the target device.  */
    status =  _ux_host_stack_interface_set(interface_ptr);

    /* Check completion status.  */
    if (status != UX_SUCCESS)
    {

        /* Error, reset the main interface alternate setting to the default.  */
        main_interface -> ux_interface_current_alternate_setting =  current_alternate_setting;

        /* Delete the current interface.  */
        _ux_host_stack_interface_instance_delete(interface_ptr);

        /* Re-create the previous interface with the old alternate setting.  */
        _ux_host_stack_interface_instance_create(previous_interface);

        /* Return error status.  */
        _ux_host_semaphore_put(&configuration -> ux_configuration_device -> ux_device_protection_semaphore);
        return(status);
    }

    /* Return to caller.  */
    return(status);
}


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _uxe_host_stack_interface_setting_select            PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks errors in host stack interface select function */
/*    call.                                                               */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    interface_ptr                         Pointer to interface          */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_interface_setting_select                             */
/*                                          Interface setting select      */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Application                                                         */
/*                                                                        */
/**************************************************************************/
UINT  _uxe_host_stack_interface_setting_select(UX_INTERFACE *interface_ptr)
{

    /* Sanity check.  */
    if (interface_ptr == UX_NULL)
        return(UX_INVALID_PARAMETER);

    /* Invoke interface setting select function.  */
    return(_ux_host_stack_interface_setting_select(interface_ptr));
}