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