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