summaryrefslogtreecommitdiff
path: root/common/usbx_device_classes/src/ux_device_class_video_initialize.c
blob: 0f69535220538ce6eab5ceea68484a724caf5be7 (plain)
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
/***************************************************************************
 * 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 Video Class                                                  */
/**                                                                       */
/**************************************************************************/
/**************************************************************************/

#define UX_SOURCE_CODE


/* Include necessary system files.  */

#include "ux_api.h"
#include "ux_device_class_video.h"
#include "ux_device_stack.h"


/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _ux_device_class_video_initialize                   PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Chaoqiong Xiao, Microsoft Corporation                               */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function initializes the USB Video device.                     */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    command                               Pointer to video command      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_utility_memory_allocate           Allocate memory               */
/*    _ux_utility_memory_copy               Copy memory                   */
/*    _ux_utility_memory_free               Free memory                   */
/*    _ux_utility_thread_create             Create thread to use          */
/*    _ux_utility_thread_delete             Delete thread                 */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Device Video Class                                                  */
/*                                                                        */
/**************************************************************************/
UINT  _ux_device_class_video_initialize(UX_SLAVE_CLASS_COMMAND *command)
{

UINT                                    status = UX_SUCCESS;
UX_DEVICE_CLASS_VIDEO                   *video;
UX_DEVICE_CLASS_VIDEO_PARAMETER         *video_parameter;
UX_DEVICE_CLASS_VIDEO_STREAM            *stream;
UX_DEVICE_CLASS_VIDEO_STREAM_PARAMETER  *stream_parameter;
UX_SLAVE_CLASS                          *class_inst;
ULONG                                   memory_size;
ULONG                                   streams_size;
ULONG                                   i;


    /* Get the class container.  */
    class_inst =  command -> ux_slave_class_command_class_ptr;

    /* Get the pointer to the application parameters for the video class.  */
    video_parameter = (UX_DEVICE_CLASS_VIDEO_PARAMETER *)command -> ux_slave_class_command_parameter;

    /* Create an instance of the device video class, with additional streams and controls instances.  */
    memory_size  = sizeof(UX_DEVICE_CLASS_VIDEO);

    /* Put 0 to default result.  */
    streams_size = 0;

    /* Confirm there is no overflow on multiply.  */
    UX_UTILITY_MULC_SAFE(video_parameter -> ux_device_class_video_parameter_streams_nb, (ULONG)sizeof(UX_DEVICE_CLASS_VIDEO_STREAM), streams_size, status);
    if (status != UX_SUCCESS)
        return(status);

    /* Confirm there is no overflow on add.  */
    UX_UTILITY_ADD_SAFE(memory_size, streams_size, memory_size, status);
    if (status != UX_SUCCESS)
        return(status);

    /* Create buffer for video and controls.  */
    video = (UX_DEVICE_CLASS_VIDEO *)_ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);

    /* Check for successful allocation.  */
    if (video == UX_NULL)
        return(UX_MEMORY_INSUFFICIENT);

    /* Save streams.  */
    if (streams_size)
    {
        video -> ux_device_class_video_streams = (UX_DEVICE_CLASS_VIDEO_STREAM *)((UCHAR *)video + sizeof(UX_DEVICE_CLASS_VIDEO));
        video -> ux_device_class_video_streams_nb = video_parameter -> ux_device_class_video_parameter_streams_nb;
    }

    /* Allocate resources for streams.  */
    stream = video -> ux_device_class_video_streams;
    stream_parameter = video_parameter -> ux_device_class_video_parameter_streams;
    for (i = 0; i < video -> ux_device_class_video_streams_nb; i ++)
    {

        /* Create memory block based on max payload buffer size and max number of payloads buffered.
           Each payload require some additional header memory (8 bytes).  */
        stream -> ux_device_class_video_stream_payload_buffer_size = stream_parameter -> ux_device_class_video_stream_parameter_max_payload_buffer_size;

        if (UX_OVERFLOW_CHECK_ADD_USHORT(stream -> ux_device_class_video_stream_payload_buffer_size, 4))
        {
            status = UX_ERROR;
            break;
        }
        stream -> ux_device_class_video_stream_payload_buffer_size += 4;

        if (UX_OVERFLOW_CHECK_MULV_ULONG(stream -> ux_device_class_video_stream_payload_buffer_size,
            stream_parameter -> ux_device_class_video_stream_parameter_max_payload_buffer_nb))
        {
            status = UX_ERROR;
            break;
        }
        memory_size = stream -> ux_device_class_video_stream_payload_buffer_size *
                            stream_parameter -> ux_device_class_video_stream_parameter_max_payload_buffer_nb;

        /* Create block of buffer buffer is cache safe for USB transfer.  */
#if UX_DEVICE_ENDPOINT_BUFFER_OWNER == 1
        stream -> ux_device_class_video_stream_buffer = (UCHAR *)_ux_utility_memory_allocate(UX_NO_ALIGN, UX_CACHE_SAFE_MEMORY, memory_size);
#else
        stream -> ux_device_class_video_stream_buffer = (UCHAR *)_ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);
#endif

        /* Check for successful allocation.  */
        if (stream -> ux_device_class_video_stream_buffer == UX_NULL)
        {
            status = UX_MEMORY_INSUFFICIENT;
            break;
        }

        stream -> ux_device_class_video_stream_buffer_size = memory_size;
        stream -> ux_device_class_video_stream_transfer_pos = (UX_DEVICE_CLASS_VIDEO_PAYLOAD *)stream -> ux_device_class_video_stream_buffer;
        stream -> ux_device_class_video_stream_access_pos = stream -> ux_device_class_video_stream_transfer_pos;

#if !defined(UX_DEVICE_STANDALONE)

        /* Create memory block for streaming thread stack in addition.  */
        if (stream_parameter -> ux_device_class_video_stream_parameter_thread_stack_size == 0)
            memory_size = UX_DEVICE_CLASS_VIDEO_THREAD_STACK_SIZE;
        else
            memory_size = stream_parameter -> ux_device_class_video_stream_parameter_thread_stack_size;
        stream -> ux_device_class_video_stream_thread_stack = _ux_utility_memory_allocate(UX_NO_ALIGN, UX_REGULAR_MEMORY, memory_size);

        /* Check for successful allocation.  */
        if (stream -> ux_device_class_video_stream_thread_stack == UX_NULL)
        {
            status = UX_MEMORY_INSUFFICIENT;
            break;
        }

        /* Create streaming thread.  */
        status =  _ux_utility_thread_create(&stream -> ux_device_class_video_stream_thread , "ux_device_class_video_stream_thread",
                    stream_parameter -> ux_device_class_video_stream_parameter_thread_entry,
                    (ULONG)(ALIGN_TYPE)stream, (VOID *) stream -> ux_device_class_video_stream_thread_stack,
                    memory_size, UX_THREAD_PRIORITY_CLASS,
                    UX_THREAD_PRIORITY_CLASS, UX_NO_TIME_SLICE, UX_DONT_START);

        /* Check for successful allocation.  */
        if (status != UX_SUCCESS)
            break;

        UX_THREAD_EXTENSION_PTR_SET(&(stream -> ux_device_class_video_stream_thread), stream)
#else

        /* Save task function for streaming.  */
        stream -> ux_device_class_video_stream_task_function = stream_parameter -> ux_device_class_video_stream_parameter_task_function;
#endif

        /* Save callbacks.  */
        _ux_utility_memory_copy(&stream -> ux_device_class_video_stream_callbacks,
                                &stream_parameter -> ux_device_class_video_stream_parameter_callbacks,
                                sizeof(UX_DEVICE_CLASS_VIDEO_STREAM_CALLBACKS)); /* Use case of memcpy is verified. */

        /* Save video instance.  */
        stream -> ux_device_class_video_stream_video = video;

        stream ++;
        stream_parameter ++;
    }

    /* Check for successful creation.  */
    if (status == UX_SUCCESS)
    {

        /* Save the address of the Video instance inside the Video container.  */
        class_inst -> ux_slave_class_instance = (VOID *) video;

        /* Link to class instance.  */
        video -> ux_device_class_video_class = class_inst;

        /* Save callbacks.  */
        _ux_utility_memory_copy(&video -> ux_device_class_video_callbacks,
            &video_parameter -> ux_device_class_video_parameter_callbacks,
            sizeof(UX_DEVICE_CLASS_VIDEO_CALLBACKS)); /* Use case of memcpy is verified. */

#if defined(UX_DEVICE_STANDALONE)

        /* Link task function.  */
        class_inst -> ux_slave_class_task_function = _ux_device_class_video_tasks_run;
#endif

        /* Return completion status.  */
        return(UX_SUCCESS);
    }

    /* Error trap!  */
    _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, status);

    /* Free allocated resources.  */
    stream = video -> ux_device_class_video_streams;
    for (i = 0; i < video -> ux_device_class_video_streams_nb; i ++)
    {

#if !defined(UX_DEVICE_STANDALONE)
        if (_ux_device_thread_created(&stream -> ux_device_class_video_stream_thread))
            _ux_utility_thread_delete(&stream -> ux_device_class_video_stream_thread);
        if (stream -> ux_device_class_video_stream_thread_stack)
            _ux_utility_memory_free(stream -> ux_device_class_video_stream_thread_stack);
#endif
        if (stream -> ux_device_class_video_stream_buffer)
            _ux_utility_memory_free(stream -> ux_device_class_video_stream_buffer);
        stream ++;
    }
    _ux_utility_memory_free(video);

    return(status);
}

/**************************************************************************/
/*                                                                        */
/*  FUNCTION                                               RELEASE        */
/*                                                                        */
/*    _uxe_device_class_video_initialize                  PORTABLE C      */
/*                                                           6.3.0        */
/*  AUTHOR                                                                */
/*                                                                        */
/*    Yajun Xia, Microsoft Corporation                                    */
/*                                                                        */
/*  DESCRIPTION                                                           */
/*                                                                        */
/*    This function checks errors in video initialization function call.  */
/*                                                                        */
/*  INPUT                                                                 */
/*                                                                        */
/*    command                               Pointer to video command      */
/*                                                                        */
/*  OUTPUT                                                                */
/*                                                                        */
/*    Completion Status                                                   */
/*                                                                        */
/*  CALLS                                                                 */
/*                                                                        */
/*    _ux_device_class_video_initialize     Initialize video instance     */
/*                                                                        */
/*  CALLED BY                                                             */
/*                                                                        */
/*    Device Video Class                                                  */
/*                                                                        */
/**************************************************************************/
UINT  _uxe_device_class_video_initialize(UX_SLAVE_CLASS_COMMAND *command)
{
UX_DEVICE_CLASS_VIDEO_PARAMETER         *video_parameter;
ULONG i;

    /* Get the pointer to the application parameters for the video class.  */
    video_parameter = (UX_DEVICE_CLASS_VIDEO_PARAMETER *)command -> ux_slave_class_command_parameter;

    /* Sanity checks.  */
    if (video_parameter == UX_NULL)
        return(UX_INVALID_PARAMETER);

    /* There must be at least one stream.  */
    if ((video_parameter -> ux_device_class_video_parameter_streams_nb == 0) ||
        (video_parameter -> ux_device_class_video_parameter_streams == UX_NULL))
        return(UX_INVALID_PARAMETER);

    for (i = 0; i < video_parameter -> ux_device_class_video_parameter_streams_nb; i ++)
    {
        if ((video_parameter -> ux_device_class_video_parameter_streams[i].ux_device_class_video_stream_parameter_max_payload_buffer_size == 0) ||
            (video_parameter -> ux_device_class_video_parameter_streams[i].ux_device_class_video_stream_parameter_max_payload_buffer_nb == 0))
            return(UX_INVALID_PARAMETER);
    }

    /* Do initialize.  */
    return(_ux_device_class_video_initialize(command));
}