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