GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: usbx_host_classes/src/ux_host_class_storage_activate.c Lines: 32 34 94.1 %
Date: 2026-03-06 18:57:10 Branches: 13 14 92.9 %

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
/**   Storage Class                                                       */
19
/**                                                                       */
20
/**************************************************************************/
21
/**************************************************************************/
22
23
24
/* Include necessary system files.  */
25
26
#define UX_SOURCE_CODE
27
28
#include "ux_api.h"
29
#include "ux_host_class_storage.h"
30
#include "ux_host_stack.h"
31
32
33
/**************************************************************************/
34
/*                                                                        */
35
/*  FUNCTION                                               RELEASE        */
36
/*                                                                        */
37
/*    _ux_host_class_storage_activate                     PORTABLE C      */
38
/*                                                           6.3.0        */
39
/*  AUTHOR                                                                */
40
/*                                                                        */
41
/*    Chaoqiong Xiao, Microsoft Corporation                               */
42
/*                                                                        */
43
/*  DESCRIPTION                                                           */
44
/*                                                                        */
45
/*    This function activates an instance of the storage class.           */
46
/*                                                                        */
47
/*  INPUT                                                                 */
48
/*                                                                        */
49
/*    command                               Pointer to class command      */
50
/*                                                                        */
51
/*  OUTPUT                                                                */
52
/*                                                                        */
53
/*    Completion Status                                                   */
54
/*                                                                        */
55
/*  CALLS                                                                 */
56
/*                                                                        */
57
/*    _ux_host_class_storage_configure      Configure storage device      */
58
/*    _ux_host_class_storage_device_support_check                         */
59
/*                                          Check protocol support        */
60
/*    _ux_host_class_storage_endpoints_get  Get all endpoints             */
61
/*    _ux_host_class_storage_device_initialize                            */
62
/*                                          Initialize storage device     */
63
/*    _ux_host_stack_class_instance_create  Create class instance         */
64
/*    _ux_host_stack_class_instance_destroy Destroy class instance        */
65
/*    _ux_utility_memory_allocate           Allocate memory block         */
66
/*    _ux_utility_memory_free               Free memory block             */
67
/*    _ux_host_semaphore_create             Create semaphore              */
68
/*                                                                        */
69
/*  CALLED BY                                                             */
70
/*                                                                        */
71
/*    Storage Class                                                       */
72
/*                                                                        */
73
/**************************************************************************/
74
140
UINT  _ux_host_class_storage_activate(UX_HOST_CLASS_COMMAND *command)
75
{
76
77
UX_INTERFACE            *interface_ptr;
78
UX_HOST_CLASS_STORAGE   *storage;
79
UINT                    status;
80
81
82
    /* The storage is always activated by the interface descriptor and not the
83
       device descriptor.  */
84
140
    interface_ptr =  (UX_INTERFACE *) command -> ux_host_class_command_container;
85
86
    /* Obtain memory for this class instance.  The memory used MUST BE allocated from a CACHE SAFE memory
87
       since the buffer for the CSW is an array contained within each storage instance. */
88
140
    storage =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, sizeof(UX_HOST_CLASS_STORAGE));
89
140
    if (storage == UX_NULL)
90
1
        return(UX_MEMORY_INSUFFICIENT);
91
92
    /* Store the class container into this instance */
93
139
    storage -> ux_host_class_storage_class =  command -> ux_host_class_command_class_ptr;
94
95
    /* Store the interface container into the storage class instance.  */
96
139
    storage -> ux_host_class_storage_interface =  interface_ptr;
97
98
    /* Store the device container into the storage class instance.  */
99
139
    storage -> ux_host_class_storage_device =  interface_ptr -> ux_interface_configuration -> ux_configuration_device;
100
101
    /* Check class,sub class, protocol.  */
102
139
    status =  _ux_host_class_storage_device_support_check(storage);
103
139
    if (status != UX_SUCCESS)
104
    {
105
        _ux_utility_memory_free(storage);
106
        return(status);
107
    }
108
109
    /* Search all the endpoints for the storage interface (Bulk Out, Bulk in,
110
       and optional Interrupt endpoint).  */
111
139
    status =  _ux_host_class_storage_endpoints_get(storage);
112
139
    if (status != UX_SUCCESS)
113
    {
114
12
        _ux_utility_memory_free(storage);
115
12
        return(status);
116
    }
117
118
    /* Create this class instance.  */
119
127
    _ux_host_stack_class_instance_create(command -> ux_host_class_command_class_ptr, (VOID *) storage);
120
121
    /* This instance of the device must also be stored in the interface container.  */
122
127
    interface_ptr -> ux_interface_class_instance =  (VOID *) storage;
123
124
#if defined(UX_HOST_STANDALONE)
125
126
    /* Activate storage class task function.  */
127
    storage -> ux_host_class_storage_class -> ux_host_class_task_function = _ux_host_class_storage_tasks_run;
128
129
    /* Mark the storage as live now (media mounts in task).  */
130
    storage -> ux_host_class_storage_state = UX_HOST_CLASS_INSTANCE_MOUNTING;
131
132
    /* Keep storage locked before it's initialized.  */
133
    storage -> ux_host_class_storage_flags |= UX_HOST_CLASS_STORAGE_FLAG_LOCK;
134
#else
135
136
    /* Configure the USB storage device.  */
137
127
    status =  _ux_host_class_storage_configure(storage);
138
139
    /* Create the semaphore to protect multiple threads from accessing the same storage instance.  */
140
127
    if (status == UX_SUCCESS)
141
    {
142
126
        status = _ux_host_semaphore_create(&storage -> ux_host_class_storage_semaphore, "ux_host_class_storage_semaphore", 1);
143
126
        if (status != UX_SUCCESS)
144
1
            status = UX_SEMAPHORE_ERROR;
145
    }
146
147
    /* Error case, free resources.  */
148
127
    if (status != UX_SUCCESS)
149
    {
150
151
        /* Last one, semaphore not created or created error, no need to free.  */
152
153
        /* Error, destroy the class and return an error.  */
154
2
        _ux_host_stack_class_instance_destroy(storage -> ux_host_class_storage_class, (VOID *) storage);
155
156
        /* This instance of the device must also be removed in the interface container.  */
157
2
        interface_ptr -> ux_interface_class_instance =  (VOID *) UX_NULL;
158
159
        /* Free memory for class instance.  */
160
2
        _ux_utility_memory_free(storage);
161
162
2
        return(status);
163
    }
164
165
    /* Mark the storage as mounting now.  */
166
125
    storage -> ux_host_class_storage_state =  UX_HOST_CLASS_INSTANCE_MOUNTING;
167
168
    /* Initialize the USB storage device.  We do not check the status at this stage. We let the instance of this
169
       class live even if there was a failure. Because the storage class has many media instance, we will let the
170
       disconnection signal clean the instance at a later stage.  */
171
125
    _ux_host_class_storage_device_initialize(storage);
172
173
    /* Mark the storage as live now.  */
174
125
    storage -> ux_host_class_storage_state =  UX_HOST_CLASS_INSTANCE_LIVE;
175
176
#endif
177
178
    /* If all is fine and the device is mounted, we may need to inform the application
179
       if a function has been programmed in the system structure.  */
180
125
    if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
181
    {
182
183
        /* Call system change function.  */
184
62
        _ux_system_host ->  ux_system_host_change_function(UX_DEVICE_INSERTION, storage -> ux_host_class_storage_class, (VOID *) storage);
185
    }
186
187
    /* If trace is enabled, insert this event into the trace buffer.  */
188
    UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_STORAGE_ACTIVATE, storage, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
189
190
    /* If trace is enabled, register this object.  */
191
    UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, storage, 0, 0, 0)
192
193
    /* Return completion status. Force it to success. */
194
125
    return(UX_SUCCESS);
195
}
196