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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
|
/***************************************************************************
* 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 */
/** */
/** Device Pima Class */
/** */
/**************************************************************************/
/**************************************************************************/
#define UX_SOURCE_CODE
/* Include necessary system files. */
#include "ux_api.h"
#include "ux_device_class_pima.h"
#include "ux_device_stack.h"
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_device_class_pima_object_data_get PORTABLE C */
/* 6.1.10 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function returns the object data to the host. */
/* */
/* INPUT */
/* */
/* pima Pointer to pima class */
/* */
/* OUTPUT */
/* */
/* Completion Status */
/* */
/* CALLS */
/* */
/* _ux_device_stack_transfer_request Transfer request */
/* _ux_utility_long_put Put 32-bit value */
/* _ux_utility_short_put Put 32-bit value */
/* _ux_device_class_pima_response_send Send PIMA response */
/* _ux_device_stack_endpoint_stall Stall endpoint */
/* */
/* CALLED BY */
/* */
/* Device Storage Class */
/* */
/**************************************************************************/
UINT _ux_device_class_pima_object_data_get(UX_SLAVE_CLASS_PIMA *pima, ULONG object_handle)
{
UINT status;
UX_SLAVE_TRANSFER *transfer_request;
UX_SLAVE_CLASS_PIMA_OBJECT *object;
UCHAR *object_data;
ULONG object_offset;
ULONG object_length;
ULONG total_length;
ULONG object_length_demanded;
ULONG object_length_received;
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_PIMA_OBJECT_DATA_GET, pima, object_handle, 0, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
/* Obtain the object info from the application. */
status = pima -> ux_device_class_pima_object_info_get(pima, object_handle, &object);
/* Check for error. */
if (status == UX_SUCCESS)
{
/* Data phase (Bulk IN). */
pima -> ux_device_class_pima_state = UX_DEVICE_CLASS_PIMA_PHASE_DATA_IN;
/* Set the object length. */
object_length = object -> ux_device_class_pima_object_compressed_size;
/* Set the total length to be sent. */
total_length = object_length + UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE;
/* Reset the offset. */
object_offset = 0;
/* Obtain the pointer to the transfer request of the bulk in endpoint. */
transfer_request = &pima -> ux_device_class_pima_bulk_in_endpoint -> ux_slave_endpoint_transfer_request;
/* Obtain memory for this object info. Use the transfer request pre-allocated memory. */
object_data = transfer_request -> ux_slave_transfer_request_data_pointer;
/* Fill in the total length to be sent (header + payload. */
_ux_utility_long_put(object_data + UX_DEVICE_CLASS_PIMA_DATA_HEADER_LENGTH,
total_length);
/* Fill in the data container type. */
_ux_utility_short_put(object_data + UX_DEVICE_CLASS_PIMA_DATA_HEADER_TYPE,
UX_DEVICE_CLASS_PIMA_CT_DATA_BLOCK);
/* Fill in the data code. */
_ux_utility_short_put(object_data + UX_DEVICE_CLASS_PIMA_DATA_HEADER_CODE,
UX_DEVICE_CLASS_PIMA_OC_GET_OBJECT);
/* Fill in the Transaction ID. */
_ux_utility_long_put(object_data + UX_DEVICE_CLASS_PIMA_DATA_HEADER_TRANSACTION_ID,
pima -> ux_device_class_pima_transaction_id);
/* Assuming the host will ask for the entire object. */
while (object_length != 0)
{
/* If this is the first packet, we have to take into account the
header. */
if (object_offset == 0)
{
/* Calculate the maximum length for the first packet. */
object_length_demanded = UX_DEVICE_CLASS_PIMA_TRANSFER_BUFFER_LENGTH -
UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE;
/* Can we get that much from the application ? */
if (object_length_demanded > object_length)
/* We ask too much. */
object_length_demanded = object_length;
/* Obtain some data from the application. */
status = pima -> ux_device_class_pima_object_data_get(pima, object_handle,
object_data + UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE,
object_offset,
object_length_demanded,
&object_length_received);
/* Check status, if we have a problem, we abort. */
if (status != UX_SUCCESS)
{
/* We need to inform the host of an error. */
break;
}
else
{
/* Do some sanity check. */
if (object_length < object_length_received)
{
/* We have an overflow. Do not proceed. */
status = UX_DEVICE_CLASS_PIMA_RC_GENERAL_ERROR;
break;
}
/* Adjust the length of the object. */
object_length -= object_length_received;
/* Adjust the offset within the object data. */
object_offset += object_length_received;
/* Adjust the length to be sent. */
object_length_received += UX_DEVICE_CLASS_PIMA_DATA_HEADER_SIZE;
}
}
else
{
/* Calculate the maximum length for the first packet. */
object_length_demanded = UX_DEVICE_CLASS_PIMA_TRANSFER_BUFFER_LENGTH;
/* Can we get that much from the application ? */
if (object_length_demanded > object_length)
/* We ask too much. */
object_length_demanded = object_length;
/* Obtain some data from the application. */
status = pima -> ux_device_class_pima_object_data_get(pima, object_handle, object_data, object_offset,
object_length_demanded,
&object_length_received);
/* Check status, if we have a problem, we abort. */
if (status != UX_SUCCESS)
{
/* We need to inform the host of an error. */
break;
}
else
{
/* Do some sanity check. */
if (object_length < object_length_received)
{
/* We have an overflow. Do not proceed. */
status = UX_DEVICE_CLASS_PIMA_RC_GENERAL_ERROR;
break;
}
/* Adjust the length of the object. */
object_length -= object_length_received;
/* Adjust the offset within the object data. */
object_offset += object_length_received;
}
}
/* It's canceled, do not proceed. */
if (pima -> ux_device_class_pima_state == UX_DEVICE_CLASS_PIMA_PHASE_IDLE)
{
pima -> ux_device_class_pima_device_status = UX_DEVICE_CLASS_PIMA_RC_OK;
return(UX_ERROR);
}
/* Not do transfer, just send the object data to the host. */
status = _ux_device_stack_transfer_request(transfer_request,
object_length_received, UX_DEVICE_CLASS_PIMA_TRANSFER_BUFFER_LENGTH);
/* It's canceled, do not proceed. */
if (pima -> ux_device_class_pima_state == UX_DEVICE_CLASS_PIMA_PHASE_IDLE)
{
pima -> ux_device_class_pima_device_status = UX_DEVICE_CLASS_PIMA_RC_OK;
return(UX_ERROR);
}
/* Check for the status. We may have had a request to cancel the transaction from the host. */
if (status != UX_SUCCESS)
{
/* Check the completion code for transfer abort from the host. */
if (transfer_request -> ux_slave_transfer_request_status == UX_TRANSFER_STATUS_ABORT)
{
/* Do not proceed. */
return(UX_ERROR);
}
else
{
/* We need to inform the host of an error. */
status = UX_DEVICE_CLASS_PIMA_RC_GENERAL_ERROR;
break;
}
}
else
{
/* Update the total length to be sent. */
total_length -= object_length_received;
}
}
/* Now we return a response. */
if (status == UX_SUCCESS)
_ux_device_class_pima_response_send(pima, UX_DEVICE_CLASS_PIMA_RC_OK, 0, 0, 0, 0);
}
/* Check if status is OK. */
if (status != UX_SUCCESS)
{
/* We need to stall the bulk in pipe. This is the method used by Pima devices to
cancel a transaction. */
_ux_device_stack_endpoint_stall(pima -> ux_device_class_pima_bulk_in_endpoint);
}
/* Return completion status. */
return(status);
}
|