summaryrefslogtreecommitdiff
path: root/common/core/src/ux_host_stack_device_resources_free.c
blob: f9f9c665ad4374d92a5543337d78238afdd0c7c2 (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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/***************************************************************************
 * 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_device_resources_free                PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function will free all the device resources allocated.         */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    device                                Device pointer                */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_stack_endpoint_transfer_abort                              */
/*                                          Abort transfer                */
/*    _ux_host_stack_endpoint_instance_delete                             */
/*                                          Delete endpoint instance      */
/*    _ux_utility_memory_free               Free memory block             */
/*    _ux_utility_memory_set                Set memory with a value       */
/*    _ux_utility_semaphore_delete          Semaphore delete              */
/*    _ux_utility_thread_schedule_other     Sleep thread to let others    */
/*                                          run                           */
/*    (ux_hcd_entry_function)               HCD entry function            */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    USBX Components                                                     */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_stack_device_resources_free(UX_DEVICE *device)
{

UX_CONFIGURATION        *configuration;
UX_INTERFACE            *interface_ptr;
UX_ENDPOINT             *endpoint;
VOID                    *container;
ULONG                   current_alternate_setting;
UX_HCD                  *hcd;
#if UX_MAX_DEVICES > 1
UINT                    device_address_byte_index;
UINT                    device_address_bit_index;
UCHAR                   device_address_byte;
#endif
#if defined(UX_HOST_STANDALONE)
UX_DEVICE               *enum_next;
#endif

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

#if defined(UX_HOST_STANDALONE)

    /* Free possible allocated enumeration resources.  */
    if (device -> ux_device_flags & UX_DEVICE_FLAG_ENUM)
    {

        /* If transfer buffer is not freed, free it.  */
        if (device -> ux_device_enum_trans &&
            device -> ux_device_enum_trans -> ux_transfer_request_data_pointer)
        {
            _ux_utility_memory_free(device -> ux_device_enum_trans ->
                                            ux_transfer_request_data_pointer);
        }

        /* If configuration is not attached, free it.  */
        if ((device -> ux_device_enum_state == UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE) ||
            ((device -> ux_device_enum_state == UX_HOST_STACK_ENUM_TRANS_WAIT) &&
            (device -> ux_device_enum_next_state == UX_HOST_STACK_ENUM_CONFIG_DESCR_PARSE)))
        {
            if (device -> ux_device_enum_inst.configuration &&
                device -> ux_device_enum_inst.configuration ->
                                            ux_configuration_device == UX_NULL)
            {
                _ux_utility_memory_free(device -> ux_device_enum_inst.ptr);
            }
        }
    }

    /* Reset device flags.  */
    device -> ux_device_flags = 0;

#endif

    /* Set the alternate setting to zero.  */
    current_alternate_setting = 0;

    /* Get the first configuration registered to the device.  */
    configuration =  device -> ux_device_first_configuration;

    /* Parse all the configurations, remove all resources for the possible configuration.  */
    while (configuration != UX_NULL)
    {

        /* We have the correct configuration, search the interface(s).  */
        interface_ptr =  configuration -> ux_configuration_first_interface;

        /* Parse all the interfaces.  */
        while (interface_ptr != UX_NULL)
        {

            /* The alternate setting 0 has the selected alternate setting value.  */
            if (interface_ptr -> ux_interface_descriptor.bAlternateSetting == 0)
                current_alternate_setting = interface_ptr -> ux_interface_current_alternate_setting;

            /* If this is the selected interface, we need to free all the endpoints
            attached to the alternate setting for this interface.  */
            endpoint =  interface_ptr -> ux_interface_first_endpoint;

            /* Parse all the endpoints.  */
            while (endpoint != UX_NULL)
            {

                /* Check if this is the selected interface.  */
                if (interface_ptr -> ux_interface_descriptor.bAlternateSetting == current_alternate_setting)
                {

                    /* Delete the endpoint instance first.  */
                    _ux_host_stack_endpoint_instance_delete(endpoint);
                }

                /* Memorize the endpoint container address.  */
                container =  (VOID *) endpoint;

                /* Get the next endpoint.  */
                endpoint =  endpoint -> ux_endpoint_next_endpoint;

                /* Delete the endpoint container.  */
                _ux_utility_memory_free(container);
            }


            /* Memorize the interface container address.  */
            container =  (VOID *) interface_ptr;

            /* Get the next interface.  */
            interface_ptr =  interface_ptr -> ux_interface_next_interface;

            /* Delete the interface container.  */
            _ux_utility_memory_free(container);
        }

        /* Memorize this configuration address before we free it.  */
        container =  (VOID *) configuration;

        /* Move to the next configuration in the list.  */
        configuration =  configuration -> ux_configuration_next_configuration;

        /* Free the configuration.  */
        _ux_utility_memory_free(container);
    }

    /* If there was a copy of packed descriptor, free it.  */
    if (device -> ux_device_packed_configuration)
    {
        _ux_utility_memory_free(device -> ux_device_packed_configuration);

        /* Pointer and keep count is set NULL later while reseting instance memory.  */
    }

    /* We need the HCD address for the control endpoint removal and to free
       the device address.  */
    hcd = UX_DEVICE_HCD_GET(device);

    /* Was the control endpoint already created ? */
    if (device -> ux_device_control_endpoint.ux_endpoint_state != 0)
    {

        /* There may be pending transactions on the control endpoint. They need to be aborted.  */
        _ux_host_stack_endpoint_transfer_abort(&device -> ux_device_control_endpoint);

        /* The enumeration thread needs to sleep a while to allow the application or the class that may be using
            the control endpoint to exit properly.  */
        _ux_host_thread_schedule_other(UX_THREAD_PRIORITY_ENUM);

        /* The control endpoint should be destroyed at the HCD level.  */
        hcd -> ux_hcd_entry_function(hcd, UX_HCD_DESTROY_ENDPOINT, (VOID *) &device -> ux_device_control_endpoint);
    }

    /* The semaphore attached to the control endpoint must be destroyed.  */
    _ux_host_semaphore_delete(&device -> ux_device_control_endpoint.ux_endpoint_transfer_request.ux_transfer_request_semaphore);

#if UX_MAX_DEVICES > 1
    /* Check if the device had an assigned address.  */
    if (device -> ux_device_address != 0)
    {

        /* The USB address of this device can now be returned to the pool
           We need the HCD pointer for this operation.  */

        /* Calculate in which byte index the device address belongs.  */
        device_address_byte_index =  (UINT) (device -> ux_device_address-1)/8;

        /* Now calculate the amount left in the byte index in bit.  */
        device_address_bit_index =  (UINT) (device -> ux_device_address-1)%8;

        /* Build the mask for the address.  */
        device_address_byte =  (UCHAR)(1 << device_address_bit_index);

        /* Free the address.  */
        hcd -> ux_hcd_address[device_address_byte_index] &=  (UCHAR)~device_address_byte;
    }
#endif

    /* The semaphore for endpoint 0 protection must be destroyed.  */
    _ux_host_semaphore_delete(&device -> ux_device_protection_semaphore);

    /* Now this device can be free and its container return to the pool.  */
#if defined(UX_HOST_STANDALONE)
    enum_next = device -> ux_device_enum_next;
    _ux_utility_memory_set(device, 0, sizeof(UX_DEVICE)); /* Use case of memset is verified. */
    device -> ux_device_enum_next = enum_next;
#else

    _ux_utility_memory_set(device, 0, sizeof(UX_DEVICE)); /* Use case of memset is verified. */
#endif

    /* Mark the device handle as unused.  */
    device -> ux_device_handle =  UX_UNUSED;

    /* Return successful completion.  */
    return(UX_SUCCESS);
}