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>
30 #include "vivid-core.h"
31 #include "vivid-vid-common.h"
32 #include "vivid-kthread-cap.h"
33 #include "vivid-vid-cap.h"
35 /* timeperframe: min/max and default */
36 static const struct v4l2_fract
37 tpf_min = {.numerator = 1, .denominator = FPS_MAX},
38 tpf_max = {.numerator = FPS_MAX, .denominator = 1},
39 tpf_default = {.numerator = 1, .denominator = 30};
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 4
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] = {
79 * Intervals must be in increasing order and there must be twice as many
80 * elements in this array as there are in webcam_sizes.
82 static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
93 static const struct v4l2_discrete_probe webcam_probe = {
98 static int vid_cap_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
99 unsigned *nbuffers, unsigned *nplanes,
100 unsigned sizes[], void *alloc_ctxs[])
102 struct vivid_dev *dev = vb2_get_drv_priv(vq);
103 unsigned buffers = tpg_g_buffers(&dev->tpg);
104 unsigned h = dev->fmt_cap_rect.height;
107 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
109 * You cannot use read() with FIELD_ALTERNATE since the field
110 * information (TOP/BOTTOM) cannot be passed back to the user.
112 if (vb2_fileio_is_active(vq))
116 if (dev->queue_setup_error) {
118 * Error injection: test what happens if queue_setup() returns
121 dev->queue_setup_error = false;
125 const struct v4l2_pix_format_mplane *mp;
126 struct v4l2_format mp_fmt;
127 const struct vivid_fmt *vfmt;
129 if (!V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
130 fmt_sp2mp(fmt, &mp_fmt);
133 mp = &fmt->fmt.pix_mp;
135 * Check if the number of planes in the specified format match
136 * the number of buffers in the current format. You can't mix that.
138 if (mp->num_planes != buffers)
140 vfmt = vivid_get_format(dev, mp->pixelformat);
141 for (p = 0; p < buffers; p++) {
142 sizes[p] = mp->plane_fmt[p].sizeimage;
143 if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
144 vfmt->data_offset[p])
148 for (p = 0; p < buffers; p++)
149 sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
150 dev->fmt_cap->data_offset[p];
153 if (vq->num_buffers + *nbuffers < 2)
154 *nbuffers = 2 - vq->num_buffers;
159 * videobuf2-vmalloc allocator is context-less so no need to set
163 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
164 for (p = 0; p < buffers; p++)
165 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
170 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
172 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
174 unsigned buffers = tpg_g_buffers(&dev->tpg);
177 dprintk(dev, 1, "%s\n", __func__);
179 if (WARN_ON(NULL == dev->fmt_cap))
182 if (dev->buf_prepare_error) {
184 * Error injection: test what happens if buf_prepare() returns
187 dev->buf_prepare_error = false;
190 for (p = 0; p < buffers; p++) {
191 size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
192 dev->fmt_cap->data_offset[p];
194 if (vb2_plane_size(vb, p) < size) {
195 dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
196 __func__, p, vb2_plane_size(vb, p), size);
200 vb2_set_plane_payload(vb, p, size);
201 vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
207 static void vid_cap_buf_finish(struct vb2_buffer *vb)
209 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
210 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
211 struct v4l2_timecode *tc = &vbuf->timecode;
213 unsigned seq = vbuf->sequence;
215 if (!vivid_is_sdtv_cap(dev))
219 * Set the timecode. Rarely used, so it is interesting to
222 vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
223 if (dev->std_cap & V4L2_STD_525_60)
225 tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
227 tc->frames = seq % fps;
228 tc->seconds = (seq / fps) % 60;
229 tc->minutes = (seq / (60 * fps)) % 60;
230 tc->hours = (seq / (60 * 60 * fps)) % 24;
233 static void vid_cap_buf_queue(struct vb2_buffer *vb)
235 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
236 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
237 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
239 dprintk(dev, 1, "%s\n", __func__);
241 spin_lock(&dev->slock);
242 list_add_tail(&buf->list, &dev->vid_cap_active);
243 spin_unlock(&dev->slock);
246 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
248 struct vivid_dev *dev = vb2_get_drv_priv(vq);
252 if (vb2_is_streaming(&dev->vb_vid_out_q))
253 dev->can_loop_video = vivid_vid_can_loop(dev);
255 if (dev->kthread_vid_cap)
258 dev->vid_cap_seq_count = 0;
259 dprintk(dev, 1, "%s\n", __func__);
260 for (i = 0; i < VIDEO_MAX_FRAME; i++)
261 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
262 if (dev->start_streaming_error) {
263 dev->start_streaming_error = false;
266 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
269 struct vivid_buffer *buf, *tmp;
271 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
272 list_del(&buf->list);
273 vb2_buffer_done(&buf->vb.vb2_buf,
274 VB2_BUF_STATE_QUEUED);
280 /* abort streaming and wait for last buffer */
281 static void vid_cap_stop_streaming(struct vb2_queue *vq)
283 struct vivid_dev *dev = vb2_get_drv_priv(vq);
285 dprintk(dev, 1, "%s\n", __func__);
286 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
287 dev->can_loop_video = false;
290 const struct vb2_ops vivid_vid_cap_qops = {
291 .queue_setup = vid_cap_queue_setup,
292 .buf_prepare = vid_cap_buf_prepare,
293 .buf_finish = vid_cap_buf_finish,
294 .buf_queue = vid_cap_buf_queue,
295 .start_streaming = vid_cap_start_streaming,
296 .stop_streaming = vid_cap_stop_streaming,
297 .wait_prepare = vb2_ops_wait_prepare,
298 .wait_finish = vb2_ops_wait_finish,
302 * Determine the 'picture' quality based on the current TV frequency: either
303 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
304 * signal or NOISE for no signal.
306 void vivid_update_quality(struct vivid_dev *dev)
308 unsigned freq_modulus;
310 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
312 * The 'noise' will only be replaced by the actual video
313 * if the output video matches the input video settings.
315 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
318 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
319 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
322 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
323 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
326 if (!vivid_is_tv_cap(dev)) {
327 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
332 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
333 * From +/- 0.25 MHz around the channel there is color, and from
334 * +/- 1 MHz there is grayscale (chroma is lost).
335 * Everywhere else it is just noise.
337 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
338 if (freq_modulus > 2 * 16) {
339 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
340 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
343 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
344 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
346 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
350 * Get the current picture quality and the associated afc value.
352 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
354 unsigned freq_modulus;
358 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
359 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
360 return tpg_g_quality(&dev->tpg);
363 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
364 * From +/- 0.25 MHz around the channel there is color, and from
365 * +/- 1 MHz there is grayscale (chroma is lost).
366 * Everywhere else it is just gray.
368 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
370 *afc = freq_modulus - 1 * 16;
371 return TPG_QUAL_GRAY;
374 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
376 if (vivid_is_sdtv_cap(dev))
377 return dev->std_aspect_ratio;
379 if (vivid_is_hdmi_cap(dev))
380 return dev->dv_timings_aspect_ratio;
382 return TPG_VIDEO_ASPECT_IMAGE;
385 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
387 if (vivid_is_sdtv_cap(dev))
388 return (dev->std_cap & V4L2_STD_525_60) ?
389 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
391 if (vivid_is_hdmi_cap(dev) &&
392 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
393 return dev->src_rect.height == 480 ?
394 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
396 return TPG_PIXEL_ASPECT_SQUARE;
400 * Called whenever the format has to be reset which can occur when
401 * changing inputs, standard, timings, etc.
403 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
405 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
408 switch (dev->input_type[dev->input]) {
411 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
412 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
413 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
414 dev->field_cap = V4L2_FIELD_NONE;
415 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
419 dev->field_cap = dev->tv_field_cap;
420 dev->src_rect.width = 720;
421 if (dev->std_cap & V4L2_STD_525_60) {
422 dev->src_rect.height = 480;
423 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
424 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
426 dev->src_rect.height = 576;
427 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
428 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
430 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
433 dev->src_rect.width = bt->width;
434 dev->src_rect.height = bt->height;
435 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
436 dev->timeperframe_vid_cap = (struct v4l2_fract) {
437 size / 100, (u32)bt->pixelclock / 100
440 dev->field_cap = V4L2_FIELD_ALTERNATE;
442 dev->field_cap = V4L2_FIELD_NONE;
445 * We can be called from within s_ctrl, in that case we can't
446 * set/get controls. Luckily we don't need to in that case.
448 if (keep_controls || !dev->colorspace)
450 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
451 if (bt->width == 720 && bt->height <= 576)
452 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
454 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
455 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
457 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
458 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
460 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
463 vivid_update_quality(dev);
464 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
465 dev->crop_cap = dev->src_rect;
466 dev->crop_bounds_cap = dev->src_rect;
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 vivid_quantization_cap(struct vivid_dev *dev)
523 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
524 return tpg_g_quantization(&dev->tpg);
525 return dev->quantization_out;
528 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
529 struct v4l2_format *f)
531 struct vivid_dev *dev = video_drvdata(file);
532 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
535 mp->width = dev->fmt_cap_rect.width;
536 mp->height = dev->fmt_cap_rect.height;
537 mp->field = dev->field_cap;
538 mp->pixelformat = dev->fmt_cap->fourcc;
539 mp->colorspace = vivid_colorspace_cap(dev);
540 mp->xfer_func = vivid_xfer_func_cap(dev);
541 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
542 mp->quantization = vivid_quantization_cap(dev);
543 mp->num_planes = dev->fmt_cap->buffers;
544 for (p = 0; p < mp->num_planes; p++) {
545 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
546 mp->plane_fmt[p].sizeimage =
547 tpg_g_line_width(&dev->tpg, p) * mp->height +
548 dev->fmt_cap->data_offset[p];
553 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
554 struct v4l2_format *f)
556 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
557 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
558 struct vivid_dev *dev = video_drvdata(file);
559 const struct vivid_fmt *fmt;
560 unsigned bytesperline, max_bpl;
565 fmt = vivid_get_format(dev, mp->pixelformat);
567 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
569 mp->pixelformat = V4L2_PIX_FMT_YUYV;
570 fmt = vivid_get_format(dev, mp->pixelformat);
573 mp->field = vivid_field_cap(dev, mp->field);
574 if (vivid_is_webcam(dev)) {
575 const struct v4l2_frmsize_discrete *sz =
576 v4l2_find_nearest_format(&webcam_probe, mp->width, mp->height);
580 } else if (vivid_is_sdtv_cap(dev)) {
582 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
584 w = dev->src_rect.width;
585 h = dev->src_rect.height;
587 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
589 if (vivid_is_webcam(dev) ||
590 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
592 mp->height = h / factor;
594 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
596 rect_set_min_size(&r, &vivid_min_rect);
597 rect_set_max_size(&r, &vivid_max_rect);
598 if (dev->has_scaler_cap && !dev->has_compose_cap) {
599 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
601 rect_set_max_size(&r, &max_r);
602 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
603 rect_set_max_size(&r, &dev->src_rect);
604 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
605 rect_set_min_size(&r, &dev->src_rect);
608 mp->height = r.height / factor;
611 /* This driver supports custom bytesperline values */
613 mp->num_planes = fmt->buffers;
614 for (p = 0; p < mp->num_planes; p++) {
615 /* Calculate the minimum supported bytesperline value */
616 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
617 /* Calculate the maximum supported bytesperline value */
618 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
620 if (pfmt[p].bytesperline > max_bpl)
621 pfmt[p].bytesperline = max_bpl;
622 if (pfmt[p].bytesperline < bytesperline)
623 pfmt[p].bytesperline = bytesperline;
624 pfmt[p].sizeimage = tpg_calc_line_width(&dev->tpg, p, pfmt[p].bytesperline) *
625 mp->height + fmt->data_offset[p];
626 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
628 mp->colorspace = vivid_colorspace_cap(dev);
629 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
630 mp->xfer_func = vivid_xfer_func_cap(dev);
631 mp->quantization = vivid_quantization_cap(dev);
632 memset(mp->reserved, 0, sizeof(mp->reserved));
636 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
637 struct v4l2_format *f)
639 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
640 struct vivid_dev *dev = video_drvdata(file);
641 struct v4l2_rect *crop = &dev->crop_cap;
642 struct v4l2_rect *compose = &dev->compose_cap;
643 struct vb2_queue *q = &dev->vb_vid_cap_q;
644 int ret = vivid_try_fmt_vid_cap(file, priv, f);
652 if (vb2_is_busy(q)) {
653 dprintk(dev, 1, "%s device busy\n", __func__);
657 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
658 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
662 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
663 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
666 /* Note: the webcam input doesn't support scaling, cropping or composing */
668 if (!vivid_is_webcam(dev) &&
669 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
670 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
672 if (dev->has_scaler_cap) {
673 if (dev->has_compose_cap)
674 rect_map_inside(compose, &r);
677 if (dev->has_crop_cap && !dev->has_compose_cap) {
678 struct v4l2_rect min_r = {
681 factor * r.height / MAX_ZOOM
683 struct v4l2_rect max_r = {
686 factor * r.height * MAX_ZOOM
689 rect_set_min_size(crop, &min_r);
690 rect_set_max_size(crop, &max_r);
691 rect_map_inside(crop, &dev->crop_bounds_cap);
692 } else if (dev->has_crop_cap) {
693 struct v4l2_rect min_r = {
695 compose->width / MAX_ZOOM,
696 factor * compose->height / MAX_ZOOM
698 struct v4l2_rect max_r = {
700 compose->width * MAX_ZOOM,
701 factor * compose->height * MAX_ZOOM
704 rect_set_min_size(crop, &min_r);
705 rect_set_max_size(crop, &max_r);
706 rect_map_inside(crop, &dev->crop_bounds_cap);
708 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
710 rect_set_size_to(crop, &r);
711 rect_map_inside(crop, &dev->crop_bounds_cap);
714 rect_set_size_to(compose, &r);
715 } else if (!dev->has_crop_cap) {
716 rect_map_inside(compose, &r);
719 rect_set_max_size(crop, &r);
720 rect_map_inside(crop, &dev->crop_bounds_cap);
721 compose->top *= factor;
722 compose->height *= factor;
723 rect_set_size_to(compose, crop);
724 rect_map_inside(compose, &r);
725 compose->top /= factor;
726 compose->height /= factor;
728 } else if (vivid_is_webcam(dev)) {
729 /* Guaranteed to be a match */
730 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
731 if (webcam_sizes[i].width == mp->width &&
732 webcam_sizes[i].height == mp->height)
734 dev->webcam_size_idx = i;
735 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
736 dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
737 vivid_update_format_cap(dev, false);
739 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
741 rect_set_size_to(compose, &r);
743 rect_set_size_to(crop, &r);
746 dev->fmt_cap_rect.width = mp->width;
747 dev->fmt_cap_rect.height = mp->height;
748 tpg_s_buf_height(&dev->tpg, mp->height);
749 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
750 for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
751 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
752 dev->field_cap = mp->field;
753 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
754 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
756 tpg_s_field(&dev->tpg, dev->field_cap, false);
757 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
758 if (vivid_is_sdtv_cap(dev))
759 dev->tv_field_cap = mp->field;
760 tpg_update_mv_step(&dev->tpg);
764 int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
765 struct v4l2_format *f)
767 struct vivid_dev *dev = video_drvdata(file);
769 if (!dev->multiplanar)
771 return vivid_g_fmt_vid_cap(file, priv, f);
774 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
775 struct v4l2_format *f)
777 struct vivid_dev *dev = video_drvdata(file);
779 if (!dev->multiplanar)
781 return vivid_try_fmt_vid_cap(file, priv, f);
784 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
785 struct v4l2_format *f)
787 struct vivid_dev *dev = video_drvdata(file);
789 if (!dev->multiplanar)
791 return vivid_s_fmt_vid_cap(file, priv, f);
794 int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
795 struct v4l2_format *f)
797 struct vivid_dev *dev = video_drvdata(file);
799 if (dev->multiplanar)
801 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
804 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
805 struct v4l2_format *f)
807 struct vivid_dev *dev = video_drvdata(file);
809 if (dev->multiplanar)
811 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
814 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
815 struct v4l2_format *f)
817 struct vivid_dev *dev = video_drvdata(file);
819 if (dev->multiplanar)
821 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
824 int vivid_vid_cap_g_selection(struct file *file, void *priv,
825 struct v4l2_selection *sel)
827 struct vivid_dev *dev = video_drvdata(file);
829 if (!dev->has_crop_cap && !dev->has_compose_cap)
831 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
833 if (vivid_is_webcam(dev))
836 sel->r.left = sel->r.top = 0;
837 switch (sel->target) {
838 case V4L2_SEL_TGT_CROP:
839 if (!dev->has_crop_cap)
841 sel->r = dev->crop_cap;
843 case V4L2_SEL_TGT_CROP_DEFAULT:
844 case V4L2_SEL_TGT_CROP_BOUNDS:
845 if (!dev->has_crop_cap)
847 sel->r = dev->src_rect;
849 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
850 if (!dev->has_compose_cap)
852 sel->r = vivid_max_rect;
854 case V4L2_SEL_TGT_COMPOSE:
855 if (!dev->has_compose_cap)
857 sel->r = dev->compose_cap;
859 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
860 if (!dev->has_compose_cap)
862 sel->r = dev->fmt_cap_rect;
870 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
872 struct vivid_dev *dev = video_drvdata(file);
873 struct v4l2_rect *crop = &dev->crop_cap;
874 struct v4l2_rect *compose = &dev->compose_cap;
875 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
878 if (!dev->has_crop_cap && !dev->has_compose_cap)
880 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
882 if (vivid_is_webcam(dev))
886 case V4L2_SEL_TGT_CROP:
887 if (!dev->has_crop_cap)
889 ret = vivid_vid_adjust_sel(s->flags, &s->r);
892 rect_set_min_size(&s->r, &vivid_min_rect);
893 rect_set_max_size(&s->r, &dev->src_rect);
894 rect_map_inside(&s->r, &dev->crop_bounds_cap);
896 s->r.height /= factor;
897 if (dev->has_scaler_cap) {
898 struct v4l2_rect fmt = dev->fmt_cap_rect;
899 struct v4l2_rect max_rect = {
901 s->r.width * MAX_ZOOM,
902 s->r.height * MAX_ZOOM
904 struct v4l2_rect min_rect = {
906 s->r.width / MAX_ZOOM,
907 s->r.height / MAX_ZOOM
910 rect_set_min_size(&fmt, &min_rect);
911 if (!dev->has_compose_cap)
912 rect_set_max_size(&fmt, &max_rect);
913 if (!rect_same_size(&dev->fmt_cap_rect, &fmt) &&
914 vb2_is_busy(&dev->vb_vid_cap_q))
916 if (dev->has_compose_cap) {
917 rect_set_min_size(compose, &min_rect);
918 rect_set_max_size(compose, &max_rect);
920 dev->fmt_cap_rect = fmt;
921 tpg_s_buf_height(&dev->tpg, fmt.height);
922 } else if (dev->has_compose_cap) {
923 struct v4l2_rect fmt = dev->fmt_cap_rect;
925 rect_set_min_size(&fmt, &s->r);
926 if (!rect_same_size(&dev->fmt_cap_rect, &fmt) &&
927 vb2_is_busy(&dev->vb_vid_cap_q))
929 dev->fmt_cap_rect = fmt;
930 tpg_s_buf_height(&dev->tpg, fmt.height);
931 rect_set_size_to(compose, &s->r);
932 rect_map_inside(compose, &dev->fmt_cap_rect);
934 if (!rect_same_size(&s->r, &dev->fmt_cap_rect) &&
935 vb2_is_busy(&dev->vb_vid_cap_q))
937 rect_set_size_to(&dev->fmt_cap_rect, &s->r);
938 rect_set_size_to(compose, &s->r);
939 rect_map_inside(compose, &dev->fmt_cap_rect);
940 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
943 s->r.height *= factor;
946 case V4L2_SEL_TGT_COMPOSE:
947 if (!dev->has_compose_cap)
949 ret = vivid_vid_adjust_sel(s->flags, &s->r);
952 rect_set_min_size(&s->r, &vivid_min_rect);
953 rect_set_max_size(&s->r, &dev->fmt_cap_rect);
954 if (dev->has_scaler_cap) {
955 struct v4l2_rect max_rect = {
957 dev->src_rect.width * MAX_ZOOM,
958 (dev->src_rect.height / factor) * MAX_ZOOM
961 rect_set_max_size(&s->r, &max_rect);
962 if (dev->has_crop_cap) {
963 struct v4l2_rect min_rect = {
965 s->r.width / MAX_ZOOM,
966 (s->r.height * factor) / MAX_ZOOM
968 struct v4l2_rect max_rect = {
970 s->r.width * MAX_ZOOM,
971 (s->r.height * factor) * MAX_ZOOM
974 rect_set_min_size(crop, &min_rect);
975 rect_set_max_size(crop, &max_rect);
976 rect_map_inside(crop, &dev->crop_bounds_cap);
978 } else if (dev->has_crop_cap) {
980 s->r.height *= factor;
981 rect_set_max_size(&s->r, &dev->src_rect);
982 rect_set_size_to(crop, &s->r);
983 rect_map_inside(crop, &dev->crop_bounds_cap);
985 s->r.height /= factor;
987 rect_set_size_to(&s->r, &dev->src_rect);
988 s->r.height /= factor;
990 rect_map_inside(&s->r, &dev->fmt_cap_rect);
991 if (dev->bitmap_cap && (compose->width != s->r.width ||
992 compose->height != s->r.height)) {
993 kfree(dev->bitmap_cap);
994 dev->bitmap_cap = NULL;
1002 tpg_s_crop_compose(&dev->tpg, crop, compose);
1006 int vivid_vid_cap_cropcap(struct file *file, void *priv,
1007 struct v4l2_cropcap *cap)
1009 struct vivid_dev *dev = video_drvdata(file);
1011 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1014 switch (vivid_get_pixel_aspect(dev)) {
1015 case TPG_PIXEL_ASPECT_NTSC:
1016 cap->pixelaspect.numerator = 11;
1017 cap->pixelaspect.denominator = 10;
1019 case TPG_PIXEL_ASPECT_PAL:
1020 cap->pixelaspect.numerator = 54;
1021 cap->pixelaspect.denominator = 59;
1023 case TPG_PIXEL_ASPECT_SQUARE:
1024 cap->pixelaspect.numerator = 1;
1025 cap->pixelaspect.denominator = 1;
1031 int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1032 struct v4l2_fmtdesc *f)
1034 struct vivid_dev *dev = video_drvdata(file);
1035 const struct vivid_fmt *fmt;
1037 if (dev->multiplanar)
1040 if (f->index >= ARRAY_SIZE(formats_ovl))
1043 fmt = &formats_ovl[f->index];
1045 f->pixelformat = fmt->fourcc;
1049 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1050 struct v4l2_format *f)
1052 struct vivid_dev *dev = video_drvdata(file);
1053 const struct v4l2_rect *compose = &dev->compose_cap;
1054 struct v4l2_window *win = &f->fmt.win;
1055 unsigned clipcount = win->clipcount;
1057 if (dev->multiplanar)
1060 win->w.top = dev->overlay_cap_top;
1061 win->w.left = dev->overlay_cap_left;
1062 win->w.width = compose->width;
1063 win->w.height = compose->height;
1064 win->field = dev->overlay_cap_field;
1065 win->clipcount = dev->clipcount_cap;
1066 if (clipcount > dev->clipcount_cap)
1067 clipcount = dev->clipcount_cap;
1068 if (dev->bitmap_cap == NULL)
1070 else if (win->bitmap) {
1071 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1072 ((compose->width + 7) / 8) * compose->height))
1075 if (clipcount && win->clips) {
1076 if (copy_to_user(win->clips, dev->clips_cap,
1077 clipcount * sizeof(dev->clips_cap[0])))
1083 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1084 struct v4l2_format *f)
1086 struct vivid_dev *dev = video_drvdata(file);
1087 const struct v4l2_rect *compose = &dev->compose_cap;
1088 struct v4l2_window *win = &f->fmt.win;
1091 if (dev->multiplanar)
1094 win->w.left = clamp_t(int, win->w.left,
1095 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1096 win->w.top = clamp_t(int, win->w.top,
1097 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1098 win->w.width = compose->width;
1099 win->w.height = compose->height;
1100 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1101 win->field = V4L2_FIELD_ANY;
1103 win->global_alpha = 0;
1104 if (win->clipcount && !win->clips)
1106 if (win->clipcount > MAX_CLIPS)
1107 win->clipcount = MAX_CLIPS;
1108 if (win->clipcount) {
1109 if (copy_from_user(dev->try_clips_cap, win->clips,
1110 win->clipcount * sizeof(dev->clips_cap[0])))
1112 for (i = 0; i < win->clipcount; i++) {
1113 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1115 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1116 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1117 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1118 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1121 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1122 * number and it's typically a one-time deal.
1124 for (i = 0; i < win->clipcount - 1; i++) {
1125 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1127 for (j = i + 1; j < win->clipcount; j++) {
1128 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1130 if (rect_overlap(r1, r2))
1134 if (copy_to_user(win->clips, dev->try_clips_cap,
1135 win->clipcount * sizeof(dev->clips_cap[0])))
1141 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1142 struct v4l2_format *f)
1144 struct vivid_dev *dev = video_drvdata(file);
1145 const struct v4l2_rect *compose = &dev->compose_cap;
1146 struct v4l2_window *win = &f->fmt.win;
1147 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1148 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1149 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1150 void *new_bitmap = NULL;
1156 new_bitmap = vzalloc(bitmap_size);
1158 if (new_bitmap == NULL)
1160 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1166 dev->overlay_cap_top = win->w.top;
1167 dev->overlay_cap_left = win->w.left;
1168 dev->overlay_cap_field = win->field;
1169 vfree(dev->bitmap_cap);
1170 dev->bitmap_cap = new_bitmap;
1171 dev->clipcount_cap = win->clipcount;
1172 if (dev->clipcount_cap)
1173 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1177 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1179 struct vivid_dev *dev = video_drvdata(file);
1181 if (dev->multiplanar)
1184 if (i && dev->fb_vbase_cap == NULL)
1187 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1188 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1192 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1194 dev->overlay_cap_owner = i ? fh : NULL;
1198 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1199 struct v4l2_framebuffer *a)
1201 struct vivid_dev *dev = video_drvdata(file);
1203 if (dev->multiplanar)
1207 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1208 V4L2_FBUF_CAP_LIST_CLIPPING;
1209 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1210 a->fmt.field = V4L2_FIELD_NONE;
1211 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1216 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1217 const struct v4l2_framebuffer *a)
1219 struct vivid_dev *dev = video_drvdata(file);
1220 const struct vivid_fmt *fmt;
1222 if (dev->multiplanar)
1225 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1228 if (dev->overlay_cap_owner)
1231 if (a->base == NULL) {
1232 dev->fb_cap.base = NULL;
1233 dev->fb_vbase_cap = NULL;
1237 if (a->fmt.width < 48 || a->fmt.height < 32)
1239 fmt = vivid_get_format(dev, a->fmt.pixelformat);
1240 if (!fmt || !fmt->can_do_overlay)
1242 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1244 if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
1247 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1249 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1250 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1251 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1252 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1256 static const struct v4l2_audio vivid_audio_inputs[] = {
1257 { 0, "TV", V4L2_AUDCAP_STEREO },
1258 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1261 int vidioc_enum_input(struct file *file, void *priv,
1262 struct v4l2_input *inp)
1264 struct vivid_dev *dev = video_drvdata(file);
1266 if (inp->index >= dev->num_inputs)
1269 inp->type = V4L2_INPUT_TYPE_CAMERA;
1270 switch (dev->input_type[inp->index]) {
1272 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1273 dev->input_name_counter[inp->index]);
1274 inp->capabilities = 0;
1277 snprintf(inp->name, sizeof(inp->name), "TV %u",
1278 dev->input_name_counter[inp->index]);
1279 inp->type = V4L2_INPUT_TYPE_TUNER;
1280 inp->std = V4L2_STD_ALL;
1281 if (dev->has_audio_inputs)
1282 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1283 inp->capabilities = V4L2_IN_CAP_STD;
1286 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1287 dev->input_name_counter[inp->index]);
1288 inp->std = V4L2_STD_ALL;
1289 if (dev->has_audio_inputs)
1290 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1291 inp->capabilities = V4L2_IN_CAP_STD;
1294 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1295 dev->input_name_counter[inp->index]);
1296 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1297 if (dev->edid_blocks == 0 ||
1298 dev->dv_timings_signal_mode == NO_SIGNAL)
1299 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1300 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1301 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1302 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1305 if (dev->sensor_hflip)
1306 inp->status |= V4L2_IN_ST_HFLIP;
1307 if (dev->sensor_vflip)
1308 inp->status |= V4L2_IN_ST_VFLIP;
1309 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1310 if (dev->std_signal_mode == NO_SIGNAL) {
1311 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1312 } else if (dev->std_signal_mode == NO_LOCK) {
1313 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1314 } else if (vivid_is_tv_cap(dev)) {
1315 switch (tpg_g_quality(&dev->tpg)) {
1317 inp->status |= V4L2_IN_ST_COLOR_KILL;
1319 case TPG_QUAL_NOISE:
1320 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1330 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1332 struct vivid_dev *dev = video_drvdata(file);
1338 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1340 struct vivid_dev *dev = video_drvdata(file);
1341 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1342 unsigned brightness;
1344 if (i >= dev->num_inputs)
1347 if (i == dev->input)
1350 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1354 dev->vid_cap_dev.tvnorms = 0;
1355 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1356 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1357 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1359 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1360 vivid_update_format_cap(dev, false);
1362 if (dev->colorspace) {
1363 switch (dev->input_type[i]) {
1365 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1369 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1372 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1373 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1374 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1376 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1378 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1385 * Modify the brightness range depending on the input.
1386 * This makes it easy to use vivid to test if applications can
1387 * handle control range modifications and is also how this is
1388 * typically used in practice as different inputs may be hooked
1389 * up to different receivers with different control ranges.
1391 brightness = 128 * i + dev->input_brightness[i];
1392 v4l2_ctrl_modify_range(dev->brightness,
1393 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1394 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1398 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1400 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1402 *vin = vivid_audio_inputs[vin->index];
1406 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1408 struct vivid_dev *dev = video_drvdata(file);
1410 if (!vivid_is_sdtv_cap(dev))
1412 *vin = vivid_audio_inputs[dev->tv_audio_input];
1416 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1418 struct vivid_dev *dev = video_drvdata(file);
1420 if (!vivid_is_sdtv_cap(dev))
1422 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1424 dev->tv_audio_input = vin->index;
1428 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1430 struct vivid_dev *dev = video_drvdata(file);
1434 vf->frequency = dev->tv_freq;
1438 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1440 struct vivid_dev *dev = video_drvdata(file);
1444 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1445 if (vivid_is_tv_cap(dev))
1446 vivid_update_quality(dev);
1450 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1452 struct vivid_dev *dev = video_drvdata(file);
1456 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1458 dev->tv_audmode = vt->audmode;
1462 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1464 struct vivid_dev *dev = video_drvdata(file);
1465 enum tpg_quality qual;
1470 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1471 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1472 vt->audmode = dev->tv_audmode;
1473 vt->rangelow = MIN_TV_FREQ;
1474 vt->rangehigh = MAX_TV_FREQ;
1475 qual = vivid_get_quality(dev, &vt->afc);
1476 if (qual == TPG_QUAL_COLOR)
1477 vt->signal = 0xffff;
1478 else if (qual == TPG_QUAL_GRAY)
1479 vt->signal = 0x8000;
1482 if (qual == TPG_QUAL_NOISE) {
1484 } else if (qual == TPG_QUAL_GRAY) {
1485 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1487 unsigned channel_nr = dev->tv_freq / (6 * 16);
1488 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1490 switch (channel_nr % options) {
1492 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1495 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1498 if (dev->std_cap & V4L2_STD_NTSC_M)
1499 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1501 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1504 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1508 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1512 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1513 const v4l2_std_id vivid_standard[] = {
1518 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1525 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1532 /* Must remain in sync with the vivid_standard array */
1533 const char * const vivid_ctrl_standard_strings[] = {
1552 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1554 struct vivid_dev *dev = video_drvdata(file);
1556 if (!vivid_is_sdtv_cap(dev))
1558 if (dev->std_signal_mode == NO_SIGNAL ||
1559 dev->std_signal_mode == NO_LOCK) {
1560 *id = V4L2_STD_UNKNOWN;
1563 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1564 *id = V4L2_STD_UNKNOWN;
1565 } else if (dev->std_signal_mode == CURRENT_STD) {
1567 } else if (dev->std_signal_mode == SELECTED_STD) {
1568 *id = dev->query_std;
1570 *id = vivid_standard[dev->query_std_last];
1571 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1577 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1579 struct vivid_dev *dev = video_drvdata(file);
1581 if (!vivid_is_sdtv_cap(dev))
1583 if (dev->std_cap == id)
1585 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1588 vivid_update_format_cap(dev, false);
1592 static void find_aspect_ratio(u32 width, u32 height,
1593 u32 *num, u32 *denom)
1595 if (!(height % 3) && ((height * 4 / 3) == width)) {
1598 } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1601 } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1604 } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1607 } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1610 } else { /* default to 16:9 */
1616 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1618 struct v4l2_bt_timings *bt = &timings->bt;
1623 if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1627 total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1628 total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1630 h_freq = (u32)bt->pixelclock / total_h_pixel;
1632 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1633 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1634 bt->polarities, bt->interlaced, timings))
1638 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1639 struct v4l2_fract aspect_ratio;
1641 find_aspect_ratio(bt->width, bt->height,
1642 &aspect_ratio.numerator,
1643 &aspect_ratio.denominator);
1644 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1645 bt->polarities, bt->interlaced,
1646 aspect_ratio, timings))
1652 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1653 struct v4l2_dv_timings *timings)
1655 struct vivid_dev *dev = video_drvdata(file);
1657 if (!vivid_is_hdmi_cap(dev))
1659 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1661 !valid_cvt_gtf_timings(timings))
1664 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0))
1666 if (vb2_is_busy(&dev->vb_vid_cap_q))
1669 dev->dv_timings_cap = *timings;
1670 vivid_update_format_cap(dev, false);
1674 int vidioc_query_dv_timings(struct file *file, void *_fh,
1675 struct v4l2_dv_timings *timings)
1677 struct vivid_dev *dev = video_drvdata(file);
1679 if (!vivid_is_hdmi_cap(dev))
1681 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1682 dev->edid_blocks == 0)
1684 if (dev->dv_timings_signal_mode == NO_LOCK)
1686 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1687 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1690 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1691 *timings = dev->dv_timings_cap;
1692 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1693 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1695 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1696 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1697 dev->query_dv_timings_size;
1702 int vidioc_s_edid(struct file *file, void *_fh,
1703 struct v4l2_edid *edid)
1705 struct vivid_dev *dev = video_drvdata(file);
1707 memset(edid->reserved, 0, sizeof(edid->reserved));
1708 if (edid->pad >= dev->num_inputs)
1710 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1712 if (edid->blocks == 0) {
1713 dev->edid_blocks = 0;
1716 if (edid->blocks > dev->edid_max_blocks) {
1717 edid->blocks = dev->edid_max_blocks;
1720 dev->edid_blocks = edid->blocks;
1721 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1725 int vidioc_enum_framesizes(struct file *file, void *fh,
1726 struct v4l2_frmsizeenum *fsize)
1728 struct vivid_dev *dev = video_drvdata(file);
1730 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1732 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1734 if (vivid_is_webcam(dev)) {
1735 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1737 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1738 fsize->discrete = webcam_sizes[fsize->index];
1743 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1744 fsize->stepwise.min_width = MIN_WIDTH;
1745 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1746 fsize->stepwise.step_width = 2;
1747 fsize->stepwise.min_height = MIN_HEIGHT;
1748 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1749 fsize->stepwise.step_height = 2;
1753 /* timeperframe is arbitrary and continuous */
1754 int vidioc_enum_frameintervals(struct file *file, void *priv,
1755 struct v4l2_frmivalenum *fival)
1757 struct vivid_dev *dev = video_drvdata(file);
1758 const struct vivid_fmt *fmt;
1761 fmt = vivid_get_format(dev, fival->pixel_format);
1765 if (!vivid_is_webcam(dev)) {
1768 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1770 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1772 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1773 fival->discrete = dev->timeperframe_vid_cap;
1777 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1778 if (fival->width == webcam_sizes[i].width &&
1779 fival->height == webcam_sizes[i].height)
1781 if (i == ARRAY_SIZE(webcam_sizes))
1783 if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1785 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1786 fival->discrete = webcam_intervals[fival->index];
1790 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1791 struct v4l2_streamparm *parm)
1793 struct vivid_dev *dev = video_drvdata(file);
1795 if (parm->type != (dev->multiplanar ?
1796 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1797 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1800 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1801 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1802 parm->parm.capture.readbuffers = 1;
1806 #define FRACT_CMP(a, OP, b) \
1807 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1809 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1810 struct v4l2_streamparm *parm)
1812 struct vivid_dev *dev = video_drvdata(file);
1813 unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1814 struct v4l2_fract tpf;
1817 if (parm->type != (dev->multiplanar ?
1818 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1819 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1821 if (!vivid_is_webcam(dev))
1822 return vivid_vid_cap_g_parm(file, priv, parm);
1824 tpf = parm->parm.capture.timeperframe;
1826 if (tpf.denominator == 0)
1827 tpf = webcam_intervals[ival_sz - 1];
1828 for (i = 0; i < ival_sz; i++)
1829 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1833 dev->webcam_ival_idx = i;
1834 tpf = webcam_intervals[dev->webcam_ival_idx];
1835 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1836 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1838 /* resync the thread's timings */
1839 dev->cap_seq_resync = true;
1840 dev->timeperframe_vid_cap = tpf;
1841 parm->parm.capture.timeperframe = tpf;
1842 parm->parm.capture.readbuffers = 1;