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 if (dev
->kthread_vid_cap
)
242 dev
->vid_cap_seq_count
= 0;
243 dprintk(dev
, 1, "%s\n", __func__
);
244 for (i
= 0; i
< VIDEO_MAX_FRAME
; i
++)
245 dev
->must_blank
[i
] = tpg_g_perc_fill(&dev
->tpg
) < 100;
246 if (dev
->start_streaming_error
) {
247 dev
->start_streaming_error
= false;
250 err
= vivid_start_generating_vid_cap(dev
, &dev
->vid_cap_streaming
);
253 struct vivid_buffer
*buf
, *tmp
;
255 list_for_each_entry_safe(buf
, tmp
, &dev
->vid_cap_active
, list
) {
256 list_del(&buf
->list
);
257 vb2_buffer_done(&buf
->vb
.vb2_buf
,
258 VB2_BUF_STATE_QUEUED
);
264 /* abort streaming and wait for last buffer */
265 static void vid_cap_stop_streaming(struct vb2_queue
*vq
)
267 struct vivid_dev
*dev
= vb2_get_drv_priv(vq
);
269 dprintk(dev
, 1, "%s\n", __func__
);
270 vivid_stop_generating_vid_cap(dev
, &dev
->vid_cap_streaming
);
271 dev
->can_loop_video
= false;
274 const struct vb2_ops vivid_vid_cap_qops
= {
275 .queue_setup
= vid_cap_queue_setup
,
276 .buf_prepare
= vid_cap_buf_prepare
,
277 .buf_finish
= vid_cap_buf_finish
,
278 .buf_queue
= vid_cap_buf_queue
,
279 .start_streaming
= vid_cap_start_streaming
,
280 .stop_streaming
= vid_cap_stop_streaming
,
281 .wait_prepare
= vb2_ops_wait_prepare
,
282 .wait_finish
= vb2_ops_wait_finish
,
286 * Determine the 'picture' quality based on the current TV frequency: either
287 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
288 * signal or NOISE for no signal.
290 void vivid_update_quality(struct vivid_dev
*dev
)
292 unsigned freq_modulus
;
294 if (dev
->loop_video
&& (vivid_is_svid_cap(dev
) || vivid_is_hdmi_cap(dev
))) {
296 * The 'noise' will only be replaced by the actual video
297 * if the output video matches the input video settings.
299 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
, 0);
302 if (vivid_is_hdmi_cap(dev
) && VIVID_INVALID_SIGNAL(dev
->dv_timings_signal_mode
)) {
303 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
, 0);
306 if (vivid_is_sdtv_cap(dev
) && VIVID_INVALID_SIGNAL(dev
->std_signal_mode
)) {
307 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
, 0);
310 if (!vivid_is_tv_cap(dev
)) {
311 tpg_s_quality(&dev
->tpg
, TPG_QUAL_COLOR
, 0);
316 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
317 * From +/- 0.25 MHz around the channel there is color, and from
318 * +/- 1 MHz there is grayscale (chroma is lost).
319 * Everywhere else it is just noise.
321 freq_modulus
= (dev
->tv_freq
- 676 /* (43.25-1) * 16 */) % (6 * 16);
322 if (freq_modulus
> 2 * 16) {
323 tpg_s_quality(&dev
->tpg
, TPG_QUAL_NOISE
,
324 next_pseudo_random32(dev
->tv_freq
^ 0x55) & 0x3f);
327 if (freq_modulus
< 12 /*0.75 * 16*/ || freq_modulus
> 20 /*1.25 * 16*/)
328 tpg_s_quality(&dev
->tpg
, TPG_QUAL_GRAY
, 0);
330 tpg_s_quality(&dev
->tpg
, TPG_QUAL_COLOR
, 0);
334 * Get the current picture quality and the associated afc value.
336 static enum tpg_quality
vivid_get_quality(struct vivid_dev
*dev
, s32
*afc
)
338 unsigned freq_modulus
;
342 if (tpg_g_quality(&dev
->tpg
) == TPG_QUAL_COLOR
||
343 tpg_g_quality(&dev
->tpg
) == TPG_QUAL_NOISE
)
344 return tpg_g_quality(&dev
->tpg
);
347 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
348 * From +/- 0.25 MHz around the channel there is color, and from
349 * +/- 1 MHz there is grayscale (chroma is lost).
350 * Everywhere else it is just gray.
352 freq_modulus
= (dev
->tv_freq
- 676 /* (43.25-1) * 16 */) % (6 * 16);
354 *afc
= freq_modulus
- 1 * 16;
355 return TPG_QUAL_GRAY
;
358 enum tpg_video_aspect
vivid_get_video_aspect(const struct vivid_dev
*dev
)
360 if (vivid_is_sdtv_cap(dev
))
361 return dev
->std_aspect_ratio
;
363 if (vivid_is_hdmi_cap(dev
))
364 return dev
->dv_timings_aspect_ratio
;
366 return TPG_VIDEO_ASPECT_IMAGE
;
369 static enum tpg_pixel_aspect
vivid_get_pixel_aspect(const struct vivid_dev
*dev
)
371 if (vivid_is_sdtv_cap(dev
))
372 return (dev
->std_cap
& V4L2_STD_525_60
) ?
373 TPG_PIXEL_ASPECT_NTSC
: TPG_PIXEL_ASPECT_PAL
;
375 if (vivid_is_hdmi_cap(dev
) &&
376 dev
->src_rect
.width
== 720 && dev
->src_rect
.height
<= 576)
377 return dev
->src_rect
.height
== 480 ?
378 TPG_PIXEL_ASPECT_NTSC
: TPG_PIXEL_ASPECT_PAL
;
380 return TPG_PIXEL_ASPECT_SQUARE
;
384 * Called whenever the format has to be reset which can occur when
385 * changing inputs, standard, timings, etc.
387 void vivid_update_format_cap(struct vivid_dev
*dev
, bool keep_controls
)
389 struct v4l2_bt_timings
*bt
= &dev
->dv_timings_cap
.bt
;
393 switch (dev
->input_type
[dev
->input
]) {
396 dev
->src_rect
.width
= webcam_sizes
[dev
->webcam_size_idx
].width
;
397 dev
->src_rect
.height
= webcam_sizes
[dev
->webcam_size_idx
].height
;
398 dev
->timeperframe_vid_cap
= webcam_intervals
[dev
->webcam_ival_idx
];
399 dev
->field_cap
= V4L2_FIELD_NONE
;
400 tpg_s_rgb_range(&dev
->tpg
, V4L2_DV_RGB_RANGE_AUTO
);
404 dev
->field_cap
= dev
->tv_field_cap
;
405 dev
->src_rect
.width
= 720;
406 if (dev
->std_cap
& V4L2_STD_525_60
) {
407 dev
->src_rect
.height
= 480;
408 dev
->timeperframe_vid_cap
= (struct v4l2_fract
) { 1001, 30000 };
409 dev
->service_set_cap
= V4L2_SLICED_CAPTION_525
;
411 dev
->src_rect
.height
= 576;
412 dev
->timeperframe_vid_cap
= (struct v4l2_fract
) { 1000, 25000 };
413 dev
->service_set_cap
= V4L2_SLICED_WSS_625
| V4L2_SLICED_TELETEXT_B
;
415 tpg_s_rgb_range(&dev
->tpg
, V4L2_DV_RGB_RANGE_AUTO
);
418 dev
->src_rect
.width
= bt
->width
;
419 dev
->src_rect
.height
= bt
->height
;
420 size
= V4L2_DV_BT_FRAME_WIDTH(bt
) * V4L2_DV_BT_FRAME_HEIGHT(bt
);
421 if (dev
->reduced_fps
&& can_reduce_fps(bt
)) {
422 pixelclock
= div_u64(bt
->pixelclock
* 1000, 1001);
423 bt
->flags
|= V4L2_DV_FL_REDUCED_FPS
;
425 pixelclock
= bt
->pixelclock
;
426 bt
->flags
&= ~V4L2_DV_FL_REDUCED_FPS
;
428 dev
->timeperframe_vid_cap
= (struct v4l2_fract
) {
429 size
/ 100, (u32
)pixelclock
/ 100
432 dev
->field_cap
= V4L2_FIELD_ALTERNATE
;
434 dev
->field_cap
= V4L2_FIELD_NONE
;
437 * We can be called from within s_ctrl, in that case we can't
438 * set/get controls. Luckily we don't need to in that case.
440 if (keep_controls
|| !dev
->colorspace
)
442 if (bt
->flags
& V4L2_DV_FL_IS_CE_VIDEO
) {
443 if (bt
->width
== 720 && bt
->height
<= 576)
444 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_170M
);
446 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_709
);
447 v4l2_ctrl_s_ctrl(dev
->real_rgb_range_cap
, 1);
449 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_SRGB
);
450 v4l2_ctrl_s_ctrl(dev
->real_rgb_range_cap
, 0);
452 tpg_s_rgb_range(&dev
->tpg
, v4l2_ctrl_g_ctrl(dev
->rgb_range_cap
));
455 vivid_update_quality(dev
);
456 tpg_reset_source(&dev
->tpg
, dev
->src_rect
.width
, dev
->src_rect
.height
, dev
->field_cap
);
457 dev
->crop_cap
= dev
->src_rect
;
458 dev
->crop_bounds_cap
= dev
->src_rect
;
459 dev
->compose_cap
= dev
->crop_cap
;
460 if (V4L2_FIELD_HAS_T_OR_B(dev
->field_cap
))
461 dev
->compose_cap
.height
/= 2;
462 dev
->fmt_cap_rect
= dev
->compose_cap
;
463 tpg_s_video_aspect(&dev
->tpg
, vivid_get_video_aspect(dev
));
464 tpg_s_pixel_aspect(&dev
->tpg
, vivid_get_pixel_aspect(dev
));
465 tpg_update_mv_step(&dev
->tpg
);
468 /* Map the field to something that is valid for the current input */
469 static enum v4l2_field
vivid_field_cap(struct vivid_dev
*dev
, enum v4l2_field field
)
471 if (vivid_is_sdtv_cap(dev
)) {
473 case V4L2_FIELD_INTERLACED_TB
:
474 case V4L2_FIELD_INTERLACED_BT
:
475 case V4L2_FIELD_SEQ_TB
:
476 case V4L2_FIELD_SEQ_BT
:
478 case V4L2_FIELD_BOTTOM
:
479 case V4L2_FIELD_ALTERNATE
:
481 case V4L2_FIELD_INTERLACED
:
483 return V4L2_FIELD_INTERLACED
;
486 if (vivid_is_hdmi_cap(dev
))
487 return dev
->dv_timings_cap
.bt
.interlaced
? V4L2_FIELD_ALTERNATE
:
489 return V4L2_FIELD_NONE
;
492 static unsigned vivid_colorspace_cap(struct vivid_dev
*dev
)
494 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
495 return tpg_g_colorspace(&dev
->tpg
);
496 return dev
->colorspace_out
;
499 static unsigned vivid_xfer_func_cap(struct vivid_dev
*dev
)
501 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
502 return tpg_g_xfer_func(&dev
->tpg
);
503 return dev
->xfer_func_out
;
506 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev
*dev
)
508 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
509 return tpg_g_ycbcr_enc(&dev
->tpg
);
510 return dev
->ycbcr_enc_out
;
513 static unsigned vivid_quantization_cap(struct vivid_dev
*dev
)
515 if (!dev
->loop_video
|| vivid_is_webcam(dev
) || vivid_is_tv_cap(dev
))
516 return tpg_g_quantization(&dev
->tpg
);
517 return dev
->quantization_out
;
520 int vivid_g_fmt_vid_cap(struct file
*file
, void *priv
,
521 struct v4l2_format
*f
)
523 struct vivid_dev
*dev
= video_drvdata(file
);
524 struct v4l2_pix_format_mplane
*mp
= &f
->fmt
.pix_mp
;
527 mp
->width
= dev
->fmt_cap_rect
.width
;
528 mp
->height
= dev
->fmt_cap_rect
.height
;
529 mp
->field
= dev
->field_cap
;
530 mp
->pixelformat
= dev
->fmt_cap
->fourcc
;
531 mp
->colorspace
= vivid_colorspace_cap(dev
);
532 mp
->xfer_func
= vivid_xfer_func_cap(dev
);
533 mp
->ycbcr_enc
= vivid_ycbcr_enc_cap(dev
);
534 mp
->quantization
= vivid_quantization_cap(dev
);
535 mp
->num_planes
= dev
->fmt_cap
->buffers
;
536 for (p
= 0; p
< mp
->num_planes
; p
++) {
537 mp
->plane_fmt
[p
].bytesperline
= tpg_g_bytesperline(&dev
->tpg
, p
);
538 mp
->plane_fmt
[p
].sizeimage
=
539 tpg_g_line_width(&dev
->tpg
, p
) * mp
->height
+
540 dev
->fmt_cap
->data_offset
[p
];
545 int vivid_try_fmt_vid_cap(struct file
*file
, void *priv
,
546 struct v4l2_format
*f
)
548 struct v4l2_pix_format_mplane
*mp
= &f
->fmt
.pix_mp
;
549 struct v4l2_plane_pix_format
*pfmt
= mp
->plane_fmt
;
550 struct vivid_dev
*dev
= video_drvdata(file
);
551 const struct vivid_fmt
*fmt
;
552 unsigned bytesperline
, max_bpl
;
557 fmt
= vivid_get_format(dev
, mp
->pixelformat
);
559 dprintk(dev
, 1, "Fourcc format (0x%08x) unknown.\n",
561 mp
->pixelformat
= V4L2_PIX_FMT_YUYV
;
562 fmt
= vivid_get_format(dev
, mp
->pixelformat
);
565 mp
->field
= vivid_field_cap(dev
, mp
->field
);
566 if (vivid_is_webcam(dev
)) {
567 const struct v4l2_frmsize_discrete
*sz
=
568 v4l2_find_nearest_format(&webcam_probe
, mp
->width
, mp
->height
);
572 } else if (vivid_is_sdtv_cap(dev
)) {
574 h
= (dev
->std_cap
& V4L2_STD_525_60
) ? 480 : 576;
576 w
= dev
->src_rect
.width
;
577 h
= dev
->src_rect
.height
;
579 if (V4L2_FIELD_HAS_T_OR_B(mp
->field
))
581 if (vivid_is_webcam(dev
) ||
582 (!dev
->has_scaler_cap
&& !dev
->has_crop_cap
&& !dev
->has_compose_cap
)) {
584 mp
->height
= h
/ factor
;
586 struct v4l2_rect r
= { 0, 0, mp
->width
, mp
->height
* factor
};
588 v4l2_rect_set_min_size(&r
, &vivid_min_rect
);
589 v4l2_rect_set_max_size(&r
, &vivid_max_rect
);
590 if (dev
->has_scaler_cap
&& !dev
->has_compose_cap
) {
591 struct v4l2_rect max_r
= { 0, 0, MAX_ZOOM
* w
, MAX_ZOOM
* h
};
593 v4l2_rect_set_max_size(&r
, &max_r
);
594 } else if (!dev
->has_scaler_cap
&& dev
->has_crop_cap
&& !dev
->has_compose_cap
) {
595 v4l2_rect_set_max_size(&r
, &dev
->src_rect
);
596 } else if (!dev
->has_scaler_cap
&& !dev
->has_crop_cap
) {
597 v4l2_rect_set_min_size(&r
, &dev
->src_rect
);
600 mp
->height
= r
.height
/ factor
;
603 /* This driver supports custom bytesperline values */
605 mp
->num_planes
= fmt
->buffers
;
606 for (p
= 0; p
< mp
->num_planes
; p
++) {
607 /* Calculate the minimum supported bytesperline value */
608 bytesperline
= (mp
->width
* fmt
->bit_depth
[p
]) >> 3;
609 /* Calculate the maximum supported bytesperline value */
610 max_bpl
= (MAX_ZOOM
* MAX_WIDTH
* fmt
->bit_depth
[p
]) >> 3;
612 if (pfmt
[p
].bytesperline
> max_bpl
)
613 pfmt
[p
].bytesperline
= max_bpl
;
614 if (pfmt
[p
].bytesperline
< bytesperline
)
615 pfmt
[p
].bytesperline
= bytesperline
;
616 pfmt
[p
].sizeimage
= tpg_calc_line_width(&dev
->tpg
, p
, pfmt
[p
].bytesperline
) *
617 mp
->height
+ fmt
->data_offset
[p
];
618 memset(pfmt
[p
].reserved
, 0, sizeof(pfmt
[p
].reserved
));
620 mp
->colorspace
= vivid_colorspace_cap(dev
);
621 mp
->ycbcr_enc
= vivid_ycbcr_enc_cap(dev
);
622 mp
->xfer_func
= vivid_xfer_func_cap(dev
);
623 mp
->quantization
= vivid_quantization_cap(dev
);
624 memset(mp
->reserved
, 0, sizeof(mp
->reserved
));
628 int vivid_s_fmt_vid_cap(struct file
*file
, void *priv
,
629 struct v4l2_format
*f
)
631 struct v4l2_pix_format_mplane
*mp
= &f
->fmt
.pix_mp
;
632 struct vivid_dev
*dev
= video_drvdata(file
);
633 struct v4l2_rect
*crop
= &dev
->crop_cap
;
634 struct v4l2_rect
*compose
= &dev
->compose_cap
;
635 struct vb2_queue
*q
= &dev
->vb_vid_cap_q
;
636 int ret
= vivid_try_fmt_vid_cap(file
, priv
, f
);
644 if (vb2_is_busy(q
)) {
645 dprintk(dev
, 1, "%s device busy\n", __func__
);
649 if (dev
->overlay_cap_owner
&& dev
->fb_cap
.fmt
.pixelformat
!= mp
->pixelformat
) {
650 dprintk(dev
, 1, "overlay is active, can't change pixelformat\n");
654 dev
->fmt_cap
= vivid_get_format(dev
, mp
->pixelformat
);
655 if (V4L2_FIELD_HAS_T_OR_B(mp
->field
))
658 /* Note: the webcam input doesn't support scaling, cropping or composing */
660 if (!vivid_is_webcam(dev
) &&
661 (dev
->has_scaler_cap
|| dev
->has_crop_cap
|| dev
->has_compose_cap
)) {
662 struct v4l2_rect r
= { 0, 0, mp
->width
, mp
->height
};
664 if (dev
->has_scaler_cap
) {
665 if (dev
->has_compose_cap
)
666 v4l2_rect_map_inside(compose
, &r
);
669 if (dev
->has_crop_cap
&& !dev
->has_compose_cap
) {
670 struct v4l2_rect min_r
= {
673 factor
* r
.height
/ MAX_ZOOM
675 struct v4l2_rect max_r
= {
678 factor
* r
.height
* MAX_ZOOM
681 v4l2_rect_set_min_size(crop
, &min_r
);
682 v4l2_rect_set_max_size(crop
, &max_r
);
683 v4l2_rect_map_inside(crop
, &dev
->crop_bounds_cap
);
684 } else if (dev
->has_crop_cap
) {
685 struct v4l2_rect min_r
= {
687 compose
->width
/ MAX_ZOOM
,
688 factor
* compose
->height
/ MAX_ZOOM
690 struct v4l2_rect max_r
= {
692 compose
->width
* MAX_ZOOM
,
693 factor
* compose
->height
* MAX_ZOOM
696 v4l2_rect_set_min_size(crop
, &min_r
);
697 v4l2_rect_set_max_size(crop
, &max_r
);
698 v4l2_rect_map_inside(crop
, &dev
->crop_bounds_cap
);
700 } else if (dev
->has_crop_cap
&& !dev
->has_compose_cap
) {
702 v4l2_rect_set_size_to(crop
, &r
);
703 v4l2_rect_map_inside(crop
, &dev
->crop_bounds_cap
);
706 v4l2_rect_set_size_to(compose
, &r
);
707 } else if (!dev
->has_crop_cap
) {
708 v4l2_rect_map_inside(compose
, &r
);
711 v4l2_rect_set_max_size(crop
, &r
);
712 v4l2_rect_map_inside(crop
, &dev
->crop_bounds_cap
);
713 compose
->top
*= factor
;
714 compose
->height
*= factor
;
715 v4l2_rect_set_size_to(compose
, crop
);
716 v4l2_rect_map_inside(compose
, &r
);
717 compose
->top
/= factor
;
718 compose
->height
/= factor
;
720 } else if (vivid_is_webcam(dev
)) {
721 /* Guaranteed to be a match */
722 for (i
= 0; i
< ARRAY_SIZE(webcam_sizes
); i
++)
723 if (webcam_sizes
[i
].width
== mp
->width
&&
724 webcam_sizes
[i
].height
== mp
->height
)
726 dev
->webcam_size_idx
= i
;
727 if (dev
->webcam_ival_idx
>= 2 * (VIVID_WEBCAM_SIZES
- i
))
728 dev
->webcam_ival_idx
= 2 * (VIVID_WEBCAM_SIZES
- i
) - 1;
729 vivid_update_format_cap(dev
, false);
731 struct v4l2_rect r
= { 0, 0, mp
->width
, mp
->height
};
733 v4l2_rect_set_size_to(compose
, &r
);
735 v4l2_rect_set_size_to(crop
, &r
);
738 dev
->fmt_cap_rect
.width
= mp
->width
;
739 dev
->fmt_cap_rect
.height
= mp
->height
;
740 tpg_s_buf_height(&dev
->tpg
, mp
->height
);
741 tpg_s_fourcc(&dev
->tpg
, dev
->fmt_cap
->fourcc
);
742 for (p
= 0; p
< tpg_g_buffers(&dev
->tpg
); p
++)
743 tpg_s_bytesperline(&dev
->tpg
, p
, mp
->plane_fmt
[p
].bytesperline
);
744 dev
->field_cap
= mp
->field
;
745 if (dev
->field_cap
== V4L2_FIELD_ALTERNATE
)
746 tpg_s_field(&dev
->tpg
, V4L2_FIELD_TOP
, true);
748 tpg_s_field(&dev
->tpg
, dev
->field_cap
, false);
749 tpg_s_crop_compose(&dev
->tpg
, &dev
->crop_cap
, &dev
->compose_cap
);
750 if (vivid_is_sdtv_cap(dev
))
751 dev
->tv_field_cap
= mp
->field
;
752 tpg_update_mv_step(&dev
->tpg
);
756 int vidioc_g_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
757 struct v4l2_format
*f
)
759 struct vivid_dev
*dev
= video_drvdata(file
);
761 if (!dev
->multiplanar
)
763 return vivid_g_fmt_vid_cap(file
, priv
, f
);
766 int vidioc_try_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
767 struct v4l2_format
*f
)
769 struct vivid_dev
*dev
= video_drvdata(file
);
771 if (!dev
->multiplanar
)
773 return vivid_try_fmt_vid_cap(file
, priv
, f
);
776 int vidioc_s_fmt_vid_cap_mplane(struct file
*file
, void *priv
,
777 struct v4l2_format
*f
)
779 struct vivid_dev
*dev
= video_drvdata(file
);
781 if (!dev
->multiplanar
)
783 return vivid_s_fmt_vid_cap(file
, priv
, f
);
786 int vidioc_g_fmt_vid_cap(struct file
*file
, void *priv
,
787 struct v4l2_format
*f
)
789 struct vivid_dev
*dev
= video_drvdata(file
);
791 if (dev
->multiplanar
)
793 return fmt_sp2mp_func(file
, priv
, f
, vivid_g_fmt_vid_cap
);
796 int vidioc_try_fmt_vid_cap(struct file
*file
, void *priv
,
797 struct v4l2_format
*f
)
799 struct vivid_dev
*dev
= video_drvdata(file
);
801 if (dev
->multiplanar
)
803 return fmt_sp2mp_func(file
, priv
, f
, vivid_try_fmt_vid_cap
);
806 int vidioc_s_fmt_vid_cap(struct file
*file
, void *priv
,
807 struct v4l2_format
*f
)
809 struct vivid_dev
*dev
= video_drvdata(file
);
811 if (dev
->multiplanar
)
813 return fmt_sp2mp_func(file
, priv
, f
, vivid_s_fmt_vid_cap
);
816 int vivid_vid_cap_g_selection(struct file
*file
, void *priv
,
817 struct v4l2_selection
*sel
)
819 struct vivid_dev
*dev
= video_drvdata(file
);
821 if (!dev
->has_crop_cap
&& !dev
->has_compose_cap
)
823 if (sel
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
825 if (vivid_is_webcam(dev
))
828 sel
->r
.left
= sel
->r
.top
= 0;
829 switch (sel
->target
) {
830 case V4L2_SEL_TGT_CROP
:
831 if (!dev
->has_crop_cap
)
833 sel
->r
= dev
->crop_cap
;
835 case V4L2_SEL_TGT_CROP_DEFAULT
:
836 case V4L2_SEL_TGT_CROP_BOUNDS
:
837 if (!dev
->has_crop_cap
)
839 sel
->r
= dev
->src_rect
;
841 case V4L2_SEL_TGT_COMPOSE_BOUNDS
:
842 if (!dev
->has_compose_cap
)
844 sel
->r
= vivid_max_rect
;
846 case V4L2_SEL_TGT_COMPOSE
:
847 if (!dev
->has_compose_cap
)
849 sel
->r
= dev
->compose_cap
;
851 case V4L2_SEL_TGT_COMPOSE_DEFAULT
:
852 if (!dev
->has_compose_cap
)
854 sel
->r
= dev
->fmt_cap_rect
;
862 int vivid_vid_cap_s_selection(struct file
*file
, void *fh
, struct v4l2_selection
*s
)
864 struct vivid_dev
*dev
= video_drvdata(file
);
865 struct v4l2_rect
*crop
= &dev
->crop_cap
;
866 struct v4l2_rect
*compose
= &dev
->compose_cap
;
867 unsigned factor
= V4L2_FIELD_HAS_T_OR_B(dev
->field_cap
) ? 2 : 1;
870 if (!dev
->has_crop_cap
&& !dev
->has_compose_cap
)
872 if (s
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
874 if (vivid_is_webcam(dev
))
878 case V4L2_SEL_TGT_CROP
:
879 if (!dev
->has_crop_cap
)
881 ret
= vivid_vid_adjust_sel(s
->flags
, &s
->r
);
884 v4l2_rect_set_min_size(&s
->r
, &vivid_min_rect
);
885 v4l2_rect_set_max_size(&s
->r
, &dev
->src_rect
);
886 v4l2_rect_map_inside(&s
->r
, &dev
->crop_bounds_cap
);
888 s
->r
.height
/= factor
;
889 if (dev
->has_scaler_cap
) {
890 struct v4l2_rect fmt
= dev
->fmt_cap_rect
;
891 struct v4l2_rect max_rect
= {
893 s
->r
.width
* MAX_ZOOM
,
894 s
->r
.height
* MAX_ZOOM
896 struct v4l2_rect min_rect
= {
898 s
->r
.width
/ MAX_ZOOM
,
899 s
->r
.height
/ MAX_ZOOM
902 v4l2_rect_set_min_size(&fmt
, &min_rect
);
903 if (!dev
->has_compose_cap
)
904 v4l2_rect_set_max_size(&fmt
, &max_rect
);
905 if (!v4l2_rect_same_size(&dev
->fmt_cap_rect
, &fmt
) &&
906 vb2_is_busy(&dev
->vb_vid_cap_q
))
908 if (dev
->has_compose_cap
) {
909 v4l2_rect_set_min_size(compose
, &min_rect
);
910 v4l2_rect_set_max_size(compose
, &max_rect
);
912 dev
->fmt_cap_rect
= fmt
;
913 tpg_s_buf_height(&dev
->tpg
, fmt
.height
);
914 } else if (dev
->has_compose_cap
) {
915 struct v4l2_rect fmt
= dev
->fmt_cap_rect
;
917 v4l2_rect_set_min_size(&fmt
, &s
->r
);
918 if (!v4l2_rect_same_size(&dev
->fmt_cap_rect
, &fmt
) &&
919 vb2_is_busy(&dev
->vb_vid_cap_q
))
921 dev
->fmt_cap_rect
= fmt
;
922 tpg_s_buf_height(&dev
->tpg
, fmt
.height
);
923 v4l2_rect_set_size_to(compose
, &s
->r
);
924 v4l2_rect_map_inside(compose
, &dev
->fmt_cap_rect
);
926 if (!v4l2_rect_same_size(&s
->r
, &dev
->fmt_cap_rect
) &&
927 vb2_is_busy(&dev
->vb_vid_cap_q
))
929 v4l2_rect_set_size_to(&dev
->fmt_cap_rect
, &s
->r
);
930 v4l2_rect_set_size_to(compose
, &s
->r
);
931 v4l2_rect_map_inside(compose
, &dev
->fmt_cap_rect
);
932 tpg_s_buf_height(&dev
->tpg
, dev
->fmt_cap_rect
.height
);
935 s
->r
.height
*= factor
;
938 case V4L2_SEL_TGT_COMPOSE
:
939 if (!dev
->has_compose_cap
)
941 ret
= vivid_vid_adjust_sel(s
->flags
, &s
->r
);
944 v4l2_rect_set_min_size(&s
->r
, &vivid_min_rect
);
945 v4l2_rect_set_max_size(&s
->r
, &dev
->fmt_cap_rect
);
946 if (dev
->has_scaler_cap
) {
947 struct v4l2_rect max_rect
= {
949 dev
->src_rect
.width
* MAX_ZOOM
,
950 (dev
->src_rect
.height
/ factor
) * MAX_ZOOM
953 v4l2_rect_set_max_size(&s
->r
, &max_rect
);
954 if (dev
->has_crop_cap
) {
955 struct v4l2_rect min_rect
= {
957 s
->r
.width
/ MAX_ZOOM
,
958 (s
->r
.height
* factor
) / MAX_ZOOM
960 struct v4l2_rect max_rect
= {
962 s
->r
.width
* MAX_ZOOM
,
963 (s
->r
.height
* factor
) * MAX_ZOOM
966 v4l2_rect_set_min_size(crop
, &min_rect
);
967 v4l2_rect_set_max_size(crop
, &max_rect
);
968 v4l2_rect_map_inside(crop
, &dev
->crop_bounds_cap
);
970 } else if (dev
->has_crop_cap
) {
972 s
->r
.height
*= factor
;
973 v4l2_rect_set_max_size(&s
->r
, &dev
->src_rect
);
974 v4l2_rect_set_size_to(crop
, &s
->r
);
975 v4l2_rect_map_inside(crop
, &dev
->crop_bounds_cap
);
977 s
->r
.height
/= factor
;
979 v4l2_rect_set_size_to(&s
->r
, &dev
->src_rect
);
980 s
->r
.height
/= factor
;
982 v4l2_rect_map_inside(&s
->r
, &dev
->fmt_cap_rect
);
983 if (dev
->bitmap_cap
&& (compose
->width
!= s
->r
.width
||
984 compose
->height
!= s
->r
.height
)) {
985 kfree(dev
->bitmap_cap
);
986 dev
->bitmap_cap
= NULL
;
994 tpg_s_crop_compose(&dev
->tpg
, crop
, compose
);
998 int vivid_vid_cap_cropcap(struct file
*file
, void *priv
,
999 struct v4l2_cropcap
*cap
)
1001 struct vivid_dev
*dev
= video_drvdata(file
);
1003 if (cap
->type
!= V4L2_BUF_TYPE_VIDEO_CAPTURE
)
1006 switch (vivid_get_pixel_aspect(dev
)) {
1007 case TPG_PIXEL_ASPECT_NTSC
:
1008 cap
->pixelaspect
.numerator
= 11;
1009 cap
->pixelaspect
.denominator
= 10;
1011 case TPG_PIXEL_ASPECT_PAL
:
1012 cap
->pixelaspect
.numerator
= 54;
1013 cap
->pixelaspect
.denominator
= 59;
1015 case TPG_PIXEL_ASPECT_SQUARE
:
1016 cap
->pixelaspect
.numerator
= 1;
1017 cap
->pixelaspect
.denominator
= 1;
1023 int vidioc_enum_fmt_vid_overlay(struct file
*file
, void *priv
,
1024 struct v4l2_fmtdesc
*f
)
1026 struct vivid_dev
*dev
= video_drvdata(file
);
1027 const struct vivid_fmt
*fmt
;
1029 if (dev
->multiplanar
)
1032 if (f
->index
>= ARRAY_SIZE(formats_ovl
))
1035 fmt
= &formats_ovl
[f
->index
];
1037 f
->pixelformat
= fmt
->fourcc
;
1041 int vidioc_g_fmt_vid_overlay(struct file
*file
, void *priv
,
1042 struct v4l2_format
*f
)
1044 struct vivid_dev
*dev
= video_drvdata(file
);
1045 const struct v4l2_rect
*compose
= &dev
->compose_cap
;
1046 struct v4l2_window
*win
= &f
->fmt
.win
;
1047 unsigned clipcount
= win
->clipcount
;
1049 if (dev
->multiplanar
)
1052 win
->w
.top
= dev
->overlay_cap_top
;
1053 win
->w
.left
= dev
->overlay_cap_left
;
1054 win
->w
.width
= compose
->width
;
1055 win
->w
.height
= compose
->height
;
1056 win
->field
= dev
->overlay_cap_field
;
1057 win
->clipcount
= dev
->clipcount_cap
;
1058 if (clipcount
> dev
->clipcount_cap
)
1059 clipcount
= dev
->clipcount_cap
;
1060 if (dev
->bitmap_cap
== NULL
)
1062 else if (win
->bitmap
) {
1063 if (copy_to_user(win
->bitmap
, dev
->bitmap_cap
,
1064 ((compose
->width
+ 7) / 8) * compose
->height
))
1067 if (clipcount
&& win
->clips
) {
1068 if (copy_to_user(win
->clips
, dev
->clips_cap
,
1069 clipcount
* sizeof(dev
->clips_cap
[0])))
1075 int vidioc_try_fmt_vid_overlay(struct file
*file
, void *priv
,
1076 struct v4l2_format
*f
)
1078 struct vivid_dev
*dev
= video_drvdata(file
);
1079 const struct v4l2_rect
*compose
= &dev
->compose_cap
;
1080 struct v4l2_window
*win
= &f
->fmt
.win
;
1083 if (dev
->multiplanar
)
1086 win
->w
.left
= clamp_t(int, win
->w
.left
,
1087 -dev
->fb_cap
.fmt
.width
, dev
->fb_cap
.fmt
.width
);
1088 win
->w
.top
= clamp_t(int, win
->w
.top
,
1089 -dev
->fb_cap
.fmt
.height
, dev
->fb_cap
.fmt
.height
);
1090 win
->w
.width
= compose
->width
;
1091 win
->w
.height
= compose
->height
;
1092 if (win
->field
!= V4L2_FIELD_BOTTOM
&& win
->field
!= V4L2_FIELD_TOP
)
1093 win
->field
= V4L2_FIELD_ANY
;
1095 win
->global_alpha
= 0;
1096 if (win
->clipcount
&& !win
->clips
)
1098 if (win
->clipcount
> MAX_CLIPS
)
1099 win
->clipcount
= MAX_CLIPS
;
1100 if (win
->clipcount
) {
1101 if (copy_from_user(dev
->try_clips_cap
, win
->clips
,
1102 win
->clipcount
* sizeof(dev
->clips_cap
[0])))
1104 for (i
= 0; i
< win
->clipcount
; i
++) {
1105 struct v4l2_rect
*r
= &dev
->try_clips_cap
[i
].c
;
1107 r
->top
= clamp_t(s32
, r
->top
, 0, dev
->fb_cap
.fmt
.height
- 1);
1108 r
->height
= clamp_t(s32
, r
->height
, 1, dev
->fb_cap
.fmt
.height
- r
->top
);
1109 r
->left
= clamp_t(u32
, r
->left
, 0, dev
->fb_cap
.fmt
.width
- 1);
1110 r
->width
= clamp_t(u32
, r
->width
, 1, dev
->fb_cap
.fmt
.width
- r
->left
);
1113 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1114 * number and it's typically a one-time deal.
1116 for (i
= 0; i
< win
->clipcount
- 1; i
++) {
1117 struct v4l2_rect
*r1
= &dev
->try_clips_cap
[i
].c
;
1119 for (j
= i
+ 1; j
< win
->clipcount
; j
++) {
1120 struct v4l2_rect
*r2
= &dev
->try_clips_cap
[j
].c
;
1122 if (v4l2_rect_overlap(r1
, r2
))
1126 if (copy_to_user(win
->clips
, dev
->try_clips_cap
,
1127 win
->clipcount
* sizeof(dev
->clips_cap
[0])))
1133 int vidioc_s_fmt_vid_overlay(struct file
*file
, void *priv
,
1134 struct v4l2_format
*f
)
1136 struct vivid_dev
*dev
= video_drvdata(file
);
1137 const struct v4l2_rect
*compose
= &dev
->compose_cap
;
1138 struct v4l2_window
*win
= &f
->fmt
.win
;
1139 int ret
= vidioc_try_fmt_vid_overlay(file
, priv
, f
);
1140 unsigned bitmap_size
= ((compose
->width
+ 7) / 8) * compose
->height
;
1141 unsigned clips_size
= win
->clipcount
* sizeof(dev
->clips_cap
[0]);
1142 void *new_bitmap
= NULL
;
1148 new_bitmap
= vzalloc(bitmap_size
);
1150 if (new_bitmap
== NULL
)
1152 if (copy_from_user(new_bitmap
, win
->bitmap
, bitmap_size
)) {
1158 dev
->overlay_cap_top
= win
->w
.top
;
1159 dev
->overlay_cap_left
= win
->w
.left
;
1160 dev
->overlay_cap_field
= win
->field
;
1161 vfree(dev
->bitmap_cap
);
1162 dev
->bitmap_cap
= new_bitmap
;
1163 dev
->clipcount_cap
= win
->clipcount
;
1164 if (dev
->clipcount_cap
)
1165 memcpy(dev
->clips_cap
, dev
->try_clips_cap
, clips_size
);
1169 int vivid_vid_cap_overlay(struct file
*file
, void *fh
, unsigned i
)
1171 struct vivid_dev
*dev
= video_drvdata(file
);
1173 if (dev
->multiplanar
)
1176 if (i
&& dev
->fb_vbase_cap
== NULL
)
1179 if (i
&& dev
->fb_cap
.fmt
.pixelformat
!= dev
->fmt_cap
->fourcc
) {
1180 dprintk(dev
, 1, "mismatch between overlay and video capture pixelformats\n");
1184 if (dev
->overlay_cap_owner
&& dev
->overlay_cap_owner
!= fh
)
1186 dev
->overlay_cap_owner
= i
? fh
: NULL
;
1190 int vivid_vid_cap_g_fbuf(struct file
*file
, void *fh
,
1191 struct v4l2_framebuffer
*a
)
1193 struct vivid_dev
*dev
= video_drvdata(file
);
1195 if (dev
->multiplanar
)
1199 a
->capability
= V4L2_FBUF_CAP_BITMAP_CLIPPING
|
1200 V4L2_FBUF_CAP_LIST_CLIPPING
;
1201 a
->flags
= V4L2_FBUF_FLAG_PRIMARY
;
1202 a
->fmt
.field
= V4L2_FIELD_NONE
;
1203 a
->fmt
.colorspace
= V4L2_COLORSPACE_SRGB
;
1208 int vivid_vid_cap_s_fbuf(struct file
*file
, void *fh
,
1209 const struct v4l2_framebuffer
*a
)
1211 struct vivid_dev
*dev
= video_drvdata(file
);
1212 const struct vivid_fmt
*fmt
;
1214 if (dev
->multiplanar
)
1217 if (!capable(CAP_SYS_ADMIN
) && !capable(CAP_SYS_RAWIO
))
1220 if (dev
->overlay_cap_owner
)
1223 if (a
->base
== NULL
) {
1224 dev
->fb_cap
.base
= NULL
;
1225 dev
->fb_vbase_cap
= NULL
;
1229 if (a
->fmt
.width
< 48 || a
->fmt
.height
< 32)
1231 fmt
= vivid_get_format(dev
, a
->fmt
.pixelformat
);
1232 if (!fmt
|| !fmt
->can_do_overlay
)
1234 if (a
->fmt
.bytesperline
< (a
->fmt
.width
* fmt
->bit_depth
[0]) / 8)
1236 if (a
->fmt
.height
* a
->fmt
.bytesperline
< a
->fmt
.sizeimage
)
1239 dev
->fb_vbase_cap
= phys_to_virt((unsigned long)a
->base
);
1241 dev
->overlay_cap_left
= clamp_t(int, dev
->overlay_cap_left
,
1242 -dev
->fb_cap
.fmt
.width
, dev
->fb_cap
.fmt
.width
);
1243 dev
->overlay_cap_top
= clamp_t(int, dev
->overlay_cap_top
,
1244 -dev
->fb_cap
.fmt
.height
, dev
->fb_cap
.fmt
.height
);
1248 static const struct v4l2_audio vivid_audio_inputs
[] = {
1249 { 0, "TV", V4L2_AUDCAP_STEREO
},
1250 { 1, "Line-In", V4L2_AUDCAP_STEREO
},
1253 int vidioc_enum_input(struct file
*file
, void *priv
,
1254 struct v4l2_input
*inp
)
1256 struct vivid_dev
*dev
= video_drvdata(file
);
1258 if (inp
->index
>= dev
->num_inputs
)
1261 inp
->type
= V4L2_INPUT_TYPE_CAMERA
;
1262 switch (dev
->input_type
[inp
->index
]) {
1264 snprintf(inp
->name
, sizeof(inp
->name
), "Webcam %u",
1265 dev
->input_name_counter
[inp
->index
]);
1266 inp
->capabilities
= 0;
1269 snprintf(inp
->name
, sizeof(inp
->name
), "TV %u",
1270 dev
->input_name_counter
[inp
->index
]);
1271 inp
->type
= V4L2_INPUT_TYPE_TUNER
;
1272 inp
->std
= V4L2_STD_ALL
;
1273 if (dev
->has_audio_inputs
)
1274 inp
->audioset
= (1 << ARRAY_SIZE(vivid_audio_inputs
)) - 1;
1275 inp
->capabilities
= V4L2_IN_CAP_STD
;
1278 snprintf(inp
->name
, sizeof(inp
->name
), "S-Video %u",
1279 dev
->input_name_counter
[inp
->index
]);
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
), "HDMI %u",
1287 dev
->input_name_counter
[inp
->index
]);
1288 inp
->capabilities
= V4L2_IN_CAP_DV_TIMINGS
;
1289 if (dev
->edid_blocks
== 0 ||
1290 dev
->dv_timings_signal_mode
== NO_SIGNAL
)
1291 inp
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1292 else if (dev
->dv_timings_signal_mode
== NO_LOCK
||
1293 dev
->dv_timings_signal_mode
== OUT_OF_RANGE
)
1294 inp
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1297 if (dev
->sensor_hflip
)
1298 inp
->status
|= V4L2_IN_ST_HFLIP
;
1299 if (dev
->sensor_vflip
)
1300 inp
->status
|= V4L2_IN_ST_VFLIP
;
1301 if (dev
->input
== inp
->index
&& vivid_is_sdtv_cap(dev
)) {
1302 if (dev
->std_signal_mode
== NO_SIGNAL
) {
1303 inp
->status
|= V4L2_IN_ST_NO_SIGNAL
;
1304 } else if (dev
->std_signal_mode
== NO_LOCK
) {
1305 inp
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1306 } else if (vivid_is_tv_cap(dev
)) {
1307 switch (tpg_g_quality(&dev
->tpg
)) {
1309 inp
->status
|= V4L2_IN_ST_COLOR_KILL
;
1311 case TPG_QUAL_NOISE
:
1312 inp
->status
|= V4L2_IN_ST_NO_H_LOCK
;
1322 int vidioc_g_input(struct file
*file
, void *priv
, unsigned *i
)
1324 struct vivid_dev
*dev
= video_drvdata(file
);
1330 int vidioc_s_input(struct file
*file
, void *priv
, unsigned i
)
1332 struct vivid_dev
*dev
= video_drvdata(file
);
1333 struct v4l2_bt_timings
*bt
= &dev
->dv_timings_cap
.bt
;
1334 unsigned brightness
;
1336 if (i
>= dev
->num_inputs
)
1339 if (i
== dev
->input
)
1342 if (vb2_is_busy(&dev
->vb_vid_cap_q
) || vb2_is_busy(&dev
->vb_vbi_cap_q
))
1346 dev
->vid_cap_dev
.tvnorms
= 0;
1347 if (dev
->input_type
[i
] == TV
|| dev
->input_type
[i
] == SVID
) {
1348 dev
->tv_audio_input
= (dev
->input_type
[i
] == TV
) ? 0 : 1;
1349 dev
->vid_cap_dev
.tvnorms
= V4L2_STD_ALL
;
1351 dev
->vbi_cap_dev
.tvnorms
= dev
->vid_cap_dev
.tvnorms
;
1352 vivid_update_format_cap(dev
, false);
1354 if (dev
->colorspace
) {
1355 switch (dev
->input_type
[i
]) {
1357 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_SRGB
);
1361 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_170M
);
1364 if (bt
->flags
& V4L2_DV_FL_IS_CE_VIDEO
) {
1365 if (dev
->src_rect
.width
== 720 && dev
->src_rect
.height
<= 576)
1366 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_170M
);
1368 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_709
);
1370 v4l2_ctrl_s_ctrl(dev
->colorspace
, VIVID_CS_SRGB
);
1377 * Modify the brightness range depending on the input.
1378 * This makes it easy to use vivid to test if applications can
1379 * handle control range modifications and is also how this is
1380 * typically used in practice as different inputs may be hooked
1381 * up to different receivers with different control ranges.
1383 brightness
= 128 * i
+ dev
->input_brightness
[i
];
1384 v4l2_ctrl_modify_range(dev
->brightness
,
1385 128 * i
, 255 + 128 * i
, 1, 128 + 128 * i
);
1386 v4l2_ctrl_s_ctrl(dev
->brightness
, brightness
);
1390 int vidioc_enumaudio(struct file
*file
, void *fh
, struct v4l2_audio
*vin
)
1392 if (vin
->index
>= ARRAY_SIZE(vivid_audio_inputs
))
1394 *vin
= vivid_audio_inputs
[vin
->index
];
1398 int vidioc_g_audio(struct file
*file
, void *fh
, struct v4l2_audio
*vin
)
1400 struct vivid_dev
*dev
= video_drvdata(file
);
1402 if (!vivid_is_sdtv_cap(dev
))
1404 *vin
= vivid_audio_inputs
[dev
->tv_audio_input
];
1408 int vidioc_s_audio(struct file
*file
, void *fh
, const struct v4l2_audio
*vin
)
1410 struct vivid_dev
*dev
= video_drvdata(file
);
1412 if (!vivid_is_sdtv_cap(dev
))
1414 if (vin
->index
>= ARRAY_SIZE(vivid_audio_inputs
))
1416 dev
->tv_audio_input
= vin
->index
;
1420 int vivid_video_g_frequency(struct file
*file
, void *fh
, struct v4l2_frequency
*vf
)
1422 struct vivid_dev
*dev
= video_drvdata(file
);
1426 vf
->frequency
= dev
->tv_freq
;
1430 int vivid_video_s_frequency(struct file
*file
, void *fh
, const struct v4l2_frequency
*vf
)
1432 struct vivid_dev
*dev
= video_drvdata(file
);
1436 dev
->tv_freq
= clamp_t(unsigned, vf
->frequency
, MIN_TV_FREQ
, MAX_TV_FREQ
);
1437 if (vivid_is_tv_cap(dev
))
1438 vivid_update_quality(dev
);
1442 int vivid_video_s_tuner(struct file
*file
, void *fh
, const struct v4l2_tuner
*vt
)
1444 struct vivid_dev
*dev
= video_drvdata(file
);
1448 if (vt
->audmode
> V4L2_TUNER_MODE_LANG1_LANG2
)
1450 dev
->tv_audmode
= vt
->audmode
;
1454 int vivid_video_g_tuner(struct file
*file
, void *fh
, struct v4l2_tuner
*vt
)
1456 struct vivid_dev
*dev
= video_drvdata(file
);
1457 enum tpg_quality qual
;
1462 vt
->capability
= V4L2_TUNER_CAP_NORM
| V4L2_TUNER_CAP_STEREO
|
1463 V4L2_TUNER_CAP_LANG1
| V4L2_TUNER_CAP_LANG2
;
1464 vt
->audmode
= dev
->tv_audmode
;
1465 vt
->rangelow
= MIN_TV_FREQ
;
1466 vt
->rangehigh
= MAX_TV_FREQ
;
1467 qual
= vivid_get_quality(dev
, &vt
->afc
);
1468 if (qual
== TPG_QUAL_COLOR
)
1469 vt
->signal
= 0xffff;
1470 else if (qual
== TPG_QUAL_GRAY
)
1471 vt
->signal
= 0x8000;
1474 if (qual
== TPG_QUAL_NOISE
) {
1476 } else if (qual
== TPG_QUAL_GRAY
) {
1477 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1479 unsigned channel_nr
= dev
->tv_freq
/ (6 * 16);
1480 unsigned options
= (dev
->std_cap
& V4L2_STD_NTSC_M
) ? 4 : 3;
1482 switch (channel_nr
% options
) {
1484 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
;
1487 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
;
1490 if (dev
->std_cap
& V4L2_STD_NTSC_M
)
1491 vt
->rxsubchans
= V4L2_TUNER_SUB_MONO
| V4L2_TUNER_SUB_SAP
;
1493 vt
->rxsubchans
= V4L2_TUNER_SUB_LANG1
| V4L2_TUNER_SUB_LANG2
;
1496 vt
->rxsubchans
= V4L2_TUNER_SUB_STEREO
| V4L2_TUNER_SUB_SAP
;
1500 strlcpy(vt
->name
, "TV Tuner", sizeof(vt
->name
));
1504 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1505 const v4l2_std_id vivid_standard
[] = {
1510 V4L2_STD_PAL_BG
| V4L2_STD_PAL_H
,
1517 V4L2_STD_SECAM_B
| V4L2_STD_SECAM_G
| V4L2_STD_SECAM_H
,
1524 /* Must remain in sync with the vivid_standard array */
1525 const char * const vivid_ctrl_standard_strings
[] = {
1544 int vidioc_querystd(struct file
*file
, void *priv
, v4l2_std_id
*id
)
1546 struct vivid_dev
*dev
= video_drvdata(file
);
1548 if (!vivid_is_sdtv_cap(dev
))
1550 if (dev
->std_signal_mode
== NO_SIGNAL
||
1551 dev
->std_signal_mode
== NO_LOCK
) {
1552 *id
= V4L2_STD_UNKNOWN
;
1555 if (vivid_is_tv_cap(dev
) && tpg_g_quality(&dev
->tpg
) == TPG_QUAL_NOISE
) {
1556 *id
= V4L2_STD_UNKNOWN
;
1557 } else if (dev
->std_signal_mode
== CURRENT_STD
) {
1559 } else if (dev
->std_signal_mode
== SELECTED_STD
) {
1560 *id
= dev
->query_std
;
1562 *id
= vivid_standard
[dev
->query_std_last
];
1563 dev
->query_std_last
= (dev
->query_std_last
+ 1) % ARRAY_SIZE(vivid_standard
);
1569 int vivid_vid_cap_s_std(struct file
*file
, void *priv
, v4l2_std_id id
)
1571 struct vivid_dev
*dev
= video_drvdata(file
);
1573 if (!vivid_is_sdtv_cap(dev
))
1575 if (dev
->std_cap
== id
)
1577 if (vb2_is_busy(&dev
->vb_vid_cap_q
) || vb2_is_busy(&dev
->vb_vbi_cap_q
))
1580 vivid_update_format_cap(dev
, false);
1584 static void find_aspect_ratio(u32 width
, u32 height
,
1585 u32
*num
, u32
*denom
)
1587 if (!(height
% 3) && ((height
* 4 / 3) == width
)) {
1590 } else if (!(height
% 9) && ((height
* 16 / 9) == width
)) {
1593 } else if (!(height
% 10) && ((height
* 16 / 10) == width
)) {
1596 } else if (!(height
% 4) && ((height
* 5 / 4) == width
)) {
1599 } else if (!(height
% 9) && ((height
* 15 / 9) == width
)) {
1602 } else { /* default to 16:9 */
1608 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings
*timings
)
1610 struct v4l2_bt_timings
*bt
= &timings
->bt
;
1615 if (!v4l2_valid_dv_timings(timings
, &vivid_dv_timings_cap
,
1619 total_h_pixel
= V4L2_DV_BT_FRAME_WIDTH(bt
);
1620 total_v_lines
= V4L2_DV_BT_FRAME_HEIGHT(bt
);
1622 h_freq
= (u32
)bt
->pixelclock
/ total_h_pixel
;
1624 if (bt
->standards
== 0 || (bt
->standards
& V4L2_DV_BT_STD_CVT
)) {
1625 if (v4l2_detect_cvt(total_v_lines
, h_freq
, bt
->vsync
, bt
->width
,
1626 bt
->polarities
, bt
->interlaced
, timings
))
1630 if (bt
->standards
== 0 || (bt
->standards
& V4L2_DV_BT_STD_GTF
)) {
1631 struct v4l2_fract aspect_ratio
;
1633 find_aspect_ratio(bt
->width
, bt
->height
,
1634 &aspect_ratio
.numerator
,
1635 &aspect_ratio
.denominator
);
1636 if (v4l2_detect_gtf(total_v_lines
, h_freq
, bt
->vsync
,
1637 bt
->polarities
, bt
->interlaced
,
1638 aspect_ratio
, timings
))
1644 int vivid_vid_cap_s_dv_timings(struct file
*file
, void *_fh
,
1645 struct v4l2_dv_timings
*timings
)
1647 struct vivid_dev
*dev
= video_drvdata(file
);
1649 if (!vivid_is_hdmi_cap(dev
))
1651 if (!v4l2_find_dv_timings_cap(timings
, &vivid_dv_timings_cap
,
1653 !valid_cvt_gtf_timings(timings
))
1656 if (v4l2_match_dv_timings(timings
, &dev
->dv_timings_cap
, 0, false))
1658 if (vb2_is_busy(&dev
->vb_vid_cap_q
))
1661 dev
->dv_timings_cap
= *timings
;
1662 vivid_update_format_cap(dev
, false);
1666 int vidioc_query_dv_timings(struct file
*file
, void *_fh
,
1667 struct v4l2_dv_timings
*timings
)
1669 struct vivid_dev
*dev
= video_drvdata(file
);
1671 if (!vivid_is_hdmi_cap(dev
))
1673 if (dev
->dv_timings_signal_mode
== NO_SIGNAL
||
1674 dev
->edid_blocks
== 0)
1676 if (dev
->dv_timings_signal_mode
== NO_LOCK
)
1678 if (dev
->dv_timings_signal_mode
== OUT_OF_RANGE
) {
1679 timings
->bt
.pixelclock
= vivid_dv_timings_cap
.bt
.max_pixelclock
* 2;
1682 if (dev
->dv_timings_signal_mode
== CURRENT_DV_TIMINGS
) {
1683 *timings
= dev
->dv_timings_cap
;
1684 } else if (dev
->dv_timings_signal_mode
== SELECTED_DV_TIMINGS
) {
1685 *timings
= v4l2_dv_timings_presets
[dev
->query_dv_timings
];
1687 *timings
= v4l2_dv_timings_presets
[dev
->query_dv_timings_last
];
1688 dev
->query_dv_timings_last
= (dev
->query_dv_timings_last
+ 1) %
1689 dev
->query_dv_timings_size
;
1694 int vidioc_s_edid(struct file
*file
, void *_fh
,
1695 struct v4l2_edid
*edid
)
1697 struct vivid_dev
*dev
= video_drvdata(file
);
1702 memset(edid
->reserved
, 0, sizeof(edid
->reserved
));
1703 if (edid
->pad
>= dev
->num_inputs
)
1705 if (dev
->input_type
[edid
->pad
] != HDMI
|| edid
->start_block
)
1707 if (edid
->blocks
== 0) {
1708 dev
->edid_blocks
= 0;
1709 phys_addr
= CEC_PHYS_ADDR_INVALID
;
1712 if (edid
->blocks
> dev
->edid_max_blocks
) {
1713 edid
->blocks
= dev
->edid_max_blocks
;
1716 phys_addr
= cec_get_edid_phys_addr(edid
->edid
, edid
->blocks
* 128, NULL
);
1717 ret
= cec_phys_addr_validate(phys_addr
, &phys_addr
, NULL
);
1721 if (vb2_is_busy(&dev
->vb_vid_cap_q
))
1724 dev
->edid_blocks
= edid
->blocks
;
1725 memcpy(dev
->edid
, edid
->edid
, edid
->blocks
* 128);
1728 /* TODO: a proper hotplug detect cycle should be emulated here */
1729 cec_s_phys_addr(dev
->cec_rx_adap
, phys_addr
, false);
1731 for (i
= 0; i
< MAX_OUTPUTS
&& dev
->cec_tx_adap
[i
]; i
++)
1732 cec_s_phys_addr(dev
->cec_tx_adap
[i
],
1733 cec_phys_addr_for_input(phys_addr
, i
+ 1),
1738 int vidioc_enum_framesizes(struct file
*file
, void *fh
,
1739 struct v4l2_frmsizeenum
*fsize
)
1741 struct vivid_dev
*dev
= video_drvdata(file
);
1743 if (!vivid_is_webcam(dev
) && !dev
->has_scaler_cap
)
1745 if (vivid_get_format(dev
, fsize
->pixel_format
) == NULL
)
1747 if (vivid_is_webcam(dev
)) {
1748 if (fsize
->index
>= ARRAY_SIZE(webcam_sizes
))
1750 fsize
->type
= V4L2_FRMSIZE_TYPE_DISCRETE
;
1751 fsize
->discrete
= webcam_sizes
[fsize
->index
];
1756 fsize
->type
= V4L2_FRMSIZE_TYPE_STEPWISE
;
1757 fsize
->stepwise
.min_width
= MIN_WIDTH
;
1758 fsize
->stepwise
.max_width
= MAX_WIDTH
* MAX_ZOOM
;
1759 fsize
->stepwise
.step_width
= 2;
1760 fsize
->stepwise
.min_height
= MIN_HEIGHT
;
1761 fsize
->stepwise
.max_height
= MAX_HEIGHT
* MAX_ZOOM
;
1762 fsize
->stepwise
.step_height
= 2;
1766 /* timeperframe is arbitrary and continuous */
1767 int vidioc_enum_frameintervals(struct file
*file
, void *priv
,
1768 struct v4l2_frmivalenum
*fival
)
1770 struct vivid_dev
*dev
= video_drvdata(file
);
1771 const struct vivid_fmt
*fmt
;
1774 fmt
= vivid_get_format(dev
, fival
->pixel_format
);
1778 if (!vivid_is_webcam(dev
)) {
1781 if (fival
->width
< MIN_WIDTH
|| fival
->width
> MAX_WIDTH
* MAX_ZOOM
)
1783 if (fival
->height
< MIN_HEIGHT
|| fival
->height
> MAX_HEIGHT
* MAX_ZOOM
)
1785 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1786 fival
->discrete
= dev
->timeperframe_vid_cap
;
1790 for (i
= 0; i
< ARRAY_SIZE(webcam_sizes
); i
++)
1791 if (fival
->width
== webcam_sizes
[i
].width
&&
1792 fival
->height
== webcam_sizes
[i
].height
)
1794 if (i
== ARRAY_SIZE(webcam_sizes
))
1796 if (fival
->index
>= 2 * (VIVID_WEBCAM_SIZES
- i
))
1798 fival
->type
= V4L2_FRMIVAL_TYPE_DISCRETE
;
1799 fival
->discrete
= webcam_intervals
[fival
->index
];
1803 int vivid_vid_cap_g_parm(struct file
*file
, void *priv
,
1804 struct v4l2_streamparm
*parm
)
1806 struct vivid_dev
*dev
= video_drvdata(file
);
1808 if (parm
->type
!= (dev
->multiplanar
?
1809 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
1810 V4L2_BUF_TYPE_VIDEO_CAPTURE
))
1813 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1814 parm
->parm
.capture
.timeperframe
= dev
->timeperframe_vid_cap
;
1815 parm
->parm
.capture
.readbuffers
= 1;
1819 #define FRACT_CMP(a, OP, b) \
1820 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1822 int vivid_vid_cap_s_parm(struct file
*file
, void *priv
,
1823 struct v4l2_streamparm
*parm
)
1825 struct vivid_dev
*dev
= video_drvdata(file
);
1826 unsigned ival_sz
= 2 * (VIVID_WEBCAM_SIZES
- dev
->webcam_size_idx
);
1827 struct v4l2_fract tpf
;
1830 if (parm
->type
!= (dev
->multiplanar
?
1831 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE
:
1832 V4L2_BUF_TYPE_VIDEO_CAPTURE
))
1834 if (!vivid_is_webcam(dev
))
1835 return vivid_vid_cap_g_parm(file
, priv
, parm
);
1837 tpf
= parm
->parm
.capture
.timeperframe
;
1839 if (tpf
.denominator
== 0)
1840 tpf
= webcam_intervals
[ival_sz
- 1];
1841 for (i
= 0; i
< ival_sz
; i
++)
1842 if (FRACT_CMP(tpf
, >=, webcam_intervals
[i
]))
1846 dev
->webcam_ival_idx
= i
;
1847 tpf
= webcam_intervals
[dev
->webcam_ival_idx
];
1848 tpf
= FRACT_CMP(tpf
, <, tpf_min
) ? tpf_min
: tpf
;
1849 tpf
= FRACT_CMP(tpf
, >, tpf_max
) ? tpf_max
: tpf
;
1851 /* resync the thread's timings */
1852 dev
->cap_seq_resync
= true;
1853 dev
->timeperframe_vid_cap
= tpf
;
1854 parm
->parm
.capture
.capability
= V4L2_CAP_TIMEPERFRAME
;
1855 parm
->parm
.capture
.timeperframe
= tpf
;
1856 parm
->parm
.capture
.readbuffers
= 1;