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