1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
|
/***************************************************************************
* Copyright (c) 2024 Microsoft Corporation
* Copyright (c) 2026-present Eclipse ThreadX contributors
*
* This program and the accompanying materials are made available under the
* terms of the MIT License which is available at
* https://opensource.org/licenses/MIT.
*
* SPDX-License-Identifier: MIT
**************************************************************************/
/**************************************************************************/
/**************************************************************************/
/** */
/** USBX Component */
/** */
/** PIMA Class */
/** */
/**************************************************************************/
/**************************************************************************/
/* Include necessary system files. */
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_class_pima.h"
#include "ux_host_stack.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_host_class_pima_request_cancel PORTABLE C */
/* 6.1 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function cancels a request. */
/* INPUT */
/* */
/* pima Pointer to pima class */
/* */
/* OUTPUT */
/* */
/* Completion Status */
/* */
/* CALLS */
/* */
/* _ux_host_stack_transfer_request Transfer request */
/* _ux_utility_memory_allocate Allocate memory */
/* _ux_utility_memory_free Free memory */
/* _ux_utility_short_put Put 16-bit value */
/* _ux_utility_short_get Get 16-bit value */
/* _ux_utility_long_put Put 32-bit value */
/* _ux_utility_delay_ms Delay ms */
/* */
/* CALLED BY */
/* */
/* USB application */
/* */
/**************************************************************************/
UINT _ux_host_class_pima_request_cancel(UX_HOST_CLASS_PIMA *pima)
{
UX_TRANSFER *transfer_request;
UX_ENDPOINT *control_endpoint;
UCHAR *request_payload;
UINT status;
UINT device_status;
ULONG payload_length;
ULONG command_retry_counter;
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_PIMA_REQUEST_CANCEL, pima, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
/* Allocate some DMA safe memory for the command data payload. We use mode than needed because
we use this buffer for both the command and the status phase. */
request_payload = _ux_utility_memory_allocate(UX_SAFE_ALIGN, UX_CACHE_SAFE_MEMORY, UX_HOST_CLASS_PIMA_REQUEST_STATUS_DATA_LENGTH);
if (request_payload == UX_NULL)
return(UX_MEMORY_INSUFFICIENT);
/* Fill in the payload buffer with cancellation code. */
_ux_utility_short_put(request_payload + UX_HOST_CLASS_PIMA_REQUEST_CANCEL_OFFSET_CODE,
UX_HOST_CLASS_PIMA_REQUEST_CANCEL_CODE );
/* Fill in the payload buffer with transactionID. */
_ux_utility_long_put(request_payload + UX_HOST_CLASS_PIMA_REQUEST_CANCEL_OFFSET_TRANSACTION_ID,
pima -> ux_host_class_pima_transaction_id++);
/* We need to get the default control endpoint transfer request pointer. */
control_endpoint = &pima -> ux_host_class_pima_device -> ux_device_control_endpoint;
transfer_request = &control_endpoint -> ux_endpoint_transfer_request;
/* Create a transfer_request for the CANCEL request. */
transfer_request -> ux_transfer_request_data_pointer = request_payload;
transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_PIMA_REQUEST_CANCEL_DATA_LENGTH;
transfer_request -> ux_transfer_request_function = UX_HOST_CLASS_PIMA_REQUEST_CANCEL_COMMAND;
transfer_request -> ux_transfer_request_type = UX_REQUEST_OUT | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
transfer_request -> ux_transfer_request_value = 0;
transfer_request -> ux_transfer_request_index = 0;
/* Send request to HCD layer. */
status = _ux_host_stack_transfer_request(transfer_request);
/* Check if status is OK. If the command did not work, we have a major problem. */
if(status == UX_SUCCESS)
{
/* Initialize the command retry counter. */
command_retry_counter = UX_HOST_CLASS_PIMA_REQUEST_STATUS_COMMAND_COUNTER;
/* This command may be retried a few times. */
while (command_retry_counter-- != 0)
{
/* We need to wait for the device to be OK. For that we issue a GET_DEVICE_STATUS command. */
transfer_request -> ux_transfer_request_data_pointer = request_payload;
transfer_request -> ux_transfer_request_requested_length = UX_HOST_CLASS_PIMA_REQUEST_CANCEL_DATA_LENGTH;
transfer_request -> ux_transfer_request_function = UX_HOST_CLASS_PIMA_REQUEST_STATUS_COMMAND;
transfer_request -> ux_transfer_request_type = UX_REQUEST_IN | UX_REQUEST_TYPE_CLASS | UX_REQUEST_TARGET_INTERFACE;
transfer_request -> ux_transfer_request_value = 0;
transfer_request -> ux_transfer_request_index = 0;
/* Send request to HCD layer. */
status = _ux_host_stack_transfer_request(transfer_request);
/* Check status, abort if there is an error. */
if (status != UX_SUCCESS)
break;
/* Extract the payload length from the status buffer. */
payload_length = _ux_utility_short_get(request_payload + UX_HOST_CLASS_PIMA_REQUEST_STATUS_OFFSET_LENGTH);
/* Check the data payload length. */
if (payload_length < UX_HOST_CLASS_PIMA_REQUEST_STATUS_OFFSET_CODE + sizeof(UINT))
{
/* We have a data format error. */
status = UX_ERROR;
break;
}
/* Extract the device status. */
device_status = _ux_utility_short_get(request_payload + UX_HOST_CLASS_PIMA_REQUEST_STATUS_OFFSET_CODE);
/* If the device status is OK, we have a successful cancellation. */
if (device_status == UX_HOST_CLASS_PIMA_RC_OK)
{
/* Status is OK. exit the command loop. */
status = UX_SUCCESS;
break;
}
else
{
/* Force the status to error. */
status = UX_ERROR;
/* We should wait a little bit before re-issuing the command. */
_ux_utility_delay_ms(UX_HOST_CLASS_PIMA_REQUEST_STATUS_COMMAND_DELAY);
}
}
}
/* Free allocated resources. */
_ux_utility_memory_free(request_payload);
/* Return completion status. */
return(status);
}
|