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