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