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