linux/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
<<
>>
Prefs
   1/*
   2 * linux/drivers/media/platform/s5p-mfc/s5p_mfc_dec.c
   3 *
   4 * Copyright (C) 2011 Samsung Electronics Co., Ltd.
   5 *              http://www.samsung.com/
   6 * Kamil Debski, <k.debski@samsung.com>
   7 *
   8 * This program is free software; you can redistribute it and/or modify
   9 * it under the terms of the GNU General Public License as published by
  10 * the Free Software Foundation; either version 2 of the License, or
  11 * (at your option) any later version.
  12 */
  13
  14#include <linux/clk.h>
  15#include <linux/interrupt.h>
  16#include <linux/io.h>
  17#include <linux/module.h>
  18#include <linux/platform_device.h>
  19#include <linux/sched.h>
  20#include <linux/slab.h>
  21#include <linux/version.h>
  22#include <linux/videodev2.h>
  23#include <linux/workqueue.h>
  24#include <media/v4l2-ctrls.h>
  25#include <media/v4l2-event.h>
  26#include <media/videobuf2-core.h>
  27#include "s5p_mfc_common.h"
  28#include "s5p_mfc_debug.h"
  29#include "s5p_mfc_dec.h"
  30#include "s5p_mfc_intr.h"
  31#include "s5p_mfc_opr.h"
  32#include "s5p_mfc_pm.h"
  33
  34#define DEF_SRC_FMT_DEC V4L2_PIX_FMT_H264
  35#define DEF_DST_FMT_DEC V4L2_PIX_FMT_NV12MT_16X16
  36
  37static struct s5p_mfc_fmt formats[] = {
  38        {
  39                .name           = "4:2:0 2 Planes 16x16 Tiles",
  40                .fourcc         = V4L2_PIX_FMT_NV12MT_16X16,
  41                .codec_mode     = S5P_MFC_CODEC_NONE,
  42                .type           = MFC_FMT_RAW,
  43                .num_planes     = 2,
  44        },
  45        {
  46                .name           = "4:2:0 2 Planes 64x32 Tiles",
  47                .fourcc         = V4L2_PIX_FMT_NV12MT,
  48                .codec_mode     = S5P_MFC_CODEC_NONE,
  49                .type           = MFC_FMT_RAW,
  50                .num_planes     = 2,
  51        },
  52        {
  53                .name           = "4:2:0 2 Planes Y/CbCr",
  54                .fourcc         = V4L2_PIX_FMT_NV12M,
  55                .codec_mode     = S5P_MFC_CODEC_NONE,
  56                .type           = MFC_FMT_RAW,
  57                .num_planes     = 2,
  58        },
  59        {
  60                .name           = "4:2:0 2 Planes Y/CrCb",
  61                .fourcc         = V4L2_PIX_FMT_NV21M,
  62                .codec_mode     = S5P_MFC_CODEC_NONE,
  63                .type           = MFC_FMT_RAW,
  64                .num_planes     = 2,
  65        },
  66        {
  67                .name           = "H264 Encoded Stream",
  68                .fourcc         = V4L2_PIX_FMT_H264,
  69                .codec_mode     = S5P_MFC_CODEC_H264_DEC,
  70                .type           = MFC_FMT_DEC,
  71                .num_planes     = 1,
  72        },
  73        {
  74                .name           = "H264/MVC Encoded Stream",
  75                .fourcc         = V4L2_PIX_FMT_H264_MVC,
  76                .codec_mode     = S5P_MFC_CODEC_H264_MVC_DEC,
  77                .type           = MFC_FMT_DEC,
  78                .num_planes     = 1,
  79        },
  80        {
  81                .name           = "H263 Encoded Stream",
  82                .fourcc         = V4L2_PIX_FMT_H263,
  83                .codec_mode     = S5P_MFC_CODEC_H263_DEC,
  84                .type           = MFC_FMT_DEC,
  85                .num_planes     = 1,
  86        },
  87        {
  88                .name           = "MPEG1 Encoded Stream",
  89                .fourcc         = V4L2_PIX_FMT_MPEG1,
  90                .codec_mode     = S5P_MFC_CODEC_MPEG2_DEC,
  91                .type           = MFC_FMT_DEC,
  92                .num_planes     = 1,
  93        },
  94        {
  95                .name           = "MPEG2 Encoded Stream",
  96                .fourcc         = V4L2_PIX_FMT_MPEG2,
  97                .codec_mode     = S5P_MFC_CODEC_MPEG2_DEC,
  98                .type           = MFC_FMT_DEC,
  99                .num_planes     = 1,
 100        },
 101        {
 102                .name           = "MPEG4 Encoded Stream",
 103                .fourcc         = V4L2_PIX_FMT_MPEG4,
 104                .codec_mode     = S5P_MFC_CODEC_MPEG4_DEC,
 105                .type           = MFC_FMT_DEC,
 106                .num_planes     = 1,
 107        },
 108        {
 109                .name           = "XviD Encoded Stream",
 110                .fourcc         = V4L2_PIX_FMT_XVID,
 111                .codec_mode     = S5P_MFC_CODEC_MPEG4_DEC,
 112                .type           = MFC_FMT_DEC,
 113                .num_planes     = 1,
 114        },
 115        {
 116                .name           = "VC1 Encoded Stream",
 117                .fourcc         = V4L2_PIX_FMT_VC1_ANNEX_G,
 118                .codec_mode     = S5P_MFC_CODEC_VC1_DEC,
 119                .type           = MFC_FMT_DEC,
 120                .num_planes     = 1,
 121        },
 122        {
 123                .name           = "VC1 RCV Encoded Stream",
 124                .fourcc         = V4L2_PIX_FMT_VC1_ANNEX_L,
 125                .codec_mode     = S5P_MFC_CODEC_VC1RCV_DEC,
 126                .type           = MFC_FMT_DEC,
 127                .num_planes     = 1,
 128        },
 129        {
 130                .name           = "VP8 Encoded Stream",
 131                .fourcc         = V4L2_PIX_FMT_VP8,
 132                .codec_mode     = S5P_MFC_CODEC_VP8_DEC,
 133                .type           = MFC_FMT_DEC,
 134                .num_planes     = 1,
 135        },
 136};
 137
 138#define NUM_FORMATS ARRAY_SIZE(formats)
 139
 140/* Find selected format description */
 141static struct s5p_mfc_fmt *find_format(struct v4l2_format *f, unsigned int t)
 142{
 143        unsigned int i;
 144
 145        for (i = 0; i < NUM_FORMATS; i++) {
 146                if (formats[i].fourcc == f->fmt.pix_mp.pixelformat &&
 147                    formats[i].type == t)
 148                        return &formats[i];
 149        }
 150        return NULL;
 151}
 152
 153static struct mfc_control controls[] = {
 154        {
 155                .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY,
 156                .type = V4L2_CTRL_TYPE_INTEGER,
 157                .name = "H264 Display Delay",
 158                .minimum = 0,
 159                .maximum = 16383,
 160                .step = 1,
 161                .default_value = 0,
 162        },
 163        {
 164                .id = V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE,
 165                .type = V4L2_CTRL_TYPE_BOOLEAN,
 166                .name = "H264 Display Delay Enable",
 167                .minimum = 0,
 168                .maximum = 1,
 169                .step = 1,
 170                .default_value = 0,
 171        },
 172        {
 173                .id = V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER,
 174                .type = V4L2_CTRL_TYPE_BOOLEAN,
 175                .name = "Mpeg4 Loop Filter Enable",
 176                .minimum = 0,
 177                .maximum = 1,
 178                .step = 1,
 179                .default_value = 0,
 180        },
 181        {
 182                .id = V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE,
 183                .type = V4L2_CTRL_TYPE_BOOLEAN,
 184                .name = "Slice Interface Enable",
 185                .minimum = 0,
 186                .maximum = 1,
 187                .step = 1,
 188                .default_value = 0,
 189        },
 190        {
 191                .id = V4L2_CID_MIN_BUFFERS_FOR_CAPTURE,
 192                .type = V4L2_CTRL_TYPE_INTEGER,
 193                .name = "Minimum number of cap bufs",
 194                .minimum = 1,
 195                .maximum = 32,
 196                .step = 1,
 197                .default_value = 1,
 198                .is_volatile = 1,
 199        },
 200};
 201
 202#define NUM_CTRLS ARRAY_SIZE(controls)
 203
 204/* Check whether a context should be run on hardware */
 205static int s5p_mfc_ctx_ready(struct s5p_mfc_ctx *ctx)
 206{
 207        /* Context is to parse header */
 208        if (ctx->src_queue_cnt >= 1 && ctx->state == MFCINST_GOT_INST)
 209                return 1;
 210        /* Context is to decode a frame */
 211        if (ctx->src_queue_cnt >= 1 &&
 212            ctx->state == MFCINST_RUNNING &&
 213            ctx->dst_queue_cnt >= ctx->pb_count)
 214                return 1;
 215        /* Context is to return last frame */
 216        if (ctx->state == MFCINST_FINISHING &&
 217            ctx->dst_queue_cnt >= ctx->pb_count)
 218                return 1;
 219        /* Context is to set buffers */
 220        if (ctx->src_queue_cnt >= 1 &&
 221            ctx->state == MFCINST_HEAD_PARSED &&
 222            ctx->capture_state == QUEUE_BUFS_MMAPED)
 223                return 1;
 224        /* Resolution change */
 225        if ((ctx->state == MFCINST_RES_CHANGE_INIT ||
 226                ctx->state == MFCINST_RES_CHANGE_FLUSH) &&
 227                ctx->dst_queue_cnt >= ctx->pb_count)
 228                return 1;
 229        if (ctx->state == MFCINST_RES_CHANGE_END &&
 230                ctx->src_queue_cnt >= 1)
 231                return 1;
 232        mfc_debug(2, "ctx is not ready\n");
 233        return 0;
 234}
 235
 236static struct s5p_mfc_codec_ops decoder_codec_ops = {
 237        .pre_seq_start          = NULL,
 238        .post_seq_start         = NULL,
 239        .pre_frame_start        = NULL,
 240        .post_frame_start       = NULL,
 241};
 242
 243/* Query capabilities of the device */
 244static int vidioc_querycap(struct file *file, void *priv,
 245                           struct v4l2_capability *cap)
 246{
 247        struct s5p_mfc_dev *dev = video_drvdata(file);
 248
 249        strncpy(cap->driver, dev->plat_dev->name, sizeof(cap->driver) - 1);
 250        strncpy(cap->card, dev->plat_dev->name, sizeof(cap->card) - 1);
 251        cap->bus_info[0] = 0;
 252        cap->version = KERNEL_VERSION(1, 0, 0);
 253        /*
 254         * This is only a mem-to-mem video device. The capture and output
 255         * device capability flags are left only for backward compatibility
 256         * and are scheduled for removal.
 257         */
 258        cap->capabilities = V4L2_CAP_VIDEO_M2M_MPLANE | V4L2_CAP_STREAMING |
 259                            V4L2_CAP_VIDEO_CAPTURE_MPLANE |
 260                            V4L2_CAP_VIDEO_OUTPUT_MPLANE;
 261        return 0;
 262}
 263
 264/* Enumerate format */
 265static int vidioc_enum_fmt(struct v4l2_fmtdesc *f, bool mplane, bool out)
 266{
 267        struct s5p_mfc_fmt *fmt;
 268        int i, j = 0;
 269
 270        for (i = 0; i < ARRAY_SIZE(formats); ++i) {
 271                if (mplane && formats[i].num_planes == 1)
 272                        continue;
 273                else if (!mplane && formats[i].num_planes > 1)
 274                        continue;
 275                if (out && formats[i].type != MFC_FMT_DEC)
 276                        continue;
 277                else if (!out && formats[i].type != MFC_FMT_RAW)
 278                        continue;
 279
 280                if (j == f->index)
 281                        break;
 282                ++j;
 283        }
 284        if (i == ARRAY_SIZE(formats))
 285                return -EINVAL;
 286        fmt = &formats[i];
 287        strlcpy(f->description, fmt->name, sizeof(f->description));
 288        f->pixelformat = fmt->fourcc;
 289        return 0;
 290}
 291
 292static int vidioc_enum_fmt_vid_cap(struct file *file, void *pirv,
 293                                                        struct v4l2_fmtdesc *f)
 294{
 295        return vidioc_enum_fmt(f, false, false);
 296}
 297
 298static int vidioc_enum_fmt_vid_cap_mplane(struct file *file, void *pirv,
 299                                                        struct v4l2_fmtdesc *f)
 300{
 301        return vidioc_enum_fmt(f, true, false);
 302}
 303
 304static int vidioc_enum_fmt_vid_out(struct file *file, void *prov,
 305                                                        struct v4l2_fmtdesc *f)
 306{
 307        return vidioc_enum_fmt(f, false, true);
 308}
 309
 310static int vidioc_enum_fmt_vid_out_mplane(struct file *file, void *prov,
 311                                                        struct v4l2_fmtdesc *f)
 312{
 313        return vidioc_enum_fmt(f, true, true);
 314}
 315
 316/* Get format */
 317static int vidioc_g_fmt(struct file *file, void *priv, struct v4l2_format *f)
 318{
 319        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 320        struct v4l2_pix_format_mplane *pix_mp;
 321
 322        mfc_debug_enter();
 323        pix_mp = &f->fmt.pix_mp;
 324        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 325            (ctx->state == MFCINST_GOT_INST || ctx->state ==
 326                                                MFCINST_RES_CHANGE_END)) {
 327                /* If the MFC is parsing the header,
 328                 * so wait until it is finished */
 329                s5p_mfc_clean_ctx_int_flags(ctx);
 330                s5p_mfc_wait_for_done_ctx(ctx, S5P_MFC_R2H_CMD_SEQ_DONE_RET,
 331                                                                        0);
 332        }
 333        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE &&
 334            ctx->state >= MFCINST_HEAD_PARSED &&
 335            ctx->state < MFCINST_ABORT) {
 336                /* This is run on CAPTURE (decode output) */
 337                /* Width and height are set to the dimensions
 338                   of the movie, the buffer is bigger and
 339                   further processing stages should crop to this
 340                   rectangle. */
 341                pix_mp->width = ctx->buf_width;
 342                pix_mp->height = ctx->buf_height;
 343                pix_mp->field = V4L2_FIELD_NONE;
 344                pix_mp->num_planes = 2;
 345                /* Set pixelformat to the format in which MFC
 346                   outputs the decoded frame */
 347                pix_mp->pixelformat = V4L2_PIX_FMT_NV12MT;
 348                pix_mp->plane_fmt[0].bytesperline = ctx->buf_width;
 349                pix_mp->plane_fmt[0].sizeimage = ctx->luma_size;
 350                pix_mp->plane_fmt[1].bytesperline = ctx->buf_width;
 351                pix_mp->plane_fmt[1].sizeimage = ctx->chroma_size;
 352        } else if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 353                /* This is run on OUTPUT
 354                   The buffer contains compressed image
 355                   so width and height have no meaning */
 356                pix_mp->width = 0;
 357                pix_mp->height = 0;
 358                pix_mp->field = V4L2_FIELD_NONE;
 359                pix_mp->plane_fmt[0].bytesperline = ctx->dec_src_buf_size;
 360                pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size;
 361                pix_mp->pixelformat = ctx->src_fmt->fourcc;
 362                pix_mp->num_planes = ctx->src_fmt->num_planes;
 363        } else {
 364                mfc_err("Format could not be read\n");
 365                mfc_debug(2, "%s-- with error\n", __func__);
 366                return -EINVAL;
 367        }
 368        mfc_debug_leave();
 369        return 0;
 370}
 371
 372/* Try format */
 373static int vidioc_try_fmt(struct file *file, void *priv, struct v4l2_format *f)
 374{
 375        struct s5p_mfc_dev *dev = video_drvdata(file);
 376        struct s5p_mfc_fmt *fmt;
 377
 378        mfc_debug(2, "Type is %d\n", f->type);
 379        if (f->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 380                fmt = find_format(f, MFC_FMT_DEC);
 381                if (!fmt) {
 382                        mfc_err("Unsupported format for source.\n");
 383                        return -EINVAL;
 384                }
 385                if (!IS_MFCV6(dev) && (fmt->fourcc == V4L2_PIX_FMT_VP8)) {
 386                        mfc_err("Not supported format.\n");
 387                        return -EINVAL;
 388                }
 389        } else if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 390                fmt = find_format(f, MFC_FMT_RAW);
 391                if (!fmt) {
 392                        mfc_err("Unsupported format for destination.\n");
 393                        return -EINVAL;
 394                }
 395                if (IS_MFCV6(dev) && (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
 396                        mfc_err("Not supported format.\n");
 397                        return -EINVAL;
 398                } else if (!IS_MFCV6(dev) &&
 399                                (fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
 400                        mfc_err("Not supported format.\n");
 401                        return -EINVAL;
 402                }
 403        }
 404
 405        return 0;
 406}
 407
 408/* Set format */
 409static int vidioc_s_fmt(struct file *file, void *priv, struct v4l2_format *f)
 410{
 411        struct s5p_mfc_dev *dev = video_drvdata(file);
 412        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 413        int ret = 0;
 414        struct s5p_mfc_fmt *fmt;
 415        struct v4l2_pix_format_mplane *pix_mp;
 416
 417        mfc_debug_enter();
 418        ret = vidioc_try_fmt(file, priv, f);
 419        pix_mp = &f->fmt.pix_mp;
 420        if (ret)
 421                return ret;
 422        if (ctx->vq_src.streaming || ctx->vq_dst.streaming) {
 423                v4l2_err(&dev->v4l2_dev, "%s queue busy\n", __func__);
 424                ret = -EBUSY;
 425                goto out;
 426        }
 427        if (f->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 428                fmt = find_format(f, MFC_FMT_RAW);
 429                if (!fmt) {
 430                        mfc_err("Unsupported format for source.\n");
 431                        return -EINVAL;
 432                }
 433                if (!IS_MFCV6(dev) && (fmt->fourcc != V4L2_PIX_FMT_NV12MT)) {
 434                        mfc_err("Not supported format.\n");
 435                        return -EINVAL;
 436                } else if (IS_MFCV6(dev) &&
 437                                (fmt->fourcc == V4L2_PIX_FMT_NV12MT)) {
 438                        mfc_err("Not supported format.\n");
 439                        return -EINVAL;
 440                }
 441                ctx->dst_fmt = fmt;
 442                mfc_debug_leave();
 443                return ret;
 444        } else if (f->type != V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 445                mfc_err("Wrong type error for S_FMT : %d", f->type);
 446                return -EINVAL;
 447        }
 448        fmt = find_format(f, MFC_FMT_DEC);
 449        if (!fmt || fmt->codec_mode == S5P_MFC_CODEC_NONE) {
 450                mfc_err("Unknown codec\n");
 451                ret = -EINVAL;
 452                goto out;
 453        }
 454        if (fmt->type != MFC_FMT_DEC) {
 455                mfc_err("Wrong format selected, you should choose "
 456                                        "format for decoding\n");
 457                ret = -EINVAL;
 458                goto out;
 459        }
 460        if (!IS_MFCV6(dev) && (fmt->fourcc == V4L2_PIX_FMT_VP8)) {
 461                mfc_err("Not supported format.\n");
 462                return -EINVAL;
 463        }
 464        ctx->src_fmt = fmt;
 465        ctx->codec_mode = fmt->codec_mode;
 466        mfc_debug(2, "The codec number is: %d\n", ctx->codec_mode);
 467        pix_mp->height = 0;
 468        pix_mp->width = 0;
 469        if (pix_mp->plane_fmt[0].sizeimage)
 470                ctx->dec_src_buf_size = pix_mp->plane_fmt[0].sizeimage;
 471        else
 472                pix_mp->plane_fmt[0].sizeimage = ctx->dec_src_buf_size =
 473                                                                DEF_CPB_SIZE;
 474        pix_mp->plane_fmt[0].bytesperline = 0;
 475        ctx->state = MFCINST_INIT;
 476out:
 477        mfc_debug_leave();
 478        return ret;
 479}
 480
 481/* Reqeust buffers */
 482static int vidioc_reqbufs(struct file *file, void *priv,
 483                                          struct v4l2_requestbuffers *reqbufs)
 484{
 485        struct s5p_mfc_dev *dev = video_drvdata(file);
 486        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 487        int ret = 0;
 488
 489        if (reqbufs->memory != V4L2_MEMORY_MMAP) {
 490                mfc_err("Only V4L2_MEMORY_MAP is supported\n");
 491                return -EINVAL;
 492        }
 493        if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 494                /* Can only request buffers after an instance has been opened.*/
 495                if (ctx->state == MFCINST_INIT) {
 496                        ctx->src_bufs_cnt = 0;
 497                        if (reqbufs->count == 0) {
 498                                mfc_debug(2, "Freeing buffers\n");
 499                                s5p_mfc_clock_on();
 500                                ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
 501                                s5p_mfc_clock_off();
 502                                return ret;
 503                        }
 504                        /* Decoding */
 505                        if (ctx->output_state != QUEUE_FREE) {
 506                                mfc_err("Bufs have already been requested\n");
 507                                return -EINVAL;
 508                        }
 509                        s5p_mfc_clock_on();
 510                        ret = vb2_reqbufs(&ctx->vq_src, reqbufs);
 511                        s5p_mfc_clock_off();
 512                        if (ret) {
 513                                mfc_err("vb2_reqbufs on output failed\n");
 514                                return ret;
 515                        }
 516                        mfc_debug(2, "vb2_reqbufs: %d\n", ret);
 517                        ctx->output_state = QUEUE_BUFS_REQUESTED;
 518                }
 519        } else if (reqbufs->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 520                ctx->dst_bufs_cnt = 0;
 521                if (reqbufs->count == 0) {
 522                        mfc_debug(2, "Freeing buffers\n");
 523                        s5p_mfc_clock_on();
 524                        ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
 525                        s5p_mfc_clock_off();
 526                        return ret;
 527                }
 528                if (ctx->capture_state != QUEUE_FREE) {
 529                        mfc_err("Bufs have already been requested\n");
 530                        return -EINVAL;
 531                }
 532                ctx->capture_state = QUEUE_BUFS_REQUESTED;
 533                s5p_mfc_clock_on();
 534                ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
 535                s5p_mfc_clock_off();
 536                if (ret) {
 537                        mfc_err("vb2_reqbufs on capture failed\n");
 538                        return ret;
 539                }
 540                if (reqbufs->count < ctx->pb_count) {
 541                        mfc_err("Not enough buffers allocated\n");
 542                        reqbufs->count = 0;
 543                        s5p_mfc_clock_on();
 544                        ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
 545                        s5p_mfc_clock_off();
 546                        return -ENOMEM;
 547                }
 548                ctx->total_dpb_count = reqbufs->count;
 549                ret = s5p_mfc_hw_call(dev->mfc_ops, alloc_codec_buffers, ctx);
 550                if (ret) {
 551                        mfc_err("Failed to allocate decoding buffers\n");
 552                        reqbufs->count = 0;
 553                        s5p_mfc_clock_on();
 554                        ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
 555                        s5p_mfc_clock_off();
 556                        return -ENOMEM;
 557                }
 558                if (ctx->dst_bufs_cnt == ctx->total_dpb_count) {
 559                        ctx->capture_state = QUEUE_BUFS_MMAPED;
 560                } else {
 561                        mfc_err("Not all buffers passed to buf_init\n");
 562                        reqbufs->count = 0;
 563                        s5p_mfc_clock_on();
 564                        ret = vb2_reqbufs(&ctx->vq_dst, reqbufs);
 565                        s5p_mfc_hw_call(dev->mfc_ops, release_codec_buffers,
 566                                        ctx);
 567                        s5p_mfc_clock_off();
 568                        return -ENOMEM;
 569                }
 570                if (s5p_mfc_ctx_ready(ctx))
 571                        set_work_bit_irqsave(ctx);
 572                s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
 573                s5p_mfc_wait_for_done_ctx(ctx,
 574                                        S5P_MFC_R2H_CMD_INIT_BUFFERS_RET, 0);
 575        }
 576        return ret;
 577}
 578
 579/* Query buffer */
 580static int vidioc_querybuf(struct file *file, void *priv,
 581                                                   struct v4l2_buffer *buf)
 582{
 583        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 584        int ret;
 585        int i;
 586
 587        if (buf->memory != V4L2_MEMORY_MMAP) {
 588                mfc_err("Only mmaped buffers can be used\n");
 589                return -EINVAL;
 590        }
 591        mfc_debug(2, "State: %d, buf->type: %d\n", ctx->state, buf->type);
 592        if (ctx->state == MFCINST_INIT &&
 593                        buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 594                ret = vb2_querybuf(&ctx->vq_src, buf);
 595        } else if (ctx->state == MFCINST_RUNNING &&
 596                        buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 597                ret = vb2_querybuf(&ctx->vq_dst, buf);
 598                for (i = 0; i < buf->length; i++)
 599                        buf->m.planes[i].m.mem_offset += DST_QUEUE_OFF_BASE;
 600        } else {
 601                mfc_err("vidioc_querybuf called in an inappropriate state\n");
 602                ret = -EINVAL;
 603        }
 604        mfc_debug_leave();
 605        return ret;
 606}
 607
 608/* Queue a buffer */
 609static int vidioc_qbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 610{
 611        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 612
 613        if (ctx->state == MFCINST_ERROR) {
 614                mfc_err("Call on QBUF after unrecoverable error\n");
 615                return -EIO;
 616        }
 617        if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 618                return vb2_qbuf(&ctx->vq_src, buf);
 619        else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 620                return vb2_qbuf(&ctx->vq_dst, buf);
 621        return -EINVAL;
 622}
 623
 624/* Dequeue a buffer */
 625static int vidioc_dqbuf(struct file *file, void *priv, struct v4l2_buffer *buf)
 626{
 627        const struct v4l2_event ev = {
 628                .type = V4L2_EVENT_EOS
 629        };
 630        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 631        int ret;
 632
 633        if (ctx->state == MFCINST_ERROR) {
 634                mfc_err("Call on DQBUF after unrecoverable error\n");
 635                return -EIO;
 636        }
 637        if (buf->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 638                ret = vb2_dqbuf(&ctx->vq_src, buf, file->f_flags & O_NONBLOCK);
 639        else if (buf->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 640                ret = vb2_dqbuf(&ctx->vq_dst, buf, file->f_flags & O_NONBLOCK);
 641                if (ret == 0 && ctx->state == MFCINST_FINISHED &&
 642                                list_empty(&ctx->vq_dst.done_list))
 643                        v4l2_event_queue_fh(&ctx->fh, &ev);
 644        } else {
 645                ret = -EINVAL;
 646        }
 647        return ret;
 648}
 649
 650/* Export DMA buffer */
 651static int vidioc_expbuf(struct file *file, void *priv,
 652        struct v4l2_exportbuffer *eb)
 653{
 654        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 655
 656        if (eb->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 657                return vb2_expbuf(&ctx->vq_src, eb);
 658        if (eb->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 659                return vb2_expbuf(&ctx->vq_dst, eb);
 660        return -EINVAL;
 661}
 662
 663/* Stream on */
 664static int vidioc_streamon(struct file *file, void *priv,
 665                           enum v4l2_buf_type type)
 666{
 667        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 668        struct s5p_mfc_dev *dev = ctx->dev;
 669        int ret = -EINVAL;
 670
 671        mfc_debug_enter();
 672        if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 673
 674                if (ctx->state == MFCINST_INIT) {
 675                        ctx->dst_bufs_cnt = 0;
 676                        ctx->src_bufs_cnt = 0;
 677                        ctx->capture_state = QUEUE_FREE;
 678                        ctx->output_state = QUEUE_FREE;
 679                        s5p_mfc_hw_call(dev->mfc_ops, alloc_instance_buffer,
 680                                        ctx);
 681                        s5p_mfc_hw_call(dev->mfc_ops, alloc_dec_temp_buffers,
 682                                        ctx);
 683                        set_work_bit_irqsave(ctx);
 684                        s5p_mfc_clean_ctx_int_flags(ctx);
 685                        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
 686
 687                        if (s5p_mfc_wait_for_done_ctx(ctx,
 688                                S5P_MFC_R2H_CMD_OPEN_INSTANCE_RET, 0)) {
 689                                /* Error or timeout */
 690                                mfc_err("Error getting instance from hardware\n");
 691                                s5p_mfc_hw_call(dev->mfc_ops,
 692                                                release_instance_buffer, ctx);
 693                                s5p_mfc_hw_call(dev->mfc_ops,
 694                                                release_dec_desc_buffer, ctx);
 695                                return -EIO;
 696                        }
 697                        mfc_debug(2, "Got instance number: %d\n", ctx->inst_no);
 698                }
 699                ret = vb2_streamon(&ctx->vq_src, type);
 700                }
 701        else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 702                ret = vb2_streamon(&ctx->vq_dst, type);
 703        mfc_debug_leave();
 704        return ret;
 705}
 706
 707/* Stream off, which equals to a pause */
 708static int vidioc_streamoff(struct file *file, void *priv,
 709                            enum v4l2_buf_type type)
 710{
 711        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 712
 713        if (type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE)
 714                return vb2_streamoff(&ctx->vq_src, type);
 715        else if (type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE)
 716                return vb2_streamoff(&ctx->vq_dst, type);
 717        return -EINVAL;
 718}
 719
 720/* Set controls - v4l2 control framework */
 721static int s5p_mfc_dec_s_ctrl(struct v4l2_ctrl *ctrl)
 722{
 723        struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
 724
 725        switch (ctrl->id) {
 726        case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY:
 727                ctx->display_delay = ctrl->val;
 728                break;
 729        case V4L2_CID_MPEG_MFC51_VIDEO_DECODER_H264_DISPLAY_DELAY_ENABLE:
 730                ctx->display_delay_enable = ctrl->val;
 731                break;
 732        case V4L2_CID_MPEG_VIDEO_DECODER_MPEG4_DEBLOCK_FILTER:
 733                ctx->loop_filter_mpeg4 = ctrl->val;
 734                break;
 735        case V4L2_CID_MPEG_VIDEO_DECODER_SLICE_INTERFACE:
 736                ctx->slice_interface = ctrl->val;
 737                break;
 738        default:
 739                mfc_err("Invalid control 0x%08x\n", ctrl->id);
 740                return -EINVAL;
 741        }
 742        return 0;
 743}
 744
 745static int s5p_mfc_dec_g_v_ctrl(struct v4l2_ctrl *ctrl)
 746{
 747        struct s5p_mfc_ctx *ctx = ctrl_to_ctx(ctrl);
 748        struct s5p_mfc_dev *dev = ctx->dev;
 749
 750        switch (ctrl->id) {
 751        case V4L2_CID_MIN_BUFFERS_FOR_CAPTURE:
 752                if (ctx->state >= MFCINST_HEAD_PARSED &&
 753                    ctx->state < MFCINST_ABORT) {
 754                        ctrl->val = ctx->pb_count;
 755                        break;
 756                } else if (ctx->state != MFCINST_INIT) {
 757                        v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
 758                        return -EINVAL;
 759                }
 760                /* Should wait for the header to be parsed */
 761                s5p_mfc_clean_ctx_int_flags(ctx);
 762                s5p_mfc_wait_for_done_ctx(ctx,
 763                                S5P_MFC_R2H_CMD_SEQ_DONE_RET, 0);
 764                if (ctx->state >= MFCINST_HEAD_PARSED &&
 765                    ctx->state < MFCINST_ABORT) {
 766                        ctrl->val = ctx->pb_count;
 767                } else {
 768                        v4l2_err(&dev->v4l2_dev, "Decoding not initialised\n");
 769                        return -EINVAL;
 770                }
 771                break;
 772        }
 773        return 0;
 774}
 775
 776
 777static const struct v4l2_ctrl_ops s5p_mfc_dec_ctrl_ops = {
 778        .s_ctrl = s5p_mfc_dec_s_ctrl,
 779        .g_volatile_ctrl = s5p_mfc_dec_g_v_ctrl,
 780};
 781
 782/* Get cropping information */
 783static int vidioc_g_crop(struct file *file, void *priv,
 784                struct v4l2_crop *cr)
 785{
 786        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 787        struct s5p_mfc_dev *dev = ctx->dev;
 788        u32 left, right, top, bottom;
 789
 790        if (ctx->state != MFCINST_HEAD_PARSED &&
 791        ctx->state != MFCINST_RUNNING && ctx->state != MFCINST_FINISHING
 792                                        && ctx->state != MFCINST_FINISHED) {
 793                        mfc_err("Cannont set crop\n");
 794                        return -EINVAL;
 795                }
 796        if (ctx->src_fmt->fourcc == V4L2_PIX_FMT_H264) {
 797                left = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_h, ctx);
 798                right = left >> S5P_FIMV_SHARED_CROP_RIGHT_SHIFT;
 799                left = left & S5P_FIMV_SHARED_CROP_LEFT_MASK;
 800                top = s5p_mfc_hw_call(dev->mfc_ops, get_crop_info_v, ctx);
 801                bottom = top >> S5P_FIMV_SHARED_CROP_BOTTOM_SHIFT;
 802                top = top & S5P_FIMV_SHARED_CROP_TOP_MASK;
 803                cr->c.left = left;
 804                cr->c.top = top;
 805                cr->c.width = ctx->img_width - left - right;
 806                cr->c.height = ctx->img_height - top - bottom;
 807                mfc_debug(2, "Cropping info [h264]: l=%d t=%d "
 808                        "w=%d h=%d (r=%d b=%d fw=%d fh=%d\n", left, top,
 809                        cr->c.width, cr->c.height, right, bottom,
 810                        ctx->buf_width, ctx->buf_height);
 811        } else {
 812                cr->c.left = 0;
 813                cr->c.top = 0;
 814                cr->c.width = ctx->img_width;
 815                cr->c.height = ctx->img_height;
 816                mfc_debug(2, "Cropping info: w=%d h=%d fw=%d "
 817                        "fh=%d\n", cr->c.width, cr->c.height, ctx->buf_width,
 818                                                        ctx->buf_height);
 819        }
 820        return 0;
 821}
 822
 823int vidioc_decoder_cmd(struct file *file, void *priv,
 824                                                struct v4l2_decoder_cmd *cmd)
 825{
 826        struct s5p_mfc_ctx *ctx = fh_to_ctx(priv);
 827        struct s5p_mfc_dev *dev = ctx->dev;
 828        struct s5p_mfc_buf *buf;
 829        unsigned long flags;
 830
 831        switch (cmd->cmd) {
 832        case V4L2_ENC_CMD_STOP:
 833                if (cmd->flags != 0)
 834                        return -EINVAL;
 835
 836                if (!ctx->vq_src.streaming)
 837                        return -EINVAL;
 838
 839                spin_lock_irqsave(&dev->irqlock, flags);
 840                if (list_empty(&ctx->src_queue)) {
 841                        mfc_err("EOS: empty src queue, entering finishing state");
 842                        ctx->state = MFCINST_FINISHING;
 843                        if (s5p_mfc_ctx_ready(ctx))
 844                                set_work_bit_irqsave(ctx);
 845                        spin_unlock_irqrestore(&dev->irqlock, flags);
 846                        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
 847                } else {
 848                        mfc_err("EOS: marking last buffer of stream");
 849                        buf = list_entry(ctx->src_queue.prev,
 850                                                struct s5p_mfc_buf, list);
 851                        if (buf->flags & MFC_BUF_FLAG_USED)
 852                                ctx->state = MFCINST_FINISHING;
 853                        else
 854                                buf->flags |= MFC_BUF_FLAG_EOS;
 855                        spin_unlock_irqrestore(&dev->irqlock, flags);
 856                }
 857                break;
 858        default:
 859                return -EINVAL;
 860        }
 861        return 0;
 862}
 863
 864static int vidioc_subscribe_event(struct v4l2_fh *fh,
 865                                const struct  v4l2_event_subscription *sub)
 866{
 867        switch (sub->type) {
 868        case V4L2_EVENT_EOS:
 869                return v4l2_event_subscribe(fh, sub, 2, NULL);
 870        default:
 871                return -EINVAL;
 872        }
 873}
 874
 875
 876/* v4l2_ioctl_ops */
 877static const struct v4l2_ioctl_ops s5p_mfc_dec_ioctl_ops = {
 878        .vidioc_querycap = vidioc_querycap,
 879        .vidioc_enum_fmt_vid_cap = vidioc_enum_fmt_vid_cap,
 880        .vidioc_enum_fmt_vid_cap_mplane = vidioc_enum_fmt_vid_cap_mplane,
 881        .vidioc_enum_fmt_vid_out = vidioc_enum_fmt_vid_out,
 882        .vidioc_enum_fmt_vid_out_mplane = vidioc_enum_fmt_vid_out_mplane,
 883        .vidioc_g_fmt_vid_cap_mplane = vidioc_g_fmt,
 884        .vidioc_g_fmt_vid_out_mplane = vidioc_g_fmt,
 885        .vidioc_try_fmt_vid_cap_mplane = vidioc_try_fmt,
 886        .vidioc_try_fmt_vid_out_mplane = vidioc_try_fmt,
 887        .vidioc_s_fmt_vid_cap_mplane = vidioc_s_fmt,
 888        .vidioc_s_fmt_vid_out_mplane = vidioc_s_fmt,
 889        .vidioc_reqbufs = vidioc_reqbufs,
 890        .vidioc_querybuf = vidioc_querybuf,
 891        .vidioc_qbuf = vidioc_qbuf,
 892        .vidioc_dqbuf = vidioc_dqbuf,
 893        .vidioc_expbuf = vidioc_expbuf,
 894        .vidioc_streamon = vidioc_streamon,
 895        .vidioc_streamoff = vidioc_streamoff,
 896        .vidioc_g_crop = vidioc_g_crop,
 897        .vidioc_decoder_cmd = vidioc_decoder_cmd,
 898        .vidioc_subscribe_event = vidioc_subscribe_event,
 899        .vidioc_unsubscribe_event = v4l2_event_unsubscribe,
 900};
 901
 902static int s5p_mfc_queue_setup(struct vb2_queue *vq,
 903                        const struct v4l2_format *fmt, unsigned int *buf_count,
 904                        unsigned int *plane_count, unsigned int psize[],
 905                        void *allocators[])
 906{
 907        struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
 908        struct s5p_mfc_dev *dev = ctx->dev;
 909
 910        /* Video output for decoding (source)
 911         * this can be set after getting an instance */
 912        if (ctx->state == MFCINST_INIT &&
 913            vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
 914                /* A single plane is required for input */
 915                *plane_count = 1;
 916                if (*buf_count < 1)
 917                        *buf_count = 1;
 918                if (*buf_count > MFC_MAX_BUFFERS)
 919                        *buf_count = MFC_MAX_BUFFERS;
 920        /* Video capture for decoding (destination)
 921         * this can be set after the header was parsed */
 922        } else if (ctx->state == MFCINST_HEAD_PARSED &&
 923                   vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 924                /* Output plane count is 2 - one for Y and one for CbCr */
 925                *plane_count = 2;
 926                /* Setup buffer count */
 927                if (*buf_count < ctx->pb_count)
 928                        *buf_count = ctx->pb_count;
 929                if (*buf_count > ctx->pb_count + MFC_MAX_EXTRA_DPB)
 930                        *buf_count = ctx->pb_count + MFC_MAX_EXTRA_DPB;
 931                if (*buf_count > MFC_MAX_BUFFERS)
 932                        *buf_count = MFC_MAX_BUFFERS;
 933        } else {
 934                mfc_err("State seems invalid. State = %d, vq->type = %d\n",
 935                                                        ctx->state, vq->type);
 936                return -EINVAL;
 937        }
 938        mfc_debug(2, "Buffer count=%d, plane count=%d\n",
 939                                                *buf_count, *plane_count);
 940        if (ctx->state == MFCINST_HEAD_PARSED &&
 941            vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 942                psize[0] = ctx->luma_size;
 943                psize[1] = ctx->chroma_size;
 944
 945                if (IS_MFCV6(dev))
 946                        allocators[0] =
 947                                ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
 948                else
 949                        allocators[0] =
 950                                ctx->dev->alloc_ctx[MFC_BANK2_ALLOC_CTX];
 951                allocators[1] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
 952        } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE &&
 953                   ctx->state == MFCINST_INIT) {
 954                psize[0] = ctx->dec_src_buf_size;
 955                allocators[0] = ctx->dev->alloc_ctx[MFC_BANK1_ALLOC_CTX];
 956        } else {
 957                mfc_err("This video node is dedicated to decoding. Decoding not initalised\n");
 958                return -EINVAL;
 959        }
 960        return 0;
 961}
 962
 963static void s5p_mfc_unlock(struct vb2_queue *q)
 964{
 965        struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
 966        struct s5p_mfc_dev *dev = ctx->dev;
 967
 968        mutex_unlock(&dev->mfc_mutex);
 969}
 970
 971static void s5p_mfc_lock(struct vb2_queue *q)
 972{
 973        struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
 974        struct s5p_mfc_dev *dev = ctx->dev;
 975
 976        mutex_lock(&dev->mfc_mutex);
 977}
 978
 979static int s5p_mfc_buf_init(struct vb2_buffer *vb)
 980{
 981        struct vb2_queue *vq = vb->vb2_queue;
 982        struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
 983        unsigned int i;
 984
 985        if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
 986                if (ctx->capture_state == QUEUE_BUFS_MMAPED)
 987                        return 0;
 988                for (i = 0; i <= ctx->src_fmt->num_planes ; i++) {
 989                        if (IS_ERR_OR_NULL(ERR_PTR(
 990                                        vb2_dma_contig_plane_dma_addr(vb, i)))) {
 991                                mfc_err("Plane mem not allocated\n");
 992                                return -EINVAL;
 993                        }
 994                }
 995                if (vb2_plane_size(vb, 0) < ctx->luma_size ||
 996                        vb2_plane_size(vb, 1) < ctx->chroma_size) {
 997                        mfc_err("Plane buffer (CAPTURE) is too small\n");
 998                        return -EINVAL;
 999                }
1000                i = vb->v4l2_buf.index;
1001                ctx->dst_bufs[i].b = vb;
1002                ctx->dst_bufs[i].cookie.raw.luma =
1003                                        vb2_dma_contig_plane_dma_addr(vb, 0);
1004                ctx->dst_bufs[i].cookie.raw.chroma =
1005                                        vb2_dma_contig_plane_dma_addr(vb, 1);
1006                ctx->dst_bufs_cnt++;
1007        } else if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1008                if (IS_ERR_OR_NULL(ERR_PTR(
1009                                        vb2_dma_contig_plane_dma_addr(vb, 0)))) {
1010                        mfc_err("Plane memory not allocated\n");
1011                        return -EINVAL;
1012                }
1013                if (vb2_plane_size(vb, 0) < ctx->dec_src_buf_size) {
1014                        mfc_err("Plane buffer (OUTPUT) is too small\n");
1015                        return -EINVAL;
1016                }
1017
1018                i = vb->v4l2_buf.index;
1019                ctx->src_bufs[i].b = vb;
1020                ctx->src_bufs[i].cookie.stream =
1021                                        vb2_dma_contig_plane_dma_addr(vb, 0);
1022                ctx->src_bufs_cnt++;
1023        } else {
1024                mfc_err("s5p_mfc_buf_init: unknown queue type\n");
1025                return -EINVAL;
1026        }
1027        return 0;
1028}
1029
1030static int s5p_mfc_start_streaming(struct vb2_queue *q, unsigned int count)
1031{
1032        struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1033        struct s5p_mfc_dev *dev = ctx->dev;
1034
1035        v4l2_ctrl_handler_setup(&ctx->ctrl_handler);
1036        if (ctx->state == MFCINST_FINISHING ||
1037                ctx->state == MFCINST_FINISHED)
1038                ctx->state = MFCINST_RUNNING;
1039        /* If context is ready then dev = work->data;schedule it to run */
1040        if (s5p_mfc_ctx_ready(ctx))
1041                set_work_bit_irqsave(ctx);
1042        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1043        return 0;
1044}
1045
1046static int s5p_mfc_stop_streaming(struct vb2_queue *q)
1047{
1048        unsigned long flags;
1049        struct s5p_mfc_ctx *ctx = fh_to_ctx(q->drv_priv);
1050        struct s5p_mfc_dev *dev = ctx->dev;
1051        int aborted = 0;
1052
1053        if ((ctx->state == MFCINST_FINISHING ||
1054                ctx->state ==  MFCINST_RUNNING) &&
1055                dev->curr_ctx == ctx->num && dev->hw_lock) {
1056                ctx->state = MFCINST_ABORT;
1057                s5p_mfc_wait_for_done_ctx(ctx,
1058                                        S5P_MFC_R2H_CMD_FRAME_DONE_RET, 0);
1059                aborted = 1;
1060        }
1061        if (q->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1062                spin_lock_irqsave(&dev->irqlock, flags);
1063                s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->dst_queue,
1064                                &ctx->vq_dst);
1065                INIT_LIST_HEAD(&ctx->dst_queue);
1066                ctx->dst_queue_cnt = 0;
1067                ctx->dpb_flush_flag = 1;
1068                ctx->dec_dst_flag = 0;
1069                spin_unlock_irqrestore(&dev->irqlock, flags);
1070                if (IS_MFCV6(dev) && (ctx->state == MFCINST_RUNNING)) {
1071                        ctx->state = MFCINST_FLUSH;
1072                        set_work_bit_irqsave(ctx);
1073                        s5p_mfc_clean_ctx_int_flags(ctx);
1074                        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1075                        if (s5p_mfc_wait_for_done_ctx(ctx,
1076                                S5P_MFC_R2H_CMD_DPB_FLUSH_RET, 0))
1077                                mfc_err("Err flushing buffers\n");
1078                }
1079        }
1080        if (q->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1081                spin_lock_irqsave(&dev->irqlock, flags);
1082                s5p_mfc_hw_call(dev->mfc_ops, cleanup_queue, &ctx->src_queue,
1083                                &ctx->vq_src);
1084                INIT_LIST_HEAD(&ctx->src_queue);
1085                ctx->src_queue_cnt = 0;
1086                spin_unlock_irqrestore(&dev->irqlock, flags);
1087        }
1088        if (aborted)
1089                ctx->state = MFCINST_RUNNING;
1090        return 0;
1091}
1092
1093
1094static void s5p_mfc_buf_queue(struct vb2_buffer *vb)
1095{
1096        struct vb2_queue *vq = vb->vb2_queue;
1097        struct s5p_mfc_ctx *ctx = fh_to_ctx(vq->drv_priv);
1098        struct s5p_mfc_dev *dev = ctx->dev;
1099        unsigned long flags;
1100        struct s5p_mfc_buf *mfc_buf;
1101
1102        if (vq->type == V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE) {
1103                mfc_buf = &ctx->src_bufs[vb->v4l2_buf.index];
1104                mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1105                spin_lock_irqsave(&dev->irqlock, flags);
1106                list_add_tail(&mfc_buf->list, &ctx->src_queue);
1107                ctx->src_queue_cnt++;
1108                spin_unlock_irqrestore(&dev->irqlock, flags);
1109        } else if (vq->type == V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE) {
1110                mfc_buf = &ctx->dst_bufs[vb->v4l2_buf.index];
1111                mfc_buf->flags &= ~MFC_BUF_FLAG_USED;
1112                /* Mark destination as available for use by MFC */
1113                spin_lock_irqsave(&dev->irqlock, flags);
1114                set_bit(vb->v4l2_buf.index, &ctx->dec_dst_flag);
1115                list_add_tail(&mfc_buf->list, &ctx->dst_queue);
1116                ctx->dst_queue_cnt++;
1117                spin_unlock_irqrestore(&dev->irqlock, flags);
1118        } else {
1119                mfc_err("Unsupported buffer type (%d)\n", vq->type);
1120        }
1121        if (s5p_mfc_ctx_ready(ctx))
1122                set_work_bit_irqsave(ctx);
1123        s5p_mfc_hw_call(dev->mfc_ops, try_run, dev);
1124}
1125
1126static struct vb2_ops s5p_mfc_dec_qops = {
1127        .queue_setup            = s5p_mfc_queue_setup,
1128        .wait_prepare           = s5p_mfc_unlock,
1129        .wait_finish            = s5p_mfc_lock,
1130        .buf_init               = s5p_mfc_buf_init,
1131        .start_streaming        = s5p_mfc_start_streaming,
1132        .stop_streaming         = s5p_mfc_stop_streaming,
1133        .buf_queue              = s5p_mfc_buf_queue,
1134};
1135
1136struct s5p_mfc_codec_ops *get_dec_codec_ops(void)
1137{
1138        return &decoder_codec_ops;
1139}
1140
1141struct vb2_ops *get_dec_queue_ops(void)
1142{
1143        return &s5p_mfc_dec_qops;
1144}
1145
1146const struct v4l2_ioctl_ops *get_dec_v4l2_ioctl_ops(void)
1147{
1148        return &s5p_mfc_dec_ioctl_ops;
1149}
1150
1151#define IS_MFC51_PRIV(x) ((V4L2_CTRL_ID2CLASS(x) == V4L2_CTRL_CLASS_MPEG) \
1152                                                && V4L2_CTRL_DRIVER_PRIV(x))
1153
1154int s5p_mfc_dec_ctrls_setup(struct s5p_mfc_ctx *ctx)
1155{
1156        struct v4l2_ctrl_config cfg;
1157        int i;
1158
1159        v4l2_ctrl_handler_init(&ctx->ctrl_handler, NUM_CTRLS);
1160        if (ctx->ctrl_handler.error) {
1161                mfc_err("v4l2_ctrl_handler_init failed\n");
1162                return ctx->ctrl_handler.error;
1163        }
1164
1165        for (i = 0; i < NUM_CTRLS; i++) {
1166                if (IS_MFC51_PRIV(controls[i].id)) {
1167                        memset(&cfg, 0, sizeof(struct v4l2_ctrl_config));
1168                        cfg.ops = &s5p_mfc_dec_ctrl_ops;
1169                        cfg.id = controls[i].id;
1170                        cfg.min = controls[i].minimum;
1171                        cfg.max = controls[i].maximum;
1172                        cfg.def = controls[i].default_value;
1173                        cfg.name = controls[i].name;
1174                        cfg.type = controls[i].type;
1175
1176                        cfg.step = controls[i].step;
1177                        cfg.menu_skip_mask = 0;
1178
1179                        ctx->ctrls[i] = v4l2_ctrl_new_custom(&ctx->ctrl_handler,
1180                                        &cfg, NULL);
1181                } else {
1182                        ctx->ctrls[i] = v4l2_ctrl_new_std(&ctx->ctrl_handler,
1183                                        &s5p_mfc_dec_ctrl_ops,
1184                                        controls[i].id, controls[i].minimum,
1185                                        controls[i].maximum, controls[i].step,
1186                                        controls[i].default_value);
1187                }
1188                if (ctx->ctrl_handler.error) {
1189                        mfc_err("Adding control (%d) failed\n", i);
1190                        return ctx->ctrl_handler.error;
1191                }
1192                if (controls[i].is_volatile && ctx->ctrls[i])
1193                        ctx->ctrls[i]->flags |= V4L2_CTRL_FLAG_VOLATILE;
1194        }
1195        return 0;
1196}
1197
1198void s5p_mfc_dec_ctrls_delete(struct s5p_mfc_ctx *ctx)
1199{
1200        int i;
1201
1202        v4l2_ctrl_handler_free(&ctx->ctrl_handler);
1203        for (i = 0; i < NUM_CTRLS; i++)
1204                ctx->ctrls[i] = NULL;
1205}
1206
1207void s5p_mfc_dec_init(struct s5p_mfc_ctx *ctx)
1208{
1209        struct v4l2_format f;
1210        f.fmt.pix_mp.pixelformat = DEF_SRC_FMT_DEC;
1211        ctx->src_fmt = find_format(&f, MFC_FMT_DEC);
1212        f.fmt.pix_mp.pixelformat = DEF_DST_FMT_DEC;
1213        ctx->dst_fmt = find_format(&f, MFC_FMT_RAW);
1214        mfc_debug(2, "Default src_fmt is %x, dest_fmt is %x\n",
1215                        (unsigned int)ctx->src_fmt, (unsigned int)ctx->dst_fmt);
1216}
1217
1218