GNU Linux-libre 5.10.215-gnu1
[releases.git] / drivers / media / test-drivers / vivid / vivid-vid-out.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * vivid-vid-out.c - video output 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/videodev2.h>
12 #include <linux/v4l2-dv-timings.h>
13 #include <media/v4l2-common.h>
14 #include <media/v4l2-event.h>
15 #include <media/v4l2-dv-timings.h>
16 #include <media/v4l2-rect.h>
17
18 #include "vivid-core.h"
19 #include "vivid-vid-common.h"
20 #include "vivid-kthread-out.h"
21 #include "vivid-vid-out.h"
22
23 static int vid_out_queue_setup(struct vb2_queue *vq,
24                        unsigned *nbuffers, unsigned *nplanes,
25                        unsigned sizes[], struct device *alloc_devs[])
26 {
27         struct vivid_dev *dev = vb2_get_drv_priv(vq);
28         const struct vivid_fmt *vfmt = dev->fmt_out;
29         unsigned planes = vfmt->buffers;
30         unsigned h = dev->fmt_out_rect.height;
31         unsigned int size = dev->bytesperline_out[0] * h + vfmt->data_offset[0];
32         unsigned p;
33
34         for (p = vfmt->buffers; p < vfmt->planes; p++)
35                 size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p] +
36                         vfmt->data_offset[p];
37
38         if (dev->field_out == V4L2_FIELD_ALTERNATE) {
39                 /*
40                  * You cannot use write() with FIELD_ALTERNATE since the field
41                  * information (TOP/BOTTOM) cannot be passed to the kernel.
42                  */
43                 if (vb2_fileio_is_active(vq))
44                         return -EINVAL;
45         }
46
47         if (dev->queue_setup_error) {
48                 /*
49                  * Error injection: test what happens if queue_setup() returns
50                  * an error.
51                  */
52                 dev->queue_setup_error = false;
53                 return -EINVAL;
54         }
55
56         if (*nplanes) {
57                 /*
58                  * Check if the number of requested planes match
59                  * the number of planes in the current format. You can't mix that.
60                  */
61                 if (*nplanes != planes)
62                         return -EINVAL;
63                 if (sizes[0] < size)
64                         return -EINVAL;
65                 for (p = 1; p < planes; p++) {
66                         if (sizes[p] < dev->bytesperline_out[p] * h +
67                                        vfmt->data_offset[p])
68                                 return -EINVAL;
69                 }
70         } else {
71                 for (p = 0; p < planes; p++)
72                         sizes[p] = p ? dev->bytesperline_out[p] * h +
73                                        vfmt->data_offset[p] : size;
74         }
75
76         if (vq->num_buffers + *nbuffers < 2)
77                 *nbuffers = 2 - vq->num_buffers;
78
79         *nplanes = planes;
80
81         dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
82         for (p = 0; p < planes; p++)
83                 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
84         return 0;
85 }
86
87 static int vid_out_buf_out_validate(struct vb2_buffer *vb)
88 {
89         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
90         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
91
92         dprintk(dev, 1, "%s\n", __func__);
93
94         if (dev->field_out != V4L2_FIELD_ALTERNATE)
95                 vbuf->field = dev->field_out;
96         else if (vbuf->field != V4L2_FIELD_TOP &&
97                  vbuf->field != V4L2_FIELD_BOTTOM)
98                 return -EINVAL;
99         return 0;
100 }
101
102 static int vid_out_buf_prepare(struct vb2_buffer *vb)
103 {
104         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
105         const struct vivid_fmt *vfmt = dev->fmt_out;
106         unsigned int planes = vfmt->buffers;
107         unsigned int h = dev->fmt_out_rect.height;
108         unsigned int size = dev->bytesperline_out[0] * h;
109         unsigned p;
110
111         for (p = vfmt->buffers; p < vfmt->planes; p++)
112                 size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
113
114         dprintk(dev, 1, "%s\n", __func__);
115
116         if (WARN_ON(NULL == dev->fmt_out))
117                 return -EINVAL;
118
119         if (dev->buf_prepare_error) {
120                 /*
121                  * Error injection: test what happens if buf_prepare() returns
122                  * an error.
123                  */
124                 dev->buf_prepare_error = false;
125                 return -EINVAL;
126         }
127
128         for (p = 0; p < planes; p++) {
129                 if (p)
130                         size = dev->bytesperline_out[p] * h;
131                 size += vb->planes[p].data_offset;
132
133                 if (vb2_get_plane_payload(vb, p) < size) {
134                         dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %u)\n",
135                                         __func__, p, vb2_get_plane_payload(vb, p), size);
136                         return -EINVAL;
137                 }
138         }
139
140         return 0;
141 }
142
143 static void vid_out_buf_queue(struct vb2_buffer *vb)
144 {
145         struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
146         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
147         struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
148
149         dprintk(dev, 1, "%s\n", __func__);
150
151         spin_lock(&dev->slock);
152         list_add_tail(&buf->list, &dev->vid_out_active);
153         spin_unlock(&dev->slock);
154 }
155
156 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
157 {
158         struct vivid_dev *dev = vb2_get_drv_priv(vq);
159         int err;
160
161         if (vb2_is_streaming(&dev->vb_vid_cap_q))
162                 dev->can_loop_video = vivid_vid_can_loop(dev);
163
164         dev->vid_out_seq_count = 0;
165         dprintk(dev, 1, "%s\n", __func__);
166         if (dev->start_streaming_error) {
167                 dev->start_streaming_error = false;
168                 err = -EINVAL;
169         } else {
170                 err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
171         }
172         if (err) {
173                 struct vivid_buffer *buf, *tmp;
174
175                 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
176                         list_del(&buf->list);
177                         vb2_buffer_done(&buf->vb.vb2_buf,
178                                         VB2_BUF_STATE_QUEUED);
179                 }
180         }
181         return err;
182 }
183
184 /* abort streaming and wait for last buffer */
185 static void vid_out_stop_streaming(struct vb2_queue *vq)
186 {
187         struct vivid_dev *dev = vb2_get_drv_priv(vq);
188
189         dprintk(dev, 1, "%s\n", __func__);
190         vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
191         dev->can_loop_video = false;
192 }
193
194 static void vid_out_buf_request_complete(struct vb2_buffer *vb)
195 {
196         struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
197
198         v4l2_ctrl_request_complete(vb->req_obj.req, &dev->ctrl_hdl_vid_out);
199 }
200
201 const struct vb2_ops vivid_vid_out_qops = {
202         .queue_setup            = vid_out_queue_setup,
203         .buf_out_validate               = vid_out_buf_out_validate,
204         .buf_prepare            = vid_out_buf_prepare,
205         .buf_queue              = vid_out_buf_queue,
206         .start_streaming        = vid_out_start_streaming,
207         .stop_streaming         = vid_out_stop_streaming,
208         .buf_request_complete   = vid_out_buf_request_complete,
209         .wait_prepare           = vb2_ops_wait_prepare,
210         .wait_finish            = vb2_ops_wait_finish,
211 };
212
213 /*
214  * Called whenever the format has to be reset which can occur when
215  * changing outputs, standard, timings, etc.
216  */
217 void vivid_update_format_out(struct vivid_dev *dev)
218 {
219         struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
220         unsigned size, p;
221         u64 pixelclock;
222
223         switch (dev->output_type[dev->output]) {
224         case SVID:
225         default:
226                 dev->field_out = dev->tv_field_out;
227                 dev->sink_rect.width = 720;
228                 if (dev->std_out & V4L2_STD_525_60) {
229                         dev->sink_rect.height = 480;
230                         dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
231                         dev->service_set_out = V4L2_SLICED_CAPTION_525;
232                 } else {
233                         dev->sink_rect.height = 576;
234                         dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
235                         dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
236                 }
237                 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
238                 break;
239         case HDMI:
240                 dev->sink_rect.width = bt->width;
241                 dev->sink_rect.height = bt->height;
242                 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
243
244                 if (can_reduce_fps(bt) && (bt->flags & V4L2_DV_FL_REDUCED_FPS))
245                         pixelclock = div_u64(bt->pixelclock * 1000, 1001);
246                 else
247                         pixelclock = bt->pixelclock;
248
249                 dev->timeperframe_vid_out = (struct v4l2_fract) {
250                         size / 100, (u32)pixelclock / 100
251                 };
252                 if (bt->interlaced)
253                         dev->field_out = V4L2_FIELD_ALTERNATE;
254                 else
255                         dev->field_out = V4L2_FIELD_NONE;
256                 if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
257                         if (bt->width == 720 && bt->height <= 576)
258                                 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
259                         else
260                                 dev->colorspace_out = V4L2_COLORSPACE_REC709;
261                 } else {
262                         dev->colorspace_out = V4L2_COLORSPACE_SRGB;
263                 }
264                 break;
265         }
266         dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
267         dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
268         dev->hsv_enc_out = V4L2_HSV_ENC_180;
269         dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
270         dev->compose_out = dev->sink_rect;
271         dev->compose_bounds_out = dev->sink_rect;
272         dev->crop_out = dev->compose_out;
273         if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
274                 dev->crop_out.height /= 2;
275         dev->fmt_out_rect = dev->crop_out;
276         for (p = 0; p < dev->fmt_out->planes; p++)
277                 dev->bytesperline_out[p] =
278                         (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
279 }
280
281 /* Map the field to something that is valid for the current output */
282 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
283 {
284         if (vivid_is_svid_out(dev)) {
285                 switch (field) {
286                 case V4L2_FIELD_INTERLACED_TB:
287                 case V4L2_FIELD_INTERLACED_BT:
288                 case V4L2_FIELD_SEQ_TB:
289                 case V4L2_FIELD_SEQ_BT:
290                 case V4L2_FIELD_ALTERNATE:
291                         return field;
292                 case V4L2_FIELD_INTERLACED:
293                 default:
294                         return V4L2_FIELD_INTERLACED;
295                 }
296         }
297         if (vivid_is_hdmi_out(dev))
298                 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
299                                                        V4L2_FIELD_NONE;
300         return V4L2_FIELD_NONE;
301 }
302
303 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
304 {
305         if (vivid_is_svid_out(dev))
306                 return (dev->std_out & V4L2_STD_525_60) ?
307                         TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
308
309         if (vivid_is_hdmi_out(dev) &&
310             dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
311                 return dev->sink_rect.height == 480 ?
312                         TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
313
314         return TPG_PIXEL_ASPECT_SQUARE;
315 }
316
317 int vivid_g_fmt_vid_out(struct file *file, void *priv,
318                                         struct v4l2_format *f)
319 {
320         struct vivid_dev *dev = video_drvdata(file);
321         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
322         const struct vivid_fmt *fmt = dev->fmt_out;
323         unsigned p;
324
325         mp->width        = dev->fmt_out_rect.width;
326         mp->height       = dev->fmt_out_rect.height;
327         mp->field        = dev->field_out;
328         mp->pixelformat  = fmt->fourcc;
329         mp->colorspace   = dev->colorspace_out;
330         mp->xfer_func    = dev->xfer_func_out;
331         mp->ycbcr_enc    = dev->ycbcr_enc_out;
332         mp->quantization = dev->quantization_out;
333         mp->num_planes = fmt->buffers;
334         for (p = 0; p < mp->num_planes; p++) {
335                 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
336                 mp->plane_fmt[p].sizeimage =
337                         mp->plane_fmt[p].bytesperline * mp->height +
338                         fmt->data_offset[p];
339         }
340         for (p = fmt->buffers; p < fmt->planes; p++) {
341                 unsigned stride = dev->bytesperline_out[p];
342
343                 mp->plane_fmt[0].sizeimage +=
344                         (stride * mp->height) / fmt->vdownsampling[p];
345         }
346         return 0;
347 }
348
349 int vivid_try_fmt_vid_out(struct file *file, void *priv,
350                         struct v4l2_format *f)
351 {
352         struct vivid_dev *dev = video_drvdata(file);
353         struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
354         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
355         struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
356         const struct vivid_fmt *fmt;
357         unsigned bytesperline, max_bpl;
358         unsigned factor = 1;
359         unsigned w, h;
360         unsigned p;
361
362         fmt = vivid_get_format(dev, mp->pixelformat);
363         if (!fmt) {
364                 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
365                         mp->pixelformat);
366                 mp->pixelformat = V4L2_PIX_FMT_YUYV;
367                 fmt = vivid_get_format(dev, mp->pixelformat);
368         }
369
370         mp->field = vivid_field_out(dev, mp->field);
371         if (vivid_is_svid_out(dev)) {
372                 w = 720;
373                 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
374         } else {
375                 w = dev->sink_rect.width;
376                 h = dev->sink_rect.height;
377         }
378         if (V4L2_FIELD_HAS_T_OR_B(mp->field))
379                 factor = 2;
380         if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
381                 mp->width = w;
382                 mp->height = h / factor;
383         } else {
384                 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
385
386                 v4l2_rect_set_min_size(&r, &vivid_min_rect);
387                 v4l2_rect_set_max_size(&r, &vivid_max_rect);
388                 if (dev->has_scaler_out && !dev->has_crop_out) {
389                         struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
390
391                         v4l2_rect_set_max_size(&r, &max_r);
392                 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
393                         v4l2_rect_set_max_size(&r, &dev->sink_rect);
394                 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
395                         v4l2_rect_set_min_size(&r, &dev->sink_rect);
396                 }
397                 mp->width = r.width;
398                 mp->height = r.height / factor;
399         }
400
401         /* This driver supports custom bytesperline values */
402
403         mp->num_planes = fmt->buffers;
404         for (p = 0; p < fmt->buffers; p++) {
405                 /* Calculate the minimum supported bytesperline value */
406                 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
407                 /* Calculate the maximum supported bytesperline value */
408                 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
409
410                 if (pfmt[p].bytesperline > max_bpl)
411                         pfmt[p].bytesperline = max_bpl;
412                 if (pfmt[p].bytesperline < bytesperline)
413                         pfmt[p].bytesperline = bytesperline;
414
415                 pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
416                                 fmt->vdownsampling[p] + fmt->data_offset[p];
417
418                 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
419         }
420         for (p = fmt->buffers; p < fmt->planes; p++)
421                 pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
422                         (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
423                         (fmt->bit_depth[0] / fmt->vdownsampling[0]);
424
425         mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
426         mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
427         mp->quantization = V4L2_QUANTIZATION_DEFAULT;
428         if (vivid_is_svid_out(dev)) {
429                 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
430         } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
431                 mp->colorspace = V4L2_COLORSPACE_SRGB;
432                 if (dev->dvi_d_out)
433                         mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
434         } else if (bt->width == 720 && bt->height <= 576) {
435                 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
436         } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
437                    mp->colorspace != V4L2_COLORSPACE_REC709 &&
438                    mp->colorspace != V4L2_COLORSPACE_OPRGB &&
439                    mp->colorspace != V4L2_COLORSPACE_BT2020 &&
440                    mp->colorspace != V4L2_COLORSPACE_SRGB) {
441                 mp->colorspace = V4L2_COLORSPACE_REC709;
442         }
443         memset(mp->reserved, 0, sizeof(mp->reserved));
444         return 0;
445 }
446
447 int vivid_s_fmt_vid_out(struct file *file, void *priv,
448                                         struct v4l2_format *f)
449 {
450         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
451         struct vivid_dev *dev = video_drvdata(file);
452         struct v4l2_rect *crop = &dev->crop_out;
453         struct v4l2_rect *compose = &dev->compose_out;
454         struct vb2_queue *q = &dev->vb_vid_out_q;
455         int ret = vivid_try_fmt_vid_out(file, priv, f);
456         unsigned factor = 1;
457         unsigned p;
458
459         if (ret < 0)
460                 return ret;
461
462         if (vb2_is_busy(q) &&
463             (vivid_is_svid_out(dev) ||
464              mp->width != dev->fmt_out_rect.width ||
465              mp->height != dev->fmt_out_rect.height ||
466              mp->pixelformat != dev->fmt_out->fourcc ||
467              mp->field != dev->field_out)) {
468                 dprintk(dev, 1, "%s device busy\n", __func__);
469                 return -EBUSY;
470         }
471
472         /*
473          * Allow for changing the colorspace on the fly. Useful for testing
474          * purposes, and it is something that HDMI transmitters are able
475          * to do.
476          */
477         if (vb2_is_busy(q))
478                 goto set_colorspace;
479
480         dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
481         if (V4L2_FIELD_HAS_T_OR_B(mp->field))
482                 factor = 2;
483
484         if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
485                 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
486
487                 if (dev->has_scaler_out) {
488                         if (dev->has_crop_out)
489                                 v4l2_rect_map_inside(crop, &r);
490                         else
491                                 *crop = r;
492                         if (dev->has_compose_out && !dev->has_crop_out) {
493                                 struct v4l2_rect min_r = {
494                                         0, 0,
495                                         r.width / MAX_ZOOM,
496                                         factor * r.height / MAX_ZOOM
497                                 };
498                                 struct v4l2_rect max_r = {
499                                         0, 0,
500                                         r.width * MAX_ZOOM,
501                                         factor * r.height * MAX_ZOOM
502                                 };
503
504                                 v4l2_rect_set_min_size(compose, &min_r);
505                                 v4l2_rect_set_max_size(compose, &max_r);
506                                 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
507                         } else if (dev->has_compose_out) {
508                                 struct v4l2_rect min_r = {
509                                         0, 0,
510                                         crop->width / MAX_ZOOM,
511                                         factor * crop->height / MAX_ZOOM
512                                 };
513                                 struct v4l2_rect max_r = {
514                                         0, 0,
515                                         crop->width * MAX_ZOOM,
516                                         factor * crop->height * MAX_ZOOM
517                                 };
518
519                                 v4l2_rect_set_min_size(compose, &min_r);
520                                 v4l2_rect_set_max_size(compose, &max_r);
521                                 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
522                         }
523                 } else if (dev->has_compose_out && !dev->has_crop_out) {
524                         v4l2_rect_set_size_to(crop, &r);
525                         r.height *= factor;
526                         v4l2_rect_set_size_to(compose, &r);
527                         v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
528                 } else if (!dev->has_compose_out) {
529                         v4l2_rect_map_inside(crop, &r);
530                         r.height /= factor;
531                         v4l2_rect_set_size_to(compose, &r);
532                 } else {
533                         r.height *= factor;
534                         v4l2_rect_set_max_size(compose, &r);
535                         v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
536                         crop->top *= factor;
537                         crop->height *= factor;
538                         v4l2_rect_set_size_to(crop, compose);
539                         v4l2_rect_map_inside(crop, &r);
540                         crop->top /= factor;
541                         crop->height /= factor;
542                 }
543         } else {
544                 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
545
546                 v4l2_rect_set_size_to(crop, &r);
547                 r.height /= factor;
548                 v4l2_rect_set_size_to(compose, &r);
549         }
550
551         dev->fmt_out_rect.width = mp->width;
552         dev->fmt_out_rect.height = mp->height;
553         for (p = 0; p < mp->num_planes; p++)
554                 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
555         for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
556                 dev->bytesperline_out[p] =
557                         (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
558                         dev->fmt_out->bit_depth[0];
559         dev->field_out = mp->field;
560         if (vivid_is_svid_out(dev))
561                 dev->tv_field_out = mp->field;
562
563 set_colorspace:
564         dev->colorspace_out = mp->colorspace;
565         dev->xfer_func_out = mp->xfer_func;
566         dev->ycbcr_enc_out = mp->ycbcr_enc;
567         dev->quantization_out = mp->quantization;
568         if (dev->loop_video) {
569                 vivid_send_source_change(dev, SVID);
570                 vivid_send_source_change(dev, HDMI);
571         }
572         return 0;
573 }
574
575 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
576                                         struct v4l2_format *f)
577 {
578         struct vivid_dev *dev = video_drvdata(file);
579
580         if (!dev->multiplanar)
581                 return -ENOTTY;
582         return vivid_g_fmt_vid_out(file, priv, f);
583 }
584
585 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
586                         struct v4l2_format *f)
587 {
588         struct vivid_dev *dev = video_drvdata(file);
589
590         if (!dev->multiplanar)
591                 return -ENOTTY;
592         return vivid_try_fmt_vid_out(file, priv, f);
593 }
594
595 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
596                         struct v4l2_format *f)
597 {
598         struct vivid_dev *dev = video_drvdata(file);
599
600         if (!dev->multiplanar)
601                 return -ENOTTY;
602         return vivid_s_fmt_vid_out(file, priv, f);
603 }
604
605 int vidioc_g_fmt_vid_out(struct file *file, void *priv,
606                                         struct v4l2_format *f)
607 {
608         struct vivid_dev *dev = video_drvdata(file);
609
610         if (dev->multiplanar)
611                 return -ENOTTY;
612         return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
613 }
614
615 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
616                         struct v4l2_format *f)
617 {
618         struct vivid_dev *dev = video_drvdata(file);
619
620         if (dev->multiplanar)
621                 return -ENOTTY;
622         return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
623 }
624
625 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
626                         struct v4l2_format *f)
627 {
628         struct vivid_dev *dev = video_drvdata(file);
629
630         if (dev->multiplanar)
631                 return -ENOTTY;
632         return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
633 }
634
635 int vivid_vid_out_g_selection(struct file *file, void *priv,
636                               struct v4l2_selection *sel)
637 {
638         struct vivid_dev *dev = video_drvdata(file);
639
640         if (!dev->has_crop_out && !dev->has_compose_out)
641                 return -ENOTTY;
642         if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
643                 return -EINVAL;
644
645         sel->r.left = sel->r.top = 0;
646         switch (sel->target) {
647         case V4L2_SEL_TGT_CROP:
648                 if (!dev->has_crop_out)
649                         return -EINVAL;
650                 sel->r = dev->crop_out;
651                 break;
652         case V4L2_SEL_TGT_CROP_DEFAULT:
653                 if (!dev->has_crop_out)
654                         return -EINVAL;
655                 sel->r = dev->fmt_out_rect;
656                 break;
657         case V4L2_SEL_TGT_CROP_BOUNDS:
658                 if (!dev->has_crop_out)
659                         return -EINVAL;
660                 sel->r = vivid_max_rect;
661                 break;
662         case V4L2_SEL_TGT_COMPOSE:
663                 if (!dev->has_compose_out)
664                         return -EINVAL;
665                 sel->r = dev->compose_out;
666                 break;
667         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
668         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
669                 if (!dev->has_compose_out)
670                         return -EINVAL;
671                 sel->r = dev->sink_rect;
672                 break;
673         default:
674                 return -EINVAL;
675         }
676         return 0;
677 }
678
679 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
680 {
681         struct vivid_dev *dev = video_drvdata(file);
682         struct v4l2_rect *crop = &dev->crop_out;
683         struct v4l2_rect *compose = &dev->compose_out;
684         unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
685         int ret;
686
687         if (!dev->has_crop_out && !dev->has_compose_out)
688                 return -ENOTTY;
689         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
690                 return -EINVAL;
691
692         switch (s->target) {
693         case V4L2_SEL_TGT_CROP:
694                 if (!dev->has_crop_out)
695                         return -EINVAL;
696                 ret = vivid_vid_adjust_sel(s->flags, &s->r);
697                 if (ret)
698                         return ret;
699                 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
700                 v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
701                 if (dev->has_scaler_out) {
702                         struct v4l2_rect max_rect = {
703                                 0, 0,
704                                 dev->sink_rect.width * MAX_ZOOM,
705                                 (dev->sink_rect.height / factor) * MAX_ZOOM
706                         };
707
708                         v4l2_rect_set_max_size(&s->r, &max_rect);
709                         if (dev->has_compose_out) {
710                                 struct v4l2_rect min_rect = {
711                                         0, 0,
712                                         s->r.width / MAX_ZOOM,
713                                         (s->r.height * factor) / MAX_ZOOM
714                                 };
715                                 struct v4l2_rect max_rect = {
716                                         0, 0,
717                                         s->r.width * MAX_ZOOM,
718                                         (s->r.height * factor) * MAX_ZOOM
719                                 };
720
721                                 v4l2_rect_set_min_size(compose, &min_rect);
722                                 v4l2_rect_set_max_size(compose, &max_rect);
723                                 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
724                         }
725                 } else if (dev->has_compose_out) {
726                         s->r.top *= factor;
727                         s->r.height *= factor;
728                         v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
729                         v4l2_rect_set_size_to(compose, &s->r);
730                         v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
731                         s->r.top /= factor;
732                         s->r.height /= factor;
733                 } else {
734                         v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
735                         s->r.height /= factor;
736                 }
737                 v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
738                 *crop = s->r;
739                 break;
740         case V4L2_SEL_TGT_COMPOSE:
741                 if (!dev->has_compose_out)
742                         return -EINVAL;
743                 ret = vivid_vid_adjust_sel(s->flags, &s->r);
744                 if (ret)
745                         return ret;
746                 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
747                 v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
748                 v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
749                 s->r.top /= factor;
750                 s->r.height /= factor;
751                 if (dev->has_scaler_out) {
752                         struct v4l2_rect fmt = dev->fmt_out_rect;
753                         struct v4l2_rect max_rect = {
754                                 0, 0,
755                                 s->r.width * MAX_ZOOM,
756                                 s->r.height * MAX_ZOOM
757                         };
758                         struct v4l2_rect min_rect = {
759                                 0, 0,
760                                 s->r.width / MAX_ZOOM,
761                                 s->r.height / MAX_ZOOM
762                         };
763
764                         v4l2_rect_set_min_size(&fmt, &min_rect);
765                         if (!dev->has_crop_out)
766                                 v4l2_rect_set_max_size(&fmt, &max_rect);
767                         if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
768                             vb2_is_busy(&dev->vb_vid_out_q))
769                                 return -EBUSY;
770                         if (dev->has_crop_out) {
771                                 v4l2_rect_set_min_size(crop, &min_rect);
772                                 v4l2_rect_set_max_size(crop, &max_rect);
773                         }
774                         dev->fmt_out_rect = fmt;
775                 } else if (dev->has_crop_out) {
776                         struct v4l2_rect fmt = dev->fmt_out_rect;
777
778                         v4l2_rect_set_min_size(&fmt, &s->r);
779                         if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
780                             vb2_is_busy(&dev->vb_vid_out_q))
781                                 return -EBUSY;
782                         dev->fmt_out_rect = fmt;
783                         v4l2_rect_set_size_to(crop, &s->r);
784                         v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
785                 } else {
786                         if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
787                             vb2_is_busy(&dev->vb_vid_out_q))
788                                 return -EBUSY;
789                         v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
790                         v4l2_rect_set_size_to(crop, &s->r);
791                         crop->height /= factor;
792                         v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
793                 }
794                 s->r.top *= factor;
795                 s->r.height *= factor;
796                 if (dev->bitmap_out && (compose->width != s->r.width ||
797                                         compose->height != s->r.height)) {
798                         vfree(dev->bitmap_out);
799                         dev->bitmap_out = NULL;
800                 }
801                 *compose = s->r;
802                 break;
803         default:
804                 return -EINVAL;
805         }
806
807         return 0;
808 }
809
810 int vivid_vid_out_g_pixelaspect(struct file *file, void *priv,
811                                 int type, struct v4l2_fract *f)
812 {
813         struct vivid_dev *dev = video_drvdata(file);
814
815         if (type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
816                 return -EINVAL;
817
818         switch (vivid_get_pixel_aspect(dev)) {
819         case TPG_PIXEL_ASPECT_NTSC:
820                 f->numerator = 11;
821                 f->denominator = 10;
822                 break;
823         case TPG_PIXEL_ASPECT_PAL:
824                 f->numerator = 54;
825                 f->denominator = 59;
826                 break;
827         default:
828                 break;
829         }
830         return 0;
831 }
832
833 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
834                                         struct v4l2_format *f)
835 {
836         struct vivid_dev *dev = video_drvdata(file);
837         const struct v4l2_rect *compose = &dev->compose_out;
838         struct v4l2_window *win = &f->fmt.win;
839         unsigned clipcount = win->clipcount;
840
841         if (!dev->has_fb)
842                 return -EINVAL;
843         win->w.top = dev->overlay_out_top;
844         win->w.left = dev->overlay_out_left;
845         win->w.width = compose->width;
846         win->w.height = compose->height;
847         win->clipcount = dev->clipcount_out;
848         win->field = V4L2_FIELD_ANY;
849         win->chromakey = dev->chromakey_out;
850         win->global_alpha = dev->global_alpha_out;
851         if (clipcount > dev->clipcount_out)
852                 clipcount = dev->clipcount_out;
853         if (dev->bitmap_out == NULL)
854                 win->bitmap = NULL;
855         else if (win->bitmap) {
856                 if (copy_to_user(win->bitmap, dev->bitmap_out,
857                     ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
858                         return -EFAULT;
859         }
860         if (clipcount && win->clips) {
861                 if (copy_to_user(win->clips, dev->clips_out,
862                                  clipcount * sizeof(dev->clips_out[0])))
863                         return -EFAULT;
864         }
865         return 0;
866 }
867
868 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
869                                         struct v4l2_format *f)
870 {
871         struct vivid_dev *dev = video_drvdata(file);
872         const struct v4l2_rect *compose = &dev->compose_out;
873         struct v4l2_window *win = &f->fmt.win;
874         int i, j;
875
876         if (!dev->has_fb)
877                 return -EINVAL;
878         win->w.left = clamp_t(int, win->w.left,
879                               -dev->display_width, dev->display_width);
880         win->w.top = clamp_t(int, win->w.top,
881                              -dev->display_height, dev->display_height);
882         win->w.width = compose->width;
883         win->w.height = compose->height;
884         /*
885          * It makes no sense for an OSD to overlay only top or bottom fields,
886          * so always set this to ANY.
887          */
888         win->field = V4L2_FIELD_ANY;
889         if (win->clipcount && !win->clips)
890                 win->clipcount = 0;
891         if (win->clipcount > MAX_CLIPS)
892                 win->clipcount = MAX_CLIPS;
893         if (win->clipcount) {
894                 if (copy_from_user(dev->try_clips_out, win->clips,
895                                    win->clipcount * sizeof(dev->clips_out[0])))
896                         return -EFAULT;
897                 for (i = 0; i < win->clipcount; i++) {
898                         struct v4l2_rect *r = &dev->try_clips_out[i].c;
899
900                         r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
901                         r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
902                         r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
903                         r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
904                 }
905                 /*
906                  * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
907                  * number and it's typically a one-time deal.
908                  */
909                 for (i = 0; i < win->clipcount - 1; i++) {
910                         struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
911
912                         for (j = i + 1; j < win->clipcount; j++) {
913                                 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
914
915                                 if (v4l2_rect_overlap(r1, r2))
916                                         return -EINVAL;
917                         }
918                 }
919                 if (copy_to_user(win->clips, dev->try_clips_out,
920                                  win->clipcount * sizeof(dev->clips_out[0])))
921                         return -EFAULT;
922         }
923         return 0;
924 }
925
926 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
927                                         struct v4l2_format *f)
928 {
929         struct vivid_dev *dev = video_drvdata(file);
930         const struct v4l2_rect *compose = &dev->compose_out;
931         struct v4l2_window *win = &f->fmt.win;
932         int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
933         unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
934         unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
935         void *new_bitmap = NULL;
936
937         if (ret)
938                 return ret;
939
940         if (win->bitmap) {
941                 new_bitmap = vzalloc(bitmap_size);
942
943                 if (!new_bitmap)
944                         return -ENOMEM;
945                 if (copy_from_user(new_bitmap, win->bitmap, bitmap_size)) {
946                         vfree(new_bitmap);
947                         return -EFAULT;
948                 }
949         }
950
951         dev->overlay_out_top = win->w.top;
952         dev->overlay_out_left = win->w.left;
953         vfree(dev->bitmap_out);
954         dev->bitmap_out = new_bitmap;
955         dev->clipcount_out = win->clipcount;
956         if (dev->clipcount_out)
957                 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
958         dev->chromakey_out = win->chromakey;
959         dev->global_alpha_out = win->global_alpha;
960         return ret;
961 }
962
963 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
964 {
965         struct vivid_dev *dev = video_drvdata(file);
966
967         if (i && !dev->fmt_out->can_do_overlay) {
968                 dprintk(dev, 1, "unsupported output format for output overlay\n");
969                 return -EINVAL;
970         }
971
972         dev->overlay_out_enabled = i;
973         return 0;
974 }
975
976 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
977                                 struct v4l2_framebuffer *a)
978 {
979         struct vivid_dev *dev = video_drvdata(file);
980
981         a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
982                         V4L2_FBUF_CAP_BITMAP_CLIPPING |
983                         V4L2_FBUF_CAP_LIST_CLIPPING |
984                         V4L2_FBUF_CAP_CHROMAKEY |
985                         V4L2_FBUF_CAP_SRC_CHROMAKEY |
986                         V4L2_FBUF_CAP_GLOBAL_ALPHA |
987                         V4L2_FBUF_CAP_LOCAL_ALPHA |
988                         V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
989         a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
990         a->base = (void *)dev->video_pbase;
991         a->fmt.width = dev->display_width;
992         a->fmt.height = dev->display_height;
993         if (dev->fb_defined.green.length == 5)
994                 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
995         else
996                 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
997         a->fmt.bytesperline = dev->display_byte_stride;
998         a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
999         a->fmt.field = V4L2_FIELD_NONE;
1000         a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
1001         a->fmt.priv = 0;
1002         return 0;
1003 }
1004
1005 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
1006                                 const struct v4l2_framebuffer *a)
1007 {
1008         struct vivid_dev *dev = video_drvdata(file);
1009         const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
1010                                       V4L2_FBUF_FLAG_SRC_CHROMAKEY;
1011         const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
1012                                      V4L2_FBUF_FLAG_LOCAL_ALPHA |
1013                                      V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1014
1015
1016         if ((a->flags & chroma_flags) == chroma_flags)
1017                 return -EINVAL;
1018         switch (a->flags & alpha_flags) {
1019         case 0:
1020         case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1021         case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1022         case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1023                 break;
1024         default:
1025                 return -EINVAL;
1026         }
1027         dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1028         dev->fbuf_out_flags |= a->flags & (chroma_flags | alpha_flags);
1029         return 0;
1030 }
1031
1032 static const struct v4l2_audioout vivid_audio_outputs[] = {
1033         { 0, "Line-Out 1" },
1034         { 1, "Line-Out 2" },
1035 };
1036
1037 int vidioc_enum_output(struct file *file, void *priv,
1038                                 struct v4l2_output *out)
1039 {
1040         struct vivid_dev *dev = video_drvdata(file);
1041
1042         if (out->index >= dev->num_outputs)
1043                 return -EINVAL;
1044
1045         out->type = V4L2_OUTPUT_TYPE_ANALOG;
1046         switch (dev->output_type[out->index]) {
1047         case SVID:
1048                 snprintf(out->name, sizeof(out->name), "S-Video %u",
1049                                 dev->output_name_counter[out->index]);
1050                 out->std = V4L2_STD_ALL;
1051                 if (dev->has_audio_outputs)
1052                         out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1053                 out->capabilities = V4L2_OUT_CAP_STD;
1054                 break;
1055         case HDMI:
1056                 snprintf(out->name, sizeof(out->name), "HDMI %u",
1057                                 dev->output_name_counter[out->index]);
1058                 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1059                 break;
1060         }
1061         return 0;
1062 }
1063
1064 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1065 {
1066         struct vivid_dev *dev = video_drvdata(file);
1067
1068         *o = dev->output;
1069         return 0;
1070 }
1071
1072 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1073 {
1074         struct vivid_dev *dev = video_drvdata(file);
1075
1076         if (o >= dev->num_outputs)
1077                 return -EINVAL;
1078
1079         if (o == dev->output)
1080                 return 0;
1081
1082         if (vb2_is_busy(&dev->vb_vid_out_q) ||
1083             vb2_is_busy(&dev->vb_vbi_out_q) ||
1084             vb2_is_busy(&dev->vb_meta_out_q))
1085                 return -EBUSY;
1086
1087         dev->output = o;
1088         dev->tv_audio_output = 0;
1089         if (dev->output_type[o] == SVID)
1090                 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1091         else
1092                 dev->vid_out_dev.tvnorms = 0;
1093
1094         dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1095         dev->meta_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1096         vivid_update_format_out(dev);
1097
1098         v4l2_ctrl_activate(dev->ctrl_display_present, vivid_is_hdmi_out(dev));
1099         if (vivid_is_hdmi_out(dev))
1100                 v4l2_ctrl_s_ctrl(dev->ctrl_display_present,
1101                                  dev->display_present[dev->output]);
1102
1103         return 0;
1104 }
1105
1106 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1107 {
1108         if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1109                 return -EINVAL;
1110         *vout = vivid_audio_outputs[vout->index];
1111         return 0;
1112 }
1113
1114 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1115 {
1116         struct vivid_dev *dev = video_drvdata(file);
1117
1118         if (!vivid_is_svid_out(dev))
1119                 return -EINVAL;
1120         *vout = vivid_audio_outputs[dev->tv_audio_output];
1121         return 0;
1122 }
1123
1124 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1125 {
1126         struct vivid_dev *dev = video_drvdata(file);
1127
1128         if (!vivid_is_svid_out(dev))
1129                 return -EINVAL;
1130         if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1131                 return -EINVAL;
1132         dev->tv_audio_output = vout->index;
1133         return 0;
1134 }
1135
1136 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1137 {
1138         struct vivid_dev *dev = video_drvdata(file);
1139
1140         if (!vivid_is_svid_out(dev))
1141                 return -ENODATA;
1142         if (dev->std_out == id)
1143                 return 0;
1144         if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1145                 return -EBUSY;
1146         dev->std_out = id;
1147         vivid_update_format_out(dev);
1148         return 0;
1149 }
1150
1151 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1152 {
1153         struct v4l2_bt_timings *bt = &timings->bt;
1154
1155         if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1156             v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1157                 return true;
1158
1159         return false;
1160 }
1161
1162 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1163                                     struct v4l2_dv_timings *timings)
1164 {
1165         struct vivid_dev *dev = video_drvdata(file);
1166         if (!vivid_is_hdmi_out(dev))
1167                 return -ENODATA;
1168         if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1169                                 0, NULL, NULL) &&
1170             !valid_cvt_gtf_timings(timings))
1171                 return -EINVAL;
1172         if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
1173                 return 0;
1174         if (vb2_is_busy(&dev->vb_vid_out_q))
1175                 return -EBUSY;
1176         dev->dv_timings_out = *timings;
1177         vivid_update_format_out(dev);
1178         return 0;
1179 }
1180
1181 int vivid_vid_out_g_parm(struct file *file, void *priv,
1182                           struct v4l2_streamparm *parm)
1183 {
1184         struct vivid_dev *dev = video_drvdata(file);
1185
1186         if (parm->type != (dev->multiplanar ?
1187                            V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1188                            V4L2_BUF_TYPE_VIDEO_OUTPUT))
1189                 return -EINVAL;
1190
1191         parm->parm.output.capability   = V4L2_CAP_TIMEPERFRAME;
1192         parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1193         parm->parm.output.writebuffers  = 1;
1194
1195         return 0;
1196 }
1197
1198 int vidioc_subscribe_event(struct v4l2_fh *fh,
1199                         const struct v4l2_event_subscription *sub)
1200 {
1201         switch (sub->type) {
1202         case V4L2_EVENT_SOURCE_CHANGE:
1203                 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1204                         return v4l2_src_change_event_subscribe(fh, sub);
1205                 break;
1206         default:
1207                 return v4l2_ctrl_subscribe_event(fh, sub);
1208         }
1209         return -EINVAL;
1210 }