summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_storage_deactivate.c
blob: 578d54f1c4276719e985ad6ab0fa3a20af97c01f (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
/***************************************************************************
 * 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                                                        */
/**                                                                       */
/**   Storage Class                                                       */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/


/* Include necessary system files.  */

#define UX_SOURCE_CODE

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


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_storage_deactivate                   PORTABLE C      */
/*                                                           6.1.10       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is called when this instance of the storage device    */
/*    has been removed from the bus either directly or indirectly. The    */
/*    bulk in\out pipes will be destroyed and the instanced removed.      */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    command                               Pointer to class command      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    ux_media_close                        Close media                   */
/*    _ux_host_stack_endpoint_transfer_abort Abort transfer request       */
/*    _ux_host_stack_class_instance_destroy Destroy class instance        */
/*    _ux_utility_memory_free               Free memory block             */
/*    _ux_host_semaphore_get                Get protection semaphore      */
/*    _ux_host_semaphore_delete             Delete protection semaphore   */
/*    _ux_utility_thread_schedule_other     Schedule other threads        */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Storage Class                                                       */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_storage_deactivate(UX_HOST_CLASS_COMMAND *command)
{

UINT                            status;
UX_HOST_CLASS_STORAGE           *storage;
UX_HOST_CLASS_STORAGE_MEDIA     *storage_media;
UX_HOST_CLASS                   *class_inst;
UINT                            inst_index;
#if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
UX_MEDIA                        *media;
VOID                            *memory;
#endif


    /* Get the instance for this class.  */
    storage =  (UX_HOST_CLASS_STORAGE *) command -> ux_host_class_command_instance;

    /* We need the class container.  */
    class_inst =  storage -> ux_host_class_storage_class;

    /* Point the media structure to the first media in the container.  */
    storage_media =  (UX_HOST_CLASS_STORAGE_MEDIA *) class_inst -> ux_host_class_media;

    /* The storage device is being shut down.  */
    storage -> ux_host_class_storage_state =  UX_HOST_CLASS_INSTANCE_SHUTDOWN;

    /* We come to this point when the device has been extracted. So there may have been a transaction
       being scheduled. We make sure the transaction has been completed by the controller driver.
       When the device is extracted, the controller tries multiple times the transaction and retires it
       with a DEVICE_NOT_RESPONDING error code.

       First we take care of endpoint OUT.  */

    /* We need to abort transactions on the bulk pipes.  */
    if (storage -> ux_host_class_storage_bulk_out_endpoint != UX_NULL)
        _ux_host_stack_endpoint_transfer_abort(storage -> ux_host_class_storage_bulk_out_endpoint);

    /* Then endpoint IN.  */
    if (storage -> ux_host_class_storage_bulk_in_endpoint != UX_NULL)
        _ux_host_stack_endpoint_transfer_abort(storage -> ux_host_class_storage_bulk_in_endpoint);

#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
    /* Was the protocol CBI ? */
    if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceProtocol == UX_HOST_CLASS_STORAGE_PROTOCOL_CBI)
    {

        /* Was there an interrupt endpoint?  */
        if (storage -> ux_host_class_storage_interrupt_endpoint != UX_NULL)
        {

            /* Abort the data transfer on the interrupt endpoint.  */
            _ux_host_stack_endpoint_transfer_abort(storage -> ux_host_class_storage_interrupt_endpoint);

            /* Free the memory that was used by the interrupt endpoint.  */
            if (storage -> ux_host_class_storage_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer != UX_NULL)
                _ux_utility_memory_free(storage -> ux_host_class_storage_interrupt_endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer);
        }
    }
#endif

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


    /* Inform UX_MEDIA (default FileX) of the deactivation of all Media attached to this instance.  */
    for (inst_index = 0; inst_index < UX_HOST_CLASS_STORAGE_MAX_MEDIA; inst_index++)
    {

#if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
        /* Get the UX_MEDIA (default FileX) attached to this media.  */
        media = &storage_media -> ux_host_class_storage_media;

        /* Check if the media belongs to the device being removed.  */
        if (((UX_HOST_CLASS_STORAGE *) ux_media_driver_info_get(media)) == storage)
        {

            /* Check if the media was properly opened.  */
            if (storage_media -> ux_host_class_storage_media_status == UX_HOST_CLASS_STORAGE_MEDIA_MOUNTED)
            {

                /* We preserve the memory used by this media.  */
                memory =  storage_media -> ux_host_class_storage_media_memory;

                /* Ask UX_MEDIA (default FileX) to unmount the partition.  */
                ux_media_close(media);

                /* This device is now unmounted.  */
                storage_media -> ux_host_class_storage_media_status =  UX_HOST_CLASS_STORAGE_MEDIA_UNMOUNTED;

                /* Reset the media ID.  */
                ux_media_id_set(media, 0);

                /* Free the memory block used for data transfer on behalf of UX_MEDIA (default FileX).  */
                _ux_utility_memory_free(memory);
            }
        }
#else

        /* Check if the media is for this storage.  */
        if (storage_media -> ux_host_class_storage_media_status == UX_USED &&
            storage_media -> ux_host_class_storage_media_storage == storage)
        {

            /* Free the storage media.  */
            storage_media -> ux_host_class_storage_media_status = UX_UNUSED;

            /* Invoke callback for media removal.  */
            if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
            {

                /* Call system change function.  */
                _ux_system_host ->  ux_system_host_change_function(UX_STORAGE_MEDIA_REMOVAL,
                                    storage -> ux_host_class_storage_class, (VOID *) storage_media);
            }
        }
#endif

        /* Move to next entry in the media array.  */
        storage_media++;
    }

    /* Protect thread reentry to this instance.  */
    status = _ux_host_semaphore_get(&storage -> ux_host_class_storage_semaphore, UX_WAIT_FOREVER);
    UX_PARAMETER_NOT_USED(status);

    /* Destroy the instance.  */
    _ux_host_stack_class_instance_destroy(storage -> ux_host_class_storage_class, (VOID *) storage);

    /* Destroy the protection semaphore.  */
    _ux_host_semaphore_delete(&storage -> ux_host_class_storage_semaphore);

    /* Before we free the device resources, we need to inform the application
        that the device is removed.  */
    if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
    {

        /* Inform the application the device is removed.  */
        _ux_system_host -> ux_system_host_change_function(UX_DEVICE_REMOVAL, storage -> ux_host_class_storage_class, (VOID *) storage);
    }

    /* If trace is enabled, insert this event into the trace buffer.  */
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_DEACTIVATE, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)

    /* If trace is enabled, register this object.  */
    UX_TRACE_OBJECT_UNREGISTER(storage);

    /* Free the storage instance memory.  */
    _ux_utility_memory_free(storage);

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