]> asedeno.scripts.mit.edu Git - linux.git/blob - drivers/media/platform/vivid/vivid-vid-cap.c
6edb0325f25f05f25bcc5258ec73b8e191d3b1cc
[linux.git] / drivers / media / platform / vivid / vivid-vid-cap.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-vid-cap.c - video capture support functions.
4  *
5  * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
6  */
7
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/sched.h>
11 #include <linux/vmalloc.h>
12 #include <linux/videodev2.h>
13 #include <linux/v4l2-dv-timings.h>
14 #include <media/v4l2-common.h>
15 #include <media/v4l2-event.h>
16 #include <media/v4l2-dv-timings.h>
17 #include <media/v4l2-rect.h>
18
19 #include "vivid-core.h"
20 #include "vivid-vid-common.h"
21 #include "vivid-kthread-cap.h"
22 #include "vivid-vid-cap.h"
23
24 static const struct vivid_fmt formats_ovl[] = {
25         {
26                 .fourcc   = V4L2_PIX_FMT_RGB565, /* gggbbbbb rrrrrggg */
27                 .vdownsampling = { 1 },
28                 .bit_depth = { 16 },
29                 .planes   = 1,
30                 .buffers = 1,
31         },
32         {
33                 .fourcc   = V4L2_PIX_FMT_XRGB555, /* gggbbbbb arrrrrgg */
34                 .vdownsampling = { 1 },
35                 .bit_depth = { 16 },
36                 .planes   = 1,
37                 .buffers = 1,
38         },
39         {
40                 .fourcc   = V4L2_PIX_FMT_ARGB555, /* gggbbbbb arrrrrgg */
41                 .vdownsampling = { 1 },
42                 .bit_depth = { 16 },
43                 .planes   = 1,
44                 .buffers = 1,
45         },
46 };
47
48 /* The number of discrete webcam framesizes */
49 #define VIVID_WEBCAM_SIZES 6
50 /* The number of discrete webcam frameintervals */
51 #define VIVID_WEBCAM_IVALS (VIVID_WEBCAM_SIZES * 2)
52
53 /* Sizes must be in increasing order */
54 static const struct v4l2_frmsize_discrete webcam_sizes[VIVID_WEBCAM_SIZES] = {
55         {  320, 180 },
56         {  640, 360 },
57         {  640, 480 },
58         { 1280, 720 },
59         { 1920, 1080 },
60         { 3840, 2160 },
61 };
62
63 /*
64  * Intervals must be in increasing order and there must be twice as many
65  * elements in this array as there are in webcam_sizes.
66  */
67 static const struct v4l2_fract webcam_intervals[VIVID_WEBCAM_IVALS] = {
68         {  1, 1 },
69         {  1, 2 },
70         {  1, 4 },
71         {  1, 5 },
72         {  1, 10 },
73         {  2, 25 },
74         {  1, 15 },
75         {  1, 25 },
76         {  1, 30 },
77         {  1, 40 },
78         {  1, 50 },
79         {  1, 60 },
80 };
81
82 static int vid_cap_queue_setup(struct vb2_queue *vq,
83                        unsigned *nbuffers, unsigned *nplanes,
84                        unsigned sizes[], struct device *alloc_devs[])
85 {
86         struct vivid_dev *dev = vb2_get_drv_priv(vq);
87         unsigned buffers = tpg_g_buffers(&dev->tpg);
88         unsigned h = dev->fmt_cap_rect.height;
89         unsigned p;
90
91         if (dev->field_cap == V4L2_FIELD_ALTERNATE) {
92                 /*
93                  * You cannot use read() with FIELD_ALTERNATE since the field
94                  * information (TOP/BOTTOM) cannot be passed back to the user.
95                  */
96                 if (vb2_fileio_is_active(vq))
97                         return -EINVAL;
98         }
99
100         if (dev->queue_setup_error) {
101                 /*
102                  * Error injection: test what happens if queue_setup() returns
103                  * an error.
104                  */
105                 dev->queue_setup_error = false;
106                 return -EINVAL;
107         }
108         if (*nplanes) {
109                 /*
110                  * Check if the number of requested planes match
111                  * the number of buffers in the current format. You can't mix that.
112                  */
113                 if (*nplanes != buffers)
114                         return -EINVAL;
115                 for (p = 0; p < buffers; p++) {
116                         if (sizes[p] < tpg_g_line_width(&dev->tpg, p) * h +
117                                                 dev->fmt_cap->data_offset[p])
118                                 return -EINVAL;
119                 }
120         } else {
121                 for (p = 0; p < buffers; p++)
122                         sizes[p] = (tpg_g_line_width(&dev->tpg, p) * h) /
123                                         dev->fmt_cap->vdownsampling[p] +
124                                         dev->fmt_cap->data_offset[p];
125         }
126
127         if (vq->num_buffers + *nbuffers < 2)
128                 *nbuffers = 2 - vq->num_buffers;
129
130         *nplanes = buffers;
131
132         dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
133         for (p = 0; p < buffers; p++)
134                 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
135
136         return 0;
137 }
138
139 static int vid_cap_buf_prepare(struct vb2_buffer *vb)
140 {
141         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
142         unsigned long size;
143         unsigned buffers = tpg_g_buffers(&dev->tpg);
144         unsigned p;
145
146         dprintk(dev, 1, "%s\n", __func__);
147
148         if (WARN_ON(NULL == dev->fmt_cap))
149                 return -EINVAL;
150
151         if (dev->buf_prepare_error) {
152                 /*
153                  * Error injection: test what happens if buf_prepare() returns
154                  * an error.
155                  */
156                 dev->buf_prepare_error = false;
157                 return -EINVAL;
158         }
159         for (p = 0; p < buffers; p++) {
160                 size = (tpg_g_line_width(&dev->tpg, p) *
161                         dev->fmt_cap_rect.height) /
162                         dev->fmt_cap->vdownsampling[p] +
163                         dev->fmt_cap->data_offset[p];
164
165                 if (vb2_plane_size(vb, p) < size) {
166                         dprintk(dev, 1, "%s data will not fit into plane %u (%lu < %lu)\n",
167                                         __func__, p, vb2_plane_size(vb, p), size);
168                         return -EINVAL;
169                 }
170
171                 vb2_set_plane_payload(vb, p, size);
172                 vb->planes[p].data_offset = dev->fmt_cap->data_offset[p];
173         }
174
175         return 0;
176 }
177
178 static void vid_cap_buf_finish(struct vb2_buffer *vb)
179 {
180         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
181         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
182         struct v4l2_timecode *tc = &vbuf->timecode;
183         unsigned fps = 25;
184         unsigned seq = vbuf->sequence;
185
186         if (!vivid_is_sdtv_cap(dev))
187                 return;
188
189         /*
190          * Set the timecode. Rarely used, so it is interesting to
191          * test this.
192          */
193         vbuf->flags |= V4L2_BUF_FLAG_TIMECODE;
194         if (dev->std_cap[dev->input] & V4L2_STD_525_60)
195                 fps = 30;
196         tc->type = (fps == 30) ? V4L2_TC_TYPE_30FPS : V4L2_TC_TYPE_25FPS;
197         tc->flags = 0;
198         tc->frames = seq % fps;
199         tc->seconds = (seq / fps) % 60;
200         tc->minutes = (seq / (60 * fps)) % 60;
201         tc->hours = (seq / (60 * 60 * fps)) % 24;
202 }
203
204 static void vid_cap_buf_queue(struct vb2_buffer *vb)
205 {
206         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
207         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
208         struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
209
210         dprintk(dev, 1, "%s\n", __func__);
211
212         spin_lock(&dev->slock);
213         list_add_tail(&buf->list, &dev->vid_cap_active);
214         spin_unlock(&dev->slock);
215 }
216
217 static int vid_cap_start_streaming(struct vb2_queue *vq, unsigned count)
218 {
219         struct vivid_dev *dev = vb2_get_drv_priv(vq);
220         unsigned i;
221         int err;
222
223         if (vb2_is_streaming(&dev->vb_vid_out_q))
224                 dev->can_loop_video = vivid_vid_can_loop(dev);
225
226         if (dev->kthread_vid_cap)
227                 return 0;
228
229         dev->vid_cap_seq_count = 0;
230         dprintk(dev, 1, "%s\n", __func__);
231         for (i = 0; i < VIDEO_MAX_FRAME; i++)
232                 dev->must_blank[i] = tpg_g_perc_fill(&dev->tpg) < 100;
233         if (dev->start_streaming_error) {
234                 dev->start_streaming_error = false;
235                 err = -EINVAL;
236         } else {
237                 err = vivid_start_generating_vid_cap(dev, &dev->vid_cap_streaming);
238         }
239         if (err) {
240                 struct vivid_buffer *buf, *tmp;
241
242                 list_for_each_entry_safe(buf, tmp, &dev->vid_cap_active, list) {
243                         list_del(&buf->list);
244                         vb2_buffer_done(&buf->vb.vb2_buf,
245                                         VB2_BUF_STATE_QUEUED);
246                 }
247         }
248         return err;
249 }
250
251 /* abort streaming and wait for last buffer */
252 static void vid_cap_stop_streaming(struct vb2_queue *vq)
253 {
254         struct vivid_dev *dev = vb2_get_drv_priv(vq);
255
256         dprintk(dev, 1, "%s\n", __func__);
257         vivid_stop_generating_vid_cap(dev, &dev->vid_cap_streaming);
258         dev->can_loop_video = false;
259 }
260
261 static void vid_cap_buf_request_complete(struct vb2_buffer *vb)
262 {
263         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
264
265         v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_vid_cap);
266 }
267
268 const struct vb2_ops vivid_vid_cap_qops = {
269         .queue_setup            = vid_cap_queue_setup,
270         .buf_prepare            = vid_cap_buf_prepare,
271         .buf_finish             = vid_cap_buf_finish,
272         .buf_queue              = vid_cap_buf_queue,
273         .start_streaming        = vid_cap_start_streaming,
274         .stop_streaming         = vid_cap_stop_streaming,
275         .buf_request_complete   = vid_cap_buf_request_complete,
276         .wait_prepare           = vb2_ops_wait_prepare,
277         .wait_finish            = vb2_ops_wait_finish,
278 };
279
280 /*
281  * Determine the 'picture' quality based on the current TV frequency: either
282  * COLOR for a good 'signal', GRAY (grayscale picture) for a slightly off
283  * signal or NOISE for no signal.
284  */
285 void vivid_update_quality(struct vivid_dev *dev)
286 {
287         unsigned freq_modulus;
288
289         if (dev->loop_video && (vivid_is_svid_cap(dev) || vivid_is_hdmi_cap(dev))) {
290                 /*
291                  * The 'noise' will only be replaced by the actual video
292                  * if the output video matches the input video settings.
293                  */
294                 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
295                 return;
296         }
297         if (vivid_is_hdmi_cap(dev) &&
298             VIVID_INVALID_SIGNAL(dev->dv_timings_signal_mode[dev->input])) {
299                 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
300                 return;
301         }
302         if (vivid_is_sdtv_cap(dev) &&
303             VIVID_INVALID_SIGNAL(dev->std_signal_mode[dev->input])) {
304                 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE, 0);
305                 return;
306         }
307         if (!vivid_is_tv_cap(dev)) {
308                 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
309                 return;
310         }
311
312         /*
313          * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
314          * From +/- 0.25 MHz around the channel there is color, and from
315          * +/- 1 MHz there is grayscale (chroma is lost).
316          * Everywhere else it is just noise.
317          */
318         freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
319         if (freq_modulus > 2 * 16) {
320                 tpg_s_quality(&dev->tpg, TPG_QUAL_NOISE,
321                         next_pseudo_random32(dev->tv_freq ^ 0x55) & 0x3f);
322                 return;
323         }
324         if (freq_modulus < 12 /*0.75 * 16*/ || freq_modulus > 20 /*1.25 * 16*/)
325                 tpg_s_quality(&dev->tpg, TPG_QUAL_GRAY, 0);
326         else
327                 tpg_s_quality(&dev->tpg, TPG_QUAL_COLOR, 0);
328 }
329
330 /*
331  * Get the current picture quality and the associated afc value.
332  */
333 static enum tpg_quality vivid_get_quality(struct vivid_dev *dev, s32 *afc)
334 {
335         unsigned freq_modulus;
336
337         if (afc)
338                 *afc = 0;
339         if (tpg_g_quality(&dev->tpg) == TPG_QUAL_COLOR ||
340             tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE)
341                 return tpg_g_quality(&dev->tpg);
342
343         /*
344          * There is a fake channel every 6 MHz at 49.25, 55.25, etc.
345          * From +/- 0.25 MHz around the channel there is color, and from
346          * +/- 1 MHz there is grayscale (chroma is lost).
347          * Everywhere else it is just gray.
348          */
349         freq_modulus = (dev->tv_freq - 676 /* (43.25-1) * 16 */) % (6 * 16);
350         if (afc)
351                 *afc = freq_modulus - 1 * 16;
352         return TPG_QUAL_GRAY;
353 }
354
355 enum tpg_video_aspect vivid_get_video_aspect(const struct vivid_dev *dev)
356 {
357         if (vivid_is_sdtv_cap(dev))
358                 return dev->std_aspect_ratio[dev->input];
359
360         if (vivid_is_hdmi_cap(dev))
361                 return dev->dv_timings_aspect_ratio[dev->input];
362
363         return TPG_VIDEO_ASPECT_IMAGE;
364 }
365
366 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
367 {
368         if (vivid_is_sdtv_cap(dev))
369                 return (dev->std_cap[dev->input] & V4L2_STD_525_60) ?
370                         TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
371
372         if (vivid_is_hdmi_cap(dev) &&
373             dev->src_rect.width == 720 && dev->src_rect.height <= 576)
374                 return dev->src_rect.height == 480 ?
375                         TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
376
377         return TPG_PIXEL_ASPECT_SQUARE;
378 }
379
380 /*
381  * Called whenever the format has to be reset which can occur when
382  * changing inputs, standard, timings, etc.
383  */
384 void vivid_update_format_cap(struct vivid_dev *dev, bool keep_controls)
385 {
386         struct v4l2_bt_timings *bt = &dev->dv_timings_cap[dev->input].bt;
387         unsigned size;
388         u64 pixelclock;
389
390         switch (dev->input_type[dev->input]) {
391         case WEBCAM:
392         default:
393                 dev->src_rect.width = webcam_sizes[dev->webcam_size_idx].width;
394                 dev->src_rect.height = webcam_sizes[dev->webcam_size_idx].height;
395                 dev->timeperframe_vid_cap = webcam_intervals[dev->webcam_ival_idx];
396                 dev->field_cap = V4L2_FIELD_NONE;
397                 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
398                 break;
399         case TV:
400         case SVID:
401                 dev->field_cap = dev->tv_field_cap;
402                 dev->src_rect.width = 720;
403                 if (dev->std_cap[dev->input] & V4L2_STD_525_60) {
404                         dev->src_rect.height = 480;
405                         dev->timeperframe_vid_cap = (struct v4l2_fract) { 1001, 30000 };
406                         dev->service_set_cap = V4L2_SLICED_CAPTION_525;
407                 } else {
408                         dev->src_rect.height = 576;
409                         dev->timeperframe_vid_cap = (struct v4l2_fract) { 1000, 25000 };
410                         dev->service_set_cap = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
411                 }
412                 tpg_s_rgb_range(&dev->tpg, V4L2_DV_RGB_RANGE_AUTO);
413                 break;
414         case HDMI:
415                 dev->src_rect.width = bt->width;
416                 dev->src_rect.height = bt->height;
417                 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
418                 if (dev->reduced_fps && can_reduce_fps(bt)) {
419                         pixelclock = div_u64(bt->pixelclock * 1000, 1001);
420                         bt->flags |= V4L2_DV_FL_REDUCED_FPS;
421                 } else {
422                         pixelclock = bt->pixelclock;
423                         bt->flags &= ~V4L2_DV_FL_REDUCED_FPS;
424                 }
425                 dev->timeperframe_vid_cap = (struct v4l2_fract) {
426                         size / 100, (u32)pixelclock / 100
427                 };
428                 if (bt->interlaced)
429                         dev->field_cap = V4L2_FIELD_ALTERNATE;
430                 else
431                         dev->field_cap = V4L2_FIELD_NONE;
432
433                 /*
434                  * We can be called from within s_ctrl, in that case we can't
435                  * set/get controls. Luckily we don't need to in that case.
436                  */
437                 if (keep_controls || !dev->colorspace)
438                         break;
439                 if (bt->flags & V4L2_DV_FL_IS_CE_VIDEO) {
440                         if (bt->width == 720 && bt->height <= 576)
441                                 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_170M);
442                         else
443                                 v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_709);
444                         v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 1);
445                 } else {
446                         v4l2_ctrl_s_ctrl(dev->colorspace, VIVID_CS_SRGB);
447                         v4l2_ctrl_s_ctrl(dev->real_rgb_range_cap, 0);
448                 }
449                 tpg_s_rgb_range(&dev->tpg, v4l2_ctrl_g_ctrl(dev->rgb_range_cap));
450                 break;
451         }
452         vfree(dev->bitmap_cap);
453         dev->bitmap_cap = NULL;
454         vivid_update_quality(dev);
455         tpg_reset_source(&dev->tpg, dev->src_rect.width, dev->src_rect.height, dev->field_cap);
456         dev->crop_cap = dev->src_rect;
457         dev->crop_bounds_cap = dev->src_rect;
458         dev->compose_cap = dev->crop_cap;
459         if (V4L2_FIELD_HAS_T_OR_B(dev->field_cap))
460                 dev->compose_cap.height /= 2;
461         dev->fmt_cap_rect = dev->compose_cap;
462         tpg_s_video_aspect(&dev->tpg, vivid_get_video_aspect(dev));
463         tpg_s_pixel_aspect(&dev->tpg, vivid_get_pixel_aspect(dev));
464         tpg_update_mv_step(&dev->tpg);
465 }
466
467 /* Map the field to something that is valid for the current input */
468 static enum v4l2_field vivid_field_cap(struct vivid_dev *dev, enum v4l2_field field)
469 {
470         if (vivid_is_sdtv_cap(dev)) {
471                 switch (field) {
472                 case V4L2_FIELD_INTERLACED_TB:
473                 case V4L2_FIELD_INTERLACED_BT:
474                 case V4L2_FIELD_SEQ_TB:
475                 case V4L2_FIELD_SEQ_BT:
476                 case V4L2_FIELD_TOP:
477                 case V4L2_FIELD_BOTTOM:
478                 case V4L2_FIELD_ALTERNATE:
479                         return field;
480                 case V4L2_FIELD_INTERLACED:
481                 default:
482                         return V4L2_FIELD_INTERLACED;
483                 }
484         }
485         if (vivid_is_hdmi_cap(dev))
486                 return dev->dv_timings_cap[dev->input].bt.interlaced ?
487                         V4L2_FIELD_ALTERNATE : V4L2_FIELD_NONE;
488         return V4L2_FIELD_NONE;
489 }
490
491 static unsigned vivid_colorspace_cap(struct vivid_dev *dev)
492 {
493         if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
494                 return tpg_g_colorspace(&dev->tpg);
495         return dev->colorspace_out;
496 }
497
498 static unsigned vivid_xfer_func_cap(struct vivid_dev *dev)
499 {
500         if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
501                 return tpg_g_xfer_func(&dev->tpg);
502         return dev->xfer_func_out;
503 }
504
505 static unsigned vivid_ycbcr_enc_cap(struct vivid_dev *dev)
506 {
507         if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
508                 return tpg_g_ycbcr_enc(&dev->tpg);
509         return dev->ycbcr_enc_out;
510 }
511
512 static unsigned int vivid_hsv_enc_cap(struct vivid_dev *dev)
513 {
514         if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
515                 return tpg_g_hsv_enc(&dev->tpg);
516         return dev->hsv_enc_out;
517 }
518
519 static unsigned vivid_quantization_cap(struct vivid_dev *dev)
520 {
521         if (!dev->loop_video || vivid_is_webcam(dev) || vivid_is_tv_cap(dev))
522                 return tpg_g_quantization(&dev->tpg);
523         return dev->quantization_out;
524 }
525
526 int vivid_g_fmt_vid_cap(struct file *file, void *priv,
527                                         struct v4l2_format *f)
528 {
529         struct vivid_dev *dev = video_drvdata(file);
530         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
531         unsigned p;
532
533         mp->width        = dev->fmt_cap_rect.width;
534         mp->height       = dev->fmt_cap_rect.height;
535         mp->field        = dev->field_cap;
536         mp->pixelformat  = dev->fmt_cap->fourcc;
537         mp->colorspace   = vivid_colorspace_cap(dev);
538         mp->xfer_func    = vivid_xfer_func_cap(dev);
539         if (dev->fmt_cap->color_enc == TGP_COLOR_ENC_HSV)
540                 mp->hsv_enc    = vivid_hsv_enc_cap(dev);
541         else
542                 mp->ycbcr_enc    = vivid_ycbcr_enc_cap(dev);
543         mp->quantization = vivid_quantization_cap(dev);
544         mp->num_planes = dev->fmt_cap->buffers;
545         for (p = 0; p < mp->num_planes; p++) {
546                 mp->plane_fmt[p].bytesperline = tpg_g_bytesperline(&dev->tpg, p);
547                 mp->plane_fmt[p].sizeimage =
548                         (tpg_g_line_width(&dev->tpg, p) * mp->height) /
549                         dev->fmt_cap->vdownsampling[p] +
550                         dev->fmt_cap->data_offset[p];
551         }
552         return 0;
553 }
554
555 int vivid_try_fmt_vid_cap(struct file *file, void *priv,
556                         struct v4l2_format *f)
557 {
558         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
559         struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
560         struct vivid_dev *dev = video_drvdata(file);
561         const struct vivid_fmt *fmt;
562         unsigned bytesperline, max_bpl;
563         unsigned factor = 1;
564         unsigned w, h;
565         unsigned p;
566
567         fmt = vivid_get_format(dev, mp->pixelformat);
568         if (!fmt) {
569                 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
570                         mp->pixelformat);
571                 mp->pixelformat = V4L2_PIX_FMT_YUYV;
572                 fmt = vivid_get_format(dev, mp->pixelformat);
573         }
574
575         mp->field = vivid_field_cap(dev, mp->field);
576         if (vivid_is_webcam(dev)) {
577                 const struct v4l2_frmsize_discrete *sz =
578                         v4l2_find_nearest_size(webcam_sizes,
579                                                VIVID_WEBCAM_SIZES, width,
580                                                height, mp->width, mp->height);
581
582                 w = sz->width;
583                 h = sz->height;
584         } else if (vivid_is_sdtv_cap(dev)) {
585                 w = 720;
586                 h = (dev->std_cap[dev->input] & V4L2_STD_525_60) ? 480 : 576;
587         } else {
588                 w = dev->src_rect.width;
589                 h = dev->src_rect.height;
590         }
591         if (V4L2_FIELD_HAS_T_OR_B(mp->field))
592                 factor = 2;
593         if (vivid_is_webcam(dev) ||
594             (!dev->has_scaler_cap && !dev->has_crop_cap && !dev->has_compose_cap)) {
595                 mp->width = w;
596                 mp->height = h / factor;
597         } else {
598                 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
599
600                 v4l2_rect_set_min_size(&r, &vivid_min_rect);
601                 v4l2_rect_set_max_size(&r, &vivid_max_rect);
602                 if (dev->has_scaler_cap && !dev->has_compose_cap) {
603                         struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
604
605                         v4l2_rect_set_max_size(&r, &max_r);
606                 } else if (!dev->has_scaler_cap && dev->has_crop_cap && !dev->has_compose_cap) {
607                         v4l2_rect_set_max_size(&r, &dev->src_rect);
608                 } else if (!dev->has_scaler_cap && !dev->has_crop_cap) {
609                         v4l2_rect_set_min_size(&r, &dev->src_rect);
610                 }
611                 mp->width = r.width;
612                 mp->height = r.height / factor;
613         }
614
615         /* This driver supports custom bytesperline values */
616
617         mp->num_planes = fmt->buffers;
618         for (p = 0; p < fmt->buffers; p++) {
619                 /* Calculate the minimum supported bytesperline value */
620                 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
621                 /* Calculate the maximum supported bytesperline value */
622                 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
623
624                 if (pfmt[p].bytesperline > max_bpl)
625                         pfmt[p].bytesperline = max_bpl;
626                 if (pfmt[p].bytesperline < bytesperline)
627                         pfmt[p].bytesperline = bytesperline;
628
629                 pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
630                                 fmt->vdownsampling[p] + fmt->data_offset[p];
631
632                 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
633         }
634         for (p = fmt->buffers; p < fmt->planes; p++)
635                 pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
636                         (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
637                         (fmt->bit_depth[0] / fmt->vdownsampling[0]);
638
639         mp->colorspace = vivid_colorspace_cap(dev);
640         if (fmt->color_enc == TGP_COLOR_ENC_HSV)
641                 mp->hsv_enc = vivid_hsv_enc_cap(dev);
642         else
643                 mp->ycbcr_enc = vivid_ycbcr_enc_cap(dev);
644         mp->xfer_func = vivid_xfer_func_cap(dev);
645         mp->quantization = vivid_quantization_cap(dev);
646         memset(mp->reserved, 0, sizeof(mp->reserved));
647         return 0;
648 }
649
650 int vivid_s_fmt_vid_cap(struct file *file, void *priv,
651                                         struct v4l2_format *f)
652 {
653         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
654         struct vivid_dev *dev = video_drvdata(file);
655         struct v4l2_rect *crop = &dev->crop_cap;
656         struct v4l2_rect *compose = &dev->compose_cap;
657         struct vb2_queue *q = &dev->vb_vid_cap_q;
658         int ret = vivid_try_fmt_vid_cap(file, priv, f);
659         unsigned factor = 1;
660         unsigned p;
661         unsigned i;
662
663         if (ret < 0)
664                 return ret;
665
666         if (vb2_is_busy(q)) {
667                 dprintk(dev, 1, "%s device busy\n", __func__);
668                 return -EBUSY;
669         }
670
671         if (dev->overlay_cap_owner && dev->fb_cap.fmt.pixelformat != mp->pixelformat) {
672                 dprintk(dev, 1, "overlay is active, can't change pixelformat\n");
673                 return -EBUSY;
674         }
675
676         dev->fmt_cap = vivid_get_format(dev, mp->pixelformat);
677         if (V4L2_FIELD_HAS_T_OR_B(mp->field))
678                 factor = 2;
679
680         /* Note: the webcam input doesn't support scaling, cropping or composing */
681
682         if (!vivid_is_webcam(dev) &&
683             (dev->has_scaler_cap || dev->has_crop_cap || dev->has_compose_cap)) {
684                 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
685
686                 if (dev->has_scaler_cap) {
687                         if (dev->has_compose_cap)
688                                 v4l2_rect_map_inside(compose, &r);
689                         else
690                                 *compose = r;
691                         if (dev->has_crop_cap && !dev->has_compose_cap) {
692                                 struct v4l2_rect min_r = {
693                                         0, 0,
694                                         r.width / MAX_ZOOM,
695                                         factor * r.height / MAX_ZOOM
696                                 };
697                                 struct v4l2_rect max_r = {
698                                         0, 0,
699                                         r.width * MAX_ZOOM,
700                                         factor * r.height * MAX_ZOOM
701                                 };
702
703                                 v4l2_rect_set_min_size(crop, &min_r);
704                                 v4l2_rect_set_max_size(crop, &max_r);
705                                 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
706                         } else if (dev->has_crop_cap) {
707                                 struct v4l2_rect min_r = {
708                                         0, 0,
709                                         compose->width / MAX_ZOOM,
710                                         factor * compose->height / MAX_ZOOM
711                                 };
712                                 struct v4l2_rect max_r = {
713                                         0, 0,
714                                         compose->width * MAX_ZOOM,
715                                         factor * compose->height * MAX_ZOOM
716                                 };
717
718                                 v4l2_rect_set_min_size(crop, &min_r);
719                                 v4l2_rect_set_max_size(crop, &max_r);
720                                 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
721                         }
722                 } else if (dev->has_crop_cap && !dev->has_compose_cap) {
723                         r.height *= factor;
724                         v4l2_rect_set_size_to(crop, &r);
725                         v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
726                         r = *crop;
727                         r.height /= factor;
728                         v4l2_rect_set_size_to(compose, &r);
729                 } else if (!dev->has_crop_cap) {
730                         v4l2_rect_map_inside(compose, &r);
731                 } else {
732                         r.height *= factor;
733                         v4l2_rect_set_max_size(crop, &r);
734                         v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
735                         compose->top *= factor;
736                         compose->height *= factor;
737                         v4l2_rect_set_size_to(compose, crop);
738                         v4l2_rect_map_inside(compose, &r);
739                         compose->top /= factor;
740                         compose->height /= factor;
741                 }
742         } else if (vivid_is_webcam(dev)) {
743                 /* Guaranteed to be a match */
744                 for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
745                         if (webcam_sizes[i].width == mp->width &&
746                                         webcam_sizes[i].height == mp->height)
747                                 break;
748                 dev->webcam_size_idx = i;
749                 if (dev->webcam_ival_idx >= 2 * (VIVID_WEBCAM_SIZES - i))
750                         dev->webcam_ival_idx = 2 * (VIVID_WEBCAM_SIZES - i) - 1;
751                 vivid_update_format_cap(dev, false);
752         } else {
753                 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
754
755                 v4l2_rect_set_size_to(compose, &r);
756                 r.height *= factor;
757                 v4l2_rect_set_size_to(crop, &r);
758         }
759
760         dev->fmt_cap_rect.width = mp->width;
761         dev->fmt_cap_rect.height = mp->height;
762         tpg_s_buf_height(&dev->tpg, mp->height);
763         tpg_s_fourcc(&dev->tpg, dev->fmt_cap->fourcc);
764         for (p = 0; p < tpg_g_buffers(&dev->tpg); p++)
765                 tpg_s_bytesperline(&dev->tpg, p, mp->plane_fmt[p].bytesperline);
766         dev->field_cap = mp->field;
767         if (dev->field_cap == V4L2_FIELD_ALTERNATE)
768                 tpg_s_field(&dev->tpg, V4L2_FIELD_TOP, true);
769         else
770                 tpg_s_field(&dev->tpg, dev->field_cap, false);
771         tpg_s_crop_compose(&dev->tpg, &dev->crop_cap, &dev->compose_cap);
772         if (vivid_is_sdtv_cap(dev))
773                 dev->tv_field_cap = mp->field;
774         tpg_update_mv_step(&dev->tpg);
775         return 0;
776 }
777
778 int vidioc_g_fmt_vid_cap_mplane(struct file *file, void *priv,
779                                         struct v4l2_format *f)
780 {
781         struct vivid_dev *dev = video_drvdata(file);
782
783         if (!dev->multiplanar)
784                 return -ENOTTY;
785         return vivid_g_fmt_vid_cap(file, priv, f);
786 }
787
788 int vidioc_try_fmt_vid_cap_mplane(struct file *file, void *priv,
789                         struct v4l2_format *f)
790 {
791         struct vivid_dev *dev = video_drvdata(file);
792
793         if (!dev->multiplanar)
794                 return -ENOTTY;
795         return vivid_try_fmt_vid_cap(file, priv, f);
796 }
797
798 int vidioc_s_fmt_vid_cap_mplane(struct file *file, void *priv,
799                         struct v4l2_format *f)
800 {
801         struct vivid_dev *dev = video_drvdata(file);
802
803         if (!dev->multiplanar)
804                 return -ENOTTY;
805         return vivid_s_fmt_vid_cap(file, priv, f);
806 }
807
808 int vidioc_g_fmt_vid_cap(struct file *file, void *priv,
809                                         struct v4l2_format *f)
810 {
811         struct vivid_dev *dev = video_drvdata(file);
812
813         if (dev->multiplanar)
814                 return -ENOTTY;
815         return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_cap);
816 }
817
818 int vidioc_try_fmt_vid_cap(struct file *file, void *priv,
819                         struct v4l2_format *f)
820 {
821         struct vivid_dev *dev = video_drvdata(file);
822
823         if (dev->multiplanar)
824                 return -ENOTTY;
825         return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_cap);
826 }
827
828 int vidioc_s_fmt_vid_cap(struct file *file, void *priv,
829                         struct v4l2_format *f)
830 {
831         struct vivid_dev *dev = video_drvdata(file);
832
833         if (dev->multiplanar)
834                 return -ENOTTY;
835         return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_cap);
836 }
837
838 int vivid_vid_cap_g_selection(struct file *file, void *priv,
839                               struct v4l2_selection *sel)
840 {
841         struct vivid_dev *dev = video_drvdata(file);
842
843         if (!dev->has_crop_cap && !dev->has_compose_cap)
844                 return -ENOTTY;
845         if (sel->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
846                 return -EINVAL;
847         if (vivid_is_webcam(dev))
848                 return -ENODATA;
849
850         sel->r.left = sel->r.top = 0;
851         switch (sel->target) {
852         case V4L2_SEL_TGT_CROP:
853                 if (!dev->has_crop_cap)
854                         return -EINVAL;
855                 sel->r = dev->crop_cap;
856                 break;
857         case V4L2_SEL_TGT_CROP_DEFAULT:
858         case V4L2_SEL_TGT_CROP_BOUNDS:
859                 if (!dev->has_crop_cap)
860                         return -EINVAL;
861                 sel->r = dev->src_rect;
862                 break;
863         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
864                 if (!dev->has_compose_cap)
865                         return -EINVAL;
866                 sel->r = vivid_max_rect;
867                 break;
868         case V4L2_SEL_TGT_COMPOSE:
869                 if (!dev->has_compose_cap)
870                         return -EINVAL;
871                 sel->r = dev->compose_cap;
872                 break;
873         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
874                 if (!dev->has_compose_cap)
875                         return -EINVAL;
876                 sel->r = dev->fmt_cap_rect;
877                 break;
878         default:
879                 return -EINVAL;
880         }
881         return 0;
882 }
883
884 int vivid_vid_cap_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
885 {
886         struct vivid_dev *dev = video_drvdata(file);
887         struct v4l2_rect *crop = &dev->crop_cap;
888         struct v4l2_rect *compose = &dev->compose_cap;
889         unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_cap) ? 2 : 1;
890         int ret;
891
892         if (!dev->has_crop_cap && !dev->has_compose_cap)
893                 return -ENOTTY;
894         if (s->type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
895                 return -EINVAL;
896         if (vivid_is_webcam(dev))
897                 return -ENODATA;
898
899         switch (s->target) {
900         case V4L2_SEL_TGT_CROP:
901                 if (!dev->has_crop_cap)
902                         return -EINVAL;
903                 ret = vivid_vid_adjust_sel(s->flags, &s->r);
904                 if (ret)
905                         return ret;
906                 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
907                 v4l2_rect_set_max_size(&s->r, &dev->src_rect);
908                 v4l2_rect_map_inside(&s->r, &dev->crop_bounds_cap);
909                 s->r.top /= factor;
910                 s->r.height /= factor;
911                 if (dev->has_scaler_cap) {
912                         struct v4l2_rect fmt = dev->fmt_cap_rect;
913                         struct v4l2_rect max_rect = {
914                                 0, 0,
915                                 s->r.width * MAX_ZOOM,
916                                 s->r.height * MAX_ZOOM
917                         };
918                         struct v4l2_rect min_rect = {
919                                 0, 0,
920                                 s->r.width / MAX_ZOOM,
921                                 s->r.height / MAX_ZOOM
922                         };
923
924                         v4l2_rect_set_min_size(&fmt, &min_rect);
925                         if (!dev->has_compose_cap)
926                                 v4l2_rect_set_max_size(&fmt, &max_rect);
927                         if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
928                             vb2_is_busy(&dev->vb_vid_cap_q))
929                                 return -EBUSY;
930                         if (dev->has_compose_cap) {
931                                 v4l2_rect_set_min_size(compose, &min_rect);
932                                 v4l2_rect_set_max_size(compose, &max_rect);
933                         }
934                         dev->fmt_cap_rect = fmt;
935                         tpg_s_buf_height(&dev->tpg, fmt.height);
936                 } else if (dev->has_compose_cap) {
937                         struct v4l2_rect fmt = dev->fmt_cap_rect;
938
939                         v4l2_rect_set_min_size(&fmt, &s->r);
940                         if (!v4l2_rect_same_size(&dev->fmt_cap_rect, &fmt) &&
941                             vb2_is_busy(&dev->vb_vid_cap_q))
942                                 return -EBUSY;
943                         dev->fmt_cap_rect = fmt;
944                         tpg_s_buf_height(&dev->tpg, fmt.height);
945                         v4l2_rect_set_size_to(compose, &s->r);
946                         v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
947                 } else {
948                         if (!v4l2_rect_same_size(&s->r, &dev->fmt_cap_rect) &&
949                             vb2_is_busy(&dev->vb_vid_cap_q))
950                                 return -EBUSY;
951                         v4l2_rect_set_size_to(&dev->fmt_cap_rect, &s->r);
952                         v4l2_rect_set_size_to(compose, &s->r);
953                         v4l2_rect_map_inside(compose, &dev->fmt_cap_rect);
954                         tpg_s_buf_height(&dev->tpg, dev->fmt_cap_rect.height);
955                 }
956                 s->r.top *= factor;
957                 s->r.height *= factor;
958                 *crop = s->r;
959                 break;
960         case V4L2_SEL_TGT_COMPOSE:
961                 if (!dev->has_compose_cap)
962                         return -EINVAL;
963                 ret = vivid_vid_adjust_sel(s->flags, &s->r);
964                 if (ret)
965                         return ret;
966                 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
967                 v4l2_rect_set_max_size(&s->r, &dev->fmt_cap_rect);
968                 if (dev->has_scaler_cap) {
969                         struct v4l2_rect max_rect = {
970                                 0, 0,
971                                 dev->src_rect.width * MAX_ZOOM,
972                                 (dev->src_rect.height / factor) * MAX_ZOOM
973                         };
974
975                         v4l2_rect_set_max_size(&s->r, &max_rect);
976                         if (dev->has_crop_cap) {
977                                 struct v4l2_rect min_rect = {
978                                         0, 0,
979                                         s->r.width / MAX_ZOOM,
980                                         (s->r.height * factor) / MAX_ZOOM
981                                 };
982                                 struct v4l2_rect max_rect = {
983                                         0, 0,
984                                         s->r.width * MAX_ZOOM,
985                                         (s->r.height * factor) * MAX_ZOOM
986                                 };
987
988                                 v4l2_rect_set_min_size(crop, &min_rect);
989                                 v4l2_rect_set_max_size(crop, &max_rect);
990                                 v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
991                         }
992                 } else if (dev->has_crop_cap) {
993                         s->r.top *= factor;
994                         s->r.height *= factor;
995                         v4l2_rect_set_max_size(&s->r, &dev->src_rect);
996                         v4l2_rect_set_size_to(crop, &s->r);
997                         v4l2_rect_map_inside(crop, &dev->crop_bounds_cap);
998                         s->r.top /= factor;
999                         s->r.height /= factor;
1000                 } else {
1001                         v4l2_rect_set_size_to(&s->r, &dev->src_rect);
1002                         s->r.height /= factor;
1003                 }
1004                 v4l2_rect_map_inside(&s->r, &dev->fmt_cap_rect);
1005                 if (dev->bitmap_cap && (compose->width != s->r.width ||
1006                                         compose->height != s->r.height)) {
1007                         vfree(dev->bitmap_cap);
1008                         dev->bitmap_cap = NULL;
1009                 }
1010                 *compose = s->r;
1011                 break;
1012         default:
1013                 return -EINVAL;
1014         }
1015
1016         tpg_s_crop_compose(&dev->tpg, crop, compose);
1017         return 0;
1018 }
1019
1020 int vivid_vid_cap_g_pixelaspect(struct file *file, void *priv,
1021                                 int type, struct v4l2_fract *f)
1022 {
1023         struct vivid_dev *dev = video_drvdata(file);
1024
1025         if (type != V4L2_BUF_TYPE_VIDEO_CAPTURE)
1026                 return -EINVAL;
1027
1028         switch (vivid_get_pixel_aspect(dev)) {
1029         case TPG_PIXEL_ASPECT_NTSC:
1030                 f->numerator = 11;
1031                 f->denominator = 10;
1032                 break;
1033         case TPG_PIXEL_ASPECT_PAL:
1034                 f->numerator = 54;
1035                 f->denominator = 59;
1036                 break;
1037         default:
1038                 break;
1039         }
1040         return 0;
1041 }
1042
1043 int vidioc_enum_fmt_vid_overlay(struct file *file, void  *priv,
1044                                         struct v4l2_fmtdesc *f)
1045 {
1046         struct vivid_dev *dev = video_drvdata(file);
1047         const struct vivid_fmt *fmt;
1048
1049         if (dev->multiplanar)
1050                 return -ENOTTY;
1051
1052         if (f->index >= ARRAY_SIZE(formats_ovl))
1053                 return -EINVAL;
1054
1055         fmt = &formats_ovl[f->index];
1056
1057         f->pixelformat = fmt->fourcc;
1058         return 0;
1059 }
1060
1061 int vidioc_g_fmt_vid_overlay(struct file *file, void *priv,
1062                                         struct v4l2_format *f)
1063 {
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;
1068
1069         if (dev->multiplanar)
1070                 return -ENOTTY;
1071
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;
1086         }
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;
1091         }
1092         return 0;
1093 }
1094
1095 int vidioc_try_fmt_vid_overlay(struct file *file, void *priv,
1096                                         struct v4l2_format *f)
1097 {
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;
1102
1103         if (dev->multiplanar)
1104                 return -ENOTTY;
1105
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;
1126
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);
1131                 }
1132                 /*
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.
1135                  */
1136                 for (i = 0; i < win->clipcount - 1; i++) {
1137                         struct v4l2_rect *r1 = &dev->try_clips_cap[i].c;
1138
1139                         for (j = i + 1; j < win->clipcount; j++) {
1140                                 struct v4l2_rect *r2 = &dev->try_clips_cap[j].c;
1141
1142                                 if (v4l2_rect_overlap(r1, r2))
1143                                         return -EINVAL;
1144                         }
1145                 }
1146                 if (copy_to_user(win->clips, dev->try_clips_cap,
1147                                  win->clipcount * sizeof(dev->clips_cap[0])))
1148                         return -EFAULT;
1149         }
1150         return 0;
1151 }
1152
1153 int vidioc_s_fmt_vid_overlay(struct file *file, void *priv,
1154                                         struct v4l2_format *f)
1155 {
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;
1163
1164         if (ret)
1165                 return ret;
1166
1167         if (win->bitmap) {
1168                 new_bitmap = vzalloc(bitmap_size);
1169
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;
1175                 }
1176         }
1177
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;
1187 }
1188
1189 int vivid_vid_cap_overlay(struct file *file, void *fh, unsigned i)
1190 {
1191         struct vivid_dev *dev = video_drvdata(file);
1192
1193         if (dev->multiplanar)
1194                 return -ENOTTY;
1195
1196         if (i && dev->fb_vbase_cap == NULL)
1197                 return -EINVAL;
1198
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;
1202         }
1203
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;
1208 }
1209
1210 int vivid_vid_cap_g_fbuf(struct file *file, void *fh,
1211                                 struct v4l2_framebuffer *a)
1212 {
1213         struct vivid_dev *dev = video_drvdata(file);
1214
1215         if (dev->multiplanar)
1216                 return -ENOTTY;
1217
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;
1226 }
1227
1228 int vivid_vid_cap_s_fbuf(struct file *file, void *fh,
1229                                 const struct v4l2_framebuffer *a)
1230 {
1231         struct vivid_dev *dev = video_drvdata(file);
1232         const struct vivid_fmt *fmt;
1233
1234         if (dev->multiplanar)
1235                 return -ENOTTY;
1236
1237         if (!capable(CAP_SYS_ADMIN) && !capable(CAP_SYS_RAWIO))
1238                 return -EPERM;
1239
1240         if (dev->overlay_cap_owner)
1241                 return -EBUSY;
1242
1243         if (a->base == NULL) {
1244                 dev->fb_cap.base = NULL;
1245                 dev->fb_vbase_cap = NULL;
1246                 return 0;
1247         }
1248
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;
1258
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;
1266 }
1267
1268 static const struct v4l2_audio vivid_audio_inputs[] = {
1269         { 0, "TV", V4L2_AUDCAP_STEREO },
1270         { 1, "Line-In", V4L2_AUDCAP_STEREO },
1271 };
1272
1273 int vidioc_enum_input(struct file *file, void *priv,
1274                                 struct v4l2_input *inp)
1275 {
1276         struct vivid_dev *dev = video_drvdata(file);
1277
1278         if (inp->index >= dev->num_inputs)
1279                 return -EINVAL;
1280
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[dev->input] == NO_SIGNAL)
1311                         inp->status |= V4L2_IN_ST_NO_SIGNAL;
1312                 else if (dev->dv_timings_signal_mode[dev->input] == NO_LOCK ||
1313                          dev->dv_timings_signal_mode[dev->input] == OUT_OF_RANGE)
1314                         inp->status |= V4L2_IN_ST_NO_H_LOCK;
1315                 break;
1316         }
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[dev->input] == NO_SIGNAL) {
1323                         inp->status |= V4L2_IN_ST_NO_SIGNAL;
1324                 } else if (dev->std_signal_mode[dev->input] == 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;
1336                         }
1337                 }
1338         }
1339         return 0;
1340 }
1341
1342 int vidioc_g_input(struct file *file, void *priv, unsigned *i)
1343 {
1344         struct vivid_dev *dev = video_drvdata(file);
1345
1346         *i = dev->input;
1347         return 0;
1348 }
1349
1350 int vidioc_s_input(struct file *file, void *priv, unsigned i)
1351 {
1352         struct vivid_dev *dev = video_drvdata(file);
1353         struct v4l2_bt_timings *bt = &dev->dv_timings_cap[dev->input].bt;
1354         unsigned brightness;
1355
1356         if (i >= dev->num_inputs)
1357                 return -EINVAL;
1358
1359         if (i == dev->input)
1360                 return 0;
1361
1362         if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1363                 return -EBUSY;
1364
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;
1370         }
1371         dev->vbi_cap_dev.tvnorms = dev->vid_cap_dev.tvnorms;
1372         vivid_update_format_cap(dev, false);
1373
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);
1391                         }
1392                         break;
1393                 }
1394         }
1395
1396         /*
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.
1402          */
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
1408         /* Restore per-input states. */
1409         v4l2_ctrl_activate(dev->ctrl_dv_timings_signal_mode,
1410                            vivid_is_hdmi_cap(dev));
1411         v4l2_ctrl_activate(dev->ctrl_dv_timings, vivid_is_hdmi_cap(dev) &&
1412                            dev->dv_timings_signal_mode[dev->input] ==
1413                            SELECTED_DV_TIMINGS);
1414         v4l2_ctrl_activate(dev->ctrl_std_signal_mode, vivid_is_sdtv_cap(dev));
1415         v4l2_ctrl_activate(dev->ctrl_standard, vivid_is_sdtv_cap(dev) &&
1416                            dev->std_signal_mode[dev->input]);
1417
1418         if (vivid_is_hdmi_cap(dev)) {
1419                 v4l2_ctrl_s_ctrl(dev->ctrl_dv_timings_signal_mode,
1420                                  dev->dv_timings_signal_mode[dev->input]);
1421                 v4l2_ctrl_s_ctrl(dev->ctrl_dv_timings,
1422                                  dev->query_dv_timings[dev->input]);
1423         } else if (vivid_is_sdtv_cap(dev)) {
1424                 v4l2_ctrl_s_ctrl(dev->ctrl_std_signal_mode,
1425                                  dev->std_signal_mode[dev->input]);
1426                 v4l2_ctrl_s_ctrl(dev->ctrl_standard,
1427                                  dev->std_signal_mode[dev->input]);
1428         }
1429
1430         return 0;
1431 }
1432
1433 int vidioc_enumaudio(struct file *file, void *fh, struct v4l2_audio *vin)
1434 {
1435         if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1436                 return -EINVAL;
1437         *vin = vivid_audio_inputs[vin->index];
1438         return 0;
1439 }
1440
1441 int vidioc_g_audio(struct file *file, void *fh, struct v4l2_audio *vin)
1442 {
1443         struct vivid_dev *dev = video_drvdata(file);
1444
1445         if (!vivid_is_sdtv_cap(dev))
1446                 return -EINVAL;
1447         *vin = vivid_audio_inputs[dev->tv_audio_input];
1448         return 0;
1449 }
1450
1451 int vidioc_s_audio(struct file *file, void *fh, const struct v4l2_audio *vin)
1452 {
1453         struct vivid_dev *dev = video_drvdata(file);
1454
1455         if (!vivid_is_sdtv_cap(dev))
1456                 return -EINVAL;
1457         if (vin->index >= ARRAY_SIZE(vivid_audio_inputs))
1458                 return -EINVAL;
1459         dev->tv_audio_input = vin->index;
1460         return 0;
1461 }
1462
1463 int vivid_video_g_frequency(struct file *file, void *fh, struct v4l2_frequency *vf)
1464 {
1465         struct vivid_dev *dev = video_drvdata(file);
1466
1467         if (vf->tuner != 0)
1468                 return -EINVAL;
1469         vf->frequency = dev->tv_freq;
1470         return 0;
1471 }
1472
1473 int vivid_video_s_frequency(struct file *file, void *fh, const struct v4l2_frequency *vf)
1474 {
1475         struct vivid_dev *dev = video_drvdata(file);
1476
1477         if (vf->tuner != 0)
1478                 return -EINVAL;
1479         dev->tv_freq = clamp_t(unsigned, vf->frequency, MIN_TV_FREQ, MAX_TV_FREQ);
1480         if (vivid_is_tv_cap(dev))
1481                 vivid_update_quality(dev);
1482         return 0;
1483 }
1484
1485 int vivid_video_s_tuner(struct file *file, void *fh, const struct v4l2_tuner *vt)
1486 {
1487         struct vivid_dev *dev = video_drvdata(file);
1488
1489         if (vt->index != 0)
1490                 return -EINVAL;
1491         if (vt->audmode > V4L2_TUNER_MODE_LANG1_LANG2)
1492                 return -EINVAL;
1493         dev->tv_audmode = vt->audmode;
1494         return 0;
1495 }
1496
1497 int vivid_video_g_tuner(struct file *file, void *fh, struct v4l2_tuner *vt)
1498 {
1499         struct vivid_dev *dev = video_drvdata(file);
1500         enum tpg_quality qual;
1501
1502         if (vt->index != 0)
1503                 return -EINVAL;
1504
1505         vt->capability = V4L2_TUNER_CAP_NORM | V4L2_TUNER_CAP_STEREO |
1506                          V4L2_TUNER_CAP_LANG1 | V4L2_TUNER_CAP_LANG2;
1507         vt->audmode = dev->tv_audmode;
1508         vt->rangelow = MIN_TV_FREQ;
1509         vt->rangehigh = MAX_TV_FREQ;
1510         qual = vivid_get_quality(dev, &vt->afc);
1511         if (qual == TPG_QUAL_COLOR)
1512                 vt->signal = 0xffff;
1513         else if (qual == TPG_QUAL_GRAY)
1514                 vt->signal = 0x8000;
1515         else
1516                 vt->signal = 0;
1517         if (qual == TPG_QUAL_NOISE) {
1518                 vt->rxsubchans = 0;
1519         } else if (qual == TPG_QUAL_GRAY) {
1520                 vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1521         } else {
1522                 unsigned int channel_nr = dev->tv_freq / (6 * 16);
1523                 unsigned int options =
1524                         (dev->std_cap[dev->input] & V4L2_STD_NTSC_M) ? 4 : 3;
1525
1526                 switch (channel_nr % options) {
1527                 case 0:
1528                         vt->rxsubchans = V4L2_TUNER_SUB_MONO;
1529                         break;
1530                 case 1:
1531                         vt->rxsubchans = V4L2_TUNER_SUB_STEREO;
1532                         break;
1533                 case 2:
1534                         if (dev->std_cap[dev->input] & V4L2_STD_NTSC_M)
1535                                 vt->rxsubchans = V4L2_TUNER_SUB_MONO | V4L2_TUNER_SUB_SAP;
1536                         else
1537                                 vt->rxsubchans = V4L2_TUNER_SUB_LANG1 | V4L2_TUNER_SUB_LANG2;
1538                         break;
1539                 case 3:
1540                         vt->rxsubchans = V4L2_TUNER_SUB_STEREO | V4L2_TUNER_SUB_SAP;
1541                         break;
1542                 }
1543         }
1544         strscpy(vt->name, "TV Tuner", sizeof(vt->name));
1545         return 0;
1546 }
1547
1548 /* Must remain in sync with the vivid_ctrl_standard_strings array */
1549 const v4l2_std_id vivid_standard[] = {
1550         V4L2_STD_NTSC_M,
1551         V4L2_STD_NTSC_M_JP,
1552         V4L2_STD_NTSC_M_KR,
1553         V4L2_STD_NTSC_443,
1554         V4L2_STD_PAL_BG | V4L2_STD_PAL_H,
1555         V4L2_STD_PAL_I,
1556         V4L2_STD_PAL_DK,
1557         V4L2_STD_PAL_M,
1558         V4L2_STD_PAL_N,
1559         V4L2_STD_PAL_Nc,
1560         V4L2_STD_PAL_60,
1561         V4L2_STD_SECAM_B | V4L2_STD_SECAM_G | V4L2_STD_SECAM_H,
1562         V4L2_STD_SECAM_DK,
1563         V4L2_STD_SECAM_L,
1564         V4L2_STD_SECAM_LC,
1565         V4L2_STD_UNKNOWN
1566 };
1567
1568 /* Must remain in sync with the vivid_standard array */
1569 const char * const vivid_ctrl_standard_strings[] = {
1570         "NTSC-M",
1571         "NTSC-M-JP",
1572         "NTSC-M-KR",
1573         "NTSC-443",
1574         "PAL-BGH",
1575         "PAL-I",
1576         "PAL-DK",
1577         "PAL-M",
1578         "PAL-N",
1579         "PAL-Nc",
1580         "PAL-60",
1581         "SECAM-BGH",
1582         "SECAM-DK",
1583         "SECAM-L",
1584         "SECAM-Lc",
1585         NULL,
1586 };
1587
1588 int vidioc_querystd(struct file *file, void *priv, v4l2_std_id *id)
1589 {
1590         struct vivid_dev *dev = video_drvdata(file);
1591         unsigned int last = dev->query_std_last[dev->input];
1592
1593         if (!vivid_is_sdtv_cap(dev))
1594                 return -ENODATA;
1595         if (dev->std_signal_mode[dev->input] == NO_SIGNAL ||
1596             dev->std_signal_mode[dev->input] == NO_LOCK) {
1597                 *id = V4L2_STD_UNKNOWN;
1598                 return 0;
1599         }
1600         if (vivid_is_tv_cap(dev) && tpg_g_quality(&dev->tpg) == TPG_QUAL_NOISE) {
1601                 *id = V4L2_STD_UNKNOWN;
1602         } else if (dev->std_signal_mode[dev->input] == CURRENT_STD) {
1603                 *id = dev->std_cap[dev->input];
1604         } else if (dev->std_signal_mode[dev->input] == SELECTED_STD) {
1605                 *id = dev->query_std[dev->input];
1606         } else {
1607                 *id = vivid_standard[last];
1608                 dev->query_std_last[dev->input] =
1609                         (last + 1) % ARRAY_SIZE(vivid_standard);
1610         }
1611
1612         return 0;
1613 }
1614
1615 int vivid_vid_cap_s_std(struct file *file, void *priv, v4l2_std_id id)
1616 {
1617         struct vivid_dev *dev = video_drvdata(file);
1618
1619         if (!vivid_is_sdtv_cap(dev))
1620                 return -ENODATA;
1621         if (dev->std_cap[dev->input] == id)
1622                 return 0;
1623         if (vb2_is_busy(&dev->vb_vid_cap_q) || vb2_is_busy(&dev->vb_vbi_cap_q))
1624                 return -EBUSY;
1625         dev->std_cap[dev->input] = id;
1626         vivid_update_format_cap(dev, false);
1627         return 0;
1628 }
1629
1630 static void find_aspect_ratio(u32 width, u32 height,
1631                                u32 *num, u32 *denom)
1632 {
1633         if (!(height % 3) && ((height * 4 / 3) == width)) {
1634                 *num = 4;
1635                 *denom = 3;
1636         } else if (!(height % 9) && ((height * 16 / 9) == width)) {
1637                 *num = 16;
1638                 *denom = 9;
1639         } else if (!(height % 10) && ((height * 16 / 10) == width)) {
1640                 *num = 16;
1641                 *denom = 10;
1642         } else if (!(height % 4) && ((height * 5 / 4) == width)) {
1643                 *num = 5;
1644                 *denom = 4;
1645         } else if (!(height % 9) && ((height * 15 / 9) == width)) {
1646                 *num = 15;
1647                 *denom = 9;
1648         } else { /* default to 16:9 */
1649                 *num = 16;
1650                 *denom = 9;
1651         }
1652 }
1653
1654 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1655 {
1656         struct v4l2_bt_timings *bt = &timings->bt;
1657         u32 total_h_pixel;
1658         u32 total_v_lines;
1659         u32 h_freq;
1660
1661         if (!v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap,
1662                                 NULL, NULL))
1663                 return false;
1664
1665         total_h_pixel = V4L2_DV_BT_FRAME_WIDTH(bt);
1666         total_v_lines = V4L2_DV_BT_FRAME_HEIGHT(bt);
1667
1668         h_freq = (u32)bt->pixelclock / total_h_pixel;
1669
1670         if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_CVT)) {
1671                 if (v4l2_detect_cvt(total_v_lines, h_freq, bt->vsync, bt->width,
1672                                     bt->polarities, bt->interlaced, timings))
1673                         return true;
1674         }
1675
1676         if (bt->standards == 0 || (bt->standards & V4L2_DV_BT_STD_GTF)) {
1677                 struct v4l2_fract aspect_ratio;
1678
1679                 find_aspect_ratio(bt->width, bt->height,
1680                                   &aspect_ratio.numerator,
1681                                   &aspect_ratio.denominator);
1682                 if (v4l2_detect_gtf(total_v_lines, h_freq, bt->vsync,
1683                                     bt->polarities, bt->interlaced,
1684                                     aspect_ratio, timings))
1685                         return true;
1686         }
1687         return false;
1688 }
1689
1690 int vivid_vid_cap_s_dv_timings(struct file *file, void *_fh,
1691                                     struct v4l2_dv_timings *timings)
1692 {
1693         struct vivid_dev *dev = video_drvdata(file);
1694
1695         if (!vivid_is_hdmi_cap(dev))
1696                 return -ENODATA;
1697         if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1698                                       0, NULL, NULL) &&
1699             !valid_cvt_gtf_timings(timings))
1700                 return -EINVAL;
1701
1702         if (v4l2_match_dv_timings(timings, &dev->dv_timings_cap[dev->input],
1703                                   0, false))
1704                 return 0;
1705         if (vb2_is_busy(&dev->vb_vid_cap_q))
1706                 return -EBUSY;
1707
1708         dev->dv_timings_cap[dev->input] = *timings;
1709         vivid_update_format_cap(dev, false);
1710         return 0;
1711 }
1712
1713 int vidioc_query_dv_timings(struct file *file, void *_fh,
1714                                     struct v4l2_dv_timings *timings)
1715 {
1716         struct vivid_dev *dev = video_drvdata(file);
1717         unsigned int input = dev->input;
1718         unsigned int last = dev->query_dv_timings_last[input];
1719
1720         if (!vivid_is_hdmi_cap(dev))
1721                 return -ENODATA;
1722         if (dev->dv_timings_signal_mode[input] == NO_SIGNAL ||
1723             dev->edid_blocks == 0)
1724                 return -ENOLINK;
1725         if (dev->dv_timings_signal_mode[input] == NO_LOCK)
1726                 return -ENOLCK;
1727         if (dev->dv_timings_signal_mode[input] == OUT_OF_RANGE) {
1728                 timings->bt.pixelclock = vivid_dv_timings_cap.bt.max_pixelclock * 2;
1729                 return -ERANGE;
1730         }
1731         if (dev->dv_timings_signal_mode[input] == CURRENT_DV_TIMINGS) {
1732                 *timings = dev->dv_timings_cap[input];
1733         } else if (dev->dv_timings_signal_mode[input] ==
1734                    SELECTED_DV_TIMINGS) {
1735                 *timings =
1736                         v4l2_dv_timings_presets[dev->query_dv_timings[input]];
1737         } else {
1738                 *timings =
1739                         v4l2_dv_timings_presets[last];
1740                 dev->query_dv_timings_last[input] =
1741                         (last + 1) % dev->query_dv_timings_size;
1742         }
1743         return 0;
1744 }
1745
1746 int vidioc_s_edid(struct file *file, void *_fh,
1747                          struct v4l2_edid *edid)
1748 {
1749         struct vivid_dev *dev = video_drvdata(file);
1750         u16 phys_addr;
1751         unsigned int i;
1752         int ret;
1753
1754         memset(edid->reserved, 0, sizeof(edid->reserved));
1755         if (edid->pad >= dev->num_inputs)
1756                 return -EINVAL;
1757         if (dev->input_type[edid->pad] != HDMI || edid->start_block)
1758                 return -EINVAL;
1759         if (edid->blocks == 0) {
1760                 dev->edid_blocks = 0;
1761                 phys_addr = CEC_PHYS_ADDR_INVALID;
1762                 goto set_phys_addr;
1763         }
1764         if (edid->blocks > dev->edid_max_blocks) {
1765                 edid->blocks = dev->edid_max_blocks;
1766                 return -E2BIG;
1767         }
1768         phys_addr = cec_get_edid_phys_addr(edid->edid, edid->blocks * 128, NULL);
1769         ret = v4l2_phys_addr_validate(phys_addr, &phys_addr, NULL);
1770         if (ret)
1771                 return ret;
1772
1773         if (vb2_is_busy(&dev->vb_vid_cap_q))
1774                 return -EBUSY;
1775
1776         dev->edid_blocks = edid->blocks;
1777         memcpy(dev->edid, edid->edid, edid->blocks * 128);
1778
1779 set_phys_addr:
1780         /* TODO: a proper hotplug detect cycle should be emulated here */
1781         cec_s_phys_addr(dev->cec_rx_adap, phys_addr, false);
1782
1783         for (i = 0; i < MAX_OUTPUTS && dev->cec_tx_adap[i]; i++)
1784                 cec_s_phys_addr(dev->cec_tx_adap[i],
1785                                 v4l2_phys_addr_for_input(phys_addr, i + 1),
1786                                 false);
1787         return 0;
1788 }
1789
1790 int vidioc_enum_framesizes(struct file *file, void *fh,
1791                                          struct v4l2_frmsizeenum *fsize)
1792 {
1793         struct vivid_dev *dev = video_drvdata(file);
1794
1795         if (!vivid_is_webcam(dev) && !dev->has_scaler_cap)
1796                 return -EINVAL;
1797         if (vivid_get_format(dev, fsize->pixel_format) == NULL)
1798                 return -EINVAL;
1799         if (vivid_is_webcam(dev)) {
1800                 if (fsize->index >= ARRAY_SIZE(webcam_sizes))
1801                         return -EINVAL;
1802                 fsize->type = V4L2_FRMSIZE_TYPE_DISCRETE;
1803                 fsize->discrete = webcam_sizes[fsize->index];
1804                 return 0;
1805         }
1806         if (fsize->index)
1807                 return -EINVAL;
1808         fsize->type = V4L2_FRMSIZE_TYPE_STEPWISE;
1809         fsize->stepwise.min_width = MIN_WIDTH;
1810         fsize->stepwise.max_width = MAX_WIDTH * MAX_ZOOM;
1811         fsize->stepwise.step_width = 2;
1812         fsize->stepwise.min_height = MIN_HEIGHT;
1813         fsize->stepwise.max_height = MAX_HEIGHT * MAX_ZOOM;
1814         fsize->stepwise.step_height = 2;
1815         return 0;
1816 }
1817
1818 /* timeperframe is arbitrary and continuous */
1819 int vidioc_enum_frameintervals(struct file *file, void *priv,
1820                                              struct v4l2_frmivalenum *fival)
1821 {
1822         struct vivid_dev *dev = video_drvdata(file);
1823         const struct vivid_fmt *fmt;
1824         int i;
1825
1826         fmt = vivid_get_format(dev, fival->pixel_format);
1827         if (!fmt)
1828                 return -EINVAL;
1829
1830         if (!vivid_is_webcam(dev)) {
1831                 if (fival->index)
1832                         return -EINVAL;
1833                 if (fival->width < MIN_WIDTH || fival->width > MAX_WIDTH * MAX_ZOOM)
1834                         return -EINVAL;
1835                 if (fival->height < MIN_HEIGHT || fival->height > MAX_HEIGHT * MAX_ZOOM)
1836                         return -EINVAL;
1837                 fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1838                 fival->discrete = dev->timeperframe_vid_cap;
1839                 return 0;
1840         }
1841
1842         for (i = 0; i < ARRAY_SIZE(webcam_sizes); i++)
1843                 if (fival->width == webcam_sizes[i].width &&
1844                     fival->height == webcam_sizes[i].height)
1845                         break;
1846         if (i == ARRAY_SIZE(webcam_sizes))
1847                 return -EINVAL;
1848         if (fival->index >= 2 * (VIVID_WEBCAM_SIZES - i))
1849                 return -EINVAL;
1850         fival->type = V4L2_FRMIVAL_TYPE_DISCRETE;
1851         fival->discrete = webcam_intervals[fival->index];
1852         return 0;
1853 }
1854
1855 int vivid_vid_cap_g_parm(struct file *file, void *priv,
1856                           struct v4l2_streamparm *parm)
1857 {
1858         struct vivid_dev *dev = video_drvdata(file);
1859
1860         if (parm->type != (dev->multiplanar ?
1861                            V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1862                            V4L2_BUF_TYPE_VIDEO_CAPTURE))
1863                 return -EINVAL;
1864
1865         parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1866         parm->parm.capture.timeperframe = dev->timeperframe_vid_cap;
1867         parm->parm.capture.readbuffers  = 1;
1868         return 0;
1869 }
1870
1871 int vivid_vid_cap_s_parm(struct file *file, void *priv,
1872                           struct v4l2_streamparm *parm)
1873 {
1874         struct vivid_dev *dev = video_drvdata(file);
1875         unsigned ival_sz = 2 * (VIVID_WEBCAM_SIZES - dev->webcam_size_idx);
1876         struct v4l2_fract tpf;
1877         unsigned i;
1878
1879         if (parm->type != (dev->multiplanar ?
1880                            V4L2_BUF_TYPE_VIDEO_CAPTURE_MPLANE :
1881                            V4L2_BUF_TYPE_VIDEO_CAPTURE))
1882                 return -EINVAL;
1883         if (!vivid_is_webcam(dev))
1884                 return vivid_vid_cap_g_parm(file, priv, parm);
1885
1886         tpf = parm->parm.capture.timeperframe;
1887
1888         if (tpf.denominator == 0)
1889                 tpf = webcam_intervals[ival_sz - 1];
1890         for (i = 0; i < ival_sz; i++)
1891                 if (V4L2_FRACT_COMPARE(tpf, >=, webcam_intervals[i]))
1892                         break;
1893         if (i == ival_sz)
1894                 i = ival_sz - 1;
1895         dev->webcam_ival_idx = i;
1896         tpf = webcam_intervals[dev->webcam_ival_idx];
1897
1898         /* resync the thread's timings */
1899         dev->cap_seq_resync = true;
1900         dev->timeperframe_vid_cap = tpf;
1901         parm->parm.capture.capability   = V4L2_CAP_TIMEPERFRAME;
1902         parm->parm.capture.timeperframe = tpf;
1903         parm->parm.capture.readbuffers  = 1;
1904         return 0;
1905 }