linux/drivers/staging/media/davinci_vpfe/vpfe_video.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0
   2/*
   3 * Copyright (C) 2012 Texas Instruments Inc
   4 *
   5 * This program is free software; you can redistribute it and/or
   6 * modify it under the terms of the GNU General Public License as
   7 * published by the Free Software Foundation version 2.
   8 *
   9 * This program is distributed in the hope that it will be useful,
  10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12 * GNU General Public License for more details.
  13 *
  14 * Contributors:
  15 *      Manjunath Hadli <manjunath.hadli@ti.com>
  16 *      Prabhakar Lad <prabhakar.lad@ti.com>
  17 */
  18
  19#include <linux/module.h>
  20#include <linux/slab.h>
  21
  22#include <media/v4l2-ioctl.h>
  23
  24#include "vpfe.h"
  25#include "vpfe_mc_capture.h"
  26
  27static int debug;
  28
  29/* get v4l2 subdev pointer to external subdev which is active */
  30static struct media_entity *vpfe_get_input_entity
  31                        (struct vpfe_video_device *video)
  32{
  33        struct vpfe_device *vpfe_dev = video->vpfe_dev;
  34        struct media_pad *remote;
  35
  36        remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
  37        if (!remote) {
  38                pr_err("Invalid media connection to isif/ccdc\n");
  39                return NULL;
  40        }
  41        return remote->entity;
  42}
  43
  44/* updates external subdev(sensor/decoder) which is active */
  45static int vpfe_update_current_ext_subdev(struct vpfe_video_device *video)
  46{
  47        struct vpfe_device *vpfe_dev = video->vpfe_dev;
  48        struct vpfe_config *vpfe_cfg;
  49        struct v4l2_subdev *subdev;
  50        struct media_pad *remote;
  51        int i;
  52
  53        remote = media_entity_remote_pad(&vpfe_dev->vpfe_isif.pads[0]);
  54        if (!remote) {
  55                pr_err("Invalid media connection to isif/ccdc\n");
  56                return -EINVAL;
  57        }
  58
  59        subdev = media_entity_to_v4l2_subdev(remote->entity);
  60        vpfe_cfg = vpfe_dev->pdev->platform_data;
  61        for (i = 0; i < vpfe_cfg->num_subdevs; i++) {
  62                if (!strcmp(vpfe_cfg->sub_devs[i].module_name, subdev->name)) {
  63                        video->current_ext_subdev = &vpfe_cfg->sub_devs[i];
  64                        break;
  65                }
  66        }
  67
  68        /* if user not linked decoder/sensor to isif/ccdc */
  69        if (i == vpfe_cfg->num_subdevs) {
  70                pr_err("Invalid media chain connection to isif/ccdc\n");
  71                return -EINVAL;
  72        }
  73        /* find the v4l2 subdev pointer */
  74        for (i = 0; i < vpfe_dev->num_ext_subdevs; i++) {
  75                if (!strcmp(video->current_ext_subdev->module_name,
  76                        vpfe_dev->sd[i]->name))
  77                        video->current_ext_subdev->subdev = vpfe_dev->sd[i];
  78        }
  79        return 0;
  80}
  81
  82/* get the subdev which is connected to the output video node */
  83static struct v4l2_subdev *
  84vpfe_video_remote_subdev(struct vpfe_video_device *video, u32 *pad)
  85{
  86        struct media_pad *remote = media_entity_remote_pad(&video->pad);
  87
  88        if (!remote || !is_media_entity_v4l2_subdev(remote->entity))
  89                return NULL;
  90        if (pad)
  91                *pad = remote->index;
  92        return media_entity_to_v4l2_subdev(remote->entity);
  93}
  94
  95/* get the format set at output pad of the adjacent subdev */
  96static int
  97__vpfe_video_get_format(struct vpfe_video_device *video,
  98                        struct v4l2_format *format)
  99{
 100        struct v4l2_subdev_format fmt;
 101        struct v4l2_subdev *subdev;
 102        struct media_pad *remote;
 103        u32 pad;
 104        int ret;
 105
 106        subdev = vpfe_video_remote_subdev(video, &pad);
 107        if (!subdev)
 108                return -EINVAL;
 109
 110        fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
 111        remote = media_entity_remote_pad(&video->pad);
 112        fmt.pad = remote->index;
 113
 114        ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt);
 115        if (ret == -ENOIOCTLCMD)
 116                return -EINVAL;
 117
 118        format->type = video->type;
 119        /* convert mbus_format to v4l2_format */
 120        v4l2_fill_pix_format(&format->fmt.pix, &fmt.format);
 121        mbus_to_pix(&fmt.format, &format->fmt.pix);
 122
 123        return 0;
 124}
 125
 126/* make a note of pipeline details */
 127static int vpfe_prepare_pipeline(struct vpfe_video_device *video)
 128{
 129        struct media_graph graph;
 130        struct media_entity *entity = &video->video_dev.entity;
 131        struct media_device *mdev = entity->graph_obj.mdev;
 132        struct vpfe_pipeline *pipe = &video->pipe;
 133        struct vpfe_video_device *far_end = NULL;
 134        int ret;
 135
 136        pipe->input_num = 0;
 137        pipe->output_num = 0;
 138
 139        if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
 140                pipe->inputs[pipe->input_num++] = video;
 141        else
 142                pipe->outputs[pipe->output_num++] = video;
 143
 144        mutex_lock(&mdev->graph_mutex);
 145        ret = media_graph_walk_init(&graph, mdev);
 146        if (ret) {
 147                mutex_unlock(&mdev->graph_mutex);
 148                return -ENOMEM;
 149        }
 150        media_graph_walk_start(&graph, entity);
 151        while ((entity = media_graph_walk_next(&graph))) {
 152                if (entity == &video->video_dev.entity)
 153                        continue;
 154                if (!is_media_entity_v4l2_video_device(entity))
 155                        continue;
 156                far_end = to_vpfe_video(media_entity_to_video_device(entity));
 157                if (far_end->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
 158                        pipe->inputs[pipe->input_num++] = far_end;
 159                else
 160                        pipe->outputs[pipe->output_num++] = far_end;
 161        }
 162        media_graph_walk_cleanup(&graph);
 163        mutex_unlock(&mdev->graph_mutex);
 164
 165        return 0;
 166}
 167
 168/* update pipe state selected by user */
 169static int vpfe_update_pipe_state(struct vpfe_video_device *video)
 170{
 171        struct vpfe_pipeline *pipe = &video->pipe;
 172        int ret;
 173
 174        ret = vpfe_prepare_pipeline(video);
 175        if (ret)
 176                return ret;
 177
 178        /*
 179         * Find out if there is any input video
 180         * if yes, it is single shot.
 181         */
 182        if (pipe->input_num == 0) {
 183                pipe->state = VPFE_PIPELINE_STREAM_CONTINUOUS;
 184                ret = vpfe_update_current_ext_subdev(video);
 185                if (ret) {
 186                        pr_err("Invalid external subdev\n");
 187                        return ret;
 188                }
 189        } else {
 190                pipe->state = VPFE_PIPELINE_STREAM_SINGLESHOT;
 191        }
 192        video->initialized = 1;
 193        video->skip_frame_count = 1;
 194        video->skip_frame_count_init = 1;
 195        return 0;
 196}
 197
 198/* checks whether pipeline is ready for enabling */
 199int vpfe_video_is_pipe_ready(struct vpfe_pipeline *pipe)
 200{
 201        int i;
 202
 203        for (i = 0; i < pipe->input_num; i++)
 204                if (!pipe->inputs[i]->started ||
 205                        pipe->inputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
 206                        return 0;
 207        for (i = 0; i < pipe->output_num; i++)
 208                if (!pipe->outputs[i]->started ||
 209                        pipe->outputs[i]->state != VPFE_VIDEO_BUFFER_QUEUED)
 210                        return 0;
 211        return 1;
 212}
 213
 214/*
 215 * Validate a pipeline by checking both ends of all links for format
 216 * discrepancies.
 217 *
 218 * Return 0 if all formats match, or -EPIPE if at least one link is found with
 219 * different formats on its two ends.
 220 */
 221static int vpfe_video_validate_pipeline(struct vpfe_pipeline *pipe)
 222{
 223        struct v4l2_subdev_format fmt_source;
 224        struct v4l2_subdev_format fmt_sink;
 225        struct v4l2_subdev *subdev;
 226        struct media_pad *pad;
 227        int ret;
 228
 229        /*
 230         * Should not matter if it is output[0] or 1 as
 231         * the general ideas is to traverse backwards and
 232         * the fact that the out video node always has the
 233         * format of the connected pad.
 234         */
 235        subdev = vpfe_video_remote_subdev(pipe->outputs[0], NULL);
 236        if (!subdev)
 237                return -EPIPE;
 238
 239        while (1) {
 240                /* Retrieve the sink format */
 241                pad = &subdev->entity.pads[0];
 242                if (!(pad->flags & MEDIA_PAD_FL_SINK))
 243                        break;
 244
 245                fmt_sink.which = V4L2_SUBDEV_FORMAT_ACTIVE;
 246                fmt_sink.pad = pad->index;
 247                ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL,
 248                                       &fmt_sink);
 249
 250                if (ret < 0 && ret != -ENOIOCTLCMD)
 251                        return -EPIPE;
 252
 253                /* Retrieve the source format */
 254                pad = media_entity_remote_pad(pad);
 255                if (!pad || !is_media_entity_v4l2_subdev(pad->entity))
 256                        break;
 257
 258                subdev = media_entity_to_v4l2_subdev(pad->entity);
 259
 260                fmt_source.which = V4L2_SUBDEV_FORMAT_ACTIVE;
 261                fmt_source.pad = pad->index;
 262                ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &fmt_source);
 263                if (ret < 0 && ret != -ENOIOCTLCMD)
 264                        return -EPIPE;
 265
 266                /* Check if the two ends match */
 267                if (fmt_source.format.code != fmt_sink.format.code ||
 268                    fmt_source.format.width != fmt_sink.format.width ||
 269                    fmt_source.format.height != fmt_sink.format.height)
 270                        return -EPIPE;
 271        }
 272        return 0;
 273}
 274
 275/*
 276 * vpfe_pipeline_enable() - Enable streaming on a pipeline
 277 * @vpfe_dev: vpfe device
 278 * @pipe: vpfe pipeline
 279 *
 280 * Walk the entities chain starting at the pipeline output video node and start
 281 * all modules in the chain in the given mode.
 282 *
 283 * Return 0 if successful, or the return value of the failed video::s_stream
 284 * operation otherwise.
 285 */
 286static int vpfe_pipeline_enable(struct vpfe_pipeline *pipe)
 287{
 288        struct media_entity *entity;
 289        struct v4l2_subdev *subdev;
 290        struct media_device *mdev;
 291        int ret;
 292
 293        if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
 294                entity = vpfe_get_input_entity(pipe->outputs[0]);
 295        else
 296                entity = &pipe->inputs[0]->video_dev.entity;
 297
 298        mdev = entity->graph_obj.mdev;
 299        mutex_lock(&mdev->graph_mutex);
 300        ret = media_graph_walk_init(&pipe->graph, mdev);
 301        if (ret)
 302                goto out;
 303        media_graph_walk_start(&pipe->graph, entity);
 304        while ((entity = media_graph_walk_next(&pipe->graph))) {
 305
 306                if (!is_media_entity_v4l2_subdev(entity))
 307                        continue;
 308                subdev = media_entity_to_v4l2_subdev(entity);
 309                ret = v4l2_subdev_call(subdev, video, s_stream, 1);
 310                if (ret < 0 && ret != -ENOIOCTLCMD)
 311                        break;
 312        }
 313out:
 314        if (ret)
 315                media_graph_walk_cleanup(&pipe->graph);
 316        mutex_unlock(&mdev->graph_mutex);
 317        return ret;
 318}
 319
 320/*
 321 * vpfe_pipeline_disable() - Disable streaming on a pipeline
 322 * @vpfe_dev: vpfe device
 323 * @pipe: VPFE pipeline
 324 *
 325 * Walk the entities chain starting at the pipeline output video node and stop
 326 * all modules in the chain.
 327 *
 328 * Return 0 if all modules have been properly stopped, or -ETIMEDOUT if a module
 329 * can't be stopped.
 330 */
 331static int vpfe_pipeline_disable(struct vpfe_pipeline *pipe)
 332{
 333        struct media_entity *entity;
 334        struct v4l2_subdev *subdev;
 335        struct media_device *mdev;
 336        int ret = 0;
 337
 338        if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
 339                entity = vpfe_get_input_entity(pipe->outputs[0]);
 340        else
 341                entity = &pipe->inputs[0]->video_dev.entity;
 342
 343        mdev = entity->graph_obj.mdev;
 344        mutex_lock(&mdev->graph_mutex);
 345        media_graph_walk_start(&pipe->graph, entity);
 346
 347        while ((entity = media_graph_walk_next(&pipe->graph))) {
 348
 349                if (!is_media_entity_v4l2_subdev(entity))
 350                        continue;
 351                subdev = media_entity_to_v4l2_subdev(entity);
 352                ret = v4l2_subdev_call(subdev, video, s_stream, 0);
 353                if (ret < 0 && ret != -ENOIOCTLCMD)
 354                        break;
 355        }
 356        mutex_unlock(&mdev->graph_mutex);
 357
 358        media_graph_walk_cleanup(&pipe->graph);
 359        return ret ? -ETIMEDOUT : 0;
 360}
 361
 362/*
 363 * vpfe_pipeline_set_stream() - Enable/disable streaming on a pipeline
 364 * @vpfe_dev: VPFE device
 365 * @pipe: VPFE pipeline
 366 * @state: Stream state (stopped or active)
 367 *
 368 * Set the pipeline to the given stream state.
 369 *
 370 * Return 0 if successful, or the return value of the failed video::s_stream
 371 * operation otherwise.
 372 */
 373static int vpfe_pipeline_set_stream(struct vpfe_pipeline *pipe,
 374                            enum vpfe_pipeline_stream_state state)
 375{
 376        if (state == VPFE_PIPELINE_STREAM_STOPPED)
 377                return vpfe_pipeline_disable(pipe);
 378
 379        return vpfe_pipeline_enable(pipe);
 380}
 381
 382static int all_videos_stopped(struct vpfe_video_device *video)
 383{
 384        struct vpfe_pipeline *pipe = &video->pipe;
 385        int i;
 386
 387        for (i = 0; i < pipe->input_num; i++)
 388                if (pipe->inputs[i]->started)
 389                        return 0;
 390        for (i = 0; i < pipe->output_num; i++)
 391                if (pipe->outputs[i]->started)
 392                        return 0;
 393        return 1;
 394}
 395
 396/*
 397 * vpfe_open() - open video device
 398 * @file: file pointer
 399 *
 400 * initialize media pipeline state, allocate memory for file handle
 401 *
 402 * Return 0 if successful, or the return -ENODEV otherwise.
 403 */
 404static int vpfe_open(struct file *file)
 405{
 406        struct vpfe_video_device *video = video_drvdata(file);
 407        struct vpfe_fh *handle;
 408
 409        /* Allocate memory for the file handle object */
 410        handle = kzalloc(sizeof(struct vpfe_fh), GFP_KERNEL);
 411
 412        if (!handle)
 413                return -ENOMEM;
 414
 415        v4l2_fh_init(&handle->vfh, &video->video_dev);
 416        v4l2_fh_add(&handle->vfh);
 417
 418        mutex_lock(&video->lock);
 419        /* If decoder is not initialized. initialize it */
 420        if (!video->initialized && vpfe_update_pipe_state(video)) {
 421                mutex_unlock(&video->lock);
 422                v4l2_fh_del(&handle->vfh);
 423                v4l2_fh_exit(&handle->vfh);
 424                kfree(handle);
 425                return -ENODEV;
 426        }
 427        /* Increment device users counter */
 428        video->usrs++;
 429        /* Set io_allowed member to false */
 430        handle->io_allowed = 0;
 431        handle->video = video;
 432        file->private_data = &handle->vfh;
 433        mutex_unlock(&video->lock);
 434
 435        return 0;
 436}
 437
 438/* get the next buffer available from dma queue */
 439static unsigned long
 440vpfe_video_get_next_buffer(struct vpfe_video_device *video)
 441{
 442        video->cur_frm = video->next_frm =
 443                list_entry(video->dma_queue.next,
 444                           struct vpfe_cap_buffer, list);
 445
 446        list_del(&video->next_frm->list);
 447        video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
 448        return vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
 449}
 450
 451/* schedule the next buffer which is available on dma queue */
 452void vpfe_video_schedule_next_buffer(struct vpfe_video_device *video)
 453{
 454        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 455        unsigned long addr;
 456
 457        if (list_empty(&video->dma_queue))
 458                return;
 459
 460        video->next_frm = list_entry(video->dma_queue.next,
 461                                        struct vpfe_cap_buffer, list);
 462
 463        if (video->pipe.state == VPFE_PIPELINE_STREAM_SINGLESHOT)
 464                video->cur_frm = video->next_frm;
 465
 466        list_del(&video->next_frm->list);
 467        video->next_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
 468        addr = vb2_dma_contig_plane_dma_addr(&video->next_frm->vb.vb2_buf, 0);
 469        video->ops->queue(vpfe_dev, addr);
 470        video->state = VPFE_VIDEO_BUFFER_QUEUED;
 471}
 472
 473/* schedule the buffer for capturing bottom field */
 474void vpfe_video_schedule_bottom_field(struct vpfe_video_device *video)
 475{
 476        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 477        unsigned long addr;
 478
 479        addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
 480        addr += video->field_off;
 481        video->ops->queue(vpfe_dev, addr);
 482}
 483
 484/* make buffer available for dequeue */
 485void vpfe_video_process_buffer_complete(struct vpfe_video_device *video)
 486{
 487        struct vpfe_pipeline *pipe = &video->pipe;
 488
 489        video->cur_frm->vb.vb2_buf.timestamp = ktime_get_ns();
 490        vb2_buffer_done(&video->cur_frm->vb.vb2_buf, VB2_BUF_STATE_DONE);
 491        if (pipe->state == VPFE_PIPELINE_STREAM_CONTINUOUS)
 492                video->cur_frm = video->next_frm;
 493}
 494
 495/* vpfe_stop_capture() - stop streaming */
 496static void vpfe_stop_capture(struct vpfe_video_device *video)
 497{
 498        struct vpfe_pipeline *pipe = &video->pipe;
 499
 500        video->started = 0;
 501
 502        if (video->type == V4L2_BUF_TYPE_VIDEO_OUTPUT)
 503                return;
 504        if (all_videos_stopped(video))
 505                vpfe_pipeline_set_stream(pipe,
 506                                         VPFE_PIPELINE_STREAM_STOPPED);
 507}
 508
 509/*
 510 * vpfe_release() - release video device
 511 * @file: file pointer
 512 *
 513 * deletes buffer queue, frees the buffers and the vpfe file handle
 514 *
 515 * Return 0
 516 */
 517static int vpfe_release(struct file *file)
 518{
 519        struct vpfe_video_device *video = video_drvdata(file);
 520        struct v4l2_fh *vfh = file->private_data;
 521        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 522        struct vpfe_fh *fh = container_of(vfh, struct vpfe_fh, vfh);
 523
 524        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_release\n");
 525
 526        /* Get the device lock */
 527        mutex_lock(&video->lock);
 528        /* if this instance is doing IO */
 529        if (fh->io_allowed) {
 530                if (video->started) {
 531                        vpfe_stop_capture(video);
 532                        /*
 533                         * mark pipe state as stopped in vpfe_release(),
 534                         * as app might call streamon() after streamoff()
 535                         * in which case driver has to start streaming.
 536                         */
 537                        video->pipe.state = VPFE_PIPELINE_STREAM_STOPPED;
 538                        vb2_streamoff(&video->buffer_queue,
 539                                      video->buffer_queue.type);
 540                }
 541                video->io_usrs = 0;
 542                /* Free buffers allocated */
 543                vb2_queue_release(&video->buffer_queue);
 544        }
 545        /* Decrement device users counter */
 546        video->usrs--;
 547        v4l2_fh_del(&fh->vfh);
 548        v4l2_fh_exit(&fh->vfh);
 549        /* If this is the last file handle */
 550        if (!video->usrs)
 551                video->initialized = 0;
 552        mutex_unlock(&video->lock);
 553        file->private_data = NULL;
 554        /* Free memory allocated to file handle object */
 555        v4l2_fh_del(vfh);
 556        kzfree(fh);
 557        return 0;
 558}
 559
 560/*
 561 * vpfe_mmap() - It is used to map kernel space buffers
 562 * into user spaces
 563 */
 564static int vpfe_mmap(struct file *file, struct vm_area_struct *vma)
 565{
 566        struct vpfe_video_device *video = video_drvdata(file);
 567        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 568
 569        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_mmap\n");
 570        return vb2_mmap(&video->buffer_queue, vma);
 571}
 572
 573/*
 574 * vpfe_poll() - It is used for select/poll system call
 575 */
 576static __poll_t vpfe_poll(struct file *file, poll_table *wait)
 577{
 578        struct vpfe_video_device *video = video_drvdata(file);
 579        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 580
 581        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_poll\n");
 582        if (video->started)
 583                return vb2_poll(&video->buffer_queue, file, wait);
 584        return 0;
 585}
 586
 587/* vpfe capture driver file operations */
 588static const struct v4l2_file_operations vpfe_fops = {
 589        .owner = THIS_MODULE,
 590        .open = vpfe_open,
 591        .release = vpfe_release,
 592        .unlocked_ioctl = video_ioctl2,
 593        .mmap = vpfe_mmap,
 594        .poll = vpfe_poll
 595};
 596
 597/*
 598 * vpfe_querycap() - query capabilities of video device
 599 * @file: file pointer
 600 * @priv: void pointer
 601 * @cap: pointer to v4l2_capability structure
 602 *
 603 * fills v4l2 capabilities structure
 604 *
 605 * Return 0
 606 */
 607static int vpfe_querycap(struct file *file, void  *priv,
 608                               struct v4l2_capability *cap)
 609{
 610        struct vpfe_video_device *video = video_drvdata(file);
 611        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 612
 613        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querycap\n");
 614
 615        cap->capabilities = V4L2_CAP_VIDEO_CAPTURE | V4L2_CAP_VIDEO_OUTPUT |
 616                            V4L2_CAP_STREAMING | V4L2_CAP_DEVICE_CAPS;
 617        strscpy(cap->driver, CAPTURE_DRV_NAME, sizeof(cap->driver));
 618        strscpy(cap->bus_info, "VPFE", sizeof(cap->bus_info));
 619        strscpy(cap->card, vpfe_dev->cfg->card_name, sizeof(cap->card));
 620
 621        return 0;
 622}
 623
 624/*
 625 * vpfe_g_fmt() - get the format which is active on video device
 626 * @file: file pointer
 627 * @priv: void pointer
 628 * @fmt: pointer to v4l2_format structure
 629 *
 630 * fills v4l2 format structure with active format
 631 *
 632 * Return 0
 633 */
 634static int vpfe_g_fmt(struct file *file, void *priv,
 635                                struct v4l2_format *fmt)
 636{
 637        struct vpfe_video_device *video = video_drvdata(file);
 638        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 639
 640        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_fmt\n");
 641        /* Fill in the information about format */
 642        *fmt = video->fmt;
 643        return 0;
 644}
 645
 646/*
 647 * vpfe_enum_fmt() - enum formats supported on media chain
 648 * @file: file pointer
 649 * @priv: void pointer
 650 * @fmt: pointer to v4l2_fmtdesc structure
 651 *
 652 * fills v4l2_fmtdesc structure with output format set on adjacent subdev,
 653 * only one format is enumearted as subdevs are already configured
 654 *
 655 * Return 0 if successful, error code otherwise
 656 */
 657static int vpfe_enum_fmt(struct file *file, void  *priv,
 658                                   struct v4l2_fmtdesc *fmt)
 659{
 660        struct vpfe_video_device *video = video_drvdata(file);
 661        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 662        struct v4l2_subdev_format sd_fmt;
 663        struct v4l2_mbus_framefmt mbus;
 664        struct v4l2_subdev *subdev;
 665        struct v4l2_format format;
 666        struct media_pad *remote;
 667        int ret;
 668
 669        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_fmt\n");
 670
 671        /*
 672         * since already subdev pad format is set,
 673         * only one pixel format is available
 674         */
 675        if (fmt->index > 0) {
 676                v4l2_err(&vpfe_dev->v4l2_dev, "Invalid index\n");
 677                return -EINVAL;
 678        }
 679        /* get the remote pad */
 680        remote = media_entity_remote_pad(&video->pad);
 681        if (!remote) {
 682                v4l2_err(&vpfe_dev->v4l2_dev,
 683                         "invalid remote pad for video node\n");
 684                return -EINVAL;
 685        }
 686        /* get the remote subdev */
 687        subdev = vpfe_video_remote_subdev(video, NULL);
 688        if (!subdev) {
 689                v4l2_err(&vpfe_dev->v4l2_dev,
 690                         "invalid remote subdev for video node\n");
 691                return -EINVAL;
 692        }
 693        sd_fmt.pad = remote->index;
 694        sd_fmt.which = V4L2_SUBDEV_FORMAT_ACTIVE;
 695        /* get output format of remote subdev */
 696        ret = v4l2_subdev_call(subdev, pad, get_fmt, NULL, &sd_fmt);
 697        if (ret) {
 698                v4l2_err(&vpfe_dev->v4l2_dev,
 699                         "invalid remote subdev for video node\n");
 700                return ret;
 701        }
 702        /* convert to pix format */
 703        mbus.code = sd_fmt.format.code;
 704        mbus_to_pix(&mbus, &format.fmt.pix);
 705        /* copy the result */
 706        fmt->pixelformat = format.fmt.pix.pixelformat;
 707
 708        return 0;
 709}
 710
 711/*
 712 * vpfe_s_fmt() - set the format on video device
 713 * @file: file pointer
 714 * @priv: void pointer
 715 * @fmt: pointer to v4l2_format structure
 716 *
 717 * validate and set the format on video device
 718 *
 719 * Return 0 on success, error code otherwise
 720 */
 721static int vpfe_s_fmt(struct file *file, void *priv,
 722                                struct v4l2_format *fmt)
 723{
 724        struct vpfe_video_device *video = video_drvdata(file);
 725        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 726        struct v4l2_format format;
 727        int ret;
 728
 729        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_fmt\n");
 730        /* If streaming is started, return error */
 731        if (video->started) {
 732                v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is started\n");
 733                return -EBUSY;
 734        }
 735        /* get adjacent subdev's output pad format */
 736        ret = __vpfe_video_get_format(video, &format);
 737        if (ret)
 738                return ret;
 739        *fmt = format;
 740        video->fmt = *fmt;
 741        return 0;
 742}
 743
 744/*
 745 * vpfe_try_fmt() - try the format on video device
 746 * @file: file pointer
 747 * @priv: void pointer
 748 * @fmt: pointer to v4l2_format structure
 749 *
 750 * validate the format, update with correct format
 751 * based on output format set on adjacent subdev
 752 *
 753 * Return 0 on success, error code otherwise
 754 */
 755static int vpfe_try_fmt(struct file *file, void *priv,
 756                                  struct v4l2_format *fmt)
 757{
 758        struct vpfe_video_device *video = video_drvdata(file);
 759        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 760        struct v4l2_format format;
 761        int ret;
 762
 763        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_try_fmt\n");
 764        /* get adjacent subdev's output pad format */
 765        ret = __vpfe_video_get_format(video, &format);
 766        if (ret)
 767                return ret;
 768
 769        *fmt = format;
 770        return 0;
 771}
 772
 773/*
 774 * vpfe_enum_input() - enum inputs supported on media chain
 775 * @file: file pointer
 776 * @priv: void pointer
 777 * @fmt: pointer to v4l2_fmtdesc structure
 778 *
 779 * fills v4l2_input structure with input available on media chain,
 780 * only one input is enumearted as media chain is setup by this time
 781 *
 782 * Return 0 if successful, -EINVAL is media chain is invalid
 783 */
 784static int vpfe_enum_input(struct file *file, void *priv,
 785                                 struct v4l2_input *inp)
 786{
 787        struct vpfe_video_device *video = video_drvdata(file);
 788        struct vpfe_ext_subdev_info *sdinfo = video->current_ext_subdev;
 789        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 790
 791        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_input\n");
 792        /* enumerate from the subdev user has chosen through mc */
 793        if (inp->index < sdinfo->num_inputs) {
 794                memcpy(inp, &sdinfo->inputs[inp->index],
 795                       sizeof(struct v4l2_input));
 796                return 0;
 797        }
 798        return -EINVAL;
 799}
 800
 801/*
 802 * vpfe_g_input() - get index of the input which is active
 803 * @file: file pointer
 804 * @priv: void pointer
 805 * @index: pointer to unsigned int
 806 *
 807 * set index with input index which is active
 808 */
 809static int vpfe_g_input(struct file *file, void *priv, unsigned int *index)
 810{
 811        struct vpfe_video_device *video = video_drvdata(file);
 812        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 813
 814        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_input\n");
 815
 816        *index = video->current_input;
 817        return 0;
 818}
 819
 820/*
 821 * vpfe_s_input() - set input which is pointed by input index
 822 * @file: file pointer
 823 * @priv: void pointer
 824 * @index: pointer to unsigned int
 825 *
 826 * set input on external subdev
 827 *
 828 * Return 0 on success, error code otherwise
 829 */
 830static int vpfe_s_input(struct file *file, void *priv, unsigned int index)
 831{
 832        struct vpfe_video_device *video = video_drvdata(file);
 833        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 834        struct vpfe_ext_subdev_info *sdinfo;
 835        struct vpfe_route *route;
 836        struct v4l2_input *inps;
 837        u32 output;
 838        u32 input;
 839        int ret;
 840        int i;
 841
 842        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_input\n");
 843
 844        ret = mutex_lock_interruptible(&video->lock);
 845        if (ret)
 846                return ret;
 847        /*
 848         * If streaming is started return device busy
 849         * error
 850         */
 851        if (video->started) {
 852                v4l2_err(&vpfe_dev->v4l2_dev, "Streaming is on\n");
 853                ret = -EBUSY;
 854                goto unlock_out;
 855        }
 856
 857        sdinfo = video->current_ext_subdev;
 858        if (!sdinfo->registered) {
 859                ret = -EINVAL;
 860                goto unlock_out;
 861        }
 862        if (vpfe_dev->cfg->setup_input &&
 863                vpfe_dev->cfg->setup_input(sdinfo->grp_id) < 0) {
 864                ret = -EFAULT;
 865                v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
 866                          "couldn't setup input for %s\n",
 867                          sdinfo->module_name);
 868                goto unlock_out;
 869        }
 870        route = &sdinfo->routes[index];
 871        if (route && sdinfo->can_route) {
 872                input = route->input;
 873                output = route->output;
 874                ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
 875                                                 sdinfo->grp_id, video,
 876                                                 s_routing, input, output, 0);
 877                if (ret) {
 878                        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
 879                                "s_input:error in setting input in decoder\n");
 880                        ret = -EINVAL;
 881                        goto unlock_out;
 882                }
 883        }
 884        /* set standards set by subdev in video device */
 885        for (i = 0; i < sdinfo->num_inputs; i++) {
 886                inps = &sdinfo->inputs[i];
 887                video->video_dev.tvnorms |= inps->std;
 888        }
 889        video->current_input = index;
 890unlock_out:
 891        mutex_unlock(&video->lock);
 892        return ret;
 893}
 894
 895/*
 896 * vpfe_querystd() - query std which is being input on external subdev
 897 * @file: file pointer
 898 * @priv: void pointer
 899 * @std_id: pointer to v4l2_std_id structure
 900 *
 901 * call external subdev through v4l2_device_call_until_err to
 902 * get the std that is being active.
 903 *
 904 * Return 0 on success, error code otherwise
 905 */
 906static int vpfe_querystd(struct file *file, void *priv, v4l2_std_id *std_id)
 907{
 908        struct vpfe_video_device *video = video_drvdata(file);
 909        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 910        struct vpfe_ext_subdev_info *sdinfo;
 911        int ret;
 912
 913        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querystd\n");
 914
 915        ret = mutex_lock_interruptible(&video->lock);
 916        sdinfo = video->current_ext_subdev;
 917        if (ret)
 918                return ret;
 919        /* Call querystd function of decoder device */
 920        ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
 921                                         video, querystd, std_id);
 922        mutex_unlock(&video->lock);
 923        return ret;
 924}
 925
 926/*
 927 * vpfe_s_std() - set std on external subdev
 928 * @file: file pointer
 929 * @priv: void pointer
 930 * @std_id: pointer to v4l2_std_id structure
 931 *
 932 * set std pointed by std_id on external subdev by calling it using
 933 * v4l2_device_call_until_err
 934 *
 935 * Return 0 on success, error code otherwise
 936 */
 937static int vpfe_s_std(struct file *file, void *priv, v4l2_std_id std_id)
 938{
 939        struct vpfe_video_device *video = video_drvdata(file);
 940        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 941        struct vpfe_ext_subdev_info *sdinfo;
 942        int ret;
 943
 944        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_std\n");
 945
 946        /* Call decoder driver function to set the standard */
 947        ret = mutex_lock_interruptible(&video->lock);
 948        if (ret)
 949                return ret;
 950        sdinfo = video->current_ext_subdev;
 951        /* If streaming is started, return device busy error */
 952        if (video->started) {
 953                v4l2_err(&vpfe_dev->v4l2_dev, "streaming is started\n");
 954                ret = -EBUSY;
 955                goto unlock_out;
 956        }
 957        ret = v4l2_device_call_until_err(&vpfe_dev->v4l2_dev, sdinfo->grp_id,
 958                                         video, s_std, std_id);
 959        if (ret < 0) {
 960                v4l2_err(&vpfe_dev->v4l2_dev, "Failed to set standard\n");
 961                video->stdid = V4L2_STD_UNKNOWN;
 962                goto unlock_out;
 963        }
 964        video->stdid = std_id;
 965unlock_out:
 966        mutex_unlock(&video->lock);
 967        return ret;
 968}
 969
 970static int vpfe_g_std(struct file *file, void *priv, v4l2_std_id *tvnorm)
 971{
 972        struct vpfe_video_device *video = video_drvdata(file);
 973        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 974
 975        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_std\n");
 976        *tvnorm = video->stdid;
 977        return 0;
 978}
 979
 980/*
 981 * vpfe_enum_dv_timings() - enumerate dv_timings which are supported by
 982 *                      to external subdev
 983 * @file: file pointer
 984 * @priv: void pointer
 985 * @timings: pointer to v4l2_enum_dv_timings structure
 986 *
 987 * enum dv_timings's which are supported by external subdev through
 988 * v4l2_subdev_call
 989 *
 990 * Return 0 on success, error code otherwise
 991 */
 992static int
 993vpfe_enum_dv_timings(struct file *file, void *fh,
 994                  struct v4l2_enum_dv_timings *timings)
 995{
 996        struct vpfe_video_device *video = video_drvdata(file);
 997        struct vpfe_device *vpfe_dev = video->vpfe_dev;
 998        struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
 999
1000        timings->pad = 0;
1001
1002        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_enum_dv_timings\n");
1003        return v4l2_subdev_call(subdev, pad, enum_dv_timings, timings);
1004}
1005
1006/*
1007 * vpfe_query_dv_timings() - query the dv_timings which is being input
1008 *                      to external subdev
1009 * @file: file pointer
1010 * @priv: void pointer
1011 * @timings: pointer to v4l2_dv_timings structure
1012 *
1013 * get dv_timings which is being input on external subdev through
1014 * v4l2_subdev_call
1015 *
1016 * Return 0 on success, error code otherwise
1017 */
1018static int
1019vpfe_query_dv_timings(struct file *file, void *fh,
1020                   struct v4l2_dv_timings *timings)
1021{
1022        struct vpfe_video_device *video = video_drvdata(file);
1023        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1024        struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1025
1026        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_query_dv_timings\n");
1027        return v4l2_subdev_call(subdev, video, query_dv_timings, timings);
1028}
1029
1030/*
1031 * vpfe_s_dv_timings() - set dv_timings on external subdev
1032 * @file: file pointer
1033 * @priv: void pointer
1034 * @timings: pointer to v4l2_dv_timings structure
1035 *
1036 * set dv_timings pointed by timings on external subdev through
1037 * v4l2_device_call_until_err, this configures amplifier also
1038 *
1039 * Return 0 on success, error code otherwise
1040 */
1041static int
1042vpfe_s_dv_timings(struct file *file, void *fh,
1043                  struct v4l2_dv_timings *timings)
1044{
1045        struct vpfe_video_device *video = video_drvdata(file);
1046        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1047
1048        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_s_dv_timings\n");
1049
1050        video->stdid = V4L2_STD_UNKNOWN;
1051        return v4l2_device_call_until_err(&vpfe_dev->v4l2_dev,
1052                                          video->current_ext_subdev->grp_id,
1053                                          video, s_dv_timings, timings);
1054}
1055
1056/*
1057 * vpfe_g_dv_timings() - get dv_timings which is set on external subdev
1058 * @file: file pointer
1059 * @priv: void pointer
1060 * @timings: pointer to v4l2_dv_timings structure
1061 *
1062 * get dv_timings which is set on external subdev through
1063 * v4l2_subdev_call
1064 *
1065 * Return 0 on success, error code otherwise
1066 */
1067static int
1068vpfe_g_dv_timings(struct file *file, void *fh,
1069              struct v4l2_dv_timings *timings)
1070{
1071        struct vpfe_video_device *video = video_drvdata(file);
1072        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1073        struct v4l2_subdev *subdev = video->current_ext_subdev->subdev;
1074
1075        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_g_dv_timings\n");
1076        return v4l2_subdev_call(subdev, video, g_dv_timings, timings);
1077}
1078
1079/*
1080 *  Videobuf operations
1081 */
1082/*
1083 * vpfe_buffer_queue_setup : Callback function for buffer setup.
1084 * @vq: vb2_queue ptr
1085 * @fmt: v4l2 format
1086 * @nbuffers: ptr to number of buffers requested by application
1087 * @nplanes:: contains number of distinct video planes needed to hold a frame
1088 * @sizes[]: contains the size (in bytes) of each plane.
1089 * @alloc_devs: ptr to allocation context
1090 *
1091 * This callback function is called when reqbuf() is called to adjust
1092 * the buffer nbuffers and buffer size
1093 */
1094static int
1095vpfe_buffer_queue_setup(struct vb2_queue *vq,
1096                        unsigned int *nbuffers, unsigned int *nplanes,
1097                        unsigned int sizes[], struct device *alloc_devs[])
1098{
1099        struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1100        struct vpfe_video_device *video = fh->video;
1101        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1102        unsigned long size;
1103
1104        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_queue_setup\n");
1105        size = video->fmt.fmt.pix.sizeimage;
1106
1107        if (vq->num_buffers + *nbuffers < 3)
1108                *nbuffers = 3 - vq->num_buffers;
1109
1110        *nplanes = 1;
1111        sizes[0] = size;
1112        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev,
1113                 "nbuffers=%d, size=%lu\n", *nbuffers, size);
1114        return 0;
1115}
1116
1117/*
1118 * vpfe_buffer_prepare : callback function for buffer prepare
1119 * @vb: ptr to vb2_buffer
1120 *
1121 * This is the callback function for buffer prepare when vb2_qbuf()
1122 * function is called. The buffer is prepared and user space virtual address
1123 * or user address is converted into  physical address
1124 */
1125static int vpfe_buffer_prepare(struct vb2_buffer *vb)
1126{
1127        struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1128        struct vpfe_video_device *video = fh->video;
1129        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1130        unsigned long addr;
1131
1132        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buffer_prepare\n");
1133
1134        /* Initialize buffer */
1135        vb2_set_plane_payload(vb, 0, video->fmt.fmt.pix.sizeimage);
1136        if (vb2_plane_vaddr(vb, 0) &&
1137            vb2_get_plane_payload(vb, 0) > vb2_plane_size(vb, 0))
1138                return -EINVAL;
1139
1140        addr = vb2_dma_contig_plane_dma_addr(vb, 0);
1141        /* Make sure user addresses are aligned to 32 bytes */
1142        if (!ALIGN(addr, 32))
1143                return -EINVAL;
1144
1145        return 0;
1146}
1147
1148static void vpfe_buffer_queue(struct vb2_buffer *vb)
1149{
1150        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1151        /* Get the file handle object and device object */
1152        struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1153        struct vpfe_video_device *video = fh->video;
1154        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1155        struct vpfe_pipeline *pipe = &video->pipe;
1156        struct vpfe_cap_buffer *buf = container_of(vbuf,
1157                                struct vpfe_cap_buffer, vb);
1158        unsigned long flags;
1159        unsigned long empty;
1160        unsigned long addr;
1161
1162        spin_lock_irqsave(&video->dma_queue_lock, flags);
1163        empty = list_empty(&video->dma_queue);
1164        /* add the buffer to the DMA queue */
1165        list_add_tail(&buf->list, &video->dma_queue);
1166        spin_unlock_irqrestore(&video->dma_queue_lock, flags);
1167        /* this case happens in case of single shot */
1168        if (empty && video->started && pipe->state ==
1169                VPFE_PIPELINE_STREAM_SINGLESHOT &&
1170                video->state == VPFE_VIDEO_BUFFER_NOT_QUEUED) {
1171                spin_lock(&video->dma_queue_lock);
1172                addr = vpfe_video_get_next_buffer(video);
1173                video->ops->queue(vpfe_dev, addr);
1174
1175                video->state = VPFE_VIDEO_BUFFER_QUEUED;
1176                spin_unlock(&video->dma_queue_lock);
1177
1178                /* enable h/w each time in single shot */
1179                if (vpfe_video_is_pipe_ready(pipe))
1180                        vpfe_pipeline_set_stream(pipe,
1181                                        VPFE_PIPELINE_STREAM_SINGLESHOT);
1182        }
1183}
1184
1185/* vpfe_start_capture() - start streaming on all the subdevs */
1186static int vpfe_start_capture(struct vpfe_video_device *video)
1187{
1188        struct vpfe_pipeline *pipe = &video->pipe;
1189        int ret = 0;
1190
1191        video->started = 1;
1192        if (vpfe_video_is_pipe_ready(pipe))
1193                ret = vpfe_pipeline_set_stream(pipe, pipe->state);
1194
1195        return ret;
1196}
1197
1198static int vpfe_start_streaming(struct vb2_queue *vq, unsigned int count)
1199{
1200        struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1201        struct vpfe_video_device *video = fh->video;
1202        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1203        unsigned long addr;
1204        int ret;
1205
1206        ret = mutex_lock_interruptible(&video->lock);
1207        if (ret)
1208                goto streamoff;
1209
1210        /* Get the next frame from the buffer queue */
1211        video->cur_frm = video->next_frm =
1212                list_entry(video->dma_queue.next, struct vpfe_cap_buffer, list);
1213        /* Remove buffer from the buffer queue */
1214        list_del(&video->cur_frm->list);
1215        /* Mark state of the current frame to active */
1216        video->cur_frm->vb.vb2_buf.state = VB2_BUF_STATE_ACTIVE;
1217        /* Initialize field_id and started member */
1218        video->field_id = 0;
1219        addr = vb2_dma_contig_plane_dma_addr(&video->cur_frm->vb.vb2_buf, 0);
1220        video->ops->queue(vpfe_dev, addr);
1221        video->state = VPFE_VIDEO_BUFFER_QUEUED;
1222
1223        ret = vpfe_start_capture(video);
1224        if (ret) {
1225                struct vpfe_cap_buffer *buf, *tmp;
1226
1227                vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1228                                VB2_BUF_STATE_QUEUED);
1229                list_for_each_entry_safe(buf, tmp, &video->dma_queue, list) {
1230                        list_del(&buf->list);
1231                        vb2_buffer_done(&buf->vb.vb2_buf,
1232                                        VB2_BUF_STATE_QUEUED);
1233                }
1234                goto unlock_out;
1235        }
1236
1237        mutex_unlock(&video->lock);
1238
1239        return ret;
1240unlock_out:
1241        mutex_unlock(&video->lock);
1242streamoff:
1243        ret = vb2_streamoff(&video->buffer_queue, video->buffer_queue.type);
1244        return 0;
1245}
1246
1247static int vpfe_buffer_init(struct vb2_buffer *vb)
1248{
1249        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1250        struct vpfe_cap_buffer *buf = container_of(vbuf,
1251                                                   struct vpfe_cap_buffer, vb);
1252
1253        INIT_LIST_HEAD(&buf->list);
1254        return 0;
1255}
1256
1257/* abort streaming and wait for last buffer */
1258static void vpfe_stop_streaming(struct vb2_queue *vq)
1259{
1260        struct vpfe_fh *fh = vb2_get_drv_priv(vq);
1261        struct vpfe_video_device *video = fh->video;
1262
1263        /* release all active buffers */
1264        if (video->cur_frm == video->next_frm) {
1265                vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1266                                VB2_BUF_STATE_ERROR);
1267        } else {
1268                if (video->cur_frm != NULL)
1269                        vb2_buffer_done(&video->cur_frm->vb.vb2_buf,
1270                                        VB2_BUF_STATE_ERROR);
1271                if (video->next_frm != NULL)
1272                        vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1273                                        VB2_BUF_STATE_ERROR);
1274        }
1275
1276        while (!list_empty(&video->dma_queue)) {
1277                video->next_frm = list_entry(video->dma_queue.next,
1278                                                struct vpfe_cap_buffer, list);
1279                list_del(&video->next_frm->list);
1280                vb2_buffer_done(&video->next_frm->vb.vb2_buf,
1281                                VB2_BUF_STATE_ERROR);
1282        }
1283}
1284
1285static void vpfe_buf_cleanup(struct vb2_buffer *vb)
1286{
1287        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
1288        struct vpfe_fh *fh = vb2_get_drv_priv(vb->vb2_queue);
1289        struct vpfe_video_device *video = fh->video;
1290        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1291        struct vpfe_cap_buffer *buf = container_of(vbuf,
1292                                        struct vpfe_cap_buffer, vb);
1293
1294        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_buf_cleanup\n");
1295        if (vb->state == VB2_BUF_STATE_ACTIVE)
1296                list_del_init(&buf->list);
1297}
1298
1299static const struct vb2_ops video_qops = {
1300        .queue_setup            = vpfe_buffer_queue_setup,
1301        .buf_init               = vpfe_buffer_init,
1302        .buf_prepare            = vpfe_buffer_prepare,
1303        .start_streaming        = vpfe_start_streaming,
1304        .stop_streaming         = vpfe_stop_streaming,
1305        .buf_cleanup            = vpfe_buf_cleanup,
1306        .buf_queue              = vpfe_buffer_queue,
1307        .wait_prepare           = vb2_ops_wait_prepare,
1308        .wait_finish            = vb2_ops_wait_finish,
1309};
1310
1311/*
1312 * vpfe_reqbufs() - supported REQBUF only once opening
1313 * the device.
1314 */
1315static int vpfe_reqbufs(struct file *file, void *priv,
1316                        struct v4l2_requestbuffers *req_buf)
1317{
1318        struct vpfe_video_device *video = video_drvdata(file);
1319        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1320        struct vpfe_fh *fh = file->private_data;
1321        struct vb2_queue *q;
1322        int ret;
1323
1324        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_reqbufs\n");
1325
1326        if (req_buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1327            req_buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT){
1328                v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buffer type\n");
1329                return -EINVAL;
1330        }
1331
1332        ret = mutex_lock_interruptible(&video->lock);
1333        if (ret)
1334                return ret;
1335
1336        if (video->io_usrs != 0) {
1337                v4l2_err(&vpfe_dev->v4l2_dev, "Only one IO user allowed\n");
1338                ret = -EBUSY;
1339                goto unlock_out;
1340        }
1341        video->memory = req_buf->memory;
1342
1343        /* Initialize videobuf2 queue as per the buffer type */
1344        q = &video->buffer_queue;
1345        q->type = req_buf->type;
1346        q->io_modes = VB2_MMAP | VB2_USERPTR;
1347        q->drv_priv = fh;
1348        q->min_buffers_needed = 1;
1349        q->ops = &video_qops;
1350        q->mem_ops = &vb2_dma_contig_memops;
1351        q->buf_struct_size = sizeof(struct vpfe_cap_buffer);
1352        q->timestamp_flags = V4L2_BUF_FLAG_TIMESTAMP_MONOTONIC;
1353        q->dev = vpfe_dev->pdev;
1354        q->lock = &video->lock;
1355
1356        ret = vb2_queue_init(q);
1357        if (ret) {
1358                v4l2_err(&vpfe_dev->v4l2_dev, "vb2_queue_init() failed\n");
1359                goto unlock_out;
1360        }
1361
1362        fh->io_allowed = 1;
1363        video->io_usrs = 1;
1364        INIT_LIST_HEAD(&video->dma_queue);
1365        ret = vb2_reqbufs(&video->buffer_queue, req_buf);
1366
1367unlock_out:
1368        mutex_unlock(&video->lock);
1369        return ret;
1370}
1371
1372/*
1373 * vpfe_querybuf() - query buffers for exchange
1374 */
1375static int vpfe_querybuf(struct file *file, void *priv,
1376                         struct v4l2_buffer *buf)
1377{
1378        struct vpfe_video_device *video = video_drvdata(file);
1379        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1380
1381        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_querybuf\n");
1382
1383        if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1384            buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1385                v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1386                return  -EINVAL;
1387        }
1388
1389        if (video->memory != V4L2_MEMORY_MMAP) {
1390                v4l2_err(&vpfe_dev->v4l2_dev, "Invalid memory\n");
1391                return -EINVAL;
1392        }
1393
1394        /* Call vb2_querybuf to get information */
1395        return vb2_querybuf(&video->buffer_queue, buf);
1396}
1397
1398/*
1399 * vpfe_qbuf() - queue buffers for capture or processing
1400 */
1401static int vpfe_qbuf(struct file *file, void *priv,
1402                     struct v4l2_buffer *p)
1403{
1404        struct vpfe_video_device *video = video_drvdata(file);
1405        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1406        struct vpfe_fh *fh = file->private_data;
1407
1408        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_qbuf\n");
1409
1410        if (p->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1411            p->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1412                v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1413                return -EINVAL;
1414        }
1415        /*
1416         * If this file handle is not allowed to do IO,
1417         * return error
1418         */
1419        if (!fh->io_allowed) {
1420                v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1421                return -EACCES;
1422        }
1423
1424        return vb2_qbuf(&video->buffer_queue,
1425                        video->video_dev.v4l2_dev->mdev, p);
1426}
1427
1428/*
1429 * vpfe_dqbuf() - deque buffer which is done with processing
1430 */
1431static int vpfe_dqbuf(struct file *file, void *priv,
1432                      struct v4l2_buffer *buf)
1433{
1434        struct vpfe_video_device *video = video_drvdata(file);
1435        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1436
1437        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_dqbuf\n");
1438
1439        if (buf->type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1440            buf->type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1441                v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1442                return -EINVAL;
1443        }
1444
1445        return vb2_dqbuf(&video->buffer_queue,
1446                         buf, (file->f_flags & O_NONBLOCK));
1447}
1448
1449/*
1450 * vpfe_streamon() - start streaming
1451 * @file: file pointer
1452 * @priv: void pointer
1453 * @buf_type: enum v4l2_buf_type
1454 *
1455 * queue buffer onto hardware for capture/processing and
1456 * start all the subdevs which are in media chain
1457 *
1458 * Return 0 on success, error code otherwise
1459 */
1460static int vpfe_streamon(struct file *file, void *priv,
1461                         enum v4l2_buf_type buf_type)
1462{
1463        struct vpfe_video_device *video = video_drvdata(file);
1464        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1465        struct vpfe_pipeline *pipe = &video->pipe;
1466        struct vpfe_fh *fh = file->private_data;
1467        int ret = -EINVAL;
1468
1469        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamon\n");
1470
1471        if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1472            buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1473                v4l2_err(&vpfe_dev->v4l2_dev, "Invalid buf type\n");
1474                return ret;
1475        }
1476        /* If file handle is not allowed IO, return error */
1477        if (!fh->io_allowed) {
1478                v4l2_err(&vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1479                return -EACCES;
1480        }
1481        /* If buffer queue is empty, return error */
1482        if (list_empty(&video->buffer_queue.queued_list)) {
1483                v4l2_err(&vpfe_dev->v4l2_dev, "buffer queue is empty\n");
1484                return -EIO;
1485        }
1486        /* Validate the pipeline */
1487        if (buf_type == V4L2_BUF_TYPE_VIDEO_CAPTURE) {
1488                ret = vpfe_video_validate_pipeline(pipe);
1489                if (ret < 0)
1490                        return ret;
1491        }
1492        /* Call vb2_streamon to start streaming */
1493        return vb2_streamon(&video->buffer_queue, buf_type);
1494}
1495
1496/*
1497 * vpfe_streamoff() - stop streaming
1498 * @file: file pointer
1499 * @priv: void pointer
1500 * @buf_type: enum v4l2_buf_type
1501 *
1502 * stop all the subdevs which are in media chain
1503 *
1504 * Return 0 on success, error code otherwise
1505 */
1506static int vpfe_streamoff(struct file *file, void *priv,
1507                          enum v4l2_buf_type buf_type)
1508{
1509        struct vpfe_video_device *video = video_drvdata(file);
1510        struct vpfe_device *vpfe_dev = video->vpfe_dev;
1511        struct vpfe_fh *fh = file->private_data;
1512        int ret = 0;
1513
1514        v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "vpfe_streamoff\n");
1515
1516        if (buf_type != V4L2_BUF_TYPE_VIDEO_CAPTURE &&
1517            buf_type != V4L2_BUF_TYPE_VIDEO_OUTPUT) {
1518                v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "Invalid buf type\n");
1519                return -EINVAL;
1520        }
1521
1522        /* If io is allowed for this file handle, return error */
1523        if (!fh->io_allowed) {
1524                v4l2_dbg(1, debug, &vpfe_dev->v4l2_dev, "fh->io_allowed\n");
1525                return -EACCES;
1526        }
1527
1528        /* If streaming is not started, return error */
1529        if (!video->started) {
1530                v4l2_err(&vpfe_dev->v4l2_dev, "device is not started\n");
1531                return -EINVAL;
1532        }
1533
1534        ret = mutex_lock_interruptible(&video->lock);
1535        if (ret)
1536                return ret;
1537
1538        vpfe_stop_capture(video);
1539        ret = vb2_streamoff(&video->buffer_queue, buf_type);
1540        mutex_unlock(&video->lock);
1541
1542        return ret;
1543}
1544
1545/* vpfe capture ioctl operations */
1546static const struct v4l2_ioctl_ops vpfe_ioctl_ops = {
1547        .vidioc_querycap         = vpfe_querycap,
1548        .vidioc_g_fmt_vid_cap    = vpfe_g_fmt,
1549        .vidioc_s_fmt_vid_cap    = vpfe_s_fmt,
1550        .vidioc_try_fmt_vid_cap  = vpfe_try_fmt,
1551        .vidioc_enum_fmt_vid_cap = vpfe_enum_fmt,
1552        .vidioc_g_fmt_vid_out    = vpfe_g_fmt,
1553        .vidioc_s_fmt_vid_out    = vpfe_s_fmt,
1554        .vidioc_try_fmt_vid_out  = vpfe_try_fmt,
1555        .vidioc_enum_fmt_vid_out = vpfe_enum_fmt,
1556        .vidioc_enum_input       = vpfe_enum_input,
1557        .vidioc_g_input          = vpfe_g_input,
1558        .vidioc_s_input          = vpfe_s_input,
1559        .vidioc_querystd         = vpfe_querystd,
1560        .vidioc_s_std            = vpfe_s_std,
1561        .vidioc_g_std            = vpfe_g_std,
1562        .vidioc_enum_dv_timings  = vpfe_enum_dv_timings,
1563        .vidioc_query_dv_timings = vpfe_query_dv_timings,
1564        .vidioc_s_dv_timings     = vpfe_s_dv_timings,
1565        .vidioc_g_dv_timings     = vpfe_g_dv_timings,
1566        .vidioc_reqbufs          = vpfe_reqbufs,
1567        .vidioc_querybuf         = vpfe_querybuf,
1568        .vidioc_qbuf             = vpfe_qbuf,
1569        .vidioc_dqbuf            = vpfe_dqbuf,
1570        .vidioc_streamon         = vpfe_streamon,
1571        .vidioc_streamoff        = vpfe_streamoff,
1572};
1573
1574/* VPFE video init function */
1575int vpfe_video_init(struct vpfe_video_device *video, const char *name)
1576{
1577        const char *direction;
1578        int ret;
1579
1580        switch (video->type) {
1581        case V4L2_BUF_TYPE_VIDEO_CAPTURE:
1582                direction = "output";
1583                video->pad.flags = MEDIA_PAD_FL_SINK;
1584                video->type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
1585                break;
1586
1587        case V4L2_BUF_TYPE_VIDEO_OUTPUT:
1588                direction = "input";
1589                video->pad.flags = MEDIA_PAD_FL_SOURCE;
1590                video->type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
1591                break;
1592
1593        default:
1594                return -EINVAL;
1595        }
1596        /* Initialize field of video device */
1597        mutex_init(&video->lock);
1598        video->video_dev.release = video_device_release;
1599        video->video_dev.fops = &vpfe_fops;
1600        video->video_dev.ioctl_ops = &vpfe_ioctl_ops;
1601        video->video_dev.minor = -1;
1602        video->video_dev.tvnorms = 0;
1603        video->video_dev.lock = &video->lock;
1604        snprintf(video->video_dev.name, sizeof(video->video_dev.name),
1605                 "DAVINCI VIDEO %s %s", name, direction);
1606
1607        spin_lock_init(&video->irqlock);
1608        spin_lock_init(&video->dma_queue_lock);
1609        ret = media_entity_pads_init(&video->video_dev.entity,
1610                                1, &video->pad);
1611        if (ret < 0)
1612                return ret;
1613
1614        video_set_drvdata(&video->video_dev, video);
1615
1616        return 0;
1617}
1618
1619/* vpfe video device register function */
1620int vpfe_video_register(struct vpfe_video_device *video,
1621                        struct v4l2_device *vdev)
1622{
1623        int ret;
1624
1625        video->video_dev.v4l2_dev = vdev;
1626
1627        if (video->type == V4L2_BUF_TYPE_VIDEO_CAPTURE)
1628                video->video_dev.device_caps = V4L2_CAP_VIDEO_CAPTURE;
1629        else
1630                video->video_dev.device_caps = V4L2_CAP_VIDEO_OUTPUT;
1631        video->video_dev.device_caps |= V4L2_CAP_STREAMING;
1632        ret = video_register_device(&video->video_dev, VFL_TYPE_GRABBER, -1);
1633        if (ret < 0)
1634                pr_err("%s: could not register video device (%d)\n",
1635                       __func__, ret);
1636        return ret;
1637}
1638
1639/* vpfe video device unregister function */
1640void vpfe_video_unregister(struct vpfe_video_device *video)
1641{
1642        if (video_is_registered(&video->video_dev)) {
1643                video_unregister_device(&video->video_dev);
1644                media_entity_cleanup(&video->video_dev.entity);
1645        }
1646}
1647