linux/drivers/media/platform/vivid/vivid-vid-cap.c
<<
>>
Prefs
   1// SPDX-License-Identifier: GPL-2.0-only
   2/*
   3 * vivid-vid-cap.c - video capture 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/vmalloc.h>
  12#include <linux/videodev2.h>
  13#include <linux/v4l2-dv-timings.h>
  14#include <media/v4l2-common.h>
  15#include <media/v4l2-event.h>
  16#include <media/v4l2-dv-timings.h>
  17#include <media/v4l2-rect.h>
  18
  19#include "vivid-core.h"
  20#include "vivid-vid-common.h"
  21#include "vivid-kthread-cap.h"
  22#include "vivid-vid-cap.h"
  23
  24/* timeperframe: min/max and default */
  25static const struct v4l2_fract
  26        tpf_min     = {.numerator = 1,          .denominator = FPS_MAX},
  27        tpf_max     = {.numerator = FPS_MAX,    .denominator = 1};
  28
  29static const struct vivid_fmt formats_ovl[] = {
  30        {
  31                .fourcc   = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
  32                .vdownsampling = { 1 },
  33                .bit_depth = { 16 },
  34                .planes   = 1,
  35                .buffers = 1,
  36        },
  37        {
  38                .fourcc   = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
  39                .vdownsampling = { 1 },
  40                .bit_depth = { 16 },
  41                .planes   = 1,
  42                .buffers = 1,
  43        },
  44        {
  45                .fourcc   = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
  46                .vdownsampling = { 1 },
  47                .bit_depth = { 16 },
  48                .planes   = 1,
  49                .buffers = 1,
  50        },
  51};
  52
  53/* The number of discrete webcam framesizes */
  54#define VIVID_WEBCAM_SIZES 5
  55/* The number of discrete webcam frameintervals */
  56#define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
  57
  58/* Sizes must be in increasing order */
  59static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
  60        {  320, 180 },
  61        {  640, 360 },
  62        { 1280, 720 },
  63        { 1920, 1080 },
  64        { 3840, 2160 },
  65};
  66
  67/*
  68 * Intervals must be in increasing order and there must be twice as many
  69 * elements in this array as there are in webcam_sizes.
  70 */
  71static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
  72        {  1, 1 },
  73        {  1, 2 },
  74        {  1, 4 },
  75        {  1, 5 },
  76        {  1, 10 },
  77        {  1, 15 },
  78        {  1, 25 },
  79        {  1, 30 },
  80        {  1, 50 },
  81        {  1, 60 },
  82};
  83
  84static int vid_cap_queue_setup(struct vb2_queue *vq,
  85                       unsigned *nbuffers, unsigned *nplanes,
  86                       unsigned sizes[], struct device *alloc_devs[])
  87{
  88        struct vivid_dev *dev = vb2_get_drv_priv(vq);
  89        unsigned buffers = tpg_g_buffers(&dev->tpg);
  90        unsigned h = dev->fmt_cap_rect.height;
  91        unsigned p;
  92
  93        if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
  94                /*
  95                 * You cannot use read() with FIELD_ALTERNATE since the field
  96                 * information (TOP/BOTTOM) cannot be passed back to the user.
  97                 */
  98                if (vb2_fileio_is_active(vq))
  99                        return -EINVAL;
 100        }
 101
 102        if (dev->queue_setup_error) {
 103                /*
 104                 * Error injection: test what happens if queue_setup() returns
 105                 * an error.
 106                 */
 107                dev->queue_setup_error = false;
 108                return -EINVAL;
 109        }
 110        if (*nplanes) {
 111                /*
 112                 * Check if the number of requested planes match
 113                 * the number of buffers in the current format. You can't mix that.
 114                 */
 115                if (*nplanes != buffers)
 116                        return -EINVAL;
 117                for (p = 0; p < buffers; p++) {
 118                        if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
 119                                                dev->fmt_cap->data_offset[p])
 120                                return -EINVAL;
 121                }
 122        } else {
 123                for (p = 0; p < buffers; p++)
 124                        sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
 125                                        dev->fmt_cap->data_offset[p];
 126        }
 127
 128        if (vq->num_buffers + *nbuffers < 2)
 129                *nbuffers = 2 - vq->num_buffers;
 130
 131        *nplanes = buffers;
 132
 133        dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
 134        for (p = 0; p < buffers; p++)
 135                dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
 136
 137        return 0;
 138}
 139
 140static int vid_cap_buf_prepare(struct vb2_buffer *vb)
 141{
 142        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 143        unsigned long size;
 144        unsigned buffers = tpg_g_buffers(&dev->tpg);
 145        unsigned p;
 146
 147        dprintk(dev, 1, "%s\n", __func__);
 148
 149        if (WARN_ON(NULL == dev->fmt_cap))
 150                return -EINVAL;
 151
 152        if (dev->buf_prepare_error) {
 153                /*
 154                 * Error injection: test what happens if buf_prepare() returns
 155                 * an error.
 156                 */
 157                dev->buf_prepare_error = false;
 158                return -EINVAL;
 159        }
 160        for (p = 0; p < buffers; p++) {
 161                size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
 162                        dev->fmt_cap->data_offset[p];
 163
 164                if (vb2_plane_size(vb, p) < size) {
 165                        dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
 166                                        __func__, p, vb2_plane_size(vb, p), size);
 167                        return -EINVAL;
 168                }
 169
 170                vb2_set_plane_payload(vb, p, size);
 171                vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
 172        }
 173
 174        return 0;
 175}
 176
 177static void vid_cap_buf_finish(struct vb2_buffer *vb)
 178{
 179        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 180        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 181        struct v4l2_timecode *tc = &vbuf->timecode;
 182        unsigned fps = 25;
 183        unsigned seq = vbuf->sequence;
 184
 185        if (!vivid_is_sdtv_cap(dev))
 186                return;
 187
 188        /*
 189         * Set the timecode. Rarely used, so it is interesting to
 190         * test this.
 191         */
 192        vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
 193        if (dev->std_cap & V4L2_STD_525_60)
 194                fps = 30;
 195        tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
 196        tc->flags = 0;
 197        tc->frames = seq % fps;
 198        tc->seconds = (seq / fps) % 60;
 199        tc->minutes = (seq / (60 * fps)) % 60;
 200        tc->hours = (seq / (60 * 60 * fps)) % 24;
 201}
 202
 203static void vid_cap_buf_queue(struct vb2_buffer *vb)
 204{
 205        struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
 206        struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
 207        struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
 208
 209        dprintk(dev, 1, "%s\n", __func__);
 210
 211        spin_lock(&dev->slock);
 212        list_add_tail(&buf->list, &dev->vid_cap_active);
 213        spin_unlock(&dev->slock);
 214}
 215
 216static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
 217{
 218        struct vivid_dev *dev = vb2_get_drv_priv(vq);
 219        unsigned i;
 220        int err;
 221
 222        if (vb2_is_streaming(&dev->vb_vid_out_q))
 223                dev->can_loop_video = vivid_vid_can_loop(dev);
 224
 225        if (dev->kthread_vid_cap)
 226                return 0;
 227
 228        dev->vid_cap_seq_count = 0;
 229        dprintk(dev, 1, "%s\n", __func__);
 230        for (i = 0; i < VIDEO_MAX_FRAME; i++)
 231                dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
 232        if (dev->start_streaming_error) {
 233                dev->start_streaming_error = false;
 234                err = -EINVAL;
 235        } else {
 236                err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
 237        }
 238        if (err) {
 239                struct vivid_buffer *buf, *tmp;
 240
 241                list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
 242                        list_del(&buf->list);
 243                        vb2_buffer_done(&buf->vb.vb2_buf,
 244                                        VB2_BUF_STATE_QUEUED);
 245                }
 246        }
 247        return err;
 248}
 249
 250/* abort streaming and wait for last buffer */
 251static void vid_cap_stop_streaming(struct vb2_queue *vq)
 252{
 253        struct vivid_dev *dev = vb2_get_drv_priv(vq);
 254
 255        dprintk(dev, 1, "%s\n", __func__);
 256        vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
 257        dev->can_loop_video = false;
 258}
 259
 260const struct vb2_ops vivid_vid_cap_qops = {
 261        .queue_setup            = vid_cap_queue_setup,
 262        .buf_prepare            = vid_cap_buf_prepare,
 263        .buf_finish             = vid_cap_buf_finish,
 264        .buf_queue              = vid_cap_buf_queue,
 265        .start_streaming        = vid_cap_start_streaming,
 266        .stop_streaming         = vid_cap_stop_streaming,
 267        .wait_prepare           = vb2_ops_wait_prepare,
 268        .wait_finish            = vb2_ops_wait_finish,
 269};
 270
 271/*
 272 * Determine the 'picture' quality based on the current TV frequency: either
 273 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
 274 * signal or NOISE for no signal.
 275 */
 276void vivid_update_quality(struct vivid_dev *dev)
 277{
 278        unsigned freq_modulus;
 279
 280        if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
 281                /*
 282                 * The 'noise' will only be replaced by the actual video
 283                 * if the output video matches the input video settings.
 284                 */
 285                tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
 286                return;
 287        }
 288        if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
 289                tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
 290                return;
 291        }
 292        if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
 293                tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
 294                return;
 295        }
 296        if (!vivid_is_tv_cap(dev)) {
 297                tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
 298                return;
 299        }
 300
 301        /*
 302         * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
 303         * From +/- 0.25 MHz around the channel there is color, and from
 304         * +/- 1 MHz there is grayscale (chroma is lost).
 305         * Everywhere else it is just noise.
 306         */
 307        freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
 308        if (freq_modulus > 2 * 16) {
 309                tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
 310                        next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
 311                return;
 312        }
 313        if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
 314                tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
 315        else
 316                tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
 317}
 318
 319/*
 320 * Get the current picture quality and the associated afc value.
 321 */
 322static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
 323{
 324        unsigned freq_modulus;
 325
 326        if (afc)
 327                *afc = 0;
 328        if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
 329            tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
 330                return tpg_g_quality(&dev->tpg);
 331
 332        /*
 333         * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
 334         * From +/- 0.25 MHz around the channel there is color, and from
 335         * +/- 1 MHz there is grayscale (chroma is lost).
 336         * Everywhere else it is just gray.
 337         */
 338        freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
 339        if (afc)
 340                *afc = freq_modulus - 1 * 16;
 341        return TPG_QUAL_GRAY;
 342}
 343
 344enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
 345{
 346        if (vivid_is_sdtv_cap(dev))
 347                return dev->std_aspect_ratio;
 348
 349        if (vivid_is_hdmi_cap(dev))
 350                return dev->dv_timings_aspect_ratio;
 351
 352        return TPG_VIDEO_ASPECT_IMAGE;
 353}
 354
 355static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
 356{
 357        if (vivid_is_sdtv_cap(dev))
 358                return (dev->std_cap & V4L2_STD_525_60) ?
 359                        TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
 360
 361        if (vivid_is_hdmi_cap(dev) &&
 362            dev->src_rect.width == 720 && dev->src_rect.height <= 576)
 363                return dev->src_rect.height == 480 ?
 364                        TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
 365
 366        return TPG_PIXEL_ASPECT_SQUARE;
 367}
 368
 369/*
 370 * Called whenever the format has to be reset which can occur when
 371 * changing inputs, standard, timings, etc.
 372 */
 373void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
 374{
 375        struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
 376        unsigned size;
 377        u64 pixelclock;
 378
 379        switch (dev->input_type[dev->input]) {
 380        case WEBCAM:
 381        default:
 382                dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
 383                dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
 384                dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
 385                dev->field_cap = V4L2_FIELD_NONE;
 386                tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
 387                break;
 388        case TV:
 389        case SVID:
 390                dev->field_cap = dev->tv_field_cap;
 391                dev->src_rect.width = 720;
 392                if (dev->std_cap & V4L2_STD_525_60) {
 393                        dev->src_rect.height = 480;
 394                        dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
 395                        dev->service_set_cap = V4L2_SLICED_CAPTION_525;
 396                } else {
 397                        dev->src_rect.height = 576;
 398                        dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
 399                        dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
 400                }
 401                tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
 402                break;
 403        case HDMI:
 404                dev->src_rect.width = bt->width;
 405                dev->src_rect.height = bt->height;
 406                size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
 407                if (dev->reduced_fps && can_reduce_fps(bt)) {
 408                        pixelclock = div_u64(bt->pixelclock * 1000, 1001);
 409                        bt->flags |= V4L2_DV_FL_REDUCED_FPS;
 410                } else {
 411                        pixelclock = bt->pixelclock;
 412                        bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
 413                }
 414                dev->timeperframe_vid_cap = (struct v4l2_fract) {
 415                        size / 100, (u32)pixelclock / 100
 416                };
 417                if (bt->interlaced)
 418                        dev->field_cap = V4L2_FIELD_ALTERNATE;
 419                else
 420                        dev->field_cap = V4L2_FIELD_NONE;
 421
 422                /*
 423                 * We can be called from within s_ctrl, in that case we can't
 424                 * set/get controls. Luckily we don't need to in that case.
 425                 */
 426                if (keep_controls || !dev->colorspace)
 427                        break;
 428                if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
 429                        if (bt->width == 720 && bt->height <= 576)
 430                                v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
 431                        else
 432                                v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
 433                        v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
 434                } else {
 435                        v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
 436                        v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
 437                }
 438                tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
 439                break;
 440        }
 441        vivid_update_quality(dev);
 442        tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
 443        dev->crop_cap = dev->src_rect;
 444        dev->crop_bounds_cap = dev->src_rect;
 445        dev->compose_cap = dev->crop_cap;
 446        if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
 447                dev->compose_cap.height /= 2;
 448        dev->fmt_cap_rect = dev->compose_cap;
 449        tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
 450        tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
 451        tpg_update_mv_step(&dev->tpg);
 452}
 453
 454/* Map the field to something that is valid for the current input */
 455static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
 456{
 457        if (vivid_is_sdtv_cap(dev)) {
 458                switch (field) {
 459                case V4L2_FIELD_INTERLACED_TB:
 460                case V4L2_FIELD_INTERLACED_BT:
 461                case V4L2_FIELD_SEQ_TB:
 462                case V4L2_FIELD_SEQ_BT:
 463                case V4L2_FIELD_TOP:
 464                case V4L2_FIELD_BOTTOM:
 465                case V4L2_FIELD_ALTERNATE:
 466                        return field;
 467                case V4L2_FIELD_INTERLACED:
 468                default:
 469                        return V4L2_FIELD_INTERLACED;
 470                }
 471        }
 472        if (vivid_is_hdmi_cap(dev))
 473                return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
 474                                                       V4L2_FIELD_NONE;
 475        return V4L2_FIELD_NONE;
 476}
 477
 478static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
 479{
 480        if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
 481                return tpg_g_colorspace(&dev->tpg);
 482        return dev->colorspace_out;
 483}
 484
 485static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
 486{
 487        if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
 488                return tpg_g_xfer_func(&dev->tpg);
 489        return dev->xfer_func_out;
 490}
 491
 492static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
 493{
 494        if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
 495                return tpg_g_ycbcr_enc(&dev->tpg);
 496        return dev->ycbcr_enc_out;
 497}
 498
 499static unsigned int vivid_hsv_enc_cap(struct vivid_dev *dev)
 500{
 501        if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
 502                return tpg_g_hsv_enc(&dev->tpg);
 503        return dev->hsv_enc_out;
 504}
 505
 506static unsigned vivid_quantization_cap(struct vivid_dev *dev)
 507{
 508        if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
 509                return tpg_g_quantization(&dev->tpg);
 510        return dev->quantization_out;
 511}
 512
 513int vivid_g_fmt_vid_cap(struct file *file, void *priv,
 514                                        struct v4l2_format *f)
 515{
 516        struct vivid_dev *dev = video_drvdata(file);
 517        struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
 518        unsigned p;
 519
 520        mp->width        = dev->fmt_cap_rect.width;
 521        mp->height       = dev->fmt_cap_rect.height;
 522        mp->field        = dev->field_cap;
 523        mp->pixelformat  = dev->fmt_cap->fourcc;
 524        mp->colorspace   = vivid_colorspace_cap(dev);
 525        mp->xfer_func    = vivid_xfer_func_cap(dev);
 526        if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_HSV)
 527                mp->hsv_enc    = vivid_hsv_enc_cap(dev);
 528        else
 529                mp->ycbcr_enc    = vivid_ycbcr_enc_cap(dev);
 530        mp->quantization = vivid_quantization_cap(dev);
 531        mp->num_planes = dev->fmt_cap->buffers;
 532        for (p = 0; p < mp->num_planes; p++) {
 533                mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
 534                mp->plane_fmt[p].sizeimage =
 535                        tpg_g_line_width(&dev->tpg, p) * mp->height +
 536                        dev->fmt_cap->data_offset[p];
 537        }
 538        return 0;
 539}
 540
 541int vivid_try_fmt_vid_cap(struct file *file, void *priv,
 542                        struct v4l2_format *f)
 543{
 544        struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
 545        struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
 546        struct vivid_dev *dev = video_drvdata(file);
 547        const struct vivid_fmt *fmt;
 548        unsigned bytesperline, max_bpl;
 549        unsigned factor = 1;
 550        unsigned w, h;
 551        unsigned p;
 552
 553        fmt = vivid_get_format(dev, mp->pixelformat);
 554        if (!fmt) {
 555                dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
 556                        mp->pixelformat);
 557                mp->pixelformat = V4L2_PIX_FMT_YUYV;
 558                fmt = vivid_get_format(dev, mp->pixelformat);
 559        }
 560
 561        mp->field = vivid_field_cap(dev, mp->field);
 562        if (vivid_is_webcam(dev)) {
 563                const struct v4l2_frmsize_discrete *sz =
 564                        v4l2_find_nearest_size(webcam_sizes,
 565                                               VIVID_WEBCAM_SIZES, width,
 566                                               height, mp->width, mp->height);
 567
 568                w = sz->width;
 569                h = sz->height;
 570        } else if (vivid_is_sdtv_cap(dev)) {
 571                w = 720;
 572                h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
 573        } else {
 574                w = dev->src_rect.width;
 575                h = dev->src_rect.height;
 576        }
 577        if (V4L2_FIELD_HAS_T_OR_B(mp->field))
 578                factor = 2;
 579        if (vivid_is_webcam(dev) ||
 580            (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
 581                mp->width = w;
 582                mp->height = h / factor;
 583        } else {
 584                struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
 585
 586                v4l2_rect_set_min_size(&r, &vivid_min_rect);
 587                v4l2_rect_set_max_size(&r, &vivid_max_rect);
 588                if (dev->has_scaler_cap && !dev->has_compose_cap) {
 589                        struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
 590
 591                        v4l2_rect_set_max_size(&r, &max_r);
 592                } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
 593                        v4l2_rect_set_max_size(&r, &dev->src_rect);
 594                } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
 595                        v4l2_rect_set_min_size(&r, &dev->src_rect);
 596                }
 597                mp->width = r.width;
 598                mp->height = r.height / factor;
 599        }
 600
 601        /* This driver supports custom bytesperline values */
 602
 603        mp->num_planes = fmt->buffers;
 604        for (p = 0; p < fmt->buffers; p++) {
 605                /* Calculate the minimum supported bytesperline value */
 606                bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
 607                /* Calculate the maximum supported bytesperline value */
 608                max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
 609
 610                if (pfmt[p].bytesperline > max_bpl)
 611                        pfmt[p].bytesperline = max_bpl;
 612                if (pfmt[p].bytesperline < bytesperline)
 613                        pfmt[p].bytesperline = bytesperline;
 614
 615                pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
 616                                fmt->vdownsampling[p] + fmt->data_offset[p];
 617
 618                memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
 619        }
 620        for (p = fmt->buffers; p < fmt->planes; p++)
 621                pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
 622                        (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
 623                        (fmt->bit_depth[0] / fmt->vdownsampling[0]);
 624
 625        mp->colorspace = vivid_colorspace_cap(dev);
 626        if (fmt->color_enc == TGP_COLOR_ENC_HSV)
 627                mp->hsv_enc = vivid_hsv_enc_cap(dev);
 628        else
 629                mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
 630        mp->xfer_func = vivid_xfer_func_cap(dev);
 631        mp->quantization = vivid_quantization_cap(dev);
 632        memset(mp->reserved, 0, sizeof(mp->reserved));
 633        return 0;
 634}
 635
 636int vivid_s_fmt_vid_cap(struct file *file, void *priv,
 637                                        struct v4l2_format *f)
 638{
 639        struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
 640        struct vivid_dev *dev = video_drvdata(file);
 641        struct v4l2_rect *crop = &dev->crop_cap;
 642        struct v4l2_rect *compose = &dev->compose_cap;
 643        struct vb2_queue *q = &dev->vb_vid_cap_q;
 644        int ret = vivid_try_fmt_vid_cap(file, priv, f);
 645        unsigned factor = 1;
 646        unsigned p;
 647        unsigned i;
 648
 649        if (ret < 0)
 650                return ret;
 651
 652        if (vb2_is_busy(q)) {
 653                dprintk(dev, 1, "%s device busy\n", __func__);
 654                return -EBUSY;
 655        }
 656
 657        if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
 658                dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
 659                return -EBUSY;
 660        }
 661
 662        dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
 663        if (V4L2_FIELD_HAS_T_OR_B(mp->field))
 664                factor = 2;
 665
 666        /* Note: the webcam input doesn't support scaling, cropping or composing */
 667
 668        if (!vivid_is_webcam(dev) &&
 669            (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
 670                struct v4l2_rect r = { 0, 0, mp->width, mp->height };
 671
 672                if (dev->has_scaler_cap) {
 673                        if (dev->has_compose_cap)
 674                                v4l2_rect_map_inside(compose, &r);
 675                        else
 676                                *compose = r;
 677                        if (dev->has_crop_cap && !dev->has_compose_cap) {
 678                                struct v4l2_rect min_r = {
 679                                        0, 0,
 680                                        r.width / MAX_ZOOM,
 681                                        factor * r.height / MAX_ZOOM
 682                                };
 683                                struct v4l2_rect max_r = {
 684                                        0, 0,
 685                                        r.width * MAX_ZOOM,
 686                                        factor * r.height * MAX_ZOOM
 687                                };
 688
 689                                v4l2_rect_set_min_size(crop, &min_r);
 690                                v4l2_rect_set_max_size(crop, &max_r);
 691                                v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
 692                        } else if (dev->has_crop_cap) {
 693                                struct v4l2_rect min_r = {
 694                                        0, 0,
 695                                        compose->width / MAX_ZOOM,
 696                                        factor * compose->height / MAX_ZOOM
 697                                };
 698                                struct v4l2_rect max_r = {
 699                                        0, 0,
 700                                        compose->width * MAX_ZOOM,
 701                                        factor * compose->height * MAX_ZOOM
 702                                };
 703
 704                                v4l2_rect_set_min_size(crop, &min_r);
 705                                v4l2_rect_set_max_size(crop, &max_r);
 706                                v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
 707                        }
 708                } else if (dev->has_crop_cap && !dev->has_compose_cap) {
 709                        r.height *= factor;
 710                        v4l2_rect_set_size_to(crop, &r);
 711                        v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
 712                        r = *crop;
 713                        r.height /= factor;
 714                        v4l2_rect_set_size_to(compose, &r);
 715                } else if (!dev->has_crop_cap) {
 716                        v4l2_rect_map_inside(compose, &r);
 717                } else {
 718                        r.height *= factor;
 719                        v4l2_rect_set_max_size(crop, &r);
 720                        v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
 721                        compose->top *= factor;
 722                        compose->height *= factor;
 723                        v4l2_rect_set_size_to(compose, crop);
 724                        v4l2_rect_map_inside(compose, &r);
 725                        compose->top /= factor;
 726                        compose->height /= factor;
 727                }
 728        } else if (vivid_is_webcam(dev)) {
 729                /* Guaranteed to be a match */
 730                for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
 731                        if (webcam_sizes[i].width == mp->width &&
 732                                        webcam_sizes[i].height == mp->height)
 733                                break;
 734                dev->webcam_size_idx = i;
 735                if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
 736                        dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
 737                vivid_update_format_cap(dev, false);
 738        } else {
 739                struct v4l2_rect r = { 0, 0, mp->width, mp->height };
 740
 741                v4l2_rect_set_size_to(compose, &r);
 742                r.height *= factor;
 743                v4l2_rect_set_size_to(crop, &r);
 744        }
 745
 746        dev->fmt_cap_rect.width = mp->width;
 747        dev->fmt_cap_rect.height = mp->height;
 748        tpg_s_buf_height(&dev->tpg, mp->height);
 749        tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
 750        for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
 751                tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
 752        dev->field_cap = mp->field;
 753        if (dev->field_cap == V4L2_FIELD_ALTERNATE)
 754                tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
 755        else
 756                tpg_s_field(&dev->tpg, dev->field_cap, false);
 757        tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
 758        if (vivid_is_sdtv_cap(dev))
 759                dev->tv_field_cap = mp->field;
 760        tpg_update_mv_step(&dev->tpg);
 761        return 0;
 762}
 763
 764int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
 765                                        struct v4l2_format *f)
 766{
 767        struct vivid_dev *dev = video_drvdata(file);
 768
 769        if (!dev->multiplanar)
 770                return -ENOTTY;
 771        return vivid_g_fmt_vid_cap(file, priv, f);
 772}
 773
 774int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
 775                        struct v4l2_format *f)
 776{
 777        struct vivid_dev *dev = video_drvdata(file);
 778
 779        if (!dev->multiplanar)
 780                return -ENOTTY;
 781        return vivid_try_fmt_vid_cap(file, priv, f);
 782}
 783
 784int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
 785                        struct v4l2_format *f)
 786{
 787        struct vivid_dev *dev = video_drvdata(file);
 788
 789        if (!dev->multiplanar)
 790                return -ENOTTY;
 791        return vivid_s_fmt_vid_cap(file, priv, f);
 792}
 793
 794int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
 795                                        struct v4l2_format *f)
 796{
 797        struct vivid_dev *dev = video_drvdata(file);
 798
 799        if (dev->multiplanar)
 800                return -ENOTTY;
 801        return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
 802}
 803
 804int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
 805                        struct v4l2_format *f)
 806{
 807        struct vivid_dev *dev = video_drvdata(file);
 808
 809        if (dev->multiplanar)
 810                return -ENOTTY;
 811        return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
 812}
 813
 814int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
 815                        struct v4l2_format *f)
 816{
 817        struct vivid_dev *dev = video_drvdata(file);
 818
 819        if (dev->multiplanar)
 820                return -ENOTTY;
 821        return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
 822}
 823
 824int vivid_vid_cap_g_selection(struct file *file, void *priv,
 825                              struct v4l2_selection *sel)
 826{
 827        struct vivid_dev *dev = video_drvdata(file);
 828
 829        if (!dev->has_crop_cap && !dev->has_compose_cap)
 830                return -ENOTTY;
 831        if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 832                return -EINVAL;
 833        if (vivid_is_webcam(dev))
 834                return -ENODATA;
 835
 836        sel->r.left = sel->r.top = 0;
 837        switch (sel->target) {
 838        case V4L2_SEL_TGT_CROP:
 839                if (!dev->has_crop_cap)
 840                        return -EINVAL;
 841                sel->r = dev->crop_cap;
 842                break;
 843        case V4L2_SEL_TGT_CROP_DEFAULT:
 844        case V4L2_SEL_TGT_CROP_BOUNDS:
 845                if (!dev->has_crop_cap)
 846                        return -EINVAL;
 847                sel->r = dev->src_rect;
 848                break;
 849        case V4L2_SEL_TGT_COMPOSE_BOUNDS:
 850                if (!dev->has_compose_cap)
 851                        return -EINVAL;
 852                sel->r = vivid_max_rect;
 853                break;
 854        case V4L2_SEL_TGT_COMPOSE:
 855                if (!dev->has_compose_cap)
 856                        return -EINVAL;
 857                sel->r = dev->compose_cap;
 858                break;
 859        case V4L2_SEL_TGT_COMPOSE_DEFAULT:
 860                if (!dev->has_compose_cap)
 861                        return -EINVAL;
 862                sel->r = dev->fmt_cap_rect;
 863                break;
 864        default:
 865                return -EINVAL;
 866        }
 867        return 0;
 868}
 869
 870int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
 871{
 872        struct vivid_dev *dev = video_drvdata(file);
 873        struct v4l2_rect *crop = &dev->crop_cap;
 874        struct v4l2_rect *compose = &dev->compose_cap;
 875        unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
 876        int ret;
 877
 878        if (!dev->has_crop_cap && !dev->has_compose_cap)
 879                return -ENOTTY;
 880        if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
 881                return -EINVAL;
 882        if (vivid_is_webcam(dev))
 883                return -ENODATA;
 884
 885        switch (s->target) {
 886        case V4L2_SEL_TGT_CROP:
 887                if (!dev->has_crop_cap)
 888                        return -EINVAL;
 889                ret = vivid_vid_adjust_sel(s->flags, &s->r);
 890                if (ret)
 891                        return ret;
 892                v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
 893                v4l2_rect_set_max_size(&s->r, &dev->src_rect);
 894                v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
 895                s->r.top /= factor;
 896                s->r.height /= factor;
 897                if (dev->has_scaler_cap) {
 898                        struct v4l2_rect fmt = dev->fmt_cap_rect;
 899                        struct v4l2_rect max_rect = {
 900                                0, 0,
 901                                s->r.width * MAX_ZOOM,
 902                                s->r.height * MAX_ZOOM
 903                        };
 904                        struct v4l2_rect min_rect = {
 905                                0, 0,
 906                                s->r.width / MAX_ZOOM,
 907                                s->r.height / MAX_ZOOM
 908                        };
 909
 910                        v4l2_rect_set_min_size(&fmt, &min_rect);
 911                        if (!dev->has_compose_cap)
 912                                v4l2_rect_set_max_size(&fmt, &max_rect);
 913                        if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
 914                            vb2_is_busy(&dev->vb_vid_cap_q))
 915                                return -EBUSY;
 916                        if (dev->has_compose_cap) {
 917                                v4l2_rect_set_min_size(compose, &min_rect);
 918                                v4l2_rect_set_max_size(compose, &max_rect);
 919                        }
 920                        dev->fmt_cap_rect = fmt;
 921                        tpg_s_buf_height(&dev->tpg, fmt.height);
 922                } else if (dev->has_compose_cap) {
 923                        struct v4l2_rect fmt = dev->fmt_cap_rect;
 924
 925                        v4l2_rect_set_min_size(&fmt, &s->r);
 926                        if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
 927                            vb2_is_busy(&dev->vb_vid_cap_q))
 928                                return -EBUSY;
 929                        dev->fmt_cap_rect = fmt;
 930                        tpg_s_buf_height(&dev->tpg, fmt.height);
 931                        v4l2_rect_set_size_to(compose, &s->r);
 932                        v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
 933                } else {
 934                        if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
 935                            vb2_is_busy(&dev->vb_vid_cap_q))
 936                                return -EBUSY;
 937                        v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
 938                        v4l2_rect_set_size_to(compose, &s->r);
 939                        v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
 940                        tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
 941                }
 942                s->r.top *= factor;
 943                s->r.height *= factor;
 944                *crop = s->r;
 945                break;
 946        case V4L2_SEL_TGT_COMPOSE:
 947                if (!dev->has_compose_cap)
 948                        return -EINVAL;
 949                ret = vivid_vid_adjust_sel(s->flags, &s->r);
 950                if (ret)
 951                        return ret;
 952                v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
 953                v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
 954                if (dev->has_scaler_cap) {
 955                        struct v4l2_rect max_rect = {
 956                                0, 0,
 957                                dev->src_rect.width * MAX_ZOOM,
 958                                (dev->src_rect.height / factor) * MAX_ZOOM
 959                        };
 960
 961                        v4l2_rect_set_max_size(&s->r, &max_rect);
 962                        if (dev->has_crop_cap) {
 963                                struct v4l2_rect min_rect = {
 964                                        0, 0,
 965                                        s->r.width / MAX_ZOOM,
 966                                        (s->r.height * factor) / MAX_ZOOM
 967                                };
 968                                struct v4l2_rect max_rect = {
 969                                        0, 0,
 970                                        s->r.width * MAX_ZOOM,
 971                                        (s->r.height * factor) * MAX_ZOOM
 972                                };
 973
 974                                v4l2_rect_set_min_size(crop, &min_rect);
 975                                v4l2_rect_set_max_size(crop, &max_rect);
 976                                v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
 977                        }
 978                } else if (dev->has_crop_cap) {
 979                        s->r.top *= factor;
 980                        s->r.height *= factor;
 981                        v4l2_rect_set_max_size(&s->r, &dev->src_rect);
 982                        v4l2_rect_set_size_to(crop, &s->r);
 983                        v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
 984                        s->r.top /= factor;
 985                        s->r.height /= factor;
 986                } else {
 987                        v4l2_rect_set_size_to(&s->r, &dev->src_rect);
 988                        s->r.height /= factor;
 989                }
 990                v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
 991                if (dev->bitmap_cap && (compose->width != s->r.width ||
 992                                        compose->height != s->r.height)) {
 993                        kfree(dev->bitmap_cap);
 994                        dev->bitmap_cap = NULL;
 995                }
 996                *compose = s->r;
 997                break;
 998        default:
 999                return -EINVAL;
1000        }
1001
1002        tpg_s_crop_compose(&dev->tpg, crop, compose);
1003        return 0;
1004}
1005
1006int vivid_vid_cap_cropcap(struct file *file, void *priv,
1007                              struct v4l2_cropcap *cap)
1008{
1009        struct vivid_dev *dev = video_drvdata(file);
1010
1011        if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1012                return -EINVAL;
1013
1014        switch (vivid_get_pixel_aspect(dev)) {
1015        case TPG_PIXEL_ASPECT_NTSC:
1016                cap->pixelaspect.numerator = 11;
1017                cap->pixelaspect.denominator = 10;
1018                break;
1019        case TPG_PIXEL_ASPECT_PAL:
1020                cap->pixelaspect.numerator = 54;
1021                cap->pixelaspect.denominator = 59;
1022                break;
1023        case TPG_PIXEL_ASPECT_SQUARE:
1024                cap->pixelaspect.numerator = 1;
1025                cap->pixelaspect.denominator = 1;
1026                break;
1027        }
1028        return 0;
1029}
1030
1031int vidioc_enum_fmt_vid_overlay(struct file *file, void  *priv,
1032                                        struct v4l2_fmtdesc *f)
1033{
1034        struct vivid_dev *dev = video_drvdata(file);
1035        const struct vivid_fmt *fmt;
1036
1037        if (dev->multiplanar)
1038                return -ENOTTY;
1039
1040        if (f->index >= ARRAY_SIZE(formats_ovl))
1041                return -EINVAL;
1042
1043        fmt = &formats_ovl[f->index];
1044
1045        f->pixelformat = fmt->fourcc;
1046        return 0;
1047}
1048
1049int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1050                                        struct v4l2_format *f)
1051{
1052        struct vivid_dev *dev = video_drvdata(file);
1053        const struct v4l2_rect *compose = &dev->compose_cap;
1054        struct v4l2_window *win = &f->fmt.win;
1055        unsigned clipcount = win->clipcount;
1056
1057        if (dev->multiplanar)
1058                return -ENOTTY;
1059
1060        win->w.top = dev->overlay_cap_top;
1061        win->w.left = dev->overlay_cap_left;
1062        win->w.width = compose->width;
1063        win->w.height = compose->height;
1064        win->field = dev->overlay_cap_field;
1065        win->clipcount = dev->clipcount_cap;
1066        if (clipcount > dev->clipcount_cap)
1067                clipcount = dev->clipcount_cap;
1068        if (dev->bitmap_cap == NULL)
1069                win->bitmap = NULL;
1070        else if (win->bitmap) {
1071                if (copy_to_user(win->bitmap, dev->bitmap_cap,
1072                    ((compose->width + 7) / 8) * compose->height))
1073                        return -EFAULT;
1074        }
1075        if (clipcount && win->clips) {
1076                if (copy_to_user(win->clips, dev->clips_cap,
1077                                 clipcount * sizeof(dev->clips_cap[0])))
1078                        return -EFAULT;
1079        }
1080        return 0;
1081}
1082
1083int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1084                                        struct v4l2_format *f)
1085{
1086        struct vivid_dev *dev = video_drvdata(file);
1087        const struct v4l2_rect *compose = &dev->compose_cap;
1088        struct v4l2_window *win = &f->fmt.win;
1089        int i, j;
1090
1091        if (dev->multiplanar)
1092                return -ENOTTY;
1093
1094        win->w.left = clamp_t(int, win->w.left,
1095                              -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1096        win->w.top = clamp_t(int, win->w.top,
1097                             -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1098        win->w.width = compose->width;
1099        win->w.height = compose->height;
1100        if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1101                win->field = V4L2_FIELD_ANY;
1102        win->chromakey = 0;
1103        win->global_alpha = 0;
1104        if (win->clipcount && !win->clips)
1105                win->clipcount = 0;
1106        if (win->clipcount > MAX_CLIPS)
1107                win->clipcount = MAX_CLIPS;
1108        if (win->clipcount) {
1109                if (copy_from_user(dev->try_clips_cap, win->clips,
1110                                   win->clipcount * sizeof(dev->clips_cap[0])))
1111                        return -EFAULT;
1112                for (i = 0; i < win->clipcount; i++) {
1113                        struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1114
1115                        r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1116                        r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1117                        r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1118                        r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1119                }
1120                /*
1121                 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1122                 * number and it's typically a one-time deal.
1123                 */
1124                for (i = 0; i < win->clipcount - 1; i++) {
1125                        struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1126
1127                        for (j = i + 1; j < win->clipcount; j++) {
1128                                struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1129
1130                                if (v4l2_rect_overlap(r1, r2))
1131                                        return -EINVAL;
1132                        }
1133                }
1134                if (copy_to_user(win->clips, dev->try_clips_cap,
1135                                 win->clipcount * sizeof(dev->clips_cap[0])))
1136                        return -EFAULT;
1137        }
1138        return 0;
1139}
1140
1141int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1142                                        struct v4l2_format *f)
1143{
1144        struct vivid_dev *dev = video_drvdata(file);
1145        const struct v4l2_rect *compose = &dev->compose_cap;
1146        struct v4l2_window *win = &f->fmt.win;
1147        int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1148        unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1149        unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1150        void *new_bitmap = NULL;
1151
1152        if (ret)
1153                return ret;
1154
1155        if (win->bitmap) {
1156                new_bitmap = vzalloc(bitmap_size);
1157
1158                if (new_bitmap == NULL)
1159                        return -ENOMEM;
1160                if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1161                        vfree(new_bitmap);
1162                        return -EFAULT;
1163                }
1164        }
1165
1166        dev->overlay_cap_top = win->w.top;
1167        dev->overlay_cap_left = win->w.left;
1168        dev->overlay_cap_field = win->field;
1169        vfree(dev->bitmap_cap);
1170        dev->bitmap_cap = new_bitmap;
1171        dev->clipcount_cap = win->clipcount;
1172        if (dev->clipcount_cap)
1173                memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1174        return 0;
1175}
1176
1177int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1178{
1179        struct vivid_dev *dev = video_drvdata(file);
1180
1181        if (dev->multiplanar)
1182                return -ENOTTY;
1183
1184        if (i && dev->fb_vbase_cap == NULL)
1185                return -EINVAL;
1186
1187        if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1188                dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1189                return -EINVAL;
1190        }
1191
1192        if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1193                return -EBUSY;
1194        dev->overlay_cap_owner = i ? fh : NULL;
1195        return 0;
1196}
1197
1198int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1199                                struct v4l2_framebuffer *a)
1200{
1201        struct vivid_dev *dev = video_drvdata(file);
1202
1203        if (dev->multiplanar)
1204                return -ENOTTY;
1205
1206        *a = dev->fb_cap;
1207        a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1208                        V4L2_FBUF_CAP_LIST_CLIPPING;
1209        a->flags = V4L2_FBUF_FLAG_PRIMARY;
1210        a->fmt.field = V4L2_FIELD_NONE;
1211        a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1212        a->fmt.priv = 0;
1213        return 0;
1214}
1215
1216int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1217                                const struct v4l2_framebuffer *a)
1218{
1219        struct vivid_dev *dev = video_drvdata(file);
1220        const struct vivid_fmt *fmt;
1221
1222        if (dev->multiplanar)
1223                return -ENOTTY;
1224
1225        if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1226                return -EPERM;
1227
1228        if (dev->overlay_cap_owner)
1229                return -EBUSY;
1230
1231        if (a->base == NULL) {
1232                dev->fb_cap.base = NULL;
1233                dev->fb_vbase_cap = NULL;
1234                return 0;
1235        }
1236
1237        if (a->fmt.width < 48 || a->fmt.height < 32)
1238                return -EINVAL;
1239        fmt = vivid_get_format(dev, a->fmt.pixelformat);
1240        if (!fmt || !fmt->can_do_overlay)
1241                return -EINVAL;
1242        if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1243                return -EINVAL;
1244        if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
1245                return -EINVAL;
1246
1247        dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1248        dev->fb_cap = *a;
1249        dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1250                                    -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1251        dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1252                                   -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1253        return 0;
1254}
1255
1256static const struct v4l2_audio vivid_audio_inputs[] = {
1257        { 0, "TV", V4L2_AUDCAP_STEREO },
1258        { 1, "Line-In", V4L2_AUDCAP_STEREO },
1259};
1260
1261int vidioc_enum_input(struct file *file, void *priv,
1262                                struct v4l2_input *inp)
1263{
1264        struct vivid_dev *dev = video_drvdata(file);
1265
1266        if (inp->index >= dev->num_inputs)
1267                return -EINVAL;
1268
1269        inp->type = V4L2_INPUT_TYPE_CAMERA;
1270        switch (dev->input_type[inp->index]) {
1271        case WEBCAM:
1272                snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1273                                dev->input_name_counter[inp->index]);
1274                inp->capabilities = 0;
1275                break;
1276        case TV:
1277                snprintf(inp->name, sizeof(inp->name), "TV %u",
1278                                dev->input_name_counter[inp->index]);
1279                inp->type = V4L2_INPUT_TYPE_TUNER;
1280                inp->std = V4L2_STD_ALL;
1281                if (dev->has_audio_inputs)
1282                        inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1283                inp->capabilities = V4L2_IN_CAP_STD;
1284                break;
1285        case SVID:
1286                snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1287                                dev->input_name_counter[inp->index]);
1288                inp->std = V4L2_STD_ALL;
1289                if (dev->has_audio_inputs)
1290                        inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1291                inp->capabilities = V4L2_IN_CAP_STD;
1292                break;
1293        case HDMI:
1294                snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1295                                dev->input_name_counter[inp->index]);
1296                inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1297                if (dev->edid_blocks == 0 ||
1298                    dev->dv_timings_signal_mode == NO_SIGNAL)
1299                        inp->status |= V4L2_IN_ST_NO_SIGNAL;
1300                else if (dev->dv_timings_signal_mode == NO_LOCK ||
1301                         dev->dv_timings_signal_mode == OUT_OF_RANGE)
1302                        inp->status |= V4L2_IN_ST_NO_H_LOCK;
1303                break;
1304        }
1305        if (dev->sensor_hflip)
1306                inp->status |= V4L2_IN_ST_HFLIP;
1307        if (dev->sensor_vflip)
1308                inp->status |= V4L2_IN_ST_VFLIP;
1309        if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1310                if (dev->std_signal_mode == NO_SIGNAL) {
1311                        inp->status |= V4L2_IN_ST_NO_SIGNAL;
1312                } else if (dev->std_signal_mode == NO_LOCK) {
1313                        inp->status |= V4L2_IN_ST_NO_H_LOCK;
1314                } else if (vivid_is_tv_cap(dev)) {
1315                        switch (tpg_g_quality(&dev->tpg)) {
1316                        case TPG_QUAL_GRAY:
1317                                inp->status |= V4L2_IN_ST_COLOR_KILL;
1318                                break;
1319                        case TPG_QUAL_NOISE:
1320                                inp->status |= V4L2_IN_ST_NO_H_LOCK;
1321                                break;
1322                        default:
1323                                break;
1324                        }
1325                }
1326        }
1327        return 0;
1328}
1329
1330int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1331{
1332        struct vivid_dev *dev = video_drvdata(file);
1333
1334        *i = dev->input;
1335        return 0;
1336}
1337
1338int vidioc_s_input(struct file *file, void *priv, unsigned i)
1339{
1340        struct vivid_dev *dev = video_drvdata(file);
1341        struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1342        unsigned brightness;
1343
1344        if (i >= dev->num_inputs)
1345                return -EINVAL;
1346
1347        if (i == dev->input)
1348                return 0;
1349
1350        if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1351                return -EBUSY;
1352
1353        dev->input = i;
1354        dev->vid_cap_dev.tvnorms = 0;
1355        if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1356                dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1357                dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1358        }
1359        dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1360        vivid_update_format_cap(dev, false);
1361
1362        if (dev->colorspace) {
1363                switch (dev->input_type[i]) {
1364                case WEBCAM:
1365                        v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1366                        break;
1367                case TV:
1368                case SVID:
1369                        v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1370                        break;
1371                case HDMI:
1372                        if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1373                                if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1374                                        v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1375                                else
1376                                        v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1377                        } else {
1378                                v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1379                        }
1380                        break;
1381                }
1382        }
1383
1384        /*
1385         * Modify the brightness range depending on the input.
1386         * This makes it easy to use vivid to test if applications can
1387         * handle control range modifications and is also how this is
1388         * typically used in practice as different inputs may be hooked
1389         * up to different receivers with different control ranges.
1390         */
1391        brightness = 128 * i + dev->input_brightness[i];
1392        v4l2_ctrl_modify_range(dev->brightness,
1393                        128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1394        v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1395        return 0;
1396}
1397
1398int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1399{
1400        if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1401                return -EINVAL;
1402        *vin = vivid_audio_inputs[vin->index];
1403        return 0;
1404}
1405
1406int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1407{
1408        struct vivid_dev *dev = video_drvdata(file);
1409
1410        if (!vivid_is_sdtv_cap(dev))
1411                return -EINVAL;
1412        *vin = vivid_audio_inputs[dev->tv_audio_input];
1413        return 0;
1414}
1415
1416int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1417{
1418        struct vivid_dev *dev = video_drvdata(file);
1419
1420        if (!vivid_is_sdtv_cap(dev))
1421                return -EINVAL;
1422        if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1423                return -EINVAL;
1424        dev->tv_audio_input = vin->index;
1425        return 0;
1426}
1427
1428int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1429{
1430        struct vivid_dev *dev = video_drvdata(file);
1431
1432        if (vf->tuner != 0)
1433                return -EINVAL;
1434        vf->frequency = dev->tv_freq;
1435        return 0;
1436}
1437
1438int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1439{
1440        struct vivid_dev *dev = video_drvdata(file);
1441
1442        if (vf->tuner != 0)
1443                return -EINVAL;
1444        dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1445        if (vivid_is_tv_cap(dev))
1446                vivid_update_quality(dev);
1447        return 0;
1448}
1449
1450int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1451{
1452        struct vivid_dev *dev = video_drvdata(file);
1453
1454        if (vt->index != 0)
1455                return -EINVAL;
1456        if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1457                return -EINVAL;
1458        dev->tv_audmode = vt->audmode;
1459        return 0;
1460}
1461
1462int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1463{
1464        struct vivid_dev *dev = video_drvdata(file);
1465        enum tpg_quality qual;
1466
1467        if (vt->index != 0)
1468                return -EINVAL;
1469
1470        vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1471                         V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1472        vt->audmode = dev->tv_audmode;
1473        vt->rangelow = MIN_TV_FREQ;
1474        vt->rangehigh = MAX_TV_FREQ;
1475        qual = vivid_get_quality(dev, &vt->afc);
1476        if (qual == TPG_QUAL_COLOR)
1477                vt->signal = 0xffff;
1478        else if (qual == TPG_QUAL_GRAY)
1479                vt->signal = 0x8000;
1480        else
1481                vt->signal = 0;
1482        if (qual == TPG_QUAL_NOISE) {
1483                vt->rxsubchans = 0;
1484        } else if (qual == TPG_QUAL_GRAY) {
1485                vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1486        } else {
1487                unsigned channel_nr = dev->tv_freq / (6 * 16);
1488                unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1489
1490                switch (channel_nr % options) {
1491                case 0:
1492                        vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1493                        break;
1494                case 1:
1495                        vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1496                        break;
1497                case 2:
1498                        if (dev->std_cap & V4L2_STD_NTSC_M)
1499                                vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1500                        else
1501                                vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1502                        break;
1503                case 3:
1504                        vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1505                        break;
1506                }
1507        }
1508        strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1509        return 0;
1510}
1511
1512/* Must remain in sync with the vivid_ctrl_standard_strings array */
1513const v4l2_std_id vivid_standard[] = {
1514        V4L2_STD_NTSC_M,
1515        V4L2_STD_NTSC_M_JP,
1516        V4L2_STD_NTSC_M_KR,
1517        V4L2_STD_NTSC_443,
1518        V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1519        V4L2_STD_PAL_I,
1520        V4L2_STD_PAL_DK,
1521        V4L2_STD_PAL_M,
1522        V4L2_STD_PAL_N,
1523        V4L2_STD_PAL_Nc,
1524        V4L2_STD_PAL_60,
1525        V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1526        V4L2_STD_SECAM_DK,
1527        V4L2_STD_SECAM_L,
1528        V4L2_STD_SECAM_LC,
1529        V4L2_STD_UNKNOWN
1530};
1531
1532/* Must remain in sync with the vivid_standard array */
1533const char * const vivid_ctrl_standard_strings[] = {
1534        "NTSC-M",
1535        "NTSC-M-JP",
1536        "NTSC-M-KR",
1537        "NTSC-443",
1538        "PAL-BGH",
1539        "PAL-I",
1540        "PAL-DK",
1541        "PAL-M",
1542        "PAL-N",
1543        "PAL-Nc",
1544        "PAL-60",
1545        "SECAM-BGH",
1546        "SECAM-DK",
1547        "SECAM-L",
1548        "SECAM-Lc",
1549        NULL,
1550};
1551
1552int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1553{
1554        struct vivid_dev *dev = video_drvdata(file);
1555
1556        if (!vivid_is_sdtv_cap(dev))
1557                return -ENODATA;
1558        if (dev->std_signal_mode == NO_SIGNAL ||
1559            dev->std_signal_mode == NO_LOCK) {
1560                *id = V4L2_STD_UNKNOWN;
1561                return 0;
1562        }
1563        if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1564                *id = V4L2_STD_UNKNOWN;
1565        } else if (dev->std_signal_mode == CURRENT_STD) {
1566                *id = dev->std_cap;
1567        } else if (dev->std_signal_mode == SELECTED_STD) {
1568                *id = dev->query_std;
1569        } else {
1570                *id = vivid_standard[dev->query_std_last];
1571                dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1572        }
1573
1574        return 0;
1575}
1576
1577int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1578{
1579        struct vivid_dev *dev = video_drvdata(file);
1580
1581        if (!vivid_is_sdtv_cap(dev))
1582                return -ENODATA;
1583        if (dev->std_cap == id)
1584                return 0;
1585        if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1586                return -EBUSY;
1587        dev->std_cap = id;
1588        vivid_update_format_cap(dev, false);
1589        return 0;
1590}
1591
1592static void find_aspect_ratio(u32 width, u32 height,
1593                               u32 *num, u32 *denom)
1594{
1595        if (!(height % 3) && ((height * 4 / 3) == width)) {
1596                *num = 4;
1597                *denom = 3;
1598        } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1599                *num = 16;
1600                *denom = 9;
1601        } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1602                *num = 16;
1603                *denom = 10;
1604        } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1605                *num = 5;
1606                *denom = 4;
1607        } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1608                *num = 15;
1609                *denom = 9;
1610        } else { /* default to 16:9 */
1611                *num = 16;
1612                *denom = 9;
1613        }
1614}
1615
1616static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1617{
1618        struct v4l2_bt_timings *bt = &timings->bt;
1619        u32 total_h_pixel;
1620        u32 total_v_lines;
1621        u32 h_freq;
1622
1623        if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1624                                NULL, NULL))
1625                return false;
1626
1627        total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1628        total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1629
1630        h_freq = (u32)bt->pixelclock / total_h_pixel;
1631
1632        if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1633                if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1634                                    bt->polarities, bt->interlaced, timings))
1635                        return true;
1636        }
1637
1638        if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1639                struct v4l2_fract aspect_ratio;
1640
1641                find_aspect_ratio(bt->width, bt->height,
1642                                  &aspect_ratio.numerator,
1643                                  &aspect_ratio.denominator);
1644                if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1645                                    bt->polarities, bt->interlaced,
1646                                    aspect_ratio, timings))
1647                        return true;
1648        }
1649        return false;
1650}
1651
1652int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1653                                    struct v4l2_dv_timings *timings)
1654{
1655        struct vivid_dev *dev = video_drvdata(file);
1656
1657        if (!vivid_is_hdmi_cap(dev))
1658                return -ENODATA;
1659        if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1660                                      0, NULL, NULL) &&
1661            !valid_cvt_gtf_timings(timings))
1662                return -EINVAL;
1663
1664        if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
1665                return 0;
1666        if (vb2_is_busy(&dev->vb_vid_cap_q))
1667                return -EBUSY;
1668
1669        dev->dv_timings_cap = *timings;
1670        vivid_update_format_cap(dev, false);
1671        return 0;
1672}
1673
1674int vidioc_query_dv_timings(struct file *file, void *_fh,
1675                                    struct v4l2_dv_timings *timings)
1676{
1677        struct vivid_dev *dev = video_drvdata(file);
1678
1679        if (!vivid_is_hdmi_cap(dev))
1680                return -ENODATA;
1681        if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1682            dev->edid_blocks == 0)
1683                return -ENOLINK;
1684        if (dev->dv_timings_signal_mode == NO_LOCK)
1685                return -ENOLCK;
1686        if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1687                timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1688                return -ERANGE;
1689        }
1690        if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1691                *timings = dev->dv_timings_cap;
1692        } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1693                *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1694        } else {
1695                *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1696                dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1697                                                dev->query_dv_timings_size;
1698        }
1699        return 0;
1700}
1701
1702int vidioc_s_edid(struct file *file, void *_fh,
1703                         struct v4l2_edid *edid)
1704{
1705        struct vivid_dev *dev = video_drvdata(file);
1706        u16 phys_addr;
1707        unsigned int i;
1708        int ret;
1709
1710        memset(edid->reserved, 0, sizeof(edid->reserved));
1711        if (edid->pad >= dev->num_inputs)
1712                return -EINVAL;
1713        if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1714                return -EINVAL;
1715        if (edid->blocks == 0) {
1716                dev->edid_blocks = 0;
1717                phys_addr = CEC_PHYS_ADDR_INVALID;
1718                goto set_phys_addr;
1719        }
1720        if (edid->blocks > dev->edid_max_blocks) {
1721                edid->blocks = dev->edid_max_blocks;
1722                return -E2BIG;
1723        }
1724        phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1725        ret = cec_phys_addr_validate(phys_addr, &phys_addr, NULL);
1726        if (ret)
1727                return ret;
1728
1729        if (vb2_is_busy(&dev->vb_vid_cap_q))
1730                return -EBUSY;
1731
1732        dev->edid_blocks = edid->blocks;
1733        memcpy(dev->edid, edid->edid, edid->blocks * 128);
1734
1735set_phys_addr:
1736        /* TODO: a proper hotplug detect cycle should be emulated here */
1737        cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1738
1739        for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1740                cec_s_phys_addr(dev->cec_tx_adap[i],
1741                                cec_phys_addr_for_input(phys_addr, i + 1),
1742                                false);
1743        return 0;
1744}
1745
1746int vidioc_enum_framesizes(struct file *file, void *fh,
1747                                         struct v4l2_frmsizeenum *fsize)
1748{
1749        struct vivid_dev *dev = video_drvdata(file);
1750
1751        if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1752                return -EINVAL;
1753        if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1754                return -EINVAL;
1755        if (vivid_is_webcam(dev)) {
1756                if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1757                        return -EINVAL;
1758                fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1759                fsize->discrete = webcam_sizes[fsize->index];
1760                return 0;
1761        }
1762        if (fsize->index)
1763                return -EINVAL;
1764        fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1765        fsize->stepwise.min_width = MIN_WIDTH;
1766        fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1767        fsize->stepwise.step_width = 2;
1768        fsize->stepwise.min_height = MIN_HEIGHT;
1769        fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1770        fsize->stepwise.step_height = 2;
1771        return 0;
1772}
1773
1774/* timeperframe is arbitrary and continuous */
1775int vidioc_enum_frameintervals(struct file *file, void *priv,
1776                                             struct v4l2_frmivalenum *fival)
1777{
1778        struct vivid_dev *dev = video_drvdata(file);
1779        const struct vivid_fmt *fmt;
1780        int i;
1781
1782        fmt = vivid_get_format(dev, fival->pixel_format);
1783        if (!fmt)
1784                return -EINVAL;
1785
1786        if (!vivid_is_webcam(dev)) {
1787                if (fival->index)
1788                        return -EINVAL;
1789                if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1790                        return -EINVAL;
1791                if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1792                        return -EINVAL;
1793                fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1794                fival->discrete = dev->timeperframe_vid_cap;
1795                return 0;
1796        }
1797
1798        for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1799                if (fival->width == webcam_sizes[i].width &&
1800                    fival->height == webcam_sizes[i].height)
1801                        break;
1802        if (i == ARRAY_SIZE(webcam_sizes))
1803                return -EINVAL;
1804        if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1805                return -EINVAL;
1806        fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1807        fival->discrete = webcam_intervals[fival->index];
1808        return 0;
1809}
1810
1811int vivid_vid_cap_g_parm(struct file *file, void *priv,
1812                          struct v4l2_streamparm *parm)
1813{
1814        struct vivid_dev *dev = video_drvdata(file);
1815
1816        if (parm->type != (dev->multiplanar ?
1817                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1818                           V4L2_BUF_TYPE_VIDEO_CAPTURE))
1819                return -EINVAL;
1820
1821        parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1822        parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1823        parm->parm.capture.readbuffers  = 1;
1824        return 0;
1825}
1826
1827#define FRACT_CMP(a, OP, b)     \
1828        ((u64)(a).numerator * (b).denominator  OP  (u64)(b).numerator * (a).denominator)
1829
1830int vivid_vid_cap_s_parm(struct file *file, void *priv,
1831                          struct v4l2_streamparm *parm)
1832{
1833        struct vivid_dev *dev = video_drvdata(file);
1834        unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1835        struct v4l2_fract tpf;
1836        unsigned i;
1837
1838        if (parm->type != (dev->multiplanar ?
1839                           V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1840                           V4L2_BUF_TYPE_VIDEO_CAPTURE))
1841                return -EINVAL;
1842        if (!vivid_is_webcam(dev))
1843                return vivid_vid_cap_g_parm(file, priv, parm);
1844
1845        tpf = parm->parm.capture.timeperframe;
1846
1847        if (tpf.denominator == 0)
1848                tpf = webcam_intervals[ival_sz - 1];
1849        for (i = 0; i < ival_sz; i++)
1850                if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1851                        break;
1852        if (i == ival_sz)
1853                i = ival_sz - 1;
1854        dev->webcam_ival_idx = i;
1855        tpf = webcam_intervals[dev->webcam_ival_idx];
1856        tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1857        tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1858
1859        /* resync the thread's timings */
1860        dev->cap_seq_resync = true;
1861        dev->timeperframe_vid_cap = tpf;
1862        parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1863        parm->parm.capture.timeperframe = tpf;
1864        parm->parm.capture.readbuffers  = 1;
1865        return 0;
1866}
1867