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