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_object_get                      PORTABLE C      */
38 /*                                                           6.1.10       */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function gets an object identified by the object_handle        */
46 /*  INPUT                                                                 */
47 /*                                                                        */
48 /*    pima                                       Pointer to pima class    */
49 /*    pima_session                               Pointer to pima session  */
50 /*    object_handle                              The object handle        */
51 /*    object                                     Pointer to object info   */
52 /*    object_buffer                              Buffer to be used        */
53 /*    object_buffer_length                       Buffer length            */
54 /*    object_actual_length                       Length read in that      */
55 /*                                               command                  */
56 /*  OUTPUT                                                                */
57 /*                                                                        */
58 /*    Completion Status                                                   */
59 /*                                                                        */
60 /*  CALLS                                                                 */
61 /*                                                                        */
62 /*    _ux_host_stack_transfer_request            Transfer request         */
63 /*    _ux_host_stack_transfer_request_abort      Abort transfer           */
64 /*    _ux_host_stack_endpoint_reset              Reset endpoint           */
65 /*    _ux_host_semaphore_get                     Get semaphore            */
66 /*    _ux_utility_memory_copy                    Copy memory              */
67 /*    _ux_utility_long_get                       Get 32-bit value         */
68 /*    _ux_utility_long_put                       Put 32-bit value         */
69 /*    _ux_utility_short_put                      Put 16-bit value         */
70 /*                                                                        */
71 /*  CALLED BY                                                             */
72 /*                                                                        */
73 /*    USB 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_object_get(UX_HOST_CLASS_PIMA * pima,UX_HOST_CLASS_PIMA_SESSION * pima_session,ULONG object_handle,UX_HOST_CLASS_PIMA_OBJECT * object,UCHAR * object_buffer,ULONG object_buffer_length,ULONG * object_actual_length)90 UINT  _ux_host_class_pima_object_get(UX_HOST_CLASS_PIMA *pima,
91                                         UX_HOST_CLASS_PIMA_SESSION *pima_session,
92                                         ULONG object_handle, UX_HOST_CLASS_PIMA_OBJECT *object,
93                                         UCHAR *object_buffer, ULONG object_buffer_length,
94                                         ULONG *object_actual_length)
95 {
96 
97 UX_HOST_CLASS_PIMA_COMMAND             command;
98 UX_TRANSFER                         *transfer_request;
99 UCHAR                                 *ptp_payload;
100 ULONG                                requested_length;
101 ULONG                               total_length;
102 UINT                                status;
103 
104     /* If trace is enabled, insert this event into the trace buffer.  */
105     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_OBJECT_GET, pima, object_handle, object, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
106 
107     /* Check if this session is valid or not.  */
108     if (pima_session -> ux_host_class_pima_session_magic != UX_HOST_CLASS_PIMA_MAGIC_NUMBER)
109         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
110 
111     /* Check if this session is opened or not.  */
112     if (pima_session -> ux_host_class_pima_session_state != UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED)
113         return (UX_HOST_CLASS_PIMA_RC_SESSION_NOT_OPEN);
114 
115     /* Check if the object is already opened.  */
116     if (object -> ux_host_class_pima_object_state != UX_HOST_CLASS_PIMA_OBJECT_STATE_OPENED)
117         return (UX_HOST_CLASS_PIMA_RC_OBJECT_NOT_OPENED);
118 
119     /* Check the transfer status. If there was an error or transfer is completed, refuse transfer.  */
120     if ((object -> ux_host_class_pima_object_transfer_status == UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_COMPLETED) ||
121         (object -> ux_host_class_pima_object_transfer_status == UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_ABORTED))
122         return (UX_HOST_CLASS_PIMA_RC_ACCESS_DENIED);
123 
124     /* Reset the actual length.  */
125     *object_actual_length =  0;
126 
127     /* This variable will remain untouched if the offset is not 0 and the requested length is 0.  */
128     status =  UX_SUCCESS;
129 
130     /* Check if the offset to be read is at 0, if so, prepare the first read command. */
131     if (object -> ux_host_class_pima_object_offset == 0)
132     {
133 
134         /* Set the object transfer status to active.  */
135         object -> ux_host_class_pima_object_transfer_status = UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_ACTIVE;
136 
137         /* Issue command to get the object info.  1 parameter.  */
138         command.ux_host_class_pima_command_nb_parameters =  1;
139 
140         /* Parameter 1 is the Object Handle.  */
141         command.ux_host_class_pima_command_parameter_1 =  object_handle;
142 
143         /* Then set the command to GET_OBJECT.  */
144         command.ux_host_class_pima_command_operation_code =  UX_HOST_CLASS_PIMA_OC_GET_OBJECT;
145 
146         /* We use the Bulk Out pipe for sending data out..  */
147         transfer_request =  &pima -> ux_host_class_pima_bulk_out_endpoint -> ux_endpoint_transfer_request;
148 
149         /* Get the pointer to the ptp payload.  */
150         ptp_payload =  pima -> ux_host_class_pima_container ;
151 
152         /* Calculate the requested length for this payload.  */
153         requested_length =  UX_HOST_CLASS_PIMA_COMMAND_HEADER_SIZE + ((ULONG)sizeof(ULONG) * command.ux_host_class_pima_command_nb_parameters);
154 
155         /* Fill the command container. First the length of the total header and payload.  */
156         _ux_utility_long_put(ptp_payload + UX_HOST_CLASS_PIMA_COMMAND_HEADER_LENGTH, requested_length);
157 
158 
159         /* Then the type of container : a command block here.  */
160         _ux_utility_short_put(ptp_payload + UX_HOST_CLASS_PIMA_COMMAND_HEADER_TYPE, UX_HOST_CLASS_PIMA_CT_COMMAND_BLOCK);
161 
162         /* Now the command code to send.  */
163         _ux_utility_short_put(ptp_payload + UX_HOST_CLASS_PIMA_COMMAND_HEADER_CODE, (USHORT)command.ux_host_class_pima_command_operation_code);
164 
165         /* Put the transaction ID.  */
166         _ux_utility_long_put(ptp_payload + UX_HOST_CLASS_PIMA_COMMAND_HEADER_TRANSACTION_ID,
167                         pima -> ux_host_class_pima_transaction_id++);
168 
169         /* Then fill in the parameters. */
170         _ux_utility_long_put(ptp_payload + UX_HOST_CLASS_PIMA_COMMAND_HEADER_PARAMETER_1,
171                             command.ux_host_class_pima_command_parameter_1);
172 
173         /* Initialize the transfer_request.  */
174         transfer_request -> ux_transfer_request_data_pointer =  ptp_payload;
175         transfer_request -> ux_transfer_request_requested_length =  requested_length;
176 
177         /* Send request to HCD layer.  */
178         status =  _ux_host_stack_transfer_request(transfer_request);
179 
180         /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
181         if (status == UX_SUCCESS)
182         {
183 
184             /* Wait for the completion of the transfer request.  */
185             status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PIMA_CLASS_TRANSFER_TIMEOUT));
186 
187             /* If the semaphore did not succeed we probably have a time out.  */
188             if (status != UX_SUCCESS || transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
189             {
190 
191                 /* All transfers pending need to abort. There may have been a partial transfer.  */
192                 _ux_host_stack_transfer_request_abort(transfer_request);
193 
194                 /* The endpoint was halted by a transfer error and needs to be reset.  */
195                 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
196 
197                 /* The endpoint was halted by a transfer error  and needs to be reset.  */
198                 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
199 
200                 /* Set the object transfer status to aborted.  */
201                 object -> ux_host_class_pima_object_transfer_status = UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_ABORTED;
202 
203                 /* There was an error, return to the caller.  */
204                 return(UX_TRANSFER_ERROR);
205             }
206         }
207         else
208         {
209 
210             /* There was a non transfer error, no partial transfer to be checked */
211             return(status);
212         }
213 
214         /* Check for completion of transfer. If the transfer is partial, return to caller.
215            Partial transfer is not OK. */
216         if (requested_length == transfer_request -> ux_transfer_request_actual_length)
217         {
218 
219             /* Obtain the first packet.  This packet contains the header and some data.
220                We use the Bulk In pipe for receiving data ..  */
221             transfer_request =  &pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_transfer_request;
222 
223             /* Get the pointer to the ptp payload.  */
224             ptp_payload =  pima -> ux_host_class_pima_container ;
225 
226             /* Calculate the requested length for this payload. It is the minimum
227                of the application's requested length and the container size.  */
228             if (object_buffer_length < UX_HOST_CLASS_PIMA_CONTAINER_SIZE)
229                 requested_length = object_buffer_length;
230             else
231                 requested_length = UX_HOST_CLASS_PIMA_CONTAINER_SIZE;
232 
233             /* Initialize the transfer_request.  */
234             transfer_request -> ux_transfer_request_data_pointer =  ptp_payload;
235             transfer_request -> ux_transfer_request_requested_length =  requested_length;
236 
237             /* Send request to HCD layer.  */
238             status =  _ux_host_stack_transfer_request(transfer_request);
239 
240             /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
241             if (status == UX_SUCCESS)
242             {
243 
244                 /* Wait for the completion of the transfer request.  */
245                 status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PIMA_CLASS_TRANSFER_TIMEOUT));
246 
247                 /* If the semaphore did not succeed we probably have a time out.  */
248                 if (status != UX_SUCCESS  || transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
249                 {
250 
251                     /* All transfers pending need to abort. There may have been a partial transfer.  */
252                     _ux_host_stack_transfer_request_abort(transfer_request);
253 
254                     /* The endpoint was halted by a transfer error and needs to be reset.  */
255                     _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
256 
257                     /* The endpoint was halted by a transfer error  and needs to be reset.  */
258                     _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
259 
260                     /* Set the object transfer status to aborted.  */
261                     object -> ux_host_class_pima_object_transfer_status = UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_ABORTED;
262 
263                     /* There was an error, return to the caller.  */
264                     return(UX_TRANSFER_ERROR);
265                 }
266             }
267             else
268             {
269 
270                 /* There was a non transfer error, no partial transfer to be checked */
271                 return(status);
272             }
273 
274             /* Ensure the transfer is larger than the header.  */
275             if (transfer_request -> ux_transfer_request_actual_length > UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE)
276             {
277 
278                 /* We need to skip the header.  Copying the necessary partial memory only.  */
279                 _ux_utility_memory_copy(object_buffer, ptp_payload + UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE,
280                                         transfer_request -> ux_transfer_request_actual_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE); /* Use case of memcpy is verified. */
281 
282                 /* Get the size of the entire data payload to be expected in this object transfer (data + header). If the size is on a boundary
283                    the pima protocol demands that the last packet is a ZLP.  */
284                 total_length = _ux_utility_long_get(ptp_payload + UX_HOST_CLASS_PIMA_DATA_HEADER_LENGTH);
285 
286                 /* Check for remainder in last packet.  */
287                 if ((total_length % pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_descriptor.wMaxPacketSize) == 0)
288 
289                     /* We have a ZLP condition on a IN.  */
290                     pima -> ux_host_class_pima_zlp_flag = UX_HOST_CLASS_PIMA_ZLP_IN;
291                 else
292 
293                     /* Do not expect a ZLP.  */
294                     pima -> ux_host_class_pima_zlp_flag = UX_HOST_CLASS_PIMA_ZLP_NONE;
295 
296                 /* Compute the total length of the data only.  */
297                 total_length -= UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE;
298 
299                 /* Check if the expected length remaining will be more than the current buffer. If so, we need to have a full payload
300                    on a packet boundary.  */
301                 if (total_length > object_buffer_length)
302 
303                     /* Update what is left to be received.  */
304                     object_buffer_length -=  transfer_request -> ux_transfer_request_actual_length;
305 
306                 else
307 
308                     /* Update what is left to be received.  */
309                     object_buffer_length -=  transfer_request -> ux_transfer_request_actual_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE;
310 
311                 /* Update the actual length.  */
312                 *object_actual_length = transfer_request -> ux_transfer_request_actual_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE;
313 
314                 /* Update where we will store the next data.  */
315                 object_buffer += transfer_request -> ux_transfer_request_actual_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE;
316 
317                 /* And the offset.  */
318                 object -> ux_host_class_pima_object_offset += transfer_request -> ux_transfer_request_actual_length - UX_HOST_CLASS_PIMA_DATA_HEADER_SIZE;
319             }
320             else
321             {
322 
323                 /* The transfer is smaller than the header, which is an error.  */
324 
325                 /* Report error to application.  */
326                 _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CLASS_MALFORMED_PACKET_RECEIVED_ERROR);
327 
328                 /* Return error.  */
329                 return(UX_CLASS_MALFORMED_PACKET_RECEIVED_ERROR);
330             }
331         }
332         else
333 
334             /* We got a premature error.  */
335             return(UX_HOST_CLASS_PIMA_RC_INCOMPLETE_TRANSFER);
336     }
337 
338     /* We use the Bulk In pipe for receiving data ..  */
339     transfer_request =  &pima -> ux_host_class_pima_bulk_in_endpoint -> ux_endpoint_transfer_request;
340 
341     /* We read a complete block.  */
342     while(object_buffer_length != 0)
343     {
344 
345         /* It may take several transactions.  */
346         if (object_buffer_length > UX_HOST_CLASS_PIMA_MAX_PAYLOAD)
347 
348             /* Set the requested length to the payload maximum.  */
349             requested_length =  UX_HOST_CLASS_PIMA_MAX_PAYLOAD;
350 
351         else
352 
353             /* We can use the user supplied length to complete this request.  */
354             requested_length =  object_buffer_length;
355 
356         /* Initialize the transfer_request.  */
357         transfer_request -> ux_transfer_request_data_pointer =  object_buffer;
358         transfer_request -> ux_transfer_request_requested_length =  requested_length;
359 
360         /* Send request to HCD layer.  */
361         status =  _ux_host_stack_transfer_request(transfer_request);
362 
363         /* If the transfer is successful, we need to wait for the transfer request to be completed.  */
364         if (status == UX_SUCCESS)
365         {
366 
367             /* Wait for the completion of the transfer request.  */
368             status =  _ux_host_semaphore_get(&transfer_request -> ux_transfer_request_semaphore, UX_MS_TO_TICK(UX_HOST_CLASS_PIMA_CLASS_TRANSFER_TIMEOUT));
369 
370             /* If the semaphore did not succeed we probably have a time out.  */
371             if (status != UX_SUCCESS  || transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
372             {
373 
374                 /* All transfers pending need to abort. There may have been a partial transfer.  */
375                 _ux_host_stack_transfer_request_abort(transfer_request);
376 
377                 /* The endpoint was halted by a transfer error and needs to be reset.  */
378                 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_in_endpoint);
379 
380                 /* The endpoint was halted by a transfer error  and needs to be reset.  */
381                 _ux_host_stack_endpoint_reset(pima -> ux_host_class_pima_bulk_out_endpoint);
382 
383                 /* Set the object transfer status to aborted.  */
384                 object -> ux_host_class_pima_object_transfer_status = UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_ABORTED;
385 
386                 /* There was an error, return to the caller.  */
387                 return(UX_TRANSFER_ERROR);
388             }
389         }
390         else
391         {
392 
393             /* There was a non transfer error, no partial transfer to be checked */
394             return(status);
395         }
396 
397         /* Update the object length expected by the user with what we actually received.  */
398         object_buffer_length -= transfer_request -> ux_transfer_request_actual_length;
399 
400         /* Update the actual length.  */
401         *object_actual_length += transfer_request -> ux_transfer_request_actual_length;
402 
403         /* And the offset.  */
404         object -> ux_host_class_pima_object_offset += transfer_request -> ux_transfer_request_actual_length;
405 
406         /* And the object buffer pointer .  */
407         object_buffer += transfer_request -> ux_transfer_request_actual_length;
408 
409         /* Check to see if we are at the end of the object.  */
410         if (object -> ux_host_class_pima_object_offset == object -> ux_host_class_pima_object_compressed_size)
411 
412             /* The transfer for this transaction is completed.  */
413             object -> ux_host_class_pima_object_transfer_status = UX_HOST_CLASS_PIMA_OBJECT_TRANSFER_STATUS_COMPLETED;
414 
415     }
416 
417     /* Return completion status.  */
418     return(status);
419 }
420 
421