hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
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
/*
 * Copyright (C) 2019 Rockchip Electronics Co., Ltd.
 *
 * This software is available to you under a choice of one of two
 * licenses.  You may choose to be licensed under the terms of the GNU
 * General Public License (GPL), available from the file
 * COPYING in the main directory of this source tree, or the
 * OpenIB.org BSD license below:
 *
 *     Redistribution and use in source and binary forms, with or
 *     without modification, are permitted provided that the following
 *     conditions are met:
 *
 *      - Redistributions of source code must retain the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer.
 *
 *      - Redistributions in binary form must reproduce the above
 *        copyright notice, this list of conditions and the following
 *        disclaimer in the documentation and/or other materials
 *        provided with the distribution.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */
 
#ifndef _UVC_GADGET_H_
#define _UVC_GADGET_H_
 
#include <linux/ioctl.h>
#include <linux/types.h>
#include <linux/usb/ch9.h>
 
#define UVC_EVENT_FIRST         (V4L2_EVENT_PRIVATE_START + 0)
#define UVC_EVENT_CONNECT       (V4L2_EVENT_PRIVATE_START + 0)
#define UVC_EVENT_DISCONNECT        (V4L2_EVENT_PRIVATE_START + 1)
#define UVC_EVENT_STREAMON      (V4L2_EVENT_PRIVATE_START + 2)
#define UVC_EVENT_STREAMOFF     (V4L2_EVENT_PRIVATE_START + 3)
#define UVC_EVENT_SETUP         (V4L2_EVENT_PRIVATE_START + 4)
#define UVC_EVENT_DATA          (V4L2_EVENT_PRIVATE_START + 5)
#define UVC_EVENT_SUSPEND       (V4L2_EVENT_PRIVATE_START + 6)
#define UVC_EVENT_RESUME        (V4L2_EVENT_PRIVATE_START + 7)
#define UVC_EVENT_LAST          (V4L2_EVENT_PRIVATE_START + 8)
 
#define MAX_UVC_REQUEST_DATA_LENGTH 60
 
#define UVC_CTRL_INTERFACE_ID        0x00
#define UVC_CTRL_CAMERA_TERMINAL_ID  0x01
#define UVC_CTRL_PROCESSING_UNIT_ID  0x02
#define UVC_CTRL_EXTENSION_UNIT_ID   0x03
#define UVC_CTRL_OUTPUT_TERMINAL_ID  0x04
#define UVC_CTRL_XU_UNIT_ID          0x06
 
struct uvc_request_data
{
    __s32 length;
    __u8 data[60];
};
 
struct uvc_event
{
    union
    {
        enum usb_device_speed speed;
        struct usb_ctrlrequest req;
        struct uvc_request_data data;
    };
};
 
#define UVCIOC_SEND_RESPONSE        _IOW('U', 1, struct uvc_request_data)
 
#define UVC_INTF_CONTROL        0
#define UVC_INTF_STREAMING      1
 
#ifdef __cplusplus
extern "C" {
#endif
 
#include "uvc_video.h"
#include "uvc_control.h"
#include "mpi_enc.h"
 
#include <stddef.h>
#include <stdbool.h>
#include <stdint.h>
 
#include <linux/usb/ch9.h>
#include <linux/usb/video.h>
#include <linux/videodev2.h>
#include "uvc_configfs.h"
 
#define V4L2_PIX_FMT_H265     v4l2_fourcc('H', '2', '6', '5') /* H265 with start codes */
#define UVC_PU_FPS_CONTROL 0xff
#define UVC_PU_ROLL_CONTROL 0xfd
#define UVC_PU_EXPOSURE_TIME_CONTROL 0xfc
#define UVC_PU_AE_MODE_CONTROL 0xfb
 
/* ---------------------------------------------------------------------------
 * Generic stuff
 */
 
enum XuCmd
{
    CMD_TOOLS_CTRL_1 = 0x01,
    CMD_GET_CAMERA_VERSION,
    CMD_SET_CAMERA_IP,
//    CMD_START_CAMERA,
    CMD_SHUTDOWN_CAMERA,
    CMD_RESET_CAMERA,
    CMD_SET_MOTOR_RATE = 0x06,
    CMD_SET_MOTOR_BY_STEPS = 0x07,
    CMD_SET_MOTOR_BY_USER = 0x08,
    CMD_STOP_MOTOR_BY_USER = 0x09,
    CMD_SET_EPTZ = 0x0a,
    CMD_SET_H265 = 0x0b,
    CMD_MAX_NUM = CMD_SET_H265,
};
 
/* IO methods supported */
enum io_method
{
    IO_METHOD_MMAP,
    IO_METHOD_USERPTR,
    IO_METHOD_DMA_BUFF,
};
 
#define UVC_IO_METHOD_MMAP 0
#define UVC_IO_METHOD_USERPTR 1
#define UVC_IO_METHOD_DMA_BUFF 2
 
#ifdef RK_MPP_USE_UVC_VIDEO_BUFFER
#define UVC_IO_METHOD   UVC_IO_METHOD_DMA_BUFF // do not modify, modify the RK_MPP_USE_UVC_VIDEO_BUFFER to choice
#else
#define UVC_IO_METHOD   UVC_IO_METHOD_MMAP //UVC_IO_METHOD_DMA_BUFF // here can set 0 or 2 //  1 still not support
#endif
 
/* Buffer representing one video frame */
struct buffer
{
    struct v4l2_buffer buf;
    void *start;
    size_t length;
};
 
struct v4l2_buffer_info
{
    struct uvc_buffer *uvc_buf;
    int fd;
    int index;
};
 
enum USB_STATE
{
    USB_STATE_FIRST_GET_READY,
    USB_STATE_FIRST_GET_OK,
    USB_STATE_FIRST_SEND_OK,
    USB_STATE_NORMAL_RUN
};
 
/* Represents a UVC based video output device */
struct uvc_device
{
    int video_id;
    /* uvc device specific */
    int uvc_fd;
    int is_streaming;
    int run_standalone;
    char *uvc_devname;
    struct uvc_function_config *fc;
    int suspend;
    int need_bypass;
    /* uvc control request specific */
    struct uvc_streaming_control probe;
    struct uvc_streaming_control commit;
    int control;
    struct uvc_request_data request_error_code;
    unsigned int brightness_val;
    unsigned short contrast_val;
    int hue_val;
    unsigned int saturation_val;
    unsigned int sharpness_val;
    unsigned int gamma_val;
    unsigned int white_balance_temperature_val;
    unsigned int white_balance_temperature_auto_val;
    unsigned int gain_val;
    unsigned int hue_auto_val;
    unsigned int zoom_val;
    int pan_val;
    int tilt_val;
    short roll_val;
    short iris_val;
    int exposure_time_val;
    unsigned char ae_mode_val;
    unsigned char power_line_frequency_val;
    unsigned char ex_tool_ctrl1[4];
    unsigned char ex_sn_data[MAX_UVC_REQUEST_DATA_LENGTH];
    unsigned char ex_ip_data[MAX_UVC_REQUEST_DATA_LENGTH];//
    unsigned char ex_date_data[MAX_UVC_REQUEST_DATA_LENGTH];
    unsigned int eptz_flag;
    unsigned int xu_h265;
 
    /* uvc buffer specific */
    enum io_method io;
    struct buffer *mem;
    struct buffer *dummy_buf;
    unsigned int nbufs;
    unsigned int fcc;
    unsigned int width;
    unsigned int height;
    unsigned int fps;
 
    unsigned int bulk;
    uint8_t color;
    unsigned int imgsize;
    void *imgdata;
 
    /* USB speed specific */
    int mult;
    int burst;
    int maxpkt;
    enum usb_device_speed speed;
 
    /* uvc specific flags */
    int first_buffer_queued;
    int uvc_shutdown_requested;
 
    /* uvc buffer queue and dequeue counters */
    unsigned long long int qbuf_count;
    unsigned long long int dqbuf_count;
 
    /* v4l2 device hook */
    struct v4l2_device *vdev;
    uint8_t cs;
    uint8_t entity_id;
    struct v4l2_buffer ubuf;
 
    struct v4l2_buffer_info *vbuf_info;
    int abandon_count;
    enum USB_STATE usb_state;
    unsigned int first_usb_get_ready_pts;
    unsigned int first_usb_get_ok_pts;
    unsigned int first_usb_send_ok_pts;
    unsigned int first_cmd_pts;
    unsigned int stream_on_pts;
    int get_buf_count;
};
 
 
 
int uvc_gadget_main(struct uvc_function_config *fc);
int uvc_video_reqbufs(struct uvc_device *dev, int nbufs);
int uvc_video_stream(struct uvc_device *dev, int enable);
 
#ifdef __cplusplus
}
#endif
 
#endif /* _UVC_GADGET_H_ */