File Index
Files and directories
Sign in
FILE

gsc-m2m.c

File preview with syntax highlighting.

linux-6.19.14/drivers/media/platform/samsung/exynos-gsc/gsc-m2m.c C
  1// SPDX-License-Identifier: GPL-2.0-or-later
  2/*
  3 * Copyright (c) 2011 - 2012 Samsung Electronics Co., Ltd.
  4 *		http://www.samsung.com
  5 *
  6 * Samsung EXYNOS5 SoC series G-Scaler driver
  7 */
  8
  9#include <linux/module.h>
 10#include <linux/kernel.h>
 11#include <linux/types.h>
 12#include <linux/errno.h>
 13#include <linux/bug.h>
 14#include <linux/interrupt.h>
 15#include <linux/workqueue.h>
 16#include <linux/device.h>
 17#include <linux/platform_device.h>
 18#include <linux/list.h>
 19#include <linux/io.h>
 20#include <linux/slab.h>
 21#include <linux/clk.h>
 22
 23#include <media/v4l2-ioctl.h>
 24
 25#include "gsc-core.h"
 26
 27static int gsc_m2m_ctx_stop_req(struct gsc_ctx *ctx)
 28{
 29	struct gsc_ctx *curr_ctx;
 30	struct gsc_dev *gsc = ctx->gsc_dev;
 31	int ret;
 32
 33	curr_ctx = v4l2_m2m_get_curr_priv(gsc->m2m.m2m_dev);
 34	if (!gsc_m2m_pending(gsc) || (curr_ctx != ctx))
 35		return 0;
 36
 37	gsc_ctx_state_lock_set(GSC_CTX_STOP_REQ, ctx);
 38	ret = wait_event_timeout(gsc->irq_queue,
 39			!gsc_ctx_state_is_set(GSC_CTX_STOP_REQ, ctx),
 40			GSC_SHUTDOWN_TIMEOUT);
 41
 42	return ret == 0 ? -ETIMEDOUT : ret;
 43}
 44
 45static void __gsc_m2m_job_abort(struct gsc_ctx *ctx)
 46{
 47	int ret;
 48
 49	ret = gsc_m2m_ctx_stop_req(ctx);
 50	if ((ret == -ETIMEDOUT) || (ctx->state & GSC_CTX_ABORT)) {
 51		gsc_ctx_state_lock_clear(GSC_CTX_STOP_REQ | GSC_CTX_ABORT, ctx);
 52		gsc_m2m_job_finish(ctx, VB2_BUF_STATE_ERROR);
 53	}
 54}
 55
 56static int gsc_m2m_start_streaming(struct vb2_queue *q, unsigned int count)
 57{
 58	struct gsc_ctx *ctx = q->drv_priv;
 59
 60	return pm_runtime_resume_and_get(&ctx->gsc_dev->pdev->dev);
 61}
 62
 63static void __gsc_m2m_cleanup_queue(struct gsc_ctx *ctx)
 64{
 65	struct vb2_v4l2_buffer *src_vb, *dst_vb;
 66
 67	while (v4l2_m2m_num_src_bufs_ready(ctx->m2m_ctx) > 0) {
 68		src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
 69		v4l2_m2m_buf_done(src_vb, VB2_BUF_STATE_ERROR);
 70	}
 71
 72	while (v4l2_m2m_num_dst_bufs_ready(ctx->m2m_ctx) > 0) {
 73		dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
 74		v4l2_m2m_buf_done(dst_vb, VB2_BUF_STATE_ERROR);
 75	}
 76}
 77
 78static void gsc_m2m_stop_streaming(struct vb2_queue *q)
 79{
 80	struct gsc_ctx *ctx = q->drv_priv;
 81
 82	__gsc_m2m_job_abort(ctx);
 83
 84	__gsc_m2m_cleanup_queue(ctx);
 85
 86	pm_runtime_put(&ctx->gsc_dev->pdev->dev);
 87}
 88
 89void gsc_m2m_job_finish(struct gsc_ctx *ctx, int vb_state)
 90{
 91	struct vb2_v4l2_buffer *src_vb, *dst_vb;
 92
 93	if (!ctx || !ctx->m2m_ctx)
 94		return;
 95
 96	src_vb = v4l2_m2m_src_buf_remove(ctx->m2m_ctx);
 97	dst_vb = v4l2_m2m_dst_buf_remove(ctx->m2m_ctx);
 98
 99	if (src_vb && dst_vb) {
100		dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
101		dst_vb->timecode = src_vb->timecode;
102		dst_vb->flags &= ~V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
103		dst_vb->flags |=
104			src_vb->flags
105			& V4L2_BUF_FLAG_TSTAMP_SRC_MASK;
106
107		v4l2_m2m_buf_done(src_vb, vb_state);
108		v4l2_m2m_buf_done(dst_vb, vb_state);
109
110		v4l2_m2m_job_finish(ctx->gsc_dev->m2m.m2m_dev,
111				    ctx->m2m_ctx);
112	}
113}
114
115static void gsc_m2m_job_abort(void *priv)
116{
117	__gsc_m2m_job_abort((struct gsc_ctx *)priv);
118}
119
120static int gsc_get_bufs(struct gsc_ctx *ctx)
121{
122	struct gsc_frame *s_frame, *d_frame;
123	struct vb2_v4l2_buffer *src_vb, *dst_vb;
124	int ret;
125
126	s_frame = &ctx->s_frame;
127	d_frame = &ctx->d_frame;
128
129	src_vb = v4l2_m2m_next_src_buf(ctx->m2m_ctx);
130	ret = gsc_prepare_addr(ctx, &src_vb->vb2_buf, s_frame, &s_frame->addr);
131	if (ret)
132		return ret;
133
134	dst_vb = v4l2_m2m_next_dst_buf(ctx->m2m_ctx);
135	ret = gsc_prepare_addr(ctx, &dst_vb->vb2_buf, d_frame, &d_frame->addr);
136	if (ret)
137		return ret;
138
139	dst_vb->vb2_buf.timestamp = src_vb->vb2_buf.timestamp;
140
141	return 0;
142}
143
144static void gsc_m2m_device_run(void *priv)
145{
146	struct gsc_ctx *ctx = priv;
147	struct gsc_dev *gsc;
148	unsigned long flags;
149	int ret;
150	bool is_set = false;
151
152	if (WARN(!ctx, "null hardware context\n"))
153		return;
154
155	gsc = ctx->gsc_dev;
156	spin_lock_irqsave(&gsc->slock, flags);
157
158	set_bit(ST_M2M_PEND, &gsc->state);
159
160	/* Reconfigure hardware if the context has changed. */
161	if (gsc->m2m.ctx != ctx) {
162		pr_debug("gsc->m2m.ctx = 0x%p, current_ctx = 0x%p",
163				gsc->m2m.ctx, ctx);
164		ctx->state |= GSC_PARAMS;
165		gsc->m2m.ctx = ctx;
166	}
167
168	is_set = ctx->state & GSC_CTX_STOP_REQ;
169	if (is_set) {
170		ctx->state &= ~GSC_CTX_STOP_REQ;
171		ctx->state |= GSC_CTX_ABORT;
172		wake_up(&gsc->irq_queue);
173		goto put_device;
174	}
175
176	ret = gsc_get_bufs(ctx);
177	if (ret) {
178		pr_err("Wrong address");
179		goto put_device;
180	}
181
182	gsc_set_prefbuf(gsc, &ctx->s_frame);
183	gsc_hw_set_input_addr(gsc, &ctx->s_frame.addr, GSC_M2M_BUF_NUM);
184	gsc_hw_set_output_addr(gsc, &ctx->d_frame.addr, GSC_M2M_BUF_NUM);
185
186	if (ctx->state & GSC_PARAMS) {
187		gsc_hw_set_input_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
188		gsc_hw_set_output_buf_masking(gsc, GSC_M2M_BUF_NUM, false);
189		gsc_hw_set_frm_done_irq_mask(gsc, false);
190		gsc_hw_set_gsc_irq_enable(gsc, true);
191
192		if (gsc_set_scaler_info(ctx)) {
193			pr_err("Scaler setup error");
194			goto put_device;
195		}
196
197		gsc_hw_set_input_path(ctx);
198		gsc_hw_set_in_size(ctx);
199		gsc_hw_set_in_image_format(ctx);
200
201		gsc_hw_set_output_path(ctx);
202		gsc_hw_set_out_size(ctx);
203		gsc_hw_set_out_image_format(ctx);
204
205		gsc_hw_set_prescaler(ctx);
206		gsc_hw_set_mainscaler(ctx);
207		gsc_hw_set_rotation(ctx);
208		gsc_hw_set_global_alpha(ctx);
209	}
210
211	/* update shadow registers */
212	gsc_hw_set_sfr_update(ctx);
213
214	ctx->state &= ~GSC_PARAMS;
215	gsc_hw_enable_control(gsc, true);
216
217	spin_unlock_irqrestore(&gsc->slock, flags);
218	return;
219
220put_device:
221	ctx->state &= ~GSC_PARAMS;
222	spin_unlock_irqrestore(&gsc->slock, flags);
223}
224
225static int gsc_m2m_queue_setup(struct vb2_queue *vq,
226			unsigned int *num_buffers, unsigned int *num_planes,
227			unsigned int sizes[], struct device *alloc_devs[])
228{
229	struct gsc_ctx *ctx = vb2_get_drv_priv(vq);
230	struct gsc_frame *frame;
231	int i;
232
233	frame = ctx_get_frame(ctx, vq->type);
234	if (IS_ERR(frame))
235		return PTR_ERR(frame);
236
237	if (!frame->fmt)
238		return -EINVAL;
239
240	*num_planes = frame->fmt->num_planes;
241	for (i = 0; i < frame->fmt->num_planes; i++)
242		sizes[i] = frame->payload[i];
243	return 0;
244}
245
246static int gsc_m2m_buf_prepare(struct vb2_buffer *vb)
247{
248	struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
249	struct gsc_frame *frame;
250	int i;
251
252	frame = ctx_get_frame(ctx, vb->vb2_queue->type);
253	if (IS_ERR(frame))
254		return PTR_ERR(frame);
255
256	if (V4L2_TYPE_IS_CAPTURE(vb->vb2_queue->type)) {
257		for (i = 0; i < frame->fmt->num_planes; i++)
258			vb2_set_plane_payload(vb, i, frame->payload[i]);
259	}
260
261	return 0;
262}
263
264static void gsc_m2m_buf_queue(struct vb2_buffer *vb)
265{
266	struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
267	struct gsc_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
268
269	pr_debug("ctx: %p, ctx->state: 0x%x", ctx, ctx->state);
270
271	if (ctx->m2m_ctx)
272		v4l2_m2m_buf_queue(ctx->m2m_ctx, vbuf);
273}
274
275static const struct vb2_ops gsc_m2m_qops = {
276	.queue_setup	 = gsc_m2m_queue_setup,
277	.buf_prepare	 = gsc_m2m_buf_prepare,
278	.buf_queue	 = gsc_m2m_buf_queue,
279	.stop_streaming	 = gsc_m2m_stop_streaming,
280	.start_streaming = gsc_m2m_start_streaming,
281};
282
283static int gsc_m2m_querycap(struct file *file, void *fh,
284			   struct v4l2_capability *cap)
285{
286	strscpy(cap->driver, GSC_MODULE_NAME, sizeof(cap->driver));
287	strscpy(cap->card, GSC_MODULE_NAME " gscaler", sizeof(cap->card));
288	return 0;
289}
290
291static int gsc_m2m_enum_fmt(struct file *file, void *priv,
292			    struct v4l2_fmtdesc *f)
293{
294	return gsc_enum_fmt(f);
295}
296
297static int gsc_m2m_g_fmt_mplane(struct file *file, void *fh,
298			     struct v4l2_format *f)
299{
300	struct gsc_ctx *ctx = file_to_ctx(file);
301
302	return gsc_g_fmt_mplane(ctx, f);
303}
304
305static int gsc_m2m_try_fmt_mplane(struct file *file, void *fh,
306				  struct v4l2_format *f)
307{
308	struct gsc_ctx *ctx = file_to_ctx(file);
309
310	return gsc_try_fmt_mplane(ctx, f);
311}
312
313static int gsc_m2m_s_fmt_mplane(struct file *file, void *fh,
314				 struct v4l2_format *f)
315{
316	struct gsc_ctx *ctx = file_to_ctx(file);
317	struct vb2_queue *vq;
318	struct gsc_frame *frame;
319	struct v4l2_pix_format_mplane *pix;
320	int i, ret = 0;
321
322	ret = gsc_m2m_try_fmt_mplane(file, fh, f);
323	if (ret)
324		return ret;
325
326	vq = v4l2_m2m_get_vq(ctx->m2m_ctx, f->type);
327
328	if (vb2_is_streaming(vq)) {
329		pr_err("queue (%d) busy", f->type);
330		return -EBUSY;
331	}
332
333	if (V4L2_TYPE_IS_OUTPUT(f->type))
334		frame = &ctx->s_frame;
335	else
336		frame = &ctx->d_frame;
337
338	pix = &f->fmt.pix_mp;
339	frame->fmt = find_fmt(&pix->pixelformat, NULL, 0);
340	frame->colorspace = pix->colorspace;
341	if (!frame->fmt)
342		return -EINVAL;
343
344	for (i = 0; i < frame->fmt->num_planes; i++)
345		frame->payload[i] = pix->plane_fmt[i].sizeimage;
346
347	gsc_set_frame_size(frame, pix->width, pix->height);
348
349	if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
350		gsc_ctx_state_lock_set(GSC_PARAMS | GSC_DST_FMT, ctx);
351	else
352		gsc_ctx_state_lock_set(GSC_PARAMS | GSC_SRC_FMT, ctx);
353
354	pr_debug("f_w: %d, f_h: %d", frame->f_width, frame->f_height);
355
356	return 0;
357}
358
359static int gsc_m2m_reqbufs(struct file *file, void *fh,
360			  struct v4l2_requestbuffers *reqbufs)
361{
362	struct gsc_ctx *ctx = file_to_ctx(file);
363	struct gsc_dev *gsc = ctx->gsc_dev;
364	u32 max_cnt;
365
366	max_cnt = (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) ?
367		gsc->variant->in_buf_cnt : gsc->variant->out_buf_cnt;
368	if (reqbufs->count > max_cnt)
369		return -EINVAL;
370
371	return v4l2_m2m_reqbufs(file, ctx->m2m_ctx, reqbufs);
372}
373
374static int gsc_m2m_expbuf(struct file *file, void *fh,
375				struct v4l2_exportbuffer *eb)
376{
377	struct gsc_ctx *ctx = file_to_ctx(file);
378	return v4l2_m2m_expbuf(file, ctx->m2m_ctx, eb);
379}
380
381static int gsc_m2m_querybuf(struct file *file, void *fh,
382					struct v4l2_buffer *buf)
383{
384	struct gsc_ctx *ctx = file_to_ctx(file);
385	return v4l2_m2m_querybuf(file, ctx->m2m_ctx, buf);
386}
387
388static int gsc_m2m_qbuf(struct file *file, void *fh,
389			  struct v4l2_buffer *buf)
390{
391	struct gsc_ctx *ctx = file_to_ctx(file);
392	return v4l2_m2m_qbuf(file, ctx->m2m_ctx, buf);
393}
394
395static int gsc_m2m_dqbuf(struct file *file, void *fh,
396			   struct v4l2_buffer *buf)
397{
398	struct gsc_ctx *ctx = file_to_ctx(file);
399	return v4l2_m2m_dqbuf(file, ctx->m2m_ctx, buf);
400}
401
402static int gsc_m2m_streamon(struct file *file, void *fh,
403			   enum v4l2_buf_type type)
404{
405	struct gsc_ctx *ctx = file_to_ctx(file);
406
407	/* The source and target color format need to be set */
408	if (V4L2_TYPE_IS_OUTPUT(type)) {
409		if (!gsc_ctx_state_is_set(GSC_SRC_FMT, ctx))
410			return -EINVAL;
411	} else if (!gsc_ctx_state_is_set(GSC_DST_FMT, ctx)) {
412		return -EINVAL;
413	}
414
415	return v4l2_m2m_streamon(file, ctx->m2m_ctx, type);
416}
417
418static int gsc_m2m_streamoff(struct file *file, void *fh,
419			    enum v4l2_buf_type type)
420{
421	struct gsc_ctx *ctx = file_to_ctx(file);
422	return v4l2_m2m_streamoff(file, ctx->m2m_ctx, type);
423}
424
425/* Return 1 if rectangle a is enclosed in rectangle b, or 0 otherwise. */
426static int is_rectangle_enclosed(struct v4l2_rect *a, struct v4l2_rect *b)
427{
428	if (a->left < b->left || a->top < b->top)
429		return 0;
430
431	if (a->left + a->width > b->left + b->width)
432		return 0;
433
434	if (a->top + a->height > b->top + b->height)
435		return 0;
436
437	return 1;
438}
439
440static int gsc_m2m_g_selection(struct file *file, void *fh,
441			struct v4l2_selection *s)
442{
443	struct gsc_ctx *ctx = file_to_ctx(file);
444	struct gsc_frame *frame;
445
446	if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
447	    (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
448		return -EINVAL;
449
450	frame = ctx_get_frame(ctx, s->type);
451	if (IS_ERR(frame))
452		return PTR_ERR(frame);
453
454	switch (s->target) {
455	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
456	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
457	case V4L2_SEL_TGT_CROP_BOUNDS:
458	case V4L2_SEL_TGT_CROP_DEFAULT:
459		s->r.left = 0;
460		s->r.top = 0;
461		s->r.width = frame->f_width;
462		s->r.height = frame->f_height;
463		return 0;
464
465	case V4L2_SEL_TGT_COMPOSE:
466	case V4L2_SEL_TGT_CROP:
467		s->r.left = frame->crop.left;
468		s->r.top = frame->crop.top;
469		s->r.width = frame->crop.width;
470		s->r.height = frame->crop.height;
471		return 0;
472	}
473
474	return -EINVAL;
475}
476
477static int gsc_m2m_s_selection(struct file *file, void *fh,
478				struct v4l2_selection *s)
479{
480	struct gsc_frame *frame;
481	struct gsc_ctx *ctx = file_to_ctx(file);
482	struct gsc_variant *variant = ctx->gsc_dev->variant;
483	struct v4l2_selection sel = *s;
484	int ret;
485
486	if ((s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE) &&
487	    (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT))
488		return -EINVAL;
489
490	ret = gsc_try_selection(ctx, &sel);
491	if (ret)
492		return ret;
493
494	if (s->flags & V4L2_SEL_FLAG_LE &&
495	    !is_rectangle_enclosed(&sel.r, &s->r))
496		return -ERANGE;
497
498	if (s->flags & V4L2_SEL_FLAG_GE &&
499	    !is_rectangle_enclosed(&s->r, &sel.r))
500		return -ERANGE;
501
502	s->r = sel.r;
503
504	switch (s->target) {
505	case V4L2_SEL_TGT_COMPOSE_BOUNDS:
506	case V4L2_SEL_TGT_COMPOSE_DEFAULT:
507	case V4L2_SEL_TGT_COMPOSE:
508		frame = &ctx->s_frame;
509		break;
510
511	case V4L2_SEL_TGT_CROP_BOUNDS:
512	case V4L2_SEL_TGT_CROP:
513	case V4L2_SEL_TGT_CROP_DEFAULT:
514		frame = &ctx->d_frame;
515		break;
516
517	default:
518		return -EINVAL;
519	}
520
521	/* Check to see if scaling ratio is within supported range */
522	if (gsc_ctx_state_is_set(GSC_DST_FMT | GSC_SRC_FMT, ctx)) {
523		if (s->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
524			ret = gsc_check_scaler_ratio(variant, sel.r.width,
525				sel.r.height, ctx->d_frame.crop.width,
526				ctx->d_frame.crop.height,
527				ctx->gsc_ctrls.rotate->val, ctx->out_path);
528		} else {
529			ret = gsc_check_scaler_ratio(variant,
530				ctx->s_frame.crop.width,
531				ctx->s_frame.crop.height, sel.r.width,
532				sel.r.height, ctx->gsc_ctrls.rotate->val,
533				ctx->out_path);
534		}
535
536		if (ret) {
537			pr_err("Out of scaler range");
538			return -EINVAL;
539		}
540	}
541
542	frame->crop = sel.r;
543
544	gsc_ctx_state_lock_set(GSC_PARAMS, ctx);
545	return 0;
546}
547
548static const struct v4l2_ioctl_ops gsc_m2m_ioctl_ops = {
549	.vidioc_querycap		= gsc_m2m_querycap,
550	.vidioc_enum_fmt_vid_cap	= gsc_m2m_enum_fmt,
551	.vidioc_enum_fmt_vid_out	= gsc_m2m_enum_fmt,
552	.vidioc_g_fmt_vid_cap_mplane	= gsc_m2m_g_fmt_mplane,
553	.vidioc_g_fmt_vid_out_mplane	= gsc_m2m_g_fmt_mplane,
554	.vidioc_try_fmt_vid_cap_mplane	= gsc_m2m_try_fmt_mplane,
555	.vidioc_try_fmt_vid_out_mplane	= gsc_m2m_try_fmt_mplane,
556	.vidioc_s_fmt_vid_cap_mplane	= gsc_m2m_s_fmt_mplane,
557	.vidioc_s_fmt_vid_out_mplane	= gsc_m2m_s_fmt_mplane,
558	.vidioc_reqbufs			= gsc_m2m_reqbufs,
559	.vidioc_expbuf                  = gsc_m2m_expbuf,
560	.vidioc_querybuf		= gsc_m2m_querybuf,
561	.vidioc_qbuf			= gsc_m2m_qbuf,
562	.vidioc_dqbuf			= gsc_m2m_dqbuf,
563	.vidioc_streamon		= gsc_m2m_streamon,
564	.vidioc_streamoff		= gsc_m2m_streamoff,
565	.vidioc_g_selection		= gsc_m2m_g_selection,
566	.vidioc_s_selection		= gsc_m2m_s_selection
567};
568
569static int queue_init(void *priv, struct vb2_queue *src_vq,
570			struct vb2_queue *dst_vq)
571{
572	struct gsc_ctx *ctx = priv;
573	int ret;
574
575	memset(src_vq, 0, sizeof(*src_vq));
576	src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
577	src_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
578	src_vq->drv_priv = ctx;
579	src_vq->ops = &gsc_m2m_qops;
580	src_vq->mem_ops = &vb2_dma_contig_memops;
581	src_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
582	src_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
583	src_vq->lock = &ctx->gsc_dev->lock;
584	src_vq->dev = &ctx->gsc_dev->pdev->dev;
585
586	ret = vb2_queue_init(src_vq);
587	if (ret)
588		return ret;
589
590	memset(dst_vq, 0, sizeof(*dst_vq));
591	dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
592	dst_vq->io_modes = VB2_MMAP | VB2_USERPTR | VB2_DMABUF;
593	dst_vq->drv_priv = ctx;
594	dst_vq->ops = &gsc_m2m_qops;
595	dst_vq->mem_ops = &vb2_dma_contig_memops;
596	dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
597	dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
598	dst_vq->lock = &ctx->gsc_dev->lock;
599	dst_vq->dev = &ctx->gsc_dev->pdev->dev;
600
601	return vb2_queue_init(dst_vq);
602}
603
604static int gsc_m2m_open(struct file *file)
605{
606	struct gsc_dev *gsc = video_drvdata(file);
607	struct gsc_ctx *ctx = NULL;
608	int ret;
609
610	pr_debug("pid: %d, state: 0x%lx", task_pid_nr(current), gsc->state);
611
612	if (mutex_lock_interruptible(&gsc->lock))
613		return -ERESTARTSYS;
614
615	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL);
616	if (!ctx) {
617		ret = -ENOMEM;
618		goto unlock;
619	}
620
621	v4l2_fh_init(&ctx->fh, gsc->m2m.vfd);
622	ret = gsc_ctrls_create(ctx);
623	if (ret)
624		goto error_fh;
625
626	/* Use separate control handler per file handle */
627	ctx->fh.ctrl_handler = &ctx->ctrl_handler;
628	v4l2_fh_add(&ctx->fh, file);
629
630	ctx->gsc_dev = gsc;
631	/* Default color format */
632	ctx->s_frame.fmt = get_format(0);
633	ctx->d_frame.fmt = get_format(0);
634	/* Setup the device context for mem2mem mode. */
635	ctx->state = GSC_CTX_M2M;
636	ctx->flags = 0;
637	ctx->in_path = GSC_DMA;
638	ctx->out_path = GSC_DMA;
639
640	ctx->m2m_ctx = v4l2_m2m_ctx_init(gsc->m2m.m2m_dev, ctx, queue_init);
641	if (IS_ERR(ctx->m2m_ctx)) {
642		pr_err("Failed to initialize m2m context");
643		ret = PTR_ERR(ctx->m2m_ctx);
644		goto error_ctrls;
645	}
646
647	if (gsc->m2m.refcnt++ == 0)
648		set_bit(ST_M2M_OPEN, &gsc->state);
649
650	pr_debug("gsc m2m driver is opened, ctx(0x%p)", ctx);
651
652	mutex_unlock(&gsc->lock);
653	return 0;
654
655error_ctrls:
656	gsc_ctrls_delete(ctx);
657	v4l2_fh_del(&ctx->fh, file);
658error_fh:
659	v4l2_fh_exit(&ctx->fh);
660	kfree(ctx);
661unlock:
662	mutex_unlock(&gsc->lock);
663	return ret;
664}
665
666static int gsc_m2m_release(struct file *file)
667{
668	struct gsc_ctx *ctx = file_to_ctx(file);
669	struct gsc_dev *gsc = ctx->gsc_dev;
670
671	pr_debug("pid: %d, state: 0x%lx, refcnt= %d",
672		task_pid_nr(current), gsc->state, gsc->m2m.refcnt);
673
674	mutex_lock(&gsc->lock);
675
676	v4l2_m2m_ctx_release(ctx->m2m_ctx);
677	gsc_ctrls_delete(ctx);
678	v4l2_fh_del(&ctx->fh, file);
679	v4l2_fh_exit(&ctx->fh);
680
681	if (--gsc->m2m.refcnt <= 0)
682		clear_bit(ST_M2M_OPEN, &gsc->state);
683	kfree(ctx);
684
685	mutex_unlock(&gsc->lock);
686	return 0;
687}
688
689static __poll_t gsc_m2m_poll(struct file *file,
690					struct poll_table_struct *wait)
691{
692	struct gsc_ctx *ctx = file_to_ctx(file);
693	struct gsc_dev *gsc = ctx->gsc_dev;
694	__poll_t ret;
695
696	if (mutex_lock_interruptible(&gsc->lock))
697		return EPOLLERR;
698
699	ret = v4l2_m2m_poll(file, ctx->m2m_ctx, wait);
700	mutex_unlock(&gsc->lock);
701
702	return ret;
703}
704
705static int gsc_m2m_mmap(struct file *file, struct vm_area_struct *vma)
706{
707	struct gsc_ctx *ctx = file_to_ctx(file);
708	struct gsc_dev *gsc = ctx->gsc_dev;
709	int ret;
710
711	if (mutex_lock_interruptible(&gsc->lock))
712		return -ERESTARTSYS;
713
714	ret = v4l2_m2m_mmap(file, ctx->m2m_ctx, vma);
715	mutex_unlock(&gsc->lock);
716
717	return ret;
718}
719
720static const struct v4l2_file_operations gsc_m2m_fops = {
721	.owner		= THIS_MODULE,
722	.open		= gsc_m2m_open,
723	.release	= gsc_m2m_release,
724	.poll		= gsc_m2m_poll,
725	.unlocked_ioctl	= video_ioctl2,
726	.mmap		= gsc_m2m_mmap,
727};
728
729static const struct v4l2_m2m_ops gsc_m2m_ops = {
730	.device_run	= gsc_m2m_device_run,
731	.job_abort	= gsc_m2m_job_abort,
732};
733
734int gsc_register_m2m_device(struct gsc_dev *gsc)
735{
736	struct platform_device *pdev;
737	int ret;
738
739	if (!gsc)
740		return -ENODEV;
741
742	pdev = gsc->pdev;
743
744	gsc->vdev.fops		= &gsc_m2m_fops;
745	gsc->vdev.ioctl_ops	= &gsc_m2m_ioctl_ops;
746	gsc->vdev.release	= video_device_release_empty;
747	gsc->vdev.lock		= &gsc->lock;
748	gsc->vdev.vfl_dir	= VFL_DIR_M2M;
749	gsc->vdev.v4l2_dev	= &gsc->v4l2_dev;
750	gsc->vdev.device_caps	= V4L2_CAP_STREAMING |
751				  V4L2_CAP_VIDEO_M2M_MPLANE;
752	snprintf(gsc->vdev.name, sizeof(gsc->vdev.name), "%s.%d:m2m",
753					GSC_MODULE_NAME, gsc->id);
754
755	video_set_drvdata(&gsc->vdev, gsc);
756
757	gsc->m2m.vfd = &gsc->vdev;
758	gsc->m2m.m2m_dev = v4l2_m2m_init(&gsc_m2m_ops);
759	if (IS_ERR(gsc->m2m.m2m_dev)) {
760		dev_err(&pdev->dev, "failed to initialize v4l2-m2m device\n");
761		return PTR_ERR(gsc->m2m.m2m_dev);
762	}
763
764	ret = video_register_device(&gsc->vdev, VFL_TYPE_VIDEO, -1);
765	if (ret) {
766		dev_err(&pdev->dev,
767			 "%s(): failed to register video device\n", __func__);
768		goto err_m2m_release;
769	}
770
771	pr_debug("gsc m2m driver registered as /dev/video%d", gsc->vdev.num);
772	return 0;
773
774err_m2m_release:
775	v4l2_m2m_release(gsc->m2m.m2m_dev);
776
777	return ret;
778}
779
780void gsc_unregister_m2m_device(struct gsc_dev *gsc)
781{
782	if (gsc) {
783		v4l2_m2m_release(gsc->m2m.m2m_dev);
784		video_unregister_device(&gsc->vdev);
785	}
786}
~/ Read-only file preview.