1 /**************************************************************************/
2 /*                                                                        */
3 /*       Copyright (c) Microsoft Corporation. All rights reserved.        */
4 /*                                                                        */
5 /*       This software is licensed under the Microsoft Software License   */
6 /*       Terms for Microsoft Azure RTOS. Full text of the license can be  */
7 /*       found in the LICENSE file at https://aka.ms/AzureRTOS_EULA       */
8 /*       and in the root directory of this software.                      */
9 /*                                                                        */
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_transport                    PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is the transport layer for all protocols. It perform  */
46 /*    the error recovery and retries if needed.                           */
47 /*                                                                        */
48 /*    It's for RTOS mode only.                                            */
49 /*                                                                        */
50 /*  INPUT                                                                 */
51 /*                                                                        */
52 /*    storage                               Pointer to storage class      */
53 /*    data_pointer                          Pointer to data               */
54 /*                                                                        */
55 /*  OUTPUT                                                                */
56 /*                                                                        */
57 /*    Completion Status                                                   */
58 /*                                                                        */
59 /*  CALLS                                                                 */
60 /*                                                                        */
61 /*    (ux_host_class_storage_transport)     Class storage transport       */
62 /*    _ux_host_class_storage_device_reset   Reset device                  */
63 /*    _ux_host_class_storage_request_sense  Class request sense           */
64 /*    _ux_host_stack_endpoint_reset         Reset endpoint                */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Storage Class                                                       */
69 /*                                                                        */
70 /*  RELEASE HISTORY                                                       */
71 /*                                                                        */
72 /*    DATE              NAME                      DESCRIPTION             */
73 /*                                                                        */
74 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
75 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
76 /*                                            resulting in version 6.1    */
77 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            refined macros names,       */
79 /*                                            resulting in version 6.1.10 */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_storage_transport(UX_HOST_CLASS_STORAGE * storage,UCHAR * data_pointer)82 UINT  _ux_host_class_storage_transport(UX_HOST_CLASS_STORAGE *storage, UCHAR *data_pointer)
83 {
84 #if defined(UX_HOST_STANDALONE)
85     UX_PARAMETER_NOT_USED(storage);
86     UX_PARAMETER_NOT_USED(data_pointer);
87     return(UX_FUNCTION_NOT_SUPPORTED);
88 #else
89 
90 UINT        status;
91 UINT        csw_status;
92 
93 
94     /* Reset the sense code.  */
95     storage -> ux_host_class_storage_sense_code =  UX_SUCCESS;
96 
97     /* Send the command to the appropriate transport.  */
98     status =  storage -> ux_host_class_storage_transport(storage, data_pointer);
99 
100 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
101     /* The treatment of errors is different according to the protocol used. BO and CB belong
102        to the CSW group. CBI is separate.  */
103     if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceProtocol != UX_HOST_CLASS_STORAGE_PROTOCOL_CBI)
104     {
105 #endif
106 
107     /* Check the status.  */
108     if (status != UX_SUCCESS)
109 
110         /* There was a more serious error. Just give up!  */
111         return(status);
112 
113     /* The command transfer was OK but maybe we have a CSW error.  */
114     csw_status =  storage -> ux_host_class_storage_csw[UX_HOST_CLASS_STORAGE_CSW_STATUS];
115     if (csw_status == 0)
116         return(UX_SUCCESS);
117 
118     /* Check for a command failure. If so, we need to sense the error with
119        a REQUEST SENSE command.  */
120     status =  _ux_host_class_storage_request_sense(storage);
121 
122     /* If we have an transport failure here, we are in trouble! The storage device is in
123        an unstable state and should be reset completely.  */
124     if (status != UX_SUCCESS)
125     {
126 
127         /* Reset device.  */
128         _ux_host_class_storage_device_reset(storage);
129         return(status);
130     }
131 
132     /* The sense code is saved in the device instance. We can return safely.  */
133     return(UX_SUCCESS);
134 
135 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
136     }
137     else
138     {
139 
140         switch (status)
141         {
142 
143         case UX_SUCCESS:
144             return(UX_SUCCESS);
145 
146         case UX_TRANSFER_STALLED:
147 
148             /* The endpoint was halted by a stall condition and needs to be reset.  */
149             _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_in_endpoint);
150 
151             /* The endpoint was halted by a stall condition and needs to be reset.  */
152             _ux_host_stack_endpoint_reset(storage -> ux_host_class_storage_bulk_out_endpoint);
153 
154             /* Check for a command failure. If so, we need to sense the error with
155                a REQUEST SENSE command.  */
156             status =  _ux_host_class_storage_request_sense(storage);
157             return(status);
158 
159         default:
160 
161             /* There was a more serious error. Just give up!  */
162             return(status);
163         }
164     }
165 #endif
166 #endif
167 }
168