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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
|
/***************************************************************************
* 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 */
/** */
/** Video Class */
/** */
/**************************************************************************/
/**************************************************************************/
/* Include necessary system files. */
#define UX_SOURCE_CODE
#include "ux_api.h"
#include "ux_host_class_video.h"
#include "ux_host_stack.h"
UCHAR _ux_system_class_video_interface_descriptor_structure[] = {1,1,1,1,1,1,1,1};
UCHAR _ux_system_class_video_input_terminal_descriptor_structure[] = {1,1,1,1,2,1,1};
UCHAR _ux_system_class_video_input_header_descriptor_structure[] = {1,1,1,1,2,1,1,1,1,1,1,1};
UCHAR _ux_system_class_video_processing_unit_descriptor_structure[] = {1,1,1,1,1,2,1,1};
UCHAR _ux_system_class_video_streaming_interface_descriptor_structure[] = {1,1,1,1,1,1};
UCHAR _ux_system_class_video_streaming_endpoint_descriptor_structure[] = {1,1,1,1,1,1};
UCHAR _ux_system_class_video_frame_descriptor_structure[] = {1,1,1,1,1,2,2,4,4,4,4,1};
UCHAR _ux_system_host_class_video_name[] = "ux_host_class_video";
typedef struct UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_STRUCT
{
UX_HOST_CLASS_VIDEO *video;
ULONG parsed_flags;
} UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER;
#define UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VC_HEADER 1
#define UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VC_IT 2
#define UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VC_PU 4
#define UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VS_HEADER 8
#define UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_DONE (1|2|4|8)
static UINT _ux_host_class_video_descriptors_parser(VOID *arg,
UCHAR *packed_interface_descriptor,
UCHAR *packed_entity_descriptor)
{
UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER *parser;
UX_HOST_CLASS_VIDEO *video;
UX_INTERFACE *streaming_interface;
UCHAR bInCollection;
UCHAR *baInterfaceNr;
/* Get parse data. */
parser = (UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER *)arg;
/* Get video instance. */
video = parser -> video;
if (packed_interface_descriptor[6] == UX_HOST_CLASS_VIDEO_SUBCLASS_CONTROL)
{
/* Parse VC descriptors. */
/* Locate control interface. */
if (video -> ux_host_class_video_control_interface_number == 0xFF)
{
/* Parse header. */
if (packed_entity_descriptor[2] == UX_HOST_CLASS_VIDEO_VC_HEADER)
{
/* Get streaming interface. */
streaming_interface = video -> ux_host_class_video_streaming_interface;
/* Check if this the VC interface is expected. */
bInCollection = packed_entity_descriptor[11];
baInterfaceNr = packed_entity_descriptor + 12;
/* Validation:
* baInterfaceNr not exceeding current descriptor.
*/
if (packed_entity_descriptor[0] + packed_entity_descriptor < baInterfaceNr + bInCollection)
return(1);
while(bInCollection)
{
/* Streaming interface belongs to this control interface. */
if (*baInterfaceNr == streaming_interface -> ux_interface_descriptor.bInterfaceNumber)
{
video -> ux_host_class_video_control_interface_number = packed_interface_descriptor[2];
parser -> parsed_flags |= UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VC_HEADER;
return(0);
}
/* Next interface number in descriptor. */
baInterfaceNr ++;
bInCollection --;
}
}
/* Not expected interface, just try next descriptor. */
return(0);
}
else if (packed_interface_descriptor[2] == video -> ux_host_class_video_control_interface_number)
{
/* It's the expected VC interface. */
/* Parse UX_HOST_CLASS_VIDEO_VC_PROCESSING_UNIT as feature_unit_id. */
if (packed_entity_descriptor[2] == UX_HOST_CLASS_VIDEO_VC_PROCESSING_UNIT &&
video -> ux_host_class_video_feature_unit_id == 0)
{
parser -> parsed_flags |= UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VC_PU;
/* Save as feature unit ID. */
video -> ux_host_class_video_feature_unit_id = packed_entity_descriptor[3];
}
/* Parse UX_HOST_CLASS_VIDEO_VC_INPUT_TERMINAL and type (first only). */
if (packed_entity_descriptor[2] == UX_HOST_CLASS_VIDEO_VC_INPUT_TERMINAL &&
video -> ux_host_class_video_terminal_id == 0)
{
parser -> parsed_flags |= UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VC_IT;
/* Save the video terminal ID. */
video -> ux_host_class_video_terminal_id = packed_entity_descriptor[3];
/* Save the video terminal type. */
video -> ux_host_class_video_terminal_type = _ux_utility_short_get(packed_entity_descriptor + 4);
}
}
}
else
{
/* Parse VS descriptors. */
if (packed_entity_descriptor[2] == UX_HOST_CLASS_VIDEO_VS_INPUT_HEADER)
{
parser -> parsed_flags |= UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_VS_HEADER;
/* Get the number of formats. */
video -> ux_host_class_video_number_formats = packed_entity_descriptor[3];
/* Get the length of formats. */
video -> ux_host_class_video_length_formats = _ux_utility_short_get(packed_entity_descriptor + 4);
/* Save the descriptor where the formats reside. */
video -> ux_host_class_video_format_address = packed_entity_descriptor;
}
}
return(0);
}
/**************************************************************************/
/* */
/* FUNCTION RELEASE */
/* */
/* _ux_host_class_video_activate PORTABLE C */
/* 6.2.0 */
/* AUTHOR */
/* */
/* Chaoqiong Xiao, Microsoft Corporation */
/* */
/* DESCRIPTION */
/* */
/* This function activates the video class. It may be called twice by */
/* the same device if there is a video control interface to this */
/* device. */
/* */
/* INPUT */
/* */
/* command Pointer to command */
/* */
/* OUTPUT */
/* */
/* Completion Status */
/* */
/* CALLS */
/* */
/* _ux_host_class_video_configure Configure the video class */
/* _ux_host_class_video_descriptor_get Get video descriptor */
/* _ux_host_class_video_input_terminal_get */
/* Get input terminal */
/* _ux_host_class_video_input_format_get Get input format */
/* _ux_host_class_video_control_list_get Get controls */
/* _ux_host_stack_class_instance_create Create class instance */
/* _ux_host_stack_class_instance_destroy Destroy class instance */
/* _ux_utility_memory_allocate Allocate a memory block */
/* _ux_utility_memory_free Free a memory block */
/* _ux_host_semaphore_create Create protection semaphore */
/* */
/* CALLED BY */
/* */
/* Video Class */
/* */
/**************************************************************************/
UINT _ux_host_class_video_activate(UX_HOST_CLASS_COMMAND *command)
{
UX_INTERFACE *interface_ptr;
UX_HOST_CLASS_VIDEO *video;
UINT status;
UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER parser;
/* The video is always activated by the interface descriptor and not the
device descriptor. */
interface_ptr = (UX_INTERFACE *) command -> ux_host_class_command_container;
/* Check the subclass of the new device. If it is a Video Control Interface,
we don't need to create an instance of this function. When we get the streaming interface,
we will search the video control interface for the device. */
if (interface_ptr -> ux_interface_descriptor.bInterfaceSubClass == UX_HOST_CLASS_VIDEO_SUBCLASS_CONTROL)
return(UX_SUCCESS);
/* Obtain memory for this class instance. */
video = (UX_HOST_CLASS_VIDEO *) _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, sizeof(UX_HOST_CLASS_VIDEO));
if (video == UX_NULL)
return(UX_MEMORY_INSUFFICIENT);
/* Store the class container into this instance. */
video -> ux_host_class_video_class = command -> ux_host_class_command_class_ptr;
/* Store the interface container into the video class instance. */
video -> ux_host_class_video_streaming_interface = interface_ptr;
/* Store the device container into the video class instance. */
video -> ux_host_class_video_device = interface_ptr -> ux_interface_configuration -> ux_configuration_device;
/* This instance of the device must also be stored in the interface container. */
interface_ptr -> ux_interface_class_instance = (VOID *) video;
/* Create this class instance. */
_ux_host_stack_class_instance_create(video -> ux_host_class_video_class, (VOID *) video);
/* Configure the video. */
status = _ux_host_class_video_configure(video);
/* Get the video descriptor (all the class specific stuff) and memorize them
as we will need these descriptors to change settings. */
if (status == UX_SUCCESS)
status = _ux_host_class_video_descriptor_get(video);
/* Use parser to locate streaming terminal, formats and video controls. */
if (status == UX_SUCCESS)
{
parser.video = video;
parser.parsed_flags = 0;
video -> ux_host_class_video_control_interface_number = 0xFF;
status = _ux_host_class_video_entities_parse(video,
_ux_host_class_video_descriptors_parser, (VOID *)&parser);
if (parser.parsed_flags != UX_HOST_CLASS_VIDEO_DESCRIPTORS_PARSER_DONE)
/* Some of expected descriptors not found. */
status = UX_HOST_CLASS_VIDEO_WRONG_TYPE;
}
/* Create the semaphore to protect multiple threads from accessing the same
video instance. */
if (status == UX_SUCCESS)
{
status = _ux_host_semaphore_create(&video -> ux_host_class_video_semaphore, "ux_video_semaphore", 1);
if (status != UX_SUCCESS)
status = UX_SEMAPHORE_ERROR;
}
/* Create the semaphore to protect multiple threads from issuing the control
request at the same time. */
if (status == UX_SUCCESS)
{
status = _ux_host_semaphore_create(&video -> ux_host_class_video_semaphore_control_request, "ux_video_semaphore_control", 1);
if (status != UX_SUCCESS)
status = UX_SEMAPHORE_ERROR;
}
if (status == UX_SUCCESS)
{
/* Mark the video as live now. */
video -> ux_host_class_video_state = UX_HOST_CLASS_INSTANCE_LIVE;
/* If all is fine and the device is mounted, we may need to inform the application
if a function has been programmed in the system structure. */
if (_ux_system_host -> ux_system_host_change_function != UX_NULL)
{
/* Call system change function. */
_ux_system_host -> ux_system_host_change_function(UX_DEVICE_INSERTION, video -> ux_host_class_video_class, (VOID *) video);
}
/* If trace is enabled, insert this event into the trace buffer. */
//UX_TRACE_IN_LINE_INSERT(UX_TRACE_HOST_CLASS_VIDEO_ACTIVATE, video, 0, 0, 0, UX_TRACE_HOST_CLASS_EVENTS, 0, 0)
/* If trace is enabled, register this object. */
UX_TRACE_OBJECT_REGISTER(UX_TRACE_HOST_OBJECT_TYPE_INTERFACE, video, 0, 0, 0)
/* Return success. */
return(UX_SUCCESS);
}
/* There was error, free resources. */
/* The last resource, video -> ux_host_class_video_semaphore_control_request is not created or created error,
no need to free. */
/* Destroy the semaphore. */
if (_ux_host_semaphore_created(&video -> ux_host_class_video_semaphore))
_ux_host_semaphore_delete(&video -> ux_host_class_video_semaphore);
/* Destroy the class instance. */
_ux_host_stack_class_instance_destroy(video -> ux_host_class_video_class, (VOID *) video);
/* This instance of the device must also be cleared in the interface container. */
interface_ptr -> ux_interface_class_instance = UX_NULL;
/* Free instance memory. */
_ux_utility_memory_free(video);
/* Return completion status. */
return(status);
}
|