media: rockchip/vpu: Move encoder logic to a common place
authorBoris Brezillon <boris.brezillon@collabora.com>
Tue, 28 May 2019 17:02:24 +0000 (13:02 -0400)
committerMauro Carvalho Chehab <mchehab+samsung@kernel.org>
Wed, 29 May 2019 14:58:59 +0000 (10:58 -0400)
The V4L2/VB2 implementation for the encoder and decoder logic are very
similar, so let's rename rockchip_vpu_enc.c file into
rockchip_vpu_v4l2.c and remove the _enc_ part in objects/functions
exposed in rockchip_vpu_v4l2.h. We also rename the enc_queue_init()
function (in rockchip_vpu_drv.c) queue_init() since it will be used
to initialize both type of queues.

The implementation itself will be patched to support the decoding case
when decoder support is added.

Suggested-by: Ezequiel Garcia <ezequiel@collabora.com>
Signed-off-by: Boris Brezillon <boris.brezillon@collabora.com>
Signed-off-by: Hans Verkuil <hverkuil-cisco@xs4all.nl>
Signed-off-by: Mauro Carvalho Chehab <mchehab+samsung@kernel.org>
drivers/staging/media/rockchip/vpu/Makefile
drivers/staging/media/rockchip/vpu/rockchip_vpu_drv.c
drivers/staging/media/rockchip/vpu/rockchip_vpu_enc.c [deleted file]
drivers/staging/media/rockchip/vpu/rockchip_vpu_v4l2.c [new file with mode: 0644]
drivers/staging/media/rockchip/vpu/rockchip_vpu_v4l2.h

index ae5d143a0bfa220d02beae2dc1a1c8273de4faab..33606391e910e14abf6c07aee2cce1e7020792d1 100644 (file)
@@ -3,7 +3,7 @@ obj-$(CONFIG_VIDEO_ROCKCHIP_VPU) += rockchip-vpu.o
 
 rockchip-vpu-y += \
                rockchip_vpu_drv.o \
-               rockchip_vpu_enc.o \
+               rockchip_vpu_v4l2.o \
                rk3288_vpu_hw.o \
                rk3288_vpu_hw_jpeg_enc.o \
                rk3399_vpu_hw.o \
index 59b72245fb0755963ed0f6218c4ed3d58d07f5e4..ec18578d55d7fc9be425965de4f5895e40b782a0 100644 (file)
@@ -140,7 +140,7 @@ static struct v4l2_m2m_ops vpu_m2m_ops = {
 };
 
 static int
-enc_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
+queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
 {
        struct rockchip_vpu_ctx *ctx = priv;
        int ret;
@@ -148,7 +148,7 @@ enc_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
        src_vq->type = V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE;
        src_vq->io_modes = VB2_MMAP | VB2_DMABUF;
        src_vq->drv_priv = ctx;
-       src_vq->ops = &rockchip_vpu_enc_queue_ops;
+       src_vq->ops = &rockchip_vpu_queue_ops;
        src_vq->mem_ops = &vb2_dma_contig_memops;
 
        /*
@@ -179,7 +179,7 @@ enc_queue_init(void *priv, struct vb2_queue *src_vq, struct vb2_queue *dst_vq)
        dst_vq->type = V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE;
        dst_vq->io_modes = VB2_MMAP | VB2_DMABUF;
        dst_vq->drv_priv = ctx;
-       dst_vq->ops = &rockchip_vpu_enc_queue_ops;
+       dst_vq->ops = &rockchip_vpu_queue_ops;
        dst_vq->mem_ops = &vb2_vmalloc_memops;
        dst_vq->buf_struct_size = sizeof(struct v4l2_m2m_buffer);
        dst_vq->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_COPY;
@@ -260,7 +260,7 @@ static int rockchip_vpu_open(struct file *filp)
        ctx->dev = vpu;
        if (func->id == MEDIA_ENT_F_PROC_VIDEO_ENCODER)
                ctx->fh.m2m_ctx = v4l2_m2m_ctx_init(vpu->m2m_dev, ctx,
-                                                   &enc_queue_init);
+                                                   queue_init);
        else
                ctx->fh.m2m_ctx = ERR_PTR(-ENODEV);
        if (IS_ERR(ctx->fh.m2m_ctx)) {
@@ -273,8 +273,8 @@ static int rockchip_vpu_open(struct file *filp)
        filp->private_data = &ctx->fh;
        v4l2_fh_add(&ctx->fh);
 
-       rockchip_vpu_enc_reset_dst_fmt(vpu, ctx);
-       rockchip_vpu_enc_reset_src_fmt(vpu, ctx);
+       rockchip_vpu_reset_dst_fmt(vpu, ctx);
+       rockchip_vpu_reset_src_fmt(vpu, ctx);
 
        ret = rockchip_vpu_ctrls_setup(vpu, ctx);
        if (ret) {
@@ -487,7 +487,7 @@ static int rockchip_vpu_add_enc_func(struct rockchip_vpu_dev *vpu)
        vfd->v4l2_dev = &vpu->v4l2_dev;
        vfd->vfl_dir = VFL_DIR_M2M;
        vfd->device_caps = V4L2_CAP_STREAMING | V4L2_CAP_VIDEO_M2M_MPLANE;
-       vfd->ioctl_ops = &rockchip_vpu_enc_ioctl_ops;
+       vfd->ioctl_ops = &rockchip_vpu_ioctl_ops;
        snprintf(vfd->name, sizeof(vfd->name), "%s-enc", match->compatible);
 
        vpu->encoder = func;
diff --git a/drivers/staging/media/rockchip/vpu/rockchip_vpu_enc.c b/drivers/staging/media/rockchip/vpu/rockchip_vpu_enc.c
deleted file mode 100644 (file)
index d2b4225..0000000
+++ /dev/null
@@ -1,574 +0,0 @@
-// SPDX-License-Identifier: GPL-2.0
-/*
- * Rockchip VPU codec driver
- *
- * Copyright (C) 2018 Collabora, Ltd.
- * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
- *     Alpha Lin <Alpha.Lin@rock-chips.com>
- *     Jeffy Chen <jeffy.chen@rock-chips.com>
- *
- * Copyright 2018 Google LLC.
- *     Tomasz Figa <tfiga@chromium.org>
- *
- * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
- * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
- */
-
-#include <linux/interrupt.h>
-#include <linux/io.h>
-#include <linux/module.h>
-#include <linux/pm_runtime.h>
-#include <linux/videodev2.h>
-#include <linux/workqueue.h>
-#include <media/v4l2-ctrls.h>
-#include <media/v4l2-event.h>
-#include <media/v4l2-mem2mem.h>
-#include <media/videobuf2-core.h>
-#include <media/videobuf2-dma-sg.h>
-
-#include "rockchip_vpu.h"
-#include "rockchip_vpu_hw.h"
-#include "rockchip_vpu_v4l2.h"
-
-static const struct rockchip_vpu_fmt *
-rockchip_vpu_find_format(struct rockchip_vpu_ctx *ctx, u32 fourcc)
-{
-       struct rockchip_vpu_dev *dev = ctx->dev;
-       const struct rockchip_vpu_fmt *formats;
-       unsigned int num_fmts, i;
-
-       formats = dev->variant->enc_fmts;
-       num_fmts = dev->variant->num_enc_fmts;
-       for (i = 0; i < num_fmts; i++)
-               if (formats[i].fourcc == fourcc)
-                       return &formats[i];
-       return NULL;
-}
-
-static const struct rockchip_vpu_fmt *
-rockchip_vpu_get_default_fmt(struct rockchip_vpu_ctx *ctx, bool bitstream)
-{
-       struct rockchip_vpu_dev *dev = ctx->dev;
-       const struct rockchip_vpu_fmt *formats;
-       unsigned int num_fmts, i;
-
-       formats = dev->variant->enc_fmts;
-       num_fmts = dev->variant->num_enc_fmts;
-       for (i = 0; i < num_fmts; i++) {
-               if (bitstream == (formats[i].codec_mode != RK_VPU_MODE_NONE))
-                       return &formats[i];
-       }
-       return NULL;
-}
-
-static int vidioc_querycap(struct file *file, void *priv,
-                          struct v4l2_capability *cap)
-{
-       struct rockchip_vpu_dev *vpu = video_drvdata(file);
-       struct video_device *vdev = video_devdata(file);
-
-       strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
-       strscpy(cap->card, vdev->name, sizeof(cap->card));
-       snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s",
-                vpu->dev->driver->name);
-       return 0;
-}
-
-static int vidioc_enum_framesizes(struct file *file, void *priv,
-                                 struct v4l2_frmsizeenum *fsize)
-{
-       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
-       const struct rockchip_vpu_fmt *fmt;
-
-       if (fsize->index != 0) {
-               vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
-                         fsize->index);
-               return -EINVAL;
-       }
-
-       fmt = rockchip_vpu_find_format(ctx, fsize->pixel_format);
-       if (!fmt) {
-               vpu_debug(0, "unsupported bitstream format (%08x)\n",
-                         fsize->pixel_format);
-               return -EINVAL;
-       }
-
-       /* This only makes sense for coded formats */
-       if (fmt->codec_mode == RK_VPU_MODE_NONE)
-               return -EINVAL;
-
-       fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
-       fsize->stepwise = fmt->frmsize;
-
-       return 0;
-}
-
-static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
-                                         struct v4l2_fmtdesc *f)
-{
-       struct rockchip_vpu_dev *dev = video_drvdata(file);
-       const struct rockchip_vpu_fmt *fmt;
-       const struct rockchip_vpu_fmt *formats;
-       int num_fmts, i, j = 0;
-
-       formats = dev->variant->enc_fmts;
-       num_fmts = dev->variant->num_enc_fmts;
-       for (i = 0; i < num_fmts; i++) {
-               /* Skip uncompressed formats */
-               if (formats[i].codec_mode == RK_VPU_MODE_NONE)
-                       continue;
-               if (j == f->index) {
-                       fmt = &formats[i];
-                       f->pixelformat = fmt->fourcc;
-                       return 0;
-               }
-               ++j;
-       }
-       return -EINVAL;
-}
-
-static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
-                                         struct v4l2_fmtdesc *f)
-{
-       struct rockchip_vpu_dev *dev = video_drvdata(file);
-       const struct rockchip_vpu_fmt *formats;
-       const struct rockchip_vpu_fmt *fmt;
-       int num_fmts, i, j = 0;
-
-       formats = dev->variant->enc_fmts;
-       num_fmts = dev->variant->num_enc_fmts;
-       for (i = 0; i < num_fmts; i++) {
-               if (formats[i].codec_mode != RK_VPU_MODE_NONE)
-                       continue;
-               if (j == f->index) {
-                       fmt = &formats[i];
-                       f->pixelformat = fmt->fourcc;
-                       return 0;
-               }
-               ++j;
-       }
-       return -EINVAL;
-}
-
-static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
-                                  struct v4l2_format *f)
-{
-       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
-
-       vpu_debug(4, "f->type = %d\n", f->type);
-
-       *pix_mp = ctx->src_fmt;
-
-       return 0;
-}
-
-static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
-                                  struct v4l2_format *f)
-{
-       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
-
-       vpu_debug(4, "f->type = %d\n", f->type);
-
-       *pix_mp = ctx->dst_fmt;
-
-       return 0;
-}
-
-static int
-vidioc_try_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
-{
-       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
-       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-       const struct rockchip_vpu_fmt *fmt;
-
-       vpu_debug(4, "%c%c%c%c\n",
-                 (pix_mp->pixelformat & 0x7f),
-                 (pix_mp->pixelformat >> 8) & 0x7f,
-                 (pix_mp->pixelformat >> 16) & 0x7f,
-                 (pix_mp->pixelformat >> 24) & 0x7f);
-
-       fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
-       if (!fmt) {
-               fmt = rockchip_vpu_get_default_fmt(ctx, true);
-               f->fmt.pix.pixelformat = fmt->fourcc;
-       }
-
-       pix_mp->num_planes = 1;
-       pix_mp->field = V4L2_FIELD_NONE;
-
-       v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
-                                      &fmt->frmsize);
-
-       /*
-        * For compressed formats the application can specify
-        * sizeimage. If the application passes a zero sizeimage,
-        * let's default to the maximum frame size.
-        */
-       if (!pix_mp->plane_fmt[0].sizeimage)
-               pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
-                       pix_mp->width * pix_mp->height * fmt->max_depth;
-       memset(pix_mp->plane_fmt[0].reserved, 0,
-              sizeof(pix_mp->plane_fmt[0].reserved));
-       return 0;
-}
-
-static int
-vidioc_try_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
-{
-       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
-       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-       const struct rockchip_vpu_fmt *fmt;
-       int i;
-
-       vpu_debug(4, "%c%c%c%c\n",
-                 (pix_mp->pixelformat & 0x7f),
-                 (pix_mp->pixelformat >> 8) & 0x7f,
-                 (pix_mp->pixelformat >> 16) & 0x7f,
-                 (pix_mp->pixelformat >> 24) & 0x7f);
-
-       fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
-       if (!fmt) {
-               fmt = rockchip_vpu_get_default_fmt(ctx, false);
-               f->fmt.pix.pixelformat = fmt->fourcc;
-       }
-
-       pix_mp->field = V4L2_FIELD_NONE;
-
-       v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
-                                      &ctx->vpu_dst_fmt->frmsize);
-
-       /* Fill remaining fields */
-       v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
-                           pix_mp->height);
-
-       for (i = 0; i < pix_mp->num_planes; i++) {
-               memset(pix_mp->plane_fmt[i].reserved, 0,
-                      sizeof(pix_mp->plane_fmt[i].reserved));
-       }
-       return 0;
-}
-
-void rockchip_vpu_enc_reset_dst_fmt(struct rockchip_vpu_dev *vpu,
-                                   struct rockchip_vpu_ctx *ctx)
-{
-       struct v4l2_pix_format_mplane *fmt = &ctx->dst_fmt;
-
-       ctx->vpu_dst_fmt = rockchip_vpu_get_default_fmt(ctx, true);
-
-       memset(fmt, 0, sizeof(*fmt));
-
-       fmt->num_planes = 1;
-       v4l2_apply_frmsize_constraints(&fmt->width, &fmt->height,
-                                      &ctx->vpu_dst_fmt->frmsize);
-       fmt->pixelformat = ctx->vpu_dst_fmt->fourcc;
-       fmt->field = V4L2_FIELD_NONE;
-       fmt->colorspace = V4L2_COLORSPACE_JPEG,
-       fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
-       fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
-       fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
-
-       fmt->plane_fmt[0].sizeimage = ctx->vpu_dst_fmt->header_size +
-               fmt->width * fmt->height * ctx->vpu_dst_fmt->max_depth;
-}
-
-void rockchip_vpu_enc_reset_src_fmt(struct rockchip_vpu_dev *vpu,
-                                   struct rockchip_vpu_ctx *ctx)
-{
-       struct v4l2_pix_format_mplane *fmt = &ctx->src_fmt;
-
-       ctx->vpu_src_fmt = rockchip_vpu_get_default_fmt(ctx, false);
-
-       memset(fmt, 0, sizeof(*fmt));
-
-       v4l2_apply_frmsize_constraints(&fmt->width, &fmt->height,
-                                      &ctx->vpu_src_fmt->frmsize);
-       fmt->field = V4L2_FIELD_NONE;
-       fmt->colorspace = V4L2_COLORSPACE_JPEG,
-       fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
-       fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
-       fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
-
-       v4l2_fill_pixfmt_mp(fmt, ctx->vpu_src_fmt->fourcc, fmt->width,
-                           fmt->height);
-}
-
-static int
-vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
-{
-       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
-       struct vb2_queue *vq;
-       int ret;
-
-       /* Change not allowed if queue is streaming. */
-       vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
-       if (vb2_is_streaming(vq))
-               return -EBUSY;
-
-       ret = vidioc_try_fmt_out_mplane(file, priv, f);
-       if (ret)
-               return ret;
-
-       ctx->vpu_src_fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
-       ctx->src_fmt = *pix_mp;
-
-       /* Propagate to the CAPTURE format */
-       ctx->dst_fmt.colorspace = pix_mp->colorspace;
-       ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
-       ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
-       ctx->dst_fmt.quantization = pix_mp->quantization;
-       ctx->dst_fmt.width = pix_mp->width;
-       ctx->dst_fmt.height = pix_mp->height;
-
-       vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
-       vpu_debug(0, "fmt - w: %d, h: %d\n",
-                 pix_mp->width, pix_mp->height);
-       return 0;
-}
-
-static int
-vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
-{
-       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
-       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
-       struct rockchip_vpu_dev *vpu = ctx->dev;
-       struct vb2_queue *vq, *peer_vq;
-       int ret;
-
-       /* Change not allowed if queue is streaming. */
-       vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
-       if (vb2_is_streaming(vq))
-               return -EBUSY;
-
-       /*
-        * Since format change on the CAPTURE queue will reset
-        * the OUTPUT queue, we can't allow doing so
-        * when the OUTPUT queue has buffers allocated.
-        */
-       peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
-                                 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
-       if (vb2_is_busy(peer_vq) &&
-           (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
-            pix_mp->height != ctx->dst_fmt.height ||
-            pix_mp->width != ctx->dst_fmt.width))
-               return -EBUSY;
-
-       ret = vidioc_try_fmt_cap_mplane(file, priv, f);
-       if (ret)
-               return ret;
-
-       ctx->vpu_dst_fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
-       ctx->dst_fmt = *pix_mp;
-
-       vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
-       vpu_debug(0, "fmt - w: %d, h: %d\n",
-                 pix_mp->width, pix_mp->height);
-
-       /*
-        * Current raw format might have become invalid with newly
-        * selected codec, so reset it to default just to be safe and
-        * keep internal driver state sane. User is mandated to set
-        * the raw format again after we return, so we don't need
-        * anything smarter.
-        */
-       rockchip_vpu_enc_reset_src_fmt(vpu, ctx);
-       return 0;
-}
-
-const struct v4l2_ioctl_ops rockchip_vpu_enc_ioctl_ops = {
-       .vidioc_querycap = vidioc_querycap,
-       .vidioc_enum_framesizes = vidioc_enum_framesizes,
-
-       .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
-       .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
-       .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
-       .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
-       .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
-       .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
-       .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
-       .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
-
-       .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
-       .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
-       .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
-       .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
-       .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
-       .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
-       .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
-
-       .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
-       .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
-
-       .vidioc_streamon = v4l2_m2m_ioctl_streamon,
-       .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
-};
-
-static int
-rockchip_vpu_queue_setup(struct vb2_queue *vq,
-                        unsigned int *num_buffers,
-                        unsigned int *num_planes,
-                        unsigned int sizes[],
-                        struct device *alloc_devs[])
-{
-       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vq);
-       struct v4l2_pix_format_mplane *pixfmt;
-       int i;
-
-       switch (vq->type) {
-       case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
-               pixfmt = &ctx->dst_fmt;
-               break;
-       case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
-               pixfmt = &ctx->src_fmt;
-               break;
-       default:
-               vpu_err("invalid queue type: %d\n", vq->type);
-               return -EINVAL;
-       }
-
-       if (*num_planes) {
-               if (*num_planes != pixfmt->num_planes)
-                       return -EINVAL;
-               for (i = 0; i < pixfmt->num_planes; ++i)
-                       if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
-                               return -EINVAL;
-               return 0;
-       }
-
-       *num_planes = pixfmt->num_planes;
-       for (i = 0; i < pixfmt->num_planes; ++i)
-               sizes[i] = pixfmt->plane_fmt[i].sizeimage;
-       return 0;
-}
-
-static int rockchip_vpu_buf_prepare(struct vb2_buffer *vb)
-{
-       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
-       struct vb2_queue *vq = vb->vb2_queue;
-       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vq);
-       struct v4l2_pix_format_mplane *pixfmt;
-       unsigned int sz;
-       int ret = 0;
-       int i;
-
-       switch (vq->type) {
-       case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
-               pixfmt = &ctx->dst_fmt;
-               break;
-       case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
-               pixfmt = &ctx->src_fmt;
-
-               if (vbuf->field == V4L2_FIELD_ANY)
-                       vbuf->field = V4L2_FIELD_NONE;
-               if (vbuf->field != V4L2_FIELD_NONE) {
-                       vpu_debug(4, "field %d not supported\n",
-                                 vbuf->field);
-                       return -EINVAL;
-               }
-               break;
-       default:
-               vpu_err("invalid queue type: %d\n", vq->type);
-               return -EINVAL;
-       }
-
-       for (i = 0; i < pixfmt->num_planes; ++i) {
-               sz = pixfmt->plane_fmt[i].sizeimage;
-               vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
-                         i, vb2_plane_size(vb, i), sz);
-               if (vb2_plane_size(vb, i) < sz) {
-                       vpu_err("plane %d is too small\n", i);
-                       ret = -EINVAL;
-                       break;
-               }
-       }
-
-       return ret;
-}
-
-static void rockchip_vpu_buf_queue(struct vb2_buffer *vb)
-{
-       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
-       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
-
-       v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
-}
-
-static int rockchip_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
-{
-       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(q);
-       enum rockchip_vpu_codec_mode codec_mode;
-       int ret = 0;
-
-       if (V4L2_TYPE_IS_OUTPUT(q->type))
-               ctx->sequence_out = 0;
-       else
-               ctx->sequence_cap = 0;
-
-       /* Set codec_ops for the chosen destination format */
-       codec_mode = ctx->vpu_dst_fmt->codec_mode;
-
-       vpu_debug(4, "Codec mode = %d\n", codec_mode);
-       ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
-
-       if (!V4L2_TYPE_IS_OUTPUT(q->type))
-               if (ctx->codec_ops && ctx->codec_ops->init)
-                       ret = ctx->codec_ops->init(ctx);
-       return ret;
-}
-
-static void rockchip_vpu_stop_streaming(struct vb2_queue *q)
-{
-       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(q);
-
-       if (!V4L2_TYPE_IS_OUTPUT(q->type))
-               if (ctx->codec_ops && ctx->codec_ops->exit)
-                       ctx->codec_ops->exit(ctx);
-
-       /*
-        * The mem2mem framework calls v4l2_m2m_cancel_job before
-        * .stop_streaming, so there isn't any job running and
-        * it is safe to return all the buffers.
-        */
-       for (;;) {
-               struct vb2_v4l2_buffer *vbuf;
-
-               if (V4L2_TYPE_IS_OUTPUT(q->type))
-                       vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
-               else
-                       vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
-               if (!vbuf)
-                       break;
-               v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
-                                          &ctx->ctrl_handler);
-               v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
-       }
-}
-
-static void rockchip_vpu_buf_request_complete(struct vb2_buffer *vb)
-{
-       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
-
-       v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
-}
-
-static int rockchip_vpu_buf_out_validate(struct vb2_buffer *vb)
-{
-       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
-
-       vbuf->field = V4L2_FIELD_NONE;
-       return 0;
-}
-
-const struct vb2_ops rockchip_vpu_enc_queue_ops = {
-       .queue_setup = rockchip_vpu_queue_setup,
-       .buf_prepare = rockchip_vpu_buf_prepare,
-       .buf_queue = rockchip_vpu_buf_queue,
-       .buf_out_validate = rockchip_vpu_buf_out_validate,
-       .buf_request_complete = rockchip_vpu_buf_request_complete,
-       .start_streaming = rockchip_vpu_start_streaming,
-       .stop_streaming = rockchip_vpu_stop_streaming,
-       .wait_prepare = vb2_ops_wait_prepare,
-       .wait_finish = vb2_ops_wait_finish,
-};
diff --git a/drivers/staging/media/rockchip/vpu/rockchip_vpu_v4l2.c b/drivers/staging/media/rockchip/vpu/rockchip_vpu_v4l2.c
new file mode 100644 (file)
index 0000000..3e8f625
--- /dev/null
@@ -0,0 +1,574 @@
+// SPDX-License-Identifier: GPL-2.0
+/*
+ * Rockchip VPU codec driver
+ *
+ * Copyright (C) 2018 Collabora, Ltd.
+ * Copyright (C) 2018 Rockchip Electronics Co., Ltd.
+ *     Alpha Lin <Alpha.Lin@rock-chips.com>
+ *     Jeffy Chen <jeffy.chen@rock-chips.com>
+ *
+ * Copyright 2018 Google LLC.
+ *     Tomasz Figa <tfiga@chromium.org>
+ *
+ * Based on s5p-mfc driver by Samsung Electronics Co., Ltd.
+ * Copyright (C) 2010-2011 Samsung Electronics Co., Ltd.
+ */
+
+#include <linux/interrupt.h>
+#include <linux/io.h>
+#include <linux/module.h>
+#include <linux/pm_runtime.h>
+#include <linux/videodev2.h>
+#include <linux/workqueue.h>
+#include <media/v4l2-ctrls.h>
+#include <media/v4l2-event.h>
+#include <media/v4l2-mem2mem.h>
+#include <media/videobuf2-core.h>
+#include <media/videobuf2-dma-sg.h>
+
+#include "rockchip_vpu.h"
+#include "rockchip_vpu_hw.h"
+#include "rockchip_vpu_v4l2.h"
+
+static const struct rockchip_vpu_fmt *
+rockchip_vpu_find_format(struct rockchip_vpu_ctx *ctx, u32 fourcc)
+{
+       struct rockchip_vpu_dev *dev = ctx->dev;
+       const struct rockchip_vpu_fmt *formats;
+       unsigned int num_fmts, i;
+
+       formats = dev->variant->enc_fmts;
+       num_fmts = dev->variant->num_enc_fmts;
+       for (i = 0; i < num_fmts; i++)
+               if (formats[i].fourcc == fourcc)
+                       return &formats[i];
+       return NULL;
+}
+
+static const struct rockchip_vpu_fmt *
+rockchip_vpu_get_default_fmt(struct rockchip_vpu_ctx *ctx, bool bitstream)
+{
+       struct rockchip_vpu_dev *dev = ctx->dev;
+       const struct rockchip_vpu_fmt *formats;
+       unsigned int num_fmts, i;
+
+       formats = dev->variant->enc_fmts;
+       num_fmts = dev->variant->num_enc_fmts;
+       for (i = 0; i < num_fmts; i++) {
+               if (bitstream == (formats[i].codec_mode != RK_VPU_MODE_NONE))
+                       return &formats[i];
+       }
+       return NULL;
+}
+
+static int vidioc_querycap(struct file *file, void *priv,
+                          struct v4l2_capability *cap)
+{
+       struct rockchip_vpu_dev *vpu = video_drvdata(file);
+       struct video_device *vdev = video_devdata(file);
+
+       strscpy(cap->driver, vpu->dev->driver->name, sizeof(cap->driver));
+       strscpy(cap->card, vdev->name, sizeof(cap->card));
+       snprintf(cap->bus_info, sizeof(cap->bus_info), "platform: %s",
+                vpu->dev->driver->name);
+       return 0;
+}
+
+static int vidioc_enum_framesizes(struct file *file, void *priv,
+                                 struct v4l2_frmsizeenum *fsize)
+{
+       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
+       const struct rockchip_vpu_fmt *fmt;
+
+       if (fsize->index != 0) {
+               vpu_debug(0, "invalid frame size index (expected 0, got %d)\n",
+                         fsize->index);
+               return -EINVAL;
+       }
+
+       fmt = rockchip_vpu_find_format(ctx, fsize->pixel_format);
+       if (!fmt) {
+               vpu_debug(0, "unsupported bitstream format (%08x)\n",
+                         fsize->pixel_format);
+               return -EINVAL;
+       }
+
+       /* This only makes sense for coded formats */
+       if (fmt->codec_mode == RK_VPU_MODE_NONE)
+               return -EINVAL;
+
+       fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
+       fsize->stepwise = fmt->frmsize;
+
+       return 0;
+}
+
+static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *priv,
+                                         struct v4l2_fmtdesc *f)
+{
+       struct rockchip_vpu_dev *dev = video_drvdata(file);
+       const struct rockchip_vpu_fmt *fmt;
+       const struct rockchip_vpu_fmt *formats;
+       int num_fmts, i, j = 0;
+
+       formats = dev->variant->enc_fmts;
+       num_fmts = dev->variant->num_enc_fmts;
+       for (i = 0; i < num_fmts; i++) {
+               /* Skip uncompressed formats */
+               if (formats[i].codec_mode == RK_VPU_MODE_NONE)
+                       continue;
+               if (j == f->index) {
+                       fmt = &formats[i];
+                       f->pixelformat = fmt->fourcc;
+                       return 0;
+               }
+               ++j;
+       }
+       return -EINVAL;
+}
+
+static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *priv,
+                                         struct v4l2_fmtdesc *f)
+{
+       struct rockchip_vpu_dev *dev = video_drvdata(file);
+       const struct rockchip_vpu_fmt *formats;
+       const struct rockchip_vpu_fmt *fmt;
+       int num_fmts, i, j = 0;
+
+       formats = dev->variant->enc_fmts;
+       num_fmts = dev->variant->num_enc_fmts;
+       for (i = 0; i < num_fmts; i++) {
+               if (formats[i].codec_mode != RK_VPU_MODE_NONE)
+                       continue;
+               if (j == f->index) {
+                       fmt = &formats[i];
+                       f->pixelformat = fmt->fourcc;
+                       return 0;
+               }
+               ++j;
+       }
+       return -EINVAL;
+}
+
+static int vidioc_g_fmt_out_mplane(struct file *file, void *priv,
+                                  struct v4l2_format *f)
+{
+       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
+
+       vpu_debug(4, "f->type = %d\n", f->type);
+
+       *pix_mp = ctx->src_fmt;
+
+       return 0;
+}
+
+static int vidioc_g_fmt_cap_mplane(struct file *file, void *priv,
+                                  struct v4l2_format *f)
+{
+       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
+
+       vpu_debug(4, "f->type = %d\n", f->type);
+
+       *pix_mp = ctx->dst_fmt;
+
+       return 0;
+}
+
+static int
+vidioc_try_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
+{
+       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
+       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+       const struct rockchip_vpu_fmt *fmt;
+
+       vpu_debug(4, "%c%c%c%c\n",
+                 (pix_mp->pixelformat & 0x7f),
+                 (pix_mp->pixelformat >> 8) & 0x7f,
+                 (pix_mp->pixelformat >> 16) & 0x7f,
+                 (pix_mp->pixelformat >> 24) & 0x7f);
+
+       fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
+       if (!fmt) {
+               fmt = rockchip_vpu_get_default_fmt(ctx, true);
+               f->fmt.pix.pixelformat = fmt->fourcc;
+       }
+
+       pix_mp->num_planes = 1;
+       pix_mp->field = V4L2_FIELD_NONE;
+
+       v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
+                                      &fmt->frmsize);
+
+       /*
+        * For compressed formats the application can specify
+        * sizeimage. If the application passes a zero sizeimage,
+        * let's default to the maximum frame size.
+        */
+       if (!pix_mp->plane_fmt[0].sizeimage)
+               pix_mp->plane_fmt[0].sizeimage = fmt->header_size +
+                       pix_mp->width * pix_mp->height * fmt->max_depth;
+       memset(pix_mp->plane_fmt[0].reserved, 0,
+              sizeof(pix_mp->plane_fmt[0].reserved));
+       return 0;
+}
+
+static int
+vidioc_try_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
+{
+       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
+       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+       const struct rockchip_vpu_fmt *fmt;
+       int i;
+
+       vpu_debug(4, "%c%c%c%c\n",
+                 (pix_mp->pixelformat & 0x7f),
+                 (pix_mp->pixelformat >> 8) & 0x7f,
+                 (pix_mp->pixelformat >> 16) & 0x7f,
+                 (pix_mp->pixelformat >> 24) & 0x7f);
+
+       fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
+       if (!fmt) {
+               fmt = rockchip_vpu_get_default_fmt(ctx, false);
+               f->fmt.pix.pixelformat = fmt->fourcc;
+       }
+
+       pix_mp->field = V4L2_FIELD_NONE;
+
+       v4l2_apply_frmsize_constraints(&pix_mp->width, &pix_mp->height,
+                                      &ctx->vpu_dst_fmt->frmsize);
+
+       /* Fill remaining fields */
+       v4l2_fill_pixfmt_mp(pix_mp, fmt->fourcc, pix_mp->width,
+                           pix_mp->height);
+
+       for (i = 0; i < pix_mp->num_planes; i++) {
+               memset(pix_mp->plane_fmt[i].reserved, 0,
+                      sizeof(pix_mp->plane_fmt[i].reserved));
+       }
+       return 0;
+}
+
+void rockchip_vpu_reset_dst_fmt(struct rockchip_vpu_dev *vpu,
+                               struct rockchip_vpu_ctx *ctx)
+{
+       struct v4l2_pix_format_mplane *fmt = &ctx->dst_fmt;
+
+       ctx->vpu_dst_fmt = rockchip_vpu_get_default_fmt(ctx, true);
+
+       memset(fmt, 0, sizeof(*fmt));
+
+       fmt->num_planes = 1;
+       v4l2_apply_frmsize_constraints(&fmt->width, &fmt->height,
+                                      &ctx->vpu_dst_fmt->frmsize);
+       fmt->pixelformat = ctx->vpu_dst_fmt->fourcc;
+       fmt->field = V4L2_FIELD_NONE;
+       fmt->colorspace = V4L2_COLORSPACE_JPEG,
+       fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
+       fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
+       fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
+
+       fmt->plane_fmt[0].sizeimage = ctx->vpu_dst_fmt->header_size +
+               fmt->width * fmt->height * ctx->vpu_dst_fmt->max_depth;
+}
+
+void rockchip_vpu_reset_src_fmt(struct rockchip_vpu_dev *vpu,
+                               struct rockchip_vpu_ctx *ctx)
+{
+       struct v4l2_pix_format_mplane *fmt = &ctx->src_fmt;
+
+       ctx->vpu_src_fmt = rockchip_vpu_get_default_fmt(ctx, false);
+
+       memset(fmt, 0, sizeof(*fmt));
+
+       v4l2_apply_frmsize_constraints(&fmt->width, &fmt->height,
+                                      &ctx->vpu_src_fmt->frmsize);
+       fmt->field = V4L2_FIELD_NONE;
+       fmt->colorspace = V4L2_COLORSPACE_JPEG,
+       fmt->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
+       fmt->quantization = V4L2_QUANTIZATION_DEFAULT;
+       fmt->xfer_func = V4L2_XFER_FUNC_DEFAULT;
+
+       v4l2_fill_pixfmt_mp(fmt, ctx->vpu_src_fmt->fourcc, fmt->width,
+                           fmt->height);
+}
+
+static int
+vidioc_s_fmt_out_mplane(struct file *file, void *priv, struct v4l2_format *f)
+{
+       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
+       struct vb2_queue *vq;
+       int ret;
+
+       /* Change not allowed if queue is streaming. */
+       vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
+       if (vb2_is_streaming(vq))
+               return -EBUSY;
+
+       ret = vidioc_try_fmt_out_mplane(file, priv, f);
+       if (ret)
+               return ret;
+
+       ctx->vpu_src_fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
+       ctx->src_fmt = *pix_mp;
+
+       /* Propagate to the CAPTURE format */
+       ctx->dst_fmt.colorspace = pix_mp->colorspace;
+       ctx->dst_fmt.ycbcr_enc = pix_mp->ycbcr_enc;
+       ctx->dst_fmt.xfer_func = pix_mp->xfer_func;
+       ctx->dst_fmt.quantization = pix_mp->quantization;
+       ctx->dst_fmt.width = pix_mp->width;
+       ctx->dst_fmt.height = pix_mp->height;
+
+       vpu_debug(0, "OUTPUT codec mode: %d\n", ctx->vpu_src_fmt->codec_mode);
+       vpu_debug(0, "fmt - w: %d, h: %d\n",
+                 pix_mp->width, pix_mp->height);
+       return 0;
+}
+
+static int
+vidioc_s_fmt_cap_mplane(struct file *file, void *priv, struct v4l2_format *f)
+{
+       struct v4l2_pix_format_mplane *pix_mp = &f->fmt.pix_mp;
+       struct rockchip_vpu_ctx *ctx = fh_to_ctx(priv);
+       struct rockchip_vpu_dev *vpu = ctx->dev;
+       struct vb2_queue *vq, *peer_vq;
+       int ret;
+
+       /* Change not allowed if queue is streaming. */
+       vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx, f->type);
+       if (vb2_is_streaming(vq))
+               return -EBUSY;
+
+       /*
+        * Since format change on the CAPTURE queue will reset
+        * the OUTPUT queue, we can't allow doing so
+        * when the OUTPUT queue has buffers allocated.
+        */
+       peer_vq = v4l2_m2m_get_vq(ctx->fh.m2m_ctx,
+                                 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE);
+       if (vb2_is_busy(peer_vq) &&
+           (pix_mp->pixelformat != ctx->dst_fmt.pixelformat ||
+            pix_mp->height != ctx->dst_fmt.height ||
+            pix_mp->width != ctx->dst_fmt.width))
+               return -EBUSY;
+
+       ret = vidioc_try_fmt_cap_mplane(file, priv, f);
+       if (ret)
+               return ret;
+
+       ctx->vpu_dst_fmt = rockchip_vpu_find_format(ctx, pix_mp->pixelformat);
+       ctx->dst_fmt = *pix_mp;
+
+       vpu_debug(0, "CAPTURE codec mode: %d\n", ctx->vpu_dst_fmt->codec_mode);
+       vpu_debug(0, "fmt - w: %d, h: %d\n",
+                 pix_mp->width, pix_mp->height);
+
+       /*
+        * Current raw format might have become invalid with newly
+        * selected codec, so reset it to default just to be safe and
+        * keep internal driver state sane. User is mandated to set
+        * the raw format again after we return, so we don't need
+        * anything smarter.
+        */
+       rockchip_vpu_reset_src_fmt(vpu, ctx);
+       return 0;
+}
+
+const struct v4l2_ioctl_ops rockchip_vpu_ioctl_ops = {
+       .vidioc_querycap = vidioc_querycap,
+       .vidioc_enum_framesizes = vidioc_enum_framesizes,
+
+       .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt_cap_mplane,
+       .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt_out_mplane,
+       .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt_out_mplane,
+       .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt_cap_mplane,
+       .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt_out_mplane,
+       .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt_cap_mplane,
+       .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
+       .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
+
+       .vidioc_reqbufs = v4l2_m2m_ioctl_reqbufs,
+       .vidioc_querybuf = v4l2_m2m_ioctl_querybuf,
+       .vidioc_qbuf = v4l2_m2m_ioctl_qbuf,
+       .vidioc_dqbuf = v4l2_m2m_ioctl_dqbuf,
+       .vidioc_prepare_buf = v4l2_m2m_ioctl_prepare_buf,
+       .vidioc_create_bufs = v4l2_m2m_ioctl_create_bufs,
+       .vidioc_expbuf = v4l2_m2m_ioctl_expbuf,
+
+       .vidioc_subscribe_event = v4l2_ctrl_subscribe_event,
+       .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
+
+       .vidioc_streamon = v4l2_m2m_ioctl_streamon,
+       .vidioc_streamoff = v4l2_m2m_ioctl_streamoff,
+};
+
+static int
+rockchip_vpu_queue_setup(struct vb2_queue *vq,
+                        unsigned int *num_buffers,
+                        unsigned int *num_planes,
+                        unsigned int sizes[],
+                        struct device *alloc_devs[])
+{
+       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vq);
+       struct v4l2_pix_format_mplane *pixfmt;
+       int i;
+
+       switch (vq->type) {
+       case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
+               pixfmt = &ctx->dst_fmt;
+               break;
+       case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
+               pixfmt = &ctx->src_fmt;
+               break;
+       default:
+               vpu_err("invalid queue type: %d\n", vq->type);
+               return -EINVAL;
+       }
+
+       if (*num_planes) {
+               if (*num_planes != pixfmt->num_planes)
+                       return -EINVAL;
+               for (i = 0; i < pixfmt->num_planes; ++i)
+                       if (sizes[i] < pixfmt->plane_fmt[i].sizeimage)
+                               return -EINVAL;
+               return 0;
+       }
+
+       *num_planes = pixfmt->num_planes;
+       for (i = 0; i < pixfmt->num_planes; ++i)
+               sizes[i] = pixfmt->plane_fmt[i].sizeimage;
+       return 0;
+}
+
+static int rockchip_vpu_buf_prepare(struct vb2_buffer *vb)
+{
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+       struct vb2_queue *vq = vb->vb2_queue;
+       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vq);
+       struct v4l2_pix_format_mplane *pixfmt;
+       unsigned int sz;
+       int ret = 0;
+       int i;
+
+       switch (vq->type) {
+       case V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE:
+               pixfmt = &ctx->dst_fmt;
+               break;
+       case V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE:
+               pixfmt = &ctx->src_fmt;
+
+               if (vbuf->field == V4L2_FIELD_ANY)
+                       vbuf->field = V4L2_FIELD_NONE;
+               if (vbuf->field != V4L2_FIELD_NONE) {
+                       vpu_debug(4, "field %d not supported\n",
+                                 vbuf->field);
+                       return -EINVAL;
+               }
+               break;
+       default:
+               vpu_err("invalid queue type: %d\n", vq->type);
+               return -EINVAL;
+       }
+
+       for (i = 0; i < pixfmt->num_planes; ++i) {
+               sz = pixfmt->plane_fmt[i].sizeimage;
+               vpu_debug(4, "plane %d size: %ld, sizeimage: %u\n",
+                         i, vb2_plane_size(vb, i), sz);
+               if (vb2_plane_size(vb, i) < sz) {
+                       vpu_err("plane %d is too small\n", i);
+                       ret = -EINVAL;
+                       break;
+               }
+       }
+
+       return ret;
+}
+
+static void rockchip_vpu_buf_queue(struct vb2_buffer *vb)
+{
+       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+
+       v4l2_m2m_buf_queue(ctx->fh.m2m_ctx, vbuf);
+}
+
+static int rockchip_vpu_start_streaming(struct vb2_queue *q, unsigned int count)
+{
+       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(q);
+       enum rockchip_vpu_codec_mode codec_mode;
+       int ret = 0;
+
+       if (V4L2_TYPE_IS_OUTPUT(q->type))
+               ctx->sequence_out = 0;
+       else
+               ctx->sequence_cap = 0;
+
+       /* Set codec_ops for the chosen destination format */
+       codec_mode = ctx->vpu_dst_fmt->codec_mode;
+
+       vpu_debug(4, "Codec mode = %d\n", codec_mode);
+       ctx->codec_ops = &ctx->dev->variant->codec_ops[codec_mode];
+
+       if (!V4L2_TYPE_IS_OUTPUT(q->type))
+               if (ctx->codec_ops && ctx->codec_ops->init)
+                       ret = ctx->codec_ops->init(ctx);
+       return ret;
+}
+
+static void rockchip_vpu_stop_streaming(struct vb2_queue *q)
+{
+       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(q);
+
+       if (!V4L2_TYPE_IS_OUTPUT(q->type))
+               if (ctx->codec_ops && ctx->codec_ops->exit)
+                       ctx->codec_ops->exit(ctx);
+
+       /*
+        * The mem2mem framework calls v4l2_m2m_cancel_job before
+        * .stop_streaming, so there isn't any job running and
+        * it is safe to return all the buffers.
+        */
+       for (;;) {
+               struct vb2_v4l2_buffer *vbuf;
+
+               if (V4L2_TYPE_IS_OUTPUT(q->type))
+                       vbuf = v4l2_m2m_src_buf_remove(ctx->fh.m2m_ctx);
+               else
+                       vbuf = v4l2_m2m_dst_buf_remove(ctx->fh.m2m_ctx);
+               if (!vbuf)
+                       break;
+               v4l2_ctrl_request_complete(vbuf->vb2_buf.req_obj.req,
+                                          &ctx->ctrl_handler);
+               v4l2_m2m_buf_done(vbuf, VB2_BUF_STATE_ERROR);
+       }
+}
+
+static void rockchip_vpu_buf_request_complete(struct vb2_buffer *vb)
+{
+       struct rockchip_vpu_ctx *ctx = vb2_get_drv_priv(vb->vb2_queue);
+
+       v4l2_ctrl_request_complete(vb->req_obj.req, &ctx->ctrl_handler);
+}
+
+static int rockchip_vpu_buf_out_validate(struct vb2_buffer *vb)
+{
+       struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
+
+       vbuf->field = V4L2_FIELD_NONE;
+       return 0;
+}
+
+const struct vb2_ops rockchip_vpu_queue_ops = {
+       .queue_setup = rockchip_vpu_queue_setup,
+       .buf_prepare = rockchip_vpu_buf_prepare,
+       .buf_queue = rockchip_vpu_buf_queue,
+       .buf_out_validate = rockchip_vpu_buf_out_validate,
+       .buf_request_complete = rockchip_vpu_buf_request_complete,
+       .start_streaming = rockchip_vpu_start_streaming,
+       .stop_streaming = rockchip_vpu_stop_streaming,
+       .wait_prepare = vb2_ops_wait_prepare,
+       .wait_finish = vb2_ops_wait_finish,
+};
index 50ad40dfb4f41ecab3a69d412681624393925738..816bd3988218fd27121da637d0466c07ebe88efb 100644 (file)
 
 #include "rockchip_vpu.h"
 
-extern const struct v4l2_ioctl_ops rockchip_vpu_enc_ioctl_ops;
-extern const struct vb2_ops rockchip_vpu_enc_queue_ops;
+extern const struct v4l2_ioctl_ops rockchip_vpu_ioctl_ops;
+extern const struct vb2_ops rockchip_vpu_queue_ops;
 
-void rockchip_vpu_enc_reset_src_fmt(struct rockchip_vpu_dev *vpu,
-                                   struct rockchip_vpu_ctx *ctx);
-void rockchip_vpu_enc_reset_dst_fmt(struct rockchip_vpu_dev *vpu,
-                                   struct rockchip_vpu_ctx *ctx);
+void rockchip_vpu_reset_src_fmt(struct rockchip_vpu_dev *vpu,
+                               struct rockchip_vpu_ctx *ctx);
+void rockchip_vpu_reset_dst_fmt(struct rockchip_vpu_dev *vpu,
+                               struct rockchip_vpu_ctx *ctx);
 
 #endif /* ROCKCHIP_VPU_V4L2_H_ */