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