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 /**   Prolific 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_prolific.h"
30 #include "ux_host_stack.h"
31 
32 
33 /**************************************************************************/
34 /*                                                                        */
35 /*  FUNCTION                                               RELEASE        */
36 /*                                                                        */
37 /*    _ux_host_class_prolific_endpoints_get               PORTABLE C      */
38 /*                                                           6.1.11       */
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 /*    prolific                             Pointer to prolific class      */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    Completion Status                                                   */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_transfer_request       Transfer request              */
60 /*    _ux_host_stack_interface_endpoint_get Get interface endpoint        */
61 /*    _ux_utility_memory_allocate           Allocate memory               */
62 /*                                                                        */
63 /*  CALLED BY                                                             */
64 /*                                                                        */
65 /*    _ux_host_class_prolific_activate      Activate prolific 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 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            internal clean up,          */
80 /*                                            resulting in version 6.1.11 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_prolific_endpoints_get(UX_HOST_CLASS_PROLIFIC * prolific)83 UINT  _ux_host_class_prolific_endpoints_get(UX_HOST_CLASS_PROLIFIC *prolific)
84 {
85 
86 UINT            status;
87 UINT            endpoint_index;
88 UX_ENDPOINT     *endpoint;
89 UX_TRANSFER     *transfer_request;
90 
91 
92     /* Search the bulk OUT endpoint. It is attached to the interface container.  */
93     for (endpoint_index = 0; endpoint_index < prolific -> ux_host_class_prolific_interface -> ux_interface_descriptor.bNumEndpoints;
94                         endpoint_index++)
95     {
96 
97         /* Get interface endpoint.  */
98         status =  _ux_host_stack_interface_endpoint_get(prolific -> ux_host_class_prolific_interface, endpoint_index, &endpoint);
99 
100         /* Check the completion status.  */
101         if (status == UX_SUCCESS)
102         {
103 
104             /* Check if endpoint is bulk and OUT.  */
105             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
106                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
107             {
108 
109                 /* This transfer_request always have the OUT direction.  */
110                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;
111 
112                 /* We have found the bulk endpoint, save it.  */
113                 prolific -> ux_host_class_prolific_bulk_out_endpoint =  endpoint;
114                 break;
115             }
116         }
117     }
118 
119     /* The bulk out endpoint is mandatory.  */
120     if (prolific -> ux_host_class_prolific_bulk_out_endpoint == UX_NULL)
121     {
122 
123         /* Error trap. */
124         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
125 
126         /* If trace is enabled, insert this event into the trace buffer.  */
127         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, prolific, 0, 0, UX_TRACE_ERRORS, 0, 0)
128 
129         return(UX_ENDPOINT_HANDLE_UNKNOWN);
130     }
131 
132     /* Search the bulk IN endpoint. It is attached to the interface container.  */
133     for (endpoint_index = 0; endpoint_index < prolific -> ux_host_class_prolific_interface -> ux_interface_descriptor.bNumEndpoints;
134                         endpoint_index++)
135     {
136 
137         /* Get the endpoint handle.  */
138         status =  _ux_host_stack_interface_endpoint_get(prolific -> ux_host_class_prolific_interface, endpoint_index, &endpoint);
139 
140         /* Check the completion status.  */
141         if (status == UX_SUCCESS)
142         {
143 
144             /* Check if endpoint is bulk and IN.  */
145             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
146                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
147             {
148 
149                 /* This transfer_request always have the IN direction.  */
150                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
151 
152                 /* We have found the bulk endpoint, save it.  */
153                 prolific -> ux_host_class_prolific_bulk_in_endpoint =  endpoint;
154                 break;
155             }
156         }
157     }
158 
159     /* The bulk in endpoint is mandatory.  */
160     if (prolific -> ux_host_class_prolific_bulk_in_endpoint == UX_NULL)
161     {
162 
163         /* Error trap. */
164         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
165 
166         /* If trace is enabled, insert this event into the trace buffer.  */
167         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, prolific, 0, 0, UX_TRACE_ERRORS, 0, 0)
168 
169         return(UX_ENDPOINT_HANDLE_UNKNOWN);
170     }
171 
172     /* Search the Interrupt endpoint. It is mandatory.  */
173     for (endpoint_index = 0; endpoint_index < prolific -> ux_host_class_prolific_interface -> ux_interface_descriptor.bNumEndpoints;
174                         endpoint_index++)
175     {
176 
177         /* Get the endpoint handle.  */
178         status =  _ux_host_stack_interface_endpoint_get(prolific -> ux_host_class_prolific_interface, endpoint_index, &endpoint);
179 
180         /* Check the completion status.  */
181         if (status == UX_SUCCESS)
182         {
183 
184             /* Check if endpoint is Interrupt and IN.  */
185             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
186                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
187             {
188 
189                 /* This transfer_request always have the IN direction.  */
190                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
191 
192                 /* We have found the interrupt endpoint, save it.  */
193                 prolific -> ux_host_class_prolific_interrupt_endpoint =  endpoint;
194 
195                 /* The endpoint is correct, Fill in the transfer request with the length requested for this endpoint.  */
196                 transfer_request =  &prolific -> ux_host_class_prolific_interrupt_endpoint -> ux_endpoint_transfer_request;
197                 transfer_request -> ux_transfer_request_requested_length =  transfer_request -> ux_transfer_request_packet_length;
198                 transfer_request -> ux_transfer_request_actual_length =     0;
199 
200                 /* The direction is always IN for the CDC interrupt endpoint.  */
201                 transfer_request -> ux_transfer_request_type =  UX_REQUEST_IN;
202 
203                 /* There is a callback function associated with the transfer request, so we need the class instance.  */
204                 transfer_request -> ux_transfer_request_class_instance =  (VOID *) prolific;
205 
206                 /* Interrupt transactions have a completion routine. */
207                 transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_prolific_transfer_request_completed;
208 
209                 /* Obtain a buffer for this transaction. The buffer will always be reused.  */
210                 transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
211                                                                 transfer_request -> ux_transfer_request_requested_length);
212 
213                 /* If the endpoint is available and we have memory, we start the interrupt endpoint.  */
214                 if (transfer_request -> ux_transfer_request_data_pointer != UX_NULL)
215                 {
216 
217                     /* The transfer on the interrupt endpoint can be started.  */
218                     _ux_host_stack_transfer_request(transfer_request);
219 
220                 }
221 
222                 else
223                 {
224 
225                     /* Error trap. */
226                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
227 
228                     /* If trace is enabled, insert this event into the trace buffer.  */
229                     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
230 
231                     /* We must return an error.  */
232                     return(UX_ENDPOINT_HANDLE_UNKNOWN);
233                 }
234 
235                 break;
236             }
237         }
238     }
239 
240     /* The interrupt endpoint is mandatory.  */
241     if (prolific -> ux_host_class_prolific_interrupt_endpoint == UX_NULL)
242     {
243 
244         /* Error trap. */
245         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
246 
247         /* If trace is enabled, insert this event into the trace buffer.  */
248         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, prolific, 0, 0, UX_TRACE_ERRORS, 0, 0)
249 
250         return(UX_ENDPOINT_HANDLE_UNKNOWN);
251     }
252     else
253 
254         /* All endpoints have been mounted.  */
255         return(UX_SUCCESS);
256 }
257 
258