blob: dd609eea4753cc6d8117787939b69b1c20a7d77d [file] [log] [blame]
Hans Verkuilef834f72014-08-25 07:56:18 -03001/*
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>
Hans Verkuild1e5d8b2016-04-03 16:42:43 -030028#include <media/v4l2-rect.h>
Hans Verkuilef834f72014-08-25 07:56:18 -030029
30#include "vivid-core.h"
31#include "vivid-vid-common.h"
32#include "vivid-kthread-out.h"
33#include "vivid-vid-out.h"
34
Hans Verkuildf9ecb02015-10-28 00:50:37 -020035static int vid_out_queue_setup(struct vb2_queue *vq,
Hans Verkuilef834f72014-08-25 07:56:18 -030036 unsigned *nbuffers, unsigned *nplanes,
Hans Verkuil36c0f8b2016-04-15 09:15:05 -030037 unsigned sizes[], struct device *alloc_devs[])
Hans Verkuilef834f72014-08-25 07:56:18 -030038{
39 struct vivid_dev *dev = vb2_get_drv_priv(vq);
Hans Verkuilddcaee92015-03-09 12:03:52 -030040 const struct vivid_fmt *vfmt = dev->fmt_out;
41 unsigned planes = vfmt->buffers;
Hans Verkuilef834f72014-08-25 07:56:18 -030042 unsigned h = dev->fmt_out_rect.height;
43 unsigned size = dev->bytesperline_out[0] * h;
Hans Verkuilddcaee92015-03-09 12:03:52 -030044 unsigned p;
45
46 for (p = vfmt->buffers; p < vfmt->planes; p++)
Hans Verkuil1f9f23f2015-03-09 12:06:27 -030047 size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
Hans Verkuilef834f72014-08-25 07:56:18 -030048
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
Hans Verkuildf9ecb02015-10-28 00:50:37 -020067 if (*nplanes) {
Hans Verkuilef834f72014-08-25 07:56:18 -030068 /*
Hans Verkuildf9ecb02015-10-28 00:50:37 -020069 * Check if the number of requested planes match
Hans Verkuilef834f72014-08-25 07:56:18 -030070 * the number of planes in the current format. You can't mix that.
71 */
Hans Verkuildf9ecb02015-10-28 00:50:37 -020072 if (*nplanes != planes)
Hans Verkuilef834f72014-08-25 07:56:18 -030073 return -EINVAL;
Hans Verkuilddcaee92015-03-09 12:03:52 -030074 if (sizes[0] < size)
Hans Verkuilef834f72014-08-25 07:56:18 -030075 return -EINVAL;
Hans Verkuilddcaee92015-03-09 12:03:52 -030076 for (p = 1; p < planes; p++) {
Hans Verkuilddcaee92015-03-09 12:03:52 -030077 if (sizes[p] < dev->bytesperline_out[p] * h)
78 return -EINVAL;
Hans Verkuilef834f72014-08-25 07:56:18 -030079 }
80 } else {
Hans Verkuilddcaee92015-03-09 12:03:52 -030081 for (p = 0; p < planes; p++)
82 sizes[p] = p ? dev->bytesperline_out[p] * h : size;
Hans Verkuilef834f72014-08-25 07:56:18 -030083 }
84
85 if (vq->num_buffers + *nbuffers < 2)
86 *nbuffers = 2 - vq->num_buffers;
87
88 *nplanes = planes;
89
Hans Verkuilddcaee92015-03-09 12:03:52 -030090 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]);
Hans Verkuilef834f72014-08-25 07:56:18 -030093 return 0;
94}
95
96static int vid_out_buf_prepare(struct vb2_buffer *vb)
97{
Junghak Sung2d700712015-09-22 10:30:30 -030098 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuilef834f72014-08-25 07:56:18 -030099 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
100 unsigned long size;
Tapasweni Pathak4eaa3a62015-03-05 23:39:40 -0300101 unsigned planes;
Hans Verkuilef834f72014-08-25 07:56:18 -0300102 unsigned p;
103
104 dprintk(dev, 1, "%s\n", __func__);
105
106 if (WARN_ON(NULL == dev->fmt_out))
107 return -EINVAL;
108
Tapasweni Pathak4eaa3a62015-03-05 23:39:40 -0300109 planes = dev->fmt_out->planes;
110
Hans Verkuilef834f72014-08-25 07:56:18 -0300111 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)
Junghak Sung2d700712015-09-22 10:30:30 -0300121 vbuf->field = dev->field_out;
122 else if (vbuf->field != V4L2_FIELD_TOP &&
123 vbuf->field != V4L2_FIELD_BOTTOM)
Hans Verkuilef834f72014-08-25 07:56:18 -0300124 return -EINVAL;
125
126 for (p = 0; p < planes; p++) {
127 size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
Junghak Sung2d700712015-09-22 10:30:30 -0300128 vb->planes[p].data_offset;
Hans Verkuilef834f72014-08-25 07:56:18 -0300129
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
140static void vid_out_buf_queue(struct vb2_buffer *vb)
141{
Junghak Sung2d700712015-09-22 10:30:30 -0300142 struct vb2_v4l2_buffer *vbuf = to_vb2_v4l2_buffer(vb);
Hans Verkuilef834f72014-08-25 07:56:18 -0300143 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
Junghak Sung2d700712015-09-22 10:30:30 -0300144 struct vivid_buffer *buf = container_of(vbuf, struct vivid_buffer, vb);
Hans Verkuilef834f72014-08-25 07:56:18 -0300145
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
153static 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 if (dev->kthread_vid_out)
162 return 0;
163
164 dev->vid_out_seq_count = 0;
165 dprintk(dev, 1, "%s\n", __func__);
166 if (dev->start_streaming_error) {
167 dev->start_streaming_error = false;
168 err = -EINVAL;
169 } else {
170 err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
171 }
172 if (err) {
173 struct vivid_buffer *buf, *tmp;
174
175 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
176 list_del(&buf->list);
Junghak Sung2d700712015-09-22 10:30:30 -0300177 vb2_buffer_done(&buf->vb.vb2_buf,
178 VB2_BUF_STATE_QUEUED);
Hans Verkuilef834f72014-08-25 07:56:18 -0300179 }
180 }
181 return err;
182}
183
184/* abort streaming and wait for last buffer */
185static void vid_out_stop_streaming(struct vb2_queue *vq)
186{
187 struct vivid_dev *dev = vb2_get_drv_priv(vq);
188
189 dprintk(dev, 1, "%s\n", __func__);
190 vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
191 dev->can_loop_video = false;
192}
193
194const struct vb2_ops vivid_vid_out_qops = {
195 .queue_setup = vid_out_queue_setup,
196 .buf_prepare = vid_out_buf_prepare,
197 .buf_queue = vid_out_buf_queue,
198 .start_streaming = vid_out_start_streaming,
199 .stop_streaming = vid_out_stop_streaming,
Prabhakar Lad6dfa5132014-11-18 08:23:39 -0300200 .wait_prepare = vb2_ops_wait_prepare,
201 .wait_finish = vb2_ops_wait_finish,
Hans Verkuilef834f72014-08-25 07:56:18 -0300202};
203
204/*
205 * Called whenever the format has to be reset which can occur when
206 * changing outputs, standard, timings, etc.
207 */
208void vivid_update_format_out(struct vivid_dev *dev)
209{
210 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300211 unsigned size, p;
Prashant Laddhab883ba72015-09-22 11:27:29 -0300212 u64 pixelclock;
Hans Verkuilef834f72014-08-25 07:56:18 -0300213
214 switch (dev->output_type[dev->output]) {
215 case SVID:
216 default:
217 dev->field_out = dev->tv_field_out;
218 dev->sink_rect.width = 720;
219 if (dev->std_out & V4L2_STD_525_60) {
220 dev->sink_rect.height = 480;
221 dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
222 dev->service_set_out = V4L2_SLICED_CAPTION_525;
223 } else {
224 dev->sink_rect.height = 576;
225 dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
Hans Verkuil62f28722014-09-20 06:11:44 -0300226 dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
Hans Verkuilef834f72014-08-25 07:56:18 -0300227 }
228 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
229 break;
230 case HDMI:
231 dev->sink_rect.width = bt->width;
232 dev->sink_rect.height = bt->height;
233 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
Prashant Laddhab883ba72015-09-22 11:27:29 -0300234
235 if (can_reduce_fps(bt) && (bt->flags & V4L2_DV_FL_REDUCED_FPS))
236 pixelclock = div_u64(bt->pixelclock * 1000, 1001);
237 else
238 pixelclock = bt->pixelclock;
239
Hans Verkuilef834f72014-08-25 07:56:18 -0300240 dev->timeperframe_vid_out = (struct v4l2_fract) {
Prashant Laddhab883ba72015-09-22 11:27:29 -0300241 size / 100, (u32)pixelclock / 100
Hans Verkuilef834f72014-08-25 07:56:18 -0300242 };
243 if (bt->interlaced)
244 dev->field_out = V4L2_FIELD_ALTERNATE;
245 else
246 dev->field_out = V4L2_FIELD_NONE;
Hans Verkuil4a203342015-03-20 14:05:06 -0300247 if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
Hans Verkuilef834f72014-08-25 07:56:18 -0300248 if (bt->width == 720 && bt->height <= 576)
249 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
250 else
251 dev->colorspace_out = V4L2_COLORSPACE_REC709;
252 } else {
253 dev->colorspace_out = V4L2_COLORSPACE_SRGB;
254 }
255 break;
256 }
Hans Verkuilca5316d2015-04-28 09:41:37 -0300257 dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
Hans Verkuil3e8a78d2014-11-17 10:26:01 -0300258 dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
259 dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
Hans Verkuilef834f72014-08-25 07:56:18 -0300260 dev->compose_out = dev->sink_rect;
261 dev->compose_bounds_out = dev->sink_rect;
262 dev->crop_out = dev->compose_out;
263 if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
264 dev->crop_out.height /= 2;
265 dev->fmt_out_rect = dev->crop_out;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300266 for (p = 0; p < dev->fmt_out->planes; p++)
267 dev->bytesperline_out[p] =
268 (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
Hans Verkuilef834f72014-08-25 07:56:18 -0300269}
270
271/* Map the field to something that is valid for the current output */
272static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
273{
274 if (vivid_is_svid_out(dev)) {
275 switch (field) {
276 case V4L2_FIELD_INTERLACED_TB:
277 case V4L2_FIELD_INTERLACED_BT:
278 case V4L2_FIELD_SEQ_TB:
279 case V4L2_FIELD_SEQ_BT:
280 case V4L2_FIELD_ALTERNATE:
281 return field;
282 case V4L2_FIELD_INTERLACED:
283 default:
284 return V4L2_FIELD_INTERLACED;
285 }
286 }
287 if (vivid_is_hdmi_out(dev))
288 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
289 V4L2_FIELD_NONE;
290 return V4L2_FIELD_NONE;
291}
292
293static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
294{
295 if (vivid_is_svid_out(dev))
296 return (dev->std_out & V4L2_STD_525_60) ?
297 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
298
299 if (vivid_is_hdmi_out(dev) &&
300 dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
301 return dev->sink_rect.height == 480 ?
302 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
303
304 return TPG_PIXEL_ASPECT_SQUARE;
305}
306
307int vivid_g_fmt_vid_out(struct file *file, void *priv,
308 struct v4l2_format *f)
309{
310 struct vivid_dev *dev = video_drvdata(file);
311 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300312 const struct vivid_fmt *fmt = dev->fmt_out;
Hans Verkuilef834f72014-08-25 07:56:18 -0300313 unsigned p;
314
315 mp->width = dev->fmt_out_rect.width;
316 mp->height = dev->fmt_out_rect.height;
317 mp->field = dev->field_out;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300318 mp->pixelformat = fmt->fourcc;
Hans Verkuilef834f72014-08-25 07:56:18 -0300319 mp->colorspace = dev->colorspace_out;
Hans Verkuilca5316d2015-04-28 09:41:37 -0300320 mp->xfer_func = dev->xfer_func_out;
Hans Verkuil3e8a78d2014-11-17 10:26:01 -0300321 mp->ycbcr_enc = dev->ycbcr_enc_out;
322 mp->quantization = dev->quantization_out;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300323 mp->num_planes = fmt->buffers;
Hans Verkuilef834f72014-08-25 07:56:18 -0300324 for (p = 0; p < mp->num_planes; p++) {
325 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
326 mp->plane_fmt[p].sizeimage =
327 mp->plane_fmt[p].bytesperline * mp->height;
328 }
Hans Verkuilddcaee92015-03-09 12:03:52 -0300329 for (p = fmt->buffers; p < fmt->planes; p++) {
330 unsigned stride = dev->bytesperline_out[p];
331
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300332 mp->plane_fmt[0].sizeimage +=
333 (stride * mp->height) / fmt->vdownsampling[p];
Hans Verkuilddcaee92015-03-09 12:03:52 -0300334 }
Hans Verkuilef834f72014-08-25 07:56:18 -0300335 return 0;
336}
337
338int vivid_try_fmt_vid_out(struct file *file, void *priv,
339 struct v4l2_format *f)
340{
341 struct vivid_dev *dev = video_drvdata(file);
342 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
343 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
344 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
345 const struct vivid_fmt *fmt;
346 unsigned bytesperline, max_bpl;
347 unsigned factor = 1;
348 unsigned w, h;
349 unsigned p;
350
Mauro Carvalho Chehab1fc78bc2014-09-02 17:52:07 -0300351 fmt = vivid_get_format(dev, mp->pixelformat);
Hans Verkuilef834f72014-08-25 07:56:18 -0300352 if (!fmt) {
353 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
354 mp->pixelformat);
355 mp->pixelformat = V4L2_PIX_FMT_YUYV;
Mauro Carvalho Chehab1fc78bc2014-09-02 17:52:07 -0300356 fmt = vivid_get_format(dev, mp->pixelformat);
Hans Verkuilef834f72014-08-25 07:56:18 -0300357 }
358
359 mp->field = vivid_field_out(dev, mp->field);
360 if (vivid_is_svid_out(dev)) {
361 w = 720;
362 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
363 } else {
364 w = dev->sink_rect.width;
365 h = dev->sink_rect.height;
366 }
367 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
368 factor = 2;
369 if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
370 mp->width = w;
371 mp->height = h / factor;
372 } else {
373 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
374
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300375 v4l2_rect_set_min_size(&r, &vivid_min_rect);
376 v4l2_rect_set_max_size(&r, &vivid_max_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300377 if (dev->has_scaler_out && !dev->has_crop_out) {
378 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
379
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300380 v4l2_rect_set_max_size(&r, &max_r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300381 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300382 v4l2_rect_set_max_size(&r, &dev->sink_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300383 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300384 v4l2_rect_set_min_size(&r, &dev->sink_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300385 }
386 mp->width = r.width;
387 mp->height = r.height / factor;
388 }
389
390 /* This driver supports custom bytesperline values */
391
392 /* Calculate the minimum supported bytesperline value */
Hans Verkuil96c76ef2015-03-07 15:04:49 -0300393 bytesperline = (mp->width * fmt->bit_depth[0]) >> 3;
Hans Verkuilef834f72014-08-25 07:56:18 -0300394 /* Calculate the maximum supported bytesperline value */
Hans Verkuil96c76ef2015-03-07 15:04:49 -0300395 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300396 mp->num_planes = fmt->buffers;
Hans Verkuilef834f72014-08-25 07:56:18 -0300397 for (p = 0; p < mp->num_planes; p++) {
398 if (pfmt[p].bytesperline > max_bpl)
399 pfmt[p].bytesperline = max_bpl;
400 if (pfmt[p].bytesperline < bytesperline)
401 pfmt[p].bytesperline = bytesperline;
402 pfmt[p].sizeimage = pfmt[p].bytesperline * mp->height;
403 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
404 }
Hans Verkuilddcaee92015-03-09 12:03:52 -0300405 for (p = fmt->buffers; p < fmt->planes; p++)
406 pfmt[0].sizeimage += (pfmt[0].bytesperline * fmt->bit_depth[p]) /
Hans Verkuil1f9f23f2015-03-09 12:06:27 -0300407 (fmt->bit_depth[0] * fmt->vdownsampling[p]);
Hans Verkuilca5316d2015-04-28 09:41:37 -0300408 mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
Hans Verkuil3e8a78d2014-11-17 10:26:01 -0300409 mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
410 mp->quantization = V4L2_QUANTIZATION_DEFAULT;
411 if (vivid_is_svid_out(dev)) {
Hans Verkuilef834f72014-08-25 07:56:18 -0300412 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuil4a203342015-03-20 14:05:06 -0300413 } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
Hans Verkuilef834f72014-08-25 07:56:18 -0300414 mp->colorspace = V4L2_COLORSPACE_SRGB;
Hans Verkuil3e8a78d2014-11-17 10:26:01 -0300415 if (dev->dvi_d_out)
416 mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
417 } else if (bt->width == 720 && bt->height <= 576) {
Hans Verkuilef834f72014-08-25 07:56:18 -0300418 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
Hans Verkuil3e8a78d2014-11-17 10:26:01 -0300419 } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
420 mp->colorspace != V4L2_COLORSPACE_REC709 &&
421 mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
422 mp->colorspace != V4L2_COLORSPACE_BT2020 &&
423 mp->colorspace != V4L2_COLORSPACE_SRGB) {
Hans Verkuilef834f72014-08-25 07:56:18 -0300424 mp->colorspace = V4L2_COLORSPACE_REC709;
Hans Verkuil3e8a78d2014-11-17 10:26:01 -0300425 }
Hans Verkuilef834f72014-08-25 07:56:18 -0300426 memset(mp->reserved, 0, sizeof(mp->reserved));
427 return 0;
428}
429
430int vivid_s_fmt_vid_out(struct file *file, void *priv,
431 struct v4l2_format *f)
432{
433 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
434 struct vivid_dev *dev = video_drvdata(file);
435 struct v4l2_rect *crop = &dev->crop_out;
436 struct v4l2_rect *compose = &dev->compose_out;
437 struct vb2_queue *q = &dev->vb_vid_out_q;
438 int ret = vivid_try_fmt_vid_out(file, priv, f);
439 unsigned factor = 1;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300440 unsigned p;
Hans Verkuilef834f72014-08-25 07:56:18 -0300441
442 if (ret < 0)
443 return ret;
444
445 if (vb2_is_busy(q) &&
446 (vivid_is_svid_out(dev) ||
447 mp->width != dev->fmt_out_rect.width ||
448 mp->height != dev->fmt_out_rect.height ||
449 mp->pixelformat != dev->fmt_out->fourcc ||
450 mp->field != dev->field_out)) {
451 dprintk(dev, 1, "%s device busy\n", __func__);
452 return -EBUSY;
453 }
454
455 /*
456 * Allow for changing the colorspace on the fly. Useful for testing
457 * purposes, and it is something that HDMI transmitters are able
458 * to do.
459 */
460 if (vb2_is_busy(q))
461 goto set_colorspace;
462
Mauro Carvalho Chehab1fc78bc2014-09-02 17:52:07 -0300463 dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
Hans Verkuilef834f72014-08-25 07:56:18 -0300464 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
465 factor = 2;
466
467 if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
468 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
469
470 if (dev->has_scaler_out) {
471 if (dev->has_crop_out)
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300472 v4l2_rect_map_inside(crop, &r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300473 else
474 *crop = r;
475 if (dev->has_compose_out && !dev->has_crop_out) {
476 struct v4l2_rect min_r = {
477 0, 0,
478 r.width / MAX_ZOOM,
479 factor * r.height / MAX_ZOOM
480 };
481 struct v4l2_rect max_r = {
482 0, 0,
483 r.width * MAX_ZOOM,
484 factor * r.height * MAX_ZOOM
485 };
486
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300487 v4l2_rect_set_min_size(compose, &min_r);
488 v4l2_rect_set_max_size(compose, &max_r);
489 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
Hans Verkuilef834f72014-08-25 07:56:18 -0300490 } else if (dev->has_compose_out) {
491 struct v4l2_rect min_r = {
492 0, 0,
493 crop->width / MAX_ZOOM,
494 factor * crop->height / MAX_ZOOM
495 };
496 struct v4l2_rect max_r = {
497 0, 0,
498 crop->width * MAX_ZOOM,
499 factor * crop->height * MAX_ZOOM
500 };
501
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300502 v4l2_rect_set_min_size(compose, &min_r);
503 v4l2_rect_set_max_size(compose, &max_r);
504 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
Hans Verkuilef834f72014-08-25 07:56:18 -0300505 }
506 } else if (dev->has_compose_out && !dev->has_crop_out) {
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300507 v4l2_rect_set_size_to(crop, &r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300508 r.height *= factor;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300509 v4l2_rect_set_size_to(compose, &r);
510 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
Hans Verkuilef834f72014-08-25 07:56:18 -0300511 } else if (!dev->has_compose_out) {
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300512 v4l2_rect_map_inside(crop, &r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300513 r.height /= factor;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300514 v4l2_rect_set_size_to(compose, &r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300515 } else {
516 r.height *= factor;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300517 v4l2_rect_set_max_size(compose, &r);
518 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
Hans Verkuilef834f72014-08-25 07:56:18 -0300519 crop->top *= factor;
520 crop->height *= factor;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300521 v4l2_rect_set_size_to(crop, compose);
522 v4l2_rect_map_inside(crop, &r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300523 crop->top /= factor;
524 crop->height /= factor;
525 }
526 } else {
527 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
528
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300529 v4l2_rect_set_size_to(crop, &r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300530 r.height /= factor;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300531 v4l2_rect_set_size_to(compose, &r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300532 }
533
534 dev->fmt_out_rect.width = mp->width;
535 dev->fmt_out_rect.height = mp->height;
Hans Verkuilddcaee92015-03-09 12:03:52 -0300536 for (p = 0; p < mp->num_planes; p++)
537 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
538 for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
539 dev->bytesperline_out[p] =
540 (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
541 dev->fmt_out->bit_depth[0];
Hans Verkuilef834f72014-08-25 07:56:18 -0300542 dev->field_out = mp->field;
543 if (vivid_is_svid_out(dev))
544 dev->tv_field_out = mp->field;
545
546set_colorspace:
547 dev->colorspace_out = mp->colorspace;
Hans Verkuilca5316d2015-04-28 09:41:37 -0300548 dev->xfer_func_out = mp->xfer_func;
Hans Verkuil3e8a78d2014-11-17 10:26:01 -0300549 dev->ycbcr_enc_out = mp->ycbcr_enc;
550 dev->quantization_out = mp->quantization;
Hans Verkuilef834f72014-08-25 07:56:18 -0300551 if (dev->loop_video) {
552 vivid_send_source_change(dev, SVID);
553 vivid_send_source_change(dev, HDMI);
554 }
555 return 0;
556}
557
558int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
559 struct v4l2_format *f)
560{
561 struct vivid_dev *dev = video_drvdata(file);
562
563 if (!dev->multiplanar)
564 return -ENOTTY;
565 return vivid_g_fmt_vid_out(file, priv, f);
566}
567
568int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
569 struct v4l2_format *f)
570{
571 struct vivid_dev *dev = video_drvdata(file);
572
573 if (!dev->multiplanar)
574 return -ENOTTY;
575 return vivid_try_fmt_vid_out(file, priv, f);
576}
577
578int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
579 struct v4l2_format *f)
580{
581 struct vivid_dev *dev = video_drvdata(file);
582
583 if (!dev->multiplanar)
584 return -ENOTTY;
585 return vivid_s_fmt_vid_out(file, priv, f);
586}
587
588int vidioc_g_fmt_vid_out(struct file *file, void *priv,
589 struct v4l2_format *f)
590{
591 struct vivid_dev *dev = video_drvdata(file);
592
593 if (dev->multiplanar)
594 return -ENOTTY;
595 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
596}
597
598int vidioc_try_fmt_vid_out(struct file *file, void *priv,
599 struct v4l2_format *f)
600{
601 struct vivid_dev *dev = video_drvdata(file);
602
603 if (dev->multiplanar)
604 return -ENOTTY;
605 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
606}
607
608int vidioc_s_fmt_vid_out(struct file *file, void *priv,
609 struct v4l2_format *f)
610{
611 struct vivid_dev *dev = video_drvdata(file);
612
613 if (dev->multiplanar)
614 return -ENOTTY;
615 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
616}
617
618int vivid_vid_out_g_selection(struct file *file, void *priv,
619 struct v4l2_selection *sel)
620{
621 struct vivid_dev *dev = video_drvdata(file);
622
623 if (!dev->has_crop_out && !dev->has_compose_out)
624 return -ENOTTY;
625 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
626 return -EINVAL;
627
628 sel->r.left = sel->r.top = 0;
629 switch (sel->target) {
630 case V4L2_SEL_TGT_CROP:
631 if (!dev->has_crop_out)
632 return -EINVAL;
633 sel->r = dev->crop_out;
634 break;
635 case V4L2_SEL_TGT_CROP_DEFAULT:
636 if (!dev->has_crop_out)
637 return -EINVAL;
638 sel->r = dev->fmt_out_rect;
639 break;
640 case V4L2_SEL_TGT_CROP_BOUNDS:
Hans Verkuilbb9ff072014-12-06 07:30:03 -0300641 if (!dev->has_crop_out)
Hans Verkuilef834f72014-08-25 07:56:18 -0300642 return -EINVAL;
643 sel->r = vivid_max_rect;
644 break;
645 case V4L2_SEL_TGT_COMPOSE:
646 if (!dev->has_compose_out)
647 return -EINVAL;
648 sel->r = dev->compose_out;
649 break;
650 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
651 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
652 if (!dev->has_compose_out)
653 return -EINVAL;
654 sel->r = dev->sink_rect;
655 break;
656 default:
657 return -EINVAL;
658 }
659 return 0;
660}
661
662int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
663{
664 struct vivid_dev *dev = video_drvdata(file);
665 struct v4l2_rect *crop = &dev->crop_out;
666 struct v4l2_rect *compose = &dev->compose_out;
667 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
668 int ret;
669
670 if (!dev->has_crop_out && !dev->has_compose_out)
671 return -ENOTTY;
672 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
673 return -EINVAL;
674
675 switch (s->target) {
676 case V4L2_SEL_TGT_CROP:
677 if (!dev->has_crop_out)
678 return -EINVAL;
679 ret = vivid_vid_adjust_sel(s->flags, &s->r);
680 if (ret)
681 return ret;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300682 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
683 v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300684 if (dev->has_scaler_out) {
685 struct v4l2_rect max_rect = {
686 0, 0,
687 dev->sink_rect.width * MAX_ZOOM,
688 (dev->sink_rect.height / factor) * MAX_ZOOM
689 };
690
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300691 v4l2_rect_set_max_size(&s->r, &max_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300692 if (dev->has_compose_out) {
693 struct v4l2_rect min_rect = {
694 0, 0,
695 s->r.width / MAX_ZOOM,
696 (s->r.height * factor) / MAX_ZOOM
697 };
698 struct v4l2_rect max_rect = {
699 0, 0,
700 s->r.width * MAX_ZOOM,
701 (s->r.height * factor) * MAX_ZOOM
702 };
703
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300704 v4l2_rect_set_min_size(compose, &min_rect);
705 v4l2_rect_set_max_size(compose, &max_rect);
706 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
Hans Verkuilef834f72014-08-25 07:56:18 -0300707 }
708 } else if (dev->has_compose_out) {
709 s->r.top *= factor;
710 s->r.height *= factor;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300711 v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
712 v4l2_rect_set_size_to(compose, &s->r);
713 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
Hans Verkuilef834f72014-08-25 07:56:18 -0300714 s->r.top /= factor;
715 s->r.height /= factor;
716 } else {
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300717 v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300718 s->r.height /= factor;
719 }
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300720 v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300721 *crop = s->r;
722 break;
723 case V4L2_SEL_TGT_COMPOSE:
724 if (!dev->has_compose_out)
725 return -EINVAL;
726 ret = vivid_vid_adjust_sel(s->flags, &s->r);
727 if (ret)
728 return ret;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300729 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
730 v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
731 v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
Hans Verkuilef834f72014-08-25 07:56:18 -0300732 s->r.top /= factor;
733 s->r.height /= factor;
734 if (dev->has_scaler_out) {
735 struct v4l2_rect fmt = dev->fmt_out_rect;
736 struct v4l2_rect max_rect = {
737 0, 0,
738 s->r.width * MAX_ZOOM,
739 s->r.height * MAX_ZOOM
740 };
741 struct v4l2_rect min_rect = {
742 0, 0,
743 s->r.width / MAX_ZOOM,
744 s->r.height / MAX_ZOOM
745 };
746
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300747 v4l2_rect_set_min_size(&fmt, &min_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300748 if (!dev->has_crop_out)
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300749 v4l2_rect_set_max_size(&fmt, &max_rect);
750 if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
Hans Verkuilef834f72014-08-25 07:56:18 -0300751 vb2_is_busy(&dev->vb_vid_out_q))
752 return -EBUSY;
753 if (dev->has_crop_out) {
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300754 v4l2_rect_set_min_size(crop, &min_rect);
755 v4l2_rect_set_max_size(crop, &max_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300756 }
757 dev->fmt_out_rect = fmt;
758 } else if (dev->has_crop_out) {
759 struct v4l2_rect fmt = dev->fmt_out_rect;
760
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300761 v4l2_rect_set_min_size(&fmt, &s->r);
762 if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
Hans Verkuilef834f72014-08-25 07:56:18 -0300763 vb2_is_busy(&dev->vb_vid_out_q))
764 return -EBUSY;
765 dev->fmt_out_rect = fmt;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300766 v4l2_rect_set_size_to(crop, &s->r);
767 v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300768 } else {
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300769 if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
Hans Verkuilef834f72014-08-25 07:56:18 -0300770 vb2_is_busy(&dev->vb_vid_out_q))
771 return -EBUSY;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300772 v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
773 v4l2_rect_set_size_to(crop, &s->r);
Hans Verkuilef834f72014-08-25 07:56:18 -0300774 crop->height /= factor;
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300775 v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
Hans Verkuilef834f72014-08-25 07:56:18 -0300776 }
777 s->r.top *= factor;
778 s->r.height *= factor;
779 if (dev->bitmap_out && (compose->width != s->r.width ||
780 compose->height != s->r.height)) {
781 kfree(dev->bitmap_out);
782 dev->bitmap_out = NULL;
783 }
784 *compose = s->r;
785 break;
786 default:
787 return -EINVAL;
788 }
789
790 return 0;
791}
792
793int vivid_vid_out_cropcap(struct file *file, void *priv,
794 struct v4l2_cropcap *cap)
795{
796 struct vivid_dev *dev = video_drvdata(file);
797
798 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
799 return -EINVAL;
800
801 switch (vivid_get_pixel_aspect(dev)) {
802 case TPG_PIXEL_ASPECT_NTSC:
803 cap->pixelaspect.numerator = 11;
804 cap->pixelaspect.denominator = 10;
805 break;
806 case TPG_PIXEL_ASPECT_PAL:
807 cap->pixelaspect.numerator = 54;
808 cap->pixelaspect.denominator = 59;
809 break;
810 case TPG_PIXEL_ASPECT_SQUARE:
811 cap->pixelaspect.numerator = 1;
812 cap->pixelaspect.denominator = 1;
813 break;
814 }
815 return 0;
816}
817
818int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
819 struct v4l2_format *f)
820{
821 struct vivid_dev *dev = video_drvdata(file);
822 const struct v4l2_rect *compose = &dev->compose_out;
823 struct v4l2_window *win = &f->fmt.win;
824 unsigned clipcount = win->clipcount;
825
826 if (!dev->has_fb)
827 return -EINVAL;
828 win->w.top = dev->overlay_out_top;
829 win->w.left = dev->overlay_out_left;
830 win->w.width = compose->width;
831 win->w.height = compose->height;
832 win->clipcount = dev->clipcount_out;
833 win->field = V4L2_FIELD_ANY;
834 win->chromakey = dev->chromakey_out;
835 win->global_alpha = dev->global_alpha_out;
836 if (clipcount > dev->clipcount_out)
837 clipcount = dev->clipcount_out;
838 if (dev->bitmap_out == NULL)
839 win->bitmap = NULL;
840 else if (win->bitmap) {
841 if (copy_to_user(win->bitmap, dev->bitmap_out,
842 ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
843 return -EFAULT;
844 }
845 if (clipcount && win->clips) {
846 if (copy_to_user(win->clips, dev->clips_out,
847 clipcount * sizeof(dev->clips_out[0])))
848 return -EFAULT;
849 }
850 return 0;
851}
852
853int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
854 struct v4l2_format *f)
855{
856 struct vivid_dev *dev = video_drvdata(file);
857 const struct v4l2_rect *compose = &dev->compose_out;
858 struct v4l2_window *win = &f->fmt.win;
859 int i, j;
860
861 if (!dev->has_fb)
862 return -EINVAL;
863 win->w.left = clamp_t(int, win->w.left,
864 -dev->display_width, dev->display_width);
865 win->w.top = clamp_t(int, win->w.top,
866 -dev->display_height, dev->display_height);
867 win->w.width = compose->width;
868 win->w.height = compose->height;
869 /*
870 * It makes no sense for an OSD to overlay only top or bottom fields,
871 * so always set this to ANY.
872 */
873 win->field = V4L2_FIELD_ANY;
874 if (win->clipcount && !win->clips)
875 win->clipcount = 0;
876 if (win->clipcount > MAX_CLIPS)
877 win->clipcount = MAX_CLIPS;
878 if (win->clipcount) {
879 if (copy_from_user(dev->try_clips_out, win->clips,
880 win->clipcount * sizeof(dev->clips_out[0])))
881 return -EFAULT;
882 for (i = 0; i < win->clipcount; i++) {
883 struct v4l2_rect *r = &dev->try_clips_out[i].c;
884
885 r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
886 r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
887 r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
888 r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
889 }
890 /*
891 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
892 * number and it's typically a one-time deal.
893 */
894 for (i = 0; i < win->clipcount - 1; i++) {
895 struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
896
897 for (j = i + 1; j < win->clipcount; j++) {
898 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
899
Hans Verkuild1e5d8b2016-04-03 16:42:43 -0300900 if (v4l2_rect_overlap(r1, r2))
Hans Verkuilef834f72014-08-25 07:56:18 -0300901 return -EINVAL;
902 }
903 }
904 if (copy_to_user(win->clips, dev->try_clips_out,
905 win->clipcount * sizeof(dev->clips_out[0])))
906 return -EFAULT;
907 }
908 return 0;
909}
910
911int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
912 struct v4l2_format *f)
913{
914 struct vivid_dev *dev = video_drvdata(file);
915 const struct v4l2_rect *compose = &dev->compose_out;
916 struct v4l2_window *win = &f->fmt.win;
917 int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
918 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
919 unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
920 void *new_bitmap = NULL;
921
922 if (ret)
923 return ret;
924
925 if (win->bitmap) {
Mauro Carvalho Chehab02829692014-09-03 14:57:30 -0300926 new_bitmap = memdup_user(win->bitmap, bitmap_size);
Hans Verkuilef834f72014-08-25 07:56:18 -0300927
Mauro Carvalho Chehab02829692014-09-03 14:57:30 -0300928 if (IS_ERR(new_bitmap))
929 return PTR_ERR(new_bitmap);
Hans Verkuilef834f72014-08-25 07:56:18 -0300930 }
931
932 dev->overlay_out_top = win->w.top;
933 dev->overlay_out_left = win->w.left;
934 kfree(dev->bitmap_out);
935 dev->bitmap_out = new_bitmap;
936 dev->clipcount_out = win->clipcount;
937 if (dev->clipcount_out)
938 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
939 dev->chromakey_out = win->chromakey;
940 dev->global_alpha_out = win->global_alpha;
941 return ret;
942}
943
944int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
945{
946 struct vivid_dev *dev = video_drvdata(file);
947
948 if (i && !dev->fmt_out->can_do_overlay) {
949 dprintk(dev, 1, "unsupported output format for output overlay\n");
950 return -EINVAL;
951 }
952
953 dev->overlay_out_enabled = i;
954 return 0;
955}
956
957int vivid_vid_out_g_fbuf(struct file *file, void *fh,
958 struct v4l2_framebuffer *a)
959{
960 struct vivid_dev *dev = video_drvdata(file);
961
962 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
963 V4L2_FBUF_CAP_BITMAP_CLIPPING |
964 V4L2_FBUF_CAP_LIST_CLIPPING |
965 V4L2_FBUF_CAP_CHROMAKEY |
966 V4L2_FBUF_CAP_SRC_CHROMAKEY |
967 V4L2_FBUF_CAP_GLOBAL_ALPHA |
968 V4L2_FBUF_CAP_LOCAL_ALPHA |
969 V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
970 a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
971 a->base = (void *)dev->video_pbase;
972 a->fmt.width = dev->display_width;
973 a->fmt.height = dev->display_height;
974 if (dev->fb_defined.green.length == 5)
975 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
976 else
977 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
978 a->fmt.bytesperline = dev->display_byte_stride;
979 a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
980 a->fmt.field = V4L2_FIELD_NONE;
981 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
982 a->fmt.priv = 0;
983 return 0;
984}
985
986int vivid_vid_out_s_fbuf(struct file *file, void *fh,
987 const struct v4l2_framebuffer *a)
988{
989 struct vivid_dev *dev = video_drvdata(file);
990 const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
991 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
992 const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
993 V4L2_FBUF_FLAG_LOCAL_ALPHA |
994 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
995
996
997 if ((a->flags & chroma_flags) == chroma_flags)
998 return -EINVAL;
999 switch (a->flags & alpha_flags) {
1000 case 0:
1001 case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1002 case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1003 case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1004 break;
1005 default:
1006 return -EINVAL;
1007 }
1008 dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1009 dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
1010 return 0;
1011}
1012
1013static const struct v4l2_audioout vivid_audio_outputs[] = {
1014 { 0, "Line-Out 1" },
1015 { 1, "Line-Out 2" },
1016};
1017
1018int vidioc_enum_output(struct file *file, void *priv,
1019 struct v4l2_output *out)
1020{
1021 struct vivid_dev *dev = video_drvdata(file);
1022
1023 if (out->index >= dev->num_outputs)
1024 return -EINVAL;
1025
1026 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1027 switch (dev->output_type[out->index]) {
1028 case SVID:
1029 snprintf(out->name, sizeof(out->name), "S-Video %u",
1030 dev->output_name_counter[out->index]);
1031 out->std = V4L2_STD_ALL;
1032 if (dev->has_audio_outputs)
1033 out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1034 out->capabilities = V4L2_OUT_CAP_STD;
1035 break;
1036 case HDMI:
1037 snprintf(out->name, sizeof(out->name), "HDMI %u",
1038 dev->output_name_counter[out->index]);
1039 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1040 break;
1041 }
1042 return 0;
1043}
1044
1045int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1046{
1047 struct vivid_dev *dev = video_drvdata(file);
1048
1049 *o = dev->output;
1050 return 0;
1051}
1052
1053int vidioc_s_output(struct file *file, void *priv, unsigned o)
1054{
1055 struct vivid_dev *dev = video_drvdata(file);
1056
1057 if (o >= dev->num_outputs)
1058 return -EINVAL;
1059
1060 if (o == dev->output)
1061 return 0;
1062
1063 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1064 return -EBUSY;
1065
1066 dev->output = o;
1067 dev->tv_audio_output = 0;
1068 if (dev->output_type[o] == SVID)
1069 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1070 else
1071 dev->vid_out_dev.tvnorms = 0;
1072
1073 dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1074 vivid_update_format_out(dev);
1075 return 0;
1076}
1077
1078int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1079{
1080 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1081 return -EINVAL;
1082 *vout = vivid_audio_outputs[vout->index];
1083 return 0;
1084}
1085
1086int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1087{
1088 struct vivid_dev *dev = video_drvdata(file);
1089
1090 if (!vivid_is_svid_out(dev))
1091 return -EINVAL;
1092 *vout = vivid_audio_outputs[dev->tv_audio_output];
1093 return 0;
1094}
1095
1096int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1097{
1098 struct vivid_dev *dev = video_drvdata(file);
1099
1100 if (!vivid_is_svid_out(dev))
1101 return -EINVAL;
1102 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1103 return -EINVAL;
1104 dev->tv_audio_output = vout->index;
1105 return 0;
1106}
1107
1108int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1109{
1110 struct vivid_dev *dev = video_drvdata(file);
1111
1112 if (!vivid_is_svid_out(dev))
1113 return -ENODATA;
1114 if (dev->std_out == id)
1115 return 0;
1116 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1117 return -EBUSY;
1118 dev->std_out = id;
1119 vivid_update_format_out(dev);
1120 return 0;
1121}
1122
Prashant Laddhab1304f92015-08-03 05:36:43 -03001123static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1124{
1125 struct v4l2_bt_timings *bt = &timings->bt;
1126
1127 if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1128 v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1129 return true;
1130
1131 return false;
1132}
1133
Hans Verkuilef834f72014-08-25 07:56:18 -03001134int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1135 struct v4l2_dv_timings *timings)
1136{
1137 struct vivid_dev *dev = video_drvdata(file);
Hans Verkuilef834f72014-08-25 07:56:18 -03001138 if (!vivid_is_hdmi_out(dev))
1139 return -ENODATA;
Hans Verkuilef834f72014-08-25 07:56:18 -03001140 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
Prashant Laddhab1304f92015-08-03 05:36:43 -03001141 0, NULL, NULL) &&
1142 !valid_cvt_gtf_timings(timings))
Hans Verkuilef834f72014-08-25 07:56:18 -03001143 return -EINVAL;
Hans Verkuil85f9e062015-11-13 09:46:26 -02001144 if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
Hans Verkuilef834f72014-08-25 07:56:18 -03001145 return 0;
Hans Verkuil1bd08352015-03-14 09:35:23 -03001146 if (vb2_is_busy(&dev->vb_vid_out_q))
1147 return -EBUSY;
Hans Verkuilef834f72014-08-25 07:56:18 -03001148 dev->dv_timings_out = *timings;
1149 vivid_update_format_out(dev);
1150 return 0;
1151}
1152
Hans Verkuilef834f72014-08-25 07:56:18 -03001153int vivid_vid_out_g_parm(struct file *file, void *priv,
1154 struct v4l2_streamparm *parm)
1155{
1156 struct vivid_dev *dev = video_drvdata(file);
1157
1158 if (parm->type != (dev->multiplanar ?
1159 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1160 V4L2_BUF_TYPE_VIDEO_OUTPUT))
1161 return -EINVAL;
1162
1163 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1164 parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1165 parm->parm.output.writebuffers = 1;
Mauro Carvalho Chehabee120ae2015-04-29 20:24:56 -03001166
1167 return 0;
Hans Verkuilef834f72014-08-25 07:56:18 -03001168}
1169
1170int vidioc_subscribe_event(struct v4l2_fh *fh,
1171 const struct v4l2_event_subscription *sub)
1172{
1173 switch (sub->type) {
1174 case V4L2_EVENT_CTRL:
1175 return v4l2_ctrl_subscribe_event(fh, sub);
1176 case V4L2_EVENT_SOURCE_CHANGE:
1177 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1178 return v4l2_src_change_event_subscribe(fh, sub);
1179 break;
1180 default:
1181 break;
1182 }
1183 return -EINVAL;
1184}