root/drivers/media/platform/vivid/vivid-vid-cap.c

/* [<][>][^][v][top][bottom][index][help] */

DEFINITIONS

This source file includes following definitions.
  1. vid_cap_queue_setup
  2. vid_cap_buf_prepare
  3. vid_cap_buf_finish
  4. vid_cap_buf_queue
  5. vid_cap_start_streaming
  6. vid_cap_stop_streaming
  7. vid_cap_buf_request_complete
  8. vivid_update_quality
  9. vivid_get_quality
  10. vivid_get_video_aspect
  11. vivid_get_pixel_aspect
  12. vivid_update_format_cap
  13. vivid_field_cap
  14. vivid_colorspace_cap
  15. vivid_xfer_func_cap
  16. vivid_ycbcr_enc_cap
  17. vivid_hsv_enc_cap
  18. vivid_quantization_cap
  19. vivid_g_fmt_vid_cap
  20. vivid_try_fmt_vid_cap
  21. vivid_s_fmt_vid_cap
  22. vidioc_g_fmt_vid_cap_mplane
  23. vidioc_try_fmt_vid_cap_mplane
  24. vidioc_s_fmt_vid_cap_mplane
  25. vidioc_g_fmt_vid_cap
  26. vidioc_try_fmt_vid_cap
  27. vidioc_s_fmt_vid_cap
  28. vivid_vid_cap_g_selection
  29. vivid_vid_cap_s_selection
  30. vivid_vid_cap_g_pixelaspect
  31. vidioc_enum_fmt_vid_overlay
  32. vidioc_g_fmt_vid_overlay
  33. vidioc_try_fmt_vid_overlay
  34. vidioc_s_fmt_vid_overlay
  35. vivid_vid_cap_overlay
  36. vivid_vid_cap_g_fbuf
  37. vivid_vid_cap_s_fbuf
  38. vidioc_enum_input
  39. vidioc_g_input
  40. vidioc_s_input
  41. vidioc_enumaudio
  42. vidioc_g_audio
  43. vidioc_s_audio
  44. vivid_video_g_frequency
  45. vivid_video_s_frequency
  46. vivid_video_s_tuner
  47. vivid_video_g_tuner
  48. vidioc_querystd
  49. vivid_vid_cap_s_std
  50. find_aspect_ratio
  51. valid_cvt_gtf_timings
  52. vivid_vid_cap_s_dv_timings
  53. vidioc_query_dv_timings
  54. vidioc_s_edid
  55. vidioc_enum_framesizes
  56. vidioc_enum_frameintervals
  57. vivid_vid_cap_g_parm
  58. vivid_vid_cap_s_parm

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

/* [<][>][^][v][top][bottom][index][help] */