]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/media/platform/vivid/vivid-vid-out.c
Merge branch 'next' of git://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux...
[mirror_ubuntu-zesty-kernel.git] / drivers / media / platform / vivid / vivid-vid-out.c
1 /*
2 * vivid-vid-out.c - video output support functions.
3 *
4 * Copyright 2014 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
5 *
6 * This program is free software; you may redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
11 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
12 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
13 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
14 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
15 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
16 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
17 * SOFTWARE.
18 */
19
20 #include <linux/errno.h>
21 #include <linux/kernel.h>
22 #include <linux/sched.h>
23 #include <linux/videodev2.h>
24 #include <linux/v4l2-dv-timings.h>
25 #include <media/v4l2-common.h>
26 #include <media/v4l2-event.h>
27 #include <media/v4l2-dv-timings.h>
28
29 #include "vivid-core.h"
30 #include "vivid-vid-common.h"
31 #include "vivid-kthread-out.h"
32 #include "vivid-vid-out.h"
33
34 static int vid_out_queue_setup(struct vb2_queue *vq, const struct v4l2_format *fmt,
35 unsigned *nbuffers, unsigned *nplanes,
36 unsigned sizes[], void *alloc_ctxs[])
37 {
38 struct vivid_dev *dev = vb2_get_drv_priv(vq);
39 const struct vivid_fmt *vfmt = dev->fmt_out;
40 unsigned planes = vfmt->buffers;
41 unsigned h = dev->fmt_out_rect.height;
42 unsigned size = dev->bytesperline_out[0] * h;
43 unsigned p;
44
45 for (p = vfmt->buffers; p < vfmt->planes; p++)
46 size += dev->bytesperline_out[p] * h / vfmt->vdownsampling[p];
47
48 if (dev->field_out == V4L2_FIELD_ALTERNATE) {
49 /*
50 * You cannot use write() with FIELD_ALTERNATE since the field
51 * information (TOP/BOTTOM) cannot be passed to the kernel.
52 */
53 if (vb2_fileio_is_active(vq))
54 return -EINVAL;
55 }
56
57 if (dev->queue_setup_error) {
58 /*
59 * Error injection: test what happens if queue_setup() returns
60 * an error.
61 */
62 dev->queue_setup_error = false;
63 return -EINVAL;
64 }
65
66 if (fmt) {
67 const struct v4l2_pix_format_mplane *mp;
68 struct v4l2_format mp_fmt;
69
70 if (!V4L2_TYPE_IS_MULTIPLANAR(fmt->type)) {
71 fmt_sp2mp(fmt, &mp_fmt);
72 fmt = &mp_fmt;
73 }
74 mp = &fmt->fmt.pix_mp;
75 /*
76 * Check if the number of planes in the specified format match
77 * the number of planes in the current format. You can't mix that.
78 */
79 if (mp->num_planes != planes)
80 return -EINVAL;
81 sizes[0] = mp->plane_fmt[0].sizeimage;
82 if (sizes[0] < size)
83 return -EINVAL;
84 for (p = 1; p < planes; p++) {
85 sizes[p] = mp->plane_fmt[p].sizeimage;
86 if (sizes[p] < dev->bytesperline_out[p] * h)
87 return -EINVAL;
88 }
89 } else {
90 for (p = 0; p < planes; p++)
91 sizes[p] = p ? dev->bytesperline_out[p] * h : size;
92 }
93
94 if (vq->num_buffers + *nbuffers < 2)
95 *nbuffers = 2 - vq->num_buffers;
96
97 *nplanes = planes;
98
99 /*
100 * videobuf2-vmalloc allocator is context-less so no need to set
101 * alloc_ctxs array.
102 */
103
104 dprintk(dev, 1, "%s: count=%d\n", __func__, *nbuffers);
105 for (p = 0; p < planes; p++)
106 dprintk(dev, 1, "%s: size[%u]=%u\n", __func__, p, sizes[p]);
107 return 0;
108 }
109
110 static int vid_out_buf_prepare(struct vb2_buffer *vb)
111 {
112 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
113 unsigned long size;
114 unsigned planes;
115 unsigned p;
116
117 dprintk(dev, 1, "%s\n", __func__);
118
119 if (WARN_ON(NULL == dev->fmt_out))
120 return -EINVAL;
121
122 planes = dev->fmt_out->planes;
123
124 if (dev->buf_prepare_error) {
125 /*
126 * Error injection: test what happens if buf_prepare() returns
127 * an error.
128 */
129 dev->buf_prepare_error = false;
130 return -EINVAL;
131 }
132
133 if (dev->field_out != V4L2_FIELD_ALTERNATE)
134 vb->v4l2_buf.field = dev->field_out;
135 else if (vb->v4l2_buf.field != V4L2_FIELD_TOP &&
136 vb->v4l2_buf.field != V4L2_FIELD_BOTTOM)
137 return -EINVAL;
138
139 for (p = 0; p < planes; p++) {
140 size = dev->bytesperline_out[p] * dev->fmt_out_rect.height +
141 vb->v4l2_planes[p].data_offset;
142
143 if (vb2_get_plane_payload(vb, p) < size) {
144 dprintk(dev, 1, "%s the payload is too small for plane %u (%lu < %lu)\n",
145 __func__, p, vb2_get_plane_payload(vb, p), size);
146 return -EINVAL;
147 }
148 }
149
150 return 0;
151 }
152
153 static void vid_out_buf_queue(struct vb2_buffer *vb)
154 {
155 struct vivid_dev *dev = vb2_get_drv_priv(vb->vb2_queue);
156 struct vivid_buffer *buf = container_of(vb, struct vivid_buffer, vb);
157
158 dprintk(dev, 1, "%s\n", __func__);
159
160 spin_lock(&dev->slock);
161 list_add_tail(&buf->list, &dev->vid_out_active);
162 spin_unlock(&dev->slock);
163 }
164
165 static int vid_out_start_streaming(struct vb2_queue *vq, unsigned count)
166 {
167 struct vivid_dev *dev = vb2_get_drv_priv(vq);
168 int err;
169
170 if (vb2_is_streaming(&dev->vb_vid_cap_q))
171 dev->can_loop_video = vivid_vid_can_loop(dev);
172
173 if (dev->kthread_vid_out)
174 return 0;
175
176 dev->vid_out_seq_count = 0;
177 dprintk(dev, 1, "%s\n", __func__);
178 if (dev->start_streaming_error) {
179 dev->start_streaming_error = false;
180 err = -EINVAL;
181 } else {
182 err = vivid_start_generating_vid_out(dev, &dev->vid_out_streaming);
183 }
184 if (err) {
185 struct vivid_buffer *buf, *tmp;
186
187 list_for_each_entry_safe(buf, tmp, &dev->vid_out_active, list) {
188 list_del(&buf->list);
189 vb2_buffer_done(&buf->vb, VB2_BUF_STATE_QUEUED);
190 }
191 }
192 return err;
193 }
194
195 /* abort streaming and wait for last buffer */
196 static void vid_out_stop_streaming(struct vb2_queue *vq)
197 {
198 struct vivid_dev *dev = vb2_get_drv_priv(vq);
199
200 dprintk(dev, 1, "%s\n", __func__);
201 vivid_stop_generating_vid_out(dev, &dev->vid_out_streaming);
202 dev->can_loop_video = false;
203 }
204
205 const struct vb2_ops vivid_vid_out_qops = {
206 .queue_setup = vid_out_queue_setup,
207 .buf_prepare = vid_out_buf_prepare,
208 .buf_queue = vid_out_buf_queue,
209 .start_streaming = vid_out_start_streaming,
210 .stop_streaming = vid_out_stop_streaming,
211 .wait_prepare = vb2_ops_wait_prepare,
212 .wait_finish = vb2_ops_wait_finish,
213 };
214
215 /*
216 * Called whenever the format has to be reset which can occur when
217 * changing outputs, standard, timings, etc.
218 */
219 void vivid_update_format_out(struct vivid_dev *dev)
220 {
221 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
222 unsigned size, p;
223
224 switch (dev->output_type[dev->output]) {
225 case SVID:
226 default:
227 dev->field_out = dev->tv_field_out;
228 dev->sink_rect.width = 720;
229 if (dev->std_out & V4L2_STD_525_60) {
230 dev->sink_rect.height = 480;
231 dev->timeperframe_vid_out = (struct v4l2_fract) { 1001, 30000 };
232 dev->service_set_out = V4L2_SLICED_CAPTION_525;
233 } else {
234 dev->sink_rect.height = 576;
235 dev->timeperframe_vid_out = (struct v4l2_fract) { 1000, 25000 };
236 dev->service_set_out = V4L2_SLICED_WSS_625 | V4L2_SLICED_TELETEXT_B;
237 }
238 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
239 break;
240 case HDMI:
241 dev->sink_rect.width = bt->width;
242 dev->sink_rect.height = bt->height;
243 size = V4L2_DV_BT_FRAME_WIDTH(bt) * V4L2_DV_BT_FRAME_HEIGHT(bt);
244 dev->timeperframe_vid_out = (struct v4l2_fract) {
245 size / 100, (u32)bt->pixelclock / 100
246 };
247 if (bt->interlaced)
248 dev->field_out = V4L2_FIELD_ALTERNATE;
249 else
250 dev->field_out = V4L2_FIELD_NONE;
251 if (!dev->dvi_d_out && (bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
252 if (bt->width == 720 && bt->height <= 576)
253 dev->colorspace_out = V4L2_COLORSPACE_SMPTE170M;
254 else
255 dev->colorspace_out = V4L2_COLORSPACE_REC709;
256 } else {
257 dev->colorspace_out = V4L2_COLORSPACE_SRGB;
258 }
259 break;
260 }
261 dev->xfer_func_out = V4L2_XFER_FUNC_DEFAULT;
262 dev->ycbcr_enc_out = V4L2_YCBCR_ENC_DEFAULT;
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 */
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
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
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 }
333 for (p = fmt->buffers; p < fmt->planes; p++) {
334 unsigned stride = dev->bytesperline_out[p];
335
336 mp->plane_fmt[0].sizeimage +=
337 (stride * mp->height) / fmt->vdownsampling[p];
338 }
339 return 0;
340 }
341
342 int vivid_try_fmt_vid_out(struct file *file, void *priv,
343 struct v4l2_format *f)
344 {
345 struct vivid_dev *dev = video_drvdata(file);
346 struct v4l2_bt_timings *bt = &dev->dv_timings_out.bt;
347 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
348 struct v4l2_plane_pix_format *pfmt = mp->plane_fmt;
349 const struct vivid_fmt *fmt;
350 unsigned bytesperline, max_bpl;
351 unsigned factor = 1;
352 unsigned w, h;
353 unsigned p;
354
355 fmt = vivid_get_format(dev, mp->pixelformat);
356 if (!fmt) {
357 dprintk(dev, 1, "Fourcc format (0x%08x) unknown.\n",
358 mp->pixelformat);
359 mp->pixelformat = V4L2_PIX_FMT_YUYV;
360 fmt = vivid_get_format(dev, mp->pixelformat);
361 }
362
363 mp->field = vivid_field_out(dev, mp->field);
364 if (vivid_is_svid_out(dev)) {
365 w = 720;
366 h = (dev->std_out & V4L2_STD_525_60) ? 480 : 576;
367 } else {
368 w = dev->sink_rect.width;
369 h = dev->sink_rect.height;
370 }
371 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
372 factor = 2;
373 if (!dev->has_scaler_out && !dev->has_crop_out && !dev->has_compose_out) {
374 mp->width = w;
375 mp->height = h / factor;
376 } else {
377 struct v4l2_rect r = { 0, 0, mp->width, mp->height * factor };
378
379 rect_set_min_size(&r, &vivid_min_rect);
380 rect_set_max_size(&r, &vivid_max_rect);
381 if (dev->has_scaler_out && !dev->has_crop_out) {
382 struct v4l2_rect max_r = { 0, 0, MAX_ZOOM * w, MAX_ZOOM * h };
383
384 rect_set_max_size(&r, &max_r);
385 } else if (!dev->has_scaler_out && dev->has_compose_out && !dev->has_crop_out) {
386 rect_set_max_size(&r, &dev->sink_rect);
387 } else if (!dev->has_scaler_out && !dev->has_compose_out) {
388 rect_set_min_size(&r, &dev->sink_rect);
389 }
390 mp->width = r.width;
391 mp->height = r.height / factor;
392 }
393
394 /* This driver supports custom bytesperline values */
395
396 /* Calculate the minimum supported bytesperline value */
397 bytesperline = (mp->width * fmt->bit_depth[0]) >> 3;
398 /* Calculate the maximum supported bytesperline value */
399 max_bpl = (MAX_ZOOM * MAX_WIDTH * fmt->bit_depth[0]) >> 3;
400 mp->num_planes = fmt->buffers;
401 for (p = 0; p < mp->num_planes; p++) {
402 if (pfmt[p].bytesperline > max_bpl)
403 pfmt[p].bytesperline = max_bpl;
404 if (pfmt[p].bytesperline < bytesperline)
405 pfmt[p].bytesperline = bytesperline;
406 pfmt[p].sizeimage = pfmt[p].bytesperline * mp->height;
407 memset(pfmt[p].reserved, 0, sizeof(pfmt[p].reserved));
408 }
409 for (p = fmt->buffers; p < fmt->planes; p++)
410 pfmt[0].sizeimage += (pfmt[0].bytesperline * fmt->bit_depth[p]) /
411 (fmt->bit_depth[0] * fmt->vdownsampling[p]);
412 mp->xfer_func = V4L2_XFER_FUNC_DEFAULT;
413 mp->ycbcr_enc = V4L2_YCBCR_ENC_DEFAULT;
414 mp->quantization = V4L2_QUANTIZATION_DEFAULT;
415 if (vivid_is_svid_out(dev)) {
416 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
417 } else if (dev->dvi_d_out || !(bt->flags & V4L2_DV_FL_IS_CE_VIDEO)) {
418 mp->colorspace = V4L2_COLORSPACE_SRGB;
419 if (dev->dvi_d_out)
420 mp->quantization = V4L2_QUANTIZATION_LIM_RANGE;
421 } else if (bt->width == 720 && bt->height <= 576) {
422 mp->colorspace = V4L2_COLORSPACE_SMPTE170M;
423 } else if (mp->colorspace != V4L2_COLORSPACE_SMPTE170M &&
424 mp->colorspace != V4L2_COLORSPACE_REC709 &&
425 mp->colorspace != V4L2_COLORSPACE_ADOBERGB &&
426 mp->colorspace != V4L2_COLORSPACE_BT2020 &&
427 mp->colorspace != V4L2_COLORSPACE_SRGB) {
428 mp->colorspace = V4L2_COLORSPACE_REC709;
429 }
430 memset(mp->reserved, 0, sizeof(mp->reserved));
431 return 0;
432 }
433
434 int vivid_s_fmt_vid_out(struct file *file, void *priv,
435 struct v4l2_format *f)
436 {
437 struct v4l2_pix_format_mplane *mp = &f->fmt.pix_mp;
438 struct vivid_dev *dev = video_drvdata(file);
439 struct v4l2_rect *crop = &dev->crop_out;
440 struct v4l2_rect *compose = &dev->compose_out;
441 struct vb2_queue *q = &dev->vb_vid_out_q;
442 int ret = vivid_try_fmt_vid_out(file, priv, f);
443 unsigned factor = 1;
444 unsigned p;
445
446 if (ret < 0)
447 return ret;
448
449 if (vb2_is_busy(q) &&
450 (vivid_is_svid_out(dev) ||
451 mp->width != dev->fmt_out_rect.width ||
452 mp->height != dev->fmt_out_rect.height ||
453 mp->pixelformat != dev->fmt_out->fourcc ||
454 mp->field != dev->field_out)) {
455 dprintk(dev, 1, "%s device busy\n", __func__);
456 return -EBUSY;
457 }
458
459 /*
460 * Allow for changing the colorspace on the fly. Useful for testing
461 * purposes, and it is something that HDMI transmitters are able
462 * to do.
463 */
464 if (vb2_is_busy(q))
465 goto set_colorspace;
466
467 dev->fmt_out = vivid_get_format(dev, mp->pixelformat);
468 if (V4L2_FIELD_HAS_T_OR_B(mp->field))
469 factor = 2;
470
471 if (dev->has_scaler_out || dev->has_crop_out || dev->has_compose_out) {
472 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
473
474 if (dev->has_scaler_out) {
475 if (dev->has_crop_out)
476 rect_map_inside(crop, &r);
477 else
478 *crop = r;
479 if (dev->has_compose_out && !dev->has_crop_out) {
480 struct v4l2_rect min_r = {
481 0, 0,
482 r.width / MAX_ZOOM,
483 factor * r.height / MAX_ZOOM
484 };
485 struct v4l2_rect max_r = {
486 0, 0,
487 r.width * MAX_ZOOM,
488 factor * r.height * MAX_ZOOM
489 };
490
491 rect_set_min_size(compose, &min_r);
492 rect_set_max_size(compose, &max_r);
493 rect_map_inside(compose, &dev->compose_bounds_out);
494 } else if (dev->has_compose_out) {
495 struct v4l2_rect min_r = {
496 0, 0,
497 crop->width / MAX_ZOOM,
498 factor * crop->height / MAX_ZOOM
499 };
500 struct v4l2_rect max_r = {
501 0, 0,
502 crop->width * MAX_ZOOM,
503 factor * crop->height * MAX_ZOOM
504 };
505
506 rect_set_min_size(compose, &min_r);
507 rect_set_max_size(compose, &max_r);
508 rect_map_inside(compose, &dev->compose_bounds_out);
509 }
510 } else if (dev->has_compose_out && !dev->has_crop_out) {
511 rect_set_size_to(crop, &r);
512 r.height *= factor;
513 rect_set_size_to(compose, &r);
514 rect_map_inside(compose, &dev->compose_bounds_out);
515 } else if (!dev->has_compose_out) {
516 rect_map_inside(crop, &r);
517 r.height /= factor;
518 rect_set_size_to(compose, &r);
519 } else {
520 r.height *= factor;
521 rect_set_max_size(compose, &r);
522 rect_map_inside(compose, &dev->compose_bounds_out);
523 crop->top *= factor;
524 crop->height *= factor;
525 rect_set_size_to(crop, compose);
526 rect_map_inside(crop, &r);
527 crop->top /= factor;
528 crop->height /= factor;
529 }
530 } else {
531 struct v4l2_rect r = { 0, 0, mp->width, mp->height };
532
533 rect_set_size_to(crop, &r);
534 r.height /= factor;
535 rect_set_size_to(compose, &r);
536 }
537
538 dev->fmt_out_rect.width = mp->width;
539 dev->fmt_out_rect.height = mp->height;
540 for (p = 0; p < mp->num_planes; p++)
541 dev->bytesperline_out[p] = mp->plane_fmt[p].bytesperline;
542 for (p = dev->fmt_out->buffers; p < dev->fmt_out->planes; p++)
543 dev->bytesperline_out[p] =
544 (dev->bytesperline_out[0] * dev->fmt_out->bit_depth[p]) /
545 dev->fmt_out->bit_depth[0];
546 dev->field_out = mp->field;
547 if (vivid_is_svid_out(dev))
548 dev->tv_field_out = mp->field;
549
550 set_colorspace:
551 dev->colorspace_out = mp->colorspace;
552 dev->xfer_func_out = mp->xfer_func;
553 dev->ycbcr_enc_out = mp->ycbcr_enc;
554 dev->quantization_out = mp->quantization;
555 if (dev->loop_video) {
556 vivid_send_source_change(dev, SVID);
557 vivid_send_source_change(dev, HDMI);
558 }
559 return 0;
560 }
561
562 int vidioc_g_fmt_vid_out_mplane(struct file *file, void *priv,
563 struct v4l2_format *f)
564 {
565 struct vivid_dev *dev = video_drvdata(file);
566
567 if (!dev->multiplanar)
568 return -ENOTTY;
569 return vivid_g_fmt_vid_out(file, priv, f);
570 }
571
572 int vidioc_try_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_try_fmt_vid_out(file, priv, f);
580 }
581
582 int vidioc_s_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_s_fmt_vid_out(file, priv, f);
590 }
591
592 int vidioc_g_fmt_vid_out(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 fmt_sp2mp_func(file, priv, f, vivid_g_fmt_vid_out);
600 }
601
602 int vidioc_try_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, vivid_try_fmt_vid_out);
610 }
611
612 int vidioc_s_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, vivid_s_fmt_vid_out);
620 }
621
622 int vivid_vid_out_g_selection(struct file *file, void *priv,
623 struct v4l2_selection *sel)
624 {
625 struct vivid_dev *dev = video_drvdata(file);
626
627 if (!dev->has_crop_out && !dev->has_compose_out)
628 return -ENOTTY;
629 if (sel->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
630 return -EINVAL;
631
632 sel->r.left = sel->r.top = 0;
633 switch (sel->target) {
634 case V4L2_SEL_TGT_CROP:
635 if (!dev->has_crop_out)
636 return -EINVAL;
637 sel->r = dev->crop_out;
638 break;
639 case V4L2_SEL_TGT_CROP_DEFAULT:
640 if (!dev->has_crop_out)
641 return -EINVAL;
642 sel->r = dev->fmt_out_rect;
643 break;
644 case V4L2_SEL_TGT_CROP_BOUNDS:
645 if (!dev->has_crop_out)
646 return -EINVAL;
647 sel->r = vivid_max_rect;
648 break;
649 case V4L2_SEL_TGT_COMPOSE:
650 if (!dev->has_compose_out)
651 return -EINVAL;
652 sel->r = dev->compose_out;
653 break;
654 case V4L2_SEL_TGT_COMPOSE_DEFAULT:
655 case V4L2_SEL_TGT_COMPOSE_BOUNDS:
656 if (!dev->has_compose_out)
657 return -EINVAL;
658 sel->r = dev->sink_rect;
659 break;
660 default:
661 return -EINVAL;
662 }
663 return 0;
664 }
665
666 int vivid_vid_out_s_selection(struct file *file, void *fh, struct v4l2_selection *s)
667 {
668 struct vivid_dev *dev = video_drvdata(file);
669 struct v4l2_rect *crop = &dev->crop_out;
670 struct v4l2_rect *compose = &dev->compose_out;
671 unsigned factor = V4L2_FIELD_HAS_T_OR_B(dev->field_out) ? 2 : 1;
672 int ret;
673
674 if (!dev->has_crop_out && !dev->has_compose_out)
675 return -ENOTTY;
676 if (s->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
677 return -EINVAL;
678
679 switch (s->target) {
680 case V4L2_SEL_TGT_CROP:
681 if (!dev->has_crop_out)
682 return -EINVAL;
683 ret = vivid_vid_adjust_sel(s->flags, &s->r);
684 if (ret)
685 return ret;
686 rect_set_min_size(&s->r, &vivid_min_rect);
687 rect_set_max_size(&s->r, &dev->fmt_out_rect);
688 if (dev->has_scaler_out) {
689 struct v4l2_rect max_rect = {
690 0, 0,
691 dev->sink_rect.width * MAX_ZOOM,
692 (dev->sink_rect.height / factor) * MAX_ZOOM
693 };
694
695 rect_set_max_size(&s->r, &max_rect);
696 if (dev->has_compose_out) {
697 struct v4l2_rect min_rect = {
698 0, 0,
699 s->r.width / MAX_ZOOM,
700 (s->r.height * factor) / MAX_ZOOM
701 };
702 struct v4l2_rect max_rect = {
703 0, 0,
704 s->r.width * MAX_ZOOM,
705 (s->r.height * factor) * MAX_ZOOM
706 };
707
708 rect_set_min_size(compose, &min_rect);
709 rect_set_max_size(compose, &max_rect);
710 rect_map_inside(compose, &dev->compose_bounds_out);
711 }
712 } else if (dev->has_compose_out) {
713 s->r.top *= factor;
714 s->r.height *= factor;
715 rect_set_max_size(&s->r, &dev->sink_rect);
716 rect_set_size_to(compose, &s->r);
717 rect_map_inside(compose, &dev->compose_bounds_out);
718 s->r.top /= factor;
719 s->r.height /= factor;
720 } else {
721 rect_set_size_to(&s->r, &dev->sink_rect);
722 s->r.height /= factor;
723 }
724 rect_map_inside(&s->r, &dev->fmt_out_rect);
725 *crop = s->r;
726 break;
727 case V4L2_SEL_TGT_COMPOSE:
728 if (!dev->has_compose_out)
729 return -EINVAL;
730 ret = vivid_vid_adjust_sel(s->flags, &s->r);
731 if (ret)
732 return ret;
733 rect_set_min_size(&s->r, &vivid_min_rect);
734 rect_set_max_size(&s->r, &dev->sink_rect);
735 rect_map_inside(&s->r, &dev->compose_bounds_out);
736 s->r.top /= factor;
737 s->r.height /= factor;
738 if (dev->has_scaler_out) {
739 struct v4l2_rect fmt = dev->fmt_out_rect;
740 struct v4l2_rect max_rect = {
741 0, 0,
742 s->r.width * MAX_ZOOM,
743 s->r.height * MAX_ZOOM
744 };
745 struct v4l2_rect min_rect = {
746 0, 0,
747 s->r.width / MAX_ZOOM,
748 s->r.height / MAX_ZOOM
749 };
750
751 rect_set_min_size(&fmt, &min_rect);
752 if (!dev->has_crop_out)
753 rect_set_max_size(&fmt, &max_rect);
754 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
755 vb2_is_busy(&dev->vb_vid_out_q))
756 return -EBUSY;
757 if (dev->has_crop_out) {
758 rect_set_min_size(crop, &min_rect);
759 rect_set_max_size(crop, &max_rect);
760 }
761 dev->fmt_out_rect = fmt;
762 } else if (dev->has_crop_out) {
763 struct v4l2_rect fmt = dev->fmt_out_rect;
764
765 rect_set_min_size(&fmt, &s->r);
766 if (!rect_same_size(&dev->fmt_out_rect, &fmt) &&
767 vb2_is_busy(&dev->vb_vid_out_q))
768 return -EBUSY;
769 dev->fmt_out_rect = fmt;
770 rect_set_size_to(crop, &s->r);
771 rect_map_inside(crop, &dev->fmt_out_rect);
772 } else {
773 if (!rect_same_size(&s->r, &dev->fmt_out_rect) &&
774 vb2_is_busy(&dev->vb_vid_out_q))
775 return -EBUSY;
776 rect_set_size_to(&dev->fmt_out_rect, &s->r);
777 rect_set_size_to(crop, &s->r);
778 crop->height /= factor;
779 rect_map_inside(crop, &dev->fmt_out_rect);
780 }
781 s->r.top *= factor;
782 s->r.height *= factor;
783 if (dev->bitmap_out && (compose->width != s->r.width ||
784 compose->height != s->r.height)) {
785 kfree(dev->bitmap_out);
786 dev->bitmap_out = NULL;
787 }
788 *compose = s->r;
789 break;
790 default:
791 return -EINVAL;
792 }
793
794 return 0;
795 }
796
797 int vivid_vid_out_cropcap(struct file *file, void *priv,
798 struct v4l2_cropcap *cap)
799 {
800 struct vivid_dev *dev = video_drvdata(file);
801
802 if (cap->type != V4L2_BUF_TYPE_VIDEO_OUTPUT)
803 return -EINVAL;
804
805 switch (vivid_get_pixel_aspect(dev)) {
806 case TPG_PIXEL_ASPECT_NTSC:
807 cap->pixelaspect.numerator = 11;
808 cap->pixelaspect.denominator = 10;
809 break;
810 case TPG_PIXEL_ASPECT_PAL:
811 cap->pixelaspect.numerator = 54;
812 cap->pixelaspect.denominator = 59;
813 break;
814 case TPG_PIXEL_ASPECT_SQUARE:
815 cap->pixelaspect.numerator = 1;
816 cap->pixelaspect.denominator = 1;
817 break;
818 }
819 return 0;
820 }
821
822 int vidioc_g_fmt_vid_out_overlay(struct file *file, void *priv,
823 struct v4l2_format *f)
824 {
825 struct vivid_dev *dev = video_drvdata(file);
826 const struct v4l2_rect *compose = &dev->compose_out;
827 struct v4l2_window *win = &f->fmt.win;
828 unsigned clipcount = win->clipcount;
829
830 if (!dev->has_fb)
831 return -EINVAL;
832 win->w.top = dev->overlay_out_top;
833 win->w.left = dev->overlay_out_left;
834 win->w.width = compose->width;
835 win->w.height = compose->height;
836 win->clipcount = dev->clipcount_out;
837 win->field = V4L2_FIELD_ANY;
838 win->chromakey = dev->chromakey_out;
839 win->global_alpha = dev->global_alpha_out;
840 if (clipcount > dev->clipcount_out)
841 clipcount = dev->clipcount_out;
842 if (dev->bitmap_out == NULL)
843 win->bitmap = NULL;
844 else if (win->bitmap) {
845 if (copy_to_user(win->bitmap, dev->bitmap_out,
846 ((dev->compose_out.width + 7) / 8) * dev->compose_out.height))
847 return -EFAULT;
848 }
849 if (clipcount && win->clips) {
850 if (copy_to_user(win->clips, dev->clips_out,
851 clipcount * sizeof(dev->clips_out[0])))
852 return -EFAULT;
853 }
854 return 0;
855 }
856
857 int vidioc_try_fmt_vid_out_overlay(struct file *file, void *priv,
858 struct v4l2_format *f)
859 {
860 struct vivid_dev *dev = video_drvdata(file);
861 const struct v4l2_rect *compose = &dev->compose_out;
862 struct v4l2_window *win = &f->fmt.win;
863 int i, j;
864
865 if (!dev->has_fb)
866 return -EINVAL;
867 win->w.left = clamp_t(int, win->w.left,
868 -dev->display_width, dev->display_width);
869 win->w.top = clamp_t(int, win->w.top,
870 -dev->display_height, dev->display_height);
871 win->w.width = compose->width;
872 win->w.height = compose->height;
873 /*
874 * It makes no sense for an OSD to overlay only top or bottom fields,
875 * so always set this to ANY.
876 */
877 win->field = V4L2_FIELD_ANY;
878 if (win->clipcount && !win->clips)
879 win->clipcount = 0;
880 if (win->clipcount > MAX_CLIPS)
881 win->clipcount = MAX_CLIPS;
882 if (win->clipcount) {
883 if (copy_from_user(dev->try_clips_out, win->clips,
884 win->clipcount * sizeof(dev->clips_out[0])))
885 return -EFAULT;
886 for (i = 0; i < win->clipcount; i++) {
887 struct v4l2_rect *r = &dev->try_clips_out[i].c;
888
889 r->top = clamp_t(s32, r->top, 0, dev->display_height - 1);
890 r->height = clamp_t(s32, r->height, 1, dev->display_height - r->top);
891 r->left = clamp_t(u32, r->left, 0, dev->display_width - 1);
892 r->width = clamp_t(u32, r->width, 1, dev->display_width - r->left);
893 }
894 /*
895 * Yeah, so sue me, it's an O(n^2) algorithm. But n is a small
896 * number and it's typically a one-time deal.
897 */
898 for (i = 0; i < win->clipcount - 1; i++) {
899 struct v4l2_rect *r1 = &dev->try_clips_out[i].c;
900
901 for (j = i + 1; j < win->clipcount; j++) {
902 struct v4l2_rect *r2 = &dev->try_clips_out[j].c;
903
904 if (rect_overlap(r1, r2))
905 return -EINVAL;
906 }
907 }
908 if (copy_to_user(win->clips, dev->try_clips_out,
909 win->clipcount * sizeof(dev->clips_out[0])))
910 return -EFAULT;
911 }
912 return 0;
913 }
914
915 int vidioc_s_fmt_vid_out_overlay(struct file *file, void *priv,
916 struct v4l2_format *f)
917 {
918 struct vivid_dev *dev = video_drvdata(file);
919 const struct v4l2_rect *compose = &dev->compose_out;
920 struct v4l2_window *win = &f->fmt.win;
921 int ret = vidioc_try_fmt_vid_out_overlay(file, priv, f);
922 unsigned bitmap_size = ((compose->width + 7) / 8) * compose->height;
923 unsigned clips_size = win->clipcount * sizeof(dev->clips_out[0]);
924 void *new_bitmap = NULL;
925
926 if (ret)
927 return ret;
928
929 if (win->bitmap) {
930 new_bitmap = memdup_user(win->bitmap, bitmap_size);
931
932 if (IS_ERR(new_bitmap))
933 return PTR_ERR(new_bitmap);
934 }
935
936 dev->overlay_out_top = win->w.top;
937 dev->overlay_out_left = win->w.left;
938 kfree(dev->bitmap_out);
939 dev->bitmap_out = new_bitmap;
940 dev->clipcount_out = win->clipcount;
941 if (dev->clipcount_out)
942 memcpy(dev->clips_out, dev->try_clips_out, clips_size);
943 dev->chromakey_out = win->chromakey;
944 dev->global_alpha_out = win->global_alpha;
945 return ret;
946 }
947
948 int vivid_vid_out_overlay(struct file *file, void *fh, unsigned i)
949 {
950 struct vivid_dev *dev = video_drvdata(file);
951
952 if (i && !dev->fmt_out->can_do_overlay) {
953 dprintk(dev, 1, "unsupported output format for output overlay\n");
954 return -EINVAL;
955 }
956
957 dev->overlay_out_enabled = i;
958 return 0;
959 }
960
961 int vivid_vid_out_g_fbuf(struct file *file, void *fh,
962 struct v4l2_framebuffer *a)
963 {
964 struct vivid_dev *dev = video_drvdata(file);
965
966 a->capability = V4L2_FBUF_CAP_EXTERNOVERLAY |
967 V4L2_FBUF_CAP_BITMAP_CLIPPING |
968 V4L2_FBUF_CAP_LIST_CLIPPING |
969 V4L2_FBUF_CAP_CHROMAKEY |
970 V4L2_FBUF_CAP_SRC_CHROMAKEY |
971 V4L2_FBUF_CAP_GLOBAL_ALPHA |
972 V4L2_FBUF_CAP_LOCAL_ALPHA |
973 V4L2_FBUF_CAP_LOCAL_INV_ALPHA;
974 a->flags = V4L2_FBUF_FLAG_OVERLAY | dev->fbuf_out_flags;
975 a->base = (void *)dev->video_pbase;
976 a->fmt.width = dev->display_width;
977 a->fmt.height = dev->display_height;
978 if (dev->fb_defined.green.length == 5)
979 a->fmt.pixelformat = V4L2_PIX_FMT_ARGB555;
980 else
981 a->fmt.pixelformat = V4L2_PIX_FMT_RGB565;
982 a->fmt.bytesperline = dev->display_byte_stride;
983 a->fmt.sizeimage = a->fmt.height * a->fmt.bytesperline;
984 a->fmt.field = V4L2_FIELD_NONE;
985 a->fmt.colorspace = V4L2_COLORSPACE_SRGB;
986 a->fmt.priv = 0;
987 return 0;
988 }
989
990 int vivid_vid_out_s_fbuf(struct file *file, void *fh,
991 const struct v4l2_framebuffer *a)
992 {
993 struct vivid_dev *dev = video_drvdata(file);
994 const unsigned chroma_flags = V4L2_FBUF_FLAG_CHROMAKEY |
995 V4L2_FBUF_FLAG_SRC_CHROMAKEY;
996 const unsigned alpha_flags = V4L2_FBUF_FLAG_GLOBAL_ALPHA |
997 V4L2_FBUF_FLAG_LOCAL_ALPHA |
998 V4L2_FBUF_FLAG_LOCAL_INV_ALPHA;
999
1000
1001 if ((a->flags & chroma_flags) == chroma_flags)
1002 return -EINVAL;
1003 switch (a->flags & alpha_flags) {
1004 case 0:
1005 case V4L2_FBUF_FLAG_GLOBAL_ALPHA:
1006 case V4L2_FBUF_FLAG_LOCAL_ALPHA:
1007 case V4L2_FBUF_FLAG_LOCAL_INV_ALPHA:
1008 break;
1009 default:
1010 return -EINVAL;
1011 }
1012 dev->fbuf_out_flags &= ~(chroma_flags | alpha_flags);
1013 dev->fbuf_out_flags = a->flags & (chroma_flags | alpha_flags);
1014 return 0;
1015 }
1016
1017 static const struct v4l2_audioout vivid_audio_outputs[] = {
1018 { 0, "Line-Out 1" },
1019 { 1, "Line-Out 2" },
1020 };
1021
1022 int vidioc_enum_output(struct file *file, void *priv,
1023 struct v4l2_output *out)
1024 {
1025 struct vivid_dev *dev = video_drvdata(file);
1026
1027 if (out->index >= dev->num_outputs)
1028 return -EINVAL;
1029
1030 out->type = V4L2_OUTPUT_TYPE_ANALOG;
1031 switch (dev->output_type[out->index]) {
1032 case SVID:
1033 snprintf(out->name, sizeof(out->name), "S-Video %u",
1034 dev->output_name_counter[out->index]);
1035 out->std = V4L2_STD_ALL;
1036 if (dev->has_audio_outputs)
1037 out->audioset = (1 << ARRAY_SIZE(vivid_audio_outputs)) - 1;
1038 out->capabilities = V4L2_OUT_CAP_STD;
1039 break;
1040 case HDMI:
1041 snprintf(out->name, sizeof(out->name), "HDMI %u",
1042 dev->output_name_counter[out->index]);
1043 out->capabilities = V4L2_OUT_CAP_DV_TIMINGS;
1044 break;
1045 }
1046 return 0;
1047 }
1048
1049 int vidioc_g_output(struct file *file, void *priv, unsigned *o)
1050 {
1051 struct vivid_dev *dev = video_drvdata(file);
1052
1053 *o = dev->output;
1054 return 0;
1055 }
1056
1057 int vidioc_s_output(struct file *file, void *priv, unsigned o)
1058 {
1059 struct vivid_dev *dev = video_drvdata(file);
1060
1061 if (o >= dev->num_outputs)
1062 return -EINVAL;
1063
1064 if (o == dev->output)
1065 return 0;
1066
1067 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1068 return -EBUSY;
1069
1070 dev->output = o;
1071 dev->tv_audio_output = 0;
1072 if (dev->output_type[o] == SVID)
1073 dev->vid_out_dev.tvnorms = V4L2_STD_ALL;
1074 else
1075 dev->vid_out_dev.tvnorms = 0;
1076
1077 dev->vbi_out_dev.tvnorms = dev->vid_out_dev.tvnorms;
1078 vivid_update_format_out(dev);
1079 return 0;
1080 }
1081
1082 int vidioc_enumaudout(struct file *file, void *fh, struct v4l2_audioout *vout)
1083 {
1084 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1085 return -EINVAL;
1086 *vout = vivid_audio_outputs[vout->index];
1087 return 0;
1088 }
1089
1090 int vidioc_g_audout(struct file *file, void *fh, struct v4l2_audioout *vout)
1091 {
1092 struct vivid_dev *dev = video_drvdata(file);
1093
1094 if (!vivid_is_svid_out(dev))
1095 return -EINVAL;
1096 *vout = vivid_audio_outputs[dev->tv_audio_output];
1097 return 0;
1098 }
1099
1100 int vidioc_s_audout(struct file *file, void *fh, const struct v4l2_audioout *vout)
1101 {
1102 struct vivid_dev *dev = video_drvdata(file);
1103
1104 if (!vivid_is_svid_out(dev))
1105 return -EINVAL;
1106 if (vout->index >= ARRAY_SIZE(vivid_audio_outputs))
1107 return -EINVAL;
1108 dev->tv_audio_output = vout->index;
1109 return 0;
1110 }
1111
1112 int vivid_vid_out_s_std(struct file *file, void *priv, v4l2_std_id id)
1113 {
1114 struct vivid_dev *dev = video_drvdata(file);
1115
1116 if (!vivid_is_svid_out(dev))
1117 return -ENODATA;
1118 if (dev->std_out == id)
1119 return 0;
1120 if (vb2_is_busy(&dev->vb_vid_out_q) || vb2_is_busy(&dev->vb_vbi_out_q))
1121 return -EBUSY;
1122 dev->std_out = id;
1123 vivid_update_format_out(dev);
1124 return 0;
1125 }
1126
1127 static bool valid_cvt_gtf_timings(struct v4l2_dv_timings *timings)
1128 {
1129 struct v4l2_bt_timings *bt = &timings->bt;
1130
1131 if ((bt->standards & (V4L2_DV_BT_STD_CVT | V4L2_DV_BT_STD_GTF)) &&
1132 v4l2_valid_dv_timings(timings, &vivid_dv_timings_cap, NULL, NULL))
1133 return true;
1134
1135 return false;
1136 }
1137
1138 int vivid_vid_out_s_dv_timings(struct file *file, void *_fh,
1139 struct v4l2_dv_timings *timings)
1140 {
1141 struct vivid_dev *dev = video_drvdata(file);
1142 if (!vivid_is_hdmi_out(dev))
1143 return -ENODATA;
1144 if (!v4l2_find_dv_timings_cap(timings, &vivid_dv_timings_cap,
1145 0, NULL, NULL) &&
1146 !valid_cvt_gtf_timings(timings))
1147 return -EINVAL;
1148 if (v4l2_match_dv_timings(timings, &dev->dv_timings_out, 0))
1149 return 0;
1150 if (vb2_is_busy(&dev->vb_vid_out_q))
1151 return -EBUSY;
1152 dev->dv_timings_out = *timings;
1153 vivid_update_format_out(dev);
1154 return 0;
1155 }
1156
1157 int vivid_vid_out_g_parm(struct file *file, void *priv,
1158 struct v4l2_streamparm *parm)
1159 {
1160 struct vivid_dev *dev = video_drvdata(file);
1161
1162 if (parm->type != (dev->multiplanar ?
1163 V4L2_BUF_TYPE_VIDEO_OUTPUT_MPLANE :
1164 V4L2_BUF_TYPE_VIDEO_OUTPUT))
1165 return -EINVAL;
1166
1167 parm->parm.output.capability = V4L2_CAP_TIMEPERFRAME;
1168 parm->parm.output.timeperframe = dev->timeperframe_vid_out;
1169 parm->parm.output.writebuffers = 1;
1170
1171 return 0;
1172 }
1173
1174 int vidioc_subscribe_event(struct v4l2_fh *fh,
1175 const struct v4l2_event_subscription *sub)
1176 {
1177 switch (sub->type) {
1178 case V4L2_EVENT_CTRL:
1179 return v4l2_ctrl_subscribe_event(fh, sub);
1180 case V4L2_EVENT_SOURCE_CHANGE:
1181 if (fh->vdev->vfl_dir == VFL_DIR_RX)
1182 return v4l2_src_change_event_subscribe(fh, sub);
1183 break;
1184 default:
1185 break;
1186 }
1187 return -EINVAL;
1188 }