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