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