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