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