Merge tag 'for_linus' of git://git.kernel.org/pub/scm/linux/kernel/git/mst/vhost
[cris-mirror.git] / drivers / media / platform / vivid / vivid-vid-cap.c
blob0fbbcde19f0d3a619f30f481a827f28082b93ce3
1 /*
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
17 * SOFTWARE.
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 },
45 .bit_depth = { 16 },
46 .planes = 1,
47 .buffers = 1,
50 .fourcc = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
51 .vdownsampling = { 1 },
52 .bit_depth = { 16 },
53 .planes = 1,
54 .buffers = 1,
57 .fourcc = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
58 .vdownsampling = { 1 },
59 .bit_depth = { 16 },
60 .planes = 1,
61 .buffers = 1,
65 /* The number of discrete webcam framesizes */
66 #define VIVID_WEBCAM_SIZES 5
67 /* The number of discrete webcam frameintervals */
68 #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
70 /* Sizes must be in increasing order */
71 static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
72 { 320, 180 },
73 { 640, 360 },
74 { 1280, 720 },
75 { 1920, 1080 },
76 { 3840, 2160 },
80 * Intervals must be in increasing order and there must be twice as many
81 * elements in this array as there are in webcam_sizes.
83 static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
84 { 1, 1 },
85 { 1, 2 },
86 { 1, 4 },
87 { 1, 5 },
88 { 1, 10 },
89 { 1, 15 },
90 { 1, 25 },
91 { 1, 30 },
92 { 1, 50 },
93 { 1, 60 },
96 static int vid_cap_queue_setup(struct vb2_queue *vq,
97 unsigned *nbuffers, unsigned *nplanes,
98 unsigned sizes[], struct device *alloc_devs[])
100 struct vivid_dev *dev = vb2_get_drv_priv(vq);
101 unsigned buffers = tpg_g_buffers(&dev->tpg);
102 unsigned h = dev->fmt_cap_rect.height;
103 unsigned p;
105 if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
107 * You cannot use read() with FIELD_ALTERNATE since the field
108 * information (TOP/BOTTOM) cannot be passed back to the user.
110 if (vb2_fileio_is_active(vq))
111 return -EINVAL;
114 if (dev->queue_setup_error) {
116 * Error injection: test what happens if queue_setup() returns
117 * an error.
119 dev->queue_setup_error = false;
120 return -EINVAL;
122 if (*nplanes) {
124 * Check if the number of requested planes match
125 * the number of buffers in the current format. You can't mix that.
127 if (*nplanes != buffers)
128 return -EINVAL;
129 for (p = 0; p < buffers; p++) {
130 if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
131 dev->fmt_cap->data_offset[p])
132 return -EINVAL;
134 } else {
135 for (p = 0; p < buffers; p++)
136 sizes[p] = tpg_g_line_width(&dev->tpg, p) * h +
137 dev->fmt_cap->data_offset[p];
140 if (vq->num_buffers + *nbuffers < 2)
141 *nbuffers = 2 - vq->num_buffers;
143 *nplanes = buffers;
145 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
146 for (p = 0; p < buffers; p++)
147 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
149 return 0;
152 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
154 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
155 unsigned long size;
156 unsigned buffers = tpg_g_buffers(&dev->tpg);
157 unsigned p;
159 dprintk(dev, 1, "%s\n", __func__);
161 if (WARN_ON(NULL == dev->fmt_cap))
162 return -EINVAL;
164 if (dev->buf_prepare_error) {
166 * Error injection: test what happens if buf_prepare() returns
167 * an error.
169 dev->buf_prepare_error = false;
170 return -EINVAL;
172 for (p = 0; p < buffers; p++) {
173 size = tpg_g_line_width(&dev->tpg, p) * dev->fmt_cap_rect.height +
174 dev->fmt_cap->data_offset[p];
176 if (vb2_plane_size(vb, p) < size) {
177 dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
178 __func__, p, vb2_plane_size(vb, p), size);
179 return -EINVAL;
182 vb2_set_plane_payload(vb, p, size);
183 vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
186 return 0;
189 static void vid_cap_buf_finish(struct vb2_buffer *vb)
191 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
192 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
193 struct v4l2_timecode *tc = &vbuf->timecode;
194 unsigned fps = 25;
195 unsigned seq = vbuf->sequence;
197 if (!vivid_is_sdtv_cap(dev))
198 return;
201 * Set the timecode. Rarely used, so it is interesting to
202 * test this.
204 vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
205 if (dev->std_cap & V4L2_STD_525_60)
206 fps = 30;
207 tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
208 tc->flags = 0;
209 tc->frames = seq % fps;
210 tc->seconds = (seq / fps) % 60;
211 tc->minutes = (seq / (60 * fps)) % 60;
212 tc->hours = (seq / (60 * 60 * fps)) % 24;
215 static void vid_cap_buf_queue(struct vb2_buffer *vb)
217 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
218 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
219 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
221 dprintk(dev, 1, "%s\n", __func__);
223 spin_lock(&dev->slock);
224 list_add_tail(&buf->list, &dev->vid_cap_active);
225 spin_unlock(&dev->slock);
228 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
230 struct vivid_dev *dev = vb2_get_drv_priv(vq);
231 unsigned i;
232 int err;
234 if (vb2_is_streaming(&dev->vb_vid_out_q))
235 dev->can_loop_video = vivid_vid_can_loop(dev);
237 if (dev->kthread_vid_cap)
238 return 0;
240 dev->vid_cap_seq_count = 0;
241 dprintk(dev, 1, "%s\n", __func__);
242 for (i = 0; i < VIDEO_MAX_FRAME; i++)
243 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
244 if (dev->start_streaming_error) {
245 dev->start_streaming_error = false;
246 err = -EINVAL;
247 } else {
248 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
250 if (err) {
251 struct vivid_buffer *buf, *tmp;
253 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
254 list_del(&buf->list);
255 vb2_buffer_done(&buf->vb.vb2_buf,
256 VB2_BUF_STATE_QUEUED);
259 return err;
262 /* abort streaming and wait for last buffer */
263 static void vid_cap_stop_streaming(struct vb2_queue *vq)
265 struct vivid_dev *dev = vb2_get_drv_priv(vq);
267 dprintk(dev, 1, "%s\n", __func__);
268 vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
269 dev->can_loop_video = false;
272 const struct vb2_ops vivid_vid_cap_qops = {
273 .queue_setup = vid_cap_queue_setup,
274 .buf_prepare = vid_cap_buf_prepare,
275 .buf_finish = vid_cap_buf_finish,
276 .buf_queue = vid_cap_buf_queue,
277 .start_streaming = vid_cap_start_streaming,
278 .stop_streaming = vid_cap_stop_streaming,
279 .wait_prepare = vb2_ops_wait_prepare,
280 .wait_finish = vb2_ops_wait_finish,
284 * Determine the 'picture' quality based on the current TV frequency: either
285 * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
286 * signal or NOISE for no signal.
288 void vivid_update_quality(struct vivid_dev *dev)
290 unsigned freq_modulus;
292 if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
294 * The 'noise' will only be replaced by the actual video
295 * if the output video matches the input video settings.
297 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
298 return;
300 if (vivid_is_hdmi_cap(dev) && VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode)) {
301 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
302 return;
304 if (vivid_is_sdtv_cap(dev) && VIVID_INVALID_SIGNAL(dev->std_signal_mode)) {
305 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
306 return;
308 if (!vivid_is_tv_cap(dev)) {
309 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
310 return;
314 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
315 * From +/- 0.25 MHz around the channel there is color, and from
316 * +/- 1 MHz there is grayscale (chroma is lost).
317 * Everywhere else it is just noise.
319 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
320 if (freq_modulus > 2 * 16) {
321 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
322 next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
323 return;
325 if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
326 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
327 else
328 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
332 * Get the current picture quality and the associated afc value.
334 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
336 unsigned freq_modulus;
338 if (afc)
339 *afc = 0;
340 if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
341 tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
342 return tpg_g_quality(&dev->tpg);
345 * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
346 * From +/- 0.25 MHz around the channel there is color, and from
347 * +/- 1 MHz there is grayscale (chroma is lost).
348 * Everywhere else it is just gray.
350 freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
351 if (afc)
352 *afc = freq_modulus - 1 * 16;
353 return TPG_QUAL_GRAY;
356 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
358 if (vivid_is_sdtv_cap(dev))
359 return dev->std_aspect_ratio;
361 if (vivid_is_hdmi_cap(dev))
362 return dev->dv_timings_aspect_ratio;
364 return TPG_VIDEO_ASPECT_IMAGE;
367 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
369 if (vivid_is_sdtv_cap(dev))
370 return (dev->std_cap & V4L2_STD_525_60) ?
371 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
373 if (vivid_is_hdmi_cap(dev) &&
374 dev->src_rect.width == 720 && dev->src_rect.height <= 576)
375 return dev->src_rect.height == 480 ?
376 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
378 return TPG_PIXEL_ASPECT_SQUARE;
382 * Called whenever the format has to be reset which can occur when
383 * changing inputs, standard, timings, etc.
385 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
387 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
388 unsigned size;
389 u64 pixelclock;
391 switch (dev->input_type[dev->input]) {
392 case WEBCAM:
393 default:
394 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
395 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
396 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
397 dev->field_cap = V4L2_FIELD_NONE;
398 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
399 break;
400 case TV:
401 case SVID:
402 dev->field_cap = dev->tv_field_cap;
403 dev->src_rect.width = 720;
404 if (dev->std_cap & V4L2_STD_525_60) {
405 dev->src_rect.height = 480;
406 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
407 dev->service_set_cap = V4L2_SLICED_CAPTION_525;
408 } else {
409 dev->src_rect.height = 576;
410 dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
411 dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
413 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
414 break;
415 case HDMI:
416 dev->src_rect.width = bt->width;
417 dev->src_rect.height = bt->height;
418 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
419 if (dev->reduced_fps && can_reduce_fps(bt)) {
420 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
421 bt->flags |= V4L2_DV_FL_REDUCED_FPS;
422 } else {
423 pixelclock = bt->pixelclock;
424 bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
426 dev->timeperframe_vid_cap = (struct v4l2_fract) {
427 size / 100, (u32)pixelclock / 100
429 if (bt->interlaced)
430 dev->field_cap = V4L2_FIELD_ALTERNATE;
431 else
432 dev->field_cap = V4L2_FIELD_NONE;
435 * We can be called from within s_ctrl, in that case we can't
436 * set/get controls. Luckily we don't need to in that case.
438 if (keep_controls || !dev->colorspace)
439 break;
440 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
441 if (bt->width == 720 && bt->height <= 576)
442 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
443 else
444 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
445 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
446 } else {
447 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
448 v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
450 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
451 break;
453 vivid_update_quality(dev);
454 tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
455 dev->crop_cap = dev->src_rect;
456 dev->crop_bounds_cap = dev->src_rect;
457 dev->compose_cap = dev->crop_cap;
458 if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
459 dev->compose_cap.height /= 2;
460 dev->fmt_cap_rect = dev->compose_cap;
461 tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
462 tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
463 tpg_update_mv_step(&dev->tpg);
466 /* Map the field to something that is valid for the current input */
467 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
469 if (vivid_is_sdtv_cap(dev)) {
470 switch (field) {
471 case V4L2_FIELD_INTERLACED_TB:
472 case V4L2_FIELD_INTERLACED_BT:
473 case V4L2_FIELD_SEQ_TB:
474 case V4L2_FIELD_SEQ_BT:
475 case V4L2_FIELD_TOP:
476 case V4L2_FIELD_BOTTOM:
477 case V4L2_FIELD_ALTERNATE:
478 return field;
479 case V4L2_FIELD_INTERLACED:
480 default:
481 return V4L2_FIELD_INTERLACED;
484 if (vivid_is_hdmi_cap(dev))
485 return dev->dv_timings_cap.bt.interlaced ? V4L2_FIELD_ALTERNATE :
486 V4L2_FIELD_NONE;
487 return V4L2_FIELD_NONE;
490 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
492 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
493 return tpg_g_colorspace(&dev->tpg);
494 return dev->colorspace_out;
497 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
499 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
500 return tpg_g_xfer_func(&dev->tpg);
501 return dev->xfer_func_out;
504 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
506 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
507 return tpg_g_ycbcr_enc(&dev->tpg);
508 return dev->ycbcr_enc_out;
511 static unsigned int vivid_hsv_enc_cap(struct vivid_dev *dev)
513 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
514 return tpg_g_hsv_enc(&dev->tpg);
515 return dev->hsv_enc_out;
518 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
520 if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
521 return tpg_g_quantization(&dev->tpg);
522 return dev->quantization_out;
525 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
526 struct v4l2_format *f)
528 struct vivid_dev *dev = video_drvdata(file);
529 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
530 unsigned p;
532 mp->width = dev->fmt_cap_rect.width;
533 mp->height = dev->fmt_cap_rect.height;
534 mp->field = dev->field_cap;
535 mp->pixelformat = dev->fmt_cap->fourcc;
536 mp->colorspace = vivid_colorspace_cap(dev);
537 mp->xfer_func = vivid_xfer_func_cap(dev);
538 if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_HSV)
539 mp->hsv_enc = vivid_hsv_enc_cap(dev);
540 else
541 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
542 mp->quantization = vivid_quantization_cap(dev);
543 mp->num_planes = dev->fmt_cap->buffers;
544 for (p = 0; p < mp->num_planes; p++) {
545 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
546 mp->plane_fmt[p].sizeimage =
547 tpg_g_line_width(&dev->tpg, p) * mp->height +
548 dev->fmt_cap->data_offset[p];
550 return 0;
553 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
554 struct v4l2_format *f)
556 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
557 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
558 struct vivid_dev *dev = video_drvdata(file);
559 const struct vivid_fmt *fmt;
560 unsigned bytesperline, max_bpl;
561 unsigned factor = 1;
562 unsigned w, h;
563 unsigned p;
565 fmt = vivid_get_format(dev, mp->pixelformat);
566 if (!fmt) {
567 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
568 mp->pixelformat);
569 mp->pixelformat = V4L2_PIX_FMT_YUYV;
570 fmt = vivid_get_format(dev, mp->pixelformat);
573 mp->field = vivid_field_cap(dev, mp->field);
574 if (vivid_is_webcam(dev)) {
575 const struct v4l2_frmsize_discrete *sz =
576 v4l2_find_nearest_format(webcam_sizes,
577 VIVID_WEBCAM_SIZES,
578 mp->width, mp->height);
580 w = sz->width;
581 h = sz->height;
582 } else if (vivid_is_sdtv_cap(dev)) {
583 w = 720;
584 h = (dev->std_cap & V4L2_STD_525_60) ? 480 : 576;
585 } else {
586 w = dev->src_rect.width;
587 h = dev->src_rect.height;
589 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
590 factor = 2;
591 if (vivid_is_webcam(dev) ||
592 (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
593 mp->width = w;
594 mp->height = h / factor;
595 } else {
596 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
598 v4l2_rect_set_min_size(&r, &vivid_min_rect);
599 v4l2_rect_set_max_size(&r, &vivid_max_rect);
600 if (dev->has_scaler_cap && !dev->has_compose_cap) {
601 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
603 v4l2_rect_set_max_size(&r, &max_r);
604 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
605 v4l2_rect_set_max_size(&r, &dev->src_rect);
606 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
607 v4l2_rect_set_min_size(&r, &dev->src_rect);
609 mp->width = r.width;
610 mp->height = r.height / factor;
613 /* This driver supports custom bytesperline values */
615 mp->num_planes = fmt->buffers;
616 for (p = 0; p < fmt->buffers; p++) {
617 /* Calculate the minimum supported bytesperline value */
618 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
619 /* Calculate the maximum supported bytesperline value */
620 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
622 if (pfmt[p].bytesperline > max_bpl)
623 pfmt[p].bytesperline = max_bpl;
624 if (pfmt[p].bytesperline < bytesperline)
625 pfmt[p].bytesperline = bytesperline;
627 pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
628 fmt->vdownsampling[p] + fmt->data_offset[p];
630 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
632 for (p = fmt->buffers; p < fmt->planes; p++)
633 pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
634 (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
635 (fmt->bit_depth[0] / fmt->vdownsampling[0]);
637 mp->colorspace = vivid_colorspace_cap(dev);
638 if (fmt->color_enc == TGP_COLOR_ENC_HSV)
639 mp->hsv_enc = vivid_hsv_enc_cap(dev);
640 else
641 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
642 mp->xfer_func = vivid_xfer_func_cap(dev);
643 mp->quantization = vivid_quantization_cap(dev);
644 memset(mp->reserved, 0, sizeof(mp->reserved));
645 return 0;
648 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
649 struct v4l2_format *f)
651 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
652 struct vivid_dev *dev = video_drvdata(file);
653 struct v4l2_rect *crop = &dev->crop_cap;
654 struct v4l2_rect *compose = &dev->compose_cap;
655 struct vb2_queue *q = &dev->vb_vid_cap_q;
656 int ret = vivid_try_fmt_vid_cap(file, priv, f);
657 unsigned factor = 1;
658 unsigned p;
659 unsigned i;
661 if (ret < 0)
662 return ret;
664 if (vb2_is_busy(q)) {
665 dprintk(dev, 1, "%s device busy\n", __func__);
666 return -EBUSY;
669 if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
670 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
671 return -EBUSY;
674 dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
675 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
676 factor = 2;
678 /* Note: the webcam input doesn't support scaling, cropping or composing */
680 if (!vivid_is_webcam(dev) &&
681 (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
682 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
684 if (dev->has_scaler_cap) {
685 if (dev->has_compose_cap)
686 v4l2_rect_map_inside(compose, &r);
687 else
688 *compose = r;
689 if (dev->has_crop_cap && !dev->has_compose_cap) {
690 struct v4l2_rect min_r = {
691 0, 0,
692 r.width / MAX_ZOOM,
693 factor * r.height / MAX_ZOOM
695 struct v4l2_rect max_r = {
696 0, 0,
697 r.width * MAX_ZOOM,
698 factor * r.height * MAX_ZOOM
701 v4l2_rect_set_min_size(crop, &min_r);
702 v4l2_rect_set_max_size(crop, &max_r);
703 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
704 } else if (dev->has_crop_cap) {
705 struct v4l2_rect min_r = {
706 0, 0,
707 compose->width / MAX_ZOOM,
708 factor * compose->height / MAX_ZOOM
710 struct v4l2_rect max_r = {
711 0, 0,
712 compose->width * MAX_ZOOM,
713 factor * compose->height * MAX_ZOOM
716 v4l2_rect_set_min_size(crop, &min_r);
717 v4l2_rect_set_max_size(crop, &max_r);
718 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
720 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
721 r.height *= factor;
722 v4l2_rect_set_size_to(crop, &r);
723 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
724 r = *crop;
725 r.height /= factor;
726 v4l2_rect_set_size_to(compose, &r);
727 } else if (!dev->has_crop_cap) {
728 v4l2_rect_map_inside(compose, &r);
729 } else {
730 r.height *= factor;
731 v4l2_rect_set_max_size(crop, &r);
732 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
733 compose->top *= factor;
734 compose->height *= factor;
735 v4l2_rect_set_size_to(compose, crop);
736 v4l2_rect_map_inside(compose, &r);
737 compose->top /= factor;
738 compose->height /= factor;
740 } else if (vivid_is_webcam(dev)) {
741 /* Guaranteed to be a match */
742 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
743 if (webcam_sizes[i].width == mp->width &&
744 webcam_sizes[i].height == mp->height)
745 break;
746 dev->webcam_size_idx = i;
747 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
748 dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
749 vivid_update_format_cap(dev, false);
750 } else {
751 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
753 v4l2_rect_set_size_to(compose, &r);
754 r.height *= factor;
755 v4l2_rect_set_size_to(crop, &r);
758 dev->fmt_cap_rect.width = mp->width;
759 dev->fmt_cap_rect.height = mp->height;
760 tpg_s_buf_height(&dev->tpg, mp->height);
761 tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
762 for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
763 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
764 dev->field_cap = mp->field;
765 if (dev->field_cap == V4L2_FIELD_ALTERNATE)
766 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
767 else
768 tpg_s_field(&dev->tpg, dev->field_cap, false);
769 tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
770 if (vivid_is_sdtv_cap(dev))
771 dev->tv_field_cap = mp->field;
772 tpg_update_mv_step(&dev->tpg);
773 return 0;
776 int vidioc_g_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)
782 return -ENOTTY;
783 return vivid_g_fmt_vid_cap(file, priv, f);
786 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
787 struct v4l2_format *f)
789 struct vivid_dev *dev = video_drvdata(file);
791 if (!dev->multiplanar)
792 return -ENOTTY;
793 return vivid_try_fmt_vid_cap(file, priv, f);
796 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
797 struct v4l2_format *f)
799 struct vivid_dev *dev = video_drvdata(file);
801 if (!dev->multiplanar)
802 return -ENOTTY;
803 return vivid_s_fmt_vid_cap(file, priv, f);
806 int vidioc_g_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)
812 return -ENOTTY;
813 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
816 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
817 struct v4l2_format *f)
819 struct vivid_dev *dev = video_drvdata(file);
821 if (dev->multiplanar)
822 return -ENOTTY;
823 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
826 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
827 struct v4l2_format *f)
829 struct vivid_dev *dev = video_drvdata(file);
831 if (dev->multiplanar)
832 return -ENOTTY;
833 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
836 int vivid_vid_cap_g_selection(struct file *file, void *priv,
837 struct v4l2_selection *sel)
839 struct vivid_dev *dev = video_drvdata(file);
841 if (!dev->has_crop_cap && !dev->has_compose_cap)
842 return -ENOTTY;
843 if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
844 return -EINVAL;
845 if (vivid_is_webcam(dev))
846 return -ENODATA;
848 sel->r.left = sel->r.top = 0;
849 switch (sel->target) {
850 case V4L2_SEL_TGT_CROP:
851 if (!dev->has_crop_cap)
852 return -EINVAL;
853 sel->r = dev->crop_cap;
854 break;
855 case V4L2_SEL_TGT_CROP_DEFAULT:
856 case V4L2_SEL_TGT_CROP_BOUNDS:
857 if (!dev->has_crop_cap)
858 return -EINVAL;
859 sel->r = dev->src_rect;
860 break;
861 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
862 if (!dev->has_compose_cap)
863 return -EINVAL;
864 sel->r = vivid_max_rect;
865 break;
866 case V4L2_SEL_TGT_COMPOSE:
867 if (!dev->has_compose_cap)
868 return -EINVAL;
869 sel->r = dev->compose_cap;
870 break;
871 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
872 if (!dev->has_compose_cap)
873 return -EINVAL;
874 sel->r = dev->fmt_cap_rect;
875 break;
876 default:
877 return -EINVAL;
879 return 0;
882 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
884 struct vivid_dev *dev = video_drvdata(file);
885 struct v4l2_rect *crop = &dev->crop_cap;
886 struct v4l2_rect *compose = &dev->compose_cap;
887 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
888 int ret;
890 if (!dev->has_crop_cap && !dev->has_compose_cap)
891 return -ENOTTY;
892 if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
893 return -EINVAL;
894 if (vivid_is_webcam(dev))
895 return -ENODATA;
897 switch (s->target) {
898 case V4L2_SEL_TGT_CROP:
899 if (!dev->has_crop_cap)
900 return -EINVAL;
901 ret = vivid_vid_adjust_sel(s->flags, &s->r);
902 if (ret)
903 return ret;
904 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
905 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
906 v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
907 s->r.top /= factor;
908 s->r.height /= factor;
909 if (dev->has_scaler_cap) {
910 struct v4l2_rect fmt = dev->fmt_cap_rect;
911 struct v4l2_rect max_rect = {
912 0, 0,
913 s->r.width * MAX_ZOOM,
914 s->r.height * MAX_ZOOM
916 struct v4l2_rect min_rect = {
917 0, 0,
918 s->r.width / MAX_ZOOM,
919 s->r.height / MAX_ZOOM
922 v4l2_rect_set_min_size(&fmt, &min_rect);
923 if (!dev->has_compose_cap)
924 v4l2_rect_set_max_size(&fmt, &max_rect);
925 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
926 vb2_is_busy(&dev->vb_vid_cap_q))
927 return -EBUSY;
928 if (dev->has_compose_cap) {
929 v4l2_rect_set_min_size(compose, &min_rect);
930 v4l2_rect_set_max_size(compose, &max_rect);
932 dev->fmt_cap_rect = fmt;
933 tpg_s_buf_height(&dev->tpg, fmt.height);
934 } else if (dev->has_compose_cap) {
935 struct v4l2_rect fmt = dev->fmt_cap_rect;
937 v4l2_rect_set_min_size(&fmt, &s->r);
938 if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
939 vb2_is_busy(&dev->vb_vid_cap_q))
940 return -EBUSY;
941 dev->fmt_cap_rect = fmt;
942 tpg_s_buf_height(&dev->tpg, fmt.height);
943 v4l2_rect_set_size_to(compose, &s->r);
944 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
945 } else {
946 if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
947 vb2_is_busy(&dev->vb_vid_cap_q))
948 return -EBUSY;
949 v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
950 v4l2_rect_set_size_to(compose, &s->r);
951 v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
952 tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
954 s->r.top *= factor;
955 s->r.height *= factor;
956 *crop = s->r;
957 break;
958 case V4L2_SEL_TGT_COMPOSE:
959 if (!dev->has_compose_cap)
960 return -EINVAL;
961 ret = vivid_vid_adjust_sel(s->flags, &s->r);
962 if (ret)
963 return ret;
964 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
965 v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
966 if (dev->has_scaler_cap) {
967 struct v4l2_rect max_rect = {
968 0, 0,
969 dev->src_rect.width * MAX_ZOOM,
970 (dev->src_rect.height / factor) * MAX_ZOOM
973 v4l2_rect_set_max_size(&s->r, &max_rect);
974 if (dev->has_crop_cap) {
975 struct v4l2_rect min_rect = {
976 0, 0,
977 s->r.width / MAX_ZOOM,
978 (s->r.height * factor) / MAX_ZOOM
980 struct v4l2_rect max_rect = {
981 0, 0,
982 s->r.width * MAX_ZOOM,
983 (s->r.height * factor) * MAX_ZOOM
986 v4l2_rect_set_min_size(crop, &min_rect);
987 v4l2_rect_set_max_size(crop, &max_rect);
988 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
990 } else if (dev->has_crop_cap) {
991 s->r.top *= factor;
992 s->r.height *= factor;
993 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
994 v4l2_rect_set_size_to(crop, &s->r);
995 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
996 s->r.top /= factor;
997 s->r.height /= factor;
998 } else {
999 v4l2_rect_set_size_to(&s->r, &dev->src_rect);
1000 s->r.height /= factor;
1002 v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
1003 if (dev->bitmap_cap && (compose->width != s->r.width ||
1004 compose->height != s->r.height)) {
1005 kfree(dev->bitmap_cap);
1006 dev->bitmap_cap = NULL;
1008 *compose = s->r;
1009 break;
1010 default:
1011 return -EINVAL;
1014 tpg_s_crop_compose(&dev->tpg, crop, compose);
1015 return 0;
1018 int vivid_vid_cap_cropcap(struct file *file, void *priv,
1019 struct v4l2_cropcap *cap)
1021 struct vivid_dev *dev = video_drvdata(file);
1023 if (cap->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1024 return -EINVAL;
1026 switch (vivid_get_pixel_aspect(dev)) {
1027 case TPG_PIXEL_ASPECT_NTSC:
1028 cap->pixelaspect.numerator = 11;
1029 cap->pixelaspect.denominator = 10;
1030 break;
1031 case TPG_PIXEL_ASPECT_PAL:
1032 cap->pixelaspect.numerator = 54;
1033 cap->pixelaspect.denominator = 59;
1034 break;
1035 case TPG_PIXEL_ASPECT_SQUARE:
1036 cap->pixelaspect.numerator = 1;
1037 cap->pixelaspect.denominator = 1;
1038 break;
1040 return 0;
1043 int vidioc_enum_fmt_vid_overlay(struct file *file, void *priv,
1044 struct v4l2_fmtdesc *f)
1046 struct vivid_dev *dev = video_drvdata(file);
1047 const struct vivid_fmt *fmt;
1049 if (dev->multiplanar)
1050 return -ENOTTY;
1052 if (f->index >= ARRAY_SIZE(formats_ovl))
1053 return -EINVAL;
1055 fmt = &formats_ovl[f->index];
1057 f->pixelformat = fmt->fourcc;
1058 return 0;
1061 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1062 struct v4l2_format *f)
1064 struct vivid_dev *dev = video_drvdata(file);
1065 const struct v4l2_rect *compose = &dev->compose_cap;
1066 struct v4l2_window *win = &f->fmt.win;
1067 unsigned clipcount = win->clipcount;
1069 if (dev->multiplanar)
1070 return -ENOTTY;
1072 win->w.top = dev->overlay_cap_top;
1073 win->w.left = dev->overlay_cap_left;
1074 win->w.width = compose->width;
1075 win->w.height = compose->height;
1076 win->field = dev->overlay_cap_field;
1077 win->clipcount = dev->clipcount_cap;
1078 if (clipcount > dev->clipcount_cap)
1079 clipcount = dev->clipcount_cap;
1080 if (dev->bitmap_cap == NULL)
1081 win->bitmap = NULL;
1082 else if (win->bitmap) {
1083 if (copy_to_user(win->bitmap, dev->bitmap_cap,
1084 ((compose->width + 7) / 8) * compose->height))
1085 return -EFAULT;
1087 if (clipcount && win->clips) {
1088 if (copy_to_user(win->clips, dev->clips_cap,
1089 clipcount * sizeof(dev->clips_cap[0])))
1090 return -EFAULT;
1092 return 0;
1095 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1096 struct v4l2_format *f)
1098 struct vivid_dev *dev = video_drvdata(file);
1099 const struct v4l2_rect *compose = &dev->compose_cap;
1100 struct v4l2_window *win = &f->fmt.win;
1101 int i, j;
1103 if (dev->multiplanar)
1104 return -ENOTTY;
1106 win->w.left = clamp_t(int, win->w.left,
1107 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1108 win->w.top = clamp_t(int, win->w.top,
1109 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1110 win->w.width = compose->width;
1111 win->w.height = compose->height;
1112 if (win->field != V4L2_FIELD_BOTTOM && win->field != V4L2_FIELD_TOP)
1113 win->field = V4L2_FIELD_ANY;
1114 win->chromakey = 0;
1115 win->global_alpha = 0;
1116 if (win->clipcount && !win->clips)
1117 win->clipcount = 0;
1118 if (win->clipcount > MAX_CLIPS)
1119 win->clipcount = MAX_CLIPS;
1120 if (win->clipcount) {
1121 if (copy_from_user(dev->try_clips_cap, win->clips,
1122 win->clipcount * sizeof(dev->clips_cap[0])))
1123 return -EFAULT;
1124 for (i = 0; i < win->clipcount; i++) {
1125 struct v4l2_rect *r = &dev->try_clips_cap[i].c;
1127 r->top = clamp_t(s32, r->top, 0, dev->fb_cap.fmt.height - 1);
1128 r->height = clamp_t(s32, r->height, 1, dev->fb_cap.fmt.height - r->top);
1129 r->left = clamp_t(u32, r->left, 0, dev->fb_cap.fmt.width - 1);
1130 r->width = clamp_t(u32, r->width, 1, dev->fb_cap.fmt.width - r->left);
1133 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
1134 * number and it's typically a one-time deal.
1136 for (i = 0; i < win->clipcount - 1; i++) {
1137 struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1139 for (j = i + 1; j < win->clipcount; j++) {
1140 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1142 if (v4l2_rect_overlap(r1, r2))
1143 return -EINVAL;
1146 if (copy_to_user(win->clips, dev->try_clips_cap,
1147 win->clipcount * sizeof(dev->clips_cap[0])))
1148 return -EFAULT;
1150 return 0;
1153 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1154 struct v4l2_format *f)
1156 struct vivid_dev *dev = video_drvdata(file);
1157 const struct v4l2_rect *compose = &dev->compose_cap;
1158 struct v4l2_window *win = &f->fmt.win;
1159 int ret = vidioc_try_fmt_vid_overlay(file, priv, f);
1160 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
1161 unsigned clips_size = win->clipcount * sizeof(dev->clips_cap[0]);
1162 void *new_bitmap = NULL;
1164 if (ret)
1165 return ret;
1167 if (win->bitmap) {
1168 new_bitmap = vzalloc(bitmap_size);
1170 if (new_bitmap == NULL)
1171 return -ENOMEM;
1172 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
1173 vfree(new_bitmap);
1174 return -EFAULT;
1178 dev->overlay_cap_top = win->w.top;
1179 dev->overlay_cap_left = win->w.left;
1180 dev->overlay_cap_field = win->field;
1181 vfree(dev->bitmap_cap);
1182 dev->bitmap_cap = new_bitmap;
1183 dev->clipcount_cap = win->clipcount;
1184 if (dev->clipcount_cap)
1185 memcpy(dev->clips_cap, dev->try_clips_cap, clips_size);
1186 return 0;
1189 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1191 struct vivid_dev *dev = video_drvdata(file);
1193 if (dev->multiplanar)
1194 return -ENOTTY;
1196 if (i && dev->fb_vbase_cap == NULL)
1197 return -EINVAL;
1199 if (i && dev->fb_cap.fmt.pixelformat != dev->fmt_cap->fourcc) {
1200 dprintk(dev, 1, "mismatch between overlay and video capture pixelformats\n");
1201 return -EINVAL;
1204 if (dev->overlay_cap_owner && dev->overlay_cap_owner != fh)
1205 return -EBUSY;
1206 dev->overlay_cap_owner = i ? fh : NULL;
1207 return 0;
1210 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1211 struct v4l2_framebuffer *a)
1213 struct vivid_dev *dev = video_drvdata(file);
1215 if (dev->multiplanar)
1216 return -ENOTTY;
1218 *a = dev->fb_cap;
1219 a->capability = V4L2_FBUF_CAP_BITMAP_CLIPPING |
1220 V4L2_FBUF_CAP_LIST_CLIPPING;
1221 a->flags = V4L2_FBUF_FLAG_PRIMARY;
1222 a->fmt.field = V4L2_FIELD_NONE;
1223 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1224 a->fmt.priv = 0;
1225 return 0;
1228 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1229 const struct v4l2_framebuffer *a)
1231 struct vivid_dev *dev = video_drvdata(file);
1232 const struct vivid_fmt *fmt;
1234 if (dev->multiplanar)
1235 return -ENOTTY;
1237 if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1238 return -EPERM;
1240 if (dev->overlay_cap_owner)
1241 return -EBUSY;
1243 if (a->base == NULL) {
1244 dev->fb_cap.base = NULL;
1245 dev->fb_vbase_cap = NULL;
1246 return 0;
1249 if (a->fmt.width < 48 || a->fmt.height < 32)
1250 return -EINVAL;
1251 fmt = vivid_get_format(dev, a->fmt.pixelformat);
1252 if (!fmt || !fmt->can_do_overlay)
1253 return -EINVAL;
1254 if (a->fmt.bytesperline < (a->fmt.width * fmt->bit_depth[0]) / 8)
1255 return -EINVAL;
1256 if (a->fmt.height * a->fmt.bytesperline < a->fmt.sizeimage)
1257 return -EINVAL;
1259 dev->fb_vbase_cap = phys_to_virt((unsigned long)a->base);
1260 dev->fb_cap = *a;
1261 dev->overlay_cap_left = clamp_t(int, dev->overlay_cap_left,
1262 -dev->fb_cap.fmt.width, dev->fb_cap.fmt.width);
1263 dev->overlay_cap_top = clamp_t(int, dev->overlay_cap_top,
1264 -dev->fb_cap.fmt.height, dev->fb_cap.fmt.height);
1265 return 0;
1268 static const struct v4l2_audio vivid_audio_inputs[] = {
1269 { 0, "TV", V4L2_AUDCAP_STEREO },
1270 { 1, "Line-In", V4L2_AUDCAP_STEREO },
1273 int vidioc_enum_input(struct file *file, void *priv,
1274 struct v4l2_input *inp)
1276 struct vivid_dev *dev = video_drvdata(file);
1278 if (inp->index >= dev->num_inputs)
1279 return -EINVAL;
1281 inp->type = V4L2_INPUT_TYPE_CAMERA;
1282 switch (dev->input_type[inp->index]) {
1283 case WEBCAM:
1284 snprintf(inp->name, sizeof(inp->name), "Webcam %u",
1285 dev->input_name_counter[inp->index]);
1286 inp->capabilities = 0;
1287 break;
1288 case TV:
1289 snprintf(inp->name, sizeof(inp->name), "TV %u",
1290 dev->input_name_counter[inp->index]);
1291 inp->type = V4L2_INPUT_TYPE_TUNER;
1292 inp->std = V4L2_STD_ALL;
1293 if (dev->has_audio_inputs)
1294 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1295 inp->capabilities = V4L2_IN_CAP_STD;
1296 break;
1297 case SVID:
1298 snprintf(inp->name, sizeof(inp->name), "S-Video %u",
1299 dev->input_name_counter[inp->index]);
1300 inp->std = V4L2_STD_ALL;
1301 if (dev->has_audio_inputs)
1302 inp->audioset = (1 << ARRAY_SIZE(vivid_audio_inputs)) - 1;
1303 inp->capabilities = V4L2_IN_CAP_STD;
1304 break;
1305 case HDMI:
1306 snprintf(inp->name, sizeof(inp->name), "HDMI %u",
1307 dev->input_name_counter[inp->index]);
1308 inp->capabilities = V4L2_IN_CAP_DV_TIMINGS;
1309 if (dev->edid_blocks == 0 ||
1310 dev->dv_timings_signal_mode == NO_SIGNAL)
1311 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1312 else if (dev->dv_timings_signal_mode == NO_LOCK ||
1313 dev->dv_timings_signal_mode == OUT_OF_RANGE)
1314 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1315 break;
1317 if (dev->sensor_hflip)
1318 inp->status |= V4L2_IN_ST_HFLIP;
1319 if (dev->sensor_vflip)
1320 inp->status |= V4L2_IN_ST_VFLIP;
1321 if (dev->input == inp->index && vivid_is_sdtv_cap(dev)) {
1322 if (dev->std_signal_mode == NO_SIGNAL) {
1323 inp->status |= V4L2_IN_ST_NO_SIGNAL;
1324 } else if (dev->std_signal_mode == NO_LOCK) {
1325 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1326 } else if (vivid_is_tv_cap(dev)) {
1327 switch (tpg_g_quality(&dev->tpg)) {
1328 case TPG_QUAL_GRAY:
1329 inp->status |= V4L2_IN_ST_COLOR_KILL;
1330 break;
1331 case TPG_QUAL_NOISE:
1332 inp->status |= V4L2_IN_ST_NO_H_LOCK;
1333 break;
1334 default:
1335 break;
1339 return 0;
1342 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1344 struct vivid_dev *dev = video_drvdata(file);
1346 *i = dev->input;
1347 return 0;
1350 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1352 struct vivid_dev *dev = video_drvdata(file);
1353 struct v4l2_bt_timings *bt = &dev->dv_timings_cap.bt;
1354 unsigned brightness;
1356 if (i >= dev->num_inputs)
1357 return -EINVAL;
1359 if (i == dev->input)
1360 return 0;
1362 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1363 return -EBUSY;
1365 dev->input = i;
1366 dev->vid_cap_dev.tvnorms = 0;
1367 if (dev->input_type[i] == TV || dev->input_type[i] == SVID) {
1368 dev->tv_audio_input = (dev->input_type[i] == TV) ? 0 : 1;
1369 dev->vid_cap_dev.tvnorms = V4L2_STD_ALL;
1371 dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1372 vivid_update_format_cap(dev, false);
1374 if (dev->colorspace) {
1375 switch (dev->input_type[i]) {
1376 case WEBCAM:
1377 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1378 break;
1379 case TV:
1380 case SVID:
1381 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1382 break;
1383 case HDMI:
1384 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
1385 if (dev->src_rect.width == 720 && dev->src_rect.height <= 576)
1386 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
1387 else
1388 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
1389 } else {
1390 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
1392 break;
1397 * Modify the brightness range depending on the input.
1398 * This makes it easy to use vivid to test if applications can
1399 * handle control range modifications and is also how this is
1400 * typically used in practice as different inputs may be hooked
1401 * up to different receivers with different control ranges.
1403 brightness = 128 * i + dev->input_brightness[i];
1404 v4l2_ctrl_modify_range(dev->brightness,
1405 128 * i, 255 + 128 * i, 1, 128 + 128 * i);
1406 v4l2_ctrl_s_ctrl(dev->brightness, brightness);
1407 return 0;
1410 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1412 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1413 return -EINVAL;
1414 *vin = vivid_audio_inputs[vin->index];
1415 return 0;
1418 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1420 struct vivid_dev *dev = video_drvdata(file);
1422 if (!vivid_is_sdtv_cap(dev))
1423 return -EINVAL;
1424 *vin = vivid_audio_inputs[dev->tv_audio_input];
1425 return 0;
1428 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1430 struct vivid_dev *dev = video_drvdata(file);
1432 if (!vivid_is_sdtv_cap(dev))
1433 return -EINVAL;
1434 if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1435 return -EINVAL;
1436 dev->tv_audio_input = vin->index;
1437 return 0;
1440 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1442 struct vivid_dev *dev = video_drvdata(file);
1444 if (vf->tuner != 0)
1445 return -EINVAL;
1446 vf->frequency = dev->tv_freq;
1447 return 0;
1450 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1452 struct vivid_dev *dev = video_drvdata(file);
1454 if (vf->tuner != 0)
1455 return -EINVAL;
1456 dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1457 if (vivid_is_tv_cap(dev))
1458 vivid_update_quality(dev);
1459 return 0;
1462 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1464 struct vivid_dev *dev = video_drvdata(file);
1466 if (vt->index != 0)
1467 return -EINVAL;
1468 if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1469 return -EINVAL;
1470 dev->tv_audmode = vt->audmode;
1471 return 0;
1474 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1476 struct vivid_dev *dev = video_drvdata(file);
1477 enum tpg_quality qual;
1479 if (vt->index != 0)
1480 return -EINVAL;
1482 vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1483 V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1484 vt->audmode = dev->tv_audmode;
1485 vt->rangelow = MIN_TV_FREQ;
1486 vt->rangehigh = MAX_TV_FREQ;
1487 qual = vivid_get_quality(dev, &vt->afc);
1488 if (qual == TPG_QUAL_COLOR)
1489 vt->signal = 0xffff;
1490 else if (qual == TPG_QUAL_GRAY)
1491 vt->signal = 0x8000;
1492 else
1493 vt->signal = 0;
1494 if (qual == TPG_QUAL_NOISE) {
1495 vt->rxsubchans = 0;
1496 } else if (qual == TPG_QUAL_GRAY) {
1497 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1498 } else {
1499 unsigned channel_nr = dev->tv_freq / (6 * 16);
1500 unsigned options = (dev->std_cap & V4L2_STD_NTSC_M) ? 4 : 3;
1502 switch (channel_nr % options) {
1503 case 0:
1504 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1505 break;
1506 case 1:
1507 vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1508 break;
1509 case 2:
1510 if (dev->std_cap & V4L2_STD_NTSC_M)
1511 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1512 else
1513 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1514 break;
1515 case 3:
1516 vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1517 break;
1520 strlcpy(vt->name, "TV Tuner", sizeof(vt->name));
1521 return 0;
1524 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1525 const v4l2_std_id vivid_standard[] = {
1526 V4L2_STD_NTSC_M,
1527 V4L2_STD_NTSC_M_JP,
1528 V4L2_STD_NTSC_M_KR,
1529 V4L2_STD_NTSC_443,
1530 V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1531 V4L2_STD_PAL_I,
1532 V4L2_STD_PAL_DK,
1533 V4L2_STD_PAL_M,
1534 V4L2_STD_PAL_N,
1535 V4L2_STD_PAL_Nc,
1536 V4L2_STD_PAL_60,
1537 V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1538 V4L2_STD_SECAM_DK,
1539 V4L2_STD_SECAM_L,
1540 V4L2_STD_SECAM_LC,
1541 V4L2_STD_UNKNOWN
1544 /* Must remain in sync with the vivid_standard array */
1545 const char * const vivid_ctrl_standard_strings[] = {
1546 "NTSC-M",
1547 "NTSC-M-JP",
1548 "NTSC-M-KR",
1549 "NTSC-443",
1550 "PAL-BGH",
1551 "PAL-I",
1552 "PAL-DK",
1553 "PAL-M",
1554 "PAL-N",
1555 "PAL-Nc",
1556 "PAL-60",
1557 "SECAM-BGH",
1558 "SECAM-DK",
1559 "SECAM-L",
1560 "SECAM-Lc",
1561 NULL,
1564 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1566 struct vivid_dev *dev = video_drvdata(file);
1568 if (!vivid_is_sdtv_cap(dev))
1569 return -ENODATA;
1570 if (dev->std_signal_mode == NO_SIGNAL ||
1571 dev->std_signal_mode == NO_LOCK) {
1572 *id = V4L2_STD_UNKNOWN;
1573 return 0;
1575 if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1576 *id = V4L2_STD_UNKNOWN;
1577 } else if (dev->std_signal_mode == CURRENT_STD) {
1578 *id = dev->std_cap;
1579 } else if (dev->std_signal_mode == SELECTED_STD) {
1580 *id = dev->query_std;
1581 } else {
1582 *id = vivid_standard[dev->query_std_last];
1583 dev->query_std_last = (dev->query_std_last + 1) % ARRAY_SIZE(vivid_standard);
1586 return 0;
1589 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1591 struct vivid_dev *dev = video_drvdata(file);
1593 if (!vivid_is_sdtv_cap(dev))
1594 return -ENODATA;
1595 if (dev->std_cap == id)
1596 return 0;
1597 if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1598 return -EBUSY;
1599 dev->std_cap = id;
1600 vivid_update_format_cap(dev, false);
1601 return 0;
1604 static void find_aspect_ratio(u32 width, u32 height,
1605 u32 *num, u32 *denom)
1607 if (!(height % 3) && ((height * 4 / 3) == width)) {
1608 *num = 4;
1609 *denom = 3;
1610 } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1611 *num = 16;
1612 *denom = 9;
1613 } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1614 *num = 16;
1615 *denom = 10;
1616 } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1617 *num = 5;
1618 *denom = 4;
1619 } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1620 *num = 15;
1621 *denom = 9;
1622 } else { /* default to 16:9 */
1623 *num = 16;
1624 *denom = 9;
1628 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1630 struct v4l2_bt_timings *bt = &timings->bt;
1631 u32 total_h_pixel;
1632 u32 total_v_lines;
1633 u32 h_freq;
1635 if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1636 NULL, NULL))
1637 return false;
1639 total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1640 total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1642 h_freq = (u32)bt->pixelclock / total_h_pixel;
1644 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1645 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1646 bt->polarities, bt->interlaced, timings))
1647 return true;
1650 if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1651 struct v4l2_fract aspect_ratio;
1653 find_aspect_ratio(bt->width, bt->height,
1654 &aspect_ratio.numerator,
1655 &aspect_ratio.denominator);
1656 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1657 bt->polarities, bt->interlaced,
1658 aspect_ratio, timings))
1659 return true;
1661 return false;
1664 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1665 struct v4l2_dv_timings *timings)
1667 struct vivid_dev *dev = video_drvdata(file);
1669 if (!vivid_is_hdmi_cap(dev))
1670 return -ENODATA;
1671 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1672 0, NULL, NULL) &&
1673 !valid_cvt_gtf_timings(timings))
1674 return -EINVAL;
1676 if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap, 0, false))
1677 return 0;
1678 if (vb2_is_busy(&dev->vb_vid_cap_q))
1679 return -EBUSY;
1681 dev->dv_timings_cap = *timings;
1682 vivid_update_format_cap(dev, false);
1683 return 0;
1686 int vidioc_query_dv_timings(struct file *file, void *_fh,
1687 struct v4l2_dv_timings *timings)
1689 struct vivid_dev *dev = video_drvdata(file);
1691 if (!vivid_is_hdmi_cap(dev))
1692 return -ENODATA;
1693 if (dev->dv_timings_signal_mode == NO_SIGNAL ||
1694 dev->edid_blocks == 0)
1695 return -ENOLINK;
1696 if (dev->dv_timings_signal_mode == NO_LOCK)
1697 return -ENOLCK;
1698 if (dev->dv_timings_signal_mode == OUT_OF_RANGE) {
1699 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1700 return -ERANGE;
1702 if (dev->dv_timings_signal_mode == CURRENT_DV_TIMINGS) {
1703 *timings = dev->dv_timings_cap;
1704 } else if (dev->dv_timings_signal_mode == SELECTED_DV_TIMINGS) {
1705 *timings = v4l2_dv_timings_presets[dev->query_dv_timings];
1706 } else {
1707 *timings = v4l2_dv_timings_presets[dev->query_dv_timings_last];
1708 dev->query_dv_timings_last = (dev->query_dv_timings_last + 1) %
1709 dev->query_dv_timings_size;
1711 return 0;
1714 int vidioc_s_edid(struct file *file, void *_fh,
1715 struct v4l2_edid *edid)
1717 struct vivid_dev *dev = video_drvdata(file);
1718 u16 phys_addr;
1719 unsigned int i;
1720 int ret;
1722 memset(edid->reserved, 0, sizeof(edid->reserved));
1723 if (edid->pad >= dev->num_inputs)
1724 return -EINVAL;
1725 if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1726 return -EINVAL;
1727 if (edid->blocks == 0) {
1728 dev->edid_blocks = 0;
1729 phys_addr = CEC_PHYS_ADDR_INVALID;
1730 goto set_phys_addr;
1732 if (edid->blocks > dev->edid_max_blocks) {
1733 edid->blocks = dev->edid_max_blocks;
1734 return -E2BIG;
1736 phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1737 ret = cec_phys_addr_validate(phys_addr, &phys_addr, NULL);
1738 if (ret)
1739 return ret;
1741 if (vb2_is_busy(&dev->vb_vid_cap_q))
1742 return -EBUSY;
1744 dev->edid_blocks = edid->blocks;
1745 memcpy(dev->edid, edid->edid, edid->blocks * 128);
1747 set_phys_addr:
1748 /* TODO: a proper hotplug detect cycle should be emulated here */
1749 cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1751 for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1752 cec_s_phys_addr(dev->cec_tx_adap[i],
1753 cec_phys_addr_for_input(phys_addr, i + 1),
1754 false);
1755 return 0;
1758 int vidioc_enum_framesizes(struct file *file, void *fh,
1759 struct v4l2_frmsizeenum *fsize)
1761 struct vivid_dev *dev = video_drvdata(file);
1763 if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1764 return -EINVAL;
1765 if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1766 return -EINVAL;
1767 if (vivid_is_webcam(dev)) {
1768 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1769 return -EINVAL;
1770 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1771 fsize->discrete = webcam_sizes[fsize->index];
1772 return 0;
1774 if (fsize->index)
1775 return -EINVAL;
1776 fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1777 fsize->stepwise.min_width = MIN_WIDTH;
1778 fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1779 fsize->stepwise.step_width = 2;
1780 fsize->stepwise.min_height = MIN_HEIGHT;
1781 fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1782 fsize->stepwise.step_height = 2;
1783 return 0;
1786 /* timeperframe is arbitrary and continuous */
1787 int vidioc_enum_frameintervals(struct file *file, void *priv,
1788 struct v4l2_frmivalenum *fival)
1790 struct vivid_dev *dev = video_drvdata(file);
1791 const struct vivid_fmt *fmt;
1792 int i;
1794 fmt = vivid_get_format(dev, fival->pixel_format);
1795 if (!fmt)
1796 return -EINVAL;
1798 if (!vivid_is_webcam(dev)) {
1799 if (fival->index)
1800 return -EINVAL;
1801 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1802 return -EINVAL;
1803 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1804 return -EINVAL;
1805 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1806 fival->discrete = dev->timeperframe_vid_cap;
1807 return 0;
1810 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1811 if (fival->width == webcam_sizes[i].width &&
1812 fival->height == webcam_sizes[i].height)
1813 break;
1814 if (i == ARRAY_SIZE(webcam_sizes))
1815 return -EINVAL;
1816 if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1817 return -EINVAL;
1818 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1819 fival->discrete = webcam_intervals[fival->index];
1820 return 0;
1823 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1824 struct v4l2_streamparm *parm)
1826 struct vivid_dev *dev = video_drvdata(file);
1828 if (parm->type != (dev->multiplanar ?
1829 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1830 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1831 return -EINVAL;
1833 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1834 parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1835 parm->parm.capture.readbuffers = 1;
1836 return 0;
1839 #define FRACT_CMP(a, OP, b) \
1840 ((u64)(a).numerator * (b).denominator OP (u64)(b).numerator * (a).denominator)
1842 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1843 struct v4l2_streamparm *parm)
1845 struct vivid_dev *dev = video_drvdata(file);
1846 unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1847 struct v4l2_fract tpf;
1848 unsigned i;
1850 if (parm->type != (dev->multiplanar ?
1851 V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1852 V4L2_BUF_TYPE_VIDEO_CAPTURE))
1853 return -EINVAL;
1854 if (!vivid_is_webcam(dev))
1855 return vivid_vid_cap_g_parm(file, priv, parm);
1857 tpf = parm->parm.capture.timeperframe;
1859 if (tpf.denominator == 0)
1860 tpf = webcam_intervals[ival_sz - 1];
1861 for (i = 0; i < ival_sz; i++)
1862 if (FRACT_CMP(tpf, >=, webcam_intervals[i]))
1863 break;
1864 if (i == ival_sz)
1865 i = ival_sz - 1;
1866 dev->webcam_ival_idx = i;
1867 tpf = webcam_intervals[dev->webcam_ival_idx];
1868 tpf = FRACT_CMP(tpf, <, tpf_min) ? tpf_min : tpf;
1869 tpf = FRACT_CMP(tpf, >, tpf_max) ? tpf_max : tpf;
1871 /* resync the thread's timings */
1872 dev->cap_seq_resync = true;
1873 dev->timeperframe_vid_cap = tpf;
1874 parm->parm.capture.capability = V4L2_CAP_TIMEPERFRAME;
1875 parm->parm.capture.timeperframe = tpf;
1876 parm->parm.capture.readbuffers = 1;
1877 return 0;