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

source code of linux/drivers/media/test-drivers/vivid/vivid-vid-out.c