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_endpoints_get                PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function searches for the handle of the bulk out endpoint and  */
46 /*    optionally the bulk in endpoint.                                    */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    storage                               Pointer to storage class      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_interface_endpoint_get Get an endpoint pointer       */
59 /*    _ux_utility_memory_allocate           Allocate memory               */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Storage Class                                                       */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            use pre-calculated value    */
74 /*                                            instead of wMaxPacketSize,  */
75 /*                                            resulting in version 6.1.9  */
76 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
77 /*                                            initial the timeout value,  */
78 /*                                            resulting in version 6.1.10 */
79 /*                                                                        */
80 /**************************************************************************/
_ux_host_class_storage_endpoints_get(UX_HOST_CLASS_STORAGE * storage)81 UINT  _ux_host_class_storage_endpoints_get(UX_HOST_CLASS_STORAGE *storage)
82 {
83 
84 UINT            endpoint_index;
85 UX_ENDPOINT     *endpoint;
86 
87 
88     /* Search for the bulk OUT endpoint. It is attached to the interface container.  */
89     for (endpoint_index = 0; endpoint_index < storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bNumEndpoints;
90                         endpoint_index++)
91     {
92 
93         /* Get an endpoint.  */
94         _ux_host_stack_interface_endpoint_get(storage -> ux_host_class_storage_interface, endpoint_index, &endpoint);
95 
96         /* We have an endpoint. Check if endpoint is bulk and OUT.  */
97         if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
98             ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
99         {
100 
101             /* We have found the bulk OUT endpoint, save it!  */
102             storage -> ux_host_class_storage_bulk_out_endpoint =  endpoint;
103 
104             /* This transfer request always has the OUT direction.  */
105             endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;
106 
107             break;
108         }
109     }
110 
111     /* The bulk OUT endpoint is mandatory.  */
112     if (storage -> ux_host_class_storage_bulk_out_endpoint == UX_NULL)
113     {
114 
115         /* Error trap. */
116         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
117 
118         /* If trace is enabled, insert this event into the trace buffer.  */
119         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, storage, 0, 0, UX_TRACE_ERRORS, 0, 0)
120 
121         return(UX_ENDPOINT_HANDLE_UNKNOWN);
122     }
123 
124     /* Search for the bulk IN endpoint. It is attached to the interface container.  */
125     for (endpoint_index = 0; endpoint_index < storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bNumEndpoints;
126                         endpoint_index++)
127     {
128 
129         /* Get an endpoint.  */
130         _ux_host_stack_interface_endpoint_get(storage -> ux_host_class_storage_interface, endpoint_index, &endpoint);
131 
132         /* We have an endpoint. Check if endpoint is bulk and IN.  */
133         if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
134             ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
135         {
136 
137             /* We have found the bulk IN endpoint, save it!  */
138             storage -> ux_host_class_storage_bulk_in_endpoint =  endpoint;
139 
140             /* This transfer request always has the IN direction.  */
141             endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
142 
143             break;
144         }
145     }
146 
147     /* The bulk IN endpoint is mandatory */
148     if (storage -> ux_host_class_storage_bulk_in_endpoint == UX_NULL)
149     {
150 
151         /* Error trap. */
152         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
153 
154         /* If trace is enabled, insert this event into the trace buffer.  */
155         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, storage, 0, 0, UX_TRACE_ERRORS, 0, 0)
156 
157         return(UX_ENDPOINT_HANDLE_UNKNOWN);
158     }
159 
160     /* Set default transfer timeout value.  */
161     endpoint = storage -> ux_host_class_storage_bulk_in_endpoint;
162     endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value =
163                 UX_MS_TO_TICK_NON_ZERO(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT);
164     endpoint = storage -> ux_host_class_storage_bulk_out_endpoint;
165     endpoint -> ux_endpoint_transfer_request.ux_transfer_request_timeout_value =
166                 UX_MS_TO_TICK_NON_ZERO(UX_HOST_CLASS_STORAGE_TRANSFER_TIMEOUT);
167 
168 #ifdef UX_HOST_CLASS_STORAGE_INCLUDE_LEGACY_PROTOCOL_SUPPORT
169     /* Search the Interrupt IN endpoint. This endpoint is optional and only valid for
170        storage devices that use the CBI protocol.  */
171     if (storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bInterfaceProtocol == UX_HOST_CLASS_STORAGE_PROTOCOL_CBI)
172     {
173 
174         /* Yes, CBI protocol is present.  Search for Interrupt IN endpoint.  */
175         for (endpoint_index = 0; endpoint_index < storage -> ux_host_class_storage_interface -> ux_interface_descriptor.bNumEndpoints;
176                             endpoint_index++)
177         {
178 
179             /* Get an endpoint.  */
180             _ux_host_stack_interface_endpoint_get(storage -> ux_host_class_storage_interface, endpoint_index, &endpoint);
181 
182             /* Check if endpoint is Interrupt and IN.  */
183             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
184                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
185             {
186 
187                 /* We have found the Interrupt IN endpoint, save it!  */
188                 storage -> ux_host_class_storage_interrupt_endpoint =  endpoint;
189 
190                 /* This transfer request always has the IN direction.  */
191                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
192 
193                 /* The size of the transfer is fixed to the endpoint size.  */
194                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_requested_length =
195                                             endpoint -> ux_endpoint_transfer_request.ux_transfer_request_packet_length;
196 
197                 /* Get some memory for this data transfer.  */
198                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer =
199                                                         _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
200                                                                                 endpoint -> ux_endpoint_descriptor.wMaxPacketSize);
201 
202                 /* Check the memory pointer.  */
203                 if (endpoint -> ux_endpoint_transfer_request.ux_transfer_request_data_pointer == UX_NULL)
204                     return(UX_MEMORY_INSUFFICIENT);
205 
206                 break;
207             }
208         }
209 
210         if (storage -> ux_host_class_storage_interrupt_endpoint == UX_NULL)
211         {
212 
213             /* Error trap. */
214             _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
215 
216             /* If trace is enabled, insert this event into the trace buffer.  */
217             UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
218 
219             return(UX_ENDPOINT_HANDLE_UNKNOWN);
220         }
221     }
222 #endif
223 
224     /* Return successful completion.  */
225     return(UX_SUCCESS);
226 }
227 
228