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_write                       PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function writes to the prolific interface. The call is blocking*/
46 /*    and only returns when there is either an error or when the transfer */
47 /*    is complete.                                                        */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    prolific                              Pointer to prolific class     */
52 /*    data_pointer                          Pointer to data to write      */
53 /*    requested_length                      Length of data to write       */
54 /*    actual_length                         Actual length of data written */
55 /*                                                                        */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_transfer_request       Process transfer request      */
63 /*    _ux_host_stack_transfer_request_abort Abort transfer request        */
64 /*    _ux_host_semaphore_get                Get protection semaphore      */
65 /*                                                                        */
66 /*  CALLED BY                                                             */
67 /*                                                                        */
68 /*    Application                                                         */
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 /*                                            prefixed UX to MS_TO_TICK,  */
77 /*                                            resulting in version 6.1    */
78 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
79 /*                                            refined macros names,       */
80 /*                                            resulting in version 6.1.10 */
81 /*                                                                        */
82 /**************************************************************************/
_ux_host_class_prolific_write(UX_HOST_CLASS_PROLIFIC * prolific,UCHAR * data_pointer,ULONG requested_length,ULONG * actual_length)83 UINT  _ux_host_class_prolific_write(UX_HOST_CLASS_PROLIFIC *prolific, UCHAR *data_pointer,
84                                     ULONG requested_length, ULONG *actual_length)
85 {
86 
87 UX_TRANSFER     *transfer_request;
88 UINT            status;
89 ULONG           transfer_request_length;
90 
91     /* If trace is enabled, insert this event into the trace buffer.  */
92     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PROLIFIC_WRITE, prolific, data_pointer, requested_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
93 
94     /* Ensure the instance is valid.  */
95     if (prolific -> ux_host_class_prolific_state !=  UX_HOST_CLASS_INSTANCE_LIVE)
96     {
97 
98         /* Error trap. */
99         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_HOST_CLASS_INSTANCE_UNKNOWN);
100 
101         /* If trace is enabled, insert this event into the trace buffer.  */
102         UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_HOST_CLASS_INSTANCE_UNKNOWN, prolific, 0, 0, UX_TRACE_ERRORS, 0, 0)
103 
104         return(UX_HOST_CLASS_INSTANCE_UNKNOWN);
105     }
106 
107     /* Start by resetting the actual length of the transfer.  */
108     *actual_length =  0;
109 
110     /* Get the pointer to the bulk out endpoint transfer request.  */
111     transfer_request =  &prolific -> ux_host_class_prolific_bulk_out_endpoint -> ux_endpoint_transfer_request;
112 
113     /* Perform a transfer on the bulk out endpoint until either the transfer is
114        completed or when there is an error.  */
115     while (requested_length)
116     {
117 
118         /* Program the maximum authorized length for this transfer_request.  */
119         if (requested_length > transfer_request -> ux_transfer_request_maximum_length)
120             transfer_request_length =  transfer_request -> ux_transfer_request_maximum_length;
121         else
122             transfer_request_length =  requested_length;
123 
124         /* Initialize the transfer_request.  */
125         transfer_request -> ux_transfer_request_data_pointer =  data_pointer;
126         transfer_request -> ux_transfer_request_requested_length =  transfer_request_length;
127 
128         /* Perform the transfer.  */
129         status =  _ux_host_stack_transfer_request(transfer_request);
130 
131         /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
132         if (status == UX_SUCCESS)
133         {
134 
135             /* Wait for the completion of the transfer request.  */
136             status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PROLIFIC_CLASS_TRANSFER_TIMEOUT));
137 
138             /* If the semaphore did not succeed we probably have a time out.  */
139             if (status != UX_SUCCESS)
140             {
141 
142                 /* All transfers pending need to abort. There may have been a partial transfer.  */
143                 _ux_host_stack_transfer_request_abort(transfer_request);
144 
145                 /* Update the length of the actual data transferred. We do this after the
146                    abort of the transfer_request in case some data actually went out.  */
147                 *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
148 
149                 /* Set the completion code.  */
150                 transfer_request -> ux_transfer_request_completion_code =  UX_TRANSFER_TIMEOUT;
151 
152                 /* Error trap. */
153                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_TRANSFER_TIMEOUT);
154 
155                 /* If trace is enabled, insert this event into the trace buffer.  */
156                 UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_TRANSFER_TIMEOUT, transfer_request, 0, 0, UX_TRACE_ERRORS, 0, 0)
157 
158                 /* There was an error, return to the caller.  */
159                 return(UX_TRANSFER_TIMEOUT);
160             }
161         }
162         else
163         {
164 
165             /* There was a non transfer error, no partial transfer to be checked */
166             return(status);
167         }
168 
169         /* Update the length of the transfer. Normally all the data has to be sent.  */
170         *actual_length +=  transfer_request -> ux_transfer_request_actual_length;
171 
172         /* Check for completion status.  */
173         if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
174             return(transfer_request -> ux_transfer_request_completion_code);
175 
176         /* Check for completion of transfer. If the transfer is partial, return to caller.
177            The transfer is marked as successful but the caller will need to check the length
178            actually sent and determine if a partial transfer is OK. */
179         if (transfer_request_length !=  transfer_request -> ux_transfer_request_actual_length)
180         {
181 
182             /* Return success.  */
183             return(UX_SUCCESS);
184         }
185 
186         /* Update the data pointer for next transfer.  */
187         data_pointer +=  transfer_request_length;
188 
189         /* Update what is left to send out.  */
190         requested_length -=  transfer_request_length;
191     }
192 
193     /* We get here when all the transfers went through without errors.  */
194     return(UX_SUCCESS);
195 }
196 
197