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
|
/***************************************************************************/
/* Copyright (c) 2024 Microsoft Corporation */
/* Copyright (c) 2026 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 */
/***************************************************************************/
#include <stdio.h>
#include "tx_api.h"
#include "ux_api.h"
#include "ux_system.h"
#include "ux_utility.h"
#include "ux_device_stack.h"
#include "ux_test.h"
#include "ux_host_class_printer.h"
#include "ux_device_class_dummy_printer.h"
#define PORT_START 1
#define PORT_STATUS_PORT_POWER_BIT 8
UCHAR _ux_device_class_printer_name[] = "_ux_device_class_printer";
static VOID _ux_device_class_printer_initialize(UX_SLAVE_CLASS_COMMAND *command)
{
UX_SLAVE_CLASS *printer_class = (UX_SLAVE_CLASS *)command->ux_slave_class_command_class_ptr;
UX_DEVICE_CLASS_PRINTER_PARAMS *printer_params = (UX_DEVICE_CLASS_PRINTER_PARAMS *)command->ux_slave_class_command_parameter;
UX_DEVICE_CLASS_PRINTER *printer;
printer = (UX_DEVICE_CLASS_PRINTER *) _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_DEVICE_CLASS_PRINTER));
UX_TEST_ASSERT(printer != UX_NULL);
printer_class->ux_slave_class_instance = printer;
/* Save parameters. */
_ux_utility_memory_copy(&printer->params, printer_params, sizeof(*printer_params));
}
static VOID _ux_device_class_printer_uninitialize(UX_SLAVE_CLASS_COMMAND *command)
{
UX_DEVICE_CLASS_PRINTER *printer = NULL;
UX_SLAVE_CLASS *class;
/* Get the class container. */
class = command -> ux_slave_class_command_class_ptr;
/* Get the class instance in the container. */
printer = (UX_DEVICE_CLASS_PRINTER *) class -> ux_slave_class_instance;
if (printer != UX_NULL)
_ux_utility_memory_free(printer);
return;
}
static UINT _ux_device_class_printer_activate(UX_SLAVE_CLASS_COMMAND *command)
{
UX_SLAVE_CLASS *printer_class = command->ux_slave_class_command_class_ptr;
UX_SLAVE_INTERFACE *interface = (UX_SLAVE_INTERFACE *)command->ux_slave_class_command_interface;
UX_SLAVE_ENDPOINT *endpoint;
UX_DEVICE_CLASS_PRINTER *printer;
UINT i;
printer = (UX_DEVICE_CLASS_PRINTER *)printer_class->ux_slave_class_instance;
interface -> ux_slave_interface_class_instance = (VOID *)printer;
printer -> interface = interface;
/* We need to get the bulk endpoints. */
printer -> bulk_in_endpoint = UX_NULL;
printer -> bulk_out_endpoint = UX_NULL;
endpoint = interface->ux_slave_interface_first_endpoint;
while (endpoint)
{
/* Is this bulk endpoint? */
if (endpoint->ux_slave_endpoint_descriptor.bmAttributes == UX_BULK_ENDPOINT)
{
/* Is this endpoint IN? */
if (endpoint->ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_IN)
{
printer -> bulk_in_endpoint = endpoint;
if (printer -> bulk_out_endpoint)
break;
}
else
{
printer -> bulk_out_endpoint = endpoint;
if (printer -> bulk_in_endpoint)
break;
}
}
endpoint = endpoint->ux_slave_endpoint_next_endpoint;
}
/* Endpoints must be available. */
if (printer->bulk_in_endpoint == UX_NULL || printer->bulk_out_endpoint == UX_NULL)
return(UX_ERROR);
/* Invoke activate callback. */
if (printer->params.instance_activate)
{
printer->params.instance_activate(printer);
}
return(UX_SUCCESS);
}
static VOID _ux_device_class_printer_deactivate(UX_SLAVE_CLASS_COMMAND *command)
{
UX_SLAVE_CLASS *printer_class = command->ux_slave_class_command_class_ptr;
UX_DEVICE_CLASS_PRINTER *printer;
printer = (UX_DEVICE_CLASS_PRINTER *)printer_class->ux_slave_class_instance;
_ux_device_stack_transfer_all_request_abort(printer->bulk_in_endpoint, UX_TRANSFER_BUS_RESET);
_ux_device_stack_transfer_all_request_abort(printer->bulk_out_endpoint, UX_TRANSFER_BUS_RESET);
if (printer->params.instance_deactivate)
{
printer->params.instance_deactivate(printer);
}
}
static VOID _ux_device_class_printer_change(UX_SLAVE_CLASS_COMMAND *command)
{
UX_TEST_ASSERT(0);
}
static UINT _configure_index_value(ULONG index)
{
UCHAR *descriptor;
ULONG configuration_index;
ULONG length;
UCHAR type;
descriptor = _ux_system_slave->ux_system_slave_device_framework;
configuration_index = 0;
while(1)
{
length = descriptor[0];
type = descriptor[1];
if (type == UX_CONFIGURATION_DESCRIPTOR_ITEM)
{
if (configuration_index == index)
return(descriptor[5]);
/* Length is total length. */
length = _ux_utility_short_get(&descriptor[2]);
/* Next configuration. */
configuration_index ++;
}
/* Next descriptor. */
descriptor += length;
}
}
static UINT _ux_device_class_printer_control_request(UX_SLAVE_CLASS_COMMAND *command)
{
UCHAR *setup_data;
UCHAR bmRequestType;
UCHAR bRequest;
USHORT wValue;
USHORT wIndex;
USHORT wLength;
UCHAR port_index;
UCHAR descriptor_type;
UCHAR *data_ptr;
UX_SLAVE_DEVICE *device;
UX_SLAVE_TRANSFER *transfer_request;
UX_SLAVE_CLASS *printer_class = (UX_SLAVE_CLASS *)command->ux_slave_class_command_class_ptr;
UX_DEVICE_CLASS_PRINTER *printer = (UX_DEVICE_CLASS_PRINTER *)printer_class->ux_slave_class_instance;
UX_SLAVE_INTERFACE *interface=printer->interface;
ULONG length;
/* Get the pointer to the device. */
device = &_ux_system_slave->ux_system_slave_device;
/* Get the pointer to the transfer request associated with the control endpoint. */
transfer_request = &device->ux_slave_device_control_endpoint.ux_slave_endpoint_transfer_request;
setup_data = transfer_request->ux_slave_transfer_request_setup;
bmRequestType = setup_data[0];
bRequest = setup_data[1];
wValue = (USHORT)_ux_utility_short_get(&setup_data[2]);
wIndex = (USHORT)_ux_utility_short_get(&setup_data[4]);
wLength = (USHORT)_ux_utility_short_get(&setup_data[6]);
data_ptr = transfer_request->ux_slave_transfer_request_data_pointer;
switch (bRequest)
{
case UX_HOST_CLASS_PRINTER_GET_DEVICE_ID:
/* Check configuration INDEX (wValue), interface (HIGH wIndex), alt (LOW wIndex). */
if (_configure_index_value(wValue) != interface->ux_slave_interface_class->ux_slave_class_configuration_number ||
setup_data[4] != interface->ux_slave_interface_descriptor.bAlternateSetting ||
setup_data[5] != interface->ux_slave_interface_descriptor.bInterfaceNumber)
return(UX_ERROR);
/* Send device ID. */
transfer_request->ux_slave_transfer_request_phase = UX_TRANSFER_PHASE_DATA_OUT;
length = UX_MIN(UX_SLAVE_REQUEST_CONTROL_MAX_LENGTH, wLength);
length = UX_MIN(length, printer->params.device_id_length + 2);
if (length)
{
_ux_utility_short_put_big_endian(data_ptr, length - 2);
_ux_utility_memory_copy(data_ptr + 2, printer->params.device_id, length - 2);
}
return _ux_device_stack_transfer_request(transfer_request, length, wLength);
case UX_HOST_CLASS_PRINTER_GET_STATUS:
/* Check interface number. */
if (wIndex != interface->ux_slave_interface_descriptor.bInterfaceNumber)
return(UX_ERROR);
/* Send status. */
transfer_request->ux_slave_transfer_request_phase = UX_TRANSFER_PHASE_DATA_OUT;
*data_ptr = printer->port_status;
return _ux_device_stack_transfer_request(transfer_request, 1, wLength);
case UX_HOST_CLASS_PRINTER_SOFT_RESET:
/* Check interface number. */
if (wIndex != interface->ux_slave_interface_descriptor.bInterfaceNumber)
return(UX_ERROR);
/* Execute soft reset. */
_ux_device_stack_transfer_all_request_abort(printer->bulk_in_endpoint, UX_TRANSFER_BUS_RESET);
_ux_device_stack_transfer_all_request_abort(printer->bulk_out_endpoint, UX_TRANSFER_BUS_RESET);
printer->soft_reset = UX_TRUE;
return(UX_SUCCESS);
default:
UX_TEST_ASSERT(0);
break;
}
}
UINT _ux_device_class_printer_entry(UX_SLAVE_CLASS_COMMAND *command)
{
switch(command -> ux_slave_class_command_request)
{
case UX_SLAVE_CLASS_COMMAND_INITIALIZE:
_ux_device_class_printer_initialize(command);
break;
case UX_SLAVE_CLASS_COMMAND_UNINITIALIZE:
_ux_device_class_printer_uninitialize(command);
break;
case UX_SLAVE_CLASS_COMMAND_QUERY:
/* For now, always return success. */
break;
case UX_SLAVE_CLASS_COMMAND_ACTIVATE:
return _ux_device_class_printer_activate(command);
case UX_SLAVE_CLASS_COMMAND_CHANGE:
UX_TEST_ASSERT(0);
break;
case UX_SLAVE_CLASS_COMMAND_DEACTIVATE:
_ux_device_class_printer_deactivate(command);
break;
case UX_SLAVE_CLASS_COMMAND_REQUEST:
return _ux_device_class_printer_control_request(command);
default:
UX_TEST_ASSERT(0);
break;
}
return(UX_SUCCESS);
}
|