1 /***************************************************************************
2  * Copyright (c) 2024 Microsoft Corporation
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the MIT License which is available at
6  * https://opensource.org/licenses/MIT.
7  *
8  * SPDX-License-Identifier: MIT
9  **************************************************************************/
10 
11 /**************************************************************************/
12 /**************************************************************************/
13 /**                                                                       */
14 /** USBX Component                                                        */
15 /**                                                                       */
16 /**   Device Video Class                                                  */
17 /**                                                                       */
18 /**************************************************************************/
19 /**************************************************************************/
20 
21 #define UX_SOURCE_CODE
22 
23 
24 /* Include necessary system files.  */
25 
26 #include "ux_api.h"
27 #include "ux_device_class_video.h"
28 #include "ux_device_stack.h"
29 
30 
31 /**************************************************************************/
32 /*                                                                        */
33 /*  FUNCTION                                               RELEASE        */
34 /*                                                                        */
35 /*    _ux_device_class_video_write_payload_commit         PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function set payload buffer valid to send in the Video class.  */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    stream                                Address of video stream       */
48 /*                                            instance                    */
49 /*    length                                Frame length in bytes         */
50 /*                                                                        */
51 /*  OUTPUT                                                                */
52 /*                                                                        */
53 /*    None                                                                */
54 /*                                                                        */
55 /*  CALLS                                                                 */
56 /*                                                                        */
57 /*                                                                        */
58 /*  CALLED BY                                                             */
59 /*                                                                        */
60 /*    Application                                                         */
61 /*                                                                        */
62 /*  RELEASE HISTORY                                                       */
63 /*                                                                        */
64 /*    DATE              NAME                      DESCRIPTION             */
65 /*                                                                        */
66 /*  04-25-2022     Chaoqiong Xiao           Initial Version 6.1.11        */
67 /*  10-31-2023     Yajun Xia                Modified comment(s),          */
68 /*                                            resulting in version 6.3.0  */
69 /*                                                                        */
70 /**************************************************************************/
_ux_device_class_video_write_payload_commit(UX_DEVICE_CLASS_VIDEO_STREAM * stream,ULONG length)71 UINT _ux_device_class_video_write_payload_commit(UX_DEVICE_CLASS_VIDEO_STREAM *stream, ULONG length)
72 {
73 
74 UX_SLAVE_ENDPOINT           *endpoint;
75 UX_SLAVE_DEVICE             *device;
76 UCHAR                       *next_pos;
77 
78 
79     /* Get the pointer to the device.  */
80     device =  &_ux_system_slave -> ux_system_slave_device;
81 
82     /* As long as the device is in the CONFIGURED state.  */
83     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
84     {
85 
86         /* Error trap. */
87         _ux_system_error_handler(UX_SYSTEM_LEVEL_THREAD, UX_SYSTEM_CONTEXT_CLASS, UX_CONFIGURATION_HANDLE_UNKNOWN);
88 
89         /* Cannot proceed with command, the interface is down.  */
90         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
91     }
92 
93     /* Check if endpoint is available.  */
94     endpoint = stream -> ux_device_class_video_stream_endpoint;
95     if (endpoint == UX_NULL)
96         return(UX_ERROR);
97 
98     /* Check if endpoint direction is OK.  */
99     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT)
100         return(UX_ERROR);
101 
102     /* Check overflow!!  */
103     if (stream -> ux_device_class_video_stream_access_pos == stream -> ux_device_class_video_stream_transfer_pos &&
104         stream -> ux_device_class_video_stream_access_pos -> ux_device_class_video_payload_length != 0)
105         return(UX_BUFFER_OVERFLOW);
106 
107     /* Check payload length.  */
108     if ((stream -> ux_device_class_video_stream_payload_buffer_size - 4) < length)
109         return(UX_ERROR);
110 
111     /* Calculate next payload buffer.  */
112     next_pos = (UCHAR *)stream -> ux_device_class_video_stream_access_pos;
113     next_pos += stream -> ux_device_class_video_stream_payload_buffer_size;
114     if (next_pos >= stream -> ux_device_class_video_stream_buffer + stream -> ux_device_class_video_stream_buffer_size)
115         next_pos = stream -> ux_device_class_video_stream_buffer;
116 
117     /* Commit payload length.  */
118     stream -> ux_device_class_video_stream_access_pos -> ux_device_class_video_payload_length = length;
119 
120     /* Move payload position.  */
121     stream -> ux_device_class_video_stream_access_pos = (UX_DEVICE_CLASS_VIDEO_PAYLOAD *)next_pos;
122 
123     return(UX_SUCCESS);
124 }
125 
126 /**************************************************************************/
127 /*                                                                        */
128 /*  FUNCTION                                               RELEASE        */
129 /*                                                                        */
130 /*    _uxe_device_class_video_write_payload_commit        PORTABLE C      */
131 /*                                                           6.3.0        */
132 /*  AUTHOR                                                                */
133 /*                                                                        */
134 /*    Yajun Xia, Microsoft Corporation                                    */
135 /*                                                                        */
136 /*  DESCRIPTION                                                           */
137 /*                                                                        */
138 /*    This function checks errors in video write payload commit function  */
139 /*    call.                                                               */
140 /*                                                                        */
141 /*  INPUT                                                                 */
142 /*                                                                        */
143 /*    stream                                Address of video stream       */
144 /*                                            instance                    */
145 /*    length                                Frame length in bytes         */
146 /*                                                                        */
147 /*  OUTPUT                                                                */
148 /*                                                                        */
149 /*    None                                                                */
150 /*                                                                        */
151 /*  CALLS                                                                 */
152 /*                                                                        */
153 /*    _ux_device_class_video_write_payload_commit                         */
154 /*                                          Video write payload commit    */
155 /*                                                                        */
156 /*  CALLED BY                                                             */
157 /*                                                                        */
158 /*    Application                                                         */
159 /*                                                                        */
160 /*  RELEASE HISTORY                                                       */
161 /*                                                                        */
162 /*    DATE              NAME                      DESCRIPTION             */
163 /*                                                                        */
164 /*  10-31-2023     Yajun Xia                Initial Version 6.3.0         */
165 /*                                                                        */
166 /**************************************************************************/
_uxe_device_class_video_write_payload_commit(UX_DEVICE_CLASS_VIDEO_STREAM * stream,ULONG length)167 UINT _uxe_device_class_video_write_payload_commit(UX_DEVICE_CLASS_VIDEO_STREAM *stream, ULONG length)
168 {
169 
170     /* Sanity check. */
171     if ((stream == UX_NULL) || (length == 0))
172         return(UX_INVALID_PARAMETER);
173 
174     /* Call the actual video write payload commit function.  */
175     return (_ux_device_class_video_write_payload_commit(stream, length));
176 }