1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 
12 /**************************************************************************/
13 /**************************************************************************/
14 /**                                                                       */
15 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Prolific Class                                                      */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 
23 /* Include necessary system files.  */
24 
25 #define UX_SOURCE_CODE
26 
27 #include "ux_api.h"
28 #include "ux_host_class_prolific.h"
29 #include "ux_host_stack.h"
30 
31 
32 /**************************************************************************/
33 /*                                                                        */
34 /*  FUNCTION                                               RELEASE        */
35 /*                                                                        */
36 /*    _ux_host_class_prolific_endpoints_get               PORTABLE C      */
37 /*                                                           6.1.11       */
38 /*  AUTHOR                                                                */
39 /*                                                                        */
40 /*    Chaoqiong Xiao, Microsoft Corporation                               */
41 /*                                                                        */
42 /*  DESCRIPTION                                                           */
43 /*                                                                        */
44 /*    This function distinguishes for either the Data or Control Class.   */
45 /*    For the data class, we mount the bulk in and bulk out endpoints.    */
46 /*    For the control class, we mount the optional interrupt endpoint.    */
47 /*                                                                        */
48 /*  INPUT                                                                 */
49 /*                                                                        */
50 /*    prolific                             Pointer to prolific class      */
51 /*                                                                        */
52 /*  OUTPUT                                                                */
53 /*                                                                        */
54 /*    Completion Status                                                   */
55 /*                                                                        */
56 /*  CALLS                                                                 */
57 /*                                                                        */
58 /*    _ux_host_stack_transfer_request       Transfer request              */
59 /*    _ux_host_stack_interface_endpoint_get Get interface endpoint        */
60 /*    _ux_utility_memory_allocate           Allocate memory               */
61 /*                                                                        */
62 /*  CALLED BY                                                             */
63 /*                                                                        */
64 /*    _ux_host_class_prolific_activate      Activate prolific class       */
65 /*                                                                        */
66 /*  RELEASE HISTORY                                                       */
67 /*                                                                        */
68 /*    DATE              NAME                      DESCRIPTION             */
69 /*                                                                        */
70 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
71 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
72 /*                                            resulting in version 6.1    */
73 /*  10-15-2021     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            use pre-calculated value    */
75 /*                                            instead of wMaxPacketSize,  */
76 /*                                            resulting in version 6.1.9  */
77 /*  04-25-2022     Chaoqiong Xiao           Modified comment(s),          */
78 /*                                            internal clean up,          */
79 /*                                            resulting in version 6.1.11 */
80 /*                                                                        */
81 /**************************************************************************/
_ux_host_class_prolific_endpoints_get(UX_HOST_CLASS_PROLIFIC * prolific)82 UINT  _ux_host_class_prolific_endpoints_get(UX_HOST_CLASS_PROLIFIC *prolific)
83 {
84 
85 UINT            status;
86 UINT            endpoint_index;
87 UX_ENDPOINT     *endpoint;
88 UX_TRANSFER     *transfer_request;
89 
90 
91     /* Search the bulk OUT endpoint. It is attached to the interface container.  */
92     for (endpoint_index = 0; endpoint_index < prolific -> ux_host_class_prolific_interface -> ux_interface_descriptor.bNumEndpoints;
93                         endpoint_index++)
94     {
95 
96         /* Get interface endpoint.  */
97         status =  _ux_host_stack_interface_endpoint_get(prolific -> ux_host_class_prolific_interface, endpoint_index, &endpoint);
98 
99         /* Check the completion status.  */
100         if (status == UX_SUCCESS)
101         {
102 
103             /* Check if endpoint is bulk and OUT.  */
104             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT) &&
105                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
106             {
107 
108                 /* This transfer_request always have the OUT direction.  */
109                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_OUT;
110 
111                 /* We have found the bulk endpoint, save it.  */
112                 prolific -> ux_host_class_prolific_bulk_out_endpoint =  endpoint;
113                 break;
114             }
115         }
116     }
117 
118     /* The bulk out endpoint is mandatory.  */
119     if (prolific -> ux_host_class_prolific_bulk_out_endpoint == UX_NULL)
120     {
121 
122         /* Error trap. */
123         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONNECTION_INCOMPATIBLE);
124 
125         /* If trace is enabled, insert this event into the trace buffer.  */
126         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, prolific, 0, 0, UX_TRACE_ERRORS, 0, 0)
127 
128         return(UX_ENDPOINT_HANDLE_UNKNOWN);
129     }
130 
131     /* Search the bulk IN endpoint. It is attached to the interface container.  */
132     for (endpoint_index = 0; endpoint_index < prolific -> ux_host_class_prolific_interface -> ux_interface_descriptor.bNumEndpoints;
133                         endpoint_index++)
134     {
135 
136         /* Get the endpoint handle.  */
137         status =  _ux_host_stack_interface_endpoint_get(prolific -> ux_host_class_prolific_interface, endpoint_index, &endpoint);
138 
139         /* Check the completion status.  */
140         if (status == UX_SUCCESS)
141         {
142 
143             /* Check if endpoint is bulk and IN.  */
144             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
145                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_BULK_ENDPOINT))
146             {
147 
148                 /* This transfer_request always have the IN direction.  */
149                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
150 
151                 /* We have found the bulk endpoint, save it.  */
152                 prolific -> ux_host_class_prolific_bulk_in_endpoint =  endpoint;
153                 break;
154             }
155         }
156     }
157 
158     /* The bulk in endpoint is mandatory.  */
159     if (prolific -> ux_host_class_prolific_bulk_in_endpoint == UX_NULL)
160     {
161 
162         /* Error trap. */
163         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
164 
165         /* If trace is enabled, insert this event into the trace buffer.  */
166         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, prolific, 0, 0, UX_TRACE_ERRORS, 0, 0)
167 
168         return(UX_ENDPOINT_HANDLE_UNKNOWN);
169     }
170 
171     /* Search the Interrupt endpoint. It is mandatory.  */
172     for (endpoint_index = 0; endpoint_index < prolific -> ux_host_class_prolific_interface -> ux_interface_descriptor.bNumEndpoints;
173                         endpoint_index++)
174     {
175 
176         /* Get the endpoint handle.  */
177         status =  _ux_host_stack_interface_endpoint_get(prolific -> ux_host_class_prolific_interface, endpoint_index, &endpoint);
178 
179         /* Check the completion status.  */
180         if (status == UX_SUCCESS)
181         {
182 
183             /* Check if endpoint is Interrupt and IN.  */
184             if (((endpoint -> ux_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_IN) &&
185                 ((endpoint -> ux_endpoint_descriptor.bmAttributes & UX_MASK_ENDPOINT_TYPE) == UX_INTERRUPT_ENDPOINT))
186             {
187 
188                 /* This transfer_request always have the IN direction.  */
189                 endpoint -> ux_endpoint_transfer_request.ux_transfer_request_type =  UX_REQUEST_IN;
190 
191                 /* We have found the interrupt endpoint, save it.  */
192                 prolific -> ux_host_class_prolific_interrupt_endpoint =  endpoint;
193 
194                 /* The endpoint is correct, Fill in the transfer request with the length requested for this endpoint.  */
195                 transfer_request =  &prolific -> ux_host_class_prolific_interrupt_endpoint -> ux_endpoint_transfer_request;
196                 transfer_request -> ux_transfer_request_requested_length =  transfer_request -> ux_transfer_request_packet_length;
197                 transfer_request -> ux_transfer_request_actual_length =     0;
198 
199                 /* The direction is always IN for the CDC interrupt endpoint.  */
200                 transfer_request -> ux_transfer_request_type =  UX_REQUEST_IN;
201 
202                 /* There is a callback function associated with the transfer request, so we need the class instance.  */
203                 transfer_request -> ux_transfer_request_class_instance =  (VOID *) prolific;
204 
205                 /* Interrupt transactions have a completion routine. */
206                 transfer_request -> ux_transfer_request_completion_function =  _ux_host_class_prolific_transfer_request_completed;
207 
208                 /* Obtain a buffer for this transaction. The buffer will always be reused.  */
209                 transfer_request -> ux_transfer_request_data_pointer =  _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY,
210                                                                 transfer_request -> ux_transfer_request_requested_length);
211 
212                 /* If the endpoint is available and we have memory, we start the interrupt endpoint.  */
213                 if (transfer_request -> ux_transfer_request_data_pointer != UX_NULL)
214                 {
215 
216                     /* The transfer on the interrupt endpoint can be started.  */
217                     _ux_host_stack_transfer_request(transfer_request);
218 
219                 }
220 
221                 else
222                 {
223 
224                     /* Error trap. */
225                     _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
226 
227                     /* If trace is enabled, insert this event into the trace buffer.  */
228                     UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, endpoint, 0, 0, UX_TRACE_ERRORS, 0, 0)
229 
230                     /* We must return an error.  */
231                     return(UX_ENDPOINT_HANDLE_UNKNOWN);
232                 }
233 
234                 break;
235             }
236         }
237     }
238 
239     /* The interrupt endpoint is mandatory.  */
240     if (prolific -> ux_host_class_prolific_interrupt_endpoint == UX_NULL)
241     {
242 
243         /* Error trap. */
244         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
245 
246         /* If trace is enabled, insert this event into the trace buffer.  */
247         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_ENDPOINT_HANDLE_UNKNOWN, prolific, 0, 0, UX_TRACE_ERRORS, 0, 0)
248 
249         return(UX_ENDPOINT_HANDLE_UNKNOWN);
250     }
251     else
252 
253         /* All endpoints have been mounted.  */
254         return(UX_SUCCESS);
255 }
256 
257