summaryrefslogtreecommitdiff
path: root/common/usbx_host_classes/src/ux_host_class_storage_transport.c
blob: af5fcdd46ec558be07652a1adac51b06da728768 (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
/***************************************************************************
 * 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_transport                    PORTABLE C      */
/*                                                           6.1.10       */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function is the transport layer for all protocols. It perform  */
/*    the error recovery and retries if needed.                           */
/*                                                                        */
/*    It's for RTOS mode only.                                            */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    storage                               Pointer to storage class      */
/*    data_pointer                          Pointer to data               */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    (ux_host_class_storage_transport)     Class storage transport       */
/*    _ux_host_class_storage_device_reset   Reset device                  */
/*    _ux_host_class_storage_request_sense  Class request sense           */
/*    _ux_host_stack_endpoint_reset         Reset endpoint                */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Storage Class                                                       */
/*                                                                        */
/**************************************************************************/
UINT  _ux_host_class_storage_transport(UX_HOST_CLASS_STORAGE *storage, UCHAR *data_pointer)
{
#if defined(UX_HOST_STANDALONE)
    UX_PARAMETER_NOT_USED(storage);
    UX_PARAMETER_NOT_USED(data_pointer);
    return(UX_FUNCTION_NOT_SUPPORTED);
#else

UINT        status;
UINT        csw_status;


    /* Reset the sense code.  */
    storage -> ux_host_class_storage_sense_code =  UX_SUCCESS;

    /* Send the command to the appropriate transport.  */
    status =  storage -> ux_host_class_storage_transport(storage, data_pointer);

#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
    /* The treatment of errors is different according to the protocol used. BO and CB belong
       to the CSW group. CBI is separate.  */
    if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceProtocol != UX_HOST_CLASS_STORAGE_PROTOCOL_CBI)
    {
#endif

    /* Check the status.  */
    if (status != UX_SUCCESS)

        /* There was a more serious error. Just give up!  */
        return(status);

    /* The command transfer was OK but maybe we have a CSW error.  */
    csw_status =  storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS];
    if (csw_status == 0)
        return(UX_SUCCESS);

    /* Check for a command failure. If so, we need to sense the error with
       a REQUEST SENSE command.  */
    status =  _ux_host_class_storage_request_sense(storage);

    /* If we have an transport failure here, we are in trouble! The storage device is in
       an unstable state and should be reset completely.  */
    if (status != UX_SUCCESS)
    {

        /* Reset device.  */
        _ux_host_class_storage_device_reset(storage);
        return(status);
    }

    /* The sense code is saved in the device instance. We can return safely.  */
    return(UX_SUCCESS);

#ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
    }
    else
    {

        switch (status)
        {

        case UX_SUCCESS:
            return(UX_SUCCESS);

        case UX_TRANSFER_STALLED:

            /* The endpoint was halted by a stall condition and needs to be reset.  */
            _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_in_endpoint);

            /* The endpoint was halted by a stall condition and needs to be reset.  */
            _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_out_endpoint);

            /* Check for a command failure. If so, we need to sense the error with
               a REQUEST SENSE command.  */
            status =  _ux_host_class_storage_request_sense(storage);
            return(status);

        default:

            /* There was a more serious error. Just give up!  */
            return(status);
        }
    }
#endif
#endif
}