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