File Index
Files and directories
Sign in
FILE

vicodec-core.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/test-drivers/vicodec/vicodec-core.c C
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * A virtual codec example device.
   4 *
   5 * Copyright 2018 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
   6 *
   7 * This is a virtual codec device driver for testing the codec framework.
   8 * It simulates a device that uses memory buffers for both source and
   9 * destination and encodes or decodes the data.
  10 */
  11
  12#include <linux/module.h>
  13#include <linux/delay.h>
  14#include <linux/fs.h>
  15#include <linux/sched.h>
  16#include <linux/slab.h>
  17
  18#include <linux/platform_device.h>
  19#include <media/v4l2-mem2mem.h>
  20#include <media/v4l2-device.h>
  21#include <media/v4l2-ioctl.h>
  22#include <media/v4l2-ctrls.h>
  23#include <media/v4l2-event.h>
  24#include <media/videobuf2-vmalloc.h>
  25
  26#include "codec-v4l2-fwht.h"
  27
  28MODULE_DESCRIPTION("Virtual codec device");
  29MODULE_AUTHOR("Hans Verkuil <hverkuil@kernel.org>");
  30MODULE_LICENSE("GPL v2");
  31
  32static bool multiplanar;
  33module_param(multiplanar, bool, 0444);
  34MODULE_PARM_DESC(multiplanar,
  35		 " use multi-planar API instead of single-planar API");
  36
  37static unsigned int debug;
  38module_param(debug, uint, 0644);
  39MODULE_PARM_DESC(debug, " activates debug info");
  40
  41#define VICODEC_NAME		"vicodec"
  42#define MAX_WIDTH		4096U
  43#define MIN_WIDTH		640U
  44#define MAX_HEIGHT		2160U
  45#define MIN_HEIGHT		360U
  46/* Recommended number of buffers for the stateful codecs */
  47#define VICODEC_REC_BUFS	2
  48
  49#define dprintk(dev, fmt, arg...) \
  50	v4l2_dbg(1, debug, &dev->v4l2_dev, "%s: " fmt, __func__, ## arg)
  51
  52
  53struct pixfmt_info {
  54	u32 id;
  55	unsigned int bytesperline_mult;
  56	unsigned int sizeimage_mult;
  57	unsigned int sizeimage_div;
  58	unsigned int luma_step;
  59	unsigned int chroma_step;
  60	/* Chroma plane subsampling */
  61	unsigned int width_div;
  62	unsigned int height_div;
  63};
  64
  65static const struct v4l2_fwht_pixfmt_info pixfmt_fwht = {
  66	V4L2_PIX_FMT_FWHT, 0, 3, 1, 1, 1, 1, 1, 0, 1
  67};
  68
  69static const struct v4l2_fwht_pixfmt_info pixfmt_stateless_fwht = {
  70	V4L2_PIX_FMT_FWHT_STATELESS, 0, 3, 1, 1, 1, 1, 1, 0, 1
  71};
  72
  73static void vicodec_dev_release(struct device *dev)
  74{
  75}
  76
  77static struct platform_device vicodec_pdev = {
  78	.name		= VICODEC_NAME,
  79	.dev.release	= vicodec_dev_release,
  80};
  81
  82/* Per-queue, driver-specific private data */
  83struct vicodec_q_data {
  84	unsigned int		coded_width;
  85	unsigned int		coded_height;
  86	unsigned int		visible_width;
  87	unsigned int		visible_height;
  88	unsigned int		sizeimage;
  89	unsigned int		vb2_sizeimage;
  90	unsigned int		sequence;
  91	const struct v4l2_fwht_pixfmt_info *info;
  92};
  93
  94enum {
  95	V4L2_M2M_SRC = 0,
  96	V4L2_M2M_DST = 1,
  97};
  98
  99struct vicodec_dev_instance {
 100	struct video_device     vfd;
 101	struct mutex            mutex;
 102	spinlock_t              lock;
 103	struct v4l2_m2m_dev     *m2m_dev;
 104};
 105
 106struct vicodec_dev {
 107	struct v4l2_device	v4l2_dev;
 108	struct vicodec_dev_instance stateful_enc;
 109	struct vicodec_dev_instance stateful_dec;
 110	struct vicodec_dev_instance stateless_dec;
 111#ifdef CONFIG_MEDIA_CONTROLLER
 112	struct media_device	mdev;
 113#endif
 114
 115};
 116
 117struct vicodec_ctx {
 118	struct v4l2_fh		fh;
 119	struct vicodec_dev	*dev;
 120	bool			is_enc;
 121	bool			is_stateless;
 122	spinlock_t		*lock;
 123
 124	struct v4l2_ctrl_handler hdl;
 125
 126	/* Source and destination queue data */
 127	struct vicodec_q_data   q_data[2];
 128	struct v4l2_fwht_state	state;
 129
 130	u32			cur_buf_offset;
 131	u32			comp_max_size;
 132	u32			comp_size;
 133	u32			header_size;
 134	u32			comp_magic_cnt;
 135	bool			comp_has_frame;
 136	bool			comp_has_next_frame;
 137	bool			first_source_change_sent;
 138	bool			source_changed;
 139};
 140
 141static const struct v4l2_event vicodec_eos_event = {
 142	.type = V4L2_EVENT_EOS
 143};
 144
 145static inline struct vicodec_ctx *file2ctx(struct file *file)
 146{
 147	return container_of(file_to_v4l2_fh(file), struct vicodec_ctx, fh);
 148}
 149
 150static struct vicodec_q_data *get_q_data(struct vicodec_ctx *ctx,
 151					 enum v4l2_buf_type type)
 152{
 153	switch (type) {
 154	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 155	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 156		return &ctx->q_data[V4L2_M2M_SRC];
 157	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 158	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 159		return &ctx->q_data[V4L2_M2M_DST];
 160	default:
 161		break;
 162	}
 163	return NULL;
 164}
 165
 166static void copy_cap_to_ref(const u8 *cap, const struct v4l2_fwht_pixfmt_info *info,
 167		struct v4l2_fwht_state *state)
 168{
 169	int plane_idx;
 170	u8 *p_ref = state->ref_frame.buf;
 171	unsigned int cap_stride = state->stride;
 172	unsigned int ref_stride = state->ref_stride;
 173
 174	for (plane_idx = 0; plane_idx < info->planes_num; plane_idx++) {
 175		int i;
 176		unsigned int h_div = (plane_idx == 1 || plane_idx == 2) ?
 177			info->height_div : 1;
 178		const u8 *row_cap = cap;
 179		u8 *row_ref = p_ref;
 180
 181		if (info->planes_num == 3 && plane_idx == 1) {
 182			cap_stride /= 2;
 183			ref_stride /= 2;
 184		}
 185
 186		if (plane_idx == 1 &&
 187		    (info->id == V4L2_PIX_FMT_NV24 ||
 188		     info->id == V4L2_PIX_FMT_NV42)) {
 189			cap_stride *= 2;
 190			ref_stride *= 2;
 191		}
 192
 193		for (i = 0; i < state->visible_height / h_div; i++) {
 194			memcpy(row_ref, row_cap, ref_stride);
 195			row_ref += ref_stride;
 196			row_cap += cap_stride;
 197		}
 198		cap += cap_stride * (state->coded_height / h_div);
 199		p_ref += ref_stride * (state->coded_height / h_div);
 200	}
 201}
 202
 203static bool validate_by_version(unsigned int flags, unsigned int version)
 204{
 205	if (!version || version > V4L2_FWHT_VERSION)
 206		return false;
 207
 208	if (version >= 2) {
 209		unsigned int components_num = 1 +
 210			((flags & V4L2_FWHT_FL_COMPONENTS_NUM_MSK) >>
 211			 V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET);
 212		unsigned int pixenc = flags & V4L2_FWHT_FL_PIXENC_MSK;
 213
 214		if (components_num == 0 || components_num > 4 || !pixenc)
 215			return false;
 216	}
 217	return true;
 218}
 219
 220static bool validate_stateless_params_flags(const struct v4l2_ctrl_fwht_params *params,
 221					    const struct v4l2_fwht_pixfmt_info *cur_info)
 222{
 223	unsigned int width_div =
 224		(params->flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2;
 225	unsigned int height_div =
 226		(params->flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2;
 227	unsigned int components_num = 3;
 228	unsigned int pixenc = 0;
 229
 230	if (params->version < 3)
 231		return false;
 232
 233	components_num = 1 + ((params->flags & V4L2_FWHT_FL_COMPONENTS_NUM_MSK) >>
 234			      V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET);
 235	pixenc = (params->flags & V4L2_FWHT_FL_PIXENC_MSK);
 236	if (v4l2_fwht_validate_fmt(cur_info, width_div, height_div,
 237				    components_num, pixenc))
 238		return true;
 239	return false;
 240}
 241
 242
 243static void update_state_from_header(struct vicodec_ctx *ctx)
 244{
 245	const struct fwht_cframe_hdr *p_hdr = &ctx->state.header;
 246
 247	ctx->state.visible_width = ntohl(p_hdr->width);
 248	ctx->state.visible_height = ntohl(p_hdr->height);
 249	ctx->state.colorspace = ntohl(p_hdr->colorspace);
 250	ctx->state.xfer_func = ntohl(p_hdr->xfer_func);
 251	ctx->state.ycbcr_enc = ntohl(p_hdr->ycbcr_enc);
 252	ctx->state.quantization = ntohl(p_hdr->quantization);
 253}
 254
 255static int device_process(struct vicodec_ctx *ctx,
 256			  struct vb2_v4l2_buffer *src_vb,
 257			  struct vb2_v4l2_buffer *dst_vb)
 258{
 259	struct vicodec_dev *dev = ctx->dev;
 260	struct v4l2_fwht_state *state = &ctx->state;
 261	u8 *p_src, *p_dst;
 262	int ret = 0;
 263
 264	if (ctx->is_enc || ctx->is_stateless)
 265		p_src = vb2_plane_vaddr(&src_vb->vb2_buf, 0);
 266	else
 267		p_src = state->compressed_frame;
 268
 269	if (ctx->is_stateless) {
 270		struct media_request *src_req = src_vb->vb2_buf.req_obj.req;
 271
 272		ret = v4l2_ctrl_request_setup(src_req, &ctx->hdl);
 273		if (ret)
 274			return ret;
 275		update_state_from_header(ctx);
 276
 277		ctx->state.header.size =
 278			htonl(vb2_get_plane_payload(&src_vb->vb2_buf, 0));
 279		/*
 280		 * set the reference buffer from the reference timestamp
 281		 * only if this is a P-frame
 282		 */
 283		if (!(ntohl(ctx->state.header.flags) & V4L2_FWHT_FL_I_FRAME)) {
 284			struct vb2_buffer *ref_vb2_buf;
 285			struct vb2_queue *vq_cap =
 286				v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
 287						V4L2_BUF_TYPE_VIDEO_CAPTURE);
 288
 289			ref_vb2_buf = vb2_find_buffer(vq_cap, ctx->state.ref_frame_ts);
 290			if (!ref_vb2_buf)
 291				return -EINVAL;
 292			if (ref_vb2_buf->state == VB2_BUF_STATE_ERROR)
 293				ret = -EINVAL;
 294			ctx->state.ref_frame.buf =
 295				vb2_plane_vaddr(ref_vb2_buf, 0);
 296		} else {
 297			ctx->state.ref_frame.buf = NULL;
 298		}
 299	}
 300	p_dst = vb2_plane_vaddr(&dst_vb->vb2_buf, 0);
 301	if (!p_src || !p_dst) {
 302		v4l2_err(&dev->v4l2_dev,
 303			 "Acquiring kernel pointers to buffers failed\n");
 304		return -EFAULT;
 305	}
 306
 307	if (ctx->is_enc) {
 308		struct vicodec_q_data *q_src;
 309		int comp_sz_or_errcode;
 310
 311		q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
 312		state->info = q_src->info;
 313		comp_sz_or_errcode = v4l2_fwht_encode(state, p_src, p_dst);
 314		if (comp_sz_or_errcode < 0)
 315			return comp_sz_or_errcode;
 316		vb2_set_plane_payload(&dst_vb->vb2_buf, 0, comp_sz_or_errcode);
 317	} else {
 318		struct vicodec_q_data *q_dst;
 319		unsigned int comp_frame_size = ntohl(ctx->state.header.size);
 320
 321		q_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
 322		if (comp_frame_size > ctx->comp_max_size)
 323			return -EINVAL;
 324		state->info = q_dst->info;
 325		ret = v4l2_fwht_decode(state, p_src, p_dst);
 326		if (ret < 0)
 327			return ret;
 328		if (!ctx->is_stateless)
 329			copy_cap_to_ref(p_dst, ctx->state.info, &ctx->state);
 330
 331		vb2_set_plane_payload(&dst_vb->vb2_buf, 0, q_dst->sizeimage);
 332		if (ntohl(ctx->state.header.flags) & V4L2_FWHT_FL_I_FRAME)
 333			dst_vb->flags |= V4L2_BUF_FLAG_KEYFRAME;
 334		else
 335			dst_vb->flags |= V4L2_BUF_FLAG_PFRAME;
 336	}
 337	return ret;
 338}
 339
 340/*
 341 * mem2mem callbacks
 342 */
 343static enum vb2_buffer_state get_next_header(struct vicodec_ctx *ctx,
 344					     u8 **pp, u32 sz)
 345{
 346	static const u8 magic[] = {
 347		0x4f, 0x4f, 0x4f, 0x4f, 0xff, 0xff, 0xff, 0xff
 348	};
 349	u8 *p = *pp;
 350	u32 state;
 351	u8 *header = (u8 *)&ctx->state.header;
 352
 353	state = VB2_BUF_STATE_DONE;
 354
 355	if (!ctx->header_size) {
 356		state = VB2_BUF_STATE_ERROR;
 357		for (; p < *pp + sz; p++) {
 358			u32 copy;
 359
 360			p = memchr(p, magic[ctx->comp_magic_cnt],
 361				   *pp + sz - p);
 362			if (!p) {
 363				ctx->comp_magic_cnt = 0;
 364				p = *pp + sz;
 365				break;
 366			}
 367			copy = sizeof(magic) - ctx->comp_magic_cnt;
 368			if (*pp + sz - p < copy)
 369				copy = *pp + sz - p;
 370
 371			memcpy(header + ctx->comp_magic_cnt, p, copy);
 372			ctx->comp_magic_cnt += copy;
 373			if (!memcmp(header, magic, ctx->comp_magic_cnt)) {
 374				p += copy;
 375				state = VB2_BUF_STATE_DONE;
 376				break;
 377			}
 378			ctx->comp_magic_cnt = 0;
 379		}
 380		if (ctx->comp_magic_cnt < sizeof(magic)) {
 381			*pp = p;
 382			return state;
 383		}
 384		ctx->header_size = sizeof(magic);
 385	}
 386
 387	if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) {
 388		u32 copy = sizeof(struct fwht_cframe_hdr) - ctx->header_size;
 389
 390		if (*pp + sz - p < copy)
 391			copy = *pp + sz - p;
 392
 393		memcpy(header + ctx->header_size, p, copy);
 394		p += copy;
 395		ctx->header_size += copy;
 396	}
 397	*pp = p;
 398	return state;
 399}
 400
 401/* device_run() - prepares and starts the device */
 402static void device_run(void *priv)
 403{
 404	struct vicodec_ctx *ctx = priv;
 405	struct vicodec_dev *dev = ctx->dev;
 406	struct vb2_v4l2_buffer *src_buf, *dst_buf;
 407	struct vicodec_q_data *q_src, *q_dst;
 408	u32 state;
 409	struct media_request *src_req;
 410
 411	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
 412	dst_buf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 413	src_req = src_buf->vb2_buf.req_obj.req;
 414
 415	q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
 416	q_dst = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
 417
 418	state = VB2_BUF_STATE_DONE;
 419	if (device_process(ctx, src_buf, dst_buf))
 420		state = VB2_BUF_STATE_ERROR;
 421	else
 422		dst_buf->sequence = q_dst->sequence++;
 423	dst_buf->flags &= ~V4L2_BUF_FLAG_LAST;
 424	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf);
 425
 426	spin_lock(ctx->lock);
 427	if (!ctx->comp_has_next_frame &&
 428	    v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx, src_buf)) {
 429		dst_buf->flags |= V4L2_BUF_FLAG_LAST;
 430		v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event);
 431		v4l2_m2m_mark_stopped(ctx->fh.m2m_ctx);
 432	}
 433	if (ctx->is_enc || ctx->is_stateless) {
 434		src_buf->sequence = q_src->sequence++;
 435		src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 436		v4l2_m2m_buf_done(src_buf, state);
 437	} else if (vb2_get_plane_payload(&src_buf->vb2_buf, 0) == ctx->cur_buf_offset) {
 438		src_buf->sequence = q_src->sequence++;
 439		src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 440		v4l2_m2m_buf_done(src_buf, state);
 441		ctx->cur_buf_offset = 0;
 442		ctx->comp_has_next_frame = false;
 443	}
 444	v4l2_m2m_buf_done(dst_buf, state);
 445
 446	ctx->comp_size = 0;
 447	ctx->header_size = 0;
 448	ctx->comp_magic_cnt = 0;
 449	ctx->comp_has_frame = false;
 450	spin_unlock(ctx->lock);
 451	if (ctx->is_stateless && src_req)
 452		v4l2_ctrl_request_complete(src_req, &ctx->hdl);
 453
 454	if (ctx->is_enc)
 455		v4l2_m2m_job_finish(dev->stateful_enc.m2m_dev, ctx->fh.m2m_ctx);
 456	else if (ctx->is_stateless)
 457		v4l2_m2m_job_finish(dev->stateless_dec.m2m_dev,
 458				    ctx->fh.m2m_ctx);
 459	else
 460		v4l2_m2m_job_finish(dev->stateful_dec.m2m_dev, ctx->fh.m2m_ctx);
 461}
 462
 463static void job_remove_src_buf(struct vicodec_ctx *ctx, u32 state)
 464{
 465	struct vb2_v4l2_buffer *src_buf;
 466	struct vicodec_q_data *q_src;
 467
 468	q_src = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_OUTPUT);
 469	spin_lock(ctx->lock);
 470	src_buf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
 471	src_buf->sequence = q_src->sequence++;
 472	v4l2_m2m_buf_done(src_buf, state);
 473	ctx->cur_buf_offset = 0;
 474	spin_unlock(ctx->lock);
 475}
 476
 477static const struct v4l2_fwht_pixfmt_info *
 478info_from_header(const struct fwht_cframe_hdr *p_hdr)
 479{
 480	unsigned int flags = ntohl(p_hdr->flags);
 481	unsigned int width_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2;
 482	unsigned int height_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2;
 483	unsigned int components_num = 3;
 484	unsigned int pixenc = 0;
 485	unsigned int version = ntohl(p_hdr->version);
 486
 487	if (version >= 2) {
 488		components_num = 1 + ((flags & V4L2_FWHT_FL_COMPONENTS_NUM_MSK) >>
 489				V4L2_FWHT_FL_COMPONENTS_NUM_OFFSET);
 490		pixenc = (flags & V4L2_FWHT_FL_PIXENC_MSK);
 491	}
 492	return v4l2_fwht_find_nth_fmt(width_div, height_div,
 493				     components_num, pixenc, 0);
 494}
 495
 496static bool is_header_valid(const struct fwht_cframe_hdr *p_hdr)
 497{
 498	const struct v4l2_fwht_pixfmt_info *info;
 499	unsigned int w = ntohl(p_hdr->width);
 500	unsigned int h = ntohl(p_hdr->height);
 501	unsigned int version = ntohl(p_hdr->version);
 502	unsigned int flags = ntohl(p_hdr->flags);
 503
 504	if (w < MIN_WIDTH || w > MAX_WIDTH || h < MIN_HEIGHT || h > MAX_HEIGHT)
 505		return false;
 506
 507	if (!validate_by_version(flags, version))
 508		return false;
 509
 510	info = info_from_header(p_hdr);
 511	if (!info)
 512		return false;
 513	return true;
 514}
 515
 516static void update_capture_data_from_header(struct vicodec_ctx *ctx)
 517{
 518	struct vicodec_q_data *q_dst = get_q_data(ctx,
 519						  V4L2_BUF_TYPE_VIDEO_CAPTURE);
 520	const struct fwht_cframe_hdr *p_hdr = &ctx->state.header;
 521	const struct v4l2_fwht_pixfmt_info *info = info_from_header(p_hdr);
 522	unsigned int flags = ntohl(p_hdr->flags);
 523	unsigned int hdr_width_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2;
 524	unsigned int hdr_height_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2;
 525
 526	/*
 527	 * This function should not be used by a stateless codec since
 528	 * it changes values in q_data that are not request specific
 529	 */
 530	WARN_ON(ctx->is_stateless);
 531
 532	q_dst->info = info;
 533	q_dst->visible_width = ntohl(p_hdr->width);
 534	q_dst->visible_height = ntohl(p_hdr->height);
 535	q_dst->coded_width = vic_round_dim(q_dst->visible_width, hdr_width_div);
 536	q_dst->coded_height = vic_round_dim(q_dst->visible_height,
 537					    hdr_height_div);
 538
 539	q_dst->sizeimage = q_dst->coded_width * q_dst->coded_height *
 540		q_dst->info->sizeimage_mult / q_dst->info->sizeimage_div;
 541	ctx->state.colorspace = ntohl(p_hdr->colorspace);
 542
 543	ctx->state.xfer_func = ntohl(p_hdr->xfer_func);
 544	ctx->state.ycbcr_enc = ntohl(p_hdr->ycbcr_enc);
 545	ctx->state.quantization = ntohl(p_hdr->quantization);
 546}
 547
 548static void set_last_buffer(struct vb2_v4l2_buffer *dst_buf,
 549			    const struct vb2_v4l2_buffer *src_buf,
 550			    struct vicodec_ctx *ctx)
 551{
 552	struct vicodec_q_data *q_dst = get_q_data(ctx,
 553						  V4L2_BUF_TYPE_VIDEO_CAPTURE);
 554
 555	vb2_set_plane_payload(&dst_buf->vb2_buf, 0, 0);
 556	dst_buf->sequence = q_dst->sequence++;
 557
 558	v4l2_m2m_buf_copy_metadata(src_buf, dst_buf);
 559	dst_buf->flags |= V4L2_BUF_FLAG_LAST;
 560	v4l2_m2m_buf_done(dst_buf, VB2_BUF_STATE_DONE);
 561}
 562
 563static int job_ready(void *priv)
 564{
 565	static const u8 magic[] = {
 566		0x4f, 0x4f, 0x4f, 0x4f, 0xff, 0xff, 0xff, 0xff
 567	};
 568	struct vicodec_ctx *ctx = priv;
 569	struct vb2_v4l2_buffer *src_buf;
 570	u8 *p_src;
 571	u8 *p;
 572	u32 sz;
 573	u32 state;
 574	struct vicodec_q_data *q_dst = get_q_data(ctx,
 575						  V4L2_BUF_TYPE_VIDEO_CAPTURE);
 576	unsigned int flags;
 577	unsigned int hdr_width_div;
 578	unsigned int hdr_height_div;
 579	unsigned int max_to_copy;
 580	unsigned int comp_frame_size;
 581
 582	if (ctx->source_changed)
 583		return 0;
 584	if (ctx->is_stateless || ctx->is_enc || ctx->comp_has_frame)
 585		return 1;
 586
 587restart:
 588	ctx->comp_has_next_frame = false;
 589	src_buf = v4l2_m2m_next_src_buf(ctx->fh.m2m_ctx);
 590	if (!src_buf)
 591		return 0;
 592	p_src = vb2_plane_vaddr(&src_buf->vb2_buf, 0);
 593	sz = vb2_get_plane_payload(&src_buf->vb2_buf, 0);
 594	p = p_src + ctx->cur_buf_offset;
 595
 596	state = VB2_BUF_STATE_DONE;
 597
 598	if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) {
 599		state = get_next_header(ctx, &p, p_src + sz - p);
 600		if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) {
 601			if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx,
 602							      src_buf))
 603				return 1;
 604			job_remove_src_buf(ctx, state);
 605			goto restart;
 606		}
 607	}
 608
 609	comp_frame_size = ntohl(ctx->state.header.size);
 610
 611	/*
 612	 * The current scanned frame might be the first frame of a new
 613	 * resolution so its size might be larger than ctx->comp_max_size.
 614	 * In that case it is copied up to the current buffer capacity and
 615	 * the copy will continue after allocating new large enough buffer
 616	 * when restreaming
 617	 */
 618	max_to_copy = min(comp_frame_size, ctx->comp_max_size);
 619
 620	if (ctx->comp_size < max_to_copy) {
 621		u32 copy = max_to_copy - ctx->comp_size;
 622
 623		if (copy > p_src + sz - p)
 624			copy = p_src + sz - p;
 625
 626		memcpy(ctx->state.compressed_frame + ctx->comp_size,
 627		       p, copy);
 628		p += copy;
 629		ctx->comp_size += copy;
 630		if (ctx->comp_size < max_to_copy) {
 631			if (v4l2_m2m_is_last_draining_src_buf(ctx->fh.m2m_ctx,
 632							      src_buf))
 633				return 1;
 634			job_remove_src_buf(ctx, state);
 635			goto restart;
 636		}
 637	}
 638	ctx->cur_buf_offset = p - p_src;
 639	if (ctx->comp_size == comp_frame_size)
 640		ctx->comp_has_frame = true;
 641	ctx->comp_has_next_frame = false;
 642	if (ctx->comp_has_frame && sz - ctx->cur_buf_offset >=
 643			sizeof(struct fwht_cframe_hdr)) {
 644		struct fwht_cframe_hdr *p_hdr = (struct fwht_cframe_hdr *)p;
 645		u32 frame_size = ntohl(p_hdr->size);
 646		u32 remaining = sz - ctx->cur_buf_offset - sizeof(*p_hdr);
 647
 648		if (!memcmp(p, magic, sizeof(magic)))
 649			ctx->comp_has_next_frame = remaining >= frame_size;
 650	}
 651	/*
 652	 * if the header is invalid the device_run will just drop the frame
 653	 * with an error
 654	 */
 655	if (!is_header_valid(&ctx->state.header) && ctx->comp_has_frame)
 656		return 1;
 657	flags = ntohl(ctx->state.header.flags);
 658	hdr_width_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_WIDTH) ? 1 : 2;
 659	hdr_height_div = (flags & V4L2_FWHT_FL_CHROMA_FULL_HEIGHT) ? 1 : 2;
 660
 661	if (ntohl(ctx->state.header.width) != q_dst->visible_width ||
 662	    ntohl(ctx->state.header.height) != q_dst->visible_height ||
 663	    !q_dst->info ||
 664	    hdr_width_div != q_dst->info->width_div ||
 665	    hdr_height_div != q_dst->info->height_div) {
 666		static const struct v4l2_event rs_event = {
 667			.type = V4L2_EVENT_SOURCE_CHANGE,
 668			.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
 669		};
 670
 671		struct vb2_v4l2_buffer *dst_buf =
 672			v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
 673
 674		update_capture_data_from_header(ctx);
 675		v4l2_event_queue_fh(&ctx->fh, &rs_event);
 676		set_last_buffer(dst_buf, src_buf, ctx);
 677		ctx->source_changed = true;
 678		return 0;
 679	}
 680	return 1;
 681}
 682
 683/*
 684 * video ioctls
 685 */
 686
 687static const struct v4l2_fwht_pixfmt_info *find_fmt(u32 fmt)
 688{
 689	const struct v4l2_fwht_pixfmt_info *info =
 690		v4l2_fwht_find_pixfmt(fmt);
 691
 692	if (!info)
 693		info = v4l2_fwht_get_pixfmt(0);
 694	return info;
 695}
 696
 697static int vidioc_querycap(struct file *file, void *priv,
 698			   struct v4l2_capability *cap)
 699{
 700	strscpy(cap->driver, VICODEC_NAME, sizeof(cap->driver));
 701	strscpy(cap->card, VICODEC_NAME, sizeof(cap->card));
 702	snprintf(cap->bus_info, sizeof(cap->bus_info),
 703			"platform:%s", VICODEC_NAME);
 704	return 0;
 705}
 706
 707static int enum_fmt(struct v4l2_fmtdesc *f, struct vicodec_ctx *ctx,
 708		    bool is_out)
 709{
 710	bool is_uncomp = (ctx->is_enc && is_out) || (!ctx->is_enc && !is_out);
 711
 712	if (V4L2_TYPE_IS_MULTIPLANAR(f->type) && !multiplanar)
 713		return -EINVAL;
 714	if (!V4L2_TYPE_IS_MULTIPLANAR(f->type) && multiplanar)
 715		return -EINVAL;
 716
 717	if (is_uncomp) {
 718		const struct v4l2_fwht_pixfmt_info *info =
 719					get_q_data(ctx, f->type)->info;
 720
 721		if (ctx->is_enc ||
 722		    !vb2_is_streaming(&ctx->fh.m2m_ctx->cap_q_ctx.q))
 723			info = v4l2_fwht_get_pixfmt(f->index);
 724		else
 725			info = v4l2_fwht_find_nth_fmt(info->width_div,
 726						     info->height_div,
 727						     info->components_num,
 728						     info->pixenc,
 729						     f->index);
 730		if (!info)
 731			return -EINVAL;
 732		f->pixelformat = info->id;
 733	} else {
 734		if (f->index)
 735			return -EINVAL;
 736		f->pixelformat = ctx->is_stateless ?
 737			V4L2_PIX_FMT_FWHT_STATELESS : V4L2_PIX_FMT_FWHT;
 738		if (!ctx->is_enc && !ctx->is_stateless)
 739			f->flags = V4L2_FMT_FLAG_DYN_RESOLUTION |
 740				   V4L2_FMT_FLAG_CONTINUOUS_BYTESTREAM;
 741	}
 742	return 0;
 743}
 744
 745static int vidioc_enum_fmt_vid_cap(struct file *file, void *priv,
 746				   struct v4l2_fmtdesc *f)
 747{
 748	struct vicodec_ctx *ctx = file2ctx(file);
 749
 750	return enum_fmt(f, ctx, false);
 751}
 752
 753static int vidioc_enum_fmt_vid_out(struct file *file, void *priv,
 754				   struct v4l2_fmtdesc *f)
 755{
 756	struct vicodec_ctx *ctx = file2ctx(file);
 757
 758	return enum_fmt(f, ctx, true);
 759}
 760
 761static int vidioc_g_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
 762{
 763	struct vicodec_q_data *q_data;
 764	struct v4l2_pix_format_mplane *pix_mp;
 765	struct v4l2_pix_format *pix;
 766	const struct v4l2_fwht_pixfmt_info *info;
 767
 768	q_data = get_q_data(ctx, f->type);
 769	info = q_data->info;
 770
 771	switch (f->type) {
 772	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 773	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 774		if (multiplanar)
 775			return -EINVAL;
 776		pix = &f->fmt.pix;
 777		pix->width = q_data->coded_width;
 778		pix->height = q_data->coded_height;
 779		pix->field = V4L2_FIELD_NONE;
 780		pix->pixelformat = info->id;
 781		pix->bytesperline = q_data->coded_width *
 782					info->bytesperline_mult;
 783		pix->sizeimage = q_data->sizeimage;
 784		pix->colorspace = ctx->state.colorspace;
 785		pix->xfer_func = ctx->state.xfer_func;
 786		pix->ycbcr_enc = ctx->state.ycbcr_enc;
 787		pix->quantization = ctx->state.quantization;
 788		break;
 789
 790	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 791	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 792		if (!multiplanar)
 793			return -EINVAL;
 794		pix_mp = &f->fmt.pix_mp;
 795		pix_mp->width = q_data->coded_width;
 796		pix_mp->height = q_data->coded_height;
 797		pix_mp->field = V4L2_FIELD_NONE;
 798		pix_mp->pixelformat = info->id;
 799		pix_mp->num_planes = 1;
 800		pix_mp->plane_fmt[0].bytesperline =
 801				q_data->coded_width * info->bytesperline_mult;
 802		pix_mp->plane_fmt[0].sizeimage = q_data->sizeimage;
 803		pix_mp->colorspace = ctx->state.colorspace;
 804		pix_mp->xfer_func = ctx->state.xfer_func;
 805		pix_mp->ycbcr_enc = ctx->state.ycbcr_enc;
 806		pix_mp->quantization = ctx->state.quantization;
 807		break;
 808	default:
 809		return -EINVAL;
 810	}
 811	return 0;
 812}
 813
 814static int vidioc_g_fmt_vid_out(struct file *file, void *priv,
 815				struct v4l2_format *f)
 816{
 817	return vidioc_g_fmt(file2ctx(file), f);
 818}
 819
 820static int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 821				struct v4l2_format *f)
 822{
 823	return vidioc_g_fmt(file2ctx(file), f);
 824}
 825
 826static int vidioc_try_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
 827{
 828	struct v4l2_pix_format_mplane *pix_mp;
 829	struct v4l2_pix_format *pix;
 830	struct v4l2_plane_pix_format *plane;
 831	const struct v4l2_fwht_pixfmt_info *info = ctx->is_stateless ?
 832		&pixfmt_stateless_fwht : &pixfmt_fwht;
 833
 834	switch (f->type) {
 835	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 836	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 837		pix = &f->fmt.pix;
 838		if (pix->pixelformat != V4L2_PIX_FMT_FWHT &&
 839		    pix->pixelformat != V4L2_PIX_FMT_FWHT_STATELESS)
 840			info = find_fmt(pix->pixelformat);
 841
 842		pix->width = clamp(pix->width, MIN_WIDTH, MAX_WIDTH);
 843		pix->width = vic_round_dim(pix->width, info->width_div);
 844
 845		pix->height = clamp(pix->height, MIN_HEIGHT, MAX_HEIGHT);
 846		pix->height = vic_round_dim(pix->height, info->height_div);
 847
 848		pix->field = V4L2_FIELD_NONE;
 849		pix->bytesperline =
 850			pix->width * info->bytesperline_mult;
 851		pix->sizeimage = pix->width * pix->height *
 852			info->sizeimage_mult / info->sizeimage_div;
 853		if (pix->pixelformat == V4L2_PIX_FMT_FWHT)
 854			pix->sizeimage += sizeof(struct fwht_cframe_hdr);
 855		break;
 856	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 857	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 858		pix_mp = &f->fmt.pix_mp;
 859		plane = pix_mp->plane_fmt;
 860		if (pix_mp->pixelformat != V4L2_PIX_FMT_FWHT &&
 861		    pix_mp->pixelformat != V4L2_PIX_FMT_FWHT_STATELESS)
 862			info = find_fmt(pix_mp->pixelformat);
 863		pix_mp->num_planes = 1;
 864
 865		pix_mp->width = clamp(pix_mp->width, MIN_WIDTH, MAX_WIDTH);
 866		pix_mp->width = vic_round_dim(pix_mp->width, info->width_div);
 867
 868		pix_mp->height = clamp(pix_mp->height, MIN_HEIGHT, MAX_HEIGHT);
 869		pix_mp->height = vic_round_dim(pix_mp->height,
 870					       info->height_div);
 871
 872		pix_mp->field = V4L2_FIELD_NONE;
 873		plane->bytesperline =
 874			pix_mp->width * info->bytesperline_mult;
 875		plane->sizeimage = pix_mp->width * pix_mp->height *
 876			info->sizeimage_mult / info->sizeimage_div;
 877		if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT)
 878			plane->sizeimage += sizeof(struct fwht_cframe_hdr);
 879		break;
 880	default:
 881		return -EINVAL;
 882	}
 883
 884	return 0;
 885}
 886
 887static int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 888				  struct v4l2_format *f)
 889{
 890	struct vicodec_ctx *ctx = file2ctx(file);
 891	struct v4l2_pix_format_mplane *pix_mp;
 892	struct v4l2_pix_format *pix;
 893
 894	switch (f->type) {
 895	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 896		if (multiplanar)
 897			return -EINVAL;
 898		pix = &f->fmt.pix;
 899		pix->pixelformat = ctx->is_enc ? V4L2_PIX_FMT_FWHT :
 900				   find_fmt(f->fmt.pix.pixelformat)->id;
 901		pix->colorspace = ctx->state.colorspace;
 902		pix->xfer_func = ctx->state.xfer_func;
 903		pix->ycbcr_enc = ctx->state.ycbcr_enc;
 904		pix->quantization = ctx->state.quantization;
 905		break;
 906	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
 907		if (!multiplanar)
 908			return -EINVAL;
 909		pix_mp = &f->fmt.pix_mp;
 910		pix_mp->pixelformat = ctx->is_enc ? V4L2_PIX_FMT_FWHT :
 911				      find_fmt(pix_mp->pixelformat)->id;
 912		pix_mp->colorspace = ctx->state.colorspace;
 913		pix_mp->xfer_func = ctx->state.xfer_func;
 914		pix_mp->ycbcr_enc = ctx->state.ycbcr_enc;
 915		pix_mp->quantization = ctx->state.quantization;
 916		break;
 917	default:
 918		return -EINVAL;
 919	}
 920
 921	return vidioc_try_fmt(ctx, f);
 922}
 923
 924static int vidioc_try_fmt_vid_out(struct file *file, void *priv,
 925				  struct v4l2_format *f)
 926{
 927	struct vicodec_ctx *ctx = file2ctx(file);
 928	struct v4l2_pix_format_mplane *pix_mp;
 929	struct v4l2_pix_format *pix;
 930
 931	switch (f->type) {
 932	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 933		if (multiplanar)
 934			return -EINVAL;
 935		pix = &f->fmt.pix;
 936		if (ctx->is_enc)
 937			pix->pixelformat = find_fmt(pix->pixelformat)->id;
 938		else if (ctx->is_stateless)
 939			pix->pixelformat = V4L2_PIX_FMT_FWHT_STATELESS;
 940		else
 941			pix->pixelformat = V4L2_PIX_FMT_FWHT;
 942		if (!pix->colorspace)
 943			pix->colorspace = V4L2_COLORSPACE_REC709;
 944		break;
 945	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
 946		if (!multiplanar)
 947			return -EINVAL;
 948		pix_mp = &f->fmt.pix_mp;
 949		if (ctx->is_enc)
 950			pix_mp->pixelformat = find_fmt(pix_mp->pixelformat)->id;
 951		else if (ctx->is_stateless)
 952			pix_mp->pixelformat = V4L2_PIX_FMT_FWHT_STATELESS;
 953		else
 954			pix_mp->pixelformat = V4L2_PIX_FMT_FWHT;
 955		if (!pix_mp->colorspace)
 956			pix_mp->colorspace = V4L2_COLORSPACE_REC709;
 957		break;
 958	default:
 959		return -EINVAL;
 960	}
 961
 962	return vidioc_try_fmt(ctx, f);
 963}
 964
 965static int vidioc_s_fmt(struct vicodec_ctx *ctx, struct v4l2_format *f)
 966{
 967	struct vicodec_q_data *q_data;
 968	struct vb2_queue *vq;
 969	bool fmt_changed = true;
 970	struct v4l2_pix_format_mplane *pix_mp;
 971	struct v4l2_pix_format *pix;
 972
 973	vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
 974
 975	q_data = get_q_data(ctx, f->type);
 976	if (!q_data)
 977		return -EINVAL;
 978
 979	switch (f->type) {
 980	case V4L2_BUF_TYPE_VIDEO_CAPTURE:
 981	case V4L2_BUF_TYPE_VIDEO_OUTPUT:
 982		pix = &f->fmt.pix;
 983		if (ctx->is_enc && V4L2_TYPE_IS_OUTPUT(f->type))
 984			fmt_changed =
 985				!q_data->info ||
 986				q_data->info->id != pix->pixelformat ||
 987				q_data->coded_width != pix->width ||
 988				q_data->coded_height != pix->height;
 989
 990		if (vb2_is_busy(vq) && fmt_changed)
 991			return -EBUSY;
 992
 993		if (pix->pixelformat == V4L2_PIX_FMT_FWHT)
 994			q_data->info = &pixfmt_fwht;
 995		else if (pix->pixelformat == V4L2_PIX_FMT_FWHT_STATELESS)
 996			q_data->info = &pixfmt_stateless_fwht;
 997		else
 998			q_data->info = find_fmt(pix->pixelformat);
 999		q_data->coded_width = pix->width;
1000		q_data->coded_height = pix->height;
1001		q_data->sizeimage = pix->sizeimage;
1002		break;
1003	case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
1004	case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1005		pix_mp = &f->fmt.pix_mp;
1006		if (ctx->is_enc && V4L2_TYPE_IS_OUTPUT(f->type))
1007			fmt_changed =
1008				!q_data->info ||
1009				q_data->info->id != pix_mp->pixelformat ||
1010				q_data->coded_width != pix_mp->width ||
1011				q_data->coded_height != pix_mp->height;
1012
1013		if (vb2_is_busy(vq) && fmt_changed)
1014			return -EBUSY;
1015
1016		if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT)
1017			q_data->info = &pixfmt_fwht;
1018		else if (pix_mp->pixelformat == V4L2_PIX_FMT_FWHT_STATELESS)
1019			q_data->info = &pixfmt_stateless_fwht;
1020		else
1021			q_data->info = find_fmt(pix_mp->pixelformat);
1022		q_data->coded_width = pix_mp->width;
1023		q_data->coded_height = pix_mp->height;
1024		q_data->sizeimage = pix_mp->plane_fmt[0].sizeimage;
1025		break;
1026	default:
1027		return -EINVAL;
1028	}
1029
1030	dprintk(ctx->dev,
1031		"Setting format for type %d, coded wxh: %dx%d, fourcc: 0x%08x\n",
1032		f->type, q_data->coded_width, q_data->coded_height,
1033		q_data->info->id);
1034
1035	return 0;
1036}
1037
1038static int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
1039				struct v4l2_format *f)
1040{
1041	int ret;
1042
1043	ret = vidioc_try_fmt_vid_cap(file, priv, f);
1044	if (ret)
1045		return ret;
1046
1047	return vidioc_s_fmt(file2ctx(file), f);
1048}
1049
1050static int vidioc_s_fmt_vid_out(struct file *file, void *priv,
1051				struct v4l2_format *f)
1052{
1053	struct vicodec_ctx *ctx = file2ctx(file);
1054	struct vicodec_q_data *q_data;
1055	struct vicodec_q_data *q_data_cap;
1056	struct v4l2_pix_format *pix;
1057	struct v4l2_pix_format_mplane *pix_mp;
1058	u32 coded_w = 0, coded_h = 0;
1059	unsigned int size = 0;
1060	int ret;
1061
1062	q_data = get_q_data(ctx, f->type);
1063	q_data_cap = get_q_data(ctx, V4L2_BUF_TYPE_VIDEO_CAPTURE);
1064
1065	ret = vidioc_try_fmt_vid_out(file, priv, f);
1066	if (ret)
1067		return ret;
1068
1069	if (ctx->is_enc) {
1070		struct vb2_queue *vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
1071		struct vb2_queue *vq_cap = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1072							   V4L2_BUF_TYPE_VIDEO_CAPTURE);
1073		const struct v4l2_fwht_pixfmt_info *info = ctx->is_stateless ?
1074			&pixfmt_stateless_fwht : &pixfmt_fwht;
1075
1076		if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1077			coded_w = f->fmt.pix.width;
1078			coded_h = f->fmt.pix.height;
1079		} else {
1080			coded_w = f->fmt.pix_mp.width;
1081			coded_h = f->fmt.pix_mp.height;
1082		}
1083		if (vb2_is_busy(vq) && (coded_w != q_data->coded_width ||
1084					coded_h != q_data->coded_height))
1085			return -EBUSY;
1086		size = coded_w * coded_h *
1087			info->sizeimage_mult / info->sizeimage_div;
1088		if (!ctx->is_stateless)
1089			size += sizeof(struct fwht_cframe_hdr);
1090
1091		if (vb2_is_busy(vq_cap) && size > q_data_cap->sizeimage)
1092			return -EBUSY;
1093	}
1094
1095	ret = vidioc_s_fmt(file2ctx(file), f);
1096	if (!ret) {
1097		if (ctx->is_enc) {
1098			q_data->visible_width = coded_w;
1099			q_data->visible_height = coded_h;
1100			q_data_cap->coded_width = coded_w;
1101			q_data_cap->coded_height = coded_h;
1102			q_data_cap->sizeimage = size;
1103		}
1104
1105		switch (f->type) {
1106		case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1107			pix = &f->fmt.pix;
1108			ctx->state.colorspace = pix->colorspace;
1109			ctx->state.xfer_func = pix->xfer_func;
1110			ctx->state.ycbcr_enc = pix->ycbcr_enc;
1111			ctx->state.quantization = pix->quantization;
1112			break;
1113		case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
1114			pix_mp = &f->fmt.pix_mp;
1115			ctx->state.colorspace = pix_mp->colorspace;
1116			ctx->state.xfer_func = pix_mp->xfer_func;
1117			ctx->state.ycbcr_enc = pix_mp->ycbcr_enc;
1118			ctx->state.quantization = pix_mp->quantization;
1119			break;
1120		default:
1121			break;
1122		}
1123	}
1124	return ret;
1125}
1126
1127static int vidioc_g_selection(struct file *file, void *priv,
1128			      struct v4l2_selection *s)
1129{
1130	struct vicodec_ctx *ctx = file2ctx(file);
1131	struct vicodec_q_data *q_data;
1132
1133	q_data = get_q_data(ctx, s->type);
1134	if (!q_data)
1135		return -EINVAL;
1136	/*
1137	 * encoder supports only cropping on the OUTPUT buffer
1138	 * decoder supports only composing on the CAPTURE buffer
1139	 */
1140	if (ctx->is_enc && s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1141		switch (s->target) {
1142		case V4L2_SEL_TGT_CROP:
1143			s->r.left = 0;
1144			s->r.top = 0;
1145			s->r.width = q_data->visible_width;
1146			s->r.height = q_data->visible_height;
1147			return 0;
1148		case V4L2_SEL_TGT_CROP_DEFAULT:
1149		case V4L2_SEL_TGT_CROP_BOUNDS:
1150			s->r.left = 0;
1151			s->r.top = 0;
1152			s->r.width = q_data->coded_width;
1153			s->r.height = q_data->coded_height;
1154			return 0;
1155		}
1156	} else if (!ctx->is_enc && s->type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1157		switch (s->target) {
1158		case V4L2_SEL_TGT_COMPOSE:
1159			s->r.left = 0;
1160			s->r.top = 0;
1161			s->r.width = q_data->visible_width;
1162			s->r.height = q_data->visible_height;
1163			return 0;
1164		case V4L2_SEL_TGT_COMPOSE_DEFAULT:
1165		case V4L2_SEL_TGT_COMPOSE_BOUNDS:
1166			s->r.left = 0;
1167			s->r.top = 0;
1168			s->r.width = q_data->coded_width;
1169			s->r.height = q_data->coded_height;
1170			return 0;
1171		}
1172	}
1173	return -EINVAL;
1174}
1175
1176static int vidioc_s_selection(struct file *file, void *priv,
1177			      struct v4l2_selection *s)
1178{
1179	struct vicodec_ctx *ctx = file2ctx(file);
1180	struct vicodec_q_data *q_data;
1181
1182	if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
1183		return -EINVAL;
1184
1185	q_data = get_q_data(ctx, s->type);
1186	if (!q_data)
1187		return -EINVAL;
1188
1189	if (!ctx->is_enc || s->target != V4L2_SEL_TGT_CROP)
1190		return -EINVAL;
1191
1192	s->r.left = 0;
1193	s->r.top = 0;
1194	q_data->visible_width = clamp(s->r.width, MIN_WIDTH,
1195				      q_data->coded_width);
1196	s->r.width = q_data->visible_width;
1197	q_data->visible_height = clamp(s->r.height, MIN_HEIGHT,
1198				       q_data->coded_height);
1199	s->r.height = q_data->visible_height;
1200	return 0;
1201}
1202
1203static int vicodec_encoder_cmd(struct file *file, void *priv,
1204			    struct v4l2_encoder_cmd *ec)
1205{
1206	struct vicodec_ctx *ctx = file2ctx(file);
1207	int ret;
1208
1209	ret = v4l2_m2m_ioctl_try_encoder_cmd(file, priv, ec);
1210	if (ret < 0)
1211		return ret;
1212
1213	if (!vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))
1214		return 0;
1215
1216	ret = v4l2_m2m_ioctl_encoder_cmd(file, priv, ec);
1217	if (ret < 0)
1218		return ret;
1219
1220	if (ec->cmd == V4L2_ENC_CMD_STOP &&
1221	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
1222		v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event);
1223
1224	if (ec->cmd == V4L2_ENC_CMD_START &&
1225	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
1226		vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
1227
1228	return 0;
1229}
1230
1231static int vicodec_decoder_cmd(struct file *file, void *priv,
1232			    struct v4l2_decoder_cmd *dc)
1233{
1234	struct vicodec_ctx *ctx = file2ctx(file);
1235	int ret;
1236
1237	/*
1238	 * This ioctl should not be used with a stateless codec that doesn't
1239	 * support holding buffers and the associated flush command.
1240	 */
1241	WARN_ON(ctx->is_stateless);
1242
1243	ret = v4l2_m2m_ioctl_try_decoder_cmd(file, priv, dc);
1244	if (ret < 0)
1245		return ret;
1246
1247	if (!vb2_is_streaming(&ctx->fh.m2m_ctx->out_q_ctx.q))
1248		return 0;
1249
1250	ret = v4l2_m2m_ioctl_decoder_cmd(file, priv, dc);
1251	if (ret < 0)
1252		return ret;
1253
1254	if (dc->cmd == V4L2_DEC_CMD_STOP &&
1255	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
1256		v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event);
1257
1258	if (dc->cmd == V4L2_DEC_CMD_START &&
1259	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
1260		vb2_clear_last_buffer_dequeued(&ctx->fh.m2m_ctx->cap_q_ctx.q);
1261
1262	return 0;
1263}
1264
1265static int vicodec_enum_framesizes(struct file *file, void *priv,
1266				   struct v4l2_frmsizeenum *fsize)
1267{
1268	switch (fsize->pixel_format) {
1269	case V4L2_PIX_FMT_FWHT_STATELESS:
1270		break;
1271	case V4L2_PIX_FMT_FWHT:
1272		break;
1273	default:
1274		if (find_fmt(fsize->pixel_format)->id == fsize->pixel_format)
1275			break;
1276		return -EINVAL;
1277	}
1278
1279	if (fsize->index)
1280		return -EINVAL;
1281
1282	fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1283
1284	fsize->stepwise.min_width = MIN_WIDTH;
1285	fsize->stepwise.max_width = MAX_WIDTH;
1286	fsize->stepwise.step_width = 8;
1287	fsize->stepwise.min_height = MIN_HEIGHT;
1288	fsize->stepwise.max_height = MAX_HEIGHT;
1289	fsize->stepwise.step_height = 8;
1290
1291	return 0;
1292}
1293
1294static int vicodec_subscribe_event(struct v4l2_fh *fh,
1295				const struct v4l2_event_subscription *sub)
1296{
1297	struct vicodec_ctx *ctx = container_of(fh, struct vicodec_ctx, fh);
1298
1299	switch (sub->type) {
1300	case V4L2_EVENT_SOURCE_CHANGE:
1301		if (ctx->is_enc)
1302			return -EINVAL;
1303		fallthrough;
1304	case V4L2_EVENT_EOS:
1305		if (ctx->is_stateless)
1306			return -EINVAL;
1307		return v4l2_event_subscribe(fh, sub, 0, NULL);
1308	default:
1309		return v4l2_ctrl_subscribe_event(fh, sub);
1310	}
1311}
1312
1313static const struct v4l2_ioctl_ops vicodec_ioctl_ops = {
1314	.vidioc_querycap	= vidioc_querycap,
1315
1316	.vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
1317	.vidioc_g_fmt_vid_cap	= vidioc_g_fmt_vid_cap,
1318	.vidioc_try_fmt_vid_cap	= vidioc_try_fmt_vid_cap,
1319	.vidioc_s_fmt_vid_cap	= vidioc_s_fmt_vid_cap,
1320
1321	.vidioc_g_fmt_vid_cap_mplane	= vidioc_g_fmt_vid_cap,
1322	.vidioc_try_fmt_vid_cap_mplane	= vidioc_try_fmt_vid_cap,
1323	.vidioc_s_fmt_vid_cap_mplane	= vidioc_s_fmt_vid_cap,
1324
1325	.vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
1326	.vidioc_g_fmt_vid_out	= vidioc_g_fmt_vid_out,
1327	.vidioc_try_fmt_vid_out	= vidioc_try_fmt_vid_out,
1328	.vidioc_s_fmt_vid_out	= vidioc_s_fmt_vid_out,
1329
1330	.vidioc_g_fmt_vid_out_mplane	= vidioc_g_fmt_vid_out,
1331	.vidioc_try_fmt_vid_out_mplane	= vidioc_try_fmt_vid_out,
1332	.vidioc_s_fmt_vid_out_mplane	= vidioc_s_fmt_vid_out,
1333
1334	.vidioc_reqbufs		= v4l2_m2m_ioctl_reqbufs,
1335	.vidioc_querybuf	= v4l2_m2m_ioctl_querybuf,
1336	.vidioc_qbuf		= v4l2_m2m_ioctl_qbuf,
1337	.vidioc_dqbuf		= v4l2_m2m_ioctl_dqbuf,
1338	.vidioc_prepare_buf	= v4l2_m2m_ioctl_prepare_buf,
1339	.vidioc_create_bufs	= v4l2_m2m_ioctl_create_bufs,
1340	.vidioc_expbuf		= v4l2_m2m_ioctl_expbuf,
1341	.vidioc_remove_bufs	= v4l2_m2m_ioctl_remove_bufs,
1342
1343	.vidioc_streamon	= v4l2_m2m_ioctl_streamon,
1344	.vidioc_streamoff	= v4l2_m2m_ioctl_streamoff,
1345
1346	.vidioc_g_selection	= vidioc_g_selection,
1347	.vidioc_s_selection	= vidioc_s_selection,
1348
1349	.vidioc_try_encoder_cmd	= v4l2_m2m_ioctl_try_encoder_cmd,
1350	.vidioc_encoder_cmd	= vicodec_encoder_cmd,
1351	.vidioc_try_decoder_cmd	= v4l2_m2m_ioctl_try_decoder_cmd,
1352	.vidioc_decoder_cmd	= vicodec_decoder_cmd,
1353	.vidioc_enum_framesizes = vicodec_enum_framesizes,
1354
1355	.vidioc_subscribe_event = vicodec_subscribe_event,
1356	.vidioc_unsubscribe_event = v4l2_event_unsubscribe,
1357};
1358
1359
1360/*
1361 * Queue operations
1362 */
1363
1364static int vicodec_queue_setup(struct vb2_queue *vq, unsigned int *nbuffers,
1365			       unsigned int *nplanes, unsigned int sizes[],
1366			       struct device *alloc_devs[])
1367{
1368	struct vicodec_ctx *ctx = vb2_get_drv_priv(vq);
1369	struct vicodec_q_data *q_data = get_q_data(ctx, vq->type);
1370	unsigned int size = q_data->sizeimage;
1371
1372	if (*nplanes)
1373		return sizes[0] < size ? -EINVAL : 0;
1374
1375	*nplanes = 1;
1376	sizes[0] = size;
1377	q_data->vb2_sizeimage = size;
1378	return 0;
1379}
1380
1381static int vicodec_buf_out_validate(struct vb2_buffer *vb)
1382{
1383	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1384
1385	vbuf->field = V4L2_FIELD_NONE;
1386	return 0;
1387}
1388
1389static int vicodec_buf_prepare(struct vb2_buffer *vb)
1390{
1391	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1392	struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1393	struct vicodec_q_data *q_data;
1394
1395	dprintk(ctx->dev, "type: %d\n", vb->vb2_queue->type);
1396
1397	q_data = get_q_data(ctx, vb->vb2_queue->type);
1398	if (V4L2_TYPE_IS_OUTPUT(vb->vb2_queue->type)) {
1399		if (vbuf->field == V4L2_FIELD_ANY)
1400			vbuf->field = V4L2_FIELD_NONE;
1401		if (vbuf->field != V4L2_FIELD_NONE) {
1402			dprintk(ctx->dev, "%s field isn't supported\n",
1403					__func__);
1404			return -EINVAL;
1405		}
1406	}
1407
1408	if (vb2_plane_size(vb, 0) < q_data->vb2_sizeimage) {
1409		dprintk(ctx->dev,
1410			"%s data will not fit into plane (%lu < %lu)\n",
1411			__func__, vb2_plane_size(vb, 0),
1412			(long)q_data->vb2_sizeimage);
1413		return -EINVAL;
1414	}
1415
1416	return 0;
1417}
1418
1419static void vicodec_buf_queue(struct vb2_buffer *vb)
1420{
1421	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1422	struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1423	unsigned int sz = vb2_get_plane_payload(&vbuf->vb2_buf, 0);
1424	u8 *p_src = vb2_plane_vaddr(&vbuf->vb2_buf, 0);
1425	u8 *p = p_src;
1426	struct vb2_queue *vq_out = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1427						   V4L2_BUF_TYPE_VIDEO_OUTPUT);
1428	struct vb2_queue *vq_cap = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
1429						   V4L2_BUF_TYPE_VIDEO_CAPTURE);
1430	bool header_valid = false;
1431	static const struct v4l2_event rs_event = {
1432		.type = V4L2_EVENT_SOURCE_CHANGE,
1433		.u.src_change.changes = V4L2_EVENT_SRC_CH_RESOLUTION,
1434	};
1435
1436	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type) &&
1437	    vb2_is_streaming(vb->vb2_queue) &&
1438	    v4l2_m2m_dst_buf_is_last(ctx->fh.m2m_ctx)) {
1439		unsigned int i;
1440
1441		for (i = 0; i < vb->num_planes; i++)
1442			vb2_set_plane_payload(vb, i, 0);
1443
1444		vbuf->field = V4L2_FIELD_NONE;
1445		vbuf->sequence =
1446			get_q_data(ctx, vb->vb2_queue->type)->sequence++;
1447
1448		v4l2_m2m_last_buffer_done(ctx->fh.m2m_ctx, vbuf);
1449		v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event);
1450		return;
1451	}
1452
1453	/* buf_queue handles only the first source change event */
1454	if (ctx->first_source_change_sent) {
1455		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1456		return;
1457	}
1458
1459	/*
1460	 * if both queues are streaming, the source change event is
1461	 * handled in job_ready
1462	 */
1463	if (vb2_is_streaming(vq_cap) && vb2_is_streaming(vq_out)) {
1464		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1465		return;
1466	}
1467
1468	/*
1469	 * source change event is relevant only for the stateful decoder
1470	 * in the compressed stream
1471	 */
1472	if (ctx->is_stateless || ctx->is_enc ||
1473	    V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) {
1474		v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1475		return;
1476	}
1477
1478	do {
1479		enum vb2_buffer_state state =
1480			get_next_header(ctx, &p, p_src + sz - p);
1481
1482		if (ctx->header_size < sizeof(struct fwht_cframe_hdr)) {
1483			v4l2_m2m_buf_done(vbuf, state);
1484			return;
1485		}
1486		header_valid = is_header_valid(&ctx->state.header);
1487		/*
1488		 * p points right after the end of the header in the
1489		 * buffer. If the header is invalid we set p to point
1490		 * to the next byte after the start of the header
1491		 */
1492		if (!header_valid) {
1493			p = p - sizeof(struct fwht_cframe_hdr) + 1;
1494			if (p < p_src)
1495				p = p_src;
1496			ctx->header_size = 0;
1497			ctx->comp_magic_cnt = 0;
1498		}
1499
1500	} while (!header_valid);
1501
1502	ctx->cur_buf_offset = p - p_src;
1503	update_capture_data_from_header(ctx);
1504	ctx->first_source_change_sent = true;
1505	v4l2_event_queue_fh(&ctx->fh, &rs_event);
1506	v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
1507}
1508
1509static void vicodec_return_bufs(struct vb2_queue *q, u32 state)
1510{
1511	struct vicodec_ctx *ctx = vb2_get_drv_priv(q);
1512	struct vb2_v4l2_buffer *vbuf;
1513
1514	for (;;) {
1515		if (V4L2_TYPE_IS_OUTPUT(q->type))
1516			vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
1517		else
1518			vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
1519		if (vbuf == NULL)
1520			return;
1521		v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
1522					   &ctx->hdl);
1523		spin_lock(ctx->lock);
1524		v4l2_m2m_buf_done(vbuf, state);
1525		spin_unlock(ctx->lock);
1526	}
1527}
1528
1529static unsigned int total_frame_size(struct vicodec_q_data *q_data)
1530{
1531	unsigned int size;
1532	unsigned int chroma_div;
1533
1534	if (!q_data->info) {
1535		WARN_ON(1);
1536		return 0;
1537	}
1538	size = q_data->coded_width * q_data->coded_height;
1539	chroma_div = q_data->info->width_div * q_data->info->height_div;
1540
1541	if (q_data->info->components_num == 4)
1542		return 2 * size + 2 * (size / chroma_div);
1543	else if (q_data->info->components_num == 3)
1544		return size + 2 * (size / chroma_div);
1545	return size;
1546}
1547
1548static int vicodec_start_streaming(struct vb2_queue *q,
1549				   unsigned int count)
1550{
1551	struct vicodec_ctx *ctx = vb2_get_drv_priv(q);
1552	struct vicodec_q_data *q_data = get_q_data(ctx, q->type);
1553	struct v4l2_fwht_state *state = &ctx->state;
1554	const struct v4l2_fwht_pixfmt_info *info = q_data->info;
1555	unsigned int size = q_data->coded_width * q_data->coded_height;
1556	unsigned int chroma_div;
1557	unsigned int total_planes_size;
1558	u8 *new_comp_frame = NULL;
1559
1560	chroma_div = info->width_div * info->height_div;
1561	q_data->sequence = 0;
1562
1563	v4l2_m2m_update_start_streaming_state(ctx->fh.m2m_ctx, q);
1564
1565	state->gop_cnt = 0;
1566
1567	if ((V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) ||
1568	    (V4L2_TYPE_IS_CAPTURE(q->type) && ctx->is_enc))
1569		return 0;
1570
1571	if (info->id == V4L2_PIX_FMT_FWHT ||
1572	    info->id == V4L2_PIX_FMT_FWHT_STATELESS) {
1573		vicodec_return_bufs(q, VB2_BUF_STATE_QUEUED);
1574		return -EINVAL;
1575	}
1576	total_planes_size = total_frame_size(q_data);
1577	ctx->comp_max_size = total_planes_size;
1578
1579	state->visible_width = q_data->visible_width;
1580	state->visible_height = q_data->visible_height;
1581	state->coded_width = q_data->coded_width;
1582	state->coded_height = q_data->coded_height;
1583	state->stride = q_data->coded_width *
1584				info->bytesperline_mult;
1585
1586	if (ctx->is_stateless) {
1587		state->ref_stride = state->stride;
1588		return 0;
1589	}
1590	state->ref_stride = q_data->coded_width * info->luma_alpha_step;
1591
1592	state->ref_frame.buf = kvmalloc(total_planes_size, GFP_KERNEL);
1593	state->ref_frame.luma = state->ref_frame.buf;
1594	new_comp_frame = kvmalloc(ctx->comp_max_size, GFP_KERNEL);
1595
1596	if (!state->ref_frame.luma || !new_comp_frame) {
1597		kvfree(state->ref_frame.luma);
1598		kvfree(new_comp_frame);
1599		vicodec_return_bufs(q, VB2_BUF_STATE_QUEUED);
1600		return -ENOMEM;
1601	}
1602	/*
1603	 * if state->compressed_frame was already allocated then
1604	 * it contain data of the first frame of the new resolution
1605	 */
1606	if (state->compressed_frame) {
1607		if (ctx->comp_size > ctx->comp_max_size)
1608			ctx->comp_size = ctx->comp_max_size;
1609
1610		memcpy(new_comp_frame,
1611		       state->compressed_frame, ctx->comp_size);
1612	}
1613
1614	kvfree(state->compressed_frame);
1615	state->compressed_frame = new_comp_frame;
1616
1617	if (info->components_num < 3) {
1618		state->ref_frame.cb = NULL;
1619		state->ref_frame.cr = NULL;
1620		state->ref_frame.alpha = NULL;
1621		return 0;
1622	}
1623
1624	state->ref_frame.cb = state->ref_frame.luma + size;
1625	state->ref_frame.cr = state->ref_frame.cb + size / chroma_div;
1626
1627	if (info->components_num == 4)
1628		state->ref_frame.alpha =
1629			state->ref_frame.cr + size / chroma_div;
1630	else
1631		state->ref_frame.alpha = NULL;
1632
1633	return 0;
1634}
1635
1636static void vicodec_stop_streaming(struct vb2_queue *q)
1637{
1638	struct vicodec_ctx *ctx = vb2_get_drv_priv(q);
1639
1640	vicodec_return_bufs(q, VB2_BUF_STATE_ERROR);
1641
1642	v4l2_m2m_update_stop_streaming_state(ctx->fh.m2m_ctx, q);
1643
1644	if (V4L2_TYPE_IS_OUTPUT(q->type) &&
1645	    v4l2_m2m_has_stopped(ctx->fh.m2m_ctx))
1646		v4l2_event_queue_fh(&ctx->fh, &vicodec_eos_event);
1647
1648	if (!ctx->is_enc && V4L2_TYPE_IS_OUTPUT(q->type))
1649		ctx->first_source_change_sent = false;
1650
1651	if ((!V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) ||
1652	    (V4L2_TYPE_IS_OUTPUT(q->type) && ctx->is_enc)) {
1653		if (!ctx->is_stateless)
1654			kvfree(ctx->state.ref_frame.buf);
1655		ctx->state.ref_frame.buf = NULL;
1656		ctx->state.ref_frame.luma = NULL;
1657		ctx->comp_max_size = 0;
1658		ctx->source_changed = false;
1659	}
1660	if (V4L2_TYPE_IS_OUTPUT(q->type) && !ctx->is_enc) {
1661		ctx->cur_buf_offset = 0;
1662		ctx->comp_size = 0;
1663		ctx->header_size = 0;
1664		ctx->comp_magic_cnt = 0;
1665		ctx->comp_has_frame = false;
1666		ctx->comp_has_next_frame = false;
1667	}
1668}
1669
1670static void vicodec_buf_request_complete(struct vb2_buffer *vb)
1671{
1672	struct vicodec_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
1673
1674	v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->hdl);
1675}
1676
1677
1678static const struct vb2_ops vicodec_qops = {
1679	.queue_setup		= vicodec_queue_setup,
1680	.buf_out_validate	= vicodec_buf_out_validate,
1681	.buf_prepare		= vicodec_buf_prepare,
1682	.buf_queue		= vicodec_buf_queue,
1683	.buf_request_complete	= vicodec_buf_request_complete,
1684	.start_streaming	= vicodec_start_streaming,
1685	.stop_streaming		= vicodec_stop_streaming,
1686};
1687
1688static int queue_init(void *priv, struct vb2_queue *src_vq,
1689		      struct vb2_queue *dst_vq)
1690{
1691	struct vicodec_ctx *ctx = priv;
1692	int ret;
1693
1694	src_vq->type = (multiplanar ?
1695			V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1696			V4L2_BUF_TYPE_VIDEO_OUTPUT);
1697	src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1698	src_vq->drv_priv = ctx;
1699	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1700	src_vq->ops = &vicodec_qops;
1701	src_vq->mem_ops = &vb2_vmalloc_memops;
1702	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1703	if (ctx->is_enc) {
1704		src_vq->lock = &ctx->dev->stateful_enc.mutex;
1705		src_vq->min_reqbufs_allocation = VICODEC_REC_BUFS;
1706	} else if (ctx->is_stateless) {
1707		src_vq->lock = &ctx->dev->stateless_dec.mutex;
1708	} else {
1709		src_vq->lock = &ctx->dev->stateful_dec.mutex;
1710	}
1711	src_vq->supports_requests = ctx->is_stateless;
1712	src_vq->requires_requests = ctx->is_stateless;
1713	ret = vb2_queue_init(src_vq);
1714	if (ret)
1715		return ret;
1716
1717	dst_vq->type = (multiplanar ?
1718			V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1719			V4L2_BUF_TYPE_VIDEO_CAPTURE);
1720	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
1721	dst_vq->max_num_buffers = 64;
1722	dst_vq->drv_priv = ctx;
1723	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
1724	dst_vq->ops = &vicodec_qops;
1725	dst_vq->mem_ops = &vb2_vmalloc_memops;
1726	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
1727	dst_vq->lock = src_vq->lock;
1728	if (!ctx->is_stateless && !ctx->is_enc)
1729		dst_vq->min_reqbufs_allocation = VICODEC_REC_BUFS;
1730
1731	return vb2_queue_init(dst_vq);
1732}
1733
1734static int vicodec_try_ctrl(struct v4l2_ctrl *ctrl)
1735{
1736	struct vicodec_ctx *ctx = container_of(ctrl->handler,
1737			struct vicodec_ctx, hdl);
1738	const struct v4l2_ctrl_fwht_params *params;
1739	struct vicodec_q_data *q_dst = get_q_data(ctx,
1740			V4L2_BUF_TYPE_VIDEO_CAPTURE);
1741
1742	switch (ctrl->id) {
1743	case V4L2_CID_STATELESS_FWHT_PARAMS:
1744		if (!q_dst->info)
1745			return -EINVAL;
1746		params = ctrl->p_new.p_fwht_params;
1747		if (params->width > q_dst->coded_width ||
1748		    params->width < MIN_WIDTH ||
1749		    params->height > q_dst->coded_height ||
1750		    params->height < MIN_HEIGHT)
1751			return -EINVAL;
1752		if (!validate_by_version(params->flags, params->version))
1753			return -EINVAL;
1754		if (!validate_stateless_params_flags(params, q_dst->info))
1755			return -EINVAL;
1756		return 0;
1757	default:
1758		return 0;
1759	}
1760	return 0;
1761}
1762
1763static void update_header_from_stateless_params(struct vicodec_ctx *ctx,
1764						const struct v4l2_ctrl_fwht_params *params)
1765{
1766	struct fwht_cframe_hdr *p_hdr = &ctx->state.header;
1767
1768	p_hdr->magic1 = FWHT_MAGIC1;
1769	p_hdr->magic2 = FWHT_MAGIC2;
1770	p_hdr->version = htonl(params->version);
1771	p_hdr->width = htonl(params->width);
1772	p_hdr->height = htonl(params->height);
1773	p_hdr->flags = htonl(params->flags);
1774	p_hdr->colorspace = htonl(params->colorspace);
1775	p_hdr->xfer_func = htonl(params->xfer_func);
1776	p_hdr->ycbcr_enc = htonl(params->ycbcr_enc);
1777	p_hdr->quantization = htonl(params->quantization);
1778}
1779
1780static int vicodec_s_ctrl(struct v4l2_ctrl *ctrl)
1781{
1782	struct vicodec_ctx *ctx = container_of(ctrl->handler,
1783					       struct vicodec_ctx, hdl);
1784	const struct v4l2_ctrl_fwht_params *params;
1785
1786	switch (ctrl->id) {
1787	case V4L2_CID_MPEG_VIDEO_GOP_SIZE:
1788		ctx->state.gop_size = ctrl->val;
1789		return 0;
1790	case V4L2_CID_FWHT_I_FRAME_QP:
1791		ctx->state.i_frame_qp = ctrl->val;
1792		return 0;
1793	case V4L2_CID_FWHT_P_FRAME_QP:
1794		ctx->state.p_frame_qp = ctrl->val;
1795		return 0;
1796	case V4L2_CID_STATELESS_FWHT_PARAMS:
1797		params = ctrl->p_new.p_fwht_params;
1798		update_header_from_stateless_params(ctx, params);
1799		ctx->state.ref_frame_ts = params->backward_ref_ts;
1800		return 0;
1801	}
1802	return -EINVAL;
1803}
1804
1805static const struct v4l2_ctrl_ops vicodec_ctrl_ops = {
1806	.s_ctrl = vicodec_s_ctrl,
1807	.try_ctrl = vicodec_try_ctrl,
1808};
1809
1810static const struct v4l2_ctrl_config vicodec_ctrl_stateless_state = {
1811	.ops		= &vicodec_ctrl_ops,
1812	.id		= V4L2_CID_STATELESS_FWHT_PARAMS,
1813	.elem_size      = sizeof(struct v4l2_ctrl_fwht_params),
1814};
1815
1816/*
1817 * File operations
1818 */
1819static int vicodec_open(struct file *file)
1820{
1821	const struct v4l2_fwht_pixfmt_info *info = v4l2_fwht_get_pixfmt(0);
1822	struct video_device *vfd = video_devdata(file);
1823	struct vicodec_dev *dev = video_drvdata(file);
1824	struct vicodec_ctx *ctx = NULL;
1825	struct v4l2_ctrl_handler *hdl;
1826	unsigned int raw_size;
1827	unsigned int comp_size;
1828	int rc = 0;
1829
1830	if (mutex_lock_interruptible(vfd->lock))
1831		return -ERESTARTSYS;
1832	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
1833	if (!ctx) {
1834		rc = -ENOMEM;
1835		goto open_unlock;
1836	}
1837
1838	if (vfd == &dev->stateful_enc.vfd)
1839		ctx->is_enc = true;
1840	else if (vfd == &dev->stateless_dec.vfd)
1841		ctx->is_stateless = true;
1842
1843	v4l2_fh_init(&ctx->fh, video_devdata(file));
1844	ctx->dev = dev;
1845	hdl = &ctx->hdl;
1846	v4l2_ctrl_handler_init(hdl, 5);
1847	v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
1848			  1, 16, 1, 10);
1849	v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_FWHT_I_FRAME_QP,
1850			  1, 31, 1, 20);
1851	v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, V4L2_CID_FWHT_P_FRAME_QP,
1852			  1, 31, 1, 20);
1853
1854	if (ctx->is_stateless)
1855		v4l2_ctrl_new_custom(hdl, &vicodec_ctrl_stateless_state, NULL);
1856	else
1857		v4l2_ctrl_new_std(hdl, &vicodec_ctrl_ops, ctx->is_enc ?
1858				  V4L2_CID_MIN_BUFFERS_FOR_OUTPUT :
1859				  V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
1860				  VICODEC_REC_BUFS, VICODEC_REC_BUFS, 1,
1861				  VICODEC_REC_BUFS);
1862
1863	if (hdl->error) {
1864		rc = hdl->error;
1865		v4l2_ctrl_handler_free(hdl);
1866		kfree(ctx);
1867		goto open_unlock;
1868	}
1869	ctx->fh.ctrl_handler = hdl;
1870	v4l2_ctrl_handler_setup(hdl);
1871
1872	if (ctx->is_enc)
1873		ctx->q_data[V4L2_M2M_SRC].info = info;
1874	else if (ctx->is_stateless)
1875		ctx->q_data[V4L2_M2M_SRC].info = &pixfmt_stateless_fwht;
1876	else
1877		ctx->q_data[V4L2_M2M_SRC].info = &pixfmt_fwht;
1878	ctx->q_data[V4L2_M2M_SRC].coded_width = 1280;
1879	ctx->q_data[V4L2_M2M_SRC].coded_height = 720;
1880	ctx->q_data[V4L2_M2M_SRC].visible_width = 1280;
1881	ctx->q_data[V4L2_M2M_SRC].visible_height = 720;
1882	raw_size = 1280 * 720 * info->sizeimage_mult / info->sizeimage_div;
1883	comp_size = 1280 * 720 * pixfmt_fwht.sizeimage_mult /
1884				 pixfmt_fwht.sizeimage_div;
1885	if (ctx->is_enc)
1886		ctx->q_data[V4L2_M2M_SRC].sizeimage = raw_size;
1887	else if (ctx->is_stateless)
1888		ctx->q_data[V4L2_M2M_SRC].sizeimage = comp_size;
1889	else
1890		ctx->q_data[V4L2_M2M_SRC].sizeimage =
1891			comp_size + sizeof(struct fwht_cframe_hdr);
1892	ctx->q_data[V4L2_M2M_DST] = ctx->q_data[V4L2_M2M_SRC];
1893	if (ctx->is_enc) {
1894		ctx->q_data[V4L2_M2M_DST].info = &pixfmt_fwht;
1895		ctx->q_data[V4L2_M2M_DST].sizeimage =
1896			comp_size + sizeof(struct fwht_cframe_hdr);
1897	} else {
1898		ctx->q_data[V4L2_M2M_DST].info = info;
1899		ctx->q_data[V4L2_M2M_DST].sizeimage = raw_size;
1900	}
1901
1902	ctx->state.colorspace = V4L2_COLORSPACE_REC709;
1903
1904	if (ctx->is_enc) {
1905		ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_enc.m2m_dev,
1906						    ctx, &queue_init);
1907		ctx->lock = &dev->stateful_enc.lock;
1908	} else if (ctx->is_stateless) {
1909		ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateless_dec.m2m_dev,
1910						    ctx, &queue_init);
1911		ctx->lock = &dev->stateless_dec.lock;
1912	} else {
1913		ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(dev->stateful_dec.m2m_dev,
1914						    ctx, &queue_init);
1915		ctx->lock = &dev->stateful_dec.lock;
1916	}
1917
1918	if (IS_ERR(ctx->fh.m2m_ctx)) {
1919		rc = PTR_ERR(ctx->fh.m2m_ctx);
1920
1921		v4l2_ctrl_handler_free(hdl);
1922		v4l2_fh_exit(&ctx->fh);
1923		kfree(ctx);
1924		goto open_unlock;
1925	}
1926
1927	v4l2_fh_add(&ctx->fh, file);
1928
1929open_unlock:
1930	mutex_unlock(vfd->lock);
1931	return rc;
1932}
1933
1934static int vicodec_release(struct file *file)
1935{
1936	struct video_device *vfd = video_devdata(file);
1937	struct vicodec_ctx *ctx = file2ctx(file);
1938
1939	mutex_lock(vfd->lock);
1940	v4l2_m2m_ctx_release(ctx->fh.m2m_ctx);
1941	mutex_unlock(vfd->lock);
1942	v4l2_fh_del(&ctx->fh, file);
1943	v4l2_fh_exit(&ctx->fh);
1944	v4l2_ctrl_handler_free(&ctx->hdl);
1945	kvfree(ctx->state.compressed_frame);
1946	kfree(ctx);
1947
1948	return 0;
1949}
1950
1951static int vicodec_request_validate(struct media_request *req)
1952{
1953	struct media_request_object *obj;
1954	struct v4l2_ctrl_handler *parent_hdl, *hdl;
1955	struct vicodec_ctx *ctx = NULL;
1956	struct v4l2_ctrl *ctrl;
1957	unsigned int count;
1958
1959	list_for_each_entry(obj, &req->objects, list) {
1960		struct vb2_buffer *vb;
1961
1962		if (vb2_request_object_is_buffer(obj)) {
1963			vb = container_of(obj, struct vb2_buffer, req_obj);
1964			ctx = vb2_get_drv_priv(vb->vb2_queue);
1965
1966			break;
1967		}
1968	}
1969
1970	if (!ctx) {
1971		pr_err("No buffer was provided with the request\n");
1972		return -ENOENT;
1973	}
1974
1975	count = vb2_request_buffer_cnt(req);
1976	if (!count) {
1977		v4l2_info(&ctx->dev->v4l2_dev,
1978			  "No buffer was provided with the request\n");
1979		return -ENOENT;
1980	} else if (count > 1) {
1981		v4l2_info(&ctx->dev->v4l2_dev,
1982			  "More than one buffer was provided with the request\n");
1983		return -EINVAL;
1984	}
1985
1986	parent_hdl = &ctx->hdl;
1987
1988	hdl = v4l2_ctrl_request_hdl_find(req, parent_hdl);
1989	if (!hdl) {
1990		v4l2_info(&ctx->dev->v4l2_dev, "Missing codec control\n");
1991		return -ENOENT;
1992	}
1993	ctrl = v4l2_ctrl_request_hdl_ctrl_find(hdl,
1994					       vicodec_ctrl_stateless_state.id);
1995	v4l2_ctrl_request_hdl_put(hdl);
1996	if (!ctrl) {
1997		v4l2_info(&ctx->dev->v4l2_dev,
1998			  "Missing required codec control\n");
1999		return -ENOENT;
2000	}
2001
2002	return vb2_request_validate(req);
2003}
2004
2005static const struct v4l2_file_operations vicodec_fops = {
2006	.owner		= THIS_MODULE,
2007	.open		= vicodec_open,
2008	.release	= vicodec_release,
2009	.poll		= v4l2_m2m_fop_poll,
2010	.unlocked_ioctl	= video_ioctl2,
2011	.mmap		= v4l2_m2m_fop_mmap,
2012};
2013
2014static const struct video_device vicodec_videodev = {
2015	.name		= VICODEC_NAME,
2016	.vfl_dir	= VFL_DIR_M2M,
2017	.fops		= &vicodec_fops,
2018	.ioctl_ops	= &vicodec_ioctl_ops,
2019	.minor		= -1,
2020	.release	= video_device_release_empty,
2021};
2022
2023static const struct media_device_ops vicodec_m2m_media_ops = {
2024	.req_validate	= vicodec_request_validate,
2025	.req_queue	= v4l2_m2m_request_queue,
2026};
2027
2028static const struct v4l2_m2m_ops m2m_ops = {
2029	.device_run	= device_run,
2030	.job_ready	= job_ready,
2031};
2032
2033static int register_instance(struct vicodec_dev *dev,
2034			     struct vicodec_dev_instance *dev_instance,
2035			     const char *name, bool is_enc, bool is_stateless)
2036{
2037	struct video_device *vfd;
2038	int ret;
2039
2040	spin_lock_init(&dev_instance->lock);
2041	mutex_init(&dev_instance->mutex);
2042	dev_instance->m2m_dev = v4l2_m2m_init(&m2m_ops);
2043	if (IS_ERR(dev_instance->m2m_dev)) {
2044		v4l2_err(&dev->v4l2_dev, "Failed to init vicodec enc device\n");
2045		return PTR_ERR(dev_instance->m2m_dev);
2046	}
2047
2048	dev_instance->vfd = vicodec_videodev;
2049	vfd = &dev_instance->vfd;
2050	vfd->lock = &dev_instance->mutex;
2051	vfd->v4l2_dev = &dev->v4l2_dev;
2052	strscpy(vfd->name, name, sizeof(vfd->name));
2053	vfd->device_caps = V4L2_CAP_STREAMING |
2054		(multiplanar ? V4L2_CAP_VIDEO_M2M_MPLANE : V4L2_CAP_VIDEO_M2M);
2055	if (is_enc || is_stateless) {
2056		v4l2_disable_ioctl(vfd, VIDIOC_DECODER_CMD);
2057		v4l2_disable_ioctl(vfd, VIDIOC_TRY_DECODER_CMD);
2058	}
2059	if (!is_enc) {
2060		v4l2_disable_ioctl(vfd, VIDIOC_ENCODER_CMD);
2061		v4l2_disable_ioctl(vfd, VIDIOC_TRY_ENCODER_CMD);
2062	}
2063	video_set_drvdata(vfd, dev);
2064
2065	ret = video_register_device(vfd, VFL_TYPE_VIDEO, 0);
2066	if (ret) {
2067		v4l2_err(&dev->v4l2_dev, "Failed to register video device '%s'\n", name);
2068		v4l2_m2m_release(dev_instance->m2m_dev);
2069		return ret;
2070	}
2071	v4l2_info(&dev->v4l2_dev, "Device '%s' registered as /dev/video%d\n",
2072		  name, vfd->num);
2073	return 0;
2074}
2075
2076static void vicodec_v4l2_dev_release(struct v4l2_device *v4l2_dev)
2077{
2078	struct vicodec_dev *dev = container_of(v4l2_dev, struct vicodec_dev, v4l2_dev);
2079
2080	v4l2_device_unregister(&dev->v4l2_dev);
2081	v4l2_m2m_release(dev->stateful_enc.m2m_dev);
2082	v4l2_m2m_release(dev->stateful_dec.m2m_dev);
2083	v4l2_m2m_release(dev->stateless_dec.m2m_dev);
2084#ifdef CONFIG_MEDIA_CONTROLLER
2085	media_device_cleanup(&dev->mdev);
2086#endif
2087	kfree(dev);
2088}
2089
2090static int vicodec_probe(struct platform_device *pdev)
2091{
2092	struct vicodec_dev *dev;
2093	int ret;
2094
2095	dev = kzalloc(sizeof(*dev), GFP_KERNEL);
2096	if (!dev)
2097		return -ENOMEM;
2098
2099	ret = v4l2_device_register(&pdev->dev, &dev->v4l2_dev);
2100	if (ret)
2101		goto free_dev;
2102
2103	dev->v4l2_dev.release = vicodec_v4l2_dev_release;
2104
2105#ifdef CONFIG_MEDIA_CONTROLLER
2106	dev->mdev.dev = &pdev->dev;
2107	strscpy(dev->mdev.model, "vicodec", sizeof(dev->mdev.model));
2108	strscpy(dev->mdev.bus_info, "platform:vicodec",
2109		sizeof(dev->mdev.bus_info));
2110	media_device_init(&dev->mdev);
2111	dev->mdev.ops = &vicodec_m2m_media_ops;
2112	dev->v4l2_dev.mdev = &dev->mdev;
2113#endif
2114
2115	platform_set_drvdata(pdev, dev);
2116
2117	ret = register_instance(dev, &dev->stateful_enc, "stateful-encoder",
2118				true, false);
2119	if (ret)
2120		goto unreg_dev;
2121
2122	ret = register_instance(dev, &dev->stateful_dec, "stateful-decoder",
2123				false, false);
2124	if (ret)
2125		goto unreg_sf_enc;
2126
2127	ret = register_instance(dev, &dev->stateless_dec, "stateless-decoder",
2128				false, true);
2129	if (ret)
2130		goto unreg_sf_dec;
2131
2132#ifdef CONFIG_MEDIA_CONTROLLER
2133	ret = v4l2_m2m_register_media_controller(dev->stateful_enc.m2m_dev,
2134						 &dev->stateful_enc.vfd,
2135						 MEDIA_ENT_F_PROC_VIDEO_ENCODER);
2136	if (ret) {
2137		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for enc\n");
2138		goto unreg_m2m;
2139	}
2140
2141	ret = v4l2_m2m_register_media_controller(dev->stateful_dec.m2m_dev,
2142						 &dev->stateful_dec.vfd,
2143						 MEDIA_ENT_F_PROC_VIDEO_DECODER);
2144	if (ret) {
2145		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for dec\n");
2146		goto unreg_m2m_sf_enc_mc;
2147	}
2148
2149	ret = v4l2_m2m_register_media_controller(dev->stateless_dec.m2m_dev,
2150						 &dev->stateless_dec.vfd,
2151						 MEDIA_ENT_F_PROC_VIDEO_DECODER);
2152	if (ret) {
2153		v4l2_err(&dev->v4l2_dev, "Failed to init mem2mem media controller for stateless dec\n");
2154		goto unreg_m2m_sf_dec_mc;
2155	}
2156
2157	ret = media_device_register(&dev->mdev);
2158	if (ret) {
2159		v4l2_err(&dev->v4l2_dev, "Failed to register mem2mem media device\n");
2160		goto unreg_m2m_sl_dec_mc;
2161	}
2162#endif
2163	return 0;
2164
2165#ifdef CONFIG_MEDIA_CONTROLLER
2166unreg_m2m_sl_dec_mc:
2167	v4l2_m2m_unregister_media_controller(dev->stateless_dec.m2m_dev);
2168unreg_m2m_sf_dec_mc:
2169	v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev);
2170unreg_m2m_sf_enc_mc:
2171	v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev);
2172unreg_m2m:
2173	video_unregister_device(&dev->stateless_dec.vfd);
2174	v4l2_m2m_release(dev->stateless_dec.m2m_dev);
2175#endif
2176unreg_sf_dec:
2177	video_unregister_device(&dev->stateful_dec.vfd);
2178	v4l2_m2m_release(dev->stateful_dec.m2m_dev);
2179unreg_sf_enc:
2180	video_unregister_device(&dev->stateful_enc.vfd);
2181	v4l2_m2m_release(dev->stateful_enc.m2m_dev);
2182unreg_dev:
2183	v4l2_device_unregister(&dev->v4l2_dev);
2184free_dev:
2185	kfree(dev);
2186
2187	return ret;
2188}
2189
2190static void vicodec_remove(struct platform_device *pdev)
2191{
2192	struct vicodec_dev *dev = platform_get_drvdata(pdev);
2193
2194	v4l2_info(&dev->v4l2_dev, "Removing " VICODEC_NAME);
2195
2196#ifdef CONFIG_MEDIA_CONTROLLER
2197	media_device_unregister(&dev->mdev);
2198	v4l2_m2m_unregister_media_controller(dev->stateful_enc.m2m_dev);
2199	v4l2_m2m_unregister_media_controller(dev->stateful_dec.m2m_dev);
2200	v4l2_m2m_unregister_media_controller(dev->stateless_dec.m2m_dev);
2201#endif
2202
2203	video_unregister_device(&dev->stateful_enc.vfd);
2204	video_unregister_device(&dev->stateful_dec.vfd);
2205	video_unregister_device(&dev->stateless_dec.vfd);
2206	v4l2_device_put(&dev->v4l2_dev);
2207}
2208
2209static struct platform_driver vicodec_pdrv = {
2210	.probe		= vicodec_probe,
2211	.remove		= vicodec_remove,
2212	.driver		= {
2213		.name	= VICODEC_NAME,
2214	},
2215};
2216
2217static void __exit vicodec_exit(void)
2218{
2219	platform_driver_unregister(&vicodec_pdrv);
2220	platform_device_unregister(&vicodec_pdev);
2221}
2222
2223static int __init vicodec_init(void)
2224{
2225	int ret;
2226
2227	ret = platform_device_register(&vicodec_pdev);
2228	if (ret)
2229		return ret;
2230
2231	ret = platform_driver_register(&vicodec_pdrv);
2232	if (ret)
2233		platform_device_unregister(&vicodec_pdev);
2234
2235	return ret;
2236}
2237
2238module_init(vicodec_init);
2239module_exit(vicodec_exit);
~/ Read-only file preview.