1/* SPDX-License-Identifier: GPL-2.0 */
2/*
3 * A virtual stateless device for stateless uAPI development purposes.
4 *
5 * This tool's objective is to help the development and testing of userspace
6 * applications that use the V4L2 stateless API to decode media.
7 *
8 * A userspace implementation can use visl to run a decoding loop even when no
9 * hardware is available or when the kernel uAPI for the codec has not been
10 * upstreamed yet. This can reveal bugs at an early stage.
11 *
12 * This driver can also trace the contents of the V4L2 controls submitted to it.
13 * It can also dump the contents of the vb2 buffers through a debugfs
14 * interface. This is in many ways similar to the tracing infrastructure
15 * available for other popular encode/decode APIs out there and can help develop
16 * a userspace application by using another (working) one as a reference.
17 *
18 * Note that no actual decoding of video frames is performed by visl. The V4L2
19 * test pattern generator is used to write various debug information to the
20 * capture buffers instead.
21 *
22 * Copyright (C) 2022 Collabora, Ltd.
23 *
24 * Based on the vim2m driver, that is:
25 *
26 * Copyright (c) 2009-2010 Samsung Electronics Co., Ltd.
27 * Pawel Osciak, <pawel@osciak.com>
28 * Marek Szyprowski, <m.szyprowski@samsung.com>
29 *
30 * Based on the vicodec driver, that is:
31 *
32 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
33 *
34 * Based on the Cedrus VPU driver, that is:
35 *
36 * Copyright (C) 2016 Florent Revest <florent.revest@free-electrons.com>
37 * Copyright (C) 2018 Paul Kocialkowski <paul.kocialkowski@bootlin.com>
38 * Copyright (C) 2018 Bootlin
39 */
40
41#ifndef _VISL_H_
42#define _VISL_H_
43
44#include <linux/debugfs.h>
45#include <linux/list.h>
46
47#include <media/v4l2-ctrls.h>
48#include <media/v4l2-device.h>
49#include <media/tpg/v4l2-tpg.h>
50
51#define VISL_NAME		"visl"
52#define VISL_M2M_NQUEUES	2
53
54#define TPG_STR_BUF_SZ		2048
55
56extern unsigned int visl_transtime_ms;
57
58struct visl_ctrls {
59	const struct visl_ctrl_desc *ctrls;
60	unsigned int num_ctrls;
61};
62
63struct visl_coded_format_desc {
64	u32 pixelformat;
65	struct v4l2_frmsize_stepwise frmsize;
66	const struct visl_ctrls *ctrls;
67	unsigned int num_decoded_fmts;
68	const u32 *decoded_fmts;
69};
70
71extern const struct visl_coded_format_desc visl_coded_fmts[];
72extern const size_t num_coded_fmts;
73
74enum {
75	V4L2_M2M_SRC = 0,
76	V4L2_M2M_DST = 1,
77};
78
79extern unsigned int visl_debug;
80#define dprintk(dev, fmt, arg...) \
81	v4l2_dbg(1, visl_debug, &(dev)->v4l2_dev, "%s: " fmt, __func__, ## arg)
82
83extern int visl_dprintk_frame_start;
84extern unsigned int visl_dprintk_nframes;
85extern bool keep_bitstream_buffers;
86extern int bitstream_trace_frame_start;
87extern unsigned int bitstream_trace_nframes;
88extern bool tpg_verbose;
89
90#define frame_dprintk(dev, current, fmt, arg...) \
91	do { \
92		if (visl_dprintk_frame_start > -1 && \
93		    (current) >= visl_dprintk_frame_start && \
94		    (current) < visl_dprintk_frame_start + visl_dprintk_nframes) \
95			dprintk(dev, fmt, ## arg); \
96	} while (0) \
97
98struct visl_q_data {
99	unsigned int		sequence;
100};
101
102struct visl_dev {
103	struct v4l2_device	v4l2_dev;
104	struct video_device	vfd;
105#ifdef CONFIG_MEDIA_CONTROLLER
106	struct media_device	mdev;
107#endif
108
109	struct mutex		dev_mutex;
110
111	struct v4l2_m2m_dev	*m2m_dev;
112
113#ifdef CONFIG_VISL_DEBUGFS
114	struct dentry		*debugfs_root;
115	struct dentry		*bitstream_debugfs;
116	struct list_head	bitstream_blobs;
117
118	/* Protects the "blob" list */
119	struct mutex		bitstream_lock;
120#endif
121};
122
123enum visl_codec {
124	VISL_CODEC_NONE,
125	VISL_CODEC_FWHT,
126	VISL_CODEC_MPEG2,
127	VISL_CODEC_VP8,
128	VISL_CODEC_VP9,
129	VISL_CODEC_H264,
130	VISL_CODEC_HEVC,
131	VISL_CODEC_AV1,
132};
133
134struct visl_blob {
135	struct list_head list;
136	struct dentry *dentry;
137	struct debugfs_blob_wrapper blob;
138};
139
140struct visl_ctx {
141	struct v4l2_fh		fh;
142	struct visl_dev	*dev;
143	struct v4l2_ctrl_handler hdl;
144
145	struct mutex		vb_mutex;
146
147	struct visl_q_data	q_data[VISL_M2M_NQUEUES];
148	enum   visl_codec	current_codec;
149
150	const struct visl_coded_format_desc *coded_format_desc;
151
152	struct v4l2_format	coded_fmt;
153	struct v4l2_format	decoded_fmt;
154
155	struct tpg_data		tpg;
156	u64			capture_streamon_jiffies;
157	char			*tpg_str_buf;
158};
159
160struct visl_ctrl_desc {
161	struct v4l2_ctrl_config cfg;
162};
163
164static inline struct visl_ctx *visl_file_to_ctx(struct file *file)
165{
166	return container_of(file->private_data, struct visl_ctx, fh);
167}
168
169static inline struct visl_ctx *visl_v4l2fh_to_ctx(struct v4l2_fh *v4l2_fh)
170{
171	return container_of(v4l2_fh, struct visl_ctx, fh);
172}
173
174void *visl_find_control_data(struct visl_ctx *ctx, u32 id);
175struct v4l2_ctrl *visl_find_control(struct visl_ctx *ctx, u32 id);
176u32 visl_control_num_elems(struct visl_ctx *ctx, u32 id);
177
178#endif /* _VISL_H_ */
179