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 /**   Asix 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_asix.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_asix_endpoints_get                   PORTABLE C      */
38 /*                                                           6.2.0        */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function distinguishes for either the Data or Control Class.   */
46 /*    For the data class, we mount the bulk in and bulk out endpoints.    */
47 /*    For the control class, we mount the optional interrupt endpoint.    */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    asix                                      Pointer to asix class     */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_interface_endpoint_get     Get interface endpoint    */
60 /*    _ux_host_stack_transfer_request           Transfer request          */
61 /*    _ux_utility_memory_allocate               Allocate memory           */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    _ux_host_class_asix_activate              Activate asix class       */
66 /*                                                                        */
67 /*  RELEASE HISTORY                                                       */
68 /*                                                                        */
69 /*    DATE              NAME                      DESCRIPTION             */
70 /*                                                                        */
71 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
72 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            resulting in version 6.1    */
74 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
75 /*                                            use pre-calculated value    */
76 /*                                            instead of wMaxPacketSize,  */
77 /*                                            resulting in version 6.1.9  */
78 /*  10-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            removed packet RX callback, */
80 /*                                            refined INT EP start time,  */
81 /*                                            resulting in version 6.2.0  */
82 /*                                                                        */
83 /**************************************************************************/
_ux_host_class_asix_endpoints_get(UX_HOST_CLASS_ASIX * asix)84 UINT  _ux_host_class_asix_endpoints_get(UX_HOST_CLASS_ASIX *asix)
85 {
86 
87 UINT            status;
88 UINT            endpoint_index;
89 UX_ENDPOINT     *endpoint;
90 UX_TRANSFER     *transfer_request;
91 
92 
93     /* Search the bulk OUT endpoint. It is attached to the interface container.  */
94     for (endpoint_index = 0; endpoint_index < asix -> ux_host_class_asix_interface -> ux_interface_descriptor.bNumEndpoints;
95                         endpoint_index++)
96     {
97 
98         /* Get interface endpoint.  */
99         status =  _ux_host_stack_interface_endpoint_get(asix -> ux_host_class_asix_interface, endpoint_index, &endpoint);
100 
101         /* Check the completion status.  */
102         if (status == UX_SUCCESS)
103         {
104 
105             /* Check if endpoint is bulk and OUT.  */
106             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
107                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
108             {
109 
110                 /* This transfer_request always have the OUT direction.  */
111                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;
112 
113 
114                 /* There is a callback function associated with the transfer request, so we need the class instance.  */
115                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_class_instance =  (VOID *) asix;
116 
117                 /* The transfer request has a callback function.  */
118                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_completion_function =  _ux_host_class_asix_transmission_callback;
119 
120                 /* We have found the bulk endpoint, save it.  */
121                 asix -> ux_host_class_asix_bulk_out_endpoint =  endpoint;
122                 break;
123             }
124         }
125     }
126 
127     /* The bulk out endpoint is mandatory.  */
128     if (asix -> ux_host_class_asix_bulk_out_endpoint == UX_NULL)
129     {
130 
131         /* Error trap. */
132         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
133 
134         /* If trace is enabled, insert this event into the trace buffer.  */
135         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, asix, 0, 0, UX_TRACE_ERRORS, 0, 0)
136 
137         return(UX_ENDPOINT_HANDLE_UNKNOWN);
138     }
139 
140     /* Search the bulk IN endpoint. It is attached to the interface container.  */
141     for (endpoint_index = 0; endpoint_index < asix -> ux_host_class_asix_interface -> ux_interface_descriptor.bNumEndpoints;
142                         endpoint_index++)
143     {
144 
145         /* Get the endpoint handle.  */
146         status =  _ux_host_stack_interface_endpoint_get(asix -> ux_host_class_asix_interface, endpoint_index, &endpoint);
147 
148         /* Check the completion status.  */
149         if (status == UX_SUCCESS)
150         {
151 
152             /* Check if endpoint is bulk and IN.  */
153             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
154                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
155             {
156 
157                 /* This transfer_request always have the IN direction.  */
158                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
159 
160                 /* There is a callback function associated with the transfer request, so we need the class instance.  */
161                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_class_instance =  (VOID *) asix;
162 
163                 /* We have found the bulk endpoint, save it.  */
164                 asix -> ux_host_class_asix_bulk_in_endpoint =  endpoint;
165                 break;
166             }
167         }
168     }
169 
170     /* The bulk in endpoint is mandatory.  */
171     if (asix -> ux_host_class_asix_bulk_in_endpoint == UX_NULL)
172     {
173 
174         /* Error trap. */
175         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
176 
177         /* If trace is enabled, insert this event into the trace buffer.  */
178         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, asix, 0, 0, UX_TRACE_ERRORS, 0, 0)
179 
180         return(UX_ENDPOINT_HANDLE_UNKNOWN);
181     }
182 
183     /* Search the Interrupt endpoint. It is mandatory.  */
184     for (endpoint_index = 0; endpoint_index < asix -> ux_host_class_asix_interface -> ux_interface_descriptor.bNumEndpoints;
185                         endpoint_index++)
186     {
187 
188         /* Get the endpoint handle.  */
189         status =  _ux_host_stack_interface_endpoint_get(asix -> ux_host_class_asix_interface, endpoint_index, &endpoint);
190 
191         /* Check the completion status.  */
192         if (status == UX_SUCCESS)
193         {
194 
195             /* Check if endpoint is Interrupt and IN.  */
196             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
197                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
198             {
199 
200                 /* This transfer_request always have the IN direction.  */
201                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
202 
203                 /* We have found the interrupt endpoint, save it.  */
204                 asix -> ux_host_class_asix_interrupt_endpoint =  endpoint;
205 
206                 /* The endpoint is correct, Fill in the transfer request with the length requested for this endpoint.  */
207                 transfer_request =  &asix -> ux_host_class_asix_interrupt_endpoint -> ux_endpoint_transfer_request;
208                 transfer_request -> ux_transfer_request_requested_length =  transfer_request -> ux_transfer_request_packet_length;
209                 transfer_request -> ux_transfer_request_actual_length =     0;
210 
211                 /* The direction is always IN for the CDC interrupt endpoint.  */
212                 transfer_request -> ux_transfer_request_type =  UX_REQUEST_IN;
213 
214                 /* There is a callback function associated with the transfer request, so we need the class instance.  */
215                 transfer_request -> ux_transfer_request_class_instance =  (VOID *) asix;
216 
217                 /* Interrupt transactions have a completion routine. */
218                 transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_asix_interrupt_notification;
219 
220                 /* Obtain a buffer for this transaction. The buffer will always be reused.  */
221                 transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
222                                                                 transfer_request -> ux_transfer_request_requested_length);
223 
224                 /* If the endpoint is available and we have memory, it's started later after setup.  */
225                 if (transfer_request -> ux_transfer_request_data_pointer == UX_NULL)
226                 {
227 
228                     /* Error trap. */
229                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_MEMORY_INSUFFICIENT);
230 
231                     /* If trace is enabled, insert this event into the trace buffer.  */
232                     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_MEMORY_INSUFFICIENT, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
233 
234                     /* We must return an error.  */
235                     return(UX_ENDPOINT_HANDLE_UNKNOWN);
236                 }
237 
238                 break;
239             }
240         }
241     }
242 
243     /* The interrupt endpoint is mandatory.  */
244     if (asix -> ux_host_class_asix_interrupt_endpoint == UX_NULL)
245     {
246 
247         /* Error trap. */
248         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
249 
250         /* If trace is enabled, insert this event into the trace buffer.  */
251         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, asix, 0, 0, UX_TRACE_ERRORS, 0, 0)
252 
253         return(UX_ENDPOINT_HANDLE_UNKNOWN);
254     }
255     else
256 
257         /* All endpoints have been mounted.  */
258         return(UX_SUCCESS);
259 }
260 
261