linux/drivers/media/platform/vivid/vivid-vid-out.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * vivid-vid-out.c - video output support functions.
   4 *
   5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
   6 */
   7
   8#include <linux/errno.h>
   9#include <linux/kernel.h>
  10#include <linux/sched.h>
  11#include <linux/videodev2.h>
  12#include <linux/v4l2-dv-timings.h>
  13#include <media/v4l2-common.h>
  14#include <media/v4l2-event.h>
  15#include <media/v4l2-dv-timings.h>
  16#include <media/v4l2-rect.h>
  17
  18#include "vivid-core.h"
  19#include "vivid-vid-common.h"
  20#include "vivid-kthread-out.h"
  21#include "vivid-vid-out.h"
  22
  23static int vid_out_queue_setup(struct vb2_queue *vq,
  24                       unsigned *nbuffers, unsigned *nplanes,
  25                       unsigned sizes[], struct device *alloc_devs[])
  26{
  27        struct vivid_dev *dev = vb2_get_drv_priv(vq);
  28        const struct vivid_fmt *vfmt = dev->fmt_out;
  29        unsigned planes = vfmt->buffers;
  30        unsigned h = dev->fmt_out_rect.height;
  31        unsigned int size = dev->bytesperline_out[0] * h + vfmt->data_offset[0];
  32        unsigned p;
  33
  34        for (p = vfmt->buffers; p < vfmt->planes; p++)
  35                size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p] +
  36                        vfmt->data_offset[p];
  37
  38        if (dev->field_out == V4L2_FIELD_ALTERNATE) {
  39                /*
  40                 * You cannot use write() with FIELD_ALTERNATE since the field
  41                 * information (TOP/BOTTOM) cannot be passed to the kernel.
  42                 */
  43                if (vb2_fileio_is_active(vq))
  44                        return -EINVAL;
  45        }
  46
  47        if (dev->queue_setup_error) {
  48                /*
  49                 * Error injection: test what happens if queue_setup() returns
  50                 * an error.
  51                 */
  52                dev->queue_setup_error = false;
  53                return -EINVAL;
  54        }
  55
  56        if (*nplanes) {
  57                /*
  58                 * Check if the number of requested planes match
  59                 * the number of planes in the current format. You can't mix that.
  60                 */
  61                if (*nplanes != planes)
  62                        return -EINVAL;
  63                if (sizes[0] < size)
  64                        return -EINVAL;
  65                for (p = 1; p < planes; p++) {
  66                        if (sizes[p] < dev->bytesperline_out[p] * h +
  67                                       vfmt->data_offset[p])
  68                                return -EINVAL;
  69                }
  70        } else {
  71                for (p = 0; p < planes; p++)
  72                        sizes[p] = p ? dev->bytesperline_out[p] * h +
  73                                       vfmt->data_offset[p] : size;
  74        }
  75
  76        if (vq->num_buffers + *nbuffers < 2)
  77                *nbuffers = 2 - vq->num_buffers;
  78
  79        *nplanes = planes;
  80
  81        dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
  82        for (p = 0; p < planes; p++)
  83                dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
  84        return 0;
  85}
  86
  87static int vid_out_buf_out_validate(struct vb2_buffer *vb)
  88{
  89        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
  90        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
  91
  92        dprintk(dev, 1, "%s\n", __func__);
  93
  94        if (dev->field_out != V4L2_FIELD_ALTERNATE)
  95                vbuf->field = dev->field_out;
  96        else if (vbuf->field != V4L2_FIELD_TOP &&
  97                 vbuf->field != V4L2_FIELD_BOTTOM)
  98                return -EINVAL;
  99        return 0;
 100}
 101
 102static int vid_out_buf_prepare(struct vb2_buffer *vb)
 103{
 104        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 105        const struct vivid_fmt *vfmt = dev->fmt_out;
 106        unsigned int planes = vfmt->buffers;
 107        unsigned int h = dev->fmt_out_rect.height;
 108        unsigned int size = dev->bytesperline_out[0] * h;
 109        unsigned p;
 110
 111        for (p = vfmt->buffers; p < vfmt->planes; p++)
 112                size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
 113
 114        dprintk(dev, 1, "%s\n", __func__);
 115
 116        if (WARN_ON(NULL == dev->fmt_out))
 117                return -EINVAL;
 118
 119        if (dev->buf_prepare_error) {
 120                /*
 121                 * Error injection: test what happens if buf_prepare() returns
 122                 * an error.
 123                 */
 124                dev->buf_prepare_error = false;
 125                return -EINVAL;
 126        }
 127
 128        for (p = 0; p < planes; p++) {
 129                if (p)
 130                        size = dev->bytesperline_out[p] * h;
 131                size += vb->planes[p].data_offset;
 132
 133                if (vb2_get_plane_payload(vb, p) < size) {
 134                        dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %u)\n",
 135                                        __func__, p, vb2_get_plane_payload(vb, p), size);
 136                        return -EINVAL;
 137                }
 138        }
 139
 140        return 0;
 141}
 142
 143static void vid_out_buf_queue(struct vb2_buffer *vb)
 144{
 145        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 146        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 147        struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
 148
 149        dprintk(dev, 1, "%s\n", __func__);
 150
 151        spin_lock(&dev->slock);
 152        list_add_tail(&buf->list, &dev->vid_out_active);
 153        spin_unlock(&dev->slock);
 154}
 155
 156static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
 157{
 158        struct vivid_dev *dev = vb2_get_drv_priv(vq);
 159        int err;
 160
 161        if (vb2_is_streaming(&dev->vb_vid_cap_q))
 162                dev->can_loop_video = vivid_vid_can_loop(dev);
 163
 164        if (dev->kthread_vid_out)
 165                return 0;
 166
 167        dev->vid_out_seq_count = 0;
 168        dprintk(dev, 1, "%s\n", __func__);
 169        if (dev->start_streaming_error) {
 170                dev->start_streaming_error = false;
 171                err = -EINVAL;
 172        } else {
 173                err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
 174        }
 175        if (err) {
 176                struct vivid_buffer *buf, *tmp;
 177
 178                list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
 179                        list_del(&buf->list);
 180                        vb2_buffer_done(&buf->vb.vb2_buf,
 181                                        VB2_BUF_STATE_QUEUED);
 182                }
 183        }
 184        return err;
 185}
 186
 187/* abort streaming and wait for last buffer */
 188static void vid_out_stop_streaming(struct vb2_queue *vq)
 189{
 190        struct vivid_dev *dev = vb2_get_drv_priv(vq);
 191
 192        dprintk(dev, 1, "%s\n", __func__);
 193        vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
 194        dev->can_loop_video = false;
 195}
 196
 197static void vid_out_buf_request_complete(struct vb2_buffer *vb)
 198{
 199        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 200
 201        v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_vid_out);
 202}
 203
 204const struct vb2_ops vivid_vid_out_qops = {
 205        .queue_setup            = vid_out_queue_setup,
 206        .buf_out_validate               = vid_out_buf_out_validate,
 207        .buf_prepare            = vid_out_buf_prepare,
 208        .buf_queue              = vid_out_buf_queue,
 209        .start_streaming        = vid_out_start_streaming,
 210        .stop_streaming         = vid_out_stop_streaming,
 211        .buf_request_complete   = vid_out_buf_request_complete,
 212        .wait_prepare           = vb2_ops_wait_prepare,
 213        .wait_finish            = vb2_ops_wait_finish,
 214};
 215
 216/*
 217 * Called whenever the format has to be reset which can occur when
 218 * changing outputs, standard, timings, etc.
 219 */
 220void vivid_update_format_out(struct vivid_dev *dev)
 221{
 222        struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
 223        unsigned size, p;
 224        u64 pixelclock;
 225
 226        switch (dev->output_type[dev->output]) {
 227        case SVID:
 228        default:
 229                dev->field_out = dev->tv_field_out;
 230                dev->sink_rect.width = 720;
 231                if (dev->std_out & V4L2_STD_525_60) {
 232                        dev->sink_rect.height = 480;
 233                        dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
 234                        dev->service_set_out = V4L2_SLICED_CAPTION_525;
 235                } else {
 236                        dev->sink_rect.height = 576;
 237                        dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
 238                        dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
 239                }
 240                dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
 241                break;
 242        case HDMI:
 243                dev->sink_rect.width = bt->width;
 244                dev->sink_rect.height = bt->height;
 245                size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
 246
 247                if (can_reduce_fps(bt) && (bt->flags & V4L2_DV_FL_REDUCED_FPS))
 248                        pixelclock = div_u64(bt->pixelclock * 1000, 1001);
 249                else
 250                        pixelclock = bt->pixelclock;
 251
 252                dev->timeperframe_vid_out = (struct v4l2_fract) {
 253                        size / 100, (u32)pixelclock / 100
 254                };
 255                if (bt->interlaced)
 256                        dev->field_out = V4L2_FIELD_ALTERNATE;
 257                else
 258                        dev->field_out = V4L2_FIELD_NONE;
 259                if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
 260                        if (bt->width == 720 && bt->height <= 576)
 261                                dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
 262                        else
 263                                dev->colorspace_out = V4L2_COLORSPACE_REC709;
 264                } else {
 265                        dev->colorspace_out = V4L2_COLORSPACE_SRGB;
 266                }
 267                break;
 268        }
 269        dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
 270        dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
 271        dev->hsv_enc_out = V4L2_HSV_ENC_180;
 272        dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
 273        dev->compose_out = dev->sink_rect;
 274        dev->compose_bounds_out = dev->sink_rect;
 275        dev->crop_out = dev->compose_out;
 276        if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
 277                dev->crop_out.height /= 2;
 278        dev->fmt_out_rect = dev->crop_out;
 279        for (p = 0; p < dev->fmt_out->planes; p++)
 280                dev->bytesperline_out[p] =
 281                        (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
 282}
 283
 284/* Map the field to something that is valid for the current output */
 285static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
 286{
 287        if (vivid_is_svid_out(dev)) {
 288                switch (field) {
 289                case V4L2_FIELD_INTERLACED_TB:
 290                case V4L2_FIELD_INTERLACED_BT:
 291                case V4L2_FIELD_SEQ_TB:
 292                case V4L2_FIELD_SEQ_BT:
 293                case V4L2_FIELD_ALTERNATE:
 294                        return field;
 295                case V4L2_FIELD_INTERLACED:
 296                default:
 297                        return V4L2_FIELD_INTERLACED;
 298                }
 299        }
 300        if (vivid_is_hdmi_out(dev))
 301                return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
 302                                                       V4L2_FIELD_NONE;
 303        return V4L2_FIELD_NONE;
 304}
 305
 306static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
 307{
 308        if (vivid_is_svid_out(dev))
 309                return (dev->std_out & V4L2_STD_525_60) ?
 310                        TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
 311
 312        if (vivid_is_hdmi_out(dev) &&
 313            dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
 314                return dev->sink_rect.height == 480 ?
 315                        TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
 316
 317        return TPG_PIXEL_ASPECT_SQUARE;
 318}
 319
 320int vivid_g_fmt_vid_out(struct file *file, void *priv,
 321                                        struct v4l2_format *f)
 322{
 323        struct vivid_dev *dev = video_drvdata(file);
 324        struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
 325        const struct vivid_fmt *fmt = dev->fmt_out;
 326        unsigned p;
 327
 328        mp->width        = dev->fmt_out_rect.width;
 329        mp->height       = dev->fmt_out_rect.height;
 330        mp->field        = dev->field_out;
 331        mp->pixelformat  = fmt->fourcc;
 332        mp->colorspace   = dev->colorspace_out;
 333        mp->xfer_func    = dev->xfer_func_out;
 334        mp->ycbcr_enc    = dev->ycbcr_enc_out;
 335        mp->quantization = dev->quantization_out;
 336        mp->num_planes = fmt->buffers;
 337        for (p = 0; p < mp->num_planes; p++) {
 338                mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
 339                mp->plane_fmt[p].sizeimage =
 340                        mp->plane_fmt[p].bytesperline * mp->height +
 341                        fmt->data_offset[p];
 342        }
 343        for (p = fmt->buffers; p < fmt->planes; p++) {
 344                unsigned stride = dev->bytesperline_out[p];
 345
 346                mp->plane_fmt[0].sizeimage +=
 347                        (stride * mp->height) / fmt->vdownsampling[p];
 348        }
 349        return 0;
 350}
 351
 352int vivid_try_fmt_vid_out(struct file *file, void *priv,
 353                        struct v4l2_format *f)
 354{
 355        struct vivid_dev *dev = video_drvdata(file);
 356        struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
 357        struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
 358        struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
 359        const struct vivid_fmt *fmt;
 360        unsigned bytesperline, max_bpl;
 361        unsigned factor = 1;
 362        unsigned w, h;
 363        unsigned p;
 364
 365        fmt = vivid_get_format(dev, mp->pixelformat);
 366        if (!fmt) {
 367                dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
 368                        mp->pixelformat);
 369                mp->pixelformat = V4L2_PIX_FMT_YUYV;
 370                fmt = vivid_get_format(dev, mp->pixelformat);
 371        }
 372
 373        mp->field = vivid_field_out(dev, mp->field);
 374        if (vivid_is_svid_out(dev)) {
 375                w = 720;
 376                h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
 377        } else {
 378                w = dev->sink_rect.width;
 379                h = dev->sink_rect.height;
 380        }
 381        if (V4L2_FIELD_HAS_T_OR_B(mp->field))
 382                factor = 2;
 383        if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
 384                mp->width = w;
 385                mp->height = h / factor;
 386        } else {
 387                struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
 388
 389                v4l2_rect_set_min_size(&r, &vivid_min_rect);
 390                v4l2_rect_set_max_size(&r, &vivid_max_rect);
 391                if (dev->has_scaler_out && !dev->has_crop_out) {
 392                        struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
 393
 394                        v4l2_rect_set_max_size(&r, &max_r);
 395                } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
 396                        v4l2_rect_set_max_size(&r, &dev->sink_rect);
 397                } else if (!dev->has_scaler_out && !dev->has_compose_out) {
 398                        v4l2_rect_set_min_size(&r, &dev->sink_rect);
 399                }
 400                mp->width = r.width;
 401                mp->height = r.height / factor;
 402        }
 403
 404        /* This driver supports custom bytesperline values */
 405
 406        mp->num_planes = fmt->buffers;
 407        for (p = 0; p < fmt->buffers; p++) {
 408                /* Calculate the minimum supported bytesperline value */
 409                bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
 410                /* Calculate the maximum supported bytesperline value */
 411                max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
 412
 413                if (pfmt[p].bytesperline > max_bpl)
 414                        pfmt[p].bytesperline = max_bpl;
 415                if (pfmt[p].bytesperline < bytesperline)
 416                        pfmt[p].bytesperline = bytesperline;
 417
 418                pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
 419                                fmt->vdownsampling[p] + fmt->data_offset[p];
 420
 421                memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
 422        }
 423        for (p = fmt->buffers; p < fmt->planes; p++)
 424                pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
 425                        (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
 426                        (fmt->bit_depth[0] / fmt->vdownsampling[0]);
 427
 428        mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
 429        mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
 430        mp->quantization = V4L2_QUANTIZATION_DEFAULT;
 431        if (vivid_is_svid_out(dev)) {
 432                mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
 433        } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
 434                mp->colorspace = V4L2_COLORSPACE_SRGB;
 435                if (dev->dvi_d_out)
 436                        mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
 437        } else if (bt->width == 720 && bt->height <= 576) {
 438                mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
 439        } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
 440                   mp->colorspace != V4L2_COLORSPACE_REC709 &&
 441                   mp->colorspace != V4L2_COLORSPACE_OPRGB &&
 442                   mp->colorspace != V4L2_COLORSPACE_BT2020 &&
 443                   mp->colorspace != V4L2_COLORSPACE_SRGB) {
 444                mp->colorspace = V4L2_COLORSPACE_REC709;
 445        }
 446        memset(mp->reserved, 0, sizeof(mp->reserved));
 447        return 0;
 448}
 449
 450int vivid_s_fmt_vid_out(struct file *file, void *priv,
 451                                        struct v4l2_format *f)
 452{
 453        struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
 454        struct vivid_dev *dev = video_drvdata(file);
 455        struct v4l2_rect *crop = &dev->crop_out;
 456        struct v4l2_rect *compose = &dev->compose_out;
 457        struct vb2_queue *q = &dev->vb_vid_out_q;
 458        int ret = vivid_try_fmt_vid_out(file, priv, f);
 459        unsigned factor = 1;
 460        unsigned p;
 461
 462        if (ret < 0)
 463                return ret;
 464
 465        if (vb2_is_busy(q) &&
 466            (vivid_is_svid_out(dev) ||
 467             mp->width != dev->fmt_out_rect.width ||
 468             mp->height != dev->fmt_out_rect.height ||
 469             mp->pixelformat != dev->fmt_out->fourcc ||
 470             mp->field != dev->field_out)) {
 471                dprintk(dev, 1, "%s device busy\n", __func__);
 472                return -EBUSY;
 473        }
 474
 475        /*
 476         * Allow for changing the colorspace on the fly. Useful for testing
 477         * purposes, and it is something that HDMI transmitters are able
 478         * to do.
 479         */
 480        if (vb2_is_busy(q))
 481                goto set_colorspace;
 482
 483        dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
 484        if (V4L2_FIELD_HAS_T_OR_B(mp->field))
 485                factor = 2;
 486
 487        if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
 488                struct v4l2_rect r = { 0, 0, mp->width, mp->height };
 489
 490                if (dev->has_scaler_out) {
 491                        if (dev->has_crop_out)
 492                                v4l2_rect_map_inside(crop, &r);
 493                        else
 494                                *crop = r;
 495                        if (dev->has_compose_out && !dev->has_crop_out) {
 496                                struct v4l2_rect min_r = {
 497                                        0, 0,
 498                                        r.width / MAX_ZOOM,
 499                                        factor * r.height / MAX_ZOOM
 500                                };
 501                                struct v4l2_rect max_r = {
 502                                        0, 0,
 503                                        r.width * MAX_ZOOM,
 504                                        factor * r.height * MAX_ZOOM
 505                                };
 506
 507                                v4l2_rect_set_min_size(compose, &min_r);
 508                                v4l2_rect_set_max_size(compose, &max_r);
 509                                v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
 510                        } else if (dev->has_compose_out) {
 511                                struct v4l2_rect min_r = {
 512                                        0, 0,
 513                                        crop->width / MAX_ZOOM,
 514                                        factor * crop->height / MAX_ZOOM
 515                                };
 516                                struct v4l2_rect max_r = {
 517                                        0, 0,
 518                                        crop->width * MAX_ZOOM,
 519                                        factor * crop->height * MAX_ZOOM
 520                                };
 521
 522                                v4l2_rect_set_min_size(compose, &min_r);
 523                                v4l2_rect_set_max_size(compose, &max_r);
 524                                v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
 525                        }
 526                } else if (dev->has_compose_out && !dev->has_crop_out) {
 527                        v4l2_rect_set_size_to(crop, &r);
 528                        r.height *= factor;
 529                        v4l2_rect_set_size_to(compose, &r);
 530                        v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
 531                } else if (!dev->has_compose_out) {
 532                        v4l2_rect_map_inside(crop, &r);
 533                        r.height /= factor;
 534                        v4l2_rect_set_size_to(compose, &r);
 535                } else {
 536                        r.height *= factor;
 537                        v4l2_rect_set_max_size(compose, &r);
 538                        v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
 539                        crop->top *= factor;
 540                        crop->height *= factor;
 541                        v4l2_rect_set_size_to(crop, compose);
 542                        v4l2_rect_map_inside(crop, &r);
 543                        crop->top /= factor;
 544                        crop->height /= factor;
 545                }
 546        } else {
 547                struct v4l2_rect r = { 0, 0, mp->width, mp->height };
 548
 549                v4l2_rect_set_size_to(crop, &r);
 550                r.height /= factor;
 551                v4l2_rect_set_size_to(compose, &r);
 552        }
 553
 554        dev->fmt_out_rect.width = mp->width;
 555        dev->fmt_out_rect.height = mp->height;
 556        for (p = 0; p < mp->num_planes; p++)
 557                dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
 558        for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
 559                dev->bytesperline_out[p] =
 560                        (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
 561                        dev->fmt_out->bit_depth[0];
 562        dev->field_out = mp->field;
 563        if (vivid_is_svid_out(dev))
 564                dev->tv_field_out = mp->field;
 565
 566set_colorspace:
 567        dev->colorspace_out = mp->colorspace;
 568        dev->xfer_func_out = mp->xfer_func;
 569        dev->ycbcr_enc_out = mp->ycbcr_enc;
 570        dev->quantization_out = mp->quantization;
 571        if (dev->loop_video) {
 572                vivid_send_source_change(dev, SVID);
 573                vivid_send_source_change(dev, HDMI);
 574        }
 575        return 0;
 576}
 577
 578int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
 579                                        struct v4l2_format *f)
 580{
 581        struct vivid_dev *dev = video_drvdata(file);
 582
 583        if (!dev->multiplanar)
 584                return -ENOTTY;
 585        return vivid_g_fmt_vid_out(file, priv, f);
 586}
 587
 588int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
 589                        struct v4l2_format *f)
 590{
 591        struct vivid_dev *dev = video_drvdata(file);
 592
 593        if (!dev->multiplanar)
 594                return -ENOTTY;
 595        return vivid_try_fmt_vid_out(file, priv, f);
 596}
 597
 598int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
 599                        struct v4l2_format *f)
 600{
 601        struct vivid_dev *dev = video_drvdata(file);
 602
 603        if (!dev->multiplanar)
 604                return -ENOTTY;
 605        return vivid_s_fmt_vid_out(file, priv, f);
 606}
 607
 608int vidioc_g_fmt_vid_out(struct file *file, void *priv,
 609                                        struct v4l2_format *f)
 610{
 611        struct vivid_dev *dev = video_drvdata(file);
 612
 613        if (dev->multiplanar)
 614                return -ENOTTY;
 615        return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
 616}
 617
 618int vidioc_try_fmt_vid_out(struct file *file, void *priv,
 619                        struct v4l2_format *f)
 620{
 621        struct vivid_dev *dev = video_drvdata(file);
 622
 623        if (dev->multiplanar)
 624                return -ENOTTY;
 625        return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
 626}
 627
 628int vidioc_s_fmt_vid_out(struct file *file, void *priv,
 629                        struct v4l2_format *f)
 630{
 631        struct vivid_dev *dev = video_drvdata(file);
 632
 633        if (dev->multiplanar)
 634                return -ENOTTY;
 635        return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
 636}
 637
 638int vivid_vid_out_g_selection(struct file *file, void *priv,
 639                              struct v4l2_selection *sel)
 640{
 641        struct vivid_dev *dev = video_drvdata(file);
 642
 643        if (!dev->has_crop_out && !dev->has_compose_out)
 644                return -ENOTTY;
 645        if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
 646                return -EINVAL;
 647
 648        sel->r.left = sel->r.top = 0;
 649        switch (sel->target) {
 650        case V4L2_SEL_TGT_CROP:
 651                if (!dev->has_crop_out)
 652                        return -EINVAL;
 653                sel->r = dev->crop_out;
 654                break;
 655        case V4L2_SEL_TGT_CROP_DEFAULT:
 656                if (!dev->has_crop_out)
 657                        return -EINVAL;
 658                sel->r = dev->fmt_out_rect;
 659                break;
 660        case V4L2_SEL_TGT_CROP_BOUNDS:
 661                if (!dev->has_crop_out)
 662                        return -EINVAL;
 663                sel->r = vivid_max_rect;
 664                break;
 665        case V4L2_SEL_TGT_COMPOSE:
 666                if (!dev->has_compose_out)
 667                        return -EINVAL;
 668                sel->r = dev->compose_out;
 669                break;
 670        case V4L2_SEL_TGT_COMPOSE_DEFAULT:
 671        case V4L2_SEL_TGT_COMPOSE_BOUNDS:
 672                if (!dev->has_compose_out)
 673                        return -EINVAL;
 674                sel->r = dev->sink_rect;
 675                break;
 676        default:
 677                return -EINVAL;
 678        }
 679        return 0;
 680}
 681
 682int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
 683{
 684        struct vivid_dev *dev = video_drvdata(file);
 685        struct v4l2_rect *crop = &dev->crop_out;
 686        struct v4l2_rect *compose = &dev->compose_out;
 687        unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
 688        int ret;
 689
 690        if (!dev->has_crop_out && !dev->has_compose_out)
 691                return -ENOTTY;
 692        if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
 693                return -EINVAL;
 694
 695        switch (s->target) {
 696        case V4L2_SEL_TGT_CROP:
 697                if (!dev->has_crop_out)
 698                        return -EINVAL;
 699                ret = vivid_vid_adjust_sel(s->flags, &s->r);
 700                if (ret)
 701                        return ret;
 702                v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
 703                v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
 704                if (dev->has_scaler_out) {
 705                        struct v4l2_rect max_rect = {
 706                                0, 0,
 707                                dev->sink_rect.width * MAX_ZOOM,
 708                                (dev->sink_rect.height / factor) * MAX_ZOOM
 709                        };
 710
 711                        v4l2_rect_set_max_size(&s->r, &max_rect);
 712                        if (dev->has_compose_out) {
 713                                struct v4l2_rect min_rect = {
 714                                        0, 0,
 715                                        s->r.width / MAX_ZOOM,
 716                                        (s->r.height * factor) / MAX_ZOOM
 717                                };
 718                                struct v4l2_rect max_rect = {
 719                                        0, 0,
 720                                        s->r.width * MAX_ZOOM,
 721                                        (s->r.height * factor) * MAX_ZOOM
 722                                };
 723
 724                                v4l2_rect_set_min_size(compose, &min_rect);
 725                                v4l2_rect_set_max_size(compose, &max_rect);
 726                                v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
 727                        }
 728                } else if (dev->has_compose_out) {
 729                        s->r.top *= factor;
 730                        s->r.height *= factor;
 731                        v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
 732                        v4l2_rect_set_size_to(compose, &s->r);
 733                        v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
 734                        s->r.top /= factor;
 735                        s->r.height /= factor;
 736                } else {
 737                        v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
 738                        s->r.height /= factor;
 739                }
 740                v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
 741                *crop = s->r;
 742                break;
 743        case V4L2_SEL_TGT_COMPOSE:
 744                if (!dev->has_compose_out)
 745                        return -EINVAL;
 746                ret = vivid_vid_adjust_sel(s->flags, &s->r);
 747                if (ret)
 748                        return ret;
 749                v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
 750                v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
 751                v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
 752                s->r.top /= factor;
 753                s->r.height /= factor;
 754                if (dev->has_scaler_out) {
 755                        struct v4l2_rect fmt = dev->fmt_out_rect;
 756                        struct v4l2_rect max_rect = {
 757                                0, 0,
 758                                s->r.width * MAX_ZOOM,
 759                                s->r.height * MAX_ZOOM
 760                        };
 761                        struct v4l2_rect min_rect = {
 762                                0, 0,
 763                                s->r.width / MAX_ZOOM,
 764                                s->r.height / MAX_ZOOM
 765                        };
 766
 767                        v4l2_rect_set_min_size(&fmt, &min_rect);
 768                        if (!dev->has_crop_out)
 769                                v4l2_rect_set_max_size(&fmt, &max_rect);
 770                        if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
 771                            vb2_is_busy(&dev->vb_vid_out_q))
 772                                return -EBUSY;
 773                        if (dev->has_crop_out) {
 774                                v4l2_rect_set_min_size(crop, &min_rect);
 775                                v4l2_rect_set_max_size(crop, &max_rect);
 776                        }
 777                        dev->fmt_out_rect = fmt;
 778                } else if (dev->has_crop_out) {
 779                        struct v4l2_rect fmt = dev->fmt_out_rect;
 780
 781                        v4l2_rect_set_min_size(&fmt, &s->r);
 782                        if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
 783                            vb2_is_busy(&dev->vb_vid_out_q))
 784                                return -EBUSY;
 785                        dev->fmt_out_rect = fmt;
 786                        v4l2_rect_set_size_to(crop, &s->r);
 787                        v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
 788                } else {
 789                        if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
 790                            vb2_is_busy(&dev->vb_vid_out_q))
 791                                return -EBUSY;
 792                        v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
 793                        v4l2_rect_set_size_to(crop, &s->r);
 794                        crop->height /= factor;
 795                        v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
 796                }
 797                s->r.top *= factor;
 798                s->r.height *= factor;
 799                if (dev->bitmap_out && (compose->width != s->r.width ||
 800                                        compose->height != s->r.height)) {
 801                        vfree(dev->bitmap_out);
 802                        dev->bitmap_out = NULL;
 803                }
 804                *compose = s->r;
 805                break;
 806        default:
 807                return -EINVAL;
 808        }
 809
 810        return 0;
 811}
 812
 813int vivid_vid_out_g_pixelaspect(struct file *file, void *priv,
 814                                int type, struct v4l2_fract *f)
 815{
 816        struct vivid_dev *dev = video_drvdata(file);
 817
 818        if (type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
 819                return -EINVAL;
 820
 821        switch (vivid_get_pixel_aspect(dev)) {
 822        case TPG_PIXEL_ASPECT_NTSC:
 823                f->numerator = 11;
 824                f->denominator = 10;
 825                break;
 826        case TPG_PIXEL_ASPECT_PAL:
 827                f->numerator = 54;
 828                f->denominator = 59;
 829                break;
 830        default:
 831                break;
 832        }
 833        return 0;
 834}
 835
 836int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
 837                                        struct v4l2_format *f)
 838{
 839        struct vivid_dev *dev = video_drvdata(file);
 840        const struct v4l2_rect *compose = &dev->compose_out;
 841        struct v4l2_window *win = &f->fmt.win;
 842        unsigned clipcount = win->clipcount;
 843
 844        if (!dev->has_fb)
 845                return -EINVAL;
 846        win->w.top = dev->overlay_out_top;
 847        win->w.left = dev->overlay_out_left;
 848        win->w.width = compose->width;
 849        win->w.height = compose->height;
 850        win->clipcount = dev->clipcount_out;
 851        win->field = V4L2_FIELD_ANY;
 852        win->chromakey = dev->chromakey_out;
 853        win->global_alpha = dev->global_alpha_out;
 854        if (clipcount > dev->clipcount_out)
 855                clipcount = dev->clipcount_out;
 856        if (dev->bitmap_out == NULL)
 857                win->bitmap = NULL;
 858        else if (win->bitmap) {
 859                if (copy_to_user(win->bitmap, dev->bitmap_out,
 860                    ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
 861                        return -EFAULT;
 862        }
 863        if (clipcount && win->clips) {
 864                if (copy_to_user(win->clips, dev->clips_out,
 865                                 clipcount * sizeof(dev->clips_out[0])))
 866                        return -EFAULT;
 867        }
 868        return 0;
 869}
 870
 871int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
 872                                        struct v4l2_format *f)
 873{
 874        struct vivid_dev *dev = video_drvdata(file);
 875        const struct v4l2_rect *compose = &dev->compose_out;
 876        struct v4l2_window *win = &f->fmt.win;
 877        int i, j;
 878
 879        if (!dev->has_fb)
 880                return -EINVAL;
 881        win->w.left = clamp_t(int, win->w.left,
 882                              -dev->display_width, dev->display_width);
 883        win->w.top = clamp_t(int, win->w.top,
 884                             -dev->display_height, dev->display_height);
 885        win->w.width = compose->width;
 886        win->w.height = compose->height;
 887        /*
 888         * It makes no sense for an OSD to overlay only top or bottom fields,
 889         * so always set this to ANY.
 890         */
 891        win->field = V4L2_FIELD_ANY;
 892        if (win->clipcount && !win->clips)
 893                win->clipcount = 0;
 894        if (win->clipcount > MAX_CLIPS)
 895                win->clipcount = MAX_CLIPS;
 896        if (win->clipcount) {
 897                if (copy_from_user(dev->try_clips_out, win->clips,
 898                                   win->clipcount * sizeof(dev->clips_out[0])))
 899                        return -EFAULT;
 900                for (i = 0; i < win->clipcount; i++) {
 901                        struct v4l2_rect *r = &dev->try_clips_out[i].c;
 902
 903                        r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
 904                        r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
 905                        r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
 906                        r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
 907                }
 908                /*
 909                 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
 910                 * number and it's typically a one-time deal.
 911                 */
 912                for (i = 0; i < win->clipcount - 1; i++) {
 913                        struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
 914
 915                        for (j = i + 1; j < win->clipcount; j++) {
 916                                struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
 917
 918                                if (v4l2_rect_overlap(r1, r2))
 919                                        return -EINVAL;
 920                        }
 921                }
 922                if (copy_to_user(win->clips, dev->try_clips_out,
 923                                 win->clipcount * sizeof(dev->clips_out[0])))
 924                        return -EFAULT;
 925        }
 926        return 0;
 927}
 928
 929int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
 930                                        struct v4l2_format *f)
 931{
 932        struct vivid_dev *dev = video_drvdata(file);
 933        const struct v4l2_rect *compose = &dev->compose_out;
 934        struct v4l2_window *win = &f->fmt.win;
 935        int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
 936        unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
 937        unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
 938        void *new_bitmap = NULL;
 939
 940        if (ret)
 941                return ret;
 942
 943        if (win->bitmap) {
 944                new_bitmap = vzalloc(bitmap_size);
 945
 946                if (!new_bitmap)
 947                        return -ENOMEM;
 948                if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
 949                        vfree(new_bitmap);
 950                        return -EFAULT;
 951                }
 952        }
 953
 954        dev->overlay_out_top = win->w.top;
 955        dev->overlay_out_left = win->w.left;
 956        vfree(dev->bitmap_out);
 957        dev->bitmap_out = new_bitmap;
 958        dev->clipcount_out = win->clipcount;
 959        if (dev->clipcount_out)
 960                memcpy(dev->clips_out, dev->try_clips_out, clips_size);
 961        dev->chromakey_out = win->chromakey;
 962        dev->global_alpha_out = win->global_alpha;
 963        return ret;
 964}
 965
 966int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
 967{
 968        struct vivid_dev *dev = video_drvdata(file);
 969
 970        if (i && !dev->fmt_out->can_do_overlay) {
 971                dprintk(dev, 1, "unsupported output format for output overlay\n");
 972                return -EINVAL;
 973        }
 974
 975        dev->overlay_out_enabled = i;
 976        return 0;
 977}
 978
 979int vivid_vid_out_g_fbuf(struct file *file, void *fh,
 980                                struct v4l2_framebuffer *a)
 981{
 982        struct vivid_dev *dev = video_drvdata(file);
 983
 984        a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
 985                        V4L2_FBUF_CAP_BITMAP_CLIPPING |
 986                        V4L2_FBUF_CAP_LIST_CLIPPING |
 987                        V4L2_FBUF_CAP_CHROMAKEY |
 988                        V4L2_FBUF_CAP_SRC_CHROMAKEY |
 989                        V4L2_FBUF_CAP_GLOBAL_ALPHA |
 990                        V4L2_FBUF_CAP_LOCAL_ALPHA |
 991                        V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
 992        a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
 993        a->base = (void *)dev->video_pbase;
 994        a->fmt.width = dev->display_width;
 995        a->fmt.height = dev->display_height;
 996        if (dev->fb_defined.green.length == 5)
 997                a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
 998        else
 999                a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
1000        a->fmt.bytesperline = dev->display_byte_stride;
1001        a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
1002        a->fmt.field = V4L2_FIELD_NONE;
1003        a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1004        a->fmt.priv = 0;
1005        return 0;
1006}
1007
1008int vivid_vid_out_s_fbuf(struct file *file, void *fh,
1009                                const struct v4l2_framebuffer *a)
1010{
1011        struct vivid_dev *dev = video_drvdata(file);
1012        const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
1013                                      V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1014        const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
1015                                     V4L2_FBUF_FLAG_LOCAL_ALPHA |
1016                                     V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1017
1018
1019        if ((a->flags & chroma_flags) == chroma_flags)
1020                return -EINVAL;
1021        switch (a->flags & alpha_flags) {
1022        case 0:
1023        case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1024        case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1025        case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1026                break;
1027        default:
1028                return -EINVAL;
1029        }
1030        dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1031        dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
1032        return 0;
1033}
1034
1035static const struct v4l2_audioout vivid_audio_outputs[] = {
1036        { 0, "Line-Out 1" },
1037        { 1, "Line-Out 2" },
1038};
1039
1040int vidioc_enum_output(struct file *file, void *priv,
1041                                struct v4l2_output *out)
1042{
1043        struct vivid_dev *dev = video_drvdata(file);
1044
1045        if (out->index >= dev->num_outputs)
1046                return -EINVAL;
1047
1048        out->type = V4L2_OUTPUT_TYPE_ANALOG;
1049        switch (dev->output_type[out->index]) {
1050        case SVID:
1051                snprintf(out->name, sizeof(out->name), "S-Video %u",
1052                                dev->output_name_counter[out->index]);
1053                out->std = V4L2_STD_ALL;
1054                if (dev->has_audio_outputs)
1055                        out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1056                out->capabilities = V4L2_OUT_CAP_STD;
1057                break;
1058        case HDMI:
1059                snprintf(out->name, sizeof(out->name), "HDMI %u",
1060                                dev->output_name_counter[out->index]);
1061                out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1062                break;
1063        }
1064        return 0;
1065}
1066
1067int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1068{
1069        struct vivid_dev *dev = video_drvdata(file);
1070
1071        *o = dev->output;
1072        return 0;
1073}
1074
1075int vidioc_s_output(struct file *file, void *priv, unsigned o)
1076{
1077        struct vivid_dev *dev = video_drvdata(file);
1078
1079        if (o >= dev->num_outputs)
1080                return -EINVAL;
1081
1082        if (o == dev->output)
1083                return 0;
1084
1085        if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1086                return -EBUSY;
1087
1088        dev->output = o;
1089        dev->tv_audio_output = 0;
1090        if (dev->output_type[o] == SVID)
1091                dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1092        else
1093                dev->vid_out_dev.tvnorms = 0;
1094
1095        dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1096        vivid_update_format_out(dev);
1097
1098        v4l2_ctrl_activate(dev->ctrl_display_present, vivid_is_hdmi_out(dev));
1099        if (vivid_is_hdmi_out(dev))
1100                v4l2_ctrl_s_ctrl(dev->ctrl_display_present,
1101                                 dev->display_present[dev->output]);
1102
1103        return 0;
1104}
1105
1106int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1107{
1108        if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1109                return -EINVAL;
1110        *vout = vivid_audio_outputs[vout->index];
1111        return 0;
1112}
1113
1114int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1115{
1116        struct vivid_dev *dev = video_drvdata(file);
1117
1118        if (!vivid_is_svid_out(dev))
1119                return -EINVAL;
1120        *vout = vivid_audio_outputs[dev->tv_audio_output];
1121        return 0;
1122}
1123
1124int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1125{
1126        struct vivid_dev *dev = video_drvdata(file);
1127
1128        if (!vivid_is_svid_out(dev))
1129                return -EINVAL;
1130        if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1131                return -EINVAL;
1132        dev->tv_audio_output = vout->index;
1133        return 0;
1134}
1135
1136int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1137{
1138        struct vivid_dev *dev = video_drvdata(file);
1139
1140        if (!vivid_is_svid_out(dev))
1141                return -ENODATA;
1142        if (dev->std_out == id)
1143                return 0;
1144        if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1145                return -EBUSY;
1146        dev->std_out = id;
1147        vivid_update_format_out(dev);
1148        return 0;
1149}
1150
1151static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1152{
1153        struct v4l2_bt_timings *bt = &timings->bt;
1154
1155        if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1156            v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1157                return true;
1158
1159        return false;
1160}
1161
1162int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1163                                    struct v4l2_dv_timings *timings)
1164{
1165        struct vivid_dev *dev = video_drvdata(file);
1166        if (!vivid_is_hdmi_out(dev))
1167                return -ENODATA;
1168        if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1169                                0, NULL, NULL) &&
1170            !valid_cvt_gtf_timings(timings))
1171                return -EINVAL;
1172        if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
1173                return 0;
1174        if (vb2_is_busy(&dev->vb_vid_out_q))
1175                return -EBUSY;
1176        dev->dv_timings_out = *timings;
1177        vivid_update_format_out(dev);
1178        return 0;
1179}
1180
1181int vivid_vid_out_g_parm(struct file *file, void *priv,
1182                          struct v4l2_streamparm *parm)
1183{
1184        struct vivid_dev *dev = video_drvdata(file);
1185
1186        if (parm->type != (dev->multiplanar ?
1187                           V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1188                           V4L2_BUF_TYPE_VIDEO_OUTPUT))
1189                return -EINVAL;
1190
1191        parm->parm.output.capability   = V4L2_CAP_TIMEPERFRAME;
1192        parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1193        parm->parm.output.writebuffers  = 1;
1194
1195        return 0;
1196}
1197
1198int vidioc_subscribe_event(struct v4l2_fh *fh,
1199                        const struct v4l2_event_subscription *sub)
1200{
1201        switch (sub->type) {
1202        case V4L2_EVENT_SOURCE_CHANGE:
1203                if (fh->vdev->vfl_dir == VFL_DIR_RX)
1204                        return v4l2_src_change_event_subscribe(fh, sub);
1205                break;
1206        default:
1207                return v4l2_ctrl_subscribe_event(fh, sub);
1208        }
1209        return -EINVAL;
1210}
1211