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
|
/***************************************************************************
* 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 DPUMP Class */
/** */
/**************************************************************************/
/**************************************************************************/
#define UX_SOURCE_CODE
/* Include necessary system files. */
#include "ux_api.h"
#include "ux_device_class_dpump.h"
#include "ux_device_stack.h"
#if defined(UX_DEVICE_STANDALONE)
#define UX_DEVICE_CLASS_DPUMP_WRITE_START (UX_STATE_STEP + 1)
#define UX_DEVICE_CLASS_DPUMP_WRITE_WAIT (UX_STATE_STEP + 2)
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_device_class_dpump_write_run PORTABLE C */
/* 6.3.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function writes to the DPUMP class. */
/* */
/* It's for standalone mode. */
/* */
/* INPUT */
/* */
/* dpump DPUMP class instance */
/* buffer Buffer data to write */
/* requested_length Bytes to write */
/* actual_length Bytes written */
/* */
/* OUTPUT */
/* */
/* State machine Status to check */
/* UX_STATE_NEXT Transfer done, to next state */
/* UX_STATE_EXIT Abnormal, to reset state */
/* (others) Keep running, waiting */
/* */
/* CALLS */
/* */
/* _ux_device_stack_transfer_request Request transfer */
/* _ux_utility_memory_copy Copy memory */
/* */
/* CALLED BY */
/* */
/* Application */
/* */
/**************************************************************************/
UINT _ux_device_class_dpump_write_run(UX_SLAVE_CLASS_DPUMP *dpump, UCHAR *buffer,
ULONG requested_length, ULONG *actual_length)
{
UX_SLAVE_ENDPOINT *endpoint;
UX_SLAVE_DEVICE *device;
UX_SLAVE_TRANSFER *transfer_request;
UINT status;
UINT zlp = UX_FALSE;
UINT read_state;
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_DEVICE_CLASS_DPUMP_WRITE, dpump, buffer, requested_length, 0, UX_TRACE_DEVICE_CLASS_EVENTS, 0, 0)
/* Get the pointer to the device. */
device = &_ux_system_slave -> ux_system_slave_device;
/* As long as the device is in the CONFIGURED state. */
if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
/* If trace is enabled, insert this event into the trace buffer. */
UX_TRACE_IN_LINE_INSERT(UX_TRACE_ERROR, UX_CONFIGURATION_HANDLE_UNKNOWN, device, 0, 0, UX_TRACE_ERRORS, 0, 0)
/* Cannot proceed with command, the interface is down. */
dpump -> ux_device_class_dpump_write_state = UX_STATE_RESET;
dpump -> ux_device_class_dpump_write_status = UX_CONFIGURATION_HANDLE_UNKNOWN;
return(UX_STATE_EXIT);
}
/* Locate the OUT endpoint. */
endpoint = dpump -> ux_slave_class_dpump_bulkin_endpoint;
/* Check endpoint. If NULL, we have not yet received the proper SET_INTERFACE command. */
if (endpoint == UX_NULL)
{
/* Error trap. */
_ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_ENDPOINT_HANDLE_UNKNOWN);
dpump -> ux_device_class_dpump_write_state = UX_STATE_RESET;
dpump -> ux_device_class_dpump_write_status = UX_ENDPOINT_HANDLE_UNKNOWN;
return(UX_STATE_EXIT);
}
/* All DPUMP reading are on the endpoint OUT, from the host. */
transfer_request = &endpoint -> ux_slave_endpoint_transfer_request;
/* Handle state cases. */
read_state = dpump -> ux_device_class_dpump_write_state;
switch(read_state)
{
case UX_STATE_RESET:
dpump -> ux_device_class_dpump_write_state = UX_DEVICE_CLASS_DPUMP_WRITE_START;
dpump -> ux_device_class_dpump_write_status = UX_TRANSFER_NO_ANSWER;
dpump -> ux_device_class_dpump_write_buffer = buffer;
dpump -> ux_device_class_dpump_write_requested_length = requested_length;
dpump -> ux_device_class_dpump_write_actual_length = 0;
if (requested_length == 0)
zlp = UX_TRUE;
/* Fall through. */
case UX_DEVICE_CLASS_DPUMP_WRITE_START:
/* Get remaining requested length. */
requested_length = dpump -> ux_device_class_dpump_write_requested_length -
dpump -> ux_device_class_dpump_write_actual_length;
/* There is no remaining, we are done. */
if (requested_length == 0 && !zlp)
{
*actual_length = dpump -> ux_device_class_dpump_write_actual_length;
dpump -> ux_device_class_dpump_write_state = UX_STATE_RESET;
dpump -> ux_device_class_dpump_write_status = UX_SUCCESS;
return(UX_STATE_NEXT);
}
/* Check if we have enough in the local buffer. */
if (requested_length > UX_DEVICE_CLASS_DPUMP_WRITE_BUFFER_SIZE)
/* We have too much to transfer. */
dpump -> ux_device_class_dpump_write_transfer_length = UX_DEVICE_CLASS_DPUMP_WRITE_BUFFER_SIZE;
else
/* We can proceed with the demanded length. */
dpump -> ux_device_class_dpump_write_transfer_length = requested_length;
/* On a out, we copy the buffer to the caller. Not very efficient but it makes the API
easier. */
_ux_utility_memory_copy(transfer_request -> ux_slave_transfer_request_data_pointer,
dpump -> ux_device_class_dpump_write_buffer,
dpump -> ux_device_class_dpump_write_transfer_length); /* Use case of memcpy is verified. */
/* Next state. */
dpump -> ux_device_class_dpump_write_state = UX_DEVICE_CLASS_DPUMP_WRITE_WAIT;
UX_SLAVE_TRANSFER_STATE_RESET(transfer_request);
/* Fall through. */
case UX_DEVICE_CLASS_DPUMP_WRITE_WAIT:
/* Send the request to the device controller. */
status = _ux_device_stack_transfer_run(transfer_request,
dpump -> ux_device_class_dpump_write_transfer_length,
dpump -> ux_device_class_dpump_write_transfer_length);
/* Error case. */
if (status < UX_STATE_NEXT)
{
dpump -> ux_device_class_dpump_write_state = UX_STATE_RESET;
dpump -> ux_device_class_dpump_write_status =
transfer_request -> ux_slave_transfer_request_completion_code;
return(UX_STATE_EXIT);
}
/* Success case. */
if (status == UX_STATE_NEXT)
{
/* Next buffer address. */
dpump -> ux_device_class_dpump_write_buffer +=
transfer_request -> ux_slave_transfer_request_actual_length;
/* Set the length actually received. */
dpump -> ux_device_class_dpump_write_actual_length +=
transfer_request -> ux_slave_transfer_request_actual_length;
/* Last transfer status. */
dpump -> ux_device_class_dpump_write_status =
transfer_request -> ux_slave_transfer_request_completion_code;
/* Update actual length. */
*actual_length = dpump -> ux_device_class_dpump_write_actual_length;
/* Check ZLP case. */
if (dpump -> ux_device_class_dpump_write_requested_length == 0)
{
dpump -> ux_device_class_dpump_write_state = UX_STATE_RESET;
return(UX_STATE_NEXT);
}
/* Next state. */
dpump -> ux_device_class_dpump_write_state = UX_DEVICE_CLASS_DPUMP_WRITE_START;
}
/* Keep waiting. */
return(UX_STATE_WAIT);
default: /* Error. */
dpump -> ux_device_class_dpump_write_state = UX_STATE_RESET;
break;
}
/* Error case. */
return(UX_STATE_EXIT);
}
#endif
|