1 // SPDX-License-Identifier: GPL-2.0-only
3 * vivid-vid-cap.c - video capture support functions.
5 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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>
19 #include "vivid-core.h"
20 #include "vivid-vid-common.h"
21 #include "vivid-kthread-cap.h"
22 #include "vivid-vid-cap.h"
24 /* timeperframe: min/max and default */
25 static const struct v4l2_fract
26 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
27 tpf_max = {.numerator = FPS_MAX, .denominator = 1};
29 static const struct vivid_fmt formats_ovl[] = {
31 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
32 .vdownsampling = { 1 },
38 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
39 .vdownsampling = { 1 },
45 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
46 .vdownsampling = { 1 },
53 /* The number of discrete webcam framesizes */
54 #define VIVID_WEBCAM_SIZES 5
55 /* The number of discrete webcam frameintervals */
56 #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
58 /* Sizes must be in increasing order */
59 static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
68 * Intervals must be in increasing order and there must be twice as many
69 * elements in this array as there are in webcam_sizes.
71 static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
84 static int vid_cap_queue_setup(struct vb2_queue *vq,
85 unsigned *nbuffers, unsigned *nplanes,
86 unsigned sizes[], struct device *alloc_devs[])
88 struct vivid_dev *dev = vb2_get_drv_priv(vq);
89 unsigned buffers = tpg_g_buffers(&dev->tpg);
90 unsigned h = dev->fmt_cap_rect.height;
93 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
95 * You cannot use read() with FIELD_ALTERNATE since the field
96 * information (TOP/BOTTOM) cannot be passed back to the user.
98 if (vb2_fileio_is_active(vq))
102 if (dev->queue_setup_error) {
104 * Error injection: test what happens if queue_setup() returns
107 dev->queue_setup_error = false;
112 * Check if the number of requested planes match
113 * the number of buffers in the current format. You can't mix that.
115 if (*nplanes != buffers)
117 for (p = 0; p < buffers; p++) {
118 if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
119 dev->fmt_cap->data_offset[p])
123 for (p = 0; p < buffers; p++)
124 sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
125 dev->fmt_cap->data_offset[p];
128 if (vq->num_buffers + *nbuffers < 2)
129 *nbuffers = 2 - vq->num_buffers;
133 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
134 for (p = 0; p < buffers; p++)
135 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
140 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
142 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
144 unsigned buffers = tpg_g_buffers(&dev->tpg);
147 dprintk(dev, 1, "%s\n", __func__);
149 if (WARN_ON(NULL == dev->fmt_cap))
152 if (dev->buf_prepare_error) {
154 * Error injection: test what happens if buf_prepare() returns
157 dev->buf_prepare_error = false;
160 for (p = 0; p < buffers; p++) {
161 size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
162 dev->fmt_cap->data_offset[p];
164 if (vb2_plane_size(vb, p) < size) {
165 dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
166 __func__, p, vb2_plane_size(vb, p), size);
170 vb2_set_plane_payload(vb, p, size);
171 vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
177 static void vid_cap_buf_finish(struct vb2_buffer *vb)
179 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
180 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
181 struct v4l2_timecode *tc = &vbuf->timecode;
183 unsigned seq = vbuf->sequence;
185 if (!vivid_is_sdtv_cap(dev))
189 * Set the timecode. Rarely used, so it is interesting to
192 vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
193 if (dev->std_cap & V4L2_STD_525_60)
195 tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
197 tc->frames = seq % fps;
198 tc->seconds = (seq / fps) % 60;
199 tc->minutes = (seq / (60 * fps)) % 60;
200 tc->hours = (seq / (60 * 60 * fps)) % 24;
203 static void vid_cap_buf_queue(struct vb2_buffer *vb)
205 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
206 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
207 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
209 dprintk(dev, 1, "%s\n", __func__);
211 spin_lock(&dev->slock);
212 list_add_tail(&buf->list, &dev->vid_cap_active);
213 spin_unlock(&dev->slock);
216 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
218 struct vivid_dev *dev = vb2_get_drv_priv(vq);
222 if (vb2_is_streaming(&dev->vb_vid_out_q))
223 dev->can_loop_video = vivid_vid_can_loop(dev);
225 if (dev->kthread_vid_cap)
228 dev->vid_cap_seq_count = 0;
229 dprintk(dev, 1, "%s\n", __func__);
230 for (i = 0; i < VIDEO_MAX_FRAME; i++)
231 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
232 if (dev->start_streaming_error) {
233 dev->start_streaming_error = false;
236 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
239 struct vivid_buffer *buf, *tmp;
241 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
242 list_del(&buf->list);
243 vb2_buffer_done(&buf->vb.vb2_buf,
244 VB2_BUF_STATE_QUEUED);
250 /* abort streaming and wait for last buffer */
251 static void vid_cap_stop_streaming(struct vb2_queue *vq)
253 struct vivid_dev *dev = vb2_get_drv_priv(vq);
255 dprintk(dev, 1, "%s\n", __func__);
256 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
257 dev->can_loop_video = false;
260 const struct vb2_ops vivid_vid_cap_qops = {
261 .queue_setup = vid_cap_queue_setup,
262 .buf_prepare = vid_cap_buf_prepare,
263 .buf_finish = vid_cap_buf_finish,
264 .buf_queue = vid_cap_buf_queue,
265 .start_streaming = vid_cap_start_streaming,
266 .stop_streaming = vid_cap_stop_streaming,
267 .wait_prepare = vb2_ops_wait_prepare,
268 .wait_finish = vb2_ops_wait_finish,
272 * Determine the 'picture' quality based on the current TV frequency: either
273 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
274 * signal or NOISE for no signal.
276 void vivid_update_quality(struct vivid_dev *dev)
278 unsigned freq_modulus;
280 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
282 * The 'noise' will only be replaced by the actual video
283 * if the output video matches the input video settings.
285 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
288 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
289 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
292 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
293 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
296 if (!vivid_is_tv_cap(dev)) {
297 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
302 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
303 * From +/- 0.25 MHz around the channel there is color, and from
304 * +/- 1 MHz there is grayscale (chroma is lost).
305 * Everywhere else it is just noise.
307 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
308 if (freq_modulus > 2 * 16) {
309 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
310 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
313 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
314 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
316 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
320 * Get the current picture quality and the associated afc value.
322 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
324 unsigned freq_modulus;
328 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
329 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
330 return tpg_g_quality(&dev->tpg);
333 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
334 * From +/- 0.25 MHz around the channel there is color, and from
335 * +/- 1 MHz there is grayscale (chroma is lost).
336 * Everywhere else it is just gray.
338 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
340 *afc = freq_modulus - 1 * 16;
341 return TPG_QUAL_GRAY;
344 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
346 if (vivid_is_sdtv_cap(dev))
347 return dev->std_aspect_ratio;
349 if (vivid_is_hdmi_cap(dev))
350 return dev->dv_timings_aspect_ratio;
352 return TPG_VIDEO_ASPECT_IMAGE;
355 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
357 if (vivid_is_sdtv_cap(dev))
358 return (dev->std_cap & V4L2_STD_525_60) ?
359 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
361 if (vivid_is_hdmi_cap(dev) &&
362 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
363 return dev->src_rect.height == 480 ?
364 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
366 return TPG_PIXEL_ASPECT_SQUARE;
370 * Called whenever the format has to be reset which can occur when
371 * changing inputs, standard, timings, etc.
373 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
375 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
379 switch (dev->input_type[dev->input]) {
382 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
383 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
384 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
385 dev->field_cap = V4L2_FIELD_NONE;
386 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
390 dev->field_cap = dev->tv_field_cap;
391 dev->src_rect.width = 720;
392 if (dev->std_cap & V4L2_STD_525_60) {
393 dev->src_rect.height = 480;
394 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
395 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
397 dev->src_rect.height = 576;
398 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
399 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
401 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
404 dev->src_rect.width = bt->width;
405 dev->src_rect.height = bt->height;
406 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
407 if (dev->reduced_fps && can_reduce_fps(bt)) {
408 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
409 bt->flags |= V4L2_DV_FL_REDUCED_FPS;
411 pixelclock = bt->pixelclock;
412 bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
414 dev->timeperframe_vid_cap = (struct v4l2_fract) {
415 size / 100, (u32)pixelclock / 100
418 dev->field_cap = V4L2_FIELD_ALTERNATE;
420 dev->field_cap = V4L2_FIELD_NONE;
423 * We can be called from within s_ctrl, in that case we can't
424 * set/get controls. Luckily we don't need to in that case.
426 if (keep_controls || !dev->colorspace)
428 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
429 if (bt->width == 720 && bt->height <= 576)
430 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
432 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
433 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
435 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
436 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
438 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
441 vivid_update_quality(dev);
442 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
443 dev->crop_cap = dev->src_rect;
444 dev->crop_bounds_cap = dev->src_rect;
445 dev->compose_cap = dev->crop_cap;
446 if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
447 dev->compose_cap.height /= 2;
448 dev->fmt_cap_rect = dev->compose_cap;
449 tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
450 tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
451 tpg_update_mv_step(&dev->tpg);
454 /* Map the field to something that is valid for the current input */
455 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
457 if (vivid_is_sdtv_cap(dev)) {
459 case V4L2_FIELD_INTERLACED_TB:
460 case V4L2_FIELD_INTERLACED_BT:
461 case V4L2_FIELD_SEQ_TB:
462 case V4L2_FIELD_SEQ_BT:
464 case V4L2_FIELD_BOTTOM:
465 case V4L2_FIELD_ALTERNATE:
467 case V4L2_FIELD_INTERLACED:
469 return V4L2_FIELD_INTERLACED;
472 if (vivid_is_hdmi_cap(dev))
473 return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
475 return V4L2_FIELD_NONE;
478 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
480 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
481 return tpg_g_colorspace(&dev->tpg);
482 return dev->colorspace_out;
485 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
487 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
488 return tpg_g_xfer_func(&dev->tpg);
489 return dev->xfer_func_out;
492 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
494 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
495 return tpg_g_ycbcr_enc(&dev->tpg);
496 return dev->ycbcr_enc_out;
499 static unsigned int vivid_hsv_enc_cap(struct vivid_dev *dev)
501 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
502 return tpg_g_hsv_enc(&dev->tpg);
503 return dev->hsv_enc_out;
506 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
508 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
509 return tpg_g_quantization(&dev->tpg);
510 return dev->quantization_out;
513 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
514 struct v4l2_format *f)
516 struct vivid_dev *dev = video_drvdata(file);
517 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
520 mp->width = dev->fmt_cap_rect.width;
521 mp->height = dev->fmt_cap_rect.height;
522 mp->field = dev->field_cap;
523 mp->pixelformat = dev->fmt_cap->fourcc;
524 mp->colorspace = vivid_colorspace_cap(dev);
525 mp->xfer_func = vivid_xfer_func_cap(dev);
526 if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_HSV)
527 mp->hsv_enc = vivid_hsv_enc_cap(dev);
529 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
530 mp->quantization = vivid_quantization_cap(dev);
531 mp->num_planes = dev->fmt_cap->buffers;
532 for (p = 0; p < mp->num_planes; p++) {
533 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
534 mp->plane_fmt[p].sizeimage =
535 tpg_g_line_width(&dev->tpg, p) * mp->height +
536 dev->fmt_cap->data_offset[p];
541 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
542 struct v4l2_format *f)
544 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
545 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
546 struct vivid_dev *dev = video_drvdata(file);
547 const struct vivid_fmt *fmt;
548 unsigned bytesperline, max_bpl;
553 fmt = vivid_get_format(dev, mp->pixelformat);
555 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
557 mp->pixelformat = V4L2_PIX_FMT_YUYV;
558 fmt = vivid_get_format(dev, mp->pixelformat);
561 mp->field = vivid_field_cap(dev, mp->field);
562 if (vivid_is_webcam(dev)) {
563 const struct v4l2_frmsize_discrete *sz =
564 v4l2_find_nearest_size(webcam_sizes,
565 VIVID_WEBCAM_SIZES, width,
566 height, mp->width, mp->height);
570 } else if (vivid_is_sdtv_cap(dev)) {
572 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
574 w = dev->src_rect.width;
575 h = dev->src_rect.height;
577 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
579 if (vivid_is_webcam(dev) ||
580 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
582 mp->height = h / factor;
584 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
586 v4l2_rect_set_min_size(&r, &vivid_min_rect);
587 v4l2_rect_set_max_size(&r, &vivid_max_rect);
588 if (dev->has_scaler_cap && !dev->has_compose_cap) {
589 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
591 v4l2_rect_set_max_size(&r, &max_r);
592 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
593 v4l2_rect_set_max_size(&r, &dev->src_rect);
594 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
595 v4l2_rect_set_min_size(&r, &dev->src_rect);
598 mp->height = r.height / factor;
601 /* This driver supports custom bytesperline values */
603 mp->num_planes = fmt->buffers;
604 for (p = 0; p < fmt->buffers; p++) {
605 /* Calculate the minimum supported bytesperline value */
606 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
607 /* Calculate the maximum supported bytesperline value */
608 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
610 if (pfmt[p].bytesperline > max_bpl)
611 pfmt[p].bytesperline = max_bpl;
612 if (pfmt[p].bytesperline < bytesperline)
613 pfmt[p].bytesperline = bytesperline;
615 pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
616 fmt->vdownsampling[p] + fmt->data_offset[p];
618 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
620 for (p = fmt->buffers; p < fmt->planes; p++)
621 pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
622 (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
623 (fmt->bit_depth[0] / fmt->vdownsampling[0]);
625 mp->colorspace = vivid_colorspace_cap(dev);
626 if (fmt->color_enc == TGP_COLOR_ENC_HSV)
627 mp->hsv_enc = vivid_hsv_enc_cap(dev);
629 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
630 mp->xfer_func = vivid_xfer_func_cap(dev);
631 mp->quantization = vivid_quantization_cap(dev);
632 memset(mp->reserved, 0, sizeof(mp->reserved));
636 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
637 struct v4l2_format *f)
639 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
640 struct vivid_dev *dev = video_drvdata(file);
641 struct v4l2_rect *crop = &dev->crop_cap;
642 struct v4l2_rect *compose = &dev->compose_cap;
643 struct vb2_queue *q = &dev->vb_vid_cap_q;
644 int ret = vivid_try_fmt_vid_cap(file, priv, f);
652 if (vb2_is_busy(q)) {
653 dprintk(dev, 1, "%s device busy\n", __func__);
657 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
658 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
662 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
663 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
666 /* Note: the webcam input doesn't support scaling, cropping or composing */
668 if (!vivid_is_webcam(dev) &&
669 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
670 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
672 if (dev->has_scaler_cap) {
673 if (dev->has_compose_cap)
674 v4l2_rect_map_inside(compose, &r);
677 if (dev->has_crop_cap && !dev->has_compose_cap) {
678 struct v4l2_rect min_r = {
681 factor * r.height / MAX_ZOOM
683 struct v4l2_rect max_r = {
686 factor * r.height * MAX_ZOOM
689 v4l2_rect_set_min_size(crop, &min_r);
690 v4l2_rect_set_max_size(crop, &max_r);
691 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
692 } else if (dev->has_crop_cap) {
693 struct v4l2_rect min_r = {
695 compose->width / MAX_ZOOM,
696 factor * compose->height / MAX_ZOOM
698 struct v4l2_rect max_r = {
700 compose->width * MAX_ZOOM,
701 factor * compose->height * MAX_ZOOM
704 v4l2_rect_set_min_size(crop, &min_r);
705 v4l2_rect_set_max_size(crop, &max_r);
706 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
708 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
710 v4l2_rect_set_size_to(crop, &r);
711 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
714 v4l2_rect_set_size_to(compose, &r);
715 } else if (!dev->has_crop_cap) {
716 v4l2_rect_map_inside(compose, &r);
719 v4l2_rect_set_max_size(crop, &r);
720 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
721 compose->top *= factor;
722 compose->height *= factor;
723 v4l2_rect_set_size_to(compose, crop);
724 v4l2_rect_map_inside(compose, &r);
725 compose->top /= factor;
726 compose->height /= factor;
728 } else if (vivid_is_webcam(dev)) {
729 /* Guaranteed to be a match */
730 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
731 if (webcam_sizes[i].width == mp->width &&
732 webcam_sizes[i].height == mp->height)
734 dev->webcam_size_idx = i;
735 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
736 dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
737 vivid_update_format_cap(dev, false);
739 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
741 v4l2_rect_set_size_to(compose, &r);
743 v4l2_rect_set_size_to(crop, &r);
746 dev->fmt_cap_rect.width = mp->width;
747 dev->fmt_cap_rect.height = mp->height;
748 tpg_s_buf_height(&dev->tpg, mp->height);
749 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
750 for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
751 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
752 dev->field_cap = mp->field;
753 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
754 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
756 tpg_s_field(&dev->tpg, dev->field_cap, false);
757 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
758 if (vivid_is_sdtv_cap(dev))
759 dev->tv_field_cap = mp->field;
760 tpg_update_mv_step(&dev->tpg);
764 int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
765 struct v4l2_format *f)
767 struct vivid_dev *dev = video_drvdata(file);
769 if (!dev->multiplanar)
771 return vivid_g_fmt_vid_cap(file, priv, f);
774 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
775 struct v4l2_format *f)
777 struct vivid_dev *dev = video_drvdata(file);
779 if (!dev->multiplanar)
781 return vivid_try_fmt_vid_cap(file, priv, f);
784 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
785 struct v4l2_format *f)
787 struct vivid_dev *dev = video_drvdata(file);
789 if (!dev->multiplanar)
791 return vivid_s_fmt_vid_cap(file, priv, f);
794 int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
795 struct v4l2_format *f)
797 struct vivid_dev *dev = video_drvdata(file);
799 if (dev->multiplanar)
801 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
804 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
805 struct v4l2_format *f)
807 struct vivid_dev *dev = video_drvdata(file);
809 if (dev->multiplanar)
811 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
814 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
815 struct v4l2_format *f)
817 struct vivid_dev *dev = video_drvdata(file);
819 if (dev->multiplanar)
821 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
824 int vivid_vid_cap_g_selection(struct file *file, void *priv,
825 struct v4l2_selection *sel)
827 struct vivid_dev *dev = video_drvdata(file);
829 if (!dev->has_crop_cap && !dev->has_compose_cap)
831 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
833 if (vivid_is_webcam(dev))
836 sel->r.left = sel->r.top = 0;
837 switch (sel->target) {
838 case V4L2_SEL_TGT_CROP:
839 if (!dev->has_crop_cap)
841 sel->r = dev->crop_cap;
843 case V4L2_SEL_TGT_CROP_DEFAULT:
844 case V4L2_SEL_TGT_CROP_BOUNDS:
845 if (!dev->has_crop_cap)
847 sel->r = dev->src_rect;
849 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
850 if (!dev->has_compose_cap)
852 sel->r = vivid_max_rect;
854 case V4L2_SEL_TGT_COMPOSE:
855 if (!dev->has_compose_cap)
857 sel->r = dev->compose_cap;
859 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
860 if (!dev->has_compose_cap)
862 sel->r = dev->fmt_cap_rect;
870 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
872 struct vivid_dev *dev = video_drvdata(file);
873 struct v4l2_rect *crop = &dev->crop_cap;
874 struct v4l2_rect *compose = &dev->compose_cap;
875 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
878 if (!dev->has_crop_cap && !dev->has_compose_cap)
880 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
882 if (vivid_is_webcam(dev))
886 case V4L2_SEL_TGT_CROP:
887 if (!dev->has_crop_cap)
889 ret = vivid_vid_adjust_sel(s->flags, &s->r);
892 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
893 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
894 v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
896 s->r.height /= factor;
897 if (dev->has_scaler_cap) {
898 struct v4l2_rect fmt = dev->fmt_cap_rect;
899 struct v4l2_rect max_rect = {
901 s->r.width * MAX_ZOOM,
902 s->r.height * MAX_ZOOM
904 struct v4l2_rect min_rect = {
906 s->r.width / MAX_ZOOM,
907 s->r.height / MAX_ZOOM
910 v4l2_rect_set_min_size(&fmt, &min_rect);
911 if (!dev->has_compose_cap)
912 v4l2_rect_set_max_size(&fmt, &max_rect);
913 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
914 vb2_is_busy(&dev->vb_vid_cap_q))
916 if (dev->has_compose_cap) {
917 v4l2_rect_set_min_size(compose, &min_rect);
918 v4l2_rect_set_max_size(compose, &max_rect);
920 dev->fmt_cap_rect = fmt;
921 tpg_s_buf_height(&dev->tpg, fmt.height);
922 } else if (dev->has_compose_cap) {
923 struct v4l2_rect fmt = dev->fmt_cap_rect;
925 v4l2_rect_set_min_size(&fmt, &s->r);
926 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
927 vb2_is_busy(&dev->vb_vid_cap_q))
929 dev->fmt_cap_rect = fmt;
930 tpg_s_buf_height(&dev->tpg, fmt.height);
931 v4l2_rect_set_size_to(compose, &s->r);
932 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
934 if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
935 vb2_is_busy(&dev->vb_vid_cap_q))
937 v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
938 v4l2_rect_set_size_to(compose, &s->r);
939 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
940 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
943 s->r.height *= factor;
946 case V4L2_SEL_TGT_COMPOSE:
947 if (!dev->has_compose_cap)
949 ret = vivid_vid_adjust_sel(s->flags, &s->r);
952 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
953 v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
954 if (dev->has_scaler_cap) {
955 struct v4l2_rect max_rect = {
957 dev->src_rect.width * MAX_ZOOM,
958 (dev->src_rect.height / factor) * MAX_ZOOM
961 v4l2_rect_set_max_size(&s->r, &max_rect);
962 if (dev->has_crop_cap) {
963 struct v4l2_rect min_rect = {
965 s->r.width / MAX_ZOOM,
966 (s->r.height * factor) / MAX_ZOOM
968 struct v4l2_rect max_rect = {
970 s->r.width * MAX_ZOOM,
971 (s->r.height * factor) * MAX_ZOOM
974 v4l2_rect_set_min_size(crop, &min_rect);
975 v4l2_rect_set_max_size(crop, &max_rect);
976 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
978 } else if (dev->has_crop_cap) {
980 s->r.height *= factor;
981 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
982 v4l2_rect_set_size_to(crop, &s->r);
983 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
985 s->r.height /= factor;
987 v4l2_rect_set_size_to(&s->r, &dev->src_rect);
988 s->r.height /= factor;
990 v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
991 if (dev->bitmap_cap && (compose->width != s->r.width ||
992 compose->height != s->r.height)) {
993 kfree(dev->bitmap_cap);
994 dev->bitmap_cap = NULL;
1002 tpg_s_crop_compose(&dev->tpg, crop, compose);
1006 int vivid_vid_cap_cropcap(struct file *file, void *priv,
1007 struct v4l2_cropcap *cap)
1009 struct vivid_dev *dev = video_drvdata(file);
1011 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1014 switch (vivid_get_pixel_aspect(dev)) {
1015 case TPG_PIXEL_ASPECT_NTSC:
1016 cap->pixelaspect.numerator = 11;
1017 cap->pixelaspect.denominator = 10;
1019 case TPG_PIXEL_ASPECT_PAL:
1020 cap->pixelaspect.numerator = 54;
1021 cap->pixelaspect.denominator = 59;
1023 case TPG_PIXEL_ASPECT_SQUARE:
1024 cap->pixelaspect.numerator = 1;
1025 cap->pixelaspect.denominator = 1;
1031 int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1032 struct v4l2_fmtdesc *f)
1034 struct vivid_dev *dev = video_drvdata(file);
1035 const struct vivid_fmt *fmt;
1037 if (dev->multiplanar)
1040 if (f->index >= ARRAY_SIZE(formats_ovl))
1043 fmt = &formats_ovl[f->index];
1045 f->pixelformat = fmt->fourcc;
1049 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1050 struct v4l2_format *f)
1052 struct vivid_dev *dev = video_drvdata(file);
1053 const struct v4l2_rect *compose = &dev->compose_cap;
1054 struct v4l2_window *win = &f->fmt.win;
1055 unsigned clipcount = win->clipcount;
1057 if (dev->multiplanar)
1060 win->w.top = dev->overlay_cap_top;
1061 win->w.left = dev->overlay_cap_left;
1062 win->w.width = compose->width;
1063 win->w.height = compose->height;
1064 win->field = dev->overlay_cap_field;
1065 win->clipcount = dev->clipcount_cap;
1066 if (clipcount > dev->clipcount_cap)
1067 clipcount = dev->clipcount_cap;
1068 if (dev->bitmap_cap == NULL)
1070 else if (win->bitmap) {
1071 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1072 ((compose->width + 7) / 8) * compose->height))
1075 if (clipcount && win->clips) {
1076 if (copy_to_user(win->clips, dev->clips_cap,
1077 clipcount * sizeof(dev->clips_cap[0])))
1083 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1084 struct v4l2_format *f)
1086 struct vivid_dev *dev = video_drvdata(file);
1087 const struct v4l2_rect *compose = &dev->compose_cap;
1088 struct v4l2_window *win = &f->fmt.win;
1091 if (dev->multiplanar)
1094 win->w.left = clamp_t(int, win->w.left,
1095 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1096 win->w.top = clamp_t(int, win->w.top,
1097 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1098 win->w.width = compose->width;
1099 win->w.height = compose->height;
1100 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1101 win->field = V4L2_FIELD_ANY;
1103 win->global_alpha = 0;
1104 if (win->clipcount && !win->clips)
1106 if (win->clipcount > MAX_CLIPS)
1107 win->clipcount = MAX_CLIPS;
1108 if (win->clipcount) {
1109 if (copy_from_user(dev->try_clips_cap, win->clips,
1110 win->clipcount * sizeof(dev->clips_cap[0])))
1112 for (i = 0; i < win->clipcount; i++) {
1113 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1115 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1116 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1117 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1118 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1121 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1122 * number and it's typically a one-time deal.
1124 for (i = 0; i < win->clipcount - 1; i++) {
1125 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1127 for (j = i + 1; j < win->clipcount; j++) {
1128 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1130 if (v4l2_rect_overlap(r1, r2))
1134 if (copy_to_user(win->clips, dev->try_clips_cap,
1135 win->clipcount * sizeof(dev->clips_cap[0])))
1141 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1142 struct v4l2_format *f)
1144 struct vivid_dev *dev = video_drvdata(file);
1145 const struct v4l2_rect *compose = &dev->compose_cap;
1146 struct v4l2_window *win = &f->fmt.win;
1147 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1148 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1149 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1150 void *new_bitmap = NULL;
1156 new_bitmap = vzalloc(bitmap_size);
1158 if (new_bitmap == NULL)
1160 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1166 dev->overlay_cap_top = win->w.top;
1167 dev->overlay_cap_left = win->w.left;
1168 dev->overlay_cap_field = win->field;
1169 vfree(dev->bitmap_cap);
1170 dev->bitmap_cap = new_bitmap;
1171 dev->clipcount_cap = win->clipcount;
1172 if (dev->clipcount_cap)
1173 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1177 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1179 struct vivid_dev *dev = video_drvdata(file);
1181 if (dev->multiplanar)
1184 if (i && dev->fb_vbase_cap == NULL)
1187 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1188 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1192 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1194 dev->overlay_cap_owner = i ? fh : NULL;
1198 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1199 struct v4l2_framebuffer *a)
1201 struct vivid_dev *dev = video_drvdata(file);
1203 if (dev->multiplanar)
1207 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1208 V4L2_FBUF_CAP_LIST_CLIPPING;
1209 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1210 a->fmt.field = V4L2_FIELD_NONE;
1211 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1216 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1217 const struct v4l2_framebuffer *a)
1219 struct vivid_dev *dev = video_drvdata(file);
1220 const struct vivid_fmt *fmt;
1222 if (dev->multiplanar)
1225 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1228 if (dev->overlay_cap_owner)
1231 if (a->base == NULL) {
1232 dev->fb_cap.base = NULL;
1233 dev->fb_vbase_cap = NULL;
1237 if (a->fmt.width < 48 || a->fmt.height < 32)
1239 fmt = vivid_get_format(dev, a->fmt.pixelformat);
1240 if (!fmt || !fmt->can_do_overlay)
1242 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1244 if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
1247 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1249 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1250 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1251 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1252 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1256 static const struct v4l2_audio vivid_audio_inputs[] = {
1257 { 0, "TV", V4L2_AUDCAP_STEREO },
1258 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1261 int vidioc_enum_input(struct file *file, void *priv,
1262 struct v4l2_input *inp)
1264 struct vivid_dev *dev = video_drvdata(file);
1266 if (inp->index >= dev->num_inputs)
1269 inp->type = V4L2_INPUT_TYPE_CAMERA;
1270 switch (dev->input_type[inp->index]) {
1272 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1273 dev->input_name_counter[inp->index]);
1274 inp->capabilities = 0;
1277 snprintf(inp->name, sizeof(inp->name), "TV %u",
1278 dev->input_name_counter[inp->index]);
1279 inp->type = V4L2_INPUT_TYPE_TUNER;
1280 inp->std = V4L2_STD_ALL;
1281 if (dev->has_audio_inputs)
1282 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1283 inp->capabilities = V4L2_IN_CAP_STD;
1286 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1287 dev->input_name_counter[inp->index]);
1288 inp->std = V4L2_STD_ALL;
1289 if (dev->has_audio_inputs)
1290 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1291 inp->capabilities = V4L2_IN_CAP_STD;
1294 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1295 dev->input_name_counter[inp->index]);
1296 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1297 if (dev->edid_blocks == 0 ||
1298 dev->dv_timings_signal_mode == NO_SIGNAL)
1299 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1300 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1301 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1302 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1305 if (dev->sensor_hflip)
1306 inp->status |= V4L2_IN_ST_HFLIP;
1307 if (dev->sensor_vflip)
1308 inp->status |= V4L2_IN_ST_VFLIP;
1309 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1310 if (dev->std_signal_mode == NO_SIGNAL) {
1311 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1312 } else if (dev->std_signal_mode == NO_LOCK) {
1313 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1314 } else if (vivid_is_tv_cap(dev)) {
1315 switch (tpg_g_quality(&dev->tpg)) {
1317 inp->status |= V4L2_IN_ST_COLOR_KILL;
1319 case TPG_QUAL_NOISE:
1320 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1330 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1332 struct vivid_dev *dev = video_drvdata(file);
1338 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1340 struct vivid_dev *dev = video_drvdata(file);
1341 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1342 unsigned brightness;
1344 if (i >= dev->num_inputs)
1347 if (i == dev->input)
1350 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1354 dev->vid_cap_dev.tvnorms = 0;
1355 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1356 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1357 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1359 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1360 vivid_update_format_cap(dev, false);
1362 if (dev->colorspace) {
1363 switch (dev->input_type[i]) {
1365 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1369 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1372 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1373 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1374 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1376 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1378 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1385 * Modify the brightness range depending on the input.
1386 * This makes it easy to use vivid to test if applications can
1387 * handle control range modifications and is also how this is
1388 * typically used in practice as different inputs may be hooked
1389 * up to different receivers with different control ranges.
1391 brightness = 128 * i + dev->input_brightness[i];
1392 v4l2_ctrl_modify_range(dev->brightness,
1393 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1394 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1398 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1400 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1402 *vin = vivid_audio_inputs[vin->index];
1406 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1408 struct vivid_dev *dev = video_drvdata(file);
1410 if (!vivid_is_sdtv_cap(dev))
1412 *vin = vivid_audio_inputs[dev->tv_audio_input];
1416 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1418 struct vivid_dev *dev = video_drvdata(file);
1420 if (!vivid_is_sdtv_cap(dev))
1422 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1424 dev->tv_audio_input = vin->index;
1428 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1430 struct vivid_dev *dev = video_drvdata(file);
1434 vf->frequency = dev->tv_freq;
1438 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1440 struct vivid_dev *dev = video_drvdata(file);
1444 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1445 if (vivid_is_tv_cap(dev))
1446 vivid_update_quality(dev);
1450 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1452 struct vivid_dev *dev = video_drvdata(file);
1456 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1458 dev->tv_audmode = vt->audmode;
1462 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1464 struct vivid_dev *dev = video_drvdata(file);
1465 enum tpg_quality qual;
1470 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1471 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1472 vt->audmode = dev->tv_audmode;
1473 vt->rangelow = MIN_TV_FREQ;
1474 vt->rangehigh = MAX_TV_FREQ;
1475 qual = vivid_get_quality(dev, &vt->afc);
1476 if (qual == TPG_QUAL_COLOR)
1477 vt->signal = 0xffff;
1478 else if (qual == TPG_QUAL_GRAY)
1479 vt->signal = 0x8000;
1482 if (qual == TPG_QUAL_NOISE) {
1484 } else if (qual == TPG_QUAL_GRAY) {
1485 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1487 unsigned channel_nr = dev->tv_freq / (6 * 16);
1488 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1490 switch (channel_nr % options) {
1492 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1495 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1498 if (dev->std_cap & V4L2_STD_NTSC_M)
1499 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1501 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1504 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1508 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1512 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1513 const v4l2_std_id vivid_standard[] = {
1518 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1525 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1532 /* Must remain in sync with the vivid_standard array */
1533 const char * const vivid_ctrl_standard_strings[] = {
1552 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1554 struct vivid_dev *dev = video_drvdata(file);
1556 if (!vivid_is_sdtv_cap(dev))
1558 if (dev->std_signal_mode == NO_SIGNAL ||
1559 dev->std_signal_mode == NO_LOCK) {
1560 *id = V4L2_STD_UNKNOWN;
1563 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1564 *id = V4L2_STD_UNKNOWN;
1565 } else if (dev->std_signal_mode == CURRENT_STD) {
1567 } else if (dev->std_signal_mode == SELECTED_STD) {
1568 *id = dev->query_std;
1570 *id = vivid_standard[dev->query_std_last];
1571 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1577 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1579 struct vivid_dev *dev = video_drvdata(file);
1581 if (!vivid_is_sdtv_cap(dev))
1583 if (dev->std_cap == id)
1585 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1588 vivid_update_format_cap(dev, false);
1592 static void find_aspect_ratio(u32 width, u32 height,
1593 u32 *num, u32 *denom)
1595 if (!(height % 3) && ((height * 4 / 3) == width)) {
1598 } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1601 } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1604 } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1607 } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1610 } else { /* default to 16:9 */
1616 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1618 struct v4l2_bt_timings *bt = &timings->bt;
1623 if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1627 total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1628 total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1630 h_freq = (u32)bt->pixelclock / total_h_pixel;
1632 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1633 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1634 bt->polarities, bt->interlaced, timings))
1638 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1639 struct v4l2_fract aspect_ratio;
1641 find_aspect_ratio(bt->width, bt->height,
1642 &aspect_ratio.numerator,
1643 &aspect_ratio.denominator);
1644 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1645 bt->polarities, bt->interlaced,
1646 aspect_ratio, timings))
1652 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1653 struct v4l2_dv_timings *timings)
1655 struct vivid_dev *dev = video_drvdata(file);
1657 if (!vivid_is_hdmi_cap(dev))
1659 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1661 !valid_cvt_gtf_timings(timings))
1664 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
1666 if (vb2_is_busy(&dev->vb_vid_cap_q))
1669 dev->dv_timings_cap = *timings;
1670 vivid_update_format_cap(dev, false);
1674 int vidioc_query_dv_timings(struct file *file, void *_fh,
1675 struct v4l2_dv_timings *timings)
1677 struct vivid_dev *dev = video_drvdata(file);
1679 if (!vivid_is_hdmi_cap(dev))
1681 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1682 dev->edid_blocks == 0)
1684 if (dev->dv_timings_signal_mode == NO_LOCK)
1686 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1687 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1690 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1691 *timings = dev->dv_timings_cap;
1692 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1693 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1695 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1696 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1697 dev->query_dv_timings_size;
1702 int vidioc_s_edid(struct file *file, void *_fh,
1703 struct v4l2_edid *edid)
1705 struct vivid_dev *dev = video_drvdata(file);
1710 memset(edid->reserved, 0, sizeof(edid->reserved));
1711 if (edid->pad >= dev->num_inputs)
1713 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1715 if (edid->blocks == 0) {
1716 dev->edid_blocks = 0;
1717 phys_addr = CEC_PHYS_ADDR_INVALID;
1720 if (edid->blocks > dev->edid_max_blocks) {
1721 edid->blocks = dev->edid_max_blocks;
1724 phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1725 ret = cec_phys_addr_validate(phys_addr, &phys_addr, NULL);
1729 if (vb2_is_busy(&dev->vb_vid_cap_q))
1732 dev->edid_blocks = edid->blocks;
1733 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1736 /* TODO: a proper hotplug detect cycle should be emulated here */
1737 cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1739 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1740 cec_s_phys_addr(dev->cec_tx_adap[i],
1741 cec_phys_addr_for_input(phys_addr, i + 1),
1746 int vidioc_enum_framesizes(struct file *file, void *fh,
1747 struct v4l2_frmsizeenum *fsize)
1749 struct vivid_dev *dev = video_drvdata(file);
1751 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1753 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1755 if (vivid_is_webcam(dev)) {
1756 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1758 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1759 fsize->discrete = webcam_sizes[fsize->index];
1764 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1765 fsize->stepwise.min_width = MIN_WIDTH;
1766 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1767 fsize->stepwise.step_width = 2;
1768 fsize->stepwise.min_height = MIN_HEIGHT;
1769 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1770 fsize->stepwise.step_height = 2;
1774 /* timeperframe is arbitrary and continuous */
1775 int vidioc_enum_frameintervals(struct file *file, void *priv,
1776 struct v4l2_frmivalenum *fival)
1778 struct vivid_dev *dev = video_drvdata(file);
1779 const struct vivid_fmt *fmt;
1782 fmt = vivid_get_format(dev, fival->pixel_format);
1786 if (!vivid_is_webcam(dev)) {
1789 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1791 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1793 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1794 fival->discrete = dev->timeperframe_vid_cap;
1798 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1799 if (fival->width == webcam_sizes[i].width &&
1800 fival->height == webcam_sizes[i].height)
1802 if (i == ARRAY_SIZE(webcam_sizes))
1804 if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1806 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1807 fival->discrete = webcam_intervals[fival->index];
1811 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1812 struct v4l2_streamparm *parm)
1814 struct vivid_dev *dev = video_drvdata(file);
1816 if (parm->type != (dev->multiplanar ?
1817 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1818 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1821 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1822 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1823 parm->parm.capture.readbuffers = 1;
1827 #define FRACT_CMP(a, OP, b) \
1828 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1830 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1831 struct v4l2_streamparm *parm)
1833 struct vivid_dev *dev = video_drvdata(file);
1834 unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1835 struct v4l2_fract tpf;
1838 if (parm->type != (dev->multiplanar ?
1839 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1840 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1842 if (!vivid_is_webcam(dev))
1843 return vivid_vid_cap_g_parm(file, priv, parm);
1845 tpf = parm->parm.capture.timeperframe;
1847 if (tpf.denominator == 0)
1848 tpf = webcam_intervals[ival_sz - 1];
1849 for (i = 0; i < ival_sz; i++)
1850 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1854 dev->webcam_ival_idx = i;
1855 tpf = webcam_intervals[dev->webcam_ival_idx];
1856 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1857 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1859 /* resync the thread's timings */
1860 dev->cap_seq_resync = true;
1861 dev->timeperframe_vid_cap = tpf;
1862 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1863 parm->parm.capture.timeperframe = tpf;
1864 parm->parm.capture.readbuffers = 1;