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 /** Pima 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_pima.h"
30 #include "ux_host_stack.h"
31
32
33 #if UX_SLAVE_REQUEST_DATA_MAX_LENGTH < UX_HOST_CLASS_PIMA_CONTAINER_SIZE
34 #error UX_SLAVE_REQUEST_DATA_MAX_LENGTH too small, please check
35 #endif
36
37 /**************************************************************************/
38 /* */
39 /* FUNCTION RELEASE */
40 /* */
41 /* _ux_host_class_pima_read PORTABLE C */
42 /* 6.1.12 */
43 /* AUTHOR */
44 /* */
45 /* Chaoqiong Xiao, Microsoft Corporation */
46 /* */
47 /* DESCRIPTION */
48 /* */
49 /* This function reads a data payload from the Pima device. This */
50 /* function first read a header followed by some data. */
51 /* */
52 /* Note header only transfer and partial transfer is not accepted. */
53 /* */
54 /* INPUT */
55 /* */
56 /* pima Pointer to pima class */
57 /* data_pointer Pointer to data to read */
58 /* data_length Length of data to read */
59 /* callback_function Application function to call */
60 /* to get the rest of the data */
61 /* max_payload_data Maximum data received in one */
62 /* data payload. */
63 /* */
64 /* OUTPUT */
65 /* */
66 /* Completion Status */
67 /* */
68 /* CALLS */
69 /* */
70 /* _ux_host_stack_transfer_request Process transfer request */
71 /* _ux_host_stack_transfer_request_abort Abort transfer request */
72 /* _ux_host_stack_endpoint_reset Reset endpoint */
73 /* _ux_host_semaphore_get Get protection semaphore */
74 /* _ux_utility_long_get Get a long 32 bit value */
75 /* _ux_utility_short_get Get a short 16 bit value */
76 /* _ux_utility_memory_copy Copy memory */
77 /* */
78 /* CALLED BY */
79 /* */
80 /* Application */
81 /* */
82 /* RELEASE HISTORY */
83 /* */
84 /* DATE NAME DESCRIPTION */
85 /* */
86 /* 05-19-2020 Chaoqiong Xiao Initial Version 6.0 */
87 /* 09-30-2020 Chaoqiong Xiao Modified comment(s), */
88 /* prefixed UX to MS_TO_TICK, */
89 /* verified memset and memcpy */
90 /* cases, */
91 /* resulting in version 6.1 */
92 /* 01-31-2022 Chaoqiong Xiao Modified comment(s), */
93 /* refined macros names, */
94 /* resulting in version 6.1.10 */
95 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
96 /* improved length checks, */
97 /* resulting in version 6.1.12 */
98 /* */
99 /**************************************************************************/
_ux_host_class_pima_read(UX_HOST_CLASS_PIMA * pima,UCHAR * data_pointer,ULONG data_length,ULONG max_payload_length)100 UINT _ux_host_class_pima_read(UX_HOST_CLASS_PIMA *pima, UCHAR *data_pointer,
101 ULONG data_length,
102 ULONG max_payload_length)
103
104 {
105
106 UX_TRANSFER *transfer_request;
107 UINT status;
108 ULONG header_length;
109 UCHAR *ptp_payload;
110 ULONG requested_length;
111 ULONG payload_length;
112
113 /* If trace is enabled, insert this event into the trace buffer. */
114 UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_READ, pima, data_pointer, data_length, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
115
116 /* We use the Bulk In pipe for receiving data .. */
117 transfer_request = &pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_transfer_request;
118
119 /* Get the pointer to the ptp payload. */
120 ptp_payload = pima -> ux_host_class_pima_container ;
121
122 /* Calculate the requested length for this payload. */
123 requested_length = UX_HOST_CLASS_PIMA_CONTAINER_SIZE;
124
125 /* Initialize the transfer_request. */
126 transfer_request -> ux_transfer_request_data_pointer = ptp_payload;
127 transfer_request -> ux_transfer_request_requested_length = requested_length;
128
129 /* Send request to HCD layer. */
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_PIMA_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 /* The endpoint was halted by a transfer error and needs to be reset. */
147 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
148
149 /* The endpoint was halted by a transfer error and needs to be reset. */
150 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
151
152 /* There was an error, return to the caller. */
153 return(status);
154 }
155 }
156 else
157 {
158
159 /* There was a non transfer error, no partial transfer to be checked */
160 return(status);
161 }
162
163 /* Ensure the transfer is greater than the size of a PIMA header. */
164 if (transfer_request -> ux_transfer_request_actual_length <= UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE)
165
166 /* We have a malformed packet. Return error. */
167 return(UX_CLASS_MALFORMED_PACKET_RECEIVED_ERROR);
168
169 /* Get the expected length from the header. */
170 header_length = _ux_utility_long_get(ptp_payload + UX_HOST_CLASS_PIMA_DATA_HEADER_LENGTH);
171
172 /* Device reported data length should be equal or more than this packet
173 * (and UX_HOST_CLASS_PIMA_DATA_HEADER_LENGTH). */
174 if (header_length < transfer_request -> ux_transfer_request_actual_length)
175 return(UX_CLASS_MALFORMED_PACKET_RECEIVED_ERROR);
176
177 /* Check for remainder in last packet. */
178 if ((header_length % pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_descriptor.wMaxPacketSize) == 0)
179
180 /* We have a ZLP condition on a IN. */
181 pima -> ux_host_class_pima_zlp_flag = UX_HOST_CLASS_PIMA_ZLP_IN;
182 else
183
184 /* Do not expect a ZLP. */
185 pima -> ux_host_class_pima_zlp_flag = UX_HOST_CLASS_PIMA_ZLP_NONE;
186
187 /* The length returned should be smaller than the length requested. */
188 if ((header_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE) > data_length)
189 return(UX_ERROR);
190
191 /* We may have had data in the first packet, if so adjust the data_length. */
192 data_length = header_length - transfer_request -> ux_transfer_request_actual_length;
193
194 /* Copying the necessary partial memory. */
195 _ux_utility_memory_copy(data_pointer, ptp_payload + UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE,
196 transfer_request -> ux_transfer_request_actual_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE); /* Use case of memcpy is verified. */
197
198 /* Adjust the data payload pointer. */
199 data_pointer += (transfer_request -> ux_transfer_request_actual_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE);
200
201 /* Now we can read the data from the device. */
202 while(data_length)
203 {
204
205 /* Check if need to split the data payload into smaller packets. */
206 if (data_length > max_payload_length)
207
208 /* We cannot send everything in this payload. */
209 payload_length = max_payload_length;
210 else
211
212 /* Either this is the last packet or we we have a small packet to send. */
213 payload_length = data_length;
214
215 /* Initialize the transfer_request. */
216 transfer_request -> ux_transfer_request_data_pointer = data_pointer;
217 transfer_request -> ux_transfer_request_requested_length = payload_length;
218
219 /* Send request to HCD layer. */
220 status = _ux_host_stack_transfer_request(transfer_request);
221
222 /* If the transfer is successful, we need to wait for the transfer request to be completed. */
223 if (status == UX_SUCCESS)
224 {
225
226 /* Wait for the completion of the transfer request. */
227 status = _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PIMA_CLASS_TRANSFER_TIMEOUT));
228
229 /* If the semaphore did not succeed we probably have a time out. */
230 if (status != UX_SUCCESS)
231 {
232
233 /* All transfers pending need to abort. There may have been a partial transfer. */
234 _ux_host_stack_transfer_request_abort(transfer_request);
235
236 /* The endpoint was halted by a transfer error and needs to be reset. */
237 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
238
239 /* The endpoint was halted by a transfer error and needs to be reset. */
240 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
241
242 /* There was an error, return to the caller. */
243 return(status);
244 }
245 }
246 else
247 {
248
249 /* There was a non transfer error, no partial transfer to be checked */
250 return(status);
251 }
252
253 /* Check for completion of transfer. If the transfer is partial, return to caller.
254 Partial transfer is not OK. */
255 if (payload_length != transfer_request -> ux_transfer_request_actual_length)
256 return(UX_TRANSFER_ERROR);
257
258 /* Adjust the total length to transfer. */
259 data_length -= payload_length;
260
261 /* Adjust the data pointer. */
262 data_pointer += payload_length;
263
264 }
265
266 /* If we have a ZLP condition, read from the device one more time with a zero packet. */
267 if (pima -> ux_host_class_pima_zlp_flag == UX_HOST_CLASS_PIMA_ZLP_IN)
268 {
269
270 /* Initialize the transfer_request. */
271 transfer_request -> ux_transfer_request_data_pointer = UX_NULL;
272 transfer_request -> ux_transfer_request_requested_length = 0;
273
274 /* Send request to HCD layer. */
275 status = _ux_host_stack_transfer_request(transfer_request);
276
277 /* If the transfer is successful, we need to wait for the transfer request to be completed. */
278 if (status == UX_SUCCESS)
279 {
280
281 /* Wait for the completion of the transfer request. */
282 status = _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PIMA_CLASS_TRANSFER_TIMEOUT));
283
284 /* If the semaphore did not succeed we probably have a time out. */
285 if (status != UX_SUCCESS)
286 {
287
288 /* All transfers pending need to abort. There may have been a partial transfer. */
289 _ux_host_stack_transfer_request_abort(transfer_request);
290
291 /* The endpoint was halted by a transfer error and needs to be reset. */
292 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
293
294 /* The endpoint was halted by a transfer error and needs to be reset. */
295 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
296
297 /* There was an error, return to the caller. */
298 return(status);
299 }
300
301 /* Reset the ZLP. */
302 pima -> ux_host_class_pima_zlp_flag = UX_HOST_CLASS_PIMA_ZLP_NONE;
303 }
304 }
305
306 /* We have finished receiving the data. */
307 return(UX_SUCCESS);
308 }
309
310