summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_storage_driver_entry.c
blob: 2dd0684af7478281d0decdc54e15d0643dc80ac0 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
/***************************************************************************
 * 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"


/* Defined if FileX is not integrated in USBX
   and it's used as external module for file system.  */

/* #define UX_HOST_CLASS_STORAGE_EXT_FILEX */

#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX)

#ifdef FX_API_H /* For test, confirm FX is not included before.  */
#error fx_api.h should not be included in this mode
#endif

/* FX not integrated in UX, but used as external module.  */
#if defined(UX_HOST_CLASS_STORAGE_EXT_FILEX)

/* FX related things needs define here.  */
#include "fx_api.h"
#define UX_MEDIA                                    FX_MEDIA
VOID    _ux_host_class_storage_driver_entry(UX_MEDIA *media);

/* FX driver is available to support FX as external module.  */
#ifndef UX_HOST_CLASS_STORAGE_DRIVER_ENTRY_ENABLE
#define UX_HOST_CLASS_STORAGE_DRIVER_ENTRY_ENABLE
#endif
#endif
#else

/* FX driver is used for RTOS mode by default.  */
#ifndef UX_HOST_CLASS_STORAGE_DRIVER_ENTRY_ENABLE
#define UX_HOST_CLASS_STORAGE_DRIVER_ENTRY_ENABLE
#endif
#endif


#if defined(UX_HOST_CLASS_STORAGE_DRIVER_ENTRY_ENABLE)
/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_host_class_storage_driver_entry                 PORTABLE C      */
/*                                                           6.1.12       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the entry point for the FileX file system. All     */
/*    FileX driver I/O calls are are multiplexed here and rerouted to     */
/*    the proper USB storage class functions.                             */
/*                                                                        */
/*    When the entry is for storage with FX support (not in standalone    */
/*    mode, and with FileX), the FX media is openned in storage mount     */
/*    flow, and can be directly used in application after mounted.        */
/*                                                                        */
/*    When the entry is for no FX mode (FX in external module, and USBX   */
/*    is compiled without FileX integration), it is an example with       */
/*    disk partition support. In this case the FX media does not operate  */
/*    inside the storage flow. Actions are taken when application mounts  */
/*    media to FX_MEDIA and then have access to media APIs.               */
/*                                                                        */
/*    In no FX mode demo, it assumes media is managed with partition      */
/*    start from sector address of FX_MEDIA::fx_media_reserved_for_user.  */
/*                                                                        */
/*    The following links are not initialized in no FX mode, they must be */
/*    initialized before using the entry in no FX mode:                   */
/*    - FX_MEDIA::fx_media_reserved_for_user                              */
/*                                          Partition start sector inside */
/*                                          the whole storage media, must */
/*                                          set before media open         */
/*    - FX_MEDIA::fx_media_driver_info      Pointer to storage media,     */
/*                                          assigned while calling media  */
/*                                          open (fx_media_open)          */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    media                                 FileX media pointer           */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    None                                                                */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_host_class_storage_sense_code_translate                         */
/*                                          Translate error status codes  */
/*    _ux_host_class_storage_media_read     Read sector(s)                */
/*    _ux_host_class_storage_media_write    Write sector(s)               */
/*    _ux_host_semaphore_get                Get protection semaphore      */
/*    _ux_host_semaphore_put                Release protection semaphore  */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    FileX                                                               */
/*                                                                        */
/**************************************************************************/
VOID  _ux_host_class_storage_driver_entry(FX_MEDIA *media)
{

UINT                            status;
UX_HOST_CLASS_STORAGE           *storage;
UX_HOST_CLASS_STORAGE_MEDIA     *storage_media;
ULONG                           partition_start;


    /* Get the pointers to the instances and partition start.  */
#if !defined(UX_HOST_CLASS_STORAGE_NO_FILEX)
    storage =  (UX_HOST_CLASS_STORAGE *) media -> fx_media_driver_info;
    storage_media =  (UX_HOST_CLASS_STORAGE_MEDIA *) media -> fx_media_reserved_for_user;
    partition_start = storage_media -> ux_host_class_storage_media_partition_start;
#else
    storage_media = (UX_HOST_CLASS_STORAGE_MEDIA *) media -> fx_media_driver_info;
    storage = storage_media -> ux_host_class_storage_media_storage;
    partition_start = (ULONG) media -> fx_media_reserved_for_user;
#endif

    /* Ensure the instance is valid.  */
    if ((storage -> ux_host_class_storage_state !=  UX_HOST_CLASS_INSTANCE_LIVE) &&
        (storage -> ux_host_class_storage_state !=  UX_HOST_CLASS_INSTANCE_MOUNTING))
    {

        /* Class instance is invalid. Return an error!  */
        media -> fx_media_driver_status =  FX_PTR_ERROR;
        return;
    }

#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX)

    /* Ensure the media is valid.  */
    if ((storage_media -> ux_host_class_storage_media_storage != storage) ||
        (storage_media -> ux_host_class_storage_media_status != UX_USED))
    {

        /* Media instance is invalid.  */
        media -> fx_media_driver_status =  FX_PTR_ERROR;
        return;
    }
#endif

    /* Protect Thread reentry to this instance.  */
    status = _ux_host_class_storage_lock(storage, UX_WAIT_FOREVER);
    if (status != UX_SUCCESS)
    {

        /* Unable to lock, return an error.  */
        media -> fx_media_driver_status =  FX_INVALID_STATE;
        return;
    }

    /* Restore the LUN number from the media instance.  */
    storage -> ux_host_class_storage_lun =  storage_media -> ux_host_class_storage_media_lun;

    /* And the sector size.  */
    storage -> ux_host_class_storage_sector_size =
                storage_media -> ux_host_class_storage_media_sector_size;

#if defined(UX_HOST_CLASS_STORAGE_NO_FILEX)

    /* Restore current used last sector number.  */
    storage -> ux_host_class_storage_last_sector_number =
                storage_media -> ux_host_class_storage_media_number_sectors - 1;
#endif

    /* Look at the request specified by the FileX caller.  */
    switch (media -> fx_media_driver_request)
    {

    case FX_DRIVER_READ:

        /* Read one or more sectors.  */
        status =  _ux_host_class_storage_media_read(storage,
                                media -> fx_media_driver_logical_sector + partition_start,
                                media -> fx_media_driver_sectors,
                                media -> fx_media_driver_buffer);

        /* Check completion status.  */
        if (status == UX_SUCCESS)
            media -> fx_media_driver_status =  FX_SUCCESS;
        else
        {

#if defined(UX_HOST_STANDALONE)

            /* Poll status.  */
            _ux_host_class_storage_media_check(storage);
#endif

            media -> fx_media_driver_status =
                _ux_host_class_storage_sense_code_translate(storage, status);
        }
        break;


    case FX_DRIVER_WRITE:

        /* Write one or more sectors.  */
        status =  _ux_host_class_storage_media_write(storage,
                                media -> fx_media_driver_logical_sector + partition_start,
                                media -> fx_media_driver_sectors,
                                media -> fx_media_driver_buffer);

        /* Check completion status.  */
        if (status == UX_SUCCESS)
            media -> fx_media_driver_status =  FX_SUCCESS;
        else
        {

#if defined(UX_HOST_STANDALONE)

            /* Poll status.  */
            _ux_host_class_storage_media_check(storage);
#endif

            media -> fx_media_driver_status =
                _ux_host_class_storage_sense_code_translate(storage,status);
        }
        break;


    case FX_DRIVER_FLUSH:

        /* Nothing to do. Just return a good status!  */
        media -> fx_media_driver_status =  FX_SUCCESS;
        break;


    case FX_DRIVER_ABORT:

        /* Nothing to do. Just return a good status!  */
        media -> fx_media_driver_status =  FX_SUCCESS;
        break;


    case FX_DRIVER_INIT:

#if defined(UX_HOST_STANDALONE)

            /* Poll status.  */
            _ux_host_class_storage_media_check(storage);
#endif

        /* Check for media protection.  We must do this operation here because FileX clears all the
           media fields before init.  */
        if (storage -> ux_host_class_storage_write_protected_media ==  UX_TRUE)

            /* The media is Write Protected. We tell FileX.  */
            media -> fx_media_driver_write_protect = UX_TRUE;

        /* This function always succeeds.  */
        media -> fx_media_driver_status =  FX_SUCCESS;
        break;


    case FX_DRIVER_UNINIT:

        /* Nothing to do. Just return a good status!  */
        media -> fx_media_driver_status =  FX_SUCCESS;
        break;


    case FX_DRIVER_BOOT_READ:

        /* Read the media boot sector.  */
        status =  _ux_host_class_storage_media_read(storage,
                partition_start, 1, media -> fx_media_driver_buffer);

        /* Check completion status.  */
        if (status == UX_SUCCESS)
            media -> fx_media_driver_status =  FX_SUCCESS;
        else
        {

#if defined(UX_HOST_STANDALONE)

            /* Poll status.  */
            _ux_host_class_storage_media_check(storage);
#endif

            media -> fx_media_driver_status =
                _ux_host_class_storage_sense_code_translate(storage,status);
        }
        break;


    case FX_DRIVER_BOOT_WRITE:

        /* Write the boot sector.  */
        status =  _ux_host_class_storage_media_write(storage,
                partition_start, 1, media -> fx_media_driver_buffer);

        /* Check completion status.  */
        if (status == UX_SUCCESS)
            media -> fx_media_driver_status =  FX_SUCCESS;
        else
        {

#if defined(UX_HOST_STANDALONE)

            /* Poll status.  */
            _ux_host_class_storage_media_check(storage);
#endif

            media -> fx_media_driver_status =
                _ux_host_class_storage_sense_code_translate(storage,status);
        }
        break;


    default:

        /* Invalid request from FileX */
        media -> fx_media_driver_status =  FX_IO_ERROR;
        break;
    }

    /* Unprotect thread reentry to this instance.  */
    _ux_host_class_storage_unlock(storage);
}
#endif