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