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 /* */
33 /* FUNCTION RELEASE */
34 /* */
35 /* _ux_device_class_pima_control_request PORTABLE C */
36 /* 6.1.12 */
37 /* AUTHOR */
38 /* */
39 /* Chaoqiong Xiao, Microsoft Corporation */
40 /* */
41 /* DESCRIPTION */
42 /* */
43 /* This function manages the based sent by the host on the control */
44 /* endpoints with a CLASS or VENDOR SPECIFIC type. */
45 /* */
46 /* INPUT */
47 /* */
48 /* pima Pointer to pima class */
49 /* */
50 /* OUTPUT */
51 /* */
52 /* None */
53 /* */
54 /* CALLS */
55 /* */
56 /* _ux_device_stack_transfer_request Transfer request */
57 /* _ux_device_stack_transfer_abort Transfer abort */
58 /* _ux_device_class_pima_event_set Put PIMA event into queue */
59 /* _ux_utility_short_put Put 16-bit value into buffer */
60 /* */
61 /* CALLED BY */
62 /* */
63 /* PIMA 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 /* improved request handling, */
74 /* resulting in version 6.1.10 */
75 /* 07-29-2022 Chaoqiong Xiao Modified comment(s), */
76 /* fixed parameter/variable */
77 /* names conflict C++ keyword, */
78 /* resulting in version 6.1.12 */
79 /* */
80 /**************************************************************************/
_ux_device_class_pima_control_request(UX_SLAVE_CLASS_COMMAND * command)81 UINT _ux_device_class_pima_control_request(UX_SLAVE_CLASS_COMMAND *command)
82 {
83
84 UX_SLAVE_DCD *dcd;
85 UX_SLAVE_ENDPOINT *endpoint;
86 UX_SLAVE_TRANSFER *transfer_request;
87 UX_SLAVE_DEVICE *device;
88 UX_SLAVE_CLASS *class_ptr;
89 ULONG request;
90 ULONG request_length;
91 ULONG length;
92 UCHAR *request_data;
93 UX_SLAVE_CLASS_PIMA *pima;
94 ULONG transaction_id;
95
96 /* Get the pointer to the DCD. */
97 dcd = &_ux_system_slave -> ux_system_slave_dcd;
98
99 /* Get the pointer to the device. */
100 device = &_ux_system_slave -> ux_system_slave_device;
101
102 /* Get the pointer to the transfer request associated with the control endpoint. */
103 transfer_request = &device -> ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
104
105 /* Extract all necessary fields of the request. */
106 request = *(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_REQUEST);
107 request_data = transfer_request -> ux_slave_transfer_request_data_pointer;
108 request_length = _ux_utility_short_get(transfer_request -> ux_slave_transfer_request_setup + UX_SETUP_LENGTH);
109
110 /* Get the class container. */
111 class_ptr = command -> ux_slave_class_command_class_ptr;
112
113 /* Get the storage instance from this class container. */
114 pima = (UX_SLAVE_CLASS_PIMA *) class_ptr -> ux_slave_class_instance;
115
116 /* Here we proceed only the standard request we know of at the device level. */
117 switch (request)
118 {
119
120 case UX_DEVICE_CLASS_PIMA_REQUEST_CANCEL_COMMAND:
121
122 /* Validate length. */
123 if (request_length != 6)
124 return(UX_ERROR);
125
126 /* Validate Cancellation code (0x4001). */
127 if (request_data[0] != 0x01 || request_data[1] != 0x40)
128 return(UX_ERROR);
129
130 /* Obtain TransactionID. */
131 transaction_id = _ux_utility_long_get(request_data + 2);
132 if (transaction_id != pima -> ux_device_class_pima_transaction_id)
133 return(UX_ERROR);
134
135 /* Cancel data phase only. */
136 if (pima -> ux_device_class_pima_state > UX_DEVICE_CLASS_PIMA_PHASE_RESPONSE)
137 {
138
139 /* Abort, device is busy until data thread end. */
140 pima -> ux_device_class_pima_device_status = UX_DEVICE_CLASS_PIMA_RC_DEVICE_BUSY;
141
142 if (pima -> ux_device_class_pima_state == UX_DEVICE_CLASS_PIMA_PHASE_DATA_IN)
143 _ux_device_stack_transfer_abort(&pima -> ux_device_class_pima_bulk_in_endpoint -> ux_slave_endpoint_transfer_request, UX_ABORTED);
144 else
145 _ux_device_stack_transfer_abort(&pima -> ux_device_class_pima_bulk_out_endpoint -> ux_slave_endpoint_transfer_request, UX_ABORTED);
146
147 /* Invoke callback if available, let application cancel/close related objects. */
148 if (pima -> ux_device_class_pima_cancel)
149 pima -> ux_device_class_pima_cancel(pima);
150
151 /* Set state to idle, time consuming threads detect it to break the loop. */
152 pima -> ux_device_class_pima_state = UX_DEVICE_CLASS_PIMA_PHASE_IDLE;
153 }
154 break ;
155
156 case UX_DEVICE_CLASS_PIMA_REQUEST_STATUS_COMMAND:
157
158 /* Validate length. */
159 if (request_length < 2)
160 return(UX_ERROR);
161
162 /* Fill the status data payload. Initialize length to 4. */
163 length = 4;
164
165 /* Fill status. */
166 _ux_utility_short_put(request_data + 2,
167 pima -> ux_device_class_pima_device_status);
168
169 /* Fill device status actual length. */
170 _ux_utility_short_put(request_data, (USHORT)length);
171 length = UX_MIN(request_length, length);
172
173 /* Change status, error is reported only once. */
174 pima->ux_device_class_pima_device_status = UX_DEVICE_CLASS_PIMA_RC_OK;
175
176 /* We have a request to obtain the status of the MTP. */
177 _ux_device_stack_transfer_request(transfer_request, length, request_length);
178 break ;
179
180 case UX_DEVICE_CLASS_PIMA_REQUEST_RESET_DEVICE:
181
182 /* Clear stall. */
183 endpoint = pima -> ux_device_class_pima_bulk_in_endpoint;
184 if (endpoint->ux_slave_endpoint_state == UX_ENDPOINT_HALTED)
185 {
186 dcd -> ux_slave_dcd_function(dcd, UX_DCD_RESET_ENDPOINT, endpoint);
187 endpoint -> ux_slave_endpoint_state = UX_ENDPOINT_RESET;
188 }
189 else
190 _ux_device_stack_transfer_abort(&endpoint->ux_slave_endpoint_transfer_request, UX_ABORTED);
191 endpoint = pima -> ux_device_class_pima_bulk_out_endpoint;
192 if (endpoint->ux_slave_endpoint_state == UX_ENDPOINT_HALTED)
193 {
194 dcd -> ux_slave_dcd_function(dcd, UX_DCD_RESET_ENDPOINT, endpoint);
195 endpoint -> ux_slave_endpoint_state = UX_ENDPOINT_RESET;
196 }
197 else
198 _ux_device_stack_transfer_abort(&endpoint->ux_slave_endpoint_transfer_request, UX_ABORTED);
199
200 /* Reset session. */
201 pima -> ux_device_class_pima_device_reset(pima);
202
203 /* Reset status. */
204 pima -> ux_device_class_pima_session_id = 0;
205 pima -> ux_device_class_pima_device_status = UX_DEVICE_CLASS_PIMA_RC_OK;
206 break;
207
208 default:
209
210 /* Unknown function. It's not handled. */
211 return(UX_ERROR);
212 }
213
214 /* It's handled. */
215 return(UX_SUCCESS);
216 }
217