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 /** USBX Component                                                        */
16 /**                                                                       */
17 /**   Device PIMA Class                                                   */
18 /**                                                                       */
19 /**************************************************************************/
20 /**************************************************************************/
21 
22 #define UX_SOURCE_CODE
23 
24 
25 /* Include necessary system files.  */
26 
27 #include "ux_api.h"
28 #include "ux_device_class_pima.h"
29 #include "ux_device_stack.h"
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_pima_response_send                 PORTABLE C      */
36 /*                                                           6.1.10       */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function returns a response to a pima command to the host.     */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    pima                                  Pointer to pima class         */
48 /*    response_code                         Response code                 */
49 /*                                                                        */
50 /*  OUTPUT                                                                */
51 /*                                                                        */
52 /*    Completion Status                                                   */
53 /*                                                                        */
54 /*  CALLS                                                                 */
55 /*                                                                        */
56 /*    _ux_device_stack_transfer_request     Transfer request              */
57 /*    _ux_utility_long_put                  Put 32-bit value              */
58 /*    _ux_utility_short_put                 Put 32-bit value              */
59 /*    _ux_device_class_pima_response_send   Send PIMA response            */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Device Storage Class                                                */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  05-19-2020     Chaoqiong Xiao           Initial Version 6.0           */
70 /*  09-30-2020     Chaoqiong Xiao           Modified comment(s),          */
71 /*                                            resulting in version 6.1    */
72 /*  01-31-2022     Chaoqiong Xiao           Modified comment(s),          */
73 /*                                            updated command phase,      */
74 /*                                            resulting in version 6.1.10 */
75 /*                                                                        */
76 /**************************************************************************/
_ux_device_class_pima_response_send(UX_SLAVE_CLASS_PIMA * pima,ULONG response_code,ULONG number_parameters,ULONG pima_parameter_1,ULONG pima_parameter_2,ULONG pima_parameter_3)77 UINT  _ux_device_class_pima_response_send(UX_SLAVE_CLASS_PIMA *pima, ULONG response_code,
78                       ULONG number_parameters,
79                       ULONG pima_parameter_1,
80                       ULONG pima_parameter_2,
81                       ULONG pima_parameter_3)
82 {
83 
84 UINT                    status;
85 UX_SLAVE_TRANSFER       *transfer_request;
86 UCHAR                   *response;
87 ULONG                   header_size;
88 
89     /* If trace is enabled, insert this event into the trace buffer.  */
90     UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PIMA_RESPONSE_SEND, pima, response_code, number_parameters, pima_parameter_1, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
91 
92     /* Obtain the pointer to the transfer request.  */
93     transfer_request =  &pima -> ux_device_class_pima_bulk_in_endpoint -> ux_slave_endpoint_transfer_request;
94 
95     /* Calculate the header size.  */
96     header_size = UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE + (ULONG)(sizeof(ULONG) * number_parameters);
97 
98     /* Obtain memory for this response. Use the transfer request pre-allocated memory.  */
99     response =  transfer_request -> ux_slave_transfer_request_data_pointer;
100 
101     /* Fill in the size of the response header.  */
102     _ux_utility_long_put(response + UX_DEVICE_CLASS_PIMA_RESPONSE_HEADER_LENGTH, header_size);
103 
104     /* Fill in the response container type.  */
105     _ux_utility_short_put(response + UX_DEVICE_CLASS_PIMA_RESPONSE_HEADER_TYPE,
106                             UX_DEVICE_CLASS_PIMA_CT_RESPONSE_BLOCK);
107 
108     /* Fill in the response code.  */
109     _ux_utility_short_put(response + UX_DEVICE_CLASS_PIMA_RESPONSE_HEADER_CODE,
110             (USHORT)response_code);
111 
112     /* Fill in the Transaction ID.  */
113     _ux_utility_long_put(response + UX_DEVICE_CLASS_PIMA_RESPONSE_HEADER_TRANSACTION_ID,
114                             pima -> ux_device_class_pima_transaction_id);
115 
116     /* Parse each parameter and insert it if needed.  */
117     switch (number_parameters)
118     {
119 
120         case 3 :
121             _ux_utility_long_put(response + UX_DEVICE_CLASS_PIMA_RESPONSE_HEADER_PARAMETERS + (sizeof(ULONG) * 2),
122                                  pima_parameter_3);
123             /* Intentionally fallthrough to "case 2" */
124             /* fall through */
125         case 2 :
126             _ux_utility_long_put(response + UX_DEVICE_CLASS_PIMA_RESPONSE_HEADER_PARAMETERS + (sizeof(ULONG) * 1),
127                                  pima_parameter_2);
128             /* Intentionally fallthrough to "case 1" */
129             /* fall through */
130         case 1 :
131             _ux_utility_long_put(response + UX_DEVICE_CLASS_PIMA_RESPONSE_HEADER_PARAMETERS ,
132                                  pima_parameter_1);
133             /* Intentionally fallthrough to "default" */
134             /* fall through */
135         default :
136             break;
137 
138     }
139 
140     /* Set phase to response.  */
141     pima -> ux_device_class_pima_state = UX_DEVICE_CLASS_PIMA_PHASE_RESPONSE;
142 
143     /* Send the response block.  */
144     status =  _ux_device_stack_transfer_request(transfer_request, header_size, 0);
145 
146     /* Return completion status.  */
147     return(status);
148 }
149 
150