2 * vivid-vid-cap.c - video capture support functions.
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/vmalloc.h>
24 #include <linux/videodev2.h>
25 #include <linux/v4l2-dv-timings.h>
26 #include <media/v4l2-common.h>
27 #include <media/v4l2-event.h>
28 #include <media/v4l2-dv-timings.h>
29 #include <media/v4l2-rect.h>
31 #include "vivid-core.h"
32 #include "vivid-vid-common.h"
33 #include "vivid-kthread-cap.h"
34 #include "vivid-vid-cap.h"
36 /* timeperframe: min/max and default */
37 static const struct v4l2_fract
38 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
39 tpf_max = {.numerator = FPS_MAX, .denominator = 1};
41 static const struct vivid_fmt formats_ovl[] = {
43 .fourcc = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
44 .vdownsampling = { 1 },
50 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
51 .vdownsampling = { 1 },
57 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
58 .vdownsampling = { 1 },
65 /* The number of discrete webcam framesizes */
66 #define VIVID_WEBCAM_SIZES 5
67 /* The number of discrete webcam frameintervals */
68 #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
70 /* Sizes must be in increasing order */
71 static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
80 * Intervals must be in increasing order and there must be twice as many
81 * elements in this array as there are in webcam_sizes.
83 static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
96 static const struct v4l2_discrete_probe webcam_probe = {
101 static int vid_cap_queue_setup(struct vb2_queue *vq,
102 unsigned *nbuffers, unsigned *nplanes,
103 unsigned sizes[], struct device *alloc_devs[])
105 struct vivid_dev *dev = vb2_get_drv_priv(vq);
106 unsigned buffers = tpg_g_buffers(&dev->tpg);
107 unsigned h = dev->fmt_cap_rect.height;
110 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
112 * You cannot use read() with FIELD_ALTERNATE since the field
113 * information (TOP/BOTTOM) cannot be passed back to the user.
115 if (vb2_fileio_is_active(vq))
119 if (dev->queue_setup_error) {
121 * Error injection: test what happens if queue_setup() returns
124 dev->queue_setup_error = false;
129 * Check if the number of requested planes match
130 * the number of buffers in the current format. You can't mix that.
132 if (*nplanes != buffers)
134 for (p = 0; p < buffers; p++) {
135 if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
136 dev->fmt_cap->data_offset[p])
140 for (p = 0; p < buffers; p++)
141 sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
142 dev->fmt_cap->data_offset[p];
145 if (vq->num_buffers + *nbuffers < 2)
146 *nbuffers = 2 - vq->num_buffers;
150 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
151 for (p = 0; p < buffers; p++)
152 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
157 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
159 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
161 unsigned buffers = tpg_g_buffers(&dev->tpg);
164 dprintk(dev, 1, "%s\n", __func__);
166 if (WARN_ON(NULL == dev->fmt_cap))
169 if (dev->buf_prepare_error) {
171 * Error injection: test what happens if buf_prepare() returns
174 dev->buf_prepare_error = false;
177 for (p = 0; p < buffers; p++) {
178 size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
179 dev->fmt_cap->data_offset[p];
181 if (vb2_plane_size(vb, p) < size) {
182 dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
183 __func__, p, vb2_plane_size(vb, p), size);
187 vb2_set_plane_payload(vb, p, size);
188 vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
194 static void vid_cap_buf_finish(struct vb2_buffer *vb)
196 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
197 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
198 struct v4l2_timecode *tc = &vbuf->timecode;
200 unsigned seq = vbuf->sequence;
202 if (!vivid_is_sdtv_cap(dev))
206 * Set the timecode. Rarely used, so it is interesting to
209 vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
210 if (dev->std_cap & V4L2_STD_525_60)
212 tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
214 tc->frames = seq % fps;
215 tc->seconds = (seq / fps) % 60;
216 tc->minutes = (seq / (60 * fps)) % 60;
217 tc->hours = (seq / (60 * 60 * fps)) % 24;
220 static void vid_cap_buf_queue(struct vb2_buffer *vb)
222 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
223 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
224 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
226 dprintk(dev, 1, "%s\n", __func__);
228 spin_lock(&dev->slock);
229 list_add_tail(&buf->list, &dev->vid_cap_active);
230 spin_unlock(&dev->slock);
233 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
235 struct vivid_dev *dev = vb2_get_drv_priv(vq);
239 if (vb2_is_streaming(&dev->vb_vid_out_q))
240 dev->can_loop_video = vivid_vid_can_loop(dev);
242 dev->vid_cap_seq_count = 0;
243 dprintk(dev, 1, "%s\n", __func__);
244 for (i = 0; i < VIDEO_MAX_FRAME; i++)
245 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
246 if (dev->start_streaming_error) {
247 dev->start_streaming_error = false;
250 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
253 struct vivid_buffer *buf, *tmp;
255 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
256 list_del(&buf->list);
257 vb2_buffer_done(&buf->vb.vb2_buf,
258 VB2_BUF_STATE_QUEUED);
264 /* abort streaming and wait for last buffer */
265 static void vid_cap_stop_streaming(struct vb2_queue *vq)
267 struct vivid_dev *dev = vb2_get_drv_priv(vq);
269 dprintk(dev, 1, "%s\n", __func__);
270 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
271 dev->can_loop_video = false;
274 const struct vb2_ops vivid_vid_cap_qops = {
275 .queue_setup = vid_cap_queue_setup,
276 .buf_prepare = vid_cap_buf_prepare,
277 .buf_finish = vid_cap_buf_finish,
278 .buf_queue = vid_cap_buf_queue,
279 .start_streaming = vid_cap_start_streaming,
280 .stop_streaming = vid_cap_stop_streaming,
281 .wait_prepare = vb2_ops_wait_prepare,
282 .wait_finish = vb2_ops_wait_finish,
286 * Determine the 'picture' quality based on the current TV frequency: either
287 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
288 * signal or NOISE for no signal.
290 void vivid_update_quality(struct vivid_dev *dev)
292 unsigned freq_modulus;
294 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
296 * The 'noise' will only be replaced by the actual video
297 * if the output video matches the input video settings.
299 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
302 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
303 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
306 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
307 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
310 if (!vivid_is_tv_cap(dev)) {
311 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
316 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
317 * From +/- 0.25 MHz around the channel there is color, and from
318 * +/- 1 MHz there is grayscale (chroma is lost).
319 * Everywhere else it is just noise.
321 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
322 if (freq_modulus > 2 * 16) {
323 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
324 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
327 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
328 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
330 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
334 * Get the current picture quality and the associated afc value.
336 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
338 unsigned freq_modulus;
342 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
343 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
344 return tpg_g_quality(&dev->tpg);
347 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
348 * From +/- 0.25 MHz around the channel there is color, and from
349 * +/- 1 MHz there is grayscale (chroma is lost).
350 * Everywhere else it is just gray.
352 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
354 *afc = freq_modulus - 1 * 16;
355 return TPG_QUAL_GRAY;
358 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
360 if (vivid_is_sdtv_cap(dev))
361 return dev->std_aspect_ratio;
363 if (vivid_is_hdmi_cap(dev))
364 return dev->dv_timings_aspect_ratio;
366 return TPG_VIDEO_ASPECT_IMAGE;
369 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
371 if (vivid_is_sdtv_cap(dev))
372 return (dev->std_cap & V4L2_STD_525_60) ?
373 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
375 if (vivid_is_hdmi_cap(dev) &&
376 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
377 return dev->src_rect.height == 480 ?
378 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
380 return TPG_PIXEL_ASPECT_SQUARE;
384 * Called whenever the format has to be reset which can occur when
385 * changing inputs, standard, timings, etc.
387 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
389 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
393 switch (dev->input_type[dev->input]) {
396 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
397 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
398 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
399 dev->field_cap = V4L2_FIELD_NONE;
400 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
404 dev->field_cap = dev->tv_field_cap;
405 dev->src_rect.width = 720;
406 if (dev->std_cap & V4L2_STD_525_60) {
407 dev->src_rect.height = 480;
408 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
409 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
411 dev->src_rect.height = 576;
412 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
413 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
415 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
418 dev->src_rect.width = bt->width;
419 dev->src_rect.height = bt->height;
420 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
421 if (dev->reduced_fps && can_reduce_fps(bt)) {
422 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
423 bt->flags |= V4L2_DV_FL_REDUCED_FPS;
425 pixelclock = bt->pixelclock;
426 bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
428 dev->timeperframe_vid_cap = (struct v4l2_fract) {
429 size / 100, (u32)pixelclock / 100
432 dev->field_cap = V4L2_FIELD_ALTERNATE;
434 dev->field_cap = V4L2_FIELD_NONE;
437 * We can be called from within s_ctrl, in that case we can't
438 * set/get controls. Luckily we don't need to in that case.
440 if (keep_controls || !dev->colorspace)
442 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
443 if (bt->width == 720 && bt->height <= 576)
444 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
446 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
447 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
449 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
450 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
452 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
455 vfree(dev->bitmap_cap);
456 dev->bitmap_cap = NULL;
457 vivid_update_quality(dev);
458 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
459 dev->crop_cap = dev->src_rect;
460 dev->crop_bounds_cap = dev->src_rect;
461 if (dev->bitmap_cap &&
462 (dev->compose_cap.width != dev->crop_cap.width ||
463 dev->compose_cap.height != dev->crop_cap.height)) {
464 vfree(dev->bitmap_cap);
465 dev->bitmap_cap = NULL;
467 dev->compose_cap = dev->crop_cap;
468 if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
469 dev->compose_cap.height /= 2;
470 dev->fmt_cap_rect = dev->compose_cap;
471 tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
472 tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
473 tpg_update_mv_step(&dev->tpg);
476 /* Map the field to something that is valid for the current input */
477 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
479 if (vivid_is_sdtv_cap(dev)) {
481 case V4L2_FIELD_INTERLACED_TB:
482 case V4L2_FIELD_INTERLACED_BT:
483 case V4L2_FIELD_SEQ_TB:
484 case V4L2_FIELD_SEQ_BT:
486 case V4L2_FIELD_BOTTOM:
487 case V4L2_FIELD_ALTERNATE:
489 case V4L2_FIELD_INTERLACED:
491 return V4L2_FIELD_INTERLACED;
494 if (vivid_is_hdmi_cap(dev))
495 return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
497 return V4L2_FIELD_NONE;
500 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
502 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
503 return tpg_g_colorspace(&dev->tpg);
504 return dev->colorspace_out;
507 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
509 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
510 return tpg_g_xfer_func(&dev->tpg);
511 return dev->xfer_func_out;
514 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
516 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
517 return tpg_g_ycbcr_enc(&dev->tpg);
518 return dev->ycbcr_enc_out;
521 static unsigned int vivid_hsv_enc_cap(struct vivid_dev *dev)
523 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
524 return tpg_g_hsv_enc(&dev->tpg);
525 return dev->hsv_enc_out;
528 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
530 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
531 return tpg_g_quantization(&dev->tpg);
532 return dev->quantization_out;
535 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
536 struct v4l2_format *f)
538 struct vivid_dev *dev = video_drvdata(file);
539 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
542 mp->width = dev->fmt_cap_rect.width;
543 mp->height = dev->fmt_cap_rect.height;
544 mp->field = dev->field_cap;
545 mp->pixelformat = dev->fmt_cap->fourcc;
546 mp->colorspace = vivid_colorspace_cap(dev);
547 mp->xfer_func = vivid_xfer_func_cap(dev);
548 if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_HSV)
549 mp->hsv_enc = vivid_hsv_enc_cap(dev);
551 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
552 mp->quantization = vivid_quantization_cap(dev);
553 mp->num_planes = dev->fmt_cap->buffers;
554 for (p = 0; p < mp->num_planes; p++) {
555 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
556 mp->plane_fmt[p].sizeimage =
557 tpg_g_line_width(&dev->tpg, p) * mp->height +
558 dev->fmt_cap->data_offset[p];
563 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
564 struct v4l2_format *f)
566 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
567 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
568 struct vivid_dev *dev = video_drvdata(file);
569 const struct vivid_fmt *fmt;
570 unsigned bytesperline, max_bpl;
575 fmt = vivid_get_format(dev, mp->pixelformat);
577 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
579 mp->pixelformat = V4L2_PIX_FMT_YUYV;
580 fmt = vivid_get_format(dev, mp->pixelformat);
583 mp->field = vivid_field_cap(dev, mp->field);
584 if (vivid_is_webcam(dev)) {
585 const struct v4l2_frmsize_discrete *sz =
586 v4l2_find_nearest_format(&webcam_probe, mp->width, mp->height);
590 } else if (vivid_is_sdtv_cap(dev)) {
592 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
594 w = dev->src_rect.width;
595 h = dev->src_rect.height;
597 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
599 if (vivid_is_webcam(dev) ||
600 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
602 mp->height = h / factor;
604 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
606 v4l2_rect_set_min_size(&r, &vivid_min_rect);
607 v4l2_rect_set_max_size(&r, &vivid_max_rect);
608 if (dev->has_scaler_cap && !dev->has_compose_cap) {
609 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
611 v4l2_rect_set_max_size(&r, &max_r);
612 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
613 v4l2_rect_set_max_size(&r, &dev->src_rect);
614 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
615 v4l2_rect_set_min_size(&r, &dev->src_rect);
618 mp->height = r.height / factor;
621 /* This driver supports custom bytesperline values */
623 mp->num_planes = fmt->buffers;
624 for (p = 0; p < fmt->buffers; p++) {
625 /* Calculate the minimum supported bytesperline value */
626 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
627 /* Calculate the maximum supported bytesperline value */
628 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
630 if (pfmt[p].bytesperline > max_bpl)
631 pfmt[p].bytesperline = max_bpl;
632 if (pfmt[p].bytesperline < bytesperline)
633 pfmt[p].bytesperline = bytesperline;
635 pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
636 fmt->vdownsampling[p] + fmt->data_offset[p];
638 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
640 for (p = fmt->buffers; p < fmt->planes; p++)
641 pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
642 (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
643 (fmt->bit_depth[0] / fmt->vdownsampling[0]);
645 mp->colorspace = vivid_colorspace_cap(dev);
646 if (fmt->color_enc == TGP_COLOR_ENC_HSV)
647 mp->hsv_enc = vivid_hsv_enc_cap(dev);
649 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
650 mp->xfer_func = vivid_xfer_func_cap(dev);
651 mp->quantization = vivid_quantization_cap(dev);
652 memset(mp->reserved, 0, sizeof(mp->reserved));
656 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
657 struct v4l2_format *f)
659 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
660 struct vivid_dev *dev = video_drvdata(file);
661 struct v4l2_rect *crop = &dev->crop_cap;
662 struct v4l2_rect *compose = &dev->compose_cap;
663 struct vb2_queue *q = &dev->vb_vid_cap_q;
664 int ret = vivid_try_fmt_vid_cap(file, priv, f);
672 if (vb2_is_busy(q)) {
673 dprintk(dev, 1, "%s device busy\n", __func__);
677 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
678 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
682 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
683 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
686 /* Note: the webcam input doesn't support scaling, cropping or composing */
688 if (!vivid_is_webcam(dev) &&
689 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
690 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
692 if (dev->has_scaler_cap) {
693 if (dev->has_compose_cap)
694 v4l2_rect_map_inside(compose, &r);
697 if (dev->has_crop_cap && !dev->has_compose_cap) {
698 struct v4l2_rect min_r = {
701 factor * r.height / MAX_ZOOM
703 struct v4l2_rect max_r = {
706 factor * r.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);
712 } else if (dev->has_crop_cap) {
713 struct v4l2_rect min_r = {
715 compose->width / MAX_ZOOM,
716 factor * compose->height / MAX_ZOOM
718 struct v4l2_rect max_r = {
720 compose->width * MAX_ZOOM,
721 factor * compose->height * MAX_ZOOM
724 v4l2_rect_set_min_size(crop, &min_r);
725 v4l2_rect_set_max_size(crop, &max_r);
726 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
728 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
730 v4l2_rect_set_size_to(crop, &r);
731 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
734 v4l2_rect_set_size_to(compose, &r);
735 } else if (!dev->has_crop_cap) {
736 v4l2_rect_map_inside(compose, &r);
739 v4l2_rect_set_max_size(crop, &r);
740 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
741 compose->top *= factor;
742 compose->height *= factor;
743 v4l2_rect_set_size_to(compose, crop);
744 v4l2_rect_map_inside(compose, &r);
745 compose->top /= factor;
746 compose->height /= factor;
748 } else if (vivid_is_webcam(dev)) {
749 /* Guaranteed to be a match */
750 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
751 if (webcam_sizes[i].width == mp->width &&
752 webcam_sizes[i].height == mp->height)
754 dev->webcam_size_idx = i;
755 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
756 dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
757 vivid_update_format_cap(dev, false);
759 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
761 v4l2_rect_set_size_to(compose, &r);
763 v4l2_rect_set_size_to(crop, &r);
766 dev->fmt_cap_rect.width = mp->width;
767 dev->fmt_cap_rect.height = mp->height;
768 tpg_s_buf_height(&dev->tpg, mp->height);
769 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
770 for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
771 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
772 dev->field_cap = mp->field;
773 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
774 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
776 tpg_s_field(&dev->tpg, dev->field_cap, false);
777 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
778 if (vivid_is_sdtv_cap(dev))
779 dev->tv_field_cap = mp->field;
780 tpg_update_mv_step(&dev->tpg);
784 int vidioc_g_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_g_fmt_vid_cap(file, priv, f);
794 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
795 struct v4l2_format *f)
797 struct vivid_dev *dev = video_drvdata(file);
799 if (!dev->multiplanar)
801 return vivid_try_fmt_vid_cap(file, priv, f);
804 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
805 struct v4l2_format *f)
807 struct vivid_dev *dev = video_drvdata(file);
809 if (!dev->multiplanar)
811 return vivid_s_fmt_vid_cap(file, priv, f);
814 int vidioc_g_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_g_fmt_vid_cap);
824 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
825 struct v4l2_format *f)
827 struct vivid_dev *dev = video_drvdata(file);
829 if (dev->multiplanar)
831 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
834 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
835 struct v4l2_format *f)
837 struct vivid_dev *dev = video_drvdata(file);
839 if (dev->multiplanar)
841 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
844 int vivid_vid_cap_g_selection(struct file *file, void *priv,
845 struct v4l2_selection *sel)
847 struct vivid_dev *dev = video_drvdata(file);
849 if (!dev->has_crop_cap && !dev->has_compose_cap)
851 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
853 if (vivid_is_webcam(dev))
856 sel->r.left = sel->r.top = 0;
857 switch (sel->target) {
858 case V4L2_SEL_TGT_CROP:
859 if (!dev->has_crop_cap)
861 sel->r = dev->crop_cap;
863 case V4L2_SEL_TGT_CROP_DEFAULT:
864 case V4L2_SEL_TGT_CROP_BOUNDS:
865 if (!dev->has_crop_cap)
867 sel->r = dev->src_rect;
869 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
870 if (!dev->has_compose_cap)
872 sel->r = vivid_max_rect;
874 case V4L2_SEL_TGT_COMPOSE:
875 if (!dev->has_compose_cap)
877 sel->r = dev->compose_cap;
879 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
880 if (!dev->has_compose_cap)
882 sel->r = dev->fmt_cap_rect;
890 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
892 struct vivid_dev *dev = video_drvdata(file);
893 struct v4l2_rect *crop = &dev->crop_cap;
894 struct v4l2_rect *compose = &dev->compose_cap;
895 unsigned orig_compose_w = compose->width;
896 unsigned orig_compose_h = compose->height;
897 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
900 if (!dev->has_crop_cap && !dev->has_compose_cap)
902 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
904 if (vivid_is_webcam(dev))
908 case V4L2_SEL_TGT_CROP:
909 if (!dev->has_crop_cap)
911 ret = vivid_vid_adjust_sel(s->flags, &s->r);
914 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
915 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
916 v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
918 s->r.height /= factor;
919 if (dev->has_scaler_cap) {
920 struct v4l2_rect fmt = dev->fmt_cap_rect;
921 struct v4l2_rect max_rect = {
923 s->r.width * MAX_ZOOM,
924 s->r.height * MAX_ZOOM
926 struct v4l2_rect min_rect = {
928 s->r.width / MAX_ZOOM,
929 s->r.height / MAX_ZOOM
932 v4l2_rect_set_min_size(&fmt, &min_rect);
933 if (!dev->has_compose_cap)
934 v4l2_rect_set_max_size(&fmt, &max_rect);
935 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
936 vb2_is_busy(&dev->vb_vid_cap_q))
938 if (dev->has_compose_cap) {
939 v4l2_rect_set_min_size(compose, &min_rect);
940 v4l2_rect_set_max_size(compose, &max_rect);
941 v4l2_rect_map_inside(compose, &fmt);
943 dev->fmt_cap_rect = fmt;
944 tpg_s_buf_height(&dev->tpg, fmt.height);
945 } else if (dev->has_compose_cap) {
946 struct v4l2_rect fmt = dev->fmt_cap_rect;
948 v4l2_rect_set_min_size(&fmt, &s->r);
949 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
950 vb2_is_busy(&dev->vb_vid_cap_q))
952 dev->fmt_cap_rect = fmt;
953 tpg_s_buf_height(&dev->tpg, fmt.height);
954 v4l2_rect_set_size_to(compose, &s->r);
955 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
957 if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
958 vb2_is_busy(&dev->vb_vid_cap_q))
960 v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
961 v4l2_rect_set_size_to(compose, &s->r);
962 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
963 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
966 s->r.height *= factor;
969 case V4L2_SEL_TGT_COMPOSE:
970 if (!dev->has_compose_cap)
972 ret = vivid_vid_adjust_sel(s->flags, &s->r);
975 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
976 v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
977 if (dev->has_scaler_cap) {
978 struct v4l2_rect max_rect = {
980 dev->src_rect.width * MAX_ZOOM,
981 (dev->src_rect.height / factor) * MAX_ZOOM
984 v4l2_rect_set_max_size(&s->r, &max_rect);
985 if (dev->has_crop_cap) {
986 struct v4l2_rect min_rect = {
988 s->r.width / MAX_ZOOM,
989 (s->r.height * factor) / MAX_ZOOM
991 struct v4l2_rect max_rect = {
993 s->r.width * MAX_ZOOM,
994 (s->r.height * factor) * MAX_ZOOM
997 v4l2_rect_set_min_size(crop, &min_rect);
998 v4l2_rect_set_max_size(crop, &max_rect);
999 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
1001 } else if (dev->has_crop_cap) {
1003 s->r.height *= factor;
1004 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
1005 v4l2_rect_set_size_to(crop, &s->r);
1006 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
1008 s->r.height /= factor;
1010 v4l2_rect_set_size_to(&s->r, &dev->src_rect);
1011 s->r.height /= factor;
1013 v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
1020 if (dev->bitmap_cap && (compose->width != orig_compose_w ||
1021 compose->height != orig_compose_h)) {
1022 vfree(dev->bitmap_cap);
1023 dev->bitmap_cap = NULL;
1025 tpg_s_crop_compose(&dev->tpg, crop, compose);
1029 int vivid_vid_cap_cropcap(struct file *file, void *priv,
1030 struct v4l2_cropcap *cap)
1032 struct vivid_dev *dev = video_drvdata(file);
1034 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1037 switch (vivid_get_pixel_aspect(dev)) {
1038 case TPG_PIXEL_ASPECT_NTSC:
1039 cap->pixelaspect.numerator = 11;
1040 cap->pixelaspect.denominator = 10;
1042 case TPG_PIXEL_ASPECT_PAL:
1043 cap->pixelaspect.numerator = 54;
1044 cap->pixelaspect.denominator = 59;
1046 case TPG_PIXEL_ASPECT_SQUARE:
1047 cap->pixelaspect.numerator = 1;
1048 cap->pixelaspect.denominator = 1;
1054 int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1055 struct v4l2_fmtdesc *f)
1057 struct vivid_dev *dev = video_drvdata(file);
1058 const struct vivid_fmt *fmt;
1060 if (dev->multiplanar)
1063 if (f->index >= ARRAY_SIZE(formats_ovl))
1066 fmt = &formats_ovl[f->index];
1068 f->pixelformat = fmt->fourcc;
1072 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1073 struct v4l2_format *f)
1075 struct vivid_dev *dev = video_drvdata(file);
1076 const struct v4l2_rect *compose = &dev->compose_cap;
1077 struct v4l2_window *win = &f->fmt.win;
1078 unsigned clipcount = win->clipcount;
1080 if (dev->multiplanar)
1083 win->w.top = dev->overlay_cap_top;
1084 win->w.left = dev->overlay_cap_left;
1085 win->w.width = compose->width;
1086 win->w.height = compose->height;
1087 win->field = dev->overlay_cap_field;
1088 win->clipcount = dev->clipcount_cap;
1089 if (clipcount > dev->clipcount_cap)
1090 clipcount = dev->clipcount_cap;
1091 if (dev->bitmap_cap == NULL)
1093 else if (win->bitmap) {
1094 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1095 ((compose->width + 7) / 8) * compose->height))
1098 if (clipcount && win->clips) {
1099 if (copy_to_user(win->clips, dev->clips_cap,
1100 clipcount * sizeof(dev->clips_cap[0])))
1106 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1107 struct v4l2_format *f)
1109 struct vivid_dev *dev = video_drvdata(file);
1110 const struct v4l2_rect *compose = &dev->compose_cap;
1111 struct v4l2_window *win = &f->fmt.win;
1114 if (dev->multiplanar)
1117 win->w.left = clamp_t(int, win->w.left,
1118 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1119 win->w.top = clamp_t(int, win->w.top,
1120 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1121 win->w.width = compose->width;
1122 win->w.height = compose->height;
1123 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1124 win->field = V4L2_FIELD_ANY;
1126 win->global_alpha = 0;
1127 if (win->clipcount && !win->clips)
1129 if (win->clipcount > MAX_CLIPS)
1130 win->clipcount = MAX_CLIPS;
1131 if (win->clipcount) {
1132 if (copy_from_user(dev->try_clips_cap, win->clips,
1133 win->clipcount * sizeof(dev->clips_cap[0])))
1135 for (i = 0; i < win->clipcount; i++) {
1136 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1138 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1139 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1140 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1141 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1144 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1145 * number and it's typically a one-time deal.
1147 for (i = 0; i < win->clipcount - 1; i++) {
1148 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1150 for (j = i + 1; j < win->clipcount; j++) {
1151 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1153 if (v4l2_rect_overlap(r1, r2))
1157 if (copy_to_user(win->clips, dev->try_clips_cap,
1158 win->clipcount * sizeof(dev->clips_cap[0])))
1164 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1165 struct v4l2_format *f)
1167 struct vivid_dev *dev = video_drvdata(file);
1168 const struct v4l2_rect *compose = &dev->compose_cap;
1169 struct v4l2_window *win = &f->fmt.win;
1170 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1171 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1172 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1173 void *new_bitmap = NULL;
1179 new_bitmap = vzalloc(bitmap_size);
1181 if (new_bitmap == NULL)
1183 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1189 dev->overlay_cap_top = win->w.top;
1190 dev->overlay_cap_left = win->w.left;
1191 dev->overlay_cap_field = win->field;
1192 vfree(dev->bitmap_cap);
1193 dev->bitmap_cap = new_bitmap;
1194 dev->clipcount_cap = win->clipcount;
1195 if (dev->clipcount_cap)
1196 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1200 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1202 struct vivid_dev *dev = video_drvdata(file);
1204 if (dev->multiplanar)
1207 if (i && dev->fb_vbase_cap == NULL)
1210 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1211 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1215 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1217 dev->overlay_cap_owner = i ? fh : NULL;
1221 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1222 struct v4l2_framebuffer *a)
1224 struct vivid_dev *dev = video_drvdata(file);
1226 if (dev->multiplanar)
1230 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1231 V4L2_FBUF_CAP_LIST_CLIPPING;
1232 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1233 a->fmt.field = V4L2_FIELD_NONE;
1234 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1239 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1240 const struct v4l2_framebuffer *a)
1242 struct vivid_dev *dev = video_drvdata(file);
1243 const struct vivid_fmt *fmt;
1245 if (dev->multiplanar)
1248 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1251 if (dev->overlay_cap_owner)
1254 if (a->base == NULL) {
1255 dev->fb_cap.base = NULL;
1256 dev->fb_vbase_cap = NULL;
1260 if (a->fmt.width < 48 || a->fmt.height < 32)
1262 fmt = vivid_get_format(dev, a->fmt.pixelformat);
1263 if (!fmt || !fmt->can_do_overlay)
1265 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1267 if (a->fmt.bytesperline > a->fmt.sizeimage / a->fmt.height)
1271 * Only support the framebuffer of one of the vivid instances.
1272 * Anything else is rejected.
1274 if (!vivid_validate_fb(a))
1277 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1279 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1280 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1281 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1282 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1286 static const struct v4l2_audio vivid_audio_inputs[] = {
1287 { 0, "TV", V4L2_AUDCAP_STEREO },
1288 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1291 int vidioc_enum_input(struct file *file, void *priv,
1292 struct v4l2_input *inp)
1294 struct vivid_dev *dev = video_drvdata(file);
1296 if (inp->index >= dev->num_inputs)
1299 inp->type = V4L2_INPUT_TYPE_CAMERA;
1300 switch (dev->input_type[inp->index]) {
1302 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1303 dev->input_name_counter[inp->index]);
1304 inp->capabilities = 0;
1307 snprintf(inp->name, sizeof(inp->name), "TV %u",
1308 dev->input_name_counter[inp->index]);
1309 inp->type = V4L2_INPUT_TYPE_TUNER;
1310 inp->std = V4L2_STD_ALL;
1311 if (dev->has_audio_inputs)
1312 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1313 inp->capabilities = V4L2_IN_CAP_STD;
1316 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1317 dev->input_name_counter[inp->index]);
1318 inp->std = V4L2_STD_ALL;
1319 if (dev->has_audio_inputs)
1320 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1321 inp->capabilities = V4L2_IN_CAP_STD;
1324 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1325 dev->input_name_counter[inp->index]);
1326 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1327 if (dev->edid_blocks == 0 ||
1328 dev->dv_timings_signal_mode == NO_SIGNAL)
1329 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1330 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1331 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1332 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1335 if (dev->sensor_hflip)
1336 inp->status |= V4L2_IN_ST_HFLIP;
1337 if (dev->sensor_vflip)
1338 inp->status |= V4L2_IN_ST_VFLIP;
1339 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1340 if (dev->std_signal_mode == NO_SIGNAL) {
1341 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1342 } else if (dev->std_signal_mode == NO_LOCK) {
1343 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1344 } else if (vivid_is_tv_cap(dev)) {
1345 switch (tpg_g_quality(&dev->tpg)) {
1347 inp->status |= V4L2_IN_ST_COLOR_KILL;
1349 case TPG_QUAL_NOISE:
1350 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1360 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1362 struct vivid_dev *dev = video_drvdata(file);
1368 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1370 struct vivid_dev *dev = video_drvdata(file);
1371 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1372 unsigned brightness;
1374 if (i >= dev->num_inputs)
1377 if (i == dev->input)
1380 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1384 dev->vid_cap_dev.tvnorms = 0;
1385 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1386 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1387 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1389 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1390 vivid_update_format_cap(dev, false);
1392 if (dev->colorspace) {
1393 switch (dev->input_type[i]) {
1395 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1399 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1402 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1403 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1404 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1406 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1408 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1415 * Modify the brightness range depending on the input.
1416 * This makes it easy to use vivid to test if applications can
1417 * handle control range modifications and is also how this is
1418 * typically used in practice as different inputs may be hooked
1419 * up to different receivers with different control ranges.
1421 brightness = 128 * i + dev->input_brightness[i];
1422 v4l2_ctrl_modify_range(dev->brightness,
1423 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1424 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1428 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1430 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1432 *vin = vivid_audio_inputs[vin->index];
1436 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1438 struct vivid_dev *dev = video_drvdata(file);
1440 if (!vivid_is_sdtv_cap(dev))
1442 *vin = vivid_audio_inputs[dev->tv_audio_input];
1446 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1448 struct vivid_dev *dev = video_drvdata(file);
1450 if (!vivid_is_sdtv_cap(dev))
1452 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1454 dev->tv_audio_input = vin->index;
1458 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1460 struct vivid_dev *dev = video_drvdata(file);
1464 vf->frequency = dev->tv_freq;
1468 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1470 struct vivid_dev *dev = video_drvdata(file);
1474 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1475 if (vivid_is_tv_cap(dev))
1476 vivid_update_quality(dev);
1480 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1482 struct vivid_dev *dev = video_drvdata(file);
1486 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1488 dev->tv_audmode = vt->audmode;
1492 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1494 struct vivid_dev *dev = video_drvdata(file);
1495 enum tpg_quality qual;
1500 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1501 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1502 vt->audmode = dev->tv_audmode;
1503 vt->rangelow = MIN_TV_FREQ;
1504 vt->rangehigh = MAX_TV_FREQ;
1505 qual = vivid_get_quality(dev, &vt->afc);
1506 if (qual == TPG_QUAL_COLOR)
1507 vt->signal = 0xffff;
1508 else if (qual == TPG_QUAL_GRAY)
1509 vt->signal = 0x8000;
1512 if (qual == TPG_QUAL_NOISE) {
1514 } else if (qual == TPG_QUAL_GRAY) {
1515 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1517 unsigned channel_nr = dev->tv_freq / (6 * 16);
1518 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1520 switch (channel_nr % options) {
1522 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1525 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1528 if (dev->std_cap & V4L2_STD_NTSC_M)
1529 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1531 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1534 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1538 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1542 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1543 const v4l2_std_id vivid_standard[] = {
1548 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1555 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1562 /* Must remain in sync with the vivid_standard array */
1563 const char * const vivid_ctrl_standard_strings[] = {
1582 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1584 struct vivid_dev *dev = video_drvdata(file);
1586 if (!vivid_is_sdtv_cap(dev))
1588 if (dev->std_signal_mode == NO_SIGNAL ||
1589 dev->std_signal_mode == NO_LOCK) {
1590 *id = V4L2_STD_UNKNOWN;
1593 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1594 *id = V4L2_STD_UNKNOWN;
1595 } else if (dev->std_signal_mode == CURRENT_STD) {
1597 } else if (dev->std_signal_mode == SELECTED_STD) {
1598 *id = dev->query_std;
1600 *id = vivid_standard[dev->query_std_last];
1601 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1607 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1609 struct vivid_dev *dev = video_drvdata(file);
1611 if (!vivid_is_sdtv_cap(dev))
1613 if (dev->std_cap == id)
1615 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1618 vivid_update_format_cap(dev, false);
1622 static void find_aspect_ratio(u32 width, u32 height,
1623 u32 *num, u32 *denom)
1625 if (!(height % 3) && ((height * 4 / 3) == width)) {
1628 } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1631 } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1634 } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1637 } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1640 } else { /* default to 16:9 */
1646 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1648 struct v4l2_bt_timings *bt = &timings->bt;
1653 if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1657 total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1658 total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1660 h_freq = (u32)bt->pixelclock / total_h_pixel;
1662 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1663 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1664 bt->polarities, bt->interlaced, timings))
1668 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1669 struct v4l2_fract aspect_ratio;
1671 find_aspect_ratio(bt->width, bt->height,
1672 &aspect_ratio.numerator,
1673 &aspect_ratio.denominator);
1674 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1675 bt->polarities, bt->interlaced,
1676 aspect_ratio, timings))
1682 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1683 struct v4l2_dv_timings *timings)
1685 struct vivid_dev *dev = video_drvdata(file);
1687 if (!vivid_is_hdmi_cap(dev))
1689 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1691 !valid_cvt_gtf_timings(timings))
1694 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
1696 if (vb2_is_busy(&dev->vb_vid_cap_q))
1699 dev->dv_timings_cap = *timings;
1700 vivid_update_format_cap(dev, false);
1704 int vidioc_query_dv_timings(struct file *file, void *_fh,
1705 struct v4l2_dv_timings *timings)
1707 struct vivid_dev *dev = video_drvdata(file);
1709 if (!vivid_is_hdmi_cap(dev))
1711 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1712 dev->edid_blocks == 0)
1714 if (dev->dv_timings_signal_mode == NO_LOCK)
1716 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1717 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1720 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1721 *timings = dev->dv_timings_cap;
1722 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1723 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1725 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1726 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1727 dev->query_dv_timings_size;
1732 int vidioc_s_edid(struct file *file, void *_fh,
1733 struct v4l2_edid *edid)
1735 struct vivid_dev *dev = video_drvdata(file);
1740 memset(edid->reserved, 0, sizeof(edid->reserved));
1741 if (edid->pad >= dev->num_inputs)
1743 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1745 if (edid->blocks == 0) {
1746 dev->edid_blocks = 0;
1747 phys_addr = CEC_PHYS_ADDR_INVALID;
1750 if (edid->blocks > dev->edid_max_blocks) {
1751 edid->blocks = dev->edid_max_blocks;
1754 phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1755 ret = cec_phys_addr_validate(phys_addr, &phys_addr, NULL);
1759 if (vb2_is_busy(&dev->vb_vid_cap_q))
1762 dev->edid_blocks = edid->blocks;
1763 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1766 /* TODO: a proper hotplug detect cycle should be emulated here */
1767 cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1769 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1770 cec_s_phys_addr(dev->cec_tx_adap[i],
1771 cec_phys_addr_for_input(phys_addr, i + 1),
1776 int vidioc_enum_framesizes(struct file *file, void *fh,
1777 struct v4l2_frmsizeenum *fsize)
1779 struct vivid_dev *dev = video_drvdata(file);
1781 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1783 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1785 if (vivid_is_webcam(dev)) {
1786 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1788 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1789 fsize->discrete = webcam_sizes[fsize->index];
1794 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1795 fsize->stepwise.min_width = MIN_WIDTH;
1796 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1797 fsize->stepwise.step_width = 2;
1798 fsize->stepwise.min_height = MIN_HEIGHT;
1799 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1800 fsize->stepwise.step_height = 2;
1804 /* timeperframe is arbitrary and continuous */
1805 int vidioc_enum_frameintervals(struct file *file, void *priv,
1806 struct v4l2_frmivalenum *fival)
1808 struct vivid_dev *dev = video_drvdata(file);
1809 const struct vivid_fmt *fmt;
1812 fmt = vivid_get_format(dev, fival->pixel_format);
1816 if (!vivid_is_webcam(dev)) {
1819 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1821 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1823 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1824 fival->discrete = dev->timeperframe_vid_cap;
1828 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1829 if (fival->width == webcam_sizes[i].width &&
1830 fival->height == webcam_sizes[i].height)
1832 if (i == ARRAY_SIZE(webcam_sizes))
1834 if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1836 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1837 fival->discrete = webcam_intervals[fival->index];
1841 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1842 struct v4l2_streamparm *parm)
1844 struct vivid_dev *dev = video_drvdata(file);
1846 if (parm->type != (dev->multiplanar ?
1847 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1848 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1851 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1852 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1853 parm->parm.capture.readbuffers = 1;
1857 #define FRACT_CMP(a, OP, b) \
1858 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1860 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1861 struct v4l2_streamparm *parm)
1863 struct vivid_dev *dev = video_drvdata(file);
1864 unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1865 struct v4l2_fract tpf;
1868 if (parm->type != (dev->multiplanar ?
1869 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1870 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1872 if (!vivid_is_webcam(dev))
1873 return vivid_vid_cap_g_parm(file, priv, parm);
1875 tpf = parm->parm.capture.timeperframe;
1877 if (tpf.denominator == 0)
1878 tpf = webcam_intervals[ival_sz - 1];
1879 for (i = 0; i < ival_sz; i++)
1880 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1884 dev->webcam_ival_idx = i;
1885 tpf = webcam_intervals[dev->webcam_ival_idx];
1886 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1887 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1889 /* resync the thread's timings */
1890 dev->cap_seq_resync = true;
1891 dev->timeperframe_vid_cap = tpf;
1892 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1893 parm->parm.capture.timeperframe = tpf;
1894 parm->parm.capture.readbuffers = 1;