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 dev->vid_cap_seq_count = 0;
226 dprintk(dev, 1, "%s\n", __func__);
227 for (i = 0; i < VIDEO_MAX_FRAME; i++)
228 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
229 if (dev->start_streaming_error) {
230 dev->start_streaming_error = false;
233 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
236 struct vivid_buffer *buf, *tmp;
238 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
239 list_del(&buf->list);
240 vb2_buffer_done(&buf->vb.vb2_buf,
241 VB2_BUF_STATE_QUEUED);
247 /* abort streaming and wait for last buffer */
248 static void vid_cap_stop_streaming(struct vb2_queue *vq)
250 struct vivid_dev *dev = vb2_get_drv_priv(vq);
252 dprintk(dev, 1, "%s\n", __func__);
253 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
254 dev->can_loop_video = false;
257 const struct vb2_ops vivid_vid_cap_qops = {
258 .queue_setup = vid_cap_queue_setup,
259 .buf_prepare = vid_cap_buf_prepare,
260 .buf_finish = vid_cap_buf_finish,
261 .buf_queue = vid_cap_buf_queue,
262 .start_streaming = vid_cap_start_streaming,
263 .stop_streaming = vid_cap_stop_streaming,
264 .wait_prepare = vb2_ops_wait_prepare,
265 .wait_finish = vb2_ops_wait_finish,
269 * Determine the 'picture' quality based on the current TV frequency: either
270 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
271 * signal or NOISE for no signal.
273 void vivid_update_quality(struct vivid_dev *dev)
275 unsigned freq_modulus;
277 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
279 * The 'noise' will only be replaced by the actual video
280 * if the output video matches the input video settings.
282 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
285 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
286 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
289 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
290 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
293 if (!vivid_is_tv_cap(dev)) {
294 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
299 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
300 * From +/- 0.25 MHz around the channel there is color, and from
301 * +/- 1 MHz there is grayscale (chroma is lost).
302 * Everywhere else it is just noise.
304 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
305 if (freq_modulus > 2 * 16) {
306 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
307 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
310 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
311 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
313 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
317 * Get the current picture quality and the associated afc value.
319 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
321 unsigned freq_modulus;
325 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
326 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
327 return tpg_g_quality(&dev->tpg);
330 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
331 * From +/- 0.25 MHz around the channel there is color, and from
332 * +/- 1 MHz there is grayscale (chroma is lost).
333 * Everywhere else it is just gray.
335 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
337 *afc = freq_modulus - 1 * 16;
338 return TPG_QUAL_GRAY;
341 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
343 if (vivid_is_sdtv_cap(dev))
344 return dev->std_aspect_ratio;
346 if (vivid_is_hdmi_cap(dev))
347 return dev->dv_timings_aspect_ratio;
349 return TPG_VIDEO_ASPECT_IMAGE;
352 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
354 if (vivid_is_sdtv_cap(dev))
355 return (dev->std_cap & V4L2_STD_525_60) ?
356 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
358 if (vivid_is_hdmi_cap(dev) &&
359 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
360 return dev->src_rect.height == 480 ?
361 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
363 return TPG_PIXEL_ASPECT_SQUARE;
367 * Called whenever the format has to be reset which can occur when
368 * changing inputs, standard, timings, etc.
370 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
372 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
376 switch (dev->input_type[dev->input]) {
379 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
380 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
381 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
382 dev->field_cap = V4L2_FIELD_NONE;
383 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
387 dev->field_cap = dev->tv_field_cap;
388 dev->src_rect.width = 720;
389 if (dev->std_cap & V4L2_STD_525_60) {
390 dev->src_rect.height = 480;
391 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
392 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
394 dev->src_rect.height = 576;
395 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
396 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
398 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
401 dev->src_rect.width = bt->width;
402 dev->src_rect.height = bt->height;
403 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
404 if (dev->reduced_fps && can_reduce_fps(bt)) {
405 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
406 bt->flags |= V4L2_DV_FL_REDUCED_FPS;
408 pixelclock = bt->pixelclock;
409 bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
411 dev->timeperframe_vid_cap = (struct v4l2_fract) {
412 size / 100, (u32)pixelclock / 100
415 dev->field_cap = V4L2_FIELD_ALTERNATE;
417 dev->field_cap = V4L2_FIELD_NONE;
420 * We can be called from within s_ctrl, in that case we can't
421 * set/get controls. Luckily we don't need to in that case.
423 if (keep_controls || !dev->colorspace)
425 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
426 if (bt->width == 720 && bt->height <= 576)
427 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
429 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
430 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
432 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
433 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
435 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
438 vfree(dev->bitmap_cap);
439 dev->bitmap_cap = NULL;
440 vivid_update_quality(dev);
441 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
442 dev->crop_cap = dev->src_rect;
443 dev->crop_bounds_cap = dev->src_rect;
444 if (dev->bitmap_cap &&
445 (dev->compose_cap.width != dev->crop_cap.width ||
446 dev->compose_cap.height != dev->crop_cap.height)) {
447 vfree(dev->bitmap_cap);
448 dev->bitmap_cap = NULL;
450 dev->compose_cap = dev->crop_cap;
451 if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
452 dev->compose_cap.height /= 2;
453 dev->fmt_cap_rect = dev->compose_cap;
454 tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
455 tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
456 tpg_update_mv_step(&dev->tpg);
459 /* Map the field to something that is valid for the current input */
460 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
462 if (vivid_is_sdtv_cap(dev)) {
464 case V4L2_FIELD_INTERLACED_TB:
465 case V4L2_FIELD_INTERLACED_BT:
466 case V4L2_FIELD_SEQ_TB:
467 case V4L2_FIELD_SEQ_BT:
469 case V4L2_FIELD_BOTTOM:
470 case V4L2_FIELD_ALTERNATE:
472 case V4L2_FIELD_INTERLACED:
474 return V4L2_FIELD_INTERLACED;
477 if (vivid_is_hdmi_cap(dev))
478 return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
480 return V4L2_FIELD_NONE;
483 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
485 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
486 return tpg_g_colorspace(&dev->tpg);
487 return dev->colorspace_out;
490 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
492 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
493 return tpg_g_xfer_func(&dev->tpg);
494 return dev->xfer_func_out;
497 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
499 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
500 return tpg_g_ycbcr_enc(&dev->tpg);
501 return dev->ycbcr_enc_out;
504 static unsigned int vivid_hsv_enc_cap(struct vivid_dev *dev)
506 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
507 return tpg_g_hsv_enc(&dev->tpg);
508 return dev->hsv_enc_out;
511 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
513 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
514 return tpg_g_quantization(&dev->tpg);
515 return dev->quantization_out;
518 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
519 struct v4l2_format *f)
521 struct vivid_dev *dev = video_drvdata(file);
522 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
525 mp->width = dev->fmt_cap_rect.width;
526 mp->height = dev->fmt_cap_rect.height;
527 mp->field = dev->field_cap;
528 mp->pixelformat = dev->fmt_cap->fourcc;
529 mp->colorspace = vivid_colorspace_cap(dev);
530 mp->xfer_func = vivid_xfer_func_cap(dev);
531 if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_HSV)
532 mp->hsv_enc = vivid_hsv_enc_cap(dev);
534 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
535 mp->quantization = vivid_quantization_cap(dev);
536 mp->num_planes = dev->fmt_cap->buffers;
537 for (p = 0; p < mp->num_planes; p++) {
538 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
539 mp->plane_fmt[p].sizeimage =
540 tpg_g_line_width(&dev->tpg, p) * mp->height +
541 dev->fmt_cap->data_offset[p];
546 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
547 struct v4l2_format *f)
549 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
550 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
551 struct vivid_dev *dev = video_drvdata(file);
552 const struct vivid_fmt *fmt;
553 unsigned bytesperline, max_bpl;
558 fmt = vivid_get_format(dev, mp->pixelformat);
560 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
562 mp->pixelformat = V4L2_PIX_FMT_YUYV;
563 fmt = vivid_get_format(dev, mp->pixelformat);
566 mp->field = vivid_field_cap(dev, mp->field);
567 if (vivid_is_webcam(dev)) {
568 const struct v4l2_frmsize_discrete *sz =
569 v4l2_find_nearest_size(webcam_sizes,
570 VIVID_WEBCAM_SIZES, width,
571 height, mp->width, mp->height);
575 } else if (vivid_is_sdtv_cap(dev)) {
577 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
579 w = dev->src_rect.width;
580 h = dev->src_rect.height;
582 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
584 if (vivid_is_webcam(dev) ||
585 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
587 mp->height = h / factor;
589 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
591 v4l2_rect_set_min_size(&r, &vivid_min_rect);
592 v4l2_rect_set_max_size(&r, &vivid_max_rect);
593 if (dev->has_scaler_cap && !dev->has_compose_cap) {
594 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
596 v4l2_rect_set_max_size(&r, &max_r);
597 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
598 v4l2_rect_set_max_size(&r, &dev->src_rect);
599 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
600 v4l2_rect_set_min_size(&r, &dev->src_rect);
603 mp->height = r.height / factor;
606 /* This driver supports custom bytesperline values */
608 mp->num_planes = fmt->buffers;
609 for (p = 0; p < fmt->buffers; p++) {
610 /* Calculate the minimum supported bytesperline value */
611 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
612 /* Calculate the maximum supported bytesperline value */
613 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
615 if (pfmt[p].bytesperline > max_bpl)
616 pfmt[p].bytesperline = max_bpl;
617 if (pfmt[p].bytesperline < bytesperline)
618 pfmt[p].bytesperline = bytesperline;
620 pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
621 fmt->vdownsampling[p] + fmt->data_offset[p];
623 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
625 for (p = fmt->buffers; p < fmt->planes; p++)
626 pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
627 (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
628 (fmt->bit_depth[0] / fmt->vdownsampling[0]);
630 mp->colorspace = vivid_colorspace_cap(dev);
631 if (fmt->color_enc == TGP_COLOR_ENC_HSV)
632 mp->hsv_enc = vivid_hsv_enc_cap(dev);
634 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
635 mp->xfer_func = vivid_xfer_func_cap(dev);
636 mp->quantization = vivid_quantization_cap(dev);
637 memset(mp->reserved, 0, sizeof(mp->reserved));
641 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
642 struct v4l2_format *f)
644 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
645 struct vivid_dev *dev = video_drvdata(file);
646 struct v4l2_rect *crop = &dev->crop_cap;
647 struct v4l2_rect *compose = &dev->compose_cap;
648 struct vb2_queue *q = &dev->vb_vid_cap_q;
649 int ret = vivid_try_fmt_vid_cap(file, priv, f);
657 if (vb2_is_busy(q)) {
658 dprintk(dev, 1, "%s device busy\n", __func__);
662 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
663 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
667 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
668 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
671 /* Note: the webcam input doesn't support scaling, cropping or composing */
673 if (!vivid_is_webcam(dev) &&
674 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
675 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
677 if (dev->has_scaler_cap) {
678 if (dev->has_compose_cap)
679 v4l2_rect_map_inside(compose, &r);
682 if (dev->has_crop_cap && !dev->has_compose_cap) {
683 struct v4l2_rect min_r = {
686 factor * r.height / MAX_ZOOM
688 struct v4l2_rect max_r = {
691 factor * r.height * MAX_ZOOM
694 v4l2_rect_set_min_size(crop, &min_r);
695 v4l2_rect_set_max_size(crop, &max_r);
696 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
697 } else if (dev->has_crop_cap) {
698 struct v4l2_rect min_r = {
700 compose->width / MAX_ZOOM,
701 factor * compose->height / MAX_ZOOM
703 struct v4l2_rect max_r = {
705 compose->width * MAX_ZOOM,
706 factor * compose->height * MAX_ZOOM
709 v4l2_rect_set_min_size(crop, &min_r);
710 v4l2_rect_set_max_size(crop, &max_r);
711 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
713 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
715 v4l2_rect_set_size_to(crop, &r);
716 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
719 v4l2_rect_set_size_to(compose, &r);
720 } else if (!dev->has_crop_cap) {
721 v4l2_rect_map_inside(compose, &r);
724 v4l2_rect_set_max_size(crop, &r);
725 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
726 compose->top *= factor;
727 compose->height *= factor;
728 v4l2_rect_set_size_to(compose, crop);
729 v4l2_rect_map_inside(compose, &r);
730 compose->top /= factor;
731 compose->height /= factor;
733 } else if (vivid_is_webcam(dev)) {
734 /* Guaranteed to be a match */
735 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
736 if (webcam_sizes[i].width == mp->width &&
737 webcam_sizes[i].height == mp->height)
739 dev->webcam_size_idx = i;
740 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
741 dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
742 vivid_update_format_cap(dev, false);
744 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
746 v4l2_rect_set_size_to(compose, &r);
748 v4l2_rect_set_size_to(crop, &r);
751 dev->fmt_cap_rect.width = mp->width;
752 dev->fmt_cap_rect.height = mp->height;
753 tpg_s_buf_height(&dev->tpg, mp->height);
754 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
755 for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
756 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
757 dev->field_cap = mp->field;
758 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
759 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
761 tpg_s_field(&dev->tpg, dev->field_cap, false);
762 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
763 if (vivid_is_sdtv_cap(dev))
764 dev->tv_field_cap = mp->field;
765 tpg_update_mv_step(&dev->tpg);
769 int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
770 struct v4l2_format *f)
772 struct vivid_dev *dev = video_drvdata(file);
774 if (!dev->multiplanar)
776 return vivid_g_fmt_vid_cap(file, priv, f);
779 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
780 struct v4l2_format *f)
782 struct vivid_dev *dev = video_drvdata(file);
784 if (!dev->multiplanar)
786 return vivid_try_fmt_vid_cap(file, priv, f);
789 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
790 struct v4l2_format *f)
792 struct vivid_dev *dev = video_drvdata(file);
794 if (!dev->multiplanar)
796 return vivid_s_fmt_vid_cap(file, priv, f);
799 int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
800 struct v4l2_format *f)
802 struct vivid_dev *dev = video_drvdata(file);
804 if (dev->multiplanar)
806 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
809 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
810 struct v4l2_format *f)
812 struct vivid_dev *dev = video_drvdata(file);
814 if (dev->multiplanar)
816 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
819 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
820 struct v4l2_format *f)
822 struct vivid_dev *dev = video_drvdata(file);
824 if (dev->multiplanar)
826 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
829 int vivid_vid_cap_g_selection(struct file *file, void *priv,
830 struct v4l2_selection *sel)
832 struct vivid_dev *dev = video_drvdata(file);
834 if (!dev->has_crop_cap && !dev->has_compose_cap)
836 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
838 if (vivid_is_webcam(dev))
841 sel->r.left = sel->r.top = 0;
842 switch (sel->target) {
843 case V4L2_SEL_TGT_CROP:
844 if (!dev->has_crop_cap)
846 sel->r = dev->crop_cap;
848 case V4L2_SEL_TGT_CROP_DEFAULT:
849 case V4L2_SEL_TGT_CROP_BOUNDS:
850 if (!dev->has_crop_cap)
852 sel->r = dev->src_rect;
854 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
855 if (!dev->has_compose_cap)
857 sel->r = vivid_max_rect;
859 case V4L2_SEL_TGT_COMPOSE:
860 if (!dev->has_compose_cap)
862 sel->r = dev->compose_cap;
864 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
865 if (!dev->has_compose_cap)
867 sel->r = dev->fmt_cap_rect;
875 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
877 struct vivid_dev *dev = video_drvdata(file);
878 struct v4l2_rect *crop = &dev->crop_cap;
879 struct v4l2_rect *compose = &dev->compose_cap;
880 unsigned orig_compose_w = compose->width;
881 unsigned orig_compose_h = compose->height;
882 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
885 if (!dev->has_crop_cap && !dev->has_compose_cap)
887 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
889 if (vivid_is_webcam(dev))
893 case V4L2_SEL_TGT_CROP:
894 if (!dev->has_crop_cap)
896 ret = vivid_vid_adjust_sel(s->flags, &s->r);
899 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
900 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
901 v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
903 s->r.height /= factor;
904 if (dev->has_scaler_cap) {
905 struct v4l2_rect fmt = dev->fmt_cap_rect;
906 struct v4l2_rect max_rect = {
908 s->r.width * MAX_ZOOM,
909 s->r.height * MAX_ZOOM
911 struct v4l2_rect min_rect = {
913 s->r.width / MAX_ZOOM,
914 s->r.height / MAX_ZOOM
917 v4l2_rect_set_min_size(&fmt, &min_rect);
918 if (!dev->has_compose_cap)
919 v4l2_rect_set_max_size(&fmt, &max_rect);
920 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
921 vb2_is_busy(&dev->vb_vid_cap_q))
923 if (dev->has_compose_cap) {
924 v4l2_rect_set_min_size(compose, &min_rect);
925 v4l2_rect_set_max_size(compose, &max_rect);
926 v4l2_rect_map_inside(compose, &fmt);
928 dev->fmt_cap_rect = fmt;
929 tpg_s_buf_height(&dev->tpg, fmt.height);
930 } else if (dev->has_compose_cap) {
931 struct v4l2_rect fmt = dev->fmt_cap_rect;
933 v4l2_rect_set_min_size(&fmt, &s->r);
934 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
935 vb2_is_busy(&dev->vb_vid_cap_q))
937 dev->fmt_cap_rect = fmt;
938 tpg_s_buf_height(&dev->tpg, fmt.height);
939 v4l2_rect_set_size_to(compose, &s->r);
940 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
942 if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
943 vb2_is_busy(&dev->vb_vid_cap_q))
945 v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
946 v4l2_rect_set_size_to(compose, &s->r);
947 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
948 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
951 s->r.height *= factor;
954 case V4L2_SEL_TGT_COMPOSE:
955 if (!dev->has_compose_cap)
957 ret = vivid_vid_adjust_sel(s->flags, &s->r);
960 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
961 v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
962 if (dev->has_scaler_cap) {
963 struct v4l2_rect max_rect = {
965 dev->src_rect.width * MAX_ZOOM,
966 (dev->src_rect.height / factor) * MAX_ZOOM
969 v4l2_rect_set_max_size(&s->r, &max_rect);
970 if (dev->has_crop_cap) {
971 struct v4l2_rect min_rect = {
973 s->r.width / MAX_ZOOM,
974 (s->r.height * factor) / MAX_ZOOM
976 struct v4l2_rect max_rect = {
978 s->r.width * MAX_ZOOM,
979 (s->r.height * factor) * MAX_ZOOM
982 v4l2_rect_set_min_size(crop, &min_rect);
983 v4l2_rect_set_max_size(crop, &max_rect);
984 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
986 } else if (dev->has_crop_cap) {
988 s->r.height *= factor;
989 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
990 v4l2_rect_set_size_to(crop, &s->r);
991 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
993 s->r.height /= factor;
995 v4l2_rect_set_size_to(&s->r, &dev->src_rect);
996 s->r.height /= factor;
998 v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
1005 if (dev->bitmap_cap && (compose->width != orig_compose_w ||
1006 compose->height != orig_compose_h)) {
1007 vfree(dev->bitmap_cap);
1008 dev->bitmap_cap = NULL;
1010 tpg_s_crop_compose(&dev->tpg, crop, compose);
1014 int vivid_vid_cap_cropcap(struct file *file, void *priv,
1015 struct v4l2_cropcap *cap)
1017 struct vivid_dev *dev = video_drvdata(file);
1019 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1022 switch (vivid_get_pixel_aspect(dev)) {
1023 case TPG_PIXEL_ASPECT_NTSC:
1024 cap->pixelaspect.numerator = 11;
1025 cap->pixelaspect.denominator = 10;
1027 case TPG_PIXEL_ASPECT_PAL:
1028 cap->pixelaspect.numerator = 54;
1029 cap->pixelaspect.denominator = 59;
1031 case TPG_PIXEL_ASPECT_SQUARE:
1032 cap->pixelaspect.numerator = 1;
1033 cap->pixelaspect.denominator = 1;
1039 int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1040 struct v4l2_fmtdesc *f)
1042 struct vivid_dev *dev = video_drvdata(file);
1043 const struct vivid_fmt *fmt;
1045 if (dev->multiplanar)
1048 if (f->index >= ARRAY_SIZE(formats_ovl))
1051 fmt = &formats_ovl[f->index];
1053 f->pixelformat = fmt->fourcc;
1057 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1058 struct v4l2_format *f)
1060 struct vivid_dev *dev = video_drvdata(file);
1061 const struct v4l2_rect *compose = &dev->compose_cap;
1062 struct v4l2_window *win = &f->fmt.win;
1063 unsigned clipcount = win->clipcount;
1065 if (dev->multiplanar)
1068 win->w.top = dev->overlay_cap_top;
1069 win->w.left = dev->overlay_cap_left;
1070 win->w.width = compose->width;
1071 win->w.height = compose->height;
1072 win->field = dev->overlay_cap_field;
1073 win->clipcount = dev->clipcount_cap;
1074 if (clipcount > dev->clipcount_cap)
1075 clipcount = dev->clipcount_cap;
1076 if (dev->bitmap_cap == NULL)
1078 else if (win->bitmap) {
1079 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1080 ((compose->width + 7) / 8) * compose->height))
1083 if (clipcount && win->clips) {
1084 if (copy_to_user(win->clips, dev->clips_cap,
1085 clipcount * sizeof(dev->clips_cap[0])))
1091 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1092 struct v4l2_format *f)
1094 struct vivid_dev *dev = video_drvdata(file);
1095 const struct v4l2_rect *compose = &dev->compose_cap;
1096 struct v4l2_window *win = &f->fmt.win;
1099 if (dev->multiplanar)
1102 win->w.left = clamp_t(int, win->w.left,
1103 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1104 win->w.top = clamp_t(int, win->w.top,
1105 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1106 win->w.width = compose->width;
1107 win->w.height = compose->height;
1108 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1109 win->field = V4L2_FIELD_ANY;
1111 win->global_alpha = 0;
1112 if (win->clipcount && !win->clips)
1114 if (win->clipcount > MAX_CLIPS)
1115 win->clipcount = MAX_CLIPS;
1116 if (win->clipcount) {
1117 if (copy_from_user(dev->try_clips_cap, win->clips,
1118 win->clipcount * sizeof(dev->clips_cap[0])))
1120 for (i = 0; i < win->clipcount; i++) {
1121 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1123 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1124 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1125 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1126 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1129 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1130 * number and it's typically a one-time deal.
1132 for (i = 0; i < win->clipcount - 1; i++) {
1133 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1135 for (j = i + 1; j < win->clipcount; j++) {
1136 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1138 if (v4l2_rect_overlap(r1, r2))
1142 if (copy_to_user(win->clips, dev->try_clips_cap,
1143 win->clipcount * sizeof(dev->clips_cap[0])))
1149 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1150 struct v4l2_format *f)
1152 struct vivid_dev *dev = video_drvdata(file);
1153 const struct v4l2_rect *compose = &dev->compose_cap;
1154 struct v4l2_window *win = &f->fmt.win;
1155 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1156 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1157 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1158 void *new_bitmap = NULL;
1164 new_bitmap = vzalloc(bitmap_size);
1166 if (new_bitmap == NULL)
1168 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1174 dev->overlay_cap_top = win->w.top;
1175 dev->overlay_cap_left = win->w.left;
1176 dev->overlay_cap_field = win->field;
1177 vfree(dev->bitmap_cap);
1178 dev->bitmap_cap = new_bitmap;
1179 dev->clipcount_cap = win->clipcount;
1180 if (dev->clipcount_cap)
1181 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1185 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1187 struct vivid_dev *dev = video_drvdata(file);
1189 if (dev->multiplanar)
1192 if (i && dev->fb_vbase_cap == NULL)
1195 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1196 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1200 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1202 dev->overlay_cap_owner = i ? fh : NULL;
1206 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1207 struct v4l2_framebuffer *a)
1209 struct vivid_dev *dev = video_drvdata(file);
1211 if (dev->multiplanar)
1215 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1216 V4L2_FBUF_CAP_LIST_CLIPPING;
1217 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1218 a->fmt.field = V4L2_FIELD_NONE;
1219 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1224 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1225 const struct v4l2_framebuffer *a)
1227 struct vivid_dev *dev = video_drvdata(file);
1228 const struct vivid_fmt *fmt;
1230 if (dev->multiplanar)
1233 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1236 if (dev->overlay_cap_owner)
1239 if (a->base == NULL) {
1240 dev->fb_cap.base = NULL;
1241 dev->fb_vbase_cap = NULL;
1245 if (a->fmt.width < 48 || a->fmt.height < 32)
1247 fmt = vivid_get_format(dev, a->fmt.pixelformat);
1248 if (!fmt || !fmt->can_do_overlay)
1250 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1252 if (a->fmt.bytesperline > a->fmt.sizeimage / a->fmt.height)
1256 * Only support the framebuffer of one of the vivid instances.
1257 * Anything else is rejected.
1259 if (!vivid_validate_fb(a))
1262 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1264 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1265 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1266 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1267 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1271 static const struct v4l2_audio vivid_audio_inputs[] = {
1272 { 0, "TV", V4L2_AUDCAP_STEREO },
1273 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1276 int vidioc_enum_input(struct file *file, void *priv,
1277 struct v4l2_input *inp)
1279 struct vivid_dev *dev = video_drvdata(file);
1281 if (inp->index >= dev->num_inputs)
1284 inp->type = V4L2_INPUT_TYPE_CAMERA;
1285 switch (dev->input_type[inp->index]) {
1287 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1288 dev->input_name_counter[inp->index]);
1289 inp->capabilities = 0;
1292 snprintf(inp->name, sizeof(inp->name), "TV %u",
1293 dev->input_name_counter[inp->index]);
1294 inp->type = V4L2_INPUT_TYPE_TUNER;
1295 inp->std = V4L2_STD_ALL;
1296 if (dev->has_audio_inputs)
1297 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1298 inp->capabilities = V4L2_IN_CAP_STD;
1301 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1302 dev->input_name_counter[inp->index]);
1303 inp->std = V4L2_STD_ALL;
1304 if (dev->has_audio_inputs)
1305 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1306 inp->capabilities = V4L2_IN_CAP_STD;
1309 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1310 dev->input_name_counter[inp->index]);
1311 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1312 if (dev->edid_blocks == 0 ||
1313 dev->dv_timings_signal_mode == NO_SIGNAL)
1314 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1315 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1316 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1317 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1320 if (dev->sensor_hflip)
1321 inp->status |= V4L2_IN_ST_HFLIP;
1322 if (dev->sensor_vflip)
1323 inp->status |= V4L2_IN_ST_VFLIP;
1324 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1325 if (dev->std_signal_mode == NO_SIGNAL) {
1326 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1327 } else if (dev->std_signal_mode == NO_LOCK) {
1328 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1329 } else if (vivid_is_tv_cap(dev)) {
1330 switch (tpg_g_quality(&dev->tpg)) {
1332 inp->status |= V4L2_IN_ST_COLOR_KILL;
1334 case TPG_QUAL_NOISE:
1335 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1345 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1347 struct vivid_dev *dev = video_drvdata(file);
1353 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1355 struct vivid_dev *dev = video_drvdata(file);
1356 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1357 unsigned brightness;
1359 if (i >= dev->num_inputs)
1362 if (i == dev->input)
1365 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1369 dev->vid_cap_dev.tvnorms = 0;
1370 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1371 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1372 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1374 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1375 vivid_update_format_cap(dev, false);
1377 if (dev->colorspace) {
1378 switch (dev->input_type[i]) {
1380 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1384 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1387 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1388 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1389 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1391 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1393 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1400 * Modify the brightness range depending on the input.
1401 * This makes it easy to use vivid to test if applications can
1402 * handle control range modifications and is also how this is
1403 * typically used in practice as different inputs may be hooked
1404 * up to different receivers with different control ranges.
1406 brightness = 128 * i + dev->input_brightness[i];
1407 v4l2_ctrl_modify_range(dev->brightness,
1408 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1409 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1413 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1415 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1417 *vin = vivid_audio_inputs[vin->index];
1421 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1423 struct vivid_dev *dev = video_drvdata(file);
1425 if (!vivid_is_sdtv_cap(dev))
1427 *vin = vivid_audio_inputs[dev->tv_audio_input];
1431 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1433 struct vivid_dev *dev = video_drvdata(file);
1435 if (!vivid_is_sdtv_cap(dev))
1437 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1439 dev->tv_audio_input = vin->index;
1443 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1445 struct vivid_dev *dev = video_drvdata(file);
1449 vf->frequency = dev->tv_freq;
1453 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1455 struct vivid_dev *dev = video_drvdata(file);
1459 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1460 if (vivid_is_tv_cap(dev))
1461 vivid_update_quality(dev);
1465 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1467 struct vivid_dev *dev = video_drvdata(file);
1471 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1473 dev->tv_audmode = vt->audmode;
1477 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1479 struct vivid_dev *dev = video_drvdata(file);
1480 enum tpg_quality qual;
1485 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1486 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1487 vt->audmode = dev->tv_audmode;
1488 vt->rangelow = MIN_TV_FREQ;
1489 vt->rangehigh = MAX_TV_FREQ;
1490 qual = vivid_get_quality(dev, &vt->afc);
1491 if (qual == TPG_QUAL_COLOR)
1492 vt->signal = 0xffff;
1493 else if (qual == TPG_QUAL_GRAY)
1494 vt->signal = 0x8000;
1497 if (qual == TPG_QUAL_NOISE) {
1499 } else if (qual == TPG_QUAL_GRAY) {
1500 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1502 unsigned channel_nr = dev->tv_freq / (6 * 16);
1503 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1505 switch (channel_nr % options) {
1507 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1510 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1513 if (dev->std_cap & V4L2_STD_NTSC_M)
1514 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1516 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1519 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1523 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1527 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1528 const v4l2_std_id vivid_standard[] = {
1533 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1540 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1547 /* Must remain in sync with the vivid_standard array */
1548 const char * const vivid_ctrl_standard_strings[] = {
1567 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1569 struct vivid_dev *dev = video_drvdata(file);
1571 if (!vivid_is_sdtv_cap(dev))
1573 if (dev->std_signal_mode == NO_SIGNAL ||
1574 dev->std_signal_mode == NO_LOCK) {
1575 *id = V4L2_STD_UNKNOWN;
1578 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1579 *id = V4L2_STD_UNKNOWN;
1580 } else if (dev->std_signal_mode == CURRENT_STD) {
1582 } else if (dev->std_signal_mode == SELECTED_STD) {
1583 *id = dev->query_std;
1585 *id = vivid_standard[dev->query_std_last];
1586 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1592 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1594 struct vivid_dev *dev = video_drvdata(file);
1596 if (!vivid_is_sdtv_cap(dev))
1598 if (dev->std_cap == id)
1600 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1603 vivid_update_format_cap(dev, false);
1607 static void find_aspect_ratio(u32 width, u32 height,
1608 u32 *num, u32 *denom)
1610 if (!(height % 3) && ((height * 4 / 3) == width)) {
1613 } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1616 } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1619 } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1622 } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1625 } else { /* default to 16:9 */
1631 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1633 struct v4l2_bt_timings *bt = &timings->bt;
1638 if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1642 total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1643 total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1645 h_freq = (u32)bt->pixelclock / total_h_pixel;
1647 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1648 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1649 bt->polarities, bt->interlaced, timings))
1653 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1654 struct v4l2_fract aspect_ratio;
1656 find_aspect_ratio(bt->width, bt->height,
1657 &aspect_ratio.numerator,
1658 &aspect_ratio.denominator);
1659 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1660 bt->polarities, bt->interlaced,
1661 aspect_ratio, timings))
1667 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1668 struct v4l2_dv_timings *timings)
1670 struct vivid_dev *dev = video_drvdata(file);
1672 if (!vivid_is_hdmi_cap(dev))
1674 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1676 !valid_cvt_gtf_timings(timings))
1679 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
1681 if (vb2_is_busy(&dev->vb_vid_cap_q))
1684 dev->dv_timings_cap = *timings;
1685 vivid_update_format_cap(dev, false);
1689 int vidioc_query_dv_timings(struct file *file, void *_fh,
1690 struct v4l2_dv_timings *timings)
1692 struct vivid_dev *dev = video_drvdata(file);
1694 if (!vivid_is_hdmi_cap(dev))
1696 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1697 dev->edid_blocks == 0)
1699 if (dev->dv_timings_signal_mode == NO_LOCK)
1701 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1702 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1705 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1706 *timings = dev->dv_timings_cap;
1707 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1708 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1710 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1711 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1712 dev->query_dv_timings_size;
1717 int vidioc_s_edid(struct file *file, void *_fh,
1718 struct v4l2_edid *edid)
1720 struct vivid_dev *dev = video_drvdata(file);
1725 memset(edid->reserved, 0, sizeof(edid->reserved));
1726 if (edid->pad >= dev->num_inputs)
1728 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1730 if (edid->blocks == 0) {
1731 dev->edid_blocks = 0;
1732 phys_addr = CEC_PHYS_ADDR_INVALID;
1735 if (edid->blocks > dev->edid_max_blocks) {
1736 edid->blocks = dev->edid_max_blocks;
1739 phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1740 ret = v4l2_phys_addr_validate(phys_addr, &phys_addr, NULL);
1744 if (vb2_is_busy(&dev->vb_vid_cap_q))
1747 dev->edid_blocks = edid->blocks;
1748 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1751 /* TODO: a proper hotplug detect cycle should be emulated here */
1752 cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1754 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1755 cec_s_phys_addr(dev->cec_tx_adap[i],
1756 v4l2_phys_addr_for_input(phys_addr, i + 1),
1761 int vidioc_enum_framesizes(struct file *file, void *fh,
1762 struct v4l2_frmsizeenum *fsize)
1764 struct vivid_dev *dev = video_drvdata(file);
1766 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1768 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1770 if (vivid_is_webcam(dev)) {
1771 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1773 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1774 fsize->discrete = webcam_sizes[fsize->index];
1779 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1780 fsize->stepwise.min_width = MIN_WIDTH;
1781 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1782 fsize->stepwise.step_width = 2;
1783 fsize->stepwise.min_height = MIN_HEIGHT;
1784 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1785 fsize->stepwise.step_height = 2;
1789 /* timeperframe is arbitrary and continuous */
1790 int vidioc_enum_frameintervals(struct file *file, void *priv,
1791 struct v4l2_frmivalenum *fival)
1793 struct vivid_dev *dev = video_drvdata(file);
1794 const struct vivid_fmt *fmt;
1797 fmt = vivid_get_format(dev, fival->pixel_format);
1801 if (!vivid_is_webcam(dev)) {
1804 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1806 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1808 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1809 fival->discrete = dev->timeperframe_vid_cap;
1813 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1814 if (fival->width == webcam_sizes[i].width &&
1815 fival->height == webcam_sizes[i].height)
1817 if (i == ARRAY_SIZE(webcam_sizes))
1819 if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1821 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1822 fival->discrete = webcam_intervals[fival->index];
1826 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1827 struct v4l2_streamparm *parm)
1829 struct vivid_dev *dev = video_drvdata(file);
1831 if (parm->type != (dev->multiplanar ?
1832 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1833 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1836 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1837 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1838 parm->parm.capture.readbuffers = 1;
1842 #define FRACT_CMP(a, OP, b) \
1843 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1845 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1846 struct v4l2_streamparm *parm)
1848 struct vivid_dev *dev = video_drvdata(file);
1849 unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1850 struct v4l2_fract tpf;
1853 if (parm->type != (dev->multiplanar ?
1854 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1855 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1857 if (!vivid_is_webcam(dev))
1858 return vivid_vid_cap_g_parm(file, priv, parm);
1860 tpf = parm->parm.capture.timeperframe;
1862 if (tpf.denominator == 0)
1863 tpf = webcam_intervals[ival_sz - 1];
1864 for (i = 0; i < ival_sz; i++)
1865 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1869 dev->webcam_ival_idx = i;
1870 tpf = webcam_intervals[dev->webcam_ival_idx];
1871 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1872 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1874 /* resync the thread's timings */
1875 dev->cap_seq_resync = true;
1876 dev->timeperframe_vid_cap = tpf;
1877 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1878 parm->parm.capture.timeperframe = tpf;
1879 parm->parm.capture.readbuffers = 1;