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 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,
99 unsigned *nbuffers, unsigned *nplanes,
100 unsigned sizes[], struct device *alloc_devs[])
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;
126 * Check if the number of requested planes match
127 * the number of buffers in the current format. You can't mix that.
129 if (*nplanes != buffers)
131 for (p = 0; p < buffers; p++) {
132 if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
133 dev->fmt_cap->data_offset[p])
137 for (p = 0; p < buffers; p++)
138 sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
139 dev->fmt_cap->data_offset[p];
142 if (vq->num_buffers + *nbuffers < 2)
143 *nbuffers = 2 - vq->num_buffers;
147 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
148 for (p = 0; p < buffers; p++)
149 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
154 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
156 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
158 unsigned buffers = tpg_g_buffers(&dev->tpg);
161 dprintk(dev, 1, "%s\n", __func__);
163 if (WARN_ON(NULL == dev->fmt_cap))
166 if (dev->buf_prepare_error) {
168 * Error injection: test what happens if buf_prepare() returns
171 dev->buf_prepare_error = false;
174 for (p = 0; p < buffers; p++) {
175 size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
176 dev->fmt_cap->data_offset[p];
178 if (vb2_plane_size(vb, p) < size) {
179 dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
180 __func__, p, vb2_plane_size(vb, p), size);
184 vb2_set_plane_payload(vb, p, size);
185 vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
191 static void vid_cap_buf_finish(struct vb2_buffer *vb)
193 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
194 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
195 struct v4l2_timecode *tc = &vbuf->timecode;
197 unsigned seq = vbuf->sequence;
199 if (!vivid_is_sdtv_cap(dev))
203 * Set the timecode. Rarely used, so it is interesting to
206 vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
207 if (dev->std_cap & V4L2_STD_525_60)
209 tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
211 tc->frames = seq % fps;
212 tc->seconds = (seq / fps) % 60;
213 tc->minutes = (seq / (60 * fps)) % 60;
214 tc->hours = (seq / (60 * 60 * fps)) % 24;
217 static void vid_cap_buf_queue(struct vb2_buffer *vb)
219 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
220 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
221 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
223 dprintk(dev, 1, "%s\n", __func__);
225 spin_lock(&dev->slock);
226 list_add_tail(&buf->list, &dev->vid_cap_active);
227 spin_unlock(&dev->slock);
230 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
232 struct vivid_dev *dev = vb2_get_drv_priv(vq);
236 if (vb2_is_streaming(&dev->vb_vid_out_q))
237 dev->can_loop_video = vivid_vid_can_loop(dev);
239 dev->vid_cap_seq_count = 0;
240 dprintk(dev, 1, "%s\n", __func__);
241 for (i = 0; i < VIDEO_MAX_FRAME; i++)
242 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
243 if (dev->start_streaming_error) {
244 dev->start_streaming_error = false;
247 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
250 struct vivid_buffer *buf, *tmp;
252 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
253 list_del(&buf->list);
254 vb2_buffer_done(&buf->vb.vb2_buf,
255 VB2_BUF_STATE_QUEUED);
261 /* abort streaming and wait for last buffer */
262 static void vid_cap_stop_streaming(struct vb2_queue *vq)
264 struct vivid_dev *dev = vb2_get_drv_priv(vq);
266 dprintk(dev, 1, "%s\n", __func__);
267 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
268 dev->can_loop_video = false;
271 const struct vb2_ops vivid_vid_cap_qops = {
272 .queue_setup = vid_cap_queue_setup,
273 .buf_prepare = vid_cap_buf_prepare,
274 .buf_finish = vid_cap_buf_finish,
275 .buf_queue = vid_cap_buf_queue,
276 .start_streaming = vid_cap_start_streaming,
277 .stop_streaming = vid_cap_stop_streaming,
278 .wait_prepare = vb2_ops_wait_prepare,
279 .wait_finish = vb2_ops_wait_finish,
283 * Determine the 'picture' quality based on the current TV frequency: either
284 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
285 * signal or NOISE for no signal.
287 void vivid_update_quality(struct vivid_dev *dev)
289 unsigned freq_modulus;
291 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
293 * The 'noise' will only be replaced by the actual video
294 * if the output video matches the input video settings.
296 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
299 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
300 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
303 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
304 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
307 if (!vivid_is_tv_cap(dev)) {
308 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
313 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
314 * From +/- 0.25 MHz around the channel there is color, and from
315 * +/- 1 MHz there is grayscale (chroma is lost).
316 * Everywhere else it is just noise.
318 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
319 if (freq_modulus > 2 * 16) {
320 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
321 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
324 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
325 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
327 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
331 * Get the current picture quality and the associated afc value.
333 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
335 unsigned freq_modulus;
339 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
340 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
341 return tpg_g_quality(&dev->tpg);
344 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
345 * From +/- 0.25 MHz around the channel there is color, and from
346 * +/- 1 MHz there is grayscale (chroma is lost).
347 * Everywhere else it is just gray.
349 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
351 *afc = freq_modulus - 1 * 16;
352 return TPG_QUAL_GRAY;
355 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
357 if (vivid_is_sdtv_cap(dev))
358 return dev->std_aspect_ratio;
360 if (vivid_is_hdmi_cap(dev))
361 return dev->dv_timings_aspect_ratio;
363 return TPG_VIDEO_ASPECT_IMAGE;
366 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
368 if (vivid_is_sdtv_cap(dev))
369 return (dev->std_cap & V4L2_STD_525_60) ?
370 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
372 if (vivid_is_hdmi_cap(dev) &&
373 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
374 return dev->src_rect.height == 480 ?
375 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
377 return TPG_PIXEL_ASPECT_SQUARE;
381 * Called whenever the format has to be reset which can occur when
382 * changing inputs, standard, timings, etc.
384 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
386 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
390 switch (dev->input_type[dev->input]) {
393 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
394 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
395 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
396 dev->field_cap = V4L2_FIELD_NONE;
397 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
401 dev->field_cap = dev->tv_field_cap;
402 dev->src_rect.width = 720;
403 if (dev->std_cap & V4L2_STD_525_60) {
404 dev->src_rect.height = 480;
405 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
406 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
408 dev->src_rect.height = 576;
409 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
410 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
412 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
415 dev->src_rect.width = bt->width;
416 dev->src_rect.height = bt->height;
417 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
418 if (dev->reduced_fps && can_reduce_fps(bt)) {
419 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
420 bt->flags |= V4L2_DV_FL_REDUCED_FPS;
422 pixelclock = bt->pixelclock;
423 bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
425 dev->timeperframe_vid_cap = (struct v4l2_fract) {
426 size / 100, (u32)pixelclock / 100
429 dev->field_cap = V4L2_FIELD_ALTERNATE;
431 dev->field_cap = V4L2_FIELD_NONE;
434 * We can be called from within s_ctrl, in that case we can't
435 * set/get controls. Luckily we don't need to in that case.
437 if (keep_controls || !dev->colorspace)
439 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
440 if (bt->width == 720 && bt->height <= 576)
441 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
443 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
444 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
446 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
447 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
449 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
452 vfree(dev->bitmap_cap);
453 dev->bitmap_cap = NULL;
454 vivid_update_quality(dev);
455 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
456 dev->crop_cap = dev->src_rect;
457 dev->crop_bounds_cap = dev->src_rect;
458 dev->compose_cap = dev->crop_cap;
459 if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
460 dev->compose_cap.height /= 2;
461 dev->fmt_cap_rect = dev->compose_cap;
462 tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
463 tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
464 tpg_update_mv_step(&dev->tpg);
467 /* Map the field to something that is valid for the current input */
468 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
470 if (vivid_is_sdtv_cap(dev)) {
472 case V4L2_FIELD_INTERLACED_TB:
473 case V4L2_FIELD_INTERLACED_BT:
474 case V4L2_FIELD_SEQ_TB:
475 case V4L2_FIELD_SEQ_BT:
477 case V4L2_FIELD_BOTTOM:
478 case V4L2_FIELD_ALTERNATE:
480 case V4L2_FIELD_INTERLACED:
482 return V4L2_FIELD_INTERLACED;
485 if (vivid_is_hdmi_cap(dev))
486 return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
488 return V4L2_FIELD_NONE;
491 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
493 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
494 return tpg_g_colorspace(&dev->tpg);
495 return dev->colorspace_out;
498 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
500 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
501 return tpg_g_xfer_func(&dev->tpg);
502 return dev->xfer_func_out;
505 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
507 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
508 return tpg_g_ycbcr_enc(&dev->tpg);
509 return dev->ycbcr_enc_out;
512 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
514 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
515 return tpg_g_quantization(&dev->tpg);
516 return dev->quantization_out;
519 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
520 struct v4l2_format *f)
522 struct vivid_dev *dev = video_drvdata(file);
523 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
526 mp->width = dev->fmt_cap_rect.width;
527 mp->height = dev->fmt_cap_rect.height;
528 mp->field = dev->field_cap;
529 mp->pixelformat = dev->fmt_cap->fourcc;
530 mp->colorspace = vivid_colorspace_cap(dev);
531 mp->xfer_func = vivid_xfer_func_cap(dev);
532 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
533 mp->quantization = vivid_quantization_cap(dev);
534 mp->num_planes = dev->fmt_cap->buffers;
535 for (p = 0; p < mp->num_planes; p++) {
536 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
537 mp->plane_fmt[p].sizeimage =
538 tpg_g_line_width(&dev->tpg, p) * mp->height +
539 dev->fmt_cap->data_offset[p];
544 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
545 struct v4l2_format *f)
547 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
548 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
549 struct vivid_dev *dev = video_drvdata(file);
550 const struct vivid_fmt *fmt;
551 unsigned bytesperline, max_bpl;
556 fmt = vivid_get_format(dev, mp->pixelformat);
558 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
560 mp->pixelformat = V4L2_PIX_FMT_YUYV;
561 fmt = vivid_get_format(dev, mp->pixelformat);
564 mp->field = vivid_field_cap(dev, mp->field);
565 if (vivid_is_webcam(dev)) {
566 const struct v4l2_frmsize_discrete *sz =
567 v4l2_find_nearest_format(&webcam_probe, mp->width, mp->height);
571 } else if (vivid_is_sdtv_cap(dev)) {
573 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
575 w = dev->src_rect.width;
576 h = dev->src_rect.height;
578 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
580 if (vivid_is_webcam(dev) ||
581 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
583 mp->height = h / factor;
585 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
587 v4l2_rect_set_min_size(&r, &vivid_min_rect);
588 v4l2_rect_set_max_size(&r, &vivid_max_rect);
589 if (dev->has_scaler_cap && !dev->has_compose_cap) {
590 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
592 v4l2_rect_set_max_size(&r, &max_r);
593 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
594 v4l2_rect_set_max_size(&r, &dev->src_rect);
595 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
596 v4l2_rect_set_min_size(&r, &dev->src_rect);
599 mp->height = r.height / factor;
602 /* This driver supports custom bytesperline values */
604 mp->num_planes = fmt->buffers;
605 for (p = 0; p < mp->num_planes; p++) {
606 /* Calculate the minimum supported bytesperline value */
607 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
608 /* Calculate the maximum supported bytesperline value */
609 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
611 if (pfmt[p].bytesperline > max_bpl)
612 pfmt[p].bytesperline = max_bpl;
613 if (pfmt[p].bytesperline < bytesperline)
614 pfmt[p].bytesperline = bytesperline;
615 pfmt[p].sizeimage = tpg_calc_line_width(&dev->tpg, p, pfmt[p].bytesperline) *
616 mp->height + fmt->data_offset[p];
617 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
619 mp->colorspace = vivid_colorspace_cap(dev);
620 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
621 mp->xfer_func = vivid_xfer_func_cap(dev);
622 mp->quantization = vivid_quantization_cap(dev);
623 memset(mp->reserved, 0, sizeof(mp->reserved));
627 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
628 struct v4l2_format *f)
630 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
631 struct vivid_dev *dev = video_drvdata(file);
632 struct v4l2_rect *crop = &dev->crop_cap;
633 struct v4l2_rect *compose = &dev->compose_cap;
634 struct vb2_queue *q = &dev->vb_vid_cap_q;
635 int ret = vivid_try_fmt_vid_cap(file, priv, f);
643 if (vb2_is_busy(q)) {
644 dprintk(dev, 1, "%s device busy\n", __func__);
648 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
649 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
653 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
654 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
657 /* Note: the webcam input doesn't support scaling, cropping or composing */
659 if (!vivid_is_webcam(dev) &&
660 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
661 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
663 if (dev->has_scaler_cap) {
664 if (dev->has_compose_cap)
665 v4l2_rect_map_inside(compose, &r);
668 if (dev->has_crop_cap && !dev->has_compose_cap) {
669 struct v4l2_rect min_r = {
672 factor * r.height / MAX_ZOOM
674 struct v4l2_rect max_r = {
677 factor * r.height * MAX_ZOOM
680 v4l2_rect_set_min_size(crop, &min_r);
681 v4l2_rect_set_max_size(crop, &max_r);
682 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
683 } else if (dev->has_crop_cap) {
684 struct v4l2_rect min_r = {
686 compose->width / MAX_ZOOM,
687 factor * compose->height / MAX_ZOOM
689 struct v4l2_rect max_r = {
691 compose->width * MAX_ZOOM,
692 factor * compose->height * MAX_ZOOM
695 v4l2_rect_set_min_size(crop, &min_r);
696 v4l2_rect_set_max_size(crop, &max_r);
697 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
699 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
701 v4l2_rect_set_size_to(crop, &r);
702 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
705 v4l2_rect_set_size_to(compose, &r);
706 } else if (!dev->has_crop_cap) {
707 v4l2_rect_map_inside(compose, &r);
710 v4l2_rect_set_max_size(crop, &r);
711 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
712 compose->top *= factor;
713 compose->height *= factor;
714 v4l2_rect_set_size_to(compose, crop);
715 v4l2_rect_map_inside(compose, &r);
716 compose->top /= factor;
717 compose->height /= factor;
719 } else if (vivid_is_webcam(dev)) {
720 /* Guaranteed to be a match */
721 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
722 if (webcam_sizes[i].width == mp->width &&
723 webcam_sizes[i].height == mp->height)
725 dev->webcam_size_idx = i;
726 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
727 dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
728 vivid_update_format_cap(dev, false);
730 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
732 v4l2_rect_set_size_to(compose, &r);
734 v4l2_rect_set_size_to(crop, &r);
737 dev->fmt_cap_rect.width = mp->width;
738 dev->fmt_cap_rect.height = mp->height;
739 tpg_s_buf_height(&dev->tpg, mp->height);
740 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
741 for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
742 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
743 dev->field_cap = mp->field;
744 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
745 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
747 tpg_s_field(&dev->tpg, dev->field_cap, false);
748 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
749 if (vivid_is_sdtv_cap(dev))
750 dev->tv_field_cap = mp->field;
751 tpg_update_mv_step(&dev->tpg);
755 int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
756 struct v4l2_format *f)
758 struct vivid_dev *dev = video_drvdata(file);
760 if (!dev->multiplanar)
762 return vivid_g_fmt_vid_cap(file, priv, f);
765 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
766 struct v4l2_format *f)
768 struct vivid_dev *dev = video_drvdata(file);
770 if (!dev->multiplanar)
772 return vivid_try_fmt_vid_cap(file, priv, f);
775 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
776 struct v4l2_format *f)
778 struct vivid_dev *dev = video_drvdata(file);
780 if (!dev->multiplanar)
782 return vivid_s_fmt_vid_cap(file, priv, f);
785 int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
786 struct v4l2_format *f)
788 struct vivid_dev *dev = video_drvdata(file);
790 if (dev->multiplanar)
792 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
795 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
796 struct v4l2_format *f)
798 struct vivid_dev *dev = video_drvdata(file);
800 if (dev->multiplanar)
802 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
805 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
806 struct v4l2_format *f)
808 struct vivid_dev *dev = video_drvdata(file);
810 if (dev->multiplanar)
812 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
815 int vivid_vid_cap_g_selection(struct file *file, void *priv,
816 struct v4l2_selection *sel)
818 struct vivid_dev *dev = video_drvdata(file);
820 if (!dev->has_crop_cap && !dev->has_compose_cap)
822 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
824 if (vivid_is_webcam(dev))
827 sel->r.left = sel->r.top = 0;
828 switch (sel->target) {
829 case V4L2_SEL_TGT_CROP:
830 if (!dev->has_crop_cap)
832 sel->r = dev->crop_cap;
834 case V4L2_SEL_TGT_CROP_DEFAULT:
835 case V4L2_SEL_TGT_CROP_BOUNDS:
836 if (!dev->has_crop_cap)
838 sel->r = dev->src_rect;
840 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
841 if (!dev->has_compose_cap)
843 sel->r = vivid_max_rect;
845 case V4L2_SEL_TGT_COMPOSE:
846 if (!dev->has_compose_cap)
848 sel->r = dev->compose_cap;
850 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
851 if (!dev->has_compose_cap)
853 sel->r = dev->fmt_cap_rect;
861 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
863 struct vivid_dev *dev = video_drvdata(file);
864 struct v4l2_rect *crop = &dev->crop_cap;
865 struct v4l2_rect *compose = &dev->compose_cap;
866 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
869 if (!dev->has_crop_cap && !dev->has_compose_cap)
871 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
873 if (vivid_is_webcam(dev))
877 case V4L2_SEL_TGT_CROP:
878 if (!dev->has_crop_cap)
880 ret = vivid_vid_adjust_sel(s->flags, &s->r);
883 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
884 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
885 v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
887 s->r.height /= factor;
888 if (dev->has_scaler_cap) {
889 struct v4l2_rect fmt = dev->fmt_cap_rect;
890 struct v4l2_rect max_rect = {
892 s->r.width * MAX_ZOOM,
893 s->r.height * MAX_ZOOM
895 struct v4l2_rect min_rect = {
897 s->r.width / MAX_ZOOM,
898 s->r.height / MAX_ZOOM
901 v4l2_rect_set_min_size(&fmt, &min_rect);
902 if (!dev->has_compose_cap)
903 v4l2_rect_set_max_size(&fmt, &max_rect);
904 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
905 vb2_is_busy(&dev->vb_vid_cap_q))
907 if (dev->has_compose_cap) {
908 v4l2_rect_set_min_size(compose, &min_rect);
909 v4l2_rect_set_max_size(compose, &max_rect);
911 dev->fmt_cap_rect = fmt;
912 tpg_s_buf_height(&dev->tpg, fmt.height);
913 } else if (dev->has_compose_cap) {
914 struct v4l2_rect fmt = dev->fmt_cap_rect;
916 v4l2_rect_set_min_size(&fmt, &s->r);
917 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
918 vb2_is_busy(&dev->vb_vid_cap_q))
920 dev->fmt_cap_rect = fmt;
921 tpg_s_buf_height(&dev->tpg, fmt.height);
922 v4l2_rect_set_size_to(compose, &s->r);
923 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
925 if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
926 vb2_is_busy(&dev->vb_vid_cap_q))
928 v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
929 v4l2_rect_set_size_to(compose, &s->r);
930 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
931 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
934 s->r.height *= factor;
937 case V4L2_SEL_TGT_COMPOSE:
938 if (!dev->has_compose_cap)
940 ret = vivid_vid_adjust_sel(s->flags, &s->r);
943 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
944 v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
945 if (dev->has_scaler_cap) {
946 struct v4l2_rect max_rect = {
948 dev->src_rect.width * MAX_ZOOM,
949 (dev->src_rect.height / factor) * MAX_ZOOM
952 v4l2_rect_set_max_size(&s->r, &max_rect);
953 if (dev->has_crop_cap) {
954 struct v4l2_rect min_rect = {
956 s->r.width / MAX_ZOOM,
957 (s->r.height * factor) / MAX_ZOOM
959 struct v4l2_rect max_rect = {
961 s->r.width * MAX_ZOOM,
962 (s->r.height * factor) * MAX_ZOOM
965 v4l2_rect_set_min_size(crop, &min_rect);
966 v4l2_rect_set_max_size(crop, &max_rect);
967 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
969 } else if (dev->has_crop_cap) {
971 s->r.height *= factor;
972 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
973 v4l2_rect_set_size_to(crop, &s->r);
974 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
976 s->r.height /= factor;
978 v4l2_rect_set_size_to(&s->r, &dev->src_rect);
979 s->r.height /= factor;
981 v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
982 if (dev->bitmap_cap && (compose->width != s->r.width ||
983 compose->height != s->r.height)) {
984 vfree(dev->bitmap_cap);
985 dev->bitmap_cap = NULL;
993 tpg_s_crop_compose(&dev->tpg, crop, compose);
997 int vivid_vid_cap_cropcap(struct file *file, void *priv,
998 struct v4l2_cropcap *cap)
1000 struct vivid_dev *dev = video_drvdata(file);
1002 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1005 switch (vivid_get_pixel_aspect(dev)) {
1006 case TPG_PIXEL_ASPECT_NTSC:
1007 cap->pixelaspect.numerator = 11;
1008 cap->pixelaspect.denominator = 10;
1010 case TPG_PIXEL_ASPECT_PAL:
1011 cap->pixelaspect.numerator = 54;
1012 cap->pixelaspect.denominator = 59;
1014 case TPG_PIXEL_ASPECT_SQUARE:
1015 cap->pixelaspect.numerator = 1;
1016 cap->pixelaspect.denominator = 1;
1022 int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1023 struct v4l2_fmtdesc *f)
1025 struct vivid_dev *dev = video_drvdata(file);
1026 const struct vivid_fmt *fmt;
1028 if (dev->multiplanar)
1031 if (f->index >= ARRAY_SIZE(formats_ovl))
1034 fmt = &formats_ovl[f->index];
1036 f->pixelformat = fmt->fourcc;
1040 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1041 struct v4l2_format *f)
1043 struct vivid_dev *dev = video_drvdata(file);
1044 const struct v4l2_rect *compose = &dev->compose_cap;
1045 struct v4l2_window *win = &f->fmt.win;
1046 unsigned clipcount = win->clipcount;
1048 if (dev->multiplanar)
1051 win->w.top = dev->overlay_cap_top;
1052 win->w.left = dev->overlay_cap_left;
1053 win->w.width = compose->width;
1054 win->w.height = compose->height;
1055 win->field = dev->overlay_cap_field;
1056 win->clipcount = dev->clipcount_cap;
1057 if (clipcount > dev->clipcount_cap)
1058 clipcount = dev->clipcount_cap;
1059 if (dev->bitmap_cap == NULL)
1061 else if (win->bitmap) {
1062 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1063 ((compose->width + 7) / 8) * compose->height))
1066 if (clipcount && win->clips) {
1067 if (copy_to_user(win->clips, dev->clips_cap,
1068 clipcount * sizeof(dev->clips_cap[0])))
1074 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1075 struct v4l2_format *f)
1077 struct vivid_dev *dev = video_drvdata(file);
1078 const struct v4l2_rect *compose = &dev->compose_cap;
1079 struct v4l2_window *win = &f->fmt.win;
1082 if (dev->multiplanar)
1085 win->w.left = clamp_t(int, win->w.left,
1086 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1087 win->w.top = clamp_t(int, win->w.top,
1088 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1089 win->w.width = compose->width;
1090 win->w.height = compose->height;
1091 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1092 win->field = V4L2_FIELD_ANY;
1094 win->global_alpha = 0;
1095 if (win->clipcount && !win->clips)
1097 if (win->clipcount > MAX_CLIPS)
1098 win->clipcount = MAX_CLIPS;
1099 if (win->clipcount) {
1100 if (copy_from_user(dev->try_clips_cap, win->clips,
1101 win->clipcount * sizeof(dev->clips_cap[0])))
1103 for (i = 0; i < win->clipcount; i++) {
1104 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1106 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1107 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1108 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1109 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1112 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1113 * number and it's typically a one-time deal.
1115 for (i = 0; i < win->clipcount - 1; i++) {
1116 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1118 for (j = i + 1; j < win->clipcount; j++) {
1119 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1121 if (v4l2_rect_overlap(r1, r2))
1125 if (copy_to_user(win->clips, dev->try_clips_cap,
1126 win->clipcount * sizeof(dev->clips_cap[0])))
1132 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1133 struct v4l2_format *f)
1135 struct vivid_dev *dev = video_drvdata(file);
1136 const struct v4l2_rect *compose = &dev->compose_cap;
1137 struct v4l2_window *win = &f->fmt.win;
1138 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1139 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1140 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1141 void *new_bitmap = NULL;
1147 new_bitmap = vzalloc(bitmap_size);
1149 if (new_bitmap == NULL)
1151 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1157 dev->overlay_cap_top = win->w.top;
1158 dev->overlay_cap_left = win->w.left;
1159 dev->overlay_cap_field = win->field;
1160 vfree(dev->bitmap_cap);
1161 dev->bitmap_cap = new_bitmap;
1162 dev->clipcount_cap = win->clipcount;
1163 if (dev->clipcount_cap)
1164 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1168 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1170 struct vivid_dev *dev = video_drvdata(file);
1172 if (dev->multiplanar)
1175 if (i && dev->fb_vbase_cap == NULL)
1178 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1179 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1183 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1185 dev->overlay_cap_owner = i ? fh : NULL;
1189 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1190 struct v4l2_framebuffer *a)
1192 struct vivid_dev *dev = video_drvdata(file);
1194 if (dev->multiplanar)
1198 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1199 V4L2_FBUF_CAP_LIST_CLIPPING;
1200 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1201 a->fmt.field = V4L2_FIELD_NONE;
1202 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1207 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1208 const struct v4l2_framebuffer *a)
1210 struct vivid_dev *dev = video_drvdata(file);
1211 const struct vivid_fmt *fmt;
1213 if (dev->multiplanar)
1216 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1219 if (dev->overlay_cap_owner)
1222 if (a->base == NULL) {
1223 dev->fb_cap.base = NULL;
1224 dev->fb_vbase_cap = NULL;
1228 if (a->fmt.width < 48 || a->fmt.height < 32)
1230 fmt = vivid_get_format(dev, a->fmt.pixelformat);
1231 if (!fmt || !fmt->can_do_overlay)
1233 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1235 if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
1238 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1240 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1241 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1242 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1243 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1247 static const struct v4l2_audio vivid_audio_inputs[] = {
1248 { 0, "TV", V4L2_AUDCAP_STEREO },
1249 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1252 int vidioc_enum_input(struct file *file, void *priv,
1253 struct v4l2_input *inp)
1255 struct vivid_dev *dev = video_drvdata(file);
1257 if (inp->index >= dev->num_inputs)
1260 inp->type = V4L2_INPUT_TYPE_CAMERA;
1261 switch (dev->input_type[inp->index]) {
1263 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1264 dev->input_name_counter[inp->index]);
1265 inp->capabilities = 0;
1268 snprintf(inp->name, sizeof(inp->name), "TV %u",
1269 dev->input_name_counter[inp->index]);
1270 inp->type = V4L2_INPUT_TYPE_TUNER;
1271 inp->std = V4L2_STD_ALL;
1272 if (dev->has_audio_inputs)
1273 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1274 inp->capabilities = V4L2_IN_CAP_STD;
1277 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1278 dev->input_name_counter[inp->index]);
1279 inp->std = V4L2_STD_ALL;
1280 if (dev->has_audio_inputs)
1281 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1282 inp->capabilities = V4L2_IN_CAP_STD;
1285 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1286 dev->input_name_counter[inp->index]);
1287 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1288 if (dev->edid_blocks == 0 ||
1289 dev->dv_timings_signal_mode == NO_SIGNAL)
1290 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1291 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1292 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1293 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1296 if (dev->sensor_hflip)
1297 inp->status |= V4L2_IN_ST_HFLIP;
1298 if (dev->sensor_vflip)
1299 inp->status |= V4L2_IN_ST_VFLIP;
1300 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1301 if (dev->std_signal_mode == NO_SIGNAL) {
1302 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1303 } else if (dev->std_signal_mode == NO_LOCK) {
1304 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1305 } else if (vivid_is_tv_cap(dev)) {
1306 switch (tpg_g_quality(&dev->tpg)) {
1308 inp->status |= V4L2_IN_ST_COLOR_KILL;
1310 case TPG_QUAL_NOISE:
1311 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1321 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1323 struct vivid_dev *dev = video_drvdata(file);
1329 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1331 struct vivid_dev *dev = video_drvdata(file);
1332 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1333 unsigned brightness;
1335 if (i >= dev->num_inputs)
1338 if (i == dev->input)
1341 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1345 dev->vid_cap_dev.tvnorms = 0;
1346 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1347 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1348 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1350 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1351 vivid_update_format_cap(dev, false);
1353 if (dev->colorspace) {
1354 switch (dev->input_type[i]) {
1356 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1360 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1363 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1364 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1365 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1367 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1369 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1376 * Modify the brightness range depending on the input.
1377 * This makes it easy to use vivid to test if applications can
1378 * handle control range modifications and is also how this is
1379 * typically used in practice as different inputs may be hooked
1380 * up to different receivers with different control ranges.
1382 brightness = 128 * i + dev->input_brightness[i];
1383 v4l2_ctrl_modify_range(dev->brightness,
1384 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1385 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1389 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1391 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1393 *vin = vivid_audio_inputs[vin->index];
1397 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1399 struct vivid_dev *dev = video_drvdata(file);
1401 if (!vivid_is_sdtv_cap(dev))
1403 *vin = vivid_audio_inputs[dev->tv_audio_input];
1407 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1409 struct vivid_dev *dev = video_drvdata(file);
1411 if (!vivid_is_sdtv_cap(dev))
1413 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1415 dev->tv_audio_input = vin->index;
1419 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1421 struct vivid_dev *dev = video_drvdata(file);
1425 vf->frequency = dev->tv_freq;
1429 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1431 struct vivid_dev *dev = video_drvdata(file);
1435 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1436 if (vivid_is_tv_cap(dev))
1437 vivid_update_quality(dev);
1441 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1443 struct vivid_dev *dev = video_drvdata(file);
1447 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1449 dev->tv_audmode = vt->audmode;
1453 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1455 struct vivid_dev *dev = video_drvdata(file);
1456 enum tpg_quality qual;
1461 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1462 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1463 vt->audmode = dev->tv_audmode;
1464 vt->rangelow = MIN_TV_FREQ;
1465 vt->rangehigh = MAX_TV_FREQ;
1466 qual = vivid_get_quality(dev, &vt->afc);
1467 if (qual == TPG_QUAL_COLOR)
1468 vt->signal = 0xffff;
1469 else if (qual == TPG_QUAL_GRAY)
1470 vt->signal = 0x8000;
1473 if (qual == TPG_QUAL_NOISE) {
1475 } else if (qual == TPG_QUAL_GRAY) {
1476 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1478 unsigned channel_nr = dev->tv_freq / (6 * 16);
1479 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1481 switch (channel_nr % options) {
1483 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1486 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1489 if (dev->std_cap & V4L2_STD_NTSC_M)
1490 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1492 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1495 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1499 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1503 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1504 const v4l2_std_id vivid_standard[] = {
1509 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1516 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1523 /* Must remain in sync with the vivid_standard array */
1524 const char * const vivid_ctrl_standard_strings[] = {
1543 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1545 struct vivid_dev *dev = video_drvdata(file);
1547 if (!vivid_is_sdtv_cap(dev))
1549 if (dev->std_signal_mode == NO_SIGNAL ||
1550 dev->std_signal_mode == NO_LOCK) {
1551 *id = V4L2_STD_UNKNOWN;
1554 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1555 *id = V4L2_STD_UNKNOWN;
1556 } else if (dev->std_signal_mode == CURRENT_STD) {
1558 } else if (dev->std_signal_mode == SELECTED_STD) {
1559 *id = dev->query_std;
1561 *id = vivid_standard[dev->query_std_last];
1562 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1568 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1570 struct vivid_dev *dev = video_drvdata(file);
1572 if (!vivid_is_sdtv_cap(dev))
1574 if (dev->std_cap == id)
1576 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1579 vivid_update_format_cap(dev, false);
1583 static void find_aspect_ratio(u32 width, u32 height,
1584 u32 *num, u32 *denom)
1586 if (!(height % 3) && ((height * 4 / 3) == width)) {
1589 } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1592 } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1595 } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1598 } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1601 } else { /* default to 16:9 */
1607 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1609 struct v4l2_bt_timings *bt = &timings->bt;
1614 if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1618 total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1619 total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1621 h_freq = (u32)bt->pixelclock / total_h_pixel;
1623 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1624 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1625 bt->polarities, bt->interlaced, timings))
1629 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1630 struct v4l2_fract aspect_ratio;
1632 find_aspect_ratio(bt->width, bt->height,
1633 &aspect_ratio.numerator,
1634 &aspect_ratio.denominator);
1635 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1636 bt->polarities, bt->interlaced,
1637 aspect_ratio, timings))
1643 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1644 struct v4l2_dv_timings *timings)
1646 struct vivid_dev *dev = video_drvdata(file);
1648 if (!vivid_is_hdmi_cap(dev))
1650 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1652 !valid_cvt_gtf_timings(timings))
1655 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
1657 if (vb2_is_busy(&dev->vb_vid_cap_q))
1660 dev->dv_timings_cap = *timings;
1661 vivid_update_format_cap(dev, false);
1665 int vidioc_query_dv_timings(struct file *file, void *_fh,
1666 struct v4l2_dv_timings *timings)
1668 struct vivid_dev *dev = video_drvdata(file);
1670 if (!vivid_is_hdmi_cap(dev))
1672 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1673 dev->edid_blocks == 0)
1675 if (dev->dv_timings_signal_mode == NO_LOCK)
1677 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1678 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1681 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1682 *timings = dev->dv_timings_cap;
1683 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1684 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1686 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1687 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1688 dev->query_dv_timings_size;
1693 int vidioc_s_edid(struct file *file, void *_fh,
1694 struct v4l2_edid *edid)
1696 struct vivid_dev *dev = video_drvdata(file);
1701 memset(edid->reserved, 0, sizeof(edid->reserved));
1702 if (edid->pad >= dev->num_inputs)
1704 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1706 if (edid->blocks == 0) {
1707 dev->edid_blocks = 0;
1708 phys_addr = CEC_PHYS_ADDR_INVALID;
1711 if (edid->blocks > dev->edid_max_blocks) {
1712 edid->blocks = dev->edid_max_blocks;
1715 phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1716 ret = cec_phys_addr_validate(phys_addr, &phys_addr, NULL);
1720 if (vb2_is_busy(&dev->vb_vid_cap_q))
1723 dev->edid_blocks = edid->blocks;
1724 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1727 /* TODO: a proper hotplug detect cycle should be emulated here */
1728 cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1730 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1731 cec_s_phys_addr(dev->cec_tx_adap[i],
1732 cec_phys_addr_for_input(phys_addr, i + 1),
1737 int vidioc_enum_framesizes(struct file *file, void *fh,
1738 struct v4l2_frmsizeenum *fsize)
1740 struct vivid_dev *dev = video_drvdata(file);
1742 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1744 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1746 if (vivid_is_webcam(dev)) {
1747 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1749 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1750 fsize->discrete = webcam_sizes[fsize->index];
1755 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1756 fsize->stepwise.min_width = MIN_WIDTH;
1757 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1758 fsize->stepwise.step_width = 2;
1759 fsize->stepwise.min_height = MIN_HEIGHT;
1760 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1761 fsize->stepwise.step_height = 2;
1765 /* timeperframe is arbitrary and continuous */
1766 int vidioc_enum_frameintervals(struct file *file, void *priv,
1767 struct v4l2_frmivalenum *fival)
1769 struct vivid_dev *dev = video_drvdata(file);
1770 const struct vivid_fmt *fmt;
1773 fmt = vivid_get_format(dev, fival->pixel_format);
1777 if (!vivid_is_webcam(dev)) {
1780 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1782 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1784 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1785 fival->discrete = dev->timeperframe_vid_cap;
1789 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1790 if (fival->width == webcam_sizes[i].width &&
1791 fival->height == webcam_sizes[i].height)
1793 if (i == ARRAY_SIZE(webcam_sizes))
1795 if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1797 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1798 fival->discrete = webcam_intervals[fival->index];
1802 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1803 struct v4l2_streamparm *parm)
1805 struct vivid_dev *dev = video_drvdata(file);
1807 if (parm->type != (dev->multiplanar ?
1808 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1809 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1812 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1813 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1814 parm->parm.capture.readbuffers = 1;
1818 #define FRACT_CMP(a, OP, b) \
1819 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1821 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1822 struct v4l2_streamparm *parm)
1824 struct vivid_dev *dev = video_drvdata(file);
1825 unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1826 struct v4l2_fract tpf;
1829 if (parm->type != (dev->multiplanar ?
1830 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1831 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1833 if (!vivid_is_webcam(dev))
1834 return vivid_vid_cap_g_parm(file, priv, parm);
1836 tpf = parm->parm.capture.timeperframe;
1838 if (tpf.denominator == 0)
1839 tpf = webcam_intervals[ival_sz - 1];
1840 for (i = 0; i < ival_sz; i++)
1841 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1845 dev->webcam_ival_idx = i;
1846 tpf = webcam_intervals[dev->webcam_ival_idx];
1847 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1848 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1850 /* resync the thread's timings */
1851 dev->cap_seq_resync = true;
1852 dev->timeperframe_vid_cap = tpf;
1853 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1854 parm->parm.capture.timeperframe = tpf;
1855 parm->parm.capture.readbuffers = 1;