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_notification                    PORTABLE C      */
38 /*                                                           6.1          */
39 /*  AUTHOR                                                                */
40 /*                                                                        */
41 /*    Chaoqiong Xiao, Microsoft Corporation                               */
42 /*                                                                        */
43 /*  DESCRIPTION                                                           */
44 /*                                                                        */
45 /*    This function is called by the completion thread when a transfer    */
46 /*    request has been completed either because the transfer is           */
47 /*    successful or there was an error.                                   */
48 /*                                                                        */
49 /*  INPUT                                                                 */
50 /*                                                                        */
51 /*    transfer_request                      Pointer to transfer request   */
52 /*                                                                        */
53 /*  OUTPUT                                                                */
54 /*                                                                        */
55 /*    None                                                                */
56 /*                                                                        */
57 /*  CALLS                                                                 */
58 /*                                                                        */
59 /*    _ux_host_stack_transfer_request       Transfer request              */
60 /*    _ux_utility_memory_copy               Copy memory                   */
61 /*    _ux_utility_short_get                 Get 16-bit value              */
62 /*    _ux_utility_long_get                  Get 32-bit value              */
63 /*                                                                        */
64 /*  CALLED BY                                                             */
65 /*                                                                        */
66 /*    USBX stack                                                          */
67 /*                                                                        */
68 /*  RELEASE HISTORY                                                       */
69 /*                                                                        */
70 /*    DATE              NAME                      DESCRIPTION             */
71 /*                                                                        */
72 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
73 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
74 /*                                            verified memset and memcpy  */
75 /*                                            cases,                      */
76 /*                                            resulting in version 6.1    */
77 /*                                                                        */
78 /**************************************************************************/
_ux_host_class_pima_notification(UX_TRANSFER * transfer_request)79 VOID  _ux_host_class_pima_notification(UX_TRANSFER *transfer_request)
80 {
81 
82 UX_HOST_CLASS_PIMA                       *pima;
83 UX_HOST_CLASS_PIMA_SESSION                 *pima_session;
84 UX_HOST_CLASS_PIMA_EVENT                pima_event;
85 
86     /* Get the class instance for this transfer request.  */
87     pima =  (UX_HOST_CLASS_PIMA *) transfer_request -> ux_transfer_request_class_instance;
88 
89     /* Check the state of the transfer.  If there is an error, we do not proceed with this notification.  */
90     if (transfer_request -> ux_transfer_request_completion_code != UX_SUCCESS)
91     {
92 
93         /* We have an error. We do not rehook another transfer if the device instance is shutting down or
94            if the transfer was aborted by the class..  */
95         if ((pima -> ux_host_class_pima_state ==  UX_HOST_CLASS_INSTANCE_SHUTDOWN) ||
96             (transfer_request -> ux_transfer_request_completion_code == UX_TRANSFER_STATUS_ABORT))
97 
98             /* We do not proceed.  */
99             return;
100         else
101 
102         {
103 
104             /* Reactivate the PIMA interrupt pipe.  */
105             _ux_host_stack_transfer_request(transfer_request);
106 
107             /* We do not proceed.  */
108             return;
109         }
110 
111     }
112 
113     /* Check if this packet is the first, if so we have the total length expected in the event.  */
114     if (pima -> ux_host_class_pima_event_buffer_current_length == 0)
115     {
116 
117         /* First packet. Maybe the only one needed. It may happen that the notification event is split amongst
118            several interrupt packets.  */
119         pima -> ux_host_class_pima_event_buffer_expected_length  = _ux_utility_long_get(transfer_request -> ux_transfer_request_data_pointer
120                                                                                             + UX_HOST_CLASS_PIMA_AEI_DATA_LENGTH);
121 
122         /* Set the current offset to the beginning of the buffer.  */
123         pima -> ux_host_class_pima_event_buffer_current_offset =  pima -> ux_host_class_pima_event_buffer;
124 
125     }
126 
127     /* Check the length of this payload and make sure we have enough space.  */
128     if ((pima -> ux_host_class_pima_event_buffer_current_length + transfer_request -> ux_transfer_request_actual_length) <=
129             UX_HOST_CLASS_PIMA_AEI_MAX_LENGTH)
130     {
131 
132         /* We copy the current payload into our event notification buffer.  */
133         _ux_utility_memory_copy(pima -> ux_host_class_pima_event_buffer_current_offset, transfer_request -> ux_transfer_request_data_pointer,
134                             transfer_request -> ux_transfer_request_actual_length); /* Use case of memcpy is verified. */
135 
136         /* Set the new offset address. */
137         pima -> ux_host_class_pima_event_buffer_current_offset += transfer_request -> ux_transfer_request_actual_length;
138 
139         /* Adjust the length.  */
140         pima -> ux_host_class_pima_event_buffer_current_length += transfer_request -> ux_transfer_request_actual_length;
141     }
142     else
143 
144         /* We come here when we have a buffer overflow. Do not proceed.  */
145         return;
146 
147     /* Check if we have a complete notification event.  */
148     if (pima -> ux_host_class_pima_event_buffer_current_length == pima -> ux_host_class_pima_event_buffer_expected_length)
149     {
150 
151         /* Save the current event in the pima instance.  First, unpack the event code.  */
152         pima -> ux_host_class_pima_event_code = _ux_utility_short_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_EVENT_CODE);
153 
154         /* Unpack the Transaction ID.  */
155         pima -> ux_host_class_pima_event_transaction_id = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_TRANSACTION_ID);
156 
157         /* Unpack the parameter 1.  */
158         pima -> ux_host_class_pima_event_parameter_1 = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_PARAMETER_1);
159 
160         /* Unpack the parameter 2.  */
161         pima -> ux_host_class_pima_event_parameter_2 = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_PARAMETER_2);
162 
163         /* Unpack the parameter 3.  */
164         pima -> ux_host_class_pima_event_parameter_3 = _ux_utility_long_get(pima -> ux_host_class_pima_event_buffer + UX_HOST_CLASS_PIMA_AEI_PARAMETER_3);
165 
166         /* Check if a session is valid.  */
167         if (pima -> ux_host_class_pima_session != UX_NULL)
168         {
169 
170             /* Get session pointer.  */
171             pima_session =  pima -> ux_host_class_pima_session;
172 
173             /* Check if this session is valid or not.  */
174             if ((pima_session -> ux_host_class_pima_session_magic == UX_HOST_CLASS_PIMA_MAGIC_NUMBER) &&
175                (pima_session -> ux_host_class_pima_session_state == UX_HOST_CLASS_PIMA_SESSION_STATE_OPENED))
176             {
177 
178                 /* If the application demanded a callback when event occur, create a event notification
179                    message.  */
180                 if (pima_session -> ux_host_class_pima_session_event_callback != UX_NULL)
181                 {
182 
183                     /* Fill in the pima event structure. First with pima instance.  */
184                     pima_event.ux_host_class_pima_event_pima_instance = pima;
185 
186                     /* Fill in the opened session pointer.  Since the pima class only supports
187                        one session, this address is hardwired to the opened session and is not coming from
188                        the event buffer.  */
189                     pima_event.ux_host_class_pima_event_session = pima_session;
190 
191                     /* Fill in all the event parameters in the event callback structure.  */
192                     pima_event.ux_host_class_pima_event_code =  pima -> ux_host_class_pima_event_code;
193                     pima_event.ux_host_class_pima_event_transaction_id =  pima -> ux_host_class_pima_event_transaction_id;
194                     pima_event.ux_host_class_pima_event_parameter_1 =  pima -> ux_host_class_pima_event_parameter_1;
195                     pima_event.ux_host_class_pima_event_parameter_2 =  pima -> ux_host_class_pima_event_parameter_2;
196                     pima_event.ux_host_class_pima_event_parameter_3 =  pima -> ux_host_class_pima_event_parameter_3;
197 
198                     /* Send this event to the application.  */
199                     pima_session -> ux_host_class_pima_session_event_callback(&pima_event);
200 
201                     /* If trace is enabled, insert this event into the trace buffer.  */
202                     UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_NOTIFICATION, pima,
203                                             pima -> ux_host_class_pima_event_code,
204                                             pima -> ux_host_class_pima_event_transaction_id,
205                                             pima -> ux_host_class_pima_event_parameter_1,
206                                             UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
207 
208                 }
209             }
210         }
211 
212         /* We will receive a complete new transaction.  */
213         pima -> ux_host_class_pima_event_buffer_current_length =  0;
214     }
215     /* Reactivate the PIMA interrupt pipe.  */
216     _ux_host_stack_transfer_request(transfer_request);
217 
218     /* Return to caller.  */
219     return;
220 }
221 
222