GNU Linux-libre 4.9.330-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->quantization_out = V4L2_QUANTIZATION_DEFAULT;
257         dev->compose_out = dev->sink_rect;
258         dev->compose_bounds_out = dev->sink_rect;
259         dev->crop_out = dev->compose_out;
260         if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
261                 dev->crop_out.height /= 2;
262         dev->fmt_out_rect = dev->crop_out;
263         for (p = 0; p < dev->fmt_out->planes; p++)
264                 dev->bytesperline_out[p] =
265                         (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
266 }
267
268 /* Map the field to something that is valid for the current output */
269 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
270 {
271         if (vivid_is_svid_out(dev)) {
272                 switch (field) {
273                 case V4L2_FIELD_INTERLACED_TB:
274                 case V4L2_FIELD_INTERLACED_BT:
275                 case V4L2_FIELD_SEQ_TB:
276                 case V4L2_FIELD_SEQ_BT:
277                 case V4L2_FIELD_ALTERNATE:
278                         return field;
279                 case V4L2_FIELD_INTERLACED:
280                 default:
281                         return V4L2_FIELD_INTERLACED;
282                 }
283         }
284         if (vivid_is_hdmi_out(dev))
285                 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
286                                                        V4L2_FIELD_NONE;
287         return V4L2_FIELD_NONE;
288 }
289
290 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
291 {
292         if (vivid_is_svid_out(dev))
293                 return (dev->std_out & V4L2_STD_525_60) ?
294                         TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
295
296         if (vivid_is_hdmi_out(dev) &&
297             dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
298                 return dev->sink_rect.height == 480 ?
299                         TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
300
301         return TPG_PIXEL_ASPECT_SQUARE;
302 }
303
304 int vivid_g_fmt_vid_out(struct file *file, void *priv,
305                                         struct v4l2_format *f)
306 {
307         struct vivid_dev *dev = video_drvdata(file);
308         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
309         const struct vivid_fmt *fmt = dev->fmt_out;
310         unsigned p;
311
312         mp->width        = dev->fmt_out_rect.width;
313         mp->height       = dev->fmt_out_rect.height;
314         mp->field        = dev->field_out;
315         mp->pixelformat  = fmt->fourcc;
316         mp->colorspace   = dev->colorspace_out;
317         mp->xfer_func    = dev->xfer_func_out;
318         mp->ycbcr_enc    = dev->ycbcr_enc_out;
319         mp->quantization = dev->quantization_out;
320         mp->num_planes = fmt->buffers;
321         for (p = 0; p < mp->num_planes; p++) {
322                 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
323                 mp->plane_fmt[p].sizeimage =
324                         mp->plane_fmt[p].bytesperline * mp->height;
325         }
326         for (p = fmt->buffers; p < fmt->planes; p++) {
327                 unsigned stride = dev->bytesperline_out[p];
328
329                 mp->plane_fmt[0].sizeimage +=
330                         (stride * mp->height) / fmt->vdownsampling[p];
331         }
332         return 0;
333 }
334
335 int vivid_try_fmt_vid_out(struct file *file, void *priv,
336                         struct v4l2_format *f)
337 {
338         struct vivid_dev *dev = video_drvdata(file);
339         struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
340         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
341         struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
342         const struct vivid_fmt *fmt;
343         unsigned bytesperline, max_bpl;
344         unsigned factor = 1;
345         unsigned w, h;
346         unsigned p;
347
348         fmt = vivid_get_format(dev, mp->pixelformat);
349         if (!fmt) {
350                 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
351                         mp->pixelformat);
352                 mp->pixelformat = V4L2_PIX_FMT_YUYV;
353                 fmt = vivid_get_format(dev, mp->pixelformat);
354         }
355
356         mp->field = vivid_field_out(dev, mp->field);
357         if (vivid_is_svid_out(dev)) {
358                 w = 720;
359                 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
360         } else {
361                 w = dev->sink_rect.width;
362                 h = dev->sink_rect.height;
363         }
364         if (V4L2_FIELD_HAS_T_OR_B(mp->field))
365                 factor = 2;
366         if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
367                 mp->width = w;
368                 mp->height = h / factor;
369         } else {
370                 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
371
372                 v4l2_rect_set_min_size(&r, &vivid_min_rect);
373                 v4l2_rect_set_max_size(&r, &vivid_max_rect);
374                 if (dev->has_scaler_out && !dev->has_crop_out) {
375                         struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
376
377                         v4l2_rect_set_max_size(&r, &max_r);
378                 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
379                         v4l2_rect_set_max_size(&r, &dev->sink_rect);
380                 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
381                         v4l2_rect_set_min_size(&r, &dev->sink_rect);
382                 }
383                 mp->width = r.width;
384                 mp->height = r.height / factor;
385         }
386
387         /* This driver supports custom bytesperline values */
388
389         /* Calculate the minimum supported bytesperline value */
390         bytesperline = (mp->width * fmt->bit_depth[0]) >> 3;
391         /* Calculate the maximum supported bytesperline value */
392         max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
393         mp->num_planes = fmt->buffers;
394         for (p = 0; p < mp->num_planes; p++) {
395                 if (pfmt[p].bytesperline > max_bpl)
396                         pfmt[p].bytesperline = max_bpl;
397                 if (pfmt[p].bytesperline < bytesperline)
398                         pfmt[p].bytesperline = bytesperline;
399                 pfmt[p].sizeimage = pfmt[p].bytesperline * mp->height;
400                 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
401         }
402         for (p = fmt->buffers; p < fmt->planes; p++)
403                 pfmt[0].sizeimage += (pfmt[0].bytesperline * fmt->bit_depth[p]) /
404                                      (fmt->bit_depth[0] * fmt->vdownsampling[p]);
405         mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
406         mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
407         mp->quantization = V4L2_QUANTIZATION_DEFAULT;
408         if (vivid_is_svid_out(dev)) {
409                 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
410         } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
411                 mp->colorspace = V4L2_COLORSPACE_SRGB;
412                 if (dev->dvi_d_out)
413                         mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
414         } else if (bt->width == 720 && bt->height <= 576) {
415                 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
416         } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
417                    mp->colorspace != V4L2_COLORSPACE_REC709 &&
418                    mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
419                    mp->colorspace != V4L2_COLORSPACE_BT2020 &&
420                    mp->colorspace != V4L2_COLORSPACE_SRGB) {
421                 mp->colorspace = V4L2_COLORSPACE_REC709;
422         }
423         memset(mp->reserved, 0, sizeof(mp->reserved));
424         return 0;
425 }
426
427 int vivid_s_fmt_vid_out(struct file *file, void *priv,
428                                         struct v4l2_format *f)
429 {
430         struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
431         struct vivid_dev *dev = video_drvdata(file);
432         struct v4l2_rect *crop = &dev->crop_out;
433         struct v4l2_rect *compose = &dev->compose_out;
434         struct vb2_queue *q = &dev->vb_vid_out_q;
435         int ret = vivid_try_fmt_vid_out(file, priv, f);
436         unsigned factor = 1;
437         unsigned p;
438
439         if (ret < 0)
440                 return ret;
441
442         if (vb2_is_busy(q) &&
443             (vivid_is_svid_out(dev) ||
444              mp->width != dev->fmt_out_rect.width ||
445              mp->height != dev->fmt_out_rect.height ||
446              mp->pixelformat != dev->fmt_out->fourcc ||
447              mp->field != dev->field_out)) {
448                 dprintk(dev, 1, "%s device busy\n", __func__);
449                 return -EBUSY;
450         }
451
452         /*
453          * Allow for changing the colorspace on the fly. Useful for testing
454          * purposes, and it is something that HDMI transmitters are able
455          * to do.
456          */
457         if (vb2_is_busy(q))
458                 goto set_colorspace;
459
460         dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
461         if (V4L2_FIELD_HAS_T_OR_B(mp->field))
462                 factor = 2;
463
464         if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
465                 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
466
467                 if (dev->has_scaler_out) {
468                         if (dev->has_crop_out)
469                                 v4l2_rect_map_inside(crop, &r);
470                         else
471                                 *crop = r;
472                         if (dev->has_compose_out && !dev->has_crop_out) {
473                                 struct v4l2_rect min_r = {
474                                         0, 0,
475                                         r.width / MAX_ZOOM,
476                                         factor * r.height / MAX_ZOOM
477                                 };
478                                 struct v4l2_rect max_r = {
479                                         0, 0,
480                                         r.width * MAX_ZOOM,
481                                         factor * r.height * MAX_ZOOM
482                                 };
483
484                                 v4l2_rect_set_min_size(compose, &min_r);
485                                 v4l2_rect_set_max_size(compose, &max_r);
486                                 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
487                         } else if (dev->has_compose_out) {
488                                 struct v4l2_rect min_r = {
489                                         0, 0,
490                                         crop->width / MAX_ZOOM,
491                                         factor * crop->height / MAX_ZOOM
492                                 };
493                                 struct v4l2_rect max_r = {
494                                         0, 0,
495                                         crop->width * MAX_ZOOM,
496                                         factor * crop->height * MAX_ZOOM
497                                 };
498
499                                 v4l2_rect_set_min_size(compose, &min_r);
500                                 v4l2_rect_set_max_size(compose, &max_r);
501                                 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
502                         }
503                 } else if (dev->has_compose_out && !dev->has_crop_out) {
504                         v4l2_rect_set_size_to(crop, &r);
505                         r.height *= factor;
506                         v4l2_rect_set_size_to(compose, &r);
507                         v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
508                 } else if (!dev->has_compose_out) {
509                         v4l2_rect_map_inside(crop, &r);
510                         r.height /= factor;
511                         v4l2_rect_set_size_to(compose, &r);
512                 } else {
513                         r.height *= factor;
514                         v4l2_rect_set_max_size(compose, &r);
515                         v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
516                         crop->top *= factor;
517                         crop->height *= factor;
518                         v4l2_rect_set_size_to(crop, compose);
519                         v4l2_rect_map_inside(crop, &r);
520                         crop->top /= factor;
521                         crop->height /= factor;
522                 }
523         } else {
524                 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
525
526                 v4l2_rect_set_size_to(crop, &r);
527                 r.height /= factor;
528                 v4l2_rect_set_size_to(compose, &r);
529         }
530
531         dev->fmt_out_rect.width = mp->width;
532         dev->fmt_out_rect.height = mp->height;
533         for (p = 0; p < mp->num_planes; p++)
534                 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
535         for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
536                 dev->bytesperline_out[p] =
537                         (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
538                         dev->fmt_out->bit_depth[0];
539         dev->field_out = mp->field;
540         if (vivid_is_svid_out(dev))
541                 dev->tv_field_out = mp->field;
542
543 set_colorspace:
544         dev->colorspace_out = mp->colorspace;
545         dev->xfer_func_out = mp->xfer_func;
546         dev->ycbcr_enc_out = mp->ycbcr_enc;
547         dev->quantization_out = mp->quantization;
548         if (dev->loop_video) {
549                 vivid_send_source_change(dev, SVID);
550                 vivid_send_source_change(dev, HDMI);
551         }
552         return 0;
553 }
554
555 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
556                                         struct v4l2_format *f)
557 {
558         struct vivid_dev *dev = video_drvdata(file);
559
560         if (!dev->multiplanar)
561                 return -ENOTTY;
562         return vivid_g_fmt_vid_out(file, priv, f);
563 }
564
565 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
566                         struct v4l2_format *f)
567 {
568         struct vivid_dev *dev = video_drvdata(file);
569
570         if (!dev->multiplanar)
571                 return -ENOTTY;
572         return vivid_try_fmt_vid_out(file, priv, f);
573 }
574
575 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
576                         struct v4l2_format *f)
577 {
578         struct vivid_dev *dev = video_drvdata(file);
579
580         if (!dev->multiplanar)
581                 return -ENOTTY;
582         return vivid_s_fmt_vid_out(file, priv, f);
583 }
584
585 int vidioc_g_fmt_vid_out(struct file *file, void *priv,
586                                         struct v4l2_format *f)
587 {
588         struct vivid_dev *dev = video_drvdata(file);
589
590         if (dev->multiplanar)
591                 return -ENOTTY;
592         return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
593 }
594
595 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
596                         struct v4l2_format *f)
597 {
598         struct vivid_dev *dev = video_drvdata(file);
599
600         if (dev->multiplanar)
601                 return -ENOTTY;
602         return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
603 }
604
605 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
606                         struct v4l2_format *f)
607 {
608         struct vivid_dev *dev = video_drvdata(file);
609
610         if (dev->multiplanar)
611                 return -ENOTTY;
612         return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
613 }
614
615 int vivid_vid_out_g_selection(struct file *file, void *priv,
616                               struct v4l2_selection *sel)
617 {
618         struct vivid_dev *dev = video_drvdata(file);
619
620         if (!dev->has_crop_out && !dev->has_compose_out)
621                 return -ENOTTY;
622         if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
623                 return -EINVAL;
624
625         sel->r.left = sel->r.top = 0;
626         switch (sel->target) {
627         case V4L2_SEL_TGT_CROP:
628                 if (!dev->has_crop_out)
629                         return -EINVAL;
630                 sel->r = dev->crop_out;
631                 break;
632         case V4L2_SEL_TGT_CROP_DEFAULT:
633                 if (!dev->has_crop_out)
634                         return -EINVAL;
635                 sel->r = dev->fmt_out_rect;
636                 break;
637         case V4L2_SEL_TGT_CROP_BOUNDS:
638                 if (!dev->has_crop_out)
639                         return -EINVAL;
640                 sel->r = vivid_max_rect;
641                 break;
642         case V4L2_SEL_TGT_COMPOSE:
643                 if (!dev->has_compose_out)
644                         return -EINVAL;
645                 sel->r = dev->compose_out;
646                 break;
647         case V4L2_SEL_TGT_COMPOSE_DEFAULT:
648         case V4L2_SEL_TGT_COMPOSE_BOUNDS:
649                 if (!dev->has_compose_out)
650                         return -EINVAL;
651                 sel->r = dev->sink_rect;
652                 break;
653         default:
654                 return -EINVAL;
655         }
656         return 0;
657 }
658
659 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
660 {
661         struct vivid_dev *dev = video_drvdata(file);
662         struct v4l2_rect *crop = &dev->crop_out;
663         struct v4l2_rect *compose = &dev->compose_out;
664         unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
665         int ret;
666
667         if (!dev->has_crop_out && !dev->has_compose_out)
668                 return -ENOTTY;
669         if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
670                 return -EINVAL;
671
672         switch (s->target) {
673         case V4L2_SEL_TGT_CROP:
674                 if (!dev->has_crop_out)
675                         return -EINVAL;
676                 ret = vivid_vid_adjust_sel(s->flags, &s->r);
677                 if (ret)
678                         return ret;
679                 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
680                 v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
681                 if (dev->has_scaler_out) {
682                         struct v4l2_rect max_rect = {
683                                 0, 0,
684                                 dev->sink_rect.width * MAX_ZOOM,
685                                 (dev->sink_rect.height / factor) * MAX_ZOOM
686                         };
687
688                         v4l2_rect_set_max_size(&s->r, &max_rect);
689                         if (dev->has_compose_out) {
690                                 struct v4l2_rect min_rect = {
691                                         0, 0,
692                                         s->r.width / MAX_ZOOM,
693                                         (s->r.height * factor) / MAX_ZOOM
694                                 };
695                                 struct v4l2_rect max_rect = {
696                                         0, 0,
697                                         s->r.width * MAX_ZOOM,
698                                         (s->r.height * factor) * MAX_ZOOM
699                                 };
700
701                                 v4l2_rect_set_min_size(compose, &min_rect);
702                                 v4l2_rect_set_max_size(compose, &max_rect);
703                                 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
704                         }
705                 } else if (dev->has_compose_out) {
706                         s->r.top *= factor;
707                         s->r.height *= factor;
708                         v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
709                         v4l2_rect_set_size_to(compose, &s->r);
710                         v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
711                         s->r.top /= factor;
712                         s->r.height /= factor;
713                 } else {
714                         v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
715                         s->r.height /= factor;
716                 }
717                 v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
718                 *crop = s->r;
719                 break;
720         case V4L2_SEL_TGT_COMPOSE:
721                 if (!dev->has_compose_out)
722                         return -EINVAL;
723                 ret = vivid_vid_adjust_sel(s->flags, &s->r);
724                 if (ret)
725                         return ret;
726                 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
727                 v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
728                 v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
729                 s->r.top /= factor;
730                 s->r.height /= factor;
731                 if (dev->has_scaler_out) {
732                         struct v4l2_rect fmt = dev->fmt_out_rect;
733                         struct v4l2_rect max_rect = {
734                                 0, 0,
735                                 s->r.width * MAX_ZOOM,
736                                 s->r.height * MAX_ZOOM
737                         };
738                         struct v4l2_rect min_rect = {
739                                 0, 0,
740                                 s->r.width / MAX_ZOOM,
741                                 s->r.height / MAX_ZOOM
742                         };
743
744                         v4l2_rect_set_min_size(&fmt, &min_rect);
745                         if (!dev->has_crop_out)
746                                 v4l2_rect_set_max_size(&fmt, &max_rect);
747                         if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
748                             vb2_is_busy(&dev->vb_vid_out_q))
749                                 return -EBUSY;
750                         if (dev->has_crop_out) {
751                                 v4l2_rect_set_min_size(crop, &min_rect);
752                                 v4l2_rect_set_max_size(crop, &max_rect);
753                         }
754                         dev->fmt_out_rect = fmt;
755                 } else if (dev->has_crop_out) {
756                         struct v4l2_rect fmt = dev->fmt_out_rect;
757
758                         v4l2_rect_set_min_size(&fmt, &s->r);
759                         if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
760                             vb2_is_busy(&dev->vb_vid_out_q))
761                                 return -EBUSY;
762                         dev->fmt_out_rect = fmt;
763                         v4l2_rect_set_size_to(crop, &s->r);
764                         v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
765                 } else {
766                         if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
767                             vb2_is_busy(&dev->vb_vid_out_q))
768                                 return -EBUSY;
769                         v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
770                         v4l2_rect_set_size_to(crop, &s->r);
771                         crop->height /= factor;
772                         v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
773                 }
774                 s->r.top *= factor;
775                 s->r.height *= factor;
776                 if (dev->bitmap_out && (compose->width != s->r.width ||
777                                         compose->height != s->r.height)) {
778                         kfree(dev->bitmap_out);
779                         dev->bitmap_out = NULL;
780                 }
781                 *compose = s->r;
782                 break;
783         default:
784                 return -EINVAL;
785         }
786
787         return 0;
788 }
789
790 int vivid_vid_out_cropcap(struct file *file, void *priv,
791                               struct v4l2_cropcap *cap)
792 {
793         struct vivid_dev *dev = video_drvdata(file);
794
795         if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
796                 return -EINVAL;
797
798         switch (vivid_get_pixel_aspect(dev)) {
799         case TPG_PIXEL_ASPECT_NTSC:
800                 cap->pixelaspect.numerator = 11;
801                 cap->pixelaspect.denominator = 10;
802                 break;
803         case TPG_PIXEL_ASPECT_PAL:
804                 cap->pixelaspect.numerator = 54;
805                 cap->pixelaspect.denominator = 59;
806                 break;
807         case TPG_PIXEL_ASPECT_SQUARE:
808                 cap->pixelaspect.numerator = 1;
809                 cap->pixelaspect.denominator = 1;
810                 break;
811         }
812         return 0;
813 }
814
815 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
816                                         struct v4l2_format *f)
817 {
818         struct vivid_dev *dev = video_drvdata(file);
819         const struct v4l2_rect *compose = &dev->compose_out;
820         struct v4l2_window *win = &f->fmt.win;
821         unsigned clipcount = win->clipcount;
822
823         if (!dev->has_fb)
824                 return -EINVAL;
825         win->w.top = dev->overlay_out_top;
826         win->w.left = dev->overlay_out_left;
827         win->w.width = compose->width;
828         win->w.height = compose->height;
829         win->clipcount = dev->clipcount_out;
830         win->field = V4L2_FIELD_ANY;
831         win->chromakey = dev->chromakey_out;
832         win->global_alpha = dev->global_alpha_out;
833         if (clipcount > dev->clipcount_out)
834                 clipcount = dev->clipcount_out;
835         if (dev->bitmap_out == NULL)
836                 win->bitmap = NULL;
837         else if (win->bitmap) {
838                 if (copy_to_user(win->bitmap, dev->bitmap_out,
839                     ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
840                         return -EFAULT;
841         }
842         if (clipcount && win->clips) {
843                 if (copy_to_user(win->clips, dev->clips_out,
844                                  clipcount * sizeof(dev->clips_out[0])))
845                         return -EFAULT;
846         }
847         return 0;
848 }
849
850 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
851                                         struct v4l2_format *f)
852 {
853         struct vivid_dev *dev = video_drvdata(file);
854         const struct v4l2_rect *compose = &dev->compose_out;
855         struct v4l2_window *win = &f->fmt.win;
856         int i, j;
857
858         if (!dev->has_fb)
859                 return -EINVAL;
860         win->w.left = clamp_t(int, win->w.left,
861                               -dev->display_width, dev->display_width);
862         win->w.top = clamp_t(int, win->w.top,
863                              -dev->display_height, dev->display_height);
864         win->w.width = compose->width;
865         win->w.height = compose->height;
866         /*
867          * It makes no sense for an OSD to overlay only top or bottom fields,
868          * so always set this to ANY.
869          */
870         win->field = V4L2_FIELD_ANY;
871         if (win->clipcount && !win->clips)
872                 win->clipcount = 0;
873         if (win->clipcount > MAX_CLIPS)
874                 win->clipcount = MAX_CLIPS;
875         if (win->clipcount) {
876                 if (copy_from_user(dev->try_clips_out, win->clips,
877                                    win->clipcount * sizeof(dev->clips_out[0])))
878                         return -EFAULT;
879                 for (i = 0; i < win->clipcount; i++) {
880                         struct v4l2_rect *r = &dev->try_clips_out[i].c;
881
882                         r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
883                         r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
884                         r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
885                         r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
886                 }
887                 /*
888                  * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
889                  * number and it's typically a one-time deal.
890                  */
891                 for (i = 0; i < win->clipcount - 1; i++) {
892                         struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
893
894                         for (j = i + 1; j < win->clipcount; j++) {
895                                 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
896
897                                 if (v4l2_rect_overlap(r1, r2))
898                                         return -EINVAL;
899                         }
900                 }
901                 if (copy_to_user(win->clips, dev->try_clips_out,
902                                  win->clipcount * sizeof(dev->clips_out[0])))
903                         return -EFAULT;
904         }
905         return 0;
906 }
907
908 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
909                                         struct v4l2_format *f)
910 {
911         struct vivid_dev *dev = video_drvdata(file);
912         const struct v4l2_rect *compose = &dev->compose_out;
913         struct v4l2_window *win = &f->fmt.win;
914         int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
915         unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
916         unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
917         void *new_bitmap = NULL;
918
919         if (ret)
920                 return ret;
921
922         if (win->bitmap) {
923                 new_bitmap = memdup_user(win->bitmap, bitmap_size);
924
925                 if (IS_ERR(new_bitmap))
926                         return PTR_ERR(new_bitmap);
927         }
928
929         dev->overlay_out_top = win->w.top;
930         dev->overlay_out_left = win->w.left;
931         kfree(dev->bitmap_out);
932         dev->bitmap_out = new_bitmap;
933         dev->clipcount_out = win->clipcount;
934         if (dev->clipcount_out)
935                 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
936         dev->chromakey_out = win->chromakey;
937         dev->global_alpha_out = win->global_alpha;
938         return ret;
939 }
940
941 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
942 {
943         struct vivid_dev *dev = video_drvdata(file);
944
945         if (i && !dev->fmt_out->can_do_overlay) {
946                 dprintk(dev, 1, "unsupported output format for output overlay\n");
947                 return -EINVAL;
948         }
949
950         dev->overlay_out_enabled = i;
951         return 0;
952 }
953
954 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
955                                 struct v4l2_framebuffer *a)
956 {
957         struct vivid_dev *dev = video_drvdata(file);
958
959         a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
960                         V4L2_FBUF_CAP_BITMAP_CLIPPING |
961                         V4L2_FBUF_CAP_LIST_CLIPPING |
962                         V4L2_FBUF_CAP_CHROMAKEY |
963                         V4L2_FBUF_CAP_SRC_CHROMAKEY |
964                         V4L2_FBUF_CAP_GLOBAL_ALPHA |
965                         V4L2_FBUF_CAP_LOCAL_ALPHA |
966                         V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
967         a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
968         a->base = (void *)dev->video_pbase;
969         a->fmt.width = dev->display_width;
970         a->fmt.height = dev->display_height;
971         if (dev->fb_defined.green.length == 5)
972                 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
973         else
974                 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
975         a->fmt.bytesperline = dev->display_byte_stride;
976         a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
977         a->fmt.field = V4L2_FIELD_NONE;
978         a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
979         a->fmt.priv = 0;
980         return 0;
981 }
982
983 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
984                                 const struct v4l2_framebuffer *a)
985 {
986         struct vivid_dev *dev = video_drvdata(file);
987         const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
988                                       V4L2_FBUF_FLAG_SRC_CHROMAKEY;
989         const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
990                                      V4L2_FBUF_FLAG_LOCAL_ALPHA |
991                                      V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
992
993
994         if ((a->flags & chroma_flags) == chroma_flags)
995                 return -EINVAL;
996         switch (a->flags & alpha_flags) {
997         case 0:
998         case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
999         case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1000         case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1001                 break;
1002         default:
1003                 return -EINVAL;
1004         }
1005         dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1006         dev->fbuf_out_flags |= a->flags & (chroma_flags | alpha_flags);
1007         return 0;
1008 }
1009
1010 static const struct v4l2_audioout vivid_audio_outputs[] = {
1011         { 0, "Line-Out 1" },
1012         { 1, "Line-Out 2" },
1013 };
1014
1015 int vidioc_enum_output(struct file *file, void *priv,
1016                                 struct v4l2_output *out)
1017 {
1018         struct vivid_dev *dev = video_drvdata(file);
1019
1020         if (out->index >= dev->num_outputs)
1021                 return -EINVAL;
1022
1023         out->type = V4L2_OUTPUT_TYPE_ANALOG;
1024         switch (dev->output_type[out->index]) {
1025         case SVID:
1026                 snprintf(out->name, sizeof(out->name), "S-Video %u",
1027                                 dev->output_name_counter[out->index]);
1028                 out->std = V4L2_STD_ALL;
1029                 if (dev->has_audio_outputs)
1030                         out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1031                 out->capabilities = V4L2_OUT_CAP_STD;
1032                 break;
1033         case HDMI:
1034                 snprintf(out->name, sizeof(out->name), "HDMI %u",
1035                                 dev->output_name_counter[out->index]);
1036                 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1037                 break;
1038         }
1039         return 0;
1040 }
1041
1042 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1043 {
1044         struct vivid_dev *dev = video_drvdata(file);
1045
1046         *o = dev->output;
1047         return 0;
1048 }
1049
1050 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1051 {
1052         struct vivid_dev *dev = video_drvdata(file);
1053
1054         if (o >= dev->num_outputs)
1055                 return -EINVAL;
1056
1057         if (o == dev->output)
1058                 return 0;
1059
1060         if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1061                 return -EBUSY;
1062
1063         dev->output = o;
1064         dev->tv_audio_output = 0;
1065         if (dev->output_type[o] == SVID)
1066                 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1067         else
1068                 dev->vid_out_dev.tvnorms = 0;
1069
1070         dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1071         vivid_update_format_out(dev);
1072         return 0;
1073 }
1074
1075 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1076 {
1077         if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1078                 return -EINVAL;
1079         *vout = vivid_audio_outputs[vout->index];
1080         return 0;
1081 }
1082
1083 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1084 {
1085         struct vivid_dev *dev = video_drvdata(file);
1086
1087         if (!vivid_is_svid_out(dev))
1088                 return -EINVAL;
1089         *vout = vivid_audio_outputs[dev->tv_audio_output];
1090         return 0;
1091 }
1092
1093 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1094 {
1095         struct vivid_dev *dev = video_drvdata(file);
1096
1097         if (!vivid_is_svid_out(dev))
1098                 return -EINVAL;
1099         if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1100                 return -EINVAL;
1101         dev->tv_audio_output = vout->index;
1102         return 0;
1103 }
1104
1105 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1106 {
1107         struct vivid_dev *dev = video_drvdata(file);
1108
1109         if (!vivid_is_svid_out(dev))
1110                 return -ENODATA;
1111         if (dev->std_out == id)
1112                 return 0;
1113         if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1114                 return -EBUSY;
1115         dev->std_out = id;
1116         vivid_update_format_out(dev);
1117         return 0;
1118 }
1119
1120 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1121 {
1122         struct v4l2_bt_timings *bt = &timings->bt;
1123
1124         if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1125             v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1126                 return true;
1127
1128         return false;
1129 }
1130
1131 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1132                                     struct v4l2_dv_timings *timings)
1133 {
1134         struct vivid_dev *dev = video_drvdata(file);
1135         if (!vivid_is_hdmi_out(dev))
1136                 return -ENODATA;
1137         if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1138                                 0, NULL, NULL) &&
1139             !valid_cvt_gtf_timings(timings))
1140                 return -EINVAL;
1141         if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
1142                 return 0;
1143         if (vb2_is_busy(&dev->vb_vid_out_q))
1144                 return -EBUSY;
1145         dev->dv_timings_out = *timings;
1146         vivid_update_format_out(dev);
1147         return 0;
1148 }
1149
1150 int vivid_vid_out_g_parm(struct file *file, void *priv,
1151                           struct v4l2_streamparm *parm)
1152 {
1153         struct vivid_dev *dev = video_drvdata(file);
1154
1155         if (parm->type != (dev->multiplanar ?
1156                            V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1157                            V4L2_BUF_TYPE_VIDEO_OUTPUT))
1158                 return -EINVAL;
1159
1160         parm->parm.output.capability   = V4L2_CAP_TIMEPERFRAME;
1161         parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1162         parm->parm.output.writebuffers  = 1;
1163
1164         return 0;
1165 }
1166
1167 int vidioc_subscribe_event(struct v4l2_fh *fh,
1168                         const struct v4l2_event_subscription *sub)
1169 {
1170         switch (sub->type) {
1171         case V4L2_EVENT_CTRL:
1172                 return v4l2_ctrl_subscribe_event(fh, sub);
1173         case V4L2_EVENT_SOURCE_CHANGE:
1174                 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1175                         return v4l2_src_change_event_subscribe(fh, sub);
1176                 break;
1177         default:
1178                 break;
1179         }
1180         return -EINVAL;
1181 }