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