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_get            PORTABLE C      */
36 /*                                                           6.3.0        */
37 /*  AUTHOR                                                                */
38 /*                                                                        */
39 /*    Chaoqiong Xiao, Microsoft Corporation                               */
40 /*                                                                        */
41 /*  DESCRIPTION                                                           */
42 /*                                                                        */
43 /*    This function get a writable payload buffer in the Video class.     */
44 /*                                                                        */
45 /*  INPUT                                                                 */
46 /*                                                                        */
47 /*    stream                                Address of video stream       */
48 /*                                            instance                    */
49 /*    payload                               Pointer to buffer to save     */
50 /*                                            payload buffer pointer      */
51 /*    length                                Pointer to save payload       */
52 /*                                            buffer length in bytes      */
53 /*                                                                        */
54 /*  OUTPUT                                                                */
55 /*                                                                        */
56 /*    None                                                                */
57 /*                                                                        */
58 /*  CALLS                                                                 */
59 /*                                                                        */
60 /*                                                                        */
61 /*  CALLED BY                                                             */
62 /*                                                                        */
63 /*    Application                                                         */
64 /*                                                                        */
65 /*  RELEASE HISTORY                                                       */
66 /*                                                                        */
67 /*    DATE              NAME                      DESCRIPTION             */
68 /*                                                                        */
69 /*  04-25-2022     Chaoqiong Xiao           Initial Version 6.1.11        */
70 /*  10-31-2023     Yajun Xia                Modified comment(s),          */
71 /*                                            resulting in version 6.3.0  */
72 /*                                                                        */
73 /**************************************************************************/
_ux_device_class_video_write_payload_get(UX_DEVICE_CLASS_VIDEO_STREAM * stream,UCHAR ** payload,ULONG * length)74 UINT _ux_device_class_video_write_payload_get(UX_DEVICE_CLASS_VIDEO_STREAM *stream, UCHAR **payload, ULONG *length)
75 {
76 
77 UX_SLAVE_ENDPOINT           *endpoint;
78 UX_SLAVE_DEVICE             *device;
79 
80 
81     /* Get the pointer to the device.  */
82     device =  &_ux_system_slave -> ux_system_slave_device;
83 
84     /* As long as the device is in the CONFIGURED state.  */
85     if (device -> ux_slave_device_state != UX_DEVICE_CONFIGURED)
86     {
87 
88         /* Cannot proceed with command, the interface is down.  */
89         return(UX_CONFIGURATION_HANDLE_UNKNOWN);
90     }
91 
92     /* Check if endpoint is available.  */
93     endpoint = stream -> ux_device_class_video_stream_endpoint;
94     if (endpoint == UX_NULL)
95         return(UX_ERROR);
96 
97     /* Check if endpoint direction is OK.  */
98     if ((endpoint -> ux_slave_endpoint_descriptor.bEndpointAddress & UX_ENDPOINT_DIRECTION) == UX_ENDPOINT_OUT)
99         return(UX_ERROR);
100 
101     /* Check overflow!!  */
102     if (stream -> ux_device_class_video_stream_access_pos == stream -> ux_device_class_video_stream_transfer_pos &&
103         stream -> ux_device_class_video_stream_access_pos -> ux_device_class_video_payload_length != 0)
104         return(UX_BUFFER_OVERFLOW);
105 
106     /* Store payload buffer pointer.  */
107     *payload = stream -> ux_device_class_video_stream_access_pos -> ux_device_class_video_payload_data;
108 
109     /* Exclude header size in payload buffer size.  */
110     *length = (stream -> ux_device_class_video_stream_payload_buffer_size - 4);
111 
112     return(UX_SUCCESS);
113 }
114 
115 /**************************************************************************/
116 /*                                                                        */
117 /*  FUNCTION                                               RELEASE        */
118 /*                                                                        */
119 /*    _uxe_device_class_video_write_payload_get           PORTABLE C      */
120 /*                                                           6.3.0        */
121 /*  AUTHOR                                                                */
122 /*                                                                        */
123 /*    Yajun Xia, Microsoft Corporation                                    */
124 /*                                                                        */
125 /*  DESCRIPTION                                                           */
126 /*                                                                        */
127 /*    This function checks errors in video write payload get function     */
128 /*    call.                                                               */
129 /*                                                                        */
130 /*  INPUT                                                                 */
131 /*                                                                        */
132 /*    stream                                Address of video stream       */
133 /*                                            instance                    */
134 /*    payload                               Pointer to buffer to save     */
135 /*                                            payload buffer pointer      */
136 /*    length                                Pointer to save payload       */
137 /*                                            buffer length in bytes      */
138 /*                                                                        */
139 /*  OUTPUT                                                                */
140 /*                                                                        */
141 /*    None                                                                */
142 /*                                                                        */
143 /*  CALLS                                                                 */
144 /*                                                                        */
145 /*    _ux_device_class_video_write_payload_get                            */
146 /*                                          Video write payload get       */
147 /*                                                                        */
148 /*  CALLED BY                                                             */
149 /*                                                                        */
150 /*    Application                                                         */
151 /*                                                                        */
152 /*  RELEASE HISTORY                                                       */
153 /*                                                                        */
154 /*    DATE              NAME                      DESCRIPTION             */
155 /*                                                                        */
156 /*  10-31-2023     Yajun Xia                Initial Version 6.3.0         */
157 /*                                                                        */
158 /**************************************************************************/
_uxe_device_class_video_write_payload_get(UX_DEVICE_CLASS_VIDEO_STREAM * stream,UCHAR ** payload,ULONG * length)159 UINT _uxe_device_class_video_write_payload_get(UX_DEVICE_CLASS_VIDEO_STREAM *stream, UCHAR **payload, ULONG *length)
160 {
161 
162     /* Sanity check. */
163     if ((stream == UX_NULL) || (payload == UX_NULL) || (length == UX_NULL))
164         return(UX_INVALID_PARAMETER);
165 
166     /* Call the actual video write payload get function.  */
167     return(_ux_device_class_video_write_payload_get(stream, payload, length));
168 }