[media] vivid: fix try_fmt behavior
[linux-2.6/btrfs-unstable.git] / drivers / media / platform / vivid / vivid-vid-out.c
blob0b1b6218ede887601ffe7f5b277ebf2767f30d8a
1 /*
2 * vivid-vid-out.c - video output support functions.
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
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.
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.
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>
30 #include "vivid-core.h"
31 #include "vivid-vid-common.h"
32 #include "vivid-kthread-out.h"
33 #include "vivid-vid-out.h"
35 static int vid_out_queue_setup(struct vb2_queue *vq,
36 unsigned *nbuffers, unsigned *nplanes,
37 unsigned sizes[], struct device *alloc_devs[])
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;
46 for (p = vfmt->buffers; p < vfmt->planes; p++)
47 size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
49 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
51 * You cannot use write() with FIELD_ALTERNATE since the field
52 * information (TOP/BOTTOM) cannot be passed to the kernel.
54 if (vb2_fileio_is_active(vq))
55 return -EINVAL;
58 if (dev->queue_setup_error) {
60 * Error injection: test what happens if queue_setup() returns
61 * an error.
63 dev->queue_setup_error = false;
64 return -EINVAL;
67 if (*nplanes) {
69 * Check if the number of requested planes match
70 * the number of planes in the current format. You can't mix that.
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;
80 } else {
81 for (p = 0; p < planes; p++)
82 sizes[p] = p ? dev->bytesperline_out[p] * h : size;
85 if (vq->num_buffers + *nbuffers < 2)
86 *nbuffers = 2 - vq->num_buffers;
88 *nplanes = planes;
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;
96 static int vid_out_buf_prepare(struct vb2_buffer *vb)
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;
104 dprintk(dev, 1, "%s\n", __func__);
106 if (WARN_ON(NULL == dev->fmt_out))
107 return -EINVAL;
109 planes = dev->fmt_out->planes;
111 if (dev->buf_prepare_error) {
113 * Error injection: test what happens if buf_prepare() returns
114 * an error.
116 dev->buf_prepare_error = false;
117 return -EINVAL;
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;
126 for (p = 0; p < planes; p++) {
127 size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
128 vb->planes[p].data_offset;
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;
137 return 0;
140 static void vid_out_buf_queue(struct vb2_buffer *vb)
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);
146 dprintk(dev, 1, "%s\n", __func__);
148 spin_lock(&dev->slock);
149 list_add_tail(&buf->list, &dev->vid_out_active);
150 spin_unlock(&dev->slock);
153 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
155 struct vivid_dev *dev = vb2_get_drv_priv(vq);
156 int err;
158 if (vb2_is_streaming(&dev->vb_vid_cap_q))
159 dev->can_loop_video = vivid_vid_can_loop(dev);
161 if (dev->kthread_vid_out)
162 return 0;
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);
172 if (err) {
173 struct vivid_buffer *buf, *tmp;
175 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
176 list_del(&buf->list);
177 vb2_buffer_done(&buf->vb.vb2_buf,
178 VB2_BUF_STATE_QUEUED);
181 return err;
184 /* abort streaming and wait for last buffer */
185 static void vid_out_stop_streaming(struct vb2_queue *vq)
187 struct vivid_dev *dev = vb2_get_drv_priv(vq);
189 dprintk(dev, 1, "%s\n", __func__);
190 vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
191 dev->can_loop_video = false;
194 const 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,
200 .wait_prepare = vb2_ops_wait_prepare,
201 .wait_finish = vb2_ops_wait_finish,
205 * Called whenever the format has to be reset which can occur when
206 * changing outputs, standard, timings, etc.
208 void vivid_update_format_out(struct vivid_dev *dev)
210 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
211 unsigned size, p;
212 u64 pixelclock;
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 };
226 dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
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);
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;
240 dev->timeperframe_vid_out = (struct v4l2_fract) {
241 size / 100, (u32)pixelclock / 100
243 if (bt->interlaced)
244 dev->field_out = V4L2_FIELD_ALTERNATE;
245 else
246 dev->field_out = V4L2_FIELD_NONE;
247 if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
248 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;
255 break;
257 dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
258 dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
259 dev->hsv_enc_out = V4L2_HSV_ENC_180;
260 dev->quantization_out = V4L2_QUANTIZATION_DEFAULT;
261 dev->compose_out = dev->sink_rect;
262 dev->compose_bounds_out = dev->sink_rect;
263 dev->crop_out = dev->compose_out;
264 if (V4L2_FIELD_HAS_T_OR_B(dev->field_out))
265 dev->crop_out.height /= 2;
266 dev->fmt_out_rect = dev->crop_out;
267 for (p = 0; p < dev->fmt_out->planes; p++)
268 dev->bytesperline_out[p] =
269 (dev->sink_rect.width * dev->fmt_out->bit_depth[p]) / 8;
272 /* Map the field to something that is valid for the current output */
273 static enum v4l2_field vivid_field_out(struct vivid_dev *dev, enum v4l2_field field)
275 if (vivid_is_svid_out(dev)) {
276 switch (field) {
277 case V4L2_FIELD_INTERLACED_TB:
278 case V4L2_FIELD_INTERLACED_BT:
279 case V4L2_FIELD_SEQ_TB:
280 case V4L2_FIELD_SEQ_BT:
281 case V4L2_FIELD_ALTERNATE:
282 return field;
283 case V4L2_FIELD_INTERLACED:
284 default:
285 return V4L2_FIELD_INTERLACED;
288 if (vivid_is_hdmi_out(dev))
289 return dev->dv_timings_out.bt.interlaced ? V4L2_FIELD_ALTERNATE :
290 V4L2_FIELD_NONE;
291 return V4L2_FIELD_NONE;
294 static enum tpg_pixel_aspect vivid_get_pixel_aspect(const struct vivid_dev *dev)
296 if (vivid_is_svid_out(dev))
297 return (dev->std_out & V4L2_STD_525_60) ?
298 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
300 if (vivid_is_hdmi_out(dev) &&
301 dev->sink_rect.width == 720 && dev->sink_rect.height <= 576)
302 return dev->sink_rect.height == 480 ?
303 TPG_PIXEL_ASPECT_NTSC : TPG_PIXEL_ASPECT_PAL;
305 return TPG_PIXEL_ASPECT_SQUARE;
308 int vivid_g_fmt_vid_out(struct file *file, void *priv,
309 struct v4l2_format *f)
311 struct vivid_dev *dev = video_drvdata(file);
312 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
313 const struct vivid_fmt *fmt = dev->fmt_out;
314 unsigned p;
316 mp->width = dev->fmt_out_rect.width;
317 mp->height = dev->fmt_out_rect.height;
318 mp->field = dev->field_out;
319 mp->pixelformat = fmt->fourcc;
320 mp->colorspace = dev->colorspace_out;
321 mp->xfer_func = dev->xfer_func_out;
322 mp->ycbcr_enc = dev->ycbcr_enc_out;
323 mp->quantization = dev->quantization_out;
324 mp->num_planes = fmt->buffers;
325 for (p = 0; p < mp->num_planes; p++) {
326 mp->plane_fmt[p].bytesperline = dev->bytesperline_out[p];
327 mp->plane_fmt[p].sizeimage =
328 mp->plane_fmt[p].bytesperline * mp->height;
330 for (p = fmt->buffers; p < fmt->planes; p++) {
331 unsigned stride = dev->bytesperline_out[p];
333 mp->plane_fmt[0].sizeimage +=
334 (stride * mp->height) / fmt->vdownsampling[p];
336 return 0;
339 int vivid_try_fmt_vid_out(struct file *file, void *priv,
340 struct v4l2_format *f)
342 struct vivid_dev *dev = video_drvdata(file);
343 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
344 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
345 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
346 const struct vivid_fmt *fmt;
347 unsigned bytesperline, max_bpl;
348 unsigned factor = 1;
349 unsigned w, h;
350 unsigned p;
352 fmt = vivid_get_format(dev, mp->pixelformat);
353 if (!fmt) {
354 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
355 mp->pixelformat);
356 mp->pixelformat = V4L2_PIX_FMT_YUYV;
357 fmt = vivid_get_format(dev, mp->pixelformat);
360 mp->field = vivid_field_out(dev, mp->field);
361 if (vivid_is_svid_out(dev)) {
362 w = 720;
363 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
364 } else {
365 w = dev->sink_rect.width;
366 h = dev->sink_rect.height;
368 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
369 factor = 2;
370 if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
371 mp->width = w;
372 mp->height = h / factor;
373 } else {
374 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
376 v4l2_rect_set_min_size(&r, &vivid_min_rect);
377 v4l2_rect_set_max_size(&r, &vivid_max_rect);
378 if (dev->has_scaler_out && !dev->has_crop_out) {
379 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
381 v4l2_rect_set_max_size(&r, &max_r);
382 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
383 v4l2_rect_set_max_size(&r, &dev->sink_rect);
384 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
385 v4l2_rect_set_min_size(&r, &dev->sink_rect);
387 mp->width = r.width;
388 mp->height = r.height / factor;
391 /* This driver supports custom bytesperline values */
393 mp->num_planes = fmt->buffers;
394 for (p = 0; p < fmt->buffers; p++) {
395 /* Calculate the minimum supported bytesperline value */
396 bytesperline = (mp->width * fmt->bit_depth[p]) >> 3;
397 /* Calculate the maximum supported bytesperline value */
398 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[p]) >> 3;
400 if (pfmt[p].bytesperline > max_bpl)
401 pfmt[p].bytesperline = max_bpl;
402 if (pfmt[p].bytesperline < bytesperline)
403 pfmt[p].bytesperline = bytesperline;
405 pfmt[p].sizeimage = (pfmt[p].bytesperline * mp->height) /
406 fmt->vdownsampling[p];
408 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
410 for (p = fmt->buffers; p < fmt->planes; p++)
411 pfmt[0].sizeimage += (pfmt[0].bytesperline * mp->height *
412 (fmt->bit_depth[p] / fmt->vdownsampling[p])) /
413 (fmt->bit_depth[0] / fmt->vdownsampling[0]);
415 mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
416 mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
417 mp->quantization = V4L2_QUANTIZATION_DEFAULT;
418 if (vivid_is_svid_out(dev)) {
419 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
420 } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
421 mp->colorspace = V4L2_COLORSPACE_SRGB;
422 if (dev->dvi_d_out)
423 mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
424 } else if (bt->width == 720 && bt->height <= 576) {
425 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
426 } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
427 mp->colorspace != V4L2_COLORSPACE_REC709 &&
428 mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
429 mp->colorspace != V4L2_COLORSPACE_BT2020 &&
430 mp->colorspace != V4L2_COLORSPACE_SRGB) {
431 mp->colorspace = V4L2_COLORSPACE_REC709;
433 memset(mp->reserved, 0, sizeof(mp->reserved));
434 return 0;
437 int vivid_s_fmt_vid_out(struct file *file, void *priv,
438 struct v4l2_format *f)
440 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
441 struct vivid_dev *dev = video_drvdata(file);
442 struct v4l2_rect *crop = &dev->crop_out;
443 struct v4l2_rect *compose = &dev->compose_out;
444 struct vb2_queue *q = &dev->vb_vid_out_q;
445 int ret = vivid_try_fmt_vid_out(file, priv, f);
446 unsigned factor = 1;
447 unsigned p;
449 if (ret < 0)
450 return ret;
452 if (vb2_is_busy(q) &&
453 (vivid_is_svid_out(dev) ||
454 mp->width != dev->fmt_out_rect.width ||
455 mp->height != dev->fmt_out_rect.height ||
456 mp->pixelformat != dev->fmt_out->fourcc ||
457 mp->field != dev->field_out)) {
458 dprintk(dev, 1, "%s device busy\n", __func__);
459 return -EBUSY;
463 * Allow for changing the colorspace on the fly. Useful for testing
464 * purposes, and it is something that HDMI transmitters are able
465 * to do.
467 if (vb2_is_busy(q))
468 goto set_colorspace;
470 dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
471 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
472 factor = 2;
474 if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
475 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
477 if (dev->has_scaler_out) {
478 if (dev->has_crop_out)
479 v4l2_rect_map_inside(crop, &r);
480 else
481 *crop = r;
482 if (dev->has_compose_out && !dev->has_crop_out) {
483 struct v4l2_rect min_r = {
484 0, 0,
485 r.width / MAX_ZOOM,
486 factor * r.height / MAX_ZOOM
488 struct v4l2_rect max_r = {
489 0, 0,
490 r.width * MAX_ZOOM,
491 factor * r.height * MAX_ZOOM
494 v4l2_rect_set_min_size(compose, &min_r);
495 v4l2_rect_set_max_size(compose, &max_r);
496 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
497 } else if (dev->has_compose_out) {
498 struct v4l2_rect min_r = {
499 0, 0,
500 crop->width / MAX_ZOOM,
501 factor * crop->height / MAX_ZOOM
503 struct v4l2_rect max_r = {
504 0, 0,
505 crop->width * MAX_ZOOM,
506 factor * crop->height * MAX_ZOOM
509 v4l2_rect_set_min_size(compose, &min_r);
510 v4l2_rect_set_max_size(compose, &max_r);
511 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
513 } else if (dev->has_compose_out && !dev->has_crop_out) {
514 v4l2_rect_set_size_to(crop, &r);
515 r.height *= factor;
516 v4l2_rect_set_size_to(compose, &r);
517 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
518 } else if (!dev->has_compose_out) {
519 v4l2_rect_map_inside(crop, &r);
520 r.height /= factor;
521 v4l2_rect_set_size_to(compose, &r);
522 } else {
523 r.height *= factor;
524 v4l2_rect_set_max_size(compose, &r);
525 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
526 crop->top *= factor;
527 crop->height *= factor;
528 v4l2_rect_set_size_to(crop, compose);
529 v4l2_rect_map_inside(crop, &r);
530 crop->top /= factor;
531 crop->height /= factor;
533 } else {
534 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
536 v4l2_rect_set_size_to(crop, &r);
537 r.height /= factor;
538 v4l2_rect_set_size_to(compose, &r);
541 dev->fmt_out_rect.width = mp->width;
542 dev->fmt_out_rect.height = mp->height;
543 for (p = 0; p < mp->num_planes; p++)
544 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
545 for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
546 dev->bytesperline_out[p] =
547 (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
548 dev->fmt_out->bit_depth[0];
549 dev->field_out = mp->field;
550 if (vivid_is_svid_out(dev))
551 dev->tv_field_out = mp->field;
553 set_colorspace:
554 dev->colorspace_out = mp->colorspace;
555 dev->xfer_func_out = mp->xfer_func;
556 dev->ycbcr_enc_out = mp->ycbcr_enc;
557 dev->quantization_out = mp->quantization;
558 if (dev->loop_video) {
559 vivid_send_source_change(dev, SVID);
560 vivid_send_source_change(dev, HDMI);
562 return 0;
565 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
566 struct v4l2_format *f)
568 struct vivid_dev *dev = video_drvdata(file);
570 if (!dev->multiplanar)
571 return -ENOTTY;
572 return vivid_g_fmt_vid_out(file, priv, f);
575 int vidioc_try_fmt_vid_out_mplane(struct file *file, void *priv,
576 struct v4l2_format *f)
578 struct vivid_dev *dev = video_drvdata(file);
580 if (!dev->multiplanar)
581 return -ENOTTY;
582 return vivid_try_fmt_vid_out(file, priv, f);
585 int vidioc_s_fmt_vid_out_mplane(struct file *file, void *priv,
586 struct v4l2_format *f)
588 struct vivid_dev *dev = video_drvdata(file);
590 if (!dev->multiplanar)
591 return -ENOTTY;
592 return vivid_s_fmt_vid_out(file, priv, f);
595 int vidioc_g_fmt_vid_out(struct file *file, void *priv,
596 struct v4l2_format *f)
598 struct vivid_dev *dev = video_drvdata(file);
600 if (dev->multiplanar)
601 return -ENOTTY;
602 return fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
605 int vidioc_try_fmt_vid_out(struct file *file, void *priv,
606 struct v4l2_format *f)
608 struct vivid_dev *dev = video_drvdata(file);
610 if (dev->multiplanar)
611 return -ENOTTY;
612 return fmt_sp2mp_func(file, priv, f, vivid_try_fmt_vid_out);
615 int vidioc_s_fmt_vid_out(struct file *file, void *priv,
616 struct v4l2_format *f)
618 struct vivid_dev *dev = video_drvdata(file);
620 if (dev->multiplanar)
621 return -ENOTTY;
622 return fmt_sp2mp_func(file, priv, f, vivid_s_fmt_vid_out);
625 int vivid_vid_out_g_selection(struct file *file, void *priv,
626 struct v4l2_selection *sel)
628 struct vivid_dev *dev = video_drvdata(file);
630 if (!dev->has_crop_out && !dev->has_compose_out)
631 return -ENOTTY;
632 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
633 return -EINVAL;
635 sel->r.left = sel->r.top = 0;
636 switch (sel->target) {
637 case V4L2_SEL_TGT_CROP:
638 if (!dev->has_crop_out)
639 return -EINVAL;
640 sel->r = dev->crop_out;
641 break;
642 case V4L2_SEL_TGT_CROP_DEFAULT:
643 if (!dev->has_crop_out)
644 return -EINVAL;
645 sel->r = dev->fmt_out_rect;
646 break;
647 case V4L2_SEL_TGT_CROP_BOUNDS:
648 if (!dev->has_crop_out)
649 return -EINVAL;
650 sel->r = vivid_max_rect;
651 break;
652 case V4L2_SEL_TGT_COMPOSE:
653 if (!dev->has_compose_out)
654 return -EINVAL;
655 sel->r = dev->compose_out;
656 break;
657 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
658 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
659 if (!dev->has_compose_out)
660 return -EINVAL;
661 sel->r = dev->sink_rect;
662 break;
663 default:
664 return -EINVAL;
666 return 0;
669 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
671 struct vivid_dev *dev = video_drvdata(file);
672 struct v4l2_rect *crop = &dev->crop_out;
673 struct v4l2_rect *compose = &dev->compose_out;
674 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
675 int ret;
677 if (!dev->has_crop_out && !dev->has_compose_out)
678 return -ENOTTY;
679 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
680 return -EINVAL;
682 switch (s->target) {
683 case V4L2_SEL_TGT_CROP:
684 if (!dev->has_crop_out)
685 return -EINVAL;
686 ret = vivid_vid_adjust_sel(s->flags, &s->r);
687 if (ret)
688 return ret;
689 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
690 v4l2_rect_set_max_size(&s->r, &dev->fmt_out_rect);
691 if (dev->has_scaler_out) {
692 struct v4l2_rect max_rect = {
693 0, 0,
694 dev->sink_rect.width * MAX_ZOOM,
695 (dev->sink_rect.height / factor) * MAX_ZOOM
698 v4l2_rect_set_max_size(&s->r, &max_rect);
699 if (dev->has_compose_out) {
700 struct v4l2_rect min_rect = {
701 0, 0,
702 s->r.width / MAX_ZOOM,
703 (s->r.height * factor) / MAX_ZOOM
705 struct v4l2_rect max_rect = {
706 0, 0,
707 s->r.width * MAX_ZOOM,
708 (s->r.height * factor) * MAX_ZOOM
711 v4l2_rect_set_min_size(compose, &min_rect);
712 v4l2_rect_set_max_size(compose, &max_rect);
713 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
715 } else if (dev->has_compose_out) {
716 s->r.top *= factor;
717 s->r.height *= factor;
718 v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
719 v4l2_rect_set_size_to(compose, &s->r);
720 v4l2_rect_map_inside(compose, &dev->compose_bounds_out);
721 s->r.top /= factor;
722 s->r.height /= factor;
723 } else {
724 v4l2_rect_set_size_to(&s->r, &dev->sink_rect);
725 s->r.height /= factor;
727 v4l2_rect_map_inside(&s->r, &dev->fmt_out_rect);
728 *crop = s->r;
729 break;
730 case V4L2_SEL_TGT_COMPOSE:
731 if (!dev->has_compose_out)
732 return -EINVAL;
733 ret = vivid_vid_adjust_sel(s->flags, &s->r);
734 if (ret)
735 return ret;
736 v4l2_rect_set_min_size(&s->r, &vivid_min_rect);
737 v4l2_rect_set_max_size(&s->r, &dev->sink_rect);
738 v4l2_rect_map_inside(&s->r, &dev->compose_bounds_out);
739 s->r.top /= factor;
740 s->r.height /= factor;
741 if (dev->has_scaler_out) {
742 struct v4l2_rect fmt = dev->fmt_out_rect;
743 struct v4l2_rect max_rect = {
744 0, 0,
745 s->r.width * MAX_ZOOM,
746 s->r.height * MAX_ZOOM
748 struct v4l2_rect min_rect = {
749 0, 0,
750 s->r.width / MAX_ZOOM,
751 s->r.height / MAX_ZOOM
754 v4l2_rect_set_min_size(&fmt, &min_rect);
755 if (!dev->has_crop_out)
756 v4l2_rect_set_max_size(&fmt, &max_rect);
757 if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
758 vb2_is_busy(&dev->vb_vid_out_q))
759 return -EBUSY;
760 if (dev->has_crop_out) {
761 v4l2_rect_set_min_size(crop, &min_rect);
762 v4l2_rect_set_max_size(crop, &max_rect);
764 dev->fmt_out_rect = fmt;
765 } else if (dev->has_crop_out) {
766 struct v4l2_rect fmt = dev->fmt_out_rect;
768 v4l2_rect_set_min_size(&fmt, &s->r);
769 if (!v4l2_rect_same_size(&dev->fmt_out_rect, &fmt) &&
770 vb2_is_busy(&dev->vb_vid_out_q))
771 return -EBUSY;
772 dev->fmt_out_rect = fmt;
773 v4l2_rect_set_size_to(crop, &s->r);
774 v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
775 } else {
776 if (!v4l2_rect_same_size(&s->r, &dev->fmt_out_rect) &&
777 vb2_is_busy(&dev->vb_vid_out_q))
778 return -EBUSY;
779 v4l2_rect_set_size_to(&dev->fmt_out_rect, &s->r);
780 v4l2_rect_set_size_to(crop, &s->r);
781 crop->height /= factor;
782 v4l2_rect_map_inside(crop, &dev->fmt_out_rect);
784 s->r.top *= factor;
785 s->r.height *= factor;
786 if (dev->bitmap_out && (compose->width != s->r.width ||
787 compose->height != s->r.height)) {
788 kfree(dev->bitmap_out);
789 dev->bitmap_out = NULL;
791 *compose = s->r;
792 break;
793 default:
794 return -EINVAL;
797 return 0;
800 int vivid_vid_out_cropcap(struct file *file, void *priv,
801 struct v4l2_cropcap *cap)
803 struct vivid_dev *dev = video_drvdata(file);
805 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
806 return -EINVAL;
808 switch (vivid_get_pixel_aspect(dev)) {
809 case TPG_PIXEL_ASPECT_NTSC:
810 cap->pixelaspect.numerator = 11;
811 cap->pixelaspect.denominator = 10;
812 break;
813 case TPG_PIXEL_ASPECT_PAL:
814 cap->pixelaspect.numerator = 54;
815 cap->pixelaspect.denominator = 59;
816 break;
817 case TPG_PIXEL_ASPECT_SQUARE:
818 cap->pixelaspect.numerator = 1;
819 cap->pixelaspect.denominator = 1;
820 break;
822 return 0;
825 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
826 struct v4l2_format *f)
828 struct vivid_dev *dev = video_drvdata(file);
829 const struct v4l2_rect *compose = &dev->compose_out;
830 struct v4l2_window *win = &f->fmt.win;
831 unsigned clipcount = win->clipcount;
833 if (!dev->has_fb)
834 return -EINVAL;
835 win->w.top = dev->overlay_out_top;
836 win->w.left = dev->overlay_out_left;
837 win->w.width = compose->width;
838 win->w.height = compose->height;
839 win->clipcount = dev->clipcount_out;
840 win->field = V4L2_FIELD_ANY;
841 win->chromakey = dev->chromakey_out;
842 win->global_alpha = dev->global_alpha_out;
843 if (clipcount > dev->clipcount_out)
844 clipcount = dev->clipcount_out;
845 if (dev->bitmap_out == NULL)
846 win->bitmap = NULL;
847 else if (win->bitmap) {
848 if (copy_to_user(win->bitmap, dev->bitmap_out,
849 ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
850 return -EFAULT;
852 if (clipcount && win->clips) {
853 if (copy_to_user(win->clips, dev->clips_out,
854 clipcount * sizeof(dev->clips_out[0])))
855 return -EFAULT;
857 return 0;
860 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
861 struct v4l2_format *f)
863 struct vivid_dev *dev = video_drvdata(file);
864 const struct v4l2_rect *compose = &dev->compose_out;
865 struct v4l2_window *win = &f->fmt.win;
866 int i, j;
868 if (!dev->has_fb)
869 return -EINVAL;
870 win->w.left = clamp_t(int, win->w.left,
871 -dev->display_width, dev->display_width);
872 win->w.top = clamp_t(int, win->w.top,
873 -dev->display_height, dev->display_height);
874 win->w.width = compose->width;
875 win->w.height = compose->height;
877 * It makes no sense for an OSD to overlay only top or bottom fields,
878 * so always set this to ANY.
880 win->field = V4L2_FIELD_ANY;
881 if (win->clipcount && !win->clips)
882 win->clipcount = 0;
883 if (win->clipcount > MAX_CLIPS)
884 win->clipcount = MAX_CLIPS;
885 if (win->clipcount) {
886 if (copy_from_user(dev->try_clips_out, win->clips,
887 win->clipcount * sizeof(dev->clips_out[0])))
888 return -EFAULT;
889 for (i = 0; i < win->clipcount; i++) {
890 struct v4l2_rect *r = &dev->try_clips_out[i].c;
892 r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
893 r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
894 r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
895 r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
898 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
899 * number and it's typically a one-time deal.
901 for (i = 0; i < win->clipcount - 1; i++) {
902 struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
904 for (j = i + 1; j < win->clipcount; j++) {
905 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
907 if (v4l2_rect_overlap(r1, r2))
908 return -EINVAL;
911 if (copy_to_user(win->clips, dev->try_clips_out,
912 win->clipcount * sizeof(dev->clips_out[0])))
913 return -EFAULT;
915 return 0;
918 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
919 struct v4l2_format *f)
921 struct vivid_dev *dev = video_drvdata(file);
922 const struct v4l2_rect *compose = &dev->compose_out;
923 struct v4l2_window *win = &f->fmt.win;
924 int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
925 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
926 unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
927 void *new_bitmap = NULL;
929 if (ret)
930 return ret;
932 if (win->bitmap) {
933 new_bitmap = memdup_user(win->bitmap, bitmap_size);
935 if (IS_ERR(new_bitmap))
936 return PTR_ERR(new_bitmap);
939 dev->overlay_out_top = win->w.top;
940 dev->overlay_out_left = win->w.left;
941 kfree(dev->bitmap_out);
942 dev->bitmap_out = new_bitmap;
943 dev->clipcount_out = win->clipcount;
944 if (dev->clipcount_out)
945 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
946 dev->chromakey_out = win->chromakey;
947 dev->global_alpha_out = win->global_alpha;
948 return ret;
951 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
953 struct vivid_dev *dev = video_drvdata(file);
955 if (i && !dev->fmt_out->can_do_overlay) {
956 dprintk(dev, 1, "unsupported output format for output overlay\n");
957 return -EINVAL;
960 dev->overlay_out_enabled = i;
961 return 0;
964 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
965 struct v4l2_framebuffer *a)
967 struct vivid_dev *dev = video_drvdata(file);
969 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
970 V4L2_FBUF_CAP_BITMAP_CLIPPING |
971 V4L2_FBUF_CAP_LIST_CLIPPING |
972 V4L2_FBUF_CAP_CHROMAKEY |
973 V4L2_FBUF_CAP_SRC_CHROMAKEY |
974 V4L2_FBUF_CAP_GLOBAL_ALPHA |
975 V4L2_FBUF_CAP_LOCAL_ALPHA |
976 V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
977 a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
978 a->base = (void *)dev->video_pbase;
979 a->fmt.width = dev->display_width;
980 a->fmt.height = dev->display_height;
981 if (dev->fb_defined.green.length == 5)
982 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
983 else
984 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
985 a->fmt.bytesperline = dev->display_byte_stride;
986 a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
987 a->fmt.field = V4L2_FIELD_NONE;
988 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
989 a->fmt.priv = 0;
990 return 0;
993 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
994 const struct v4l2_framebuffer *a)
996 struct vivid_dev *dev = video_drvdata(file);
997 const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
998 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
999 const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
1000 V4L2_FBUF_FLAG_LOCAL_ALPHA |
1001 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
1004 if ((a->flags & chroma_flags) == chroma_flags)
1005 return -EINVAL;
1006 switch (a->flags & alpha_flags) {
1007 case 0:
1008 case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1009 case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1010 case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1011 break;
1012 default:
1013 return -EINVAL;
1015 dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1016 dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
1017 return 0;
1020 static const struct v4l2_audioout vivid_audio_outputs[] = {
1021 { 0, "Line-Out 1" },
1022 { 1, "Line-Out 2" },
1025 int vidioc_enum_output(struct file *file, void *priv,
1026 struct v4l2_output *out)
1028 struct vivid_dev *dev = video_drvdata(file);
1030 if (out->index >= dev->num_outputs)
1031 return -EINVAL;
1033 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1034 switch (dev->output_type[out->index]) {
1035 case SVID:
1036 snprintf(out->name, sizeof(out->name), "S-Video %u",
1037 dev->output_name_counter[out->index]);
1038 out->std = V4L2_STD_ALL;
1039 if (dev->has_audio_outputs)
1040 out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1041 out->capabilities = V4L2_OUT_CAP_STD;
1042 break;
1043 case HDMI:
1044 snprintf(out->name, sizeof(out->name), "HDMI %u",
1045 dev->output_name_counter[out->index]);
1046 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1047 break;
1049 return 0;
1052 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1054 struct vivid_dev *dev = video_drvdata(file);
1056 *o = dev->output;
1057 return 0;
1060 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1062 struct vivid_dev *dev = video_drvdata(file);
1064 if (o >= dev->num_outputs)
1065 return -EINVAL;
1067 if (o == dev->output)
1068 return 0;
1070 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1071 return -EBUSY;
1073 dev->output = o;
1074 dev->tv_audio_output = 0;
1075 if (dev->output_type[o] == SVID)
1076 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1077 else
1078 dev->vid_out_dev.tvnorms = 0;
1080 dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1081 vivid_update_format_out(dev);
1082 return 0;
1085 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1087 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1088 return -EINVAL;
1089 *vout = vivid_audio_outputs[vout->index];
1090 return 0;
1093 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1095 struct vivid_dev *dev = video_drvdata(file);
1097 if (!vivid_is_svid_out(dev))
1098 return -EINVAL;
1099 *vout = vivid_audio_outputs[dev->tv_audio_output];
1100 return 0;
1103 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1105 struct vivid_dev *dev = video_drvdata(file);
1107 if (!vivid_is_svid_out(dev))
1108 return -EINVAL;
1109 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1110 return -EINVAL;
1111 dev->tv_audio_output = vout->index;
1112 return 0;
1115 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1117 struct vivid_dev *dev = video_drvdata(file);
1119 if (!vivid_is_svid_out(dev))
1120 return -ENODATA;
1121 if (dev->std_out == id)
1122 return 0;
1123 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1124 return -EBUSY;
1125 dev->std_out = id;
1126 vivid_update_format_out(dev);
1127 return 0;
1130 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1132 struct v4l2_bt_timings *bt = &timings->bt;
1134 if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1135 v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1136 return true;
1138 return false;
1141 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1142 struct v4l2_dv_timings *timings)
1144 struct vivid_dev *dev = video_drvdata(file);
1145 if (!vivid_is_hdmi_out(dev))
1146 return -ENODATA;
1147 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1148 0, NULL, NULL) &&
1149 !valid_cvt_gtf_timings(timings))
1150 return -EINVAL;
1151 if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0, true))
1152 return 0;
1153 if (vb2_is_busy(&dev->vb_vid_out_q))
1154 return -EBUSY;
1155 dev->dv_timings_out = *timings;
1156 vivid_update_format_out(dev);
1157 return 0;
1160 int vivid_vid_out_g_parm(struct file *file, void *priv,
1161 struct v4l2_streamparm *parm)
1163 struct vivid_dev *dev = video_drvdata(file);
1165 if (parm->type != (dev->multiplanar ?
1166 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1167 V4L2_BUF_TYPE_VIDEO_OUTPUT))
1168 return -EINVAL;
1170 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1171 parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1172 parm->parm.output.writebuffers = 1;
1174 return 0;
1177 int vidioc_subscribe_event(struct v4l2_fh *fh,
1178 const struct v4l2_event_subscription *sub)
1180 switch (sub->type) {
1181 case V4L2_EVENT_SOURCE_CHANGE:
1182 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1183 return v4l2_src_change_event_subscribe(fh, sub);
1184 break;
1185 default:
1186 return v4l2_ctrl_subscribe_event(fh, sub);
1188 return -EINVAL;